]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/inf-ptrace.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / inf-ptrace.c
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 \f
38
39 static PTRACE_TYPE_RET
40 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
41 PTRACE_TYPE_ARG4 data)
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. */
52 event_pipe inf_ptrace_target::m_event_pipe;
53
54 inf_ptrace_target::~inf_ptrace_target ()
55 {}
56
57 \f
58
59 /* Prepare to be traced. */
60
61 static void
62 inf_ptrace_me (void)
63 {
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");
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
74 void
75 inf_ptrace_target::create_inferior (const char *exec_file,
76 const std::string &allargs,
77 char **env, int from_tty)
78 {
79 inferior *inf = current_inferior ();
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
105 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
106
107 /* On some targets, there must be some explicit actions taken after
108 the inferior has been started up. */
109 post_startup_inferior (ptid);
110 }
111
112 /* Clean up a rotting corpse of an inferior after it died. */
113
114 void
115 inf_ptrace_target::mourn_inferior ()
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
125 inf_child_target::mourn_inferior ();
126 }
127
128 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
129 be chatty about it. */
130
131 void
132 inf_ptrace_target::attach (const char *args, int from_tty)
133 {
134 inferior *inf = current_inferior ();
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
165 inferior_appeared (inf, pid);
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
182 void
183 inf_ptrace_target::detach (inferior *inf, int from_tty)
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
202 detach_success (inf);
203 }
204
205 /* See inf-ptrace.h. */
206
207 void
208 inf_ptrace_target::detach_success (inferior *inf)
209 {
210 switch_to_no_thread ();
211 detach_inferior (inf);
212
213 maybe_unpush_target ();
214 }
215
216 /* Kill the inferior. */
217
218 void
219 inf_ptrace_target::kill ()
220 {
221 pid_t pid = inferior_ptid.pid ();
222 int status;
223
224 if (pid == 0)
225 return;
226
227 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
228 waitpid (pid, &status, 0);
229
230 target_mourn_inferior (inferior_ptid);
231 }
232
233 #ifndef __NetBSD__
234
235 /* See inf-ptrace.h. */
236
237 pid_t
238 get_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
255 void
256 inf_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
293 ptid_t
294 inf_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 {
306 set_sigint_trap ();
307
308 do
309 {
310 pid = waitpid (ptid.pid (), &status, options);
311 save_errno = errno;
312 }
313 while (pid == -1 && errno == EINTR);
314
315 clear_sigint_trap ();
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
336 gdb_printf (gdb_stderr,
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
360 static ULONGEST
361 inf_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
423 enum target_xfer_status
424 inf_ptrace_target::xfer_partial (enum target_object object,
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 {
433 case TARGET_OBJECT_MEMORY:
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
470 case TARGET_OBJECT_UNWIND_TABLE:
471 return TARGET_XFER_E_IO;
472
473 case TARGET_OBJECT_AUXV:
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
499 case TARGET_OBJECT_WCOOKIE:
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
509 bool
510 inf_ptrace_target::thread_alive (ptid_t ptid)
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
518 void
519 inf_ptrace_target::files_info ()
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 (inferior_ptid).c_str ());
526 }
527
528 std::string
529 inf_ptrace_target::pid_to_str (ptid_t ptid)
530 {
531 return normal_pid_to_str (ptid);
532 }
533
534 /* Implement the "close" target method. */
535
536 void
537 inf_ptrace_target::close ()
538 {
539 /* Unregister from the event loop. */
540 if (is_async_p ())
541 async (false);
542
543 inf_child_target::close ();
544 }