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/>. */
25 #include "nat/gdb_ptrace.h"
26 #include "gdbsupport/gdb_wait.h"
29 #include "inf-ptrace.h"
30 #include "inf-child.h"
31 #include "gdbthread.h"
32 #include "nat/fork-inferior.h"
35 #include "gdbsupport/eintr.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
)
44 return ptrace (request
, ptid
.pid (), addr
, data
);
46 pid_t pid
= get_ptrace_pid (ptid
);
47 return ptrace (request
, pid
, addr
, data
);
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. */
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
75 inf_ptrace_target::create_inferior (const char *exec_file
,
76 const std::string
&allargs
,
77 char **env
, int from_tty
)
79 if (exec_file
== nullptr)
80 no_executable_specified_error ();
82 inferior
*inf
= current_inferior ();
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
= inf
->target_is_pushed (this);
88 target_unpush_up unpusher
;
89 if (! ops_already_pushed
)
91 /* Clear possible core file with its process_stratum. */
92 inf
->push_target (this);
93 unpusher
.reset (this);
96 pid_t pid
= fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, NULL
,
100 /* We have something that executes now. We'll be running through
101 the shell at this point (if startup-with-shell is true), but the
102 pid shouldn't change. */
103 thread_info
*thr
= add_thread_silent (this, ptid
);
104 switch_to_thread (thr
);
108 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
110 /* On some targets, there must be some explicit actions taken after
111 the inferior has been started up. */
112 post_startup_inferior (ptid
);
115 /* Clean up a rotting corpse of an inferior after it died. */
118 inf_ptrace_target::mourn_inferior ()
122 /* Wait just one more time to collect the inferior's exit status.
123 Do not check whether this succeeds though, since we may be
124 dealing with a process that we attached to. Such a process will
125 only report its exit status to its original parent. */
126 gdb::waitpid (inferior_ptid
.pid (), &status
, 0);
128 inf_child_target::mourn_inferior ();
131 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
132 be chatty about it. */
135 inf_ptrace_target::attach (const char *args
, int from_tty
)
137 inferior
*inf
= current_inferior ();
139 /* Do not change either targets above or the same target if already present.
140 The reason is the target stack is shared across multiple inferiors. */
141 int ops_already_pushed
= inf
->target_is_pushed (this);
143 pid_t pid
= parse_pid_to_attach (args
);
145 if (pid
== getpid ())
146 error (_("I refuse to debug myself!"));
148 target_unpush_up unpusher
;
149 if (! ops_already_pushed
)
151 /* target_pid_to_str already uses the target. Also clear possible core
152 file with its process_stratum. */
153 inf
->push_target (this);
154 unpusher
.reset (this);
157 target_announce_attach (from_tty
, pid
);
161 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
163 perror_with_name (("ptrace"));
165 error (_("This system does not support attaching to a process"));
168 inferior_appeared (inf
, pid
);
169 inf
->attach_flag
= true;
171 /* Always add a main thread. If some target extends the ptrace
172 target, it should decorate the ptid later with more info. */
173 thread_info
*thr
= add_thread_silent (this, ptid_t (pid
));
174 switch_to_thread (thr
);
176 /* Don't consider the thread stopped until we've processed its
177 initial SIGSTOP stop. */
178 set_executing (this, thr
->ptid
, true);
183 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
186 inf_ptrace_target::detach (inferior
*inf
, int from_tty
)
188 pid_t pid
= inferior_ptid
.pid ();
190 target_announce_detach (from_tty
);
193 /* We'd better not have left any breakpoints in the program or it'll
194 die when it hits one. Also note that this may only work if we
195 previously attached to the inferior. It *might* work if we
196 started the process ourselves. */
198 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
)1, 0);
200 perror_with_name (("ptrace"));
202 error (_("This system does not support detaching from a process"));
205 detach_success (inf
);
208 /* See inf-ptrace.h. */
211 inf_ptrace_target::detach_success (inferior
*inf
)
213 switch_to_no_thread ();
214 detach_inferior (inf
);
216 maybe_unpush_target ();
219 /* Kill the inferior. */
222 inf_ptrace_target::kill ()
224 pid_t pid
= inferior_ptid
.pid ();
230 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
231 gdb::waitpid (pid
, &status
, 0);
233 target_mourn_inferior (inferior_ptid
);
238 /* See inf-ptrace.h. */
241 get_ptrace_pid (ptid_t ptid
)
245 /* If we have an LWPID to work with, use it. Otherwise, we're
246 dealing with a non-threaded program/target. */
254 /* Resume execution of thread PTID, or all threads if PTID is -1. If
255 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
259 inf_ptrace_target::resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
261 PTRACE_TYPE_ARG1 request
;
263 if (minus_one_ptid
== ptid
)
264 /* Resume all threads. Traditionally ptrace() only supports
265 single-threaded processes, so simply resume the inferior. */
266 ptid
= ptid_t (inferior_ptid
.pid ());
268 if (catch_syscall_enabled ())
269 request
= PT_SYSCALL
;
271 request
= PT_CONTINUE
;
275 /* If this system does not support PT_STEP, a higher level
276 function will have called the appropriate functions to transmute the
277 step request into a continue request (by setting breakpoints on
278 all possible successor instructions), so we don't have to
279 worry about that here. */
283 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
284 where it was. If GDB wanted it to start some other way, we have
285 already written a new program counter value to the child. */
287 gdb_ptrace (request
, ptid
, (PTRACE_TYPE_ARG3
)1, gdb_signal_to_host (signal
));
289 perror_with_name (("ptrace"));
292 /* Wait for the child specified by PTID to do something. Return the
293 process ID of the child, or MINUS_ONE_PTID in case of error; store
294 the status in *OURSTATUS. */
297 inf_ptrace_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
298 target_wait_flags target_options
)
301 int options
, status
, save_errno
;
304 if (target_options
& TARGET_WNOHANG
)
311 pid
= gdb::waitpid (ptid
.pid (), &status
, options
);
314 clear_sigint_trap ();
318 gdb_assert (target_options
& TARGET_WNOHANG
);
319 ourstatus
->set_ignore ();
320 return minus_one_ptid
;
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)
349 *ourstatus
= host_status_to_waitstatus (status
);
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. */
360 inf_ptrace_peek_poke (ptid_t ptid
, gdb_byte
*readbuf
,
361 const gdb_byte
*writebuf
,
362 ULONGEST addr
, ULONGEST len
)
367 /* We transfer aligned words. Thus align ADDR down to a word
368 boundary and determine how many bytes to skip at the
370 ULONGEST skip
= addr
& (sizeof (PTRACE_TYPE_RET
) - 1);
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. */
383 PTRACE_TYPE_RET word
;
384 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
387 /* Read the word, also when doing a partial word write. */
388 if (readbuf
!= NULL
|| chunk
< sizeof (PTRACE_TYPE_RET
))
391 buf
.word
= gdb_ptrace (PT_READ_I
, ptid
,
392 (PTRACE_TYPE_ARG3
)(uintptr_t) addr
, 0);
396 memcpy (readbuf
+ n
, buf
.byte
+ skip
, chunk
);
398 if (writebuf
!= NULL
)
400 memcpy (buf
.byte
+ skip
, writebuf
+ n
, chunk
);
402 gdb_ptrace (PT_WRITE_D
, ptid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
406 /* Using the appropriate one (I or D) is necessary for
407 Gould NP1, at least. */
409 gdb_ptrace (PT_WRITE_I
, ptid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
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
;
432 case TARGET_OBJECT_MEMORY
:
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
;
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. */
462 return TARGET_XFER_EOF
;
465 *xfered_len
= inf_ptrace_peek_poke (ptid
, readbuf
, writebuf
,
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 auxiliary vector. Other
476 BSD's may follow if they feel the need to support PIE. */
478 struct ptrace_io_desc piod
;
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
;
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
;
496 return TARGET_XFER_E_IO
;
498 case TARGET_OBJECT_WCOOKIE
:
499 return TARGET_XFER_E_IO
;
502 return TARGET_XFER_E_IO
;
506 /* Return non-zero if the thread specified by PTID is alive. */
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. */
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 ());
528 inf_ptrace_target::pid_to_str (ptid_t ptid
)
530 return normal_pid_to_str (ptid
);
533 /* Implement the "close" target method. */
536 inf_ptrace_target::close ()
538 /* Unregister from the event loop. */
542 inf_child_target::close ();