Can GDB call user-defined signal handler and still break on the code which threw a signal?

53 Views Asked by At

I have a Linux C/C++ application, including a custom signal handler which sends an email.

When I run without GDB and a signal is thrown, my signal handler is called.

However, when I run using GDB it obviously intercepts the signal and my handler is not called. I presume this is so it can show the backtrace etc.

Is it possible to have both? I'd like to see where the code died (and if possible, the interactive stack) but I'd still like my handler called, to send the email.

I tried this method:

#include <signal.h>
#include <unistd.h>

struct sigaction oldSA;
void handler(int signal)
{
    const char msg[] = "Caught, should dump core now\n";
    write(STDERR_FILENO, msg, sizeof msg - 1);

    sigaction(SIGSEGV, &oldSA, NULL);
}

int main()
{
    struct sigaction sa={0};
    sa.sa_handler=handler;
    sigaction(SIGSEGV, &sa, &oldSA);

    int* volatile p=NULL;
    *p=5; // cause segfault
}

https://stackoverflow.com/a/69988396/1107474

but unfortunately my handler works without GDB but doesn't get called when used with GDB.

0

There are 0 best solutions below