1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2020 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 /* A unique_ptr helper to unpush a target. */
54 struct target_unpusher
56 void operator() (struct target_ops
*ops
) const
62 /* A unique_ptr that unpushes a target on destruction. */
64 typedef std::unique_ptr
<struct target_ops
, target_unpusher
> target_unpush_up
;
68 inf_ptrace_target::~inf_ptrace_target ()
73 /* Prepare to be traced. */
78 /* "Trace me, Dr. Memory!" */
79 if (ptrace (PT_TRACE_ME
, 0, (PTRACE_TYPE_ARG3
) 0, 0) < 0)
80 trace_start_error_with_name ("ptrace");
83 /* Start a new inferior Unix child process. EXEC_FILE is the file to
84 run, ALLARGS is a string containing the arguments to the program.
85 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
89 inf_ptrace_target::create_inferior (const char *exec_file
,
90 const std::string
&allargs
,
91 char **env
, int from_tty
)
96 /* Do not change either targets above or the same target if already present.
97 The reason is the target stack is shared across multiple inferiors. */
98 int ops_already_pushed
= target_is_pushed (this);
100 target_unpush_up unpusher
;
101 if (! ops_already_pushed
)
103 /* Clear possible core file with its process_stratum. */
105 unpusher
.reset (this);
108 pid
= fork_inferior (exec_file
, allargs
, env
, inf_ptrace_me
, NULL
,
112 /* We have something that executes now. We'll be running through
113 the shell at this point (if startup-with-shell is true), but the
114 pid shouldn't change. */
115 add_thread_silent (this, ptid
);
119 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
121 /* On some targets, there must be some explicit actions taken after
122 the inferior has been started up. */
123 target_post_startup_inferior (ptid
);
126 /* Clean up a rotting corpse of an inferior after it died. */
129 inf_ptrace_target::mourn_inferior ()
133 /* Wait just one more time to collect the inferior's exit status.
134 Do not check whether this succeeds though, since we may be
135 dealing with a process that we attached to. Such a process will
136 only report its exit status to its original parent. */
137 waitpid (inferior_ptid
.pid (), &status
, 0);
139 inf_child_target::mourn_inferior ();
142 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
143 be chatty about it. */
146 inf_ptrace_target::attach (const char *args
, int from_tty
)
149 struct inferior
*inf
;
151 /* Do not change either targets above or the same target if already present.
152 The reason is the target stack is shared across multiple inferiors. */
153 int ops_already_pushed
= target_is_pushed (this);
155 pid
= parse_pid_to_attach (args
);
157 if (pid
== getpid ()) /* Trying to masturbate? */
158 error (_("I refuse to debug myself!"));
160 target_unpush_up unpusher
;
161 if (! ops_already_pushed
)
163 /* target_pid_to_str already uses the target. Also clear possible core
164 file with its process_stratum. */
166 unpusher
.reset (this);
171 const char *exec_file
= get_exec_file (0);
174 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
175 target_pid_to_str (ptid_t (pid
)).c_str ());
177 printf_unfiltered (_("Attaching to %s\n"),
178 target_pid_to_str (ptid_t (pid
)).c_str ());
183 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
185 perror_with_name (("ptrace"));
187 error (_("This system does not support attaching to a process"));
190 inf
= current_inferior ();
191 inferior_appeared (inf
, pid
);
192 inf
->attach_flag
= 1;
193 inferior_ptid
= ptid_t (pid
);
195 /* Always add a main thread. If some target extends the ptrace
196 target, it should decorate the ptid later with more info. */
197 thread_info
*thr
= add_thread_silent (this, inferior_ptid
);
198 /* Don't consider the thread stopped until we've processed its
199 initial SIGSTOP stop. */
200 set_executing (this, thr
->ptid
, true);
205 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
208 inf_ptrace_target::detach (inferior
*inf
, int from_tty
)
210 pid_t pid
= inferior_ptid
.pid ();
212 target_announce_detach (from_tty
);
215 /* We'd better not have left any breakpoints in the program or it'll
216 die when it hits one. Also note that this may only work if we
217 previously attached to the inferior. It *might* work if we
218 started the process ourselves. */
220 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
)1, 0);
222 perror_with_name (("ptrace"));
224 error (_("This system does not support detaching from a process"));
227 detach_success (inf
);
230 /* See inf-ptrace.h. */
233 inf_ptrace_target::detach_success (inferior
*inf
)
235 inferior_ptid
= null_ptid
;
236 detach_inferior (inf
);
238 maybe_unpush_target ();
241 /* Kill the inferior. */
244 inf_ptrace_target::kill ()
246 pid_t pid
= inferior_ptid
.pid ();
252 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
)0, 0);
253 waitpid (pid
, &status
, 0);
255 target_mourn_inferior (inferior_ptid
);
260 /* See inf-ptrace.h. */
263 get_ptrace_pid (ptid_t ptid
)
267 /* If we have an LWPID to work with, use it. Otherwise, we're
268 dealing with a non-threaded program/target. */
276 /* Resume execution of thread PTID, or all threads if PTID is -1. If
277 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
281 inf_ptrace_target::resume (ptid_t ptid
, int step
, enum gdb_signal signal
)
283 PTRACE_TYPE_ARG1 request
;
285 if (minus_one_ptid
== ptid
)
286 /* Resume all threads. Traditionally ptrace() only supports
287 single-threaded processes, so simply resume the inferior. */
288 ptid
= ptid_t (inferior_ptid
.pid ());
290 if (catch_syscall_enabled () > 0)
291 request
= PT_SYSCALL
;
293 request
= PT_CONTINUE
;
297 /* If this system does not support PT_STEP, a higher level
298 function will have called the appropriate functions to transmute the
299 step request into a continue request (by setting breakpoints on
300 all possible successor instructions), so we don't have to
301 worry about that here. */
305 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
306 where it was. If GDB wanted it to start some other way, we have
307 already written a new program counter value to the child. */
309 gdb_ptrace (request
, ptid
, (PTRACE_TYPE_ARG3
)1, gdb_signal_to_host (signal
));
311 perror_with_name (("ptrace"));
314 /* Wait for the child specified by PTID to do something. Return the
315 process ID of the child, or MINUS_ONE_PTID in case of error; store
316 the status in *OURSTATUS. */
319 inf_ptrace_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
323 int status
, save_errno
;
331 pid
= waitpid (ptid
.pid (), &status
, 0);
334 while (pid
== -1 && errno
== EINTR
);
336 clear_sigint_trap ();
340 fprintf_unfiltered (gdb_stderr
,
341 _("Child process unexpectedly missing: %s.\n"),
342 safe_strerror (save_errno
));
344 /* Claim it exited with unknown signal. */
345 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
346 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
347 return inferior_ptid
;
350 /* Ignore terminated detached child processes. */
351 if (!WIFSTOPPED (status
) && pid
!= inferior_ptid
.pid ())
356 store_waitstatus (ourstatus
, status
);
360 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
361 from process PID's memory into READBUF. Start at target address ADDR
362 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
363 be non-null. Return the number of transferred bytes. */
366 inf_ptrace_peek_poke (ptid_t ptid
, gdb_byte
*readbuf
,
367 const gdb_byte
*writebuf
,
368 ULONGEST addr
, ULONGEST len
)
373 /* We transfer aligned words. Thus align ADDR down to a word
374 boundary and determine how many bytes to skip at the
376 ULONGEST skip
= addr
& (sizeof (PTRACE_TYPE_RET
) - 1);
381 n
+= chunk
, addr
+= sizeof (PTRACE_TYPE_RET
), skip
= 0)
383 /* Restrict to a chunk that fits in the current word. */
384 chunk
= std::min (sizeof (PTRACE_TYPE_RET
) - skip
, len
- n
);
386 /* Use a union for type punning. */
389 PTRACE_TYPE_RET word
;
390 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
393 /* Read the word, also when doing a partial word write. */
394 if (readbuf
!= NULL
|| chunk
< sizeof (PTRACE_TYPE_RET
))
397 buf
.word
= gdb_ptrace (PT_READ_I
, ptid
,
398 (PTRACE_TYPE_ARG3
)(uintptr_t) addr
, 0);
402 memcpy (readbuf
+ n
, buf
.byte
+ skip
, chunk
);
404 if (writebuf
!= NULL
)
406 memcpy (buf
.byte
+ skip
, writebuf
+ n
, chunk
);
408 gdb_ptrace (PT_WRITE_D
, ptid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
412 /* Using the appropriate one (I or D) is necessary for
413 Gould NP1, at least. */
415 gdb_ptrace (PT_WRITE_I
, ptid
, (PTRACE_TYPE_ARG3
)(uintptr_t) addr
,
426 /* Implement the to_xfer_partial target_ops method. */
428 enum target_xfer_status
429 inf_ptrace_target::xfer_partial (enum target_object object
,
430 const char *annex
, gdb_byte
*readbuf
,
431 const gdb_byte
*writebuf
,
432 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
434 ptid_t ptid
= inferior_ptid
;
438 case TARGET_OBJECT_MEMORY
:
440 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
441 request that promises to be much more efficient in reading
442 and writing data in the traced process's address space. */
444 struct ptrace_io_desc piod
;
446 /* NOTE: We assume that there are no distinct address spaces
447 for instruction and data. However, on OpenBSD 3.9 and
448 later, PIOD_WRITE_D doesn't allow changing memory that's
449 mapped read-only. Since most code segments will be
450 read-only, using PIOD_WRITE_D will prevent us from
451 inserting breakpoints, so we use PIOD_WRITE_I instead. */
452 piod
.piod_op
= writebuf
? PIOD_WRITE_I
: PIOD_READ_D
;
453 piod
.piod_addr
= writebuf
? (void *) writebuf
: readbuf
;
454 piod
.piod_offs
= (void *) (long) offset
;
458 if (gdb_ptrace (PT_IO
, ptid
, (caddr_t
)&piod
, 0) == 0)
460 /* Return the actual number of bytes read or written. */
461 *xfered_len
= piod
.piod_len
;
462 return (piod
.piod_len
== 0) ? TARGET_XFER_EOF
: TARGET_XFER_OK
;
464 /* If the PT_IO request is somehow not supported, fallback on
465 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
466 to indicate failure. */
468 return TARGET_XFER_EOF
;
471 *xfered_len
= inf_ptrace_peek_poke (ptid
, readbuf
, writebuf
,
473 return *xfered_len
!= 0 ? TARGET_XFER_OK
: TARGET_XFER_EOF
;
475 case TARGET_OBJECT_UNWIND_TABLE
:
476 return TARGET_XFER_E_IO
;
478 case TARGET_OBJECT_AUXV
:
479 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
480 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
481 request that allows us to read the auxilliary vector. Other
482 BSD's may follow if they feel the need to support PIE. */
484 struct ptrace_io_desc piod
;
487 return TARGET_XFER_E_IO
;
488 piod
.piod_op
= PIOD_READ_AUXV
;
489 piod
.piod_addr
= readbuf
;
490 piod
.piod_offs
= (void *) (long) offset
;
494 if (gdb_ptrace (PT_IO
, ptid
, (caddr_t
)&piod
, 0) == 0)
496 /* Return the actual number of bytes read or written. */
497 *xfered_len
= piod
.piod_len
;
498 return (piod
.piod_len
== 0) ? TARGET_XFER_EOF
: TARGET_XFER_OK
;
502 return TARGET_XFER_E_IO
;
504 case TARGET_OBJECT_WCOOKIE
:
505 return TARGET_XFER_E_IO
;
508 return TARGET_XFER_E_IO
;
512 /* Return non-zero if the thread specified by PTID is alive. */
515 inf_ptrace_target::thread_alive (ptid_t ptid
)
517 /* ??? Is kill the right way to do this? */
518 return (::kill (ptid
.pid (), 0) != -1);
521 /* Print status information about what we're accessing. */
524 inf_ptrace_target::files_info ()
526 struct inferior
*inf
= current_inferior ();
528 printf_filtered (_("\tUsing the running image of %s %s.\n"),
529 inf
->attach_flag
? "attached" : "child",
530 target_pid_to_str (inferior_ptid
).c_str ());
534 inf_ptrace_target::pid_to_str (ptid_t ptid
)
536 return normal_pid_to_str (ptid
);