我正在开发一个包含大量文件的C项目。现在我想找到信号处理函数,但在浏览项目树时没有成功。
我认为解决此问题的第一种方法是使用GDB运行二进制文件。
有没有办法可以在收到信号(例如Ctrl-C
)后立即要求GDB中断?
答案 0 :(得分:3)
以下是一些参考:
http://nirbhay.in/2012/09/debug-signal-handlers-using-gdb/
让我们对这个程序做一些实验:
/*
@file : sig.c
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void signalhandler(int signum) {
printf("\n SIGINT caught : %d", signum);
}
int main() {
signal(SIGINT, signalhandler);
while (1) {
printf("\n looping : inside main()");
sleep(1);
}
}
在这种情况下,您可以这样做:
(gdb) handle SIGINT stop pass
之后,您前进以获取信号处理函数。我得到了:
$ gdb ./a
...
(gdb) handle SIGINT stop pass
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y
Signal Stop Print Pass to program Description
SIGINT Yes Yes Yes Interrupt
(gdb) r
Starting program: /home/arc/a
looping : inside main()
looping : inside main()
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7aef900 in __nanosleep_nocancel () from /usr/lib/libc.so.6
(gdb) s
Single stepping until exit from function __nanosleep_nocancel,
which has no line number information.
0x0000000000400596 in signalhandler(int) ()