Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / inf-ptrace.c
blobb9767978744e0f4cac5edb42abf88d101ac9dbe0
1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2022 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/>. */
20 #include "defs.h"
21 #include "command.h"
22 #include "inferior.h"
23 #include "terminal.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "nat/gdb_ptrace.h"
27 #include "gdbsupport/gdb_wait.h"
28 #include <signal.h>
30 #include "inf-ptrace.h"
31 #include "inf-child.h"
32 #include "gdbthread.h"
33 #include "nat/fork-inferior.h"
34 #include "utils.h"
35 #include "gdbarch.h"
39 static PTRACE_TYPE_RET
40 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
41 PTRACE_TYPE_ARG4 data)
43 #ifdef __NetBSD__
44 return ptrace (request, ptid.pid (), addr, data);
45 #else
46 pid_t pid = get_ptrace_pid (ptid);
47 return ptrace (request, pid, addr, data);
48 #endif
51 /* The event pipe registered as a waitable file in the event loop. */
52 event_pipe inf_ptrace_target::m_event_pipe;
54 inf_ptrace_target::~inf_ptrace_target ()
59 /* Prepare to be traced. */
61 static void
62 inf_ptrace_me (void)
64 /* "Trace me, Dr. Memory!" */
65 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
66 trace_start_error_with_name ("ptrace");
69 /* Start a new inferior Unix child process. EXEC_FILE is the file to
70 run, ALLARGS is a string containing the arguments to the program.
71 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
72 chatty about it. */
74 void
75 inf_ptrace_target::create_inferior (const char *exec_file,
76 const std::string &allargs,
77 char **env, int from_tty)
79 inferior *inf = current_inferior ();
81 /* Do not change either targets above or the same target if already present.
82 The reason is the target stack is shared across multiple inferiors. */
83 int ops_already_pushed = inf->target_is_pushed (this);
85 target_unpush_up unpusher;
86 if (! ops_already_pushed)
88 /* Clear possible core file with its process_stratum. */
89 inf->push_target (this);
90 unpusher.reset (this);
93 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
94 NULL, NULL, NULL);
96 ptid_t ptid (pid);
97 /* We have something that executes now. We'll be running through
98 the shell at this point (if startup-with-shell is true), but the
99 pid shouldn't change. */
100 thread_info *thr = add_thread_silent (this, ptid);
101 switch_to_thread (thr);
103 unpusher.release ();
105 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
107 /* On some targets, there must be some explicit actions taken after
108 the inferior has been started up. */
109 post_startup_inferior (ptid);
112 /* Clean up a rotting corpse of an inferior after it died. */
114 void
115 inf_ptrace_target::mourn_inferior ()
117 int status;
119 /* Wait just one more time to collect the inferior's exit status.
120 Do not check whether this succeeds though, since we may be
121 dealing with a process that we attached to. Such a process will
122 only report its exit status to its original parent. */
123 waitpid (inferior_ptid.pid (), &status, 0);
125 inf_child_target::mourn_inferior ();
128 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
129 be chatty about it. */
131 void
132 inf_ptrace_target::attach (const char *args, int from_tty)
134 inferior *inf = current_inferior ();
136 /* Do not change either targets above or the same target if already present.
137 The reason is the target stack is shared across multiple inferiors. */
138 int ops_already_pushed = inf->target_is_pushed (this);
140 pid_t pid = parse_pid_to_attach (args);
142 if (pid == getpid ()) /* Trying to masturbate? */
143 error (_("I refuse to debug myself!"));
145 target_unpush_up unpusher;
146 if (! ops_already_pushed)
148 /* target_pid_to_str already uses the target. Also clear possible core
149 file with its process_stratum. */
150 inf->push_target (this);
151 unpusher.reset (this);
154 target_announce_attach (from_tty, pid);
156 #ifdef PT_ATTACH
157 errno = 0;
158 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
159 if (errno != 0)
160 perror_with_name (("ptrace"));
161 #else
162 error (_("This system does not support attaching to a process"));
163 #endif
165 inferior_appeared (inf, pid);
166 inf->attach_flag = 1;
168 /* Always add a main thread. If some target extends the ptrace
169 target, it should decorate the ptid later with more info. */
170 thread_info *thr = add_thread_silent (this, ptid_t (pid));
171 switch_to_thread (thr);
173 /* Don't consider the thread stopped until we've processed its
174 initial SIGSTOP stop. */
175 set_executing (this, thr->ptid, true);
177 unpusher.release ();
180 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
182 void
183 inf_ptrace_target::detach (inferior *inf, int from_tty)
185 pid_t pid = inferior_ptid.pid ();
187 target_announce_detach (from_tty);
189 #ifdef PT_DETACH
190 /* We'd better not have left any breakpoints in the program or it'll
191 die when it hits one. Also note that this may only work if we
192 previously attached to the inferior. It *might* work if we
193 started the process ourselves. */
194 errno = 0;
195 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
196 if (errno != 0)
197 perror_with_name (("ptrace"));
198 #else
199 error (_("This system does not support detaching from a process"));
200 #endif
202 detach_success (inf);
205 /* See inf-ptrace.h. */
207 void
208 inf_ptrace_target::detach_success (inferior *inf)
210 switch_to_no_thread ();
211 detach_inferior (inf);
213 maybe_unpush_target ();
216 /* Kill the inferior. */
218 void
219 inf_ptrace_target::kill ()
221 pid_t pid = inferior_ptid.pid ();
222 int status;
224 if (pid == 0)
225 return;
227 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
228 waitpid (pid, &status, 0);
230 target_mourn_inferior (inferior_ptid);
233 #ifndef __NetBSD__
235 /* See inf-ptrace.h. */
237 pid_t
238 get_ptrace_pid (ptid_t ptid)
240 pid_t pid;
242 /* If we have an LWPID to work with, use it. Otherwise, we're
243 dealing with a non-threaded program/target. */
244 pid = ptid.lwp ();
245 if (pid == 0)
246 pid = ptid.pid ();
247 return pid;
249 #endif
251 /* Resume execution of thread PTID, or all threads if PTID is -1. If
252 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
253 that signal. */
255 void
256 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
258 PTRACE_TYPE_ARG1 request;
260 if (minus_one_ptid == ptid)
261 /* Resume all threads. Traditionally ptrace() only supports
262 single-threaded processes, so simply resume the inferior. */
263 ptid = ptid_t (inferior_ptid.pid ());
265 if (catch_syscall_enabled () > 0)
266 request = PT_SYSCALL;
267 else
268 request = PT_CONTINUE;
270 if (step)
272 /* If this system does not support PT_STEP, a higher level
273 function will have called the appropriate functions to transmute the
274 step request into a continue request (by setting breakpoints on
275 all possible successor instructions), so we don't have to
276 worry about that here. */
277 request = PT_STEP;
280 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
281 where it was. If GDB wanted it to start some other way, we have
282 already written a new program counter value to the child. */
283 errno = 0;
284 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
285 if (errno != 0)
286 perror_with_name (("ptrace"));
289 /* Wait for the child specified by PTID to do something. Return the
290 process ID of the child, or MINUS_ONE_PTID in case of error; store
291 the status in *OURSTATUS. */
293 ptid_t
294 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
295 target_wait_flags target_options)
297 pid_t pid;
298 int options, status, save_errno;
300 options = 0;
301 if (target_options & TARGET_WNOHANG)
302 options |= WNOHANG;
306 set_sigint_trap ();
310 pid = waitpid (ptid.pid (), &status, options);
311 save_errno = errno;
313 while (pid == -1 && errno == EINTR);
315 clear_sigint_trap ();
317 if (pid == 0)
319 gdb_assert (target_options & TARGET_WNOHANG);
320 ourstatus->set_ignore ();
321 return minus_one_ptid;
324 if (pid == -1)
326 /* In async mode the SIGCHLD might have raced and triggered
327 a check for an event that had already been reported. If
328 the event was the exit of the only remaining child,
329 waitpid() will fail with ECHILD. */
330 if (ptid == minus_one_ptid && save_errno == ECHILD)
332 ourstatus->set_no_resumed ();
333 return minus_one_ptid;
336 gdb_printf (gdb_stderr,
337 _("Child process unexpectedly missing: %s.\n"),
338 safe_strerror (save_errno));
340 ourstatus->set_ignore ();
341 return minus_one_ptid;
344 /* Ignore terminated detached child processes. */
345 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
346 pid = -1;
348 while (pid == -1);
350 *ourstatus = host_status_to_waitstatus (status);
352 return ptid_t (pid);
355 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
356 from process PID's memory into READBUF. Start at target address ADDR
357 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
358 be non-null. Return the number of transferred bytes. */
360 static ULONGEST
361 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
362 const gdb_byte *writebuf,
363 ULONGEST addr, ULONGEST len)
365 ULONGEST n;
366 unsigned int chunk;
368 /* We transfer aligned words. Thus align ADDR down to a word
369 boundary and determine how many bytes to skip at the
370 beginning. */
371 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
372 addr -= skip;
374 for (n = 0;
375 n < len;
376 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
378 /* Restrict to a chunk that fits in the current word. */
379 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
381 /* Use a union for type punning. */
382 union
384 PTRACE_TYPE_RET word;
385 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
386 } buf;
388 /* Read the word, also when doing a partial word write. */
389 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
391 errno = 0;
392 buf.word = gdb_ptrace (PT_READ_I, ptid,
393 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
394 if (errno != 0)
395 break;
396 if (readbuf != NULL)
397 memcpy (readbuf + n, buf.byte + skip, chunk);
399 if (writebuf != NULL)
401 memcpy (buf.byte + skip, writebuf + n, chunk);
402 errno = 0;
403 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
404 buf.word);
405 if (errno != 0)
407 /* Using the appropriate one (I or D) is necessary for
408 Gould NP1, at least. */
409 errno = 0;
410 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
411 buf.word);
412 if (errno != 0)
413 break;
418 return n;
421 /* Implement the to_xfer_partial target_ops method. */
423 enum target_xfer_status
424 inf_ptrace_target::xfer_partial (enum target_object object,
425 const char *annex, gdb_byte *readbuf,
426 const gdb_byte *writebuf,
427 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
429 ptid_t ptid = inferior_ptid;
431 switch (object)
433 case TARGET_OBJECT_MEMORY:
434 #ifdef PT_IO
435 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
436 request that promises to be much more efficient in reading
437 and writing data in the traced process's address space. */
439 struct ptrace_io_desc piod;
441 /* NOTE: We assume that there are no distinct address spaces
442 for instruction and data. However, on OpenBSD 3.9 and
443 later, PIOD_WRITE_D doesn't allow changing memory that's
444 mapped read-only. Since most code segments will be
445 read-only, using PIOD_WRITE_D will prevent us from
446 inserting breakpoints, so we use PIOD_WRITE_I instead. */
447 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
448 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
449 piod.piod_offs = (void *) (long) offset;
450 piod.piod_len = len;
452 errno = 0;
453 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
455 /* Return the actual number of bytes read or written. */
456 *xfered_len = piod.piod_len;
457 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
459 /* If the PT_IO request is somehow not supported, fallback on
460 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
461 to indicate failure. */
462 if (errno != EINVAL)
463 return TARGET_XFER_EOF;
465 #endif
466 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
467 offset, len);
468 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
470 case TARGET_OBJECT_UNWIND_TABLE:
471 return TARGET_XFER_E_IO;
473 case TARGET_OBJECT_AUXV:
474 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
475 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
476 request that allows us to read the auxilliary vector. Other
477 BSD's may follow if they feel the need to support PIE. */
479 struct ptrace_io_desc piod;
481 if (writebuf)
482 return TARGET_XFER_E_IO;
483 piod.piod_op = PIOD_READ_AUXV;
484 piod.piod_addr = readbuf;
485 piod.piod_offs = (void *) (long) offset;
486 piod.piod_len = len;
488 errno = 0;
489 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
491 /* Return the actual number of bytes read or written. */
492 *xfered_len = piod.piod_len;
493 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
496 #endif
497 return TARGET_XFER_E_IO;
499 case TARGET_OBJECT_WCOOKIE:
500 return TARGET_XFER_E_IO;
502 default:
503 return TARGET_XFER_E_IO;
507 /* Return non-zero if the thread specified by PTID is alive. */
509 bool
510 inf_ptrace_target::thread_alive (ptid_t ptid)
512 /* ??? Is kill the right way to do this? */
513 return (::kill (ptid.pid (), 0) != -1);
516 /* Print status information about what we're accessing. */
518 void
519 inf_ptrace_target::files_info ()
521 struct inferior *inf = current_inferior ();
523 gdb_printf (_("\tUsing the running image of %s %s.\n"),
524 inf->attach_flag ? "attached" : "child",
525 target_pid_to_str (inferior_ptid).c_str ());
528 std::string
529 inf_ptrace_target::pid_to_str (ptid_t ptid)
531 return normal_pid_to_str (ptid);
534 /* Implement the "close" target method. */
536 void
537 inf_ptrace_target::close ()
539 /* Unregister from the event loop. */
540 if (is_async_p ())
541 async (0);
543 inf_child_target::close ();