Path: utzoo!attcan!uunet!swlabs!csd-v!bak From: bak@csd-v.UUCP (Bruce A. Kern) Newsgroups: comp.unix.questions Subject: Re: Debugging programs with fork Message-ID: <175@csd-v.UUCP> Date: 4 Oct 88 02:25:36 GMT References: <12523@oberon.USC.EDU> <13819@mimsy.UUCP> Reply-To: bak@csd-v.UUCP (000-Bruce A. Kern) Organization: Computer Systems Design, Sandy Hook, Ct. Lines: 79 In article <13819@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >In article <12523@oberon.USC.EDU> pgarg@pollux.usc.edu (Pankaj K. Garg) writes: >> I am having trouble debugging programs which fork other >>processes. Dbx doesn't seem to handle them easily. > >You are quite right; the existing Unix debuggers cannot handle programs >that fork. SunOS 4.0 provides new facilities that make it possible, >but if you are dealing with older Unixes, you must either delete the >fork, or fall back on more `traditional' debugging schemes. >-- I'm not sure what Chris here means by 'traditional' schemes (print statements, etc.?). In the case where a executable is execed after the fork, I've had success by relpacing the execed program with an execed debugger as in the following example: /******************************************************************************/ /* Code for parent process */ #include #include void main( argc, argv ) int argc; char **argv; { fprintf( stderr, "Here is some output from the parent\n" ); switch( fork() ) { case -1: fprintf( stderr, "failed fork\n" ); exit( 1 ); case 0: fprintf( stderr, "Here is child output prior to the exec\n" ); # ifdef DEBUG execl( "/usr/bin/sdb", "/usr/bin/sdb", "kid", NULL ); # else execl( "kid", "kid", NULL ); # endif fprintf( stderr, "failed exec\n" ); exit( 2 ); default: fprintf( stderr, "more output from the parent\n" ); break; } } /******************************************************************************/ /* Code for child process */ #include #include void main( argc, argv ) int argc; char **argv; { fprintf( stderr, "this is the child\n" ); } /******************************************************************************/ In the case of purely forked code (but not execed) the above technique of course doesn't work. Also the above only allows debugging of the child, but sometimes that is what is required. -- Bruce A. Kern 1-203-270-0399 Computer Systems Design Voice: 730 - 1700 Mon. thru Fri. 29 High Rock Road Data: All other times Sandy Hook, Ct. 06482