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