8

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?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
broun
  • 2,483
  • 5
  • 40
  • 55
  • Is the child program written by you ? – sirgeorge Mar 04 '12 at 05:00
  • no, but i do have the source with me. – broun Mar 04 '12 at 05:09
  • Can you build it (the child) from source in debug mode ? – sirgeorge Mar 04 '12 at 05:12
  • It is a open source application that I am using and I am not able to figure out where exactly in the make files i should change it. I guess you are suggesting "pg" option for gprof? I have dev team regarding it and awaiting the response. – broun Mar 04 '12 at 05:35
  • 1
    GNU `gprof` is for profiling, not debugging. I'm (still) assuming you want to find out where you child process dies and not to profile it. To rebuild it in debug mode you should do `export CFLAGS=-g` in your shell (bash) before you do `configure` (I am assuming you know how to build programs from sources). I did a lot of debugging of child processes, and I cannot recall even one situation when `follow-fork-mode` in gdb worked as expected without giving me a headache, so I am trying to provide you with a workaround. – sirgeorge Mar 04 '12 at 05:44
  • Yes, I know how to build it from source. So now I should export the CFLAGS and then do the configure and make ? – broun Mar 04 '12 at 05:50
  • I have built the source code as you said. Pls let me know what I should do further.. – broun Mar 04 '12 at 07:22
  • 1
    1) find the 'main' function of your child program and add infinite loop to it: volatile int iHang=1; while(iHang); 2) build it again – sirgeorge Mar 04 '12 at 07:26
  • If you are fast enough you could also attach to the child after it has started using `gdb -pid `. – alk Mar 04 '12 at 08:22
  • @sirgeorge - I really appreciate you helping out with this. I ran the child and It goes into infinite loop as expected. Also I started gdb with the childs PID attached and it showed the trace upto the infinite loop and came back to the gdb> prompt while the loop is still running. – broun Mar 04 '12 at 09:27
  • 1
    Attach to the child running in infinite loop: gdb -p . And then in gdb: set iHang=0. And (also in gdb): handle SIGSEGV stop. That should enable you to be able to see exactly when your child is crashing. – sirgeorge Mar 04 '12 at 09:31
  • Thank you, I am now able to find where its occurring!! – broun Mar 04 '12 at 10:13

1 Answers1

12

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
Employed Russian
  • 199,314
  • 34
  • 295
  • 362