* configure: Ignore new autoconf configure options.
[binutils-gdb.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, 1993, 1994, 1995
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "target.h"
25 #include "gdb_string.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
36 #ifndef NO_PTRACE_H
37 #ifdef PTRACE_IN_WRONG_PLACE
38 #include <ptrace.h>
39 #else
40 #include <sys/ptrace.h>
41 #endif
42 #endif /* NO_PTRACE_H */
43
44 #if !defined (PT_KILL)
45 #define PT_KILL 8
46 #endif
47
48 #if !defined (PT_STEP)
49 #define PT_STEP 9
50 #define PT_CONTINUE 7
51 #define PT_READ_U 3
52 #define PT_WRITE_U 6
53 #define PT_READ_I 1
54 #define PT_READ_D 2
55 #define PT_WRITE_I 4
56 #define PT_WRITE_D 5
57 #endif /* No PT_STEP. */
58
59 #ifndef PT_ATTACH
60 #define PT_ATTACH PTRACE_ATTACH
61 #endif
62 #ifndef PT_DETACH
63 #define PT_DETACH PTRACE_DETACH
64 #endif
65
66 #include "gdbcore.h"
67 #ifndef NO_SYS_FILE
68 #include <sys/file.h>
69 #endif
70 #if 0
71 /* Don't think this is used anymore. On the sequent (not sure whether it's
72 dynix or ptx or both), it is included unconditionally by sys/user.h and
73 not protected against multiple inclusion. */
74 #include "gdb_stat.h"
75 #endif
76
77 #if !defined (FETCH_INFERIOR_REGISTERS)
78 #include <sys/user.h> /* Probably need to poke the user structure */
79 #if defined (KERNEL_U_ADDR_BSD)
80 #include <a.out.h> /* For struct nlist */
81 #endif /* KERNEL_U_ADDR_BSD. */
82 #endif /* !FETCH_INFERIOR_REGISTERS */
83
84 \f
85 /* This function simply calls ptrace with the given arguments.
86 It exists so that all calls to ptrace are isolated in this
87 machine-dependent file. */
88 int
89 call_ptrace (request, pid, addr, data)
90 int request, pid;
91 PTRACE_ARG3_TYPE addr;
92 int data;
93 {
94 return ptrace (request, pid, addr, data
95 #if defined (FIVE_ARG_PTRACE)
96 /* Deal with HPUX 8.0 braindamage. We never use the
97 calls which require the fifth argument. */
98 , 0
99 #endif
100 );
101 }
102
103 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
104 /* For the rest of the file, use an extra level of indirection */
105 /* This lets us breakpoint usefully on call_ptrace. */
106 #define ptrace call_ptrace
107 #endif
108
109 void
110 kill_inferior ()
111 {
112 if (inferior_pid == 0)
113 return;
114 /* ptrace PT_KILL only works if process is stopped!!! So stop it with
115 a real signal first, if we can. FIXME: This is bogus. When the inferior
116 is not stopped, GDB should just be waiting for it. Either the following
117 line is unecessary, or there is some problem elsewhere in GDB which
118 causes us to get here when the inferior is not stopped. */
119 kill (inferior_pid, SIGKILL);
120 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
121 wait ((int *)0);
122 target_mourn_inferior ();
123 }
124
125 #ifndef CHILD_RESUME
126
127 /* Resume execution of the inferior process.
128 If STEP is nonzero, single-step it.
129 If SIGNAL is nonzero, give it that signal. */
130
131 void
132 child_resume (pid, step, signal)
133 int pid;
134 int step;
135 enum target_signal signal;
136 {
137 errno = 0;
138
139 if (pid == -1)
140 /* Resume all threads. */
141 /* I think this only gets used in the non-threaded case, where "resume
142 all threads" and "resume inferior_pid" are the same. */
143 pid = inferior_pid;
144
145 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
146 it was. (If GDB wanted it to start some other way, we have already
147 written a new PC value to the child.)
148
149 If this system does not support PT_STEP, a higher level function will
150 have called single_step() to transmute the step request into a
151 continue request (by setting breakpoints on all possible successor
152 instructions), so we don't have to worry about that here. */
153
154 if (step)
155 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
156 target_signal_to_host (signal));
157 else
158 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
159 target_signal_to_host (signal));
160
161 if (errno)
162 perror_with_name ("ptrace");
163 }
164 #endif /* CHILD_RESUME */
165
166 \f
167 #ifdef ATTACH_DETACH
168 /* Start debugging the process whose number is PID. */
169 int
170 attach (pid)
171 int pid;
172 {
173 errno = 0;
174 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
175 if (errno)
176 perror_with_name ("ptrace");
177 attach_flag = 1;
178 return pid;
179 }
180
181 /* Stop debugging the process whose number is PID
182 and continue it with signal number SIGNAL.
183 SIGNAL = 0 means just continue it. */
184
185 void
186 detach (signal)
187 int signal;
188 {
189 errno = 0;
190 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
191 if (errno)
192 perror_with_name ("ptrace");
193 attach_flag = 0;
194 }
195 #endif /* ATTACH_DETACH */
196 \f
197 /* Default the type of the ptrace transfer to int. */
198 #ifndef PTRACE_XFER_TYPE
199 #define PTRACE_XFER_TYPE int
200 #endif
201
202 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
203 to get the offset in the core file of the register values. */
204 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
205 /* Get kernel_u_addr using BSD-style nlist(). */
206 CORE_ADDR kernel_u_addr;
207 #endif /* KERNEL_U_ADDR_BSD. */
208
209 void
210 _initialize_kernel_u_addr ()
211 {
212 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
213 struct nlist names[2];
214
215 names[0].n_un.n_name = "_u";
216 names[1].n_un.n_name = NULL;
217 if (nlist ("/vmunix", names) == 0)
218 kernel_u_addr = names[0].n_value;
219 else
220 fatal ("Unable to get kernel u area address.");
221 #endif /* KERNEL_U_ADDR_BSD. */
222 }
223
224 #if !defined (FETCH_INFERIOR_REGISTERS)
225
226 #if !defined (offsetof)
227 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
228 #endif
229
230 /* U_REGS_OFFSET is the offset of the registers within the u area. */
231 #if !defined (U_REGS_OFFSET)
232 #define U_REGS_OFFSET \
233 ptrace (PT_READ_U, inferior_pid, \
234 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
235 - KERNEL_U_ADDR
236 #endif
237
238 /* Registers we shouldn't try to fetch. */
239 #if !defined (CANNOT_FETCH_REGISTER)
240 #define CANNOT_FETCH_REGISTER(regno) 0
241 #endif
242
243 /* Fetch one register. */
244
245 static void
246 fetch_register (regno)
247 int regno;
248 {
249 /* This isn't really an address. But ptrace thinks of it as one. */
250 CORE_ADDR regaddr;
251 char buf[MAX_REGISTER_RAW_SIZE];
252 char mess[128]; /* For messages */
253 register int i;
254
255 /* Offset of registers within the u area. */
256 unsigned int offset;
257
258 if (CANNOT_FETCH_REGISTER (regno))
259 {
260 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
261 supply_register (regno, buf);
262 return;
263 }
264
265 offset = U_REGS_OFFSET;
266
267 regaddr = register_addr (regno, offset);
268 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
269 {
270 errno = 0;
271 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
272 (PTRACE_ARG3_TYPE) regaddr, 0);
273 regaddr += sizeof (PTRACE_XFER_TYPE);
274 if (errno != 0)
275 {
276 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
277 perror_with_name (mess);
278 }
279 }
280 supply_register (regno, buf);
281 }
282
283
284 /* Fetch all registers, or just one, from the child process. */
285
286 void
287 fetch_inferior_registers (regno)
288 int regno;
289 {
290 int numregs;
291
292 if (regno == -1)
293 {
294 numregs = ARCH_NUM_REGS;
295 for (regno = 0; regno < numregs; regno++)
296 fetch_register (regno);
297 }
298 else
299 fetch_register (regno);
300 }
301
302 /* Registers we shouldn't try to store. */
303 #if !defined (CANNOT_STORE_REGISTER)
304 #define CANNOT_STORE_REGISTER(regno) 0
305 #endif
306
307 /* Store our register values back into the inferior.
308 If REGNO is -1, do this for all registers.
309 Otherwise, REGNO specifies which register (so we can save time). */
310
311 void
312 store_inferior_registers (regno)
313 int regno;
314 {
315 /* This isn't really an address. But ptrace thinks of it as one. */
316 CORE_ADDR regaddr;
317 char buf[80];
318 register int i, numregs;
319
320 unsigned int offset = U_REGS_OFFSET;
321
322 if (regno >= 0)
323 {
324 regaddr = register_addr (regno, offset);
325 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
326 {
327 errno = 0;
328 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
329 *(PTRACE_XFER_TYPE *) &registers[REGISTER_BYTE (regno) + i]);
330 if (errno != 0)
331 {
332 sprintf (buf, "writing register number %d(%d)", regno, i);
333 perror_with_name (buf);
334 }
335 regaddr += sizeof(PTRACE_XFER_TYPE);
336 }
337 }
338 else
339 {
340 numregs = ARCH_NUM_REGS;
341 for (regno = 0; regno < numregs; regno++)
342 {
343 if (CANNOT_STORE_REGISTER (regno))
344 continue;
345 regaddr = register_addr (regno, offset);
346 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
347 {
348 errno = 0;
349 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
350 *(PTRACE_XFER_TYPE *) &registers[REGISTER_BYTE (regno) + i]);
351 if (errno != 0)
352 {
353 sprintf (buf, "writing register number %d(%d)", regno, i);
354 perror_with_name (buf);
355 }
356 regaddr += sizeof(PTRACE_XFER_TYPE);
357 }
358 }
359 }
360 }
361 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
362 \f
363
364 #if !defined (CHILD_XFER_MEMORY)
365 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
366 in the NEW_SUN_PTRACE case.
367 It ought to be straightforward. But it appears that writing did
368 not write the data that I specified. I cannot understand where
369 it got the data that it actually did write. */
370
371 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
372 to debugger memory starting at MYADDR. Copy to inferior if
373 WRITE is nonzero.
374
375 Returns the length copied, which is either the LEN argument or zero.
376 This xfer function does not do partial moves, since child_ops
377 doesn't allow memory operations to cross below us in the target stack
378 anyway. */
379
380 int
381 child_xfer_memory (memaddr, myaddr, len, write, target)
382 CORE_ADDR memaddr;
383 char *myaddr;
384 int len;
385 int write;
386 struct target_ops *target; /* ignored */
387 {
388 register int i;
389 /* Round starting address down to longword boundary. */
390 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
391 /* Round ending address up; get number of longwords that makes. */
392 register int count
393 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
394 / sizeof (PTRACE_XFER_TYPE);
395 /* Allocate buffer of that many longwords. */
396 register PTRACE_XFER_TYPE *buffer
397 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
398
399 if (write)
400 {
401 /* Fill start and end extra bytes of buffer with existing memory data. */
402
403 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
404 /* Need part of initial word -- fetch it. */
405 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
406 0);
407 }
408
409 if (count > 1) /* FIXME, avoid if even boundary */
410 {
411 buffer[count - 1]
412 = ptrace (PT_READ_I, inferior_pid,
413 ((PTRACE_ARG3_TYPE)
414 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
415 0);
416 }
417
418 /* Copy data to be written over corresponding part of buffer */
419
420 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
421 myaddr,
422 len);
423
424 /* Write the entire buffer. */
425
426 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
427 {
428 errno = 0;
429 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
430 buffer[i]);
431 if (errno)
432 {
433 /* Using the appropriate one (I or D) is necessary for
434 Gould NP1, at least. */
435 errno = 0;
436 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
437 buffer[i]);
438 }
439 if (errno)
440 return 0;
441 }
442 }
443 else
444 {
445 /* Read all the longwords */
446 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
447 {
448 errno = 0;
449 buffer[i] = ptrace (PT_READ_I, inferior_pid,
450 (PTRACE_ARG3_TYPE) addr, 0);
451 if (errno)
452 return 0;
453 QUIT;
454 }
455
456 /* Copy appropriate bytes out of the buffer. */
457 memcpy (myaddr,
458 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
459 len);
460 }
461 return len;
462 }
463 #endif /* !defined (CHILD_XFER_MEMORY). */