home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!munnari.oz.au!metro!usage!spectrum!trevore
- From: trevore@spectrum.cs.unsw.oz.au (Trevor Robert Elbourne)
- Subject: help using ptrace
- Message-ID: <1992Aug20.045452.10878@usage.csd.unsw.OZ.AU>
- Sender: news@usage.csd.unsw.OZ.AU
- Nntp-Posting-Host: canescent.spectrum.cs.unsw.oz.au
- Reply-To: trevore@spectrum.cs.unsw.oz.au (Trevor Robert Elbourne)
- Organization: none
- Date: Thu, 20 Aug 1992 04:54:52 GMT
- Lines: 71
-
- Does anybody know how to use the ptrace system call successfully? I have tried with no success.
- I am trying to trace a child process after doing a fork(). After the fork I get the child to do
- a ptrace (TRACEME) and then generate a signal to stop it (using kill). The parent does a wait,
- when restarted the parent tries to trace the child, but it doesn't seem to work. Somehow the
- child is being killed or something. Here's what I tried:
-
- -------------------------------------------------------------------------------------------------
- #include <stdio.h>
- #include <ptrace.h>
- #include <signal.h>
- #include <sys/wait.h>
-
- extern int errno;
-
- main(int argc, char **argv, char **envp)
- {
- int pid;
- int x,count;
- union wait status;
- char buff[127];
-
- switch (pid = fork())
- {
- case 0 : /* child */
- if (ptrace(PTRACE_TRACEME, 0, NULL, 0, NULL, 0) < 0)
- {
- perror("child ptrace failed"); exit(2);
- }
-
- printf("child signals ... \n");
- fflush(stdout);
- kill(getpid(), SIGUSR1);
- x=0; /* something to trace */
- x=1;
- ++x;
- printf("child is done\n");
- exit(0);
-
- default : /* parent */
- if (wait(&status) != pid)
- {
- perror("wait failed"); exit(3);
- }
-
- printf("status: %08x\n", status);
- errno = 0;
- if (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0) < 0)
- {
- perror("parent attach ptrace failed"); exit(2);
- }
- while (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0)==0)
- ++count;
- perror("while done");
- printf("count: %u\n",count);
- break;
-
- case -1 : /* error */
- perror("fork failed");
- exit(1);
- }
-
- exit(0);
- }
- ---------------------------------------------------------------------------------------------------------------
-
- Can anyone help with this?
-
- Thanks in advance.
-
- Cheers
- -Trevor
-