Revert "PowerPC PLT speculative execution barriers"
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
CommitLineData
2c4a536d 1/* Low-level child interface to ptrace.
5bf970f9 2
e2882c85 3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
5bf970f9
AC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
5bf970f9
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
5bf970f9
AC
19
20#include "defs.h"
5bf970f9 21#include "command.h"
2c4a536d
MK
22#include "inferior.h"
23#include "inflow.h"
191c4426 24#include "terminal.h"
5bf970f9 25#include "gdbcore.h"
8785ced0 26#include "regcache.h"
e3790375 27#include "nat/gdb_ptrace.h"
34a17005 28#include "gdb_wait.h"
5bf970f9
AC
29#include <signal.h>
30
2c0b251b 31#include "inf-ptrace.h"
2c4a536d 32#include "inf-child.h"
af990527 33#include "gdbthread.h"
2090129c
SDJ
34#include "nat/fork-inferior.h"
35#include "utils.h"
2c4a536d 36
c7c14b96
MK
37\f
38
735f54b4
MK
39#ifdef PT_GET_PROCESS_STATE
40
d83ad864
DB
41/* Target hook for follow_fork. On entry and at return inferior_ptid is
42 the ptid of the followed inferior. */
43
735f54b4 44static int
07107ca6
LM
45inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
46 int detach_fork)
735f54b4 47{
d83ad864 48 if (!follow_child)
735f54b4 49 {
ebf3aa72
MK
50 struct thread_info *tp = inferior_thread ();
51 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
191c4426 52
b242c3c2
PA
53 /* Breakpoints have already been detached from the child by
54 infrun.c. */
735f54b4 55
d83ad864 56 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
735f54b4
MK
57 perror_with_name (("ptrace"));
58 }
59
60 return 0;
61}
62
e85e8e5e
MK
63static int
64inf_ptrace_insert_fork_catchpoint (struct target_ops *self, int pid)
65{
66 return 0;
67}
68
69static int
70inf_ptrace_remove_fork_catchpoint (struct target_ops *self, int pid)
71{
72 return 0;
73}
74
735f54b4
MK
75#endif /* PT_GET_PROCESS_STATE */
76\f
77
4b8a1a28 78/* Prepare to be traced. */
5bf970f9
AC
79
80static void
c7c14b96 81inf_ptrace_me (void)
5bf970f9 82{
c7c14b96 83 /* "Trace me, Dr. Memory!" */
0db8980c
SDJ
84 if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
85 trace_start_error_with_name ("ptrace");
5bf970f9
AC
86}
87
136d6dae
VP
88/* Start a new inferior Unix child process. EXEC_FILE is the file to
89 run, ALLARGS is a string containing the arguments to the program.
90 ENV is the environment vector to pass. If FROM_TTY is non-zero, be
91 chatty about it. */
5bf970f9
AC
92
93static void
136d6dae 94inf_ptrace_create_inferior (struct target_ops *ops,
7c5ded6a
SDJ
95 const char *exec_file, const std::string &allargs,
96 char **env, int from_tty)
5bf970f9 97{
2090129c
SDJ
98 pid_t pid;
99 ptid_t ptid;
136d6dae 100
c0edd9ed
JK
101 /* Do not change either targets above or the same target if already present.
102 The reason is the target stack is shared across multiple inferiors. */
103 int ops_already_pushed = target_is_pushed (ops);
25f43500 104 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
c0edd9ed
JK
105
106 if (! ops_already_pushed)
107 {
108 /* Clear possible core file with its process_stratum. */
109 push_target (ops);
25f43500 110 make_cleanup_unpush_target (ops);
c0edd9ed
JK
111 }
112
136d6dae 113 pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
e69860f1 114 NULL, NULL, NULL);
136d6dae 115
2090129c
SDJ
116 ptid = pid_to_ptid (pid);
117 /* We have something that executes now. We'll be running through
118 the shell at this point (if startup-with-shell is true), but the
119 pid shouldn't change. */
120 add_thread_silent (ptid);
121
25f43500 122 discard_cleanups (back_to);
5bf970f9 123
2090129c 124 gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
c7c14b96
MK
125
126 /* On some targets, there must be some explicit actions taken after
127 the inferior has been started up. */
2090129c 128 target_post_startup_inferior (ptid);
5bf970f9
AC
129}
130
e4ef629d
MK
131#ifdef PT_GET_PROCESS_STATE
132
133static void
2e97a79e 134inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
e4ef629d
MK
135{
136 ptrace_event_t pe;
137
138 /* Set the initial event mask. */
139 memset (&pe, 0, sizeof pe);
140 pe.pe_set_event |= PTRACE_FORK;
141 if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
142 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
143 perror_with_name (("ptrace"));
144}
145
146#endif
147
4b8a1a28
MK
148/* Clean up a rotting corpse of an inferior after it died. */
149
c7c14b96 150static void
136d6dae 151inf_ptrace_mourn_inferior (struct target_ops *ops)
5bf970f9 152{
4b8a1a28
MK
153 int status;
154
155 /* Wait just one more time to collect the inferior's exit status.
f010475d 156 Do not check whether this succeeds though, since we may be
4b8a1a28 157 dealing with a process that we attached to. Such a process will
3d450bdd 158 only report its exit status to its original parent. */
4b8a1a28
MK
159 waitpid (ptid_get_pid (inferior_ptid), &status, 0);
160
c1ee2fb3 161 inf_child_mourn_inferior (ops);
5bf970f9
AC
162}
163
4b8a1a28
MK
164/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
165 be chatty about it. */
5bf970f9
AC
166
167static void
c0939df1 168inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
5bf970f9
AC
169{
170 char *exec_file;
4b8a1a28 171 pid_t pid;
181e7f93 172 struct inferior *inf;
5bf970f9 173
c0edd9ed
JK
174 /* Do not change either targets above or the same target if already present.
175 The reason is the target stack is shared across multiple inferiors. */
176 int ops_already_pushed = target_is_pushed (ops);
25f43500 177 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
c0edd9ed 178
74164c56 179 pid = parse_pid_to_attach (args);
5bf970f9 180
f6ffd89b 181 if (pid == getpid ()) /* Trying to masturbate? */
8a3fe4f8 182 error (_("I refuse to debug myself!"));
5bf970f9 183
c0edd9ed
JK
184 if (! ops_already_pushed)
185 {
186 /* target_pid_to_str already uses the target. Also clear possible core
187 file with its process_stratum. */
188 push_target (ops);
25f43500 189 make_cleanup_unpush_target (ops);
c0edd9ed
JK
190 }
191
5bf970f9
AC
192 if (from_tty)
193 {
4b8a1a28 194 exec_file = get_exec_file (0);
5bf970f9
AC
195
196 if (exec_file)
a3f17187 197 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
5bf970f9
AC
198 target_pid_to_str (pid_to_ptid (pid)));
199 else
a3f17187 200 printf_unfiltered (_("Attaching to %s\n"),
5bf970f9
AC
201 target_pid_to_str (pid_to_ptid (pid)));
202
203 gdb_flush (gdb_stdout);
204 }
205
6e1e94ea
MK
206#ifdef PT_ATTACH
207 errno = 0;
4b8a1a28 208 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
6e1e94ea 209 if (errno != 0)
e2e0b3e5 210 perror_with_name (("ptrace"));
6e1e94ea 211#else
8a3fe4f8 212 error (_("This system does not support attaching to a process"));
6e1e94ea 213#endif
5bf970f9 214
6c95b8df
PA
215 inf = current_inferior ();
216 inferior_appeared (inf, pid);
181e7f93 217 inf->attach_flag = 1;
6c95b8df 218 inferior_ptid = pid_to_ptid (pid);
7f9f62ba 219
af990527
PA
220 /* Always add a main thread. If some target extends the ptrace
221 target, it should decorate the ptid later with more info. */
222 add_thread_silent (inferior_ptid);
223
25f43500 224 discard_cleanups (back_to);
5bf970f9
AC
225}
226
e4ef629d
MK
227#ifdef PT_GET_PROCESS_STATE
228
460fac3c 229static void
f045800c 230inf_ptrace_post_attach (struct target_ops *self, int pid)
e4ef629d
MK
231{
232 ptrace_event_t pe;
233
234 /* Set the initial event mask. */
235 memset (&pe, 0, sizeof pe);
236 pe.pe_set_event |= PTRACE_FORK;
237 if (ptrace (PT_SET_EVENT_MASK, pid,
238 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
239 perror_with_name (("ptrace"));
240}
241
242#endif
243
6bd6f3b6 244/* Detach from the inferior. If FROM_TTY is non-zero, be chatty about it. */
5bf970f9
AC
245
246static void
6e1e1966 247inf_ptrace_detach (struct target_ops *ops, inferior *inf, int from_tty)
5bf970f9 248{
4b8a1a28 249 pid_t pid = ptid_get_pid (inferior_ptid);
5bf970f9 250
0f48b757 251 target_announce_detach (from_tty);
5bf970f9 252
6e1e94ea 253#ifdef PT_DETACH
4b8a1a28 254 /* We'd better not have left any breakpoints in the program or it'll
f010475d 255 die when it hits one. Also note that this may only work if we
4b8a1a28
MK
256 previously attached to the inferior. It *might* work if we
257 started the process ourselves. */
6e1e94ea 258 errno = 0;
6bd6f3b6 259 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
6e1e94ea 260 if (errno != 0)
e2e0b3e5 261 perror_with_name (("ptrace"));
6e1e94ea 262#else
8a3fe4f8 263 error (_("This system does not support detaching from a process"));
6e1e94ea 264#endif
5bf970f9 265
bc09b0c1 266 inf_ptrace_detach_success (ops, inf);
ced2dffb
PA
267}
268
269/* See inf-ptrace.h. */
270
271void
bc09b0c1 272inf_ptrace_detach_success (struct target_ops *ops, inferior *inf)
ced2dffb 273{
5bf970f9 274 inferior_ptid = null_ptid;
bc09b0c1 275 detach_inferior (inf);
7a7d3353 276
6a3cb8e8 277 inf_child_maybe_unpush_target (ops);
5bf970f9
AC
278}
279
4b8a1a28
MK
280/* Kill the inferior. */
281
5bf970f9 282static void
7d85a9c0 283inf_ptrace_kill (struct target_ops *ops)
5bf970f9 284{
4b8a1a28 285 pid_t pid = ptid_get_pid (inferior_ptid);
c7c14b96 286 int status;
c7c14b96
MK
287
288 if (pid == 0)
289 return;
290
4b8a1a28
MK
291 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
292 waitpid (pid, &status, 0);
293
bc1e6c81 294 target_mourn_inferior (inferior_ptid);
5bf970f9
AC
295}
296
90ad5e1d
PA
297/* Return which PID to pass to ptrace in order to observe/control the
298 tracee identified by PTID. */
299
94309df7 300pid_t
90ad5e1d
PA
301get_ptrace_pid (ptid_t ptid)
302{
303 pid_t pid;
304
305 /* If we have an LWPID to work with, use it. Otherwise, we're
306 dealing with a non-threaded program/target. */
307 pid = ptid_get_lwp (ptid);
308 if (pid == 0)
309 pid = ptid_get_pid (ptid);
310 return pid;
311}
312
4b8a1a28
MK
313/* Resume execution of thread PTID, or all threads if PTID is -1. If
314 STEP is nonzero, single-step it. If SIGNAL is nonzero, give it
315 that signal. */
5bf970f9
AC
316
317static void
28439f5e 318inf_ptrace_resume (struct target_ops *ops,
2ea28649 319 ptid_t ptid, int step, enum gdb_signal signal)
5bf970f9 320{
90ad5e1d 321 pid_t pid;
a96d9b2e 322 int request;
c7c14b96 323
90ad5e1d 324 if (ptid_equal (minus_one_ptid, ptid))
4b8a1a28
MK
325 /* Resume all threads. Traditionally ptrace() only supports
326 single-threaded processes, so simply resume the inferior. */
c1593e4f 327 pid = ptid_get_pid (inferior_ptid);
90ad5e1d
PA
328 else
329 pid = get_ptrace_pid (ptid);
c7c14b96 330
a96d9b2e
SDJ
331 if (catch_syscall_enabled () > 0)
332 request = PT_SYSCALL;
333 else
334 request = PT_CONTINUE;
335
c7c14b96
MK
336 if (step)
337 {
338 /* If this system does not support PT_STEP, a higher level
339 function will have called single_step() to transmute the step
340 request into a continue request (by setting breakpoints on
341 all possible successor instructions), so we don't have to
342 worry about that here. */
343 request = PT_STEP;
344 }
345
346 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
347 where it was. If GDB wanted it to start some other way, we have
4b8a1a28 348 already written a new program counter value to the child. */
c7c14b96 349 errno = 0;
2ea28649 350 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
c7c14b96
MK
351 if (errno != 0)
352 perror_with_name (("ptrace"));
5bf970f9
AC
353}
354
4b8a1a28
MK
355/* Wait for the child specified by PTID to do something. Return the
356 process ID of the child, or MINUS_ONE_PTID in case of error; store
357 the status in *OURSTATUS. */
5bf970f9 358
c7c14b96 359static ptid_t
117de6a9 360inf_ptrace_wait (struct target_ops *ops,
47608cb1 361 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
5bf970f9 362{
4b8a1a28
MK
363 pid_t pid;
364 int status, save_errno;
5bf970f9 365
c7c14b96
MK
366 do
367 {
4b8a1a28 368 set_sigint_trap ();
5bf970f9 369
4b8a1a28
MK
370 do
371 {
372 pid = waitpid (ptid_get_pid (ptid), &status, 0);
373 save_errno = errno;
374 }
375 while (pid == -1 && errno == EINTR);
5bf970f9 376
c7c14b96 377 clear_sigint_trap ();
5bf970f9 378
c7c14b96
MK
379 if (pid == -1)
380 {
c7c14b96 381 fprintf_unfiltered (gdb_stderr,
4b8a1a28 382 _("Child process unexpectedly missing: %s.\n"),
c7c14b96
MK
383 safe_strerror (save_errno));
384
385 /* Claim it exited with unknown signal. */
386 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 387 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 388 return inferior_ptid;
c7c14b96
MK
389 }
390
4b8a1a28
MK
391 /* Ignore terminated detached child processes. */
392 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
393 pid = -1;
c7c14b96 394 }
4b8a1a28 395 while (pid == -1);
c7c14b96 396
735f54b4
MK
397#ifdef PT_GET_PROCESS_STATE
398 if (WIFSTOPPED (status))
399 {
400 ptrace_state_t pe;
401 pid_t fpid;
402
403 if (ptrace (PT_GET_PROCESS_STATE, pid,
404 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
405 perror_with_name (("ptrace"));
406
407 switch (pe.pe_report_event)
408 {
409 case PTRACE_FORK:
410 ourstatus->kind = TARGET_WAITKIND_FORKED;
3a3e9ee3 411 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
735f54b4
MK
412
413 /* Make sure the other end of the fork is stopped too. */
414 fpid = waitpid (pe.pe_other_pid, &status, 0);
415 if (fpid == -1)
416 perror_with_name (("waitpid"));
417
418 if (ptrace (PT_GET_PROCESS_STATE, fpid,
419 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
420 perror_with_name (("ptrace"));
421
422 gdb_assert (pe.pe_report_event == PTRACE_FORK);
423 gdb_assert (pe.pe_other_pid == pid);
424 if (fpid == ptid_get_pid (inferior_ptid))
425 {
3a3e9ee3 426 ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
735f54b4
MK
427 return pid_to_ptid (fpid);
428 }
429
430 return pid_to_ptid (pid);
431 }
432 }
433#endif
434
c7c14b96
MK
435 store_waitstatus (ourstatus, status);
436 return pid_to_ptid (pid);
5bf970f9
AC
437}
438
87c336f6
AA
439/* Transfer data via ptrace into process PID's memory from WRITEBUF, or
440 from process PID's memory into READBUF. Start at target address ADDR
441 and transfer up to LEN bytes. Exactly one of READBUF and WRITEBUF must
442 be non-null. Return the number of transferred bytes. */
443
444static ULONGEST
445inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf,
446 const gdb_byte *writebuf,
447 ULONGEST addr, ULONGEST len)
448{
449 ULONGEST n;
450 unsigned int chunk;
451
452 /* We transfer aligned words. Thus align ADDR down to a word
453 boundary and determine how many bytes to skip at the
454 beginning. */
28f1c605 455 ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
87c336f6
AA
456 addr -= skip;
457
458 for (n = 0;
459 n < len;
460 n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
461 {
462 /* Restrict to a chunk that fits in the current word. */
463 chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
464
465 /* Use a union for type punning. */
466 union
467 {
468 PTRACE_TYPE_RET word;
469 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
470 } buf;
471
472 /* Read the word, also when doing a partial word write. */
473 if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
474 {
475 errno = 0;
476 buf.word = ptrace (PT_READ_I, pid,
477 (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
478 if (errno != 0)
479 break;
480 if (readbuf != NULL)
481 memcpy (readbuf + n, buf.byte + skip, chunk);
482 }
483 if (writebuf != NULL)
484 {
485 memcpy (buf.byte + skip, writebuf + n, chunk);
486 errno = 0;
487 ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
488 buf.word);
489 if (errno != 0)
490 {
491 /* Using the appropriate one (I or D) is necessary for
492 Gould NP1, at least. */
493 errno = 0;
494 ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
495 buf.word);
496 if (errno != 0)
497 break;
498 }
499 }
500 }
501
502 return n;
503}
504
edcc890f 505/* Implement the to_xfer_partial target_ops method. */
5bf970f9 506
9b409511 507static enum target_xfer_status
5bf970f9 508inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
509 const char *annex, gdb_byte *readbuf,
510 const gdb_byte *writebuf,
9b409511 511 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
5bf970f9 512{
b67aeab0 513 pid_t pid = get_ptrace_pid (inferior_ptid);
4b8a1a28 514
5bf970f9
AC
515 switch (object)
516 {
517 case TARGET_OBJECT_MEMORY:
f929a579
AC
518#ifdef PT_IO
519 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
520 request that promises to be much more efficient in reading
521 and writing data in the traced process's address space. */
522 {
523 struct ptrace_io_desc piod;
4b8a1a28 524
f929a579 525 /* NOTE: We assume that there are no distinct address spaces
b457b3dd
MK
526 for instruction and data. However, on OpenBSD 3.9 and
527 later, PIOD_WRITE_D doesn't allow changing memory that's
528 mapped read-only. Since most code segments will be
529 read-only, using PIOD_WRITE_D will prevent us from
530 inserting breakpoints, so we use PIOD_WRITE_I instead. */
531 piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
f929a579
AC
532 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
533 piod.piod_offs = (void *) (long) offset;
534 piod.piod_len = len;
535
536 errno = 0;
4b8a1a28 537 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
9b409511 538 {
9b409511 539 /* Return the actual number of bytes read or written. */
493443a4
MK
540 *xfered_len = piod.piod_len;
541 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
9b409511 542 }
f929a579
AC
543 /* If the PT_IO request is somehow not supported, fallback on
544 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
545 to indicate failure. */
546 if (errno != EINVAL)
9b409511 547 return TARGET_XFER_EOF;
f929a579
AC
548 }
549#endif
87c336f6
AA
550 *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf,
551 offset, len);
552 return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
5bf970f9
AC
553
554 case TARGET_OBJECT_UNWIND_TABLE:
2ed4b548 555 return TARGET_XFER_E_IO;
5bf970f9
AC
556
557 case TARGET_OBJECT_AUXV:
e8ace1c0
MK
558#if defined (PT_IO) && defined (PIOD_READ_AUXV)
559 /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
560 request that allows us to read the auxilliary vector. Other
561 BSD's may follow if they feel the need to support PIE. */
562 {
563 struct ptrace_io_desc piod;
564
565 if (writebuf)
2ed4b548 566 return TARGET_XFER_E_IO;
e8ace1c0
MK
567 piod.piod_op = PIOD_READ_AUXV;
568 piod.piod_addr = readbuf;
569 piod.piod_offs = (void *) (long) offset;
570 piod.piod_len = len;
571
572 errno = 0;
573 if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
9b409511 574 {
9b409511 575 /* Return the actual number of bytes read or written. */
493443a4
MK
576 *xfered_len = piod.piod_len;
577 return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
9b409511 578 }
e8ace1c0
MK
579 }
580#endif
2ed4b548 581 return TARGET_XFER_E_IO;
5bf970f9
AC
582
583 case TARGET_OBJECT_WCOOKIE:
2ed4b548 584 return TARGET_XFER_E_IO;
5bf970f9
AC
585
586 default:
2ed4b548 587 return TARGET_XFER_E_IO;
5bf970f9
AC
588 }
589}
590
4b8a1a28 591/* Return non-zero if the thread specified by PTID is alive. */
c7c14b96
MK
592
593static int
28439f5e 594inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
c7c14b96 595{
4b8a1a28
MK
596 /* ??? Is kill the right way to do this? */
597 return (kill (ptid_get_pid (ptid), 0) != -1);
c7c14b96
MK
598}
599
600/* Print status information about what we're accessing. */
601
602static void
603inf_ptrace_files_info (struct target_ops *ignore)
604{
181e7f93
PA
605 struct inferior *inf = current_inferior ();
606
4b8a1a28 607 printf_filtered (_("\tUsing the running image of %s %s.\n"),
181e7f93 608 inf->attach_flag ? "attached" : "child",
4b8a1a28 609 target_pid_to_str (inferior_ptid));
5bf970f9
AC
610}
611
7a114964 612static const char *
117de6a9
PA
613inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
614{
615 return normal_pid_to_str (ptid);
616}
617
e8ace1c0
MK
618#if defined (PT_IO) && defined (PIOD_READ_AUXV)
619
620/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
621 Return 0 if *READPTR is already at the end of the buffer.
622 Return -1 if there is insufficient buffer for a whole entry.
623 Return 1 if an entry was read into *TYPEP and *VALP. */
624
625static int
626inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
627 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
628{
f5656ead
TT
629 struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
630 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
e8ace1c0
MK
631 const int sizeof_auxv_type = TYPE_LENGTH (int_type);
632 const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
f5656ead 633 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
e8ace1c0
MK
634 gdb_byte *ptr = *readptr;
635
636 if (endptr == ptr)
637 return 0;
638
639 if (endptr - ptr < 2 * sizeof_auxv_val)
640 return -1;
641
642 *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
643 ptr += sizeof_auxv_val; /* Alignment. */
644 *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
645 ptr += sizeof_auxv_val;
646
647 *readptr = ptr;
648 return 1;
649}
650
651#endif
652
8785ced0
MK
653/* Create a prototype ptrace target. The client can override it with
654 local methods. */
655
5bf970f9
AC
656struct target_ops *
657inf_ptrace_target (void)
658{
659 struct target_ops *t = inf_child_target ();
8785ced0 660
5bf970f9 661 t->to_attach = inf_ptrace_attach;
5bf970f9
AC
662 t->to_detach = inf_ptrace_detach;
663 t->to_resume = inf_ptrace_resume;
664 t->to_wait = inf_ptrace_wait;
5bf970f9 665 t->to_files_info = inf_ptrace_files_info;
4b8a1a28 666 t->to_kill = inf_ptrace_kill;
5bf970f9 667 t->to_create_inferior = inf_ptrace_create_inferior;
735f54b4
MK
668#ifdef PT_GET_PROCESS_STATE
669 t->to_follow_fork = inf_ptrace_follow_fork;
e85e8e5e
MK
670 t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
671 t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
e4ef629d
MK
672 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
673 t->to_post_attach = inf_ptrace_post_attach;
735f54b4 674#endif
5bf970f9 675 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
5bf970f9 676 t->to_thread_alive = inf_ptrace_thread_alive;
117de6a9 677 t->to_pid_to_str = inf_ptrace_pid_to_str;
c7c14b96 678 t->to_xfer_partial = inf_ptrace_xfer_partial;
e8ace1c0
MK
679#if defined (PT_IO) && defined (PIOD_READ_AUXV)
680 t->to_auxv_parse = inf_ptrace_auxv_parse;
681#endif
8785ced0
MK
682
683 return t;
684}
685\f
686
4b8a1a28 687/* Pointer to a function that returns the offset within the user area
8785ced0 688 where a particular register is stored. */
7714d83a 689static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
8785ced0
MK
690
691/* Fetch register REGNUM from the inferior. */
692
693static void
56be3814 694inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
8785ced0 695{
ac7936df 696 struct gdbarch *gdbarch = regcache->arch ();
8785ced0
MK
697 CORE_ADDR addr;
698 size_t size;
699 PTRACE_TYPE_RET *buf;
bcc0c096
SM
700 pid_t pid;
701 int i;
8785ced0 702
7714d83a 703 /* This isn't really an address, but ptrace thinks of it as one. */
3b3b1423 704 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
8d4c1ba3 705 if (addr == (CORE_ADDR)-1
3b3b1423 706 || gdbarch_cannot_fetch_register (gdbarch, regnum))
10d6c8cd 707 {
56be3814 708 regcache_raw_supply (regcache, regnum, NULL);
10d6c8cd
DJ
709 return;
710 }
711
bcc0c096 712 pid = get_ptrace_pid (regcache_get_ptid (regcache));
8785ced0 713
3b3b1423 714 size = register_size (gdbarch, regnum);
8785ced0 715 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
4397c913 716 buf = (PTRACE_TYPE_RET *) alloca (size);
8785ced0 717
10d6c8cd 718 /* Read the register contents from the inferior a chunk at a time. */
8785ced0
MK
719 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
720 {
721 errno = 0;
f7dd0ed7 722 buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
8785ced0 723 if (errno != 0)
4b8a1a28 724 error (_("Couldn't read register %s (#%d): %s."),
3b3b1423 725 gdbarch_register_name (gdbarch, regnum),
c9f4d572 726 regnum, safe_strerror (errno));
8785ced0
MK
727
728 addr += sizeof (PTRACE_TYPE_RET);
729 }
56be3814 730 regcache_raw_supply (regcache, regnum, buf);
8785ced0
MK
731}
732
733/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
734 for all registers. */
735
736static void
28439f5e
PA
737inf_ptrace_fetch_registers (struct target_ops *ops,
738 struct regcache *regcache, int regnum)
8785ced0
MK
739{
740 if (regnum == -1)
3b3b1423 741 for (regnum = 0;
ac7936df 742 regnum < gdbarch_num_regs (regcache->arch ());
3b3b1423 743 regnum++)
56be3814 744 inf_ptrace_fetch_register (regcache, regnum);
8785ced0 745 else
56be3814 746 inf_ptrace_fetch_register (regcache, regnum);
8785ced0
MK
747}
748
749/* Store register REGNUM into the inferior. */
750
751static void
56be3814 752inf_ptrace_store_register (const struct regcache *regcache, int regnum)
8785ced0 753{
ac7936df 754 struct gdbarch *gdbarch = regcache->arch ();
8785ced0
MK
755 CORE_ADDR addr;
756 size_t size;
757 PTRACE_TYPE_RET *buf;
bcc0c096
SM
758 pid_t pid;
759 int i;
8785ced0 760
7714d83a 761 /* This isn't really an address, but ptrace thinks of it as one. */
3b3b1423 762 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
8d4c1ba3 763 if (addr == (CORE_ADDR)-1
3b3b1423 764 || gdbarch_cannot_store_register (gdbarch, regnum))
10d6c8cd
DJ
765 return;
766
bcc0c096 767 pid = get_ptrace_pid (regcache_get_ptid (regcache));
8785ced0 768
3b3b1423 769 size = register_size (gdbarch, regnum);
8785ced0 770 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
4397c913 771 buf = (PTRACE_TYPE_RET *) alloca (size);
8785ced0 772
10d6c8cd 773 /* Write the register contents into the inferior a chunk at a time. */
56be3814 774 regcache_raw_collect (regcache, regnum, buf);
8785ced0
MK
775 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
776 {
777 errno = 0;
f7dd0ed7 778 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
8785ced0 779 if (errno != 0)
4b8a1a28 780 error (_("Couldn't write register %s (#%d): %s."),
3b3b1423 781 gdbarch_register_name (gdbarch, regnum),
c9f4d572 782 regnum, safe_strerror (errno));
8785ced0
MK
783
784 addr += sizeof (PTRACE_TYPE_RET);
785 }
786}
787
788/* Store register REGNUM back into the inferior. If REGNUM is -1, do
789 this for all registers. */
790
2c0b251b 791static void
28439f5e
PA
792inf_ptrace_store_registers (struct target_ops *ops,
793 struct regcache *regcache, int regnum)
8785ced0
MK
794{
795 if (regnum == -1)
3b3b1423 796 for (regnum = 0;
ac7936df 797 regnum < gdbarch_num_regs (regcache->arch ());
3b3b1423 798 regnum++)
56be3814 799 inf_ptrace_store_register (regcache, regnum);
8785ced0 800 else
56be3814 801 inf_ptrace_store_register (regcache, regnum);
8785ced0
MK
802}
803
804/* Create a "traditional" ptrace target. REGISTER_U_OFFSET should be
805 a function returning the offset within the user area where a
806 particular register is stored. */
807
808struct target_ops *
7714d83a
UW
809inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
810 (struct gdbarch *, int, int))
8785ced0
MK
811{
812 struct target_ops *t = inf_ptrace_target();
813
814 gdb_assert (register_u_offset);
815 inf_ptrace_register_u_offset = register_u_offset;
816 t->to_fetch_registers = inf_ptrace_fetch_registers;
817 t->to_store_registers = inf_ptrace_store_registers;
818
5bf970f9
AC
819 return t;
820}
This page took 1.133942 seconds and 4 git commands to generate.