gdb: refactor the initialization file lookup code
[deliverable/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 "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 \f
39
40 static PTRACE_TYPE_RET
41 gdb_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
52 inf_ptrace_target::~inf_ptrace_target ()
53 {}
54
55 \f
56
57 /* Prepare to be traced. */
58
59 static void
60 inf_ptrace_me (void)
61 {
62 /* "Trace me, Dr. Memory!" */
63 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
64 trace_start_error_with_name ("ptrace");
65 }
66
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. */
71
72 void
73 inf_ptrace_target::create_inferior (const char *exec_file,
74 const std::string &allargs,
75 char **env, int from_tty)
76 {
77 inferior *inf = current_inferior ();
78
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. */
81 int ops_already_pushed = inf->target_is_pushed (this);
82
83 target_unpush_up unpusher;
84 if (! ops_already_pushed)
85 {
86 /* Clear possible core file with its process_stratum. */
87 inf->push_target (this);
88 unpusher.reset (this);
89 }
90
91 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
92 NULL, NULL, NULL);
93
94 ptid_t ptid (pid);
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. */
98 thread_info *thr = add_thread_silent (this, ptid);
99 switch_to_thread (thr);
100
101 unpusher.release ();
102
103 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
104
105 /* On some targets, there must be some explicit actions taken after
106 the inferior has been started up. */
107 target_post_startup_inferior (ptid);
108 }
109
110 /* Clean up a rotting corpse of an inferior after it died. */
111
112 void
113 inf_ptrace_target::mourn_inferior ()
114 {
115 int status;
116
117 /* Wait just one more time to collect the inferior's exit status.
118 Do not check whether this succeeds though, since we may be
119 dealing with a process that we attached to. Such a process will
120 only report its exit status to its original parent. */
121 waitpid (inferior_ptid.pid (), &status, 0);
122
123 inf_child_target::mourn_inferior ();
124 }
125
126 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
127 be chatty about it. */
128
129 void
130 inf_ptrace_target::attach (const char *args, int from_tty)
131 {
132 inferior *inf = current_inferior ();
133
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. */
136 int ops_already_pushed = inf->target_is_pushed (this);
137
138 pid_t pid = parse_pid_to_attach (args);
139
140 if (pid == getpid ()) /* Trying to masturbate? */
141 error (_("I refuse to debug myself!"));
142
143 target_unpush_up unpusher;
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. */
148 inf->push_target (this);
149 unpusher.reset (this);
150 }
151
152 if (from_tty)
153 {
154 const char *exec_file = get_exec_file (0);
155
156 if (exec_file)
157 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
158 target_pid_to_str (ptid_t (pid)).c_str ());
159 else
160 printf_unfiltered (_("Attaching to %s\n"),
161 target_pid_to_str (ptid_t (pid)).c_str ());
162 }
163
164 #ifdef PT_ATTACH
165 errno = 0;
166 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
167 if (errno != 0)
168 perror_with_name (("ptrace"));
169 #else
170 error (_("This system does not support attaching to a process"));
171 #endif
172
173 inferior_appeared (inf, pid);
174 inf->attach_flag = 1;
175
176 /* Always add a main thread. If some target extends the ptrace
177 target, it should decorate the ptid later with more info. */
178 thread_info *thr = add_thread_silent (this, ptid_t (pid));
179 switch_to_thread (thr);
180
181 /* Don't consider the thread stopped until we've processed its
182 initial SIGSTOP stop. */
183 set_executing (this, thr->ptid, true);
184
185 unpusher.release ();
186 }
187
188 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
189
190 void
191 inf_ptrace_target::detach (inferior *inf, int from_tty)
192 {
193 pid_t pid = inferior_ptid.pid ();
194
195 target_announce_detach (from_tty);
196
197 #ifdef PT_DETACH
198 /* We'd better not have left any breakpoints in the program or it'll
199 die when it hits one. Also note that this may only work if we
200 previously attached to the inferior. It *might* work if we
201 started the process ourselves. */
202 errno = 0;
203 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
204 if (errno != 0)
205 perror_with_name (("ptrace"));
206 #else
207 error (_("This system does not support detaching from a process"));
208 #endif
209
210 detach_success (inf);
211 }
212
213 /* See inf-ptrace.h. */
214
215 void
216 inf_ptrace_target::detach_success (inferior *inf)
217 {
218 switch_to_no_thread ();
219 detach_inferior (inf);
220
221 maybe_unpush_target ();
222 }
223
224 /* Kill the inferior. */
225
226 void
227 inf_ptrace_target::kill ()
228 {
229 pid_t pid = inferior_ptid.pid ();
230 int status;
231
232 if (pid == 0)
233 return;
234
235 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
236 waitpid (pid, &status, 0);
237
238 target_mourn_inferior (inferior_ptid);
239 }
240
241 #ifndef __NetBSD__
242
243 /* See inf-ptrace.h. */
244
245 pid_t
246 get_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. */
252 pid = ptid.lwp ();
253 if (pid == 0)
254 pid = ptid.pid ();
255 return pid;
256 }
257 #endif
258
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. */
262
263 void
264 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
265 {
266 PTRACE_TYPE_ARG1 request;
267
268 if (minus_one_ptid == ptid)
269 /* Resume all threads. Traditionally ptrace() only supports
270 single-threaded processes, so simply resume the inferior. */
271 ptid = ptid_t (inferior_ptid.pid ());
272
273 if (catch_syscall_enabled () > 0)
274 request = PT_SYSCALL;
275 else
276 request = PT_CONTINUE;
277
278 if (step)
279 {
280 /* If this system does not support PT_STEP, a higher level
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. */
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
290 already written a new program counter value to the child. */
291 errno = 0;
292 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
293 if (errno != 0)
294 perror_with_name (("ptrace"));
295 }
296
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. */
300
301 ptid_t
302 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
303 target_wait_flags options)
304 {
305 pid_t pid;
306 int status, save_errno;
307
308 do
309 {
310 set_sigint_trap ();
311
312 do
313 {
314 pid = waitpid (ptid.pid (), &status, 0);
315 save_errno = errno;
316 }
317 while (pid == -1 && errno == EINTR);
318
319 clear_sigint_trap ();
320
321 if (pid == -1)
322 {
323 fprintf_unfiltered (gdb_stderr,
324 _("Child process unexpectedly missing: %s.\n"),
325 safe_strerror (save_errno));
326
327 /* Claim it exited with unknown signal. */
328 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
329 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
330 return inferior_ptid;
331 }
332
333 /* Ignore terminated detached child processes. */
334 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
335 pid = -1;
336 }
337 while (pid == -1);
338
339 store_waitstatus (ourstatus, status);
340 return ptid_t (pid);
341 }
342
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
348 static ULONGEST
349 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
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. */
359 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
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;
380 buf.word = gdb_ptrace (PT_READ_I, ptid,
381 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
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;
391 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
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;
398 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
399 buf.word);
400 if (errno != 0)
401 break;
402 }
403 }
404 }
405
406 return n;
407 }
408
409 /* Implement the to_xfer_partial target_ops method. */
410
411 enum target_xfer_status
412 inf_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)
416 {
417 ptid_t ptid = inferior_ptid;
418
419 switch (object)
420 {
421 case TARGET_OBJECT_MEMORY:
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;
428
429 /* NOTE: We assume that there are no distinct address spaces
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;
436 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
437 piod.piod_offs = (void *) (long) offset;
438 piod.piod_len = len;
439
440 errno = 0;
441 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
442 {
443 /* Return the actual number of bytes read or written. */
444 *xfered_len = piod.piod_len;
445 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
446 }
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)
451 return TARGET_XFER_EOF;
452 }
453 #endif
454 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
455 offset, len);
456 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
457
458 case TARGET_OBJECT_UNWIND_TABLE:
459 return TARGET_XFER_E_IO;
460
461 case TARGET_OBJECT_AUXV:
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)
470 return TARGET_XFER_E_IO;
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;
477 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
478 {
479 /* Return the actual number of bytes read or written. */
480 *xfered_len = piod.piod_len;
481 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
482 }
483 }
484 #endif
485 return TARGET_XFER_E_IO;
486
487 case TARGET_OBJECT_WCOOKIE:
488 return TARGET_XFER_E_IO;
489
490 default:
491 return TARGET_XFER_E_IO;
492 }
493 }
494
495 /* Return non-zero if the thread specified by PTID is alive. */
496
497 bool
498 inf_ptrace_target::thread_alive (ptid_t ptid)
499 {
500 /* ??? Is kill the right way to do this? */
501 return (::kill (ptid.pid (), 0) != -1);
502 }
503
504 /* Print status information about what we're accessing. */
505
506 void
507 inf_ptrace_target::files_info ()
508 {
509 struct inferior *inf = current_inferior ();
510
511 printf_filtered (_("\tUsing the running image of %s %s.\n"),
512 inf->attach_flag ? "attached" : "child",
513 target_pid_to_str (inferior_ptid).c_str ());
514 }
515
516 std::string
517 inf_ptrace_target::pid_to_str (ptid_t ptid)
518 {
519 return normal_pid_to_str (ptid);
520 }
This page took 0.038985 seconds and 4 git commands to generate.