说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C3-C_debug/gdb_commands.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
3.2. GDB Commands in Detail
本节列出常用的 GDB 命令,并通过例子展示它们的一些功能。我们首先讨论一些让 GDB 更好用的常用键盘快捷方式。
In this section, we list common GDB commands and show some of their features with examples. We first discuss some common keyboard shortcuts that make GDB even easier to use.
3.2.1. GDB 中的键盘快捷方式
3.2.1. Keyboard Shortcuts in GDB
GDB 支持命令行补全(command line completion)。用户可以输入某个命令的唯一前缀,然后按 TAB 键,GDB 会尝试补全命令行。此外,许多常用 GDB 命令都可以用唯一的简短缩写(short abbreviation)来输入。例如,要打印 x 的值,用户不必输入 print x,只需输入 p x;list 命令可以用 l,next 可以用 n。
GDB supports command line completion. A user can enter a unique prefix of a command and hit the
TABkey, and GDB will try to complete the command line. Also, a unique short abbreviation can be used to issue many common GDB commands. For example, rather than entering the commandprint x, a user can just enterp xto print out the value ofx, orlcan be used for thelistcommand, ornfornext.
上、下方向键可以翻看之前输入过的 GDB 命令行,省去每次重新输入的麻烦。
The up and down arrow keys scroll through previous GDB command lines, eliminating the need to retype them each time.
在 GDB 提示符下直接按 RETURN(回车)键,会执行最近一次输入的命令。在用一连串 next 或 step 命令单步执行时,这特别有用:只要按 RETURN,GDB 就会执行下一条指令。
Hitting the
RETURNkey at the GDB prompt executes the most recent previous command. This is particularly useful when stepping through the execution with a sequence ofnextorstepcommands; just pressRETURNand GDB executes the next instruction.
3.2.2. 常用 GDB 命令
3.2.2. Common GDB Commands
这里我们按功能相近的原则分组总结 GDB 最常用的命令:控制程序执行的命令;查看程序当前执行位置的命令;设置和控制断点的命令;以及打印程序状态和对表达式求值的命令。GDB 的 help 命令提供所有 GDB 命令的相关信息:
We summarize GDB’s most common commands here, grouping them by similar functionality: commands for controlling program execution; commands for evaluating the point in the program’s execution; commands for setting and controlling breakpoints; and commands for printing program state and evaluating expressions. The GDB
helpcommand provides information about all GDB commands:
**
help**:关于各个主题和 GDB 命令的帮助文档。1
2
3
4help <topic or command> Shows help available for topic or command
help breakpoints Shows help information about breakpoints
help print Shows help information about print command
help: Help documentation for topics and GDB commands.
1
2
3
4 help <topic or command> Shows help available for topic or command
help breakpoints Shows help information about breakpoints
help print Shows help information about print command
控制执行流程的命令
Commands for Execution Control Flow
**
break**:设置断点。1
2
3
4
5
6
7
8break <func-name> Set breakpoint at start of function <func-name>
break <line> Set breakpoint at line number <line>
break <filename:><line> Set breakpoint at <line> in file <filename>
break main Set breakpoint at beginning of main
break 13 Set breakpoint at line 13
break gofish.c:34 Set breakpoint at line 34 in gofish.c
break main.c:34 Set breakpoint at line 34 in main.c指定某个文件中的某一行(如
break gofish.c:34),使用户可以在由多个 C 源代码文件(.c 文件)组成的 C 程序中设置断点。当要设置的断点与程序暂停点处的代码不在同一个文件中时,这个功能特别有用。
break: Set a breakpoint.
1
2
3
4
5
6
7
8 break <func-name> Set breakpoint at start of function <func-name>
break <line> Set breakpoint at line number <line>
break <filename:><line> Set breakpoint at <line> in file <filename>
break main Set breakpoint at beginning of main
break 13 Set breakpoint at line 13
break gofish.c:34 Set breakpoint at line 34 in gofish.c
break main.c:34 Set breakpoint at line 34 in main.cSpecifying a line in a specific file (as in
break gofish.c:34) allows a user to set breakpoints in C programs that span several C source code files (.c files). This feature is particularly useful when the breakpoint being set is not in the same file as the code at the pause point of the program.
**
run**:从头开始运行被调试的程序。1
2
3
4run <command line arguments>
run Run with no command line arguments
run 2 40 100 Run with 3 command line arguments: 2, 40, 100
run: Start running the debugged program from the beginning.
1
2
3
4 run <command line arguments>
run Run with no command line arguments
run 2 40 100 Run with 3 command line arguments: 2, 40, 100
**
continue**(cont):从断点处继续执行1
continue
continue(cont): Continue execution from breakpoint
1 continue
**
step**(s):执行程序 C 源代码的下一行(或下几行),如果这些行中执行了函数调用,就进入该函数。1
2
3
4step Execute next line (stepping into a function)
step <count> Executes next <count> lines of program code
step 10 Executes the next 10 lines (stepping into functions)对于
step <count>命令,如果某一行含有函数调用,被调用函数中的代码行也会计入要单步执行的count行总数。因此,step <count>可能会让程序暂停在某个函数内部,而这个函数是从发出step <count>命令时的暂停点调用的。
step(s): Execute the next line(s) of the program’s C source code, stepping into a function if a function call is executed on the line(s).
1
2
3
4 step Execute next line (stepping into a function)
step <count> Executes next <count> lines of program code
step 10 Executes the next 10 lines (stepping into functions)In the case of the
step <count>command, if a line contains a function call, lines of the called function are counted in thecounttotal of lines to step through. Thus,step <count>may result in the program pausing inside a function that was called from the pause point at which thestep <count>command was issued.
**
next**(n):与step命令类似,但把函数调用当作一行来处理。换句话说,当下一条指令含有函数调用时,next不会进入函数内部执行,而是在函数调用返回后暂停程序(暂停在含有函数调用的那一行之后的下一行)。1
2next Execute the next line
next <count> Executes next <count> instructions
next(n): Similar to thestepcommand, but it treats a function call as a single line. In other words, when the next instruction contains a function call,nextdoes not step into the execution of the function but pauses the program after the function call returns (pausing the program at the next line in the code following the one with the function call).
1
2 next Execute the next line
next <count> Executes next <count> instructions
**
until**:执行程序,直到到达指定的源代码行号。1
until <line> Executes until hit line number <line>
until: Execute the program until it reaches the specified source code line number.
1 until <line> Executes until hit line number <line>
**
quit**:退出 GDB1
quit
quit: Exit GDB
1 quit
查看执行位置和列出程序代码的命令
Commands for Examining the Execution Point and Listing Program Code
**
list**:列出程序源代码。1
2
3
4
5
6list Lists next few lines of program source code
list <line> Lists lines around line number <line> of program
list <start> <end> Lists line numbers <start> through <end>
list <func-name> Lists lines around beginning of function <func-name>
list 30 100 List source code lines 30 to 100
list: List program source code.
1
2
3
4
5
6 list Lists next few lines of program source code
list <line> Lists lines around line number <line> of program
list <start> <end> Lists line numbers <start> through <end>
list <func-name> Lists lines around beginning of function <func-name>
list 30 100 List source code lines 30 to 100
**
where**(backtrace、bt):显示栈的内容(即程序执行到当前位置时的函数调用序列)。where命令有助于精确定位程序崩溃的位置,也有助于检查函数调用和返回之间的接口处的状态,比如传给函数的实参值。1
where
where(backtrace,bt): Show the contents of the stack (the sequence of function calls at the current point in the program’s execution). Thewherecommand is helpful for pinpointing the location of a program crash and for examining state at the interface between function calls and returns, such as argument values passed to functions.
1 where
frame<frame-num>:进入编号为 <frame-num> 的栈帧的上下文。默认情况下,程序暂停在第 0 帧(即栈顶的帧)的上下文中。可以用frame命令进入另一个栈帧的上下文。GDB 用户通常进入另一个栈帧,是为了打印另一个函数的参数和局部变量的值。1
2
3
4frame <frame-num> Sets current stack frame to <frame-num>
info frame Show state about current stack frame
frame 3 Move into stack frame 3's context (0 is top frame)
frame<frame-num>: Move into the context of stack frame number <frame-num>. As a default, the program is paused in the context of frame 0, the frame at the top of the stack. Theframecommand can be used to move into the context of another stack frame. Typically, GDB users move into another stack frame to print out the values of parameters and local variables of another function.
1
2
3
4 frame <frame-num> Sets current stack frame to <frame-num>
info frame Show state about current stack frame
frame 3 Move into stack frame 3's context (0 is top frame)
设置和管理断点的命令
Commands for Setting and Manipulating Breakpoints
**
break**:设置断点(上面的控制执行流程的命令一节对这个命令有更多说明)。1
2
3
4
5
6break <func-name> Set a breakpoint at start of a function
break <line> Set a breakpoint at a line number
break main Set a breakpoint at start of main
break 12 Set a breakpoint at line 12
break file.c:34 Set a breakpoint at line 34 of file.c
break: Set a breakpoint (there is more explanation about this command in Commands for execution control flow section above.)
1
2
3
4
5
6 break <func-name> Set a breakpoint at start of a function
break <line> Set a breakpoint at a line number
break main Set a breakpoint at start of main
break 12 Set a breakpoint at line 12
break file.c:34 Set a breakpoint at line 34 of file.c
**
enable、disable、ignore、delete、clear**:启用、禁用、在若干次内忽略、或删除一个或多个断点。delete命令按编号删除断点,而clear命令删除源代码中某个特定位置的断点。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15disable <bnums ...> Disable one or more breakpoints
enable <bnums ...> Enable one or more breakpoints
ignore <bpnum> <num> Don't pause at breakpoint <bpnum>
the next <num> times it's hit
delete <bpnum> Delete breakpoint number <bpnum>
delete Deletes all breakpoints
clear <line> Delete breakpoint at line <line>
clear <func-name> Delete breakpoint at function <func-name>
info break List breakpoint info (including breakpoint bnums)
disable 3 Disable breakpoint number 3
ignore 2 5 Ignore the next 5 times breakpoint 2 is hit
enable 3 Enable breakpoint number 3
delete 1 Delete breakpoint number 1
clear 124 Delete breakpoint at source code line 124
enable,disable,ignore,delete,clear: Enable, disable, ignore for some number of times, or delete one or more breakpoints. Thedeletecommand deletes a breakpoint by its number. In contrast, using theclearcommand deletes a breakpoint at a particular location in the source code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 disable <bnums ...> Disable one or more breakpoints
enable <bnums ...> Enable one or more breakpoints
ignore <bpnum> <num> Don't pause at breakpoint <bpnum>
the next <num> times it's hit
delete <bpnum> Delete breakpoint number <bpnum>
delete Deletes all breakpoints
clear <line> Delete breakpoint at line <line>
clear <func-name> Delete breakpoint at function <func-name>
info break List breakpoint info (including breakpoint bnums)
disable 3 Disable breakpoint number 3
ignore 2 5 Ignore the next 5 times breakpoint 2 is hit
enable 3 Enable breakpoint number 3
delete 1 Delete breakpoint number 1
clear 124 Delete breakpoint at source code line 124
**
condition**:给断点设置条件。条件断点只有在某个条件为真时才把控制权交给 GDB。它可以用来让循环内的断点只在若干次迭代之后才暂停(通过给循环计数变量加条件),或者只在某个变量取了调试时感兴趣的值时才让程序在断点处暂停(避免在其他时候暂停程序)。1
2
3
4
5
6
7
8
9condition <bpnum> <exp> Sets breakpoint number <bpnum> to break
only when expression <exp> is true
break 28 Set breakpoint at line 28 (in function play)
info break Lists information about all breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483a3 in play at gofish.c:28
condition 1 (i > 1000) Set condition on breakpoint 1
condition: Set conditions on breakpoints. A conditional breakpoint is one that only transfers control to GDB when a certain condition is true. It can be used to pause at a breakpoint inside a loop only after some number of iterations (by adding a condition on the loop counter variable), or to pause the program at a breakpoint only when the value of a variable has an interesting value for debugging purposes (avoiding pausing the program at other times).
1
2
3
4
5
6
7
8
9 condition <bpnum> <exp> Sets breakpoint number <bpnum> to break
only when expression <exp> is true
break 28 Set breakpoint at line 28 (in function play)
info break Lists information about all breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483a3 in play at gofish.c:28
condition 1 (i > 1000) Set condition on breakpoint 1
检查和计算程序状态与表达式的命令
Commands for Examining and Evaluating Program State and Expressions
**
print**(p):显示一个表达式的值。虽然 GDB 用户通常打印的是程序变量的值,但 GDB 可以打印任何 C 表达式的值(即使是程序代码中没有的表达式)。print 命令支持以不同格式打印,也支持不同数值表示形式的操作数。1
2
3
4print <exp> Display the value of expression <exp>
p i print the value of i
p i+3 print the value of (i+3)以不同格式打印:
1
2
3
4
5
6
7
8
9
10
11
12print <exp> Print value of the expression as unsigned int
print/x <exp> Print value of the expression in hexadecimal
print/t <exp> Print value of the expression in binary
print/d <exp> Print value of the expression as signed int
print/c <exp> Print ASCII value of the expression
print (int)<exp> Print value of the expression as unsigned int
print/x 123 Prints 0x7b
print/t 123 Print 1111011
print/d 0x1c Prints 28
print/c 99 Prints 'c'
print (int)'c' Prints 99在表达式中使用不同的数值表示形式(数字默认按十进制表示):
1
2
3
4
5
6
70x prefix for hex: 0x1c
0b prefix for binary: 0b101
print 0b101 Prints 5 (default format is decimal)
print 0b101 + 3 Prints 8
print 0x12 + 2 Prints 20 (hex 12 is 18 in decimal)
print/x 0x12 + 2 Prints 0x14 (decimal 20 in hexadecimal format)有时表达式可能需要显式的类型转换,告诉
print如何解释它们。例如,这里在对地址值解引用之前,必须先把它强制转换为某个具体类型(int *)(否则 GDB 不知道该如何对这个地址解引用):1
print *(int *)0x8ff4bc10 Print int value at address 0x8ff4bc10
用
print显示对指针变量解引用得到的值时,不需要类型转换,因为 GDB 知道指针变量的类型,也知道如何对它的值解引用。例如,如果ptr声明为int *,可以这样显示它所指向的 int 值:1
print *ptr Print the int value pointed to by ptr
打印存放在硬件寄存器中的值:
1
print $eax Print the value stored in the eax register
p): Display the value of an expression. Although GDB users typically print the value of a program variable, GDB will print the value of any C expression (even expressions that are not in the program code). The print command supports printing in different formats and supports operands in different numeric representations.
1
2
3
4 print <exp> Display the value of expression <exp>
p i print the value of i
p i+3 print the value of (i+3)To print in different formats:
1
2
3
4
5
6
7
8
9
10
11
12 print <exp> Print value of the expression as unsigned int
print/x <exp> Print value of the expression in hexadecimal
print/t <exp> Print value of the expression in binary
print/d <exp> Print value of the expression as signed int
print/c <exp> Print ASCII value of the expression
print (int)<exp> Print value of the expression as unsigned int
print/x 123 Prints 0x7b
print/t 123 Print 1111011
print/d 0x1c Prints 28
print/c 99 Prints 'c'
print (int)'c' Prints 99To specify different numeric representations in the expression (the default for numbers is decimal representation):
1
2
3
4
5
6
7 0x prefix for hex: 0x1c
0b prefix for binary: 0b101
print 0b101 Prints 5 (default format is decimal)
print 0b101 + 3 Prints 8
print 0x12 + 2 Prints 20 (hex 12 is 18 in decimal)
print/x 0x12 + 2 Prints 0x14 (decimal 20 in hexadecimal format)Sometimes, expressions may require explicit type casting to inform
int *) is necessary before the address can be dereferenced (otherwise, GDB does not know how to dereference the address):
1 print *(int *)0x8ff4bc10 Print int value at address 0x8ff4bc10When using
ptris declared as anint *, the int value it points to can be displayed like this:
1 print *ptr Print the int value pointed to by ptrTo print out a value stored in a hardware register:
1 print $eax Print the value stored in the eax register
译注
上面「以不同格式打印」的代码注释中有两处不准确:不带格式的 print <exp> 是按表达式本身的类型打印(int 就按有符号整数打印),并不是按 unsigned int;print (int)<exp> 是先转换成 int 再打印,即按有符号整数打印。另外 $eax 是 32 位 x86 的寄存器名;在 x86-64 上通常写 $rax(或其低 32 位 $eax),在 ARM64 上是 $x0 等。
**
display**:在到达断点时自动显示某个表达式的值。表达式的语法与print命令相同。1
2
3
4display <exp> Display value of <exp> at every breakpoint
display i
display array[i]
display: Automatically display the value of an expression upon reaching a breakpoint. The expression syntax is the same as the
1
2
3
4 display <exp> Display value of <exp> at every breakpoint
display i
display array[i]
**
x**(examine memory,检查内存):显示某个内存位置的内容。这个命令与print类似,但它把参数解释为一个地址值,对它解引用并打印该地址处存放的值。1
2
3
4
5
6x <memory address expression>
x 0x5678 Examine the contents of memory location 0x5678
x ptr Examine the contents of memory that ptr points to
x &temp Can specify the address of a variable
(this command is equivalent to: print temp)与
print一样,x可以用不同的格式显示值(例如作为int、char或字符串)。警示:x 命令的格式是「粘性」的
粘性格式(sticky formatting)是指 GDB 会记住当前的格式设置,并把它应用到之后没有指定格式的
x调用上。例如,如果用户输入了命令x/c,之后所有不带格式的x都会使用/c格式。因此,只有当用户想改变最近一次x调用的内存地址单位、重复次数或显示格式时,才需要在x命令中显式指定格式选项。一般来说,
x最多接受三个格式参数(x/nfu <memory address>),它们的书写顺序无关紧要:n:重复次数(一个正整数)
f:显示格式(s:字符串,i:指令,x:十六进制,d:十进制,t:二进制,a:地址,……)
u:单位格式(字节数)(b:1 字节,h:2 字节,w:4 字节,g:8 字节)
下面是一些例子(假设
s1 = "Hello There"位于内存地址0x40062d):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18x/d ptr Print value stored at what ptr points to, in decimal
x/a &ptr Print value stored at address of ptr, as an address
x/wx &temp Print 4-byte value at address of temp, in hexadecimal
x/10dh 0x1234 Print 10 short values starting at address 0x1234, in decimal
x/4c s1 Examine the first 4 chars in s1
0x40062d 72 'H' 101 'e' 108 'l' 108 'l'
x/s s1 Examine memory location associated with var s1 as a string
0x40062d "Hello There"
x/wd s1 Examine the memory location assoc with var s1 as an int
(because formatting is sticky, need to explicitly set
units to word (w) after x/s command sets units to byte)
0x40062d 72
x/8d s1 Examine ASCII values of the first 8 chars of s1
0x40062d: 72 101 108 108 111 32 84 104
x(examine memory): Display the contents of a memory location. This command is similar to
1
2
3
4
5
6 x <memory address expression>
x 0x5678 Examine the contents of memory location 0x5678
x ptr Examine the contents of memory that ptr points to
x &temp Can specify the address of a variable
(this command is equivalent to: print temp)Like
xcan display values in different formats (for example, as anint, achar, or a string).Warning: Examine’s Formatting is Sticky
Sticky formatting means that GDB remembers the current format setting, and applies it to subsequent calls to
xthat do not specify formatting. For example, if the user enters the commandx/c, all subsequent executions ofxwithout formatting will use the/cformat. As a result, formatting options only need to be explicitly specified with anxcommand when the user desires changes in the memory address units, repetition, or display format of the most recent call tox.In general,
xtakes up to three formatting arguments (x/nfu <memory address>); the order in which they are listed does not matter:1. n: the repeat count (a positive integer value)
2. f: the display format (s: string, i: instruction, x: hex, d: decimal, t: binary, a: address, …)
3. u: the units format (number of bytes) (b: byte, h: 2 bytes, w: 4 bytes, g: 8 bytes)
Here are some examples (assume
s1 = "Hello There"is at memory address0x40062d):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 x/d ptr Print value stored at what ptr points to, in decimal
x/a &ptr Print value stored at address of ptr, as an address
x/wx &temp Print 4-byte value at address of temp, in hexadecimal
x/10dh 0x1234 Print 10 short values starting at address 0x1234, in decimal
x/4c s1 Examine the first 4 chars in s1
0x40062d 72 'H' 101 'e' 108 'l' 108 'l'
x/s s1 Examine memory location associated with var s1 as a string
0x40062d "Hello There"
x/wd s1 Examine the memory location assoc with var s1 as an int
(because formatting is sticky, need to explicitly set
units to word (w) after x/s command sets units to byte)
0x40062d 72
x/8d s1 Examine ASCII values of the first 8 chars of s1
0x40062d: 72 101 108 108 111 32 84 104
**
whatis**:显示表达式的类型。1
2
3whatis <exp> Display the data type of an expression
whatis (x + 3.4) Displays: type = double
whatis: show the type of an expression.
1
2
3 whatis <exp> Display the data type of an expression
whatis (x + 3.4) Displays: type = double
**
set**:给程序变量赋值或修改其值,或者把一个值存放到特定的内存地址或特定的机器寄存器中。1
2
3set <variable> = <exp> Sets variable <variable> to expression <exp>
set x = 123 * y Set var x's value to (123 * y)
set: assign/change the value of a program variable, or assign a value to be stored at a specific memory address, or in a specific machine register.
1
2
3 set <variable> = <exp> Sets variable <variable> to expression <exp>
set x = 123 * y Set var x's value to (123 * y)
**
info**:列出关于程序状态和调试器状态的信息。info有大量选项,用来获取程序当前执行状态和调试器的相关信息。几个例子如下:1
2
3
4
5
6
7
8
9help info Shows all the info options
help status Lists more info and show commands
info locals Shows local variables in current stack frame
info args Shows the argument variable of current stack frame
info break Shows breakpoints
info frame Shows information about the current stack frame
info registers Shows register values
info breakpoints Shows the status of all breakpoints
info: lists information about program state and debugger state. There are a large number ofinfooptions for obtaining information about the program’s current execution state and about the debugger. A few examples include:
1
2
3
4
5
6
7
8
9 help info Shows all the info options
help status Lists more info and show commands
info locals Shows local variables in current stack frame
info args Shows the argument variable of current stack frame
info break Shows breakpoints
info frame Shows information about the current stack frame
info registers Shows register values
info breakpoints Shows the status of all breakpoints
关于这些以及其他 GDB 命令的更多信息,请参见 GDB 的手册页(man gdb)和 GNU 调试器主页。
For more information about these and other GDB commands, see the GDB man page (
man gdb) and the GNU Debugger homepage.