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 /* Set an upper limit on alloca. */
99 #define GDB_MAX_ALLOCA 0x1000
101 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
102 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
103 it appears that writing did not write the data that I specified. I
104 cannot understand where it got the data that it actually did
107 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
108 debugger memory starting at MYADDR. Copy to inferior if WRITE is
109 nonzero. TARGET is ignored.
111 Returns the length copied, which is either the LEN argument or
112 zero. This xfer function does not do partial moves, since
113 ptrace_ops_hack doesn't allow memory operations to cross below us in the
114 target stack anyway. */
117 inf_ptrace_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
118 struct mem_attrib
*attrib
, struct target_ops
*target
)
121 /* Round starting address down to longword boundary. */
122 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_TYPE_RET
);
123 /* Round ending address up; get number of longwords that makes. */
124 int count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_TYPE_RET
) - 1)
125 / sizeof (PTRACE_TYPE_RET
));
126 int alloc
= count
* sizeof (PTRACE_TYPE_RET
);
127 PTRACE_TYPE_RET
*buffer
;
128 struct cleanup
*old_chain
= NULL
;
131 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
132 that promises to be much more efficient in reading and writing
133 data in the traced process's address space. */
136 struct ptrace_io_desc piod
;
138 /* NOTE: We assume that there are no distinct address spaces for
139 instruction and data. */
140 piod
.piod_op
= write
? PIOD_WRITE_D
: PIOD_READ_D
;
141 piod
.piod_offs
= (void *) memaddr
;
142 piod
.piod_addr
= myaddr
;
145 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == -1)
147 /* If the PT_IO request is somehow not supported, fallback on
148 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
149 to indicate failure. */
155 /* Return the actual number of bytes read or written. */
156 return piod
.piod_len
;
161 /* Allocate buffer of that many longwords. */
162 if (len
< GDB_MAX_ALLOCA
)
164 buffer
= (PTRACE_TYPE_RET
*) alloca (alloc
);
168 buffer
= (PTRACE_TYPE_RET
*) xmalloc (alloc
);
169 old_chain
= make_cleanup (xfree
, buffer
);
174 /* Fill start and end extra bytes of buffer with existing memory
176 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_TYPE_RET
))
178 /* Need part of initial word -- fetch it. */
179 buffer
[0] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
180 (PTRACE_TYPE_ARG3
) addr
, 0);
183 if (count
> 1) /* FIXME, avoid if even boundary. */
186 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
188 (addr
+ (count
- 1) * sizeof (PTRACE_TYPE_RET
))), 0);
191 /* Copy data to be written over corresponding part of buffer. */
192 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_TYPE_RET
) - 1)),
195 /* Write the entire buffer. */
196 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_TYPE_RET
))
199 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
200 (PTRACE_TYPE_ARG3
) addr
, buffer
[i
]);
203 /* Using the appropriate one (I or D) is necessary for
204 Gould NP1, at least. */
206 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
207 (PTRACE_TYPE_ARG3
) addr
, buffer
[i
]);
215 /* Read all the longwords. */
216 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_TYPE_RET
))
219 buffer
[i
] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
220 (PTRACE_TYPE_ARG3
) addr
, 0);
226 /* Copy appropriate bytes out of the buffer. */
228 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_TYPE_RET
) - 1)),
232 if (old_chain
!= NULL
)
233 do_cleanups (old_chain
);
237 /* Wait for child to do something. Return pid of child, or -1 in case
238 of error; store status through argument pointer OURSTATUS. */
241 inf_ptrace_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
245 char *execd_pathname
= NULL
;
249 enum target_waitkind kind
;
254 set_sigint_trap (); /* Causes SIGINT to be passed on to the
258 pid
= wait (&status
);
264 clear_sigint_trap ();
268 if (save_errno
== EINTR
)
271 fprintf_unfiltered (gdb_stderr
,
272 "Child process unexpectedly missing: %s.\n",
273 safe_strerror (save_errno
));
275 /* Claim it exited with unknown signal. */
276 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
277 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
278 return pid_to_ptid (-1);
283 if (target_has_exited (pid
, status
, &exit_status
))
285 /* ??rehrauer: For now, ignore this. */
289 if (!target_thread_alive (pid_to_ptid (pid
)))
291 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
292 return pid_to_ptid (pid
);
295 while (pid
!= PIDGET (inferior_ptid
)); /* Some other child died or stopped */
297 store_waitstatus (ourstatus
, status
);
298 return pid_to_ptid (pid
);
301 /* Check to see if the given thread is alive.
303 FIXME: Is kill() ever the right way to do this? I doubt it, but
304 for now we're going to try and be compatable with the old thread
308 inf_ptrace_thread_alive (ptid_t ptid
)
310 pid_t pid
= PIDGET (ptid
);
312 return (kill (pid
, 0) != -1);
315 /* Attach to process PID, then initialize for debugging it. */
318 inf_ptrace_attach (char *args
, int from_tty
)
325 error_no_arg ("process-id to attach");
328 pid
= strtol (args
, &dummy
, 0);
329 /* Some targets don't set errno on errors, grrr! */
330 if (pid
== 0 && args
== dummy
)
331 error ("Illegal process-id: %s\n", args
);
333 if (pid
== getpid ()) /* Trying to masturbate? */
334 error ("I refuse to debug myself!");
338 exec_file
= (char *) get_exec_file (0);
341 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
342 target_pid_to_str (pid_to_ptid (pid
)));
344 printf_unfiltered ("Attaching to %s\n",
345 target_pid_to_str (pid_to_ptid (pid
)));
347 gdb_flush (gdb_stdout
);
352 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
354 perror_with_name ("ptrace");
357 error ("This system does not support attaching to a process");
360 inferior_ptid
= pid_to_ptid (pid
);
361 push_target (ptrace_ops_hack
);
365 inf_ptrace_post_attach (int pid
)
367 /* This version of Unix doesn't require a meaningful "post attach"
368 operation by a debugger. */
371 /* Take a program previously attached to and detaches it. The program
372 resumes execution and will no longer stop on signals, etc. We'd
373 better not have left any breakpoints in the program or it'll die
374 when it hits one. For this to work, it may be necessary for the
375 process to have been previously attached. It *might* work if the
376 program was started via the normal ptrace (PTRACE_TRACEME). */
379 inf_ptrace_detach (char *args
, int from_tty
)
382 int pid
= PIDGET (inferior_ptid
);
386 char *exec_file
= get_exec_file (0);
389 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
390 target_pid_to_str (pid_to_ptid (pid
)));
391 gdb_flush (gdb_stdout
);
398 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
) 1, sig
);
400 perror_with_name ("ptrace");
403 error ("This system does not support detaching from a process");
406 inferior_ptid
= null_ptid
;
407 unpush_target (ptrace_ops_hack
);
410 /* Get ready to modify the registers array. On machines which store
411 individual registers, this doesn't need to do anything. On
412 machines which store all the registers in one fell swoop, this
413 makes sure that registers contains all the registers from the
414 program being debugged. */
417 inf_ptrace_prepare_to_store (void)
421 /* Print status information about what we're accessing. */
424 inf_ptrace_files_info (struct target_ops
*ignore
)
426 printf_unfiltered ("\tUsing the running image of %s %s.\n",
427 attach_flag
? "attached" : "child",
428 target_pid_to_str (inferior_ptid
));
432 inf_ptrace_open (char *arg
, int from_tty
)
434 error ("Use the \"run\" command to start a Unix child process.");
437 /* Stub function which causes the inferior that runs it, to be ptrace-able
438 by its parent process. */
443 /* "Trace me, Dr. Memory!" */
444 ptrace (0, 0, (PTRACE_TYPE_ARG3
) 0, 0);
447 /* Stub function which causes the GDB that runs it, to start ptrace-ing
448 the child process. */
451 inf_ptrace_him (int pid
)
453 push_target (ptrace_ops_hack
);
455 /* On some targets, there must be some explicit synchronization
456 between the parent and child processes after the debugger
457 forks, and before the child execs the debuggee program. This
458 call basically gives permission for the child to exec.
461 target_acknowledge_created_inferior (pid
);
463 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
464 * and will be 1 or 2 depending on whether we're starting
465 * without or with a shell.
467 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
469 /* On some targets, there must be some explicit actions taken after
470 the inferior has been started up.
472 target_post_startup_inferior (pid_to_ptid (pid
));
475 /* Start an inferior Unix child process and sets inferior_ptid to its
476 pid. EXEC_FILE is the file to run. ALLARGS is a string containing
477 the arguments to the program. ENV is the environment vector to
478 pass. Errors reported with error(). */
481 inf_ptrace_create_inferior (char *exec_file
, char *allargs
, char **env
,
484 fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, inf_ptrace_him
,
486 /* We are at the first instruction we care about. */
487 observer_notify_inferior_created (¤t_target
, from_tty
);
488 /* Pedal to the metal... */
489 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
493 inf_ptrace_post_startup_inferior (ptid_t ptid
)
495 /* This version of Unix doesn't require a meaningful "post startup inferior"
496 operation by a debugger.
501 inf_ptrace_acknowledge_created_inferior (int pid
)
503 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
504 operation by a debugger.
509 inf_ptrace_insert_fork_catchpoint (int pid
)
511 /* This version of Unix doesn't support notification of fork events. */
516 inf_ptrace_remove_fork_catchpoint (int pid
)
518 /* This version of Unix doesn't support notification of fork events. */
523 inf_ptrace_insert_vfork_catchpoint (int pid
)
525 /* This version of Unix doesn't support notification of vfork events. */
530 inf_ptrace_remove_vfork_catchpoint (int pid
)
532 /* This version of Unix doesn't support notification of vfork events. */
537 inf_ptrace_follow_fork (int follow_child
)
539 /* This version of Unix doesn't support following fork or vfork events. */
544 inf_ptrace_insert_exec_catchpoint (int pid
)
546 /* This version of Unix doesn't support notification of exec events. */
551 inf_ptrace_remove_exec_catchpoint (int pid
)
553 /* This version of Unix doesn't support notification of exec events. */
558 inf_ptrace_reported_exec_events_per_exec_call (void)
560 /* This version of Unix doesn't support notification of exec events.
566 inf_ptrace_has_exited (int pid
, int wait_status
, int *exit_status
)
568 if (WIFEXITED (wait_status
))
570 *exit_status
= WEXITSTATUS (wait_status
);
574 if (WIFSIGNALED (wait_status
))
576 *exit_status
= 0; /* ?? Don't know what else to say here. */
580 /* ?? Do we really need to consult the event state, too? Assume the
581 wait_state alone suffices.
587 inf_ptrace_mourn_inferior (void)
589 unpush_target (ptrace_ops_hack
);
590 generic_mourn_inferior ();
594 inf_ptrace_can_run (void)
599 /* Send a SIGINT to the process group. This acts just like the user
600 typed a ^C on the controlling terminal.
602 XXX - This may not be correct for all systems. Some may want to
603 use killpg() instead of kill (-pgrp). */
606 inf_ptrace_stop (void)
608 kill (-inferior_process_group
, SIGINT
);
611 /* Perform a partial transfer to/from the specified object. For
612 memory transfers, fall back to the old memory xfer functions. */
615 inf_ptrace_xfer_partial (struct target_ops
*ops
, enum target_object object
,
616 const char *annex
, void *readbuf
,
617 const void *writebuf
, ULONGEST offset
, LONGEST len
)
621 case TARGET_OBJECT_MEMORY
:
623 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
624 request that promises to be much more efficient in reading
625 and writing data in the traced process's address space. */
627 struct ptrace_io_desc piod
;
629 /* NOTE: We assume that there are no distinct address spaces
630 for instruction and data. */
631 piod
.piod_op
= writebuf
? PIOD_WRITE_D
: PIOD_READ_D
;
632 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
633 piod
.piod_offs
= (void *) (long) offset
;
637 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == 0)
638 /* Return the actual number of bytes read or written. */
639 return piod
.piod_len
;
640 /* If the PT_IO request is somehow not supported, fallback on
641 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
642 to indicate failure. */
650 PTRACE_TYPE_RET word
;
651 unsigned char byte
[sizeof (PTRACE_TYPE_RET
)];
653 ULONGEST rounded_offset
;
656 /* Round the start address down to the next long word boundary. */
657 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
659 /* Truncate the length so that at max a single word will be
660 transfered. Remember, this function is required to perform
661 only a partial read so no more than one word should be
663 if (rounded_offset
+ sizeof (PTRACE_TYPE_RET
) < offset
+ len
)
664 partial_len
= sizeof (PTRACE_TYPE_RET
) - (offset
- rounded_offset
);
670 /* Fill start and end extra bytes of buffer with existing
672 if (rounded_offset
< offset
673 || (partial_len
+ (offset
- rounded_offset
)
674 < sizeof (PTRACE_TYPE_RET
)))
675 /* Need part of initial word -- fetch it. */
676 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
677 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
680 /* Copy data to be written over corresponding part of
682 memcpy (buffer
.byte
+ (offset
- rounded_offset
), writebuf
, partial_len
);
685 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
686 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
690 /* Using the appropriate one (I or D) is necessary for
691 Gould NP1, at least. */
693 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
694 (PTRACE_TYPE_ARG3
) (long) rounded_offset
,
703 buffer
.word
= ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
704 (PTRACE_TYPE_ARG3
) (long) rounded_offset
, 0);
707 /* Copy appropriate bytes out of the buffer. */
708 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
714 case TARGET_OBJECT_UNWIND_TABLE
:
717 case TARGET_OBJECT_AUXV
:
720 case TARGET_OBJECT_WCOOKIE
:
729 inf_ptrace_pid_to_str (ptid_t ptid
)
731 return normal_pid_to_str (ptid
);
735 inf_ptrace_target (void)
737 struct target_ops
*t
= inf_child_target ();
738 t
->to_open
= inf_ptrace_open
;
739 t
->to_attach
= inf_ptrace_attach
;
740 t
->to_post_attach
= inf_ptrace_post_attach
;
741 t
->to_detach
= inf_ptrace_detach
;
742 t
->to_resume
= inf_ptrace_resume
;
743 t
->to_wait
= inf_ptrace_wait
;
744 t
->to_prepare_to_store
= inf_ptrace_prepare_to_store
;
745 t
->to_xfer_memory
= inf_ptrace_xfer_memory
;
746 t
->to_xfer_partial
= inf_ptrace_xfer_partial
;
747 t
->to_files_info
= inf_ptrace_files_info
;
748 t
->to_kill
= inf_ptrace_kill_inferior
;
749 t
->to_create_inferior
= inf_ptrace_create_inferior
;
750 t
->to_post_startup_inferior
= inf_ptrace_post_startup_inferior
;
751 t
->to_acknowledge_created_inferior
=
752 inf_ptrace_acknowledge_created_inferior
;
753 t
->to_insert_fork_catchpoint
= inf_ptrace_insert_fork_catchpoint
;
754 t
->to_remove_fork_catchpoint
= inf_ptrace_remove_fork_catchpoint
;
755 t
->to_insert_vfork_catchpoint
= inf_ptrace_insert_vfork_catchpoint
;
756 t
->to_remove_vfork_catchpoint
= inf_ptrace_remove_vfork_catchpoint
;
757 t
->to_follow_fork
= inf_ptrace_follow_fork
;
758 t
->to_insert_exec_catchpoint
= inf_ptrace_insert_exec_catchpoint
;
759 t
->to_remove_exec_catchpoint
= inf_ptrace_remove_exec_catchpoint
;
760 t
->to_reported_exec_events_per_exec_call
=
761 inf_ptrace_reported_exec_events_per_exec_call
;
762 t
->to_has_exited
= inf_ptrace_has_exited
;
763 t
->to_mourn_inferior
= inf_ptrace_mourn_inferior
;
764 t
->to_can_run
= inf_ptrace_can_run
;
765 t
->to_thread_alive
= inf_ptrace_thread_alive
;
766 t
->to_pid_to_str
= inf_ptrace_pid_to_str
;
767 t
->to_stop
= inf_ptrace_stop
;
768 t
->to_stratum
= process_stratum
;
769 t
->to_has_all_memory
= 1;
770 t
->to_has_memory
= 1;
772 t
->to_has_registers
= 1;
773 t
->to_has_execution
= 1;
774 t
->to_magic
= OPS_MAGIC
;