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