Debugging Child Processes.

Theo Deraadt deraadt at enme3.ucalgary.ca
Sun Aug 27 17:50:12 AEST 1989


In article <6840004 at hpcllcm.HP.COM> pratap at hpcllcm.HP.COM (Pratap Subrahmanyam) writes:
>I have been running into the following problem. I want to debug a child process
>and all the debugger's that we have here cannot do that. Does anyone know where
>such a debugger can be got ?
>
>main() {
>    ...
>    if((pid = fork()) == 0) {
>        /* How can you step into this area ???? */
>    }
>}

Under SunOS4.0 I can suggest one trick that I use when I have a lot of
weird things happening. It's only useful in a fork()ed child after you
do an exec().. I have not figured out a neat trick for doing things
there, although I guess I could start to look at ptrace()..

    char *args[10];

    switch( (child=fork()) {
    case -1:
	perror("fork");
	break;
    case 0:
	/* Sorry, I can't help you debug this part.... */
#if TRACE_DEBUG
	args[0] = "/bin/trace";
	args[1] = "-o";
	args[2] = "child.trace";
	args[3] = "child";
	args[4] = "child_argv[1]";
	args[5] = NULL;
#else
	args[0] = "child";
	args[1] = "child_argv[1]");
	args[2] = NULL;
#endif
	execv("/path/child", args);
	_exit(0);
	break;
    default:
	break;
    }

As the child runs (after the exec()), all the system calls it makes and
their parameters will be deposited in child.trace. Oh yeah, if you need
to get the value of a variable, do something stupid with it, like
	kill(-1, int);
	write(-1, string, strlen(string));
Then you can see what it is in the tracefile... Gad, I hope you have a Sun
on you.. this debugging has proved incredibly usefull in writing code that
played with weird stuff....
 <tdr.

Theo de Raadt                    (403) 289-5894     Calgary, Alberta, Canada



More information about the Comp.unix.wizards mailing list