821e4240ffbab27d53b64f5cf30132dbc7c2db09
[platform/upstream/binutils.git] / gdb / inf-ptrace.c
1 /* Low-level child interface to ptrace.
2
3    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4    1998, 1999, 2000, 2001, 2002, 2004, 2005
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "command.h"
26 #include "inferior.h"
27 #include "inflow.h"
28 #include "gdbcore.h"
29 #include "observer.h"
30 #include "regcache.h"
31
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
34 #include "gdb_ptrace.h"
35 #include "gdb_wait.h"
36 #include <signal.h>
37
38 #include "inf-child.h"
39
40 /* HACK: Save the ptrace ops returned by inf_ptrace_target.  */
41 static struct target_ops *ptrace_ops_hack;
42
43 static void
44 inf_ptrace_kill_inferior (void)
45 {
46   int status;
47   int pid = PIDGET (inferior_ptid);
48
49   if (pid == 0)
50     return;
51
52   /* This once used to call "kill" to kill the inferior just in case
53      the inferior was still running.  As others have noted in the past
54      (kingdon) there shouldn't be any way to get here if the inferior
55      is still running -- else there's a major problem elsewere in GDB
56      and it needs to be fixed.
57
58      The kill call causes problems under HP-UX 10, so it's been
59      removed; if this causes problems we'll deal with them as they
60      arise.  */
61   ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
62   wait (&status);
63   target_mourn_inferior ();
64 }
65
66 /* Resume execution of the inferior process.  If STEP is nonzero,
67    single-step it.  If SIGNAL is nonzero, give it that signal.  */
68
69 static void
70 inf_ptrace_resume (ptid_t ptid, int step, enum target_signal signal)
71 {
72   int request = PT_CONTINUE;
73   int pid = PIDGET (ptid);
74
75   if (pid == -1)
76     /* Resume all threads.  */
77     /* I think this only gets used in the non-threaded case, where
78        "resume all threads" and "resume inferior_ptid" are the
79        same.  */
80     pid = PIDGET (inferior_ptid);
81
82   if (step)
83     {
84       /* If this system does not support PT_STEP, a higher level
85          function will have called single_step() to transmute the step
86          request into a continue request (by setting breakpoints on
87          all possible successor instructions), so we don't have to
88          worry about that here.  */
89       request = PT_STEP;
90     }
91
92   /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
93      where it was.  If GDB wanted it to start some other way, we have
94      already written a new PC value to the child.  */
95   errno = 0;
96   ptrace (request, pid, (PTRACE_TYPE_ARG3) 1, target_signal_to_host (signal));
97   if (errno != 0)
98     perror_with_name ("ptrace");
99 }
100
101 /* Wait for child to do something.  Return pid of child, or -1 in case
102    of error; store status through argument pointer OURSTATUS.  */
103
104 static ptid_t
105 inf_ptrace_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
106 {
107   int save_errno;
108   int status;
109   char *execd_pathname = NULL;
110   int exit_status;
111   int related_pid;
112   int syscall_id;
113   enum target_waitkind kind;
114   int pid;
115
116   do
117     {
118       set_sigint_trap ();       /* Causes SIGINT to be passed on to the
119                                    attached process. */
120       set_sigio_trap ();
121
122       pid = wait (&status);
123
124       save_errno = errno;
125
126       clear_sigio_trap ();
127
128       clear_sigint_trap ();
129
130       if (pid == -1)
131         {
132           if (save_errno == EINTR)
133             continue;
134
135           fprintf_unfiltered (gdb_stderr,
136                               "Child process unexpectedly missing: %s.\n",
137                               safe_strerror (save_errno));
138
139           /* Claim it exited with unknown signal.  */
140           ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
141           ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
142           return pid_to_ptid (-1);
143         }
144
145       /* Did it exit?  */
146       if (target_has_exited (pid, status, &exit_status))
147         {
148           /* ??rehrauer: For now, ignore this. */
149           continue;
150         }
151
152       if (!target_thread_alive (pid_to_ptid (pid)))
153         {
154           ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
155           return pid_to_ptid (pid);
156         }
157     }
158   while (pid != PIDGET (inferior_ptid)); /* Some other child died or
159                                             stopped.  */
160
161   store_waitstatus (ourstatus, status);
162   return pid_to_ptid (pid);
163 }
164
165 /* Check to see if the given thread is alive.
166
167    FIXME: Is kill() ever the right way to do this?  I doubt it, but
168    for now we're going to try and be compatable with the old thread
169    code.  */
170
171 static int
172 inf_ptrace_thread_alive (ptid_t ptid)
173 {
174   pid_t pid = PIDGET (ptid);
175
176   return (kill (pid, 0) != -1);
177 }
178
179 /* Attach to process PID, then initialize for debugging it.  */
180
181 static void
182 inf_ptrace_attach (char *args, int from_tty)
183 {
184   char *exec_file;
185   int pid;
186   char *dummy;
187
188   if (!args)
189     error_no_arg ("process-id to attach");
190
191   dummy = args;
192   pid = strtol (args, &dummy, 0);
193   /* Some targets don't set errno on errors, grrr!  */
194   if (pid == 0 && args == dummy)
195     error ("Illegal process-id: %s\n", args);
196
197   if (pid == getpid ())         /* Trying to masturbate?  */
198     error ("I refuse to debug myself!");
199
200   if (from_tty)
201     {
202       exec_file = (char *) get_exec_file (0);
203
204       if (exec_file)
205         printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
206                            target_pid_to_str (pid_to_ptid (pid)));
207       else
208         printf_unfiltered ("Attaching to %s\n",
209                            target_pid_to_str (pid_to_ptid (pid)));
210
211       gdb_flush (gdb_stdout);
212     }
213
214 #ifdef PT_ATTACH
215   errno = 0;
216   ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
217   if (errno != 0)
218     perror_with_name ("ptrace");
219   attach_flag = 1;
220 #else
221   error ("This system does not support attaching to a process");
222 #endif
223
224   inferior_ptid = pid_to_ptid (pid);
225   push_target (ptrace_ops_hack);
226
227   /* Do this first, before anything has had a chance to query the
228      inferior's symbol table or similar.  */
229   observer_notify_inferior_created (&current_target, from_tty);
230 }
231
232 static void
233 inf_ptrace_post_attach (int pid)
234 {
235   /* This version of Unix doesn't require a meaningful "post attach"
236      operation by a debugger.  */
237 }
238
239 /* Take a program previously attached to and detaches it.  The program
240    resumes execution and will no longer stop on signals, etc.  We'd
241    better not have left any breakpoints in the program or it'll die
242    when it hits one.  For this to work, it may be necessary for the
243    process to have been previously attached.  It *might* work if the
244    program was started via the normal ptrace (PTRACE_TRACEME).  */
245
246 static void
247 inf_ptrace_detach (char *args, int from_tty)
248 {
249   int sig = 0;
250   int pid = PIDGET (inferior_ptid);
251
252   if (from_tty)
253     {
254       char *exec_file = get_exec_file (0);
255       if (exec_file == 0)
256         exec_file = "";
257       printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
258                          target_pid_to_str (pid_to_ptid (pid)));
259       gdb_flush (gdb_stdout);
260     }
261   if (args)
262     sig = atoi (args);
263
264 #ifdef PT_DETACH
265   errno = 0;
266   ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, sig);
267   if (errno != 0)
268     perror_with_name ("ptrace");
269   attach_flag = 0;
270 #else
271   error ("This system does not support detaching from a process");
272 #endif
273
274   inferior_ptid = null_ptid;
275   unpush_target (ptrace_ops_hack);
276 }
277
278 /* Get ready to modify the registers array.  On machines which store
279    individual registers, this doesn't need to do anything.  On
280    machines which store all the registers in one fell swoop, this
281    makes sure that registers contains all the registers from the
282    program being debugged.  */
283
284 static void
285 inf_ptrace_prepare_to_store (void)
286 {
287 }
288
289 /* Print status information about what we're accessing.  */
290
291 static void
292 inf_ptrace_files_info (struct target_ops *ignore)
293 {
294   printf_unfiltered ("\tUsing the running image of %s %s.\n",
295                      attach_flag ? "attached" : "child",
296                      target_pid_to_str (inferior_ptid));
297 }
298
299 static void
300 inf_ptrace_open (char *arg, int from_tty)
301 {
302   error ("Use the \"run\" command to start a Unix child process.");
303 }
304
305 /* Stub function which causes the inferior that runs it, to be ptrace-able
306    by its parent process.  */
307
308 static void
309 inf_ptrace_me (void)
310 {
311   /* "Trace me, Dr. Memory!"  */
312   ptrace (0, 0, (PTRACE_TYPE_ARG3) 0, 0);
313 }
314
315 /* Stub function which causes the GDB that runs it, to start ptrace-ing
316    the child process.  */
317
318 static void
319 inf_ptrace_him (int pid)
320 {
321   push_target (ptrace_ops_hack);
322
323   /* On some targets, there must be some explicit synchronization
324      between the parent and child processes after the debugger
325      forks, and before the child execs the debuggee program.  This
326      call basically gives permission for the child to exec.  */
327
328   target_acknowledge_created_inferior (pid);
329
330   /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
331      be 1 or 2 depending on whether we're starting without or with a
332      shell.  */
333   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
334
335   /* On some targets, there must be some explicit actions taken after
336      the inferior has been started up.  */
337   target_post_startup_inferior (pid_to_ptid (pid));
338 }
339
340 /* Start an inferior Unix child process and sets inferior_ptid to its
341    pid.  EXEC_FILE is the file to run.  ALLARGS is a string containing
342    the arguments to the program.  ENV is the environment vector to
343    pass.  Errors reported with error().  */
344
345 static void
346 inf_ptrace_create_inferior (char *exec_file, char *allargs, char **env,
347                             int from_tty)
348 {
349   fork_inferior (exec_file, allargs, env, inf_ptrace_me, inf_ptrace_him,
350                  NULL, NULL);
351   /* We are at the first instruction we care about.  */
352   observer_notify_inferior_created (&current_target, from_tty);
353   /* Pedal to the metal...  */
354   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
355 }
356
357 static void
358 inf_ptrace_post_startup_inferior (ptid_t ptid)
359 {
360   /* This version of Unix doesn't require a meaningful "post startup
361      inferior" operation by a debugger.  */
362 }
363
364 static void
365 inf_ptrace_acknowledge_created_inferior (int pid)
366 {
367   /* This version of Unix doesn't require a meaningful "acknowledge
368      created inferior" operation by a debugger.  */
369 }
370
371 static int
372 inf_ptrace_insert_fork_catchpoint (int pid)
373 {
374   /* This version of Unix doesn't support notification of fork events.  */
375   return 0;
376 }
377
378 static int
379 inf_ptrace_remove_fork_catchpoint (int pid)
380 {
381   /* This version of Unix doesn't support notification of fork events.  */
382   return 0;
383 }
384
385 static int
386 inf_ptrace_insert_vfork_catchpoint (int pid)
387 {
388   /* This version of Unix doesn't support notification of vfork events.  */
389   return 0;
390 }
391
392 static int
393 inf_ptrace_remove_vfork_catchpoint (int pid)
394 {
395   /* This version of Unix doesn't support notification of vfork events.  */
396   return 0;
397 }
398
399 static int
400 inf_ptrace_follow_fork (int follow_child)
401 {
402   /* This version of Unix doesn't support following fork or vfork events.  */
403   return 0;
404 }
405
406 static int
407 inf_ptrace_insert_exec_catchpoint (int pid)
408 {
409   /* This version of Unix doesn't support notification of exec events.  */
410   return 0;
411 }
412
413 static int
414 inf_ptrace_remove_exec_catchpoint (int pid)
415 {
416   /* This version of Unix doesn't support notification of exec events.  */
417   return 0;
418 }
419
420 static int
421 inf_ptrace_reported_exec_events_per_exec_call (void)
422 {
423   /* This version of Unix doesn't support notification of exec events.  */
424   return 1;
425 }
426
427 static int
428 inf_ptrace_has_exited (int pid, int wait_status, int *exit_status)
429 {
430   if (WIFEXITED (wait_status))
431     {
432       *exit_status = WEXITSTATUS (wait_status);
433       return 1;
434     }
435
436   if (WIFSIGNALED (wait_status))
437     {
438       *exit_status = 0;         /* ?? Don't know what else to say here. */
439       return 1;
440     }
441
442   /* ??? Do we really need to consult the event state, too?
443      Assume the wait_state alone suffices.  */
444   return 0;
445 }
446
447 static void
448 inf_ptrace_mourn_inferior (void)
449 {
450   unpush_target (ptrace_ops_hack);
451   generic_mourn_inferior ();
452 }
453
454 static int
455 inf_ptrace_can_run (void)
456 {
457   return 1;
458 }
459
460 /* Send a SIGINT to the process group.  This acts just like the user
461    typed a ^C on the controlling terminal.
462
463    FIXME: This may not be correct for all systems.  Some may want to
464    use killpg() instead of kill (-pgrp).  */
465
466 static void
467 inf_ptrace_stop (void)
468 {
469   kill (-inferior_process_group, SIGINT);
470 }
471
472 /* Perform a partial transfer to/from the specified object.  For
473    memory transfers, fall back to the old memory xfer functions.  */
474
475 static LONGEST
476 inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
477                          const char *annex, void *readbuf,
478                          const void *writebuf, ULONGEST offset, LONGEST len)
479 {
480   switch (object)
481     {
482     case TARGET_OBJECT_MEMORY:
483 #ifdef PT_IO
484       /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
485          request that promises to be much more efficient in reading
486          and writing data in the traced process's address space.  */
487       {
488         struct ptrace_io_desc piod;
489         
490         /* NOTE: We assume that there are no distinct address spaces
491            for instruction and data.  */
492         piod.piod_op = writebuf ? PIOD_WRITE_D : PIOD_READ_D;
493         piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
494         piod.piod_offs = (void *) (long) offset;
495         piod.piod_len = len;
496
497         errno = 0;
498         if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == 0)
499           /* Return the actual number of bytes read or written.  */
500           return piod.piod_len;
501         /* If the PT_IO request is somehow not supported, fallback on
502            using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
503            to indicate failure.  */
504         if (errno != EINVAL)
505           return 0;
506       }
507 #endif
508       {
509         union
510         {
511           PTRACE_TYPE_RET word;
512           unsigned char byte[sizeof (PTRACE_TYPE_RET)];
513         } buffer;
514         ULONGEST rounded_offset;
515         LONGEST partial_len;
516         
517         /* Round the start offset down to the next long word
518            boundary.  */
519         rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
520         
521         /* Since ptrace will transfer a single word starting at that
522            rounded_offset the partial_len needs to be adjusted down to
523            that (remember this function only does a single transfer).
524            Should the required length be even less, adjust it down
525            again.  */
526         partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
527         if (partial_len > len)
528           partial_len = len;
529         
530         if (writebuf)
531           {
532             /* If OFFSET:PARTIAL_LEN is smaller than
533                ROUNDED_OFFSET:WORDSIZE then a read/modify write will
534                be needed.  Read in the entire word.  */
535             if (rounded_offset < offset
536                 || (offset + partial_len
537                     < rounded_offset + sizeof (PTRACE_TYPE_RET)))
538               /* Need part of initial word -- fetch it.  */
539               buffer.word = ptrace (PT_READ_I, PIDGET (inferior_ptid),
540                                     (PTRACE_TYPE_ARG3) (long) rounded_offset,
541                                     0);
542             
543             /* Copy data to be written over corresponding part of
544                buffer.  */
545             memcpy (buffer.byte + (offset - rounded_offset),
546                     writebuf, partial_len);
547             
548             errno = 0;
549             ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
550                     (PTRACE_TYPE_ARG3) (long) rounded_offset,
551                     buffer.word);
552             if (errno)
553               {
554                 /* Using the appropriate one (I or D) is necessary for
555                    Gould NP1, at least.  */
556                 errno = 0;
557                 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
558                         (PTRACE_TYPE_ARG3) (long) rounded_offset,
559                         buffer.word);
560                 if (errno)
561                   return 0;
562               }
563           }
564         if (readbuf)
565           {
566             errno = 0;
567             buffer.word = ptrace (PT_READ_I, PIDGET (inferior_ptid),
568                                   (PTRACE_TYPE_ARG3) (long) rounded_offset, 0);
569             if (errno)
570               return 0;
571             /* Copy appropriate bytes out of the buffer.  */
572             memcpy (readbuf, buffer.byte + (offset - rounded_offset),
573                     partial_len);
574           }
575         return partial_len;
576       }
577
578     case TARGET_OBJECT_UNWIND_TABLE:
579       return -1;
580
581     case TARGET_OBJECT_AUXV:
582       return -1;
583
584     case TARGET_OBJECT_WCOOKIE:
585       return -1;
586
587     default:
588       return -1;
589     }
590 }
591
592 static char *
593 inf_ptrace_pid_to_str (ptid_t ptid)
594 {
595   return normal_pid_to_str (ptid);
596 }
597
598 /* Create a prototype ptrace target.  The client can override it with
599    local methods.  */
600
601 struct target_ops *
602 inf_ptrace_target (void)
603 {
604   struct target_ops *t = inf_child_target ();
605
606   t->to_open = inf_ptrace_open;
607   t->to_attach = inf_ptrace_attach;
608   t->to_post_attach = inf_ptrace_post_attach;
609   t->to_detach = inf_ptrace_detach;
610   t->to_resume = inf_ptrace_resume;
611   t->to_wait = inf_ptrace_wait;
612   t->to_prepare_to_store = inf_ptrace_prepare_to_store;
613   t->to_xfer_partial = inf_ptrace_xfer_partial;
614   t->to_files_info = inf_ptrace_files_info;
615   t->to_kill = inf_ptrace_kill_inferior;
616   t->to_create_inferior = inf_ptrace_create_inferior;
617   t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
618   t->to_acknowledge_created_inferior =
619     inf_ptrace_acknowledge_created_inferior;
620   t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
621   t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
622   t->to_insert_vfork_catchpoint = inf_ptrace_insert_vfork_catchpoint;
623   t->to_remove_vfork_catchpoint = inf_ptrace_remove_vfork_catchpoint;
624   t->to_follow_fork = inf_ptrace_follow_fork;
625   t->to_insert_exec_catchpoint = inf_ptrace_insert_exec_catchpoint;
626   t->to_remove_exec_catchpoint = inf_ptrace_remove_exec_catchpoint;
627   t->to_reported_exec_events_per_exec_call =
628     inf_ptrace_reported_exec_events_per_exec_call;
629   t->to_has_exited = inf_ptrace_has_exited;
630   t->to_mourn_inferior = inf_ptrace_mourn_inferior;
631   t->to_can_run = inf_ptrace_can_run;
632   t->to_thread_alive = inf_ptrace_thread_alive;
633   t->to_pid_to_str = inf_ptrace_pid_to_str;
634   t->to_stop = inf_ptrace_stop;
635   t->to_stratum = process_stratum;
636   t->to_has_all_memory = 1;
637   t->to_has_memory = 1;
638   t->to_has_stack = 1;
639   t->to_has_registers = 1;
640   t->to_has_execution = 1;
641   t->to_magic = OPS_MAGIC;
642   ptrace_ops_hack = t;
643
644   return t;
645 }
646 \f
647
648 /* Pointer to a function that returns the oggset within the user area
649    where a particular register is stored.  */
650 static CORE_ADDR (*inf_ptrace_register_u_offset)(int);
651
652 /* Fetch register REGNUM from the inferior.  */
653
654 static void
655 inf_ptrace_fetch_register (int regnum)
656 {
657   CORE_ADDR addr;
658   size_t size;
659   PTRACE_TYPE_RET *buf;
660   int pid, i;
661
662   /* Cater for systems like GNU/Linux, that implement threads as
663      seperate processes.  */
664   pid = ptid_get_lwp (inferior_ptid);
665   if (pid == 0)
666     pid = ptid_get_pid (inferior_ptid);
667
668   /* This isn't really an address, but ptrace thinks of it as one.  */
669   addr = inf_ptrace_register_u_offset (regnum);
670   size = register_size (current_gdbarch, regnum);
671
672   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
673   buf = alloca (size);
674
675   /* Read the register contents from the inferior a chuck at the time.  */
676   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
677     {
678       errno = 0;
679       buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3) addr, 0);
680       if (errno != 0)
681         error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regnum),
682                regnum, safe_strerror (errno));
683
684       addr += sizeof (PTRACE_TYPE_RET);
685     }
686   regcache_raw_supply (current_regcache, regnum, buf);
687 }
688
689 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
690    for all registers.  */
691
692 static void
693 inf_ptrace_fetch_registers (int regnum)
694 {
695   if (regnum == -1)
696     for (regnum = 0; regnum < NUM_REGS; regnum++)
697       inf_ptrace_fetch_register (regnum);
698   else
699     inf_ptrace_fetch_register (regnum);
700 }
701
702 /* Store register REGNUM into the inferior.  */
703
704 static void
705 inf_ptrace_store_register (int regnum)
706 {
707   CORE_ADDR addr;
708   size_t size;
709   PTRACE_TYPE_RET *buf;
710   int pid, i;
711
712   /* Cater for systems like GNU/Linux, that implement threads as
713      seperate processes.  */
714   pid = ptid_get_lwp (inferior_ptid);
715   if (pid == 0)
716     pid = ptid_get_pid (inferior_ptid);
717
718   /* This isn't really an address, but ptrace thinks of it as one.  */
719   addr = inf_ptrace_register_u_offset (regnum);
720   size = register_size (current_gdbarch, regnum);
721
722   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
723   buf = alloca (size);
724
725   /* Write the register contents into the inferior a chunk at the time.  */
726   regcache_raw_collect (current_regcache, regnum, buf);
727   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
728     {
729       errno = 0;
730       ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3) addr, buf[i]);
731       if (errno != 0)
732         error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regnum),
733                regnum, safe_strerror (errno));
734
735       addr += sizeof (PTRACE_TYPE_RET);
736     }
737 }
738
739 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
740    this for all registers.  */
741
742 void
743 inf_ptrace_store_registers (int regnum)
744 {
745   if (regnum == -1)
746     for (regnum = 0; regnum < NUM_REGS; regnum++)
747       inf_ptrace_store_register (regnum);
748   else
749     inf_ptrace_store_register (regnum);
750 }
751
752 /* Create a "traditional" ptrace target.  REGISTER_U_OFFSET should be
753    a function returning the offset within the user area where a
754    particular register is stored.  */
755
756 struct target_ops *
757 inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)(int))
758 {
759   struct target_ops *t = inf_ptrace_target();
760
761   gdb_assert (register_u_offset);
762   inf_ptrace_register_u_offset = register_u_offset;
763   t->to_fetch_registers = inf_ptrace_fetch_registers;
764   t->to_store_registers = inf_ptrace_store_registers;
765
766   return t;
767 }