home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / symm-nat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-30  |  23.8 KB  |  849 lines

  1. /* Sequent Symmetry host interface, for GDB when running under Unix.
  2.    Copyright 1986, 1987, 1989, 1991, 1992, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* FIXME, some 387-specific items of use taken from i387-tdep.c -- ought to be
  21.    merged back in. */
  22.  
  23. #include "defs.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26. #include "symtab.h"
  27. #include "target.h"
  28.  
  29. /* FIXME: What is the _INKERNEL define for?  */
  30. #define _INKERNEL
  31. #include <signal.h>
  32. #undef _INKERNEL
  33. #include <sys/wait.h>
  34. #include <sys/param.h>
  35. #include <sys/user.h>
  36. #include <sys/proc.h>
  37. #include <sys/dir.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/stat.h>
  40. #ifdef _SEQUENT_
  41. #include <sys/ptrace.h>
  42. #else
  43. /* Dynix has only machine/ptrace.h, which is already included by sys/user.h  */
  44. /* Dynix has no mptrace call */
  45. #define mptrace ptrace
  46. #endif
  47. #include "gdbcore.h"
  48. #include <fcntl.h>
  49. #include <sgtty.h>
  50. #define TERMINAL struct sgttyb
  51.  
  52. #include "gdbcore.h"
  53.  
  54. void
  55. store_inferior_registers(regno)
  56. int regno;
  57. {
  58.   struct pt_regset regs;
  59.   int i;
  60.   extern char registers[];
  61.  
  62.   /* FIXME: Fetching the registers is a kludge to initialize all elements
  63.      in the fpu and fpa status. This works for normal debugging, but
  64.      might cause problems when calling functions in the inferior.
  65.      At least fpu_control and fpa_pcr (probably more) should be added 
  66.      to the registers array to solve this properly.  */
  67.   mptrace (XPT_RREGS, inferior_pid, (PTRACE_ARG3_TYPE) ®s, 0);
  68.  
  69.   regs.pr_eax = *(int *)®isters[REGISTER_BYTE(0)];
  70.   regs.pr_ebx = *(int *)®isters[REGISTER_BYTE(5)];
  71.   regs.pr_ecx = *(int *)®isters[REGISTER_BYTE(2)];
  72.   regs.pr_edx = *(int *)®isters[REGISTER_BYTE(1)];
  73.   regs.pr_esi = *(int *)®isters[REGISTER_BYTE(6)];
  74.   regs.pr_edi = *(int *)®isters[REGISTER_BYTE(7)];
  75.   regs.pr_esp = *(int *)®isters[REGISTER_BYTE(14)];
  76.   regs.pr_ebp = *(int *)®isters[REGISTER_BYTE(15)];
  77.   regs.pr_eip = *(int *)®isters[REGISTER_BYTE(16)];
  78.   regs.pr_flags = *(int *)®isters[REGISTER_BYTE(17)];
  79.   for (i = 0; i < 31; i++)
  80.     {
  81.       regs.pr_fpa.fpa_regs[i] =
  82.     *(int *)®isters[REGISTER_BYTE(FP1_REGNUM+i)];
  83.     }
  84.   memcpy (regs.pr_fpu.fpu_stack[0], ®isters[REGISTER_BYTE(ST0_REGNUM)], 10);
  85.   memcpy (regs.pr_fpu.fpu_stack[1], ®isters[REGISTER_BYTE(ST1_REGNUM)], 10);
  86.   memcpy (regs.pr_fpu.fpu_stack[2], ®isters[REGISTER_BYTE(ST2_REGNUM)], 10);
  87.   memcpy (regs.pr_fpu.fpu_stack[3], ®isters[REGISTER_BYTE(ST3_REGNUM)], 10);
  88.   memcpy (regs.pr_fpu.fpu_stack[4], ®isters[REGISTER_BYTE(ST4_REGNUM)], 10);
  89.   memcpy (regs.pr_fpu.fpu_stack[5], ®isters[REGISTER_BYTE(ST5_REGNUM)], 10);
  90.   memcpy (regs.pr_fpu.fpu_stack[6], ®isters[REGISTER_BYTE(ST6_REGNUM)], 10);
  91.   memcpy (regs.pr_fpu.fpu_stack[7], ®isters[REGISTER_BYTE(ST7_REGNUM)], 10);
  92.   mptrace (XPT_WREGS, inferior_pid, (PTRACE_ARG3_TYPE) ®s, 0);
  93. }
  94.  
  95. void
  96. fetch_inferior_registers (regno)
  97.      int regno;
  98. {
  99.   int i;
  100.   struct pt_regset regs;
  101.   extern char registers[];
  102.  
  103.   registers_fetched ();
  104.  
  105.   mptrace (XPT_RREGS, inferior_pid, (PTRACE_ARG3_TYPE) ®s, 0);
  106.   *(int *)®isters[REGISTER_BYTE(EAX_REGNUM)] = regs.pr_eax;
  107.   *(int *)®isters[REGISTER_BYTE(EBX_REGNUM)] = regs.pr_ebx;
  108.   *(int *)®isters[REGISTER_BYTE(ECX_REGNUM)] = regs.pr_ecx;
  109.   *(int *)®isters[REGISTER_BYTE(EDX_REGNUM)] = regs.pr_edx;
  110.   *(int *)®isters[REGISTER_BYTE(ESI_REGNUM)] = regs.pr_esi;
  111.   *(int *)®isters[REGISTER_BYTE(EDI_REGNUM)] = regs.pr_edi;
  112.   *(int *)®isters[REGISTER_BYTE(EBP_REGNUM)] = regs.pr_ebp;
  113.   *(int *)®isters[REGISTER_BYTE(ESP_REGNUM)] = regs.pr_esp;
  114.   *(int *)®isters[REGISTER_BYTE(EIP_REGNUM)] = regs.pr_eip;
  115.   *(int *)®isters[REGISTER_BYTE(EFLAGS_REGNUM)] = regs.pr_flags;
  116.   for (i = 0; i < FPA_NREGS; i++)
  117.     {
  118.       *(int *)®isters[REGISTER_BYTE(FP1_REGNUM+i)] =
  119.     regs.pr_fpa.fpa_regs[i];
  120.     }
  121.   memcpy (®isters[REGISTER_BYTE(ST0_REGNUM)], regs.pr_fpu.fpu_stack[0], 10);
  122.   memcpy (®isters[REGISTER_BYTE(ST1_REGNUM)], regs.pr_fpu.fpu_stack[1], 10);
  123.   memcpy (®isters[REGISTER_BYTE(ST2_REGNUM)], regs.pr_fpu.fpu_stack[2], 10);
  124.   memcpy (®isters[REGISTER_BYTE(ST3_REGNUM)], regs.pr_fpu.fpu_stack[3], 10);
  125.   memcpy (®isters[REGISTER_BYTE(ST4_REGNUM)], regs.pr_fpu.fpu_stack[4], 10);
  126.   memcpy (®isters[REGISTER_BYTE(ST5_REGNUM)], regs.pr_fpu.fpu_stack[5], 10);
  127.   memcpy (®isters[REGISTER_BYTE(ST6_REGNUM)], regs.pr_fpu.fpu_stack[6], 10);
  128.   memcpy (®isters[REGISTER_BYTE(ST7_REGNUM)], regs.pr_fpu.fpu_stack[7], 10);
  129. }
  130.  
  131. /* FIXME:  This should be merged with i387-tdep.c as well. */
  132. static
  133. print_fpu_status(ep)
  134. struct pt_regset ep;
  135. {
  136.     int i;
  137.     int bothstatus;
  138.     int top;
  139.     int fpreg;
  140.     unsigned char *p;
  141.     
  142.     printf_unfiltered("80387:");
  143.     if (ep.pr_fpu.fpu_ip == 0) {
  144.     printf_unfiltered(" not in use.\n");
  145.     return;
  146.     } else {
  147.     printf_unfiltered("\n");
  148.     }
  149.     if (ep.pr_fpu.fpu_status != 0) {
  150.     print_387_status_word (ep.pr_fpu.fpu_status);
  151.     }
  152.     print_387_control_word (ep.pr_fpu.fpu_control);
  153.     printf_unfiltered ("last exception: ");
  154.     printf_unfiltered ("opcode 0x%x; ", ep.pr_fpu.fpu_rsvd4);
  155.     printf_unfiltered ("pc 0x%x:0x%x; ", ep.pr_fpu.fpu_cs, ep.pr_fpu.fpu_ip);
  156.     printf_unfiltered ("operand 0x%x:0x%x\n", ep.pr_fpu.fpu_data_offset, ep.pr_fpu.fpu_op_sel);
  157.     
  158.     top = (ep.pr_fpu.fpu_status >> 11) & 7;
  159.     
  160.     printf_unfiltered ("regno  tag  msb              lsb  value\n");
  161.     for (fpreg = 7; fpreg >= 0; fpreg--) 
  162.     {
  163.         double val;
  164.         
  165.         printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : "  ", fpreg);
  166.         
  167.         switch ((ep.pr_fpu.fpu_tag >> (fpreg * 2)) & 3) 
  168.         {
  169.         case 0: printf_unfiltered ("valid "); break;
  170.         case 1: printf_unfiltered ("zero  "); break;
  171.         case 2: printf_unfiltered ("trap  "); break;
  172.         case 3: printf_unfiltered ("empty "); break;
  173.         }
  174.         for (i = 9; i >= 0; i--)
  175.         printf_unfiltered ("%02x", ep.pr_fpu.fpu_stack[fpreg][i]);
  176.         
  177.         i387_to_double ((char *)ep.pr_fpu.fpu_stack[fpreg], (char *)&val);
  178.         printf_unfiltered ("  %g\n", val);
  179.     }
  180.     if (ep.pr_fpu.fpu_rsvd1)
  181.     warning ("rsvd1 is 0x%x\n", ep.pr_fpu.fpu_rsvd1);
  182.     if (ep.pr_fpu.fpu_rsvd2)
  183.     warning ("rsvd2 is 0x%x\n", ep.pr_fpu.fpu_rsvd2);
  184.     if (ep.pr_fpu.fpu_rsvd3)
  185.     warning ("rsvd3 is 0x%x\n", ep.pr_fpu.fpu_rsvd3);
  186.     if (ep.pr_fpu.fpu_rsvd5)
  187.     warning ("rsvd5 is 0x%x\n", ep.pr_fpu.fpu_rsvd5);
  188. }
  189.  
  190.  
  191. print_1167_control_word(pcr)
  192. unsigned int pcr;
  193.  
  194. {
  195.     int pcr_tmp;
  196.  
  197.     pcr_tmp = pcr & FPA_PCR_MODE;
  198.     printf_unfiltered("\tMODE= %#x; RND= %#x ", pcr_tmp, pcr_tmp & 12);
  199.     switch (pcr_tmp & 12) {
  200.     case 0:
  201.     printf_unfiltered("RN (Nearest Value)");
  202.     break;
  203.     case 1:
  204.     printf_unfiltered("RZ (Zero)");
  205.     break;
  206.     case 2:
  207.     printf_unfiltered("RP (Positive Infinity)");
  208.     break;
  209.     case 3:
  210.     printf_unfiltered("RM (Negative Infinity)");
  211.     break;
  212.     }
  213.     printf_unfiltered("; IRND= %d ", pcr_tmp & 2);
  214.     if (0 == pcr_tmp & 2) {
  215.     printf_unfiltered("(same as RND)\n");
  216.     } else {
  217.     printf_unfiltered("(toward zero)\n");
  218.     }
  219.     pcr_tmp = pcr & FPA_PCR_EM;
  220.     printf_unfiltered("\tEM= %#x", pcr_tmp);
  221.     if (pcr_tmp & FPA_PCR_EM_DM) printf_unfiltered(" DM");
  222.     if (pcr_tmp & FPA_PCR_EM_UOM) printf_unfiltered(" UOM");
  223.     if (pcr_tmp & FPA_PCR_EM_PM) printf_unfiltered(" PM");
  224.     if (pcr_tmp & FPA_PCR_EM_UM) printf_unfiltered(" UM");
  225.     if (pcr_tmp & FPA_PCR_EM_OM) printf_unfiltered(" OM");
  226.     if (pcr_tmp & FPA_PCR_EM_ZM) printf_unfiltered(" ZM");
  227.     if (pcr_tmp & FPA_PCR_EM_IM) printf_unfiltered(" IM");
  228.     printf_unfiltered("\n");
  229.     pcr_tmp = FPA_PCR_CC;
  230.     printf_unfiltered("\tCC= %#x", pcr_tmp);
  231.     if (pcr_tmp & FPA_PCR_20MHZ) printf_unfiltered(" 20MHZ");
  232.     if (pcr_tmp & FPA_PCR_CC_Z) printf_unfiltered(" Z");
  233.     if (pcr_tmp & FPA_PCR_CC_C2) printf_unfiltered(" C2");
  234.  
  235.     /* Dynix defines FPA_PCR_CC_C0 to 0x100 and ptx defines
  236.        FPA_PCR_CC_C1 to 0x100.  Use whichever is defined and assume
  237.        the OS knows what it is doing.  */
  238. #ifdef FPA_PCR_CC_C1
  239.     if (pcr_tmp & FPA_PCR_CC_C1) printf_unfiltered(" C1");
  240. #else
  241.     if (pcr_tmp & FPA_PCR_CC_C0) printf_unfiltered(" C0");
  242. #endif
  243.  
  244.     switch (pcr_tmp)
  245.       {
  246.       case FPA_PCR_CC_Z:
  247.     printf_unfiltered(" (Equal)");
  248.     break;
  249. #ifdef FPA_PCR_CC_C1
  250.       case FPA_PCR_CC_C1:
  251. #else
  252.       case FPA_PCR_CC_C0:
  253. #endif
  254.     printf_unfiltered(" (Less than)");
  255.     break;
  256.       case 0:
  257.     printf_unfiltered(" (Greater than)");
  258.     break;
  259.       case FPA_PCR_CC_Z | 
  260. #ifdef FPA_PCR_CC_C1
  261.     FPA_PCR_CC_C1
  262. #else
  263.     FPA_PCR_CC_C0
  264. #endif
  265.       | FPA_PCR_CC_C2:
  266.     printf_unfiltered(" (Unordered)");
  267.     break;
  268.       default:
  269.     printf_unfiltered(" (Undefined)");
  270.     break;
  271.       }
  272.     printf_unfiltered("\n");
  273.     pcr_tmp = pcr & FPA_PCR_AE;
  274.     printf_unfiltered("\tAE= %#x", pcr_tmp);
  275.     if (pcr_tmp & FPA_PCR_AE_DE) printf_unfiltered(" DE");
  276.     if (pcr_tmp & FPA_PCR_AE_UOE) printf_unfiltered(" UOE");
  277.     if (pcr_tmp & FPA_PCR_AE_PE) printf_unfiltered(" PE");
  278.     if (pcr_tmp & FPA_PCR_AE_UE) printf_unfiltered(" UE");
  279.     if (pcr_tmp & FPA_PCR_AE_OE) printf_unfiltered(" OE");
  280.     if (pcr_tmp & FPA_PCR_AE_ZE) printf_unfiltered(" ZE");
  281.     if (pcr_tmp & FPA_PCR_AE_EE) printf_unfiltered(" EE");
  282.     if (pcr_tmp & FPA_PCR_AE_IE) printf_unfiltered(" IE");
  283.     printf_unfiltered("\n");
  284. }
  285.  
  286. print_1167_regs(regs)
  287. long regs[FPA_NREGS];
  288.  
  289. {
  290.     int i;
  291.  
  292.     union {
  293.     double    d;
  294.     long    l[2];
  295.     } xd;
  296.     union {
  297.     float    f;
  298.     long    l;
  299.     } xf;
  300.  
  301.  
  302.     for (i = 0; i < FPA_NREGS; i++) {
  303.     xf.l = regs[i];
  304.     printf_unfiltered("%%fp%d: raw= %#x, single= %f", i+1, regs[i], xf.f);
  305.     if (!(i & 1)) {
  306.         printf_unfiltered("\n");
  307.     } else {
  308.         xd.l[1] = regs[i];
  309.         xd.l[0] = regs[i+1];
  310.         printf_unfiltered(", double= %f\n", xd.d);
  311.     }
  312.     }
  313. }
  314.  
  315. print_fpa_status(ep)
  316. struct pt_regset ep;
  317.  
  318. {
  319.  
  320.     printf_unfiltered("WTL 1167:");
  321.     if (ep.pr_fpa.fpa_pcr !=0) {
  322.     printf_unfiltered("\n");
  323.     print_1167_control_word(ep.pr_fpa.fpa_pcr);
  324.     print_1167_regs(ep.pr_fpa.fpa_regs);
  325.     } else {
  326.     printf_unfiltered(" not in use.\n");
  327.     }
  328. }
  329.  
  330. #if 0 /* disabled because it doesn't go through the target vector.  */
  331. i386_float_info ()
  332. {
  333.   char ubuf[UPAGES*NBPG];
  334.   struct pt_regset regset;
  335.  
  336.   if (have_inferior_p())
  337.     {
  338.       PTRACE_READ_REGS (inferior_pid, (PTRACE_ARG3_TYPE) ®set);
  339.     }
  340.   else
  341.     {
  342.       int corechan = bfd_cache_lookup (core_bfd);
  343.       if (lseek (corechan, 0, 0) < 0)
  344.     {
  345.       perror ("seek on core file");
  346.     }
  347.       if (myread (corechan, ubuf, UPAGES*NBPG) < 0)
  348.     {
  349.       perror ("read on core file");
  350.     }
  351.       /* only interested in the floating point registers */
  352.       regset.pr_fpu = ((struct user *) ubuf)->u_fpusave;
  353.       regset.pr_fpa = ((struct user *) ubuf)->u_fpasave;
  354.     }
  355.   print_fpu_status(regset);
  356.   print_fpa_status(regset);
  357. }
  358. #endif
  359.  
  360. static volatile int got_sigchld;
  361.  
  362. /*ARGSUSED*/
  363. /* This will eventually be more interesting. */
  364. void
  365. sigchld_handler(signo)
  366.     int signo;
  367. {
  368.     got_sigchld++;
  369. }
  370.  
  371. /*
  372.  * Signals for which the default action does not cause the process
  373.  * to die.  See <sys/signal.h> for where this came from (alas, we
  374.  * can't use those macros directly)
  375.  */
  376. #ifndef sigmask
  377. #define sigmask(s) (1 << ((s) - 1))
  378. #endif
  379. #define SIGNALS_DFL_SAFE sigmask(SIGSTOP) | sigmask(SIGTSTP) | \
  380.     sigmask(SIGTTIN) | sigmask(SIGTTOU) | sigmask(SIGCHLD) | \
  381.     sigmask(SIGCONT) | sigmask(SIGWINCH) | sigmask(SIGPWR) | \
  382.     sigmask(SIGURG) | sigmask(SIGPOLL)
  383.  
  384. #ifdef ATTACH_DETACH
  385. /*
  386.  * Thanks to XPT_MPDEBUGGER, we have to mange child_wait().
  387.  */
  388. int
  389. child_wait(pid, status)
  390.      int pid;
  391.      struct target_waitstatus *status;
  392. {
  393.   int save_errno, rv, xvaloff, saoff, sa_hand;
  394.   struct pt_stop pt;
  395.   struct user u;
  396.   sigset_t set;
  397.   /* Host signal number for a signal which the inferior terminates with, or
  398.      0 if it hasn't terminated due to a signal.  */
  399.   static int death_by_signal = 0;
  400. #ifdef SVR4_SHARED_LIBS        /* use this to distinguish ptx 2 vs ptx 4 */
  401.   prstatus_t pstatus;
  402. #endif
  403.  
  404.   do {
  405.     if (attach_flag)
  406.       set_sigint_trap();    /* Causes SIGINT to be passed on to the
  407.                    attached process. */
  408.     save_errno = errno;
  409.  
  410.     got_sigchld = 0;
  411.  
  412.     sigemptyset(&set);
  413.  
  414.     while (got_sigchld == 0) {
  415.         sigsuspend(&set);
  416.     }
  417.     
  418.     if (attach_flag)
  419.       clear_sigint_trap();
  420.  
  421.     rv = mptrace(XPT_STOPSTAT, 0, (char *)&pt, 0);
  422.     if (-1 == rv) {
  423.         printf("XPT_STOPSTAT: errno %d\n", errno); /* DEBUG */
  424.         continue;
  425.     }
  426.  
  427.     pid = pt.ps_pid;
  428.  
  429.     if (pid != inferior_pid) {
  430.         /* NOTE: the mystery fork in csh/tcsh needs to be ignored.
  431.          * We should not return new children for the initial run
  432.          * of a process until it has done the exec.
  433.          */
  434.         /* inferior probably forked; send it on its way */
  435.         rv = mptrace(XPT_UNDEBUG, pid, 0, 0);
  436.         if (-1 == rv) {
  437.             printf("child_wait: XPT_UNDEBUG: pid %d: %s\n", pid,
  438.                safe_strerror(errno));
  439.         }
  440.         continue;
  441.     }
  442.     /* FIXME: Do we deal with fork notification correctly?  */
  443.     switch (pt.ps_reason) {
  444.     case PTS_FORK:
  445.     /* multi proc: treat like PTS_EXEC */
  446.         /*
  447.          * Pretend this didn't happen, since gdb isn't set up
  448.          * to deal with stops on fork.
  449.          */
  450.         rv = ptrace(PT_CONTSIG, pid, 1, 0);
  451.         if (-1 == rv) {
  452.             printf("PTS_FORK: PT_CONTSIG: error %d\n", errno);
  453.         }
  454.         continue;
  455.     case PTS_EXEC:
  456.         /*
  457.          * Pretend this is a SIGTRAP.
  458.          */
  459.         status->kind = TARGET_WAITKIND_STOPPED;
  460.         status->value.sig = TARGET_SIGNAL_TRAP;
  461.         break;
  462.     case PTS_EXIT:
  463.         /*
  464.          * Note: we stop before the exit actually occurs.  Extract
  465.          * the exit code from the uarea.  If we're stopped in the
  466.          * exit() system call, the exit code will be in
  467.          * u.u_ap[0].  An exit due to an uncaught signal will have
  468.          * something else in here, see the comment in the default:
  469.          * case, below.  Finally,let the process exit.
  470.          */
  471.         if (death_by_signal)
  472.           {
  473.         status->kind = TARGET_WAITKIND_SIGNALED;
  474.         status->value.sig = target_signal_from_host (death_by_signal);
  475.         death_by_signal = 0;
  476.         break;
  477.           }
  478.         xvaloff = (unsigned long)&u.u_ap[0] - (unsigned long)&u;
  479.         errno = 0;
  480.         rv = ptrace(PT_RUSER, pid, (char *)xvaloff, 0);
  481.         status->kind = TARGET_WAITKIND_EXITED;
  482.         status->value.integer = rv;
  483.         /*
  484.          * addr & data to mptrace() don't matter here, since
  485.          * the process is already dead.
  486.          */
  487.         rv = mptrace(XPT_UNDEBUG, pid, 0, 0);
  488.         if (-1 == rv) {
  489.             printf("child_wait: PTS_EXIT: XPT_UNDEBUG: pid %d error %d\n", pid,
  490.                errno);
  491.         }
  492.         break;
  493.     case PTS_WATCHPT_HIT:
  494.         fatal("PTS_WATCHPT_HIT\n");
  495.         break;
  496.     default:
  497.         /* stopped by signal */
  498.         status->kind = TARGET_WAITKIND_STOPPED;
  499.         status->value.sig = target_signal_from_host (pt.ps_reason);
  500.         death_by_signal = 0;
  501.  
  502.         if (0 == (SIGNALS_DFL_SAFE & sigmask(pt.ps_reason))) {
  503.             break;
  504.         }
  505.         /* else default action of signal is to die */
  506. #ifdef SVR4_SHARED_LIBS
  507.         rv = ptrace(PT_GET_PRSTATUS, pid, (char *)&pstatus, 0);
  508.         if (-1 == rv)
  509.         error("child_wait: signal %d PT_GET_PRSTATUS: %s\n",
  510.             pt.ps_reason, safe_strerror(errno));
  511.         if (pstatus.pr_cursig != pt.ps_reason) {
  512.         printf("pstatus signal %d, pt signal %d\n",
  513.             pstatus.pr_cursig, pt.ps_reason);
  514.         }
  515.         sa_hand = (int)pstatus.pr_action.sa_handler;
  516. #else
  517.         saoff = (unsigned long)&u.u_sa[0] - (unsigned long)&u;
  518.         saoff += sizeof(struct sigaction) * (pt.ps_reason - 1);
  519.         errno = 0;
  520.         sa_hand = ptrace(PT_RUSER, pid, (char *)saoff, 0);
  521.         if (errno)
  522.             error("child_wait: signal %d: RUSER: %s\n",
  523.                pt.ps_reason, safe_strerror(errno));
  524. #endif
  525.         if ((int)SIG_DFL == sa_hand) {
  526.             /* we will be dying */
  527.             death_by_signal = pt.ps_reason;
  528.         }
  529.         break;
  530.     }
  531.  
  532.   } while (pid != inferior_pid); /* Some other child died or stopped */
  533.  
  534.   return pid;
  535. }
  536. #else /* !ATTACH_DETACH */
  537. /*
  538.  * Simple child_wait() based on inftarg.c child_wait() for use until
  539.  * the MPDEBUGGER child_wait() works properly.  This will go away when
  540.  * that is fixed.
  541.  */
  542. child_wait (pid, ourstatus)
  543.      int pid;
  544.      struct target_waitstatus *ourstatus;
  545. {
  546.   int save_errno;
  547.   int status;
  548.  
  549.   do {
  550.     pid = wait (&status);
  551.     save_errno = errno;
  552.  
  553.     if (pid == -1)
  554.       {
  555.     if (save_errno == EINTR)
  556.       continue;
  557.     fprintf (stderr, "Child process unexpectedly missing: %s.\n",
  558.          safe_strerror (save_errno));
  559.     ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  560.     ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
  561.         return -1;
  562.       }
  563.   } while (pid != inferior_pid); /* Some other child died or stopped */
  564.   store_waitstatus (ourstatus, status);
  565.   return pid;
  566. }
  567. #endif /* ATTACH_DETACH */
  568.  
  569.  
  570.  
  571. /* This function simply calls ptrace with the given arguments.  
  572.    It exists so that all calls to ptrace are isolated in this 
  573.    machine-dependent file. */
  574. int
  575. call_ptrace (request, pid, addr, data)
  576.      int request, pid;
  577.      PTRACE_ARG3_TYPE addr;
  578.      int data;
  579. {
  580.   return ptrace (request, pid, addr, data);
  581. }
  582.  
  583. int
  584. call_mptrace(request, pid, addr, data)
  585.     int request, pid;
  586.     PTRACE_ARG3_TYPE addr;
  587.     int data;
  588. {
  589.     return mptrace(request, pid, addr, data);
  590. }
  591.  
  592. #if defined (DEBUG_PTRACE)
  593. /* For the rest of the file, use an extra level of indirection */
  594. /* This lets us breakpoint usefully on call_ptrace. */
  595. #define ptrace call_ptrace
  596. #define mptrace call_mptrace
  597. #endif
  598.  
  599. void
  600. kill_inferior ()
  601. {
  602.   if (inferior_pid == 0)
  603.     return;
  604.  
  605.   /* For MPDEBUGGER, don't use PT_KILL, since the child will stop
  606.      again with a PTS_EXIT.  Just hit him with SIGKILL (so he stops)
  607.      and detach. */
  608.  
  609.   kill (inferior_pid, SIGKILL);
  610. #ifdef ATTACH_DETACH
  611.   detach(SIGKILL);
  612. #else /* ATTACH_DETACH */
  613.   ptrace(PT_KILL, inferior_pid, 0, 0);
  614.   wait((int *)NULL);
  615. #endif /* ATTACH_DETACH */
  616.   target_mourn_inferior ();
  617. }
  618.  
  619. /* Resume execution of the inferior process.
  620.    If STEP is nonzero, single-step it.
  621.    If SIGNAL is nonzero, give it that signal.  */
  622.  
  623. void
  624. child_resume (pid, step, signal)
  625.      int pid;
  626.      int step;
  627.      enum target_signal signal;
  628. {
  629.   errno = 0;
  630.  
  631.   if (pid == -1)
  632.     pid = inferior_pid;
  633.  
  634.   /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
  635.      it was.  (If GDB wanted it to start some other way, we have already
  636.      written a new PC value to the child.)
  637.  
  638.      If this system does not support PT_SSTEP, a higher level function will
  639.      have called single_step() to transmute the step request into a
  640.      continue request (by setting breakpoints on all possible successor
  641.      instructions), so we don't have to worry about that here.  */
  642.  
  643.   if (step)
  644.     ptrace (PT_SSTEP,     pid, (PTRACE_ARG3_TYPE) 1, signal);
  645.   else
  646.     ptrace (PT_CONTSIG, pid, (PTRACE_ARG3_TYPE) 1, signal);
  647.  
  648.   if (errno)
  649.     perror_with_name ("ptrace");
  650. }
  651.  
  652. #ifdef ATTACH_DETACH
  653. /* Start debugging the process whose number is PID.  */
  654. int
  655. attach (pid)
  656.      int pid;
  657. {
  658.     sigset_t set;
  659.     int rv;
  660.  
  661.     rv = mptrace(XPT_DEBUG, pid, 0, 0);
  662.     if (-1 == rv) {
  663.         error("mptrace(XPT_DEBUG): %s", safe_strerror(errno));
  664.     }
  665.     rv = mptrace(XPT_SIGNAL, pid, 0, SIGSTOP);
  666.     if (-1 == rv) {
  667.         error("mptrace(XPT_SIGNAL): %s", safe_strerror(errno));
  668.     }
  669.     attach_flag = 1;
  670.     return pid;
  671. }
  672.  
  673. void
  674. detach (signo)
  675.      int signo;
  676. {
  677.     int rv;
  678.  
  679.     rv = mptrace(XPT_UNDEBUG, inferior_pid, 1, signo);
  680.     if (-1 == rv) {
  681.         error("mptrace(XPT_UNDEBUG): %s", safe_strerror(errno));
  682.     }
  683.     attach_flag = 0;
  684. }
  685.  
  686. #endif /* ATTACH_DETACH */
  687.  
  688. /* Default the type of the ptrace transfer to int.  */
  689. #ifndef PTRACE_XFER_TYPE
  690. #define PTRACE_XFER_TYPE int
  691. #endif
  692.  
  693.  
  694. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  695.    in the NEW_SUN_PTRACE case.
  696.    It ought to be straightforward.  But it appears that writing did
  697.    not write the data that I specified.  I cannot understand where
  698.    it got the data that it actually did write.  */
  699.  
  700. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  701.    to debugger memory starting at MYADDR.   Copy to inferior if
  702.    WRITE is nonzero.
  703.   
  704.    Returns the length copied, which is either the LEN argument or zero.
  705.    This xfer function does not do partial moves, since child_ops
  706.    doesn't allow memory operations to cross below us in the target stack
  707.    anyway.  */
  708.  
  709. int
  710. child_xfer_memory (memaddr, myaddr, len, write, target)
  711.      CORE_ADDR memaddr;
  712.      char *myaddr;
  713.      int len;
  714.      int write;
  715.      struct target_ops *target;        /* ignored */
  716. {
  717.   register int i;
  718.   /* Round starting address down to longword boundary.  */
  719.   register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
  720.   /* Round ending address up; get number of longwords that makes.  */
  721.   register int count
  722.     = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
  723.       / sizeof (PTRACE_XFER_TYPE);
  724.   /* Allocate buffer of that many longwords.  */
  725.   register PTRACE_XFER_TYPE *buffer
  726.     = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
  727.  
  728.   if (write)
  729.     {
  730.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  731.  
  732.       if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
  733.     /* Need part of initial word -- fetch it.  */
  734.         buffer[0] = ptrace (PT_RTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  735.                 0);
  736.       }
  737.  
  738.       if (count > 1)        /* FIXME, avoid if even boundary */
  739.     {
  740.       buffer[count - 1]
  741.         = ptrace (PT_RTEXT, inferior_pid,
  742.               ((PTRACE_ARG3_TYPE)
  743.                (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
  744.               0);
  745.     }
  746.  
  747.       /* Copy data to be written over corresponding part of buffer */
  748.  
  749.       memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
  750.           myaddr,
  751.           len);
  752.  
  753.       /* Write the entire buffer.  */
  754.  
  755.       for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
  756.     {
  757.       errno = 0;
  758.       ptrace (PT_WDATA, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  759.           buffer[i]);
  760.       if (errno)
  761.         {
  762.           /* Using the appropriate one (I or D) is necessary for
  763.          Gould NP1, at least.  */
  764.           errno = 0;
  765.           ptrace (PT_WTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  766.               buffer[i]);
  767.         }
  768.       if (errno)
  769.         return 0;
  770.     }
  771.     }
  772.   else
  773.     {
  774.       /* Read all the longwords */
  775.       for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
  776.     {
  777.       errno = 0;
  778.       buffer[i] = ptrace (PT_RTEXT, inferior_pid,
  779.                   (PTRACE_ARG3_TYPE) addr, 0);
  780.       if (errno)
  781.         return 0;
  782.       QUIT;
  783.     }
  784.  
  785.       /* Copy appropriate bytes out of the buffer.  */
  786.       memcpy (myaddr,
  787.           (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
  788.           len);
  789.     }
  790.   return len;
  791. }
  792.  
  793.  
  794. void
  795. _initialize_symm_nat ()
  796. {
  797. #ifdef ATTACH_DETACH
  798. /*
  799.  * the MPDEBUGGER is necessary for process tree debugging and attach
  800.  * to work, but it alters the behavior of debugged processes, so other
  801.  * things (at least child_wait()) will have to change to accomodate
  802.  * that.
  803.  *
  804.  * Note that attach is not implemented in dynix 3, and not in ptx
  805.  * until version 2.1 of the OS.
  806.  */
  807.     int rv;
  808.     sigset_t set;
  809.     struct sigaction sact;
  810.  
  811.     rv = mptrace(XPT_MPDEBUGGER, 0, 0, 0);
  812.     if (-1 == rv) {
  813.         fatal("_initialize_symm_nat(): mptrace(XPT_MPDEBUGGER): %s",
  814.               safe_strerror(errno));
  815.     }
  816.  
  817.     /*
  818.      * Under MPDEBUGGER, we get SIGCLHD when a traced process does
  819.      * anything of interest.
  820.      */
  821.  
  822.     /*
  823.      * Block SIGCHLD.  We leave it blocked all the time, and then
  824.      * call sigsuspend() in child_wait() to wait for the child
  825.      * to do something.  None of these ought to fail, but check anyway.
  826.      */
  827.     sigemptyset(&set);
  828.     rv = sigaddset(&set, SIGCHLD);
  829.     if (-1 == rv) {
  830.         fatal("_initialize_symm_nat(): sigaddset(SIGCHLD): %s",
  831.               safe_strerror(errno));
  832.     }
  833.     rv = sigprocmask(SIG_BLOCK, &set, (sigset_t *)NULL);
  834.     if (-1 == rv) {
  835.         fatal("_initialize_symm_nat(): sigprocmask(SIG_BLOCK): %s",
  836.               safe_strerror(errno));
  837.     }
  838.  
  839.     sact.sa_handler = sigchld_handler;
  840.     sigemptyset(&sact.sa_mask);
  841.     sact.sa_flags = SA_NOCLDWAIT; /* keep the zombies away */
  842.     rv = sigaction(SIGCHLD, &sact, (struct sigaction *)NULL);
  843.     if (-1 == rv) {
  844.         fatal("_initialize_symm_nat(): sigaction(SIGCHLD): %s",
  845.               safe_strerror(errno));
  846.     }
  847. #endif
  848. }
  849.