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 HP-UX 10, so it's been
57 removed; if this causes problems we'll deal with them as they
59 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
61 target_mourn_inferior ();
64 /* Resume execution of the inferior process. If STEP is nonzero,
65 single-step it. If SIGNAL is nonzero, give it that signal. */
68 inf_ptrace_resume (ptid_t ptid
, int step
, enum target_signal signal
)
70 int request
= PT_CONTINUE
;
71 int pid
= PIDGET (ptid
);
74 /* Resume all threads. */
75 /* I think this only gets used in the non-threaded case, where
76 "resume all threads" and "resume inferior_ptid" are the
78 pid
= PIDGET (inferior_ptid
);
82 /* If this system does not support PT_STEP, a higher level
83 function will have called single_step() to transmute the step
84 request into a continue request (by setting breakpoints on
85 all possible successor instructions), so we don't have to
86 worry about that here. */
90 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
91 where it was. If GDB wanted it to start some other way, we have
92 already written a new PC value to the child. */
94 ptrace (request
, pid
, (PTRACE_TYPE_ARG3
) 1, target_signal_to_host (signal
));
96 perror_with_name ("ptrace");
99 /* Wait for child to do something. Return pid of child, or -1 in case
100 of error; store status through argument pointer OURSTATUS. */
103 inf_ptrace_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
107 char *execd_pathname
= NULL
;
111 enum target_waitkind kind
;
116 set_sigint_trap (); /* Causes SIGINT to be passed on to the
120 pid
= wait (&status
);
126 clear_sigint_trap ();
130 if (save_errno
== EINTR
)
133 fprintf_unfiltered (gdb_stderr
,
134 "Child process unexpectedly missing: %s.\n",
135 safe_strerror (save_errno
));
137 /* Claim it exited with unknown signal. */
138 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
139 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
140 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
159 store_waitstatus (ourstatus
, status
);
160 return pid_to_ptid (pid
);
163 /* Check to see if the given thread is alive.
165 FIXME: Is kill() ever the right way to do this? I doubt it, but
166 for now we're going to try and be compatable with the old thread
170 inf_ptrace_thread_alive (ptid_t ptid
)
172 pid_t pid
= PIDGET (ptid
);
174 return (kill (pid
, 0) != -1);
177 /* Attach to process PID, then initialize for debugging it. */
180 inf_ptrace_attach (char *args
, int from_tty
)
187 error_no_arg ("process-id to attach");
190 pid
= strtol (args
, &dummy
, 0);
191 /* Some targets don't set errno on errors, grrr! */
192 if (pid
== 0 && args
== dummy
)
193 error ("Illegal process-id: %s\n", args
);
195 if (pid
== getpid ()) /* Trying to masturbate? */
196 error ("I refuse to debug myself!");
200 exec_file
= (char *) get_exec_file (0);
203 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
204 target_pid_to_str (pid_to_ptid (pid
)));
206 printf_unfiltered ("Attaching to %s\n",
207 target_pid_to_str (pid_to_ptid (pid
)));
209 gdb_flush (gdb_stdout
);
214 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
216 perror_with_name ("ptrace");
219 error ("This system does not support attaching to a process");
222 inferior_ptid
= pid_to_ptid (pid
);
223 push_target (ptrace_ops_hack
);
225 /* Do this first, before anything has had a chance to query the
226 inferior's symbol table or similar. */
227 observer_notify_inferior_created (¤t_target
, from_tty
);
231 inf_ptrace_post_attach (int pid
)
233 /* This version of Unix doesn't require a meaningful "post attach"
234 operation by a debugger. */
237 /* Take a program previously attached to and detaches it. The program
238 resumes execution and will no longer stop on signals, etc. We'd
239 better not have left any breakpoints in the program or it'll die
240 when it hits one. For this to work, it may be necessary for the
241 process to have been previously attached. It *might* work if the
242 program was started via the normal ptrace (PTRACE_TRACEME). */
245 inf_ptrace_detach (char *args
, int from_tty
)
248 int pid
= PIDGET (inferior_ptid
);
252 char *exec_file
= get_exec_file (0);
255 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
256 target_pid_to_str (pid_to_ptid (pid
)));
257 gdb_flush (gdb_stdout
);
264 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
) 1, sig
);
266 perror_with_name ("ptrace");
269 error ("This system does not support detaching from a process");
272 inferior_ptid
= null_ptid
;
273 unpush_target (ptrace_ops_hack
);
276 /* Get ready to modify the registers array. On machines which store
277 individual registers, this doesn't need to do anything. On
278 machines which store all the registers in one fell swoop, this
279 makes sure that registers contains all the registers from the
280 program being debugged. */
283 inf_ptrace_prepare_to_store (void)
287 /* Print status information about what we're accessing. */
290 inf_ptrace_files_info (struct target_ops
*ignore
)
292 printf_unfiltered ("\tUsing the running image of %s %s.\n",
293 attach_flag
? "attached" : "child",
294 target_pid_to_str (inferior_ptid
));
298 inf_ptrace_open (char *arg
, int from_tty
)
300 error ("Use the \"run\" command to start a Unix child process.");
303 /* Stub function which causes the inferior that runs it, to be ptrace-able
304 by its parent process. */
309 /* "Trace me, Dr. Memory!" */
310 ptrace (0, 0, (PTRACE_TYPE_ARG3
) 0, 0);
313 /* Stub function which causes the GDB that runs it, to start ptrace-ing
314 the child process. */
317 inf_ptrace_him (int pid
)
319 push_target (ptrace_ops_hack
);
321 /* On some targets, there must be some explicit synchronization
322 between the parent and child processes after the debugger
323 forks, and before the child execs the debuggee program. This
324 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, and will
329 be 1 or 2 depending on whether we're starting without or with a
331 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
333 /* On some targets, there must be some explicit actions taken after
334 the inferior has been started up. */
335 target_post_startup_inferior (pid_to_ptid (pid
));
338 /* Start an inferior Unix child process and sets inferior_ptid to its
339 pid. EXEC_FILE is the file to run. ALLARGS is a string containing
340 the arguments to the program. ENV is the environment vector to
341 pass. Errors reported with error(). */
344 inf_ptrace_create_inferior (char *exec_file
, char *allargs
, char **env
,
347 fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, inf_ptrace_him
,
349 /* We are at the first instruction we care about. */
350 observer_notify_inferior_created (¤t_target
, from_tty
);
351 /* Pedal to the metal... */
352 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
356 inf_ptrace_post_startup_inferior (ptid_t ptid
)
358 /* This version of Unix doesn't require a meaningful "post startup
359 inferior" operation by a debugger. */
363 inf_ptrace_acknowledge_created_inferior (int pid
)
365 /* This version of Unix doesn't require a meaningful "acknowledge
366 created inferior" operation by a debugger. */
370 inf_ptrace_insert_fork_catchpoint (int pid
)
372 /* This version of Unix doesn't support notification of fork events. */
377 inf_ptrace_remove_fork_catchpoint (int pid
)
379 /* This version of Unix doesn't support notification of fork events. */
384 inf_ptrace_insert_vfork_catchpoint (int pid
)
386 /* This version of Unix doesn't support notification of vfork events. */
391 inf_ptrace_remove_vfork_catchpoint (int pid
)
393 /* This version of Unix doesn't support notification of vfork events. */
398 inf_ptrace_follow_fork (int follow_child
)
400 /* This version of Unix doesn't support following fork or vfork events. */
405 inf_ptrace_insert_exec_catchpoint (int pid
)
407 /* This version of Unix doesn't support notification of exec events. */
412 inf_ptrace_remove_exec_catchpoint (int pid
)
414 /* This version of Unix doesn't support notification of exec events. */
419 inf_ptrace_reported_exec_events_per_exec_call (void)
421 /* This version of Unix doesn't support notification of exec events. */
426 inf_ptrace_has_exited (int pid
, int wait_status
, int *exit_status
)
428 if (WIFEXITED (wait_status
))
430 *exit_status
= WEXITSTATUS (wait_status
);
434 if (WIFSIGNALED (wait_status
))
436 *exit_status
= 0; /* ?? Don't know what else to say here. */
440 /* ??? Do we really need to consult the event state, too?
441 Assume the wait_state alone suffices. */
446 inf_ptrace_mourn_inferior (void)
448 unpush_target (ptrace_ops_hack
);
449 generic_mourn_inferior ();
453 inf_ptrace_can_run (void)
458 /* Send a SIGINT to the process group. This acts just like the user
459 typed a ^C on the controlling terminal.
461 FIXME: This may not be correct for all systems. Some may want to
462 use killpg() instead of kill (-pgrp). */
465 inf_ptrace_stop (void)
467 kill (-inferior_process_group
, SIGINT
);
470 /* Perform a partial transfer to/from the specified object. For
471 memory transfers, fall back to the old memory xfer functions. */
474 inf_ptrace_xfer_partial (struct target_ops
*ops
, enum target_object object
,
475 const char *annex
, void *readbuf
,
476 const void *writebuf
, ULONGEST offset
, LONGEST len
)
480 case TARGET_OBJECT_MEMORY
:
482 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
483 request that promises to be much more efficient in reading
484 and writing data in the traced process's address space. */
486 struct ptrace_io_desc piod
;
488 /* NOTE: We assume that there are no distinct address spaces
489 for instruction and data. */
490 piod
.piod_op
= writebuf
? PIOD_WRITE_D
: PIOD_READ_D
;
491 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
492 piod
.piod_offs
= (void *) (long) offset
;
496 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == 0)
497 /* Return the actual number of bytes read or written. */
498 return piod
.piod_len
;
499 /* If the PT_IO request is somehow not supported, fallback on
500 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
501 to indicate failure. */
509 PTRACE_TYPE_RET word
;
510 unsigned char byte
[sizeof (PTRACE_TYPE_RET
)];
512 ULONGEST rounded_offset
;
515 /* Round the start offset down to the next long word
517 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
519 /* Since ptrace will transfer a single word starting at that
520 rounded_offset the partial_len needs to be adjusted down to
521 that (remember this function only does a single transfer).
522 Should the required length be even less, adjust it down
524 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
525 if (partial_len
> len
)
530 /* If OFFSET:PARTIAL_LEN is smaller than
531 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
532 be needed. Read in the entire word. */
533 if (rounded_offset
< offset
534 || (offset
+ partial_len
535 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
536 /* Need part of initial word -- fetch it. */
537 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
538 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
541 /* Copy data to be written over corresponding part of
543 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
544 writebuf
, partial_len
);
547 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
548 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
552 /* Using the appropriate one (I or D) is necessary for
553 Gould NP1, at least. */
555 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
556 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
565 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
566 (PTRACE_TYPE_ARG3
) (long) rounded_offset
, 0);
569 /* Copy appropriate bytes out of the buffer. */
570 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
576 case TARGET_OBJECT_UNWIND_TABLE
:
579 case TARGET_OBJECT_AUXV
:
582 case TARGET_OBJECT_WCOOKIE
:
591 inf_ptrace_pid_to_str (ptid_t ptid
)
593 return normal_pid_to_str (ptid
);
597 inf_ptrace_target (void)
599 struct target_ops
*t
= inf_child_target ();
600 t
->to_open
= inf_ptrace_open
;
601 t
->to_attach
= inf_ptrace_attach
;
602 t
->to_post_attach
= inf_ptrace_post_attach
;
603 t
->to_detach
= inf_ptrace_detach
;
604 t
->to_resume
= inf_ptrace_resume
;
605 t
->to_wait
= inf_ptrace_wait
;
606 t
->to_prepare_to_store
= inf_ptrace_prepare_to_store
;
607 t
->to_xfer_partial
= inf_ptrace_xfer_partial
;
608 t
->to_files_info
= inf_ptrace_files_info
;
609 t
->to_kill
= inf_ptrace_kill_inferior
;
610 t
->to_create_inferior
= inf_ptrace_create_inferior
;
611 t
->to_post_startup_inferior
= inf_ptrace_post_startup_inferior
;
612 t
->to_acknowledge_created_inferior
=
613 inf_ptrace_acknowledge_created_inferior
;
614 t
->to_insert_fork_catchpoint
= inf_ptrace_insert_fork_catchpoint
;
615 t
->to_remove_fork_catchpoint
= inf_ptrace_remove_fork_catchpoint
;
616 t
->to_insert_vfork_catchpoint
= inf_ptrace_insert_vfork_catchpoint
;
617 t
->to_remove_vfork_catchpoint
= inf_ptrace_remove_vfork_catchpoint
;
618 t
->to_follow_fork
= inf_ptrace_follow_fork
;
619 t
->to_insert_exec_catchpoint
= inf_ptrace_insert_exec_catchpoint
;
620 t
->to_remove_exec_catchpoint
= inf_ptrace_remove_exec_catchpoint
;
621 t
->to_reported_exec_events_per_exec_call
=
622 inf_ptrace_reported_exec_events_per_exec_call
;
623 t
->to_has_exited
= inf_ptrace_has_exited
;
624 t
->to_mourn_inferior
= inf_ptrace_mourn_inferior
;
625 t
->to_can_run
= inf_ptrace_can_run
;
626 t
->to_thread_alive
= inf_ptrace_thread_alive
;
627 t
->to_pid_to_str
= inf_ptrace_pid_to_str
;
628 t
->to_stop
= inf_ptrace_stop
;
629 t
->to_stratum
= process_stratum
;
630 t
->to_has_all_memory
= 1;
631 t
->to_has_memory
= 1;
633 t
->to_has_registers
= 1;
634 t
->to_has_execution
= 1;
635 t
->to_magic
= OPS_MAGIC
;