gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
CommitLineData
2c4a536d 1/* Low-level child interface to ptrace.
5bf970f9 2
b811d2c2 3 Copyright (C) 1988-2020 Free Software Foundation, Inc.
5bf970f9
AC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
5bf970f9
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bf970f9
AC
19
20#include "defs.h"
5bf970f9 21#include "command.h"
2c4a536d
MK
22#include "inferior.h"
23#include "inflow.h"
191c4426 24#include "terminal.h"
5bf970f9 25#include "gdbcore.h"
8785ced0 26#include "regcache.h"
e3790375 27#include "nat/gdb_ptrace.h"
268a13a5 28#include "gdbsupport/gdb_wait.h"
5bf970f9
AC
29#include <signal.h>
30
2c0b251b 31#include "inf-ptrace.h"
2c4a536d 32#include "inf-child.h"
af990527 33#include "gdbthread.h"
2090129c
SDJ
34#include "nat/fork-inferior.h"
35#include "utils.h"
0d12e84c 36#include "gdbarch.h"
2c4a536d 37
c7c14b96
MK
38\f
39
f09db380
KR
40static PTRACE_TYPE_RET
41gdb_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
9ae79dac
TT
52/* A unique_ptr helper to unpush a target. */
53
54struct 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
64typedef std::unique_ptr<struct target_ops, target_unpusher> target_unpush_up;
65
66\f
67
f6ac5f3d
PA
68inf_ptrace_target::~inf_ptrace_target ()
69{}
70
735f54b4
MK
71\f
72
4b8a1a28 73/* Prepare to be traced. */
5bf970f9
AC
74
75static void
c7c14b96 76inf_ptrace_me (void)
5bf970f9 77{
c7c14b96 78 /* "Trace me, Dr. Memory!" */
0db8980c 79 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
50fa3001 80 trace_start_error_with_name ("ptrace");
5bf970f9
AC
81}
82
136d6dae
VP
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
86 chatty about it. */
5bf970f9 87
f6ac5f3d
PA
88void
89inf_ptrace_target::create_inferior (const char *exec_file,
90 const std::string &allargs,
91 char **env, int from_tty)
5bf970f9 92{
2090129c
SDJ
93 pid_t pid;
94 ptid_t ptid;
136d6dae 95
c0edd9ed
JK
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. */
f6ac5f3d 98 int ops_already_pushed = target_is_pushed (this);
c0edd9ed 99
9ae79dac 100 target_unpush_up unpusher;
c0edd9ed
JK
101 if (! ops_already_pushed)
102 {
103 /* Clear possible core file with its process_stratum. */
f6ac5f3d
PA
104 push_target (this);
105 unpusher.reset (this);
c0edd9ed
JK
106 }
107
136d6dae 108 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
e69860f1 109 NULL, NULL, NULL);
136d6dae 110
f2907e49 111 ptid = ptid_t (pid);
2090129c
SDJ
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. */
5b6d1e4f 115 add_thread_silent (this, ptid);
2090129c 116
9ae79dac 117 unpusher.release ();
5bf970f9 118
2090129c 119 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
c7c14b96
MK
120
121 /* On some targets, there must be some explicit actions taken after
122 the inferior has been started up. */
2090129c 123 target_post_startup_inferior (ptid);
5bf970f9
AC
124}
125
4b8a1a28
MK
126/* Clean up a rotting corpse of an inferior after it died. */
127
f6ac5f3d
PA
128void
129inf_ptrace_target::mourn_inferior ()
5bf970f9 130{
4b8a1a28
MK
131 int status;
132
133 /* Wait just one more time to collect the inferior's exit status.
f010475d 134 Do not check whether this succeeds though, since we may be
4b8a1a28 135 dealing with a process that we attached to. Such a process will
3d450bdd 136 only report its exit status to its original parent. */
e99b03dc 137 waitpid (inferior_ptid.pid (), &status, 0);
4b8a1a28 138
f6ac5f3d 139 inf_child_target::mourn_inferior ();
5bf970f9
AC
140}
141
4b8a1a28
MK
142/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
143 be chatty about it. */
5bf970f9 144
f6ac5f3d
PA
145void
146inf_ptrace_target::attach (const char *args, int from_tty)
5bf970f9 147{
4b8a1a28 148 pid_t pid;
181e7f93 149 struct inferior *inf;
5bf970f9 150
c0edd9ed
JK
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. */
f6ac5f3d 153 int ops_already_pushed = target_is_pushed (this);
c0edd9ed 154
74164c56 155 pid = parse_pid_to_attach (args);
5bf970f9 156
f6ffd89b 157 if (pid == getpid ()) /* Trying to masturbate? */
8a3fe4f8 158 error (_("I refuse to debug myself!"));
5bf970f9 159
9ae79dac 160 target_unpush_up unpusher;
c0edd9ed
JK
161 if (! ops_already_pushed)
162 {
163 /* target_pid_to_str already uses the target. Also clear possible core
164 file with its process_stratum. */
f6ac5f3d
PA
165 push_target (this);
166 unpusher.reset (this);
c0edd9ed
JK
167 }
168
5bf970f9
AC
169 if (from_tty)
170 {
d9fa87f4 171 const char *exec_file = get_exec_file (0);
5bf970f9
AC
172
173 if (exec_file)
a3f17187 174 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
a068643d 175 target_pid_to_str (ptid_t (pid)).c_str ());
5bf970f9 176 else
a3f17187 177 printf_unfiltered (_("Attaching to %s\n"),
a068643d 178 target_pid_to_str (ptid_t (pid)).c_str ());
5bf970f9
AC
179 }
180
6e1e94ea
MK
181#ifdef PT_ATTACH
182 errno = 0;
4b8a1a28 183 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
6e1e94ea 184 if (errno != 0)
e2e0b3e5 185 perror_with_name (("ptrace"));
6e1e94ea 186#else
8a3fe4f8 187 error (_("This system does not support attaching to a process"));
6e1e94ea 188#endif
5bf970f9 189
6c95b8df
PA
190 inf = current_inferior ();
191 inferior_appeared (inf, pid);
181e7f93 192 inf->attach_flag = 1;
f2907e49 193 inferior_ptid = ptid_t (pid);
7f9f62ba 194
af990527
PA
195 /* Always add a main thread. If some target extends the ptrace
196 target, it should decorate the ptid later with more info. */
5b6d1e4f 197 thread_info *thr = add_thread_silent (this, inferior_ptid);
00aecdcf
PA
198 /* Don't consider the thread stopped until we've processed its
199 initial SIGSTOP stop. */
5b6d1e4f 200 set_executing (this, thr->ptid, true);
af990527 201
9ae79dac 202 unpusher.release ();
5bf970f9
AC
203}
204
6bd6f3b6 205/* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
5bf970f9 206
f6ac5f3d
PA
207void
208inf_ptrace_target::detach (inferior *inf, int from_tty)
5bf970f9 209{
e99b03dc 210 pid_t pid = inferior_ptid.pid ();
5bf970f9 211
0f48b757 212 target_announce_detach (from_tty);
5bf970f9 213
6e1e94ea 214#ifdef PT_DETACH
4b8a1a28 215 /* We'd better not have left any breakpoints in the program or it'll
f010475d 216 die when it hits one. Also note that this may only work if we
4b8a1a28
MK
217 previously attached to the inferior. It *might* work if we
218 started the process ourselves. */
6e1e94ea 219 errno = 0;
6bd6f3b6 220 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
6e1e94ea 221 if (errno != 0)
e2e0b3e5 222 perror_with_name (("ptrace"));
6e1e94ea 223#else
8a3fe4f8 224 error (_("This system does not support detaching from a process"));
6e1e94ea 225#endif
5bf970f9 226
f6ac5f3d 227 detach_success (inf);
ced2dffb
PA
228}
229
230/* See inf-ptrace.h. */
231
232void
f6ac5f3d 233inf_ptrace_target::detach_success (inferior *inf)
ced2dffb 234{
5bf970f9 235 inferior_ptid = null_ptid;
bc09b0c1 236 detach_inferior (inf);
7a7d3353 237
f6ac5f3d 238 maybe_unpush_target ();
5bf970f9
AC
239}
240
4b8a1a28
MK
241/* Kill the inferior. */
242
f6ac5f3d
PA
243void
244inf_ptrace_target::kill ()
5bf970f9 245{
e99b03dc 246 pid_t pid = inferior_ptid.pid ();
c7c14b96 247 int status;
c7c14b96
MK
248
249 if (pid == 0)
250 return;
251
4b8a1a28
MK
252 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
253 waitpid (pid, &status, 0);
254
bc1e6c81 255 target_mourn_inferior (inferior_ptid);
5bf970f9
AC
256}
257
f09db380
KR
258#ifndef __NetBSD__
259
260/* See inf-ptrace.h. */
90ad5e1d 261
94309df7 262pid_t
90ad5e1d
PA
263get_ptrace_pid (ptid_t ptid)
264{
265 pid_t pid;
266
267 /* If we have an LWPID to work with, use it. Otherwise, we're
268 dealing with a non-threaded program/target. */
e38504b3 269 pid = ptid.lwp ();
90ad5e1d 270 if (pid == 0)
e99b03dc 271 pid = ptid.pid ();
90ad5e1d
PA
272 return pid;
273}
f09db380 274#endif
90ad5e1d 275
4b8a1a28
MK
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
278 that signal. */
5bf970f9 279
f6ac5f3d
PA
280void
281inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
5bf970f9 282{
f09db380 283 PTRACE_TYPE_ARG1 request;
c7c14b96 284
d7e15655 285 if (minus_one_ptid == ptid)
4b8a1a28
MK
286 /* Resume all threads. Traditionally ptrace() only supports
287 single-threaded processes, so simply resume the inferior. */
f09db380 288 ptid = ptid_t (inferior_ptid.pid ());
c7c14b96 289
a96d9b2e
SDJ
290 if (catch_syscall_enabled () > 0)
291 request = PT_SYSCALL;
292 else
293 request = PT_CONTINUE;
294
c7c14b96
MK
295 if (step)
296 {
297 /* If this system does not support PT_STEP, a higher level
7da6a5b9
LM
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. */
c7c14b96
MK
302 request = PT_STEP;
303 }
304
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
4b8a1a28 307 already written a new program counter value to the child. */
c7c14b96 308 errno = 0;
f09db380 309 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
c7c14b96
MK
310 if (errno != 0)
311 perror_with_name (("ptrace"));
5bf970f9
AC
312}
313
4b8a1a28
MK
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. */
5bf970f9 317
f6ac5f3d
PA
318ptid_t
319inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
320 int options)
5bf970f9 321{
4b8a1a28
MK
322 pid_t pid;
323 int status, save_errno;
5bf970f9 324
c7c14b96
MK
325 do
326 {
4b8a1a28 327 set_sigint_trap ();
5bf970f9 328
4b8a1a28
MK
329 do
330 {
e99b03dc 331 pid = waitpid (ptid.pid (), &status, 0);
4b8a1a28
MK
332 save_errno = errno;
333 }
334 while (pid == -1 && errno == EINTR);
5bf970f9 335
c7c14b96 336 clear_sigint_trap ();
5bf970f9 337
c7c14b96
MK
338 if (pid == -1)
339 {
c7c14b96 340 fprintf_unfiltered (gdb_stderr,
4b8a1a28 341 _("Child process unexpectedly missing: %s.\n"),
c7c14b96
MK
342 safe_strerror (save_errno));
343
344 /* Claim it exited with unknown signal. */
345 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 346 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 347 return inferior_ptid;
c7c14b96
MK
348 }
349
4b8a1a28 350 /* Ignore terminated detached child processes. */
e99b03dc 351 if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
4b8a1a28 352 pid = -1;
c7c14b96 353 }
4b8a1a28 354 while (pid == -1);
c7c14b96
MK
355
356 store_waitstatus (ourstatus, status);
f2907e49 357 return ptid_t (pid);
5bf970f9
AC
358}
359
87c336f6
AA
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. */
364
365static ULONGEST
f09db380 366inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
87c336f6
AA
367 const gdb_byte *writebuf,
368 ULONGEST addr, ULONGEST len)
369{
370 ULONGEST n;
371 unsigned int chunk;
372
373 /* We transfer aligned words. Thus align ADDR down to a word
374 boundary and determine how many bytes to skip at the
375 beginning. */
28f1c605 376 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
87c336f6
AA
377 addr -= skip;
378
379 for (n = 0;
380 n < len;
381 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
382 {
383 /* Restrict to a chunk that fits in the current word. */
384 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
385
386 /* Use a union for type punning. */
387 union
388 {
389 PTRACE_TYPE_RET word;
390 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
391 } buf;
392
393 /* Read the word, also when doing a partial word write. */
394 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
395 {
396 errno = 0;
f09db380
KR
397 buf.word = gdb_ptrace (PT_READ_I, ptid,
398 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
87c336f6
AA
399 if (errno != 0)
400 break;
401 if (readbuf != NULL)
402 memcpy (readbuf + n, buf.byte + skip, chunk);
403 }
404 if (writebuf != NULL)
405 {
406 memcpy (buf.byte + skip, writebuf + n, chunk);
407 errno = 0;
f09db380 408 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
87c336f6
AA
409 buf.word);
410 if (errno != 0)
411 {
412 /* Using the appropriate one (I or D) is necessary for
413 Gould NP1, at least. */
414 errno = 0;
f09db380
KR
415 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
416 buf.word);
87c336f6
AA
417 if (errno != 0)
418 break;
419 }
420 }
421 }
422
423 return n;
424}
425
edcc890f 426/* Implement the to_xfer_partial target_ops method. */
5bf970f9 427
f6ac5f3d
PA
428enum target_xfer_status
429inf_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)
5bf970f9 433{
f09db380 434 ptid_t ptid = inferior_ptid;
4b8a1a28 435
5bf970f9
AC
436 switch (object)
437 {
438 case TARGET_OBJECT_MEMORY:
f929a579
AC
439#ifdef PT_IO
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. */
443 {
444 struct ptrace_io_desc piod;
4b8a1a28 445
f929a579 446 /* NOTE: We assume that there are no distinct address spaces
b457b3dd
MK
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;
f929a579
AC
453 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
454 piod.piod_offs = (void *) (long) offset;
455 piod.piod_len = len;
456
457 errno = 0;
f09db380 458 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
9b409511 459 {
9b409511 460 /* Return the actual number of bytes read or written. */
493443a4
MK
461 *xfered_len = piod.piod_len;
462 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
9b409511 463 }
f929a579
AC
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. */
467 if (errno != EINVAL)
9b409511 468 return TARGET_XFER_EOF;
f929a579
AC
469 }
470#endif
f09db380 471 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
87c336f6
AA
472 offset, len);
473 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
5bf970f9
AC
474
475 case TARGET_OBJECT_UNWIND_TABLE:
2ed4b548 476 return TARGET_XFER_E_IO;
5bf970f9
AC
477
478 case TARGET_OBJECT_AUXV:
e8ace1c0
MK
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. */
483 {
484 struct ptrace_io_desc piod;
485
486 if (writebuf)
2ed4b548 487 return TARGET_XFER_E_IO;
e8ace1c0
MK
488 piod.piod_op = PIOD_READ_AUXV;
489 piod.piod_addr = readbuf;
490 piod.piod_offs = (void *) (long) offset;
491 piod.piod_len = len;
492
493 errno = 0;
f09db380 494 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
9b409511 495 {
9b409511 496 /* Return the actual number of bytes read or written. */
493443a4
MK
497 *xfered_len = piod.piod_len;
498 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
9b409511 499 }
e8ace1c0
MK
500 }
501#endif
2ed4b548 502 return TARGET_XFER_E_IO;
5bf970f9
AC
503
504 case TARGET_OBJECT_WCOOKIE:
2ed4b548 505 return TARGET_XFER_E_IO;
5bf970f9
AC
506
507 default:
2ed4b548 508 return TARGET_XFER_E_IO;
5bf970f9
AC
509 }
510}
511
4b8a1a28 512/* Return non-zero if the thread specified by PTID is alive. */
c7c14b96 513
57810aa7 514bool
f6ac5f3d 515inf_ptrace_target::thread_alive (ptid_t ptid)
c7c14b96 516{
4b8a1a28 517 /* ??? Is kill the right way to do this? */
e99b03dc 518 return (::kill (ptid.pid (), 0) != -1);
c7c14b96
MK
519}
520
521/* Print status information about what we're accessing. */
522
f6ac5f3d
PA
523void
524inf_ptrace_target::files_info ()
c7c14b96 525{
181e7f93
PA
526 struct inferior *inf = current_inferior ();
527
4b8a1a28 528 printf_filtered (_("\tUsing the running image of %s %s.\n"),
181e7f93 529 inf->attach_flag ? "attached" : "child",
a068643d 530 target_pid_to_str (inferior_ptid).c_str ());
5bf970f9
AC
531}
532
a068643d 533std::string
f6ac5f3d 534inf_ptrace_target::pid_to_str (ptid_t ptid)
117de6a9
PA
535{
536 return normal_pid_to_str (ptid);
537}
This page took 1.369945 seconds and 4 git commands to generate.