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