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