Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug programs using signals?

Tags:

signals

gdb

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

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

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?

like image 744
compile-fan Avatar asked May 15 '11 11:05

compile-fan


1 Answers

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.

like image 95
Employed Russian Avatar answered Oct 03 '22 16:10

Employed Russian