Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
CommitLineData
2c4a536d 1/* Low-level child interface to ptrace.
5bf970f9 2
3666a048 3 Copyright (C) 1988-2021 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 22#include "inferior.h"
191c4426 23#include "terminal.h"
5bf970f9 24#include "gdbcore.h"
8785ced0 25#include "regcache.h"
e3790375 26#include "nat/gdb_ptrace.h"
268a13a5 27#include "gdbsupport/gdb_wait.h"
5bf970f9
AC
28#include <signal.h>
29
2c0b251b 30#include "inf-ptrace.h"
2c4a536d 31#include "inf-child.h"
af990527 32#include "gdbthread.h"
2090129c
SDJ
33#include "nat/fork-inferior.h"
34#include "utils.h"
0d12e84c 35#include "gdbarch.h"
2c4a536d 36
c7c14b96
MK
37\f
38
f09db380
KR
39static PTRACE_TYPE_RET
40gdb_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
f6ac5f3d
PA
51inf_ptrace_target::~inf_ptrace_target ()
52{}
53
735f54b4
MK
54\f
55
4b8a1a28 56/* Prepare to be traced. */
5bf970f9
AC
57
58static void
c7c14b96 59inf_ptrace_me (void)
5bf970f9 60{
c7c14b96 61 /* "Trace me, Dr. Memory!" */
0db8980c 62 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
50fa3001 63 trace_start_error_with_name ("ptrace");
5bf970f9
AC
64}
65
136d6dae
VP
66/* Start a new inferior Unix child process. EXEC_FILE is the file to
67 run, ALLARGS is a string containing the arguments to the program.
68 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
69 chatty about it. */
5bf970f9 70
f6ac5f3d
PA
71void
72inf_ptrace_target::create_inferior (const char *exec_file,
73 const std::string &allargs,
74 char **env, int from_tty)
5bf970f9 75{
c8fbd44a
SM
76 inferior *inf = current_inferior ();
77
c0edd9ed
JK
78 /* Do not change either targets above or the same target if already present.
79 The reason is the target stack is shared across multiple inferiors. */
c8fbd44a 80 int ops_already_pushed = inf->target_is_pushed (this);
c0edd9ed 81
9ae79dac 82 target_unpush_up unpusher;
c0edd9ed
JK
83 if (! ops_already_pushed)
84 {
85 /* Clear possible core file with its process_stratum. */
c8fbd44a 86 inf->push_target (this);
f6ac5f3d 87 unpusher.reset (this);
c0edd9ed
JK
88 }
89
6155c136
PA
90 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
91 NULL, NULL, NULL);
136d6dae 92
6155c136 93 ptid_t ptid (pid);
2090129c
SDJ
94 /* We have something that executes now. We'll be running through
95 the shell at this point (if startup-with-shell is true), but the
96 pid shouldn't change. */
6155c136
PA
97 thread_info *thr = add_thread_silent (this, ptid);
98 switch_to_thread (thr);
2090129c 99
9ae79dac 100 unpusher.release ();
5bf970f9 101
2090129c 102 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
c7c14b96
MK
103
104 /* On some targets, there must be some explicit actions taken after
105 the inferior has been started up. */
2090129c 106 target_post_startup_inferior (ptid);
5bf970f9
AC
107}
108
4b8a1a28
MK
109/* Clean up a rotting corpse of an inferior after it died. */
110
f6ac5f3d
PA
111void
112inf_ptrace_target::mourn_inferior ()
5bf970f9 113{
4b8a1a28
MK
114 int status;
115
116 /* Wait just one more time to collect the inferior's exit status.
f010475d 117 Do not check whether this succeeds though, since we may be
4b8a1a28 118 dealing with a process that we attached to. Such a process will
3d450bdd 119 only report its exit status to its original parent. */
e99b03dc 120 waitpid (inferior_ptid.pid (), &status, 0);
4b8a1a28 121
f6ac5f3d 122 inf_child_target::mourn_inferior ();
5bf970f9
AC
123}
124
4b8a1a28
MK
125/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
126 be chatty about it. */
5bf970f9 127
f6ac5f3d
PA
128void
129inf_ptrace_target::attach (const char *args, int from_tty)
5bf970f9 130{
c8fbd44a 131 inferior *inf = current_inferior ();
5bf970f9 132
c0edd9ed
JK
133 /* Do not change either targets above or the same target if already present.
134 The reason is the target stack is shared across multiple inferiors. */
c8fbd44a 135 int ops_already_pushed = inf->target_is_pushed (this);
c0edd9ed 136
c8fbd44a 137 pid_t pid = parse_pid_to_attach (args);
5bf970f9 138
f6ffd89b 139 if (pid == getpid ()) /* Trying to masturbate? */
8a3fe4f8 140 error (_("I refuse to debug myself!"));
5bf970f9 141
9ae79dac 142 target_unpush_up unpusher;
c0edd9ed
JK
143 if (! ops_already_pushed)
144 {
145 /* target_pid_to_str already uses the target. Also clear possible core
146 file with its process_stratum. */
02980c56 147 inf->push_target (this);
f6ac5f3d 148 unpusher.reset (this);
c0edd9ed
JK
149 }
150
5bf970f9
AC
151 if (from_tty)
152 {
d9fa87f4 153 const char *exec_file = get_exec_file (0);
5bf970f9
AC
154
155 if (exec_file)
a3f17187 156 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
a068643d 157 target_pid_to_str (ptid_t (pid)).c_str ());
5bf970f9 158 else
a3f17187 159 printf_unfiltered (_("Attaching to %s\n"),
a068643d 160 target_pid_to_str (ptid_t (pid)).c_str ());
5bf970f9
AC
161 }
162
6e1e94ea
MK
163#ifdef PT_ATTACH
164 errno = 0;
4b8a1a28 165 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
6e1e94ea 166 if (errno != 0)
e2e0b3e5 167 perror_with_name (("ptrace"));
6e1e94ea 168#else
8a3fe4f8 169 error (_("This system does not support attaching to a process"));
6e1e94ea 170#endif
5bf970f9 171
6c95b8df 172 inferior_appeared (inf, pid);
181e7f93 173 inf->attach_flag = 1;
7f9f62ba 174
af990527
PA
175 /* Always add a main thread. If some target extends the ptrace
176 target, it should decorate the ptid later with more info. */
6155c136
PA
177 thread_info *thr = add_thread_silent (this, ptid_t (pid));
178 switch_to_thread (thr);
179
00aecdcf
PA
180 /* Don't consider the thread stopped until we've processed its
181 initial SIGSTOP stop. */
5b6d1e4f 182 set_executing (this, thr->ptid, true);
af990527 183
9ae79dac 184 unpusher.release ();
5bf970f9
AC
185}
186
6bd6f3b6 187/* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
5bf970f9 188
f6ac5f3d
PA
189void
190inf_ptrace_target::detach (inferior *inf, int from_tty)
5bf970f9 191{
e99b03dc 192 pid_t pid = inferior_ptid.pid ();
5bf970f9 193
0f48b757 194 target_announce_detach (from_tty);
5bf970f9 195
6e1e94ea 196#ifdef PT_DETACH
4b8a1a28 197 /* We'd better not have left any breakpoints in the program or it'll
f010475d 198 die when it hits one. Also note that this may only work if we
4b8a1a28
MK
199 previously attached to the inferior. It *might* work if we
200 started the process ourselves. */
6e1e94ea 201 errno = 0;
6bd6f3b6 202 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
6e1e94ea 203 if (errno != 0)
e2e0b3e5 204 perror_with_name (("ptrace"));
6e1e94ea 205#else
8a3fe4f8 206 error (_("This system does not support detaching from a process"));
6e1e94ea 207#endif
5bf970f9 208
f6ac5f3d 209 detach_success (inf);
ced2dffb
PA
210}
211
212/* See inf-ptrace.h. */
213
214void
f6ac5f3d 215inf_ptrace_target::detach_success (inferior *inf)
ced2dffb 216{
6155c136 217 switch_to_no_thread ();
bc09b0c1 218 detach_inferior (inf);
7a7d3353 219
f6ac5f3d 220 maybe_unpush_target ();
5bf970f9
AC
221}
222
4b8a1a28
MK
223/* Kill the inferior. */
224
f6ac5f3d
PA
225void
226inf_ptrace_target::kill ()
5bf970f9 227{
e99b03dc 228 pid_t pid = inferior_ptid.pid ();
c7c14b96 229 int status;
c7c14b96
MK
230
231 if (pid == 0)
232 return;
233
4b8a1a28
MK
234 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
235 waitpid (pid, &status, 0);
236
bc1e6c81 237 target_mourn_inferior (inferior_ptid);
5bf970f9
AC
238}
239
f09db380
KR
240#ifndef __NetBSD__
241
242/* See inf-ptrace.h. */
90ad5e1d 243
94309df7 244pid_t
90ad5e1d
PA
245get_ptrace_pid (ptid_t ptid)
246{
247 pid_t pid;
248
249 /* If we have an LWPID to work with, use it. Otherwise, we're
250 dealing with a non-threaded program/target. */
e38504b3 251 pid = ptid.lwp ();
90ad5e1d 252 if (pid == 0)
e99b03dc 253 pid = ptid.pid ();
90ad5e1d
PA
254 return pid;
255}
f09db380 256#endif
90ad5e1d 257
4b8a1a28
MK
258/* Resume execution of thread PTID, or all threads if PTID is -1. If
259 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
260 that signal. */
5bf970f9 261
f6ac5f3d
PA
262void
263inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
5bf970f9 264{
f09db380 265 PTRACE_TYPE_ARG1 request;
c7c14b96 266
d7e15655 267 if (minus_one_ptid == ptid)
4b8a1a28
MK
268 /* Resume all threads. Traditionally ptrace() only supports
269 single-threaded processes, so simply resume the inferior. */
f09db380 270 ptid = ptid_t (inferior_ptid.pid ());
c7c14b96 271
a96d9b2e
SDJ
272 if (catch_syscall_enabled () > 0)
273 request = PT_SYSCALL;
274 else
275 request = PT_CONTINUE;
276
c7c14b96
MK
277 if (step)
278 {
279 /* If this system does not support PT_STEP, a higher level
7da6a5b9
LM
280 function will have called the appropriate functions to transmute the
281 step request into a continue request (by setting breakpoints on
282 all possible successor instructions), so we don't have to
283 worry about that here. */
c7c14b96
MK
284 request = PT_STEP;
285 }
286
287 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
288 where it was. If GDB wanted it to start some other way, we have
4b8a1a28 289 already written a new program counter value to the child. */
c7c14b96 290 errno = 0;
f09db380 291 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
c7c14b96
MK
292 if (errno != 0)
293 perror_with_name (("ptrace"));
5bf970f9
AC
294}
295
4b8a1a28
MK
296/* Wait for the child specified by PTID to do something. Return the
297 process ID of the child, or MINUS_ONE_PTID in case of error; store
298 the status in *OURSTATUS. */
5bf970f9 299
f6ac5f3d
PA
300ptid_t
301inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
b60cea74 302 target_wait_flags options)
5bf970f9 303{
4b8a1a28
MK
304 pid_t pid;
305 int status, save_errno;
5bf970f9 306
c7c14b96
MK
307 do
308 {
4b8a1a28 309 set_sigint_trap ();
5bf970f9 310
4b8a1a28
MK
311 do
312 {
e99b03dc 313 pid = waitpid (ptid.pid (), &status, 0);
4b8a1a28
MK
314 save_errno = errno;
315 }
316 while (pid == -1 && errno == EINTR);
5bf970f9 317
c7c14b96 318 clear_sigint_trap ();
5bf970f9 319
c7c14b96
MK
320 if (pid == -1)
321 {
c7c14b96 322 fprintf_unfiltered (gdb_stderr,
4b8a1a28 323 _("Child process unexpectedly missing: %s.\n"),
c7c14b96
MK
324 safe_strerror (save_errno));
325
326 /* Claim it exited with unknown signal. */
327 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 328 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 329 return inferior_ptid;
c7c14b96
MK
330 }
331
4b8a1a28 332 /* Ignore terminated detached child processes. */
f37e5866 333 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
4b8a1a28 334 pid = -1;
c7c14b96 335 }
4b8a1a28 336 while (pid == -1);
c7c14b96
MK
337
338 store_waitstatus (ourstatus, status);
f2907e49 339 return ptid_t (pid);
5bf970f9
AC
340}
341
87c336f6
AA
342/* Transfer data via ptrace into process PID's memory from WRITEBUF, or
343 from process PID's memory into READBUF. Start at target address ADDR
344 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
345 be non-null. Return the number of transferred bytes. */
346
347static ULONGEST
f09db380 348inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
87c336f6
AA
349 const gdb_byte *writebuf,
350 ULONGEST addr, ULONGEST len)
351{
352 ULONGEST n;
353 unsigned int chunk;
354
355 /* We transfer aligned words. Thus align ADDR down to a word
356 boundary and determine how many bytes to skip at the
357 beginning. */
28f1c605 358 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
87c336f6
AA
359 addr -= skip;
360
361 for (n = 0;
362 n < len;
363 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
364 {
365 /* Restrict to a chunk that fits in the current word. */
366 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
367
368 /* Use a union for type punning. */
369 union
370 {
371 PTRACE_TYPE_RET word;
372 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
373 } buf;
374
375 /* Read the word, also when doing a partial word write. */
376 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
377 {
378 errno = 0;
f09db380
KR
379 buf.word = gdb_ptrace (PT_READ_I, ptid,
380 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
87c336f6
AA
381 if (errno != 0)
382 break;
383 if (readbuf != NULL)
384 memcpy (readbuf + n, buf.byte + skip, chunk);
385 }
386 if (writebuf != NULL)
387 {
388 memcpy (buf.byte + skip, writebuf + n, chunk);
389 errno = 0;
f09db380 390 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
87c336f6
AA
391 buf.word);
392 if (errno != 0)
393 {
394 /* Using the appropriate one (I or D) is necessary for
395 Gould NP1, at least. */
396 errno = 0;
f09db380
KR
397 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
398 buf.word);
87c336f6
AA
399 if (errno != 0)
400 break;
401 }
402 }
403 }
404
405 return n;
406}
407
edcc890f 408/* Implement the to_xfer_partial target_ops method. */
5bf970f9 409
f6ac5f3d
PA
410enum target_xfer_status
411inf_ptrace_target::xfer_partial (enum target_object object,
412 const char *annex, gdb_byte *readbuf,
413 const gdb_byte *writebuf,
414 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
5bf970f9 415{
f09db380 416 ptid_t ptid = inferior_ptid;
4b8a1a28 417
5bf970f9
AC
418 switch (object)
419 {
420 case TARGET_OBJECT_MEMORY:
f929a579
AC
421#ifdef PT_IO
422 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
423 request that promises to be much more efficient in reading
424 and writing data in the traced process's address space. */
425 {
426 struct ptrace_io_desc piod;
4b8a1a28 427
f929a579 428 /* NOTE: We assume that there are no distinct address spaces
b457b3dd
MK
429 for instruction and data. However, on OpenBSD 3.9 and
430 later, PIOD_WRITE_D doesn't allow changing memory that's
431 mapped read-only. Since most code segments will be
432 read-only, using PIOD_WRITE_D will prevent us from
433 inserting breakpoints, so we use PIOD_WRITE_I instead. */
434 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
f929a579
AC
435 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
436 piod.piod_offs = (void *) (long) offset;
437 piod.piod_len = len;
438
439 errno = 0;
f09db380 440 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
9b409511 441 {
9b409511 442 /* Return the actual number of bytes read or written. */
493443a4
MK
443 *xfered_len = piod.piod_len;
444 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
9b409511 445 }
f929a579
AC
446 /* If the PT_IO request is somehow not supported, fallback on
447 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
448 to indicate failure. */
449 if (errno != EINVAL)
9b409511 450 return TARGET_XFER_EOF;
f929a579
AC
451 }
452#endif
f09db380 453 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
87c336f6
AA
454 offset, len);
455 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
5bf970f9
AC
456
457 case TARGET_OBJECT_UNWIND_TABLE:
2ed4b548 458 return TARGET_XFER_E_IO;
5bf970f9
AC
459
460 case TARGET_OBJECT_AUXV:
e8ace1c0
MK
461#if defined (PT_IO) && defined (PIOD_READ_AUXV)
462 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
463 request that allows us to read the auxilliary vector. Other
464 BSD's may follow if they feel the need to support PIE. */
465 {
466 struct ptrace_io_desc piod;
467
468 if (writebuf)
2ed4b548 469 return TARGET_XFER_E_IO;
e8ace1c0
MK
470 piod.piod_op = PIOD_READ_AUXV;
471 piod.piod_addr = readbuf;
472 piod.piod_offs = (void *) (long) offset;
473 piod.piod_len = len;
474
475 errno = 0;
f09db380 476 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
9b409511 477 {
9b409511 478 /* Return the actual number of bytes read or written. */
493443a4
MK
479 *xfered_len = piod.piod_len;
480 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
9b409511 481 }
e8ace1c0
MK
482 }
483#endif
2ed4b548 484 return TARGET_XFER_E_IO;
5bf970f9
AC
485
486 case TARGET_OBJECT_WCOOKIE:
2ed4b548 487 return TARGET_XFER_E_IO;
5bf970f9
AC
488
489 default:
2ed4b548 490 return TARGET_XFER_E_IO;
5bf970f9
AC
491 }
492}
493
4b8a1a28 494/* Return non-zero if the thread specified by PTID is alive. */
c7c14b96 495
57810aa7 496bool
f6ac5f3d 497inf_ptrace_target::thread_alive (ptid_t ptid)
c7c14b96 498{
4b8a1a28 499 /* ??? Is kill the right way to do this? */
e99b03dc 500 return (::kill (ptid.pid (), 0) != -1);
c7c14b96
MK
501}
502
503/* Print status information about what we're accessing. */
504
f6ac5f3d
PA
505void
506inf_ptrace_target::files_info ()
c7c14b96 507{
181e7f93
PA
508 struct inferior *inf = current_inferior ();
509
4b8a1a28 510 printf_filtered (_("\tUsing the running image of %s %s.\n"),
181e7f93 511 inf->attach_flag ? "attached" : "child",
a068643d 512 target_pid_to_str (inferior_ptid).c_str ());
5bf970f9
AC
513}
514
a068643d 515std::string
f6ac5f3d 516inf_ptrace_target::pid_to_str (ptid_t ptid)
117de6a9
PA
517{
518 return normal_pid_to_str (ptid);
519}
This page took 1.422554 seconds and 4 git commands to generate.