3f97ff0
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
3f97ff0
From: Pedro Alves <pedro@palves.net>
3f97ff0
Date: Fri, 30 Oct 2020 18:26:15 +0100
3f97ff0
Subject: gdb-rhbz1909902-frame_id_p-assert-1.patch
3f97ff0
3f97ff0
;; Backport fix for frame_id_p assertion failure (RH BZ 1909902).
3f97ff0
3f97ff0
Make scoped_restore_current_thread's cdtors exception free (RFC)
3f97ff0
3f97ff0
If the remote target closes while we're reading registers/memory for
3f97ff0
restoring the selected frame in scoped_restore_current_thread's dtor,
3f97ff0
the corresponding TARGET_CLOSE_ERROR error is swallowed by the
3f97ff0
scoped_restore_current_thread's dtor, because letting exceptions
3f97ff0
escape from a dtor is bad.  It isn't great to lose that errors like
3f97ff0
that, though.  I've been thinking about how to avoid it, and I came up
3f97ff0
with this patch.
3f97ff0
3f97ff0
The idea here is to make scoped_restore_current_thread's dtor do as
3f97ff0
little as possible, to avoid any work that might throw in the first
3f97ff0
place.  And to do that, instead of having the dtor call
3f97ff0
restore_selected_frame, which re-finds the previously selected frame,
3f97ff0
just record the frame_id/level of the desired selected frame, and have
3f97ff0
get_selected_frame find the frame the next time it is called.  In
3f97ff0
effect, this implements most of Cagney's suggestion, here:
3f97ff0
3f97ff0
  /* On demand, create the selected frame and then return it.  If the
3f97ff0
     selected frame can not be created, this function prints then throws
3f97ff0
     an error.  When MESSAGE is non-NULL, use it for the error message,
3f97ff0
     otherwize use a generic error message.  */
3f97ff0
  /* FIXME: cagney/2002-11-28: At present, when there is no selected
3f97ff0
     frame, this function always returns the current (inner most) frame.
3f97ff0
     It should instead, when a thread has previously had its frame
3f97ff0
     selected (but not resumed) and the frame cache invalidated, find
3f97ff0
     and then return that thread's previously selected frame.  */
3f97ff0
  extern struct frame_info *get_selected_frame (const char *message);
3f97ff0
3f97ff0
The only thing missing to fully implement that would be to make
3f97ff0
reinit_frame_cache just clear selected_frame instead of calling
3f97ff0
select_frame(NULL), and the call select_frame(NULL) explicitly in the
3f97ff0
places where we really wanted reinit_frame_cache to go back to the
3f97ff0
current frame too.  That can done separately, though, I'm not
3f97ff0
proposing to do that in this patch.
3f97ff0
3f97ff0
Note that this patch renames restore_selected_frame to
3f97ff0
lookup_selected_frame, and adds a new restore_selected_frame function
3f97ff0
that doesn't throw, to be paired with the also-new save_selected_frame
3f97ff0
function.
3f97ff0
3f97ff0
There's a restore_selected_frame function in infrun.c that I think can
3f97ff0
be replaced by the new one in frame.c.
3f97ff0
3f97ff0
Also done in this patch is make the get_selected_frame's parameter be
3f97ff0
optional, so that we don't have to pass down nullptr explicitly all
3f97ff0
over the place.
3f97ff0
3f97ff0
lookup_selected_frame should really move from thread.c to frame.c, but
3f97ff0
I didn't do that here, just to avoid churn in the patch while it
3f97ff0
collects comments.  I did make it extern and declared it in frame.h
3f97ff0
already, preparing for the move.  I will do the move as a follow up
3f97ff0
patch if people agree with this approach.
3f97ff0
3f97ff0
Incidentally, this patch alone would fix the crashes fixed by the
3f97ff0
previous patches in the series, because with this,
3f97ff0
scoped_restore_current_thread's constructor doesn't throw either.
3f97ff0
3f97ff0
gdb/ChangeLog:
3f97ff0
3f97ff0
	* blockframe.c (block_innermost_frame): Use get_selected_frame.
3f97ff0
	* frame.c
3f97ff0
	(scoped_restore_selected_frame::scoped_restore_selected_frame):
3f97ff0
	Use save_selected_frame.  Save language as well.
3f97ff0
	(scoped_restore_selected_frame::~scoped_restore_selected_frame):
3f97ff0
	Use restore_selected_frame, and restore language as well.
3f97ff0
	(selected_frame_id, selected_frame_level): New.
3f97ff0
	(selected_frame): Update comments.
3f97ff0
	(save_selected_frame, restore_selected_frame): New.
3f97ff0
	(get_selected_frame): Use lookup_selected_frame.
3f97ff0
	(get_selected_frame_if_set): Delete.
3f97ff0
	(select_frame): Record selected_frame_level and selected_frame_id.
3f97ff0
	* frame.h (scoped_restore_selected_frame) <m_level, m_lang>: New
3f97ff0
	fields.
3f97ff0
	(get_selected_frame): Make 'message' parameter optional.
3f97ff0
	(get_selected_frame_if_set): Delete declaration.
3f97ff0
	(select_frame): Update comments.
3f97ff0
	(save_selected_frame, restore_selected_frame)
3f97ff0
	(lookup_selected_frame): Declare.
3f97ff0
	* gdbthread.h (scoped_restore_current_thread) <m_lang>: New field.
3f97ff0
	* infrun.c (struct infcall_control_state) <selected_frame_level>:
3f97ff0
	New field.
3f97ff0
	(save_infcall_control_state): Use save_selected_frame.
3f97ff0
	(restore_selected_frame): Delete.
3f97ff0
	(restore_infcall_control_state): Use restore_selected_frame.
3f97ff0
	* stack.c (select_frame_command_core, frame_command_core): Use
3f97ff0
	get_selected_frame.
3f97ff0
	* thread.c (restore_selected_frame): Rename to ...
3f97ff0
	(lookup_selected_frame): ... this and make extern.  Select the
3f97ff0
	current frame if the frame level is -1.
3f97ff0
	(scoped_restore_current_thread::restore): Also restore the
3f97ff0
	language.
3f97ff0
	(scoped_restore_current_thread::~scoped_restore_current_thread):
3f97ff0
	Don't try/catch.
3f97ff0
	(scoped_restore_current_thread::scoped_restore_current_thread):
3f97ff0
	Save the language as well.  Use save_selected_frame.
3f97ff0
3f97ff0
Change-Id: I73fd1cfc40d8513c28e5596383b7ecd8bcfe700f
3f97ff0
3f97ff0
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
3f97ff0
--- a/gdb/blockframe.c
3f97ff0
+++ b/gdb/blockframe.c
3f97ff0
@@ -464,14 +464,10 @@ find_gnu_ifunc_target_type (CORE_ADDR resolver_funaddr)
3f97ff0
 struct frame_info *
3f97ff0
 block_innermost_frame (const struct block *block)
3f97ff0
 {
3f97ff0
-  struct frame_info *frame;
3f97ff0
-
3f97ff0
   if (block == NULL)
3f97ff0
     return NULL;
3f97ff0
 
3f97ff0
-  frame = get_selected_frame_if_set ();
3f97ff0
-  if (frame == NULL)
3f97ff0
-    frame = get_current_frame ();
3f97ff0
+  frame_info *frame = get_selected_frame ();
3f97ff0
   while (frame != NULL)
3f97ff0
     {
3f97ff0
       const struct block *frame_block = get_frame_block (frame, NULL);
3f97ff0
diff --git a/gdb/frame.c b/gdb/frame.c
3f97ff0
--- a/gdb/frame.c
3f97ff0
+++ b/gdb/frame.c
3f97ff0
@@ -317,17 +317,15 @@ frame_stash_invalidate (void)
3f97ff0
 /* See frame.h  */
3f97ff0
 scoped_restore_selected_frame::scoped_restore_selected_frame ()
3f97ff0
 {
3f97ff0
-  m_fid = get_frame_id (get_selected_frame (NULL));
3f97ff0
+  m_lang = current_language->la_language;
3f97ff0
+  save_selected_frame (&m_fid, &m_level);
3f97ff0
 }
3f97ff0
 
3f97ff0
 /* See frame.h  */
3f97ff0
 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
3f97ff0
 {
3f97ff0
-  frame_info *frame = frame_find_by_id (m_fid);
3f97ff0
-  if (frame == NULL)
3f97ff0
-    warning (_("Unable to restore previously selected frame."));
3f97ff0
-  else
3f97ff0
-    select_frame (frame);
3f97ff0
+  restore_selected_frame (m_fid, m_level);
3f97ff0
+  set_language (m_lang);
3f97ff0
 }
3f97ff0
 
3f97ff0
 /* Flag to control debugging.  */
3f97ff0
@@ -1685,10 +1683,63 @@ get_current_frame (void)
3f97ff0
 }
3f97ff0
 
3f97ff0
 /* The "selected" stack frame is used by default for local and arg
3f97ff0
-   access.  May be zero, for no selected frame.  */
3f97ff0
-
3f97ff0
+   access.
3f97ff0
+
3f97ff0
+   The "single source of truth" for the selected frame is the
3f97ff0
+   SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL pair.
3f97ff0
+
3f97ff0
+   Frame IDs can be saved/restored across reinitializing the frame
3f97ff0
+   cache, while frame_info pointers can't (frame_info objects are
3f97ff0
+   invalidated).  If we know the corresponding frame_info object, it
3f97ff0
+   is cached in SELECTED_FRAME.
3f97ff0
+
3f97ff0
+   If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
3f97ff0
+   and the target has stack and is stopped, the selected frame is the
3f97ff0
+   current (innermost) frame.  This means that SELECTED_FRAME_LEVEL is
3f97ff0
+   never 0 and SELECTED_FRAME_ID is never the ID of the innermost
3f97ff0
+   frame.
3f97ff0
+
3f97ff0
+   If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
3f97ff0
+   and the target has no stack or is executing, then there's no
3f97ff0
+   selected frame.  */
3f97ff0
+static frame_id selected_frame_id = null_frame_id;
3f97ff0
+static int selected_frame_level = -1;
3f97ff0
+
3f97ff0
+/* The cached frame_info object pointing to the selected frame.
3f97ff0
+   Looked up on demand by get_selected_frame.  */
3f97ff0
 static struct frame_info *selected_frame;
3f97ff0
 
3f97ff0
+/* See frame.h.  */
3f97ff0
+
3f97ff0
+void
3f97ff0
+save_selected_frame (frame_id *frame_id, int *frame_level)
3f97ff0
+  noexcept
3f97ff0
+{
3f97ff0
+  *frame_id = selected_frame_id;
3f97ff0
+  *frame_level = selected_frame_level;
3f97ff0
+}
3f97ff0
+
3f97ff0
+/* See frame.h.  */
3f97ff0
+
3f97ff0
+void
3f97ff0
+restore_selected_frame (frame_id frame_id, int frame_level)
3f97ff0
+  noexcept
3f97ff0
+{
3f97ff0
+  /* save_selected_frame never returns level == 0, so we shouldn't see
3f97ff0
+     it here either.  */
3f97ff0
+  gdb_assert (frame_level != 0);
3f97ff0
+
3f97ff0
+  /* FRAME_ID can be null_frame_id only IFF frame_level is -1.  */
3f97ff0
+  gdb_assert ((frame_level == -1 && !frame_id_p (frame_id))
3f97ff0
+	      || (frame_level != -1 && frame_id_p (frame_id)));
3f97ff0
+
3f97ff0
+  selected_frame_id = frame_id;
3f97ff0
+  selected_frame_level = frame_level;
3f97ff0
+
3f97ff0
+  /* Will be looked up later by get_selected_frame.  */
3f97ff0
+  selected_frame = nullptr;
3f97ff0
+}
3f97ff0
+
3f97ff0
 bool
3f97ff0
 has_stack_frames ()
3f97ff0
 {
3f97ff0
@@ -1715,9 +1766,7 @@ has_stack_frames ()
3f97ff0
   return true;
3f97ff0
 }
3f97ff0
 
3f97ff0
-/* Return the selected frame.  Always non-NULL (unless there isn't an
3f97ff0
-   inferior sufficient for creating a frame) in which case an error is
3f97ff0
-   thrown.  */
3f97ff0
+/* See frame.h.  */
3f97ff0
 
3f97ff0
 struct frame_info *
3f97ff0
 get_selected_frame (const char *message)
3f97ff0
@@ -1726,24 +1775,14 @@ get_selected_frame (const char *message)
3f97ff0
     {
3f97ff0
       if (message != NULL && !has_stack_frames ())
3f97ff0
 	error (("%s"), message);
3f97ff0
-      /* Hey!  Don't trust this.  It should really be re-finding the
3f97ff0
-	 last selected frame of the currently selected thread.  This,
3f97ff0
-	 though, is better than nothing.  */
3f97ff0
-      select_frame (get_current_frame ());
3f97ff0
+
3f97ff0
+      lookup_selected_frame (selected_frame_id, selected_frame_level);
3f97ff0
     }
3f97ff0
   /* There is always a frame.  */
3f97ff0
   gdb_assert (selected_frame != NULL);
3f97ff0
   return selected_frame;
3f97ff0
 }
3f97ff0
 
3f97ff0
-/* If there is a selected frame, return it.  Otherwise, return NULL.  */
3f97ff0
-
3f97ff0
-struct frame_info *
3f97ff0
-get_selected_frame_if_set (void)
3f97ff0
-{
3f97ff0
-  return selected_frame;
3f97ff0
-}
3f97ff0
-
3f97ff0
 /* This is a variant of get_selected_frame() which can be called when
3f97ff0
    the inferior does not have a frame; in that case it will return
3f97ff0
    NULL instead of calling error().  */
3f97ff0
@@ -1756,12 +1795,42 @@ deprecated_safe_get_selected_frame (void)
3f97ff0
   return get_selected_frame (NULL);
3f97ff0
 }
3f97ff0
 
3f97ff0
-/* Select frame FI (or NULL - to invalidate the current frame).  */
3f97ff0
+/* Select frame FI (or NULL - to invalidate the selected frame).  */
3f97ff0
 
3f97ff0
 void
3f97ff0
 select_frame (struct frame_info *fi)
3f97ff0
 {
3f97ff0
   selected_frame = fi;
3f97ff0
+  selected_frame_level = frame_relative_level (fi);
3f97ff0
+  if (selected_frame_level == 0)
3f97ff0
+    {
3f97ff0
+      /* Treat the current frame especially -- we want to always
3f97ff0
+	 save/restore it without warning, even if the frame ID changes
3f97ff0
+	 (see lookup_selected_frame).  E.g.:
3f97ff0
+
3f97ff0
+	  // The current frame is selected, the target had just stopped.
3f97ff0
+	  {
3f97ff0
+	    scoped_restore_selected_frame restore_frame;
3f97ff0
+	    some_operation_that_changes_the_stack ();
3f97ff0
+	  }
3f97ff0
+	  // scoped_restore_selected_frame's dtor runs, but the
3f97ff0
+	  // original frame_id can't be found.  No matter whether it
3f97ff0
+	  // is found or not, we still end up with the now-current
3f97ff0
+	  // frame selected.  Warning in lookup_selected_frame in this
3f97ff0
+	  // case seems pointless.
3f97ff0
+
3f97ff0
+	 Also get_frame_id may access the target's registers/memory,
3f97ff0
+	 and thus skipping get_frame_id optimizes the common case.
3f97ff0
+
3f97ff0
+	 Saving the selected frame this way makes get_selected_frame
3f97ff0
+	 and restore_current_frame return/re-select whatever frame is
3f97ff0
+	 the innermost (current) then.  */
3f97ff0
+      selected_frame_level = -1;
3f97ff0
+      selected_frame_id = null_frame_id;
3f97ff0
+    }
3f97ff0
+  else
3f97ff0
+    selected_frame_id = get_frame_id (fi);
3f97ff0
+
3f97ff0
   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
3f97ff0
      frame is being invalidated.  */
3f97ff0
 
3f97ff0
diff --git a/gdb/frame.h b/gdb/frame.h
3f97ff0
--- a/gdb/frame.h
3f97ff0
+++ b/gdb/frame.h
3f97ff0
@@ -186,8 +186,14 @@ class scoped_restore_selected_frame
3f97ff0
 
3f97ff0
 private:
3f97ff0
 
3f97ff0
-  /* The ID of the previously selected frame.  */
3f97ff0
+  /* The ID and level of the previously selected frame.  */
3f97ff0
   struct frame_id m_fid;
3f97ff0
+  int m_level;
3f97ff0
+
3f97ff0
+  /* Save/restore the language as well, because selecting a frame
3f97ff0
+     changes the current language to the frame's language if "set
3f97ff0
+     language auto".  */
3f97ff0
+  enum language m_lang;
3f97ff0
 };
3f97ff0
 
3f97ff0
 /* Methods for constructing and comparing Frame IDs.  */
3f97ff0
@@ -316,24 +322,49 @@ extern bool has_stack_frames ();
3f97ff0
    modifies the target invalidating the frame cache).  */
3f97ff0
 extern void reinit_frame_cache (void);
3f97ff0
 
3f97ff0
-/* On demand, create the selected frame and then return it.  If the
3f97ff0
-   selected frame can not be created, this function prints then throws
3f97ff0
-   an error.  When MESSAGE is non-NULL, use it for the error message,
3f97ff0
+/* Return the selected frame.  Always returns non-NULL.  If there
3f97ff0
+   isn't an inferior sufficient for creating a frame, an error is
3f97ff0
+   thrown.  When MESSAGE is non-NULL, use it for the error message,
3f97ff0
    otherwise use a generic error message.  */
3f97ff0
 /* FIXME: cagney/2002-11-28: At present, when there is no selected
3f97ff0
    frame, this function always returns the current (inner most) frame.
3f97ff0
    It should instead, when a thread has previously had its frame
3f97ff0
    selected (but not resumed) and the frame cache invalidated, find
3f97ff0
    and then return that thread's previously selected frame.  */
3f97ff0
-extern struct frame_info *get_selected_frame (const char *message);
3f97ff0
-
3f97ff0
-/* If there is a selected frame, return it.  Otherwise, return NULL.  */
3f97ff0
-extern struct frame_info *get_selected_frame_if_set (void);
3f97ff0
+extern struct frame_info *get_selected_frame (const char *message = nullptr);
3f97ff0
 
3f97ff0
-/* Select a specific frame.  NULL, apparently implies re-select the
3f97ff0
-   inner most frame.  */
3f97ff0
+/* Select a specific frame.  NULL implies re-select the inner most
3f97ff0
+   frame.  */
3f97ff0
 extern void select_frame (struct frame_info *);
3f97ff0
 
3f97ff0
+/* Save the frame ID and frame level of the selected frame in FRAME_ID
3f97ff0
+   and FRAME_LEVEL, to be restored later with restore_selected_frame.
3f97ff0
+
3f97ff0
+   This is preferred over getting the same info out of
3f97ff0
+   get_selected_frame directly because this function does not create
3f97ff0
+   the selected-frame's frame_info object if it hasn't been created
3f97ff0
+   yet, and thus is more efficient and doesn't throw.  */
3f97ff0
+extern void save_selected_frame (frame_id *frame_id, int *frame_level)
3f97ff0
+  noexcept;
3f97ff0
+
3f97ff0
+/* Restore selected frame as saved with save_selected_frame.
3f97ff0
+
3f97ff0
+   Does not try to find the corresponding frame_info object.  Instead
3f97ff0
+   the next call to get_selected_frame will look it up and cache the
3f97ff0
+   result.
3f97ff0
+
3f97ff0
+   This function does not throw.  It is designed to be safe to called
3f97ff0
+   from the destructors of RAII types.  */
3f97ff0
+extern void restore_selected_frame (frame_id frame_id, int frame_level)
3f97ff0
+  noexcept;
3f97ff0
+
3f97ff0
+/* Lookup the frame_info object for the selected frame FRAME_ID /
3f97ff0
+   FRAME_LEVEL and cache the result.
3f97ff0
+
3f97ff0
+   If FRAME_LEVEL > 0 and the originally selected frame isn't found,
3f97ff0
+   warn and select the innermost (current) frame.  */
3f97ff0
+extern void lookup_selected_frame (frame_id frame_id, int frame_level);
3f97ff0
+
3f97ff0
 /* Given a FRAME, return the next (more inner, younger) or previous
3f97ff0
    (more outer, older) frame.  */
3f97ff0
 extern struct frame_info *get_prev_frame (struct frame_info *);
3f97ff0
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
3f97ff0
--- a/gdb/gdbthread.h
3f97ff0
+++ b/gdb/gdbthread.h
3f97ff0
@@ -673,6 +673,10 @@ class scoped_restore_current_thread
3f97ff0
   frame_id m_selected_frame_id;
3f97ff0
   int m_selected_frame_level;
3f97ff0
   bool m_was_stopped;
3f97ff0
+  /* Save/restore the language as well, because selecting a frame
3f97ff0
+     changes the current language to the frame's language if "set
3f97ff0
+     language auto".  */
3f97ff0
+  enum language m_lang;
3f97ff0
 };
3f97ff0
 
3f97ff0
 /* Returns a pointer into the thread_info corresponding to
3f97ff0
diff --git a/gdb/infrun.c b/gdb/infrun.c
3f97ff0
--- a/gdb/infrun.c
3f97ff0
+++ b/gdb/infrun.c
3f97ff0
@@ -9017,8 +9017,10 @@ struct infcall_control_state
3f97ff0
   enum stop_stack_kind stop_stack_dummy = STOP_NONE;
3f97ff0
   int stopped_by_random_signal = 0;
3f97ff0
 
3f97ff0
-  /* ID if the selected frame when the inferior function call was made.  */
3f97ff0
+  /* ID and level of the selected frame when the inferior function
3f97ff0
+     call was made.  */
3f97ff0
   struct frame_id selected_frame_id {};
3f97ff0
+  int selected_frame_level = -1;
3f97ff0
 };
3f97ff0
 
3f97ff0
 /* Save all of the information associated with the inferior<==>gdb
3f97ff0
@@ -9047,27 +9049,12 @@ save_infcall_control_state ()
3f97ff0
   inf_status->stop_stack_dummy = stop_stack_dummy;
3f97ff0
   inf_status->stopped_by_random_signal = stopped_by_random_signal;
3f97ff0
 
3f97ff0
-  inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
3f97ff0
+  save_selected_frame (&inf_status->selected_frame_id,
3f97ff0
+		       &inf_status->selected_frame_level);
3f97ff0
 
3f97ff0
   return inf_status;
3f97ff0
 }
3f97ff0
 
3f97ff0
-static void
3f97ff0
-restore_selected_frame (const frame_id &fid)
3f97ff0
-{
3f97ff0
-  frame_info *frame = frame_find_by_id (fid);
3f97ff0
-
3f97ff0
-  /* If inf_status->selected_frame_id is NULL, there was no previously
3f97ff0
-     selected frame.  */
3f97ff0
-  if (frame == NULL)
3f97ff0
-    {
3f97ff0
-      warning (_("Unable to restore previously selected frame."));
3f97ff0
-      return;
3f97ff0
-    }
3f97ff0
-
3f97ff0
-  select_frame (frame);
3f97ff0
-}
3f97ff0
-
3f97ff0
 /* Restore inferior session state to INF_STATUS.  */
3f97ff0
 
3f97ff0
 void
3f97ff0
@@ -9095,21 +9082,8 @@ restore_infcall_control_state (struct infcall_control_state *inf_status)
3f97ff0
 
3f97ff0
   if (target_has_stack)
3f97ff0
     {
3f97ff0
-      /* The point of the try/catch is that if the stack is clobbered,
3f97ff0
-         walking the stack might encounter a garbage pointer and
3f97ff0
-         error() trying to dereference it.  */
3f97ff0
-      try
3f97ff0
-	{
3f97ff0
-	  restore_selected_frame (inf_status->selected_frame_id);
3f97ff0
-	}
3f97ff0
-      catch (const gdb_exception_error &ex)
3f97ff0
-	{
3f97ff0
-	  exception_fprintf (gdb_stderr, ex,
3f97ff0
-			     "Unable to restore previously selected frame:\n");
3f97ff0
-	  /* Error in restoring the selected frame.  Select the
3f97ff0
-	     innermost frame.  */
3f97ff0
-	  select_frame (get_current_frame ());
3f97ff0
-	}
3f97ff0
+      restore_selected_frame (inf_status->selected_frame_id,
3f97ff0
+			      inf_status->selected_frame_level);
3f97ff0
     }
3f97ff0
 
3f97ff0
   delete inf_status;
3f97ff0
diff --git a/gdb/stack.c b/gdb/stack.c
3f97ff0
--- a/gdb/stack.c
3f97ff0
+++ b/gdb/stack.c
3f97ff0
@@ -1842,9 +1842,9 @@ trailing_outermost_frame (int count)
3f97ff0
 static void
3f97ff0
 select_frame_command_core (struct frame_info *fi, bool ignored)
3f97ff0
 {
3f97ff0
-  struct frame_info *prev_frame = get_selected_frame_if_set ();
3f97ff0
+  frame_info *prev_frame = get_selected_frame ();
3f97ff0
   select_frame (fi);
3f97ff0
-  if (get_selected_frame_if_set () != prev_frame)
3f97ff0
+  if (get_selected_frame () != prev_frame)
3f97ff0
     gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
3f97ff0
 }
3f97ff0
 
3f97ff0
@@ -1863,10 +1863,9 @@ select_frame_for_mi (struct frame_info *fi)
3f97ff0
 static void
3f97ff0
 frame_command_core (struct frame_info *fi, bool ignored)
3f97ff0
 {
3f97ff0
-  struct frame_info *prev_frame = get_selected_frame_if_set ();
3f97ff0
-
3f97ff0
+  frame_info *prev_frame = get_selected_frame ();
3f97ff0
   select_frame (fi);
3f97ff0
-  if (get_selected_frame_if_set () != prev_frame)
3f97ff0
+  if (get_selected_frame () != prev_frame)
3f97ff0
     gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
3f97ff0
   else
3f97ff0
     print_selected_thread_frame (current_uiout, USER_SELECTED_FRAME);
3f97ff0
diff --git a/gdb/thread.c b/gdb/thread.c
3f97ff0
--- a/gdb/thread.c
3f97ff0
+++ b/gdb/thread.c
3f97ff0
@@ -1325,20 +1325,26 @@ switch_to_thread (process_stratum_target *proc_target, ptid_t ptid)
3f97ff0
   switch_to_thread (thr);
3f97ff0
 }
3f97ff0
 
3f97ff0
-static void
3f97ff0
-restore_selected_frame (struct frame_id a_frame_id, int frame_level)
3f97ff0
+/* See frame.h.  */
3f97ff0
+
3f97ff0
+void
3f97ff0
+lookup_selected_frame (struct frame_id a_frame_id, int frame_level)
3f97ff0
 {
3f97ff0
   struct frame_info *frame = NULL;
3f97ff0
   int count;
3f97ff0
 
3f97ff0
-  /* This means there was no selected frame.  */
3f97ff0
+  /* This either means there was no selected frame, or the selected
3f97ff0
+     frame was the current frame.  In either case, select the current
3f97ff0
+     frame.  */
3f97ff0
   if (frame_level == -1)
3f97ff0
     {
3f97ff0
-      select_frame (NULL);
3f97ff0
+      select_frame (get_current_frame ());
3f97ff0
       return;
3f97ff0
     }
3f97ff0
 
3f97ff0
-  gdb_assert (frame_level >= 0);
3f97ff0
+  /* select_frame never saves 0 in SELECTED_FRAME_LEVEL, so we
3f97ff0
+     shouldn't see it here.  */
3f97ff0
+  gdb_assert (frame_level > 0);
3f97ff0
 
3f97ff0
   /* Restore by level first, check if the frame id is the same as
3f97ff0
      expected.  If that fails, try restoring by frame id.  If that
3f97ff0
@@ -1409,64 +1415,28 @@ scoped_restore_current_thread::restore ()
3f97ff0
       && target_has_stack
3f97ff0
       && target_has_memory)
3f97ff0
     restore_selected_frame (m_selected_frame_id, m_selected_frame_level);
3f97ff0
+
3f97ff0
+  set_language (m_lang);
3f97ff0
 }
3f97ff0
 
3f97ff0
 scoped_restore_current_thread::~scoped_restore_current_thread ()
3f97ff0
 {
3f97ff0
   if (!m_dont_restore)
3f97ff0
-    {
3f97ff0
-      try
3f97ff0
-	{
3f97ff0
-	  restore ();
3f97ff0
-	}
3f97ff0
-      catch (const gdb_exception &ex)
3f97ff0
-	{
3f97ff0
-	  /* We're in a dtor, there's really nothing else we can do
3f97ff0
-	     but swallow the exception.  */
3f97ff0
-	}
3f97ff0
-    }
3f97ff0
+    restore ();
3f97ff0
 }
3f97ff0
 
3f97ff0
 scoped_restore_current_thread::scoped_restore_current_thread ()
3f97ff0
 {
3f97ff0
   m_inf = inferior_ref::new_reference (current_inferior ());
3f97ff0
 
3f97ff0
+  m_lang = current_language->la_language;
3f97ff0
+
3f97ff0
   if (inferior_ptid != null_ptid)
3f97ff0
     {
3f97ff0
       m_thread = thread_info_ref::new_reference (inferior_thread ());
3f97ff0
 
3f97ff0
-      struct frame_info *frame;
3f97ff0
-
3f97ff0
       m_was_stopped = m_thread->state == THREAD_STOPPED;
3f97ff0
-      if (m_was_stopped
3f97ff0
-	  && target_has_registers
3f97ff0
-	  && target_has_stack
3f97ff0
-	  && target_has_memory)
3f97ff0
-	{
3f97ff0
-	  /* When processing internal events, there might not be a
3f97ff0
-	     selected frame.  If we naively call get_selected_frame
3f97ff0
-	     here, then we can end up reading debuginfo for the
3f97ff0
-	     current frame, but we don't generally need the debuginfo
3f97ff0
-	     at this point.  */
3f97ff0
-	  frame = get_selected_frame_if_set ();
3f97ff0
-	}
3f97ff0
-      else
3f97ff0
-	frame = NULL;
3f97ff0
-
3f97ff0
-      try
3f97ff0
-	{
3f97ff0
-	  m_selected_frame_id = get_frame_id (frame);
3f97ff0
-	  m_selected_frame_level = frame_relative_level (frame);
3f97ff0
-	}
3f97ff0
-      catch (const gdb_exception_error &ex)
3f97ff0
-	{
3f97ff0
-	  m_selected_frame_id = null_frame_id;
3f97ff0
-	  m_selected_frame_level = -1;
3f97ff0
-
3f97ff0
-	  /* Better let this propagate.  */
3f97ff0
-	  if (ex.error == TARGET_CLOSE_ERROR)
3f97ff0
-	    throw;
3f97ff0
-	}
3f97ff0
+      save_selected_frame (&m_selected_frame_id, &m_selected_frame_level);
3f97ff0
     }
3f97ff0
 }
3f97ff0