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