Memory.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Memory</title>
  16. <meta name="description" content="Debugging with GDB: Memory">
  17. <meta name="keywords" content="Debugging with GDB: Memory">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Data.html#Data" rel="up" title="Data">
  26. <link href="Auto-Display.html#Auto-Display" rel="next" title="Auto Display">
  27. <link href="Output-Formats.html#Output-Formats" rel="previous" title="Output Formats">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Memory"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Auto-Display.html#Auto-Display" accesskey="n" rel="next">Auto Display</a>, Previous: <a href="Output-Formats.html#Output-Formats" accesskey="p" rel="previous">Output Formats</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Examining-Memory"></a>
  65. <h3 class="section">10.6 Examining Memory</h3>
  66. <p>You can use the command <code>x</code> (for &ldquo;examine&rdquo;) to examine memory in
  67. any of several formats, independently of your program&rsquo;s data types.
  68. </p>
  69. <a name="index-examining-memory"></a>
  70. <dl compact="compact">
  71. <dd><a name="index-x-_0028examine-memory_0029"></a>
  72. </dd>
  73. <dt><code>x/<var>nfu</var> <var>addr</var></code></dt>
  74. <dt><code>x <var>addr</var></code></dt>
  75. <dt><code>x</code></dt>
  76. <dd><p>Use the <code>x</code> command to examine memory.
  77. </p></dd>
  78. </dl>
  79. <p><var>n</var>, <var>f</var>, and <var>u</var> are all optional parameters that specify how
  80. much memory to display and how to format it; <var>addr</var> is an
  81. expression giving the address where you want to start displaying memory.
  82. If you use defaults for <var>nfu</var>, you need not type the slash &lsquo;<samp>/</samp>&rsquo;.
  83. Several commands set convenient defaults for <var>addr</var>.
  84. </p>
  85. <dl compact="compact">
  86. <dt><span class="roman"><var>n</var>, the repeat count</span></dt>
  87. <dd><p>The repeat count is a decimal integer; the default is 1. It specifies
  88. how much memory (counting by units <var>u</var>) to display. If a negative
  89. number is specified, memory is examined backward from <var>addr</var>.
  90. </p>
  91. </dd>
  92. <dt><span class="roman"><var>f</var>, the display format</span></dt>
  93. <dd><p>The display format is one of the formats used by <code>print</code>
  94. (&lsquo;<samp>x</samp>&rsquo;, &lsquo;<samp>d</samp>&rsquo;, &lsquo;<samp>u</samp>&rsquo;, &lsquo;<samp>o</samp>&rsquo;, &lsquo;<samp>t</samp>&rsquo;, &lsquo;<samp>a</samp>&rsquo;, &lsquo;<samp>c</samp>&rsquo;,
  95. &lsquo;<samp>f</samp>&rsquo;, &lsquo;<samp>s</samp>&rsquo;), and in addition &lsquo;<samp>i</samp>&rsquo; (for machine instructions).
  96. The default is &lsquo;<samp>x</samp>&rsquo; (hexadecimal) initially. The default changes
  97. each time you use either <code>x</code> or <code>print</code>.
  98. </p>
  99. </dd>
  100. <dt><span class="roman"><var>u</var>, the unit size</span></dt>
  101. <dd><p>The unit size is any of
  102. </p>
  103. <dl compact="compact">
  104. <dt><code>b</code></dt>
  105. <dd><p>Bytes.
  106. </p></dd>
  107. <dt><code>h</code></dt>
  108. <dd><p>Halfwords (two bytes).
  109. </p></dd>
  110. <dt><code>w</code></dt>
  111. <dd><p>Words (four bytes). This is the initial default.
  112. </p></dd>
  113. <dt><code>g</code></dt>
  114. <dd><p>Giant words (eight bytes).
  115. </p></dd>
  116. </dl>
  117. <p>Each time you specify a unit size with <code>x</code>, that size becomes the
  118. default unit the next time you use <code>x</code>. For the &lsquo;<samp>i</samp>&rsquo; format,
  119. the unit size is ignored and is normally not written. For the &lsquo;<samp>s</samp>&rsquo; format,
  120. the unit size defaults to &lsquo;<samp>b</samp>&rsquo;, unless it is explicitly given.
  121. Use <kbd>x /hs</kbd> to display 16-bit char strings and <kbd>x /ws</kbd> to display
  122. 32-bit strings. The next use of <kbd>x /s</kbd> will again display 8-bit strings.
  123. Note that the results depend on the programming language of the
  124. current compilation unit. If the language is C, the &lsquo;<samp>s</samp>&rsquo;
  125. modifier will use the UTF-16 encoding while &lsquo;<samp>w</samp>&rsquo; will use
  126. UTF-32. The encoding is set by the programming language and cannot
  127. be altered.
  128. </p>
  129. </dd>
  130. <dt><span class="roman"><var>addr</var>, starting display address</span></dt>
  131. <dd><p><var>addr</var> is the address where you want <small>GDB</small> to begin displaying
  132. memory. The expression need not have a pointer value (though it may);
  133. it is always interpreted as an integer address of a byte of memory.
  134. See <a href="Expressions.html#Expressions">Expressions</a>, for more information on expressions. The default for
  135. <var>addr</var> is usually just after the last address examined&mdash;but several
  136. other commands also set the default address: <code>info breakpoints</code> (to
  137. the address of the last breakpoint listed), <code>info line</code> (to the
  138. starting address of a line), and <code>print</code> (if you use it to display
  139. a value from memory).
  140. </p></dd>
  141. </dl>
  142. <p>For example, &lsquo;<samp>x/3uh 0x54320</samp>&rsquo; is a request to display three halfwords
  143. (<code>h</code>) of memory, formatted as unsigned decimal integers (&lsquo;<samp>u</samp>&rsquo;),
  144. starting at address <code>0x54320</code>. &lsquo;<samp>x/4xw $sp</samp>&rsquo; prints the four
  145. words (&lsquo;<samp>w</samp>&rsquo;) of memory above the stack pointer (here, &lsquo;<samp>$sp</samp>&rsquo;;
  146. see <a href="Registers.html#Registers">Registers</a>) in hexadecimal (&lsquo;<samp>x</samp>&rsquo;).
  147. </p>
  148. <p>You can also specify a negative repeat count to examine memory backward
  149. from the given address. For example, &lsquo;<samp>x/-3uh 0x54320</samp>&rsquo; prints three
  150. halfwords (<code>h</code>) at <code>0x54314</code>, <code>0x54328</code>, and <code>0x5431c</code>.
  151. </p>
  152. <p>Since the letters indicating unit sizes are all distinct from the
  153. letters specifying output formats, you do not have to remember whether
  154. unit size or format comes first; either order works. The output
  155. specifications &lsquo;<samp>4xw</samp>&rsquo; and &lsquo;<samp>4wx</samp>&rsquo; mean exactly the same thing.
  156. (However, the count <var>n</var> must come first; &lsquo;<samp>wx4</samp>&rsquo; does not work.)
  157. </p>
  158. <p>Even though the unit size <var>u</var> is ignored for the formats &lsquo;<samp>s</samp>&rsquo;
  159. and &lsquo;<samp>i</samp>&rsquo;, you might still want to use a count <var>n</var>; for example,
  160. &lsquo;<samp>3i</samp>&rsquo; specifies that you want to see three machine instructions,
  161. including any operands. For convenience, especially when used with
  162. the <code>display</code> command, the &lsquo;<samp>i</samp>&rsquo; format also prints branch delay
  163. slot instructions, if any, beyond the count specified, which immediately
  164. follow the last instruction that is within the count. The command
  165. <code>disassemble</code> gives an alternative way of inspecting machine
  166. instructions; see <a href="Machine-Code.html#Machine-Code">Source and Machine Code</a>.
  167. </p>
  168. <p>If a negative repeat count is specified for the formats &lsquo;<samp>s</samp>&rsquo; or &lsquo;<samp>i</samp>&rsquo;,
  169. the command displays null-terminated strings or instructions before the given
  170. address as many as the absolute value of the given number. For the &lsquo;<samp>i</samp>&rsquo;
  171. format, we use line number information in the debug info to accurately locate
  172. instruction boundaries while disassembling backward. If line info is not
  173. available, the command stops examining memory with an error message.
  174. </p>
  175. <p>All the defaults for the arguments to <code>x</code> are designed to make it
  176. easy to continue scanning memory with minimal specifications each time
  177. you use <code>x</code>. For example, after you have inspected three machine
  178. instructions with &lsquo;<samp>x/3i <var>addr</var></samp>&rsquo;, you can inspect the next seven
  179. with just &lsquo;<samp>x/7</samp>&rsquo;. If you use <tt class="key">RET</tt> to repeat the <code>x</code> command,
  180. the repeat count <var>n</var> is used again; the other arguments default as
  181. for successive uses of <code>x</code>.
  182. </p>
  183. <p>When examining machine instructions, the instruction at current program
  184. counter is shown with a <code>=&gt;</code> marker. For example:
  185. </p>
  186. <div class="smallexample">
  187. <pre class="smallexample">(gdb) x/5i $pc-6
  188. 0x804837f &lt;main+11&gt;: mov %esp,%ebp
  189. 0x8048381 &lt;main+13&gt;: push %ecx
  190. 0x8048382 &lt;main+14&gt;: sub $0x4,%esp
  191. =&gt; 0x8048385 &lt;main+17&gt;: movl $0x8048460,(%esp)
  192. 0x804838c &lt;main+24&gt;: call 0x80482d4 &lt;puts@plt&gt;
  193. </pre></div>
  194. <a name="index-_0024_005f_002c-_0024_005f_005f_002c-and-value-history"></a>
  195. <p>The addresses and contents printed by the <code>x</code> command are not saved
  196. in the value history because there is often too much of them and they
  197. would get in the way. Instead, <small>GDB</small> makes these values available for
  198. subsequent use in expressions as values of the convenience variables
  199. <code>$_</code> and <code>$__</code>. After an <code>x</code> command, the last address
  200. examined is available for use in expressions in the convenience variable
  201. <code>$_</code>. The contents of that address, as examined, are available in
  202. the convenience variable <code>$__</code>.
  203. </p>
  204. <p>If the <code>x</code> command has a repeat count, the address and contents saved
  205. are from the last memory unit printed; this is not the same as the last
  206. address printed if several units were printed on the last line of output.
  207. </p>
  208. <a name="addressable-memory-unit"></a><a name="index-addressable-memory-unit"></a>
  209. <p>Most targets have an addressable memory unit size of 8 bits. This means
  210. that to each memory address are associated 8 bits of data. Some
  211. targets, however, have other addressable memory unit sizes.
  212. Within <small>GDB</small> and this document, the term
  213. <em>addressable memory unit</em> (or <em>memory unit</em> for short) is used
  214. when explicitly referring to a chunk of data of that size. The word
  215. <em>byte</em> is used to refer to a chunk of data of 8 bits, regardless of
  216. the addressable memory unit size of the target. For most systems,
  217. addressable memory unit is a synonym of byte.
  218. </p>
  219. <a name="index-remote-memory-comparison"></a>
  220. <a name="index-target-memory-comparison"></a>
  221. <a name="index-verify-remote-memory-image"></a>
  222. <a name="index-verify-target-memory-image"></a>
  223. <p>When you are debugging a program running on a remote target machine
  224. (see <a href="Remote-Debugging.html#Remote-Debugging">Remote Debugging</a>), you may wish to verify the program&rsquo;s image
  225. in the remote machine&rsquo;s memory against the executable file you
  226. downloaded to the target. Or, on any target, you may want to check
  227. whether the program has corrupted its own read-only sections. The
  228. <code>compare-sections</code> command is provided for such situations.
  229. </p>
  230. <dl compact="compact">
  231. <dd><a name="index-compare_002dsections"></a>
  232. </dd>
  233. <dt><code>compare-sections <span class="roman">[</span><var>section-name</var><span class="roman">|</span><code>-r</code><span class="roman">]</span></code></dt>
  234. <dd><p>Compare the data of a loadable section <var>section-name</var> in the
  235. executable file of the program being debugged with the same section in
  236. the target machine&rsquo;s memory, and report any mismatches. With no
  237. arguments, compares all loadable sections. With an argument of
  238. <code>-r</code>, compares all loadable read-only sections.
  239. </p>
  240. <p>Note: for remote targets, this command can be accelerated if the
  241. target supports computing the CRC checksum of a block of memory
  242. (see <a href="General-Query-Packets.html#qCRC-packet">qCRC packet</a>).
  243. </p></dd>
  244. </dl>
  245. <hr>
  246. <div class="header">
  247. <p>
  248. Next: <a href="Auto-Display.html#Auto-Display" accesskey="n" rel="next">Auto Display</a>, Previous: <a href="Output-Formats.html#Output-Formats" accesskey="p" rel="previous">Output Formats</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  249. </div>
  250. </body>
  251. </html>