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