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 #include <sys/types.h>
34 #include <sys/param.h>
37 #include <sys/ioctl.h>
42 #ifdef HAVE_SYS_PTRACE_H
43 #include <sys/ptrace.h>
47 #if !defined (PT_READ_I)
48 #define PT_READ_I 1 /* Read word from text space */
50 #if !defined (PT_READ_D)
51 #define PT_READ_D 2 /* Read word from data space */
53 #if !defined (PT_READ_U)
54 #define PT_READ_U 3 /* Read word from kernel user struct */
56 #if !defined (PT_WRITE_I)
57 #define PT_WRITE_I 4 /* Write word to text space */
59 #if !defined (PT_WRITE_D)
60 #define PT_WRITE_D 5 /* Write word to data space */
62 #if !defined (PT_WRITE_U)
63 #define PT_WRITE_U 6 /* Write word to kernel user struct */
65 #if !defined (PT_CONTINUE)
66 #define PT_CONTINUE 7 /* Continue after signal */
68 #if !defined (PT_STEP)
69 #define PT_STEP 9 /* Set flag for single stepping */
71 #if !defined (PT_KILL)
72 #define PT_KILL 8 /* Send child a SIGKILL signal */
76 #define PT_ATTACH PTRACE_ATTACH
79 #define PT_DETACH PTRACE_DETACH
87 /* Don't think this is used anymore. On the sequent (not sure whether it's
88 dynix or ptx or both), it is included unconditionally by sys/user.h and
89 not protected against multiple inclusion. */
93 #if !defined (FETCH_INFERIOR_REGISTERS)
94 #include <sys/user.h> /* Probably need to poke the user structure */
95 #if defined (KERNEL_U_ADDR_BSD)
96 #include <a.out.h> /* For struct nlist */
97 #endif /* KERNEL_U_ADDR_BSD. */
98 #endif /* !FETCH_INFERIOR_REGISTERS */
100 #if !defined (CHILD_XFER_MEMORY)
101 static void udot_info
PARAMS ((char *, int));
104 #if !defined (FETCH_INFERIOR_REGISTERS)
105 static void fetch_register
PARAMS ((int));
106 static void store_register
PARAMS ((int));
109 void _initialize_kernel_u_addr
PARAMS ((void));
110 void _initialize_infptrace
PARAMS ((void));
113 /* This function simply calls ptrace with the given arguments.
114 It exists so that all calls to ptrace are isolated in this
115 machine-dependent file. */
117 call_ptrace (request
, pid
, addr
, data
)
119 PTRACE_ARG3_TYPE addr
;
127 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
128 request
, pid
, addr
, data
);
130 #if defined(PT_SETTRC)
131 /* If the parent can be told to attach to us, try to do it. */
132 if (request
== PT_SETTRC
)
135 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
136 #if defined (FIVE_ARG_PTRACE)
137 /* Deal with HPUX 8.0 braindamage. We never use the
138 calls which require the fifth argument. */
144 perror_with_name ("ptrace");
146 printf (" = %d\n", pt_status
);
151 return parent_attach_all (pid
, addr
, data
);
155 #if defined(PT_CONTIN1)
156 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
157 signals. If it's available, use it. */
158 if (request
== PT_CONTINUE
)
159 request
= PT_CONTIN1
;
162 #if defined(PT_SINGLE1)
163 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
164 signals. If it's available, use it. */
165 if (request
== PT_STEP
)
166 request
= PT_SINGLE1
;
173 pt_status
= ptrace (request
, pid
, addr
, data
174 #if defined (FIVE_ARG_PTRACE)
175 /* Deal with HPUX 8.0 braindamage. We never use the
176 calls which require the fifth argument. */
182 printf (" [errno = %d]", errno
);
185 printf (" = 0x%x\n", pt_status
);
191 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
192 /* For the rest of the file, use an extra level of indirection */
193 /* This lets us breakpoint usefully on call_ptrace. */
194 #define ptrace call_ptrace
197 /* Wait for a process to finish, possibly running a target-specific
198 hook before returning. */
201 ptrace_wait (pid
, status
)
207 wstate
= wait (status
);
208 target_post_wait (wstate
, *status
);
217 if (inferior_pid
== 0)
220 /* This once used to call "kill" to kill the inferior just in case
221 the inferior was still running. As others have noted in the past
222 (kingdon) there shouldn't be any way to get here if the inferior
223 is still running -- else there's a major problem elsewere in gdb
224 and it needs to be fixed.
226 The kill call causes problems under hpux10, so it's been removed;
227 if this causes problems we'll deal with them as they arise. */
228 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
229 ptrace_wait (0, &status
);
230 target_mourn_inferior ();
235 /* Resume execution of the inferior process.
236 If STEP is nonzero, single-step it.
237 If SIGNAL is nonzero, give it that signal. */
240 child_resume (pid
, step
, signal
)
243 enum target_signal signal
;
248 /* Resume all threads. */
249 /* I think this only gets used in the non-threaded case, where "resume
250 all threads" and "resume inferior_pid" are the same. */
253 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
254 it was. (If GDB wanted it to start some other way, we have already
255 written a new PC value to the child.)
257 If this system does not support PT_STEP, a higher level function will
258 have called single_step() to transmute the step request into a
259 continue request (by setting breakpoints on all possible successor
260 instructions), so we don't have to worry about that here. */
264 if (SOFTWARE_SINGLE_STEP_P
)
265 abort (); /* Make sure this doesn't happen. */
267 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1,
268 target_signal_to_host (signal
));
271 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1,
272 target_signal_to_host (signal
));
275 perror_with_name ("ptrace");
277 #endif /* CHILD_RESUME */
281 /* Start debugging the process whose number is PID. */
287 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
289 perror_with_name ("ptrace");
294 /* Stop debugging the process whose number is PID
295 and continue it with signal number SIGNAL.
296 SIGNAL = 0 means just continue it. */
303 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
305 perror_with_name ("ptrace");
308 #endif /* ATTACH_DETACH */
310 /* Default the type of the ptrace transfer to int. */
311 #ifndef PTRACE_XFER_TYPE
312 #define PTRACE_XFER_TYPE int
315 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
316 to get the offset in the core file of the register values. */
317 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
318 /* Get kernel_u_addr using BSD-style nlist(). */
319 CORE_ADDR kernel_u_addr
;
320 #endif /* KERNEL_U_ADDR_BSD. */
323 _initialize_kernel_u_addr ()
325 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
326 struct nlist names
[2];
328 names
[0].n_un
.n_name
= "_u";
329 names
[1].n_un
.n_name
= NULL
;
330 if (nlist ("/vmunix", names
) == 0)
331 kernel_u_addr
= names
[0].n_value
;
333 internal_error ("Unable to get kernel u area address.");
334 #endif /* KERNEL_U_ADDR_BSD. */
337 #if !defined (FETCH_INFERIOR_REGISTERS)
339 #if !defined (offsetof)
340 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
343 /* U_REGS_OFFSET is the offset of the registers within the u area. */
344 #if !defined (U_REGS_OFFSET)
345 #define U_REGS_OFFSET \
346 ptrace (PT_READ_U, inferior_pid, \
347 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
351 /* Registers we shouldn't try to fetch. */
352 #if !defined (CANNOT_FETCH_REGISTER)
353 #define CANNOT_FETCH_REGISTER(regno) 0
356 /* Fetch one register. */
359 fetch_register (regno
)
362 /* This isn't really an address. But ptrace thinks of it as one. */
364 char mess
[128]; /* For messages */
366 unsigned int offset
; /* Offset of registers within the u area. */
367 char buf
[MAX_REGISTER_RAW_SIZE
];
369 if (CANNOT_FETCH_REGISTER (regno
))
371 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
372 supply_register (regno
, buf
);
376 offset
= U_REGS_OFFSET
;
378 regaddr
= register_addr (regno
, offset
);
379 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
382 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, inferior_pid
,
383 (PTRACE_ARG3_TYPE
) regaddr
, 0);
384 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
387 sprintf (mess
, "reading register %s (#%d)", REGISTER_NAME (regno
), regno
);
388 perror_with_name (mess
);
391 supply_register (regno
, buf
);
395 /* Fetch register values from the inferior.
396 If REGNO is negative, do this for all registers.
397 Otherwise, REGNO specifies which register (so we can save time). */
400 fetch_inferior_registers (regno
)
405 fetch_register (regno
);
409 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
411 fetch_register (regno
);
416 /* Registers we shouldn't try to store. */
417 #if !defined (CANNOT_STORE_REGISTER)
418 #define CANNOT_STORE_REGISTER(regno) 0
421 /* Store one register. */
424 store_register (regno
)
427 /* This isn't really an address. But ptrace thinks of it as one. */
429 char mess
[128]; /* For messages */
431 unsigned int offset
; /* Offset of registers within the u area. */
433 if (CANNOT_STORE_REGISTER (regno
))
438 offset
= U_REGS_OFFSET
;
440 regaddr
= register_addr (regno
, offset
);
441 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
444 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
445 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
446 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
449 sprintf (mess
, "writing register %s (#%d)", REGISTER_NAME (regno
), regno
);
450 perror_with_name (mess
);
455 /* Store our register values back into the inferior.
456 If REGNO is negative, do this for all registers.
457 Otherwise, REGNO specifies which register (so we can save time). */
460 store_inferior_registers (regno
)
465 store_register (regno
);
469 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
471 store_register (regno
);
475 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
478 #if !defined (CHILD_XFER_MEMORY)
479 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
480 in the NEW_SUN_PTRACE case.
481 It ought to be straightforward. But it appears that writing did
482 not write the data that I specified. I cannot understand where
483 it got the data that it actually did write. */
485 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
486 to debugger memory starting at MYADDR. Copy to inferior if
489 Returns the length copied, which is either the LEN argument or zero.
490 This xfer function does not do partial moves, since child_ops
491 doesn't allow memory operations to cross below us in the target stack
495 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
500 struct target_ops
*target
; /* ignored */
503 /* Round starting address down to longword boundary. */
504 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
505 /* Round ending address up; get number of longwords that makes. */
507 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
508 / sizeof (PTRACE_XFER_TYPE
);
509 /* Allocate buffer of that many longwords. */
510 register PTRACE_XFER_TYPE
*buffer
511 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
515 /* Fill start and end extra bytes of buffer with existing memory data. */
517 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
519 /* Need part of initial word -- fetch it. */
520 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
524 if (count
> 1) /* FIXME, avoid if even boundary */
527 = ptrace (PT_READ_I
, inferior_pid
,
529 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
533 /* Copy data to be written over corresponding part of buffer */
535 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
539 /* Write the entire buffer. */
541 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
544 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
548 /* Using the appropriate one (I or D) is necessary for
549 Gould NP1, at least. */
551 ptrace (PT_WRITE_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
557 #ifdef CLEAR_INSN_CACHE
563 /* Read all the longwords */
564 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
567 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
,
568 (PTRACE_ARG3_TYPE
) addr
, 0);
574 /* Copy appropriate bytes out of the buffer. */
576 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
584 udot_info (dummy1
, dummy2
)
588 #if defined (KERNEL_U_SIZE)
589 int udot_off
; /* Offset into user struct */
590 int udot_val
; /* Value from user struct at udot_off */
591 char mess
[128]; /* For messages */
594 if (!target_has_execution
)
596 error ("The program is not being run.");
599 #if !defined (KERNEL_U_SIZE)
601 /* Adding support for this command is easy. Typically you just add a
602 routine, called "kernel_u_size" that returns the size of the user
603 struct, to the appropriate *-nat.c file and then add to the native
604 config file "#define KERNEL_U_SIZE kernel_u_size()" */
605 error ("Don't know how large ``struct user'' is in this version of gdb.");
609 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
611 if ((udot_off
% 24) == 0)
615 printf_filtered ("\n");
617 printf_filtered ("%04x:", udot_off
);
619 udot_val
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) udot_off
, 0);
622 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
623 perror_with_name (mess
);
625 /* Avoid using nonportable (?) "*" in print specs */
626 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
628 printf_filtered ("\n");
632 #endif /* !defined (CHILD_XFER_MEMORY). */
636 _initialize_infptrace ()
638 #if !defined (CHILD_XFER_MEMORY)
639 add_info ("udot", udot_info
,
640 "Print contents of kernel ``struct user'' for current child.");