home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / ultrix / 8250 < prev    next >
Encoding:
Text File  |  1992-11-11  |  3.6 KB  |  136 lines

  1. Newsgroups: comp.unix.ultrix
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!pasteur!asami
  3. From: asami@cs.berkeley.edu
  4. Subject: Re: ptrace(20, pid...) doesn't work
  5. In-Reply-To: boyd@prl.dec.com's message of Wed, 11 Nov 1992 14:34:23 GMT
  6. Message-ID: <1992Nov12.015127.837@pasteur.Berkeley.EDU>
  7. Originator: asami@adder.cs.Berkeley.EDU
  8. Sender: nntp@pasteur.Berkeley.EDU (NNTP Poster)
  9. Nntp-Posting-Host: adder.cs.berkeley.edu
  10. Reply-To: asami@snake.cs.berkeley.edu
  11. Organization: University of California, at Berkeley
  12. References: <ASAMI.92Nov10025407@anaconda.cs.Berkeley.EDU>
  13.     <ASAMI.92Nov10230226@moccasin.cs.Berkeley.EDU>
  14.     <1992Nov11.143423.23743@prl.dec.com>
  15. Date: Thu, 12 Nov 1992 01:51:27 GMT
  16. Lines: 118
  17.  
  18. In article <1992Nov11.143423.23743@prl.dec.com>
  19.         boyd@prl.dec.com (Boyd Roberts) writes:
  20.  
  21.  * >  * Um, how about a ptrace(0, ...) call in the child?
  22.  * > 
  23.  * > Hmmm....it worked!  Thanks.  By the way, was I missing something, or
  24.  * > is it just an undocumented feature (read: bug :).
  25.  * > 
  26.  * 
  27.  * No it's always worked that.  Slight case of RTM.
  28.  
  29. Err, sorry.  I was trying to ask to why ptrace(20, ...) doesn't work---
  30. I know that ptrace(0, ...) is in the manual.  I guess I have to improve
  31. my Englisch.
  32.  
  33. By the way, on a side note, it seems to me that requests larger than
  34. or equal to 20 don't work at all.  Is this a disabled feature or
  35. something?  Following is an example of 28 (detach child).  According
  36. to the manual, EIO means (probably) the request code is invalid.
  37.  
  38. --
  39.                                                    o o   Satoshi Asami
  40.   Form follows Function.                            ^
  41.                                                (asami@CS.Berkeley.EDU)
  42.   Tel: (510)643-2730 (home)       Computer Science Div., Dept. of EECS
  43.        (510)643-6228 (office)     University of California at Berkeley
  44. ---
  45. Sample run:
  46. ---
  47. % ptrace xeyes
  48. stopped child...
  49. detaching child....
  50. error: ptrace 28, 28352, 1, 0 => -1 5 ("I/O error")
  51. %
  52. ---
  53. Source file:
  54. ---
  55. /* ptrace.c: ptrace test program */
  56.  
  57. #include <stdio.h>
  58. #include <signal.h>
  59. #include <sys/ptrace.h>
  60. #include <sys/wait.h>
  61. #include <sys/types.h>
  62. #include <unistd.h>
  63. #include <errno.h>
  64. #include <string.h>
  65.  
  66. #define OK      0
  67.  
  68. #define DEBUG   0
  69.  
  70. int ptrace(int, int, int *, int) ;
  71. int xptrace(int, int, int *, int) ;
  72.  
  73. void causeandinfo(int) ;
  74.  
  75. #define P_C (PC)
  76. #define TRAP_CAUSE (TRAPCAUSE)
  77. #define TRAP_INFO (TRAPINFO)
  78.  
  79. int debug = DEBUG ;
  80.  
  81. int main(int argc, char **argv)
  82. {
  83.     pid_t       pid ;
  84.  
  85.     if (argc < 2)       {
  86.         fprintf(stderr, "usage: %s prog [args...]\n", argv[0]) ;
  87.         exit(1) ;
  88.     }
  89.  
  90.     if (!strncmp(argv[1], "-d", 2))     {
  91.         debug = 1 ;
  92.         argv++ ;
  93.         argc-- ;
  94.     }
  95.  
  96.     if ((pid = fork()) == -1)   {
  97.         fprintf(stderr, "panic: can't fork()\n") ;
  98.         exit(2) ;
  99.     }
  100.  
  101.     if (pid == 0)       {
  102.         argv++ ;
  103.         if (xptrace(PT_TRACE_ME, 0, NULL, 0) == -1)
  104.             exit(4) ;
  105.         printf("stopped child...\n") ;
  106.         execvp(argv[0], argv) ;
  107.         fprintf(stderr, "panic: can't exec()\n") ;
  108.         exit(3) ;
  109.     }
  110.  
  111.     sleep(5) ;
  112.  
  113.     printf("detaching child....\n") ;
  114.     xptrace(28, pid, (int *)1, 0) ;
  115.  
  116.     sleep(5) ;
  117.  
  118.     return (OK) ;
  119. }
  120.  
  121. int     xptrace(int request, int pid, int *addr, int data)
  122. {
  123.     int retval ;
  124.  
  125.     retval = ptrace(request, pid, addr, data) ;
  126.     if (retval == -1)
  127.         fprintf(stderr, "error: ptrace %d, %d, %x, %d => %d %d (\"%s\")\n",
  128.                 request, pid, (unsigned int)addr, data, retval, errno,
  129.                 strerror(errno)) ;
  130.     else if (debug)
  131.         fprintf(stderr, "debug: ptrace %d, %d, %x, %d\n",
  132.                 request, pid, (unsigned int)addr, data) ;
  133.  
  134.     return (retval) ;
  135. }
  136.