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