You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
14KB

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