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-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Machine Code</title> 18 19<meta name="description" content="Debugging with GDB: Machine Code"> 20<meta name="keywords" content="Debugging with GDB: Machine Code"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Source.html#Source" rel="up" title="Source"> 29<link href="Data.html#Data" rel="next" title="Data"> 30<link href="Source-Path.html#Source-Path" rel="previous" title="Source Path"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Machine-Code"></a> 65<div class="header"> 66<p> 67Previous: <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> [<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> 68</div> 69<hr> 70<a name="Source-and-Machine-Code"></a> 71<h3 class="section">9.6 Source and Machine Code</h3> 72<a name="index-source-line-and-its-code-address"></a> 73 74<p>You can use the command <code>info line</code> to map source lines to program 75addresses (and vice versa), and the command <code>disassemble</code> to display 76a range of addresses as machine instructions. You can use the command 77<code>set disassemble-next-line</code> to set whether to disassemble next 78source line when execution stops. When run under <small>GNU</small> Emacs 79mode, the <code>info line</code> command causes the arrow to point to the 80line specified. Also, <code>info line</code> prints addresses in symbolic form as 81well as hex. 82</p> 83<dl compact="compact"> 84<dd><a name="index-info-line"></a> 85</dd> 86<dt><code>info line</code></dt> 87<dt><code>info line <var>location</var></code></dt> 88<dd><p>Print the starting and ending addresses of the compiled code for 89source line <var>location</var>. You can specify source lines in any of 90the ways documented in <a href="Specify-Location.html#Specify-Location">Specify Location</a>. With no <var>location</var> 91information about the current source line is printed. 92</p></dd> 93</dl> 94 95<p>For example, we can use <code>info line</code> to discover the location of 96the object code for the first line of function 97<code>m4_changequote</code>: 98</p> 99<div class="smallexample"> 100<pre class="smallexample">(gdb) info line m4_changequote 101Line 895 of "builtin.c" starts at pc 0x634c <m4_changequote> and \ 102 ends at 0x6350 <m4_changequote+4>. 103</pre></div> 104 105<a name="index-code-address-and-its-source-line"></a> 106<p>We can also inquire (using <code>*<var>addr</var></code> as the form for 107<var>location</var>) what source line covers a particular address: 108</p><div class="smallexample"> 109<pre class="smallexample">(gdb) info line *0x63ff 110Line 926 of "builtin.c" starts at pc 0x63e4 <m4_changequote+152> and \ 111 ends at 0x6404 <m4_changequote+184>. 112</pre></div> 113 114<a name="index-_0024_005f-and-info-line"></a> 115<a name="index-x-command_002c-default-address"></a> 116<a name="index-x_0028examine_0029_002c-and-info-line"></a> 117<p>After <code>info line</code>, the default address for the <code>x</code> command 118is changed to the starting address of the line, so that ‘<samp>x/i</samp>’ is 119sufficient 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 120convenience variable <code>$_</code> (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience 121Variables</a>). 122</p> 123<a name="index-info-line_002c-repeated-calls"></a> 124<p>After <code>info line</code>, using <code>info line</code> again without 125specifying a location will display information about the next source 126line. 127</p> 128<dl compact="compact"> 129<dd><a name="index-disassemble"></a> 130<a name="index-assembly-instructions"></a> 131<a name="index-instructions_002c-assembly"></a> 132<a name="index-machine-instructions"></a> 133<a name="index-listing-machine-instructions"></a> 134</dd> 135<dt><code>disassemble</code></dt> 136<dt><code>disassemble /m</code></dt> 137<dt><code>disassemble /s</code></dt> 138<dt><code>disassemble /r</code></dt> 139<dd><p>This specialized command dumps a range of memory as machine 140instructions. It can also print mixed source+disassembly by specifying 141the <code>/m</code> or <code>/s</code> modifier and print the raw instructions in hex 142as well as in symbolic form by specifying the <code>/r</code> modifier. 143The default memory range is the function surrounding the 144program counter of the selected frame. A single argument to this 145command is a program counter value; <small>GDB</small> dumps the function 146surrounding this value. When two arguments are given, they should 147be separated by a comma, possibly surrounded by whitespace. The 148arguments specify a range of addresses to dump, in one of two forms: 149</p> 150<dl compact="compact"> 151<dt><code><var>start</var>,<var>end</var></code></dt> 152<dd><p>the addresses from <var>start</var> (inclusive) to <var>end</var> (exclusive) 153</p></dd> 154<dt><code><var>start</var>,+<var>length</var></code></dt> 155<dd><p>the addresses from <var>start</var> (inclusive) to 156<code><var>start</var>+<var>length</var></code> (exclusive). 157</p></dd> 158</dl> 159 160<p>When 2 arguments are specified, the name of the function is also 161printed (since there could be several functions in the given range). 162</p> 163<p>The argument(s) can be any expression yielding a numeric value, such as 164‘<samp>0x32c4</samp>’, ‘<samp>&main+10</samp>’ or ‘<samp>$pc - 8</samp>’. 165</p> 166<p>If the range of memory being disassembled contains current program counter, 167the instruction at that location is shown with a <code>=></code> marker. 168</p></dd> 169</dl> 170 171<p>The following example shows the disassembly of a range of addresses of 172HP PA-RISC 2.0 code: 173</p> 174<div class="smallexample"> 175<pre class="smallexample">(gdb) disas 0x32c4, 0x32e4 176Dump of assembler code from 0x32c4 to 0x32e4: 177 0x32c4 <main+204>: addil 0,dp 178 0x32c8 <main+208>: ldw 0x22c(sr0,r1),r26 179 0x32cc <main+212>: ldil 0x3000,r31 180 0x32d0 <main+216>: ble 0x3f8(sr4,r31) 181 0x32d4 <main+220>: ldo 0(r31),rp 182 0x32d8 <main+224>: addil -0x800,dp 183 0x32dc <main+228>: ldo 0x588(r1),r26 184 0x32e0 <main+232>: ldil 0x3000,r31 185End of assembler dump. 186</pre></div> 187 188<p>Here is an example showing mixed source+assembly for Intel x86 189with <code>/m</code> or <code>/s</code>, when the program is stopped just after 190function prologue in a non-optimized function with no inline code. 191</p> 192<div class="smallexample"> 193<pre class="smallexample">(gdb) disas /m main 194Dump of assembler code for function main: 1955 { 196 0x08048330 <+0>: push %ebp 197 0x08048331 <+1>: mov %esp,%ebp 198 0x08048333 <+3>: sub $0x8,%esp 199 0x08048336 <+6>: and $0xfffffff0,%esp 200 0x08048339 <+9>: sub $0x10,%esp 201 2026 printf ("Hello.\n"); 203=> 0x0804833c <+12>: movl $0x8048440,(%esp) 204 0x08048343 <+19>: call 0x8048284 <puts@plt> 205 2067 return 0; 2078 } 208 0x08048348 <+24>: mov $0x0,%eax 209 0x0804834d <+29>: leave 210 0x0804834e <+30>: ret 211 212End of assembler dump. 213</pre></div> 214 215<p>The <code>/m</code> option is deprecated as its output is not useful when 216there is either inlined code or re-ordered code. 217The <code>/s</code> option is the preferred choice. 218Here is an example for AMD x86-64 showing the difference between 219<code>/m</code> output and <code>/s</code> output. 220This example has one inline function defined in a header file, 221and the code is compiled with ‘<samp>-O2</samp>’ optimization. 222Note how the <code>/m</code> output is missing the disassembly of 223several instructions that are present in the <code>/s</code> output. 224</p> 225<p><samp>foo.h</samp>: 226</p> 227<div class="smallexample"> 228<pre class="smallexample">int 229foo (int a) 230{ 231 if (a < 0) 232 return a * 2; 233 if (a == 0) 234 return 1; 235 return a + 10; 236} 237</pre></div> 238 239<p><samp>foo.c</samp>: 240</p> 241<div class="smallexample"> 242<pre class="smallexample">#include "foo.h" 243volatile int x, y; 244int 245main () 246{ 247 x = foo (y); 248 return 0; 249} 250</pre></div> 251 252<div class="smallexample"> 253<pre class="smallexample">(gdb) disas /m main 254Dump of assembler code for function main: 2555 { 256 2576 x = foo (y); 258 0x0000000000400400 <+0>: mov 0x200c2e(%rip),%eax # 0x601034 <y> 259 0x0000000000400417 <+23>: mov %eax,0x200c13(%rip) # 0x601030 <x> 260 2617 return 0; 2628 } 263 0x000000000040041d <+29>: xor %eax,%eax 264 0x000000000040041f <+31>: retq 265 0x0000000000400420 <+32>: add %eax,%eax 266 0x0000000000400422 <+34>: jmp 0x400417 <main+23> 267 268End of assembler dump. 269(gdb) disas /s main 270Dump of assembler code for function main: 271foo.c: 2725 { 2736 x = foo (y); 274 0x0000000000400400 <+0>: mov 0x200c2e(%rip),%eax # 0x601034 <y> 275 276foo.h: 2774 if (a < 0) 278 0x0000000000400406 <+6>: test %eax,%eax 279 0x0000000000400408 <+8>: js 0x400420 <main+32> 280 2816 if (a == 0) 2827 return 1; 2838 return a + 10; 284 0x000000000040040a <+10>: lea 0xa(%rax),%edx 285 0x000000000040040d <+13>: test %eax,%eax 286 0x000000000040040f <+15>: mov $0x1,%eax 287 0x0000000000400414 <+20>: cmovne %edx,%eax 288 289foo.c: 2906 x = foo (y); 291 0x0000000000400417 <+23>: mov %eax,0x200c13(%rip) # 0x601030 <x> 292 2937 return 0; 2948 } 295 0x000000000040041d <+29>: xor %eax,%eax 296 0x000000000040041f <+31>: retq 297 298foo.h: 2995 return a * 2; 300 0x0000000000400420 <+32>: add %eax,%eax 301 0x0000000000400422 <+34>: jmp 0x400417 <main+23> 302End of assembler dump. 303</pre></div> 304 305<p>Here is another example showing raw instructions in hex for AMD x86-64, 306</p> 307<div class="smallexample"> 308<pre class="smallexample">(gdb) disas /r 0x400281,+10 309Dump of assembler code from 0x400281 to 0x40028b: 310 0x0000000000400281: 38 36 cmp %dh,(%rsi) 311 0x0000000000400283: 2d 36 34 2e 73 sub $0x732e3436,%eax 312 0x0000000000400288: 6f outsl %ds:(%rsi),(%dx) 313 0x0000000000400289: 2e 32 00 xor %cs:(%rax),%al 314End of assembler dump. 315</pre></div> 316 317<p>Addresses cannot be specified as a location (see <a href="Specify-Location.html#Specify-Location">Specify Location</a>). 318So, for example, if you want to disassemble function <code>bar</code> 319in file <samp>foo.c</samp>, you must type ‘<samp>disassemble 'foo.c'::bar</samp>’ 320and not ‘<samp>disassemble foo.c:bar</samp>’. 321</p> 322<p>Some architectures have more than one commonly-used set of instruction 323mnemonics or other syntax. 324</p> 325<p>For programs that were dynamically linked and use shared libraries, 326instructions that call functions or branch to locations in the shared 327libraries might show a seemingly bogus location—it’s actually a 328location of the relocation table. On some architectures, <small>GDB</small> 329might be able to resolve these to actual function names. 330</p> 331<dl compact="compact"> 332<dd><a name="index-set-disassembler_002doptions"></a> 333<a name="index-disassembler-options"></a> 334</dd> 335<dt><code>set disassembler-options <var>option1</var>[,<var>option2</var>…]</code></dt> 336<dd><p>This command controls the passing of target specific information to 337the disassembler. For a list of valid options, please refer to the 338<code>-M</code>/<code>--disassembler-options</code> section of the ‘<samp>objdump</samp>’ 339manual and/or the output of <kbd>objdump --help</kbd> 340(see <a href="http://sourceware.org/binutils/docs/binutils/objdump.html#objdump">objdump</a> in <cite>The GNU Binary Utilities</cite>). 341The default value is the empty string. 342</p> 343<p>If it is necessary to specify more than one disassembler option, then 344multiple options can be placed together into a comma separated list. 345Currently this command is only supported on targets ARM, MIPS, PowerPC 346and S/390. 347</p> 348<a name="index-show-disassembler_002doptions"></a> 349</dd> 350<dt><code>show disassembler-options</code></dt> 351<dd><p>Show the current setting of the disassembler options. 352</p></dd> 353</dl> 354 355<dl compact="compact"> 356<dd><a name="index-set-disassembly_002dflavor"></a> 357<a name="index-Intel-disassembly-flavor"></a> 358<a name="index-AT_0026T-disassembly-flavor"></a> 359</dd> 360<dt><code>set disassembly-flavor <var>instruction-set</var></code></dt> 361<dd><p>Select the instruction set to use when disassembling the 362program via the <code>disassemble</code> or <code>x/i</code> commands. 363</p> 364<p>Currently this command is only defined for the Intel x86 family. You 365can set <var>instruction-set</var> to either <code>intel</code> or <code>att</code>. 366The default is <code>att</code>, the AT&T flavor used by default by Unix 367assemblers for x86-based targets. 368</p> 369<a name="index-show-disassembly_002dflavor"></a> 370</dd> 371<dt><code>show disassembly-flavor</code></dt> 372<dd><p>Show the current setting of the disassembly flavor. 373</p></dd> 374</dl> 375 376<dl compact="compact"> 377<dd><a name="index-set-disassemble_002dnext_002dline"></a> 378<a name="index-show-disassemble_002dnext_002dline"></a> 379</dd> 380<dt><code>set disassemble-next-line</code></dt> 381<dt><code>show disassemble-next-line</code></dt> 382<dd><p>Control whether or not <small>GDB</small> will disassemble the next source 383line or instruction when execution stops. If ON, <small>GDB</small> will 384display disassembly of the next source line when execution of the 385program being debugged stops. This is <em>in addition</em> to 386displaying the source line itself, which <small>GDB</small> always does if 387possible. If the next source line cannot be displayed for some reason 388(e.g., if <small>GDB</small> cannot find the source file, or there’s no line 389info in the debug info), <small>GDB</small> will display disassembly of the 390next <em>instruction</em> instead of showing the next source line. If 391AUTO, <small>GDB</small> will display disassembly of next instruction only 392if the source line cannot be displayed. This setting causes 393<small>GDB</small> to display some feedback when you step through a function 394with no line info or whose source file is unavailable. The default is 395OFF, which means never display the disassembly of the next line or 396instruction. 397</p></dd> 398</dl> 399 400 401<hr> 402<div class="header"> 403<p> 404Previous: <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> [<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> 405</div> 406 407 408 409</body> 410</html> 411