326 lines
15 KiB
HTML
326 lines
15 KiB
HTML
<html lang="en">
|
|
<head>
|
|
<title>Machine Code - Debugging with GDB</title>
|
|
<meta http-equiv="Content-Type" content="text/html">
|
|
<meta name="description" content="Debugging with GDB">
|
|
<meta name="generator" content="makeinfo 4.13">
|
|
<link title="Top" rel="start" href="index.html#Top">
|
|
<link rel="up" href="Source.html#Source" title="Source">
|
|
<link rel="prev" href="Source-Path.html#Source-Path" title="Source Path">
|
|
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
|
|
<!--
|
|
Copyright (C) 1988-2019 Free Software Foundation, Inc.
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
|
any later version published by the Free Software Foundation; with the
|
|
Invariant Sections being ``Free Software'' and ``Free Software Needs
|
|
Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,''
|
|
and with the Back-Cover Texts as in (a) below.
|
|
|
|
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
|
|
this GNU Manual. Buying copies from GNU Press supports the FSF in
|
|
developing GNU and promoting software freedom.''
|
|
-->
|
|
<meta http-equiv="Content-Style-Type" content="text/css">
|
|
<style type="text/css"><!--
|
|
pre.display { font-family:inherit }
|
|
pre.format { font-family:inherit }
|
|
pre.smalldisplay { font-family:inherit; font-size:smaller }
|
|
pre.smallformat { font-family:inherit; font-size:smaller }
|
|
pre.smallexample { font-size:smaller }
|
|
pre.smalllisp { font-size:smaller }
|
|
span.sc { font-variant:small-caps }
|
|
span.roman { font-family:serif; font-weight:normal; }
|
|
span.sansserif { font-family:sans-serif; font-weight:normal; }
|
|
--></style>
|
|
</head>
|
|
<body>
|
|
<div class="node">
|
|
<a name="Machine-Code"></a>
|
|
<p>
|
|
Previous: <a rel="previous" accesskey="p" href="Source-Path.html#Source-Path">Source Path</a>,
|
|
Up: <a rel="up" accesskey="u" href="Source.html#Source">Source</a>
|
|
<hr>
|
|
</div>
|
|
|
|
<h3 class="section">9.6 Source and Machine Code</h3>
|
|
|
|
<p><a name="index-source-line-and-its-code-address-595"></a>
|
|
You can use the command <code>info line</code> to map source lines to program
|
|
addresses (and vice versa), and the command <code>disassemble</code> to display
|
|
a range of addresses as machine instructions. You can use the command
|
|
<code>set disassemble-next-line</code> to set whether to disassemble next
|
|
source line when execution stops. When run under <span class="sc">gnu</span> Emacs
|
|
mode, the <code>info line</code> command causes the arrow to point to the
|
|
line specified. Also, <code>info line</code> prints addresses in symbolic form as
|
|
well as hex.
|
|
|
|
|
|
<a name="index-info-line-596"></a>
|
|
<dl><dt><code>info line</code><dt><code>info line </code><var>location</var><dd>Print the starting and ending addresses of the compiled code for
|
|
source line <var>location</var>. You can specify source lines in any of
|
|
the ways documented in <a href="Specify-Location.html#Specify-Location">Specify Location</a>. With no <var>location</var>
|
|
information about the current source line is printed.
|
|
</dl>
|
|
|
|
<p>For example, we can use <code>info line</code> to discover the location of
|
|
the object code for the first line of function
|
|
<code>m4_changequote</code>:
|
|
|
|
<pre class="smallexample"> (gdb) info line m4_changequote
|
|
Line 895 of "builtin.c" starts at pc 0x634c <m4_changequote> and \
|
|
ends at 0x6350 <m4_changequote+4>.
|
|
</pre>
|
|
<p class="noindent"><a name="index-code-address-and-its-source-line-597"></a>We can also inquire (using <code>*</code><var>addr</var> as the form for
|
|
<var>location</var>) what source line covers a particular address:
|
|
<pre class="smallexample"> (gdb) info line *0x63ff
|
|
Line 926 of "builtin.c" starts at pc 0x63e4 <m4_changequote+152> and \
|
|
ends at 0x6404 <m4_changequote+184>.
|
|
</pre>
|
|
<p><a name="index-g_t_0040code_007b_0024_005f_007d-and-_0040code_007binfo-line_007d-598"></a><a name="index-g_t_0040code_007bx_007d-command_002c-default-address-599"></a><a name="index-x_0040r_007b_0028examine_0029_002c-and_007d-info-line-600"></a>After <code>info line</code>, the default address for the <code>x</code> command
|
|
is changed to the starting address of the line, so that ‘<samp><span class="samp">x/i</span></samp>’ is
|
|
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
|
|
convenience variable <code>$_</code> (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience Variables</a>).
|
|
|
|
<p><a name="index-info-line_002c-repeated-calls-601"></a>After <code>info line</code>, using <code>info line</code> again without
|
|
specifying a location will display information about the next source
|
|
line.
|
|
|
|
|
|
<a name="index-disassemble-602"></a>
|
|
<a name="index-assembly-instructions-603"></a>
|
|
<a name="index-instructions_002c-assembly-604"></a>
|
|
<a name="index-machine-instructions-605"></a>
|
|
<a name="index-listing-machine-instructions-606"></a>
|
|
<dl><dt><code>disassemble</code><dt><code>disassemble /m</code><dt><code>disassemble /s</code><dt><code>disassemble /r</code><dd>This specialized command dumps a range of memory as machine
|
|
instructions. It can also print mixed source+disassembly by specifying
|
|
the <code>/m</code> or <code>/s</code> modifier and print the raw instructions in hex
|
|
as well as in symbolic form by specifying the <code>/r</code> modifier.
|
|
The default memory range is the function surrounding the
|
|
program counter of the selected frame. A single argument to this
|
|
command is a program counter value; <span class="sc">gdb</span> dumps the function
|
|
surrounding this value. When two arguments are given, they should
|
|
be separated by a comma, possibly surrounded by whitespace. The
|
|
arguments specify a range of addresses to dump, in one of two forms:
|
|
|
|
<dl>
|
|
<dt><var>start</var><code>,</code><var>end</var><dd>the addresses from <var>start</var> (inclusive) to <var>end</var> (exclusive)
|
|
<br><dt><var>start</var><code>,+</code><var>length</var><dd>the addresses from <var>start</var> (inclusive) to
|
|
<var>start</var><code>+</code><var>length</var> (exclusive).
|
|
</dl>
|
|
|
|
<p class="noindent">When 2 arguments are specified, the name of the function is also
|
|
printed (since there could be several functions in the given range).
|
|
|
|
<p>The argument(s) can be any expression yielding a numeric value, such as
|
|
‘<samp><span class="samp">0x32c4</span></samp>’, ‘<samp><span class="samp">&main+10</span></samp>’ or ‘<samp><span class="samp">$pc - 8</span></samp>’.
|
|
|
|
<p>If the range of memory being disassembled contains current program counter,
|
|
the instruction at that location is shown with a <code>=></code> marker.
|
|
</dl>
|
|
|
|
<p>The following example shows the disassembly of a range of addresses of
|
|
HP PA-RISC 2.0 code:
|
|
|
|
<pre class="smallexample"> (gdb) disas 0x32c4, 0x32e4
|
|
Dump of assembler code from 0x32c4 to 0x32e4:
|
|
0x32c4 <main+204>: addil 0,dp
|
|
0x32c8 <main+208>: ldw 0x22c(sr0,r1),r26
|
|
0x32cc <main+212>: ldil 0x3000,r31
|
|
0x32d0 <main+216>: ble 0x3f8(sr4,r31)
|
|
0x32d4 <main+220>: ldo 0(r31),rp
|
|
0x32d8 <main+224>: addil -0x800,dp
|
|
0x32dc <main+228>: ldo 0x588(r1),r26
|
|
0x32e0 <main+232>: ldil 0x3000,r31
|
|
End of assembler dump.
|
|
</pre>
|
|
<p>Here is an example showing mixed source+assembly for Intel x86
|
|
with <code>/m</code> or <code>/s</code>, when the program is stopped just after
|
|
function prologue in a non-optimized function with no inline code.
|
|
|
|
<pre class="smallexample"> (gdb) disas /m main
|
|
Dump of assembler code for function main:
|
|
5 {
|
|
0x08048330 <+0>: push %ebp
|
|
0x08048331 <+1>: mov %esp,%ebp
|
|
0x08048333 <+3>: sub $0x8,%esp
|
|
0x08048336 <+6>: and $0xfffffff0,%esp
|
|
0x08048339 <+9>: sub $0x10,%esp
|
|
|
|
6 printf ("Hello.\n");
|
|
=> 0x0804833c <+12>: movl $0x8048440,(%esp)
|
|
0x08048343 <+19>: call 0x8048284 <puts@plt>
|
|
|
|
7 return 0;
|
|
8 }
|
|
0x08048348 <+24>: mov $0x0,%eax
|
|
0x0804834d <+29>: leave
|
|
0x0804834e <+30>: ret
|
|
|
|
End of assembler dump.
|
|
</pre>
|
|
<p>The <code>/m</code> option is deprecated as its output is not useful when
|
|
there is either inlined code or re-ordered code.
|
|
The <code>/s</code> option is the preferred choice.
|
|
Here is an example for AMD x86-64 showing the difference between
|
|
<code>/m</code> output and <code>/s</code> output.
|
|
This example has one inline function defined in a header file,
|
|
and the code is compiled with ‘<samp><span class="samp">-O2</span></samp>’ optimization.
|
|
Note how the <code>/m</code> output is missing the disassembly of
|
|
several instructions that are present in the <code>/s</code> output.
|
|
|
|
<p><samp><span class="file">foo.h</span></samp>:
|
|
|
|
<pre class="smallexample"> int
|
|
foo (int a)
|
|
{
|
|
if (a < 0)
|
|
return a * 2;
|
|
if (a == 0)
|
|
return 1;
|
|
return a + 10;
|
|
}
|
|
</pre>
|
|
<p><samp><span class="file">foo.c</span></samp>:
|
|
|
|
<pre class="smallexample"> #include "foo.h"
|
|
volatile int x, y;
|
|
int
|
|
main ()
|
|
{
|
|
x = foo (y);
|
|
return 0;
|
|
}
|
|
</pre>
|
|
<pre class="smallexample"> (gdb) disas /m main
|
|
Dump of assembler code for function main:
|
|
5 {
|
|
|
|
6 x = foo (y);
|
|
0x0000000000400400 <+0>: mov 0x200c2e(%rip),%eax # 0x601034 <y>
|
|
0x0000000000400417 <+23>: mov %eax,0x200c13(%rip) # 0x601030 <x>
|
|
|
|
7 return 0;
|
|
8 }
|
|
0x000000000040041d <+29>: xor %eax,%eax
|
|
0x000000000040041f <+31>: retq
|
|
0x0000000000400420 <+32>: add %eax,%eax
|
|
0x0000000000400422 <+34>: jmp 0x400417 <main+23>
|
|
|
|
End of assembler dump.
|
|
(gdb) disas /s main
|
|
Dump of assembler code for function main:
|
|
foo.c:
|
|
5 {
|
|
6 x = foo (y);
|
|
0x0000000000400400 <+0>: mov 0x200c2e(%rip),%eax # 0x601034 <y>
|
|
|
|
foo.h:
|
|
4 if (a < 0)
|
|
0x0000000000400406 <+6>: test %eax,%eax
|
|
0x0000000000400408 <+8>: js 0x400420 <main+32>
|
|
|
|
6 if (a == 0)
|
|
7 return 1;
|
|
8 return a + 10;
|
|
0x000000000040040a <+10>: lea 0xa(%rax),%edx
|
|
0x000000000040040d <+13>: test %eax,%eax
|
|
0x000000000040040f <+15>: mov $0x1,%eax
|
|
0x0000000000400414 <+20>: cmovne %edx,%eax
|
|
|
|
foo.c:
|
|
6 x = foo (y);
|
|
0x0000000000400417 <+23>: mov %eax,0x200c13(%rip) # 0x601030 <x>
|
|
|
|
7 return 0;
|
|
8 }
|
|
0x000000000040041d <+29>: xor %eax,%eax
|
|
0x000000000040041f <+31>: retq
|
|
|
|
foo.h:
|
|
5 return a * 2;
|
|
0x0000000000400420 <+32>: add %eax,%eax
|
|
0x0000000000400422 <+34>: jmp 0x400417 <main+23>
|
|
End of assembler dump.
|
|
</pre>
|
|
<p>Here is another example showing raw instructions in hex for AMD x86-64,
|
|
|
|
<pre class="smallexample"> (gdb) disas /r 0x400281,+10
|
|
Dump of assembler code from 0x400281 to 0x40028b:
|
|
0x0000000000400281: 38 36 cmp %dh,(%rsi)
|
|
0x0000000000400283: 2d 36 34 2e 73 sub $0x732e3436,%eax
|
|
0x0000000000400288: 6f outsl %ds:(%rsi),(%dx)
|
|
0x0000000000400289: 2e 32 00 xor %cs:(%rax),%al
|
|
End of assembler dump.
|
|
</pre>
|
|
<p>Addresses cannot be specified as a location (see <a href="Specify-Location.html#Specify-Location">Specify Location</a>).
|
|
So, for example, if you want to disassemble function <code>bar</code>
|
|
in file <samp><span class="file">foo.c</span></samp>, you must type ‘<samp><span class="samp">disassemble 'foo.c'::bar</span></samp>’
|
|
and not ‘<samp><span class="samp">disassemble foo.c:bar</span></samp>’.
|
|
|
|
<p>Some architectures have more than one commonly-used set of instruction
|
|
mnemonics or other syntax.
|
|
|
|
<p>For programs that were dynamically linked and use shared libraries,
|
|
instructions that call functions or branch to locations in the shared
|
|
libraries might show a seemingly bogus location—it's actually a
|
|
location of the relocation table. On some architectures, <span class="sc">gdb</span>
|
|
might be able to resolve these to actual function names.
|
|
|
|
|
|
<a name="index-set-disassembler_002doptions-607"></a>
|
|
<a name="index-disassembler-options-608"></a>
|
|
<dl><dt><code>set disassembler-options </code><var>option1</var><code>[,</code><var>option2</var><code>...]</code><dd>This command controls the passing of target specific information to
|
|
the disassembler. For a list of valid options, please refer to the
|
|
<code>-M</code>/<code>--disassembler-options</code> section of the ‘<samp><span class="samp">objdump</span></samp>’
|
|
manual and/or the output of <kbd>objdump --help</kbd>
|
|
(see <a href="../binutils/objdump.html#objdump">objdump</a>).
|
|
The default value is the empty string.
|
|
|
|
<p>If it is necessary to specify more than one disassembler option, then
|
|
multiple options can be placed together into a comma separated list.
|
|
Currently this command is only supported on targets ARM, MIPS, PowerPC
|
|
and S/390.
|
|
|
|
<p><a name="index-show-disassembler_002doptions-609"></a><br><dt><code>show disassembler-options</code><dd>Show the current setting of the disassembler options.
|
|
</dl>
|
|
|
|
|
|
<a name="index-set-disassembly_002dflavor-610"></a>
|
|
<a name="index-Intel-disassembly-flavor-611"></a>
|
|
<a name="index-AT_0026T-disassembly-flavor-612"></a>
|
|
<dl><dt><code>set disassembly-flavor </code><var>instruction-set</var><dd>Select the instruction set to use when disassembling the
|
|
program via the <code>disassemble</code> or <code>x/i</code> commands.
|
|
|
|
<p>Currently this command is only defined for the Intel x86 family. You
|
|
can set <var>instruction-set</var> to either <code>intel</code> or <code>att</code>.
|
|
The default is <code>att</code>, the AT&T flavor used by default by Unix
|
|
assemblers for x86-based targets.
|
|
|
|
<p><a name="index-show-disassembly_002dflavor-613"></a><br><dt><code>show disassembly-flavor</code><dd>Show the current setting of the disassembly flavor.
|
|
</dl>
|
|
|
|
|
|
<a name="index-set-disassemble_002dnext_002dline-614"></a>
|
|
<a name="index-show-disassemble_002dnext_002dline-615"></a>
|
|
<dl><dt><code>set disassemble-next-line</code><dt><code>show disassemble-next-line</code><dd>Control whether or not <span class="sc">gdb</span> will disassemble the next source
|
|
line or instruction when execution stops. If ON, <span class="sc">gdb</span> will
|
|
display disassembly of the next source line when execution of the
|
|
program being debugged stops. This is <em>in addition</em> to
|
|
displaying the source line itself, which <span class="sc">gdb</span> always does if
|
|
possible. If the next source line cannot be displayed for some reason
|
|
(e.g., if <span class="sc">gdb</span> cannot find the source file, or there's no line
|
|
info in the debug info), <span class="sc">gdb</span> will display disassembly of the
|
|
next <em>instruction</em> instead of showing the next source line. If
|
|
AUTO, <span class="sc">gdb</span> will display disassembly of next instruction only
|
|
if the source line cannot be displayed. This setting causes
|
|
<span class="sc">gdb</span> to display some feedback when you step through a function
|
|
with no line info or whose source file is unavailable. The default is
|
|
OFF, which means never display the disassembly of the next line or
|
|
instruction.
|
|
</dl>
|
|
|
|
</body></html>
|
|
|