Debugging programs with fork

Bruce A. Kern bak at csd-v.UUCP
Tue Oct 4 12:25:36 AEST 1988


In article <13819 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>In article <12523 at oberon.USC.EDU> pgarg at 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 <stdio.h>
#include <fcntl.h>

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 <stdio.h>
#include <fcntl.h>

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                         



More information about the Comp.unix.questions mailing list