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