1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2021 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 inf_ptrace_target::~inf_ptrace_target ()
57 /* Prepare to be traced. */
62 /* "Trace me, Dr. Memory!" */
63 if (ptrace (PT_TRACE_ME
, 0, (PTRACE_TYPE_ARG3
) 0, 0) < 0)
64 trace_start_error_with_name ("ptrace");
67 /* Start a new inferior Unix child process. EXEC_FILE is the file to
68 run, ALLARGS is a string containing the arguments to the program.
69 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
73 inf_ptrace_target::create_inferior (const char *exec_file
,
74 const std::string
&allargs
,
75 char **env
, int from_tty
)
77 /* Do not change either targets above or the same target if already present.
78 The reason is the target stack is shared across multiple inferiors. */
79 int ops_already_pushed
= target_is_pushed (this);
81 target_unpush_up unpusher
;
82 if (! ops_already_pushed
)
84 /* Clear possible core file with its process_stratum. */
86 unpusher
.reset (this);
89 pid_t pid
= fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, NULL
,
93 /* We have something that executes now. We'll be running through
94 the shell at this point (if startup-with-shell is true), but the
95 pid shouldn't change. */
96 thread_info
*thr
= add_thread_silent (this, ptid
);
97 switch_to_thread (thr
);
101 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
103 /* On some targets, there must be some explicit actions taken after
104 the inferior has been started up. */
105 target_post_startup_inferior (ptid
);
108 /* Clean up a rotting corpse of an inferior after it died. */
111 inf_ptrace_target::mourn_inferior ()
115 /* Wait just one more time to collect the inferior's exit status.
116 Do not check whether this succeeds though, since we may be
117 dealing with a process that we attached to. Such a process will
118 only report its exit status to its original parent. */
119 waitpid (inferior_ptid
.pid (), &status
, 0);
121 inf_child_target::mourn_inferior ();
124 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
125 be chatty about it. */
128 inf_ptrace_target::attach (const char *args
, int from_tty
)
131 struct inferior
*inf
;
133 /* Do not change either targets above or the same target if already present.
134 The reason is the target stack is shared across multiple inferiors. */
135 int ops_already_pushed
= target_is_pushed (this);
137 pid
= parse_pid_to_attach (args
);
139 if (pid
== getpid ()) /* Trying to masturbate? */
140 error (_("I refuse to debug myself!"));
142 target_unpush_up unpusher
;
143 if (! ops_already_pushed
)
145 /* target_pid_to_str already uses the target. Also clear possible core
146 file with its process_stratum. */
148 unpusher
.reset (this);
153 const char *exec_file
= get_exec_file (0);
156 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
157 target_pid_to_str (ptid_t (pid
)).c_str ());
159 printf_unfiltered (_("Attaching to %s\n"),
160 target_pid_to_str (ptid_t (pid
)).c_str ());
165 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
167 perror_with_name (("ptrace"));
169 error (_("This system does not support attaching to a process"));
172 inf
= current_inferior ();
173 inferior_appeared (inf
, pid
);
174 inf
->attach_flag
= 1;
176 /* Always add a main thread. If some target extends the ptrace
177 target, it should decorate the ptid later with more info. */
178 thread_info
*thr
= add_thread_silent (this, ptid_t (pid
));
179 switch_to_thread (thr
);
181 /* Don't consider the thread stopped until we've processed its
182 initial SIGSTOP stop. */
183 set_executing (this, thr
->ptid
, true);
188 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
191 inf_ptrace_target::detach (inferior
*inf
, int from_tty
)
193 pid_t pid
= inferior_ptid
.pid ();
195 target_announce_detach (from_tty
);
198 /* We'd better not have left any breakpoints in the program or it'll
199 die when it hits one. Also note that this may only work if we
200 previously attached to the inferior. It *might* work if we
201 started the process ourselves. */
203 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
)1, 0);
205 perror_with_name (("ptrace"));
207 error (_("This system does not support detaching from a process"));
210 detach_success (inf
);
213 /* See inf-ptrace.h. */
216 inf_ptrace_target::detach_success (inferior
*inf
)
218 switch_to_no_thread ();
219 detach_inferior (inf
);
221 maybe_unpush_target ();
224 /* Kill the inferior. */
227 inf_ptrace_target::kill ()
229 pid_t pid
= inferior_ptid
.pid ();
235 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
236 waitpid (pid
, &status
, 0);
238 target_mourn_inferior (inferior_ptid
);
243 /* See inf-ptrace.h. */
246 get_ptrace_pid (ptid_t ptid
)
250 /* If we have an LWPID to work with, use it. Otherwise, we're
251 dealing with a non-threaded program/target. */
259 /* Resume execution of thread PTID, or all threads if PTID is -1. If
260 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
264 inf_ptrace_target::resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
266 PTRACE_TYPE_ARG1 request
;
268 if (minus_one_ptid
== ptid
)
269 /* Resume all threads. Traditionally ptrace() only supports
270 single-threaded processes, so simply resume the inferior. */
271 ptid
= ptid_t (inferior_ptid
.pid ());
273 if (catch_syscall_enabled () > 0)
274 request
= PT_SYSCALL
;
276 request
= PT_CONTINUE
;
280 /* If this system does not support PT_STEP, a higher level
281 function will have called the appropriate functions to transmute the
282 step request into a continue request (by setting breakpoints on
283 all possible successor instructions), so we don't have to
284 worry about that here. */
288 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
289 where it was. If GDB wanted it to start some other way, we have
290 already written a new program counter value to the child. */
292 gdb_ptrace (request
, ptid
, (PTRACE_TYPE_ARG3
)1, gdb_signal_to_host (signal
));
294 perror_with_name (("ptrace"));
297 /* Wait for the child specified by PTID to do something. Return the
298 process ID of the child, or MINUS_ONE_PTID in case of error; store
299 the status in *OURSTATUS. */
302 inf_ptrace_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
303 target_wait_flags options
)
306 int status
, save_errno
;
314 pid
= waitpid (ptid
.pid (), &status
, 0);
317 while (pid
== -1 && errno
== EINTR
);
319 clear_sigint_trap ();
323 fprintf_unfiltered (gdb_stderr
,
324 _("Child process unexpectedly missing: %s.\n"),
325 safe_strerror (save_errno
));
327 /* Claim it exited with unknown signal. */
328 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
329 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
330 return inferior_ptid
;
333 /* Ignore terminated detached child processes. */
334 if (!WIFSTOPPED (status
) && find_inferior_pid (this, pid
) == nullptr)
339 store_waitstatus (ourstatus
, status
);
343 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
344 from process PID's memory into READBUF. Start at target address ADDR
345 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
346 be non-null. Return the number of transferred bytes. */
349 inf_ptrace_peek_poke (ptid_t ptid
, gdb_byte
*readbuf
,
350 const gdb_byte
*writebuf
,
351 ULONGEST addr
, ULONGEST len
)
356 /* We transfer aligned words. Thus align ADDR down to a word
357 boundary and determine how many bytes to skip at the
359 ULONGEST skip
= addr
& (sizeof (PTRACE_TYPE_RET
) - 1);
364 n
+= chunk
, addr
+= sizeof (PTRACE_TYPE_RET
), skip
= 0)
366 /* Restrict to a chunk that fits in the current word. */
367 chunk
= std::min (sizeof (PTRACE_TYPE_RET
) - skip
, len
- n
);
369 /* Use a union for type punning. */
372 PTRACE_TYPE_RET word
;
373 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
376 /* Read the word, also when doing a partial word write. */
377 if (readbuf
!= NULL
|| chunk
< sizeof (PTRACE_TYPE_RET
))
380 buf
.word
= gdb_ptrace (PT_READ_I
, ptid
,
381 (PTRACE_TYPE_ARG3
)(uintptr_t) addr
, 0);
385 memcpy (readbuf
+ n
, buf
.byte
+ skip
, chunk
);
387 if (writebuf
!= NULL
)
389 memcpy (buf
.byte
+ skip
, writebuf
+ n
, chunk
);
391 gdb_ptrace (PT_WRITE_D
, ptid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
395 /* Using the appropriate one (I or D) is necessary for
396 Gould NP1, at least. */
398 gdb_ptrace (PT_WRITE_I
, ptid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
409 /* Implement the to_xfer_partial target_ops method. */
411 enum target_xfer_status
412 inf_ptrace_target::xfer_partial (enum target_object object
,
413 const char *annex
, gdb_byte
*readbuf
,
414 const gdb_byte
*writebuf
,
415 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
417 ptid_t ptid
= inferior_ptid
;
421 case TARGET_OBJECT_MEMORY
:
423 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
424 request that promises to be much more efficient in reading
425 and writing data in the traced process's address space. */
427 struct ptrace_io_desc piod
;
429 /* NOTE: We assume that there are no distinct address spaces
430 for instruction and data. However, on OpenBSD 3.9 and
431 later, PIOD_WRITE_D doesn't allow changing memory that's
432 mapped read-only. Since most code segments will be
433 read-only, using PIOD_WRITE_D will prevent us from
434 inserting breakpoints, so we use PIOD_WRITE_I instead. */
435 piod
.piod_op
= writebuf
? PIOD_WRITE_I
: PIOD_READ_D
;
436 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
437 piod
.piod_offs
= (void *) (long) offset
;
441 if (gdb_ptrace (PT_IO
, ptid
, (caddr_t
)&piod
, 0) == 0)
443 /* Return the actual number of bytes read or written. */
444 *xfered_len
= piod
.piod_len
;
445 return (piod
.piod_len
== 0) ? TARGET_XFER_EOF
: TARGET_XFER_OK
;
447 /* If the PT_IO request is somehow not supported, fallback on
448 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
449 to indicate failure. */
451 return TARGET_XFER_EOF
;
454 *xfered_len
= inf_ptrace_peek_poke (ptid
, readbuf
, writebuf
,
456 return *xfered_len
!= 0 ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
458 case TARGET_OBJECT_UNWIND_TABLE
:
459 return TARGET_XFER_E_IO
;
461 case TARGET_OBJECT_AUXV
:
462 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
463 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
464 request that allows us to read the auxilliary vector. Other
465 BSD's may follow if they feel the need to support PIE. */
467 struct ptrace_io_desc piod
;
470 return TARGET_XFER_E_IO
;
471 piod
.piod_op
= PIOD_READ_AUXV
;
472 piod
.piod_addr
= readbuf
;
473 piod
.piod_offs
= (void *) (long) offset
;
477 if (gdb_ptrace (PT_IO
, ptid
, (caddr_t
)&piod
, 0) == 0)
479 /* Return the actual number of bytes read or written. */
480 *xfered_len
= piod
.piod_len
;
481 return (piod
.piod_len
== 0) ? TARGET_XFER_EOF
: TARGET_XFER_OK
;
485 return TARGET_XFER_E_IO
;
487 case TARGET_OBJECT_WCOOKIE
:
488 return TARGET_XFER_E_IO
;
491 return TARGET_XFER_E_IO
;
495 /* Return non-zero if the thread specified by PTID is alive. */
498 inf_ptrace_target::thread_alive (ptid_t ptid
)
500 /* ??? Is kill the right way to do this? */
501 return (::kill (ptid
.pid (), 0) != -1);
504 /* Print status information about what we're accessing. */
507 inf_ptrace_target::files_info ()
509 struct inferior
*inf
= current_inferior ();
511 printf_filtered (_("\tUsing the running image of %s %s.\n"),
512 inf
->attach_flag
? "attached" : "child",
513 target_pid_to_str (inferior_ptid
).c_str ());
517 inf_ptrace_target::pid_to_str (ptid_t ptid
)
519 return normal_pid_to_str (ptid
);