PPC64: Fix gdb.arch/ppc64-atomic-inst.exp with displaced stepping
[deliverable/binutils-gdb.git] / gdb / infrun.c
index 9690a3608f50dfd23bc355a13cc36c68ebe57950..21aa8cf3ad8a185dc79c3feb525a2633c508f5f0 100644 (file)
@@ -1468,6 +1468,10 @@ struct displaced_step_inferior_state
   /* The process this displaced step state refers to.  */
   int pid;
 
+  /* True if preparing a displaced step ever failed.  If so, we won't
+     try displaced stepping for this inferior again.  */
+  int failed_before;
+
   /* If this is not null_ptid, this is the thread carrying out a
      displaced single-step in process PID.  This thread's state will
      require fixing up once it has completed its step.  */
@@ -1630,7 +1634,7 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
     fprintf_filtered (file,
                      _("Debugger's willingness to use displaced stepping "
                        "to step over breakpoints is %s (currently %s).\n"),
-                     value, non_stop ? "on" : "off");
+                     value, target_is_non_stop_p () ? "on" : "off");
   else
     fprintf_filtered (file,
                      _("Debugger's willingness to use displaced stepping "
@@ -1638,15 +1642,24 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
 }
 
 /* Return non-zero if displaced stepping can/should be used to step
-   over breakpoints.  */
+   over breakpoints of thread TP.  */
 
 static int
-use_displaced_stepping (struct gdbarch *gdbarch)
+use_displaced_stepping (struct thread_info *tp)
 {
-  return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO && non_stop)
+  struct regcache *regcache = get_thread_regcache (tp->ptid);
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  struct displaced_step_inferior_state *displaced_state;
+
+  displaced_state = get_displaced_stepping_state (ptid_get_pid (tp->ptid));
+
+  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);
+         && find_record_target () == NULL
+         && (displaced_state == NULL
+             || !displaced_state->failed_before));
 }
 
 /* Clean out any stray displaced stepping state.  */
@@ -1698,9 +1711,11 @@ displaced_step_dump_bytes (struct ui_file *file,
    explain how we handle this case instead.
 
    Returns 1 if preparing was successful -- this thread is going to be
-   stepped now; or 0 if displaced stepping this thread got queued.  */
+   stepped now; 0 if displaced stepping this thread got queued; or -1
+   if this instruction can't be displaced stepped.  */
+
 static int
-displaced_step_prepare (ptid_t ptid)
+displaced_step_prepare_throw (ptid_t ptid)
 {
   struct cleanup *old_cleanups, *ignore_cleanups;
   struct thread_info *tp = find_thread_ptid (ptid);
@@ -1782,9 +1797,14 @@ displaced_step_prepare (ptid_t ptid)
 
   closure = gdbarch_displaced_step_copy_insn (gdbarch,
                                              original, copy, regcache);
-
-  /* We don't support the fully-simulated case at present.  */
-  gdb_assert (closure);
+  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.  */
+      do_cleanups (old_cleanups);
+      return -1;
+    }
 
   /* Save the information we need to fix things up if the step
      succeeds.  */
@@ -1810,6 +1830,50 @@ displaced_step_prepare (ptid_t ptid)
   return 1;
 }
 
+/* Wrapper for displaced_step_prepare_throw that disabled further
+   attempts at displaced stepping if we get a memory error.  */
+
+static int
+displaced_step_prepare (ptid_t ptid)
+{
+  int prepared = -1;
+
+  TRY
+    {
+      prepared = displaced_step_prepare_throw (ptid);
+    }
+  CATCH (ex, RETURN_MASK_ERROR)
+    {
+      struct displaced_step_inferior_state *displaced_state;
+
+      if (ex.error != MEMORY_ERROR)
+       throw_exception (ex);
+
+      if (debug_infrun)
+       {
+         fprintf_unfiltered (gdb_stdlog,
+                             "infrun: disabling displaced stepping: %s\n",
+                             ex.message);
+       }
+
+      /* Be verbose if "set displaced-stepping" is "on", silent if
+        "auto".  */
+      if (can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
+       {
+         warning (_("disabling displaced stepping: %s\n"),
+                  ex.message);
+       }
+
+      /* Disable further displaced stepping attempts.  */
+      displaced_state
+       = get_displaced_stepping_state (ptid_get_pid (ptid));
+      displaced_state->failed_before = 1;
+    }
+  END_CATCH
+
+  return prepared;
+}
+
 static void
 write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
                   const gdb_byte *myaddr, int len)
@@ -1940,6 +2004,7 @@ static void keep_going_pass_signal (struct execution_control_state *ecs);
 static void prepare_to_wait (struct execution_control_state *ecs);
 static int keep_going_stepped_thread (struct thread_info *tp);
 static int thread_still_needs_step_over (struct thread_info *tp);
+static void stop_all_threads (void);
 
 /* Are there any pending step-over requests?  If so, run all we can
    now and return true.  Otherwise, return false.  */
@@ -1960,8 +2025,6 @@ start_step_over (void)
       struct execution_control_state *ecs = &ecss;
       enum step_over_what step_what;
       int must_be_in_line;
-      struct regcache *regcache = get_thread_regcache (tp->ptid);
-      struct gdbarch *gdbarch = get_regcache_arch (regcache);
 
       next = thread_step_over_chain_next (tp);
 
@@ -1973,7 +2036,7 @@ start_step_over (void)
       step_what = thread_still_needs_step_over (tp);
       must_be_in_line = ((step_what & STEP_OVER_WATCHPOINT)
                         || ((step_what & STEP_OVER_BREAKPOINT)
-                            && !use_displaced_stepping (gdbarch)));
+                            && !use_displaced_stepping (tp)));
 
       /* 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
@@ -2014,7 +2077,7 @@ start_step_over (void)
         because we wouldn't be able to resume anything else until the
         target stops again.  In non-stop, the resume always resumes
         only TP, so it's OK to let the thread resume freely.  */
-      if (!non_stop && !step_what)
+      if (!target_is_non_stop_p () && !step_what)
        continue;
 
       switch_to_thread (tp->ptid);
@@ -2033,7 +2096,7 @@ start_step_over (void)
          return 1;
        }
 
-      if (!non_stop)
+      if (!target_is_non_stop_p ())
        {
          /* On all-stop, shouldn't have resumed unless we needed a
             step over.  */
@@ -2178,6 +2241,25 @@ user_visible_resume_ptid (int step)
   return resume_ptid;
 }
 
+/* Return a ptid representing the set of threads that we will resume,
+   in the perspective of the target, assuming run control handling
+   does not require leaving some threads stopped (e.g., stepping past
+   breakpoint).  USER_STEP indicates whether we're about to start the
+   target for a stepping command.  */
+
+static ptid_t
+internal_resume_ptid (int user_step)
+{
+  /* In non-stop, we always control threads individually.  Note that
+     the target may always work in non-stop mode even with "set
+     non-stop off", in which case user_visible_resume_ptid could
+     return a wildcard ptid.  */
+  if (target_is_non_stop_p ())
+    return inferior_ptid;
+  else
+    return user_visible_resume_ptid (user_step);
+}
+
 /* Wrapper for target_resume, that handles infrun-specific
    bookkeeping.  */
 
@@ -2389,7 +2471,7 @@ resume (enum gdb_signal sig)
              insert_single_step_breakpoint (gdbarch, aspace, pc);
              insert_breakpoints ();
 
-             resume_ptid = user_visible_resume_ptid (user_step);
+             resume_ptid = internal_resume_ptid (user_step);
              do_target_resume (resume_ptid, 0, GDB_SIGNAL_0);
              discard_cleanups (old_cleanups);
              tp->resumed = 1;
@@ -2414,15 +2496,15 @@ resume (enum gdb_signal sig)
      We can't use displaced stepping when we are waiting for vfork_done
      event, displaced stepping breaks the vfork child similarly as single
      step software breakpoint.  */
-  if (use_displaced_stepping (gdbarch)
-      && tp->control.trap_expected
+  if (tp->control.trap_expected
+      && use_displaced_stepping (tp)
       && !step_over_info_valid_p ()
       && sig == GDB_SIGNAL_0
       && !current_inferior ()->waiting_for_vfork_done)
     {
-      struct displaced_step_inferior_state *displaced;
+      int prepared = displaced_step_prepare (inferior_ptid);
 
-      if (!displaced_step_prepare (inferior_ptid))
+      if (prepared == 0)
        {
          if (debug_infrun)
            fprintf_unfiltered (gdb_stdlog,
@@ -2432,14 +2514,32 @@ resume (enum gdb_signal sig)
          discard_cleanups (old_cleanups);
          return;
        }
+      else if (prepared < 0)
+       {
+         /* Fallback to stepping over the breakpoint in-line.  */
 
-      /* Update pc to reflect the new address from which we will execute
-        instructions due to displaced stepping.  */
-      pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
+         if (target_is_non_stop_p ())
+           stop_all_threads ();
 
-      displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
-      step = gdbarch_displaced_step_hw_singlestep (gdbarch,
-                                                  displaced->step_closure);
+         set_step_over_info (get_regcache_aspace (regcache),
+                             regcache_read_pc (regcache), 0);
+
+         step = maybe_software_singlestep (gdbarch, pc);
+
+         insert_breakpoints ();
+       }
+      else if (prepared > 0)
+       {
+         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 (inferior_ptid));
+
+         displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
+         step = gdbarch_displaced_step_hw_singlestep (gdbarch,
+                                                      displaced->step_closure);
+       }
     }
 
   /* Do we need to do it the hard way, w/temp breakpoints?  */
@@ -2498,12 +2598,7 @@ resume (enum gdb_signal sig)
      use singlestep breakpoint.  */
   gdb_assert (!(thread_has_single_step_breakpoints_set (tp) && step));
 
-  /* Decide the set of threads to ask the target to resume.  Start
-     by assuming everything will be resumed, than narrow the set
-     by applying increasingly restricting conditions.  */
-  resume_ptid = user_visible_resume_ptid (user_step);
-
-  /* Maybe resume a single thread after all.  */
+  /* Decide the set of threads to ask the target to resume.  */
   if ((step || thread_has_single_step_breakpoints_set (tp))
       && tp->control.trap_expected)
     {
@@ -2514,6 +2609,8 @@ resume (enum gdb_signal sig)
         breakpoint if allowed to run.  */
       resume_ptid = inferior_ptid;
     }
+  else
+    resume_ptid = internal_resume_ptid (user_step);
 
   if (execution_direction != EXEC_REVERSE
       && step && breakpoint_inserted_here_p (aspace, pc))
@@ -2561,8 +2658,8 @@ resume (enum gdb_signal sig)
     }
 
   if (debug_displaced
-      && use_displaced_stepping (gdbarch)
       && tp->control.trap_expected
+      && use_displaced_stepping (tp)
       && !step_over_info_valid_p ())
     {
       struct regcache *resume_regcache = get_thread_regcache (tp->ptid);
@@ -2935,11 +3032,52 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
         other thread was already doing one.  In either case, don't
         resume anything else until the step-over is finished.  */
     }
-  else if (started && !non_stop)
+  else if (started && !target_is_non_stop_p ())
     {
       /* A new displaced stepping sequence was started.  In all-stop,
         we can't talk to the target anymore until it next stops.  */
     }
+  else if (!non_stop && target_is_non_stop_p ())
+    {
+      /* In all-stop, but the target is always in non-stop mode.
+        Start all other threads that are implicitly resumed too.  */
+      ALL_NON_EXITED_THREADS (tp)
+        {
+         /* Ignore threads of processes we're not resuming.  */
+         if (!ptid_match (tp->ptid, resume_ptid))
+           continue;
+
+         if (tp->resumed)
+           {
+             if (debug_infrun)
+               fprintf_unfiltered (gdb_stdlog,
+                                   "infrun: proceed: [%s] resumed\n",
+                                   target_pid_to_str (tp->ptid));
+             gdb_assert (tp->executing || tp->suspend.waitstatus_pending_p);
+             continue;
+           }
+
+         if (thread_is_in_step_over_chain (tp))
+           {
+             if (debug_infrun)
+               fprintf_unfiltered (gdb_stdlog,
+                                   "infrun: proceed: [%s] needs step-over\n",
+                                   target_pid_to_str (tp->ptid));
+             continue;
+           }
+
+         if (debug_infrun)
+           fprintf_unfiltered (gdb_stdlog,
+                               "infrun: proceed: resuming %s\n",
+                               target_pid_to_str (tp->ptid));
+
+         reset_ecs (ecs, tp);
+         switch_to_thread (tp->ptid);
+         keep_going_pass_signal (ecs);
+         if (!ecs->wait_some_more)
+           error ("Command aborted.");
+       }
+    }
   else if (!tp->resumed && !thread_is_in_step_over_chain (tp))
     {
       /* The thread wasn't started, and isn't queued, run it now.  */
@@ -3151,7 +3289,7 @@ for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func)
   if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
     return;
 
-  if (non_stop)
+  if (target_is_non_stop_p ())
     {
       /* If in non-stop mode, only the current thread stopped.  */
       func (inferior_thread ());
@@ -3632,7 +3770,7 @@ fetch_inferior_event (void *client_data)
   /* If an error happens while handling the event, propagate GDB's
      knowledge of the executing state to the frontend/user running
      state.  */
-  if (!non_stop)
+  if (!target_is_non_stop_p ())
     ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
   else
     ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
@@ -3871,7 +4009,8 @@ adjust_pc_after_break (struct thread_info *thread,
      to get the "stopped by SW BP and needs adjustment" info out of
      the target/kernel (and thus never reach here; see above).  */
   if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
-      || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
+      || (target_is_non_stop_p ()
+         && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
     {
       struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
 
@@ -4148,7 +4287,7 @@ stop_all_threads (void)
   ptid_t entry_ptid;
   struct cleanup *old_chain;
 
-  gdb_assert (non_stop);
+  gdb_assert (target_is_non_stop_p ());
 
   if (debug_infrun)
     fprintf_unfiltered (gdb_stdlog, "infrun: stop_all_threads\n");
@@ -4460,7 +4599,7 @@ handle_inferior_event_1 (struct execution_control_state *ecs)
   {
     ptid_t mark_ptid;
 
-    if (!non_stop)
+    if (!target_is_non_stop_p ())
       mark_ptid = minus_one_ptid;
     else if (ecs->ws.kind == TARGET_WAITKIND_SIGNALLED
             || ecs->ws.kind == TARGET_WAITKIND_EXITED)
@@ -4774,7 +4913,8 @@ Cannot fill $_exitsignal with the correct signal number.\n"));
          child = ecs->ws.value.related_pid;
 
          /* In non-stop mode, also resume the other branch.  */
-         if (non_stop && !detach_fork)
+         if (!detach_fork && (non_stop
+                              || (sched_multi && target_is_non_stop_p ())))
            {
              if (follow_child)
                switch_to_thread (parent);
@@ -5058,7 +5198,7 @@ finish_step_over (struct execution_control_state *ecs)
        clear_step_over_info ();
     }
 
-  if (!non_stop)
+  if (!target_is_non_stop_p ())
     return 0;
 
   /* Start a new step-over in another thread if there's one that
@@ -5638,15 +5778,17 @@ handle_signal_stop (struct execution_control_state *ecs)
          /* Reset trap_expected to ensure breakpoints are re-inserted.  */
          ecs->event_thread->control.trap_expected = 0;
 
-         if (non_stop)
+         if (target_is_non_stop_p ())
            {
+             /* Either "set non-stop" is "on", or the target is
+                always in non-stop mode.  In this case, we have a bit
+                more work to do.  Resume the current thread, and if
+                we had paused all threads, restart them while the
+                signal handler runs.  */
              keep_going (ecs);
 
-             /* The step-over has been canceled temporarily while the
-                signal handler executes.  */
              if (was_in_line)
                {
-                 /* We had paused all threads, restart them.  */
                  restart_threads (ecs->event_thread);
                }
              else if (debug_infrun)
@@ -6541,7 +6683,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 static int
 switch_back_to_stepped_thread (struct execution_control_state *ecs)
 {
-  if (!non_stop)
+  if (!target_is_non_stop_p ())
     {
       struct thread_info *tp;
       struct thread_info *stepping_thread;
@@ -6632,7 +6774,8 @@ switch_back_to_stepped_thread (struct execution_control_state *ecs)
 
       ALL_NON_EXITED_THREADS (tp)
         {
-         /* Ignore threads of processes we're not resuming.  */
+         /* Ignore threads of processes the caller is not
+            resuming.  */
          if (!sched_multi
              && ptid_get_pid (tp->ptid) != ptid_get_pid (ecs->ptid))
            continue;
@@ -6778,7 +6921,7 @@ keep_going_stepped_thread (struct thread_info *tp)
                                     stop_pc);
 
       tp->resumed = 1;
-      resume_ptid = user_visible_resume_ptid (tp->control.stepping_command);
+      resume_ptid = internal_resume_ptid (tp->control.stepping_command);
       do_target_resume (resume_ptid, 0, GDB_SIGNAL_0);
     }
   else
@@ -7199,6 +7342,11 @@ stop_waiting (struct execution_control_state *ecs)
 
   /* Let callers know we don't want to wait for the inferior anymore.  */
   ecs->wait_some_more = 0;
+
+  /* If all-stop, but the target is always in non-stop mode, stop all
+     threads now that we're presenting the stop to the user.  */
+  if (!non_stop && target_is_non_stop_p ())
+    stop_all_threads ();
 }
 
 /* Like keep_going, but passes the signal to the inferior, even if the
@@ -7299,8 +7447,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
         watchpoint.  The instruction copied to the scratch pad would
         still trigger the watchpoint.  */
       if (remove_bp
-         && (remove_wps
-             || !use_displaced_stepping (get_regcache_arch (regcache))))
+         && (remove_wps || !use_displaced_stepping (ecs->event_thread)))
        {
          set_step_over_info (get_regcache_aspace (regcache),
                              regcache_read_pc (regcache), remove_wps);
@@ -7313,7 +7460,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
         insert_breakpoints below, because that removes the breakpoint
         we're about to step over, otherwise other threads could miss
         it.  */
-      if (step_over_info_valid_p () && non_stop)
+      if (step_over_info_valid_p () && target_is_non_stop_p ())
        stop_all_threads ();
 
       /* Stop stepping if inserting breakpoints fails.  */
This page took 0.030704 seconds and 4 git commands to generate.