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 "common/gdb_wait.h"
31 #include "inf-ptrace.h"
32 #include "inf-child.h"
33 #include "gdbthread.h"
34 #include "nat/fork-inferior.h"
39 /* A unique_ptr helper to unpush a target. */
41 struct target_unpusher
43 void operator() (struct target_ops *ops) const
49 /* A unique_ptr that unpushes a target on destruction. */
51 typedef std::unique_ptr<struct target_ops, target_unpusher> target_unpush_up;
55 inf_ptrace_target::~inf_ptrace_target ()
58 #ifdef PT_GET_PROCESS_STATE
60 /* Target hook for follow_fork. On entry and at return inferior_ptid is
61 the ptid of the followed inferior. */
64 inf_ptrace_target::follow_fork (int follow_child, int detach_fork)
68 struct thread_info *tp = inferior_thread ();
69 pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
71 /* Breakpoints have already been detached from the child by
74 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
75 perror_with_name (("ptrace"));
82 inf_ptrace_target::insert_fork_catchpoint (int pid)
88 inf_ptrace_target::remove_fork_catchpoint (int pid)
93 #endif /* PT_GET_PROCESS_STATE */
96 /* Prepare to be traced. */
101 /* "Trace me, Dr. Memory!" */
102 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
103 trace_start_error_with_name ("ptrace");
106 /* Start a new inferior Unix child process. EXEC_FILE is the file to
107 run, ALLARGS is a string containing the arguments to the program.
108 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
112 inf_ptrace_target::create_inferior (const char *exec_file,
113 const std::string &allargs,
114 char **env, int from_tty)
119 /* Do not change either targets above or the same target if already present.
120 The reason is the target stack is shared across multiple inferiors. */
121 int ops_already_pushed = target_is_pushed (this);
123 target_unpush_up unpusher;
124 if (! ops_already_pushed)
126 /* Clear possible core file with its process_stratum. */
128 unpusher.reset (this);
131 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
135 /* We have something that executes now. We'll be running through
136 the shell at this point (if startup-with-shell is true), but the
137 pid shouldn't change. */
138 add_thread_silent (ptid);
142 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
144 /* On some targets, there must be some explicit actions taken after
145 the inferior has been started up. */
146 target_post_startup_inferior (ptid);
149 #ifdef PT_GET_PROCESS_STATE
152 inf_ptrace_target::post_startup_inferior (ptid_t pid)
156 /* Set the initial event mask. */
157 memset (&pe, 0, sizeof pe);
158 pe.pe_set_event |= PTRACE_FORK;
159 if (ptrace (PT_SET_EVENT_MASK, pid.pid (),
160 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
161 perror_with_name (("ptrace"));
166 /* Clean up a rotting corpse of an inferior after it died. */
169 inf_ptrace_target::mourn_inferior ()
173 /* Wait just one more time to collect the inferior's exit status.
174 Do not check whether this succeeds though, since we may be
175 dealing with a process that we attached to. Such a process will
176 only report its exit status to its original parent. */
177 waitpid (inferior_ptid.pid (), &status, 0);
179 inf_child_target::mourn_inferior ();
182 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
183 be chatty about it. */
186 inf_ptrace_target::attach (const char *args, int from_tty)
190 struct inferior *inf;
192 /* Do not change either targets above or the same target if already present.
193 The reason is the target stack is shared across multiple inferiors. */
194 int ops_already_pushed = target_is_pushed (this);
196 pid = parse_pid_to_attach (args);
198 if (pid == getpid ()) /* Trying to masturbate? */
199 error (_("I refuse to debug myself!"));
201 target_unpush_up unpusher;
202 if (! ops_already_pushed)
204 /* target_pid_to_str already uses the target. Also clear possible core
205 file with its process_stratum. */
207 unpusher.reset (this);
212 exec_file = get_exec_file (0);
215 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
216 target_pid_to_str (ptid_t (pid)).c_str ());
218 printf_unfiltered (_("Attaching to %s\n"),
219 target_pid_to_str (ptid_t (pid)).c_str ());
224 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
226 perror_with_name (("ptrace"));
228 error (_("This system does not support attaching to a process"));
231 inf = current_inferior ();
232 inferior_appeared (inf, pid);
233 inf->attach_flag = 1;
234 inferior_ptid = ptid_t (pid);
236 /* Always add a main thread. If some target extends the ptrace
237 target, it should decorate the ptid later with more info. */
238 thread_info *thr = add_thread_silent (inferior_ptid);
239 /* Don't consider the thread stopped until we've processed its
240 initial SIGSTOP stop. */
241 set_executing (thr->ptid, true);
246 #ifdef PT_GET_PROCESS_STATE
249 inf_ptrace_target::post_attach (int pid)
253 /* Set the initial event mask. */
254 memset (&pe, 0, sizeof pe);
255 pe.pe_set_event |= PTRACE_FORK;
256 if (ptrace (PT_SET_EVENT_MASK, pid,
257 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
258 perror_with_name (("ptrace"));
263 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
266 inf_ptrace_target::detach (inferior *inf, int from_tty)
268 pid_t pid = inferior_ptid.pid ();
270 target_announce_detach (from_tty);
273 /* We'd better not have left any breakpoints in the program or it'll
274 die when it hits one. Also note that this may only work if we
275 previously attached to the inferior. It *might* work if we
276 started the process ourselves. */
278 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
280 perror_with_name (("ptrace"));
282 error (_("This system does not support detaching from a process"));
285 detach_success (inf);
288 /* See inf-ptrace.h. */
291 inf_ptrace_target::detach_success (inferior *inf)
293 inferior_ptid = null_ptid;
294 detach_inferior (inf);
296 maybe_unpush_target ();
299 /* Kill the inferior. */
302 inf_ptrace_target::kill ()
304 pid_t pid = inferior_ptid.pid ();
310 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
311 waitpid (pid, &status, 0);
313 target_mourn_inferior (inferior_ptid);
316 /* Return which PID to pass to ptrace in order to observe/control the
317 tracee identified by PTID. */
320 get_ptrace_pid (ptid_t ptid)
324 /* If we have an LWPID to work with, use it. Otherwise, we're
325 dealing with a non-threaded program/target. */
332 /* Resume execution of thread PTID, or all threads if PTID is -1. If
333 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
337 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
342 if (minus_one_ptid == ptid)
343 /* Resume all threads. Traditionally ptrace() only supports
344 single-threaded processes, so simply resume the inferior. */
345 pid = inferior_ptid.pid ();
347 pid = get_ptrace_pid (ptid);
349 if (catch_syscall_enabled () > 0)
350 request = PT_SYSCALL;
352 request = PT_CONTINUE;
356 /* If this system does not support PT_STEP, a higher level
357 function will have called single_step() to transmute the step
358 request into a continue request (by setting breakpoints on
359 all possible successor instructions), so we don't have to
360 worry about that here. */
364 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
365 where it was. If GDB wanted it to start some other way, we have
366 already written a new program counter value to the child. */
368 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
370 perror_with_name (("ptrace"));
373 /* Wait for the child specified by PTID to do something. Return the
374 process ID of the child, or MINUS_ONE_PTID in case of error; store
375 the status in *OURSTATUS. */
378 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
382 int status, save_errno;
390 pid = waitpid (ptid.pid (), &status, 0);
393 while (pid == -1 && errno == EINTR);
395 clear_sigint_trap ();
399 fprintf_unfiltered (gdb_stderr,
400 _("Child process unexpectedly missing: %s.\n"),
401 safe_strerror (save_errno));
403 /* Claim it exited with unknown signal. */
404 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
405 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
406 return inferior_ptid;
409 /* Ignore terminated detached child processes. */
410 if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
415 #ifdef PT_GET_PROCESS_STATE
416 if (WIFSTOPPED (status))
421 if (ptrace (PT_GET_PROCESS_STATE, pid,
422 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
423 perror_with_name (("ptrace"));
425 switch (pe.pe_report_event)
428 ourstatus->kind = TARGET_WAITKIND_FORKED;
429 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
431 /* Make sure the other end of the fork is stopped too. */
432 fpid = waitpid (pe.pe_other_pid, &status, 0);
434 perror_with_name (("waitpid"));
436 if (ptrace (PT_GET_PROCESS_STATE, fpid,
437 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
438 perror_with_name (("ptrace"));
440 gdb_assert (pe.pe_report_event == PTRACE_FORK);
441 gdb_assert (pe.pe_other_pid == pid);
442 if (fpid == inferior_ptid.pid ())
444 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
445 return ptid_t (fpid);
453 store_waitstatus (ourstatus, status);
457 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
458 from process PID's memory into READBUF. Start at target address ADDR
459 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
460 be non-null. Return the number of transferred bytes. */
463 inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf,
464 const gdb_byte *writebuf,
465 ULONGEST addr, ULONGEST len)
470 /* We transfer aligned words. Thus align ADDR down to a word
471 boundary and determine how many bytes to skip at the
473 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
478 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
480 /* Restrict to a chunk that fits in the current word. */
481 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
483 /* Use a union for type punning. */
486 PTRACE_TYPE_RET word;
487 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
490 /* Read the word, also when doing a partial word write. */
491 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
494 buf.word = ptrace (PT_READ_I, pid,
495 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
499 memcpy (readbuf + n, buf.byte + skip, chunk);
501 if (writebuf != NULL)
503 memcpy (buf.byte + skip, writebuf + n, chunk);
505 ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
509 /* Using the appropriate one (I or D) is necessary for
510 Gould NP1, at least. */
512 ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
523 /* Implement the to_xfer_partial target_ops method. */
525 enum target_xfer_status
526 inf_ptrace_target::xfer_partial (enum target_object object,
527 const char *annex, gdb_byte *readbuf,
528 const gdb_byte *writebuf,
529 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
531 pid_t pid = get_ptrace_pid (inferior_ptid);
535 case TARGET_OBJECT_MEMORY:
537 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
538 request that promises to be much more efficient in reading
539 and writing data in the traced process's address space. */
541 struct ptrace_io_desc piod;
543 /* NOTE: We assume that there are no distinct address spaces
544 for instruction and data. However, on OpenBSD 3.9 and
545 later, PIOD_WRITE_D doesn't allow changing memory that's
546 mapped read-only. Since most code segments will be
547 read-only, using PIOD_WRITE_D will prevent us from
548 inserting breakpoints, so we use PIOD_WRITE_I instead. */
549 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
550 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
551 piod.piod_offs = (void *) (long) offset;
555 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
557 /* Return the actual number of bytes read or written. */
558 *xfered_len = piod.piod_len;
559 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
561 /* If the PT_IO request is somehow not supported, fallback on
562 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
563 to indicate failure. */
565 return TARGET_XFER_EOF;
568 *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf,
570 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
572 case TARGET_OBJECT_UNWIND_TABLE:
573 return TARGET_XFER_E_IO;
575 case TARGET_OBJECT_AUXV:
576 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
577 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
578 request that allows us to read the auxilliary vector. Other
579 BSD's may follow if they feel the need to support PIE. */
581 struct ptrace_io_desc piod;
584 return TARGET_XFER_E_IO;
585 piod.piod_op = PIOD_READ_AUXV;
586 piod.piod_addr = readbuf;
587 piod.piod_offs = (void *) (long) offset;
591 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
593 /* Return the actual number of bytes read or written. */
594 *xfered_len = piod.piod_len;
595 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
599 return TARGET_XFER_E_IO;
601 case TARGET_OBJECT_WCOOKIE:
602 return TARGET_XFER_E_IO;
605 return TARGET_XFER_E_IO;
609 /* Return non-zero if the thread specified by PTID is alive. */
612 inf_ptrace_target::thread_alive (ptid_t ptid)
614 /* ??? Is kill the right way to do this? */
615 return (::kill (ptid.pid (), 0) != -1);
618 /* Print status information about what we're accessing. */
621 inf_ptrace_target::files_info ()
623 struct inferior *inf = current_inferior ();
625 printf_filtered (_("\tUsing the running image of %s %s.\n"),
626 inf->attach_flag ? "attached" : "child",
627 target_pid_to_str (inferior_ptid).c_str ());
631 inf_ptrace_target::pid_to_str (ptid_t ptid)
633 return normal_pid_to_str (ptid);
636 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
638 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
639 Return 0 if *READPTR is already at the end of the buffer.
640 Return -1 if there is insufficient buffer for a whole entry.
641 Return 1 if an entry was read into *TYPEP and *VALP. */
644 inf_ptrace_target::auxv_parse (gdb_byte **readptr, gdb_byte *endptr,
645 CORE_ADDR *typep, CORE_ADDR *valp)
647 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
648 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
649 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
650 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
651 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
652 gdb_byte *ptr = *readptr;
657 if (endptr - ptr < 2 * sizeof_auxv_val)
660 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
661 ptr += sizeof_auxv_val; /* Alignment. */
662 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
663 ptr += sizeof_auxv_val;