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

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