Search code examples
cgdbforkdebug-backtrace

GDB backtrace on child process


I have a application which forks a child process.

Child process does some work and somewhere in the middle it gives Segmentation fault. I used GDB to debug this, I used:

set follow-fork-mode child

I have also set a breakpoint to a function within the child. But GDB doesn't pause at my breakpoint.

Also the parent process handles the seg-fault so I had to ctrl-c to exit. Then when I use backtrace to print the stack all I got is

No stack

Why is the breakpoint not being set and why didn't I get the stack?


Solution

  • Why is the breakpoint not being set

    The breakpoint is being set, but it is not being hit because ...

    and why didn't I get the stack?

    ... you are apparently debugging the wrong process.

    With set follow-fork-mode child, GDB will follow the first child you create. Perhaps you create more than one?

    One way to debug this is to establish a SIGSEGV handler using signal or sigaction.

    In the handler, do this:

    void handler(int signo)
    {
      int i = 1;
      fprintf(stderr, "pid=%d, got signal=%d\n", getpid(), signo);
      while (i) { }
    }
    

    Once you see the message printed, in another window:

     gdb /proc/<pid>/exe <pid>
     (gdb) where