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