Automatic date update in version.in
[binutils-gdb.git] / gdb / inf-ptrace.c
blobce303eb87eaf2d56ab28c59afb1dcbd0715cd2b6
1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2024 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 "command.h"
21 #include "inferior.h"
22 #include "terminal.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "nat/gdb_ptrace.h"
26 #include "gdbsupport/gdb_wait.h"
27 #include <signal.h>
29 #include "inf-ptrace.h"
30 #include "inf-child.h"
31 #include "gdbthread.h"
32 #include "nat/fork-inferior.h"
33 #include "utils.h"
34 #include "gdbarch.h"
38 static PTRACE_TYPE_RET
39 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
40 PTRACE_TYPE_ARG4 data)
42 #ifdef __NetBSD__
43 return ptrace (request, ptid.pid (), addr, data);
44 #else
45 pid_t pid = get_ptrace_pid (ptid);
46 return ptrace (request, pid, addr, data);
47 #endif
50 /* The event pipe registered as a waitable file in the event loop. */
51 event_pipe inf_ptrace_target::m_event_pipe;
53 inf_ptrace_target::~inf_ptrace_target ()
58 /* Prepare to be traced. */
60 static void
61 inf_ptrace_me (void)
63 /* "Trace me, Dr. Memory!" */
64 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
65 trace_start_error_with_name ("ptrace");
68 /* Start a new inferior Unix child process. EXEC_FILE is the file to
69 run, ALLARGS is a string containing the arguments to the program.
70 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
71 chatty about it. */
73 void
74 inf_ptrace_target::create_inferior (const char *exec_file,
75 const std::string &allargs,
76 char **env, int from_tty)
78 inferior *inf = current_inferior ();
80 /* Do not change either targets above or the same target if already present.
81 The reason is the target stack is shared across multiple inferiors. */
82 int ops_already_pushed = inf->target_is_pushed (this);
84 target_unpush_up unpusher;
85 if (! ops_already_pushed)
87 /* Clear possible core file with its process_stratum. */
88 inf->push_target (this);
89 unpusher.reset (this);
92 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
93 NULL, NULL, NULL);
95 ptid_t ptid (pid);
96 /* We have something that executes now. We'll be running through
97 the shell at this point (if startup-with-shell is true), but the
98 pid shouldn't change. */
99 thread_info *thr = add_thread_silent (this, ptid);
100 switch_to_thread (thr);
102 unpusher.release ();
104 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
106 /* On some targets, there must be some explicit actions taken after
107 the inferior has been started up. */
108 post_startup_inferior (ptid);
111 /* Clean up a rotting corpse of an inferior after it died. */
113 void
114 inf_ptrace_target::mourn_inferior ()
116 int status;
118 /* Wait just one more time to collect the inferior's exit status.
119 Do not check whether this succeeds though, since we may be
120 dealing with a process that we attached to. Such a process will
121 only report its exit status to its original parent. */
122 waitpid (inferior_ptid.pid (), &status, 0);
124 inf_child_target::mourn_inferior ();
127 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
128 be chatty about it. */
130 void
131 inf_ptrace_target::attach (const char *args, int from_tty)
133 inferior *inf = current_inferior ();
135 /* Do not change either targets above or the same target if already present.
136 The reason is the target stack is shared across multiple inferiors. */
137 int ops_already_pushed = inf->target_is_pushed (this);
139 pid_t pid = parse_pid_to_attach (args);
141 if (pid == getpid ()) /* Trying to masturbate? */
142 error (_("I refuse to debug myself!"));
144 target_unpush_up unpusher;
145 if (! ops_already_pushed)
147 /* target_pid_to_str already uses the target. Also clear possible core
148 file with its process_stratum. */
149 inf->push_target (this);
150 unpusher.reset (this);
153 target_announce_attach (from_tty, pid);
155 #ifdef PT_ATTACH
156 errno = 0;
157 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
158 if (errno != 0)
159 perror_with_name (("ptrace"));
160 #else
161 error (_("This system does not support attaching to a process"));
162 #endif
164 inferior_appeared (inf, pid);
165 inf->attach_flag = true;
167 /* Always add a main thread. If some target extends the ptrace
168 target, it should decorate the ptid later with more info. */
169 thread_info *thr = add_thread_silent (this, ptid_t (pid));
170 switch_to_thread (thr);
172 /* Don't consider the thread stopped until we've processed its
173 initial SIGSTOP stop. */
174 set_executing (this, thr->ptid, true);
176 unpusher.release ();
179 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
181 void
182 inf_ptrace_target::detach (inferior *inf, int from_tty)
184 pid_t pid = inferior_ptid.pid ();
186 target_announce_detach (from_tty);
188 #ifdef PT_DETACH
189 /* We'd better not have left any breakpoints in the program or it'll
190 die when it hits one. Also note that this may only work if we
191 previously attached to the inferior. It *might* work if we
192 started the process ourselves. */
193 errno = 0;
194 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
195 if (errno != 0)
196 perror_with_name (("ptrace"));
197 #else
198 error (_("This system does not support detaching from a process"));
199 #endif
201 detach_success (inf);
204 /* See inf-ptrace.h. */
206 void
207 inf_ptrace_target::detach_success (inferior *inf)
209 switch_to_no_thread ();
210 detach_inferior (inf);
212 maybe_unpush_target ();
215 /* Kill the inferior. */
217 void
218 inf_ptrace_target::kill ()
220 pid_t pid = inferior_ptid.pid ();
221 int status;
223 if (pid == 0)
224 return;
226 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
227 waitpid (pid, &status, 0);
229 target_mourn_inferior (inferior_ptid);
232 #ifndef __NetBSD__
234 /* See inf-ptrace.h. */
236 pid_t
237 get_ptrace_pid (ptid_t ptid)
239 pid_t pid;
241 /* If we have an LWPID to work with, use it. Otherwise, we're
242 dealing with a non-threaded program/target. */
243 pid = ptid.lwp ();
244 if (pid == 0)
245 pid = ptid.pid ();
246 return pid;
248 #endif
250 /* Resume execution of thread PTID, or all threads if PTID is -1. If
251 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
252 that signal. */
254 void
255 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
257 PTRACE_TYPE_ARG1 request;
259 if (minus_one_ptid == ptid)
260 /* Resume all threads. Traditionally ptrace() only supports
261 single-threaded processes, so simply resume the inferior. */
262 ptid = ptid_t (inferior_ptid.pid ());
264 if (catch_syscall_enabled ())
265 request = PT_SYSCALL;
266 else
267 request = PT_CONTINUE;
269 if (step)
271 /* If this system does not support PT_STEP, a higher level
272 function will have called the appropriate functions to transmute the
273 step request into a continue request (by setting breakpoints on
274 all possible successor instructions), so we don't have to
275 worry about that here. */
276 request = PT_STEP;
279 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
280 where it was. If GDB wanted it to start some other way, we have
281 already written a new program counter value to the child. */
282 errno = 0;
283 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
284 if (errno != 0)
285 perror_with_name (("ptrace"));
288 /* Wait for the child specified by PTID to do something. Return the
289 process ID of the child, or MINUS_ONE_PTID in case of error; store
290 the status in *OURSTATUS. */
292 ptid_t
293 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
294 target_wait_flags target_options)
296 pid_t pid;
297 int options, status, save_errno;
299 options = 0;
300 if (target_options & TARGET_WNOHANG)
301 options |= WNOHANG;
305 set_sigint_trap ();
309 pid = waitpid (ptid.pid (), &status, options);
310 save_errno = errno;
312 while (pid == -1 && errno == EINTR);
314 clear_sigint_trap ();
316 if (pid == 0)
318 gdb_assert (target_options & TARGET_WNOHANG);
319 ourstatus->set_ignore ();
320 return minus_one_ptid;
323 if (pid == -1)
325 /* In async mode the SIGCHLD might have raced and triggered
326 a check for an event that had already been reported. If
327 the event was the exit of the only remaining child,
328 waitpid() will fail with ECHILD. */
329 if (ptid == minus_one_ptid && save_errno == ECHILD)
331 ourstatus->set_no_resumed ();
332 return minus_one_ptid;
335 gdb_printf (gdb_stderr,
336 _("Child process unexpectedly missing: %s.\n"),
337 safe_strerror (save_errno));
339 ourstatus->set_ignore ();
340 return minus_one_ptid;
343 /* Ignore terminated detached child processes. */
344 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
345 pid = -1;
347 while (pid == -1);
349 *ourstatus = host_status_to_waitstatus (status);
351 return ptid_t (pid);
354 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
355 from process PID's memory into READBUF. Start at target address ADDR
356 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
357 be non-null. Return the number of transferred bytes. */
359 static ULONGEST
360 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
361 const gdb_byte *writebuf,
362 ULONGEST addr, ULONGEST len)
364 ULONGEST n;
365 unsigned int chunk;
367 /* We transfer aligned words. Thus align ADDR down to a word
368 boundary and determine how many bytes to skip at the
369 beginning. */
370 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
371 addr -= skip;
373 for (n = 0;
374 n < len;
375 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
377 /* Restrict to a chunk that fits in the current word. */
378 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
380 /* Use a union for type punning. */
381 union
383 PTRACE_TYPE_RET word;
384 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
385 } buf;
387 /* Read the word, also when doing a partial word write. */
388 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
390 errno = 0;
391 buf.word = gdb_ptrace (PT_READ_I, ptid,
392 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
393 if (errno != 0)
394 break;
395 if (readbuf != NULL)
396 memcpy (readbuf + n, buf.byte + skip, chunk);
398 if (writebuf != NULL)
400 memcpy (buf.byte + skip, writebuf + n, chunk);
401 errno = 0;
402 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
403 buf.word);
404 if (errno != 0)
406 /* Using the appropriate one (I or D) is necessary for
407 Gould NP1, at least. */
408 errno = 0;
409 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
410 buf.word);
411 if (errno != 0)
412 break;
417 return n;
420 /* Implement the to_xfer_partial target_ops method. */
422 enum target_xfer_status
423 inf_ptrace_target::xfer_partial (enum target_object object,
424 const char *annex, gdb_byte *readbuf,
425 const gdb_byte *writebuf,
426 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
428 ptid_t ptid = inferior_ptid;
430 switch (object)
432 case TARGET_OBJECT_MEMORY:
433 #ifdef PT_IO
434 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
435 request that promises to be much more efficient in reading
436 and writing data in the traced process's address space. */
438 struct ptrace_io_desc piod;
440 /* NOTE: We assume that there are no distinct address spaces
441 for instruction and data. However, on OpenBSD 3.9 and
442 later, PIOD_WRITE_D doesn't allow changing memory that's
443 mapped read-only. Since most code segments will be
444 read-only, using PIOD_WRITE_D will prevent us from
445 inserting breakpoints, so we use PIOD_WRITE_I instead. */
446 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
447 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
448 piod.piod_offs = (void *) (long) offset;
449 piod.piod_len = len;
451 errno = 0;
452 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
454 /* Return the actual number of bytes read or written. */
455 *xfered_len = piod.piod_len;
456 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
458 /* If the PT_IO request is somehow not supported, fallback on
459 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
460 to indicate failure. */
461 if (errno != EINVAL)
462 return TARGET_XFER_EOF;
464 #endif
465 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
466 offset, len);
467 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
469 case TARGET_OBJECT_UNWIND_TABLE:
470 return TARGET_XFER_E_IO;
472 case TARGET_OBJECT_AUXV:
473 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
474 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
475 request that allows us to read the auxilliary vector. Other
476 BSD's may follow if they feel the need to support PIE. */
478 struct ptrace_io_desc piod;
480 if (writebuf)
481 return TARGET_XFER_E_IO;
482 piod.piod_op = PIOD_READ_AUXV;
483 piod.piod_addr = readbuf;
484 piod.piod_offs = (void *) (long) offset;
485 piod.piod_len = len;
487 errno = 0;
488 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
490 /* Return the actual number of bytes read or written. */
491 *xfered_len = piod.piod_len;
492 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
495 #endif
496 return TARGET_XFER_E_IO;
498 case TARGET_OBJECT_WCOOKIE:
499 return TARGET_XFER_E_IO;
501 default:
502 return TARGET_XFER_E_IO;
506 /* Return non-zero if the thread specified by PTID is alive. */
508 bool
509 inf_ptrace_target::thread_alive (ptid_t ptid)
511 /* ??? Is kill the right way to do this? */
512 return (::kill (ptid.pid (), 0) != -1);
515 /* Print status information about what we're accessing. */
517 void
518 inf_ptrace_target::files_info ()
520 struct inferior *inf = current_inferior ();
522 gdb_printf (_("\tUsing the running image of %s %s.\n"),
523 inf->attach_flag ? "attached" : "child",
524 target_pid_to_str (ptid_t (inf->pid)).c_str ());
527 std::string
528 inf_ptrace_target::pid_to_str (ptid_t ptid)
530 return normal_pid_to_str (ptid);
533 /* Implement the "close" target method. */
535 void
536 inf_ptrace_target::close ()
538 /* Unregister from the event loop. */
539 if (is_async_p ())
540 async (false);
542 inf_child_target::close ();