tags:

views:

1094

answers:

2

I have a C linux application (A) that spawns another process (P) when it is started. When I want to debug P I start A as usual and I connect with ddd/gdb to P.

Problems appear when I want to debug the entry-point (start of main) of P. If I follow the usual approach when I connect the debugger to P is already to late. The solution I've found was to insert a sleep at the begining of the main of P so I have time to connect with gdb but this is not a very elegant solution.

I've also tried using asm("int $3") but it doesn't seems to work.

Do you have any idea how I could solve this problem? (preferably without altering the code of A or P)

A: 

You should be able to do this by making use of gdb's remote debugging features, specifically gdbserver. In effect, launch (P) using gdbserver. These links have more detailed info:

codelogic
gdbserver allows remote debugging but does not solve the problem at hand, which is more a case of how GDB follows fork/clone.
Phillip Whelan
I don't think this is a case for remote debugging. It's more about which process gdb follows on a fork.
Nathan Fellman
+9  A: 

You should use this option:

set follow-fork-mode mode

Where mode is one of parent, child or ask.

To follow the parent (this is the default) use:

set follow-fork mode parent

To follow the child:

set follow-fork mode child

To have the debugger ask you each time:

set follow-fork mode ask

So basically you'd start out connecting gdb to A, then set gdb to follow the child, and then when A spawns P, gdb will connect to P and detach from A.

Nathan Fellman