home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / infptrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  11.2 KB  |  436 lines

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