1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 89, 90, 91, 92, 93, 94, 95, 96, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
31 #ifdef HAVE_SYS_WAIT_H
36 /* "wait.h" fills in the gaps left by <wait.h> */
37 #include "wait.h" /* NOTE: This is ../include/wait.h */
42 #include <sys/types.h>
45 #include <sys/param.h>
48 #include <sys/ioctl.h>
53 #ifdef HAVE_SYS_PTRACE_H
54 #include <sys/ptrace.h>
58 #if !defined (PT_READ_I)
59 #define PT_READ_I 1 /* Read word from text space */
61 #if !defined (PT_READ_D)
62 #define PT_READ_D 2 /* Read word from data space */
64 #if !defined (PT_READ_U)
65 #define PT_READ_U 3 /* Read word from kernel user struct */
67 #if !defined (PT_WRITE_I)
68 #define PT_WRITE_I 4 /* Write word to text space */
70 #if !defined (PT_WRITE_D)
71 #define PT_WRITE_D 5 /* Write word to data space */
73 #if !defined (PT_WRITE_U)
74 #define PT_WRITE_U 6 /* Write word to kernel user struct */
76 #if !defined (PT_CONTINUE)
77 #define PT_CONTINUE 7 /* Continue after signal */
79 #if !defined (PT_STEP)
80 #define PT_STEP 9 /* Set flag for single stepping */
82 #if !defined (PT_KILL)
83 #define PT_KILL 8 /* Send child a SIGKILL signal */
87 #define PT_ATTACH PTRACE_ATTACH
90 #define PT_DETACH PTRACE_DETACH
98 /* Don't think this is used anymore. On the sequent (not sure whether it's
99 dynix or ptx or both), it is included unconditionally by sys/user.h and
100 not protected against multiple inclusion. */
101 #include "gdb_stat.h"
104 #if !defined (FETCH_INFERIOR_REGISTERS)
105 #include <sys/user.h> /* Probably need to poke the user structure */
106 #if defined (KERNEL_U_ADDR_BSD)
107 #include <a.out.h> /* For struct nlist */
108 #endif /* KERNEL_U_ADDR_BSD. */
109 #endif /* !FETCH_INFERIOR_REGISTERS */
111 #if !defined (CHILD_XFER_MEMORY)
112 static void udot_info
PARAMS ((char *, int));
115 #if !defined (FETCH_INFERIOR_REGISTERS)
116 static void fetch_register
PARAMS ((int));
117 static void store_register
PARAMS ((int));
121 * Some systems (Linux) may have threads implemented as pseudo-processes,
122 * in which case we may be tracing more than one process at a time.
123 * In that case, inferior_pid will contain the main process ID and the
124 * individual thread (process) id mashed together. These macros are
125 * used to separate them out. The definitions may be overridden in tm.h
127 * NOTE: default definitions here are for systems with no threads.
128 * Useful definitions MUST be provided in tm.h
131 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
132 #define PIDGET(PID) PID
133 #define TIDGET(PID) 0
136 void _initialize_kernel_u_addr
PARAMS ((void));
137 void _initialize_infptrace
PARAMS ((void));
140 /* This function simply calls ptrace with the given arguments.
141 It exists so that all calls to ptrace are isolated in this
142 machine-dependent file. */
144 call_ptrace (request
, pid
, addr
, data
)
146 PTRACE_ARG3_TYPE addr
;
154 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
155 request
, pid
, addr
, data
);
157 #if defined(PT_SETTRC)
158 /* If the parent can be told to attach to us, try to do it. */
159 if (request
== PT_SETTRC
)
162 #if !defined (FIVE_ARG_PTRACE)
163 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
);
165 /* Deal with HPUX 8.0 braindamage. We never use the
166 calls which require the fifth argument. */
167 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
, 0);
170 perror_with_name ("ptrace");
172 printf (" = %d\n", pt_status
);
177 return parent_attach_all (pid
, addr
, data
);
181 #if defined(PT_CONTIN1)
182 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
183 signals. If it's available, use it. */
184 if (request
== PT_CONTINUE
)
185 request
= PT_CONTIN1
;
188 #if defined(PT_SINGLE1)
189 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
190 signals. If it's available, use it. */
191 if (request
== PT_STEP
)
192 request
= PT_SINGLE1
;
199 #if !defined (FIVE_ARG_PTRACE)
200 pt_status
= ptrace (request
, pid
, addr
, data
);
202 /* Deal with HPUX 8.0 braindamage. We never use the
203 calls which require the fifth argument. */
204 pt_status
= ptrace (request
, pid
, addr
, data
, 0);
209 printf (" [errno = %d]", errno
);
212 printf (" = 0x%x\n", pt_status
);
218 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
219 /* For the rest of the file, use an extra level of indirection */
220 /* This lets us breakpoint usefully on call_ptrace. */
221 #define ptrace call_ptrace
224 /* Wait for a process to finish, possibly running a target-specific
225 hook before returning. */
228 ptrace_wait (pid
, status
)
234 wstate
= wait (status
);
235 target_post_wait (wstate
, *status
);
244 if (inferior_pid
== 0)
247 /* This once used to call "kill" to kill the inferior just in case
248 the inferior was still running. As others have noted in the past
249 (kingdon) there shouldn't be any way to get here if the inferior
250 is still running -- else there's a major problem elsewere in gdb
251 and it needs to be fixed.
253 The kill call causes problems under hpux10, so it's been removed;
254 if this causes problems we'll deal with them as they arise. */
255 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
256 ptrace_wait (0, &status
);
257 target_mourn_inferior ();
262 /* Resume execution of the inferior process.
263 If STEP is nonzero, single-step it.
264 If SIGNAL is nonzero, give it that signal. */
267 child_resume (pid
, step
, signal
)
270 enum target_signal signal
;
275 /* Resume all threads. */
276 /* I think this only gets used in the non-threaded case, where "resume
277 all threads" and "resume inferior_pid" are the same. */
280 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
281 it was. (If GDB wanted it to start some other way, we have already
282 written a new PC value to the child.)
284 If this system does not support PT_STEP, a higher level function will
285 have called single_step() to transmute the step request into a
286 continue request (by setting breakpoints on all possible successor
287 instructions), so we don't have to worry about that here. */
291 if (SOFTWARE_SINGLE_STEP_P
)
292 abort (); /* Make sure this doesn't happen. */
294 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1,
295 target_signal_to_host (signal
));
298 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1,
299 target_signal_to_host (signal
));
303 perror_with_name ("ptrace");
306 #endif /* CHILD_RESUME */
310 /* Start debugging the process whose number is PID. */
316 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
318 perror_with_name ("ptrace");
323 /* Stop debugging the process whose number is PID
324 and continue it with signal number SIGNAL.
325 SIGNAL = 0 means just continue it. */
332 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
334 perror_with_name ("ptrace");
337 #endif /* ATTACH_DETACH */
339 /* Default the type of the ptrace transfer to int. */
340 #ifndef PTRACE_XFER_TYPE
341 #define PTRACE_XFER_TYPE int
344 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
345 to get the offset in the core file of the register values. */
346 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
347 /* Get kernel_u_addr using BSD-style nlist(). */
348 CORE_ADDR kernel_u_addr
;
349 #endif /* KERNEL_U_ADDR_BSD. */
352 _initialize_kernel_u_addr ()
354 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
355 struct nlist names
[2];
357 names
[0].n_un
.n_name
= "_u";
358 names
[1].n_un
.n_name
= NULL
;
359 if (nlist ("/vmunix", names
) == 0)
360 kernel_u_addr
= names
[0].n_value
;
362 internal_error ("Unable to get kernel u area address.");
363 #endif /* KERNEL_U_ADDR_BSD. */
366 #if !defined (FETCH_INFERIOR_REGISTERS)
368 #if !defined (offsetof)
369 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
372 /* U_REGS_OFFSET is the offset of the registers within the u area. */
373 #if !defined (U_REGS_OFFSET)
374 #define U_REGS_OFFSET \
375 ptrace (PT_READ_U, inferior_pid, \
376 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
380 /* Registers we shouldn't try to fetch. */
381 #if !defined (CANNOT_FETCH_REGISTER)
382 #define CANNOT_FETCH_REGISTER(regno) 0
385 /* Fetch one register. */
388 fetch_register (regno
)
391 /* This isn't really an address. But ptrace thinks of it as one. */
393 char mess
[128]; /* For messages */
395 unsigned int offset
; /* Offset of registers within the u area. */
396 char buf
[MAX_REGISTER_RAW_SIZE
];
399 if (CANNOT_FETCH_REGISTER (regno
))
401 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
402 supply_register (regno
, buf
);
406 /* Overload thread id onto process id */
407 if ((tid
= TIDGET (inferior_pid
)) == 0)
408 tid
= inferior_pid
; /* no thread id, just use process id */
410 offset
= U_REGS_OFFSET
;
412 regaddr
= register_addr (regno
, offset
);
413 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
416 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
417 (PTRACE_ARG3_TYPE
) regaddr
, 0);
418 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
421 sprintf (mess
, "reading register %s (#%d)",
422 REGISTER_NAME (regno
), regno
);
423 perror_with_name (mess
);
426 supply_register (regno
, buf
);
430 /* Fetch register values from the inferior.
431 If REGNO is negative, do this for all registers.
432 Otherwise, REGNO specifies which register (so we can save time). */
435 fetch_inferior_registers (regno
)
440 fetch_register (regno
);
444 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
446 fetch_register (regno
);
451 /* Registers we shouldn't try to store. */
452 #if !defined (CANNOT_STORE_REGISTER)
453 #define CANNOT_STORE_REGISTER(regno) 0
456 /* Store one register. */
459 store_register (regno
)
462 /* This isn't really an address. But ptrace thinks of it as one. */
464 char mess
[128]; /* For messages */
466 unsigned int offset
; /* Offset of registers within the u area. */
469 if (CANNOT_STORE_REGISTER (regno
))
474 /* Overload thread id onto process id */
475 if ((tid
= TIDGET (inferior_pid
)) == 0)
476 tid
= inferior_pid
; /* no thread id, just use process id */
478 offset
= U_REGS_OFFSET
;
480 regaddr
= register_addr (regno
, offset
);
481 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
484 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
485 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
486 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
489 sprintf (mess
, "writing register %s (#%d)",
490 REGISTER_NAME (regno
), regno
);
491 perror_with_name (mess
);
496 /* Store our register values back into the inferior.
497 If REGNO is negative, do this for all registers.
498 Otherwise, REGNO specifies which register (so we can save time). */
501 store_inferior_registers (regno
)
506 store_register (regno
);
510 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
512 store_register (regno
);
516 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
519 #if !defined (CHILD_XFER_MEMORY)
520 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
521 in the NEW_SUN_PTRACE case.
522 It ought to be straightforward. But it appears that writing did
523 not write the data that I specified. I cannot understand where
524 it got the data that it actually did write. */
526 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
527 to debugger memory starting at MYADDR. Copy to inferior if
530 Returns the length copied, which is either the LEN argument or zero.
531 This xfer function does not do partial moves, since child_ops
532 doesn't allow memory operations to cross below us in the target stack
536 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
541 struct target_ops
*target
; /* ignored */
544 /* Round starting address down to longword boundary. */
545 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
546 /* Round ending address up; get number of longwords that makes. */
548 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
549 / sizeof (PTRACE_XFER_TYPE
);
550 /* Allocate buffer of that many longwords. */
551 register PTRACE_XFER_TYPE
*buffer
552 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
556 /* Fill start and end extra bytes of buffer with existing memory data. */
558 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
560 /* Need part of initial word -- fetch it. */
561 buffer
[0] = ptrace (PT_READ_I
, PIDGET (inferior_pid
),
562 (PTRACE_ARG3_TYPE
) addr
, 0);
565 if (count
> 1) /* FIXME, avoid if even boundary */
568 = ptrace (PT_READ_I
, PIDGET (inferior_pid
),
570 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
574 /* Copy data to be written over corresponding part of buffer */
576 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
580 /* Write the entire buffer. */
582 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
585 ptrace (PT_WRITE_D
, PIDGET (inferior_pid
),
586 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
589 /* Using the appropriate one (I or D) is necessary for
590 Gould NP1, at least. */
592 ptrace (PT_WRITE_I
, PIDGET (inferior_pid
),
593 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
598 #ifdef CLEAR_INSN_CACHE
604 /* Read all the longwords */
605 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
608 buffer
[i
] = ptrace (PT_READ_I
, PIDGET (inferior_pid
),
609 (PTRACE_ARG3_TYPE
) addr
, 0);
615 /* Copy appropriate bytes out of the buffer. */
617 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
625 udot_info (dummy1
, dummy2
)
629 #if defined (KERNEL_U_SIZE)
630 int udot_off
; /* Offset into user struct */
631 int udot_val
; /* Value from user struct at udot_off */
632 char mess
[128]; /* For messages */
635 if (!target_has_execution
)
637 error ("The program is not being run.");
640 #if !defined (KERNEL_U_SIZE)
642 /* Adding support for this command is easy. Typically you just add a
643 routine, called "kernel_u_size" that returns the size of the user
644 struct, to the appropriate *-nat.c file and then add to the native
645 config file "#define KERNEL_U_SIZE kernel_u_size()" */
646 error ("Don't know how large ``struct user'' is in this version of gdb.");
650 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
652 if ((udot_off
% 24) == 0)
656 printf_filtered ("\n");
658 printf_filtered ("%04x:", udot_off
);
660 udot_val
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) udot_off
, 0);
663 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
664 perror_with_name (mess
);
666 /* Avoid using nonportable (?) "*" in print specs */
667 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
669 printf_filtered ("\n");
673 #endif /* !defined (CHILD_XFER_MEMORY). */
677 _initialize_infptrace ()
679 #if !defined (CHILD_XFER_MEMORY)
680 add_info ("udot", udot_info
,
681 "Print contents of kernel ``struct user'' for current child.");