开发者

How to debug programs using signals?

#include <stdio.h> 
#include <signal.h>

static volatile sig_atomic_t being_debugged = 1;
static void int3_handler(int signo) { being_debugged = 0; }

开发者_JS百科int main()
{
        signal(SIGTRAP, int3_handler);
        __asm__ __volatile__("int3");
        if (being_debugged) {
        puts("No, I don't want to serve you.");
                while (1) {
            /* endless loop */ ;
        }
        }
        puts("Yes, real routines go here.");
        return 0;
}

The above will give different output when run inside/outside gdb,because gdb captures the sigtrap signal.

How to make my program behaves the same in gdb?


GDB will stop the inferior (being debugged) program when the inferior receives any signal.

If you simply continue from GDB, the signal will be "swallowed", which is not what you want.

You can ask GDB to continue the program and send it a signal with signal SIGTRAP.

You can also ask GDB to pass a given signal directly to the inferior, and not stop at all with handle SIGTRAP nostop noprint pass GDB command. You'll need to do that before you hit the first SIGTRAP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜