Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging child process after fork (follow-fork-mode child configured)

Tags:

c

fork

signals

gdb

I'm developing an application which the parent forks a child to handle certain tasks. I'm having an issue where I've configured gdb to follow-fork-mode child but after fork, after reaching a breakpoint, it sends a SIGTRAP but the child somehow terminates and send SIGCHLD to the parent.

I've configured signal(SIGTRAP, SIG_IGN) before fork so my understanding is that the child should inherit and ignore SIGTRAP when the breakpoint is reached but it's not happening.

Please help me to understand this if I'm incorrect.

How can I successfully debug the child process?

like image 738
ihsan Avatar asked Feb 28 '13 03:02

ihsan


People also ask

How do I debug child processes?

Once you install the power tool from the Visual Studio Gallery, a new menu item will appear on the “Debug” menu under the “Other Debug Targets” sub-menu. When you open the settings page, you'll see a checkbox to enable child process debugging.

What is follow-fork-mode?

show follow-fork-mode. Display the current debugger response to a fork or vfork call. If you ask to debug a child process and a vfork is followed by an exec , GDB executes the new target up to the first breakpoint in the new target.

What do you need to do to tell GDB to follow the child process after typing next?

By default, when a program forks, GDB will continue to debug the parent process and the child process will run unimpeded. If you want to follow the child process instead of the parent process, use the command set follow-fork-mode .


1 Answers

The child process inherits signal handlers from the parent, but not the pending signal.

After forking try installing the signal handler for SIGTRAP at a place in code where the child process executes after forking. If you don't handle SIGTRAP, the default action is that the child is terminated.

If you want to debug the child process, you must use follow-fork-mode. You must set the mode using

set follow-fork-mode child 

However, now only the child can be debugged, and the parent runs unchecked.

There is an alternative way of debugging the child process.

After fork() is executed, put a sleep() call in the code where the child executes, get the PID of the child using the ps utility, then attach the PID.

attach <PID of child process> 

Now, you can debug the child process, like any other process.

After debugging, you can detach the PID using

detach 
like image 110
Barath Ravikumar Avatar answered Oct 10 '22 03:10

Barath Ravikumar