Machine-Code.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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: Machine Code</title>
  16. <meta name="description" content="Debugging with GDB: Machine Code">
  17. <meta name="keywords" content="Debugging with GDB: Machine Code">
  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="Source.html#Source" rel="up" title="Source">
  26. <link href="Data.html#Data" rel="next" title="Data">
  27. <link href="Source-Path.html#Source-Path" rel="previous" title="Source Path">
  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="Machine-Code"></a>
  59. <div class="header">
  60. <p>
  61. Previous: <a href="Source-Path.html#Source-Path" accesskey="p" rel="previous">Source Path</a>, Up: <a href="Source.html#Source" accesskey="u" rel="up">Source</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="Source-and-Machine-Code"></a>
  65. <h3 class="section">9.6 Source and Machine Code</h3>
  66. <a name="index-source-line-and-its-code-address"></a>
  67. <p>You can use the command <code>info line</code> to map source lines to program
  68. addresses (and vice versa), and the command <code>disassemble</code> to display
  69. a range of addresses as machine instructions. You can use the command
  70. <code>set disassemble-next-line</code> to set whether to disassemble next
  71. source line when execution stops. When run under <small>GNU</small> Emacs
  72. mode, the <code>info line</code> command causes the arrow to point to the
  73. line specified. Also, <code>info line</code> prints addresses in symbolic form as
  74. well as hex.
  75. </p>
  76. <dl compact="compact">
  77. <dd><a name="index-info-line"></a>
  78. </dd>
  79. <dt><code>info line</code></dt>
  80. <dt><code>info line <var>location</var></code></dt>
  81. <dd><p>Print the starting and ending addresses of the compiled code for
  82. source line <var>location</var>. You can specify source lines in any of
  83. the ways documented in <a href="Specify-Location.html#Specify-Location">Specify Location</a>. With no <var>location</var>
  84. information about the current source line is printed.
  85. </p></dd>
  86. </dl>
  87. <p>For example, we can use <code>info line</code> to discover the location of
  88. the object code for the first line of function
  89. <code>m4_changequote</code>:
  90. </p>
  91. <div class="smallexample">
  92. <pre class="smallexample">(gdb) info line m4_changequote
  93. Line 895 of &quot;builtin.c&quot; starts at pc 0x634c &lt;m4_changequote&gt; and \
  94. ends at 0x6350 &lt;m4_changequote+4&gt;.
  95. </pre></div>
  96. <a name="index-code-address-and-its-source-line"></a>
  97. <p>We can also inquire (using <code>*<var>addr</var></code> as the form for
  98. <var>location</var>) what source line covers a particular address:
  99. </p><div class="smallexample">
  100. <pre class="smallexample">(gdb) info line *0x63ff
  101. Line 926 of &quot;builtin.c&quot; starts at pc 0x63e4 &lt;m4_changequote+152&gt; and \
  102. ends at 0x6404 &lt;m4_changequote+184&gt;.
  103. </pre></div>
  104. <a name="index-_0024_005f-and-info-line"></a>
  105. <a name="index-x-command_002c-default-address"></a>
  106. <a name="index-x_0028examine_0029_002c-and-info-line"></a>
  107. <p>After <code>info line</code>, the default address for the <code>x</code> command
  108. is changed to the starting address of the line, so that &lsquo;<samp>x/i</samp>&rsquo; is
  109. sufficient to begin examining the machine code (see <a href="Memory.html#Memory">Examining Memory</a>). Also, this address is saved as the value of the
  110. convenience variable <code>$_</code> (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience
  111. Variables</a>).
  112. </p>
  113. <a name="index-info-line_002c-repeated-calls"></a>
  114. <p>After <code>info line</code>, using <code>info line</code> again without
  115. specifying a location will display information about the next source
  116. line.
  117. </p>
  118. <dl compact="compact">
  119. <dd><a name="index-disassemble"></a>
  120. <a name="index-assembly-instructions"></a>
  121. <a name="index-instructions_002c-assembly"></a>
  122. <a name="index-machine-instructions"></a>
  123. <a name="index-listing-machine-instructions"></a>
  124. </dd>
  125. <dt><code>disassemble</code></dt>
  126. <dt><code>disassemble /m</code></dt>
  127. <dt><code>disassemble /s</code></dt>
  128. <dt><code>disassemble /r</code></dt>
  129. <dd><p>This specialized command dumps a range of memory as machine
  130. instructions. It can also print mixed source+disassembly by specifying
  131. the <code>/m</code> or <code>/s</code> modifier and print the raw instructions in hex
  132. as well as in symbolic form by specifying the <code>/r</code> modifier.
  133. The default memory range is the function surrounding the
  134. program counter of the selected frame. A single argument to this
  135. command is a program counter value; <small>GDB</small> dumps the function
  136. surrounding this value. When two arguments are given, they should
  137. be separated by a comma, possibly surrounded by whitespace. The
  138. arguments specify a range of addresses to dump, in one of two forms:
  139. </p>
  140. <dl compact="compact">
  141. <dt><code><var>start</var>,<var>end</var></code></dt>
  142. <dd><p>the addresses from <var>start</var> (inclusive) to <var>end</var> (exclusive)
  143. </p></dd>
  144. <dt><code><var>start</var>,+<var>length</var></code></dt>
  145. <dd><p>the addresses from <var>start</var> (inclusive) to
  146. <code><var>start</var>+<var>length</var></code> (exclusive).
  147. </p></dd>
  148. </dl>
  149. <p>When 2 arguments are specified, the name of the function is also
  150. printed (since there could be several functions in the given range).
  151. </p>
  152. <p>The argument(s) can be any expression yielding a numeric value, such as
  153. &lsquo;<samp>0x32c4</samp>&rsquo;, &lsquo;<samp>&amp;main+10</samp>&rsquo; or &lsquo;<samp>$pc - 8</samp>&rsquo;.
  154. </p>
  155. <p>If the range of memory being disassembled contains current program counter,
  156. the instruction at that location is shown with a <code>=&gt;</code> marker.
  157. </p></dd>
  158. </dl>
  159. <p>The following example shows the disassembly of a range of addresses of
  160. HP PA-RISC 2.0 code:
  161. </p>
  162. <div class="smallexample">
  163. <pre class="smallexample">(gdb) disas 0x32c4, 0x32e4
  164. Dump of assembler code from 0x32c4 to 0x32e4:
  165. 0x32c4 &lt;main+204&gt;: addil 0,dp
  166. 0x32c8 &lt;main+208&gt;: ldw 0x22c(sr0,r1),r26
  167. 0x32cc &lt;main+212&gt;: ldil 0x3000,r31
  168. 0x32d0 &lt;main+216&gt;: ble 0x3f8(sr4,r31)
  169. 0x32d4 &lt;main+220&gt;: ldo 0(r31),rp
  170. 0x32d8 &lt;main+224&gt;: addil -0x800,dp
  171. 0x32dc &lt;main+228&gt;: ldo 0x588(r1),r26
  172. 0x32e0 &lt;main+232&gt;: ldil 0x3000,r31
  173. End of assembler dump.
  174. </pre></div>
  175. <p>Here is an example showing mixed source+assembly for Intel x86
  176. with <code>/m</code> or <code>/s</code>, when the program is stopped just after
  177. function prologue in a non-optimized function with no inline code.
  178. </p>
  179. <div class="smallexample">
  180. <pre class="smallexample">(gdb) disas /m main
  181. Dump of assembler code for function main:
  182. 5 {
  183. 0x08048330 &lt;+0&gt;: push %ebp
  184. 0x08048331 &lt;+1&gt;: mov %esp,%ebp
  185. 0x08048333 &lt;+3&gt;: sub $0x8,%esp
  186. 0x08048336 &lt;+6&gt;: and $0xfffffff0,%esp
  187. 0x08048339 &lt;+9&gt;: sub $0x10,%esp
  188. 6 printf (&quot;Hello.\n&quot;);
  189. =&gt; 0x0804833c &lt;+12&gt;: movl $0x8048440,(%esp)
  190. 0x08048343 &lt;+19&gt;: call 0x8048284 &lt;puts@plt&gt;
  191. 7 return 0;
  192. 8 }
  193. 0x08048348 &lt;+24&gt;: mov $0x0,%eax
  194. 0x0804834d &lt;+29&gt;: leave
  195. 0x0804834e &lt;+30&gt;: ret
  196. End of assembler dump.
  197. </pre></div>
  198. <p>The <code>/m</code> option is deprecated as its output is not useful when
  199. there is either inlined code or re-ordered code.
  200. The <code>/s</code> option is the preferred choice.
  201. Here is an example for AMD x86-64 showing the difference between
  202. <code>/m</code> output and <code>/s</code> output.
  203. This example has one inline function defined in a header file,
  204. and the code is compiled with &lsquo;<samp>-O2</samp>&rsquo; optimization.
  205. Note how the <code>/m</code> output is missing the disassembly of
  206. several instructions that are present in the <code>/s</code> output.
  207. </p>
  208. <p><samp>foo.h</samp>:
  209. </p>
  210. <div class="smallexample">
  211. <pre class="smallexample">int
  212. foo (int a)
  213. {
  214. if (a &lt; 0)
  215. return a * 2;
  216. if (a == 0)
  217. return 1;
  218. return a + 10;
  219. }
  220. </pre></div>
  221. <p><samp>foo.c</samp>:
  222. </p>
  223. <div class="smallexample">
  224. <pre class="smallexample">#include &quot;foo.h&quot;
  225. volatile int x, y;
  226. int
  227. main ()
  228. {
  229. x = foo (y);
  230. return 0;
  231. }
  232. </pre></div>
  233. <div class="smallexample">
  234. <pre class="smallexample">(gdb) disas /m main
  235. Dump of assembler code for function main:
  236. 5 {
  237. 6 x = foo (y);
  238. 0x0000000000400400 &lt;+0&gt;: mov 0x200c2e(%rip),%eax # 0x601034 &lt;y&gt;
  239. 0x0000000000400417 &lt;+23&gt;: mov %eax,0x200c13(%rip) # 0x601030 &lt;x&gt;
  240. 7 return 0;
  241. 8 }
  242. 0x000000000040041d &lt;+29&gt;: xor %eax,%eax
  243. 0x000000000040041f &lt;+31&gt;: retq
  244. 0x0000000000400420 &lt;+32&gt;: add %eax,%eax
  245. 0x0000000000400422 &lt;+34&gt;: jmp 0x400417 &lt;main+23&gt;
  246. End of assembler dump.
  247. (gdb) disas /s main
  248. Dump of assembler code for function main:
  249. foo.c:
  250. 5 {
  251. 6 x = foo (y);
  252. 0x0000000000400400 &lt;+0&gt;: mov 0x200c2e(%rip),%eax # 0x601034 &lt;y&gt;
  253. foo.h:
  254. 4 if (a &lt; 0)
  255. 0x0000000000400406 &lt;+6&gt;: test %eax,%eax
  256. 0x0000000000400408 &lt;+8&gt;: js 0x400420 &lt;main+32&gt;
  257. 6 if (a == 0)
  258. 7 return 1;
  259. 8 return a + 10;
  260. 0x000000000040040a &lt;+10&gt;: lea 0xa(%rax),%edx
  261. 0x000000000040040d &lt;+13&gt;: test %eax,%eax
  262. 0x000000000040040f &lt;+15&gt;: mov $0x1,%eax
  263. 0x0000000000400414 &lt;+20&gt;: cmovne %edx,%eax
  264. foo.c:
  265. 6 x = foo (y);
  266. 0x0000000000400417 &lt;+23&gt;: mov %eax,0x200c13(%rip) # 0x601030 &lt;x&gt;
  267. 7 return 0;
  268. 8 }
  269. 0x000000000040041d &lt;+29&gt;: xor %eax,%eax
  270. 0x000000000040041f &lt;+31&gt;: retq
  271. foo.h:
  272. 5 return a * 2;
  273. 0x0000000000400420 &lt;+32&gt;: add %eax,%eax
  274. 0x0000000000400422 &lt;+34&gt;: jmp 0x400417 &lt;main+23&gt;
  275. End of assembler dump.
  276. </pre></div>
  277. <p>Here is another example showing raw instructions in hex for AMD x86-64,
  278. </p>
  279. <div class="smallexample">
  280. <pre class="smallexample">(gdb) disas /r 0x400281,+10
  281. Dump of assembler code from 0x400281 to 0x40028b:
  282. 0x0000000000400281: 38 36 cmp %dh,(%rsi)
  283. 0x0000000000400283: 2d 36 34 2e 73 sub $0x732e3436,%eax
  284. 0x0000000000400288: 6f outsl %ds:(%rsi),(%dx)
  285. 0x0000000000400289: 2e 32 00 xor %cs:(%rax),%al
  286. End of assembler dump.
  287. </pre></div>
  288. <p>Addresses cannot be specified as a location (see <a href="Specify-Location.html#Specify-Location">Specify Location</a>).
  289. So, for example, if you want to disassemble function <code>bar</code>
  290. in file <samp>foo.c</samp>, you must type &lsquo;<samp>disassemble 'foo.c'::bar</samp>&rsquo;
  291. and not &lsquo;<samp>disassemble foo.c:bar</samp>&rsquo;.
  292. </p>
  293. <p>Some architectures have more than one commonly-used set of instruction
  294. mnemonics or other syntax.
  295. </p>
  296. <p>For programs that were dynamically linked and use shared libraries,
  297. instructions that call functions or branch to locations in the shared
  298. libraries might show a seemingly bogus location&mdash;it&rsquo;s actually a
  299. location of the relocation table. On some architectures, <small>GDB</small>
  300. might be able to resolve these to actual function names.
  301. </p>
  302. <dl compact="compact">
  303. <dd><a name="index-set-disassembler_002doptions"></a>
  304. <a name="index-disassembler-options"></a>
  305. </dd>
  306. <dt><code>set disassembler-options <var>option1</var>[,<var>option2</var>&hellip;]</code></dt>
  307. <dd><p>This command controls the passing of target specific information to
  308. the disassembler. For a list of valid options, please refer to the
  309. <code>-M</code>/<code>--disassembler-options</code> section of the &lsquo;<samp>objdump</samp>&rsquo;
  310. manual and/or the output of <kbd>objdump --help</kbd>
  311. (see <a href="http://sourceware.org/binutils/docs/binutils/objdump.html#objdump">objdump</a> in <cite>The GNU Binary Utilities</cite>).
  312. The default value is the empty string.
  313. </p>
  314. <p>If it is necessary to specify more than one disassembler option, then
  315. multiple options can be placed together into a comma separated list.
  316. Currently this command is only supported on targets ARM, MIPS, PowerPC
  317. and S/390.
  318. </p>
  319. <a name="index-show-disassembler_002doptions"></a>
  320. </dd>
  321. <dt><code>show disassembler-options</code></dt>
  322. <dd><p>Show the current setting of the disassembler options.
  323. </p></dd>
  324. </dl>
  325. <dl compact="compact">
  326. <dd><a name="index-set-disassembly_002dflavor"></a>
  327. <a name="index-Intel-disassembly-flavor"></a>
  328. <a name="index-AT_0026T-disassembly-flavor"></a>
  329. </dd>
  330. <dt><code>set disassembly-flavor <var>instruction-set</var></code></dt>
  331. <dd><p>Select the instruction set to use when disassembling the
  332. program via the <code>disassemble</code> or <code>x/i</code> commands.
  333. </p>
  334. <p>Currently this command is only defined for the Intel x86 family. You
  335. can set <var>instruction-set</var> to either <code>intel</code> or <code>att</code>.
  336. The default is <code>att</code>, the AT&amp;T flavor used by default by Unix
  337. assemblers for x86-based targets.
  338. </p>
  339. <a name="index-show-disassembly_002dflavor"></a>
  340. </dd>
  341. <dt><code>show disassembly-flavor</code></dt>
  342. <dd><p>Show the current setting of the disassembly flavor.
  343. </p></dd>
  344. </dl>
  345. <dl compact="compact">
  346. <dd><a name="index-set-disassemble_002dnext_002dline"></a>
  347. <a name="index-show-disassemble_002dnext_002dline"></a>
  348. </dd>
  349. <dt><code>set disassemble-next-line</code></dt>
  350. <dt><code>show disassemble-next-line</code></dt>
  351. <dd><p>Control whether or not <small>GDB</small> will disassemble the next source
  352. line or instruction when execution stops. If ON, <small>GDB</small> will
  353. display disassembly of the next source line when execution of the
  354. program being debugged stops. This is <em>in addition</em> to
  355. displaying the source line itself, which <small>GDB</small> always does if
  356. possible. If the next source line cannot be displayed for some reason
  357. (e.g., if <small>GDB</small> cannot find the source file, or there&rsquo;s no line
  358. info in the debug info), <small>GDB</small> will display disassembly of the
  359. next <em>instruction</em> instead of showing the next source line. If
  360. AUTO, <small>GDB</small> will display disassembly of next instruction only
  361. if the source line cannot be displayed. This setting causes
  362. <small>GDB</small> to display some feedback when you step through a function
  363. with no line info or whose source file is unavailable. The default is
  364. OFF, which means never display the disassembly of the next line or
  365. instruction.
  366. </p></dd>
  367. </dl>
  368. <hr>
  369. <div class="header">
  370. <p>
  371. Previous: <a href="Source-Path.html#Source-Path" accesskey="p" rel="previous">Source Path</a>, Up: <a href="Source.html#Source" accesskey="u" rel="up">Source</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>
  372. </div>
  373. </body>
  374. </html>