arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / inf-ptrace.c
blob36d6e2aa697166164457b8570013f57a4bc9798b
1 /* Low-level child interface to ptrace.
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "command.h"
21 #include "inferior.h"
22 #include "terminal.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "nat/gdb_ptrace.h"
26 #include "gdbsupport/gdb_wait.h"
27 #include <signal.h>
29 #include "inf-ptrace.h"
30 #include "inf-child.h"
31 #include "gdbthread.h"
32 #include "nat/fork-inferior.h"
33 #include "utils.h"
34 #include "gdbarch.h"
38 static PTRACE_TYPE_RET
39 gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
40 PTRACE_TYPE_ARG4 data)
42 #ifdef __NetBSD__
43 return ptrace (request, ptid.pid (), addr, data);
44 #else
45 pid_t pid = get_ptrace_pid (ptid);
46 return ptrace (request, pid, addr, data);
47 #endif
50 /* The event pipe registered as a waitable file in the event loop. */
51 event_pipe inf_ptrace_target::m_event_pipe;
53 inf_ptrace_target::~inf_ptrace_target ()
58 /* Prepare to be traced. */
60 static void
61 inf_ptrace_me (void)
63 /* "Trace me, Dr. Memory!" */
64 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
65 trace_start_error_with_name ("ptrace");
68 /* Start a new inferior Unix child process. EXEC_FILE is the file to
69 run, ALLARGS is a string containing the arguments to the program.
70 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
71 chatty about it. */
73 void
74 inf_ptrace_target::create_inferior (const char *exec_file,
75 const std::string &allargs,
76 char **env, int from_tty)
78 if (exec_file == nullptr)
79 no_executable_specified_error ();
81 inferior *inf = current_inferior ();
83 /* Do not change either targets above or the same target if already present.
84 The reason is the target stack is shared across multiple inferiors. */
85 int ops_already_pushed = inf->target_is_pushed (this);
87 target_unpush_up unpusher;
88 if (! ops_already_pushed)
90 /* Clear possible core file with its process_stratum. */
91 inf->push_target (this);
92 unpusher.reset (this);
95 pid_t pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
96 NULL, NULL, NULL);
98 ptid_t ptid (pid);
99 /* We have something that executes now. We'll be running through
100 the shell at this point (if startup-with-shell is true), but the
101 pid shouldn't change. */
102 thread_info *thr = add_thread_silent (this, ptid);
103 switch_to_thread (thr);
105 unpusher.release ();
107 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
109 /* On some targets, there must be some explicit actions taken after
110 the inferior has been started up. */
111 post_startup_inferior (ptid);
114 /* Clean up a rotting corpse of an inferior after it died. */
116 void
117 inf_ptrace_target::mourn_inferior ()
119 int status;
121 /* Wait just one more time to collect the inferior's exit status.
122 Do not check whether this succeeds though, since we may be
123 dealing with a process that we attached to. Such a process will
124 only report its exit status to its original parent. */
125 waitpid (inferior_ptid.pid (), &status, 0);
127 inf_child_target::mourn_inferior ();
130 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
131 be chatty about it. */
133 void
134 inf_ptrace_target::attach (const char *args, int from_tty)
136 inferior *inf = current_inferior ();
138 /* Do not change either targets above or the same target if already present.
139 The reason is the target stack is shared across multiple inferiors. */
140 int ops_already_pushed = inf->target_is_pushed (this);
142 pid_t pid = parse_pid_to_attach (args);
144 if (pid == getpid ()) /* Trying to masturbate? */
145 error (_("I refuse to debug myself!"));
147 target_unpush_up unpusher;
148 if (! ops_already_pushed)
150 /* target_pid_to_str already uses the target. Also clear possible core
151 file with its process_stratum. */
152 inf->push_target (this);
153 unpusher.reset (this);
156 target_announce_attach (from_tty, pid);
158 #ifdef PT_ATTACH
159 errno = 0;
160 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
161 if (errno != 0)
162 perror_with_name (("ptrace"));
163 #else
164 error (_("This system does not support attaching to a process"));
165 #endif
167 inferior_appeared (inf, pid);
168 inf->attach_flag = true;
170 /* Always add a main thread. If some target extends the ptrace
171 target, it should decorate the ptid later with more info. */
172 thread_info *thr = add_thread_silent (this, ptid_t (pid));
173 switch_to_thread (thr);
175 /* Don't consider the thread stopped until we've processed its
176 initial SIGSTOP stop. */
177 set_executing (this, thr->ptid, true);
179 unpusher.release ();
182 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
184 void
185 inf_ptrace_target::detach (inferior *inf, int from_tty)
187 pid_t pid = inferior_ptid.pid ();
189 target_announce_detach (from_tty);
191 #ifdef PT_DETACH
192 /* We'd better not have left any breakpoints in the program or it'll
193 die when it hits one. Also note that this may only work if we
194 previously attached to the inferior. It *might* work if we
195 started the process ourselves. */
196 errno = 0;
197 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
198 if (errno != 0)
199 perror_with_name (("ptrace"));
200 #else
201 error (_("This system does not support detaching from a process"));
202 #endif
204 detach_success (inf);
207 /* See inf-ptrace.h. */
209 void
210 inf_ptrace_target::detach_success (inferior *inf)
212 switch_to_no_thread ();
213 detach_inferior (inf);
215 maybe_unpush_target ();
218 /* Kill the inferior. */
220 void
221 inf_ptrace_target::kill ()
223 pid_t pid = inferior_ptid.pid ();
224 int status;
226 if (pid == 0)
227 return;
229 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
230 waitpid (pid, &status, 0);
232 target_mourn_inferior (inferior_ptid);
235 #ifndef __NetBSD__
237 /* See inf-ptrace.h. */
239 pid_t
240 get_ptrace_pid (ptid_t ptid)
242 pid_t pid;
244 /* If we have an LWPID to work with, use it. Otherwise, we're
245 dealing with a non-threaded program/target. */
246 pid = ptid.lwp ();
247 if (pid == 0)
248 pid = ptid.pid ();
249 return pid;
251 #endif
253 /* Resume execution of thread PTID, or all threads if PTID is -1. If
254 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
255 that signal. */
257 void
258 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
260 PTRACE_TYPE_ARG1 request;
262 if (minus_one_ptid == ptid)
263 /* Resume all threads. Traditionally ptrace() only supports
264 single-threaded processes, so simply resume the inferior. */
265 ptid = ptid_t (inferior_ptid.pid ());
267 if (catch_syscall_enabled ())
268 request = PT_SYSCALL;
269 else
270 request = PT_CONTINUE;
272 if (step)
274 /* If this system does not support PT_STEP, a higher level
275 function will have called the appropriate functions to transmute the
276 step request into a continue request (by setting breakpoints on
277 all possible successor instructions), so we don't have to
278 worry about that here. */
279 request = PT_STEP;
282 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
283 where it was. If GDB wanted it to start some other way, we have
284 already written a new program counter value to the child. */
285 errno = 0;
286 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
287 if (errno != 0)
288 perror_with_name (("ptrace"));
291 /* Wait for the child specified by PTID to do something. Return the
292 process ID of the child, or MINUS_ONE_PTID in case of error; store
293 the status in *OURSTATUS. */
295 ptid_t
296 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
297 target_wait_flags target_options)
299 pid_t pid;
300 int options, status, save_errno;
302 options = 0;
303 if (target_options & TARGET_WNOHANG)
304 options |= WNOHANG;
308 set_sigint_trap ();
312 pid = waitpid (ptid.pid (), &status, options);
313 save_errno = errno;
315 while (pid == -1 && errno == EINTR);
317 clear_sigint_trap ();
319 if (pid == 0)
321 gdb_assert (target_options & TARGET_WNOHANG);
322 ourstatus->set_ignore ();
323 return minus_one_ptid;
326 if (pid == -1)
328 /* In async mode the SIGCHLD might have raced and triggered
329 a check for an event that had already been reported. If
330 the event was the exit of the only remaining child,
331 waitpid() will fail with ECHILD. */
332 if (ptid == minus_one_ptid && save_errno == ECHILD)
334 ourstatus->set_no_resumed ();
335 return minus_one_ptid;
338 gdb_printf (gdb_stderr,
339 _("Child process unexpectedly missing: %s.\n"),
340 safe_strerror (save_errno));
342 ourstatus->set_ignore ();
343 return minus_one_ptid;
346 /* Ignore terminated detached child processes. */
347 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
348 pid = -1;
350 while (pid == -1);
352 *ourstatus = host_status_to_waitstatus (status);
354 return ptid_t (pid);
357 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
358 from process PID's memory into READBUF. Start at target address ADDR
359 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
360 be non-null. Return the number of transferred bytes. */
362 static ULONGEST
363 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
364 const gdb_byte *writebuf,
365 ULONGEST addr, ULONGEST len)
367 ULONGEST n;
368 unsigned int chunk;
370 /* We transfer aligned words. Thus align ADDR down to a word
371 boundary and determine how many bytes to skip at the
372 beginning. */
373 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
374 addr -= skip;
376 for (n = 0;
377 n < len;
378 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
380 /* Restrict to a chunk that fits in the current word. */
381 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
383 /* Use a union for type punning. */
384 union
386 PTRACE_TYPE_RET word;
387 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
388 } buf;
390 /* Read the word, also when doing a partial word write. */
391 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
393 errno = 0;
394 buf.word = gdb_ptrace (PT_READ_I, ptid,
395 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
396 if (errno != 0)
397 break;
398 if (readbuf != NULL)
399 memcpy (readbuf + n, buf.byte + skip, chunk);
401 if (writebuf != NULL)
403 memcpy (buf.byte + skip, writebuf + n, chunk);
404 errno = 0;
405 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
406 buf.word);
407 if (errno != 0)
409 /* Using the appropriate one (I or D) is necessary for
410 Gould NP1, at least. */
411 errno = 0;
412 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
413 buf.word);
414 if (errno != 0)
415 break;
420 return n;
423 /* Implement the to_xfer_partial target_ops method. */
425 enum target_xfer_status
426 inf_ptrace_target::xfer_partial (enum target_object object,
427 const char *annex, gdb_byte *readbuf,
428 const gdb_byte *writebuf,
429 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
431 ptid_t ptid = inferior_ptid;
433 switch (object)
435 case TARGET_OBJECT_MEMORY:
436 #ifdef PT_IO
437 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
438 request that promises to be much more efficient in reading
439 and writing data in the traced process's address space. */
441 struct ptrace_io_desc piod;
443 /* NOTE: We assume that there are no distinct address spaces
444 for instruction and data. However, on OpenBSD 3.9 and
445 later, PIOD_WRITE_D doesn't allow changing memory that's
446 mapped read-only. Since most code segments will be
447 read-only, using PIOD_WRITE_D will prevent us from
448 inserting breakpoints, so we use PIOD_WRITE_I instead. */
449 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
450 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
451 piod.piod_offs = (void *) (long) offset;
452 piod.piod_len = len;
454 errno = 0;
455 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
457 /* Return the actual number of bytes read or written. */
458 *xfered_len = piod.piod_len;
459 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
461 /* If the PT_IO request is somehow not supported, fallback on
462 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
463 to indicate failure. */
464 if (errno != EINVAL)
465 return TARGET_XFER_EOF;
467 #endif
468 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
469 offset, len);
470 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
472 case TARGET_OBJECT_UNWIND_TABLE:
473 return TARGET_XFER_E_IO;
475 case TARGET_OBJECT_AUXV:
476 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
477 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
478 request that allows us to read the auxiliary vector. Other
479 BSD's may follow if they feel the need to support PIE. */
481 struct ptrace_io_desc piod;
483 if (writebuf)
484 return TARGET_XFER_E_IO;
485 piod.piod_op = PIOD_READ_AUXV;
486 piod.piod_addr = readbuf;
487 piod.piod_offs = (void *) (long) offset;
488 piod.piod_len = len;
490 errno = 0;
491 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
493 /* Return the actual number of bytes read or written. */
494 *xfered_len = piod.piod_len;
495 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
498 #endif
499 return TARGET_XFER_E_IO;
501 case TARGET_OBJECT_WCOOKIE:
502 return TARGET_XFER_E_IO;
504 default:
505 return TARGET_XFER_E_IO;
509 /* Return non-zero if the thread specified by PTID is alive. */
511 bool
512 inf_ptrace_target::thread_alive (ptid_t ptid)
514 /* ??? Is kill the right way to do this? */
515 return (::kill (ptid.pid (), 0) != -1);
518 /* Print status information about what we're accessing. */
520 void
521 inf_ptrace_target::files_info ()
523 struct inferior *inf = current_inferior ();
525 gdb_printf (_("\tUsing the running image of %s %s.\n"),
526 inf->attach_flag ? "attached" : "child",
527 target_pid_to_str (ptid_t (inf->pid)).c_str ());
530 std::string
531 inf_ptrace_target::pid_to_str (ptid_t ptid)
533 return normal_pid_to_str (ptid);
536 /* Implement the "close" target method. */
538 void
539 inf_ptrace_target::close ()
541 /* Unregister from the event loop. */
542 if (is_async_p ())
543 async (false);
545 inf_child_target::close ();