1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2014 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 "gdb_ptrace.h"
31 #include "inf-ptrace.h"
32 #include "inf-child.h"
33 #include "gdbthread.h"
37 #ifdef PT_GET_PROCESS_STATE
39 /* Target hook for follow_fork. On entry and at return inferior_ptid is
40 the ptid of the followed inferior. */
43 inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
48 pid_t child_pid = inferior_thread->pending_follow.value.related_pid;
50 /* Breakpoints have already been detached from the child by
53 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
54 perror_with_name (("ptrace"));
60 #endif /* PT_GET_PROCESS_STATE */
63 /* Prepare to be traced. */
68 /* "Trace me, Dr. Memory!" */
69 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
72 /* Start a new inferior Unix child process. EXEC_FILE is the file to
73 run, ALLARGS is a string containing the arguments to the program.
74 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
78 inf_ptrace_create_inferior (struct target_ops *ops,
79 char *exec_file, char *allargs, char **env,
84 /* Do not change either targets above or the same target if already present.
85 The reason is the target stack is shared across multiple inferiors. */
86 int ops_already_pushed = target_is_pushed (ops);
87 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
89 if (! ops_already_pushed)
91 /* Clear possible core file with its process_stratum. */
93 make_cleanup_unpush_target (ops);
96 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
99 discard_cleanups (back_to);
101 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
103 /* On some targets, there must be some explicit actions taken after
104 the inferior has been started up. */
105 target_post_startup_inferior (pid_to_ptid (pid));
108 #ifdef PT_GET_PROCESS_STATE
111 inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
115 /* Set the initial event mask. */
116 memset (&pe, 0, sizeof pe);
117 pe.pe_set_event |= PTRACE_FORK;
118 if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
119 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
120 perror_with_name (("ptrace"));
125 /* Clean up a rotting corpse of an inferior after it died. */
128 inf_ptrace_mourn_inferior (struct target_ops *ops)
132 /* Wait just one more time to collect the inferior's exit status.
133 Do not check whether this succeeds though, since we may be
134 dealing with a process that we attached to. Such a process will
135 only report its exit status to its original parent. */
136 waitpid (ptid_get_pid (inferior_ptid), &status, 0);
138 inf_child_mourn_inferior (ops);
141 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
142 be chatty about it. */
145 inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
149 struct inferior *inf;
151 /* Do not change either targets above or the same target if already present.
152 The reason is the target stack is shared across multiple inferiors. */
153 int ops_already_pushed = target_is_pushed (ops);
154 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
156 pid = parse_pid_to_attach (args);
158 if (pid == getpid ()) /* Trying to masturbate? */
159 error (_("I refuse to debug myself!"));
161 if (! ops_already_pushed)
163 /* target_pid_to_str already uses the target. Also clear possible core
164 file with its process_stratum. */
166 make_cleanup_unpush_target (ops);
171 exec_file = get_exec_file (0);
174 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
175 target_pid_to_str (pid_to_ptid (pid)));
177 printf_unfiltered (_("Attaching to %s\n"),
178 target_pid_to_str (pid_to_ptid (pid)));
180 gdb_flush (gdb_stdout);
185 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
187 perror_with_name (("ptrace"));
189 error (_("This system does not support attaching to a process"));
192 inf = current_inferior ();
193 inferior_appeared (inf, pid);
194 inf->attach_flag = 1;
195 inferior_ptid = pid_to_ptid (pid);
197 /* Always add a main thread. If some target extends the ptrace
198 target, it should decorate the ptid later with more info. */
199 add_thread_silent (inferior_ptid);
201 discard_cleanups (back_to);
204 #ifdef PT_GET_PROCESS_STATE
207 inf_ptrace_post_attach (struct target_ops *self, int pid)
211 /* Set the initial event mask. */
212 memset (&pe, 0, sizeof pe);
213 pe.pe_set_event |= PTRACE_FORK;
214 if (ptrace (PT_SET_EVENT_MASK, pid,
215 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
216 perror_with_name (("ptrace"));
221 /* Detach from the inferior, optionally passing it the signal
222 specified by ARGS. If FROM_TTY is non-zero, be chatty about it. */
225 inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
227 pid_t pid = ptid_get_pid (inferior_ptid);
232 char *exec_file = get_exec_file (0);
235 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
236 target_pid_to_str (pid_to_ptid (pid)));
237 gdb_flush (gdb_stdout);
243 /* We'd better not have left any breakpoints in the program or it'll
244 die when it hits one. Also note that this may only work if we
245 previously attached to the inferior. It *might* work if we
246 started the process ourselves. */
248 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
250 perror_with_name (("ptrace"));
252 error (_("This system does not support detaching from a process"));
255 inferior_ptid = null_ptid;
256 detach_inferior (pid);
258 inf_child_maybe_unpush_target (ops);
261 /* Kill the inferior. */
264 inf_ptrace_kill (struct target_ops *ops)
266 pid_t pid = ptid_get_pid (inferior_ptid);
272 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
273 waitpid (pid, &status, 0);
275 target_mourn_inferior ();
278 /* Stop the inferior. */
281 inf_ptrace_stop (struct target_ops *self, ptid_t ptid)
283 /* Send a SIGINT to the process group. This acts just like the user
284 typed a ^C on the controlling terminal. Note that using a
285 negative process number in kill() is a System V-ism. The proper
286 BSD interface is killpg(). However, all modern BSDs support the
287 System V interface too. */
288 kill (-inferior_process_group (), SIGINT);
291 /* Resume execution of thread PTID, or all threads if PTID is -1. If
292 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
296 inf_ptrace_resume (struct target_ops *ops,
297 ptid_t ptid, int step, enum gdb_signal signal)
299 pid_t pid = ptid_get_pid (ptid);
303 /* Resume all threads. Traditionally ptrace() only supports
304 single-threaded processes, so simply resume the inferior. */
305 pid = ptid_get_pid (inferior_ptid);
307 if (catch_syscall_enabled () > 0)
308 request = PT_SYSCALL;
310 request = PT_CONTINUE;
314 /* If this system does not support PT_STEP, a higher level
315 function will have called single_step() to transmute the step
316 request into a continue request (by setting breakpoints on
317 all possible successor instructions), so we don't have to
318 worry about that here. */
322 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
323 where it was. If GDB wanted it to start some other way, we have
324 already written a new program counter value to the child. */
326 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
328 perror_with_name (("ptrace"));
331 /* Wait for the child specified by PTID to do something. Return the
332 process ID of the child, or MINUS_ONE_PTID in case of error; store
333 the status in *OURSTATUS. */
336 inf_ptrace_wait (struct target_ops *ops,
337 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
340 int status, save_errno;
348 pid = waitpid (ptid_get_pid (ptid), &status, 0);
351 while (pid == -1 && errno == EINTR);
353 clear_sigint_trap ();
357 fprintf_unfiltered (gdb_stderr,
358 _("Child process unexpectedly missing: %s.\n"),
359 safe_strerror (save_errno));
361 /* Claim it exited with unknown signal. */
362 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
363 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
364 return inferior_ptid;
367 /* Ignore terminated detached child processes. */
368 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
373 #ifdef PT_GET_PROCESS_STATE
374 if (WIFSTOPPED (status))
379 if (ptrace (PT_GET_PROCESS_STATE, pid,
380 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
381 perror_with_name (("ptrace"));
383 switch (pe.pe_report_event)
386 ourstatus->kind = TARGET_WAITKIND_FORKED;
387 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
389 /* Make sure the other end of the fork is stopped too. */
390 fpid = waitpid (pe.pe_other_pid, &status, 0);
392 perror_with_name (("waitpid"));
394 if (ptrace (PT_GET_PROCESS_STATE, fpid,
395 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
396 perror_with_name (("ptrace"));
398 gdb_assert (pe.pe_report_event == PTRACE_FORK);
399 gdb_assert (pe.pe_other_pid == pid);
400 if (fpid == ptid_get_pid (inferior_ptid))
402 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
403 return pid_to_ptid (fpid);
406 return pid_to_ptid (pid);
411 store_waitstatus (ourstatus, status);
412 return pid_to_ptid (pid);
415 /* Implement the to_xfer_partial target_ops method. */
417 static enum target_xfer_status
418 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
419 const char *annex, gdb_byte *readbuf,
420 const gdb_byte *writebuf,
421 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
423 pid_t pid = ptid_get_pid (inferior_ptid);
427 case TARGET_OBJECT_MEMORY:
429 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
430 request that promises to be much more efficient in reading
431 and writing data in the traced process's address space. */
433 struct ptrace_io_desc piod;
435 /* NOTE: We assume that there are no distinct address spaces
436 for instruction and data. However, on OpenBSD 3.9 and
437 later, PIOD_WRITE_D doesn't allow changing memory that's
438 mapped read-only. Since most code segments will be
439 read-only, using PIOD_WRITE_D will prevent us from
440 inserting breakpoints, so we use PIOD_WRITE_I instead. */
441 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
442 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
443 piod.piod_offs = (void *) (long) offset;
447 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
449 /* Return the actual number of bytes read or written. */
450 *xfered_len = piod.piod_len;
451 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
453 /* If the PT_IO request is somehow not supported, fallback on
454 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
455 to indicate failure. */
457 return TARGET_XFER_EOF;
463 PTRACE_TYPE_RET word;
464 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
466 ULONGEST rounded_offset;
467 ULONGEST partial_len;
469 /* Round the start offset down to the next long word
471 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
473 /* Since ptrace will transfer a single word starting at that
474 rounded_offset the partial_len needs to be adjusted down to
475 that (remember this function only does a single transfer).
476 Should the required length be even less, adjust it down
478 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
479 if (partial_len > len)
484 /* If OFFSET:PARTIAL_LEN is smaller than
485 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
486 be needed. Read in the entire word. */
487 if (rounded_offset < offset
488 || (offset + partial_len
489 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
490 /* Need part of initial word -- fetch it. */
491 buffer.word = ptrace (PT_READ_I, pid,
492 (PTRACE_TYPE_ARG3)(uintptr_t)
495 /* Copy data to be written over corresponding part of
497 memcpy (buffer.byte + (offset - rounded_offset),
498 writebuf, partial_len);
501 ptrace (PT_WRITE_D, pid,
502 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
506 /* Using the appropriate one (I or D) is necessary for
507 Gould NP1, at least. */
509 ptrace (PT_WRITE_I, pid,
510 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
513 return TARGET_XFER_EOF;
520 buffer.word = ptrace (PT_READ_I, pid,
521 (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
524 return TARGET_XFER_EOF;
525 /* Copy appropriate bytes out of the buffer. */
526 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
530 *xfered_len = partial_len;
531 return TARGET_XFER_OK;
534 case TARGET_OBJECT_UNWIND_TABLE:
535 return TARGET_XFER_E_IO;
537 case TARGET_OBJECT_AUXV:
538 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
539 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
540 request that allows us to read the auxilliary vector. Other
541 BSD's may follow if they feel the need to support PIE. */
543 struct ptrace_io_desc piod;
546 return TARGET_XFER_E_IO;
547 piod.piod_op = PIOD_READ_AUXV;
548 piod.piod_addr = readbuf;
549 piod.piod_offs = (void *) (long) offset;
553 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
555 /* Return the actual number of bytes read or written. */
556 *xfered_len = piod.piod_len;
557 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
561 return TARGET_XFER_E_IO;
563 case TARGET_OBJECT_WCOOKIE:
564 return TARGET_XFER_E_IO;
567 return TARGET_XFER_E_IO;
571 /* Return non-zero if the thread specified by PTID is alive. */
574 inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
576 /* ??? Is kill the right way to do this? */
577 return (kill (ptid_get_pid (ptid), 0) != -1);
580 /* Print status information about what we're accessing. */
583 inf_ptrace_files_info (struct target_ops *ignore)
585 struct inferior *inf = current_inferior ();
587 printf_filtered (_("\tUsing the running image of %s %s.\n"),
588 inf->attach_flag ? "attached" : "child",
589 target_pid_to_str (inferior_ptid));
593 inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
595 return normal_pid_to_str (ptid);
598 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
600 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
601 Return 0 if *READPTR is already at the end of the buffer.
602 Return -1 if there is insufficient buffer for a whole entry.
603 Return 1 if an entry was read into *TYPEP and *VALP. */
606 inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
607 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
609 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
610 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
611 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
612 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
613 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
614 gdb_byte *ptr = *readptr;
619 if (endptr - ptr < 2 * sizeof_auxv_val)
622 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
623 ptr += sizeof_auxv_val; /* Alignment. */
624 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
625 ptr += sizeof_auxv_val;
633 /* Create a prototype ptrace target. The client can override it with
637 inf_ptrace_target (void)
639 struct target_ops *t = inf_child_target ();
641 t->to_attach = inf_ptrace_attach;
642 t->to_detach = inf_ptrace_detach;
643 t->to_resume = inf_ptrace_resume;
644 t->to_wait = inf_ptrace_wait;
645 t->to_files_info = inf_ptrace_files_info;
646 t->to_kill = inf_ptrace_kill;
647 t->to_create_inferior = inf_ptrace_create_inferior;
648 #ifdef PT_GET_PROCESS_STATE
649 t->to_follow_fork = inf_ptrace_follow_fork;
650 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
651 t->to_post_attach = inf_ptrace_post_attach;
653 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
654 t->to_thread_alive = inf_ptrace_thread_alive;
655 t->to_pid_to_str = inf_ptrace_pid_to_str;
656 t->to_stop = inf_ptrace_stop;
657 t->to_xfer_partial = inf_ptrace_xfer_partial;
658 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
659 t->to_auxv_parse = inf_ptrace_auxv_parse;
666 /* Pointer to a function that returns the offset within the user area
667 where a particular register is stored. */
668 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
670 /* Fetch register REGNUM from the inferior. */
673 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
675 struct gdbarch *gdbarch = get_regcache_arch (regcache);
678 PTRACE_TYPE_RET *buf;
681 /* This isn't really an address, but ptrace thinks of it as one. */
682 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
683 if (addr == (CORE_ADDR)-1
684 || gdbarch_cannot_fetch_register (gdbarch, regnum))
686 regcache_raw_supply (regcache, regnum, NULL);
690 /* Cater for systems like GNU/Linux, that implement threads as
691 separate processes. */
692 pid = ptid_get_lwp (inferior_ptid);
694 pid = ptid_get_pid (inferior_ptid);
696 size = register_size (gdbarch, regnum);
697 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
700 /* Read the register contents from the inferior a chunk at a time. */
701 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
704 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
706 error (_("Couldn't read register %s (#%d): %s."),
707 gdbarch_register_name (gdbarch, regnum),
708 regnum, safe_strerror (errno));
710 addr += sizeof (PTRACE_TYPE_RET);
712 regcache_raw_supply (regcache, regnum, buf);
715 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
716 for all registers. */
719 inf_ptrace_fetch_registers (struct target_ops *ops,
720 struct regcache *regcache, int regnum)
724 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
726 inf_ptrace_fetch_register (regcache, regnum);
728 inf_ptrace_fetch_register (regcache, regnum);
731 /* Store register REGNUM into the inferior. */
734 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
736 struct gdbarch *gdbarch = get_regcache_arch (regcache);
739 PTRACE_TYPE_RET *buf;
742 /* This isn't really an address, but ptrace thinks of it as one. */
743 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
744 if (addr == (CORE_ADDR)-1
745 || gdbarch_cannot_store_register (gdbarch, regnum))
748 /* Cater for systems like GNU/Linux, that implement threads as
749 separate processes. */
750 pid = ptid_get_lwp (inferior_ptid);
752 pid = ptid_get_pid (inferior_ptid);
754 size = register_size (gdbarch, regnum);
755 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
758 /* Write the register contents into the inferior a chunk at a time. */
759 regcache_raw_collect (regcache, regnum, buf);
760 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
763 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
765 error (_("Couldn't write register %s (#%d): %s."),
766 gdbarch_register_name (gdbarch, regnum),
767 regnum, safe_strerror (errno));
769 addr += sizeof (PTRACE_TYPE_RET);
773 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
774 this for all registers. */
777 inf_ptrace_store_registers (struct target_ops *ops,
778 struct regcache *regcache, int regnum)
782 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
784 inf_ptrace_store_register (regcache, regnum);
786 inf_ptrace_store_register (regcache, regnum);
789 /* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
790 a function returning the offset within the user area where a
791 particular register is stored. */
794 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
795 (struct gdbarch *, int, int))
797 struct target_ops *t = inf_ptrace_target();
799 gdb_assert (register_u_offset);
800 inf_ptrace_register_u_offset = register_u_offset;
801 t->to_fetch_registers = inf_ptrace_fetch_registers;
802 t->to_store_registers = inf_ptrace_store_registers;