| <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-2019 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> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Memory"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Auto-Display.html#Auto-Display">Auto Display</a>, |
| Previous: <a rel="previous" accesskey="p" href="Output-Formats.html#Output-Formats">Output Formats</a>, |
| Up: <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 “examine”) to examine memory in |
| any of several formats, independently of your program's data types. |
| |
| <p><a name="index-examining-memory-656"></a> |
| |
| <a name="index-x-_0040r_007b_0028examine-memory_0029_007d-657"></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 ‘<samp><span class="samp">/</span></samp>’. |
| 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. If a negative |
| number is specified, memory is examined backward from <var>addr</var>. |
| <!-- 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> |
| (‘<samp><span class="samp">x</span></samp>’, ‘<samp><span class="samp">d</span></samp>’, ‘<samp><span class="samp">u</span></samp>’, ‘<samp><span class="samp">o</span></samp>’, ‘<samp><span class="samp">t</span></samp>’, ‘<samp><span class="samp">a</span></samp>’, ‘<samp><span class="samp">c</span></samp>’, |
| ‘<samp><span class="samp">f</span></samp>’, ‘<samp><span class="samp">s</span></samp>’), and in addition ‘<samp><span class="samp">i</span></samp>’ (for machine instructions). |
| The default is ‘<samp><span class="samp">x</span></samp>’ (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 ‘<samp><span class="samp">i</span></samp>’ format, |
| the unit size is ignored and is normally not written. For the ‘<samp><span class="samp">s</span></samp>’ format, |
| the unit size defaults to ‘<samp><span class="samp">b</span></samp>’, 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 ‘<samp><span class="samp">s</span></samp>’ |
| modifier will use the UTF-16 encoding while ‘<samp><span class="samp">w</span></samp>’ 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—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, ‘<samp><span class="samp">x/3uh 0x54320</span></samp>’ is a request to display three halfwords |
| (<code>h</code>) of memory, formatted as unsigned decimal integers (‘<samp><span class="samp">u</span></samp>’), |
| starting at address <code>0x54320</code>. ‘<samp><span class="samp">x/4xw $sp</span></samp>’ prints the four |
| words (‘<samp><span class="samp">w</span></samp>’) of memory above the stack pointer (here, ‘<samp><span class="samp">$sp</span></samp>’; |
| see <a href="Registers.html#Registers">Registers</a>) in hexadecimal (‘<samp><span class="samp">x</span></samp>’). |
| |
| <p>You can also specify a negative repeat count to examine memory backward |
| from the given address. For example, ‘<samp><span class="samp">x/-3uh 0x54320</span></samp>’ prints three |
| halfwords (<code>h</code>) at <code>0x54314</code>, <code>0x54328</code>, and <code>0x5431c</code>. |
| |
| <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 ‘<samp><span class="samp">4xw</span></samp>’ and ‘<samp><span class="samp">4wx</span></samp>’ mean exactly the same thing. |
| (However, the count <var>n</var> must come first; ‘<samp><span class="samp">wx4</span></samp>’ does not work.) |
| |
| <p>Even though the unit size <var>u</var> is ignored for the formats ‘<samp><span class="samp">s</span></samp>’ |
| and ‘<samp><span class="samp">i</span></samp>’, you might still want to use a count <var>n</var>; for example, |
| ‘<samp><span class="samp">3i</span></samp>’ specifies that you want to see three machine instructions, |
| including any operands. For convenience, especially when used with |
| the <code>display</code> command, the ‘<samp><span class="samp">i</span></samp>’ 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>If a negative repeat count is specified for the formats ‘<samp><span class="samp">s</span></samp>’ or ‘<samp><span class="samp">i</span></samp>’, |
| the command displays null-terminated strings or instructions before the given |
| address as many as the absolute value of the given number. For the ‘<samp><span class="samp">i</span></samp>’ |
| format, we use line number information in the debug info to accurately locate |
| instruction boundaries while disassembling backward. If line info is not |
| available, the command stops examining memory with an error message. |
| |
| <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 ‘<samp><span class="samp">x/3i </span><var>addr</var></samp>’, you can inspect the next seven |
| with just ‘<samp><span class="samp">x/7</span></samp>’. If you use <RET> 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>=></code> marker. For example: |
| |
| <pre class="smallexample"> (gdb) x/5i $pc-6 |
| 0x804837f <main+11>: mov %esp,%ebp |
| 0x8048381 <main+13>: push %ecx |
| 0x8048382 <main+14>: sub $0x4,%esp |
| => 0x8048385 <main+17>: movl $0x8048460,(%esp) |
| 0x804838c <main+24>: call 0x80482d4 <puts@plt> |
| </pre> |
| <p><a name="index-g_t_0040code_007b_0024_005f_007d_002c-_0040code_007b_0024_005f_005f_007d_002c-and-value-history-658"></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="addressable-memory-unit"></a><a name="index-addressable-memory-unit-659"></a>Most targets have an addressable memory unit size of 8 bits. This means |
| that to each memory address are associated 8 bits of data. Some |
| targets, however, have other addressable memory unit sizes. |
| Within <span class="sc">gdb</span> and this document, the term |
| <dfn>addressable memory unit</dfn> (or <dfn>memory unit</dfn> for short) is used |
| when explicitly referring to a chunk of data of that size. The word |
| <dfn>byte</dfn> is used to refer to a chunk of data of 8 bits, regardless of |
| the addressable memory unit size of the target. For most systems, |
| addressable memory unit is a synonym of byte. |
| |
| <p><a name="index-remote-memory-comparison-660"></a><a name="index-target-memory-comparison-661"></a><a name="index-verify-remote-memory-image-662"></a><a name="index-verify-target-memory-image-663"></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. Or, on any target, you may want to check |
| whether the program has corrupted its own read-only sections. The |
| <code>compare-sections</code> command is provided for such situations. |
| |
| |
| <a name="index-compare_002dsections-664"></a> |
| <dl><dt><code>compare-sections </code><span class="roman">[</span><var>section-name</var><span class="roman">|</span><code>-r</code><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 target machine's memory, and report any mismatches. With no |
| arguments, compares all loadable sections. With an argument of |
| <code>-r</code>, compares all loadable read-only sections. |
| |
| <p>Note: for remote targets, this command can be accelerated if the |
| target supports computing the CRC checksum of a block of memory |
| (see <a href="qCRC-packet.html#qCRC-packet">qCRC packet</a>). |
| </dl> |
| |
| </body></html> |
| |