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 /* Prepare to be traced. */
49 /* "Trace me, Dr. Memory!" */
50 ptrace (PT_TRACE_ME
, 0, (PTRACE_TYPE_ARG3
)0, 0);
53 /* Start tracing PID. */
56 inf_ptrace_him (int pid
)
58 push_target (ptrace_ops_hack
);
60 /* On some targets, there must be some explicit synchronization
61 between the parent and child processes after the debugger
62 forks, and before the child execs the debuggee program. This
63 call basically gives permission for the child to exec. */
65 target_acknowledge_created_inferior (pid
);
67 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
68 be 1 or 2 depending on whether we're starting without or with a
70 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
72 /* On some targets, there must be some explicit actions taken after
73 the inferior has been started up. */
74 target_post_startup_inferior (pid_to_ptid (pid
));
77 /* Start a new inferior Unix child process. EXEC_FILE is the file to
78 run, ALLARGS is a string containing the arguments to the program.
79 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
83 inf_ptrace_create_inferior (char *exec_file
, char *allargs
, char **env
,
86 fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, inf_ptrace_him
,
89 /* We are at the first instruction we care about. */
90 observer_notify_inferior_created (¤t_target
, from_tty
);
92 /* Pedal to the metal... */
93 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
96 /* Clean up a rotting corpse of an inferior after it died. */
99 inf_ptrace_mourn_inferior (void)
103 /* Wait just one more time to collect the inferior's exit status.
104 Don not check whether this succeeds though, since we may be
105 dealing with a process that we attached to. Such a process will
106 only report its exit status to its origional parent. */
107 waitpid (ptid_get_pid (inferior_ptid
), &status
, 0);
109 unpush_target (ptrace_ops_hack
);
110 generic_mourn_inferior ();
113 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
114 be chatty about it. */
117 inf_ptrace_attach (char *args
, int from_tty
)
124 error_no_arg (_("process-id to attach"));
127 pid
= strtol (args
, &dummy
, 0);
128 /* Some targets don't set errno on errors, grrr! */
129 if (pid
== 0 && args
== dummy
)
130 error (_("Illegal process-id: %s."), args
);
132 if (pid
== getpid ()) /* Trying to masturbate? */
133 error (_("I refuse to debug myself!"));
137 exec_file
= get_exec_file (0);
140 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
141 target_pid_to_str (pid_to_ptid (pid
)));
143 printf_unfiltered (_("Attaching to %s\n"),
144 target_pid_to_str (pid_to_ptid (pid
)));
146 gdb_flush (gdb_stdout
);
151 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
153 perror_with_name (("ptrace"));
156 error (_("This system does not support attaching to a process"));
159 inferior_ptid
= pid_to_ptid (pid
);
160 push_target (ptrace_ops_hack
);
162 /* Do this first, before anything has had a chance to query the
163 inferior's symbol table or similar. */
164 observer_notify_inferior_created (¤t_target
, from_tty
);
167 /* Detach from the inferior, optionally passing it the signal
168 specified ARGS. If FROM_TTY is non-zero, be chatty about it. */
171 inf_ptrace_detach (char *args
, int from_tty
)
173 pid_t pid
= ptid_get_pid (inferior_ptid
);
178 char *exec_file
= get_exec_file (0);
181 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
182 target_pid_to_str (pid_to_ptid (pid
)));
183 gdb_flush (gdb_stdout
);
189 /* We'd better not have left any breakpoints in the program or it'll
190 die when it hits one. Alsno note that this may only work if we
191 previously attached to the inferior. It *might* work if we
192 started the process ourselves. */
194 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
)1, sig
);
196 perror_with_name (("ptrace"));
199 error (_("This system does not support detaching from a process"));
202 inferior_ptid
= null_ptid
;
203 unpush_target (ptrace_ops_hack
);
206 /* Kill the inferior. */
209 inf_ptrace_kill (void)
211 pid_t pid
= ptid_get_pid (inferior_ptid
);
217 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
218 waitpid (pid
, &status
, 0);
220 target_mourn_inferior ();
223 /* Stop the inferior. */
226 inf_ptrace_stop (void)
228 /* Send a SIGINT to the process group. This acts just like the user
229 typed a ^C on the controlling terminal. Note that using a
230 negative process number in kill() is a System V-ism. The proper
231 BSD interface is killpg(). However, all modern BSDs support the
232 System V interface too. */
233 kill (-inferior_process_group
, SIGINT
);
236 /* Resume execution of thread PTID, or all threads if PTID is -1. If
237 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
241 inf_ptrace_resume (ptid_t ptid
, int step
, enum target_signal signal
)
243 pid_t pid
= ptid_get_pid (ptid
);
244 int request
= PT_CONTINUE
;
247 /* Resume all threads. Traditionally ptrace() only supports
248 single-threaded processes, so simply resume the inferior. */
249 pid
= ptid_get_pid (inferior_ptid
);
253 /* If this system does not support PT_STEP, a higher level
254 function will have called single_step() to transmute the step
255 request into a continue request (by setting breakpoints on
256 all possible successor instructions), so we don't have to
257 worry about that here. */
261 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
262 where it was. If GDB wanted it to start some other way, we have
263 already written a new program counter value to the child. */
265 ptrace (request
, pid
, (PTRACE_TYPE_ARG3
)1, target_signal_to_host (signal
));
267 perror_with_name (("ptrace"));
270 /* Wait for the child specified by PTID to do something. Return the
271 process ID of the child, or MINUS_ONE_PTID in case of error; store
272 the status in *OURSTATUS. */
275 inf_ptrace_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
278 int status
, save_errno
;
287 pid
= waitpid (ptid_get_pid (ptid
), &status
, 0);
290 while (pid
== -1 && errno
== EINTR
);
293 clear_sigint_trap ();
297 fprintf_unfiltered (gdb_stderr
,
298 _("Child process unexpectedly missing: %s.\n"),
299 safe_strerror (save_errno
));
301 /* Claim it exited with unknown signal. */
302 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
303 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
304 return minus_one_ptid
;
307 /* Ignore terminated detached child processes. */
308 if (!WIFSTOPPED (status
) && pid
!= ptid_get_pid (inferior_ptid
))
313 store_waitstatus (ourstatus
, status
);
314 return pid_to_ptid (pid
);
317 /* Attempt a transfer all LEN bytes starting at OFFSET between the
318 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
319 Return the number of bytes actually transferred. */
322 inf_ptrace_xfer_partial (struct target_ops
*ops
, enum target_object object
,
323 const char *annex
, gdb_byte
*readbuf
,
324 const gdb_byte
*writebuf
,
325 ULONGEST offset
, LONGEST len
)
327 pid_t pid
= ptid_get_pid (inferior_ptid
);
331 case TARGET_OBJECT_MEMORY
:
333 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
334 request that promises to be much more efficient in reading
335 and writing data in the traced process's address space. */
337 struct ptrace_io_desc piod
;
339 /* NOTE: We assume that there are no distinct address spaces
340 for instruction and data. */
341 piod
.piod_op
= writebuf
? PIOD_WRITE_D
: PIOD_READ_D
;
342 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
343 piod
.piod_offs
= (void *) (long) offset
;
347 if (ptrace (PT_IO
, pid
, (caddr_t
)&piod
, 0) == 0)
348 /* Return the actual number of bytes read or written. */
349 return piod
.piod_len
;
350 /* If the PT_IO request is somehow not supported, fallback on
351 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
352 to indicate failure. */
360 PTRACE_TYPE_RET word
;
361 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
363 ULONGEST rounded_offset
;
366 /* Round the start offset down to the next long word
368 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
370 /* Since ptrace will transfer a single word starting at that
371 rounded_offset the partial_len needs to be adjusted down to
372 that (remember this function only does a single transfer).
373 Should the required length be even less, adjust it down
375 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
376 if (partial_len
> len
)
381 /* If OFFSET:PARTIAL_LEN is smaller than
382 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
383 be needed. Read in the entire word. */
384 if (rounded_offset
< offset
385 || (offset
+ partial_len
386 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
387 /* Need part of initial word -- fetch it. */
388 buffer
.word
= ptrace (PT_READ_I
, pid
,
389 (PTRACE_TYPE_ARG3
)(long)rounded_offset
, 0);
391 /* Copy data to be written over corresponding part of
393 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
394 writebuf
, partial_len
);
397 ptrace (PT_WRITE_D
, pid
,
398 (PTRACE_TYPE_ARG3
)(long)rounded_offset
, buffer
.word
);
401 /* Using the appropriate one (I or D) is necessary for
402 Gould NP1, at least. */
404 ptrace (PT_WRITE_I
, pid
,
405 (PTRACE_TYPE_ARG3
)(long)rounded_offset
, buffer
.word
);
414 buffer
.word
= ptrace (PT_READ_I
, pid
,
415 (PTRACE_TYPE_ARG3
)(long)rounded_offset
, 0);
418 /* Copy appropriate bytes out of the buffer. */
419 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
426 case TARGET_OBJECT_UNWIND_TABLE
:
429 case TARGET_OBJECT_AUXV
:
432 case TARGET_OBJECT_WCOOKIE
:
440 /* Return non-zero if the thread specified by PTID is alive. */
443 inf_ptrace_thread_alive (ptid_t ptid
)
445 /* ??? Is kill the right way to do this? */
446 return (kill (ptid_get_pid (ptid
), 0) != -1);
449 /* Print status information about what we're accessing. */
452 inf_ptrace_files_info (struct target_ops
*ignore
)
454 printf_filtered (_("\tUsing the running image of %s %s.\n"),
455 attach_flag
? "attached" : "child",
456 target_pid_to_str (inferior_ptid
));
459 /* Create a prototype ptrace target. The client can override it with
463 inf_ptrace_target (void)
465 struct target_ops
*t
= inf_child_target ();
467 t
->to_attach
= inf_ptrace_attach
;
468 t
->to_detach
= inf_ptrace_detach
;
469 t
->to_resume
= inf_ptrace_resume
;
470 t
->to_wait
= inf_ptrace_wait
;
471 t
->to_files_info
= inf_ptrace_files_info
;
472 t
->to_kill
= inf_ptrace_kill
;
473 t
->to_create_inferior
= inf_ptrace_create_inferior
;
474 t
->to_mourn_inferior
= inf_ptrace_mourn_inferior
;
475 t
->to_thread_alive
= inf_ptrace_thread_alive
;
476 t
->to_pid_to_str
= normal_pid_to_str
;
477 t
->to_stop
= inf_ptrace_stop
;
478 t
->to_xfer_partial
= inf_ptrace_xfer_partial
;
485 /* Pointer to a function that returns the offset within the user area
486 where a particular register is stored. */
487 static CORE_ADDR (*inf_ptrace_register_u_offset
)(int);
489 /* Fetch register REGNUM from the inferior. */
492 inf_ptrace_fetch_register (int regnum
)
496 PTRACE_TYPE_RET
*buf
;
499 /* Cater for systems like GNU/Linux, that implement threads as
500 seperate processes. */
501 pid
= ptid_get_lwp (inferior_ptid
);
503 pid
= ptid_get_pid (inferior_ptid
);
505 /* This isn't really an address, but ptrace thinks of it as one. */
506 addr
= inf_ptrace_register_u_offset (regnum
);
507 size
= register_size (current_gdbarch
, regnum
);
509 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
512 /* Read the register contents from the inferior a chuck at the time. */
513 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
516 buf
[i
] = ptrace (PT_READ_U
, pid
, (PTRACE_TYPE_ARG3
)addr
, 0);
518 error (_("Couldn't read register %s (#%d): %s."),
519 REGISTER_NAME (regnum
), regnum
, safe_strerror (errno
));
521 addr
+= sizeof (PTRACE_TYPE_RET
);
523 regcache_raw_supply (current_regcache
, regnum
, buf
);
526 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
527 for all registers. */
530 inf_ptrace_fetch_registers (int regnum
)
533 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
534 inf_ptrace_fetch_register (regnum
);
536 inf_ptrace_fetch_register (regnum
);
539 /* Store register REGNUM into the inferior. */
542 inf_ptrace_store_register (int regnum
)
546 PTRACE_TYPE_RET
*buf
;
549 /* Cater for systems like GNU/Linux, that implement threads as
550 seperate processes. */
551 pid
= ptid_get_lwp (inferior_ptid
);
553 pid
= ptid_get_pid (inferior_ptid
);
555 /* This isn't really an address, but ptrace thinks of it as one. */
556 addr
= inf_ptrace_register_u_offset (regnum
);
557 size
= register_size (current_gdbarch
, regnum
);
559 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
562 /* Write the register contents into the inferior a chunk at the time. */
563 regcache_raw_collect (current_regcache
, regnum
, buf
);
564 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
567 ptrace (PT_WRITE_U
, pid
, (PTRACE_TYPE_ARG3
)addr
, buf
[i
]);
569 error (_("Couldn't write register %s (#%d): %s."),
570 REGISTER_NAME (regnum
), regnum
, safe_strerror (errno
));
572 addr
+= sizeof (PTRACE_TYPE_RET
);
576 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
577 this for all registers. */
580 inf_ptrace_store_registers (int regnum
)
583 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
584 inf_ptrace_store_register (regnum
);
586 inf_ptrace_store_register (regnum
);
589 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
590 a function returning the offset within the user area where a
591 particular register is stored. */
594 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset
)(int))
596 struct target_ops
*t
= inf_ptrace_target();
598 gdb_assert (register_u_offset
);
599 inf_ptrace_register_u_offset
= register_u_offset
;
600 t
->to_fetch_registers
= inf_ptrace_fetch_registers
;
601 t
->to_store_registers
= inf_ptrace_store_registers
;