diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 596ddeb..8559d2b 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1028,7 +1028,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	linespec.o dictionary.o \
 	location.o infcall.o \
 	infcmd.o infrun.o \
-	expprint.o environ.o stack.o thread.o \
+	expprint.o environ.o stack.o thread.o thread-fsm.o \
 	exceptions.o \
 	extension.o \
 	filesystem.o \
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index dbf69da..ef04bde 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5694,6 +5694,9 @@ handle_jit_event (void)
   struct frame_info *frame;
   struct gdbarch *gdbarch;
 
+  if (debug_infrun)
+    fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
+
   /* Switch terminal for any messages produced by
      breakpoint_re_set.  */
   target_terminal_ours_for_output ();
@@ -5885,16 +5888,13 @@ bpstat_what (bpstat bs_head)
       retval.main_action = max (retval.main_action, this_action);
     }
 
-  /* These operations may affect the bs->breakpoint_at state so they are
-     delayed after MAIN_ACTION is decided above.  */
-
-  if (jit_event)
-    {
-      if (debug_infrun)
-	fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
+  return retval;
+}
 
-      handle_jit_event ();
-    }
+void
+bpstat_run_callbacks (bpstat bs_head)
+{
+  bpstat bs;
 
   for (bs = bs_head; bs != NULL; bs = bs->next)
     {
@@ -5904,6 +5904,9 @@ bpstat_what (bpstat bs_head)
 	continue;
       switch (b->type)
 	{
+	case bp_jit_event:
+	  handle_jit_event ();
+	  break;
 	case bp_gnu_ifunc_resolver:
 	  gnu_ifunc_resolver_stop (b);
 	  break;
@@ -5912,8 +5915,6 @@ bpstat_what (bpstat bs_head)
 	  break;
 	}
     }
-
-  return retval;
 }
 
 /* Nonzero if we should step constantly (e.g. watchpoints on machines
@@ -13205,12 +13206,6 @@ momentary_bkpt_print_it (bpstat bs)
 
       switch (b->type)
 	{
-	case bp_finish:
-	  ui_out_field_string
-	    (uiout, "reason",
-	     async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
-	  break;
-
 	case bp_until:
 	  ui_out_field_string
 	    (uiout, "reason",
diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h
index 877766a..896d3eb 100644
--- a/gdb/breakpoint.h
+++ b/gdb/breakpoint.h
@@ -1005,7 +1005,11 @@ struct bpstat_what
 
 /* Tell what to do about this bpstat.  */
 struct bpstat_what bpstat_what (bpstat);
-
+
+/* Run breakpoint event callbacks associated with the breakpoints that
+   triggered.  */
+extern void bpstat_run_callbacks (bpstat bs_head);
+
 /* Find the bpstat associated with a breakpoint.  NULL otherwise.  */
 bpstat bpstat_find_breakpoint (bpstat, struct breakpoint *);
 
diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index ce43a4a..174a10b 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -40,6 +40,18 @@ static struct gdb_exception safe_execute_command (struct ui_out *uiout,
    quiet (i.e., another interpreter is being run with
    interpreter-exec), print nothing.  */
 
+/* Observer for the normal_stop notification.  */
+
+static void
+cli_on_normal_stop (struct bpstats *bs, int print_frame)
+{
+  if (!interp_quiet_p (cli_interp))
+    {
+      if (print_frame)
+	print_stop_event (cli_uiout);
+    }
+}
+
 /* Observer for the signal_received notification.  */
 
 static void
@@ -109,6 +121,7 @@ static void *
 cli_interpreter_init (struct interp *self, int top_level)
 {
   /* If changing this, remember to update tui-interp.c as well.  */
+  observer_attach_normal_stop (cli_on_normal_stop);
   observer_attach_end_stepping_range (cli_on_end_stepping_range);
   observer_attach_signal_received (cli_on_signal_received);
   observer_attach_signal_exited (cli_on_signal_exited);
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 83b2616..0c99782 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -277,9 +277,10 @@ struct thread_info
      command.  */
   struct continuation *intermediate_continuations;
 
-  /* If stepping, nonzero means step count is > 1 so don't print frame
-     next time inferior stops if it stops due to stepping.  */
-  int step_multi;
+  /* Pointer to the state machine manager object that handles what is
+     left to do for the thread's execution command after the target
+     stops.  Several execution commands use it.  */
+  struct thread_fsm *thread_fsm;
 
   /* This is used to remember when a fork or vfork event was caught by
      a catchpoint, and thus the event is to be followed at the next
@@ -557,6 +558,10 @@ extern struct thread_info *thread_step_over_chain_next (struct thread_info *tp);
 
 extern int thread_is_in_step_over_chain (struct thread_info *tp);
 
+/* Cancel any ongoing execution command.  */
+
+extern void thread_cancel_execution_command (struct thread_info *thr);
+
 extern struct thread_info *thread_list;
 
 #endif /* GDBTHREAD_H */
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 3faff72..67b5df0 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -55,6 +55,7 @@
 #include "linespec.h"
 #include "cli/cli-utils.h"
 #include "infcall.h"
+#include "thread-fsm.h"
 
 /* Local functions: */
 
@@ -89,8 +90,6 @@ static void signal_command (char *, int);
 static void jump_command (char *, int);
 
 static void step_1 (int, int, char *);
-static void step_once (int skip_subroutines, int single_inst,
-		       int count, int thread);
 
 static void next_command (char *, int);
 
@@ -900,14 +899,88 @@ delete_longjmp_breakpoint_cleanup (void *arg)
   delete_longjmp_breakpoint (thread);
 }
 
+/* Data for the FSM that manages the step/next/stepi/nexti
+   commands.  */
+
+struct step_command_fsm
+{
+  /* The base class.  */
+  struct thread_fsm thread_fsm;
+
+  /* How many steps left in a "step N"-like command.  */
+  int count;
+
+  /* If true, this is a next/nexti, otherwise a step/stepi.  */
+  int skip_subroutines;
+
+  /* If true, this is a stepi/nexti, otherwise a step/step.  */
+  int single_inst;
+
+  /* The thread that the command was run on.  */
+  int thread;
+};
+
+static void step_command_fsm_clean_up (struct thread_fsm *self);
+static int step_command_fsm_should_stop (struct thread_fsm *self);
+static enum async_reply_reason
+  step_command_fsm_async_reply_reason (struct thread_fsm *self);
+
+/* step_command_fsm's vtable.  */
+
+static struct thread_fsm_ops step_command_fsm_ops =
+{
+  NULL,
+  step_command_fsm_clean_up,
+  step_command_fsm_should_stop,
+  NULL,	/* return_value */
+  step_command_fsm_async_reply_reason,
+};
+
+/* Allocate a new step_command_fsm.  */
+
+static struct step_command_fsm *
+new_step_command_fsm (void)
+{
+  struct step_command_fsm *sm;
+
+  sm = XCNEW (struct step_command_fsm);
+  thread_fsm_ctor (&sm->thread_fsm, &step_command_fsm_ops);
+
+  return sm;
+}
+
+/* Prepare for a step/next/etc. command.  Any target resource
+   allocated here is undone in the FSM's clean_up method.  */
+
+static void
+step_command_fsm_prepare (struct step_command_fsm *sm,
+			  int skip_subroutines, int single_inst,
+			  int count, int thread)
+{
+  struct thread_info *tp = inferior_thread ();
+
+  sm->skip_subroutines = skip_subroutines;
+  sm->single_inst = single_inst;
+  sm->count = count;
+  sm->thread = thread;
+
+  /* Leave the si command alone.  */
+  if (!sm->single_inst || sm->skip_subroutines)
+    set_longjmp_breakpoint (tp, get_frame_id (get_current_frame ()));
+
+  tp->control.stepping_command = 1;
+}
+
+static int prepare_one_step (struct step_command_fsm *sm);
+
 static void
 step_1 (int skip_subroutines, int single_inst, char *count_string)
 {
-  int count = 1;
-  struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
+  int count;
   int async_exec;
-  int thread = -1;
   struct cleanup *args_chain;
+  struct thread_info *thr;
+  struct step_command_fsm *step_sm;
 
   ERROR_NO_INFERIOR;
   ensure_not_tfind_mode ();
@@ -924,101 +997,105 @@ step_1 (int skip_subroutines, int single_inst, char *count_string)
   /* Done with ARGS.  */
   do_cleanups (args_chain);
 
-  if (!single_inst || skip_subroutines)		/* Leave si command alone.  */
-    {
-      struct thread_info *tp = inferior_thread ();
-
-      if (in_thread_list (inferior_ptid))
- 	thread = pid_to_thread_id (inferior_ptid);
+  clear_proceed_status (1);
 
-      set_longjmp_breakpoint (tp, get_frame_id (get_current_frame ()));
+  /* Setup the execution command state machine to handle all the COUNT
+     steps.  */
+  thr = inferior_thread ();
+  step_sm = new_step_command_fsm ();
+  thr->thread_fsm = &step_sm->thread_fsm;
 
-      make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
-    }
+  step_command_fsm_prepare (step_sm, skip_subroutines,
+			    single_inst, count, thr->num);
 
-  /* do only one step for now, before returning control to the event
+  /* Do only one step for now, before returning control to the event
      loop.  Let the continuation figure out how many other steps we
      need to do, and handle them one at the time, through
      step_once.  */
-  step_once (skip_subroutines, single_inst, count, thread);
-
-  /* We are running, and the continuation is installed.  It will
-     disable the longjmp breakpoint as appropriate.  */
-  discard_cleanups (cleanups);
+  if (!prepare_one_step (step_sm))
+    proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
+  else
+    {
+      /* Stepped into an inline frame.  Pretend that we've
+	 stopped.  */
+      thread_fsm_clean_up (thr->thread_fsm);
+      normal_stop ();
+      inferior_event_handler (INF_EXEC_COMPLETE, NULL);
+    }
 }
 
-struct step_1_continuation_args
-{
-  int count;
-  int skip_subroutines;
-  int single_inst;
-  int thread;
-};
-
-/* Called after we are done with one step operation, to check whether
-   we need to step again, before we print the prompt and return control
-   to the user.  If count is > 1, we will need to do one more call to
-   proceed(), via step_once().  Basically it is like step_once and
-   step_1_continuation are co-recursive.  */
+/* Implementation of the 'should_stop' FSM method for stepping
+   commands.  Called after we are done with one step operation, to
+   check whether we need to step again, before we print the prompt and
+   return control to the user.  If count is > 1, returns false, as we
+   will need to keep going.  */
 
-static void
-step_1_continuation (void *args, int err)
+static int
+step_command_fsm_should_stop (struct thread_fsm *self)
 {
-  struct step_1_continuation_args *a = args;
+  struct step_command_fsm *sm = (struct step_command_fsm *) self;
+  struct thread_info *tp;
 
-  if (target_has_execution)
+  tp = inferior_thread ();
+  if (tp->control.stop_step)
     {
-      struct thread_info *tp;
+      /* There are more steps to make, and we did stop due to
+	 ending a stepping range.  Do another step.  */
+      if (--sm->count > 0)
+	return prepare_one_step (sm);
 
-      tp = inferior_thread ();
-      if (!err
-	  && tp->step_multi && tp->control.stop_step)
-	{
-	  /* There are more steps to make, and we did stop due to
-	     ending a stepping range.  Do another step.  */
-	  step_once (a->skip_subroutines, a->single_inst,
-		     a->count - 1, a->thread);
-	  return;
-	}
-      tp->step_multi = 0;
+      thread_fsm_set_finished (self);
     }
 
-  /* We either hit an error, or stopped for some reason that is
-     not stepping, or there are no further steps to make.
-     Cleanup.  */
-  if (!a->single_inst || a->skip_subroutines)
-    delete_longjmp_breakpoint (a->thread);
+  return 1;
 }
 
-/* Do just one step operation.  This is useful to implement the 'step
-   n' kind of commands.  In case of asynchronous targets, we will have
-   to set up a continuation to be done after the target stops (after
-   this one step).  For synch targets, the caller handles further
-   stepping.  */
+/* Implementation of the 'clean_up' FSM method for stepping commands.  */
 
 static void
-step_once (int skip_subroutines, int single_inst, int count, int thread)
+step_command_fsm_clean_up (struct thread_fsm *self)
 {
-  struct frame_info *frame = get_current_frame ();
+  struct step_command_fsm *sm = (struct step_command_fsm *) self;
+
+  if (!sm->single_inst || sm->skip_subroutines)
+    delete_longjmp_breakpoint (sm->thread);
+}
 
-  if (count > 0)
+/* Implementation of the 'async_reply_reason' FSM method for stepping
+   commands.  */
+
+static enum async_reply_reason
+step_command_fsm_async_reply_reason (struct thread_fsm *self)
+{
+  return EXEC_ASYNC_END_STEPPING_RANGE;
+}
+
+/* Prepare for one step in "step N".  The actual target resumption is
+   done by the caller.  Return true if we're done and should thus
+   report a stop to the user.  Returns false if the target needs to be
+   resumed.  */
+
+static int
+prepare_one_step (struct step_command_fsm *sm)
+{
+  if (sm->count > 0)
     {
-      struct step_1_continuation_args *args;
+      struct frame_info *frame = get_current_frame ();
+
       /* Don't assume THREAD is a valid thread id.  It is set to -1 if
 	 the longjmp breakpoint was not required.  Use the
 	 INFERIOR_PTID thread instead, which is the same thread when
 	 THREAD is set.  */
       struct thread_info *tp = inferior_thread ();
 
-      clear_proceed_status (1);
       set_step_frame ();
 
-      if (!single_inst)
+      if (!sm->single_inst)
 	{
 	  CORE_ADDR pc;
 
 	  /* Step at an inlined function behaves like "down".  */
-	  if (!skip_subroutines
+	  if (!sm->skip_subroutines
 	      && inline_skipped_frames (inferior_ptid))
 	    {
 	      ptid_t resume_ptid;
@@ -1028,17 +1105,8 @@ step_once (int skip_subroutines, int single_inst, int count, int thread)
 	      set_running (resume_ptid, 1);
 
 	      step_into_inline_frame (inferior_ptid);
-	      if (count > 1)
-		step_once (skip_subroutines, single_inst, count - 1, thread);
-	      else
-		{
-		  /* Pretend that we've stopped.  */
-		  normal_stop ();
-
-		  if (target_can_async_p ())
-		    inferior_event_handler (INF_EXEC_COMPLETE, NULL);
-		}
-	      return;
+	      sm->count--;
+	      return prepare_one_step (sm);
 	    }
 
 	  pc = get_frame_pc (frame);
@@ -1073,29 +1141,22 @@ step_once (int skip_subroutines, int single_inst, int count, int thread)
 	{
 	  /* Say we are stepping, but stop after one insn whatever it does.  */
 	  tp->control.step_range_start = tp->control.step_range_end = 1;
-	  if (!skip_subroutines)
+	  if (!sm->skip_subroutines)
 	    /* It is stepi.
 	       Don't step over function calls, not even to functions lacking
 	       line numbers.  */
 	    tp->control.step_over_calls = STEP_OVER_NONE;
 	}
 
-      if (skip_subroutines)
+      if (sm->skip_subroutines)
 	tp->control.step_over_calls = STEP_OVER_ALL;
 
-      tp->step_multi = (count > 1);
-      tp->control.stepping_command = 1;
-      proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
-
-      /* Register a continuation to do any additional steps.  */
-      args = xmalloc (sizeof (*args));
-      args->skip_subroutines = skip_subroutines;
-      args->single_inst = single_inst;
-      args->count = count;
-      args->thread = thread;
-
-      add_intermediate_continuation (tp, step_1_continuation, args, xfree);
+      return 0;
     }
+
+  /* Done.  */
+  thread_fsm_set_finished (&sm->thread_fsm);
+  return 1;
 }
 
 
@@ -1399,8 +1460,6 @@ until_next_command (int from_tty)
 
   tp->control.step_over_calls = STEP_OVER_ALL;
 
-  tp->step_multi = 0;		/* Only one call to proceed */
-
   set_longjmp_breakpoint (tp, get_frame_id (frame));
   old_chain = make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
 
@@ -1528,18 +1587,29 @@ get_return_value (struct value *function, struct type *value_type,
   return value;
 }
 
-/* Print the result of a function at the end of a 'finish' command.
-   DTOR_DATA (if not NULL) can represent inferior registers right after
-   an inferior call has finished.  */
+/* The captured function return value/type and its position in the
+   value history.  */
 
-static void
-print_return_value (struct value *function, struct type *value_type,
-		    struct dummy_frame_context_saver *ctx_saver)
+struct return_value_info
 {
-  struct value *value = get_return_value (function, value_type, ctx_saver);
-  struct ui_out *uiout = current_uiout;
+  /* The captured return value.  May be NULL if we weren't able to
+     retrieve it.  See get_return_value.  */
+  struct value *value;
 
-  if (value)
+  /* The return type.  In some cases, we'll not be able extract the
+     return value, but we always know the type.  */
+  struct type *type;
+
+  /* If we captured a value, this is the value history index.  */
+  int value_history_index;
+};
+
+/* Helper for print_return_value.  */
+
+static void
+print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
+{
+  if (rv->value != NULL)
     {
       struct value_print_options opts;
       struct ui_file *stb;
@@ -1550,10 +1620,10 @@ print_return_value (struct value *function, struct type *value_type,
       old_chain = make_cleanup_ui_file_delete (stb);
       ui_out_text (uiout, "Value returned is ");
       ui_out_field_fmt (uiout, "gdb-result-var", "$%d",
-			record_latest_value (value));
+			rv->value_history_index);
       ui_out_text (uiout, " = ");
       get_no_prettyformat_print_options (&opts);
-      value_print (value, stb, &opts);
+      value_print (rv->value, stb, &opts);
       ui_out_field_stream (uiout, "return-value", stb);
       ui_out_text (uiout, "\n");
       do_cleanups (old_chain);
@@ -1563,7 +1633,7 @@ print_return_value (struct value *function, struct type *value_type,
       struct cleanup *oldchain;
       char *type_name;
 
-      type_name = type_to_string (value_type);
+      type_name = type_to_string (rv->type);
       oldchain = make_cleanup (xfree, type_name);
       ui_out_text (uiout, "Value returned has type: ");
       ui_out_field_string (uiout, "return-type", type_name);
@@ -1573,100 +1643,173 @@ print_return_value (struct value *function, struct type *value_type,
     }
 }
 
-/* Stuff that needs to be done by the finish command after the target
-   has stopped.  In asynchronous mode, we wait for the target to stop
-   in the call to poll or select in the event loop, so it is
-   impossible to do all the stuff as part of the finish_command
-   function itself.  The only chance we have to complete this command
-   is in fetch_inferior_event, which is called by the event loop as
-   soon as it detects that the target has stopped.  */
+/* Print the result of a function at the end of a 'finish' command.
+   RV points at an object representing the captured return value/type
+   and its position in the value history.  */
 
-struct finish_command_continuation_args
+void
+print_return_value (struct ui_out *uiout, struct return_value_info *rv)
 {
-  /* The thread that as current when the command was executed.  */
+  if (rv->type == NULL || TYPE_CODE (rv->type) == TYPE_CODE_VOID)
+    return;
+
+  TRY
+    {
+      /* print_return_value_1 can throw an exception in some
+	 circumstances.  We need to catch this so that we still
+	 delete the breakpoint.  */
+      print_return_value_1 (uiout, rv);
+    }
+  CATCH (ex, RETURN_MASK_ALL)
+    {
+      exception_print (gdb_stdout, ex);
+    }
+  END_CATCH
+}
+
+/* Data for the FSM that manages the finish command.  */
+
+struct finish_command_fsm
+{
+  /* The base class.  */
+  struct thread_fsm thread_fsm;
+
+  /* The thread that was current when the command was executed.  */
   int thread;
+
+  /* The momentary breakpoint set at the function's return address in
+     the caller.  */
   struct breakpoint *breakpoint;
+
+  /* The function that we're stepping out of.  */
   struct symbol *function;
 
-  /* Inferior registers stored right before dummy_frame has been freed
-     after an inferior call.  It can be NULL if no inferior call was
-     involved, GDB will then use current inferior registers.  */
-  struct dummy_frame_context_saver *ctx_saver;
+  /* If the FSM finishes successfully, this stores the function's
+     return value.  */
+  struct return_value_info return_value;
 };
 
-static void
-finish_command_continuation (void *arg, int err)
+static int finish_command_fsm_should_stop (struct thread_fsm *self);
+static void finish_command_fsm_clean_up (struct thread_fsm *self);
+static struct return_value_info *
+  finish_command_fsm_return_value (struct thread_fsm *self);
+static enum async_reply_reason
+  finish_command_fsm_async_reply_reason (struct thread_fsm *self);
+
+/* finish_command_fsm's vtable.  */
+
+static struct thread_fsm_ops finish_command_fsm_ops =
+{
+  NULL, /* dtor */
+  finish_command_fsm_clean_up,
+  finish_command_fsm_should_stop,
+  finish_command_fsm_return_value,
+  finish_command_fsm_async_reply_reason,
+};
+
+/* Allocate a new finish_command_fsm.  */
+
+static struct finish_command_fsm *
+new_finish_command_fsm (int thread)
+{
+  struct finish_command_fsm *sm;
+
+  sm = XCNEW (struct finish_command_fsm);
+  thread_fsm_ctor (&sm->thread_fsm, &finish_command_fsm_ops);
+
+  sm->thread = thread;
+
+  return sm;
+}
+
+/* Implementation of the 'should_stop' FSM method for the finish
+   commands.  Detects whether the thread stepped out of the function
+   successfully, and if so, captures the function's return value and
+   marks the FSM finished.  */
+
+static int
+finish_command_fsm_should_stop (struct thread_fsm *self)
 {
-  struct finish_command_continuation_args *a = arg;
+  struct finish_command_fsm *f = (struct finish_command_fsm *) self;
+  struct return_value_info *rv = &f->return_value;
+  struct thread_info *tp = inferior_thread ();
 
-  if (!err)
+  if (f->function != NULL
+      && bpstat_find_breakpoint (tp->control.stop_bpstat,
+				 f->breakpoint) != NULL)
     {
-      struct thread_info *tp = NULL;
-      bpstat bs = NULL;
+      /* We're done.  */
+      thread_fsm_set_finished (self);
 
-      if (!ptid_equal (inferior_ptid, null_ptid)
-	  && target_has_execution
-	  && is_stopped (inferior_ptid))
-	{
-	  tp = inferior_thread ();
-	  bs = tp->control.stop_bpstat;
-	}
+      rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (f->function));
+      if (rv->type == NULL)
+	internal_error (__FILE__, __LINE__,
+			_("finish_command: function has no target type"));
 
-      if (bpstat_find_breakpoint (bs, a->breakpoint) != NULL
-	  && a->function != NULL)
+      if (TYPE_CODE (rv->type) != TYPE_CODE_VOID)
 	{
-	  struct type *value_type;
+	  struct value *func;
 
-	  value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (a->function));
-	  if (!value_type)
-	    internal_error (__FILE__, __LINE__,
-			    _("finish_command: function has no target type"));
+	  func = read_var_value (f->function, get_current_frame ());
+	  rv->value = get_return_value (func, rv->type, NULL);
+	  rv->value_history_index = record_latest_value (rv->value);
+	}
+    }
+  else if (tp->control.stop_step)
+    {
+      /* Finishing from an inline frame, or reverse finishing.  In
+	 either case, there's no way to retrieve the return value.  */
+      thread_fsm_set_finished (self);
+    }
 
-	  if (TYPE_CODE (value_type) != TYPE_CODE_VOID)
-	    {
-	      struct value *func;
+  return 1;
+}
 
-	      func = read_var_value (a->function, get_current_frame ());
-	      TRY
-		{
-		  /* print_return_value can throw an exception in some
-		     circumstances.  We need to catch this so that we still
-		     delete the breakpoint.  */
-		  print_return_value (func, value_type, a->ctx_saver);
-		}
-	      CATCH (ex, RETURN_MASK_ALL)
-		{
-		  exception_print (gdb_stdout, ex);
-		}
-	      END_CATCH
-	    }
-	}
+/* Implementation of the 'clean_up' FSM method for the finish
+   commands.  */
 
-      /* We suppress normal call of normal_stop observer and do it
-	 here so that the *stopped notification includes the return
-	 value.  */
-      if (bs != NULL && tp->control.proceed_to_finish)
-	observer_notify_normal_stop (bs, 1 /* print frame */);
+static void
+finish_command_fsm_clean_up (struct thread_fsm *self)
+{
+  struct finish_command_fsm *f = (struct finish_command_fsm *) self;
+
+  if (f->breakpoint != NULL)
+    {
+      delete_breakpoint (f->breakpoint);
+      f->breakpoint = NULL;
     }
+  delete_longjmp_breakpoint (f->thread);
+}
 
-  delete_breakpoint (a->breakpoint);
-  delete_longjmp_breakpoint (a->thread);
+/* Implementation of the 'return_value' FSM method for the finish
+   commands.  */
+
+static struct return_value_info *
+finish_command_fsm_return_value (struct thread_fsm *self)
+{
+  struct finish_command_fsm *f = (struct finish_command_fsm *) self;
+
+  return &f->return_value;
 }
 
-static void
-finish_command_continuation_free_arg (void *arg)
+/* Implementation of the 'async_reply_reason' FSM method for the
+   finish commands.  */
+
+static enum async_reply_reason
+finish_command_fsm_async_reply_reason (struct thread_fsm *self)
 {
-  struct finish_command_continuation_args *cargs = arg;
+  struct finish_command_fsm *f = (struct finish_command_fsm *) self;
 
-  if (cargs->ctx_saver != NULL)
-    dummy_frame_context_saver_drop (cargs->ctx_saver);
-  xfree (cargs);
+  if (execution_direction == EXEC_REVERSE)
+    return EXEC_ASYNC_END_STEPPING_RANGE;
+  else
+    return EXEC_ASYNC_FUNCTION_FINISHED;
 }
 
 /* finish_backward -- helper function for finish_command.  */
 
 static void
-finish_backward (struct symbol *function)
+finish_backward (struct finish_command_fsm *sm)
 {
   struct symtab_and_line sal;
   struct thread_info *tp = inferior_thread ();
@@ -1715,56 +1858,33 @@ finish_backward (struct symbol *function)
     }
 }
 
-/* finish_forward -- helper function for finish_command.  */
+/* finish_forward -- helper function for finish_command.  FRAME is the
+   frame that called the function we're about to step out of.  */
 
 static void
-finish_forward (struct symbol *function, struct frame_info *frame)
+finish_forward (struct finish_command_fsm *sm, struct frame_info *frame)
 {
   struct frame_id frame_id = get_frame_id (frame);
   struct gdbarch *gdbarch = get_frame_arch (frame);
   struct symtab_and_line sal;
   struct thread_info *tp = inferior_thread ();
-  struct breakpoint *breakpoint;
-  struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
-  struct finish_command_continuation_args *cargs;
-  int thread = tp->num;
-  struct dummy_frame_context_saver *saver = NULL;
 
   sal = find_pc_line (get_frame_pc (frame), 0);
   sal.pc = get_frame_pc (frame);
 
-  if (get_frame_type (frame) == DUMMY_FRAME)
-    {
-      saver = dummy_frame_context_saver_setup (get_stack_frame_id (frame),
-					       inferior_ptid);
-      make_cleanup (dummy_frame_context_saver_cleanup, saver);
-    }
-
-  breakpoint = set_momentary_breakpoint (gdbarch, sal,
-					 get_stack_frame_id (frame),
-                                         bp_finish);
+  sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
+					     get_stack_frame_id (frame),
+					     bp_finish);
 
   /* set_momentary_breakpoint invalidates FRAME.  */
   frame = NULL;
 
-  make_cleanup_delete_breakpoint (breakpoint);
-
   set_longjmp_breakpoint (tp, frame_id);
-  make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
 
   /* We want to print return value, please...  */
   tp->control.proceed_to_finish = 1;
-  cargs = xmalloc (sizeof (*cargs));
-
-  cargs->thread = thread;
-  cargs->breakpoint = breakpoint;
-  cargs->function = function;
-  cargs->ctx_saver = saver;
-  add_continuation (tp, finish_command_continuation, cargs,
-                    finish_command_continuation_free_arg);
-  proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
 
-  discard_cleanups (old_chain);
+  proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
 }
 
 /* "finish": Set a temporary breakpoint at the place the selected
@@ -1774,9 +1894,10 @@ static void
 finish_command (char *arg, int from_tty)
 {
   struct frame_info *frame;
-  struct symbol *function;
   int async_exec;
   struct cleanup *args_chain;
+  struct finish_command_fsm *sm;
+  struct thread_info *tp;
 
   ERROR_NO_INFERIOR;
   ensure_not_tfind_mode ();
@@ -1801,9 +1922,14 @@ finish_command (char *arg, int from_tty)
 
   clear_proceed_status (0);
 
+  tp = inferior_thread ();
+
+  sm = new_finish_command_fsm (tp->num);
+
+  tp->thread_fsm = &sm->thread_fsm;
+
   /* Finishing from an inline frame is completely different.  We don't
-     try to show the "return value" - no way to locate it.  So we do
-     not need a completion.  */
+     try to show the "return value" - no way to locate it.  */
   if (get_frame_type (get_selected_frame (_("No selected frame.")))
       == INLINE_FRAME)
     {
@@ -1812,7 +1938,6 @@ finish_command (char *arg, int from_tty)
 	 called by that frame.  We don't use the magic "1" value for
 	 step_range_end, because then infrun will think this is nexti,
 	 and not step over the rest of this inlined function call.  */
-      struct thread_info *tp = inferior_thread ();
       struct symtab_and_line empty_sal;
 
       init_sal (&empty_sal);
@@ -1840,7 +1965,7 @@ finish_command (char *arg, int from_tty)
 
   /* Find the function we will return from.  */
 
-  function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
+  sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
 
   /* Print info on the selected frame, including level number but not
      source.  */
@@ -1850,10 +1975,10 @@ finish_command (char *arg, int from_tty)
 	printf_filtered (_("Run back to call of "));
       else
 	{
-	  if (function != NULL && TYPE_NO_RETURN (function->type)
+	  if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type)
 	      && !query (_("warning: Function %s does not return normally.\n"
 			   "Try to finish anyway? "),
-			 SYMBOL_PRINT_NAME (function)))
+			 SYMBOL_PRINT_NAME (sm->function)))
 	    error (_("Not confirmed."));
 	  printf_filtered (_("Run till exit from "));
 	}
@@ -1862,9 +1987,9 @@ finish_command (char *arg, int from_tty)
     }
 
   if (execution_direction == EXEC_REVERSE)
-    finish_backward (function);
+    finish_backward (sm);
   else
-    finish_forward (function, frame);
+    finish_forward (sm, frame);
 }
 
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 195ee81..06526f0 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -62,6 +62,7 @@
 #include "terminal.h"
 #include "solist.h"
 #include "event-loop.h"
+#include "thread-fsm.h"
 
 /* Prototypes for local functions */
 
@@ -2745,6 +2746,9 @@ clear_proceed_status_thread (struct thread_info *tp)
   if (!signal_pass_state (tp->suspend.stop_signal))
     tp->suspend.stop_signal = GDB_SIGNAL_0;
 
+  thread_fsm_delete (tp->thread_fsm);
+  tp->thread_fsm = NULL;
+
   tp->control.trap_expected = 0;
   tp->control.step_range_start = 0;
   tp->control.step_range_end = 0;
@@ -3216,7 +3220,7 @@ infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
 	 have consistent output as if the stop event had been
 	 reported.  */
       ecs->ptid = info->ptid;
-      ecs->event_thread = find_thread_ptid (info->ptid);
+      ecs->event_thread = info;
       ecs->ws.kind = TARGET_WAITKIND_STOPPED;
       ecs->ws.value.sig = GDB_SIGNAL_0;
 
@@ -3226,6 +3230,9 @@ infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
 	{
 	  struct thread_info *tp;
 
+	  /* Cancel any running execution command.  */
+	  thread_cancel_execution_command (info);
+
 	  normal_stop ();
 
 	  /* Finish off the continuations.  */
@@ -3714,6 +3721,35 @@ reinstall_readline_callback_handler_cleanup (void *arg)
     gdb_rl_callback_handler_reinstall ();
 }
 
+/* Clean up the FSMs of threads that are now stopped.  In non-stop,
+   that's just the event thread.  In all-stop, that's all threads.  */
+
+static void
+clean_up_just_stopped_threads_fsms (struct execution_control_state *ecs)
+{
+  struct thread_info *thr = ecs->event_thread;
+
+  if (thr != NULL && thr->thread_fsm != NULL)
+    thread_fsm_clean_up (thr->thread_fsm);
+
+  if (!non_stop)
+    {
+      ALL_NON_EXITED_THREADS (thr)
+        {
+	  if (thr->thread_fsm == NULL)
+	    continue;
+	  if (thr == ecs->event_thread)
+	    continue;
+
+	  switch_to_thread (thr->ptid);
+	  thread_fsm_clean_up (thr->thread_fsm);
+	}
+
+      if (ecs->event_thread != NULL)
+	switch_to_thread (ecs->event_thread->ptid);
+    }
+}
+
 /* Asynchronous version of wait_for_inferior.  It is called by the
    event loop whenever a change of state is detected on the file
    descriptor corresponding to the target.  It can be called more than
@@ -3792,22 +3828,31 @@ fetch_inferior_event (void *client_data)
   if (!ecs->wait_some_more)
     {
       struct inferior *inf = find_inferior_ptid (ecs->ptid);
+      int should_stop = 1;
+      struct thread_info *thr = ecs->event_thread;
 
       delete_just_stopped_threads_infrun_breakpoints ();
 
-      /* We may not find an inferior if this was a process exit.  */
-      if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
-	normal_stop ();
-
-      if (target_has_execution
-	  && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED
-	  && ecs->ws.kind != TARGET_WAITKIND_EXITED
-	  && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
-	  && ecs->event_thread->step_multi
-	  && ecs->event_thread->control.stop_step)
-	inferior_event_handler (INF_EXEC_CONTINUE, NULL);
+      if (thr != NULL)
+	{
+	  struct thread_fsm *thread_fsm = thr->thread_fsm;
+
+	  if (thread_fsm != NULL)
+	    should_stop = thread_fsm_should_stop (thread_fsm);
+	}
+
+      if (!should_stop)
+	{
+	  keep_going (ecs);
+	}
       else
 	{
+	  clean_up_just_stopped_threads_fsms (ecs);
+
+	  /* We may not find an inferior if this was a process exit.  */
+	  if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
+	    normal_stop ();
+
 	  inferior_event_handler (INF_EXEC_COMPLETE, NULL);
 	  cmd_done = 1;
 	}
@@ -5889,6 +5934,10 @@ process_event_stop_test (struct execution_control_state *ecs)
       stop_stack_dummy = what.call_dummy;
     }
 
+  /* A few breakpoint types have callbacks associated (e.g.,
+     bp_jit_event).  Run them now.  */
+  bpstat_run_callbacks (ecs->event_thread->control.stop_bpstat);
+
   /* If we hit an internal event that triggers symbol changes, the
      current frame will be invalidated within bpstat_what (e.g., if we
      hit an internal solib event).  Re-fetch it.  */
@@ -7655,8 +7704,8 @@ print_no_history_reason (struct ui_out *uiout)
    bpstat_print contains the logic deciding in detail what to print,
    based on the event(s) that just occurred.  */
 
-void
-print_stop_event (struct target_waitstatus *ws)
+static void
+print_stop_location (struct target_waitstatus *ws)
 {
   int bpstat_ret;
   enum print_what source_flag;
@@ -7707,9 +7756,50 @@ print_stop_event (struct target_waitstatus *ws)
      SRC_AND_LOC: Print location and source line.  */
   if (do_frame_printing)
     print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
+}
+
+/* Cleanup that restores a previous current uiout.  */
+
+static void
+restore_current_uiout_cleanup (void *arg)
+{
+  struct ui_out *saved_uiout = arg;
+
+  current_uiout = saved_uiout;
+}
+
+/* See infrun.h.  */
+
+void
+print_stop_event (struct ui_out *uiout)
+{
+  struct cleanup *old_chain;
+  struct target_waitstatus last;
+  ptid_t last_ptid;
+  struct thread_info *tp;
+
+  get_last_target_status (&last_ptid, &last);
+
+  old_chain = make_cleanup (restore_current_uiout_cleanup, current_uiout);
+  current_uiout = uiout;
+
+  print_stop_location (&last);
 
   /* Display the auto-display expressions.  */
   do_displays ();
+
+  do_cleanups (old_chain);
+
+  tp = inferior_thread ();
+  if (tp->thread_fsm != NULL
+      && thread_fsm_finished_p (tp->thread_fsm))
+    {
+      struct return_value_info *rv;
+
+      rv = thread_fsm_return_value (tp->thread_fsm);
+      if (rv != NULL)
+	print_return_value (uiout, rv);
+    }
 }
 
 /* Here to return control to GDB when the inferior stops for real.
@@ -7822,20 +7912,6 @@ normal_stop (void)
   if (stopped_by_random_signal)
     disable_current_display ();
 
-  /* Notify observers if we finished a "step"-like command, etc.  */
-  if (target_has_execution
-      && last.kind != TARGET_WAITKIND_SIGNALLED
-      && last.kind != TARGET_WAITKIND_EXITED
-      && inferior_thread ()->control.stop_step)
-    {
-      /* But not if in the middle of doing a "step n" operation for
-	 n > 1 */
-      if (inferior_thread ()->step_multi)
-	goto done;
-
-      observer_notify_end_stepping_range ();
-    }
-
   target_terminal_ours ();
   async_enable_stdin ();
 
@@ -7877,15 +7953,7 @@ normal_stop (void)
      or if the program has exited.  */
 
   if (!stop_stack_dummy)
-    {
-      select_frame (get_current_frame ());
-
-      /* If --batch-silent is enabled then there's no need to print the current
-	 source location, and to try risks causing an error message about
-	 missing source files.  */
-      if (stop_print_frame && !batch_silent)
-	print_stop_event (&last);
-    }
+    select_frame (get_current_frame ());
 
   if (stop_stack_dummy == STOP_STACK_DUMMY)
     {
@@ -7909,16 +7977,9 @@ normal_stop (void)
     }
 
 done:
-  annotate_stopped ();
 
   /* Suppress the stop observer if we're in the middle of:
 
-     - a step n (n > 1), as there still more steps to be done.
-
-     - a "finish" command, as the observer will be called in
-       finish_command_continuation, so it can include the inferior
-       function's return value.
-
      - calling an inferior function, as we pretend we inferior didn't
        run at all.  The return value of the call is handled by the
        expression evaluator, through call_function_by_hand.  */
@@ -7927,11 +7988,7 @@ done:
       || last.kind == TARGET_WAITKIND_SIGNALLED
       || last.kind == TARGET_WAITKIND_EXITED
       || last.kind == TARGET_WAITKIND_NO_RESUMED
-      || (!(inferior_thread ()->step_multi
-	    && inferior_thread ()->control.stop_step)
-	  && !(inferior_thread ()->control.stop_bpstat
-	       && inferior_thread ()->control.proceed_to_finish)
-	  && !inferior_thread ()->control.in_infcall))
+      || !inferior_thread ()->control.in_infcall)
     {
       if (!ptid_equal (inferior_ptid, null_ptid))
 	observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
@@ -7940,6 +7997,8 @@ done:
 	observer_notify_normal_stop (NULL, stop_print_frame);
     }
 
+  annotate_stopped ();
+
   if (target_has_execution)
     {
       if (last.kind != TARGET_WAITKIND_SIGNALLED
diff --git a/gdb/infrun.h b/gdb/infrun.h
index 1af64a6..34a8d4a 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -23,6 +23,7 @@
 struct target_waitstatus;
 struct frame_info;
 struct address_space;
+struct return_value_info;
 
 /* True if we are debugging run control.  */
 extern unsigned int debug_infrun;
@@ -148,7 +149,18 @@ extern void print_exited_reason (struct ui_out *uiout, int exitstatus);
    inferior has stopped.  */
 extern void print_no_history_reason (struct ui_out *uiout);
 
-extern void print_stop_event (struct target_waitstatus *ws);
+/* Print the result of a function at the end of a 'finish' command.
+   RV points at an object representing the captured return value/type
+   and its position in the value history.  */
+
+extern void print_return_value (struct ui_out *uiout,
+				struct return_value_info *rv);
+
+/* Print current location without a level number, if we have changed
+   functions or hit a breakpoint.  Print source line if we have one.
+   If the execution command captured a return value, print it.  */
+
+extern void print_stop_event (struct ui_out *uiout);
 
 extern int signal_stop_state (int);
 
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index bdd8e21..4e1cf31 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -37,6 +37,7 @@
 #include "objfiles.h"
 #include "tracepoint.h"
 #include "cli-out.h"
+#include "thread-fsm.h"
 
 /* These are the interpreter setup, etc. functions for the MI
    interpreter.  */
@@ -454,16 +455,6 @@ mi_inferior_removed (struct inferior *inf)
   gdb_flush (mi->event_channel);
 }
 
-/* Cleanup that restores a previous current uiout.  */
-
-static void
-restore_current_uiout_cleanup (void *arg)
-{
-  struct ui_out *saved_uiout = arg;
-
-  current_uiout = saved_uiout;
-}
-
 /* Return the MI interpreter, if it is active -- either because it's
    the top-level interpreter or the interpreter executing the current
    command.  Returns NULL if the MI interpreter is not being used.  */
@@ -581,73 +572,48 @@ mi_on_normal_stop (struct bpstats *bs, int print_frame)
 
   if (print_frame)
     {
+      struct thread_info *tp;
       int core;
 
-      if (current_uiout != mi_uiout)
-	{
-	  /* The normal_stop function has printed frame information
-	     into CLI uiout, or some other non-MI uiout.  There's no
-	     way we can extract proper fields from random uiout
-	     object, so we print the frame again.  In practice, this
-	     can only happen when running a CLI command in MI.  */
-	  struct ui_out *saved_uiout = current_uiout;
-	  struct target_waitstatus last;
-	  ptid_t last_ptid;
-
-	  current_uiout = mi_uiout;
+      tp = inferior_thread ();
 
-	  get_last_target_status (&last_ptid, &last);
-	  print_stop_event (&last);
+      if (tp->thread_fsm != NULL
+	  && thread_fsm_finished_p (tp->thread_fsm))
+	{
+	  enum async_reply_reason reason;
 
-	  current_uiout = saved_uiout;
+	  reason = thread_fsm_async_reply_reason (tp->thread_fsm);
+	  ui_out_field_string (mi_uiout, "reason",
+			       async_reason_lookup (reason));
 	}
-      /* Otherwise, frame information has already been printed by
-	 normal_stop.  */
-      else
+      print_stop_event (mi_uiout);
+
+      /* Breakpoint hits should always be mirrored to the console.
+	 Deciding what to mirror to the console wrt to breakpoints and
+	 random stops gets messy real fast.  E.g., say "s" trips on a
+	 breakpoint.  We'd clearly want to mirror the event to the
+	 console in this case.  But what about more complicated cases
+	 like "s&; thread n; s&", and one of those steps spawning a
+	 new thread, and that thread hitting a breakpoint?  It's
+	 impossible in general to track whether the thread had any
+	 relation to the commands that had been executed.  So we just
+	 simplify and always mirror breakpoints and random events to
+	 the console.
+
+	 OTOH, we should print the source line to the console when
+	 stepping or other similar commands, iff the step was started
+	 by a console command, but not if it was started with
+	 -exec-step or similar.  */
+      if ((bpstat_what (tp->control.stop_bpstat).main_action
+	   == BPSTAT_WHAT_STOP_NOISY)
+	  || !(tp->thread_fsm != NULL
+	       && thread_fsm_finished_p (tp->thread_fsm))
+	  || (tp->control.command_interp != NULL
+	      && tp->control.command_interp != top_level_interpreter ()))
 	{
-	  /* Breakpoint hits should always be mirrored to the console.
-	     Deciding what to mirror to the console wrt to breakpoints
-	     and random stops gets messy real fast.  E.g., say "s"
-	     trips on a breakpoint.  We'd clearly want to mirror the
-	     event to the console in this case.  But what about more
-	     complicated cases like "s&; thread n; s&", and one of
-	     those steps spawning a new thread, and that thread
-	     hitting a breakpoint?  It's impossible in general to
-	     track whether the thread had any relation to the commands
-	     that had been executed.  So we just simplify and always
-	     mirror breakpoints and random events to the console.
-
-	     Also, CLI execution commands (-interpreter-exec console
-	     "next", for example) in async mode have the opposite
-	     issue as described in the "then" branch above --
-	     normal_stop has already printed frame information to MI
-	     uiout, but nothing has printed the same information to
-	     the CLI channel.  We should print the source line to the
-	     console when stepping or other similar commands, iff the
-	     step was started by a console command (but not if it was
-	     started with -exec-step or similar).  */
-	  struct thread_info *tp = inferior_thread ();
-
-	  if ((!tp->control.stop_step
-		  && !tp->control.proceed_to_finish)
-	      || (tp->control.command_interp != NULL
-		  && tp->control.command_interp != top_level_interpreter ()))
-	    {
-	      struct mi_interp *mi = top_level_interpreter_data ();
-	      struct target_waitstatus last;
-	      ptid_t last_ptid;
-	      struct cleanup *old_chain;
-
-	      /* Set the current uiout to CLI uiout temporarily.  */
-	      old_chain = make_cleanup (restore_current_uiout_cleanup,
-					current_uiout);
-	      current_uiout = mi->cli_uiout;
-
-	      get_last_target_status (&last_ptid, &last);
-	      print_stop_event (&last);
-
-	      do_cleanups (old_chain);
-	    }
+	  struct mi_interp *mi = top_level_interpreter_data ();
+
+	  print_stop_event (mi->cli_uiout);
 	}
 
       ui_out_field_int (mi_uiout, "thread-id",
diff --git a/gdb/testsuite/gdb.mi/mi-cli.exp b/gdb/testsuite/gdb.mi/mi-cli.exp
index 08c8f02..5f75bef 100644
--- a/gdb/testsuite/gdb.mi/mi-cli.exp
+++ b/gdb/testsuite/gdb.mi/mi-cli.exp
@@ -156,6 +156,24 @@ if {[regexp "A + B" "$output"]} {
 mi_expect_stop "end-stepping-range" "callee4" "" ".*basics.c" $line_callee4_next_step \
     "" "check *stopped from CLI command 2"
 
+# Test that CLI's "finish" command prints the function's return value
+# to both the CLI and MI streams, and that the same result variable is
+# printed to both streams.
+with_test_prefix "CLI finish" {
+    mi_send_resuming_command "interpreter-exec console finish" "send CLI command"
+
+    set output [mi_gdb_expect_cli_output "\\*stopped" "collect CLI output"]
+    gdb_assert {[regexp \
+		     "callee3 .* at .*basics.c:.*.*Value returned is .1 = 0\\\\n" \
+		     $output]} \
+	"check CLI output"
+
+    mi_expect_stop "function-finished" "callee3" ".*" \
+	".*basics.c" ".*" \
+	",gdb-result-var=\".1\",return-value=\"0\"" \
+	"check MI output"
+}
+
 mi_gdb_test "600-break-insert -t basics.c:$line_main_hello" \
 	{600\^done,bkpt=.number="3",type="breakpoint".*\}} \
 	"-break-insert -t basics.c:\$line_main_hello"
diff --git a/gdb/thread-fsm.c b/gdb/thread-fsm.c
new file mode 100644
index 0000000..761ad1c
--- /dev/null
+++ b/gdb/thread-fsm.c
@@ -0,0 +1,97 @@
+/* Thread command's finish-state machine, for GDB, the GNU debugger.
+   Copyright (C) 2015 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 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   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, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "thread-fsm.h"
+
+/* See thread-fsm.h.  */
+
+void
+thread_fsm_ctor (struct thread_fsm *self, struct thread_fsm_ops *ops)
+{
+  self->finished = 0;
+  self->ops = ops;
+}
+
+/* See thread-fsm.h.  */
+
+void
+thread_fsm_delete (struct thread_fsm *self)
+{
+  if (self != NULL)
+    {
+      if (self->ops->dtor != NULL)
+	self->ops->dtor (self);
+      xfree (self);
+    }
+}
+
+/* See thread-fsm.h.  */
+
+void
+thread_fsm_clean_up (struct thread_fsm *self)
+{
+  if (self->ops->clean_up != NULL)
+    self->ops->clean_up (self);
+}
+
+/* See thread-fsm.h.  */
+
+int
+thread_fsm_should_stop (struct thread_fsm *self)
+{
+  return self->ops->should_stop (self);
+}
+
+/* See thread-fsm.h.  */
+
+struct return_value_info *
+thread_fsm_return_value (struct thread_fsm *self)
+{
+  if (self->ops->return_value != NULL)
+    return self->ops->return_value (self);
+  return NULL;
+}
+
+/* See thread-fsm.h.  */
+
+void
+thread_fsm_set_finished (struct thread_fsm *self)
+{
+  self->finished = 1;
+}
+
+/* See thread-fsm.h.  */
+
+int
+thread_fsm_finished_p (struct thread_fsm *self)
+{
+  return self->finished;
+}
+
+/* See thread-fsm.h.  */
+
+enum async_reply_reason
+thread_fsm_async_reply_reason (struct thread_fsm *self)
+{
+  /* If we didn't finish, then the stop reason must come from
+     elsewhere.  E.g., a breakpoint hit or a signal intercepted.  */
+  gdb_assert (thread_fsm_finished_p (self));
+
+  return self->ops->async_reply_reason (self);
+}
diff --git a/gdb/thread-fsm.h b/gdb/thread-fsm.h
new file mode 100644
index 0000000..031684b
--- /dev/null
+++ b/gdb/thread-fsm.h
@@ -0,0 +1,98 @@
+/* Thread command's finish-state machine, for GDB, the GNU debugger.
+   Copyright (C) 2015 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 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   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, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef THREAD_FSM_H
+#define THREAD_FSM_H
+
+#include "mi/mi-common.h" /* For enum async_reply_reason.  */
+
+struct return_value_info;
+struct thread_fsm_ops;
+
+/* A thread finite-state machine structure contains the necessary info
+   and callbacks to manage the state machine protocol of a thread's
+   execution command.  */
+
+struct thread_fsm
+{
+  /* Pointer of the virtual table of methods.  */
+  struct thread_fsm_ops *ops;
+
+  /* Whether the FSM is done successfully.  */
+  int finished;
+};
+
+/* The virtual table of a thread_fsm.  */
+
+struct thread_fsm_ops
+{
+  /* The destructor.  This should simply free heap allocated data
+     structures.  Cleaning up target resources (like, e.g.,
+     breakpoints) should be done in the clean_up method.  */
+  void (*dtor) (struct thread_fsm *self);
+
+  /* Called to clean up target resources after the FSM.  E.g., if the
+     FSM created internal breakpoints, this is where they should be
+     deleted.  */
+  void (*clean_up) (struct thread_fsm *self);
+
+  /* Called after handle_inferior_event decides the target is done
+     (that is, after stop_waiting).  The FSM is given a chance to
+     decide whether the command is done and thus the target should
+     stop, or whether there's still more to do and thus the thread
+     should be re-resumed.  This is a good place to cache target data
+     too.  For example, the "finish" command saves the just-finished
+     function's return value here.  */
+  int (*should_stop) (struct thread_fsm *self);
+
+  /* If this FSM saved a function's return value, you can use this
+     method to retrieve it.  Otherwise, this returns NULL.  */
+  struct return_value_info *(*return_value) (struct thread_fsm *self);
+
+  /* The async_reply_reason that is broadcast to MI clients if this
+     FSM finishes successfully.  */
+  enum async_reply_reason (*async_reply_reason) (struct thread_fsm *self);
+};
+/* Initialize FSM.  */
+extern void thread_fsm_ctor (struct thread_fsm *fsm,
+			     struct thread_fsm_ops *ops);
+
+/* Calls the FSM's dtor method, and then frees FSM.  */
+extern void thread_fsm_delete (struct thread_fsm *fsm);
+
+/* Calls the FSM's clean_up method.  */
+extern void thread_fsm_clean_up (struct thread_fsm *fsm);
+
+/* Calls the FSM's should_stop method.  */
+extern int thread_fsm_should_stop (struct thread_fsm *fsm);
+
+/* Calls the FSM's return_value method.  */
+extern struct return_value_info *
+  thread_fsm_return_value (struct thread_fsm *fsm);
+
+/* Marks the FSM as completed successfully.  */
+extern void thread_fsm_set_finished (struct thread_fsm *fsm);
+
+/* Returns true if the FSM completed successfully.  */
+extern int thread_fsm_finished_p (struct thread_fsm *fsm);
+
+/* Calls the FSM's reply_reason method.  */
+extern enum async_reply_reason
+  thread_fsm_async_reply_reason (struct thread_fsm *fsm);
+
+#endif /* THREAD_FSM_H */
diff --git a/gdb/thread.c b/gdb/thread.c
index 4dde722..87a5950 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -43,6 +43,7 @@
 #include "gdb_regex.h"
 #include "cli/cli-utils.h"
 #include "continuations.h"
+#include "thread-fsm.h"
 
 /* Definition of struct thread_info exported to gdbthread.h.  */
 
@@ -158,6 +159,19 @@ thread_has_single_step_breakpoint_here (struct thread_info *tp,
 	  && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
 }
 
+/* See gdbthread.h.  */
+
+void
+thread_cancel_execution_command (struct thread_info *thr)
+{
+  if (thr->thread_fsm != NULL)
+    {
+      thread_fsm_clean_up (thr->thread_fsm);
+      thread_fsm_delete (thr->thread_fsm);
+      thr->thread_fsm = NULL;
+    }
+}
+
 static void
 clear_thread_inferior_resources (struct thread_info *tp)
 {
@@ -175,6 +189,8 @@ clear_thread_inferior_resources (struct thread_info *tp)
 
   btrace_teardown (tp);
 
+  thread_cancel_execution_command (tp);
+
   do_all_intermediate_continuations_thread (tp, 1);
   do_all_continuations_thread (tp, 1);
 }
diff --git a/gdb/tui/tui-interp.c b/gdb/tui/tui-interp.c
index 1a5639d..dbb6976 100644
--- a/gdb/tui/tui-interp.c
+++ b/gdb/tui/tui-interp.c
@@ -55,6 +55,18 @@ tui_exit (void)
    quiet (i.e., another interpreter is being run with
    interpreter-exec), print nothing.  */
 
+/* Observer for the normal_stop notification.  */
+
+static void
+tui_on_normal_stop (struct bpstats *bs, int print_frame)
+{
+  if (!interp_quiet_p (tui_interp))
+    {
+      if (print_frame)
+	print_stop_event (tui_ui_out (tui_interp));
+    }
+}
+
 /* Observer for the signal_received notification.  */
 
 static void
@@ -134,6 +146,7 @@ tui_init (struct interp *self, int top_level)
     tui_initialize_readline ();
 
   /* If changing this, remember to update cli-interp.c as well.  */
+  observer_attach_normal_stop (tui_on_normal_stop);
   observer_attach_signal_received (tui_on_signal_received);
   observer_attach_end_stepping_range (tui_on_end_stepping_range);
   observer_attach_signal_exited (tui_on_signal_exited);
