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