blob: 39154c6d4c73384e0f8d0a893f71369450167446 [file] [log] [blame]
<html lang="en">
<head>
<title>Memory - Debugging with GDB</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Debugging with GDB">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Data.html#Data" title="Data">
<link rel="prev" href="Output-Formats.html#Output-Formats" title="Output Formats">
<link rel="next" href="Auto-Display.html#Auto-Display" title="Auto Display">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software'' and ``Free Software Needs
Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,''
and with the Back-Cover Texts as in (a) below.
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Memory"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Auto-Display.html#Auto-Display">Auto Display</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Output-Formats.html#Output-Formats">Output Formats</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Data.html#Data">Data</a>
<hr>
</div>
<h3 class="section">10.6 Examining Memory</h3>
<p>You can use the command <code>x</code> (for &ldquo;examine&rdquo;) to examine memory in
any of several formats, independently of your program's data types.
<p><a name="index-examining-memory-524"></a>
<a name="index-x-_0040r_007b_0028examine-memory_0029_007d-525"></a>
<dl><dt><code>x/</code><var>nfu</var> <var>addr</var><dt><code>x </code><var>addr</var><dt><code>x</code><dd>Use the <code>x</code> command to examine memory.
</dl>
<p><var>n</var>, <var>f</var>, and <var>u</var> are all optional parameters that specify how
much memory to display and how to format it; <var>addr</var> is an
expression giving the address where you want to start displaying memory.
If you use defaults for <var>nfu</var>, you need not type the slash &lsquo;<samp><span class="samp">/</span></samp>&rsquo;.
Several commands set convenient defaults for <var>addr</var>.
<dl>
<dt><var>n</var><span class="roman">, the repeat count</span><dd>The repeat count is a decimal integer; the default is 1. It specifies
how much memory (counting by units <var>u</var>) to display.
<!-- This really is **decimal**; unaffected by 'set radix' as of GDB -->
<!-- 4.1.2. -->
<br><dt><var>f</var><span class="roman">, the display format</span><dd>The display format is one of the formats used by <code>print</code>
(&lsquo;<samp><span class="samp">x</span></samp>&rsquo;, &lsquo;<samp><span class="samp">d</span></samp>&rsquo;, &lsquo;<samp><span class="samp">u</span></samp>&rsquo;, &lsquo;<samp><span class="samp">o</span></samp>&rsquo;, &lsquo;<samp><span class="samp">t</span></samp>&rsquo;, &lsquo;<samp><span class="samp">a</span></samp>&rsquo;, &lsquo;<samp><span class="samp">c</span></samp>&rsquo;,
&lsquo;<samp><span class="samp">f</span></samp>&rsquo;, &lsquo;<samp><span class="samp">s</span></samp>&rsquo;), and in addition &lsquo;<samp><span class="samp">i</span></samp>&rsquo; (for machine instructions).
The default is &lsquo;<samp><span class="samp">x</span></samp>&rsquo; (hexadecimal) initially. The default changes
each time you use either <code>x</code> or <code>print</code>.
<br><dt><var>u</var><span class="roman">, the unit size</span><dd>The unit size is any of
<dl>
<dt><code>b</code><dd>Bytes.
<br><dt><code>h</code><dd>Halfwords (two bytes).
<br><dt><code>w</code><dd>Words (four bytes). This is the initial default.
<br><dt><code>g</code><dd>Giant words (eight bytes).
</dl>
<p>Each time you specify a unit size with <code>x</code>, that size becomes the
default unit the next time you use <code>x</code>. For the &lsquo;<samp><span class="samp">i</span></samp>&rsquo; format,
the unit size is ignored and is normally not written. For the &lsquo;<samp><span class="samp">s</span></samp>&rsquo; format,
the unit size defaults to &lsquo;<samp><span class="samp">b</span></samp>&rsquo;, unless it is explicitly given.
Use <kbd>x /hs</kbd> to display 16-bit char strings and <kbd>x /ws</kbd> to display
32-bit strings. The next use of <kbd>x /s</kbd> will again display 8-bit strings.
Note that the results depend on the programming language of the
current compilation unit. If the language is C, the &lsquo;<samp><span class="samp">s</span></samp>&rsquo;
modifier will use the UTF-16 encoding while &lsquo;<samp><span class="samp">w</span></samp>&rsquo; will use
UTF-32. The encoding is set by the programming language and cannot
be altered.
<br><dt><var>addr</var><span class="roman">, starting display address</span><dd><var>addr</var> is the address where you want <span class="sc">gdb</span> to begin displaying
memory. The expression need not have a pointer value (though it may);
it is always interpreted as an integer address of a byte of memory.
See <a href="Expressions.html#Expressions">Expressions</a>, for more information on expressions. The default for
<var>addr</var> is usually just after the last address examined&mdash;but several
other commands also set the default address: <code>info breakpoints</code> (to
the address of the last breakpoint listed), <code>info line</code> (to the
starting address of a line), and <code>print</code> (if you use it to display
a value from memory).
</dl>
<p>For example, &lsquo;<samp><span class="samp">x/3uh 0x54320</span></samp>&rsquo; is a request to display three halfwords
(<code>h</code>) of memory, formatted as unsigned decimal integers (&lsquo;<samp><span class="samp">u</span></samp>&rsquo;),
starting at address <code>0x54320</code>. &lsquo;<samp><span class="samp">x/4xw $sp</span></samp>&rsquo; prints the four
words (&lsquo;<samp><span class="samp">w</span></samp>&rsquo;) of memory above the stack pointer (here, &lsquo;<samp><span class="samp">$sp</span></samp>&rsquo;;
see <a href="Registers.html#Registers">Registers</a>) in hexadecimal (&lsquo;<samp><span class="samp">x</span></samp>&rsquo;).
<p>Since the letters indicating unit sizes are all distinct from the
letters specifying output formats, you do not have to remember whether
unit size or format comes first; either order works. The output
specifications &lsquo;<samp><span class="samp">4xw</span></samp>&rsquo; and &lsquo;<samp><span class="samp">4wx</span></samp>&rsquo; mean exactly the same thing.
(However, the count <var>n</var> must come first; &lsquo;<samp><span class="samp">wx4</span></samp>&rsquo; does not work.)
<p>Even though the unit size <var>u</var> is ignored for the formats &lsquo;<samp><span class="samp">s</span></samp>&rsquo;
and &lsquo;<samp><span class="samp">i</span></samp>&rsquo;, you might still want to use a count <var>n</var>; for example,
&lsquo;<samp><span class="samp">3i</span></samp>&rsquo; specifies that you want to see three machine instructions,
including any operands. For convenience, especially when used with
the <code>display</code> command, the &lsquo;<samp><span class="samp">i</span></samp>&rsquo; format also prints branch delay
slot instructions, if any, beyond the count specified, which immediately
follow the last instruction that is within the count. The command
<code>disassemble</code> gives an alternative way of inspecting machine
instructions; see <a href="Machine-Code.html#Machine-Code">Source and Machine Code</a>.
<p>All the defaults for the arguments to <code>x</code> are designed to make it
easy to continue scanning memory with minimal specifications each time
you use <code>x</code>. For example, after you have inspected three machine
instructions with &lsquo;<samp><span class="samp">x/3i </span><var>addr</var></samp>&rsquo;, you can inspect the next seven
with just &lsquo;<samp><span class="samp">x/7</span></samp>&rsquo;. If you use &lt;RET&gt; to repeat the <code>x</code> command,
the repeat count <var>n</var> is used again; the other arguments default as
for successive uses of <code>x</code>.
<p>When examining machine instructions, the instruction at current program
counter is shown with a <code>=&gt;</code> marker. For example:
<pre class="smallexample"> (gdb) x/5i $pc-6
0x804837f &lt;main+11&gt;: mov %esp,%ebp
0x8048381 &lt;main+13&gt;: push %ecx
0x8048382 &lt;main+14&gt;: sub $0x4,%esp
=&gt; 0x8048385 &lt;main+17&gt;: movl $0x8048460,(%esp)
0x804838c &lt;main+24&gt;: call 0x80482d4 &lt;puts@plt&gt;
</pre>
<p><a name="index-g_t_0040code_007b_0024_005f_007d_002c-_0040code_007b_0024_005f_005f_007d_002c-and-value-history-526"></a>The addresses and contents printed by the <code>x</code> command are not saved
in the value history because there is often too much of them and they
would get in the way. Instead, <span class="sc">gdb</span> makes these values available for
subsequent use in expressions as values of the convenience variables
<code>$_</code> and <code>$__</code>. After an <code>x</code> command, the last address
examined is available for use in expressions in the convenience variable
<code>$_</code>. The contents of that address, as examined, are available in
the convenience variable <code>$__</code>.
<p>If the <code>x</code> command has a repeat count, the address and contents saved
are from the last memory unit printed; this is not the same as the last
address printed if several units were printed on the last line of output.
<p><a name="index-remote-memory-comparison-527"></a><a name="index-verify-remote-memory-image-528"></a>When you are debugging a program running on a remote target machine
(see <a href="Remote-Debugging.html#Remote-Debugging">Remote Debugging</a>), you may wish to verify the program's image in the
remote machine's memory against the executable file you downloaded to
the target. The <code>compare-sections</code> command is provided for such
situations.
<a name="index-compare_002dsections-529"></a>
<dl><dt><code>compare-sections </code><span class="roman">[</span><var>section-name</var><span class="roman">]</span><dd>Compare the data of a loadable section <var>section-name</var> in the
executable file of the program being debugged with the same section in
the remote machine's memory, and report any mismatches. With no
arguments, compares all loadable sections. This command's
availability depends on the target's support for the <code>"qCRC"</code>
remote request.
</dl>
</body></html>