1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2018 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"
31 #include "inf-ptrace.h"
32 #include "inf-child.h"
33 #include "gdbthread.h"
34 #include "nat/fork-inferior.h"
39 #ifdef PT_GET_PROCESS_STATE
41 /* Target hook for follow_fork. On entry and at return inferior_ptid is
42 the ptid of the followed inferior. */
45 inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
50 struct thread_info *tp = inferior_thread ();
51 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
53 /* Breakpoints have already been detached from the child by
56 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
57 perror_with_name (("ptrace"));
64 inf_ptrace_insert_fork_catchpoint (struct target_ops *self, int pid)
70 inf_ptrace_remove_fork_catchpoint (struct target_ops *self, int pid)
75 #endif /* PT_GET_PROCESS_STATE */
78 /* Prepare to be traced. */
83 /* "Trace me, Dr. Memory!" */
84 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
85 trace_start_error_with_name ("ptrace");
88 /* Start a new inferior Unix child process. EXEC_FILE is the file to
89 run, ALLARGS is a string containing the arguments to the program.
90 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
94 inf_ptrace_create_inferior (struct target_ops *ops,
95 const char *exec_file, const std::string &allargs,
96 char **env, int from_tty)
101 /* Do not change either targets above or the same target if already present.
102 The reason is the target stack is shared across multiple inferiors. */
103 int ops_already_pushed = target_is_pushed (ops);
104 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
106 if (! ops_already_pushed)
108 /* Clear possible core file with its process_stratum. */
110 make_cleanup_unpush_target (ops);
113 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
116 ptid = pid_to_ptid (pid);
117 /* We have something that executes now. We'll be running through
118 the shell at this point (if startup-with-shell is true), but the
119 pid shouldn't change. */
120 add_thread_silent (ptid);
122 discard_cleanups (back_to);
124 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
126 /* On some targets, there must be some explicit actions taken after
127 the inferior has been started up. */
128 target_post_startup_inferior (ptid);
131 #ifdef PT_GET_PROCESS_STATE
134 inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
138 /* Set the initial event mask. */
139 memset (&pe, 0, sizeof pe);
140 pe.pe_set_event |= PTRACE_FORK;
141 if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
142 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
143 perror_with_name (("ptrace"));
148 /* Clean up a rotting corpse of an inferior after it died. */
151 inf_ptrace_mourn_inferior (struct target_ops *ops)
155 /* Wait just one more time to collect the inferior's exit status.
156 Do not check whether this succeeds though, since we may be
157 dealing with a process that we attached to. Such a process will
158 only report its exit status to its original parent. */
159 waitpid (ptid_get_pid (inferior_ptid), &status, 0);
161 inf_child_mourn_inferior (ops);
164 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
165 be chatty about it. */
168 inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
172 struct inferior *inf;
174 /* Do not change either targets above or the same target if already present.
175 The reason is the target stack is shared across multiple inferiors. */
176 int ops_already_pushed = target_is_pushed (ops);
177 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
179 pid = parse_pid_to_attach (args);
181 if (pid == getpid ()) /* Trying to masturbate? */
182 error (_("I refuse to debug myself!"));
184 if (! ops_already_pushed)
186 /* target_pid_to_str already uses the target. Also clear possible core
187 file with its process_stratum. */
189 make_cleanup_unpush_target (ops);
194 exec_file = get_exec_file (0);
197 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
198 target_pid_to_str (pid_to_ptid (pid)));
200 printf_unfiltered (_("Attaching to %s\n"),
201 target_pid_to_str (pid_to_ptid (pid)));
203 gdb_flush (gdb_stdout);
208 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
210 perror_with_name (("ptrace"));
212 error (_("This system does not support attaching to a process"));
215 inf = current_inferior ();
216 inferior_appeared (inf, pid);
217 inf->attach_flag = 1;
218 inferior_ptid = pid_to_ptid (pid);
220 /* Always add a main thread. If some target extends the ptrace
221 target, it should decorate the ptid later with more info. */
222 add_thread_silent (inferior_ptid);
224 discard_cleanups (back_to);
227 #ifdef PT_GET_PROCESS_STATE
230 inf_ptrace_post_attach (struct target_ops *self, int pid)
234 /* Set the initial event mask. */
235 memset (&pe, 0, sizeof pe);
236 pe.pe_set_event |= PTRACE_FORK;
237 if (ptrace (PT_SET_EVENT_MASK, pid,
238 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
239 perror_with_name (("ptrace"));
244 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
247 inf_ptrace_detach (struct target_ops *ops, inferior *inf, int from_tty)
249 pid_t pid = ptid_get_pid (inferior_ptid);
251 target_announce_detach (from_tty);
254 /* We'd better not have left any breakpoints in the program or it'll
255 die when it hits one. Also note that this may only work if we
256 previously attached to the inferior. It *might* work if we
257 started the process ourselves. */
259 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
261 perror_with_name (("ptrace"));
263 error (_("This system does not support detaching from a process"));
266 inf_ptrace_detach_success (ops, inf);
269 /* See inf-ptrace.h. */
272 inf_ptrace_detach_success (struct target_ops *ops, inferior *inf)
274 inferior_ptid = null_ptid;
275 detach_inferior (inf);
277 inf_child_maybe_unpush_target (ops);
280 /* Kill the inferior. */
283 inf_ptrace_kill (struct target_ops *ops)
285 pid_t pid = ptid_get_pid (inferior_ptid);
291 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
292 waitpid (pid, &status, 0);
294 target_mourn_inferior (inferior_ptid);
297 /* Return which PID to pass to ptrace in order to observe/control the
298 tracee identified by PTID. */
301 get_ptrace_pid (ptid_t ptid)
305 /* If we have an LWPID to work with, use it. Otherwise, we're
306 dealing with a non-threaded program/target. */
307 pid = ptid_get_lwp (ptid);
309 pid = ptid_get_pid (ptid);
313 /* Resume execution of thread PTID, or all threads if PTID is -1. If
314 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
318 inf_ptrace_resume (struct target_ops *ops,
319 ptid_t ptid, int step, enum gdb_signal signal)
324 if (ptid_equal (minus_one_ptid, ptid))
325 /* Resume all threads. Traditionally ptrace() only supports
326 single-threaded processes, so simply resume the inferior. */
327 pid = ptid_get_pid (inferior_ptid);
329 pid = get_ptrace_pid (ptid);
331 if (catch_syscall_enabled () > 0)
332 request = PT_SYSCALL;
334 request = PT_CONTINUE;
338 /* If this system does not support PT_STEP, a higher level
339 function will have called single_step() to transmute the step
340 request into a continue request (by setting breakpoints on
341 all possible successor instructions), so we don't have to
342 worry about that here. */
346 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
347 where it was. If GDB wanted it to start some other way, we have
348 already written a new program counter value to the child. */
350 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
352 perror_with_name (("ptrace"));
355 /* Wait for the child specified by PTID to do something. Return the
356 process ID of the child, or MINUS_ONE_PTID in case of error; store
357 the status in *OURSTATUS. */
360 inf_ptrace_wait (struct target_ops *ops,
361 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
364 int status, save_errno;
372 pid = waitpid (ptid_get_pid (ptid), &status, 0);
375 while (pid == -1 && errno == EINTR);
377 clear_sigint_trap ();
381 fprintf_unfiltered (gdb_stderr,
382 _("Child process unexpectedly missing: %s.\n"),
383 safe_strerror (save_errno));
385 /* Claim it exited with unknown signal. */
386 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
387 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
388 return inferior_ptid;
391 /* Ignore terminated detached child processes. */
392 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
397 #ifdef PT_GET_PROCESS_STATE
398 if (WIFSTOPPED (status))
403 if (ptrace (PT_GET_PROCESS_STATE, pid,
404 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
405 perror_with_name (("ptrace"));
407 switch (pe.pe_report_event)
410 ourstatus->kind = TARGET_WAITKIND_FORKED;
411 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
413 /* Make sure the other end of the fork is stopped too. */
414 fpid = waitpid (pe.pe_other_pid, &status, 0);
416 perror_with_name (("waitpid"));
418 if (ptrace (PT_GET_PROCESS_STATE, fpid,
419 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
420 perror_with_name (("ptrace"));
422 gdb_assert (pe.pe_report_event == PTRACE_FORK);
423 gdb_assert (pe.pe_other_pid == pid);
424 if (fpid == ptid_get_pid (inferior_ptid))
426 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
427 return pid_to_ptid (fpid);
430 return pid_to_ptid (pid);
435 store_waitstatus (ourstatus, status);
436 return pid_to_ptid (pid);
439 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
440 from process PID's memory into READBUF. Start at target address ADDR
441 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
442 be non-null. Return the number of transferred bytes. */
445 inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf,
446 const gdb_byte *writebuf,
447 ULONGEST addr, ULONGEST len)
452 /* We transfer aligned words. Thus align ADDR down to a word
453 boundary and determine how many bytes to skip at the
455 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
460 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
462 /* Restrict to a chunk that fits in the current word. */
463 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
465 /* Use a union for type punning. */
468 PTRACE_TYPE_RET word;
469 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
472 /* Read the word, also when doing a partial word write. */
473 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
476 buf.word = ptrace (PT_READ_I, pid,
477 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
481 memcpy (readbuf + n, buf.byte + skip, chunk);
483 if (writebuf != NULL)
485 memcpy (buf.byte + skip, writebuf + n, chunk);
487 ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
491 /* Using the appropriate one (I or D) is necessary for
492 Gould NP1, at least. */
494 ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
505 /* Implement the to_xfer_partial target_ops method. */
507 static enum target_xfer_status
508 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
509 const char *annex, gdb_byte *readbuf,
510 const gdb_byte *writebuf,
511 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
513 pid_t pid = get_ptrace_pid (inferior_ptid);
517 case TARGET_OBJECT_MEMORY:
519 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
520 request that promises to be much more efficient in reading
521 and writing data in the traced process's address space. */
523 struct ptrace_io_desc piod;
525 /* NOTE: We assume that there are no distinct address spaces
526 for instruction and data. However, on OpenBSD 3.9 and
527 later, PIOD_WRITE_D doesn't allow changing memory that's
528 mapped read-only. Since most code segments will be
529 read-only, using PIOD_WRITE_D will prevent us from
530 inserting breakpoints, so we use PIOD_WRITE_I instead. */
531 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
532 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
533 piod.piod_offs = (void *) (long) offset;
537 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
539 /* Return the actual number of bytes read or written. */
540 *xfered_len = piod.piod_len;
541 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
543 /* If the PT_IO request is somehow not supported, fallback on
544 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
545 to indicate failure. */
547 return TARGET_XFER_EOF;
550 *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf,
552 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
554 case TARGET_OBJECT_UNWIND_TABLE:
555 return TARGET_XFER_E_IO;
557 case TARGET_OBJECT_AUXV:
558 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
559 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
560 request that allows us to read the auxilliary vector. Other
561 BSD's may follow if they feel the need to support PIE. */
563 struct ptrace_io_desc piod;
566 return TARGET_XFER_E_IO;
567 piod.piod_op = PIOD_READ_AUXV;
568 piod.piod_addr = readbuf;
569 piod.piod_offs = (void *) (long) offset;
573 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
575 /* Return the actual number of bytes read or written. */
576 *xfered_len = piod.piod_len;
577 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
581 return TARGET_XFER_E_IO;
583 case TARGET_OBJECT_WCOOKIE:
584 return TARGET_XFER_E_IO;
587 return TARGET_XFER_E_IO;
591 /* Return non-zero if the thread specified by PTID is alive. */
594 inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
596 /* ??? Is kill the right way to do this? */
597 return (kill (ptid_get_pid (ptid), 0) != -1);
600 /* Print status information about what we're accessing. */
603 inf_ptrace_files_info (struct target_ops *ignore)
605 struct inferior *inf = current_inferior ();
607 printf_filtered (_("\tUsing the running image of %s %s.\n"),
608 inf->attach_flag ? "attached" : "child",
609 target_pid_to_str (inferior_ptid));
613 inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
615 return normal_pid_to_str (ptid);
618 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
620 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
621 Return 0 if *READPTR is already at the end of the buffer.
622 Return -1 if there is insufficient buffer for a whole entry.
623 Return 1 if an entry was read into *TYPEP and *VALP. */
626 inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
627 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
629 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
630 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
631 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
632 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
633 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
634 gdb_byte *ptr = *readptr;
639 if (endptr - ptr < 2 * sizeof_auxv_val)
642 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
643 ptr += sizeof_auxv_val; /* Alignment. */
644 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
645 ptr += sizeof_auxv_val;
653 /* Create a prototype ptrace target. The client can override it with
657 inf_ptrace_target (void)
659 struct target_ops *t = inf_child_target ();
661 t->to_attach = inf_ptrace_attach;
662 t->to_detach = inf_ptrace_detach;
663 t->to_resume = inf_ptrace_resume;
664 t->to_wait = inf_ptrace_wait;
665 t->to_files_info = inf_ptrace_files_info;
666 t->to_kill = inf_ptrace_kill;
667 t->to_create_inferior = inf_ptrace_create_inferior;
668 #ifdef PT_GET_PROCESS_STATE
669 t->to_follow_fork = inf_ptrace_follow_fork;
670 t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
671 t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
672 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
673 t->to_post_attach = inf_ptrace_post_attach;
675 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
676 t->to_thread_alive = inf_ptrace_thread_alive;
677 t->to_pid_to_str = inf_ptrace_pid_to_str;
678 t->to_xfer_partial = inf_ptrace_xfer_partial;
679 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
680 t->to_auxv_parse = inf_ptrace_auxv_parse;
687 /* Pointer to a function that returns the offset within the user area
688 where a particular register is stored. */
689 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
691 /* Fetch register REGNUM from the inferior. */
694 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
696 struct gdbarch *gdbarch = regcache->arch ();
699 PTRACE_TYPE_RET *buf;
703 /* This isn't really an address, but ptrace thinks of it as one. */
704 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
705 if (addr == (CORE_ADDR)-1
706 || gdbarch_cannot_fetch_register (gdbarch, regnum))
708 regcache_raw_supply (regcache, regnum, NULL);
712 pid = get_ptrace_pid (regcache_get_ptid (regcache));
714 size = register_size (gdbarch, regnum);
715 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
716 buf = (PTRACE_TYPE_RET *) alloca (size);
718 /* Read the register contents from the inferior a chunk at a time. */
719 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
722 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
724 error (_("Couldn't read register %s (#%d): %s."),
725 gdbarch_register_name (gdbarch, regnum),
726 regnum, safe_strerror (errno));
728 addr += sizeof (PTRACE_TYPE_RET);
730 regcache_raw_supply (regcache, regnum, buf);
733 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
734 for all registers. */
737 inf_ptrace_fetch_registers (struct target_ops *ops,
738 struct regcache *regcache, int regnum)
742 regnum < gdbarch_num_regs (regcache->arch ());
744 inf_ptrace_fetch_register (regcache, regnum);
746 inf_ptrace_fetch_register (regcache, regnum);
749 /* Store register REGNUM into the inferior. */
752 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
754 struct gdbarch *gdbarch = regcache->arch ();
757 PTRACE_TYPE_RET *buf;
761 /* This isn't really an address, but ptrace thinks of it as one. */
762 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
763 if (addr == (CORE_ADDR)-1
764 || gdbarch_cannot_store_register (gdbarch, regnum))
767 pid = get_ptrace_pid (regcache_get_ptid (regcache));
769 size = register_size (gdbarch, regnum);
770 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
771 buf = (PTRACE_TYPE_RET *) alloca (size);
773 /* Write the register contents into the inferior a chunk at a time. */
774 regcache_raw_collect (regcache, regnum, buf);
775 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
778 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
780 error (_("Couldn't write register %s (#%d): %s."),
781 gdbarch_register_name (gdbarch, regnum),
782 regnum, safe_strerror (errno));
784 addr += sizeof (PTRACE_TYPE_RET);
788 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
789 this for all registers. */
792 inf_ptrace_store_registers (struct target_ops *ops,
793 struct regcache *regcache, int regnum)
797 regnum < gdbarch_num_regs (regcache->arch ());
799 inf_ptrace_store_register (regcache, regnum);
801 inf_ptrace_store_register (regcache, regnum);
804 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
805 a function returning the offset within the user area where a
806 particular register is stored. */
809 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
810 (struct gdbarch *, int, int))
812 struct target_ops *t = inf_ptrace_target();
814 gdb_assert (register_u_offset);
815 inf_ptrace_register_u_offset = register_u_offset;
816 t->to_fetch_registers = inf_ptrace_fetch_registers;
817 t->to_store_registers = inf_ptrace_store_registers;