GDB调试信号

GDB可以发送信号,拦截信号,方便地对信号进行调试。

捕获信号

GDB调试时,如果有信号产生,会切换到处理到信号的进程上去,十分影响调试。

在GDB中handle指令用于设置GDB对于信号的处理,可以输入help handle来查看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(gdb) help handle
Specify how to handle signals.
Usage: handle SIGNAL [ACTIONS]
Args are signals and actions to apply to those signals.
If no actions are specified, the current settings for the specified signals
will be displayed instead.

Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals
from 1-15 are allowed for compatibility with old versions of GDB.
Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).
The special arg "all" is recognized to mean all signals except those
used by the debugger, typically SIGTRAP and SIGINT.

Recognized actions include "stop", "nostop", "print", "noprint",
"pass", "nopass", "ignore", or "noignore".
Stop means reenter debugger if this signal happens (implies print).
Print means print a message if this signal happens.
Pass means let program see this signal; otherwise program doesn't know.
Ignore is a synonym for nopass and noignore is a synonym for pass.
Pass and Stop may be combined.

Multiple signals may be specified. Signal numbers and signal names
may be interspersed with actions, with the actions being performed for
all signals cumulatively specified.

可以看到用法为:handle SIGNAL [ACTIONS]

  • signal 参数表示要设定的目标信号,它通常为某个信号的全名(SIGINT)或者简称(去除‘SIG’后的部分,如 INT);如果要指定所有信号,可以用 all 表示。

  • ACTION 参数用于明确 GDB 处理该目标信息的方式,其值可以是如下几个:

    • nostop:当信号发生时,GDB 不会暂停程序,其可以继续执行,但会打印出一条提示信息,告诉我们信号已经发生;
    • stop:当信号发生时,GDB 会暂停程序执行。
    • noprint:当信号发生时,GDB 不会打印出任何提示信息;
    • print:当信号发生时,GDB 会打印出必要的提示信息;
    • nopass(或者 ignore):GDB 捕获目标信号的同时,不允许程序自行处理该信号;
    • pass(或者 noignore):GDB 调试在捕获目标信号的同时,也允许程序自动处理该信号。

上面的6项配置可划分成3组,具备相互叠加的状态集合 ,即:

  • stop/nostop
  • print/noprint
  • pass/nopass

查看信号的处理方式

GDB 调试器提供了info signals指令,用于查看 GDB 可以处理的信号种类,以及各个信号的具体处理方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
(gdb) info signals
Signal Stop Print Pass to program Description

SIGHUP Yes Yes Yes Hangup
SIGINT Yes Yes No Interrupt
SIGQUIT Yes Yes Yes Quit
SIGILL Yes Yes Yes Illegal instruction
SIGTRAP Yes Yes No Trace/breakpoint trap
SIGABRT Yes Yes Yes Aborted
SIGEMT Yes Yes Yes Emulation trap
SIGFPE Yes Yes Yes Arithmetic exception
SIGKILL Yes Yes Yes Killed
......

第一项(Signal):标示每个信号。
第二项(Stop):表示被调试的程序有对应的信号发生时,gdb是否会暂停程序。
第三项(Print):表示被调试的程序有对应的信号发生时,gdb是否会打印相关信息。
第四项(Pass to program):gdb是否会把这个信号发给被调试的程序。
第五项(Description):信号的描述信息。

发送信号

在GDB调试状态中,可以在命令号输入signal 信号来向程序发送信号。

作者

Desirer

发布于

2024-08-25

更新于

2024-08-25

许可协议