1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "nat/gdb_ptrace.h"
28 #include "gdbsupport/gdb_wait.h"
31 #include "inf-ptrace.h"
32 #include "inf-child.h"
33 #include "gdbthread.h"
34 #include "nat/fork-inferior.h"
40 /* A unique_ptr helper to unpush a target. */
42 struct target_unpusher
44 void operator() (struct target_ops
*ops
) const
50 /* A unique_ptr that unpushes a target on destruction. */
52 typedef std::unique_ptr
<struct target_ops
, target_unpusher
> target_unpush_up
;
56 inf_ptrace_target::~inf_ptrace_target ()
59 #ifdef PT_GET_PROCESS_STATE
61 /* Target hook for follow_fork. On entry and at return inferior_ptid is
62 the ptid of the followed inferior. */
65 inf_ptrace_target::follow_fork (int follow_child
, int detach_fork
)
69 struct thread_info
*tp
= inferior_thread ();
70 pid_t child_pid
= tp
->pending_follow
.value
.related_pid
.pid ();
72 /* Breakpoints have already been detached from the child by
75 if (ptrace (PT_DETACH
, child_pid
, (PTRACE_TYPE_ARG3
)1, 0) == -1)
76 perror_with_name (("ptrace"));
83 inf_ptrace_target::insert_fork_catchpoint (int pid
)
89 inf_ptrace_target::remove_fork_catchpoint (int pid
)
94 #endif /* PT_GET_PROCESS_STATE */
97 /* Default method for "inf_ptrace_me_fail_reason", which returns an
101 default_inf_ptrace_me_fail_reason (int err
)
106 /* See inf-ptrace.h. */
108 std::string (*inf_ptrace_me_fail_reason
) (int err
)
109 = default_inf_ptrace_me_fail_reason
;
111 /* Prepare to be traced. */
116 /* "Trace me, Dr. Memory!" */
117 if (ptrace (PT_TRACE_ME
, 0, (PTRACE_TYPE_ARG3
) 0, 0) < 0)
118 trace_start_error_with_name ("ptrace",
119 inf_ptrace_me_fail_reason (errno
).c_str ());
122 /* Start a new inferior Unix child process. EXEC_FILE is the file to
123 run, ALLARGS is a string containing the arguments to the program.
124 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
128 inf_ptrace_target::create_inferior (const char *exec_file
,
129 const std::string
&allargs
,
130 char **env
, int from_tty
)
135 /* Do not change either targets above or the same target if already present.
136 The reason is the target stack is shared across multiple inferiors. */
137 int ops_already_pushed
= target_is_pushed (this);
139 target_unpush_up unpusher
;
140 if (! ops_already_pushed
)
142 /* Clear possible core file with its process_stratum. */
144 unpusher
.reset (this);
147 pid
= fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, NULL
,
151 /* We have something that executes now. We'll be running through
152 the shell at this point (if startup-with-shell is true), but the
153 pid shouldn't change. */
154 add_thread_silent (ptid
);
158 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
160 /* On some targets, there must be some explicit actions taken after
161 the inferior has been started up. */
162 target_post_startup_inferior (ptid
);
165 #ifdef PT_GET_PROCESS_STATE
168 inf_ptrace_target::post_startup_inferior (ptid_t pid
)
172 /* Set the initial event mask. */
173 memset (&pe
, 0, sizeof pe
);
174 pe
.pe_set_event
|= PTRACE_FORK
;
175 if (ptrace (PT_SET_EVENT_MASK
, pid
.pid (),
176 (PTRACE_TYPE_ARG3
)&pe
, sizeof pe
) == -1)
177 perror_with_name (("ptrace"));
182 /* Clean up a rotting corpse of an inferior after it died. */
185 inf_ptrace_target::mourn_inferior ()
189 /* Wait just one more time to collect the inferior's exit status.
190 Do not check whether this succeeds though, since we may be
191 dealing with a process that we attached to. Such a process will
192 only report its exit status to its original parent. */
193 waitpid (inferior_ptid
.pid (), &status
, 0);
195 inf_child_target::mourn_inferior ();
198 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
199 be chatty about it. */
202 inf_ptrace_target::attach (const char *args
, int from_tty
)
206 struct inferior
*inf
;
208 /* Do not change either targets above or the same target if already present.
209 The reason is the target stack is shared across multiple inferiors. */
210 int ops_already_pushed
= target_is_pushed (this);
212 pid
= parse_pid_to_attach (args
);
214 if (pid
== getpid ()) /* Trying to masturbate? */
215 error (_("I refuse to debug myself!"));
217 target_unpush_up unpusher
;
218 if (! ops_already_pushed
)
220 /* target_pid_to_str already uses the target. Also clear possible core
221 file with its process_stratum. */
223 unpusher
.reset (this);
228 exec_file
= get_exec_file (0);
231 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
232 target_pid_to_str (ptid_t (pid
)).c_str ());
234 printf_unfiltered (_("Attaching to %s\n"),
235 target_pid_to_str (ptid_t (pid
)).c_str ());
240 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
242 perror_with_name (("ptrace"));
244 error (_("This system does not support attaching to a process"));
247 inf
= current_inferior ();
248 inferior_appeared (inf
, pid
);
249 inf
->attach_flag
= 1;
250 inferior_ptid
= ptid_t (pid
);
252 /* Always add a main thread. If some target extends the ptrace
253 target, it should decorate the ptid later with more info. */
254 thread_info
*thr
= add_thread_silent (inferior_ptid
);
255 /* Don't consider the thread stopped until we've processed its
256 initial SIGSTOP stop. */
257 set_executing (thr
->ptid
, true);
262 #ifdef PT_GET_PROCESS_STATE
265 inf_ptrace_target::post_attach (int pid
)
269 /* Set the initial event mask. */
270 memset (&pe
, 0, sizeof pe
);
271 pe
.pe_set_event
|= PTRACE_FORK
;
272 if (ptrace (PT_SET_EVENT_MASK
, pid
,
273 (PTRACE_TYPE_ARG3
)&pe
, sizeof pe
) == -1)
274 perror_with_name (("ptrace"));
279 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
282 inf_ptrace_target::detach (inferior
*inf
, int from_tty
)
284 pid_t pid
= inferior_ptid
.pid ();
286 target_announce_detach (from_tty
);
289 /* We'd better not have left any breakpoints in the program or it'll
290 die when it hits one. Also note that this may only work if we
291 previously attached to the inferior. It *might* work if we
292 started the process ourselves. */
294 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
)1, 0);
296 perror_with_name (("ptrace"));
298 error (_("This system does not support detaching from a process"));
301 detach_success (inf
);
304 /* See inf-ptrace.h. */
307 inf_ptrace_target::detach_success (inferior
*inf
)
309 inferior_ptid
= null_ptid
;
310 detach_inferior (inf
);
312 maybe_unpush_target ();
315 /* Kill the inferior. */
318 inf_ptrace_target::kill ()
320 pid_t pid
= inferior_ptid
.pid ();
326 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
327 waitpid (pid
, &status
, 0);
329 target_mourn_inferior (inferior_ptid
);
332 /* Return which PID to pass to ptrace in order to observe/control the
333 tracee identified by PTID. */
336 get_ptrace_pid (ptid_t ptid
)
340 /* If we have an LWPID to work with, use it. Otherwise, we're
341 dealing with a non-threaded program/target. */
348 /* Resume execution of thread PTID, or all threads if PTID is -1. If
349 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
353 inf_ptrace_target::resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
358 if (minus_one_ptid
== ptid
)
359 /* Resume all threads. Traditionally ptrace() only supports
360 single-threaded processes, so simply resume the inferior. */
361 pid
= inferior_ptid
.pid ();
363 pid
= get_ptrace_pid (ptid
);
365 if (catch_syscall_enabled () > 0)
366 request
= PT_SYSCALL
;
368 request
= PT_CONTINUE
;
372 /* If this system does not support PT_STEP, a higher level
373 function will have called single_step() to transmute the step
374 request into a continue request (by setting breakpoints on
375 all possible successor instructions), so we don't have to
376 worry about that here. */
380 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
381 where it was. If GDB wanted it to start some other way, we have
382 already written a new program counter value to the child. */
384 ptrace (request
, pid
, (PTRACE_TYPE_ARG3
)1, gdb_signal_to_host (signal
));
386 perror_with_name (("ptrace"));
389 /* Wait for the child specified by PTID to do something. Return the
390 process ID of the child, or MINUS_ONE_PTID in case of error; store
391 the status in *OURSTATUS. */
394 inf_ptrace_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
398 int status
, save_errno
;
406 pid
= waitpid (ptid
.pid (), &status
, 0);
409 while (pid
== -1 && errno
== EINTR
);
411 clear_sigint_trap ();
415 fprintf_unfiltered (gdb_stderr
,
416 _("Child process unexpectedly missing: %s.\n"),
417 safe_strerror (save_errno
));
419 /* Claim it exited with unknown signal. */
420 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
421 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
422 return inferior_ptid
;
425 /* Ignore terminated detached child processes. */
426 if (!WIFSTOPPED (status
) && pid
!= inferior_ptid
.pid ())
431 #ifdef PT_GET_PROCESS_STATE
432 if (WIFSTOPPED (status
))
437 if (ptrace (PT_GET_PROCESS_STATE
, pid
,
438 (PTRACE_TYPE_ARG3
)&pe
, sizeof pe
) == -1)
439 perror_with_name (("ptrace"));
441 switch (pe
.pe_report_event
)
444 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
445 ourstatus
->value
.related_pid
= ptid_t (pe
.pe_other_pid
);
447 /* Make sure the other end of the fork is stopped too. */
448 fpid
= waitpid (pe
.pe_other_pid
, &status
, 0);
450 perror_with_name (("waitpid"));
452 if (ptrace (PT_GET_PROCESS_STATE
, fpid
,
453 (PTRACE_TYPE_ARG3
)&pe
, sizeof pe
) == -1)
454 perror_with_name (("ptrace"));
456 gdb_assert (pe
.pe_report_event
== PTRACE_FORK
);
457 gdb_assert (pe
.pe_other_pid
== pid
);
458 if (fpid
== inferior_ptid
.pid ())
460 ourstatus
->value
.related_pid
= ptid_t (pe
.pe_other_pid
);
461 return ptid_t (fpid
);
469 store_waitstatus (ourstatus
, status
);
473 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
474 from process PID's memory into READBUF. Start at target address ADDR
475 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
476 be non-null. Return the number of transferred bytes. */
479 inf_ptrace_peek_poke (pid_t pid
, gdb_byte
*readbuf
,
480 const gdb_byte
*writebuf
,
481 ULONGEST addr
, ULONGEST len
)
486 /* We transfer aligned words. Thus align ADDR down to a word
487 boundary and determine how many bytes to skip at the
489 ULONGEST skip
= addr
& (sizeof (PTRACE_TYPE_RET
) - 1);
494 n
+= chunk
, addr
+= sizeof (PTRACE_TYPE_RET
), skip
= 0)
496 /* Restrict to a chunk that fits in the current word. */
497 chunk
= std::min (sizeof (PTRACE_TYPE_RET
) - skip
, len
- n
);
499 /* Use a union for type punning. */
502 PTRACE_TYPE_RET word
;
503 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
506 /* Read the word, also when doing a partial word write. */
507 if (readbuf
!= NULL
|| chunk
< sizeof (PTRACE_TYPE_RET
))
510 buf
.word
= ptrace (PT_READ_I
, pid
,
511 (PTRACE_TYPE_ARG3
)(uintptr_t) addr
, 0);
515 memcpy (readbuf
+ n
, buf
.byte
+ skip
, chunk
);
517 if (writebuf
!= NULL
)
519 memcpy (buf
.byte
+ skip
, writebuf
+ n
, chunk
);
521 ptrace (PT_WRITE_D
, pid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
525 /* Using the appropriate one (I or D) is necessary for
526 Gould NP1, at least. */
528 ptrace (PT_WRITE_I
, pid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
539 /* Implement the to_xfer_partial target_ops method. */
541 enum target_xfer_status
542 inf_ptrace_target::xfer_partial (enum target_object object
,
543 const char *annex
, gdb_byte
*readbuf
,
544 const gdb_byte
*writebuf
,
545 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
547 pid_t pid
= get_ptrace_pid (inferior_ptid
);
551 case TARGET_OBJECT_MEMORY
:
553 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
554 request that promises to be much more efficient in reading
555 and writing data in the traced process's address space. */
557 struct ptrace_io_desc piod
;
559 /* NOTE: We assume that there are no distinct address spaces
560 for instruction and data. However, on OpenBSD 3.9 and
561 later, PIOD_WRITE_D doesn't allow changing memory that's
562 mapped read-only. Since most code segments will be
563 read-only, using PIOD_WRITE_D will prevent us from
564 inserting breakpoints, so we use PIOD_WRITE_I instead. */
565 piod
.piod_op
= writebuf
? PIOD_WRITE_I
: PIOD_READ_D
;
566 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
567 piod
.piod_offs
= (void *) (long) offset
;
571 if (ptrace (PT_IO
, pid
, (caddr_t
)&piod
, 0) == 0)
573 /* Return the actual number of bytes read or written. */
574 *xfered_len
= piod
.piod_len
;
575 return (piod
.piod_len
== 0) ? TARGET_XFER_EOF
: TARGET_XFER_OK
;
577 /* If the PT_IO request is somehow not supported, fallback on
578 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
579 to indicate failure. */
581 return TARGET_XFER_EOF
;
584 *xfered_len
= inf_ptrace_peek_poke (pid
, readbuf
, writebuf
,
586 return *xfered_len
!= 0 ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
588 case TARGET_OBJECT_UNWIND_TABLE
:
589 return TARGET_XFER_E_IO
;
591 case TARGET_OBJECT_AUXV
:
592 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
593 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
594 request that allows us to read the auxilliary vector. Other
595 BSD's may follow if they feel the need to support PIE. */
597 struct ptrace_io_desc piod
;
600 return TARGET_XFER_E_IO
;
601 piod
.piod_op
= PIOD_READ_AUXV
;
602 piod
.piod_addr
= readbuf
;
603 piod
.piod_offs
= (void *) (long) offset
;
607 if (ptrace (PT_IO
, pid
, (caddr_t
)&piod
, 0) == 0)
609 /* Return the actual number of bytes read or written. */
610 *xfered_len
= piod
.piod_len
;
611 return (piod
.piod_len
== 0) ? TARGET_XFER_EOF
: TARGET_XFER_OK
;
615 return TARGET_XFER_E_IO
;
617 case TARGET_OBJECT_WCOOKIE
:
618 return TARGET_XFER_E_IO
;
621 return TARGET_XFER_E_IO
;
625 /* Return non-zero if the thread specified by PTID is alive. */
628 inf_ptrace_target::thread_alive (ptid_t ptid
)
630 /* ??? Is kill the right way to do this? */
631 return (::kill (ptid
.pid (), 0) != -1);
634 /* Print status information about what we're accessing. */
637 inf_ptrace_target::files_info ()
639 struct inferior
*inf
= current_inferior ();
641 printf_filtered (_("\tUsing the running image of %s %s.\n"),
642 inf
->attach_flag
? "attached" : "child",
643 target_pid_to_str (inferior_ptid
).c_str ());
647 inf_ptrace_target::pid_to_str (ptid_t ptid
)
649 return normal_pid_to_str (ptid
);
652 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
654 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
655 Return 0 if *READPTR is already at the end of the buffer.
656 Return -1 if there is insufficient buffer for a whole entry.
657 Return 1 if an entry was read into *TYPEP and *VALP. */
660 inf_ptrace_target::auxv_parse (gdb_byte
**readptr
, gdb_byte
*endptr
,
661 CORE_ADDR
*typep
, CORE_ADDR
*valp
)
663 struct type
*int_type
= builtin_type (target_gdbarch ())->builtin_int
;
664 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
665 const int sizeof_auxv_type
= TYPE_LENGTH (int_type
);
666 const int sizeof_auxv_val
= TYPE_LENGTH (ptr_type
);
667 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
668 gdb_byte
*ptr
= *readptr
;
673 if (endptr
- ptr
< 2 * sizeof_auxv_val
)
676 *typep
= extract_unsigned_integer (ptr
, sizeof_auxv_type
, byte_order
);
677 ptr
+= sizeof_auxv_val
; /* Alignment. */
678 *valp
= extract_unsigned_integer (ptr
, sizeof_auxv_val
, byte_order
);
679 ptr
+= sizeof_auxv_val
;