GDB (xrefs)
Loading...
Searching...
No Matches
inf-ptrace.c
Go to the documentation of this file.
1/* Low-level child interface to ptrace.
2
3 Copyright (C) 1988-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "command.h"
22#include "inferior.h"
23#include "terminal.h"
24#include "gdbcore.h"
25#include "regcache.h"
26#include "nat/gdb_ptrace.h"
27#include "gdbsupport/gdb_wait.h"
28#include <signal.h>
29
30#include "inf-ptrace.h"
31#include "inf-child.h"
32#include "gdbthread.h"
33#include "nat/fork-inferior.h"
34#include "utils.h"
35#include "gdbarch.h"
36
37
38
39static PTRACE_TYPE_RET
40gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
42{
43#ifdef __NetBSD__
44 return ptrace (request, ptid.pid (), addr, data);
45#else
46 pid_t pid = get_ptrace_pid (ptid);
47 return ptrace (request, pid, addr, data);
48#endif
49}
50
51/* The event pipe registered as a waitable file in the event loop. */
53
56
57
58
59/* Prepare to be traced. */
60
61static void
63{
64 /* "Trace me, Dr. Memory!" */
65 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
67}
68
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
72 chatty about it. */
73
74void
76 const std::string &allargs,
77 char **env, int from_tty)
78{
80
81 /* Do not change either targets above or the same target if already present.
82 The reason is the target stack is shared across multiple inferiors. */
83 int ops_already_pushed = inf->target_is_pushed (this);
84
85 target_unpush_up unpusher;
86 if (! ops_already_pushed)
87 {
88 /* Clear possible core file with its process_stratum. */
89 inf->push_target (this);
90 unpusher.reset (this);
91 }
92
93 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
94 NULL, NULL, NULL);
95
96 ptid_t ptid (pid);
97 /* We have something that executes now. We'll be running through
98 the shell at this point (if startup-with-shell is true), but the
99 pid shouldn't change. */
100 thread_info *thr = add_thread_silent (this, ptid);
101 switch_to_thread (thr);
102
103 unpusher.release ();
104
106
107 /* On some targets, there must be some explicit actions taken after
108 the inferior has been started up. */
110}
111
112/* Clean up a rotting corpse of an inferior after it died. */
113
114void
116{
117 int status;
118
119 /* Wait just one more time to collect the inferior's exit status.
120 Do not check whether this succeeds though, since we may be
121 dealing with a process that we attached to. Such a process will
122 only report its exit status to its original parent. */
123 waitpid (inferior_ptid.pid (), &status, 0);
124
126}
127
128/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
129 be chatty about it. */
130
131void
132inf_ptrace_target::attach (const char *args, int from_tty)
133{
135
136 /* Do not change either targets above or the same target if already present.
137 The reason is the target stack is shared across multiple inferiors. */
138 int ops_already_pushed = inf->target_is_pushed (this);
139
140 pid_t pid = parse_pid_to_attach (args);
141
142 if (pid == getpid ()) /* Trying to masturbate? */
143 error (_("I refuse to debug myself!"));
144
145 target_unpush_up unpusher;
146 if (! ops_already_pushed)
147 {
148 /* target_pid_to_str already uses the target. Also clear possible core
149 file with its process_stratum. */
150 inf->push_target (this);
151 unpusher.reset (this);
152 }
153
154 target_announce_attach (from_tty, pid);
155
156#ifdef PT_ATTACH
157 errno = 0;
158 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
159 if (errno != 0)
160 perror_with_name (("ptrace"));
161#else
162 error (_("This system does not support attaching to a process"));
163#endif
164
166 inf->attach_flag = true;
167
168 /* Always add a main thread. If some target extends the ptrace
169 target, it should decorate the ptid later with more info. */
170 thread_info *thr = add_thread_silent (this, ptid_t (pid));
171 switch_to_thread (thr);
172
173 /* Don't consider the thread stopped until we've processed its
174 initial SIGSTOP stop. */
175 set_executing (this, thr->ptid, true);
176
177 unpusher.release ();
178}
179
180/* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
181
182void
184{
185 pid_t pid = inferior_ptid.pid ();
186
187 target_announce_detach (from_tty);
188
189#ifdef PT_DETACH
190 /* We'd better not have left any breakpoints in the program or it'll
191 die when it hits one. Also note that this may only work if we
192 previously attached to the inferior. It *might* work if we
193 started the process ourselves. */
194 errno = 0;
195 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
196 if (errno != 0)
197 perror_with_name (("ptrace"));
198#else
199 error (_("This system does not support detaching from a process"));
200#endif
201
203}
204
205/* See inf-ptrace.h. */
206
207void
215
216/* Kill the inferior. */
217
218void
220{
221 pid_t pid = inferior_ptid.pid ();
222 int status;
223
224 if (pid == 0)
225 return;
226
228 waitpid (pid, &status, 0);
229
231}
232
233#ifndef __NetBSD__
234
235/* See inf-ptrace.h. */
236
237pid_t
238get_ptrace_pid (ptid_t ptid)
239{
240 pid_t pid;
241
242 /* If we have an LWPID to work with, use it. Otherwise, we're
243 dealing with a non-threaded program/target. */
244 pid = ptid.lwp ();
245 if (pid == 0)
246 pid = ptid.pid ();
247 return pid;
248}
249#endif
250
251/* Resume execution of thread PTID, or all threads if PTID is -1. If
252 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
253 that signal. */
254
255void
256inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
257{
258 PTRACE_TYPE_ARG1 request;
259
260 if (minus_one_ptid == ptid)
261 /* Resume all threads. Traditionally ptrace() only supports
262 single-threaded processes, so simply resume the inferior. */
263 ptid = ptid_t (inferior_ptid.pid ());
264
265 if (catch_syscall_enabled () > 0)
266 request = PT_SYSCALL;
267 else
268 request = PT_CONTINUE;
269
270 if (step)
271 {
272 /* If this system does not support PT_STEP, a higher level
273 function will have called the appropriate functions to transmute the
274 step request into a continue request (by setting breakpoints on
275 all possible successor instructions), so we don't have to
276 worry about that here. */
277 request = PT_STEP;
278 }
279
280 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
281 where it was. If GDB wanted it to start some other way, we have
282 already written a new program counter value to the child. */
283 errno = 0;
284 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
285 if (errno != 0)
286 perror_with_name (("ptrace"));
287}
288
289/* Wait for the child specified by PTID to do something. Return the
290 process ID of the child, or MINUS_ONE_PTID in case of error; store
291 the status in *OURSTATUS. */
292
293ptid_t
294inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
295 target_wait_flags target_options)
296{
297 pid_t pid;
298 int options, status, save_errno;
299
300 options = 0;
301 if (target_options & TARGET_WNOHANG)
302 options |= WNOHANG;
303
304 do
305 {
307
308 do
309 {
310 pid = waitpid (ptid.pid (), &status, options);
311 save_errno = errno;
312 }
313 while (pid == -1 && errno == EINTR);
314
316
317 if (pid == 0)
318 {
319 gdb_assert (target_options & TARGET_WNOHANG);
320 ourstatus->set_ignore ();
321 return minus_one_ptid;
322 }
323
324 if (pid == -1)
325 {
326 /* In async mode the SIGCHLD might have raced and triggered
327 a check for an event that had already been reported. If
328 the event was the exit of the only remaining child,
329 waitpid() will fail with ECHILD. */
330 if (ptid == minus_one_ptid && save_errno == ECHILD)
331 {
332 ourstatus->set_no_resumed ();
333 return minus_one_ptid;
334 }
335
337 _("Child process unexpectedly missing: %s.\n"),
338 safe_strerror (save_errno));
339
340 ourstatus->set_ignore ();
341 return minus_one_ptid;
342 }
343
344 /* Ignore terminated detached child processes. */
345 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
346 pid = -1;
347 }
348 while (pid == -1);
349
350 *ourstatus = host_status_to_waitstatus (status);
351
352 return ptid_t (pid);
353}
354
355/* Transfer data via ptrace into process PID's memory from WRITEBUF, or
356 from process PID's memory into READBUF. Start at target address ADDR
357 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
358 be non-null. Return the number of transferred bytes. */
359
360static ULONGEST
361inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
362 const gdb_byte *writebuf,
363 ULONGEST addr, ULONGEST len)
364{
365 ULONGEST n;
366 unsigned int chunk;
367
368 /* We transfer aligned words. Thus align ADDR down to a word
369 boundary and determine how many bytes to skip at the
370 beginning. */
371 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
372 addr -= skip;
373
374 for (n = 0;
375 n < len;
376 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
377 {
378 /* Restrict to a chunk that fits in the current word. */
379 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
380
381 /* Use a union for type punning. */
382 union
383 {
384 PTRACE_TYPE_RET word;
385 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
386 } buf;
387
388 /* Read the word, also when doing a partial word write. */
389 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
390 {
391 errno = 0;
392 buf.word = gdb_ptrace (PT_READ_I, ptid,
393 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
394 if (errno != 0)
395 break;
396 if (readbuf != NULL)
397 memcpy (readbuf + n, buf.byte + skip, chunk);
398 }
399 if (writebuf != NULL)
400 {
401 memcpy (buf.byte + skip, writebuf + n, chunk);
402 errno = 0;
403 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
404 buf.word);
405 if (errno != 0)
406 {
407 /* Using the appropriate one (I or D) is necessary for
408 Gould NP1, at least. */
409 errno = 0;
410 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
411 buf.word);
412 if (errno != 0)
413 break;
414 }
415 }
416 }
417
418 return n;
419}
420
421/* Implement the to_xfer_partial target_ops method. */
422
425 const char *annex, gdb_byte *readbuf,
426 const gdb_byte *writebuf,
427 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
428{
429 ptid_t ptid = inferior_ptid;
430
431 switch (object)
432 {
434#ifdef PT_IO
435 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
436 request that promises to be much more efficient in reading
437 and writing data in the traced process's address space. */
438 {
439 struct ptrace_io_desc piod;
440
441 /* NOTE: We assume that there are no distinct address spaces
442 for instruction and data. However, on OpenBSD 3.9 and
443 later, PIOD_WRITE_D doesn't allow changing memory that's
444 mapped read-only. Since most code segments will be
445 read-only, using PIOD_WRITE_D will prevent us from
446 inserting breakpoints, so we use PIOD_WRITE_I instead. */
447 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
448 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
449 piod.piod_offs = (void *) (long) offset;
450 piod.piod_len = len;
451
452 errno = 0;
453 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
454 {
455 /* Return the actual number of bytes read or written. */
456 *xfered_len = piod.piod_len;
457 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
458 }
459 /* If the PT_IO request is somehow not supported, fallback on
460 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
461 to indicate failure. */
462 if (errno != EINVAL)
463 return TARGET_XFER_EOF;
464 }
465#endif
466 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
467 offset, len);
468 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
469
471 return TARGET_XFER_E_IO;
472
474#if defined (PT_IO) && defined (PIOD_READ_AUXV)
475 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
476 request that allows us to read the auxilliary vector. Other
477 BSD's may follow if they feel the need to support PIE. */
478 {
479 struct ptrace_io_desc piod;
480
481 if (writebuf)
482 return TARGET_XFER_E_IO;
483 piod.piod_op = PIOD_READ_AUXV;
484 piod.piod_addr = readbuf;
485 piod.piod_offs = (void *) (long) offset;
486 piod.piod_len = len;
487
488 errno = 0;
489 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
490 {
491 /* Return the actual number of bytes read or written. */
492 *xfered_len = piod.piod_len;
493 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
494 }
495 }
496#endif
497 return TARGET_XFER_E_IO;
498
500 return TARGET_XFER_E_IO;
501
502 default:
503 return TARGET_XFER_E_IO;
504 }
505}
506
507/* Return non-zero if the thread specified by PTID is alive. */
508
509bool
511{
512 /* ??? Is kill the right way to do this? */
513 return (::kill (ptid.pid (), 0) != -1);
514}
515
516/* Print status information about what we're accessing. */
517
518void
520{
521 struct inferior *inf = current_inferior ();
522
523 gdb_printf (_("\tUsing the running image of %s %s.\n"),
524 inf->attach_flag ? "attached" : "child",
525 target_pid_to_str (ptid_t (inf->pid)).c_str ());
526}
527
528std::string
530{
531 return normal_pid_to_str (ptid);
532}
533
534/* Implement the "close" target method. */
535
536void
538{
539 /* Unregister from the event loop. */
540 if (is_async_p ())
541 async (false);
542
544}
static PTRACE_TYPE_RET gdb_ptrace(PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr, PTRACE_TYPE_ARG4 data)
int catch_syscall_enabled(void)
void mourn_inferior() override
Definition inf-child.c:190
void maybe_unpush_target()
Definition inf-child.c:199
void close() override
Definition inf-child.c:183
const std::string & args() const
Definition inferior.h:533
ptid_t ptid
Definition gdbthread.h:259
#define PTRACE_TYPE_RET
Definition config.h:675
#define PTRACE_TYPE_ARG1
Definition config.h:663
ptid_t gdb_startup_inferior(pid_t pid, int num_traps)
Definition fork-child.c:124
void trace_start_error_with_name(const char *string)
pid_t fork_inferior(const char *exec_file_arg, const std::string &allargs, char **env, void(*traceme_fun)(), gdb::function_view< void(int)> init_trace_fun, void(*pre_trace_fun)(), const char *shell_file_arg, void(*exec_fun)(const char *file, char *const *argv, char *const *env))
#define START_INFERIOR_TRAPS_EXPECTED
#define PT_STEP
Definition gdb_ptrace.h:91
#define PT_TRACE_ME
Definition gdb_ptrace.h:43
#define PT_CONTINUE
Definition gdb_ptrace.h:79
#define PT_KILL
Definition gdb_ptrace.h:84
#define ptrace(request, pid, addr, data)
Definition gdb_ptrace.h:141
#define PT_WRITE_I
Definition gdb_ptrace.h:59
#define PT_WRITE_D
Definition gdb_ptrace.h:63
#define PT_READ_I
Definition gdb_ptrace.h:47
#define PT_SYSCALL
Definition gdb_ptrace.h:120
struct thread_info * add_thread_silent(process_stratum_target *targ, ptid_t ptid)
Definition thread.c:296
void set_executing(process_stratum_target *targ, ptid_t ptid, bool executing)
Definition thread.c:908
void switch_to_thread(struct thread_info *thr)
Definition thread.c:1360
void switch_to_no_thread()
Definition thread.c:1345
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition gnu-nat.c:1791
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition gnu-nat.c:1790
target_waitstatus host_status_to_waitstatus(int hoststatus)
Definition inf-child.c:57
static PTRACE_TYPE_RET gdb_ptrace(PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr, PTRACE_TYPE_ARG4 data)
Definition inf-ptrace.c:40
static ULONGEST inf_ptrace_peek_poke(ptid_t ptid, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST addr, ULONGEST len)
Definition inf-ptrace.c:361
pid_t get_ptrace_pid(ptid_t ptid)
Definition inf-ptrace.c:238
static void inf_ptrace_me(void)
Definition inf-ptrace.c:62
ptid_t inferior_ptid
Definition infcmd.c:74
void inferior_appeared(struct inferior *inf, int pid)
Definition inferior.c:363
struct inferior * find_inferior_pid(process_stratum_target *targ, int pid)
Definition inferior.c:389
void detach_inferior(inferior *inf)
Definition inferior.c:340
struct inferior * current_inferior(void)
Definition inferior.c:55
void set_sigint_trap(void)
Definition inflow.c:866
void clear_sigint_trap(void)
Definition inflow.c:881
#define PTRACE_TYPE_ARG3
#define PTRACE_TYPE_ARG4
void create_inferior(const char *, const std::string &, char **, int) override
Definition inf-ptrace.c:75
virtual void post_startup_inferior(ptid_t ptid)=0
~inf_ptrace_target() override=0
Definition inf-ptrace.c:54
void detach_success(inferior *inf)
Definition inf-ptrace.c:208
bool is_async_p() override
Definition inf-ptrace.h:63
void resume(ptid_t, int, enum gdb_signal) override
Definition inf-ptrace.c:256
void mourn_inferior() override
Definition inf-ptrace.c:115
void files_info() override
Definition inf-ptrace.c:519
bool thread_alive(ptid_t ptid) override
Definition inf-ptrace.c:510
static event_pipe m_event_pipe
Definition inf-ptrace.h:103
void attach(const char *, int) override
Definition inf-ptrace.c:132
void kill() override
Definition inf-ptrace.c:219
void detach(inferior *inf, int) override
Definition inf-ptrace.c:183
enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) override
Definition inf-ptrace.c:424
ptid_t wait(ptid_t, struct target_waitstatus *, target_wait_flags) override
Definition inf-ptrace.c:294
void close() override
Definition inf-ptrace.c:537
std::string pid_to_str(ptid_t) override
Definition inf-ptrace.c:529
Definition gnu-nat.c:153
pid_t pid
Definition gnu-nat.c:165
virtual void async(bool) TARGET_DEFAULT_NORETURN(tcomplain())
target_waitstatus & set_no_resumed()
Definition waitstatus.h:321
target_waitstatus & set_ignore()
Definition waitstatus.h:307
void target_announce_detach(int from_tty)
Definition target.c:3622
void target_announce_attach(int from_tty, int pid)
Definition target.c:3643
std::string target_pid_to_str(ptid_t ptid)
Definition target.c:2623
std::string normal_pid_to_str(ptid_t ptid)
Definition target.c:3693
void target_mourn_inferior(ptid_t ptid)
Definition target.c:2758
std::unique_ptr< struct target_ops, target_unpusher > target_unpush_up
Definition target.h:2388
target_xfer_status
Definition target.h:219
@ TARGET_XFER_E_IO
Definition target.h:232
@ TARGET_XFER_EOF
Definition target.h:224
@ TARGET_XFER_OK
Definition target.h:221
target_object
Definition target.h:143
@ TARGET_OBJECT_UNWIND_TABLE
Definition target.h:160
@ TARGET_OBJECT_AUXV
Definition target.h:162
@ TARGET_OBJECT_MEMORY
Definition target.h:147
@ TARGET_OBJECT_WCOOKIE
Definition target.h:164
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
int parse_pid_to_attach(const char *args)
Definition utils.c:3331
#define gdb_stderr
Definition utils.h:187
@ TARGET_WNOHANG
Definition wait.h:32