home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / infptrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-17  |  12.3 KB  |  463 lines

  1. /* Low level Unix child interface to ptrace, for GDB when running under Unix.
  2.    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "frame.h"
  23. #include "inferior.h"
  24. #include "target.h"
  25.  
  26. #ifdef USG
  27. #include <sys/types.h>
  28. #endif
  29.  
  30. #include <sys/param.h>
  31. #include <sys/dir.h>
  32. #include <signal.h>
  33. #include <sys/ioctl.h>
  34.  
  35. #ifndef NO_PTRACE_H
  36. #ifdef PTRACE_IN_WRONG_PLACE
  37. #include <ptrace.h>
  38. #else
  39. #include <sys/ptrace.h>
  40. #endif
  41. #endif /* NO_PTRACE_H */
  42.  
  43. #if !defined (PT_KILL)
  44. #define PT_KILL 8
  45. #endif
  46.  
  47. #if !defined (PT_STEP)
  48. #define PT_STEP 9
  49. #define PT_CONTINUE 7
  50. #define PT_READ_U 3
  51. #define PT_WRITE_U 6
  52. #define PT_READ_I 1
  53. #define    PT_READ_D 2
  54. #define PT_WRITE_I 4
  55. #define PT_WRITE_D 5
  56. #endif /* No PT_STEP.  */
  57.  
  58. #ifndef PT_ATTACH
  59. #define PT_ATTACH PTRACE_ATTACH
  60. #endif
  61. #ifndef PT_DETACH
  62. #define PT_DETACH PTRACE_DETACH
  63. #endif
  64.  
  65. #include "gdbcore.h"
  66. #ifndef    NO_SYS_FILE
  67. #include <sys/file.h>
  68. #endif
  69. #if 0
  70. /* Don't think this is used anymore.  On the sequent (not sure whether it's
  71.    dynix or ptx or both), it is included unconditionally by sys/user.h and
  72.    not protected against multiple inclusion.  */
  73. #include <sys/stat.h>
  74. #endif
  75.  
  76. #if !defined (FETCH_INFERIOR_REGISTERS)
  77. #include <sys/user.h>        /* Probably need to poke the user structure */
  78. #if defined (KERNEL_U_ADDR_BSD)
  79. #include <a.out.h>        /* For struct nlist */
  80. #endif /* KERNEL_U_ADDR_BSD.  */
  81. #endif /* !FETCH_INFERIOR_REGISTERS */
  82.  
  83.  
  84. /* This function simply calls ptrace with the given arguments.  
  85.    It exists so that all calls to ptrace are isolated in this 
  86.    machine-dependent file. */
  87. int
  88. call_ptrace (request, pid, addr, data)
  89.      int request, pid;
  90.      PTRACE_ARG3_TYPE addr;
  91.      int data;
  92. {
  93.   return ptrace (request, pid, addr, data
  94. #if defined (FIVE_ARG_PTRACE)
  95.          /* Deal with HPUX 8.0 braindamage.  We never use the
  96.             calls which require the fifth argument.  */
  97.          , 0
  98. #endif
  99.          );
  100. }
  101.  
  102. #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
  103. /* For the rest of the file, use an extra level of indirection */
  104. /* This lets us breakpoint usefully on call_ptrace. */
  105. #define ptrace call_ptrace
  106. #endif
  107.  
  108. void
  109. kill_inferior ()
  110. {
  111.   if (inferior_pid == 0)
  112.     return;
  113.   /* ptrace PT_KILL only works if process is stopped!!!  So stop it with
  114.      a real signal first, if we can.  FIXME: This is bogus.  When the inferior
  115.      is not stopped, GDB should just be waiting for it.  Either the following
  116.      line is unecessary, or there is some problem elsewhere in GDB which
  117.      causes us to get here when the inferior is not stopped.  */
  118.   kill (inferior_pid, SIGKILL);
  119.   ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
  120.   wait ((int *)0);
  121.   target_mourn_inferior ();
  122. }
  123.  
  124. #ifndef CHILD_RESUME
  125.  
  126. /* Resume execution of the inferior process.
  127.    If STEP is nonzero, single-step it.
  128.    If SIGNAL is nonzero, give it that signal.  */
  129.  
  130. void
  131. child_resume (pid, step, signal)
  132.      int pid;
  133.      int step;
  134.      enum target_signal signal;
  135. {
  136.   errno = 0;
  137.  
  138.   if (pid == -1)
  139.     /* Resume all threads.  */
  140.     /* I think this only gets used in the non-threaded case, where "resume
  141.        all threads" and "resume inferior_pid" are the same.  */
  142.     pid = inferior_pid;
  143.  
  144.   /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
  145.      it was.  (If GDB wanted it to start some other way, we have already
  146.      written a new PC value to the child.)
  147.  
  148.      If this system does not support PT_STEP, a higher level function will
  149.      have called single_step() to transmute the step request into a
  150.      continue request (by setting breakpoints on all possible successor
  151.      instructions), so we don't have to worry about that here.  */
  152.  
  153.   if (step)
  154.     ptrace (PT_STEP,     pid, (PTRACE_ARG3_TYPE) 1,
  155.         target_signal_to_host (signal));
  156.   else
  157.     ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
  158.         target_signal_to_host (signal));
  159.  
  160.   if (errno)
  161.     perror_with_name ("ptrace");
  162. }
  163. #endif /* CHILD_RESUME */
  164.  
  165.  
  166. #ifdef ATTACH_DETACH
  167. /* Start debugging the process whose number is PID.  */
  168. int
  169. attach (pid)
  170.      int pid;
  171. {
  172.   errno = 0;
  173.   ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
  174.   if (errno)
  175.     perror_with_name ("ptrace");
  176.   attach_flag = 1;
  177.   return pid;
  178. }
  179.  
  180. /* Stop debugging the process whose number is PID
  181.    and continue it with signal number SIGNAL.
  182.    SIGNAL = 0 means just continue it.  */
  183.  
  184. void
  185. detach (signal)
  186.      int signal;
  187. {
  188.   errno = 0;
  189.   ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
  190.   if (errno)
  191.     perror_with_name ("ptrace");
  192.   attach_flag = 0;
  193. }
  194. #endif /* ATTACH_DETACH */
  195.  
  196. /* Default the type of the ptrace transfer to int.  */
  197. #ifndef PTRACE_XFER_TYPE
  198. #define PTRACE_XFER_TYPE int
  199. #endif
  200.  
  201. /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
  202.    to get the offset in the core file of the register values.  */
  203. #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
  204. /* Get kernel_u_addr using BSD-style nlist().  */
  205. CORE_ADDR kernel_u_addr;
  206. #endif /* KERNEL_U_ADDR_BSD.  */
  207.  
  208. void
  209. _initialize_kernel_u_addr ()
  210. {
  211. #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
  212.   struct nlist names[2];
  213.  
  214.   names[0].n_un.n_name = "_u";
  215.   names[1].n_un.n_name = NULL;
  216.   if (nlist ("/vmunix", names) == 0)
  217.     kernel_u_addr = names[0].n_value;
  218.   else
  219.     fatal ("Unable to get kernel u area address.");
  220. #endif /* KERNEL_U_ADDR_BSD.  */
  221. }
  222.  
  223. #if !defined (FETCH_INFERIOR_REGISTERS)
  224.  
  225. #if !defined (offsetof)
  226. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  227. #endif
  228.  
  229. /* U_REGS_OFFSET is the offset of the registers within the u area.  */
  230. #if !defined (U_REGS_OFFSET)
  231. #define U_REGS_OFFSET \
  232.   ptrace (PT_READ_U, inferior_pid, \
  233.       (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
  234.     - KERNEL_U_ADDR
  235. #endif
  236.  
  237. /* Registers we shouldn't try to fetch.  */
  238. #if !defined (CANNOT_FETCH_REGISTER)
  239. #define CANNOT_FETCH_REGISTER(regno) 0
  240. #endif
  241.  
  242. /* Fetch one register.  */
  243.  
  244. static void
  245. fetch_register (regno)
  246.      int regno;
  247. {
  248.   /* This isn't really an address.  But ptrace thinks of it as one.  */
  249.   CORE_ADDR regaddr;
  250.   char buf[MAX_REGISTER_RAW_SIZE];
  251.   char mess[128];                /* For messages */
  252.   register int i;
  253.  
  254.   /* Offset of registers within the u area.  */
  255.   unsigned int offset;
  256.  
  257.   if (CANNOT_FETCH_REGISTER (regno))
  258.     {
  259.       memset (buf, '\0', REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
  260.       supply_register (regno, buf);
  261.       return;
  262.     }
  263.  
  264.   offset = U_REGS_OFFSET;
  265.  
  266.   regaddr = register_addr (regno, offset);
  267.   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
  268.     {
  269.       errno = 0;
  270.       *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
  271.                           (PTRACE_ARG3_TYPE) regaddr, 0);
  272.       regaddr += sizeof (PTRACE_XFER_TYPE);
  273.       if (errno != 0)
  274.     {
  275.       sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
  276.       perror_with_name (mess);
  277.     }
  278.     }
  279.   supply_register (regno, buf);
  280. }
  281.  
  282.  
  283. /* Fetch all registers, or just one, from the child process.  */
  284.  
  285. void
  286. fetch_inferior_registers (regno)
  287.      int regno;
  288. {
  289.   int numregs;
  290.  
  291.   if (regno == -1)
  292.     {
  293.       numregs = ARCH_NUM_REGS;
  294.       for (regno = 0; regno < numregs; regno++)
  295.         fetch_register (regno);
  296.     }
  297.   else
  298.     fetch_register (regno);
  299. }
  300.  
  301. /* Registers we shouldn't try to store.  */
  302. #if !defined (CANNOT_STORE_REGISTER)
  303. #define CANNOT_STORE_REGISTER(regno) 0
  304. #endif
  305.  
  306. /* Store our register values back into the inferior.
  307.    If REGNO is -1, do this for all registers.
  308.    Otherwise, REGNO specifies which register (so we can save time).  */
  309.  
  310. void
  311. store_inferior_registers (regno)
  312.      int regno;
  313. {
  314.   /* This isn't really an address.  But ptrace thinks of it as one.  */
  315.   CORE_ADDR regaddr;
  316.   char buf[80];
  317.   register int i, numregs;
  318.  
  319.   unsigned int offset = U_REGS_OFFSET;
  320.  
  321.   if (regno >= 0)
  322.     {
  323.       regaddr = register_addr (regno, offset);
  324.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
  325.     {
  326.       errno = 0;
  327.       ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
  328.           *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]);
  329.       if (errno != 0)
  330.         {
  331.           sprintf (buf, "writing register number %d(%d)", regno, i);
  332.           perror_with_name (buf);
  333.         }
  334.       regaddr += sizeof(PTRACE_XFER_TYPE);
  335.     }
  336.     }
  337.   else
  338.     {
  339.       numregs = ARCH_NUM_REGS;
  340.       for (regno = 0; regno < numregs; regno++)
  341.     {
  342.       if (CANNOT_STORE_REGISTER (regno))
  343.         continue;
  344.       regaddr = register_addr (regno, offset);
  345.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
  346.         {
  347.           errno = 0;
  348.           ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
  349.               *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]);
  350.           if (errno != 0)
  351.         {
  352.           sprintf (buf, "writing register number %d(%d)", regno, i);
  353.           perror_with_name (buf);
  354.         }
  355.           regaddr += sizeof(PTRACE_XFER_TYPE);
  356.         }
  357.     }
  358.     }
  359. }
  360. #endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
  361.  
  362.  
  363. #if !defined (CHILD_XFER_MEMORY)
  364. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  365.    in the NEW_SUN_PTRACE case.
  366.    It ought to be straightforward.  But it appears that writing did
  367.    not write the data that I specified.  I cannot understand where
  368.    it got the data that it actually did write.  */
  369.  
  370. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  371.    to debugger memory starting at MYADDR.   Copy to inferior if
  372.    WRITE is nonzero.
  373.   
  374.    Returns the length copied, which is either the LEN argument or zero.
  375.    This xfer function does not do partial moves, since child_ops
  376.    doesn't allow memory operations to cross below us in the target stack
  377.    anyway.  */
  378.  
  379. int
  380. child_xfer_memory (memaddr, myaddr, len, write, target)
  381.      CORE_ADDR memaddr;
  382.      char *myaddr;
  383.      int len;
  384.      int write;
  385.      struct target_ops *target;        /* ignored */
  386. {
  387.   register int i;
  388.   /* Round starting address down to longword boundary.  */
  389.   register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
  390.   /* Round ending address up; get number of longwords that makes.  */
  391.   register int count
  392.     = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
  393.       / sizeof (PTRACE_XFER_TYPE);
  394.   /* Allocate buffer of that many longwords.  */
  395.   register PTRACE_XFER_TYPE *buffer
  396.     = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
  397.  
  398.   if (write)
  399.     {
  400.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  401.  
  402.       if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
  403.     /* Need part of initial word -- fetch it.  */
  404.         buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  405.                 0);
  406.       }
  407.  
  408.       if (count > 1)        /* FIXME, avoid if even boundary */
  409.     {
  410.       buffer[count - 1]
  411.         = ptrace (PT_READ_I, inferior_pid,
  412.               ((PTRACE_ARG3_TYPE)
  413.                (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
  414.               0);
  415.     }
  416.  
  417.       /* Copy data to be written over corresponding part of buffer */
  418.  
  419.       memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
  420.           myaddr,
  421.           len);
  422.  
  423.       /* Write the entire buffer.  */
  424.  
  425.       for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
  426.     {
  427.       errno = 0;
  428.       ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  429.           buffer[i]);
  430.       if (errno)
  431.         {
  432.           /* Using the appropriate one (I or D) is necessary for
  433.          Gould NP1, at least.  */
  434.           errno = 0;
  435.           ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
  436.               buffer[i]);
  437.         }
  438.       if (errno)
  439.         return 0;
  440.     }
  441.     }
  442.   else
  443.     {
  444.       /* Read all the longwords */
  445.       for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
  446.     {
  447.       errno = 0;
  448.       buffer[i] = ptrace (PT_READ_I, inferior_pid,
  449.                   (PTRACE_ARG3_TYPE) addr, 0);
  450.       if (errno)
  451.         return 0;
  452.       QUIT;
  453.     }
  454.  
  455.       /* Copy appropriate bytes out of the buffer.  */
  456.       memcpy (myaddr,
  457.           (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
  458.           len);
  459.     }
  460.   return len;
  461. }
  462. #endif /* !defined (CHILD_XFER_MEMORY).  */
  463.