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