开发者

How to debug programs using signals?

开发者 https://www.devze.com 2023-03-06 05:33 出处:网络
#include <stdio.h> #include <signal.h> static volatile sig_atomic_t being_debugged = 1; static void int3_handler(int signo) { being_debugged = 0; }
#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

精彩评论

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