46

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?

jww
  • 97,681
  • 90
  • 411
  • 885
ihsan
  • 551
  • 1
  • 5
  • 10
  • 2
    "*... after reaching a breakpoint, it sends a SIGTRAP ...*" that's the way the debugger is notified about the breakpoint. "*... the child somehow terminates ...*" terminates normally or abnormally? You can use `WIFEXITED(status)` on the value returned by `wait[...]([...,]&status[, ...])` in the parent to check this. See `man 3 wait` for details. – alk Feb 28 '13 at 07:01

1 Answers1

104

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
malat
  • 12,152
  • 13
  • 89
  • 158
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • `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.` - Wouldn't attaching to the child process will again lead to `sleep()` execution? Pls correct me If I am wrong. – Yug Singh Feb 09 '20 at 18:43
  • @YugSingh no, you need to put the sleep() just in the child code using a construction like this: `switch (fork()) { case -1: /* fork error code */ case 0: sleep(30); /* and the rest of child code */ default: /* this is the parent code */ }` . – jyz Mar 19 '20 at 20:40