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

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