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