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

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