home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.wizards
- Path: sparky!uunet!munnari.oz.au!metro!usage!trevore
- From: trevore@vast.unsw.edu.au (Trevor Elbourne (Supr. Hell) BE)
- Subject: help with ptrace
- Message-ID: <1992Sep1.022202.10547@usage.csd.unsw.OZ.AU>
- Sender: news@usage.csd.unsw.OZ.AU
- Nntp-Posting-Host: oyster.vast.unsw.edu.au
- Organization: University of New South Wales
- X-Newsreader: Tin 1.1 PL4
- Date: Tue, 1 Sep 1992 02:22:02 GMT
- Lines: 78
-
-
- 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 an
- ptrace(TRACE_ME,.....) and then an execve. Because the child does
- the ptrace(TRACE_ME,...) call the subsequent execve will cause a
- signal to be sent to the parent (0177), which does a wait. The child
- should then be stopped and the parent should now be able to control
- the child via ptrace (single stepping it, examining its registers
- .. etc).
-
- In my case the parent recieves the 0177 signal ok and the first attemp
- to use ptrace(SINGLE_STEP,...) works fine, but any subsequent attempt
- to single step the child fails with the error: no such process. Can anyone
- please help me? Anyway here's the test program 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);
- ++argv;
- execve(argv[0],argv,envp); /* process to trace */
- 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);
- }
- /* single step child until its done */
- while (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0)==0)
- ++count;
- printf("count: %u\n",count);
- break;
-
- case -1 : /* error */
- perror("fork failed");
- exit(1);
- }
-
- exit(0);
- }
- -----------------------------------------------------------------------------
-
- Thanks in advance.
-
- Cheers
- -Trevor
-
-
-