]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/inf-ptrace.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / inf-ptrace.c
1 /* Low-level child interface to ptrace.
2
3 Copyright (C) 1988-2020 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 "inflow.h"
24 #include "terminal.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "nat/gdb_ptrace.h"
28 #include "gdbsupport/gdb_wait.h"
29 #include <signal.h>
30
31 #include "inf-ptrace.h"
32 #include "inf-child.h"
33 #include "gdbthread.h"
34 #include "nat/fork-inferior.h"
35 #include "utils.h"
36 #include "gdbarch.h"
37
38 \f
39
40 static PTRACE_TYPE_RET
41 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
42 PTRACE_TYPE_ARG4 data)
43 {
44 #ifdef __NetBSD__
45 return ptrace (request, ptid.pid (), addr, data);
46 #else
47 pid_t pid = get_ptrace_pid (ptid);
48 return ptrace (request, pid, addr, data);
49 #endif
50 }
51
52 /* A unique_ptr helper to unpush a target. */
53
54 struct target_unpusher
55 {
56 void operator() (struct target_ops *ops) const
57 {
58 unpush_target (ops);
59 }
60 };
61
62 /* A unique_ptr that unpushes a target on destruction. */
63
64 typedef std::unique_ptr<struct target_ops, target_unpusher> target_unpush_up;
65
66 \f
67
68 inf_ptrace_target::~inf_ptrace_target ()
69 {}
70
71 #ifdef PT_GET_PROCESS_STATE
72
73 /* Target hook for follow_fork. On entry and at return inferior_ptid is
74 the ptid of the followed inferior. */
75
76 bool
77 inf_ptrace_target::follow_fork (bool follow_child, bool detach_fork)
78 {
79 if (!follow_child)
80 {
81 struct thread_info *tp = inferior_thread ();
82 pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
83
84 /* Breakpoints have already been detached from the child by
85 infrun.c. */
86
87 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
88 perror_with_name (("ptrace"));
89 }
90
91 return false;
92 }
93
94 int
95 inf_ptrace_target::insert_fork_catchpoint (int pid)
96 {
97 return 0;
98 }
99
100 int
101 inf_ptrace_target::remove_fork_catchpoint (int pid)
102 {
103 return 0;
104 }
105
106 #endif /* PT_GET_PROCESS_STATE */
107 \f
108
109 /* Prepare to be traced. */
110
111 static void
112 inf_ptrace_me (void)
113 {
114 /* "Trace me, Dr. Memory!" */
115 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
116 trace_start_error_with_name ("ptrace");
117 }
118
119 /* Start a new inferior Unix child process. EXEC_FILE is the file to
120 run, ALLARGS is a string containing the arguments to the program.
121 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
122 chatty about it. */
123
124 void
125 inf_ptrace_target::create_inferior (const char *exec_file,
126 const std::string &allargs,
127 char **env, int from_tty)
128 {
129 pid_t pid;
130 ptid_t ptid;
131
132 /* Do not change either targets above or the same target if already present.
133 The reason is the target stack is shared across multiple inferiors. */
134 int ops_already_pushed = target_is_pushed (this);
135
136 target_unpush_up unpusher;
137 if (! ops_already_pushed)
138 {
139 /* Clear possible core file with its process_stratum. */
140 push_target (this);
141 unpusher.reset (this);
142 }
143
144 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
145 NULL, NULL, NULL);
146
147 ptid = ptid_t (pid);
148 /* We have something that executes now. We'll be running through
149 the shell at this point (if startup-with-shell is true), but the
150 pid shouldn't change. */
151 add_thread_silent (this, ptid);
152
153 unpusher.release ();
154
155 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
156
157 /* On some targets, there must be some explicit actions taken after
158 the inferior has been started up. */
159 target_post_startup_inferior (ptid);
160 }
161
162 #ifdef PT_GET_PROCESS_STATE
163
164 void
165 inf_ptrace_target::post_startup_inferior (ptid_t pid)
166 {
167 ptrace_event_t pe;
168
169 /* Set the initial event mask. */
170 memset (&pe, 0, sizeof pe);
171 pe.pe_set_event |= PTRACE_FORK;
172 if (ptrace (PT_SET_EVENT_MASK, pid.pid (),
173 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
174 perror_with_name (("ptrace"));
175 }
176
177 #endif
178
179 /* Clean up a rotting corpse of an inferior after it died. */
180
181 void
182 inf_ptrace_target::mourn_inferior ()
183 {
184 int status;
185
186 /* Wait just one more time to collect the inferior's exit status.
187 Do not check whether this succeeds though, since we may be
188 dealing with a process that we attached to. Such a process will
189 only report its exit status to its original parent. */
190 waitpid (inferior_ptid.pid (), &status, 0);
191
192 inf_child_target::mourn_inferior ();
193 }
194
195 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
196 be chatty about it. */
197
198 void
199 inf_ptrace_target::attach (const char *args, int from_tty)
200 {
201 pid_t pid;
202 struct inferior *inf;
203
204 /* Do not change either targets above or the same target if already present.
205 The reason is the target stack is shared across multiple inferiors. */
206 int ops_already_pushed = target_is_pushed (this);
207
208 pid = parse_pid_to_attach (args);
209
210 if (pid == getpid ()) /* Trying to masturbate? */
211 error (_("I refuse to debug myself!"));
212
213 target_unpush_up unpusher;
214 if (! ops_already_pushed)
215 {
216 /* target_pid_to_str already uses the target. Also clear possible core
217 file with its process_stratum. */
218 push_target (this);
219 unpusher.reset (this);
220 }
221
222 if (from_tty)
223 {
224 const char *exec_file = get_exec_file (0);
225
226 if (exec_file)
227 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
228 target_pid_to_str (ptid_t (pid)).c_str ());
229 else
230 printf_unfiltered (_("Attaching to %s\n"),
231 target_pid_to_str (ptid_t (pid)).c_str ());
232 }
233
234 #ifdef PT_ATTACH
235 errno = 0;
236 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
237 if (errno != 0)
238 perror_with_name (("ptrace"));
239 #else
240 error (_("This system does not support attaching to a process"));
241 #endif
242
243 inf = current_inferior ();
244 inferior_appeared (inf, pid);
245 inf->attach_flag = 1;
246 inferior_ptid = ptid_t (pid);
247
248 /* Always add a main thread. If some target extends the ptrace
249 target, it should decorate the ptid later with more info. */
250 thread_info *thr = add_thread_silent (this, inferior_ptid);
251 /* Don't consider the thread stopped until we've processed its
252 initial SIGSTOP stop. */
253 set_executing (this, thr->ptid, true);
254
255 unpusher.release ();
256 }
257
258 #ifdef PT_GET_PROCESS_STATE
259
260 void
261 inf_ptrace_target::post_attach (int pid)
262 {
263 ptrace_event_t pe;
264
265 /* Set the initial event mask. */
266 memset (&pe, 0, sizeof pe);
267 pe.pe_set_event |= PTRACE_FORK;
268 if (ptrace (PT_SET_EVENT_MASK, pid,
269 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
270 perror_with_name (("ptrace"));
271 }
272
273 #endif
274
275 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
276
277 void
278 inf_ptrace_target::detach (inferior *inf, int from_tty)
279 {
280 pid_t pid = inferior_ptid.pid ();
281
282 target_announce_detach (from_tty);
283
284 #ifdef PT_DETACH
285 /* We'd better not have left any breakpoints in the program or it'll
286 die when it hits one. Also note that this may only work if we
287 previously attached to the inferior. It *might* work if we
288 started the process ourselves. */
289 errno = 0;
290 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
291 if (errno != 0)
292 perror_with_name (("ptrace"));
293 #else
294 error (_("This system does not support detaching from a process"));
295 #endif
296
297 detach_success (inf);
298 }
299
300 /* See inf-ptrace.h. */
301
302 void
303 inf_ptrace_target::detach_success (inferior *inf)
304 {
305 inferior_ptid = null_ptid;
306 detach_inferior (inf);
307
308 maybe_unpush_target ();
309 }
310
311 /* Kill the inferior. */
312
313 void
314 inf_ptrace_target::kill ()
315 {
316 pid_t pid = inferior_ptid.pid ();
317 int status;
318
319 if (pid == 0)
320 return;
321
322 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
323 waitpid (pid, &status, 0);
324
325 target_mourn_inferior (inferior_ptid);
326 }
327
328 #ifndef __NetBSD__
329
330 /* See inf-ptrace.h. */
331
332 pid_t
333 get_ptrace_pid (ptid_t ptid)
334 {
335 pid_t pid;
336
337 /* If we have an LWPID to work with, use it. Otherwise, we're
338 dealing with a non-threaded program/target. */
339 pid = ptid.lwp ();
340 if (pid == 0)
341 pid = ptid.pid ();
342 return pid;
343 }
344 #endif
345
346 /* Resume execution of thread PTID, or all threads if PTID is -1. If
347 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
348 that signal. */
349
350 void
351 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
352 {
353 PTRACE_TYPE_ARG1 request;
354
355 if (minus_one_ptid == ptid)
356 /* Resume all threads. Traditionally ptrace() only supports
357 single-threaded processes, so simply resume the inferior. */
358 ptid = ptid_t (inferior_ptid.pid ());
359
360 if (catch_syscall_enabled () > 0)
361 request = PT_SYSCALL;
362 else
363 request = PT_CONTINUE;
364
365 if (step)
366 {
367 /* If this system does not support PT_STEP, a higher level
368 function will have called the appropriate functions to transmute the
369 step request into a continue request (by setting breakpoints on
370 all possible successor instructions), so we don't have to
371 worry about that here. */
372 request = PT_STEP;
373 }
374
375 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
376 where it was. If GDB wanted it to start some other way, we have
377 already written a new program counter value to the child. */
378 errno = 0;
379 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
380 if (errno != 0)
381 perror_with_name (("ptrace"));
382 }
383
384 /* Wait for the child specified by PTID to do something. Return the
385 process ID of the child, or MINUS_ONE_PTID in case of error; store
386 the status in *OURSTATUS. */
387
388 ptid_t
389 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
390 int options)
391 {
392 pid_t pid;
393 int status, save_errno;
394
395 do
396 {
397 set_sigint_trap ();
398
399 do
400 {
401 pid = waitpid (ptid.pid (), &status, 0);
402 save_errno = errno;
403 }
404 while (pid == -1 && errno == EINTR);
405
406 clear_sigint_trap ();
407
408 if (pid == -1)
409 {
410 fprintf_unfiltered (gdb_stderr,
411 _("Child process unexpectedly missing: %s.\n"),
412 safe_strerror (save_errno));
413
414 /* Claim it exited with unknown signal. */
415 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
416 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
417 return inferior_ptid;
418 }
419
420 /* Ignore terminated detached child processes. */
421 if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
422 pid = -1;
423 }
424 while (pid == -1);
425
426 #ifdef PT_GET_PROCESS_STATE
427 if (WIFSTOPPED (status))
428 {
429 ptrace_state_t pe;
430 pid_t fpid;
431
432 if (ptrace (PT_GET_PROCESS_STATE, pid,
433 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
434 perror_with_name (("ptrace"));
435
436 switch (pe.pe_report_event)
437 {
438 case PTRACE_FORK:
439 ourstatus->kind = TARGET_WAITKIND_FORKED;
440 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
441
442 /* Make sure the other end of the fork is stopped too. */
443 fpid = waitpid (pe.pe_other_pid, &status, 0);
444 if (fpid == -1)
445 perror_with_name (("waitpid"));
446
447 if (ptrace (PT_GET_PROCESS_STATE, fpid,
448 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
449 perror_with_name (("ptrace"));
450
451 gdb_assert (pe.pe_report_event == PTRACE_FORK);
452 gdb_assert (pe.pe_other_pid == pid);
453 if (fpid == inferior_ptid.pid ())
454 {
455 ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
456 return ptid_t (fpid);
457 }
458
459 return ptid_t (pid);
460 }
461 }
462 #endif
463
464 store_waitstatus (ourstatus, status);
465 return ptid_t (pid);
466 }
467
468 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
469 from process PID's memory into READBUF. Start at target address ADDR
470 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
471 be non-null. Return the number of transferred bytes. */
472
473 static ULONGEST
474 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
475 const gdb_byte *writebuf,
476 ULONGEST addr, ULONGEST len)
477 {
478 ULONGEST n;
479 unsigned int chunk;
480
481 /* We transfer aligned words. Thus align ADDR down to a word
482 boundary and determine how many bytes to skip at the
483 beginning. */
484 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
485 addr -= skip;
486
487 for (n = 0;
488 n < len;
489 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
490 {
491 /* Restrict to a chunk that fits in the current word. */
492 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
493
494 /* Use a union for type punning. */
495 union
496 {
497 PTRACE_TYPE_RET word;
498 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
499 } buf;
500
501 /* Read the word, also when doing a partial word write. */
502 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
503 {
504 errno = 0;
505 buf.word = gdb_ptrace (PT_READ_I, ptid,
506 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
507 if (errno != 0)
508 break;
509 if (readbuf != NULL)
510 memcpy (readbuf + n, buf.byte + skip, chunk);
511 }
512 if (writebuf != NULL)
513 {
514 memcpy (buf.byte + skip, writebuf + n, chunk);
515 errno = 0;
516 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
517 buf.word);
518 if (errno != 0)
519 {
520 /* Using the appropriate one (I or D) is necessary for
521 Gould NP1, at least. */
522 errno = 0;
523 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
524 buf.word);
525 if (errno != 0)
526 break;
527 }
528 }
529 }
530
531 return n;
532 }
533
534 /* Implement the to_xfer_partial target_ops method. */
535
536 enum target_xfer_status
537 inf_ptrace_target::xfer_partial (enum target_object object,
538 const char *annex, gdb_byte *readbuf,
539 const gdb_byte *writebuf,
540 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
541 {
542 ptid_t ptid = inferior_ptid;
543
544 switch (object)
545 {
546 case TARGET_OBJECT_MEMORY:
547 #ifdef PT_IO
548 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
549 request that promises to be much more efficient in reading
550 and writing data in the traced process's address space. */
551 {
552 struct ptrace_io_desc piod;
553
554 /* NOTE: We assume that there are no distinct address spaces
555 for instruction and data. However, on OpenBSD 3.9 and
556 later, PIOD_WRITE_D doesn't allow changing memory that's
557 mapped read-only. Since most code segments will be
558 read-only, using PIOD_WRITE_D will prevent us from
559 inserting breakpoints, so we use PIOD_WRITE_I instead. */
560 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
561 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
562 piod.piod_offs = (void *) (long) offset;
563 piod.piod_len = len;
564
565 errno = 0;
566 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
567 {
568 /* Return the actual number of bytes read or written. */
569 *xfered_len = piod.piod_len;
570 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
571 }
572 /* If the PT_IO request is somehow not supported, fallback on
573 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
574 to indicate failure. */
575 if (errno != EINVAL)
576 return TARGET_XFER_EOF;
577 }
578 #endif
579 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
580 offset, len);
581 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
582
583 case TARGET_OBJECT_UNWIND_TABLE:
584 return TARGET_XFER_E_IO;
585
586 case TARGET_OBJECT_AUXV:
587 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
588 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
589 request that allows us to read the auxilliary vector. Other
590 BSD's may follow if they feel the need to support PIE. */
591 {
592 struct ptrace_io_desc piod;
593
594 if (writebuf)
595 return TARGET_XFER_E_IO;
596 piod.piod_op = PIOD_READ_AUXV;
597 piod.piod_addr = readbuf;
598 piod.piod_offs = (void *) (long) offset;
599 piod.piod_len = len;
600
601 errno = 0;
602 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
603 {
604 /* Return the actual number of bytes read or written. */
605 *xfered_len = piod.piod_len;
606 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
607 }
608 }
609 #endif
610 return TARGET_XFER_E_IO;
611
612 case TARGET_OBJECT_WCOOKIE:
613 return TARGET_XFER_E_IO;
614
615 default:
616 return TARGET_XFER_E_IO;
617 }
618 }
619
620 /* Return non-zero if the thread specified by PTID is alive. */
621
622 bool
623 inf_ptrace_target::thread_alive (ptid_t ptid)
624 {
625 /* ??? Is kill the right way to do this? */
626 return (::kill (ptid.pid (), 0) != -1);
627 }
628
629 /* Print status information about what we're accessing. */
630
631 void
632 inf_ptrace_target::files_info ()
633 {
634 struct inferior *inf = current_inferior ();
635
636 printf_filtered (_("\tUsing the running image of %s %s.\n"),
637 inf->attach_flag ? "attached" : "child",
638 target_pid_to_str (inferior_ptid).c_str ());
639 }
640
641 std::string
642 inf_ptrace_target::pid_to_str (ptid_t ptid)
643 {
644 return normal_pid_to_str (ptid);
645 }
646
647 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
648
649 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
650 Return 0 if *READPTR is already at the end of the buffer.
651 Return -1 if there is insufficient buffer for a whole entry.
652 Return 1 if an entry was read into *TYPEP and *VALP. */
653
654 int
655 inf_ptrace_target::auxv_parse (gdb_byte **readptr, gdb_byte *endptr,
656 CORE_ADDR *typep, CORE_ADDR *valp)
657 {
658 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
659 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
660 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
661 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
662 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
663 gdb_byte *ptr = *readptr;
664
665 if (endptr == ptr)
666 return 0;
667
668 if (endptr - ptr < 2 * sizeof_auxv_val)
669 return -1;
670
671 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
672 ptr += sizeof_auxv_val; /* Alignment. */
673 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
674 ptr += sizeof_auxv_val;
675
676 *readptr = ptr;
677 return 1;
678 }
679
680 #endif
681 \f