gdbserver: include linux-arm-tdesc.h in linux-arm-tdesc.c
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
index 9e07706ba1603e9e60c0ff319f073f51d36fb5f6..ecd82ada4ed3deaa5cfed5c575a51fdd830c338a 100644 (file)
@@ -1,13 +1,12 @@
-/* Low level Unix child interface to ptrace, for GDB when running under Unix.
+/* Low-level child interface to ptrace.
 
-   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
-   1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1988-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
-#include "observer.h"
-#include "gdb_ptrace.h"
-#include "inflow.h"
-#include "inferior.h"
-#include "regcache.h"
 #include "command.h"
+#include "inferior.h"
+#include "inflow.h"
+#include "terminal.h"
 #include "gdbcore.h"
-#include "inf-child.h"
-#include "gdbcmd.h"
-#include "gdb_string.h"
-
-#include <sys/wait.h>
+#include "regcache.h"
+#include "nat/gdb_ptrace.h"
+#include "gdbsupport/gdb_wait.h"
 #include <signal.h>
 
-/* HACK: Save the ptrace ops returned by ptrace_target.  */
-static struct target_ops *ptrace_ops_hack;
-
-static void
-inf_ptrace_kill_inferior (void)
-{
-  int status;
-  int pid = PIDGET (inferior_ptid);
-
-  if (pid == 0)
-    return;
+#include "inf-ptrace.h"
+#include "inf-child.h"
+#include "gdbthread.h"
+#include "nat/fork-inferior.h"
+#include "utils.h"
+#include "gdbarch.h"
 
-  /* This once used to call "kill" to kill the inferior just in case
-     the inferior was still running.  As others have noted in the past
-     (kingdon) there shouldn't be any way to get here if the inferior
-     is still running -- else there's a major problem elsewere in gdb
-     and it needs to be fixed.
-
-     The kill call causes problems under hpux10, so it's been removed;
-     if this causes problems we'll deal with them as they arise.  */
-  call_ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
-  ptrace_wait (null_ptid, &status);
-  target_mourn_inferior ();
-}
+\f
 
-/* Resume execution of the inferior process.  If STEP is nonzero,
-   single-step it.  If SIGNAL is nonzero, give it that signal.  */
+/* A unique_ptr helper to unpush a target.  */
 
-static void
-inf_ptrace_resume (ptid_t ptid, int step, enum target_signal signal)
+struct target_unpusher
 {
-  int request = PT_CONTINUE;
-  int pid = PIDGET (ptid);
-
-  if (pid == -1)
-    /* Resume all threads.  */
-    /* I think this only gets used in the non-threaded case, where
-       "resume all threads" and "resume inferior_ptid" are the
-       same.  */
-    pid = PIDGET (inferior_ptid);
+  void operator() (struct target_ops *ops) const
+  {
+    unpush_target (ops);
+  }
+};
 
-  if (step)
-    {
-      /* If this system does not support PT_STEP, a higher level
-         function will have called single_step() to transmute the step
-         request into a continue request (by setting breakpoints on
-         all possible successor instructions), so we don't have to
-         worry about that here.  */
-      request = PT_STEP;
-    }
+/* A unique_ptr that unpushes a target on destruction.  */
 
-  /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
-     where it was.  If GDB wanted it to start some other way, we have
-     already written a new PC value to the child.  */
-  errno = 0;
-  ptrace (request, pid, (PTRACE_TYPE_ARG3) 1, target_signal_to_host (signal));
-  if (errno != 0)
-    perror_with_name ("ptrace");
-}
+typedef std::unique_ptr<struct target_ops, target_unpusher> target_unpush_up;
 
-/* Set an upper limit on alloca.  */
-#define GDB_MAX_ALLOCA 0x1000
+\f
 
-/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
-   in the NEW_SUN_PTRACE case.  It ought to be straightforward.  But
-   it appears that writing did not write the data that I specified.  I
-   cannot understand where it got the data that it actually did
-   write.  */
+inf_ptrace_target::~inf_ptrace_target ()
+{}
 
-/* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
-   debugger memory starting at MYADDR.  Copy to inferior if WRITE is
-   nonzero.  TARGET is ignored.
+#ifdef PT_GET_PROCESS_STATE
 
-   Returns the length copied, which is either the LEN argument or
-   zero.  This xfer function does not do partial moves, since
-   ptrace_ops_hack doesn't allow memory operations to cross below us in the
-   target stack anyway.  */
+/* Target hook for follow_fork.  On entry and at return inferior_ptid is
+   the ptid of the followed inferior.  */
 
 int
-inf_ptrace_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
-                       struct mem_attrib *attrib, struct target_ops *target)
+inf_ptrace_target::follow_fork (int follow_child, int detach_fork)
 {
-  int i;
-  /* Round starting address down to longword boundary.  */
-  CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
-  /* Round ending address up; get number of longwords that makes.  */
-  int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
-              / sizeof (PTRACE_TYPE_RET));
-  int alloc = count * sizeof (PTRACE_TYPE_RET);
-  PTRACE_TYPE_RET *buffer;
-  struct cleanup *old_chain = NULL;
-
-#ifdef PT_IO
-  /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
-     that promises to be much more efficient in reading and writing
-     data in the traced process's address space.  */
-
-  {
-    struct ptrace_io_desc piod;
-
-    /* NOTE: We assume that there are no distinct address spaces for
-       instruction and data.  */
-    piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
-    piod.piod_offs = (void *) memaddr;
-    piod.piod_addr = myaddr;
-    piod.piod_len = len;
-
-    if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) & piod, 0) == -1)
-      {
-       /* If the PT_IO request is somehow not supported, fallback on
-          using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
-          to indicate failure.  */
-       if (errno != EINVAL)
-         return 0;
-      }
-    else
-      {
-       /* Return the actual number of bytes read or written.  */
-       return piod.piod_len;
-      }
-  }
-#endif
-
-  /* Allocate buffer of that many longwords.  */
-  if (len < GDB_MAX_ALLOCA)
+  if (!follow_child)
     {
-      buffer = (PTRACE_TYPE_RET *) alloca (alloc);
-    }
-  else
-    {
-      buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
-      old_chain = make_cleanup (xfree, buffer);
-    }
+      struct thread_info *tp = inferior_thread ();
+      pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
 
-  if (write)
-    {
-      /* Fill start and end extra bytes of buffer with existing memory
-         data.  */
-      if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
-       {
-         /* Need part of initial word -- fetch it.  */
-         buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
-                             (PTRACE_TYPE_ARG3) addr, 0);
-       }
+      /* Breakpoints have already been detached from the child by
+        infrun.c.  */
 
-      if (count > 1)           /* FIXME, avoid if even boundary.  */
-       {
-         buffer[count - 1] =
-           ptrace (PT_READ_I, PIDGET (inferior_ptid),
-                   ((PTRACE_TYPE_ARG3)
-                    (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
-       }
-
-      /* Copy data to be written over corresponding part of buffer.  */
-      memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
-             myaddr, len);
-
-      /* Write the entire buffer.  */
-      for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
-       {
-         errno = 0;
-         ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
-                 (PTRACE_TYPE_ARG3) addr, buffer[i]);
-         if (errno)
-           {
-             /* Using the appropriate one (I or D) is necessary for
-                Gould NP1, at least.  */
-             errno = 0;
-             ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
-                     (PTRACE_TYPE_ARG3) addr, buffer[i]);
-           }
-         if (errno)
-           return 0;
-       }
-    }
-  else
-    {
-      /* Read all the longwords.  */
-      for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
-       {
-         errno = 0;
-         buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
-                             (PTRACE_TYPE_ARG3) addr, 0);
-         if (errno)
-           return 0;
-         QUIT;
-       }
-
-      /* Copy appropriate bytes out of the buffer.  */
-      memcpy (myaddr,
-             (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
-             len);
+      if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
+       perror_with_name (("ptrace"));
     }
 
-  if (old_chain != NULL)
-    do_cleanups (old_chain);
-  return len;
+  return 0;
 }
 
-/* Wait for child to do something.  Return pid of child, or -1 in case
-   of error; store status through argument pointer OURSTATUS.  */
+int
+inf_ptrace_target::insert_fork_catchpoint (int pid)
+{
+  return 0;
+}
 
-static ptid_t
-inf_ptrace_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
+int
+inf_ptrace_target::remove_fork_catchpoint (int pid)
 {
-  int save_errno;
-  int status;
-  char *execd_pathname = NULL;
-  int exit_status;
-  int related_pid;
-  int syscall_id;
-  enum target_waitkind kind;
-  int pid;
+  return 0;
+}
 
-  do
-    {
-      set_sigint_trap ();      /* Causes SIGINT to be passed on to the
-                                  attached process. */
-      set_sigio_trap ();
+#endif /* PT_GET_PROCESS_STATE */
+\f
 
-      pid = ptrace_wait (inferior_ptid, &status);
+/* Prepare to be traced.  */
 
-      save_errno = errno;
+static void
+inf_ptrace_me (void)
+{
+  /* "Trace me, Dr. Memory!"  */
+  if (ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3) 0, 0) < 0)
+    trace_start_error_with_name ("ptrace");
+}
 
-      clear_sigio_trap ();
+/* Start a new inferior Unix child process.  EXEC_FILE is the file to
+   run, ALLARGS is a string containing the arguments to the program.
+   ENV is the environment vector to pass.  If FROM_TTY is non-zero, be
+   chatty about it.  */
 
-      clear_sigint_trap ();
+void
+inf_ptrace_target::create_inferior (const char *exec_file,
+                                   const std::string &allargs,
+                                   char **env, int from_tty)
+{
+  pid_t pid;
+  ptid_t ptid;
 
-      if (pid == -1)
-       {
-         if (save_errno == EINTR)
-           continue;
+  /* Do not change either targets above or the same target if already present.
+     The reason is the target stack is shared across multiple inferiors.  */
+  int ops_already_pushed = target_is_pushed (this);
 
-         fprintf_unfiltered (gdb_stderr,
-                             "Child process unexpectedly missing: %s.\n",
-                             safe_strerror (save_errno));
+  target_unpush_up unpusher;
+  if (! ops_already_pushed)
+    {
+      /* Clear possible core file with its process_stratum.  */
+      push_target (this);
+      unpusher.reset (this);
+    }
 
-         /* Claim it exited with unknown signal.  */
-         ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
-         ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
-         return pid_to_ptid (-1);
-       }
+  pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
+                      NULL, NULL, NULL);
 
-      /* Did it exit?
-       */
-      if (target_has_exited (pid, status, &exit_status))
-       {
-         /* ??rehrauer: For now, ignore this. */
-         continue;
-       }
+  ptid = ptid_t (pid);
+  /* We have something that executes now.  We'll be running through
+     the shell at this point (if startup-with-shell is true), but the
+     pid shouldn't change.  */
+  add_thread_silent (this, ptid);
 
-      if (!target_thread_alive (pid_to_ptid (pid)))
-       {
-         ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
-         return pid_to_ptid (pid);
-       }
-    }
-  while (pid != PIDGET (inferior_ptid));       /* Some other child died or stopped */
+  unpusher.release ();
 
-  store_waitstatus (ourstatus, status);
-  return pid_to_ptid (pid);
+  gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED);
+
+  /* On some targets, there must be some explicit actions taken after
+     the inferior has been started up.  */
+  target_post_startup_inferior (ptid);
 }
 
+#ifdef PT_GET_PROCESS_STATE
+
 void
-inf_ptrace_post_wait (ptid_t ptid, int wait_status)
+inf_ptrace_target::post_startup_inferior (ptid_t pid)
 {
-  /* This version of Unix doesn't require a meaningful "post wait"
-     operation.
-   */
+  ptrace_event_t pe;
+
+  /* Set the initial event mask.  */
+  memset (&pe, 0, sizeof pe);
+  pe.pe_set_event |= PTRACE_FORK;
+  if (ptrace (PT_SET_EVENT_MASK, pid.pid (),
+             (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
+    perror_with_name (("ptrace"));
 }
 
-/* Check to see if the given thread is alive.
+#endif
 
-   FIXME: Is kill() ever the right way to do this?  I doubt it, but
-   for now we're going to try and be compatable with the old thread
-   code.  */
+/* Clean up a rotting corpse of an inferior after it died.  */
 
-static int
-inf_ptrace_thread_alive (ptid_t ptid)
+void
+inf_ptrace_target::mourn_inferior ()
 {
-  pid_t pid = PIDGET (ptid);
+  int status;
+
+  /* Wait just one more time to collect the inferior's exit status.
+     Do not check whether this succeeds though, since we may be
+     dealing with a process that we attached to.  Such a process will
+     only report its exit status to its original parent.  */
+  waitpid (inferior_ptid.pid (), &status, 0);
 
-  return (kill (pid, 0) != -1);
+  inf_child_target::mourn_inferior ();
 }
 
-/* Attach to process PID, then initialize for debugging it.  */
+/* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
+   be chatty about it.  */
 
-static void
-inf_ptrace_attach (char *args, int from_tty)
+void
+inf_ptrace_target::attach (const char *args, int from_tty)
 {
-  char *exec_file;
-  int pid;
-  char *dummy;
+  pid_t pid;
+  struct inferior *inf;
 
-  if (!args)
-    error_no_arg ("process-id to attach");
+  /* Do not change either targets above or the same target if already present.
+     The reason is the target stack is shared across multiple inferiors.  */
+  int ops_already_pushed = target_is_pushed (this);
 
-  dummy = args;
-  pid = strtol (args, &dummy, 0);
-  /* Some targets don't set errno on errors, grrr! */
-  if ((pid == 0) && (args == dummy))
-    error ("Illegal process-id: %s\n", args);
+  pid = parse_pid_to_attach (args);
 
-  if (pid == getpid ())                /* Trying to masturbate? */
-    error ("I refuse to debug myself!");
+  if (pid == getpid ())                /* Trying to masturbate?  */
+    error (_("I refuse to debug myself!"));
+
+  target_unpush_up unpusher;
+  if (! ops_already_pushed)
+    {
+      /* target_pid_to_str already uses the target.  Also clear possible core
+        file with its process_stratum.  */
+      push_target (this);
+      unpusher.reset (this);
+    }
 
   if (from_tty)
     {
-      exec_file = (char *) get_exec_file (0);
+      const char *exec_file = get_exec_file (0);
 
       if (exec_file)
-       printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
-                          target_pid_to_str (pid_to_ptid (pid)));
+       printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
+                          target_pid_to_str (ptid_t (pid)).c_str ());
       else
-       printf_unfiltered ("Attaching to %s\n",
-                          target_pid_to_str (pid_to_ptid (pid)));
-
-      gdb_flush (gdb_stdout);
+       printf_unfiltered (_("Attaching to %s\n"),
+                          target_pid_to_str (ptid_t (pid)).c_str ());
     }
 
-  attach (pid);
+#ifdef PT_ATTACH
+  errno = 0;
+  ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
+  if (errno != 0)
+    perror_with_name (("ptrace"));
+#else
+  error (_("This system does not support attaching to a process"));
+#endif
 
-  inferior_ptid = pid_to_ptid (pid);
-  push_target (ptrace_ops_hack);
-}
+  inf = current_inferior ();
+  inferior_appeared (inf, pid);
+  inf->attach_flag = 1;
+  inferior_ptid = ptid_t (pid);
 
-static void
-inf_ptrace_post_attach (int pid)
-{
-  /* This version of Unix doesn't require a meaningful "post attach"
-     operation by a debugger.  */
+  /* Always add a main thread.  If some target extends the ptrace
+     target, it should decorate the ptid later with more info.  */
+  thread_info *thr = add_thread_silent (this, inferior_ptid);
+  /* Don't consider the thread stopped until we've processed its
+     initial SIGSTOP stop.  */
+  set_executing (this, thr->ptid, true);
+
+  unpusher.release ();
 }
 
-/* Take a program previously attached to and detaches it.  The program
-   resumes execution and will no longer stop on signals, etc.  We'd
-   better not have left any breakpoints in the program or it'll die
-   when it hits one.  For this to work, it may be necessary for the
-   process to have been previously attached.  It *might* work if the
-   program was started via the normal ptrace (PTRACE_TRACEME).  */
+#ifdef PT_GET_PROCESS_STATE
 
-static void
-inf_ptrace_detach (char *args, int from_tty)
+void
+inf_ptrace_target::post_attach (int pid)
 {
-  int siggnal = 0;
-  int pid = PIDGET (inferior_ptid);
-
-  if (from_tty)
-    {
-      char *exec_file = get_exec_file (0);
-      if (exec_file == 0)
-       exec_file = "";
-      printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
-                        target_pid_to_str (pid_to_ptid (pid)));
-      gdb_flush (gdb_stdout);
-    }
-  if (args)
-    siggnal = atoi (args);
-
-  detach (siggnal);
+  ptrace_event_t pe;
 
-  inferior_ptid = null_ptid;
-  unpush_target (ptrace_ops_hack);
+  /* Set the initial event mask.  */
+  memset (&pe, 0, sizeof pe);
+  pe.pe_set_event |= PTRACE_FORK;
+  if (ptrace (PT_SET_EVENT_MASK, pid,
+             (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
+    perror_with_name (("ptrace"));
 }
 
-/* Get ready to modify the registers array.  On machines which store
-   individual registers, this doesn't need to do anything.  On
-   machines which store all the registers in one fell swoop, this
-   makes sure that registers contains all the registers from the
-   program being debugged.  */
+#endif
 
-static void
-inf_ptrace_prepare_to_store (void)
+/* Detach from the inferior.  If FROM_TTY is non-zero, be chatty about it.  */
+
+void
+inf_ptrace_target::detach (inferior *inf, int from_tty)
 {
-}
+  pid_t pid = inferior_ptid.pid ();
 
-/* Print status information about what we're accessing.  */
+  target_announce_detach (from_tty);
 
-static void
-inf_ptrace_files_info (struct target_ops *ignore)
-{
-  printf_unfiltered ("\tUsing the running image of %s %s.\n",
-                    attach_flag ? "attached" : "child",
-                    target_pid_to_str (inferior_ptid));
-}
+#ifdef PT_DETACH
+  /* We'd better not have left any breakpoints in the program or it'll
+     die when it hits one.  Also note that this may only work if we
+     previously attached to the inferior.  It *might* work if we
+     started the process ourselves.  */
+  errno = 0;
+  ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, 0);
+  if (errno != 0)
+    perror_with_name (("ptrace"));
+#else
+  error (_("This system does not support detaching from a process"));
+#endif
 
-static void
-inf_ptrace_open (char *arg, int from_tty)
-{
-  error ("Use the \"run\" command to start a Unix child process.");
+  detach_success (inf);
 }
 
-/* Stub function which causes the inferior that runs it, to be ptrace-able
-   by its parent process.  */
+/* See inf-ptrace.h.  */
 
-static void
-inf_ptrace_me (void)
+void
+inf_ptrace_target::detach_success (inferior *inf)
 {
-  /* "Trace me, Dr. Memory!" */
-  call_ptrace (0, 0, (PTRACE_TYPE_ARG3) 0, 0);
+  inferior_ptid = null_ptid;
+  detach_inferior (inf);
+
+  maybe_unpush_target ();
 }
 
-/* Stub function which causes the GDB that runs it, to start ptrace-ing
-   the child process.  */
+/* Kill the inferior.  */
 
-static void
-inf_ptrace_him (int pid)
+void
+inf_ptrace_target::kill ()
 {
-  push_target (ptrace_ops_hack);
-
-  /* On some targets, there must be some explicit synchronization
-     between the parent and child processes after the debugger
-     forks, and before the child execs the debuggee program.  This
-     call basically gives permission for the child to exec.
-   */
+  pid_t pid = inferior_ptid.pid ();
+  int status;
 
-  target_acknowledge_created_inferior (pid);
+  if (pid == 0)
+    return;
 
-  /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
-   * and will be 1 or 2 depending on whether we're starting
-   * without or with a shell.
-   */
-  startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
+  ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
+  waitpid (pid, &status, 0);
 
-  /* On some targets, there must be some explicit actions taken after
-     the inferior has been started up.
-   */
-  target_post_startup_inferior (pid_to_ptid (pid));
+  target_mourn_inferior (inferior_ptid);
 }
 
-/* Start an inferior Unix child process and sets inferior_ptid to its
-   pid.  EXEC_FILE is the file to run.  ALLARGS is a string containing
-   the arguments to the program.  ENV is the environment vector to
-   pass.  Errors reported with error().  */
+/* Return which PID to pass to ptrace in order to observe/control the
+   tracee identified by PTID.  */
 
-static void
-inf_ptrace_create_inferior (char *exec_file, char *allargs, char **env,
-                           int from_tty)
+pid_t
+get_ptrace_pid (ptid_t ptid)
 {
-  fork_inferior (exec_file, allargs, env, inf_ptrace_me, inf_ptrace_him,
-                NULL, NULL);
-  /* We are at the first instruction we care about.  */
-  observer_notify_inferior_created (&current_target, from_tty);
-  /* Pedal to the metal... */
-  proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
-}
-
-static void
-inf_ptrace_post_startup_inferior (ptid_t ptid)
-{
-  /* This version of Unix doesn't require a meaningful "post startup inferior"
-     operation by a debugger.
-   */
-}
+  pid_t pid;
 
-static void
-inf_ptrace_acknowledge_created_inferior (int pid)
-{
-  /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
-     operation by a debugger.
-   */
+  /* If we have an LWPID to work with, use it.  Otherwise, we're
+     dealing with a non-threaded program/target.  */
+  pid = ptid.lwp ();
+  if (pid == 0)
+    pid = ptid.pid ();
+  return pid;
 }
 
-static int
-inf_ptrace_insert_fork_catchpoint (int pid)
-{
-  /* This version of Unix doesn't support notification of fork events.  */
-  return 0;
-}
+/* Resume execution of thread PTID, or all threads if PTID is -1.  If
+   STEP is nonzero, single-step it.  If SIGNAL is nonzero, give it
+   that signal.  */
 
-static int
-inf_ptrace_remove_fork_catchpoint (int pid)
+void
+inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
 {
-  /* This version of Unix doesn't support notification of fork events.  */
-  return 0;
-}
+  pid_t pid;
+  int request;
 
-static int
-inf_ptrace_insert_vfork_catchpoint (int pid)
-{
-  /* This version of Unix doesn't support notification of vfork events.  */
-  return 0;
-}
+  if (minus_one_ptid == ptid)
+    /* Resume all threads.  Traditionally ptrace() only supports
+       single-threaded processes, so simply resume the inferior.  */
+    pid = inferior_ptid.pid ();
+  else
+    pid = get_ptrace_pid (ptid);
 
-static int
-inf_ptrace_remove_vfork_catchpoint (int pid)
-{
-  /* This version of Unix doesn't support notification of vfork events.  */
-  return 0;
-}
+  if (catch_syscall_enabled () > 0)
+    request = PT_SYSCALL;
+  else
+    request = PT_CONTINUE;
 
-static int
-inf_ptrace_follow_fork (int follow_child)
-{
-  /* This version of Unix doesn't support following fork or vfork events.  */
-  return 0;
-}
+  if (step)
+    {
+      /* If this system does not support PT_STEP, a higher level
+         function will have called single_step() to transmute the step
+         request into a continue request (by setting breakpoints on
+         all possible successor instructions), so we don't have to
+         worry about that here.  */
+      request = PT_STEP;
+    }
 
-static int
-inf_ptrace_insert_exec_catchpoint (int pid)
-{
-  /* This version of Unix doesn't support notification of exec events.  */
-  return 0;
+  /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
+     where it was.  If GDB wanted it to start some other way, we have
+     already written a new program counter value to the child.  */
+  errno = 0;
+  ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
+  if (errno != 0)
+    perror_with_name (("ptrace"));
 }
 
-static int
-inf_ptrace_remove_exec_catchpoint (int pid)
-{
-  /* This version of Unix doesn't support notification of exec events.  */
-  return 0;
-}
+/* Wait for the child specified by PTID to do something.  Return the
+   process ID of the child, or MINUS_ONE_PTID in case of error; store
+   the status in *OURSTATUS.  */
 
-static int
-inf_ptrace_reported_exec_events_per_exec_call (void)
+ptid_t
+inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
+                        int options)
 {
-  /* This version of Unix doesn't support notification of exec events.
-   */
-  return 1;
-}
+  pid_t pid;
+  int status, save_errno;
 
-static int
-inf_ptrace_has_exited (int pid, int wait_status, int *exit_status)
-{
-  if (WIFEXITED (wait_status))
+  do
     {
-      *exit_status = WEXITSTATUS (wait_status);
-      return 1;
+      set_sigint_trap ();
+
+      do
+       {
+         pid = waitpid (ptid.pid (), &status, 0);
+         save_errno = errno;
+       }
+      while (pid == -1 && errno == EINTR);
+
+      clear_sigint_trap ();
+
+      if (pid == -1)
+       {
+         fprintf_unfiltered (gdb_stderr,
+                             _("Child process unexpectedly missing: %s.\n"),
+                             safe_strerror (save_errno));
+
+         /* Claim it exited with unknown signal.  */
+         ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
+         ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
+         return inferior_ptid;
+       }
+
+      /* Ignore terminated detached child processes.  */
+      if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
+       pid = -1;
     }
+  while (pid == -1);
 
-  if (WIFSIGNALED (wait_status))
+#ifdef PT_GET_PROCESS_STATE
+  if (WIFSTOPPED (status))
     {
-      *exit_status = 0;                /* ?? Don't know what else to say here. */
-      return 1;
+      ptrace_state_t pe;
+      pid_t fpid;
+
+      if (ptrace (PT_GET_PROCESS_STATE, pid,
+                 (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
+       perror_with_name (("ptrace"));
+
+      switch (pe.pe_report_event)
+       {
+       case PTRACE_FORK:
+         ourstatus->kind = TARGET_WAITKIND_FORKED;
+         ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
+
+         /* Make sure the other end of the fork is stopped too.  */
+         fpid = waitpid (pe.pe_other_pid, &status, 0);
+         if (fpid == -1)
+           perror_with_name (("waitpid"));
+
+         if (ptrace (PT_GET_PROCESS_STATE, fpid,
+                     (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
+           perror_with_name (("ptrace"));
+
+         gdb_assert (pe.pe_report_event == PTRACE_FORK);
+         gdb_assert (pe.pe_other_pid == pid);
+         if (fpid == inferior_ptid.pid ())
+           {
+             ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
+             return ptid_t (fpid);
+           }
+
+         return ptid_t (pid);
+       }
     }
+#endif
 
-  /* ?? Do we really need to consult the event state, too?  Assume the
-     wait_state alone suffices.
-   */
-  return 0;
+  store_waitstatus (ourstatus, status);
+  return ptid_t (pid);
 }
 
-static void
-inf_ptrace_mourn_inferior (void)
-{
-  unpush_target (ptrace_ops_hack);
-  generic_mourn_inferior ();
-}
+/* Transfer data via ptrace into process PID's memory from WRITEBUF, or
+   from process PID's memory into READBUF.  Start at target address ADDR
+   and transfer up to LEN bytes.  Exactly one of READBUF and WRITEBUF must
+   be non-null.  Return the number of transferred bytes.  */
 
-static int
-inf_ptrace_can_run (void)
+static ULONGEST
+inf_ptrace_peek_poke (pid_t pid, gdb_byte *readbuf,
+                     const gdb_byte *writebuf,
+                     ULONGEST addr, ULONGEST len)
 {
-  return 1;
-}
+  ULONGEST n;
+  unsigned int chunk;
 
-/* Send a SIGINT to the process group.  This acts just like the user
-   typed a ^C on the controlling terminal.
+  /* We transfer aligned words.  Thus align ADDR down to a word
+     boundary and determine how many bytes to skip at the
+     beginning.  */
+  ULONGEST skip = addr & (sizeof (PTRACE_TYPE_RET) - 1);
+  addr -= skip;
 
-   XXX - This may not be correct for all systems.  Some may want to
-   use killpg() instead of kill (-pgrp). */
+  for (n = 0;
+       n < len;
+       n += chunk, addr += sizeof (PTRACE_TYPE_RET), skip = 0)
+    {
+      /* Restrict to a chunk that fits in the current word.  */
+      chunk = std::min (sizeof (PTRACE_TYPE_RET) - skip, len - n);
 
-static void
-inf_ptrace_stop (void)
-{
-  kill (-inferior_process_group, SIGINT);
+      /* Use a union for type punning.  */
+      union
+      {
+       PTRACE_TYPE_RET word;
+       gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
+      } buf;
+
+      /* Read the word, also when doing a partial word write.  */
+      if (readbuf != NULL || chunk < sizeof (PTRACE_TYPE_RET))
+       {
+         errno = 0;
+         buf.word = ptrace (PT_READ_I, pid,
+                            (PTRACE_TYPE_ARG3)(uintptr_t) addr, 0);
+         if (errno != 0)
+           break;
+         if (readbuf != NULL)
+           memcpy (readbuf + n, buf.byte + skip, chunk);
+       }
+      if (writebuf != NULL)
+       {
+         memcpy (buf.byte + skip, writebuf + n, chunk);
+         errno = 0;
+         ptrace (PT_WRITE_D, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
+                 buf.word);
+         if (errno != 0)
+           {
+             /* Using the appropriate one (I or D) is necessary for
+                Gould NP1, at least.  */
+             errno = 0;
+             ptrace (PT_WRITE_I, pid, (PTRACE_TYPE_ARG3)(uintptr_t) addr,
+                     buf.word);
+             if (errno != 0)
+               break;
+           }
+       }
+    }
+
+  return n;
 }
 
-/* Perform a partial transfer to/from the specified object.  For
-   memory transfers, fall back to the old memory xfer functions.  */
+/* Implement the to_xfer_partial target_ops method.  */
 
-static LONGEST
-inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
-                        const char *annex, void *readbuf,
-                        const void *writebuf, ULONGEST offset, LONGEST len)
+enum target_xfer_status
+inf_ptrace_target::xfer_partial (enum target_object object,
+                                const char *annex, gdb_byte *readbuf,
+                                const gdb_byte *writebuf,
+                                ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
 {
+  pid_t pid = get_ptrace_pid (inferior_ptid);
+
   switch (object)
     {
     case TARGET_OBJECT_MEMORY:
-      if (readbuf)
-       return inf_ptrace_xfer_memory (offset, readbuf, len, 0 /*write */ ,
-                                      NULL, ops);
-      if (writebuf)
-       return inf_ptrace_xfer_memory (offset, readbuf, len, 1 /*write */ ,
-                                      NULL, ops);
-      return -1;
+#ifdef PT_IO
+      /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
+        request that promises to be much more efficient in reading
+        and writing data in the traced process's address space.  */
+      {
+       struct ptrace_io_desc piod;
+
+       /* NOTE: We assume that there are no distinct address spaces
+          for instruction and data.  However, on OpenBSD 3.9 and
+          later, PIOD_WRITE_D doesn't allow changing memory that's
+          mapped read-only.  Since most code segments will be
+          read-only, using PIOD_WRITE_D will prevent us from
+          inserting breakpoints, so we use PIOD_WRITE_I instead.  */
+       piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
+       piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
+       piod.piod_offs = (void *) (long) offset;
+       piod.piod_len = len;
+
+       errno = 0;
+       if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
+         {
+           /* Return the actual number of bytes read or written.  */
+           *xfered_len = piod.piod_len;
+           return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
+         }
+       /* If the PT_IO request is somehow not supported, fallback on
+          using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
+          to indicate failure.  */
+       if (errno != EINVAL)
+         return TARGET_XFER_EOF;
+      }
+#endif
+      *xfered_len = inf_ptrace_peek_poke (pid, readbuf, writebuf,
+                                         offset, len);
+      return *xfered_len != 0 ? TARGET_XFER_OK : TARGET_XFER_EOF;
 
     case TARGET_OBJECT_UNWIND_TABLE:
-      return -1;
+      return TARGET_XFER_E_IO;
 
     case TARGET_OBJECT_AUXV:
-      return -1;
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+      /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
+        request that allows us to read the auxilliary vector.  Other
+        BSD's may follow if they feel the need to support PIE.  */
+      {
+       struct ptrace_io_desc piod;
+
+       if (writebuf)
+         return TARGET_XFER_E_IO;
+       piod.piod_op = PIOD_READ_AUXV;
+       piod.piod_addr = readbuf;
+       piod.piod_offs = (void *) (long) offset;
+       piod.piod_len = len;
+
+       errno = 0;
+       if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
+         {
+           /* Return the actual number of bytes read or written.  */
+           *xfered_len = piod.piod_len;
+           return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
+         }
+      }
+#endif
+      return TARGET_XFER_E_IO;
 
     case TARGET_OBJECT_WCOOKIE:
-      return -1;
+      return TARGET_XFER_E_IO;
 
     default:
-      return -1;
+      return TARGET_XFER_E_IO;
     }
 }
 
-static char *
-inf_ptrace_pid_to_str (ptid_t ptid)
+/* Return non-zero if the thread specified by PTID is alive.  */
+
+bool
+inf_ptrace_target::thread_alive (ptid_t ptid)
+{
+  /* ??? Is kill the right way to do this?  */
+  return (::kill (ptid.pid (), 0) != -1);
+}
+
+/* Print status information about what we're accessing.  */
+
+void
+inf_ptrace_target::files_info ()
+{
+  struct inferior *inf = current_inferior ();
+
+  printf_filtered (_("\tUsing the running image of %s %s.\n"),
+                  inf->attach_flag ? "attached" : "child",
+                  target_pid_to_str (inferior_ptid).c_str ());
+}
+
+std::string
+inf_ptrace_target::pid_to_str (ptid_t ptid)
 {
   return normal_pid_to_str (ptid);
 }
 
-struct target_ops *
-inf_ptrace_target (void)
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+
+/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
+   Return 0 if *READPTR is already at the end of the buffer.
+   Return -1 if there is insufficient buffer for a whole entry.
+   Return 1 if an entry was read into *TYPEP and *VALP.  */
+
+int
+inf_ptrace_target::auxv_parse (gdb_byte **readptr, gdb_byte *endptr,
+                              CORE_ADDR *typep, CORE_ADDR *valp)
 {
-  struct target_ops *t = inf_child_target ();
-  t->to_open = inf_ptrace_open;
-  t->to_attach = inf_ptrace_attach;
-  t->to_post_attach = inf_ptrace_post_attach;
-  t->to_detach = inf_ptrace_detach;
-  t->to_resume = inf_ptrace_resume;
-  t->to_wait = inf_ptrace_wait;
-  t->to_post_wait = inf_ptrace_post_wait;
-  t->to_prepare_to_store = inf_ptrace_prepare_to_store;
-  t->to_xfer_memory = inf_ptrace_xfer_memory;
-  t->to_xfer_partial = inf_ptrace_xfer_partial;
-  t->to_files_info = inf_ptrace_files_info;
-  t->to_kill = inf_ptrace_kill_inferior;
-  t->to_create_inferior = inf_ptrace_create_inferior;
-  t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
-  t->to_acknowledge_created_inferior =
-    inf_ptrace_acknowledge_created_inferior;
-  t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
-  t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
-  t->to_insert_vfork_catchpoint = inf_ptrace_insert_vfork_catchpoint;
-  t->to_remove_vfork_catchpoint = inf_ptrace_remove_vfork_catchpoint;
-  t->to_follow_fork = inf_ptrace_follow_fork;
-  t->to_insert_exec_catchpoint = inf_ptrace_insert_exec_catchpoint;
-  t->to_remove_exec_catchpoint = inf_ptrace_remove_exec_catchpoint;
-  t->to_reported_exec_events_per_exec_call =
-    inf_ptrace_reported_exec_events_per_exec_call;
-  t->to_has_exited = inf_ptrace_has_exited;
-  t->to_mourn_inferior = inf_ptrace_mourn_inferior;
-  t->to_can_run = inf_ptrace_can_run;
-  t->to_thread_alive = inf_ptrace_thread_alive;
-  t->to_pid_to_str = inf_ptrace_pid_to_str;
-  t->to_stop = inf_ptrace_stop;
-  t->to_stratum = process_stratum;
-  t->to_has_all_memory = 1;
-  t->to_has_memory = 1;
-  t->to_has_stack = 1;
-  t->to_has_registers = 1;
-  t->to_has_execution = 1;
-  t->to_magic = OPS_MAGIC;
-  ptrace_ops_hack = t;
-  return t;
+  struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
+  struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
+  const int sizeof_auxv_type = TYPE_LENGTH (int_type);
+  const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
+  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
+  gdb_byte *ptr = *readptr;
+
+  if (endptr == ptr)
+    return 0;
+
+  if (endptr - ptr < 2 * sizeof_auxv_val)
+    return -1;
+
+  *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
+  ptr += sizeof_auxv_val;      /* Alignment.  */
+  *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
+  ptr += sizeof_auxv_val;
+
+  *readptr = ptr;
+  return 1;
 }
+
+#endif
+\f
This page took 0.04434 seconds and 4 git commands to generate.