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