This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Stepping over fork() turns the child into a zombie


I have a toy program that forks.  If I step over the call to fork, the
child immediately becomes a zombie.  If I set a breakpoint in the parent
code and continue past the call to fork, the child process is fine. 
Surprisingly enough, I didn't find anything in the mailing list archives
about this.  I'm running a freshly updated gdb, and

uname -a
Linux ironmaiden 2.2.17 #2 Fri Jan 19 13:12:40 CET 2001 i686 unknown

Here's an example gdb session:

GNU gdb 5.0
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) b main
Breakpoint 1 at 0x80484e6: file fork_test.c, line 10.
(gdb) r
Starting program: /home/orjanf/test_programs/fork_test 

Breakpoint 1, main (argc=1, argv=0xbffff774) at fork_test.c:10
10        pid = fork();
(gdb) n
12        if (pid == 0)
(gdb) shell ps
  PID TTY          TIME CMD
 9214 pts/15   00:00:00 zsh
 9304 pts/15   00:00:02 emacs
 9433 pts/15   00:00:00 gdb-HEAD
 9434 pts/15   00:00:00 fork_test
 9435 pts/15   00:00:00 fork_test <defunct>
 9436 pts/15   00:00:00 ps
(gdb) 

The parent process continues to execute as expected.

On a related note, is there a simple way to start debugging of the child
process?  I'm running in an embedded environment where I have a
gdbserver capable of attaching to a process by means of PTRACE_ATTACH. 
Ideally, I would like to take control of the child process by the time
it reaches main (or in the library startup routines).  I'd rather not
insert a sleep(30) in the beginning of the child's main function to
allocate enough time to start a new gdbserver and do the attach.  Any
ideas?


This is the program that eventually becomes the parent process:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (int argc, char **argv)
{
  pid_t pid;
  
  pid = fork();
  
  if (pid == 0)
  {
    execv ("child", 0);
    _exit (0);
  }
  
  /* In parent.  */
  while (1)
  {
    printf ("In parent...\n");
    sleep (1);
  }
  
  /* Wait for child.  */
  wait (0);
  return 0;
  
}


This is the child process:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (int argc, char **argv)
{
  while (1)
  {
    printf ("In child...\n");
    sleep (1);
  }
  return 0;
}

-- 
Orjan Friberg              E-mail: orjan.friberg@axis.com
Axis Communications AB     Phone:  +46 46 272 17 68


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]