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