cleanup fixes for inf-ptrace.c
[external/binutils.git] / gdb / inf-ptrace.c
1 /* Low-level child interface to ptrace.
2
3    Copyright (C) 1988-2013 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 "gdb_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 {
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   /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
140      be 1 or 2 depending on whether we're starting without or with a
141      shell.  */
142   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
143
144   /* On some targets, there must be some explicit actions taken after
145      the inferior has been started up.  */
146   target_post_startup_inferior (pid_to_ptid (pid));
147 }
148
149 #ifdef PT_GET_PROCESS_STATE
150
151 static void
152 inf_ptrace_post_startup_inferior (ptid_t pid)
153 {
154   ptrace_event_t pe;
155
156   /* Set the initial event mask.  */
157   memset (&pe, 0, sizeof pe);
158   pe.pe_set_event |= PTRACE_FORK;
159   if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
160               (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
161     perror_with_name (("ptrace"));
162 }
163
164 #endif
165
166 /* Clean up a rotting corpse of an inferior after it died.  */
167
168 static void
169 inf_ptrace_mourn_inferior (struct target_ops *ops)
170 {
171   int status;
172
173   /* Wait just one more time to collect the inferior's exit status.
174      Do not check whether this succeeds though, since we may be
175      dealing with a process that we attached to.  Such a process will
176      only report its exit status to its original parent.  */
177   waitpid (ptid_get_pid (inferior_ptid), &status, 0);
178
179   generic_mourn_inferior ();
180
181   if (!have_inferiors ())
182     unpush_target (ops);
183 }
184
185 /* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
186    be chatty about it.  */
187
188 static void
189 inf_ptrace_attach (struct target_ops *ops, char *args, int from_tty)
190 {
191   char *exec_file;
192   pid_t pid;
193   struct inferior *inf;
194
195   /* Do not change either targets above or the same target if already present.
196      The reason is the target stack is shared across multiple inferiors.  */
197   int ops_already_pushed = target_is_pushed (ops);
198   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
199
200   pid = parse_pid_to_attach (args);
201
202   if (pid == getpid ())         /* Trying to masturbate?  */
203     error (_("I refuse to debug myself!"));
204
205   if (! ops_already_pushed)
206     {
207       /* target_pid_to_str already uses the target.  Also clear possible core
208          file with its process_stratum.  */
209       push_target (ops);
210       make_cleanup_unpush_target (ops);
211     }
212
213   if (from_tty)
214     {
215       exec_file = get_exec_file (0);
216
217       if (exec_file)
218         printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
219                            target_pid_to_str (pid_to_ptid (pid)));
220       else
221         printf_unfiltered (_("Attaching to %s\n"),
222                            target_pid_to_str (pid_to_ptid (pid)));
223
224       gdb_flush (gdb_stdout);
225     }
226
227 #ifdef PT_ATTACH
228   errno = 0;
229   ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
230   if (errno != 0)
231     perror_with_name (("ptrace"));
232 #else
233   error (_("This system does not support attaching to a process"));
234 #endif
235
236   inf = current_inferior ();
237   inferior_appeared (inf, pid);
238   inf->attach_flag = 1;
239   inferior_ptid = pid_to_ptid (pid);
240
241   /* Always add a main thread.  If some target extends the ptrace
242      target, it should decorate the ptid later with more info.  */
243   add_thread_silent (inferior_ptid);
244
245   discard_cleanups (back_to);
246 }
247
248 #ifdef PT_GET_PROCESS_STATE
249
250 static void
251 inf_ptrace_post_attach (int pid)
252 {
253   ptrace_event_t pe;
254
255   /* Set the initial event mask.  */
256   memset (&pe, 0, sizeof pe);
257   pe.pe_set_event |= PTRACE_FORK;
258   if (ptrace (PT_SET_EVENT_MASK, pid,
259               (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
260     perror_with_name (("ptrace"));
261 }
262
263 #endif
264
265 /* Detach from the inferior, optionally passing it the signal
266    specified by ARGS.  If FROM_TTY is non-zero, be chatty about it.  */
267
268 static void
269 inf_ptrace_detach (struct target_ops *ops, char *args, int from_tty)
270 {
271   pid_t pid = ptid_get_pid (inferior_ptid);
272   int sig = 0;
273
274   if (from_tty)
275     {
276       char *exec_file = get_exec_file (0);
277       if (exec_file == 0)
278         exec_file = "";
279       printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
280                          target_pid_to_str (pid_to_ptid (pid)));
281       gdb_flush (gdb_stdout);
282     }
283   if (args)
284     sig = atoi (args);
285
286 #ifdef PT_DETACH
287   /* We'd better not have left any breakpoints in the program or it'll
288      die when it hits one.  Also note that this may only work if we
289      previously attached to the inferior.  It *might* work if we
290      started the process ourselves.  */
291   errno = 0;
292   ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
293   if (errno != 0)
294     perror_with_name (("ptrace"));
295 #else
296   error (_("This system does not support detaching from a process"));
297 #endif
298
299   inferior_ptid = null_ptid;
300   detach_inferior (pid);
301
302   if (!have_inferiors ())
303     unpush_target (ops);
304 }
305
306 /* Kill the inferior.  */
307
308 static void
309 inf_ptrace_kill (struct target_ops *ops)
310 {
311   pid_t pid = ptid_get_pid (inferior_ptid);
312   int status;
313
314   if (pid == 0)
315     return;
316
317   ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
318   waitpid (pid, &status, 0);
319
320   target_mourn_inferior ();
321 }
322
323 /* Stop the inferior.  */
324
325 static void
326 inf_ptrace_stop (ptid_t ptid)
327 {
328   /* Send a SIGINT to the process group.  This acts just like the user
329      typed a ^C on the controlling terminal.  Note that using a
330      negative process number in kill() is a System V-ism.  The proper
331      BSD interface is killpg().  However, all modern BSDs support the
332      System V interface too.  */
333   kill (-inferior_process_group (), SIGINT);
334 }
335
336 /* Resume execution of thread PTID, or all threads if PTID is -1.  If
337    STEP is nonzero, single-step it.  If SIGNAL is nonzero, give it
338    that signal.  */
339
340 static void
341 inf_ptrace_resume (struct target_ops *ops,
342                    ptid_t ptid, int step, enum gdb_signal signal)
343 {
344   pid_t pid = ptid_get_pid (ptid);
345   int request;
346
347   if (pid == -1)
348     /* Resume all threads.  Traditionally ptrace() only supports
349        single-threaded processes, so simply resume the inferior.  */
350     pid = ptid_get_pid (inferior_ptid);
351
352   if (catch_syscall_enabled () > 0)
353     request = PT_SYSCALL;
354   else
355     request = PT_CONTINUE;
356
357   if (step)
358     {
359       /* If this system does not support PT_STEP, a higher level
360          function will have called single_step() to transmute the step
361          request into a continue request (by setting breakpoints on
362          all possible successor instructions), so we don't have to
363          worry about that here.  */
364       request = PT_STEP;
365     }
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, gdb_signal_to_host (signal));
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 /* Attempt a transfer all LEN bytes starting at OFFSET between the
461    inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
462    Return the number of bytes actually transferred.  */
463
464 static LONGEST
465 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
466                          const char *annex, gdb_byte *readbuf,
467                          const gdb_byte *writebuf,
468                          ULONGEST offset, LONGEST len)
469 {
470   pid_t pid = ptid_get_pid (inferior_ptid);
471
472   switch (object)
473     {
474     case TARGET_OBJECT_MEMORY:
475 #ifdef PT_IO
476       /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
477          request that promises to be much more efficient in reading
478          and writing data in the traced process's address space.  */
479       {
480         struct ptrace_io_desc piod;
481
482         /* NOTE: We assume that there are no distinct address spaces
483            for instruction and data.  However, on OpenBSD 3.9 and
484            later, PIOD_WRITE_D doesn't allow changing memory that's
485            mapped read-only.  Since most code segments will be
486            read-only, using PIOD_WRITE_D will prevent us from
487            inserting breakpoints, so we use PIOD_WRITE_I instead.  */
488         piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
489         piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
490         piod.piod_offs = (void *) (long) offset;
491         piod.piod_len = len;
492
493         errno = 0;
494         if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
495           /* Return the actual number of bytes read or written.  */
496           return piod.piod_len;
497         /* If the PT_IO request is somehow not supported, fallback on
498            using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
499            to indicate failure.  */
500         if (errno != EINVAL)
501           return 0;
502       }
503 #endif
504       {
505         union
506         {
507           PTRACE_TYPE_RET word;
508           gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
509         } buffer;
510         ULONGEST rounded_offset;
511         LONGEST partial_len;
512
513         /* Round the start offset down to the next long word
514            boundary.  */
515         rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
516
517         /* Since ptrace will transfer a single word starting at that
518            rounded_offset the partial_len needs to be adjusted down to
519            that (remember this function only does a single transfer).
520            Should the required length be even less, adjust it down
521            again.  */
522         partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
523         if (partial_len > len)
524           partial_len = len;
525
526         if (writebuf)
527           {
528             /* If OFFSET:PARTIAL_LEN is smaller than
529                ROUNDED_OFFSET:WORDSIZE then a read/modify write will
530                be needed.  Read in the entire word.  */
531             if (rounded_offset < offset
532                 || (offset + partial_len
533                     < rounded_offset + sizeof (PTRACE_TYPE_RET)))
534               /* Need part of initial word -- fetch it.  */
535               buffer.word = ptrace (PT_READ_I, pid,
536                                     (PTRACE_TYPE_ARG3)(uintptr_t)
537                                     rounded_offset, 0);
538
539             /* Copy data to be written over corresponding part of
540                buffer.  */
541             memcpy (buffer.byte + (offset - rounded_offset),
542                     writebuf, partial_len);
543
544             errno = 0;
545             ptrace (PT_WRITE_D, pid,
546                     (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
547                     buffer.word);
548             if (errno)
549               {
550                 /* Using the appropriate one (I or D) is necessary for
551                    Gould NP1, at least.  */
552                 errno = 0;
553                 ptrace (PT_WRITE_I, pid,
554                         (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
555                         buffer.word);
556                 if (errno)
557                   return 0;
558               }
559           }
560
561         if (readbuf)
562           {
563             errno = 0;
564             buffer.word = ptrace (PT_READ_I, pid,
565                                   (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
566                                   0);
567             if (errno)
568               return 0;
569             /* Copy appropriate bytes out of the buffer.  */
570             memcpy (readbuf, buffer.byte + (offset - rounded_offset),
571                     partial_len);
572           }
573
574         return partial_len;
575       }
576
577     case TARGET_OBJECT_UNWIND_TABLE:
578       return -1;
579
580     case TARGET_OBJECT_AUXV:
581 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
582       /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
583          request that allows us to read the auxilliary vector.  Other
584          BSD's may follow if they feel the need to support PIE.  */
585       {
586         struct ptrace_io_desc piod;
587
588         if (writebuf)
589           return -1;
590         piod.piod_op = PIOD_READ_AUXV;
591         piod.piod_addr = readbuf;
592         piod.piod_offs = (void *) (long) offset;
593         piod.piod_len = len;
594
595         errno = 0;
596         if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
597           /* Return the actual number of bytes read or written.  */
598           return piod.piod_len;
599       }
600 #endif
601       return -1;
602
603     case TARGET_OBJECT_WCOOKIE:
604       return -1;
605
606     default:
607       return -1;
608     }
609 }
610
611 /* Return non-zero if the thread specified by PTID is alive.  */
612
613 static int
614 inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
615 {
616   /* ??? Is kill the right way to do this?  */
617   return (kill (ptid_get_pid (ptid), 0) != -1);
618 }
619
620 /* Print status information about what we're accessing.  */
621
622 static void
623 inf_ptrace_files_info (struct target_ops *ignore)
624 {
625   struct inferior *inf = current_inferior ();
626
627   printf_filtered (_("\tUsing the running image of %s %s.\n"),
628                    inf->attach_flag ? "attached" : "child",
629                    target_pid_to_str (inferior_ptid));
630 }
631
632 static char *
633 inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
634 {
635   return normal_pid_to_str (ptid);
636 }
637
638 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
639
640 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
641    Return 0 if *READPTR is already at the end of the buffer.
642    Return -1 if there is insufficient buffer for a whole entry.
643    Return 1 if an entry was read into *TYPEP and *VALP.  */
644
645 static int
646 inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
647                        gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
648 {
649   struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
650   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
651   const int sizeof_auxv_type = TYPE_LENGTH (int_type);
652   const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
653   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
654   gdb_byte *ptr = *readptr;
655
656   if (endptr == ptr)
657     return 0;
658
659   if (endptr - ptr < 2 * sizeof_auxv_val)
660     return -1;
661
662   *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
663   ptr += sizeof_auxv_val;       /* Alignment.  */
664   *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
665   ptr += sizeof_auxv_val;
666
667   *readptr = ptr;
668   return 1;
669 }
670
671 #endif
672
673 /* Create a prototype ptrace target.  The client can override it with
674    local methods.  */
675
676 struct target_ops *
677 inf_ptrace_target (void)
678 {
679   struct target_ops *t = inf_child_target ();
680
681   t->to_attach = inf_ptrace_attach;
682   t->to_detach = inf_ptrace_detach;
683   t->to_resume = inf_ptrace_resume;
684   t->to_wait = inf_ptrace_wait;
685   t->to_files_info = inf_ptrace_files_info;
686   t->to_kill = inf_ptrace_kill;
687   t->to_create_inferior = inf_ptrace_create_inferior;
688 #ifdef PT_GET_PROCESS_STATE
689   t->to_follow_fork = inf_ptrace_follow_fork;
690   t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
691   t->to_post_attach = inf_ptrace_post_attach;
692 #endif
693   t->to_mourn_inferior = inf_ptrace_mourn_inferior;
694   t->to_thread_alive = inf_ptrace_thread_alive;
695   t->to_pid_to_str = inf_ptrace_pid_to_str;
696   t->to_stop = inf_ptrace_stop;
697   t->to_xfer_partial = inf_ptrace_xfer_partial;
698 #if defined (PT_IO) && defined (PIOD_READ_AUXV)
699   t->to_auxv_parse = inf_ptrace_auxv_parse;
700 #endif
701
702   return t;
703 }
704 \f
705
706 /* Pointer to a function that returns the offset within the user area
707    where a particular register is stored.  */
708 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
709
710 /* Fetch register REGNUM from the inferior.  */
711
712 static void
713 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
714 {
715   struct gdbarch *gdbarch = get_regcache_arch (regcache);
716   CORE_ADDR addr;
717   size_t size;
718   PTRACE_TYPE_RET *buf;
719   int pid, i;
720
721   /* This isn't really an address, but ptrace thinks of it as one.  */
722   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
723   if (addr == (CORE_ADDR)-1
724       || gdbarch_cannot_fetch_register (gdbarch, regnum))
725     {
726       regcache_raw_supply (regcache, regnum, NULL);
727       return;
728     }
729
730   /* Cater for systems like GNU/Linux, that implement threads as
731      separate processes.  */
732   pid = ptid_get_lwp (inferior_ptid);
733   if (pid == 0)
734     pid = ptid_get_pid (inferior_ptid);
735
736   size = register_size (gdbarch, regnum);
737   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
738   buf = alloca (size);
739
740   /* Read the register contents from the inferior a chunk at a time.  */
741   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
742     {
743       errno = 0;
744       buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
745       if (errno != 0)
746         error (_("Couldn't read register %s (#%d): %s."),
747                gdbarch_register_name (gdbarch, regnum),
748                regnum, safe_strerror (errno));
749
750       addr += sizeof (PTRACE_TYPE_RET);
751     }
752   regcache_raw_supply (regcache, regnum, buf);
753 }
754
755 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
756    for all registers.  */
757
758 static void
759 inf_ptrace_fetch_registers (struct target_ops *ops,
760                             struct regcache *regcache, int regnum)
761 {
762   if (regnum == -1)
763     for (regnum = 0;
764          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
765          regnum++)
766       inf_ptrace_fetch_register (regcache, regnum);
767   else
768     inf_ptrace_fetch_register (regcache, regnum);
769 }
770
771 /* Store register REGNUM into the inferior.  */
772
773 static void
774 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
775 {
776   struct gdbarch *gdbarch = get_regcache_arch (regcache);
777   CORE_ADDR addr;
778   size_t size;
779   PTRACE_TYPE_RET *buf;
780   int pid, i;
781
782   /* This isn't really an address, but ptrace thinks of it as one.  */
783   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
784   if (addr == (CORE_ADDR)-1 
785       || gdbarch_cannot_store_register (gdbarch, regnum))
786     return;
787
788   /* Cater for systems like GNU/Linux, that implement threads as
789      separate processes.  */
790   pid = ptid_get_lwp (inferior_ptid);
791   if (pid == 0)
792     pid = ptid_get_pid (inferior_ptid);
793
794   size = register_size (gdbarch, regnum);
795   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
796   buf = alloca (size);
797
798   /* Write the register contents into the inferior a chunk at a time.  */
799   regcache_raw_collect (regcache, regnum, buf);
800   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
801     {
802       errno = 0;
803       ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
804       if (errno != 0)
805         error (_("Couldn't write register %s (#%d): %s."),
806                gdbarch_register_name (gdbarch, regnum),
807                regnum, safe_strerror (errno));
808
809       addr += sizeof (PTRACE_TYPE_RET);
810     }
811 }
812
813 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
814    this for all registers.  */
815
816 static void
817 inf_ptrace_store_registers (struct target_ops *ops,
818                             struct regcache *regcache, int regnum)
819 {
820   if (regnum == -1)
821     for (regnum = 0;
822          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
823          regnum++)
824       inf_ptrace_store_register (regcache, regnum);
825   else
826     inf_ptrace_store_register (regcache, regnum);
827 }
828
829 /* Create a "traditional" ptrace target.  REGISTER_U_OFFSET should be
830    a function returning the offset within the user area where a
831    particular register is stored.  */
832
833 struct target_ops *
834 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
835                                         (struct gdbarch *, int, int))
836 {
837   struct target_ops *t = inf_ptrace_target();
838
839   gdb_assert (register_u_offset);
840   inf_ptrace_register_u_offset = register_u_offset;
841   t->to_fetch_registers = inf_ptrace_fetch_registers;
842   t->to_store_registers = inf_ptrace_store_registers;
843
844   return t;
845 }