第 40 / 187 篇

3.5. 调试汇编代码

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C3-C_debug/gdb_assembly.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

3.5. Debugging Assembly Code

除了在 C 和 C++ 高级语言层面调试之外,GDB 还可以在汇编代码层面调试程序。这样,GDB 可以列出函数反汇编后的代码序列、在汇编指令级别设置断点、一次一条汇编指令地单步执行程序,以及在运行时检查机器寄存器和栈、堆内存地址中存放的值。本节以 IA32 作为示例汇编语言,但这里介绍的 GDB 命令适用于 GCC 支持的任何汇编语言。需要说明的是,读者可能要在后面的章节中进一步了解汇编代码之后,才会觉得这一小节最有用。

In addition to high-level C and C++ debugging, GDB can debug a program at its assembly code level. Doing so enables GDB to list disassembled code sequences from functions, set breakpoints at the assembly instruction level, step through program execution one assembly instruction at a time, and examine the values stored in machine registers and in stack and heap memory addresses at runtime. We use IA32 as the example assembly language in this section, but the GDB commands presented here apply to any assembly language that GCC supports. We note that readers may find this subsection most useful after reading more about assembly code in later chapters.

我们用下面这个简短的 C 程序作为例子:

We use the following short C program as an example:

1
2
3
4
5
6
7
8
9
10
11
int main(void) {
int x, y;

x = 1;
x = x + 2;
x = x - 14;
y = x * 100;
x = x + y * 6;

return 0;
}

要编译成 IA32 可执行文件,使用 -m32 标志:

To compile to an IA32 executable, use the -m32 flag:

1
$ gcc -m32 -o simpleops simpleops.c

也可以选择用 gcc 的 -fno-asynchronous-unwind-tables 命令行选项编译,这样生成的 IA32 代码对程序员来说更容易阅读和理解:

Optionally, compiling with gcc‘s -fno-asynchronous-unwind-tables command line option generates IA32 code that’s a bit easier for the programmer to read and understand:

1
$ gcc -m32 -fno-asynchronous-unwind-tables -o simpleops simpleops.c

3.5.1. 用 GDB 检查二进制代码

3.5.1. Using GDB to Examine Binary Code

本节给出一些在汇编代码层面调试这个简短 C 程序的 GDB 命令示例。下表总结了本节将演示的许多命令:

In this section we show some example GDB commands to debug the short C program at the assembly code level. The following table summarizes many of the commands this section demonstrates:

GDB 命令 说明
break sum 在函数 sum 的开头设置断点
break *0x0804851a 在内存地址 0x0804851a 处设置断点
disass main 反汇编 main 函数
ni 执行下一条指令
si 进入函数调用(单步执行指令)
info registers 列出寄存器的内容
p $eax 打印寄存器 %eax 中存放的值
p *(int *)($ebp+8) 打印某个地址(%ebp+8)处的 int 值
x/d $ebp+8 检查某个地址处的内存内容
GDB command Description
break sum Set a breakpoint at the beginning of the function sum
break *0x0804851a Set a breakpoint at memory address 0x0804851a
disass main Disassemble the main function
ni Execute the next instruction
si Step into a function call (step instruction)
info registers List the register contents
p $eax Print the value stored in register %eax
p *(int *)($ebp+8) Print out the value of an int at an address (%ebp+8)
x/d $ebp+8 Examine the contents of memory at an address

首先,编译成 IA32 汇编,并对 IA32 可执行程序 simpleops 运行 GDB:

First, compile to IA32 assembly and run GDB on the IA32 executable program simpleops:

1
2
$ gcc -m32 -fno-asynchronous-unwind-tables -o simpleops simpleops.c
$ gdb ./simpleops

然后,在 main 中设置断点,再用 run 命令开始运行程序:

Then, set a breakpoint in main, and then start running the program with the run command:

1
2
(gdb) break main
(gdb) run

disass 命令反汇编程序的某些部分(列出与之对应的汇编代码)。例如,查看 main 函数的汇编指令:

The disass command disassembles (lists the assembly code associated with) parts of the program. For example, to view the assembly instructions of the main function:

1
(gdb) disass main         # Disassemble the main function

GDB 允许程序员在单条汇编指令处设置断点,方法是对指令的内存地址解引用:

GDB allows a programmer to set breakpoints at individual assembly instructions by dereferencing the memory address of the instruction:

1
(gdb) break *0x080483c1   # Set breakpoint at instruction at 0x080483c1

可以用 si 或 ni 一次一条汇编指令地执行程序,分别是进入下一条指令或执行下一条指令:

The program’s execution can be executed one assembly instruction at a time using si or ni to step into or execute the next instruction:

1
2
3
4
(gdb) ni     # Execute the next instruction

(gdb) si # Execute next instruction; if it is a call instruction,
# then step into the function

si 命令会进入函数调用,即 GDB 会让程序暂停在被调用函数的第一条指令处。ni 命令则跳过函数调用,即 GDB 会让程序暂停在 call 指令之后的下一条指令处(在函数执行完并返回调用者之后)。

The si command steps into function calls, meaning that GDB will pause the program at the first instruction of the called function. The ni command skips over them, meaning that GDB will pause the program at the next instruction following the call instruction (after the function executes and returns to the caller).

程序员可以用 print 命令加上以 $ 为前缀的寄存器名,打印机器寄存器中存放的值:

The programmer can print values stored in machine registers using the print command and the name of the register prefixed by $:

1
(gdb) print $eax    # print the value stored in register eax

display 命令会在到达断点时自动显示值:

The display command automatically displays values upon reaching a breakpoint:

1
2
(gdb) display $eax
(gdb) display $edx

info registers 命令显示所有机器寄存器中存放的值:

The info registers command shows all of the values stored in the machine registers:

1
(gdb) info registers

3.5.2. 用 DDD 在汇编层面调试

3.5.2. Using DDD to Debug at the Assembly Level

DDD 调试器在另一个调试器(这里是 GDB)之上提供了图形界面。它为显示汇编代码、查看寄存器和单步执行 IA32 指令提供了很好的界面。由于 DDD 有分别显示反汇编代码、寄存器值和 GDB 命令提示符的独立窗口,在汇编代码层面调试时,它往往比 GDB 更好用。

The DDD debugger provides a graphical interface on top of another debugger (GDB in this case). It provides a nice interface for displaying assembly code, viewing registers, and stepping through IA32 instruction execution. Because DDD has separate windows for displaying disassembled code, register values, and the GDB command prompt, it’s often easier to use than GDB when debugging at the assembly code level.

要用 DDD 调试,把 gdb 换成 ddd 即可:

To debug with DDD, substitute ddd for gdb:

1
$ ddd ./simpleops

GDB 提示符出现在底部窗口中,在那里可以输入 GDB 命令。虽然 DDD 为一些 GDB 命令提供了菜单选项和按钮,但使用底部的 GDB 提示符往往更方便。

The GDB prompt appears in the bottom window, where it accepts GDB commands at the prompt. Although it provides menu options and buttons for some GDB commands, often the GDB prompt at the bottom is easier to use.

选择 View → Machine Code Window 菜单选项,DDD 就会显示程序的汇编代码视图。这个选项会新建一个子窗口,列出程序的汇编代码(你可能会想把这个窗口拉大一些)。

DDD displays the assembly code view of a program by selecting the View → Machine Code Window menu option. That option creates a new subwindow with a listing of the program’s assembly code (you will likely want to resize this window to make it larger).

要在单独的窗口中查看程序所有寄存器的值,启用 Status → Registers 菜单选项。

To view all of the program’s register values in a separate window, enable the Status → Registers menu option.

3.5.3. GDB 汇编代码调试命令与示例

3.5.3. GDB Assembly Code Debugging Commands and Examples

下面是一些在汇编代码层面调试时有用的 GDB 命令的细节和示例(关于其中一些命令,特别是 print 和 x 的格式选项的更多细节,请参见常用 GDB 命令一节):

Here are some details and examples of GDB commands that are useful for debugging at the assembly code level (see the Common GDB Commands section for more details about some of these commands, particularly for the print and x formatting options):

  • disass:反汇编某个函数或某个地址范围内的代码。

    1
    2
    3
    4
    5
    disass <func_name>   # Lists assembly code for function
    disass <start> <end> # Lists assembly instructions between start & end address

    disass main # Disassemble main function
    disass 0x1234 0x1248 # Disassemble instructions between addr 0x1234 & 0x1248
  • disass: Disassemble code for a function or range of addresses.

    1
    2
    3
    4
    5
    disass <func_name>   # Lists assembly code for function
    disass <start> <end> # Lists assembly instructions between start & end address

    disass main # Disassemble main function
    disass 0x1234 0x1248 # Disassemble instructions between addr 0x1234 & 0x1248

译注

按 GDB 手册,指定地址范围时两个参数之间要用逗号隔开:disass 0x1234,0x1248(结束地址不包含在内),或者 disass 0x1234,+20 表示从起始地址开始的 20 个字节。上面代码中用空格分隔的写法 GDB 不认。GDB 手册:Machine Code

  • break:在某个指令地址处设置断点。

    1
    break *0x80dbef10  # Sets breakpoint at the instruction at address 0x80dbef10
  • break: Set a breakpoint at an instruction address.

    1
    break *0x80dbef10  # Sets breakpoint at the instruction at address 0x80dbef10
  • stepi(si)、nexti(ni):

    1
    2
    3
    4
    stepi, si          # Execute next machine code instruction,
    # stepping into function call if it is a call instr
    nexti, ni # Execute next machine code instruction,
    # treating function call as a single instruction
  • stepi (si), nexti (ni) :

    1
    2
    3
    4
    stepi, si          # Execute next machine code instruction,
    # stepping into function call if it is a call instr
    nexti, ni # Execute next machine code instruction,
    # treating function call as a single instruction
  • info registers:列出所有寄存器的值。
  • info registers: Lists all the register values.
  • print:显示一个表达式的值。

    1
    2
    print $eax                # Print the value stored in the eax register
    print *(int *)0x8ff4bc10 # Print int value stored at memory addr 0x8ff4bc10
  • print: Display the value of an expression.

    1
    2
    print $eax                # Print the value stored in the eax register
    print *(int *)0x8ff4bc10 # Print int value stored at memory addr 0x8ff4bc10
  • x:给定一个地址,显示该内存位置的内容。记住 x 的格式是粘性的,所以需要显式地改变它。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    (gdb) x $ebp-4      # Examine memory at address: (contents of register ebp)-4
    # if the location stores an address x/a, an int x/wd, ...

    (gdb) x/s 0x40062d # Examine the memory location 0x40062d as a string
    0x40062d "Hello There"

    (gdb) x/4c 0x40062d # Examine the first 4 char memory locations
    # starting at address 0x40062d
    0x40062d 72 'H' 101 'e' 108 'l' 108 'l'

    (gdb) x/d 0x40062d # Examine the memory location 0x40062d in decimal
    0x40062d 72 # NOTE: units is 1 byte, set by previous x/4c command

    (gdb) x/wd 0x400000 # Examine memory location 0x400000 as 4 bytes in decimal
    0x400000 100 # NOTE: units was 1 byte set, need to reset to w
  • x Display the contents of the memory location given an address. Remember that the format of x is sticky, so it needs to be explicitly changed.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    (gdb) x $ebp-4      # Examine memory at address: (contents of register ebp)-4
    # if the location stores an address x/a, an int x/wd, ...

    (gdb) x/s 0x40062d # Examine the memory location 0x40062d as a string
    0x40062d "Hello There"

    (gdb) x/4c 0x40062d # Examine the first 4 char memory locations
    # starting at address 0x40062d
    0x40062d 72 'H' 101 'e' 108 'l' 108 'l'

    (gdb) x/d 0x40062d # Examine the memory location 0x40062d in decimal
    0x40062d 72 # NOTE: units is 1 byte, set by previous x/4c command

    (gdb) x/wd 0x400000 # Examine memory location 0x400000 as 4 bytes in decimal
    0x400000 100 # NOTE: units was 1 byte set, need to reset to w
  • set:设置内存位置和寄存器的内容。

    1
    2
    3
    set $eax = 10                 Set the value of register eax to 10
    set $esp = $esp + 4 Pop a 4-byte value off the stack
    set *(int *)0x8ff4bc10 = 44 Store 44 at address 0x8ff4bc10
  • set: Set the contents of memory locations and registers.

    1
    2
    3
    set $eax = 10                 Set the value of register eax to 10
    set $esp = $esp + 4 Pop a 4-byte value off the stack
    set *(int *)0x8ff4bc10 = 44 Store 44 at address 0x8ff4bc10
  • display:每次遇到断点时打印一个表达式。

    1
    display $eax         Display value of register eax
  • display: Print an expression each time a breakpoint is hit.

    1
    display $eax         Display value of register eax

3.5.4. 汇编调试常用命令速查

3.5.4. Quick Summary of Common Commands for Assembly Debugging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ ddd ./a.out
(gdb) break main
(gdb) run

(gdb) disass main # Disassemble the main function
(gdb) break sum # Set a breakpoint at the beginning of a function
(gdb) cont # Continue execution of the program
(gdb) break *0x0804851a # Set a breakpoint at memory address 0x0804851a
(gdb) ni # Execute the next instruction
(gdb) si # Step into a function call (step instruction)
(gdb) info registers # List the register contents
(gdb) p $eax # Print the value stored in register %eax
(gdb) p *(int *)($ebp+8) # Print out value of an int at addr (%ebp+8)
(gdb) x/d $ebp+8 # Examine the contents of memory at the given
# address (/d: prints the value as an int)
(gdb) x/s 0x0800004 # Examine contents of memory at address as a string
(gdb) x/wd 0xff5634 # After x/s, the unit size is 1 byte, so if want
# to examine as an int specify both the width w & d