home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / hp / 10035 < prev    next >
Encoding:
Internet Message Format  |  1992-09-03  |  4.5 KB

  1. From: jmn@hpfcso.FC.HP.COM (John Newman)
  2. Date: Thu, 3 Sep 1992 23:17:43 GMT
  3. Subject: Re: attaching xdb to a running process...
  4. Message-ID: <7371287@hpfcso.FC.HP.COM>
  5. Organization: Hewlett-Packard, Fort Collins, CO, USA
  6. Path: sparky!uunet!usc!sdd.hp.com!hp-cv!hp-pcd!hpfcso!jmn
  7. Newsgroups: comp.sys.hp
  8. References: <3739@tivoli.UUCP>
  9. Lines: 182
  10.  
  11.  
  12. > I suspect that I just dont know enough about xdb to see what it needs to 
  13. > do to get a process started, but the only manual Ive seen is a really good
  14. > users manual,
  15.  
  16. Thanks.
  17.  
  18. > and this is a little beyond that.
  19. > If anyone knows more about xdb and what signals it uses to start/stop a 
  20. > process once it has gotten it with ptrace, please let me know.  
  21.  
  22. Xdb uses ptrace, not a signal, to (re)start process.  The only signal
  23. xdb uses itself is SIGTRAP.
  24.  
  25. ---
  26.  
  27. I played around with your example program, and got it to work by doing
  28. this:
  29.  
  30.     1. put in an short signal handler for SIGUSR1 (any innocuous signal
  31.        will do here).
  32.  
  33.     2. Set up the handler in exec_debugger(), before the fork.
  34.  
  35. I needed the handler in order to get back out of the pause().
  36. But, here's the key:
  37.  
  38.     3. Once the debugger is attached, I used the following commands:
  39.  
  40.         >b
  41.             This sets a breakpoint at the return addr from 
  42.             the call to pause().  This gives control back to
  43.             the debugger (user) when the pause() returns.
  44.  
  45.         >p $signal = 16
  46.         >C
  47.             This passes the SIGUSR1 to the process and completes
  48.             the pause().
  49.  
  50.        If you use 's' instead of 'C' here, you end up at the 1st stmt
  51.        in the signal handler.
  52.  
  53. Check out the discussion of $signal and Continue (capital 'C') in TFM.
  54.  
  55. You can avoid manually doing this every time with a playback file.
  56. Put the above 3 commands into a file, and list it with -p in your
  57. call to exec:
  58.  
  59.     execlp( ... , "-p", "playback", proc, NULL);
  60.  
  61. NOTE:  Using the 'C' with a breakpoint works great on PA-RISC if archive
  62. libs are used (compiled with -Wl,-a,archive on HP-UX 8.0x).  If a shared libc
  63. (libc.sl) is used, then you'll need to give an explicit location to the
  64. 'b' command.  This is a limitation having to do with shared-libs,
  65. stack unwinding, and attaching while in the kernel.  I'll leave the
  66. grody details out of this discussion.
  67.  
  68. Using the 's' to step into the signal-handler works okay regardless.
  69.  
  70. BTW, Chan's suggestion to use "while(i) ;" will work find too. Just
  71. put something like this into the playback:  p i=0; s
  72.  
  73.          ___        
  74.         /  /        John Newman
  75. HEWLETT/hp/PACKARD  Colorado Language Lab
  76.       /__/          jmn@hpfcrt.fc.hp.com
  77.  
  78. --------- --------- --------- --------- --------- --------- --------- -----
  79. This response does not represent the official position of, or statement by,
  80. the Hewlett-Packard Company.  The above data is provided for informational
  81. purposes only.  It is supplied without warranty of any kind.
  82.  
  83. #---------------------------------- cut here ------------------
  84. # This is a shell archive.  Remove anything before this line,
  85. # then unpack it by saving it in a file and typing "sh file".
  86. #
  87. # This archive contains:
  88. #    prog.c        playback    
  89. #
  90.  
  91. LANG=""; export LANG
  92. PATH=/bin:/usr/bin:$PATH; export PATH
  93.  
  94. echo x - prog.c
  95. cat >prog.c <<'@EOF'
  96.  
  97. /*
  98.  * Compile with: cc -g -Aa -Wl,-a,archive
  99.  */
  100. #define _POSIX_SOURCE
  101. #include <sys/stdsyms.h>
  102. #include <sys/signal.h>
  103. #include <stdio.h>
  104.  
  105. main(int argc, char *argv[])
  106. {
  107.  
  108.     int pid, pid1, pid2, i;
  109.     int exitcode;
  110.  
  111.     printf("in main program\n");
  112.  
  113.     pid = fork();
  114.  
  115.     if (pid != 0) {        /* parent */
  116.         waitpid(pid, &exitcode, 0);
  117.         exit(0);
  118.     }
  119.  
  120.     /*
  121.      * here is the child 
  122.      */
  123.  
  124.     printf(" in the child\n");
  125.  
  126.     pid1 = fork();
  127.  
  128.     if (pid1 != 0) {    /* parent */
  129.         pid2 = getpid();
  130.         printf(" in the first child (debugger parent) %d\n", pid2);
  131.  
  132.         exec_debugger(argv[0], pid2);
  133.  
  134.         pause();
  135.  
  136.         printf("starting the loop\n");
  137.         for (i=1; i < 100; i++) {
  138.             printf("I is %d\n",i);
  139.         }
  140.         exit(1);
  141.     }
  142. }
  143.  
  144. void catch (int sig, unsigned long code, struct sigcontext *scp)
  145. {
  146.     if (sig == SIGUSR1)
  147.         (void) signal (SIGUSR1,SIG_IGN);
  148.  
  149.     return ;
  150. }
  151.  
  152. exec_debugger(char *proc, int pid)
  153. {
  154.     char pidstr[20];
  155.     char *display;
  156.     extern char *getenv(char *);
  157.  
  158.     if ((display = getenv("DISPLAY")) == (char *)NULL || !*display) {
  159.         display = "local:0";
  160.     }
  161.  
  162.     (void) signal(SIGUSR1,(void (*) ())catch);
  163.  
  164.     sprintf(pidstr,"%d",pid);
  165.     printf("starting the debugger %s\n", pidstr);
  166.  
  167.     if (fork() == 0) {
  168.         execlp("/usr/bin/X11/hpterm", "/usr/bin/X11/hpterm", "-display",
  169.             display, "-e", "/usr/bin/xdb", "-P", pidstr, 
  170. #ifdef USE_PLAYBK
  171.             "-p", "playback",
  172. #endif
  173.             proc, NULL);
  174.         exit(2);
  175.     }
  176. }
  177.  
  178. @EOF
  179.  
  180. chmod 666 prog.c
  181.  
  182. echo x - playback
  183. cat >playback <<'@EOF'
  184. b
  185. p $signal = 16
  186. C
  187. @EOF
  188.  
  189. chmod 666 playback
  190.  
  191. exit 0
  192.