home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / programm / 4386 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.2 KB  |  84 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!munnari.oz.au!metro!usage!spectrum!trevore
  3. From: trevore@spectrum.cs.unsw.oz.au (Trevor Robert Elbourne)
  4. Subject: help using ptrace
  5. Message-ID: <1992Aug20.045452.10878@usage.csd.unsw.OZ.AU>
  6. Sender: news@usage.csd.unsw.OZ.AU
  7. Nntp-Posting-Host: canescent.spectrum.cs.unsw.oz.au
  8. Reply-To: trevore@spectrum.cs.unsw.oz.au (Trevor Robert Elbourne)
  9. Organization: none
  10. Date: Thu, 20 Aug 1992 04:54:52 GMT
  11. Lines: 71
  12.  
  13. Does anybody know how to use the ptrace system call successfully? I have tried with no success.
  14. I am trying to trace a child process after doing a fork(). After the fork I get the child to do
  15. a ptrace (TRACEME) and then generate a signal to stop it (using kill). The parent does a wait,
  16. when restarted the parent tries to trace the child, but it doesn't seem to work. Somehow the 
  17. child is being killed or something. Here's what I tried:
  18.  
  19. -------------------------------------------------------------------------------------------------
  20. #include    <stdio.h>
  21. #include    <ptrace.h>
  22. #include    <signal.h>
  23. #include    <sys/wait.h>
  24.  
  25. extern    int    errno;
  26.  
  27. main(int argc, char **argv, char **envp)
  28. {
  29.     int        pid;
  30.     int        x,count;
  31.     union wait    status;                  
  32.     char        buff[127];
  33.  
  34.     switch (pid = fork())
  35.     {
  36.     case 0     : /* child */
  37.         if (ptrace(PTRACE_TRACEME, 0, NULL, 0, NULL, 0) < 0)
  38.         {
  39.             perror("child ptrace failed"); exit(2);
  40.         }
  41.  
  42.         printf("child signals ... \n");
  43.         fflush(stdout);
  44.         kill(getpid(), SIGUSR1);
  45.         x=0;            /* something to trace */
  46.         x=1;
  47.         ++x;
  48.         printf("child is done\n");
  49.         exit(0);
  50.  
  51.     default    : /* parent */
  52.         if (wait(&status) != pid)
  53.         {
  54.             perror("wait failed"); exit(3);
  55.         }
  56.  
  57.         printf("status: %08x\n", status);
  58.         errno = 0;
  59.         if (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0) < 0)
  60.         {
  61.             perror("parent attach ptrace failed"); exit(2);
  62.         }
  63.         while (ptrace(PTRACE_SINGLESTEP, pid, NULL, 0, NULL, 0)==0)
  64.             ++count;
  65.         perror("while done");
  66.         printf("count: %u\n",count);
  67.         break;
  68.  
  69.     case -1    : /* error */
  70.         perror("fork failed");
  71.         exit(1);
  72.     }
  73.  
  74.     exit(0);
  75. }                                                               
  76. ---------------------------------------------------------------------------------------------------------------
  77.  
  78. Can anyone help with this?
  79.  
  80. Thanks in advance.
  81.  
  82. Cheers
  83. -Trevor
  84.