gdb: add target_ops methods for displaced stepping
[deliverable/binutils-gdb.git] / gdb / infrun.c
index 3cbad4b24b15c8c4b526f0a507503a73f4b8e0f4..9366cbe25ff838f1ed64051967cdfc26a83eac8c 100644 (file)
@@ -2,7 +2,7 @@
    process.
 
    Copyright (C) 1986-2020 Free Software Foundation, Inc.
-   Copyright (C) 2019 Advanced Micro Devices, Inc. All rights reserved.
+   Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
 
    This file is part of GDB.
 
@@ -20,6 +20,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "displaced-stepping.h"
 #include "infrun.h"
 #include <ctype.h>
 #include "symtab.h"
@@ -35,6 +36,7 @@
 #include "top.h"
 #include "inf-loop.h"
 #include "regcache.h"
+#include "utils.h"
 #include "value.h"
 #include "observable.h"
 #include "language.h"
@@ -1234,6 +1236,8 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      to symbol_file_command...).  */
   insert_breakpoints ();
 
+  gdb::observers::inferior_execd.notify (inf);
+
   /* The next resume of this inferior should bring it to the shlib
      startup breakpoints.  (If the user had also set bp's on
      "main" from the old (parent) process, then they'll auto-
@@ -1247,7 +1251,7 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
    to avoid starvation, otherwise, we could e.g., find ourselves
    constantly stepping the same couple threads past their breakpoints
    over and over, if the single-step finish fast enough.  */
-struct thread_info *step_over_queue_head;
+struct thread_info *global_thread_step_over_chain_head;
 
 /* Bit flags indicating what the thread needs to step over.  */
 
@@ -1464,11 +1468,7 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_fixup for details.  */
 
-/* Default destructor for displaced_step_closure.  */
-
-displaced_step_closure::~displaced_step_closure () = default;
-
-/* Get the displaced stepping state of process PID.  */
+/* Get the displaced stepping state of inferior INF.  */
 
 static displaced_step_inferior_state *
 get_displaced_stepping_state (inferior *inf)
@@ -1476,55 +1476,68 @@ get_displaced_stepping_state (inferior *inf)
   return &inf->displaced_step_state;
 }
 
-/* Returns true if any inferior has a thread doing a displaced
-   step.  */
+/* Get the displaced stepping state of thread THREAD.  */
 
-static bool
-displaced_step_in_progress_any_inferior ()
+static displaced_step_thread_state *
+get_displaced_stepping_state (thread_info *thread)
 {
-  for (inferior *i : all_inferiors ())
-    {
-      if (i->displaced_step_state.step_thread != nullptr)
-       return true;
-    }
-
-  return false;
+  return &thread->displaced_step_state;
 }
 
-/* Return true if thread represented by PTID is doing a displaced
-   step.  */
+/* Return true if the given thread is doing a displaced step.  */
 
-static int
-displaced_step_in_progress_thread (thread_info *thread)
+static bool
+displaced_step_in_progress (thread_info *thread)
 {
   gdb_assert (thread != NULL);
 
-  return get_displaced_stepping_state (thread->inf)->step_thread == thread;
+  return get_displaced_stepping_state (thread)->in_progress ();
 }
 
-/* Return true if process PID has a thread doing a displaced step.  */
+/* Return true if any thread of this inferior is doing a displaced step.  */
 
-static int
+static bool
 displaced_step_in_progress (inferior *inf)
 {
-  return get_displaced_stepping_state (inf)->step_thread != nullptr;
+  for (thread_info *thread : inf->non_exited_threads ())
+    {
+      if (displaced_step_in_progress (thread))
+       return true;
+    }
+
+  return false;
 }
 
-/* If inferior is in displaced stepping, and ADDR equals to starting address
-   of copy area, return corresponding displaced_step_closure.  Otherwise,
-   return NULL.  */
+/* Return true if any thread is doing a displaced step.  */
 
-struct displaced_step_closure*
-get_displaced_step_closure_by_addr (CORE_ADDR addr)
+static bool
+displaced_step_in_progress_any_thread ()
 {
-  displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (current_inferior ());
+  for (thread_info *thread : all_non_exited_threads ())
+    {
+      if (displaced_step_in_progress (thread))
+       return true;
+    }
 
-  /* If checking the mode of displaced instruction in copy area.  */
-  if (displaced->step_thread != nullptr
-      && displaced->step_copy == addr)
-    return displaced->step_closure;
+  return false;
+}
 
+/* If inferior is in displaced stepping, and ADDR equals to starting address
+   of copy area, return corresponding displaced_step_copy_insn_closure.  Otherwise,
+   return NULL.  */
+
+struct displaced_step_copy_insn_closure *
+get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr)
+{
+// FIXME: implement me (only needed on ARM).
+//  displaced_step_inferior_state *displaced
+//    = get_displaced_stepping_state (current_inferior ());
+//
+//  /* If checking the mode of displaced instruction in copy area.  */
+//  if (displaced->step_thread != nullptr
+//      && displaced->step_copy == addr)
+//    return displaced->step_closure.get ();
+//
   return NULL;
 }
 
@@ -1539,8 +1552,7 @@ infrun_inferior_exit (struct inferior *inf)
    doesn't support it, GDB will instead use the traditional
    hold-and-step approach.  If AUTO (which is the default), GDB will
    decide which technique to use to step over breakpoints depending on
-   which of all-stop or non-stop mode is active --- displaced stepping
-   in non-stop mode; hold-and-step in all-stop mode.  */
+   whether the target works in a non-stop way (see use_displaced_stepping).  */
 
 static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
 
@@ -1560,39 +1572,68 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
                        "to step over breakpoints is %s.\n"), value);
 }
 
+/* Return true if the gdbarch implements the required methods to use
+   displaced stepping.  */
+
+static bool
+gdbarch_supports_displaced_stepping (gdbarch *arch)
+{
+  /* Only check for the presence of copy_insn.  Other required methods
+     are checked by the gdbarch validation to be provided if copy_insn is
+     provided.  */
+  return gdbarch_displaced_step_copy_insn_p (arch);
+}
+
 /* Return non-zero if displaced stepping can/should be used to step
    over breakpoints of thread TP.  */
 
-static int
-use_displaced_stepping (struct thread_info *tp)
+static bool
+use_displaced_stepping (thread_info *tp)
 {
-  struct regcache *regcache = get_thread_regcache (tp);
-  struct gdbarch *gdbarch = regcache->arch ();
+  /* If the user disabled it explicitly, don't use displaced stepping.  */
+  if (can_use_displaced_stepping == AUTO_BOOLEAN_FALSE)
+    return false;
+
+  /* If "auto", only use displaced stepping if the target operates in a non-stop
+     way.  */
+  if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO
+      && !target_is_non_stop_p ())
+    return false;
+
+  gdbarch *gdbarch = get_thread_regcache (tp)->arch ();
+
+  /* If the architecture doesn't implement displaced stepping, don't use
+     it.  */
+  if (!gdbarch_supports_displaced_stepping (gdbarch))
+    return false;
+
+  /* If recording, don't use displaced stepping.  */
+  if (find_record_target () != nullptr)
+    return false;
+
   displaced_step_inferior_state *displaced_state
     = get_displaced_stepping_state (tp->inf);
 
-  return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO
-           && target_is_non_stop_p ())
-          || can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
-         && gdbarch_displaced_step_copy_insn_p (gdbarch)
-         && find_record_target () == NULL
-         && !displaced_state->failed_before);
+  /* If displaced stepping failed before for this inferior, don't bother trying
+     again.  */
+  if (displaced_state->failed_before)
+    return false;
+
+  return true;
 }
 
-/* Clean out any stray displaced stepping state.  */
+/* Simple function wrapper around displaced_step_thread_state::reset.  */
+
 static void
-displaced_step_clear (struct displaced_step_inferior_state *displaced)
+displaced_step_reset (displaced_step_thread_state *displaced)
 {
-  /* Indicate that there is no cleanup pending.  */
-  displaced->step_thread = nullptr;
-
-  delete displaced->step_closure;
-  displaced->step_closure = NULL;
+  displaced->reset ();
 }
 
-/* A cleanup that wraps displaced_step_clear.  */
-using displaced_step_clear_cleanup
-  = FORWARD_SCOPE_EXIT (displaced_step_clear);
+/* A cleanup that wraps displaced_step_reset.  We use this instead of, say,
+   SCOPE_EXIT, because it needs to be discardable with "cleanup.release ()".  */
+
+using displaced_step_reset_cleanup = FORWARD_SCOPE_EXIT (displaced_step_reset);
 
 /* Dump LEN bytes at BUF in hex to FILE, followed by a newline.  */
 void
@@ -1623,20 +1664,17 @@ displaced_step_dump_bytes (struct ui_file *file,
    stepped now; 0 if displaced stepping this thread got queued; or -1
    if this instruction can't be displaced stepped.  */
 
-static int
+static displaced_step_prepare_status
 displaced_step_prepare_throw (thread_info *tp)
 {
   regcache *regcache = get_thread_regcache (tp);
   struct gdbarch *gdbarch = regcache->arch ();
-  const address_space *aspace = regcache->aspace ();
-  CORE_ADDR original, copy;
-  ULONGEST len;
-  struct displaced_step_closure *closure;
-  int status;
+  displaced_step_thread_state *thread_disp_step_state
+    = get_displaced_stepping_state (tp);
 
   /* We should never reach this function if the architecture does not
      support displaced stepping.  */
-  gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
+  gdb_assert (gdbarch_supports_displaced_stepping (gdbarch));
 
   /* Nor if the thread isn't meant to step over a breakpoint.  */
   gdb_assert (tp->control.trap_expected);
@@ -1647,127 +1685,103 @@ displaced_step_prepare_throw (thread_info *tp)
      jump/branch).  */
   tp->control.may_range_step = 0;
 
-  /* We have to displaced step one thread at a time, as we only have
-     access to a single scratch space per inferior.  */
-
-  displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (tp->inf);
-
-  if (displaced->step_thread != nullptr)
-    {
-      /* Already waiting for a displaced step to finish.  Defer this
-        request and place in queue.  */
-
-      if (debug_displaced)
-       fprintf_unfiltered (gdb_stdlog,
-                           "displaced: deferring step of %s\n",
-                           target_pid_to_str (tp->ptid).c_str ());
-
-      thread_step_over_chain_enqueue (tp);
-      return 0;
-    }
-  else
-    {
-      if (debug_displaced)
-       fprintf_unfiltered (gdb_stdlog,
-                           "displaced: stepping %s now\n",
-                           target_pid_to_str (tp->ptid).c_str ());
-    }
-
-  displaced_step_clear (displaced);
+  /* We are about to start a displaced step for this thread, if one is already
+     in progress, we goofed up somewhere.  */
+  gdb_assert (!thread_disp_step_state->in_progress ());
 
   scoped_restore_current_thread restore_thread;
 
   switch_to_thread (tp);
 
-  original = regcache_read_pc (regcache);
+  CORE_ADDR original_pc = regcache_read_pc (regcache);
 
-  copy = gdbarch_displaced_step_location (gdbarch);
-  len = gdbarch_max_insn_length (gdbarch);
+  displaced_step_prepare_status status =
+    current_top_target ()->displaced_step_prepare (tp);
 
-  if (breakpoint_in_range_p (aspace, copy, len))
+  if (status == DISPLACED_STEP_PREPARE_STATUS_ERROR)
     {
-      /* There's a breakpoint set in the scratch pad location range
-        (which is usually around the entry point).  We'd either
-        install it before resuming, which would overwrite/corrupt the
-        scratch pad, or if it was already inserted, this displaced
-        step would overwrite it.  The latter is OK in the sense that
-        we already assume that no thread is going to execute the code
-        in the scratch pad range (after initial startup) anyway, but
-        the former is unacceptable.  Simply punt and fallback to
-        stepping over this breakpoint in-line.  */
       if (debug_displaced)
-       {
-         fprintf_unfiltered (gdb_stdlog,
-                             "displaced: breakpoint set in scratch pad.  "
-                             "Stepping over breakpoint in-line instead.\n");
-       }
+       fprintf_unfiltered (gdb_stdlog,
+                           "displaced: failed to prepare (%s)",
+                           target_pid_to_str (tp->ptid).c_str ());
 
-      return -1;
+      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }
-
-  /* Save the original contents of the copy area.  */
-  displaced->step_saved_copy.resize (len);
-  status = target_read_memory (copy, displaced->step_saved_copy.data (), len);
-  if (status != 0)
-    throw_error (MEMORY_ERROR,
-                _("Error accessing memory address %s (%s) for "
-                  "displaced-stepping scratch space."),
-                paddress (gdbarch, copy), safe_strerror (status));
-  if (debug_displaced)
+  else if (status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
     {
-      fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
-                         paddress (gdbarch, copy));
-      displaced_step_dump_bytes (gdb_stdlog,
-                                displaced->step_saved_copy.data (),
-                                len);
-    };
+      /* Not enough displaced stepping resources available, defer this
+        request by placing it the queue.  */
 
-  closure = gdbarch_displaced_step_copy_insn (gdbarch,
-                                             original, copy, regcache);
-  if (closure == NULL)
-    {
-      /* The architecture doesn't know how or want to displaced step
-        this instruction or instruction sequence.  Fallback to
-        stepping over the breakpoint in-line.  */
-      return -1;
-    }
+      if (debug_displaced)
+       fprintf_unfiltered (gdb_stdlog,
+                           "displaced: not enough resources available, "
+                           "deferring step of %s\n",
+                           target_pid_to_str (tp->ptid).c_str ());
+
+      global_thread_step_over_chain_enqueue (tp);
+      tp->inf->displaced_step_state.unavailable = true;
+
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
+    }
+
+  gdb_assert (status == DISPLACED_STEP_PREPARE_STATUS_OK);
+
+// FIXME: Should probably replicated in the arch implementation now.
+//
+//  if (breakpoint_in_range_p (aspace, copy, len))
+//    {
+//      /* There's a breakpoint set in the scratch pad location range
+//      (which is usually around the entry point).  We'd either
+//      install it before resuming, which would overwrite/corrupt the
+//      scratch pad, or if it was already inserted, this displaced
+//      step would overwrite it.  The latter is OK in the sense that
+//      we already assume that no thread is going to execute the code
+//      in the scratch pad range (after initial startup) anyway, but
+//      the former is unacceptable.  Simply punt and fallback to
+//      stepping over this breakpoint in-line.  */
+//      if (debug_displaced)
+//     {
+//       fprintf_unfiltered (gdb_stdlog,
+//                           "displaced: breakpoint set in scratch pad.  "
+//                           "Stepping over breakpoint in-line instead.\n");
+//     }
+//
+//      gdb_assert (false);
+//      gdbarch_displaced_step_release_location (gdbarch, copy);
+//
+//      return -1;
+//    }
 
   /* Save the information we need to fix things up if the step
      succeeds.  */
-  displaced->step_thread = tp;
-  displaced->step_gdbarch = gdbarch;
-  displaced->step_closure = closure;
-  displaced->step_original = original;
-  displaced->step_copy = copy;
-
-  {
-    displaced_step_clear_cleanup cleanup (displaced);
+  thread_disp_step_state->set (gdbarch);
 
-    /* Resume execution at the copy.  */
-    regcache_write_pc (regcache, copy);
-
-    cleanup.release ();
-  }
+  // FIXME: get it from _prepare?
+  CORE_ADDR displaced_pc = 0;
 
   if (debug_displaced)
-    fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
-                       paddress (gdbarch, copy));
+    fprintf_unfiltered (gdb_stdlog,
+                       "displaced: prepared successfully thread=%s, "
+                       "original_pc=%s, displaced_pc=%s\n",
+                       target_pid_to_str (tp->ptid).c_str (),
+                       paddress (gdbarch, original_pc),
+                       paddress (gdbarch, displaced_pc));
 
-  return 1;
+  return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
 
 /* Wrapper for displaced_step_prepare_throw that disabled further
    attempts at displaced stepping if we get a memory error.  */
 
-static int
+static displaced_step_prepare_status
 displaced_step_prepare (thread_info *thread)
 {
-  int prepared = -1;
+  displaced_step_prepare_status status
+    = DISPLACED_STEP_PREPARE_STATUS_ERROR;
 
   try
     {
-      prepared = displaced_step_prepare_throw (thread);
+      status = displaced_step_prepare_throw (thread);
     }
   catch (const gdb_exception_error &ex)
     {
@@ -1798,34 +1812,7 @@ displaced_step_prepare (thread_info *thread)
       displaced_state->failed_before = 1;
     }
 
-  return prepared;
-}
-
-static void
-write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
-                  const gdb_byte *myaddr, int len)
-{
-  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
-
-  inferior_ptid = ptid;
-  write_memory (memaddr, myaddr, len);
-}
-
-/* Restore the contents of the copy area for thread PTID.  */
-
-static void
-displaced_step_restore (struct displaced_step_inferior_state *displaced,
-                       ptid_t ptid)
-{
-  ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
-
-  write_memory_ptid (ptid, displaced->step_copy,
-                    displaced->step_saved_copy.data (), len);
-  if (debug_displaced)
-    fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n",
-                       target_pid_to_str (ptid).c_str (),
-                       paddress (displaced->step_gdbarch,
-                                 displaced->step_copy));
+  return status;
 }
 
 /* If we displaced stepped an instruction successfully, adjust
@@ -1835,52 +1822,32 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
    -1.  If the thread wasn't displaced stepping, return 0.  */
 
 static int
-displaced_step_fixup (thread_info *event_thread, enum gdb_signal signal)
+displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
-  struct displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (event_thread->inf);
-  int ret;
+  displaced_step_thread_state *displaced
+    = get_displaced_stepping_state (event_thread);
 
-  /* Was this event for the thread we displaced?  */
-  if (displaced->step_thread != event_thread)
+  /* Was this thread performing a displaced step?  */
+  if (!displaced->in_progress ())
     return 0;
 
-  displaced_step_clear_cleanup cleanup (displaced);
-
-  displaced_step_restore (displaced, displaced->step_thread->ptid);
+  displaced_step_reset_cleanup cleanup (displaced);
 
   /* Fixup may need to read memory/registers.  Switch to the thread
      that we're fixing up.  Also, target_stopped_by_watchpoint checks
-     the current thread.  */
+     the current thread, and displaced_step_restore performs ptid-dependent
+     memory accesses using current_inferior() and current_top_target().  */
   switch_to_thread (event_thread);
 
-  /* Did the instruction complete successfully?  */
-  if (signal == GDB_SIGNAL_TRAP
-      && !(target_stopped_by_watchpoint ()
-          && (gdbarch_have_nonsteppable_watchpoint (displaced->step_gdbarch)
-              || target_have_steppable_watchpoint)))
-    {
-      /* Fix up the resulting state.  */
-      gdbarch_displaced_step_fixup (displaced->step_gdbarch,
-                                    displaced->step_closure,
-                                    displaced->step_original,
-                                    displaced->step_copy,
-                                    get_thread_regcache (displaced->step_thread));
-      ret = 1;
-    }
-  else
-    {
-      /* Since the instruction didn't complete, all we can do is
-         relocate the PC.  */
-      struct regcache *regcache = get_thread_regcache (event_thread);
-      CORE_ADDR pc = regcache_read_pc (regcache);
-
-      pc = displaced->step_original + (pc - displaced->step_copy);
-      regcache_write_pc (regcache, pc);
-      ret = -1;
-    }
+  /* Do the fixup, and release the resources acquired to do the displaced
+     step. */
+  displaced_step_finish_status finish_status =
+    current_top_target ()->displaced_step_finish (event_thread, signal);
 
-  return ret;
+  if (finish_status == DISPLACED_STEP_FINISH_STATUS_OK)
+    return 1;
+  else
+    return -1;
 }
 
 /* Data to be passed around while handling an event.  This data is
@@ -1928,13 +1895,26 @@ static int
 start_step_over (void)
 {
   struct thread_info *tp, *next;
+  int started = 0;
 
   /* Don't start a new step-over if we already have an in-line
      step-over operation ongoing.  */
   if (step_over_info_valid_p ())
-    return 0;
+    return started;
+
+  /* Steal the global thread step over chain.  */
+  thread_info *threads_to_step = global_thread_step_over_chain_head;
+  global_thread_step_over_chain_head = NULL;
+
+  if (debug_infrun)
+    fprintf_unfiltered (gdb_stdlog,
+                       "infrun: stealing list of %d threads to step from global queue\n",
+                       thread_step_over_chain_length (threads_to_step));
 
-  for (tp = step_over_queue_head; tp != NULL; tp = next)
+  for (inferior *inf : all_inferiors ())
+    inf->displaced_step_state.unavailable = false;
+
+  for (tp = threads_to_step; tp != NULL; tp = next)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs = &ecss;
@@ -1943,12 +1923,7 @@ start_step_over (void)
 
       gdb_assert (!tp->stop_requested);
 
-      next = thread_step_over_chain_next (tp);
-
-      /* If this inferior already has a displaced step in process,
-        don't start a new one.  */
-      if (displaced_step_in_progress (tp->inf))
-       continue;
+      next = thread_step_over_chain_next (threads_to_step, tp);
 
       step_what = thread_still_needs_step_over (tp);
       must_be_in_line = ((step_what & STEP_OVER_WATCHPOINT)
@@ -1958,17 +1933,10 @@ start_step_over (void)
       /* We currently stop all threads of all processes to step-over
         in-line.  If we need to start a new in-line step-over, let
         any pending displaced steps finish first.  */
-      if (must_be_in_line && displaced_step_in_progress_any_inferior ())
-       return 0;
-
-      thread_step_over_chain_remove (tp);
+      if (must_be_in_line && displaced_step_in_progress_any_thread ())
+       continue;
 
-      if (step_over_queue_head == NULL)
-       {
-         if (debug_infrun)
-           fprintf_unfiltered (gdb_stdlog,
-                               "infrun: step-over queue now empty\n");
-       }
+      thread_step_over_chain_remove (&threads_to_step, tp);
 
       if (tp->control.trap_expected
          || tp->resumed
@@ -1997,6 +1965,12 @@ start_step_over (void)
       if (!target_is_non_stop_p () && !step_what)
        continue;
 
+      if (tp->inf->displaced_step_state.unavailable)
+       {
+         global_thread_step_over_chain_enqueue (tp);
+         continue;
+       }
+
       switch_to_thread (tp);
       reset_ecs (ecs, tp);
       keep_going_pass_signal (ecs);
@@ -2004,13 +1978,30 @@ start_step_over (void)
       if (!ecs->wait_some_more)
        error (_("Command aborted."));
 
-      gdb_assert (tp->resumed);
+      /* If the thread's step over could not be initiated, it was re-added
+         to the global step over chain.  */
+      if (tp->resumed)
+       {
+         if (debug_infrun)
+           fprintf_unfiltered (gdb_stdlog, "infrun: start_step_over: [%s] was resumed.\n",
+                               target_pid_to_str (tp->ptid).c_str ());
+         gdb_assert (!thread_is_in_step_over_chain (tp));
+       }
+      else
+       {
+         if (debug_infrun)
+           fprintf_unfiltered (gdb_stdlog, "infrun: start_step_over: [%s] was NOT resumed.\n",
+                           target_pid_to_str (tp->ptid).c_str ());
+         gdb_assert (thread_is_in_step_over_chain (tp));
+
+       }
 
       /* If we started a new in-line step-over, we're done.  */
       if (step_over_info_valid_p ())
        {
          gdb_assert (tp->control.trap_expected);
-         return 1;
+         started = 1;
+         break;
        }
 
       if (!target_is_non_stop_p ())
@@ -2023,7 +2014,8 @@ start_step_over (void)
          /* With remote targets (at least), in all-stop, we can't
             issue any further remote commands until the program stops
             again.  */
-         return 1;
+         started = 1;
+         break;
        }
 
       /* Either the thread no longer needed a step-over, or a new
@@ -2032,7 +2024,35 @@ start_step_over (void)
         displaced step on a thread of other process. */
     }
 
-  return 0;
+    /* If there are threads left in the THREADS_TO_STEP list, but we have
+       detected that we can't start anything more, put back these threads
+       in the global list.  */
+    if (threads_to_step == NULL)
+      {
+       if (debug_infrun)
+         fprintf_unfiltered (gdb_stdlog,
+                             "infrun: step-over queue now empty\n");
+      }
+    else
+      {
+       if (debug_infrun)
+         fprintf_unfiltered (gdb_stdlog,
+                             "infrun: putting back %d threads to step in global queue\n",
+                             thread_step_over_chain_length (threads_to_step));
+       while (threads_to_step != nullptr)
+         {
+           thread_info *thread = threads_to_step;
+
+           /* Remove from that list.  */
+           thread_step_over_chain_remove (&threads_to_step, thread);
+
+           /* Add to global list.  */
+           global_thread_step_over_chain_enqueue (thread);
+
+         }
+      }
+
+  return started;
 }
 
 /* Update global variables holding ptids to hold NEW_PTID if they were
@@ -2406,9 +2426,10 @@ resume_1 (enum gdb_signal sig)
       && sig == GDB_SIGNAL_0
       && !current_inferior ()->waiting_for_vfork_done)
     {
-      int prepared = displaced_step_prepare (tp);
+      displaced_step_prepare_status prepare_status
+       = displaced_step_prepare (tp);
 
-      if (prepared == 0)
+      if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
        {
          if (debug_infrun)
            fprintf_unfiltered (gdb_stdlog,
@@ -2417,7 +2438,7 @@ resume_1 (enum gdb_signal sig)
          tp->control.trap_expected = 0;
          return;
        }
-      else if (prepared < 0)
+      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_ERROR)
        {
          /* Fallback to stepping over the breakpoint in-line.  */
 
@@ -2431,18 +2452,12 @@ resume_1 (enum gdb_signal sig)
 
          insert_breakpoints ();
        }
-      else if (prepared > 0)
+      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_OK)
        {
-         struct displaced_step_inferior_state *displaced;
-
-         /* Update pc to reflect the new address from which we will
-            execute instructions due to displaced stepping.  */
-         pc = regcache_read_pc (get_thread_regcache (tp));
-
-         displaced = get_displaced_stepping_state (tp->inf);
-         step = gdbarch_displaced_step_hw_singlestep (gdbarch,
-                                                      displaced->step_closure);
+         step = gdbarch_displaced_step_hw_singlestep (gdbarch, NULL);
        }
+      else
+       gdb_assert_not_reached ("invalid displaced_step_prepare_status value");
     }
 
   /* Do we need to do it the hard way, w/temp breakpoints?  */
@@ -2956,14 +2971,14 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
                                "infrun: need to step-over [%s] first\n",
                                target_pid_to_str (tp->ptid).c_str ());
 
-         thread_step_over_chain_enqueue (tp);
+         global_thread_step_over_chain_enqueue (tp);
        }
     }
 
   /* Enqueue the current thread last, so that we move all other
      threads over their breakpoints first.  */
   if (cur_thr->stepping_over_breakpoint)
-    thread_step_over_chain_enqueue (cur_thr);
+    global_thread_step_over_chain_enqueue (cur_thr);
 
   /* If the thread isn't started, we'll still need to set its prev_pc,
      so that switch_back_to_stepped_thread knows the thread hasn't
@@ -3138,7 +3153,7 @@ infrun_thread_stop_requested (ptid_t ptid)
         start_step_over doesn't try to resume them
         automatically.  */
       if (thread_is_in_step_over_chain (tp))
-       thread_step_over_chain_remove (tp);
+       global_thread_step_over_chain_remove (tp);
 
       /* If the thread is stopped, but the user/frontend doesn't
         know about that yet, queue a pending event, as if the
@@ -3447,11 +3462,11 @@ prepare_for_detach (void)
   struct inferior *inf = current_inferior ();
   ptid_t pid_ptid = ptid_t (inf->pid);
 
-  displaced_step_inferior_state *displaced = get_displaced_stepping_state (inf);
+  // displaced_step_inferior_state *displaced = get_displaced_stepping_state (inf);
 
   /* Is any thread of this process displaced stepping?  If not,
      there's nothing else to do.  */
-  if (displaced->step_thread == nullptr)
+  if (displaced_step_in_progress (inf))
     return;
 
   if (debug_infrun)
@@ -3460,7 +3475,8 @@ prepare_for_detach (void)
 
   scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
 
-  while (displaced->step_thread != nullptr)
+  // FIXME
+  while (false)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs;
@@ -4406,7 +4422,7 @@ stop_all_threads (void)
                  t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
                  t->suspend.waitstatus_pending_p = 0;
 
-                 if (displaced_step_fixup (t, GDB_SIGNAL_0) < 0)
+                 if (displaced_step_finish (t, GDB_SIGNAL_0) < 0)
                    {
                      /* Add it back to the step-over queue.  */
                      if (debug_infrun)
@@ -4418,7 +4434,7 @@ stop_all_threads (void)
                                              target_pid_to_str (t->ptid).c_str ());
                        }
                      t->control.trap_expected = 0;
-                     thread_step_over_chain_enqueue (t);
+                     global_thread_step_over_chain_enqueue (t);
                    }
                }
              else
@@ -4445,11 +4461,11 @@ stop_all_threads (void)
                  sig = (ws.kind == TARGET_WAITKIND_STOPPED
                         ? ws.value.sig : GDB_SIGNAL_0);
 
-                 if (displaced_step_fixup (t, sig) < 0)
+                 if (displaced_step_finish (t, sig) < 0)
                    {
                      /* Add it back to the step-over queue.  */
                      t->control.trap_expected = 0;
-                     thread_step_over_chain_enqueue (t);
+                     global_thread_step_over_chain_enqueue (t);
                    }
 
                  regcache = get_thread_regcache (t);
@@ -4901,32 +4917,33 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
 
        /* If checking displaced stepping is supported, and thread
           ecs->ptid is displaced stepping.  */
-       if (displaced_step_in_progress_thread (ecs->event_thread))
+       if (displaced_step_in_progress (ecs->event_thread))
          {
            struct inferior *parent_inf
              = find_inferior_ptid (ecs->ptid);
            struct regcache *child_regcache;
            CORE_ADDR parent_pc;
 
+           if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
+             {
+               // struct displaced_step_inferior_state *displaced
+               //  = get_displaced_stepping_state (parent_inf);
+
+               /* Restore scratch pad for child process.  */
+               //displaced_step_restore (displaced, ecs->ws.value.related_pid);
+               // FIXME: we should restore all the buffers that were currently in use
+             }
+
            /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
               indicating that the displaced stepping of syscall instruction
               has been done.  Perform cleanup for parent process here.  Note
               that this operation also cleans up the child process for vfork,
               because their pages are shared.  */
-           displaced_step_fixup (ecs->event_thread, GDB_SIGNAL_TRAP);
+           displaced_step_finish (ecs->event_thread, GDB_SIGNAL_TRAP);
            /* Start a new step-over in another thread if there's one
               that needs it.  */
            start_step_over ();
 
-           if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
-             {
-               struct displaced_step_inferior_state *displaced
-                 = get_displaced_stepping_state (parent_inf);
-
-               /* Restore scratch pad for child process.  */
-               displaced_step_restore (displaced, ecs->ws.value.related_pid);
-             }
-
            /* Since the vfork/fork syscall instruction was executed in the scratchpad,
               the child's PC is also within the scratchpad.  Set the child's PC
               to the parent's PC value, which has already been fixed up.
@@ -5280,8 +5297,8 @@ finish_step_over (struct execution_control_state *ecs)
 {
   int had_step_over_info;
 
-  displaced_step_fixup (ecs->event_thread,
-                       ecs->event_thread->suspend.stop_signal);
+  displaced_step_finish (ecs->event_thread,
+                        ecs->event_thread->suspend.stop_signal);
 
   had_step_over_info = step_over_info_valid_p ();
 
@@ -7473,7 +7490,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
                                "infrun: step-over already in progress: "
                                "step-over for %s deferred\n",
                                target_pid_to_str (tp->ptid).c_str ());
-         thread_step_over_chain_enqueue (tp);
+         global_thread_step_over_chain_enqueue (tp);
        }
       else
        {
This page took 0.049382 seconds and 4 git commands to generate.