Debugging programs with multiple processes

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.

However, if you want to debug the child process, there's a workaround that isn't too painful:

  1. Put a call to raise(SIGSTOP) in the code that the child process executes after the fork(). It may be useful to stop only if a certain environment variable is set, or a certain file exists, so that the child doesn't stop when you're running it without GDB.
  2. While the child is stopped, get its process ID by using the pidin utility (see the Utilities Reference) or by using GDB's info pidlist command.
  3. Tell GDB (a new invocation of GDB if you're also debugging the parent process) to attach to the child process (see "Debugging an already-running process"). From that point on you can debug the child process just like any other process that you've attached to.