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