1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2020 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 "gdbsupport/gdb_wait.h"
31 #include "inf-ptrace.h"
32 #include "inf-child.h"
33 #include "gdbthread.h"
34 #include "nat/fork-inferior.h"
40 static PTRACE_TYPE_RET
41 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
42 PTRACE_TYPE_ARG4 data)
45 return ptrace (request, ptid.pid (), addr, data);
47 pid_t pid = get_ptrace_pid (ptid);
48 return ptrace (request, pid, addr, data);
52 /* A unique_ptr helper to unpush a target. */
54 struct target_unpusher
56 void operator() (struct target_ops *ops) const
62 /* A unique_ptr that unpushes a target on destruction. */
64 typedef std::unique_ptr<struct target_ops, target_unpusher> target_unpush_up;
68 inf_ptrace_target::~inf_ptrace_target ()
73 /* Prepare to be traced. */
78 /* "Trace me, Dr. Memory!" */
79 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
80 trace_start_error_with_name ("ptrace");
83 /* Start a new inferior Unix child process. EXEC_FILE is the file to
84 run, ALLARGS is a string containing the arguments to the program.
85 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
89 inf_ptrace_target::create_inferior (const char *exec_file,
90 const std::string &allargs,
91 char **env, int from_tty)
93 /* Do not change either targets above or the same target if already present.
94 The reason is the target stack is shared across multiple inferiors. */
95 int ops_already_pushed = target_is_pushed (this);
97 target_unpush_up unpusher;
98 if (! ops_already_pushed)
100 /* Clear possible core file with its process_stratum. */
102 unpusher.reset (this);
105 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
109 /* We have something that executes now. We'll be running through
110 the shell at this point (if startup-with-shell is true), but the
111 pid shouldn't change. */
112 thread_info *thr = add_thread_silent (this, ptid);
113 switch_to_thread (thr);
117 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
119 /* On some targets, there must be some explicit actions taken after
120 the inferior has been started up. */
121 target_post_startup_inferior (ptid);
124 /* Clean up a rotting corpse of an inferior after it died. */
127 inf_ptrace_target::mourn_inferior ()
131 /* Wait just one more time to collect the inferior's exit status.
132 Do not check whether this succeeds though, since we may be
133 dealing with a process that we attached to. Such a process will
134 only report its exit status to its original parent. */
135 waitpid (inferior_ptid.pid (), &status, 0);
137 inf_child_target::mourn_inferior ();
140 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
141 be chatty about it. */
144 inf_ptrace_target::attach (const char *args, int from_tty)
147 struct inferior *inf;
149 /* Do not change either targets above or the same target if already present.
150 The reason is the target stack is shared across multiple inferiors. */
151 int ops_already_pushed = target_is_pushed (this);
153 pid = parse_pid_to_attach (args);
155 if (pid == getpid ()) /* Trying to masturbate? */
156 error (_("I refuse to debug myself!"));
158 target_unpush_up unpusher;
159 if (! ops_already_pushed)
161 /* target_pid_to_str already uses the target. Also clear possible core
162 file with its process_stratum. */
164 unpusher.reset (this);
169 const char *exec_file = get_exec_file (0);
172 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
173 target_pid_to_str (ptid_t (pid)).c_str ());
175 printf_unfiltered (_("Attaching to %s\n"),
176 target_pid_to_str (ptid_t (pid)).c_str ());
181 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
183 perror_with_name (("ptrace"));
185 error (_("This system does not support attaching to a process"));
188 inf = current_inferior ();
189 inferior_appeared (inf, pid);
190 inf->attach_flag = 1;
192 /* Always add a main thread. If some target extends the ptrace
193 target, it should decorate the ptid later with more info. */
194 thread_info *thr = add_thread_silent (this, ptid_t (pid));
195 switch_to_thread (thr);
197 /* Don't consider the thread stopped until we've processed its
198 initial SIGSTOP stop. */
199 set_executing (this, thr->ptid, true);
204 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
207 inf_ptrace_target::detach (inferior *inf, int from_tty)
209 pid_t pid = inferior_ptid.pid ();
211 target_announce_detach (from_tty);
214 /* We'd better not have left any breakpoints in the program or it'll
215 die when it hits one. Also note that this may only work if we
216 previously attached to the inferior. It *might* work if we
217 started the process ourselves. */
219 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
221 perror_with_name (("ptrace"));
223 error (_("This system does not support detaching from a process"));
226 detach_success (inf);
229 /* See inf-ptrace.h. */
232 inf_ptrace_target::detach_success (inferior *inf)
234 switch_to_no_thread ();
235 detach_inferior (inf);
237 maybe_unpush_target ();
240 /* Kill the inferior. */
243 inf_ptrace_target::kill ()
245 pid_t pid = inferior_ptid.pid ();
251 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
252 waitpid (pid, &status, 0);
254 target_mourn_inferior (inferior_ptid);
259 /* See inf-ptrace.h. */
262 get_ptrace_pid (ptid_t ptid)
266 /* If we have an LWPID to work with, use it. Otherwise, we're
267 dealing with a non-threaded program/target. */
275 /* Resume execution of thread PTID, or all threads if PTID is -1. If
276 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
280 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
282 PTRACE_TYPE_ARG1 request;
284 if (minus_one_ptid == ptid)
285 /* Resume all threads. Traditionally ptrace() only supports
286 single-threaded processes, so simply resume the inferior. */
287 ptid = ptid_t (inferior_ptid.pid ());
289 if (catch_syscall_enabled () > 0)
290 request = PT_SYSCALL;
292 request = PT_CONTINUE;
296 /* If this system does not support PT_STEP, a higher level
297 function will have called the appropriate functions to transmute the
298 step request into a continue request (by setting breakpoints on
299 all possible successor instructions), so we don't have to
300 worry about that here. */
304 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
305 where it was. If GDB wanted it to start some other way, we have
306 already written a new program counter value to the child. */
308 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
310 perror_with_name (("ptrace"));
313 /* Wait for the child specified by PTID to do something. Return the
314 process ID of the child, or MINUS_ONE_PTID in case of error; store
315 the status in *OURSTATUS. */
318 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
319 target_wait_flags options)
322 int status, save_errno;
330 pid = waitpid (ptid.pid (), &status, 0);
333 while (pid == -1 && errno == EINTR);
335 clear_sigint_trap ();
339 fprintf_unfiltered (gdb_stderr,
340 _("Child process unexpectedly missing: %s.\n"),
341 safe_strerror (save_errno));
343 /* Claim it exited with unknown signal. */
344 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
345 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
346 return inferior_ptid;
349 /* Ignore terminated detached child processes. */
350 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
355 store_waitstatus (ourstatus, status);
359 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
360 from process PID's memory into READBUF. Start at target address ADDR
361 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
362 be non-null. Return the number of transferred bytes. */
365 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
366 const gdb_byte *writebuf,
367 ULONGEST addr, ULONGEST len)
372 /* We transfer aligned words. Thus align ADDR down to a word
373 boundary and determine how many bytes to skip at the
375 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
380 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
382 /* Restrict to a chunk that fits in the current word. */
383 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
385 /* Use a union for type punning. */
388 PTRACE_TYPE_RET word;
389 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
392 /* Read the word, also when doing a partial word write. */
393 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
396 buf.word = gdb_ptrace (PT_READ_I, ptid,
397 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
401 memcpy (readbuf + n, buf.byte + skip, chunk);
403 if (writebuf != NULL)
405 memcpy (buf.byte + skip, writebuf + n, chunk);
407 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
411 /* Using the appropriate one (I or D) is necessary for
412 Gould NP1, at least. */
414 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
425 /* Implement the to_xfer_partial target_ops method. */
427 enum target_xfer_status
428 inf_ptrace_target::xfer_partial (enum target_object object,
429 const char *annex, gdb_byte *readbuf,
430 const gdb_byte *writebuf,
431 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
433 ptid_t ptid = inferior_ptid;
437 case TARGET_OBJECT_MEMORY:
439 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
440 request that promises to be much more efficient in reading
441 and writing data in the traced process's address space. */
443 struct ptrace_io_desc piod;
445 /* NOTE: We assume that there are no distinct address spaces
446 for instruction and data. However, on OpenBSD 3.9 and
447 later, PIOD_WRITE_D doesn't allow changing memory that's
448 mapped read-only. Since most code segments will be
449 read-only, using PIOD_WRITE_D will prevent us from
450 inserting breakpoints, so we use PIOD_WRITE_I instead. */
451 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
452 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
453 piod.piod_offs = (void *) (long) offset;
457 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
459 /* Return the actual number of bytes read or written. */
460 *xfered_len = piod.piod_len;
461 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
463 /* If the PT_IO request is somehow not supported, fallback on
464 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
465 to indicate failure. */
467 return TARGET_XFER_EOF;
470 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
472 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
474 case TARGET_OBJECT_UNWIND_TABLE:
475 return TARGET_XFER_E_IO;
477 case TARGET_OBJECT_AUXV:
478 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
479 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
480 request that allows us to read the auxilliary vector. Other
481 BSD's may follow if they feel the need to support PIE. */
483 struct ptrace_io_desc piod;
486 return TARGET_XFER_E_IO;
487 piod.piod_op = PIOD_READ_AUXV;
488 piod.piod_addr = readbuf;
489 piod.piod_offs = (void *) (long) offset;
493 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
495 /* Return the actual number of bytes read or written. */
496 *xfered_len = piod.piod_len;
497 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
501 return TARGET_XFER_E_IO;
503 case TARGET_OBJECT_WCOOKIE:
504 return TARGET_XFER_E_IO;
507 return TARGET_XFER_E_IO;
511 /* Return non-zero if the thread specified by PTID is alive. */
514 inf_ptrace_target::thread_alive (ptid_t ptid)
516 /* ??? Is kill the right way to do this? */
517 return (::kill (ptid.pid (), 0) != -1);
520 /* Print status information about what we're accessing. */
523 inf_ptrace_target::files_info ()
525 struct inferior *inf = current_inferior ();
527 printf_filtered (_("\tUsing the running image of %s %s.\n"),
528 inf->attach_flag ? "attached" : "child",
529 target_pid_to_str (inferior_ptid).c_str ());
533 inf_ptrace_target::pid_to_str (ptid_t ptid)
535 return normal_pid_to_str (ptid);