tags:

views:

63

answers:

2

Hi,

I have a faulty program that when execute receive a SIGSEGV.

I can use gdb like this:

$ gdb ./prog 

But I would prefer that gdb catch the SIGSEGV from prog and attach it automatically.

$ ./prog
Segmentation Fault
(gdb) ...

Is there a way to do that?

Thanks

+2  A: 

Well you can always create a core file and then analyse the callstack using gdb on that core. You can check out the man page for ulimit to do so.

Check this link for more info.

Praveen S
+4  A: 

Hmm. You can set up a signal handler to launch the debugger with the current process. That way you can inspect the whole state "live".

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

const char *prog=0;
void fn()
{

    char buf[256]; 
    snprintf(buf,255,"ddd %s %d",prog,getpid());
    system(buf);
}
int main(int argc, char **argv)
{
    prog=argv[0];
    signal(SIGSEGV,&fn);
    int *p=0; 
    int k=*p;
}
Luther Blissett
+1 good to know you can catch this signal, btw, you need an exit() after system() so it won't be called more than once.
miedwar
The prototype for fn needs an integer argument to get the code compiled.
Fanatic23