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"
30 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/types.h>
40 #include <sys/param.h>
43 #include <sys/ioctl.h>
48 #ifdef HAVE_SYS_PTRACE_H
49 #include <sys/ptrace.h>
53 #if !defined (PT_READ_I)
54 #define PT_READ_I 1 /* Read word from text space */
56 #if !defined (PT_READ_D)
57 #define PT_READ_D 2 /* Read word from data space */
59 #if !defined (PT_READ_U)
60 #define PT_READ_U 3 /* Read word from kernel user struct */
62 #if !defined (PT_WRITE_I)
63 #define PT_WRITE_I 4 /* Write word to text space */
65 #if !defined (PT_WRITE_D)
66 #define PT_WRITE_D 5 /* Write word to data space */
68 #if !defined (PT_WRITE_U)
69 #define PT_WRITE_U 6 /* Write word to kernel user struct */
71 #if !defined (PT_CONTINUE)
72 #define PT_CONTINUE 7 /* Continue after signal */
74 #if !defined (PT_STEP)
75 #define PT_STEP 9 /* Set flag for single stepping */
77 #if !defined (PT_KILL)
78 #define PT_KILL 8 /* Send child a SIGKILL signal */
82 #define PT_ATTACH PTRACE_ATTACH
85 #define PT_DETACH PTRACE_DETACH
93 /* Don't think this is used anymore. On the sequent (not sure whether it's
94 dynix or ptx or both), it is included unconditionally by sys/user.h and
95 not protected against multiple inclusion. */
99 #if !defined (FETCH_INFERIOR_REGISTERS)
100 #include <sys/user.h> /* Probably need to poke the user structure */
101 #if defined (KERNEL_U_ADDR_BSD)
102 #include <a.out.h> /* For struct nlist */
103 #endif /* KERNEL_U_ADDR_BSD. */
104 #endif /* !FETCH_INFERIOR_REGISTERS */
106 #if !defined (CHILD_XFER_MEMORY)
107 static void udot_info
PARAMS ((char *, int));
110 #if !defined (FETCH_INFERIOR_REGISTERS)
111 static void fetch_register
PARAMS ((int));
112 static void store_register
PARAMS ((int));
115 void _initialize_kernel_u_addr
PARAMS ((void));
116 void _initialize_infptrace
PARAMS ((void));
119 /* This function simply calls ptrace with the given arguments.
120 It exists so that all calls to ptrace are isolated in this
121 machine-dependent file. */
123 call_ptrace (request
, pid
, addr
, data
)
125 PTRACE_ARG3_TYPE addr
;
133 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
134 request
, pid
, addr
, data
);
136 #if defined(PT_SETTRC)
137 /* If the parent can be told to attach to us, try to do it. */
138 if (request
== PT_SETTRC
)
141 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
142 #if defined (FIVE_ARG_PTRACE)
143 /* Deal with HPUX 8.0 braindamage. We never use the
144 calls which require the fifth argument. */
150 perror_with_name ("ptrace");
152 printf (" = %d\n", pt_status
);
157 return parent_attach_all (pid
, addr
, data
);
161 #if defined(PT_CONTIN1)
162 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
163 signals. If it's available, use it. */
164 if (request
== PT_CONTINUE
)
165 request
= PT_CONTIN1
;
168 #if defined(PT_SINGLE1)
169 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
170 signals. If it's available, use it. */
171 if (request
== PT_STEP
)
172 request
= PT_SINGLE1
;
179 pt_status
= ptrace (request
, pid
, addr
, data
180 #if defined (FIVE_ARG_PTRACE)
181 /* Deal with HPUX 8.0 braindamage. We never use the
182 calls which require the fifth argument. */
188 printf (" [errno = %d]", errno
);
191 printf (" = 0x%x\n", pt_status
);
197 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
198 /* For the rest of the file, use an extra level of indirection */
199 /* This lets us breakpoint usefully on call_ptrace. */
200 #define ptrace call_ptrace
203 /* Wait for a process to finish, possibly running a target-specific
204 hook before returning. */
207 ptrace_wait (pid
, status
)
213 wstate
= wait (status
);
214 target_post_wait (wstate
, *status
);
223 if (inferior_pid
== 0)
226 /* This once used to call "kill" to kill the inferior just in case
227 the inferior was still running. As others have noted in the past
228 (kingdon) there shouldn't be any way to get here if the inferior
229 is still running -- else there's a major problem elsewere in gdb
230 and it needs to be fixed.
232 The kill call causes problems under hpux10, so it's been removed;
233 if this causes problems we'll deal with them as they arise. */
234 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
235 ptrace_wait (0, &status
);
236 target_mourn_inferior ();
241 /* Resume execution of the inferior process.
242 If STEP is nonzero, single-step it.
243 If SIGNAL is nonzero, give it that signal. */
246 child_resume (pid
, step
, signal
)
249 enum target_signal signal
;
254 /* Resume all threads. */
255 /* I think this only gets used in the non-threaded case, where "resume
256 all threads" and "resume inferior_pid" are the same. */
259 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
260 it was. (If GDB wanted it to start some other way, we have already
261 written a new PC value to the child.)
263 If this system does not support PT_STEP, a higher level function will
264 have called single_step() to transmute the step request into a
265 continue request (by setting breakpoints on all possible successor
266 instructions), so we don't have to worry about that here. */
270 if (SOFTWARE_SINGLE_STEP_P
)
271 abort (); /* Make sure this doesn't happen. */
273 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1,
274 target_signal_to_host (signal
));
277 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1,
278 target_signal_to_host (signal
));
281 perror_with_name ("ptrace");
283 #endif /* CHILD_RESUME */
287 /* Start debugging the process whose number is PID. */
293 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
295 perror_with_name ("ptrace");
300 /* Stop debugging the process whose number is PID
301 and continue it with signal number SIGNAL.
302 SIGNAL = 0 means just continue it. */
309 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
311 perror_with_name ("ptrace");
314 #endif /* ATTACH_DETACH */
316 /* Default the type of the ptrace transfer to int. */
317 #ifndef PTRACE_XFER_TYPE
318 #define PTRACE_XFER_TYPE int
321 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
322 to get the offset in the core file of the register values. */
323 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
324 /* Get kernel_u_addr using BSD-style nlist(). */
325 CORE_ADDR kernel_u_addr
;
326 #endif /* KERNEL_U_ADDR_BSD. */
329 _initialize_kernel_u_addr ()
331 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
332 struct nlist names
[2];
334 names
[0].n_un
.n_name
= "_u";
335 names
[1].n_un
.n_name
= NULL
;
336 if (nlist ("/vmunix", names
) == 0)
337 kernel_u_addr
= names
[0].n_value
;
339 internal_error ("Unable to get kernel u area address.");
340 #endif /* KERNEL_U_ADDR_BSD. */
343 #if !defined (FETCH_INFERIOR_REGISTERS)
345 #if !defined (offsetof)
346 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
349 /* U_REGS_OFFSET is the offset of the registers within the u area. */
350 #if !defined (U_REGS_OFFSET)
351 #define U_REGS_OFFSET \
352 ptrace (PT_READ_U, inferior_pid, \
353 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
357 /* Registers we shouldn't try to fetch. */
358 #if !defined (CANNOT_FETCH_REGISTER)
359 #define CANNOT_FETCH_REGISTER(regno) 0
362 /* Fetch one register. */
365 fetch_register (regno
)
368 /* This isn't really an address. But ptrace thinks of it as one. */
370 char mess
[128]; /* For messages */
372 unsigned int offset
; /* Offset of registers within the u area. */
373 char buf
[MAX_REGISTER_RAW_SIZE
];
375 if (CANNOT_FETCH_REGISTER (regno
))
377 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
378 supply_register (regno
, buf
);
382 offset
= U_REGS_OFFSET
;
384 regaddr
= register_addr (regno
, offset
);
385 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
388 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, inferior_pid
,
389 (PTRACE_ARG3_TYPE
) regaddr
, 0);
390 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
393 sprintf (mess
, "reading register %s (#%d)", REGISTER_NAME (regno
), regno
);
394 perror_with_name (mess
);
397 supply_register (regno
, buf
);
401 /* Fetch register values from the inferior.
402 If REGNO is negative, do this for all registers.
403 Otherwise, REGNO specifies which register (so we can save time). */
406 fetch_inferior_registers (regno
)
411 fetch_register (regno
);
415 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
417 fetch_register (regno
);
422 /* Registers we shouldn't try to store. */
423 #if !defined (CANNOT_STORE_REGISTER)
424 #define CANNOT_STORE_REGISTER(regno) 0
427 /* Store one register. */
430 store_register (regno
)
433 /* This isn't really an address. But ptrace thinks of it as one. */
435 char mess
[128]; /* For messages */
437 unsigned int offset
; /* Offset of registers within the u area. */
439 if (CANNOT_STORE_REGISTER (regno
))
444 offset
= U_REGS_OFFSET
;
446 regaddr
= register_addr (regno
, offset
);
447 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
450 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
451 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
452 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
455 sprintf (mess
, "writing register %s (#%d)", REGISTER_NAME (regno
), regno
);
456 perror_with_name (mess
);
461 /* Store our register values back into the inferior.
462 If REGNO is negative, do this for all registers.
463 Otherwise, REGNO specifies which register (so we can save time). */
466 store_inferior_registers (regno
)
471 store_register (regno
);
475 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
477 store_register (regno
);
481 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
484 #if !defined (CHILD_XFER_MEMORY)
485 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
486 in the NEW_SUN_PTRACE case.
487 It ought to be straightforward. But it appears that writing did
488 not write the data that I specified. I cannot understand where
489 it got the data that it actually did write. */
491 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
492 to debugger memory starting at MYADDR. Copy to inferior if
495 Returns the length copied, which is either the LEN argument or zero.
496 This xfer function does not do partial moves, since child_ops
497 doesn't allow memory operations to cross below us in the target stack
501 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
506 struct target_ops
*target
; /* ignored */
509 /* Round starting address down to longword boundary. */
510 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
511 /* Round ending address up; get number of longwords that makes. */
513 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
514 / sizeof (PTRACE_XFER_TYPE
);
515 /* Allocate buffer of that many longwords. */
516 register PTRACE_XFER_TYPE
*buffer
517 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
521 /* Fill start and end extra bytes of buffer with existing memory data. */
523 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
525 /* Need part of initial word -- fetch it. */
526 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
530 if (count
> 1) /* FIXME, avoid if even boundary */
533 = ptrace (PT_READ_I
, inferior_pid
,
535 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
539 /* Copy data to be written over corresponding part of buffer */
541 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
545 /* Write the entire buffer. */
547 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
550 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
554 /* Using the appropriate one (I or D) is necessary for
555 Gould NP1, at least. */
557 ptrace (PT_WRITE_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
563 #ifdef CLEAR_INSN_CACHE
569 /* Read all the longwords */
570 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
573 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
,
574 (PTRACE_ARG3_TYPE
) addr
, 0);
580 /* Copy appropriate bytes out of the buffer. */
582 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
590 udot_info (dummy1
, dummy2
)
594 #if defined (KERNEL_U_SIZE)
595 int udot_off
; /* Offset into user struct */
596 int udot_val
; /* Value from user struct at udot_off */
597 char mess
[128]; /* For messages */
600 if (!target_has_execution
)
602 error ("The program is not being run.");
605 #if !defined (KERNEL_U_SIZE)
607 /* Adding support for this command is easy. Typically you just add a
608 routine, called "kernel_u_size" that returns the size of the user
609 struct, to the appropriate *-nat.c file and then add to the native
610 config file "#define KERNEL_U_SIZE kernel_u_size()" */
611 error ("Don't know how large ``struct user'' is in this version of gdb.");
615 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
617 if ((udot_off
% 24) == 0)
621 printf_filtered ("\n");
623 printf_filtered ("%04x:", udot_off
);
625 udot_val
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) udot_off
, 0);
628 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
629 perror_with_name (mess
);
631 /* Avoid using nonportable (?) "*" in print specs */
632 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
634 printf_filtered ("\n");
638 #endif /* !defined (CHILD_XFER_MEMORY). */
642 _initialize_infptrace ()
644 #if !defined (CHILD_XFER_MEMORY)
645 add_info ("udot", udot_info
,
646 "Print contents of kernel ``struct user'' for current child.");