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