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