1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "gdb_ptrace.h"
31 #include "inf-child.h"
33 #include "gdb_string.h"
38 /* HACK: Save the ptrace ops returned by ptrace_target. */
39 static struct target_ops
*ptrace_ops_hack
;
42 inf_ptrace_kill_inferior (void)
45 int pid
= PIDGET (inferior_ptid
);
50 /* This once used to call "kill" to kill the inferior just in case
51 the inferior was still running. As others have noted in the past
52 (kingdon) there shouldn't be any way to get here if the inferior
53 is still running -- else there's a major problem elsewere in gdb
54 and it needs to be fixed.
56 The kill call causes problems under hpux10, so it's been removed;
57 if this causes problems we'll deal with them as they arise. */
58 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
60 target_mourn_inferior ();
63 /* Resume execution of the inferior process. If STEP is nonzero,
64 single-step it. If SIGNAL is nonzero, give it that signal. */
67 inf_ptrace_resume (ptid_t ptid
, int step
, enum target_signal signal
)
69 int request
= PT_CONTINUE
;
70 int pid
= PIDGET (ptid
);
73 /* Resume all threads. */
74 /* I think this only gets used in the non-threaded case, where
75 "resume all threads" and "resume inferior_ptid" are the
77 pid
= PIDGET (inferior_ptid
);
81 /* If this system does not support PT_STEP, a higher level
82 function will have called single_step() to transmute the step
83 request into a continue request (by setting breakpoints on
84 all possible successor instructions), so we don't have to
85 worry about that here. */
89 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
90 where it was. If GDB wanted it to start some other way, we have
91 already written a new PC value to the child. */
93 ptrace (request
, pid
, (PTRACE_TYPE_ARG3
) 1, target_signal_to_host (signal
));
95 perror_with_name ("ptrace");
98 /* Wait for child to do something. Return pid of child, or -1 in case
99 of error; store status through argument pointer OURSTATUS. */
102 inf_ptrace_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
106 char *execd_pathname
= NULL
;
110 enum target_waitkind kind
;
115 set_sigint_trap (); /* Causes SIGINT to be passed on to the
119 pid
= wait (&status
);
125 clear_sigint_trap ();
129 if (save_errno
== EINTR
)
132 fprintf_unfiltered (gdb_stderr
,
133 "Child process unexpectedly missing: %s.\n",
134 safe_strerror (save_errno
));
136 /* Claim it exited with unknown signal. */
137 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
138 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
139 return pid_to_ptid (-1);
144 if (target_has_exited (pid
, status
, &exit_status
))
146 /* ??rehrauer: For now, ignore this. */
150 if (!target_thread_alive (pid_to_ptid (pid
)))
152 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
153 return pid_to_ptid (pid
);
156 while (pid
!= PIDGET (inferior_ptid
)); /* Some other child died or stopped */
158 store_waitstatus (ourstatus
, status
);
159 return pid_to_ptid (pid
);
162 /* Check to see if the given thread is alive.
164 FIXME: Is kill() ever the right way to do this? I doubt it, but
165 for now we're going to try and be compatable with the old thread
169 inf_ptrace_thread_alive (ptid_t ptid
)
171 pid_t pid
= PIDGET (ptid
);
173 return (kill (pid
, 0) != -1);
176 /* Attach to process PID, then initialize for debugging it. */
179 inf_ptrace_attach (char *args
, int from_tty
)
186 error_no_arg ("process-id to attach");
189 pid
= strtol (args
, &dummy
, 0);
190 /* Some targets don't set errno on errors, grrr! */
191 if (pid
== 0 && args
== dummy
)
192 error ("Illegal process-id: %s\n", args
);
194 if (pid
== getpid ()) /* Trying to masturbate? */
195 error ("I refuse to debug myself!");
199 exec_file
= (char *) get_exec_file (0);
202 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
203 target_pid_to_str (pid_to_ptid (pid
)));
205 printf_unfiltered ("Attaching to %s\n",
206 target_pid_to_str (pid_to_ptid (pid
)));
208 gdb_flush (gdb_stdout
);
213 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
215 perror_with_name ("ptrace");
218 error ("This system does not support attaching to a process");
221 inferior_ptid
= pid_to_ptid (pid
);
222 push_target (ptrace_ops_hack
);
224 /* Do this first, before anything has had a chance to query the
225 inferior's symbol table or similar. */
226 observer_notify_inferior_created (¤t_target
, from_tty
);
230 inf_ptrace_post_attach (int pid
)
232 /* This version of Unix doesn't require a meaningful "post attach"
233 operation by a debugger. */
236 /* Take a program previously attached to and detaches it. The program
237 resumes execution and will no longer stop on signals, etc. We'd
238 better not have left any breakpoints in the program or it'll die
239 when it hits one. For this to work, it may be necessary for the
240 process to have been previously attached. It *might* work if the
241 program was started via the normal ptrace (PTRACE_TRACEME). */
244 inf_ptrace_detach (char *args
, int from_tty
)
247 int pid
= PIDGET (inferior_ptid
);
251 char *exec_file
= get_exec_file (0);
254 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
255 target_pid_to_str (pid_to_ptid (pid
)));
256 gdb_flush (gdb_stdout
);
263 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
) 1, sig
);
265 perror_with_name ("ptrace");
268 error ("This system does not support detaching from a process");
271 inferior_ptid
= null_ptid
;
272 unpush_target (ptrace_ops_hack
);
275 /* Get ready to modify the registers array. On machines which store
276 individual registers, this doesn't need to do anything. On
277 machines which store all the registers in one fell swoop, this
278 makes sure that registers contains all the registers from the
279 program being debugged. */
282 inf_ptrace_prepare_to_store (void)
286 /* Print status information about what we're accessing. */
289 inf_ptrace_files_info (struct target_ops
*ignore
)
291 printf_unfiltered ("\tUsing the running image of %s %s.\n",
292 attach_flag
? "attached" : "child",
293 target_pid_to_str (inferior_ptid
));
297 inf_ptrace_open (char *arg
, int from_tty
)
299 error ("Use the \"run\" command to start a Unix child process.");
302 /* Stub function which causes the inferior that runs it, to be ptrace-able
303 by its parent process. */
308 /* "Trace me, Dr. Memory!" */
309 ptrace (0, 0, (PTRACE_TYPE_ARG3
) 0, 0);
312 /* Stub function which causes the GDB that runs it, to start ptrace-ing
313 the child process. */
316 inf_ptrace_him (int pid
)
318 push_target (ptrace_ops_hack
);
320 /* On some targets, there must be some explicit synchronization
321 between the parent and child processes after the debugger
322 forks, and before the child execs the debuggee program. This
323 call basically gives permission for the child to exec.
326 target_acknowledge_created_inferior (pid
);
328 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
329 * and will be 1 or 2 depending on whether we're starting
330 * without or with a shell.
332 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
334 /* On some targets, there must be some explicit actions taken after
335 the inferior has been started up.
337 target_post_startup_inferior (pid_to_ptid (pid
));
340 /* Start an inferior Unix child process and sets inferior_ptid to its
341 pid. EXEC_FILE is the file to run. ALLARGS is a string containing
342 the arguments to the program. ENV is the environment vector to
343 pass. Errors reported with error(). */
346 inf_ptrace_create_inferior (char *exec_file
, char *allargs
, char **env
,
349 fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, inf_ptrace_him
,
351 /* We are at the first instruction we care about. */
352 observer_notify_inferior_created (¤t_target
, from_tty
);
353 /* Pedal to the metal... */
354 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
358 inf_ptrace_post_startup_inferior (ptid_t ptid
)
360 /* This version of Unix doesn't require a meaningful "post startup inferior"
361 operation by a debugger.
366 inf_ptrace_acknowledge_created_inferior (int pid
)
368 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
369 operation by a debugger.
374 inf_ptrace_insert_fork_catchpoint (int pid
)
376 /* This version of Unix doesn't support notification of fork events. */
381 inf_ptrace_remove_fork_catchpoint (int pid
)
383 /* This version of Unix doesn't support notification of fork events. */
388 inf_ptrace_insert_vfork_catchpoint (int pid
)
390 /* This version of Unix doesn't support notification of vfork events. */
395 inf_ptrace_remove_vfork_catchpoint (int pid
)
397 /* This version of Unix doesn't support notification of vfork events. */
402 inf_ptrace_follow_fork (int follow_child
)
404 /* This version of Unix doesn't support following fork or vfork events. */
409 inf_ptrace_insert_exec_catchpoint (int pid
)
411 /* This version of Unix doesn't support notification of exec events. */
416 inf_ptrace_remove_exec_catchpoint (int pid
)
418 /* This version of Unix doesn't support notification of exec events. */
423 inf_ptrace_reported_exec_events_per_exec_call (void)
425 /* This version of Unix doesn't support notification of exec events.
431 inf_ptrace_has_exited (int pid
, int wait_status
, int *exit_status
)
433 if (WIFEXITED (wait_status
))
435 *exit_status
= WEXITSTATUS (wait_status
);
439 if (WIFSIGNALED (wait_status
))
441 *exit_status
= 0; /* ?? Don't know what else to say here. */
445 /* ?? Do we really need to consult the event state, too? Assume the
446 wait_state alone suffices.
452 inf_ptrace_mourn_inferior (void)
454 unpush_target (ptrace_ops_hack
);
455 generic_mourn_inferior ();
459 inf_ptrace_can_run (void)
464 /* Send a SIGINT to the process group. This acts just like the user
465 typed a ^C on the controlling terminal.
467 XXX - This may not be correct for all systems. Some may want to
468 use killpg() instead of kill (-pgrp). */
471 inf_ptrace_stop (void)
473 kill (-inferior_process_group
, SIGINT
);
476 /* Perform a partial transfer to/from the specified object. For
477 memory transfers, fall back to the old memory xfer functions. */
480 inf_ptrace_xfer_partial (struct target_ops
*ops
, enum target_object object
,
481 const char *annex
, void *readbuf
,
482 const void *writebuf
, ULONGEST offset
, LONGEST len
)
486 case TARGET_OBJECT_MEMORY
:
488 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
489 request that promises to be much more efficient in reading
490 and writing data in the traced process's address space. */
492 struct ptrace_io_desc piod
;
494 /* NOTE: We assume that there are no distinct address spaces
495 for instruction and data. */
496 piod
.piod_op
= writebuf
? PIOD_WRITE_D
: PIOD_READ_D
;
497 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
498 piod
.piod_offs
= (void *) (long) offset
;
502 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == 0)
503 /* Return the actual number of bytes read or written. */
504 return piod
.piod_len
;
505 /* If the PT_IO request is somehow not supported, fallback on
506 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
507 to indicate failure. */
515 PTRACE_TYPE_RET word
;
516 unsigned char byte
[sizeof (PTRACE_TYPE_RET
)];
518 ULONGEST rounded_offset
;
521 /* Round the start offset down to the next long word
523 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
525 /* Since ptrace will transfer a single word starting at that
526 rounded_offset the partial_len needs to be adjusted down to
527 that (remember this function only does a single transfer).
528 Should the required length be even less, adjust it down
530 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
531 if (partial_len
> len
)
536 /* If OFFSET:PARTIAL_LEN is smaller than
537 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
538 be needed. Read in the entire word. */
539 if (rounded_offset
< offset
540 || (offset
+ partial_len
541 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
542 /* Need part of initial word -- fetch it. */
543 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
544 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
547 /* Copy data to be written over corresponding part of
549 memcpy (buffer
.byte
+ (offset
- rounded_offset
), writebuf
, partial_len
);
552 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
553 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
557 /* Using the appropriate one (I or D) is necessary for
558 Gould NP1, at least. */
560 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
561 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
570 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
571 (PTRACE_TYPE_ARG3
) (long) rounded_offset
, 0);
574 /* Copy appropriate bytes out of the buffer. */
575 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
581 case TARGET_OBJECT_UNWIND_TABLE
:
584 case TARGET_OBJECT_AUXV
:
587 case TARGET_OBJECT_WCOOKIE
:
596 inf_ptrace_pid_to_str (ptid_t ptid
)
598 return normal_pid_to_str (ptid
);
602 inf_ptrace_target (void)
604 struct target_ops
*t
= inf_child_target ();
605 t
->to_open
= inf_ptrace_open
;
606 t
->to_attach
= inf_ptrace_attach
;
607 t
->to_post_attach
= inf_ptrace_post_attach
;
608 t
->to_detach
= inf_ptrace_detach
;
609 t
->to_resume
= inf_ptrace_resume
;
610 t
->to_wait
= inf_ptrace_wait
;
611 t
->to_prepare_to_store
= inf_ptrace_prepare_to_store
;
612 t
->to_xfer_partial
= inf_ptrace_xfer_partial
;
613 t
->to_files_info
= inf_ptrace_files_info
;
614 t
->to_kill
= inf_ptrace_kill_inferior
;
615 t
->to_create_inferior
= inf_ptrace_create_inferior
;
616 t
->to_post_startup_inferior
= inf_ptrace_post_startup_inferior
;
617 t
->to_acknowledge_created_inferior
=
618 inf_ptrace_acknowledge_created_inferior
;
619 t
->to_insert_fork_catchpoint
= inf_ptrace_insert_fork_catchpoint
;
620 t
->to_remove_fork_catchpoint
= inf_ptrace_remove_fork_catchpoint
;
621 t
->to_insert_vfork_catchpoint
= inf_ptrace_insert_vfork_catchpoint
;
622 t
->to_remove_vfork_catchpoint
= inf_ptrace_remove_vfork_catchpoint
;
623 t
->to_follow_fork
= inf_ptrace_follow_fork
;
624 t
->to_insert_exec_catchpoint
= inf_ptrace_insert_exec_catchpoint
;
625 t
->to_remove_exec_catchpoint
= inf_ptrace_remove_exec_catchpoint
;
626 t
->to_reported_exec_events_per_exec_call
=
627 inf_ptrace_reported_exec_events_per_exec_call
;
628 t
->to_has_exited
= inf_ptrace_has_exited
;
629 t
->to_mourn_inferior
= inf_ptrace_mourn_inferior
;
630 t
->to_can_run
= inf_ptrace_can_run
;
631 t
->to_thread_alive
= inf_ptrace_thread_alive
;
632 t
->to_pid_to_str
= inf_ptrace_pid_to_str
;
633 t
->to_stop
= inf_ptrace_stop
;
634 t
->to_stratum
= process_stratum
;
635 t
->to_has_all_memory
= 1;
636 t
->to_has_memory
= 1;
638 t
->to_has_registers
= 1;
639 t
->to_has_execution
= 1;
640 t
->to_magic
= OPS_MAGIC
;