1 /* Low-level child interface to ptrace.
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1998, 1999, 2000, 2001, 2002, 2004, 2005
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
34 #include "gdb_ptrace.h"
38 #include "inf-child.h"
40 /* HACK: Save the ptrace ops returned by inf_ptrace_target. */
41 static struct target_ops
*ptrace_ops_hack
;
44 inf_ptrace_kill_inferior (void)
47 int pid
= PIDGET (inferior_ptid
);
52 /* This once used to call "kill" to kill the inferior just in case
53 the inferior was still running. As others have noted in the past
54 (kingdon) there shouldn't be any way to get here if the inferior
55 is still running -- else there's a major problem elsewere in GDB
56 and it needs to be fixed.
58 The kill call causes problems under HP-UX 10, so it's been
59 removed; if this causes problems we'll deal with them as they
61 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
63 target_mourn_inferior ();
66 /* Resume execution of the inferior process. If STEP is nonzero,
67 single-step it. If SIGNAL is nonzero, give it that signal. */
70 inf_ptrace_resume (ptid_t ptid
, int step
, enum target_signal signal
)
72 int request
= PT_CONTINUE
;
73 int pid
= PIDGET (ptid
);
76 /* Resume all threads. */
77 /* I think this only gets used in the non-threaded case, where
78 "resume all threads" and "resume inferior_ptid" are the
80 pid
= PIDGET (inferior_ptid
);
84 /* If this system does not support PT_STEP, a higher level
85 function will have called single_step() to transmute the step
86 request into a continue request (by setting breakpoints on
87 all possible successor instructions), so we don't have to
88 worry about that here. */
92 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
93 where it was. If GDB wanted it to start some other way, we have
94 already written a new PC value to the child. */
96 ptrace (request
, pid
, (PTRACE_TYPE_ARG3
) 1, target_signal_to_host (signal
));
98 perror_with_name (("ptrace"));
101 /* Wait for child to do something. Return pid of child, or -1 in case
102 of error; store status through argument pointer OURSTATUS. */
105 inf_ptrace_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
109 char *execd_pathname
= NULL
;
113 enum target_waitkind kind
;
118 set_sigint_trap (); /* Causes SIGINT to be passed on to the
122 pid
= wait (&status
);
128 clear_sigint_trap ();
132 if (save_errno
== EINTR
)
135 fprintf_unfiltered (gdb_stderr
,
136 "Child process unexpectedly missing: %s.\n",
137 safe_strerror (save_errno
));
139 /* Claim it exited with unknown signal. */
140 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
141 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
142 return pid_to_ptid (-1);
146 if (target_has_exited (pid
, status
, &exit_status
))
148 /* ??rehrauer: For now, ignore this. */
152 if (!target_thread_alive (pid_to_ptid (pid
)))
154 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
155 return pid_to_ptid (pid
);
158 while (pid
!= PIDGET (inferior_ptid
)); /* Some other child died or
161 store_waitstatus (ourstatus
, status
);
162 return pid_to_ptid (pid
);
165 /* Check to see if the given thread is alive.
167 FIXME: Is kill() ever the right way to do this? I doubt it, but
168 for now we're going to try and be compatable with the old thread
172 inf_ptrace_thread_alive (ptid_t ptid
)
174 pid_t pid
= PIDGET (ptid
);
176 return (kill (pid
, 0) != -1);
179 /* Attach to process PID, then initialize for debugging it. */
182 inf_ptrace_attach (char *args
, int from_tty
)
189 error_no_arg (_("process-id to attach"));
192 pid
= strtol (args
, &dummy
, 0);
193 /* Some targets don't set errno on errors, grrr! */
194 if (pid
== 0 && args
== dummy
)
195 error (_("Illegal process-id: %s."), args
);
197 if (pid
== getpid ()) /* Trying to masturbate? */
198 error (_("I refuse to debug myself!"));
202 exec_file
= (char *) get_exec_file (0);
205 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
206 target_pid_to_str (pid_to_ptid (pid
)));
208 printf_unfiltered (_("Attaching to %s\n"),
209 target_pid_to_str (pid_to_ptid (pid
)));
211 gdb_flush (gdb_stdout
);
216 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
218 perror_with_name (("ptrace"));
221 error (_("This system does not support attaching to a process"));
224 inferior_ptid
= pid_to_ptid (pid
);
225 push_target (ptrace_ops_hack
);
227 /* Do this first, before anything has had a chance to query the
228 inferior's symbol table or similar. */
229 observer_notify_inferior_created (¤t_target
, from_tty
);
233 inf_ptrace_post_attach (int pid
)
235 /* This version of Unix doesn't require a meaningful "post attach"
236 operation by a debugger. */
239 /* Take a program previously attached to and detaches it. The program
240 resumes execution and will no longer stop on signals, etc. We'd
241 better not have left any breakpoints in the program or it'll die
242 when it hits one. For this to work, it may be necessary for the
243 process to have been previously attached. It *might* work if the
244 program was started via the normal ptrace (PTRACE_TRACEME). */
247 inf_ptrace_detach (char *args
, int from_tty
)
250 int pid
= PIDGET (inferior_ptid
);
254 char *exec_file
= get_exec_file (0);
257 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
258 target_pid_to_str (pid_to_ptid (pid
)));
259 gdb_flush (gdb_stdout
);
266 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
) 1, sig
);
268 perror_with_name (("ptrace"));
271 error (_("This system does not support detaching from a process"));
274 inferior_ptid
= null_ptid
;
275 unpush_target (ptrace_ops_hack
);
278 /* Print status information about what we're accessing. */
281 inf_ptrace_files_info (struct target_ops
*ignore
)
283 printf_unfiltered (_("\tUsing the running image of %s %s.\n"),
284 attach_flag
? "attached" : "child",
285 target_pid_to_str (inferior_ptid
));
289 inf_ptrace_open (char *arg
, int from_tty
)
291 error (_("Use the \"run\" command to start a Unix child process."));
294 /* Stub function which causes the inferior that runs it, to be ptrace-able
295 by its parent process. */
300 /* "Trace me, Dr. Memory!" */
301 ptrace (0, 0, (PTRACE_TYPE_ARG3
) 0, 0);
304 /* Stub function which causes the GDB that runs it, to start ptrace-ing
305 the child process. */
308 inf_ptrace_him (int pid
)
310 push_target (ptrace_ops_hack
);
312 /* On some targets, there must be some explicit synchronization
313 between the parent and child processes after the debugger
314 forks, and before the child execs the debuggee program. This
315 call basically gives permission for the child to exec. */
317 target_acknowledge_created_inferior (pid
);
319 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
320 be 1 or 2 depending on whether we're starting without or with a
322 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
324 /* On some targets, there must be some explicit actions taken after
325 the inferior has been started up. */
326 target_post_startup_inferior (pid_to_ptid (pid
));
329 /* Start an inferior Unix child process and sets inferior_ptid to its
330 pid. EXEC_FILE is the file to run. ALLARGS is a string containing
331 the arguments to the program. ENV is the environment vector to
332 pass. Errors reported with error(). */
335 inf_ptrace_create_inferior (char *exec_file
, char *allargs
, char **env
,
338 fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, inf_ptrace_him
,
340 /* We are at the first instruction we care about. */
341 observer_notify_inferior_created (¤t_target
, from_tty
);
342 /* Pedal to the metal... */
343 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
347 inf_ptrace_reported_exec_events_per_exec_call (void)
349 /* Typically, we get a single SIGTRAP per exec. */
354 inf_ptrace_has_exited (int pid
, int wait_status
, int *exit_status
)
356 if (WIFEXITED (wait_status
))
358 *exit_status
= WEXITSTATUS (wait_status
);
362 if (WIFSIGNALED (wait_status
))
364 *exit_status
= 0; /* ?? Don't know what else to say here. */
368 /* ??? Do we really need to consult the event state, too?
369 Assume the wait_state alone suffices. */
374 inf_ptrace_mourn_inferior (void)
376 unpush_target (ptrace_ops_hack
);
377 generic_mourn_inferior ();
381 inf_ptrace_can_run (void)
386 /* Send a SIGINT to the process group. This acts just like the user
387 typed a ^C on the controlling terminal.
389 FIXME: This may not be correct for all systems. Some may want to
390 use killpg() instead of kill (-pgrp). */
393 inf_ptrace_stop (void)
395 kill (-inferior_process_group
, SIGINT
);
398 /* Perform a partial transfer to/from the specified object. For
399 memory transfers, fall back to the old memory xfer functions. */
402 inf_ptrace_xfer_partial (struct target_ops
*ops
, enum target_object object
,
403 const char *annex
, gdb_byte
*readbuf
,
404 const gdb_byte
*writebuf
,
405 ULONGEST offset
, LONGEST len
)
409 case TARGET_OBJECT_MEMORY
:
411 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
412 request that promises to be much more efficient in reading
413 and writing data in the traced process's address space. */
415 struct ptrace_io_desc piod
;
417 /* NOTE: We assume that there are no distinct address spaces
418 for instruction and data. */
419 piod
.piod_op
= writebuf
? PIOD_WRITE_D
: PIOD_READ_D
;
420 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
421 piod
.piod_offs
= (void *) (long) offset
;
425 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == 0)
426 /* Return the actual number of bytes read or written. */
427 return piod
.piod_len
;
428 /* If the PT_IO request is somehow not supported, fallback on
429 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
430 to indicate failure. */
438 PTRACE_TYPE_RET word
;
439 unsigned char byte
[sizeof (PTRACE_TYPE_RET
)];
441 ULONGEST rounded_offset
;
444 /* Round the start offset down to the next long word
446 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
448 /* Since ptrace will transfer a single word starting at that
449 rounded_offset the partial_len needs to be adjusted down to
450 that (remember this function only does a single transfer).
451 Should the required length be even less, adjust it down
453 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
454 if (partial_len
> len
)
459 /* If OFFSET:PARTIAL_LEN is smaller than
460 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
461 be needed. Read in the entire word. */
462 if (rounded_offset
< offset
463 || (offset
+ partial_len
464 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
465 /* Need part of initial word -- fetch it. */
466 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
467 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
470 /* Copy data to be written over corresponding part of
472 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
473 writebuf
, partial_len
);
476 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
477 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
481 /* Using the appropriate one (I or D) is necessary for
482 Gould NP1, at least. */
484 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
485 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
494 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
495 (PTRACE_TYPE_ARG3
) (long) rounded_offset
, 0);
498 /* Copy appropriate bytes out of the buffer. */
499 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
505 case TARGET_OBJECT_UNWIND_TABLE
:
508 case TARGET_OBJECT_AUXV
:
511 case TARGET_OBJECT_WCOOKIE
:
520 inf_ptrace_pid_to_str (ptid_t ptid
)
522 return normal_pid_to_str (ptid
);
525 /* Create a prototype ptrace target. The client can override it with
529 inf_ptrace_target (void)
531 struct target_ops
*t
= inf_child_target ();
533 t
->to_open
= inf_ptrace_open
;
534 t
->to_attach
= inf_ptrace_attach
;
535 t
->to_post_attach
= inf_ptrace_post_attach
;
536 t
->to_detach
= inf_ptrace_detach
;
537 t
->to_resume
= inf_ptrace_resume
;
538 t
->to_wait
= inf_ptrace_wait
;
539 t
->to_xfer_partial
= inf_ptrace_xfer_partial
;
540 t
->to_files_info
= inf_ptrace_files_info
;
541 t
->to_kill
= inf_ptrace_kill_inferior
;
542 t
->to_create_inferior
= inf_ptrace_create_inferior
;
543 t
->to_reported_exec_events_per_exec_call
=
544 inf_ptrace_reported_exec_events_per_exec_call
;
545 t
->to_has_exited
= inf_ptrace_has_exited
;
546 t
->to_mourn_inferior
= inf_ptrace_mourn_inferior
;
547 t
->to_can_run
= inf_ptrace_can_run
;
548 t
->to_thread_alive
= inf_ptrace_thread_alive
;
549 t
->to_pid_to_str
= inf_ptrace_pid_to_str
;
550 t
->to_stop
= inf_ptrace_stop
;
551 t
->to_stratum
= process_stratum
;
552 t
->to_has_all_memory
= 1;
553 t
->to_has_memory
= 1;
555 t
->to_has_registers
= 1;
556 t
->to_has_execution
= 1;
557 t
->to_magic
= OPS_MAGIC
;
564 /* Pointer to a function that returns the oggset within the user area
565 where a particular register is stored. */
566 static CORE_ADDR (*inf_ptrace_register_u_offset
)(int);
568 /* Fetch register REGNUM from the inferior. */
571 inf_ptrace_fetch_register (int regnum
)
575 PTRACE_TYPE_RET
*buf
;
578 /* Cater for systems like GNU/Linux, that implement threads as
579 seperate processes. */
580 pid
= ptid_get_lwp (inferior_ptid
);
582 pid
= ptid_get_pid (inferior_ptid
);
584 /* This isn't really an address, but ptrace thinks of it as one. */
585 addr
= inf_ptrace_register_u_offset (regnum
);
586 size
= register_size (current_gdbarch
, regnum
);
588 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
591 /* Read the register contents from the inferior a chuck at the time. */
592 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
595 buf
[i
] = ptrace (PT_READ_U
, pid
, (PTRACE_TYPE_ARG3
) addr
, 0);
597 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regnum
),
598 regnum
, safe_strerror (errno
));
600 addr
+= sizeof (PTRACE_TYPE_RET
);
602 regcache_raw_supply (current_regcache
, regnum
, buf
);
605 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
606 for all registers. */
609 inf_ptrace_fetch_registers (int regnum
)
612 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
613 inf_ptrace_fetch_register (regnum
);
615 inf_ptrace_fetch_register (regnum
);
618 /* Store register REGNUM into the inferior. */
621 inf_ptrace_store_register (int regnum
)
625 PTRACE_TYPE_RET
*buf
;
628 /* Cater for systems like GNU/Linux, that implement threads as
629 seperate processes. */
630 pid
= ptid_get_lwp (inferior_ptid
);
632 pid
= ptid_get_pid (inferior_ptid
);
634 /* This isn't really an address, but ptrace thinks of it as one. */
635 addr
= inf_ptrace_register_u_offset (regnum
);
636 size
= register_size (current_gdbarch
, regnum
);
638 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
641 /* Write the register contents into the inferior a chunk at the time. */
642 regcache_raw_collect (current_regcache
, regnum
, buf
);
643 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
646 ptrace (PT_WRITE_U
, pid
, (PTRACE_TYPE_ARG3
) addr
, buf
[i
]);
648 error (_("Couldn't write register %s (#%d): %s."), REGISTER_NAME (regnum
),
649 regnum
, safe_strerror (errno
));
651 addr
+= sizeof (PTRACE_TYPE_RET
);
655 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
656 this for all registers. */
659 inf_ptrace_store_registers (int regnum
)
662 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
663 inf_ptrace_store_register (regnum
);
665 inf_ptrace_store_register (regnum
);
668 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
669 a function returning the offset within the user area where a
670 particular register is stored. */
673 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset
)(int))
675 struct target_ops
*t
= inf_ptrace_target();
677 gdb_assert (register_u_offset
);
678 inf_ptrace_register_u_offset
= register_u_offset
;
679 t
->to_fetch_registers
= inf_ptrace_fetch_registers
;
680 t
->to_store_registers
= inf_ptrace_store_registers
;