Update/correct copyright notices.
[deliverable/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, 1996, 1998,
3 1999, 2000, 2001
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "target.h"
27 #include "gdb_string.h"
28 #include "regcache.h"
29
30 #include "gdb_wait.h"
31
32 #include "command.h"
33
34 #ifdef USG
35 #include <sys/types.h>
36 #endif
37
38 #include <sys/param.h>
39 #include "gdb_dirent.h"
40 #include <signal.h>
41 #include <sys/ioctl.h>
42
43 #ifdef HAVE_PTRACE_H
44 #include <ptrace.h>
45 #else
46 #ifdef HAVE_SYS_PTRACE_H
47 #include <sys/ptrace.h>
48 #endif
49 #endif
50
51 #if !defined (PT_READ_I)
52 #define PT_READ_I 1 /* Read word from text space */
53 #endif
54 #if !defined (PT_READ_D)
55 #define PT_READ_D 2 /* Read word from data space */
56 #endif
57 #if !defined (PT_READ_U)
58 #define PT_READ_U 3 /* Read word from kernel user struct */
59 #endif
60 #if !defined (PT_WRITE_I)
61 #define PT_WRITE_I 4 /* Write word to text space */
62 #endif
63 #if !defined (PT_WRITE_D)
64 #define PT_WRITE_D 5 /* Write word to data space */
65 #endif
66 #if !defined (PT_WRITE_U)
67 #define PT_WRITE_U 6 /* Write word to kernel user struct */
68 #endif
69 #if !defined (PT_CONTINUE)
70 #define PT_CONTINUE 7 /* Continue after signal */
71 #endif
72 #if !defined (PT_STEP)
73 #define PT_STEP 9 /* Set flag for single stepping */
74 #endif
75 #if !defined (PT_KILL)
76 #define PT_KILL 8 /* Send child a SIGKILL signal */
77 #endif
78
79 #ifndef PT_ATTACH
80 #define PT_ATTACH PTRACE_ATTACH
81 #endif
82 #ifndef PT_DETACH
83 #define PT_DETACH PTRACE_DETACH
84 #endif
85
86 #include "gdbcore.h"
87 #ifndef NO_SYS_FILE
88 #include <sys/file.h>
89 #endif
90 #if 0
91 /* Don't think this is used anymore. On the sequent (not sure whether it's
92 dynix or ptx or both), it is included unconditionally by sys/user.h and
93 not protected against multiple inclusion. */
94 #include "gdb_stat.h"
95 #endif
96
97 #if !defined (FETCH_INFERIOR_REGISTERS)
98 #include <sys/user.h> /* Probably need to poke the user structure */
99 #if defined (KERNEL_U_ADDR_BSD)
100 #include <a.out.h> /* For struct nlist */
101 #endif /* KERNEL_U_ADDR_BSD. */
102 #endif /* !FETCH_INFERIOR_REGISTERS */
103
104 #if !defined (CHILD_XFER_MEMORY)
105 static void udot_info (char *, int);
106 #endif
107
108 #if !defined (FETCH_INFERIOR_REGISTERS)
109 static void fetch_register (int);
110 static void store_register (int);
111 #endif
112
113 /*
114 * Some systems (Linux) may have threads implemented as pseudo-processes,
115 * in which case we may be tracing more than one process at a time.
116 * In that case, inferior_pid will contain the main process ID and the
117 * individual thread (process) id mashed together. These macros are
118 * used to separate them out. The definitions may be overridden in tm.h
119 *
120 * NOTE: default definitions here are for systems with no threads.
121 * Useful definitions MUST be provided in tm.h
122 */
123
124 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
125 #define PIDGET(PID) PID
126 #define TIDGET(PID) 0
127 #endif
128
129 void _initialize_kernel_u_addr (void);
130 void _initialize_infptrace (void);
131 \f
132
133 /* This function simply calls ptrace with the given arguments.
134 It exists so that all calls to ptrace are isolated in this
135 machine-dependent file. */
136 int
137 call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
138 {
139 int pt_status = 0;
140
141 #if 0
142 int saved_errno;
143
144 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
145 request, pid, addr, data);
146 #endif
147 #if defined(PT_SETTRC)
148 /* If the parent can be told to attach to us, try to do it. */
149 if (request == PT_SETTRC)
150 {
151 errno = 0;
152 #if !defined (FIVE_ARG_PTRACE)
153 pt_status = ptrace (PT_SETTRC, pid, addr, data);
154 #else
155 /* Deal with HPUX 8.0 braindamage. We never use the
156 calls which require the fifth argument. */
157 pt_status = ptrace (PT_SETTRC, pid, addr, data, 0);
158 #endif
159 if (errno)
160 perror_with_name ("ptrace");
161 #if 0
162 printf (" = %d\n", pt_status);
163 #endif
164 if (pt_status < 0)
165 return pt_status;
166 else
167 return parent_attach_all (pid, addr, data);
168 }
169 #endif
170
171 #if defined(PT_CONTIN1)
172 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
173 signals. If it's available, use it. */
174 if (request == PT_CONTINUE)
175 request = PT_CONTIN1;
176 #endif
177
178 #if defined(PT_SINGLE1)
179 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
180 signals. If it's available, use it. */
181 if (request == PT_STEP)
182 request = PT_SINGLE1;
183 #endif
184
185 #if 0
186 saved_errno = errno;
187 errno = 0;
188 #endif
189 #if !defined (FIVE_ARG_PTRACE)
190 pt_status = ptrace (request, pid, addr, data);
191 #else
192 /* Deal with HPUX 8.0 braindamage. We never use the
193 calls which require the fifth argument. */
194 pt_status = ptrace (request, pid, addr, data, 0);
195 #endif
196
197 #if 0
198 if (errno)
199 printf (" [errno = %d]", errno);
200
201 errno = saved_errno;
202 printf (" = 0x%x\n", pt_status);
203 #endif
204 return pt_status;
205 }
206
207
208 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
209 /* For the rest of the file, use an extra level of indirection */
210 /* This lets us breakpoint usefully on call_ptrace. */
211 #define ptrace call_ptrace
212 #endif
213
214 /* Wait for a process to finish, possibly running a target-specific
215 hook before returning. */
216
217 int
218 ptrace_wait (int pid, int *status)
219 {
220 int wstate;
221
222 wstate = wait (status);
223 target_post_wait (wstate, *status);
224 return wstate;
225 }
226
227 void
228 kill_inferior (void)
229 {
230 int status;
231
232 if (inferior_pid == 0)
233 return;
234
235 /* This once used to call "kill" to kill the inferior just in case
236 the inferior was still running. As others have noted in the past
237 (kingdon) there shouldn't be any way to get here if the inferior
238 is still running -- else there's a major problem elsewere in gdb
239 and it needs to be fixed.
240
241 The kill call causes problems under hpux10, so it's been removed;
242 if this causes problems we'll deal with them as they arise. */
243 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
244 ptrace_wait (0, &status);
245 target_mourn_inferior ();
246 }
247
248 #ifndef CHILD_RESUME
249
250 /* Resume execution of the inferior process.
251 If STEP is nonzero, single-step it.
252 If SIGNAL is nonzero, give it that signal. */
253
254 void
255 child_resume (int pid, int step, enum target_signal signal)
256 {
257 errno = 0;
258
259 if (pid == -1)
260 /* Resume all threads. */
261 /* I think this only gets used in the non-threaded case, where "resume
262 all threads" and "resume inferior_pid" are the same. */
263 pid = inferior_pid;
264
265 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
266 it was. (If GDB wanted it to start some other way, we have already
267 written a new PC value to the child.)
268
269 If this system does not support PT_STEP, a higher level function will
270 have called single_step() to transmute the step request into a
271 continue request (by setting breakpoints on all possible successor
272 instructions), so we don't have to worry about that here. */
273
274 if (step)
275 {
276 if (SOFTWARE_SINGLE_STEP_P)
277 internal_error (__FILE__, __LINE__, "failed internal consistency check"); /* Make sure this doesn't happen. */
278 else
279 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
280 target_signal_to_host (signal));
281 }
282 else
283 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
284 target_signal_to_host (signal));
285
286 if (errno)
287 {
288 perror_with_name ("ptrace");
289 }
290 }
291 #endif /* CHILD_RESUME */
292 \f
293
294 #ifdef ATTACH_DETACH
295 /* Start debugging the process whose number is PID. */
296 int
297 attach (int pid)
298 {
299 errno = 0;
300 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
301 if (errno)
302 perror_with_name ("ptrace");
303 attach_flag = 1;
304 return pid;
305 }
306
307 /* Stop debugging the process whose number is PID
308 and continue it with signal number SIGNAL.
309 SIGNAL = 0 means just continue it. */
310
311 void
312 detach (int signal)
313 {
314 errno = 0;
315 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
316 if (errno)
317 perror_with_name ("ptrace");
318 attach_flag = 0;
319 }
320 #endif /* ATTACH_DETACH */
321 \f
322 /* Default the type of the ptrace transfer to int. */
323 #ifndef PTRACE_XFER_TYPE
324 #define PTRACE_XFER_TYPE int
325 #endif
326
327 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
328 to get the offset in the core file of the register values. */
329 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
330 /* Get kernel_u_addr using BSD-style nlist(). */
331 CORE_ADDR kernel_u_addr;
332 #endif /* KERNEL_U_ADDR_BSD. */
333
334 void
335 _initialize_kernel_u_addr (void)
336 {
337 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
338 struct nlist names[2];
339
340 names[0].n_un.n_name = "_u";
341 names[1].n_un.n_name = NULL;
342 if (nlist ("/vmunix", names) == 0)
343 kernel_u_addr = names[0].n_value;
344 else
345 internal_error (__FILE__, __LINE__,
346 "Unable to get kernel u area address.");
347 #endif /* KERNEL_U_ADDR_BSD. */
348 }
349
350 #if !defined (FETCH_INFERIOR_REGISTERS)
351
352 #if !defined (offsetof)
353 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
354 #endif
355
356 /* U_REGS_OFFSET is the offset of the registers within the u area. */
357 #if !defined (U_REGS_OFFSET)
358 #define U_REGS_OFFSET \
359 ptrace (PT_READ_U, inferior_pid, \
360 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
361 - KERNEL_U_ADDR
362 #endif
363
364 /* Registers we shouldn't try to fetch. */
365 #if !defined (CANNOT_FETCH_REGISTER)
366 #define CANNOT_FETCH_REGISTER(regno) 0
367 #endif
368
369 /* Fetch one register. */
370
371 static void
372 fetch_register (int regno)
373 {
374 /* This isn't really an address. But ptrace thinks of it as one. */
375 CORE_ADDR regaddr;
376 char mess[128]; /* For messages */
377 register int i;
378 unsigned int offset; /* Offset of registers within the u area. */
379 char buf[MAX_REGISTER_RAW_SIZE];
380 int tid;
381
382 if (CANNOT_FETCH_REGISTER (regno))
383 {
384 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
385 supply_register (regno, buf);
386 return;
387 }
388
389 /* Overload thread id onto process id */
390 if ((tid = TIDGET (inferior_pid)) == 0)
391 tid = inferior_pid; /* no thread id, just use process id */
392
393 offset = U_REGS_OFFSET;
394
395 regaddr = register_addr (regno, offset);
396 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
397 {
398 errno = 0;
399 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
400 (PTRACE_ARG3_TYPE) regaddr, 0);
401 regaddr += sizeof (PTRACE_XFER_TYPE);
402 if (errno != 0)
403 {
404 sprintf (mess, "reading register %s (#%d)",
405 REGISTER_NAME (regno), regno);
406 perror_with_name (mess);
407 }
408 }
409 supply_register (regno, buf);
410 }
411
412
413 /* Fetch register values from the inferior.
414 If REGNO is negative, do this for all registers.
415 Otherwise, REGNO specifies which register (so we can save time). */
416
417 void
418 fetch_inferior_registers (int regno)
419 {
420 if (regno >= 0)
421 {
422 fetch_register (regno);
423 }
424 else
425 {
426 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
427 {
428 fetch_register (regno);
429 }
430 }
431 }
432
433 /* Registers we shouldn't try to store. */
434 #if !defined (CANNOT_STORE_REGISTER)
435 #define CANNOT_STORE_REGISTER(regno) 0
436 #endif
437
438 /* Store one register. */
439
440 static void
441 store_register (int regno)
442 {
443 /* This isn't really an address. But ptrace thinks of it as one. */
444 CORE_ADDR regaddr;
445 char mess[128]; /* For messages */
446 register int i;
447 unsigned int offset; /* Offset of registers within the u area. */
448 int tid;
449
450 if (CANNOT_STORE_REGISTER (regno))
451 {
452 return;
453 }
454
455 /* Overload thread id onto process id */
456 if ((tid = TIDGET (inferior_pid)) == 0)
457 tid = inferior_pid; /* no thread id, just use process id */
458
459 offset = U_REGS_OFFSET;
460
461 regaddr = register_addr (regno, offset);
462 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
463 {
464 errno = 0;
465 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
466 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
467 regaddr += sizeof (PTRACE_XFER_TYPE);
468 if (errno != 0)
469 {
470 sprintf (mess, "writing register %s (#%d)",
471 REGISTER_NAME (regno), regno);
472 perror_with_name (mess);
473 }
474 }
475 }
476
477 /* Store our register values back into the inferior.
478 If REGNO is negative, do this for all registers.
479 Otherwise, REGNO specifies which register (so we can save time). */
480
481 void
482 store_inferior_registers (int regno)
483 {
484 if (regno >= 0)
485 {
486 store_register (regno);
487 }
488 else
489 {
490 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
491 {
492 store_register (regno);
493 }
494 }
495 }
496 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
497 \f
498
499 #if !defined (CHILD_XFER_MEMORY)
500 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
501 in the NEW_SUN_PTRACE case.
502 It ought to be straightforward. But it appears that writing did
503 not write the data that I specified. I cannot understand where
504 it got the data that it actually did write. */
505
506 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
507 to debugger memory starting at MYADDR. Copy to inferior if
508 WRITE is nonzero. TARGET is ignored.
509
510 Returns the length copied, which is either the LEN argument or zero.
511 This xfer function does not do partial moves, since child_ops
512 doesn't allow memory operations to cross below us in the target stack
513 anyway. */
514
515 int
516 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
517 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
518 struct target_ops *target)
519 {
520 register int i;
521 /* Round starting address down to longword boundary. */
522 register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
523 /* Round ending address up; get number of longwords that makes. */
524 register int count
525 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
526 / sizeof (PTRACE_XFER_TYPE);
527 /* Allocate buffer of that many longwords. */
528 register PTRACE_XFER_TYPE *buffer
529 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
530
531 if (write)
532 {
533 /* Fill start and end extra bytes of buffer with existing memory data. */
534
535 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
536 {
537 /* Need part of initial word -- fetch it. */
538 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_pid),
539 (PTRACE_ARG3_TYPE) addr, 0);
540 }
541
542 if (count > 1) /* FIXME, avoid if even boundary */
543 {
544 buffer[count - 1]
545 = ptrace (PT_READ_I, PIDGET (inferior_pid),
546 ((PTRACE_ARG3_TYPE)
547 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
548 0);
549 }
550
551 /* Copy data to be written over corresponding part of buffer */
552
553 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
554 myaddr,
555 len);
556
557 /* Write the entire buffer. */
558
559 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
560 {
561 errno = 0;
562 ptrace (PT_WRITE_D, PIDGET (inferior_pid),
563 (PTRACE_ARG3_TYPE) addr, buffer[i]);
564 if (errno)
565 {
566 /* Using the appropriate one (I or D) is necessary for
567 Gould NP1, at least. */
568 errno = 0;
569 ptrace (PT_WRITE_I, PIDGET (inferior_pid),
570 (PTRACE_ARG3_TYPE) addr, buffer[i]);
571 }
572 if (errno)
573 return 0;
574 }
575 #ifdef CLEAR_INSN_CACHE
576 CLEAR_INSN_CACHE ();
577 #endif
578 }
579 else
580 {
581 /* Read all the longwords */
582 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
583 {
584 errno = 0;
585 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_pid),
586 (PTRACE_ARG3_TYPE) addr, 0);
587 if (errno)
588 return 0;
589 QUIT;
590 }
591
592 /* Copy appropriate bytes out of the buffer. */
593 memcpy (myaddr,
594 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
595 len);
596 }
597 return len;
598 }
599 \f
600
601 static void
602 udot_info (char *dummy1, int dummy2)
603 {
604 #if defined (KERNEL_U_SIZE)
605 int udot_off; /* Offset into user struct */
606 int udot_val; /* Value from user struct at udot_off */
607 char mess[128]; /* For messages */
608 #endif
609
610 if (!target_has_execution)
611 {
612 error ("The program is not being run.");
613 }
614
615 #if !defined (KERNEL_U_SIZE)
616
617 /* Adding support for this command is easy. Typically you just add a
618 routine, called "kernel_u_size" that returns the size of the user
619 struct, to the appropriate *-nat.c file and then add to the native
620 config file "#define KERNEL_U_SIZE kernel_u_size()" */
621 error ("Don't know how large ``struct user'' is in this version of gdb.");
622
623 #else
624
625 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
626 {
627 if ((udot_off % 24) == 0)
628 {
629 if (udot_off > 0)
630 {
631 printf_filtered ("\n");
632 }
633 printf_filtered ("%04x:", udot_off);
634 }
635 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
636 if (errno != 0)
637 {
638 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
639 perror_with_name (mess);
640 }
641 /* Avoid using nonportable (?) "*" in print specs */
642 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
643 }
644 printf_filtered ("\n");
645
646 #endif
647 }
648 #endif /* !defined (CHILD_XFER_MEMORY). */
649 \f
650
651 void
652 _initialize_infptrace (void)
653 {
654 #if !defined (CHILD_XFER_MEMORY)
655 add_info ("udot", udot_info,
656 "Print contents of kernel ``struct user'' for current child.");
657 #endif
658 }
This page took 0.075221 seconds and 5 git commands to generate.