home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / wizards / 3756 < prev    next >
Encoding:
Text File  |  1992-08-31  |  2.4 KB  |  91 lines

  1. Newsgroups: comp.unix.wizards
  2. Path: sparky!uunet!munnari.oz.au!metro!usage!trevore
  3. From: trevore@vast.unsw.edu.au (Trevor Elbourne (Supr. Hell) BE)
  4. Subject: help with ptrace
  5. Message-ID: <1992Sep1.022202.10547@usage.csd.unsw.OZ.AU>
  6. Sender: news@usage.csd.unsw.OZ.AU
  7. Nntp-Posting-Host: oyster.vast.unsw.edu.au
  8. Organization: University of New South Wales
  9. X-Newsreader: Tin 1.1 PL4
  10. Date: Tue, 1 Sep 1992 02:22:02 GMT
  11. Lines: 78
  12.  
  13.  
  14. Does anybody know how to use the ptrace system call successfully? 
  15. I have tried with no success. I am trying to trace a child process 
  16. after doing a fork(). After the fork I get the child to do an
  17. ptrace(TRACE_ME,.....) and then an execve. Because the child does
  18. the ptrace(TRACE_ME,...) call the subsequent execve will cause a 
  19. signal to be sent to the parent (0177), which does a wait. The child
  20. should then be stopped and the parent should now be able to control
  21. the child via ptrace (single stepping it, examining its registers
  22. .. etc).
  23.  
  24. In my case the parent recieves the 0177 signal ok and the first attemp
  25. to use ptrace(SINGLE_STEP,...) works fine, but any subsequent attempt
  26. to single step the child fails with the error: no such process. Can anyone
  27. please help me? Anyway here's the test program I tried;
  28.  
  29. -------------------------------------------------------------------------
  30. #include    <stdio.h>
  31. #include    <ptrace.h>
  32. #include    <signal.h>
  33. #include    <sys/wait.h>
  34.  
  35. extern    int    errno;
  36.  
  37. main(int argc, char **argv, char **envp)
  38. {
  39.     int        pid;
  40.     int        x,count;
  41.     union wait    status;                  
  42.     char        buff[127];
  43.  
  44.     switch (pid = fork())
  45.     {
  46.     case 0     : /* child */
  47.         if (ptrace(PTRACE_TRACEME, 0, NULL, 0, NULL, 0) < 0)
  48.         {
  49.             perror("child ptrace failed"); exit(2);
  50.         }
  51.  
  52.         printf("child signals ... \n");
  53.         fflush(stdout);
  54.         ++argv;
  55.         execve(argv[0],argv,envp); /* process to trace */
  56.         exit(0);
  57.  
  58.     default    : /* parent */
  59.         if (wait(&status) != pid)
  60.         {
  61.             perror("wait failed"); exit(3);
  62.         }
  63.  
  64.         printf("status: %08x\n", status);
  65.         errno = 0;
  66.         if (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0) < 0)
  67.         {
  68.             perror("parent attach ptrace failed"); exit(2);
  69.         }
  70.         /* single step child until its done */
  71.         while (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0)==0)
  72.             ++count;
  73.         printf("count: %u\n",count);
  74.         break;
  75.  
  76.     case -1    : /* error */
  77.         perror("fork failed");
  78.         exit(1);
  79.     }
  80.  
  81.     exit(0);
  82. }                                                               
  83. -----------------------------------------------------------------------------
  84.  
  85. Thanks in advance.
  86.  
  87. Cheers
  88. -Trevor
  89.  
  90.  
  91.