Blame SOURCES/gdb-rhbz1909902-frame_id_p-assert-1.patch

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