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