gdb: fix some clang-tidy readability-misleading-indentation warnings
[binutils-gdb.git] / gdb / inf-ptrace.c
1 /* Low-level child interface to ptrace.
2
3 Copyright (C) 1988-2022 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 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 target_announce_attach (from_tty, pid);
152
153 #ifdef PT_ATTACH
154 errno = 0;
155 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
156 if (errno != 0)
157 perror_with_name (("ptrace"));
158 #else
159 error (_("This system does not support attaching to a process"));
160 #endif
161
162 inferior_appeared (inf, pid);
163 inf->attach_flag = 1;
164
165 /* Always add a main thread. If some target extends the ptrace
166 target, it should decorate the ptid later with more info. */
167 thread_info *thr = add_thread_silent (this, ptid_t (pid));
168 switch_to_thread (thr);
169
170 /* Don't consider the thread stopped until we've processed its
171 initial SIGSTOP stop. */
172 set_executing (this, thr->ptid, true);
173
174 unpusher.release ();
175 }
176
177 /* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
178
179 void
180 inf_ptrace_target::detach (inferior *inf, int from_tty)
181 {
182 pid_t pid = inferior_ptid.pid ();
183
184 target_announce_detach (from_tty);
185
186 #ifdef PT_DETACH
187 /* We'd better not have left any breakpoints in the program or it'll
188 die when it hits one. Also note that this may only work if we
189 previously attached to the inferior. It *might* work if we
190 started the process ourselves. */
191 errno = 0;
192 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
193 if (errno != 0)
194 perror_with_name (("ptrace"));
195 #else
196 error (_("This system does not support detaching from a process"));
197 #endif
198
199 detach_success (inf);
200 }
201
202 /* See inf-ptrace.h. */
203
204 void
205 inf_ptrace_target::detach_success (inferior *inf)
206 {
207 switch_to_no_thread ();
208 detach_inferior (inf);
209
210 maybe_unpush_target ();
211 }
212
213 /* Kill the inferior. */
214
215 void
216 inf_ptrace_target::kill ()
217 {
218 pid_t pid = inferior_ptid.pid ();
219 int status;
220
221 if (pid == 0)
222 return;
223
224 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
225 waitpid (pid, &status, 0);
226
227 target_mourn_inferior (inferior_ptid);
228 }
229
230 #ifndef __NetBSD__
231
232 /* See inf-ptrace.h. */
233
234 pid_t
235 get_ptrace_pid (ptid_t ptid)
236 {
237 pid_t pid;
238
239 /* If we have an LWPID to work with, use it. Otherwise, we're
240 dealing with a non-threaded program/target. */
241 pid = ptid.lwp ();
242 if (pid == 0)
243 pid = ptid.pid ();
244 return pid;
245 }
246 #endif
247
248 /* Resume execution of thread PTID, or all threads if PTID is -1. If
249 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
250 that signal. */
251
252 void
253 inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
254 {
255 PTRACE_TYPE_ARG1 request;
256
257 if (minus_one_ptid == ptid)
258 /* Resume all threads. Traditionally ptrace() only supports
259 single-threaded processes, so simply resume the inferior. */
260 ptid = ptid_t (inferior_ptid.pid ());
261
262 if (catch_syscall_enabled () > 0)
263 request = PT_SYSCALL;
264 else
265 request = PT_CONTINUE;
266
267 if (step)
268 {
269 /* If this system does not support PT_STEP, a higher level
270 function will have called the appropriate functions to transmute the
271 step request into a continue request (by setting breakpoints on
272 all possible successor instructions), so we don't have to
273 worry about that here. */
274 request = PT_STEP;
275 }
276
277 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
278 where it was. If GDB wanted it to start some other way, we have
279 already written a new program counter value to the child. */
280 errno = 0;
281 gdb_ptrace (request, ptid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
282 if (errno != 0)
283 perror_with_name (("ptrace"));
284 }
285
286 /* Wait for the child specified by PTID to do something. Return the
287 process ID of the child, or MINUS_ONE_PTID in case of error; store
288 the status in *OURSTATUS. */
289
290 ptid_t
291 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
292 target_wait_flags options)
293 {
294 pid_t pid;
295 int status, save_errno;
296
297 do
298 {
299 set_sigint_trap ();
300
301 do
302 {
303 pid = waitpid (ptid.pid (), &status, 0);
304 save_errno = errno;
305 }
306 while (pid == -1 && errno == EINTR);
307
308 clear_sigint_trap ();
309
310 if (pid == -1)
311 {
312 fprintf_unfiltered (gdb_stderr,
313 _("Child process unexpectedly missing: %s.\n"),
314 safe_strerror (save_errno));
315
316 /* Claim it exited with unknown signal. */
317 ourstatus->set_signalled (GDB_SIGNAL_UNKNOWN);
318 return inferior_ptid;
319 }
320
321 /* Ignore terminated detached child processes. */
322 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
323 pid = -1;
324 }
325 while (pid == -1);
326
327 *ourstatus = host_status_to_waitstatus (status);
328
329 return ptid_t (pid);
330 }
331
332 /* Transfer data via ptrace into process PID's memory from WRITEBUF, or
333 from process PID's memory into READBUF. Start at target address ADDR
334 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
335 be non-null. Return the number of transferred bytes. */
336
337 static ULONGEST
338 inf_ptrace_peek_poke (ptid_t ptid, gdb_byte *readbuf,
339 const gdb_byte *writebuf,
340 ULONGEST addr, ULONGEST len)
341 {
342 ULONGEST n;
343 unsigned int chunk;
344
345 /* We transfer aligned words. Thus align ADDR down to a word
346 boundary and determine how many bytes to skip at the
347 beginning. */
348 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
349 addr -= skip;
350
351 for (n = 0;
352 n < len;
353 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
354 {
355 /* Restrict to a chunk that fits in the current word. */
356 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
357
358 /* Use a union for type punning. */
359 union
360 {
361 PTRACE_TYPE_RET word;
362 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
363 } buf;
364
365 /* Read the word, also when doing a partial word write. */
366 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
367 {
368 errno = 0;
369 buf.word = gdb_ptrace (PT_READ_I, ptid,
370 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
371 if (errno != 0)
372 break;
373 if (readbuf != NULL)
374 memcpy (readbuf + n, buf.byte + skip, chunk);
375 }
376 if (writebuf != NULL)
377 {
378 memcpy (buf.byte + skip, writebuf + n, chunk);
379 errno = 0;
380 gdb_ptrace (PT_WRITE_D, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
381 buf.word);
382 if (errno != 0)
383 {
384 /* Using the appropriate one (I or D) is necessary for
385 Gould NP1, at least. */
386 errno = 0;
387 gdb_ptrace (PT_WRITE_I, ptid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
388 buf.word);
389 if (errno != 0)
390 break;
391 }
392 }
393 }
394
395 return n;
396 }
397
398 /* Implement the to_xfer_partial target_ops method. */
399
400 enum target_xfer_status
401 inf_ptrace_target::xfer_partial (enum target_object object,
402 const char *annex, gdb_byte *readbuf,
403 const gdb_byte *writebuf,
404 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
405 {
406 ptid_t ptid = inferior_ptid;
407
408 switch (object)
409 {
410 case TARGET_OBJECT_MEMORY:
411 #ifdef PT_IO
412 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
413 request that promises to be much more efficient in reading
414 and writing data in the traced process's address space. */
415 {
416 struct ptrace_io_desc piod;
417
418 /* NOTE: We assume that there are no distinct address spaces
419 for instruction and data. However, on OpenBSD 3.9 and
420 later, PIOD_WRITE_D doesn't allow changing memory that's
421 mapped read-only. Since most code segments will be
422 read-only, using PIOD_WRITE_D will prevent us from
423 inserting breakpoints, so we use PIOD_WRITE_I instead. */
424 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
425 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
426 piod.piod_offs = (void *) (long) offset;
427 piod.piod_len = len;
428
429 errno = 0;
430 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
431 {
432 /* Return the actual number of bytes read or written. */
433 *xfered_len = piod.piod_len;
434 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
435 }
436 /* If the PT_IO request is somehow not supported, fallback on
437 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
438 to indicate failure. */
439 if (errno != EINVAL)
440 return TARGET_XFER_EOF;
441 }
442 #endif
443 *xfered_len = inf_ptrace_peek_poke (ptid, readbuf, writebuf,
444 offset, len);
445 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
446
447 case TARGET_OBJECT_UNWIND_TABLE:
448 return TARGET_XFER_E_IO;
449
450 case TARGET_OBJECT_AUXV:
451 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
452 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
453 request that allows us to read the auxilliary vector. Other
454 BSD's may follow if they feel the need to support PIE. */
455 {
456 struct ptrace_io_desc piod;
457
458 if (writebuf)
459 return TARGET_XFER_E_IO;
460 piod.piod_op = PIOD_READ_AUXV;
461 piod.piod_addr = readbuf;
462 piod.piod_offs = (void *) (long) offset;
463 piod.piod_len = len;
464
465 errno = 0;
466 if (gdb_ptrace (PT_IO, ptid, (caddr_t)&piod, 0) == 0)
467 {
468 /* Return the actual number of bytes read or written. */
469 *xfered_len = piod.piod_len;
470 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
471 }
472 }
473 #endif
474 return TARGET_XFER_E_IO;
475
476 case TARGET_OBJECT_WCOOKIE:
477 return TARGET_XFER_E_IO;
478
479 default:
480 return TARGET_XFER_E_IO;
481 }
482 }
483
484 /* Return non-zero if the thread specified by PTID is alive. */
485
486 bool
487 inf_ptrace_target::thread_alive (ptid_t ptid)
488 {
489 /* ??? Is kill the right way to do this? */
490 return (::kill (ptid.pid (), 0) != -1);
491 }
492
493 /* Print status information about what we're accessing. */
494
495 void
496 inf_ptrace_target::files_info ()
497 {
498 struct inferior *inf = current_inferior ();
499
500 printf_filtered (_("\tUsing the running image of %s %s.\n"),
501 inf->attach_flag ? "attached" : "child",
502 target_pid_to_str (inferior_ptid).c_str ());
503 }
504
505 std::string
506 inf_ptrace_target::pid_to_str (ptid_t ptid)
507 {
508 return normal_pid_to_str (ptid);
509 }