Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

410 lines
16KB

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