#include <stdio.h>
#include <signal.h>
void handler(int sig);
void handler(int sig)
{
signal(sig, handler);
printf("Receive signal: %d\n", sig);
}
int main(void) {
signal(SIGHUP, handler);
while (1)
{
sleep(1);
}
return 0;
}
用gdb調試程序時,可以用“handle signal pass(noignore)/nopass(ignore)
”命令設置當信號發生時,是否把信號丟給程序處理.其中pass
和noignore
含義相同,nopass
和ignore
含義相同。以上面程序為例:
(gdb) i signals
Signal Stop Print Pass to program Description
SIGHUP Yes Yes Yes Hangup
......
(gdb) r
Starting program: /data1/nan/test
[Thread debugging using libthread_db enabled]
[New Thread 1 (LWP 1)]
Program received signal SIGHUP, Hangup.
[Switching to Thread 1 (LWP 1)]
0xfeeeae55 in ___nanosleep () from /lib/libc.so.1
(gdb) c
Continuing.
Receive signal: 1
可以看到,默認情況下,發生SIGHUP
信號時,gdb會把信號丟給程序處理。
接下來用“handle SIGHUP nopass
”命令設置當SIGHUP
信號發生時,gdb不把信號丟給程序處理,執行如下:
(gdb) handle SIGHUP nopass
Signal Stop Print Pass to program Description
SIGHUP Yes Yes No Hangup
(gdb) c
Continuing.
Program received signal SIGHUP, Hangup.
0xfeeeae55 in ___nanosleep () from /lib/libc.so.1
(gdb) c
Continuing.
可以看到,SIGHUP
信號發生時,程序沒有打印“Receive signal: 1”,說明gdb沒有把信號丟給程序處理。
如果想恢復之前的行為,用“handle SIGHUP pass
”命令即可。
參見gdb手冊.
nanxiao