e1d64f8cd7eec08e7e88a40bd2535acc0076ab91
[external/binutils.git] / gdb / inf-ptrace.c
1 /* Low-level child interface to ptrace.
2
3    Copyright (C) 1988-2014 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
28 #include "gdb_assert.h"
29 #include <string.h>
30 #include "gdb_ptrace.h"
31 #include "gdb_wait.h"
32 #include <signal.h>
33
34 #include "inf-ptrace.h"
35 #include "inf-child.h"
36 #include "gdbthread.h"
37
38 \f
39
40 #ifdef PT_GET_PROCESS_STATE
41
42 static int
43 inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
44                         int detach_fork)
45 {
46   pid_t pid, fpid;
47   ptrace_state_t pe;
48
49   pid = ptid_get_pid (inferior_ptid);
50
51   if (ptrace (PT_GET_PROCESS_STATE, pid,
52                (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
53     perror_with_name (("ptrace"));
54
55   gdb_assert (pe.pe_report_event == PTRACE_FORK);
56   fpid = pe.pe_other_pid;
57
58   if (follow_child)
59     {
60       struct inferior *parent_inf, *child_inf;
61       struct thread_info *tp;
62
63       parent_inf = find_inferior_pid (pid);
64
65       /* Add the child.  */
66       child_inf = add_inferior (fpid);
67       child_inf->attach_flag = parent_inf->attach_flag;
68       copy_terminal_info (child_inf, parent_inf);
69       child_inf->pspace = parent_inf->pspace;
70       child_inf->aspace = parent_inf->aspace;
71
72       /* Before detaching from the parent, remove all breakpoints from
73          it.  */
74       remove_breakpoints ();
75
76       if (ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
77         perror_with_name (("ptrace"));
78
79       /* Switch inferior_ptid out of the parent's way.  */
80       inferior_ptid = pid_to_ptid (fpid);
81
82       /* Delete the parent.  */
83       detach_inferior (pid);
84
85       add_thread_silent (inferior_ptid);
86     }
87   else
88     {
89       /* Breakpoints have already been detached from the child by
90          infrun.c.  */
91
92       if (ptrace (PT_DETACH, fpid, (PTRACE_TYPE_ARG3)1, 0) == -1)
93         perror_with_name (("ptrace"));
94     }
95
96   return 0;
97 }
98
99 #endif /* PT_GET_PROCESS_STATE */
100 \f
101
102 /* Prepare to be traced.  */
103
104 static void
105 inf_ptrace_me (void)
106 {
107   /* "Trace me, Dr. Memory!"  */
108   ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
109 }
110
111 /* Start a new inferior Unix child process.  EXEC_FILE is the file to
112    run, ALLARGS is a string containing the arguments to the program.
113    ENV is the environment vector to pass.  If FROM_TTY is non-zero, be
114    chatty about it.  */
115
116 static void
117 inf_ptrace_create_inferior (struct target_ops *ops,
118                             char *exec_file, char *allargs, char **env,
119                             int from_tty)
120 {
121   int pid;
122
123   /* Do not change either targets above or the same target if already present.
124      The reason is the target stack is shared across multiple inferiors.  */
125   int ops_already_pushed = target_is_pushed (ops);
126   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
127
128   if (! ops_already_pushed)
129     {
130       /* Clear possible core file with its process_stratum.  */
131       push_target (ops);
132       make_cleanup_unpush_target (ops);
133     }
134
135   pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
136                        NULL, NULL, NULL);
137
138   discard_cleanups (back_to);
139
140   startup_inferior (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 (pid_to_ptid (pid));
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   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
194
195   pid = parse_pid_to_attach (args);
196
197   if (pid == getpid ())         /* Trying to masturbate?  */
198     error (_("I refuse to debug myself!"));
199
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       make_cleanup_unpush_target (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   add_thread_silent (inferior_ptid);
239
240   discard_cleanups (back_to);
241 }
242
243 #ifdef PT_GET_PROCESS_STATE
244
245 static void
246 inf_ptrace_post_attach (struct target_ops *self, int pid)
247 {
248   ptrace_event_t pe;
249
250   /* Set the initial event mask.  */
251   memset (&pe, 0, sizeof pe);
252   pe.pe_set_event |= PTRACE_FORK;
253   if (ptrace (PT_SET_EVENT_MASK, pid,
254               (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
255     perror_with_name (("ptrace"));
256 }
257
258 #endif
259
260 /* Detach from the inferior, optionally passing it the signal
261    specified by ARGS.  If FROM_TTY is non-zero, be chatty about it.  */
262
263 static void
264 inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
265 {
266   pid_t pid = ptid_get_pid (inferior_ptid);
267   int sig = 0;
268
269   if (from_tty)
270     {
271       char *exec_file = get_exec_file (0);
272       if (exec_file == 0)
273         exec_file = "";
274       printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
275                          target_pid_to_str (pid_to_ptid (pid)));
276       gdb_flush (gdb_stdout);
277     }
278   if (args)
279     sig = atoi (args);
280
281 #ifdef PT_DETACH
282   /* We'd better not have left any breakpoints in the program or it'll
283      die when it hits one.  Also note that this may only work if we
284      previously attached to the inferior.  It *might* work if we
285      started the process ourselves.  */
286   errno = 0;
287   ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
288   if (errno != 0)
289     perror_with_name (("ptrace"));
290 #else
291   error (_("This system does not support detaching from a process"));
292 #endif
293
294   inferior_ptid = null_ptid;
295   detach_inferior (pid);
296
297   inf_child_maybe_unpush_target (ops);
298 }
299
300 /* Kill the inferior.  */
301
302 static void
303 inf_ptrace_kill (struct target_ops *ops)
304 {
305   pid_t pid = ptid_get_pid (inferior_ptid);
306   int status;
307
308   if (pid == 0)
309     return;
310
311   ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
312   waitpid (pid, &status, 0);
313
314   target_mourn_inferior ();
315 }
316
317 /* Stop the inferior.  */
318
319 static void
320 inf_ptrace_stop (struct target_ops *self, ptid_t ptid)
321 {
322   /* Send a SIGINT to the process group.  This acts just like the user
323      typed a ^C on the controlling terminal.  Note that using a
324      negative process number in kill() is a System V-ism.  The proper
325      BSD interface is killpg().  However, all modern BSDs support the
326      System V interface too.  */
327   kill (-inferior_process_group (), SIGINT);
328 }
329
330 /* Resume execution of thread PTID, or all threads if PTID is -1.  If
331    STEP is nonzero, single-step it.  If SIGNAL is nonzero, give it
332    that signal.  */
333
334 static void
335 inf_ptrace_resume (struct target_ops *ops,
336                    ptid_t ptid, int step, enum gdb_signal signal)
337 {
338   pid_t pid = ptid_get_pid (ptid);
339   int request;
340
341   if (pid == -1)
342     /* Resume all threads.  Traditionally ptrace() only supports
343        single-threaded processes, so simply resume the inferior.  */
344     pid = ptid_get_pid (inferior_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 /* Implement the to_xfer_partial target_ops method.  */
455
456 static enum target_xfer_status
457 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
458                          const char *annex, gdb_byte *readbuf,
459                          const gdb_byte *writebuf,
460                          ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
461 {
462   pid_t pid = ptid_get_pid (inferior_ptid);
463
464   switch (object)
465     {
466     case TARGET_OBJECT_MEMORY:
467 #ifdef PT_IO
468       /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
469          request that promises to be much more efficient in reading
470          and writing data in the traced process's address space.  */
471       {
472         struct ptrace_io_desc piod;
473
474         /* NOTE: We assume that there are no distinct address spaces
475            for instruction and data.  However, on OpenBSD 3.9 and
476            later, PIOD_WRITE_D doesn't allow changing memory that's
477            mapped read-only.  Since most code segments will be
478            read-only, using PIOD_WRITE_D will prevent us from
479            inserting breakpoints, so we use PIOD_WRITE_I instead.  */
480         piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
481         piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
482         piod.piod_offs = (void *) (long) offset;
483         piod.piod_len = len;
484
485         errno = 0;
486         if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
487           {
488             /* Return the actual number of bytes read or written.  */
489             *xfered_len = piod.piod_len;
490             return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
491           }
492         /* If the PT_IO request is somehow not supported, fallback on
493            using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
494            to indicate failure.  */
495         if (errno != EINVAL)
496           return TARGET_XFER_EOF;
497       }
498 #endif
499       {
500         union
501         {
502           PTRACE_TYPE_RET word;
503           gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
504         } buffer;
505         ULONGEST rounded_offset;
506         ULONGEST partial_len;
507
508         /* Round the start offset down to the next long word
509            boundary.  */
510         rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
511
512         /* Since ptrace will transfer a single word starting at that
513            rounded_offset the partial_len needs to be adjusted down to
514            that (remember this function only does a single transfer).
515            Should the required length be even less, adjust it down
516            again.  */
517         partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
518         if (partial_len > len)
519           partial_len = len;
520
521         if (writebuf)
522           {
523             /* If OFFSET:PARTIAL_LEN is smaller than
524                ROUNDED_OFFSET:WORDSIZE then a read/modify write will
525                be needed.  Read in the entire word.  */
526             if (rounded_offset < offset
527                 || (offset + partial_len
528                     < rounded_offset + sizeof (PTRACE_TYPE_RET)))
529               /* Need part of initial word -- fetch it.  */
530               buffer.word = ptrace (PT_READ_I, pid,
531                                     (PTRACE_TYPE_ARG3)(uintptr_t)
532                                     rounded_offset, 0);
533
534             /* Copy data to be written over corresponding part of
535                buffer.  */
536             memcpy (buffer.byte + (offset - rounded_offset),
537                     writebuf, partial_len);
538
539             errno = 0;
540             ptrace (PT_WRITE_D, pid,
541                     (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
542                     buffer.word);
543             if (errno)
544               {
545                 /* Using the appropriate one (I or D) is necessary for
546                    Gould NP1, at least.  */
547                 errno = 0;
548                 ptrace (PT_WRITE_I, pid,
549                         (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
550                         buffer.word);
551                 if (errno)
552                   return TARGET_XFER_EOF;
553               }
554           }
555
556         if (readbuf)
557           {
558             errno = 0;
559             buffer.word = ptrace (PT_READ_I, pid,
560                                   (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
561                                   0);
562             if (errno)
563               return TARGET_XFER_EOF;
564             /* Copy appropriate bytes out of the buffer.  */
565             memcpy (readbuf, buffer.byte + (offset - rounded_offset),
566                     partial_len);
567           }
568
569         *xfered_len = partial_len;
570         return TARGET_XFER_OK;
571       }
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 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_post_startup_inferior = inf_ptrace_post_startup_inferior;
690   t->to_post_attach = inf_ptrace_post_attach;
691 #endif
692   t->to_mourn_inferior = inf_ptrace_mourn_inferior;
693   t->to_thread_alive = inf_ptrace_thread_alive;
694   t->to_pid_to_str = inf_ptrace_pid_to_str;
695   t->to_stop = inf_ptrace_stop;
696   t->to_xfer_partial = inf_ptrace_xfer_partial;
697 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
698   t->to_auxv_parse = inf_ptrace_auxv_parse;
699 #endif
700
701   return t;
702 }
703 \f
704
705 /* Pointer to a function that returns the offset within the user area
706    where a particular register is stored.  */
707 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
708
709 /* Fetch register REGNUM from the inferior.  */
710
711 static void
712 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
713 {
714   struct gdbarch *gdbarch = get_regcache_arch (regcache);
715   CORE_ADDR addr;
716   size_t size;
717   PTRACE_TYPE_RET *buf;
718   int pid, i;
719
720   /* This isn't really an address, but ptrace thinks of it as one.  */
721   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
722   if (addr == (CORE_ADDR)-1
723       || gdbarch_cannot_fetch_register (gdbarch, regnum))
724     {
725       regcache_raw_supply (regcache, regnum, NULL);
726       return;
727     }
728
729   /* Cater for systems like GNU/Linux, that implement threads as
730      separate processes.  */
731   pid = ptid_get_lwp (inferior_ptid);
732   if (pid == 0)
733     pid = ptid_get_pid (inferior_ptid);
734
735   size = register_size (gdbarch, regnum);
736   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
737   buf = alloca (size);
738
739   /* Read the register contents from the inferior a chunk at a time.  */
740   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
741     {
742       errno = 0;
743       buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
744       if (errno != 0)
745         error (_("Couldn't read register %s (#%d): %s."),
746                gdbarch_register_name (gdbarch, regnum),
747                regnum, safe_strerror (errno));
748
749       addr += sizeof (PTRACE_TYPE_RET);
750     }
751   regcache_raw_supply (regcache, regnum, buf);
752 }
753
754 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
755    for all registers.  */
756
757 static void
758 inf_ptrace_fetch_registers (struct target_ops *ops,
759                             struct regcache *regcache, int regnum)
760 {
761   if (regnum == -1)
762     for (regnum = 0;
763          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
764          regnum++)
765       inf_ptrace_fetch_register (regcache, regnum);
766   else
767     inf_ptrace_fetch_register (regcache, regnum);
768 }
769
770 /* Store register REGNUM into the inferior.  */
771
772 static void
773 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
774 {
775   struct gdbarch *gdbarch = get_regcache_arch (regcache);
776   CORE_ADDR addr;
777   size_t size;
778   PTRACE_TYPE_RET *buf;
779   int pid, i;
780
781   /* This isn't really an address, but ptrace thinks of it as one.  */
782   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
783   if (addr == (CORE_ADDR)-1 
784       || gdbarch_cannot_store_register (gdbarch, regnum))
785     return;
786
787   /* Cater for systems like GNU/Linux, that implement threads as
788      separate processes.  */
789   pid = ptid_get_lwp (inferior_ptid);
790   if (pid == 0)
791     pid = ptid_get_pid (inferior_ptid);
792
793   size = register_size (gdbarch, regnum);
794   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
795   buf = alloca (size);
796
797   /* Write the register contents into the inferior a chunk at a time.  */
798   regcache_raw_collect (regcache, regnum, buf);
799   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
800     {
801       errno = 0;
802       ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
803       if (errno != 0)
804         error (_("Couldn't write register %s (#%d): %s."),
805                gdbarch_register_name (gdbarch, regnum),
806                regnum, safe_strerror (errno));
807
808       addr += sizeof (PTRACE_TYPE_RET);
809     }
810 }
811
812 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
813    this for all registers.  */
814
815 static void
816 inf_ptrace_store_registers (struct target_ops *ops,
817                             struct regcache *regcache, int regnum)
818 {
819   if (regnum == -1)
820     for (regnum = 0;
821          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
822          regnum++)
823       inf_ptrace_store_register (regcache, regnum);
824   else
825     inf_ptrace_store_register (regcache, regnum);
826 }
827
828 /* Create a "traditional" ptrace target.  REGISTER_U_OFFSET should be
829    a function returning the offset within the user area where a
830    particular register is stored.  */
831
832 struct target_ops *
833 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
834                                         (struct gdbarch *, int, int))
835 {
836   struct target_ops *t = inf_ptrace_target();
837
838   gdb_assert (register_u_offset);
839   inf_ptrace_register_u_offset = register_u_offset;
840   t->to_fetch_registers = inf_ptrace_fetch_registers;
841   t->to_store_registers = inf_ptrace_store_registers;
842
843   return t;
844 }