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"
27 #ifdef HAVE_SYS_WAIT_H
30 #include "wait.h" /* NOTE: This is ../include/wait.h */
34 #include <sys/types.h>
37 #include <sys/param.h>
40 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_PTRACE_H
46 #include <sys/ptrace.h>
50 #if !defined (PT_READ_I)
51 #define PT_READ_I 1 /* Read word from text space */
53 #if !defined (PT_READ_D)
54 #define PT_READ_D 2 /* Read word from data space */
56 #if !defined (PT_READ_U)
57 #define PT_READ_U 3 /* Read word from kernel user struct */
59 #if !defined (PT_WRITE_I)
60 #define PT_WRITE_I 4 /* Write word to text space */
62 #if !defined (PT_WRITE_D)
63 #define PT_WRITE_D 5 /* Write word to data space */
65 #if !defined (PT_WRITE_U)
66 #define PT_WRITE_U 6 /* Write word to kernel user struct */
68 #if !defined (PT_CONTINUE)
69 #define PT_CONTINUE 7 /* Continue after signal */
71 #if !defined (PT_STEP)
72 #define PT_STEP 9 /* Set flag for single stepping */
74 #if !defined (PT_KILL)
75 #define PT_KILL 8 /* Send child a SIGKILL signal */
79 #define PT_ATTACH PTRACE_ATTACH
82 #define PT_DETACH PTRACE_DETACH
90 /* Don't think this is used anymore. On the sequent (not sure whether it's
91 dynix or ptx or both), it is included unconditionally by sys/user.h and
92 not protected against multiple inclusion. */
96 #if !defined (FETCH_INFERIOR_REGISTERS)
97 #include <sys/user.h> /* Probably need to poke the user structure */
98 #if defined (KERNEL_U_ADDR_BSD)
99 #include <a.out.h> /* For struct nlist */
100 #endif /* KERNEL_U_ADDR_BSD. */
101 #endif /* !FETCH_INFERIOR_REGISTERS */
103 #if !defined (CHILD_XFER_MEMORY)
104 static void udot_info
PARAMS ((char *, int));
107 #if !defined (FETCH_INFERIOR_REGISTERS)
108 static void fetch_register
PARAMS ((int));
109 static void store_register
PARAMS ((int));
112 void _initialize_kernel_u_addr
PARAMS ((void));
113 void _initialize_infptrace
PARAMS ((void));
116 /* This function simply calls ptrace with the given arguments.
117 It exists so that all calls to ptrace are isolated in this
118 machine-dependent file. */
120 call_ptrace (request
, pid
, addr
, data
)
122 PTRACE_ARG3_TYPE addr
;
130 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
131 request
, pid
, addr
, data
);
133 #if defined(PT_SETTRC)
134 /* If the parent can be told to attach to us, try to do it. */
135 if (request
== PT_SETTRC
)
138 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
139 #if defined (FIVE_ARG_PTRACE)
140 /* Deal with HPUX 8.0 braindamage. We never use the
141 calls which require the fifth argument. */
147 perror_with_name ("ptrace");
149 printf (" = %d\n", pt_status
);
154 return parent_attach_all (pid
, addr
, data
);
158 #if defined(PT_CONTIN1)
159 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
160 signals. If it's available, use it. */
161 if (request
== PT_CONTINUE
)
162 request
= PT_CONTIN1
;
165 #if defined(PT_SINGLE1)
166 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
167 signals. If it's available, use it. */
168 if (request
== PT_STEP
)
169 request
= PT_SINGLE1
;
176 pt_status
= ptrace (request
, pid
, addr
, data
177 #if defined (FIVE_ARG_PTRACE)
178 /* Deal with HPUX 8.0 braindamage. We never use the
179 calls which require the fifth argument. */
185 printf (" [errno = %d]", errno
);
188 printf (" = 0x%x\n", pt_status
);
194 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
195 /* For the rest of the file, use an extra level of indirection */
196 /* This lets us breakpoint usefully on call_ptrace. */
197 #define ptrace call_ptrace
200 /* Wait for a process to finish, possibly running a target-specific
201 hook before returning. */
204 ptrace_wait (pid
, status
)
210 wstate
= wait (status
);
211 target_post_wait (wstate
, *status
);
220 if (inferior_pid
== 0)
223 /* This once used to call "kill" to kill the inferior just in case
224 the inferior was still running. As others have noted in the past
225 (kingdon) there shouldn't be any way to get here if the inferior
226 is still running -- else there's a major problem elsewere in gdb
227 and it needs to be fixed.
229 The kill call causes problems under hpux10, so it's been removed;
230 if this causes problems we'll deal with them as they arise. */
231 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
232 ptrace_wait (0, &status
);
233 target_mourn_inferior ();
238 /* Resume execution of the inferior process.
239 If STEP is nonzero, single-step it.
240 If SIGNAL is nonzero, give it that signal. */
243 child_resume (pid
, step
, signal
)
246 enum target_signal signal
;
251 /* Resume all threads. */
252 /* I think this only gets used in the non-threaded case, where "resume
253 all threads" and "resume inferior_pid" are the same. */
256 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
257 it was. (If GDB wanted it to start some other way, we have already
258 written a new PC value to the child.)
260 If this system does not support PT_STEP, a higher level function will
261 have called single_step() to transmute the step request into a
262 continue request (by setting breakpoints on all possible successor
263 instructions), so we don't have to worry about that here. */
267 if (SOFTWARE_SINGLE_STEP_P
)
268 abort (); /* Make sure this doesn't happen. */
270 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1,
271 target_signal_to_host (signal
));
274 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1,
275 target_signal_to_host (signal
));
278 perror_with_name ("ptrace");
280 #endif /* CHILD_RESUME */
284 /* Start debugging the process whose number is PID. */
290 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
292 perror_with_name ("ptrace");
297 /* Stop debugging the process whose number is PID
298 and continue it with signal number SIGNAL.
299 SIGNAL = 0 means just continue it. */
306 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
308 perror_with_name ("ptrace");
311 #endif /* ATTACH_DETACH */
313 /* Default the type of the ptrace transfer to int. */
314 #ifndef PTRACE_XFER_TYPE
315 #define PTRACE_XFER_TYPE int
318 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
319 to get the offset in the core file of the register values. */
320 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
321 /* Get kernel_u_addr using BSD-style nlist(). */
322 CORE_ADDR kernel_u_addr
;
323 #endif /* KERNEL_U_ADDR_BSD. */
326 _initialize_kernel_u_addr ()
328 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
329 struct nlist names
[2];
331 names
[0].n_un
.n_name
= "_u";
332 names
[1].n_un
.n_name
= NULL
;
333 if (nlist ("/vmunix", names
) == 0)
334 kernel_u_addr
= names
[0].n_value
;
336 internal_error ("Unable to get kernel u area address.");
337 #endif /* KERNEL_U_ADDR_BSD. */
340 #if !defined (FETCH_INFERIOR_REGISTERS)
342 #if !defined (offsetof)
343 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
346 /* U_REGS_OFFSET is the offset of the registers within the u area. */
347 #if !defined (U_REGS_OFFSET)
348 #define U_REGS_OFFSET \
349 ptrace (PT_READ_U, inferior_pid, \
350 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
354 /* Registers we shouldn't try to fetch. */
355 #if !defined (CANNOT_FETCH_REGISTER)
356 #define CANNOT_FETCH_REGISTER(regno) 0
359 /* Fetch one register. */
362 fetch_register (regno
)
365 /* This isn't really an address. But ptrace thinks of it as one. */
367 char mess
[128]; /* For messages */
369 unsigned int offset
; /* Offset of registers within the u area. */
370 char buf
[MAX_REGISTER_RAW_SIZE
];
372 if (CANNOT_FETCH_REGISTER (regno
))
374 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
375 supply_register (regno
, buf
);
379 offset
= U_REGS_OFFSET
;
381 regaddr
= register_addr (regno
, offset
);
382 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
385 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, inferior_pid
,
386 (PTRACE_ARG3_TYPE
) regaddr
, 0);
387 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
390 sprintf (mess
, "reading register %s (#%d)", REGISTER_NAME (regno
), regno
);
391 perror_with_name (mess
);
394 supply_register (regno
, buf
);
398 /* Fetch register values from the inferior.
399 If REGNO is negative, do this for all registers.
400 Otherwise, REGNO specifies which register (so we can save time). */
403 fetch_inferior_registers (regno
)
408 fetch_register (regno
);
412 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
414 fetch_register (regno
);
419 /* Registers we shouldn't try to store. */
420 #if !defined (CANNOT_STORE_REGISTER)
421 #define CANNOT_STORE_REGISTER(regno) 0
424 /* Store one register. */
427 store_register (regno
)
430 /* This isn't really an address. But ptrace thinks of it as one. */
432 char mess
[128]; /* For messages */
434 unsigned int offset
; /* Offset of registers within the u area. */
436 if (CANNOT_STORE_REGISTER (regno
))
441 offset
= U_REGS_OFFSET
;
443 regaddr
= register_addr (regno
, offset
);
444 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
447 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
448 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
449 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
452 sprintf (mess
, "writing register %s (#%d)", REGISTER_NAME (regno
), regno
);
453 perror_with_name (mess
);
458 /* Store our register values back into the inferior.
459 If REGNO is negative, do this for all registers.
460 Otherwise, REGNO specifies which register (so we can save time). */
463 store_inferior_registers (regno
)
468 store_register (regno
);
472 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
474 store_register (regno
);
478 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
481 #if !defined (CHILD_XFER_MEMORY)
482 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
483 in the NEW_SUN_PTRACE case.
484 It ought to be straightforward. But it appears that writing did
485 not write the data that I specified. I cannot understand where
486 it got the data that it actually did write. */
488 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
489 to debugger memory starting at MYADDR. Copy to inferior if
492 Returns the length copied, which is either the LEN argument or zero.
493 This xfer function does not do partial moves, since child_ops
494 doesn't allow memory operations to cross below us in the target stack
498 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
503 struct target_ops
*target
; /* ignored */
506 /* Round starting address down to longword boundary. */
507 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
508 /* Round ending address up; get number of longwords that makes. */
510 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
511 / sizeof (PTRACE_XFER_TYPE
);
512 /* Allocate buffer of that many longwords. */
513 register PTRACE_XFER_TYPE
*buffer
514 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
518 /* Fill start and end extra bytes of buffer with existing memory data. */
520 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
522 /* Need part of initial word -- fetch it. */
523 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
527 if (count
> 1) /* FIXME, avoid if even boundary */
530 = ptrace (PT_READ_I
, inferior_pid
,
532 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
536 /* Copy data to be written over corresponding part of buffer */
538 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
542 /* Write the entire buffer. */
544 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
547 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
551 /* Using the appropriate one (I or D) is necessary for
552 Gould NP1, at least. */
554 ptrace (PT_WRITE_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
560 #ifdef CLEAR_INSN_CACHE
566 /* Read all the longwords */
567 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
570 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
,
571 (PTRACE_ARG3_TYPE
) addr
, 0);
577 /* Copy appropriate bytes out of the buffer. */
579 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
587 udot_info (dummy1
, dummy2
)
591 #if defined (KERNEL_U_SIZE)
592 int udot_off
; /* Offset into user struct */
593 int udot_val
; /* Value from user struct at udot_off */
594 char mess
[128]; /* For messages */
597 if (!target_has_execution
)
599 error ("The program is not being run.");
602 #if !defined (KERNEL_U_SIZE)
604 /* Adding support for this command is easy. Typically you just add a
605 routine, called "kernel_u_size" that returns the size of the user
606 struct, to the appropriate *-nat.c file and then add to the native
607 config file "#define KERNEL_U_SIZE kernel_u_size()" */
608 error ("Don't know how large ``struct user'' is in this version of gdb.");
612 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
614 if ((udot_off
% 24) == 0)
618 printf_filtered ("\n");
620 printf_filtered ("%04x:", udot_off
);
622 udot_val
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) udot_off
, 0);
625 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
626 perror_with_name (mess
);
628 /* Avoid using nonportable (?) "*" in print specs */
629 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
631 printf_filtered ("\n");
635 #endif /* !defined (CHILD_XFER_MEMORY). */
639 _initialize_infptrace ()
641 #if !defined (CHILD_XFER_MEMORY)
642 add_info ("udot", udot_info
,
643 "Print contents of kernel ``struct user'' for current child.");