1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2013 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/>. */
28 #include "gdb_assert.h"
30 #include "gdb_ptrace.h"
34 #include "inf-ptrace.h"
35 #include "inf-child.h"
36 #include "gdbthread.h"
40 #ifdef PT_GET_PROCESS_STATE
43 inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
49 pid = ptid_get_pid (inferior_ptid);
51 if (ptrace (PT_GET_PROCESS_STATE, pid,
52 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
53 perror_with_name (("ptrace"));
55 gdb_assert (pe.pe_report_event == PTRACE_FORK);
56 fpid = pe.pe_other_pid;
60 struct inferior *parent_inf, *child_inf;
61 struct thread_info *tp;
63 parent_inf = find_inferior_pid (pid);
66 child_inf = add_inferior (fpid);
67 child_inf->attach_flag = parent_inf->attach_flag;
68 copy_terminal_info (child_inf, parent_inf);
69 child_inf->pspace = parent_inf->pspace;
70 child_inf->aspace = parent_inf->aspace;
72 /* Before detaching from the parent, remove all breakpoints from
74 remove_breakpoints ();
76 if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
77 perror_with_name (("ptrace"));
79 /* Switch inferior_ptid out of the parent's way. */
80 inferior_ptid = pid_to_ptid (fpid);
82 /* Delete the parent. */
83 detach_inferior (pid);
85 add_thread_silent (inferior_ptid);
89 /* Breakpoints have already been detached from the child by
92 if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1)
93 perror_with_name (("ptrace"));
99 #endif /* PT_GET_PROCESS_STATE */
102 /* Prepare to be traced. */
107 /* "Trace me, Dr. Memory!" */
108 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
111 /* Start a new inferior Unix child process. EXEC_FILE is the file to
112 run, ALLARGS is a string containing the arguments to the program.
113 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
117 inf_ptrace_create_inferior (struct target_ops *ops,
118 char *exec_file, char *allargs, char **env,
123 /* Do not change either targets above or the same target if already present.
124 The reason is the target stack is shared across multiple inferiors. */
125 int ops_already_pushed = target_is_pushed (ops);
126 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
128 if (! ops_already_pushed)
130 /* Clear possible core file with its process_stratum. */
132 make_cleanup_unpush_target (ops);
135 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
138 discard_cleanups (back_to);
140 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
142 /* On some targets, there must be some explicit actions taken after
143 the inferior has been started up. */
144 target_post_startup_inferior (pid_to_ptid (pid));
147 #ifdef PT_GET_PROCESS_STATE
150 inf_ptrace_post_startup_inferior (ptid_t pid)
154 /* Set the initial event mask. */
155 memset (&pe, 0, sizeof pe);
156 pe.pe_set_event |= PTRACE_FORK;
157 if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
158 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
159 perror_with_name (("ptrace"));
164 /* Clean up a rotting corpse of an inferior after it died. */
167 inf_ptrace_mourn_inferior (struct target_ops *ops)
171 /* Wait just one more time to collect the inferior's exit status.
172 Do not check whether this succeeds though, since we may be
173 dealing with a process that we attached to. Such a process will
174 only report its exit status to its original parent. */
175 waitpid (ptid_get_pid (inferior_ptid), &status, 0);
177 generic_mourn_inferior ();
179 if (!have_inferiors ())
183 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
184 be chatty about it. */
187 inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty)
191 struct inferior *inf;
193 /* Do not change either targets above or the same target if already present.
194 The reason is the target stack is shared across multiple inferiors. */
195 int ops_already_pushed = target_is_pushed (ops);
196 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
198 pid = parse_pid_to_attach (args);
200 if (pid == getpid ()) /* Trying to masturbate? */
201 error (_("I refuse to debug myself!"));
203 if (! ops_already_pushed)
205 /* target_pid_to_str already uses the target. Also clear possible core
206 file with its process_stratum. */
208 make_cleanup_unpush_target (ops);
213 exec_file = get_exec_file (0);
216 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
217 target_pid_to_str (pid_to_ptid (pid)));
219 printf_unfiltered (_("Attaching to %s\n"),
220 target_pid_to_str (pid_to_ptid (pid)));
222 gdb_flush (gdb_stdout);
227 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
229 perror_with_name (("ptrace"));
231 error (_("This system does not support attaching to a process"));
234 inf = current_inferior ();
235 inferior_appeared (inf, pid);
236 inf->attach_flag = 1;
237 inferior_ptid = pid_to_ptid (pid);
239 /* Always add a main thread. If some target extends the ptrace
240 target, it should decorate the ptid later with more info. */
241 add_thread_silent (inferior_ptid);
243 discard_cleanups (back_to);
246 #ifdef PT_GET_PROCESS_STATE
249 inf_ptrace_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, optionally passing it the signal
264 specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
267 inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
269 pid_t pid = ptid_get_pid (inferior_ptid);
274 char *exec_file = get_exec_file (0);
277 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
278 target_pid_to_str (pid_to_ptid (pid)));
279 gdb_flush (gdb_stdout);
285 /* We'd better not have left any breakpoints in the program or it'll
286 die when it hits one. Also note that this may only work if we
287 previously attached to the inferior. It *might* work if we
288 started the process ourselves. */
290 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
292 perror_with_name (("ptrace"));
294 error (_("This system does not support detaching from a process"));
297 inferior_ptid = null_ptid;
298 detach_inferior (pid);
300 if (!have_inferiors ())
304 /* Kill the inferior. */
307 inf_ptrace_kill (struct target_ops *ops)
309 pid_t pid = ptid_get_pid (inferior_ptid);
315 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
316 waitpid (pid, &status, 0);
318 target_mourn_inferior ();
321 /* Stop the inferior. */
324 inf_ptrace_stop (ptid_t ptid)
326 /* Send a SIGINT to the process group. This acts just like the user
327 typed a ^C on the controlling terminal. Note that using a
328 negative process number in kill() is a System V-ism. The proper
329 BSD interface is killpg(). However, all modern BSDs support the
330 System V interface too. */
331 kill (-inferior_process_group (), SIGINT);
334 /* Resume execution of thread PTID, or all threads if PTID is -1. If
335 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
339 inf_ptrace_resume (struct target_ops *ops,
340 ptid_t ptid, int step, enum gdb_signal signal)
342 pid_t pid = ptid_get_pid (ptid);
346 /* Resume all threads. Traditionally ptrace() only supports
347 single-threaded processes, so simply resume the inferior. */
348 pid = ptid_get_pid (inferior_ptid);
350 if (catch_syscall_enabled () > 0)
351 request = PT_SYSCALL;
353 request = PT_CONTINUE;
357 /* If this system does not support PT_STEP, a higher level
358 function will have called single_step() to transmute the step
359 request into a continue request (by setting breakpoints on
360 all possible successor instructions), so we don't have to
361 worry about that here. */
365 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
366 where it was. If GDB wanted it to start some other way, we have
367 already written a new program counter value to the child. */
369 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
371 perror_with_name (("ptrace"));
374 /* Wait for the child specified by PTID to do something. Return the
375 process ID of the child, or MINUS_ONE_PTID in case of error; store
376 the status in *OURSTATUS. */
379 inf_ptrace_wait (struct target_ops *ops,
380 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
383 int status, save_errno;
391 pid = waitpid (ptid_get_pid (ptid), &status, 0);
394 while (pid == -1 && errno == EINTR);
396 clear_sigint_trap ();
400 fprintf_unfiltered (gdb_stderr,
401 _("Child process unexpectedly missing: %s.\n"),
402 safe_strerror (save_errno));
404 /* Claim it exited with unknown signal. */
405 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
406 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
407 return inferior_ptid;
410 /* Ignore terminated detached child processes. */
411 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
416 #ifdef PT_GET_PROCESS_STATE
417 if (WIFSTOPPED (status))
422 if (ptrace (PT_GET_PROCESS_STATE, pid,
423 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
424 perror_with_name (("ptrace"));
426 switch (pe.pe_report_event)
429 ourstatus->kind = TARGET_WAITKIND_FORKED;
430 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
432 /* Make sure the other end of the fork is stopped too. */
433 fpid = waitpid (pe.pe_other_pid, &status, 0);
435 perror_with_name (("waitpid"));
437 if (ptrace (PT_GET_PROCESS_STATE, fpid,
438 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
439 perror_with_name (("ptrace"));
441 gdb_assert (pe.pe_report_event == PTRACE_FORK);
442 gdb_assert (pe.pe_other_pid == pid);
443 if (fpid == ptid_get_pid (inferior_ptid))
445 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
446 return pid_to_ptid (fpid);
449 return pid_to_ptid (pid);
454 store_waitstatus (ourstatus, status);
455 return pid_to_ptid (pid);
458 /* Attempt a transfer all LEN bytes starting at OFFSET between the
459 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
460 Return the number of bytes actually transferred. */
463 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
464 const char *annex, gdb_byte *readbuf,
465 const gdb_byte *writebuf,
466 ULONGEST offset, LONGEST len)
468 pid_t pid = ptid_get_pid (inferior_ptid);
472 case TARGET_OBJECT_MEMORY:
474 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
475 request that promises to be much more efficient in reading
476 and writing data in the traced process's address space. */
478 struct ptrace_io_desc piod;
480 /* NOTE: We assume that there are no distinct address spaces
481 for instruction and data. However, on OpenBSD 3.9 and
482 later, PIOD_WRITE_D doesn't allow changing memory that's
483 mapped read-only. Since most code segments will be
484 read-only, using PIOD_WRITE_D will prevent us from
485 inserting breakpoints, so we use PIOD_WRITE_I instead. */
486 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
487 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
488 piod.piod_offs = (void *) (long) offset;
492 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
493 /* Return the actual number of bytes read or written. */
494 return piod.piod_len;
495 /* If the PT_IO request is somehow not supported, fallback on
496 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
497 to indicate failure. */
505 PTRACE_TYPE_RET word;
506 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
508 ULONGEST rounded_offset;
511 /* Round the start offset down to the next long word
513 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
515 /* Since ptrace will transfer a single word starting at that
516 rounded_offset the partial_len needs to be adjusted down to
517 that (remember this function only does a single transfer).
518 Should the required length be even less, adjust it down
520 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
521 if (partial_len > len)
526 /* If OFFSET:PARTIAL_LEN is smaller than
527 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
528 be needed. Read in the entire word. */
529 if (rounded_offset < offset
530 || (offset + partial_len
531 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
532 /* Need part of initial word -- fetch it. */
533 buffer.word = ptrace (PT_READ_I, pid,
534 (PTRACE_TYPE_ARG3)(uintptr_t)
537 /* Copy data to be written over corresponding part of
539 memcpy (buffer.byte + (offset - rounded_offset),
540 writebuf, partial_len);
543 ptrace (PT_WRITE_D, pid,
544 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
548 /* Using the appropriate one (I or D) is necessary for
549 Gould NP1, at least. */
551 ptrace (PT_WRITE_I, pid,
552 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
562 buffer.word = ptrace (PT_READ_I, pid,
563 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
567 /* Copy appropriate bytes out of the buffer. */
568 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
575 case TARGET_OBJECT_UNWIND_TABLE:
578 case TARGET_OBJECT_AUXV:
579 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
580 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
581 request that allows us to read the auxilliary vector. Other
582 BSD's may follow if they feel the need to support PIE. */
584 struct ptrace_io_desc piod;
588 piod.piod_op = PIOD_READ_AUXV;
589 piod.piod_addr = readbuf;
590 piod.piod_offs = (void *) (long) offset;
594 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
595 /* Return the actual number of bytes read or written. */
596 return piod.piod_len;
601 case TARGET_OBJECT_WCOOKIE:
609 /* Return non-zero if the thread specified by PTID is alive. */
612 inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
614 /* ??? Is kill the right way to do this? */
615 return (kill (ptid_get_pid (ptid), 0) != -1);
618 /* Print status information about what we're accessing. */
621 inf_ptrace_files_info (struct target_ops *ignore)
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));
631 inf_ptrace_pid_to_str (struct target_ops *ops, 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_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
645 gdb_byte *endptr, 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;
671 /* Create a prototype ptrace target. The client can override it with
675 inf_ptrace_target (void)
677 struct target_ops *t = inf_child_target ();
679 t->to_attach = inf_ptrace_attach;
680 t->to_detach = inf_ptrace_detach;
681 t->to_resume = inf_ptrace_resume;
682 t->to_wait = inf_ptrace_wait;
683 t->to_files_info = inf_ptrace_files_info;
684 t->to_kill = inf_ptrace_kill;
685 t->to_create_inferior = inf_ptrace_create_inferior;
686 #ifdef PT_GET_PROCESS_STATE
687 t->to_follow_fork = inf_ptrace_follow_fork;
688 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
689 t->to_post_attach = inf_ptrace_post_attach;
691 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
692 t->to_thread_alive = inf_ptrace_thread_alive;
693 t->to_pid_to_str = inf_ptrace_pid_to_str;
694 t->to_stop = inf_ptrace_stop;
695 t->to_xfer_partial = inf_ptrace_xfer_partial;
696 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
697 t->to_auxv_parse = inf_ptrace_auxv_parse;
704 /* Pointer to a function that returns the offset within the user area
705 where a particular register is stored. */
706 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
708 /* Fetch register REGNUM from the inferior. */
711 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
713 struct gdbarch *gdbarch = get_regcache_arch (regcache);
716 PTRACE_TYPE_RET *buf;
719 /* This isn't really an address, but ptrace thinks of it as one. */
720 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
721 if (addr == (CORE_ADDR)-1
722 || gdbarch_cannot_fetch_register (gdbarch, regnum))
724 regcache_raw_supply (regcache, regnum, NULL);
728 /* Cater for systems like GNU/Linux, that implement threads as
729 separate processes. */
730 pid = ptid_get_lwp (inferior_ptid);
732 pid = ptid_get_pid (inferior_ptid);
734 size = register_size (gdbarch, regnum);
735 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
738 /* Read the register contents from the inferior a chunk at a time. */
739 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
742 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
744 error (_("Couldn't read register %s (#%d): %s."),
745 gdbarch_register_name (gdbarch, regnum),
746 regnum, safe_strerror (errno));
748 addr += sizeof (PTRACE_TYPE_RET);
750 regcache_raw_supply (regcache, regnum, buf);
753 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
754 for all registers. */
757 inf_ptrace_fetch_registers (struct target_ops *ops,
758 struct regcache *regcache, int regnum)
762 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
764 inf_ptrace_fetch_register (regcache, regnum);
766 inf_ptrace_fetch_register (regcache, regnum);
769 /* Store register REGNUM into the inferior. */
772 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
774 struct gdbarch *gdbarch = get_regcache_arch (regcache);
777 PTRACE_TYPE_RET *buf;
780 /* This isn't really an address, but ptrace thinks of it as one. */
781 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
782 if (addr == (CORE_ADDR)-1
783 || gdbarch_cannot_store_register (gdbarch, regnum))
786 /* Cater for systems like GNU/Linux, that implement threads as
787 separate processes. */
788 pid = ptid_get_lwp (inferior_ptid);
790 pid = ptid_get_pid (inferior_ptid);
792 size = register_size (gdbarch, regnum);
793 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
796 /* Write the register contents into the inferior a chunk at a time. */
797 regcache_raw_collect (regcache, regnum, buf);
798 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
801 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
803 error (_("Couldn't write register %s (#%d): %s."),
804 gdbarch_register_name (gdbarch, regnum),
805 regnum, safe_strerror (errno));
807 addr += sizeof (PTRACE_TYPE_RET);
811 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
812 this for all registers. */
815 inf_ptrace_store_registers (struct target_ops *ops,
816 struct regcache *regcache, int regnum)
820 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
822 inf_ptrace_store_register (regcache, regnum);
824 inf_ptrace_store_register (regcache, regnum);
827 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
828 a function returning the offset within the user area where a
829 particular register is stored. */
832 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
833 (struct gdbarch *, int, int))
835 struct target_ops *t = inf_ptrace_target();
837 gdb_assert (register_u_offset);
838 inf_ptrace_register_u_offset = register_u_offset;
839 t->to_fetch_registers = inf_ptrace_fetch_registers;
840 t->to_store_registers = inf_ptrace_store_registers;