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"
38 static PTRACE_TYPE_RET
39 gdb_ptrace (PTRACE_TYPE_ARG1 request
, ptid_t ptid
, PTRACE_TYPE_ARG3 addr
,
40 PTRACE_TYPE_ARG4 data
)
43 return ptrace (request
, ptid
.pid (), addr
, data
);
45 pid_t pid
= get_ptrace_pid (ptid
);
46 return ptrace (request
, pid
, addr
, data
);
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. */
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
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
,
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
);
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. */
114 inf_ptrace_target::mourn_inferior ()
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. */
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
);
157 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
159 perror_with_name (("ptrace"));
161 error (_("This system does not support attaching to a process"));
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);
179 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
182 inf_ptrace_target::detach (inferior
*inf
, int from_tty
)
184 pid_t pid
= inferior_ptid
.pid ();
186 target_announce_detach (from_tty
);
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. */
194 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
)1, 0);
196 perror_with_name (("ptrace"));
198 error (_("This system does not support detaching from a process"));
201 detach_success (inf
);
204 /* See inf-ptrace.h. */
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. */
218 inf_ptrace_target::kill ()
220 pid_t pid
= inferior_ptid
.pid ();
226 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
227 waitpid (pid
, &status
, 0);
229 target_mourn_inferior (inferior_ptid
);
234 /* See inf-ptrace.h. */
237 get_ptrace_pid (ptid_t ptid
)
241 /* If we have an LWPID to work with, use it. Otherwise, we're
242 dealing with a non-threaded program/target. */
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
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
;
267 request
= PT_CONTINUE
;
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. */
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. */
283 gdb_ptrace (request
, ptid
, (PTRACE_TYPE_ARG3
)1, gdb_signal_to_host (signal
));
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. */
293 inf_ptrace_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
294 target_wait_flags target_options
)
297 int options
, status
, save_errno
;
300 if (target_options
& TARGET_WNOHANG
)
309 pid
= waitpid (ptid
.pid (), &status
, options
);
312 while (pid
== -1 && errno
== EINTR
);
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 auxilliary 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 ();