说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C3-C_debug/gdb_advanced.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
3.4. Advanced GDB Features
本节介绍 GDB 的高级功能,其中一些可能要在读过操作系统一章之后才好理解。
This section presents advanced GDB features, some of which may make sense only after reading the Operating Systems chapter.
3.4.1. GDB 与 make
3.4.1. GDB and make
GDB 支持在调试会话中用 make 命令重新构建可执行文件;如果构建成功,(在输入 run 命令时)它会运行新构建出的程序。
GDB accepts the
makecommand to rebuild an executable during a debugging session, and if the build is successful it will run the newly built program (when issued theruncommand).
1 | (gdb) make |
如果用户设置了很多断点、已经修复了一个缺陷但还想继续调试会话,在 GDB 内部构建就很方便。这种情况下,GDB 用户不必退出 GDB、重新编译、用新的可执行文件重新启动 GDB 并重新设置所有断点,而只需运行 make,就能在所有断点仍然有效的情况下开始调试新版本的程序。不过要记住,如果源代码中增加或删除了行,修改 C 源代码并在 GDB 内运行 make 重新编译后,断点在新版本程序中的逻辑位置可能与旧版本中的不同。出现这个问题时,要么退出 GDB 并对新的可执行文件重新启动 GDB 会话,要么用 disable 或 delete 禁用或删除旧断点,再用 break 在新编译版本程序中的正确位置设置新的断点。
Building from within GDB is convenient for a user who has set many breakpoints and has fixed one bug but wants to continue the debugging session. In this case, rather than quitting GDB, recompiling, restarting GDB with the new executable, and resetting all the breakpoints, a GDB user can run
makeand start debugging the new version of the program with all the breakpoints still set. Keep in mind, however, that modifying the C source and recompiling by runningmakefrom within GDB may result in the breakpoints not being at the same logical location in the new version of the program as in the old version if source code lines have been added or deleted. When this problem occurs, either exit GDB and restart the GDB session on the new executable, or usedisableordeleteto disable or delete the old breakpoints and thenbreakto set new breakpoints at the correct locations in the newly compiled version of the program.
3.4.2. 把 GDB 附加到正在运行的进程
3.4.2. Attaching GDB to a Running Process
GDB 支持调试已经在运行的程序(而不是在 GDB 会话中启动程序),方法是把 GDB 附加(attach)到正在运行的进程上。为此,用户需要先得到进程 ID(PID)的值:
GDB supports debugging a program that is already running (rather than starting a program to run from within a GDB session) by attaching GDB to a running process. To do this, the user needs to get the process ID (PID) value:
用 shell 命令
ps获取进程的 PID:1
2
3
4
5
6
7# ps to get process's PID (lists all processes started in current shell):
$ ps
# list all processes and pipe through grep for just those named a.out:
$ ps -A | grep a.out
PID TTY TIME CMD
12345 pts/3 00:00:00 a.out
Get the process’s PID using the
psshell command:
1
2
3
4
5
6
7 # ps to get process's PID (lists all processes started in current shell):
$ ps
# list all processes and pipe through grep for just those named a.out:
$ ps -A | grep a.out
PID TTY TIME CMD
12345 pts/3 00:00:00 a.out
启动 GDB,并把它附加到特定的运行中进程(PID 为 12345):
1
2
3
4
5
6
7# gdb <executable> <pid>
$ gdb a.out 12345
(gdb)
# OR alternative syntax: gdb attach <pid> <executable>
$ gdb attach 12345 a.out
(gdb)
Start GDB and attach it to the specific running process (with PID 12345):
1
2
3
4
5
6
7 # gdb <executable> <pid>
$ gdb a.out 12345
(gdb)
# OR alternative syntax: gdb attach <pid> <executable>
$ gdb attach 12345 a.out
(gdb)
译注
- 上面的「替代语法」
gdb attach 12345 a.out并不是 GDB 手册中的写法(GDB 会把attach当成程序文件名)。手册给出的两种写法是gdb a.out 12345和gdb -p 12345;也可以先启动gdb a.out,再在 GDB 提示符下输入attach 12345。GDB 手册:Invoking GDB - 在 Ubuntu 等启用了 Yama 安全模块的发行版上,
kernel.yama.ptrace_scope默认为 1:普通用户只能附加到自己的子孙进程,附加到其他已在运行的进程会报Operation not permitted。这时需要用sudo运行 GDB,或临时把该值改为 0。Linux 内核文档:Yama
把 GDB 附加到一个进程会让该进程暂停,用户可以在它继续执行之前输入 GDB 命令。
Attaching GDB to a process pauses it, and the user can issue GDB commands before continuing its execution.
另一种做法是,程序可以调用 kill(getpid(), SIGSTOP) 显式地让自己暂停,等待调试(如 attach_example.c 示例所示)。程序在这个位置暂停时,程序员就可以把 GDB 附加到这个进程上进行调试。
Alternatively, a program can explicitly pause itself to wait for debugging by calling
kill(getpid(), SIGSTOP)(as in the attach_example.c example). When the program pauses at this point, a programmer can attach GDB to the process to debug it.
无论程序是如何暂停的,在 GDB 附加上去、用户输入了一些 GDB 命令之后,都可以用 cont 让程序从附加点继续执行。如果 cont 不起作用,GDB 可能需要显式地向进程发送一个 SIGCONT 信号,让它继续执行:
Regardless of how a program pauses, after GDB attaches and the user enters some GDB commands, the program’s execution continues from its attach point using
cont. Ifcontdoesn’t work, GDB may need to explicitly send the process aSIGCONTsignal in order to continue its execution:
1 | (gdb) signal SIGCONT |
3.4.3. 在 fork 时跟随进程
3.4.3. Following a Process on a Fork
当 GDB 调试的程序调用 fork() 函数创建一个新的子进程时,可以设置 GDB 跟随(调试)父进程或子进程,另一个进程的执行则不受 GDB 影响。默认情况下,调用 fork() 之后 GDB 跟随父进程。要让 GDB 改为跟随子进程,使用 set follow-fork-mode 命令:
When GDB debugs a program that calls the
fork()function to create a new child process, GDB can be set to follow (to debug) either the parent process or the child process, leaving the execution of the other process unaffected by GDB. By default, GDB follows the parent after a call tofork(). To set GDB to follow the child process, instead, use theset follow-fork-modecommand:
1 | (gdb) set follow-fork-mode child # Set gdb to follow child on fork |
如果用户想在 GDB 会话过程中改变这一行为,在程序中的 fork() 调用处设置断点会很有用。
Setting breakpoints at
fork()calls in the program is useful when the user wants to change this behavior during a GDB session.
attach_example.c 示例展示了在 fork 时「跟随」两个进程的一种方法:fork 之后 GDB 跟随父进程,而子进程在 fork 之后给自己发送一个 SIGSTOP 信号显式地暂停,让程序员可以在它继续执行之前,把第二个 GDB 进程附加到子进程上。
The attach_example.c example shows one way to “follow” both processes on a fork: GDB follows the parent process after the fork, and the child sends itself a
SIGSTOPsignal to explicitly pause after the fork, allowing the programmer to attach a second GDB process to the child before it continues.
3.4.4. 信号控制
3.4.4. Signal Control
GDB 进程可以向它正在调试的目标进程发送信号,也可以处理目标进程收到的信号。
The GDB process can send signals to the target process it is debugging and can handle signals received by the target process.
GDB 可以用 signal 命令向它调试的进程发送信号:
GDB can send signals to the process it debugs using the
signalcommand:
译注
下面代码中的 SIGALARM 是笔误,闹钟信号的正确名称是 SIGALRM(下文的代码里写对了)。
1 | (gdb) signal SIGCONT |
有时用户希望在被调试的进程收到某个信号时,GDB 执行某个动作。例如,如果程序试图用一个对所访问类型而言未对齐的内存地址访问内存,它会收到 SIGBUS 信号,通常会因此退出。GDB 对 SIGBUS 的默认行为也是让进程退出。但是,如果你想让 GDB 在程序收到 SIGBUS 时检查程序状态,可以用 handle 命令指定 GDB 以不同方式处理 SIGBUS 信号(info 命令可以显示更多关于 GDB 在调试过程中如何处理进程所收信号的信息):
Sometimes a user would like GDB to perform some action when a signal is received by the debugged process. For example, if a program tries to access memory with a misaligned memory address for the type it is accessing, it receives a
SIGBUSsignal and usually exits. The default behavior of GDB on aSIGBUSis also to let the process exit. If, however, you want GDB to examine the program state when it receives aSIGBUS, you can specify that GDB handle theSIGBUSsignal differently using thehandlecommand (theinfocommand shows additional information about how GDB handles signals received by the process during debugging):
译注
在 x86 上,绝大多数未对齐访问其实由硬件自动处理,并不会产生 SIGBUS;在 Linux 上,SIGBUS 更常见的触发原因是访问了已被截断的 mmap 文件映射之外的区域。原书的例子说的是在严格要求对齐的体系结构上的情形。
1 | (gdb) handle SIGBUS stop # if program gets a SIGBUS, gdb gets control |
3.4.5. DDD 的设置与问题修复
3.4.5. DDD Settings and Bug Fixes
运行 DDD 会在你的主目录下创建一个 .ddd 目录,它用这个目录保存设置,这样用户就不必在每次启动时从头重新设置所有偏好。保存的设置包括子窗口的大小、菜单显示选项,以及是否打开查看寄存器值和汇编代码的窗口等。
Running DDD creates a
.ddddirectory in your home directory, which it uses to store its settings so that users don’t need to reset all their preferences from scratch on each invocation. Some examples of saved settings include sizes of subwindows, menu display options, and enabling windows to view register values and assembly code.
有时 DDD 在启动时会卡住,显示 “Waiting until GDB ready”(等待 GDB 就绪)的消息。这通常表明它保存的设置文件出了错。最简单的修复方法是删除 .ddd 目录(你会丢失所有保存的设置,需要在它再次启动时重新设置):
Sometimes DDD hangs on startup with a “Waiting until GDB ready” message. This often indicates an error in its saved settings files. The easiest way to fix this is remove the
.ddddirectory (you will lose all your saved settings and need to reset them when it starts up again):
1 | $ rm -rf ~/.ddd # Be careful when entering this command! |