GDB has no special support for debugging programs that create additional processes using the fork() function. When a program forks, GDB continues to debug the parent process, and the child process runs unimpeded. If you’ve set a breakpoint in any code that the child then executes, the child gets a SIGTRAP signal, which (unless it catches the signal) causes it to terminate.

If you want to debug the child process, then follow these steps:

  1. Put a call to sleep() in the code that the child process executes after the fork(). It may be useful to sleep only if a certain environment variable is set, or a certain file exists, so that the delay doesn’t occur when you don’t want to run GDB on the child.
  2. While the child is sleeping, get its process ID by using the ps command utility or by using GDB’s info pidlist command.
  3. After finding pid of child process or parent process you can attach any process ( parent and child) through  “gdb –attach <pid>”. From that point on you can debug the attached process just like any other process.

Related Contents to follow