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"
29 #include "gdb_string.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)
48 pid = ptid_get_pid (inferior_ptid);
50 if (ptrace (PT_GET_PROCESS_STATE, pid,
51 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
52 perror_with_name (("ptrace"));
54 gdb_assert (pe.pe_report_event == PTRACE_FORK);
55 fpid = pe.pe_other_pid;
59 struct inferior *parent_inf, *child_inf;
60 struct thread_info *tp;
62 parent_inf = find_inferior_pid (pid);
65 child_inf = add_inferior (fpid);
66 child_inf->attach_flag = parent_inf->attach_flag;
67 copy_terminal_info (child_inf, parent_inf);
68 child_inf->pspace = parent_inf->pspace;
69 child_inf->aspace = parent_inf->aspace;
71 /* Before detaching from the parent, remove all breakpoints from
73 remove_breakpoints ();
75 if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
76 perror_with_name (("ptrace"));
78 /* Switch inferior_ptid out of the parent's way. */
79 inferior_ptid = pid_to_ptid (fpid);
81 /* Delete the parent. */
82 detach_inferior (pid);
84 add_thread_silent (inferior_ptid);
88 /* Breakpoints have already been detached from the child by
91 if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1)
92 perror_with_name (("ptrace"));
98 #endif /* PT_GET_PROCESS_STATE */
101 /* Prepare to be traced. */
106 /* "Trace me, Dr. Memory!" */
107 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
110 /* Start a new inferior Unix child process. EXEC_FILE is the file to
111 run, ALLARGS is a string containing the arguments to the program.
112 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
116 inf_ptrace_create_inferior (struct target_ops *ops,
117 char *exec_file, char *allargs, char **env,
122 /* Do not change either targets above or the same target if already present.
123 The reason is the target stack is shared across multiple inferiors. */
124 int ops_already_pushed = target_is_pushed (ops);
125 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
127 if (! ops_already_pushed)
129 /* Clear possible core file with its process_stratum. */
131 make_cleanup_unpush_target (ops);
134 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
137 discard_cleanups (back_to);
139 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
140 be 1 or 2 depending on whether we're starting without or with a
142 startup_inferior (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 (pid_to_ptid (pid));
149 #ifdef PT_GET_PROCESS_STATE
152 inf_ptrace_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, ptid_get_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_mourn_inferior (struct target_ops *ops)
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 (ptid_get_pid (inferior_ptid), &status, 0);
179 generic_mourn_inferior ();
181 if (!have_inferiors ())
185 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
186 be chatty about it. */
189 inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty)
193 struct inferior *inf;
195 /* Do not change either targets above or the same target if already present.
196 The reason is the target stack is shared across multiple inferiors. */
197 int ops_already_pushed = target_is_pushed (ops);
198 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
200 pid = parse_pid_to_attach (args);
202 if (pid == getpid ()) /* Trying to masturbate? */
203 error (_("I refuse to debug myself!"));
205 if (! ops_already_pushed)
207 /* target_pid_to_str already uses the target. Also clear possible core
208 file with its process_stratum. */
210 make_cleanup_unpush_target (ops);
215 exec_file = get_exec_file (0);
218 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
219 target_pid_to_str (pid_to_ptid (pid)));
221 printf_unfiltered (_("Attaching to %s\n"),
222 target_pid_to_str (pid_to_ptid (pid)));
224 gdb_flush (gdb_stdout);
229 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
231 perror_with_name (("ptrace"));
233 error (_("This system does not support attaching to a process"));
236 inf = current_inferior ();
237 inferior_appeared (inf, pid);
238 inf->attach_flag = 1;
239 inferior_ptid = pid_to_ptid (pid);
241 /* Always add a main thread. If some target extends the ptrace
242 target, it should decorate the ptid later with more info. */
243 add_thread_silent (inferior_ptid);
245 discard_cleanups (back_to);
248 #ifdef PT_GET_PROCESS_STATE
251 inf_ptrace_post_attach (int pid)
255 /* Set the initial event mask. */
256 memset (&pe, 0, sizeof pe);
257 pe.pe_set_event |= PTRACE_FORK;
258 if (ptrace (PT_SET_EVENT_MASK, pid,
259 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
260 perror_with_name (("ptrace"));
265 /* Detach from the inferior, optionally passing it the signal
266 specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
269 inf_ptrace_detach (struct target_ops *ops, char *args, int from_tty)
271 pid_t pid = ptid_get_pid (inferior_ptid);
276 char *exec_file = get_exec_file (0);
279 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
280 target_pid_to_str (pid_to_ptid (pid)));
281 gdb_flush (gdb_stdout);
287 /* We'd better not have left any breakpoints in the program or it'll
288 die when it hits one. Also note that this may only work if we
289 previously attached to the inferior. It *might* work if we
290 started the process ourselves. */
292 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
294 perror_with_name (("ptrace"));
296 error (_("This system does not support detaching from a process"));
299 inferior_ptid = null_ptid;
300 detach_inferior (pid);
302 if (!have_inferiors ())
306 /* Kill the inferior. */
309 inf_ptrace_kill (struct target_ops *ops)
311 pid_t pid = ptid_get_pid (inferior_ptid);
317 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
318 waitpid (pid, &status, 0);
320 target_mourn_inferior ();
323 /* Stop the inferior. */
326 inf_ptrace_stop (ptid_t ptid)
328 /* Send a SIGINT to the process group. This acts just like the user
329 typed a ^C on the controlling terminal. Note that using a
330 negative process number in kill() is a System V-ism. The proper
331 BSD interface is killpg(). However, all modern BSDs support the
332 System V interface too. */
333 kill (-inferior_process_group (), SIGINT);
336 /* Resume execution of thread PTID, or all threads if PTID is -1. If
337 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
341 inf_ptrace_resume (struct target_ops *ops,
342 ptid_t ptid, int step, enum gdb_signal signal)
344 pid_t pid = ptid_get_pid (ptid);
348 /* Resume all threads. Traditionally ptrace() only supports
349 single-threaded processes, so simply resume the inferior. */
350 pid = ptid_get_pid (inferior_ptid);
352 if (catch_syscall_enabled () > 0)
353 request = PT_SYSCALL;
355 request = PT_CONTINUE;
359 /* If this system does not support PT_STEP, a higher level
360 function will have called single_step() to transmute the step
361 request into a continue request (by setting breakpoints on
362 all possible successor instructions), so we don't have to
363 worry about that here. */
367 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
368 where it was. If GDB wanted it to start some other way, we have
369 already written a new program counter value to the child. */
371 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
373 perror_with_name (("ptrace"));
376 /* Wait for the child specified by PTID to do something. Return the
377 process ID of the child, or MINUS_ONE_PTID in case of error; store
378 the status in *OURSTATUS. */
381 inf_ptrace_wait (struct target_ops *ops,
382 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
385 int status, save_errno;
393 pid = waitpid (ptid_get_pid (ptid), &status, 0);
396 while (pid == -1 && errno == EINTR);
398 clear_sigint_trap ();
402 fprintf_unfiltered (gdb_stderr,
403 _("Child process unexpectedly missing: %s.\n"),
404 safe_strerror (save_errno));
406 /* Claim it exited with unknown signal. */
407 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
408 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
409 return inferior_ptid;
412 /* Ignore terminated detached child processes. */
413 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
418 #ifdef PT_GET_PROCESS_STATE
419 if (WIFSTOPPED (status))
424 if (ptrace (PT_GET_PROCESS_STATE, pid,
425 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
426 perror_with_name (("ptrace"));
428 switch (pe.pe_report_event)
431 ourstatus->kind = TARGET_WAITKIND_FORKED;
432 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
434 /* Make sure the other end of the fork is stopped too. */
435 fpid = waitpid (pe.pe_other_pid, &status, 0);
437 perror_with_name (("waitpid"));
439 if (ptrace (PT_GET_PROCESS_STATE, fpid,
440 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
441 perror_with_name (("ptrace"));
443 gdb_assert (pe.pe_report_event == PTRACE_FORK);
444 gdb_assert (pe.pe_other_pid == pid);
445 if (fpid == ptid_get_pid (inferior_ptid))
447 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
448 return pid_to_ptid (fpid);
451 return pid_to_ptid (pid);
456 store_waitstatus (ourstatus, status);
457 return pid_to_ptid (pid);
460 /* Attempt a transfer all LEN bytes starting at OFFSET between the
461 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
462 Return the number of bytes actually transferred. */
465 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
466 const char *annex, gdb_byte *readbuf,
467 const gdb_byte *writebuf,
468 ULONGEST offset, LONGEST len)
470 pid_t pid = ptid_get_pid (inferior_ptid);
474 case TARGET_OBJECT_MEMORY:
476 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
477 request that promises to be much more efficient in reading
478 and writing data in the traced process's address space. */
480 struct ptrace_io_desc piod;
482 /* NOTE: We assume that there are no distinct address spaces
483 for instruction and data. However, on OpenBSD 3.9 and
484 later, PIOD_WRITE_D doesn't allow changing memory that's
485 mapped read-only. Since most code segments will be
486 read-only, using PIOD_WRITE_D will prevent us from
487 inserting breakpoints, so we use PIOD_WRITE_I instead. */
488 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
489 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
490 piod.piod_offs = (void *) (long) offset;
494 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
495 /* Return the actual number of bytes read or written. */
496 return piod.piod_len;
497 /* If the PT_IO request is somehow not supported, fallback on
498 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
499 to indicate failure. */
507 PTRACE_TYPE_RET word;
508 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
510 ULONGEST rounded_offset;
513 /* Round the start offset down to the next long word
515 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
517 /* Since ptrace will transfer a single word starting at that
518 rounded_offset the partial_len needs to be adjusted down to
519 that (remember this function only does a single transfer).
520 Should the required length be even less, adjust it down
522 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
523 if (partial_len > len)
528 /* If OFFSET:PARTIAL_LEN is smaller than
529 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
530 be needed. Read in the entire word. */
531 if (rounded_offset < offset
532 || (offset + partial_len
533 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
534 /* Need part of initial word -- fetch it. */
535 buffer.word = ptrace (PT_READ_I, pid,
536 (PTRACE_TYPE_ARG3)(uintptr_t)
539 /* Copy data to be written over corresponding part of
541 memcpy (buffer.byte + (offset - rounded_offset),
542 writebuf, partial_len);
545 ptrace (PT_WRITE_D, pid,
546 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
550 /* Using the appropriate one (I or D) is necessary for
551 Gould NP1, at least. */
553 ptrace (PT_WRITE_I, pid,
554 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
564 buffer.word = ptrace (PT_READ_I, pid,
565 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
569 /* Copy appropriate bytes out of the buffer. */
570 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
577 case TARGET_OBJECT_UNWIND_TABLE:
580 case TARGET_OBJECT_AUXV:
581 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
582 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
583 request that allows us to read the auxilliary vector. Other
584 BSD's may follow if they feel the need to support PIE. */
586 struct ptrace_io_desc piod;
590 piod.piod_op = PIOD_READ_AUXV;
591 piod.piod_addr = readbuf;
592 piod.piod_offs = (void *) (long) offset;
596 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
597 /* Return the actual number of bytes read or written. */
598 return piod.piod_len;
603 case TARGET_OBJECT_WCOOKIE:
611 /* Return non-zero if the thread specified by PTID is alive. */
614 inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
616 /* ??? Is kill the right way to do this? */
617 return (kill (ptid_get_pid (ptid), 0) != -1);
620 /* Print status information about what we're accessing. */
623 inf_ptrace_files_info (struct target_ops *ignore)
625 struct inferior *inf = current_inferior ();
627 printf_filtered (_("\tUsing the running image of %s %s.\n"),
628 inf->attach_flag ? "attached" : "child",
629 target_pid_to_str (inferior_ptid));
633 inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
635 return normal_pid_to_str (ptid);
638 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
640 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
641 Return 0 if *READPTR is already at the end of the buffer.
642 Return -1 if there is insufficient buffer for a whole entry.
643 Return 1 if an entry was read into *TYPEP and *VALP. */
646 inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
647 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
649 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
650 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
651 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
652 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
653 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
654 gdb_byte *ptr = *readptr;
659 if (endptr - ptr < 2 * sizeof_auxv_val)
662 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
663 ptr += sizeof_auxv_val; /* Alignment. */
664 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
665 ptr += sizeof_auxv_val;
673 /* Create a prototype ptrace target. The client can override it with
677 inf_ptrace_target (void)
679 struct target_ops *t = inf_child_target ();
681 t->to_attach = inf_ptrace_attach;
682 t->to_detach = inf_ptrace_detach;
683 t->to_resume = inf_ptrace_resume;
684 t->to_wait = inf_ptrace_wait;
685 t->to_files_info = inf_ptrace_files_info;
686 t->to_kill = inf_ptrace_kill;
687 t->to_create_inferior = inf_ptrace_create_inferior;
688 #ifdef PT_GET_PROCESS_STATE
689 t->to_follow_fork = inf_ptrace_follow_fork;
690 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
691 t->to_post_attach = inf_ptrace_post_attach;
693 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
694 t->to_thread_alive = inf_ptrace_thread_alive;
695 t->to_pid_to_str = inf_ptrace_pid_to_str;
696 t->to_stop = inf_ptrace_stop;
697 t->to_xfer_partial = inf_ptrace_xfer_partial;
698 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
699 t->to_auxv_parse = inf_ptrace_auxv_parse;
706 /* Pointer to a function that returns the offset within the user area
707 where a particular register is stored. */
708 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
710 /* Fetch register REGNUM from the inferior. */
713 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
715 struct gdbarch *gdbarch = get_regcache_arch (regcache);
718 PTRACE_TYPE_RET *buf;
721 /* This isn't really an address, but ptrace thinks of it as one. */
722 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
723 if (addr == (CORE_ADDR)-1
724 || gdbarch_cannot_fetch_register (gdbarch, regnum))
726 regcache_raw_supply (regcache, regnum, NULL);
730 /* Cater for systems like GNU/Linux, that implement threads as
731 separate processes. */
732 pid = ptid_get_lwp (inferior_ptid);
734 pid = ptid_get_pid (inferior_ptid);
736 size = register_size (gdbarch, regnum);
737 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
740 /* Read the register contents from the inferior a chunk at a time. */
741 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
744 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
746 error (_("Couldn't read register %s (#%d): %s."),
747 gdbarch_register_name (gdbarch, regnum),
748 regnum, safe_strerror (errno));
750 addr += sizeof (PTRACE_TYPE_RET);
752 regcache_raw_supply (regcache, regnum, buf);
755 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
756 for all registers. */
759 inf_ptrace_fetch_registers (struct target_ops *ops,
760 struct regcache *regcache, int regnum)
764 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
766 inf_ptrace_fetch_register (regcache, regnum);
768 inf_ptrace_fetch_register (regcache, regnum);
771 /* Store register REGNUM into the inferior. */
774 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
776 struct gdbarch *gdbarch = get_regcache_arch (regcache);
779 PTRACE_TYPE_RET *buf;
782 /* This isn't really an address, but ptrace thinks of it as one. */
783 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
784 if (addr == (CORE_ADDR)-1
785 || gdbarch_cannot_store_register (gdbarch, regnum))
788 /* Cater for systems like GNU/Linux, that implement threads as
789 separate processes. */
790 pid = ptid_get_lwp (inferior_ptid);
792 pid = ptid_get_pid (inferior_ptid);
794 size = register_size (gdbarch, regnum);
795 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
798 /* Write the register contents into the inferior a chunk at a time. */
799 regcache_raw_collect (regcache, regnum, buf);
800 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
803 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
805 error (_("Couldn't write register %s (#%d): %s."),
806 gdbarch_register_name (gdbarch, regnum),
807 regnum, safe_strerror (errno));
809 addr += sizeof (PTRACE_TYPE_RET);
813 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
814 this for all registers. */
817 inf_ptrace_store_registers (struct target_ops *ops,
818 struct regcache *regcache, int regnum)
822 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
824 inf_ptrace_store_register (regcache, regnum);
826 inf_ptrace_store_register (regcache, regnum);
829 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
830 a function returning the offset within the user area where a
831 particular register is stored. */
834 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
835 (struct gdbarch *, int, int))
837 struct target_ops *t = inf_ptrace_target();
839 gdb_assert (register_u_offset);
840 inf_ptrace_register_u_offset = register_u_offset;
841 t->to_fetch_registers = inf_ptrace_fetch_registers;
842 t->to_store_registers = inf_ptrace_store_registers;