title: GDB: debugging a child process after many fork()s
body: <p>I'm debugging a program which <em>repeats</em> the typical procedure of using <code>fork()</code> where the child process does some delegated task as the parent calls <code>waitpid()</code> to wait for the child to finish and then continues. For example:</p>
  
  <pre><code>while (!finished) {
      pid_t p = fork();
      if (p &lt; 0) {
          perror("fork");
          exit(EXIT_FAILURE);
      }
      else if (p == 0) {
          /* Do something. For example: */
          if (/* some (rare) condition */) 
              raise(SIGSEGV);
          exit(EXIT_SUCCESS);
      }
      else {
          int status;
          pid_t w = waitpid(p, &amp;status, 0);
          if (w &lt; 0) {
              perror("waitpid");
              exit(EXIT_FAILURE);
          }
          /* Do something. */
      }
  }
  </code></pre>
  
  <p>I'd like to run the program inside GDB and debug a child process that receives a signal, <em>no matter how many other child processes successfully finished and vanished before it</em>.</p>
  
  <p>Of course, I need <code>set follow-fork-mode child</code>, because otherwise GDB won't look into the child processes. But this alone will detach the parent, lead GDB to the first child, and finish debugging upon its exit.</p>
  
  <p>Therefore, I also need <code>set detach-on-fork off</code> to prevent the parent from being detached. However, this causes GDB to stop and give me a prompt, with the parent being suspended, when the first child exits. I could use <code>inferior</code> to choose the parent and issue <code>continue</code>, but <em>I need to do this for every child</em>. If the 1000th child is the one that receives the signal and that I want to look into, I need to repeat this process 999 times.</p>
  
  <p>So I'm wondering if there is a way to, kind of, do this automatically and let GDB follow what the CPU would be executing, e.g. parent → child1 → parent → child2 → … , without stopping and prompting me, and stop at the first childN that receives a signal.</p>