Remove path name from test case
[binutils-gdb.git] / gdb / frame.c
index c746a6a231ed8abc7a12856cbbdf8690e060b3a8..4a46ccb9abc0485b2c2b160aa52d858641b113d2 100644 (file)
@@ -1,6 +1,6 @@
 /* Cache and manage frames for GDB, the GNU debugger.
 
-   Copyright (C) 1986-2020 Free Software Foundation, Inc.
+   Copyright (C) 1986-2023 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "frame.h"
 #include "target.h"
 #include "value.h"
-#include "inferior.h"  /* for inferior_ptid */
+#include "inferior.h"
 #include "regcache.h"
 #include "user-regs.h"
-#include "gdb_obstack.h"
+#include "gdbsupport/gdb_obstack.h"
 #include "dummy-frame.h"
 #include "sentinel-frame.h"
 #include "gdbcore.h"
 #include "hashtab.h"
 #include "valprint.h"
 #include "cli/cli-option.h"
+#include "dwarf2/loc.h"
 
 /* The sentinel frame terminates the innermost end of the frame chain.
    If unwound, it returns the information needed to construct an
    innermost frame.
 
    The current frame, which is the innermost frame, can be found at
-   sentinel_frame->prev.  */
+   sentinel_frame->prev.
 
-static struct frame_info *sentinel_frame;
+   This is an optimization to be able to find the sentinel frame quickly,
+   it could otherwise be found in the frame cache.  */
+
+static frame_info *sentinel_frame;
+
+/* Number of calls to reinit_frame_cache.  */
+static unsigned int frame_cache_generation = 0;
+
+/* See frame.h.  */
+
+unsigned int
+get_frame_cache_generation ()
+{
+  return frame_cache_generation;
+}
 
 /* The values behind the global "set backtrace ..." settings.  */
 set_backtrace_options user_set_backtrace_options;
 
-static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
+static frame_info_ptr get_prev_frame_raw (frame_info_ptr this_frame);
 static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
+static frame_info_ptr create_new_frame (frame_id id);
 
 /* Status of some values cached in the frame_info object.  */
 
@@ -76,6 +92,18 @@ enum cached_copy_status
   CC_UNAVAILABLE
 };
 
+enum class frame_id_status
+{
+  /* Frame id is not computed.  */
+  NOT_COMPUTED = 0,
+
+  /* Frame id is being computed (compute_frame_id is active).  */
+  COMPUTING,
+
+  /* Frame id has been computed.  */
+  COMPUTED,
+};
+
 /* We keep a cache of stack frames, each of which is a "struct
    frame_info".  The innermost one gets allocated (in
    wait_for_inferior) each time the inferior stops; sentinel_frame
@@ -88,6 +116,9 @@ enum cached_copy_status
 
 struct frame_info
 {
+  /* Return a string representation of this frame.  */
+  std::string to_string () const;
+
   /* Level of this frame.  The inner-most (youngest) frame is at level
      0.  As you move towards the outer-most (oldest) frame, the level
      increases.  This is a cached value.  It could just as easily be
@@ -116,32 +147,32 @@ struct frame_info
   /* Cached copy of the previous frame's architecture.  */
   struct
   {
-    int p;
+    bool p;
     struct gdbarch *arch;
   } prev_arch;
 
   /* Cached copy of the previous frame's resume address.  */
   struct {
-    enum cached_copy_status status;
+    cached_copy_status status;
     /* Did VALUE require unmasking when being read.  */
     bool masked;
     CORE_ADDR value;
   } prev_pc;
-  
+
   /* Cached copy of the previous frame's function address.  */
   struct
   {
     CORE_ADDR addr;
-    int p;
+    cached_copy_status status;
   } prev_func;
-  
+
   /* This frame's ID.  */
   struct
   {
-    int p;
+    frame_id_status p;
     struct frame_id value;
   } this_id;
-  
+
   /* The frame's high-level base methods, and corresponding cache.
      The high level base methods are selected based on the frame's
      debug info.  */
@@ -151,7 +182,7 @@ struct frame_info
   /* Pointers to the next (down, inner, younger) and previous (up,
      outer, older) frame_info's in the frame cache.  */
   struct frame_info *next; /* down, inner, younger */
-  int prev_p;
+  bool prev_p;
   struct frame_info *prev; /* up, outer, older */
 
   /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
@@ -166,7 +197,7 @@ struct frame_info
 /* See frame.h.  */
 
 void
-set_frame_previous_pc_masked (struct frame_info *frame)
+set_frame_previous_pc_masked (frame_info_ptr frame)
 {
   frame->prev_pc.masked = true;
 }
@@ -174,7 +205,7 @@ set_frame_previous_pc_masked (struct frame_info *frame)
 /* See frame.h.  */
 
 bool
-get_frame_pc_masked (const struct frame_info *frame)
+get_frame_pc_masked (frame_info_ptr frame)
 {
   gdb_assert (frame->next != nullptr);
   gdb_assert (frame->next->prev_pc.status == CC_VALUE);
@@ -196,7 +227,7 @@ static htab_t frame_stash;
 static hashval_t
 frame_addr_hash (const void *ap)
 {
-  const struct frame_info *frame = (const struct frame_info *) ap;
+  const frame_info *frame = (const frame_info *) ap;
   const struct frame_id f_id = frame->this_id.value;
   hashval_t hash = 0;
 
@@ -214,20 +245,36 @@ frame_addr_hash (const void *ap)
     hash = iterative_hash (&f_id.special_addr,
                           sizeof (f_id.special_addr), hash);
 
+  char user_created_p = f_id.user_created_p;
+  hash = iterative_hash (&user_created_p, sizeof (user_created_p), hash);
+
   return hash;
 }
 
 /* Internal equality function for the hash table.  This function
-   defers equality operations to frame_id_eq.  */
+   defers equality operations to frame_id::operator==.  */
 
 static int
 frame_addr_hash_eq (const void *a, const void *b)
 {
-  const struct frame_info *f_entry = (const struct frame_info *) a;
-  const struct frame_info *f_element = (const struct frame_info *) b;
+  const frame_info *f_entry = (const frame_info *) a;
+  const frame_info *f_element = (const frame_info *) b;
+
+  return f_entry->this_id.value == f_element->this_id.value;
+}
+
+/* Deletion function for the frame cache hash table.  */
+
+static void
+frame_info_del (frame_info *frame)
+{
+  if (frame->prologue_cache != nullptr
+      && frame->unwind->dealloc_cache != nullptr)
+    frame->unwind->dealloc_cache (frame, frame->prologue_cache);
 
-  return frame_id_eq (f_entry->this_id.value,
-                     f_element->this_id.value);
+  if (frame->base_cache != nullptr
+      && frame->base->unwind->dealloc_cache != nullptr)
+    frame->base->unwind->dealloc_cache (frame, frame->base_cache);
 }
 
 /* Internal function to create the frame_stash hash table.  100 seems
@@ -236,52 +283,52 @@ frame_addr_hash_eq (const void *a, const void *b)
 static void
 frame_stash_create (void)
 {
-  frame_stash = htab_create (100,
-                            frame_addr_hash,
-                            frame_addr_hash_eq,
-                            NULL);
+  frame_stash = htab_create
+    (100, frame_addr_hash, frame_addr_hash_eq,
+     [] (void *p)
+       {
+        auto frame = static_cast<frame_info *> (p);
+        frame_info_del (frame);
+       });
 }
 
 /* Internal function to add a frame to the frame_stash hash table.
    Returns false if a frame with the same ID was already stashed, true
    otherwise.  */
 
-static int
-frame_stash_add (struct frame_info *frame)
+static bool
+frame_stash_add (frame_info *frame)
 {
-  struct frame_info **slot;
+  /* Valid frame levels are -1 (sentinel frames) and above.  */
+  gdb_assert (frame->level >= -1);
 
-  /* Do not try to stash the sentinel frame.  */
-  gdb_assert (frame->level >= 0);
-
-  slot = (struct frame_info **) htab_find_slot (frame_stash,
-                                               frame,
-                                               INSERT);
+  frame_info **slot = (frame_info **) htab_find_slot (frame_stash,
+                                                     frame, INSERT);
 
   /* If we already have a frame in the stack with the same id, we
      either have a stack cycle (corrupted stack?), or some bug
      elsewhere in GDB.  In any case, ignore the duplicate and return
      an indication to the caller.  */
-  if (*slot != NULL)
-    return 0;
+  if (*slot != nullptr)
+    return false;
 
   *slot = frame;
-  return 1;
+  return true;
 }
 
 /* Internal function to search the frame stash for an entry with the
    given frame ID.  If found, return that frame.  Otherwise return
    NULL.  */
 
-static struct frame_info *
+static frame_info_ptr
 frame_stash_find (struct frame_id id)
 {
   struct frame_info dummy;
-  struct frame_info *frame;
+  frame_info *frame;
 
   dummy.this_id.value = id;
-  frame = (struct frame_info *) htab_find (frame_stash, &dummy);
-  return frame;
+  frame = (frame_info *) htab_find (frame_stash, &dummy);
+  return frame_info_ptr (frame);
 }
 
 /* Internal function to invalidate the frame stash by removing all
@@ -297,27 +344,26 @@ frame_stash_invalidate (void)
 /* See frame.h  */
 scoped_restore_selected_frame::scoped_restore_selected_frame ()
 {
-  m_fid = get_frame_id (get_selected_frame (NULL));
+  m_lang = current_language->la_language;
+  save_selected_frame (&m_fid, &m_level);
 }
 
 /* See frame.h  */
 scoped_restore_selected_frame::~scoped_restore_selected_frame ()
 {
-  frame_info *frame = frame_find_by_id (m_fid);
-  if (frame == NULL)
-    warning (_("Unable to restore previously selected frame."));
-  else
-    select_frame (frame);
+  restore_selected_frame (m_fid, m_level);
+  set_language (m_lang);
 }
 
 /* Flag to control debugging.  */
 
-unsigned int frame_debug;
+bool frame_debug;
+
 static void
 show_frame_debug (struct ui_file *file, int from_tty,
                  struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
+  gdb_printf (file, _("Frame debugging is %s.\n"), value);
 }
 
 /* Implementation of "show backtrace past-main".  */
@@ -326,10 +372,10 @@ static void
 show_backtrace_past_main (struct ui_file *file, int from_tty,
                          struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file,
-                   _("Whether backtraces should "
-                     "continue past \"main\" is %s.\n"),
-                   value);
+  gdb_printf (file,
+             _("Whether backtraces should "
+               "continue past \"main\" is %s.\n"),
+             value);
 }
 
 /* Implementation of "show backtrace past-entry".  */
@@ -338,9 +384,9 @@ static void
 show_backtrace_past_entry (struct ui_file *file, int from_tty,
                           struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("Whether backtraces should continue past the "
-                           "entry point of a program is %s.\n"),
-                   value);
+  gdb_printf (file, _("Whether backtraces should continue past the "
+                     "entry point of a program is %s.\n"),
+             value);
 }
 
 /* Implementation of "show backtrace limit".  */
@@ -349,129 +395,131 @@ static void
 show_backtrace_limit (struct ui_file *file, int from_tty,
                      struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file,
-                   _("An upper bound on the number "
-                     "of backtrace levels is %s.\n"),
-                   value);
+  gdb_printf (file,
+             _("An upper bound on the number "
+               "of backtrace levels is %s.\n"),
+             value);
 }
 
+/* See frame.h.  */
 
-static void
-fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
+std::string
+frame_id::to_string () const
 {
-  if (p)
-    fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
-  else
-    fprintf_unfiltered (file, "!%s", name);
-}
+  const struct frame_id &id = *this;
 
-void
-fprint_frame_id (struct ui_file *file, struct frame_id id)
-{
-  fprintf_unfiltered (file, "{");
+  std::string res = "{";
 
   if (id.stack_status == FID_STACK_INVALID)
-    fprintf_unfiltered (file, "!stack");
+    res += "!stack";
   else if (id.stack_status == FID_STACK_UNAVAILABLE)
-    fprintf_unfiltered (file, "stack=<unavailable>");
+    res += "stack=<unavailable>";
   else if (id.stack_status == FID_STACK_SENTINEL)
-    fprintf_unfiltered (file, "stack=<sentinel>");
+    res += "stack=<sentinel>";
+  else if (id.stack_status == FID_STACK_OUTER)
+    res += "stack=<outer>";
   else
-    fprintf_unfiltered (file, "stack=%s", hex_string (id.stack_addr));
-  fprintf_unfiltered (file, ",");
+    res += std::string ("stack=") + hex_string (id.stack_addr);
 
-  fprint_field (file, "code", id.code_addr_p, id.code_addr);
-  fprintf_unfiltered (file, ",");
+  /* Helper function to format 'N=A' if P is true, otherwise '!N'.  */
+  auto field_to_string = [] (const char *n, bool p, CORE_ADDR a) -> std::string
+  {
+    if (p)
+      return std::string (n) + "=" + core_addr_to_string (a);
+    else
+      return std::string ("!") + std::string (n);
+  };
 
-  fprint_field (file, "special", id.special_addr_p, id.special_addr);
+  res += (std::string (",")
+         + field_to_string ("code", id.code_addr_p, id.code_addr)
+         + std::string (",")
+         + field_to_string ("special", id.special_addr_p, id.special_addr));
 
   if (id.artificial_depth)
-    fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
-
-  fprintf_unfiltered (file, "}");
+    res += ",artificial=" + std::to_string (id.artificial_depth);
+  res += "}";
+  return res;
 }
 
-static void
-fprint_frame_type (struct ui_file *file, enum frame_type type)
+/* See frame.h.  */
+
+const char *
+frame_type_str (frame_type type)
 {
   switch (type)
     {
     case NORMAL_FRAME:
-      fprintf_unfiltered (file, "NORMAL_FRAME");
-      return;
+      return "NORMAL_FRAME";
+
     case DUMMY_FRAME:
-      fprintf_unfiltered (file, "DUMMY_FRAME");
-      return;
+      return "DUMMY_FRAME";
+
     case INLINE_FRAME:
-      fprintf_unfiltered (file, "INLINE_FRAME");
-      return;
+      return "INLINE_FRAME";
+
     case TAILCALL_FRAME:
-      fprintf_unfiltered (file, "TAILCALL_FRAME");
-      return;
+      return "TAILCALL_FRAME";
+
     case SIGTRAMP_FRAME:
-      fprintf_unfiltered (file, "SIGTRAMP_FRAME");
-      return;
+      return "SIGTRAMP_FRAME";
+
     case ARCH_FRAME:
-      fprintf_unfiltered (file, "ARCH_FRAME");
-      return;
+      return "ARCH_FRAME";
+
     case SENTINEL_FRAME:
-      fprintf_unfiltered (file, "SENTINEL_FRAME");
-      return;
+      return "SENTINEL_FRAME";
+
     default:
-      fprintf_unfiltered (file, "<unknown type>");
-      return;
+      return "<unknown type>";
     };
 }
 
-static void
-fprint_frame (struct ui_file *file, struct frame_info *fi)
+ /* See struct frame_info.  */
+
+std::string
+frame_info::to_string () const
 {
-  if (fi == NULL)
-    {
-      fprintf_unfiltered (file, "<NULL frame>");
-      return;
-    }
-  fprintf_unfiltered (file, "{");
-  fprintf_unfiltered (file, "level=%d", fi->level);
-  fprintf_unfiltered (file, ",");
-  fprintf_unfiltered (file, "type=");
+  const frame_info *fi = this;
+
+  std::string res;
+
+  res += string_printf ("{level=%d,", fi->level);
+
   if (fi->unwind != NULL)
-    fprint_frame_type (file, fi->unwind->type);
+    res += string_printf ("type=%s,", frame_type_str (fi->unwind->type));
   else
-    fprintf_unfiltered (file, "<unknown>");
-  fprintf_unfiltered (file, ",");
-  fprintf_unfiltered (file, "unwind=");
+    res += "type=<unknown>,";
+
   if (fi->unwind != NULL)
-    gdb_print_host_address (fi->unwind, file);
+    res += string_printf ("unwinder=\"%s\",", fi->unwind->name);
   else
-    fprintf_unfiltered (file, "<unknown>");
-  fprintf_unfiltered (file, ",");
-  fprintf_unfiltered (file, "pc=");
+    res += "unwinder=<unknown>,";
+
   if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
-    fprintf_unfiltered (file, "<unknown>");
+    res += "pc=<unknown>,";
   else if (fi->next->prev_pc.status == CC_VALUE)
-    {
-      fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_pc.value));
-      if (fi->next->prev_pc.masked)
-       fprintf_unfiltered (file, "[PAC]");
-    }
+    res += string_printf ("pc=%s%s,", hex_string (fi->next->prev_pc.value),
+                         fi->next->prev_pc.masked ? "[PAC]" : "");
   else if (fi->next->prev_pc.status == CC_NOT_SAVED)
-    val_print_not_saved (file);
+    res += "pc=<not saved>,";
   else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
-    val_print_unavailable (file);
-  fprintf_unfiltered (file, ",");
-  fprintf_unfiltered (file, "id=");
-  if (fi->this_id.p)
-    fprint_frame_id (file, fi->this_id.value);
+    res += "pc=<unavailable>,";
+
+  if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
+    res += "id=<not computed>,";
+  else if (fi->this_id.p == frame_id_status::COMPUTING)
+    res += "id=<computing>,";
   else
-    fprintf_unfiltered (file, "<unknown>");
-  fprintf_unfiltered (file, ",");
-  fprintf_unfiltered (file, "func=");
-  if (fi->next != NULL && fi->next->prev_func.p)
-    fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
+    res += string_printf ("id=%s,", fi->this_id.value.to_string ().c_str ());
+
+  if (fi->next != NULL && fi->next->prev_func.status == CC_VALUE)
+    res += string_printf ("func=%s", hex_string (fi->next->prev_func.addr));
   else
-    fprintf_unfiltered (file, "<unknown>");
-  fprintf_unfiltered (file, "}");
+    res += "func=<unknown>";
+
+  res += "}";
+
+  return res;
 }
 
 /* Given FRAME, return the enclosing frame as found in real frames read-in from
@@ -479,8 +527,8 @@ fprint_frame (struct ui_file *file, struct frame_info *fi)
    Return FRAME if FRAME is a non-artificial frame.
    Return NULL if FRAME is the start of an artificial-only chain.  */
 
-static struct frame_info *
-skip_artificial_frames (struct frame_info *frame)
+static frame_info_ptr
+skip_artificial_frames (frame_info_ptr frame)
 {
   /* Note we use get_prev_frame_always, and not get_prev_frame.  The
      latter will truncate the frame chain, leading to this function
@@ -500,8 +548,8 @@ skip_artificial_frames (struct frame_info *frame)
   return frame;
 }
 
-struct frame_info *
-skip_unwritable_frames (struct frame_info *frame)
+frame_info_ptr
+skip_unwritable_frames (frame_info_ptr frame)
 {
   while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0)
     {
@@ -515,8 +563,8 @@ skip_unwritable_frames (struct frame_info *frame)
 
 /* See frame.h.  */
 
-struct frame_info *
-skip_tailcall_frames (struct frame_info *frame)
+frame_info_ptr
+skip_tailcall_frames (frame_info_ptr frame)
 {
   while (get_frame_type (frame) == TAILCALL_FRAME)
     {
@@ -534,27 +582,45 @@ skip_tailcall_frames (struct frame_info *frame)
    frame.  */
 
 static void
-compute_frame_id (struct frame_info *fi)
+compute_frame_id (frame_info_ptr fi)
 {
-  gdb_assert (!fi->this_id.p);
+  FRAME_SCOPED_DEBUG_ENTER_EXIT;
 
-  if (frame_debug)
-    fprintf_unfiltered (gdb_stdlog, "{ compute_frame_id (fi=%d) ",
-                       fi->level);
-  /* Find the unwinder.  */
-  if (fi->unwind == NULL)
-    frame_unwind_find_by_frame (fi, &fi->prologue_cache);
-  /* Find THIS frame's ID.  */
-  /* Default to outermost if no ID is found.  */
-  fi->this_id.value = outer_frame_id;
-  fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
-  gdb_assert (frame_id_p (fi->this_id.value));
-  fi->this_id.p = 1;
-  if (frame_debug)
+  gdb_assert (fi->this_id.p == frame_id_status::NOT_COMPUTED);
+
+  unsigned int entry_generation = get_frame_cache_generation ();
+
+  try
     {
-      fprintf_unfiltered (gdb_stdlog, "-> ");
-      fprint_frame_id (gdb_stdlog, fi->this_id.value);
-      fprintf_unfiltered (gdb_stdlog, " }\n");
+      /* Mark this frame's id as "being computed.  */
+      fi->this_id.p = frame_id_status::COMPUTING;
+
+      frame_debug_printf ("fi=%d", fi->level);
+
+      /* Find the unwinder.  */
+      if (fi->unwind == NULL)
+       frame_unwind_find_by_frame (fi, &fi->prologue_cache);
+
+      /* Find THIS frame's ID.  */
+      /* Default to outermost if no ID is found.  */
+      fi->this_id.value = outer_frame_id;
+      fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
+      gdb_assert (frame_id_p (fi->this_id.value));
+
+      /* Mark this frame's id as "computed".  */
+      fi->this_id.p = frame_id_status::COMPUTED;
+
+      frame_debug_printf ("  -> %s", fi->this_id.value.to_string ().c_str ());
+    }
+  catch (const gdb_exception &ex)
+    {
+      /* On error, revert the frame id status to not computed.  If the frame
+        cache generation changed, the frame object doesn't exist anymore, so
+        don't touch it.  */
+      if (get_frame_cache_generation () == entry_generation)
+       fi->this_id.p = frame_id_status::NOT_COMPUTED;
+
+      throw;
     }
 }
 
@@ -562,15 +628,17 @@ compute_frame_id (struct frame_info *fi)
    frame.  */
 
 struct frame_id
-get_frame_id (struct frame_info *fi)
+get_frame_id (frame_info_ptr fi)
 {
   if (fi == NULL)
     return null_frame_id;
 
-  if (!fi->this_id.p)
-    {
-      int stashed;
+  /* It's always invalid to try to get a frame's id while it is being
+     computed.  */
+  gdb_assert (fi->this_id.p != frame_id_status::COMPUTING);
 
+  if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
+    {
       /* If we haven't computed the frame id yet, then it must be that
         this is the current frame.  Compute it now, and stash the
         result.  The IDs of other frames are computed as soon as
@@ -583,7 +651,7 @@ get_frame_id (struct frame_info *fi)
 
       /* Since this is the first frame in the chain, this should
         always succeed.  */
-      stashed = frame_stash_add (fi);
+      bool stashed = frame_stash_add (fi.get ());
       gdb_assert (stashed);
     }
 
@@ -591,15 +659,15 @@ get_frame_id (struct frame_info *fi)
 }
 
 struct frame_id
-get_stack_frame_id (struct frame_info *next_frame)
+get_stack_frame_id (frame_info_ptr next_frame)
 {
   return get_frame_id (skip_artificial_frames (next_frame));
 }
 
 struct frame_id
-frame_unwind_caller_id (struct frame_info *next_frame)
+frame_unwind_caller_id (frame_info_ptr next_frame)
 {
-  struct frame_info *this_frame;
+  frame_info_ptr this_frame;
 
   /* Use get_prev_frame_always, and not get_prev_frame.  The latter
      will truncate the frame chain, leading to this function
@@ -618,21 +686,20 @@ frame_unwind_caller_id (struct frame_info *next_frame)
 }
 
 const struct frame_id null_frame_id = { 0 }; /* All zeros.  */
-const struct frame_id sentinel_frame_id = { 0, 0, 0, FID_STACK_SENTINEL, 0, 1, 0 };
-const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_INVALID, 0, 1, 0 };
+const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_OUTER, 0, 1, 0 };
 
 struct frame_id
 frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
-                        CORE_ADDR special_addr)
+                       CORE_ADDR special_addr)
 {
   struct frame_id id = null_frame_id;
 
   id.stack_addr = stack_addr;
   id.stack_status = FID_STACK_VALID;
   id.code_addr = code_addr;
-  id.code_addr_p = 1;
+  id.code_addr_p = true;
   id.special_addr = special_addr;
-  id.special_addr_p = 1;
+  id.special_addr_p = true;
   return id;
 }
 
@@ -645,7 +712,7 @@ frame_id_build_unavailable_stack (CORE_ADDR code_addr)
 
   id.stack_status = FID_STACK_UNAVAILABLE;
   id.code_addr = code_addr;
-  id.code_addr_p = 1;
+  id.code_addr_p = true;
   return id;
 }
 
@@ -659,9 +726,9 @@ frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
 
   id.stack_status = FID_STACK_UNAVAILABLE;
   id.code_addr = code_addr;
-  id.code_addr_p = 1;
+  id.code_addr_p = true;
   id.special_addr = special_addr;
-  id.special_addr_p = 1;
+  id.special_addr_p = true;
   return id;
 }
 
@@ -673,7 +740,7 @@ frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
   id.stack_addr = stack_addr;
   id.stack_status = FID_STACK_VALID;
   id.code_addr = code_addr;
-  id.code_addr_p = 1;
+  id.code_addr_p = true;
   return id;
 }
 
@@ -687,79 +754,83 @@ frame_id_build_wild (CORE_ADDR stack_addr)
   return id;
 }
 
-int
-frame_id_p (struct frame_id l)
+/* See frame.h.  */
+
+frame_id
+frame_id_build_sentinel (CORE_ADDR stack_addr, CORE_ADDR code_addr)
 {
-  int p;
+  frame_id id = null_frame_id;
 
-  /* The frame is valid iff it has a valid stack address.  */
-  p = l.stack_status != FID_STACK_INVALID;
-  /* outer_frame_id is also valid.  */
-  if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
-    p = 1;
-  if (frame_debug)
+  id.stack_status = FID_STACK_SENTINEL;
+  id.special_addr_p = 1;
+
+  if (stack_addr != 0 || code_addr != 0)
     {
-      fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
-      fprint_frame_id (gdb_stdlog, l);
-      fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
+      /* The purpose of saving these in the sentinel frame ID is to be able to
+        differentiate the IDs of several sentinel frames that could exist
+        simultaneously in the frame cache.  */
+      id.stack_addr = stack_addr;
+      id.code_addr = code_addr;
+      id.code_addr_p = 1;
     }
+
+  return id;
+}
+
+bool
+frame_id_p (frame_id l)
+{
+  /* The frame is valid iff it has a valid stack address.  */
+  bool p = l.stack_status != FID_STACK_INVALID;
+
+  frame_debug_printf ("l=%s -> %d", l.to_string ().c_str (), p);
+
   return p;
 }
 
-int
-frame_id_artificial_p (struct frame_id l)
+bool
+frame_id_artificial_p (frame_id l)
 {
   if (!frame_id_p (l))
-    return 0;
+    return false;
 
-  return (l.artificial_depth != 0);
+  return l.artificial_depth != 0;
 }
 
-int
-frame_id_eq (struct frame_id l, struct frame_id r)
-{
-  int eq;
-
-  if (l.stack_status == FID_STACK_INVALID && l.special_addr_p
-      && r.stack_status == FID_STACK_INVALID && r.special_addr_p)
-    /* The outermost frame marker is equal to itself.  This is the
-       dodgy thing about outer_frame_id, since between execution steps
-       we might step into another function - from which we can't
-       unwind either.  More thought required to get rid of
-       outer_frame_id.  */
-    eq = 1;
-  else if (l.stack_status == FID_STACK_INVALID
-          || r.stack_status == FID_STACK_INVALID)
+bool
+frame_id::operator== (const frame_id &r) const
+{
+  bool eq;
+
+  if (stack_status == FID_STACK_INVALID
+      || r.stack_status == FID_STACK_INVALID)
     /* Like a NaN, if either ID is invalid, the result is false.
        Note that a frame ID is invalid iff it is the null frame ID.  */
-    eq = 0;
-  else if (l.stack_status != r.stack_status || l.stack_addr != r.stack_addr)
+    eq = false;
+  else if (stack_status != r.stack_status || stack_addr != r.stack_addr)
     /* If .stack addresses are different, the frames are different.  */
-    eq = 0;
-  else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
+    eq = false;
+  else if (code_addr_p && r.code_addr_p && code_addr != r.code_addr)
     /* An invalid code addr is a wild card.  If .code addresses are
        different, the frames are different.  */
-    eq = 0;
-  else if (l.special_addr_p && r.special_addr_p
-          && l.special_addr != r.special_addr)
+    eq = false;
+  else if (special_addr_p && r.special_addr_p
+          && special_addr != r.special_addr)
     /* An invalid special addr is a wild card (or unused).  Otherwise
        if special addresses are different, the frames are different.  */
-    eq = 0;
-  else if (l.artificial_depth != r.artificial_depth)
+    eq = false;
+  else if (artificial_depth != r.artificial_depth)
     /* If artificial depths are different, the frames must be different.  */
-    eq = 0;
+    eq = false;
+  else if (user_created_p != r.user_created_p)
+    eq = false;
   else
     /* Frames are equal.  */
-    eq = 1;
+    eq = true;
+
+  frame_debug_printf ("l=%s, r=%s -> %d",
+                     to_string ().c_str (), r.to_string ().c_str (), eq);
 
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
-      fprint_frame_id (gdb_stdlog, l);
-      fprintf_unfiltered (gdb_stdlog, ",r=");
-      fprint_frame_id (gdb_stdlog, r);
-      fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
-    }
   return eq;
 }
 
@@ -795,15 +866,15 @@ frame_id_eq (struct frame_id l, struct frame_id r)
    a stack overflow strategy that cause the handler to be run on a
    different stack.  */
 
-static int
+static bool
 frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
 {
-  int inner;
+  bool inner;
 
   if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
     /* Like NaN, any operation involving an invalid ID always fails.
        Likewise if either ID has an unavailable stack address.  */
-    inner = 0;
+    inner = false;
   else if (l.artificial_depth > r.artificial_depth
           && l.stack_addr == r.stack_addr
           && l.code_addr_p == r.code_addr_p
@@ -820,12 +891,12 @@ frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
 
       if (lb == NULL || rb == NULL)
        /* Something's gone wrong.  */
-       inner = 0;
+       inner = false;
       else
        /* This will return true if LB and RB are the same block, or
           if the block with the smaller depth lexically encloses the
           block with the greater depth.  */
-       inner = contained_in (lb, rb);
+       inner = rb->contains (lb);
     }
   else
     /* Only return non-zero when strictly inner than.  Note that, per
@@ -833,21 +904,18 @@ frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
        functions are not strictly inner than (same .stack but
        different .code and/or .special address).  */
     inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
-      fprint_frame_id (gdb_stdlog, l);
-      fprintf_unfiltered (gdb_stdlog, ",r=");
-      fprint_frame_id (gdb_stdlog, r);
-      fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
-    }
+
+  frame_debug_printf ("is l=%s inner than r=%s? %d",
+                     l.to_string ().c_str (), r.to_string ().c_str (),
+                     inner);
+
   return inner;
 }
 
-struct frame_info *
+frame_info_ptr
 frame_find_by_id (struct frame_id id)
 {
-  struct frame_info *frame, *prev_frame;
+  frame_info_ptr frame, prev_frame;
 
   /* ZERO denotes the null frame, let the caller decide what to do
      about it.  Should it instead return get_current_frame()?  */
@@ -855,8 +923,8 @@ frame_find_by_id (struct frame_id id)
     return NULL;
 
   /* Check for the sentinel frame.  */
-  if (frame_id_eq (id, sentinel_frame_id))
-    return sentinel_frame;
+  if (id == frame_id_build_sentinel (0, 0))
+    return frame_info_ptr (sentinel_frame);
 
   /* Try using the frame stash first.  Finding it there removes the need
      to perform the search by looping over all frames, which can be very
@@ -864,7 +932,7 @@ frame_find_by_id (struct frame_id id)
      and get_prev_frame performs a series of checks that are relatively
      expensive).  This optimization is particularly useful when this function
      is called from another function (such as value_fetch_lazy, case
-     VALUE_LVAL (val) == lval_register) which already loops over all frames,
+     val->lval () == lval_register) which already loops over all frames,
      making the overall behavior O(n^2).  */
   frame = frame_stash_find (id);
   if (frame)
@@ -874,7 +942,7 @@ frame_find_by_id (struct frame_id id)
     {
       struct frame_id self = get_frame_id (frame);
 
-      if (frame_id_eq (id, self))
+      if (id == self)
        /* An exact match.  */
        return frame;
 
@@ -897,13 +965,13 @@ frame_find_by_id (struct frame_id id)
 }
 
 static CORE_ADDR
-frame_unwind_pc (struct frame_info *this_frame)
+frame_unwind_pc (frame_info_ptr this_frame)
 {
   if (this_frame->prev_pc.status == CC_UNKNOWN)
     {
       struct gdbarch *prev_gdbarch;
       CORE_ADDR pc = 0;
-      int pc_p = 0;
+      bool pc_p = false;
 
       /* The right way.  The `pure' way.  The one true way.  This
         method depends solely on the register-unwind code to
@@ -926,7 +994,7 @@ frame_unwind_pc (struct frame_info *this_frame)
       try
        {
          pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
-         pc_p = 1;
+         pc_p = true;
        }
       catch (const gdb_exception_error &ex)
        {
@@ -934,21 +1002,15 @@ frame_unwind_pc (struct frame_info *this_frame)
            {
              this_frame->prev_pc.status = CC_UNAVAILABLE;
 
-             if (frame_debug)
-               fprintf_unfiltered (gdb_stdlog,
-                                   "{ frame_unwind_pc (this_frame=%d)"
-                                   " -> <unavailable> }\n",
-                                   this_frame->level);
+             frame_debug_printf ("this_frame=%d -> <unavailable>",
+                                 this_frame->level);
            }
          else if (ex.error == OPTIMIZED_OUT_ERROR)
            {
              this_frame->prev_pc.status = CC_NOT_SAVED;
 
-             if (frame_debug)
-               fprintf_unfiltered (gdb_stdlog,
-                                   "{ frame_unwind_pc (this_frame=%d)"
-                                   " -> <not saved> }\n",
-                                   this_frame->level);
+             frame_debug_printf ("this_frame=%d -> <not saved>",
+                                 this_frame->level);
            }
          else
            throw;
@@ -958,12 +1020,10 @@ frame_unwind_pc (struct frame_info *this_frame)
        {
          this_frame->prev_pc.value = pc;
          this_frame->prev_pc.status = CC_VALUE;
-         if (frame_debug)
-           fprintf_unfiltered (gdb_stdlog,
-                               "{ frame_unwind_pc (this_frame=%d) "
-                               "-> %s }\n",
-                               this_frame->level,
-                               hex_string (this_frame->prev_pc.value));
+
+         frame_debug_printf ("this_frame=%d -> %s",
+                             this_frame->level,
+                             hex_string (this_frame->prev_pc.value));
        }
     }
 
@@ -974,13 +1034,12 @@ frame_unwind_pc (struct frame_info *this_frame)
   else if (this_frame->prev_pc.status == CC_NOT_SAVED)
     throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
   else
-    internal_error (__FILE__, __LINE__,
-                   "unexpected prev_pc status: %d",
+    internal_error ("unexpected prev_pc status: %d",
                    (int) this_frame->prev_pc.status);
 }
 
 CORE_ADDR
-frame_unwind_caller_pc (struct frame_info *this_frame)
+frame_unwind_caller_pc (frame_info_ptr this_frame)
 {
   this_frame = skip_artificial_frames (this_frame);
 
@@ -992,52 +1051,51 @@ frame_unwind_caller_pc (struct frame_info *this_frame)
   return frame_unwind_pc (this_frame);
 }
 
-int
-get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
+bool
+get_frame_func_if_available (frame_info_ptr this_frame, CORE_ADDR *pc)
 {
-  struct frame_info *next_frame = this_frame->next;
+  frame_info *next_frame = this_frame->next;
 
-  if (!next_frame->prev_func.p)
+  if (next_frame->prev_func.status == CC_UNKNOWN)
     {
       CORE_ADDR addr_in_block;
 
       /* Make certain that this, and not the adjacent, function is
-         found.  */
+        found.  */
       if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
        {
-         next_frame->prev_func.p = -1;
-         if (frame_debug)
-           fprintf_unfiltered (gdb_stdlog,
-                               "{ get_frame_func (this_frame=%d)"
-                               " -> unavailable }\n",
-                               this_frame->level);
+         next_frame->prev_func.status = CC_UNAVAILABLE;
+
+         frame_debug_printf ("this_frame=%d -> unavailable",
+                             this_frame->level);
        }
       else
        {
-         next_frame->prev_func.p = 1;
+         next_frame->prev_func.status = CC_VALUE;
          next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
-         if (frame_debug)
-           fprintf_unfiltered (gdb_stdlog,
-                               "{ get_frame_func (this_frame=%d) -> %s }\n",
-                               this_frame->level,
-                               hex_string (next_frame->prev_func.addr));
+
+         frame_debug_printf ("this_frame=%d -> %s",
+                             this_frame->level,
+                             hex_string (next_frame->prev_func.addr));
        }
     }
 
-  if (next_frame->prev_func.p < 0)
+  if (next_frame->prev_func.status == CC_UNAVAILABLE)
     {
       *pc = -1;
-      return 0;
+      return false;
     }
   else
     {
+      gdb_assert (next_frame->prev_func.status == CC_VALUE);
+
       *pc = next_frame->prev_func.addr;
-      return 1;
+      return true;
     }
 }
 
 CORE_ADDR
-get_frame_func (struct frame_info *this_frame)
+get_frame_func (frame_info_ptr this_frame)
 {
   CORE_ADDR pc;
 
@@ -1048,7 +1106,7 @@ get_frame_func (struct frame_info *this_frame)
 }
 
 std::unique_ptr<readonly_detached_regcache>
-frame_save_as_regcache (struct frame_info *this_frame)
+frame_save_as_regcache (frame_info_ptr this_frame)
 {
   auto cooked_read = [this_frame] (int regnum, gdb_byte *buf)
     {
@@ -1065,9 +1123,9 @@ frame_save_as_regcache (struct frame_info *this_frame)
 }
 
 void
-frame_pop (struct frame_info *this_frame)
+frame_pop (frame_info_ptr this_frame)
 {
-  struct frame_info *prev_frame;
+  frame_info_ptr prev_frame;
 
   if (get_frame_type (this_frame) == DUMMY_FRAME)
     {
@@ -1114,7 +1172,7 @@ frame_pop (struct frame_info *this_frame)
 }
 
 void
-frame_register_unwind (frame_info *next_frame, int regnum,
+frame_register_unwind (frame_info_ptr next_frame, int regnum,
                       int *optimizedp, int *unavailablep,
                       enum lval_type *lvalp, CORE_ADDR *addrp,
                       int *realnump, gdb_byte *bufferp)
@@ -1133,10 +1191,10 @@ frame_register_unwind (frame_info *next_frame, int regnum,
 
   gdb_assert (value != NULL);
 
-  *optimizedp = value_optimized_out (value);
-  *unavailablep = !value_entirely_available (value);
-  *lvalp = VALUE_LVAL (value);
-  *addrp = value_address (value);
+  *optimizedp = value->optimized_out ();
+  *unavailablep = !value->entirely_available ();
+  *lvalp = value->lval ();
+  *addrp = value->address ();
   if (*lvalp == lval_register)
     *realnump = VALUE_REGNUM (value);
   else
@@ -1145,10 +1203,10 @@ frame_register_unwind (frame_info *next_frame, int regnum,
   if (bufferp)
     {
       if (!*optimizedp && !*unavailablep)
-       memcpy (bufferp, value_contents_all (value),
-               TYPE_LENGTH (value_type (value)));
+       memcpy (bufferp, value->contents_all ().data (),
+               value->type ()->length ());
       else
-       memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
+       memset (bufferp, 0, value->type ()->length ());
     }
 
   /* Dispose of the new value.  This prevents watchpoints from
@@ -1156,8 +1214,13 @@ frame_register_unwind (frame_info *next_frame, int regnum,
   release_value (value);
 }
 
-void
-frame_register (struct frame_info *frame, int regnum,
+/* Get the value of the register that belongs to this FRAME.  This
+   function is a wrapper to the call sequence ``frame_register_unwind
+   (get_next_frame (FRAME))''.  As per frame_register_unwind(), if
+   VALUEP is NULL, the registers value is not fetched/computed.  */
+
+static void
+frame_register (frame_info_ptr frame, int regnum,
                int *optimizedp, int *unavailablep, enum lval_type *lvalp,
                CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
 {
@@ -1172,12 +1235,12 @@ frame_register (struct frame_info *frame, int regnum,
   /* Obtain the register value by unwinding the register from the next
      (more inner frame).  */
   gdb_assert (frame != NULL && frame->next != NULL);
-  frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
-                        lvalp, addrp, realnump, bufferp);
+  frame_register_unwind (frame_info_ptr (frame->next), regnum, optimizedp,
+                        unavailablep, lvalp, addrp, realnump, bufferp);
 }
 
 void
-frame_unwind_register (frame_info *next_frame, int regnum, gdb_byte *buf)
+frame_unwind_register (frame_info_ptr next_frame, int regnum, gdb_byte *buf)
 {
   int optimized;
   int unavailable;
@@ -1197,122 +1260,115 @@ frame_unwind_register (frame_info *next_frame, int regnum, gdb_byte *buf)
 }
 
 void
-get_frame_register (struct frame_info *frame,
+get_frame_register (frame_info_ptr frame,
                    int regnum, gdb_byte *buf)
 {
-  frame_unwind_register (frame->next, regnum, buf);
+  frame_unwind_register (frame_info_ptr (frame->next), regnum, buf);
 }
 
 struct value *
-frame_unwind_register_value (frame_info *next_frame, int regnum)
+frame_unwind_register_value (frame_info_ptr next_frame, int regnum)
 {
-  struct gdbarch *gdbarch;
-  struct value *value;
+  FRAME_SCOPED_DEBUG_ENTER_EXIT;
 
   gdb_assert (next_frame != NULL);
-  gdbarch = frame_unwind_arch (next_frame);
-
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog,
-                         "{ frame_unwind_register_value "
-                         "(frame=%d,regnum=%d(%s),...) ",
-                         next_frame->level, regnum,
-                         user_reg_map_regnum_to_name (gdbarch, regnum));
-    }
+  gdbarch *gdbarch = frame_unwind_arch (next_frame);
+  frame_debug_printf ("frame=%d, regnum=%d(%s)",
+                     next_frame->level, regnum,
+                     user_reg_map_regnum_to_name (gdbarch, regnum));
 
   /* Find the unwinder.  */
   if (next_frame->unwind == NULL)
     frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
 
   /* Ask this frame to unwind its register.  */
-  value = next_frame->unwind->prev_register (next_frame,
-                                            &next_frame->prologue_cache,
-                                            regnum);
+  value *value = next_frame->unwind->prev_register (next_frame,
+                                                   &next_frame->prologue_cache,
+                                                   regnum);
 
   if (frame_debug)
     {
-      fprintf_unfiltered (gdb_stdlog, "->");
-      if (value_optimized_out (value))
+      string_file debug_file;
+
+      gdb_printf (&debug_file, "  ->");
+      if (value->optimized_out ())
        {
-         fprintf_unfiltered (gdb_stdlog, " ");
-         val_print_optimized_out (value, gdb_stdlog);
+         gdb_printf (&debug_file, " ");
+         val_print_not_saved (&debug_file);
        }
       else
        {
-         if (VALUE_LVAL (value) == lval_register)
-           fprintf_unfiltered (gdb_stdlog, " register=%d",
-                               VALUE_REGNUM (value));
-         else if (VALUE_LVAL (value) == lval_memory)
-           fprintf_unfiltered (gdb_stdlog, " address=%s",
-                               paddress (gdbarch,
-                                         value_address (value)));
+         if (value->lval () == lval_register)
+           gdb_printf (&debug_file, " register=%d",
+                       VALUE_REGNUM (value));
+         else if (value->lval () == lval_memory)
+           gdb_printf (&debug_file, " address=%s",
+                       paddress (gdbarch,
+                                 value->address ()));
          else
-           fprintf_unfiltered (gdb_stdlog, " computed");
+           gdb_printf (&debug_file, " computed");
 
-         if (value_lazy (value))
-           fprintf_unfiltered (gdb_stdlog, " lazy");
+         if (value->lazy ())
+           gdb_printf (&debug_file, " lazy");
          else
            {
              int i;
-             const gdb_byte *buf = value_contents (value);
+             gdb::array_view<const gdb_byte> buf = value->contents ();
 
-             fprintf_unfiltered (gdb_stdlog, " bytes=");
-             fprintf_unfiltered (gdb_stdlog, "[");
+             gdb_printf (&debug_file, " bytes=");
+             gdb_printf (&debug_file, "[");
              for (i = 0; i < register_size (gdbarch, regnum); i++)
-               fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
-             fprintf_unfiltered (gdb_stdlog, "]");
+               gdb_printf (&debug_file, "%02x", buf[i]);
+             gdb_printf (&debug_file, "]");
            }
        }
 
-      fprintf_unfiltered (gdb_stdlog, " }\n");
+      frame_debug_printf ("%s", debug_file.c_str ());
     }
 
   return value;
 }
 
 struct value *
-get_frame_register_value (struct frame_info *frame, int regnum)
+get_frame_register_value (frame_info_ptr frame, int regnum)
 {
-  return frame_unwind_register_value (frame->next, regnum);
+  return frame_unwind_register_value (frame_info_ptr (frame->next), regnum);
 }
 
 LONGEST
-frame_unwind_register_signed (frame_info *next_frame, int regnum)
+frame_unwind_register_signed (frame_info_ptr next_frame, int regnum)
 {
   struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
-  int size = register_size (gdbarch, regnum);
   struct value *value = frame_unwind_register_value (next_frame, regnum);
 
   gdb_assert (value != NULL);
 
-  if (value_optimized_out (value))
+  if (value->optimized_out ())
     {
       throw_error (OPTIMIZED_OUT_ERROR,
                   _("Register %d was not saved"), regnum);
     }
-  if (!value_entirely_available (value))
+  if (!value->entirely_available ())
     {
       throw_error (NOT_AVAILABLE_ERROR,
                   _("Register %d is not available"), regnum);
     }
 
-  LONGEST r = extract_signed_integer (value_contents_all (value), size,
-                                     byte_order);
+  LONGEST r = extract_signed_integer (value->contents_all (), byte_order);
 
   release_value (value);
   return r;
 }
 
 LONGEST
-get_frame_register_signed (struct frame_info *frame, int regnum)
+get_frame_register_signed (frame_info_ptr frame, int regnum)
 {
-  return frame_unwind_register_signed (frame->next, regnum);
+  return frame_unwind_register_signed (frame_info_ptr (frame->next), regnum);
 }
 
 ULONGEST
-frame_unwind_register_unsigned (frame_info *next_frame, int regnum)
+frame_unwind_register_unsigned (frame_info_ptr next_frame, int regnum)
 {
   struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
@@ -1321,52 +1377,53 @@ frame_unwind_register_unsigned (frame_info *next_frame, int regnum)
 
   gdb_assert (value != NULL);
 
-  if (value_optimized_out (value))
+  if (value->optimized_out ())
     {
       throw_error (OPTIMIZED_OUT_ERROR,
                   _("Register %d was not saved"), regnum);
     }
-  if (!value_entirely_available (value))
+  if (!value->entirely_available ())
     {
       throw_error (NOT_AVAILABLE_ERROR,
                   _("Register %d is not available"), regnum);
     }
 
-  ULONGEST r = extract_unsigned_integer (value_contents_all (value), size,
-                                        byte_order);
+  ULONGEST r = extract_unsigned_integer (value->contents_all ().data (),
+                                        size, byte_order);
 
   release_value (value);
   return r;
 }
 
 ULONGEST
-get_frame_register_unsigned (struct frame_info *frame, int regnum)
+get_frame_register_unsigned (frame_info_ptr frame, int regnum)
 {
-  return frame_unwind_register_unsigned (frame->next, regnum);
+  return frame_unwind_register_unsigned (frame_info_ptr (frame->next), regnum);
 }
 
-int
-read_frame_register_unsigned (struct frame_info *frame, int regnum,
+bool
+read_frame_register_unsigned (frame_info_ptr frame, int regnum,
                              ULONGEST *val)
 {
   struct value *regval = get_frame_register_value (frame, regnum);
 
-  if (!value_optimized_out (regval)
-      && value_entirely_available (regval))
+  if (!regval->optimized_out ()
+      && regval->entirely_available ())
     {
       struct gdbarch *gdbarch = get_frame_arch (frame);
       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
       int size = register_size (gdbarch, VALUE_REGNUM (regval));
 
-      *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
-      return 1;
+      *val = extract_unsigned_integer (regval->contents ().data (), size,
+                                      byte_order);
+      return true;
     }
 
-  return 0;
+  return false;
 }
 
 void
-put_frame_register (struct frame_info *frame, int regnum,
+put_frame_register (frame_info_ptr frame, int regnum,
                    const gdb_byte *buf)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
@@ -1403,9 +1460,9 @@ put_frame_register (struct frame_info *frame, int regnum,
 
    Returns 0 if the register value could not be found.  */
 
-int
-deprecated_frame_register_read (struct frame_info *frame, int regnum,
-                    gdb_byte *myaddr)
+bool
+deprecated_frame_register_read (frame_info_ptr frame, int regnum,
+                               gdb_byte *myaddr)
 {
   int optimized;
   int unavailable;
@@ -1419,9 +1476,10 @@ deprecated_frame_register_read (struct frame_info *frame, int regnum,
   return !optimized && !unavailable;
 }
 
-int
-get_frame_register_bytes (struct frame_info *frame, int regnum,
-                         CORE_ADDR offset, int len, gdb_byte *myaddr,
+bool
+get_frame_register_bytes (frame_info_ptr frame, int regnum,
+                         CORE_ADDR offset,
+                         gdb::array_view<gdb_byte> buffer,
                          int *optimizedp, int *unavailablep)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
@@ -1448,6 +1506,8 @@ get_frame_register_bytes (struct frame_info *frame, int regnum,
        break;  /* This register is not available on this architecture.  */
       maxsize += thissize;
     }
+
+  int len = buffer.size ();
   if (len > maxsize)
     error (_("Bad debug information detected: "
             "Attempt to read %d bytes from registers."), len);
@@ -1460,6 +1520,8 @@ get_frame_register_bytes (struct frame_info *frame, int regnum,
       if (curr_len > len)
        curr_len = len;
 
+      gdb_byte *myaddr = buffer.data ();
+
       if (curr_len == register_size (gdbarch, regnum))
        {
          enum lval_type lval;
@@ -1469,22 +1531,25 @@ get_frame_register_bytes (struct frame_info *frame, int regnum,
          frame_register (frame, regnum, optimizedp, unavailablep,
                          &lval, &addr, &realnum, myaddr);
          if (*optimizedp || *unavailablep)
-           return 0;
+           return false;
        }
       else
        {
-         struct value *value = frame_unwind_register_value (frame->next,
-                                                            regnum);
+         struct value *value
+           = frame_unwind_register_value (frame_info_ptr (frame->next),
+                                          regnum);
          gdb_assert (value != NULL);
-         *optimizedp = value_optimized_out (value);
-         *unavailablep = !value_entirely_available (value);
+         *optimizedp = value->optimized_out ();
+         *unavailablep = !value->entirely_available ();
 
          if (*optimizedp || *unavailablep)
            {
              release_value (value);
-             return 0;
+             return false;
            }
-         memcpy (myaddr, value_contents_all (value) + offset, curr_len);
+
+         memcpy (myaddr, value->contents_all ().data () + offset,
+                 curr_len);
          release_value (value);
        }
 
@@ -1496,12 +1561,14 @@ get_frame_register_bytes (struct frame_info *frame, int regnum,
 
   *optimizedp = 0;
   *unavailablep = 0;
-  return 1;
+
+  return true;
 }
 
 void
-put_frame_register_bytes (struct frame_info *frame, int regnum,
-                         CORE_ADDR offset, int len, const gdb_byte *myaddr)
+put_frame_register_bytes (frame_info_ptr frame, int regnum,
+                         CORE_ADDR offset,
+                         gdb::array_view<const gdb_byte> buffer)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
 
@@ -1512,6 +1579,7 @@ put_frame_register_bytes (struct frame_info *frame, int regnum,
       regnum++;
     }
 
+  int len = buffer.size ();
   /* Copy the data.  */
   while (len > 0)
     {
@@ -1520,19 +1588,22 @@ put_frame_register_bytes (struct frame_info *frame, int regnum,
       if (curr_len > len)
        curr_len = len;
 
+      const gdb_byte *myaddr = buffer.data ();
       if (curr_len == register_size (gdbarch, regnum))
        {
          put_frame_register (frame, regnum, myaddr);
        }
       else
        {
-         struct value *value = frame_unwind_register_value (frame->next,
-                                                            regnum);
+         struct value *value
+           = frame_unwind_register_value (frame_info_ptr (frame->next),
+                                          regnum);
          gdb_assert (value != NULL);
 
-         memcpy ((char *) value_contents_writeable (value) + offset, myaddr,
-                 curr_len);
-         put_frame_register (frame, regnum, value_contents_raw (value));
+         memcpy ((char *) value->contents_writeable ().data () + offset,
+                 myaddr, curr_len);
+         put_frame_register (frame, regnum,
+                             value->contents_raw ().data ());
          release_value (value);
        }
 
@@ -1543,12 +1614,16 @@ put_frame_register_bytes (struct frame_info *frame, int regnum,
     }
 }
 
-/* Create a sentinel frame.  */
+/* Create a sentinel frame.
+
+   See frame_id_build_sentinel for the description of STACK_ADDR and
+   CODE_ADDR.  */
 
-static struct frame_info *
-create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
+static frame_info_ptr
+create_sentinel_frame (struct program_space *pspace, struct regcache *regcache,
+                      CORE_ADDR stack_addr, CORE_ADDR code_addr)
 {
-  struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
+  frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
 
   frame->level = -1;
   frame->pspace = pspace;
@@ -1563,15 +1638,15 @@ create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
      (the unwound PC is the same as the pc), so make it so.  */
   frame->next = frame;
   /* The sentinel frame has a special ID.  */
-  frame->this_id.p = 1;
-  frame->this_id.value = sentinel_frame_id;
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
-      fprint_frame (gdb_stdlog, frame);
-      fprintf_unfiltered (gdb_stdlog, " }\n");
-    }
-  return frame;
+  frame->this_id.p = frame_id_status::COMPUTED;
+  frame->this_id.value = frame_id_build_sentinel (stack_addr, code_addr);
+
+  bool added = frame_stash_add (frame);
+  gdb_assert (added);
+
+  frame_debug_printf ("  -> %s", frame->to_string ().c_str ());
+
+  return frame_info_ptr (frame);
 }
 
 /* Cache for frame addresses already read by gdb.  Valid only while
@@ -1589,23 +1664,23 @@ frame_obstack_zalloc (unsigned long size)
   return data;
 }
 
-static struct frame_info *get_prev_frame_always_1 (struct frame_info *this_frame);
+static frame_info_ptr get_prev_frame_always_1 (frame_info_ptr this_frame);
 
-struct frame_info *
+frame_info_ptr
 get_current_frame (void)
 {
-  struct frame_info *current_frame;
+  frame_info_ptr current_frame;
 
   /* First check, and report, the lack of registers.  Having GDB
      report "No stack!" or "No memory" when the target doesn't even
      have registers is very confusing.  Besides, "printcmd.exp"
      explicitly checks that ``print $pc'' with no registers prints "No
      registers".  */
-  if (!target_has_registers)
+  if (!target_has_registers ())
     error (_("No registers."));
-  if (!target_has_stack)
+  if (!target_has_stack ())
     error (_("No stack."));
-  if (!target_has_memory)
+  if (!target_has_memory ())
     error (_("No memory."));
   /* Traceframes are effectively a substitute for the live inferior.  */
   if (get_traceframe_number () < 0)
@@ -1613,7 +1688,8 @@ get_current_frame (void)
 
   if (sentinel_frame == NULL)
     sentinel_frame =
-      create_sentinel_frame (current_program_space, get_current_regcache ());
+      create_sentinel_frame (current_program_space, get_current_regcache (),
+                            0, 0).get ();
 
   /* Set the current frame before computing the frame id, to avoid
      recursion inside compute_frame_id, in case the frame's
@@ -1626,77 +1702,208 @@ get_current_frame (void)
      want to leave with the current frame created and linked in --
      we should never end up with the sentinel frame as outermost
      frame.  */
-  current_frame = get_prev_frame_always_1 (sentinel_frame);
+  current_frame = get_prev_frame_always_1 (frame_info_ptr (sentinel_frame));
   gdb_assert (current_frame != NULL);
 
   return current_frame;
 }
 
 /* The "selected" stack frame is used by default for local and arg
-   access.  May be zero, for no selected frame.  */
+   access.
+
+   The "single source of truth" for the selected frame is the
+   SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL pair.
+
+   Frame IDs can be saved/restored across reinitializing the frame
+   cache, while frame_info pointers can't (frame_info objects are
+   invalidated).  If we know the corresponding frame_info object, it
+   is cached in SELECTED_FRAME.
+
+   If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
+   and the target has stack and is stopped, the selected frame is the
+   current (innermost) target frame.  SELECTED_FRAME_ID is never the ID
+   of the current (innermost) target frame.  SELECTED_FRAME_LEVEL may
+   only be 0 if the selected frame is a user-created one (created and
+   selected through the "select-frame view" command), in which case
+   SELECTED_FRAME_ID is the frame id derived from the user-provided
+   addresses.
+
+   If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
+   and the target has no stack or is executing, then there's no
+   selected frame.  */
+static frame_id selected_frame_id = null_frame_id;
+static int selected_frame_level = -1;
+
+/* See frame.h.  This definition should come before any definition of a static
+   frame_info_ptr, to ensure that frame_list is destroyed after any static
+   frame_info_ptr.  This is necessary because the destructor of frame_info_ptr
+   uses frame_list.  */
+
+intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
+
+/* The cached frame_info object pointing to the selected frame.
+   Looked up on demand by get_selected_frame.  */
+static frame_info_ptr selected_frame;
 
-static struct frame_info *selected_frame;
+/* See frame.h.  */
 
-int
-has_stack_frames (void)
+void
+save_selected_frame (frame_id *frame_id, int *frame_level)
+  noexcept
 {
-  if (!target_has_registers || !target_has_stack || !target_has_memory)
-    return 0;
+  *frame_id = selected_frame_id;
+  *frame_level = selected_frame_level;
+}
+
+/* See frame.h.  */
+
+void
+restore_selected_frame (frame_id frame_id, int frame_level)
+  noexcept
+{
+  /* Unless it is a user-created frame, save_selected_frame never returns
+     level == 0, so we shouldn't see it here either.  */
+  gdb_assert (frame_level != 0 || frame_id.user_created_p);
+
+  /* FRAME_ID can be null_frame_id only IFF frame_level is -1.  */
+  gdb_assert ((frame_level == -1 && !frame_id_p (frame_id))
+             || (frame_level != -1 && frame_id_p (frame_id)));
+
+  selected_frame_id = frame_id;
+  selected_frame_level = frame_level;
+
+  /* Will be looked up later by get_selected_frame.  */
+  selected_frame = nullptr;
+}
+
+/* Lookup the frame_info object for the selected frame FRAME_ID /
+   FRAME_LEVEL and cache the result.
+
+   If FRAME_LEVEL > 0 and the originally selected frame isn't found,
+   warn and select the innermost (current) frame.  */
+
+static void
+lookup_selected_frame (struct frame_id a_frame_id, int frame_level)
+{
+  frame_info_ptr frame = NULL;
+  int count;
+
+  /* This either means there was no selected frame, or the selected
+     frame was the current frame.  In either case, select the current
+     frame.  */
+  if (frame_level == -1)
+    {
+      select_frame (get_current_frame ());
+      return;
+    }
+
+  /* This means the selected frame was a user-created one.  Create a new one
+     using the user-provided addresses, which happen to be in the frame id.  */
+  if (frame_level == 0)
+    {
+      gdb_assert (a_frame_id.user_created_p);
+      select_frame (create_new_frame (a_frame_id));
+      return;
+    }
+
+  /* select_frame never saves 0 in SELECTED_FRAME_LEVEL, so we
+     shouldn't see it here.  */
+  gdb_assert (frame_level > 0);
+
+  /* Restore by level first, check if the frame id is the same as
+     expected.  If that fails, try restoring by frame id.  If that
+     fails, nothing to do, just warn the user.  */
+
+  count = frame_level;
+  frame = find_relative_frame (get_current_frame (), &count);
+  if (count == 0
+      && frame != NULL
+      /* The frame ids must match - either both valid or both
+        outer_frame_id.  The latter case is not failsafe, but since
+        it's highly unlikely the search by level finds the wrong
+        frame, it's 99.9(9)% of the time (for all practical purposes)
+        safe.  */
+      && get_frame_id (frame) == a_frame_id)
+    {
+      /* Cool, all is fine.  */
+      select_frame (frame);
+      return;
+    }
+
+  frame = frame_find_by_id (a_frame_id);
+  if (frame != NULL)
+    {
+      /* Cool, refound it.  */
+      select_frame (frame);
+      return;
+    }
+
+  /* Nothing else to do, the frame layout really changed.  Select the
+     innermost stack frame.  */
+  select_frame (get_current_frame ());
+
+  /* Warn the user.  */
+  if (frame_level > 0 && !current_uiout->is_mi_like_p ())
+    {
+      warning (_("Couldn't restore frame #%d in "
+                "current thread.  Bottom (innermost) frame selected:"),
+              frame_level);
+      /* For MI, we should probably have a notification about current
+        frame change.  But this error is not very likely, so don't
+        bother for now.  */
+      print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+    }
+}
+
+bool
+has_stack_frames ()
+{
+  if (!target_has_registers () || !target_has_stack ()
+      || !target_has_memory ())
+    return false;
 
   /* Traceframes are effectively a substitute for the live inferior.  */
   if (get_traceframe_number () < 0)
     {
       /* No current inferior, no frame.  */
       if (inferior_ptid == null_ptid)
-       return 0;
+       return false;
 
       thread_info *tp = inferior_thread ();
       /* Don't try to read from a dead thread.  */
       if (tp->state == THREAD_EXITED)
-       return 0;
+       return false;
 
       /* ... or from a spinning thread.  */
-      if (tp->executing)
-       return 0;
+      if (tp->executing ())
+       return false;
     }
 
-  return 1;
+  return true;
 }
 
-/* Return the selected frame.  Always non-NULL (unless there isn't an
-   inferior sufficient for creating a frame) in which case an error is
-   thrown.  */
+/* See frame.h.  */
 
-struct frame_info *
+frame_info_ptr
 get_selected_frame (const char *message)
 {
   if (selected_frame == NULL)
     {
       if (message != NULL && !has_stack_frames ())
        error (("%s"), message);
-      /* Hey!  Don't trust this.  It should really be re-finding the
-        last selected frame of the currently selected thread.  This,
-        though, is better than nothing.  */
-      select_frame (get_current_frame ());
+
+      lookup_selected_frame (selected_frame_id, selected_frame_level);
     }
   /* There is always a frame.  */
   gdb_assert (selected_frame != NULL);
   return selected_frame;
 }
 
-/* If there is a selected frame, return it.  Otherwise, return NULL.  */
-
-struct frame_info *
-get_selected_frame_if_set (void)
-{
-  return selected_frame;
-}
-
 /* This is a variant of get_selected_frame() which can be called when
    the inferior does not have a frame; in that case it will return
    NULL instead of calling error().  */
 
-struct frame_info *
+frame_info_ptr
 deprecated_safe_get_selected_frame (void)
 {
   if (!has_stack_frames ())
@@ -1704,12 +1911,57 @@ deprecated_safe_get_selected_frame (void)
   return get_selected_frame (NULL);
 }
 
-/* Select frame FI (or NULL - to invalidate the current frame).  */
+/* Invalidate the selected frame.  */
+
+static void
+invalidate_selected_frame ()
+{
+  selected_frame = nullptr;
+  selected_frame_level = -1;
+  selected_frame_id = null_frame_id;
+}
+
+/* See frame.h.  */
 
 void
-select_frame (struct frame_info *fi)
+select_frame (frame_info_ptr fi)
 {
+  gdb_assert (fi != nullptr);
+
   selected_frame = fi;
+  selected_frame_level = frame_relative_level (fi);
+
+  /* If the frame is a user-created one, save its level and frame id just like
+     any other non-level-0 frame.  */
+  if (selected_frame_level == 0 && !fi->this_id.value.user_created_p)
+    {
+      /* Treat the current frame especially -- we want to always
+        save/restore it without warning, even if the frame ID changes
+        (see lookup_selected_frame).  E.g.:
+
+         // The current frame is selected, the target had just stopped.
+         {
+           scoped_restore_selected_frame restore_frame;
+           some_operation_that_changes_the_stack ();
+         }
+         // scoped_restore_selected_frame's dtor runs, but the
+         // original frame_id can't be found.  No matter whether it
+         // is found or not, we still end up with the now-current
+         // frame selected.  Warning in lookup_selected_frame in this
+         // case seems pointless.
+
+        Also get_frame_id may access the target's registers/memory,
+        and thus skipping get_frame_id optimizes the common case.
+
+        Saving the selected frame this way makes get_selected_frame
+        and restore_current_frame return/re-select whatever frame is
+        the innermost (current) then.  */
+      selected_frame_level = -1;
+      selected_frame_id = null_frame_id;
+    }
+  else
+    selected_frame_id = get_frame_id (fi);
+
   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
      frame is being invalidated.  */
 
@@ -1740,10 +1992,10 @@ select_frame (struct frame_info *fi)
          struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
 
          if (cust != NULL
-             && compunit_language (cust) != current_language->la_language
-             && compunit_language (cust) != language_unknown
+             && cust->language () != current_language->la_language
+             && cust->language () != language_unknown
              && language_mode == language_mode_auto)
-           set_language (compunit_language (cust));
+           set_language (cust->language ());
        }
     }
 }
@@ -1751,28 +2003,33 @@ select_frame (struct frame_info *fi)
 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
    Always returns a non-NULL value.  */
 
-struct frame_info *
-create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
+static frame_info_ptr
+create_new_frame (frame_id id)
 {
-  struct frame_info *fi;
+  gdb_assert (id.user_created_p);
+  gdb_assert (id.stack_status == frame_id_stack_status::FID_STACK_VALID);
+  gdb_assert (id.code_addr_p);
 
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog,
-                         "{ create_new_frame (addr=%s, pc=%s) ",
-                         hex_string (addr), hex_string (pc));
-    }
+  frame_debug_printf ("stack_addr=%s, core_addr=%s",
+                     hex_string (id.stack_addr), hex_string (id.code_addr));
 
-  fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
+  /* Avoid creating duplicate frames, search for an existing frame with that id
+     in the stash.  */
+  frame_info_ptr frame = frame_stash_find (id);
+  if (frame != nullptr)
+    return frame;
+
+  frame_info *fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
 
   fi->next = create_sentinel_frame (current_program_space,
-                                   get_current_regcache ());
+                                   get_current_regcache (),
+                                   id.stack_addr, id.code_addr).get ();
 
   /* Set/update this frame's cached PC value, found in the next frame.
      Do this before looking for this frame's unwinder.  A sniffer is
      very likely to read this, and the corresponding unwinder is
      entitled to rely that the PC doesn't magically change.  */
-  fi->next->prev_pc.value = pc;
+  fi->next->prev_pc.value = id.code_addr;
   fi->next->prev_pc.status = CC_VALUE;
 
   /* We currently assume that frame chain's can't cross spaces.  */
@@ -1781,30 +2038,37 @@ create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
 
   /* Select/initialize both the unwind function and the frame's type
      based on the PC.  */
-  frame_unwind_find_by_frame (fi, &fi->prologue_cache);
+  frame_unwind_find_by_frame (frame_info_ptr (fi), &fi->prologue_cache);
 
-  fi->this_id.p = 1;
-  fi->this_id.value = frame_id_build (addr, pc);
+  fi->this_id.p = frame_id_status::COMPUTED;
+  fi->this_id.value = id;
 
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog, "-> ");
-      fprint_frame (gdb_stdlog, fi);
-      fprintf_unfiltered (gdb_stdlog, " }\n");
-    }
+  bool added = frame_stash_add (fi);
+  gdb_assert (added);
+
+  frame_debug_printf ("  -> %s", fi->to_string ().c_str ());
+
+  return frame_info_ptr (fi);
+}
+
+frame_info_ptr
+create_new_frame (CORE_ADDR stack, CORE_ADDR pc)
+{
+  frame_id id = frame_id_build (stack, pc);
+  id.user_created_p = 1;
 
-  return fi;
+  return create_new_frame (id);
 }
 
 /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
    innermost frame).  Be careful to not fall off the bottom of the
    frame chain and onto the sentinel frame.  */
 
-struct frame_info *
-get_next_frame (struct frame_info *this_frame)
+frame_info_ptr
+get_next_frame (frame_info_ptr this_frame)
 {
   if (this_frame->level > 0)
-    return this_frame->next;
+    return frame_info_ptr (this_frame->next);
   else
     return NULL;
 }
@@ -1813,8 +2077,8 @@ get_next_frame (struct frame_info *this_frame)
    innermost (i.e. current) frame, return the sentinel frame.  Thus,
    unlike get_next_frame(), NULL will never be returned.  */
 
-struct frame_info *
-get_next_frame_sentinel_okay (struct frame_info *this_frame)
+frame_info_ptr
+get_next_frame_sentinel_okay (frame_info_ptr this_frame)
 {
   gdb_assert (this_frame != NULL);
 
@@ -1823,9 +2087,10 @@ get_next_frame_sentinel_okay (struct frame_info *this_frame)
      is the sentinel frame.  But we disallow it here anyway because
      calling get_next_frame_sentinel_okay() on the sentinel frame
      is likely a coding error.  */
-  gdb_assert (this_frame != sentinel_frame);
+  if (this_frame->this_id.p == frame_id_status::COMPUTED)
+    gdb_assert (!is_sentinel_frame_id (this_frame->this_id.value));
 
-  return this_frame->next;
+  return frame_info_ptr (this_frame->next);
 }
 
 /* Observer for the target_changed event.  */
@@ -1841,29 +2106,37 @@ frame_observer_target_changed (struct target_ops *target)
 void
 reinit_frame_cache (void)
 {
-  struct frame_info *fi;
+  ++frame_cache_generation;
+
+  if (htab_elements (frame_stash) > 0)
+    annotate_frames_invalid ();
 
-  /* Tear down all frame caches.  */
-  for (fi = sentinel_frame; fi != NULL; fi = fi->prev)
+  invalidate_selected_frame ();
+
+  /* Invalidate cache.  */
+  if (sentinel_frame != nullptr)
     {
-      if (fi->prologue_cache && fi->unwind->dealloc_cache)
-       fi->unwind->dealloc_cache (fi, fi->prologue_cache);
-      if (fi->base_cache && fi->base->unwind->dealloc_cache)
-       fi->base->unwind->dealloc_cache (fi, fi->base_cache);
+      /* If frame 0's id is not computed, it is not in the frame stash, so its
+        dealloc functions will not be called when emptying the frame stash.
+        Call frame_info_del manually in that case.  */
+      frame_info *current_frame = sentinel_frame->prev;
+      if (current_frame != nullptr
+         && current_frame->this_id.p == frame_id_status::NOT_COMPUTED)
+       frame_info_del (current_frame);
+
+      sentinel_frame = nullptr;
     }
 
+  frame_stash_invalidate ();
+
   /* Since we can't really be sure what the first object allocated was.  */
   obstack_free (&frame_cache_obstack, 0);
   obstack_init (&frame_cache_obstack);
 
-  if (sentinel_frame != NULL)
-    annotate_frames_invalid ();
+  for (frame_info_ptr &iter : frame_info_ptr::frame_list)
+    iter.invalidate ();
 
-  sentinel_frame = NULL;               /* Invalidate cache */
-  select_frame (NULL);
-  frame_stash_invalidate ();
-  if (frame_debug)
-    fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
+  frame_debug_printf ("generation=%d", frame_cache_generation);
 }
 
 /* Find where a register is saved (in memory or another register).
@@ -1871,7 +2144,7 @@ reinit_frame_cache (void)
    relative to this particular frame.  */
 
 static void
-frame_register_unwind_location (struct frame_info *this_frame, int regnum,
+frame_register_unwind_location (frame_info_ptr this_frame, int regnum,
                                int *optimizedp, enum lval_type *lvalp,
                                CORE_ADDR *addrp, int *realnump)
 {
@@ -1901,14 +2174,23 @@ frame_register_unwind_location (struct frame_info *this_frame, int regnum,
    outermost, with UNWIND_SAME_ID stop reason.  Unlike the other
    validity tests, that compare THIS_FRAME and the next frame, we do
    this right after creating the previous frame, to avoid ever ending
-   up with two frames with the same id in the frame chain.  */
+   up with two frames with the same id in the frame chain.
 
-static struct frame_info *
-get_prev_frame_if_no_cycle (struct frame_info *this_frame)
-{
-  struct frame_info *prev_frame;
+   There is however, one case where this cycle detection is not desirable,
+   when asking for the previous frame of an inline frame, in this case, if
+   the previous frame is a duplicate and we return nullptr then we will be
+   unable to calculate the frame_id of the inline frame, this in turn
+   causes inline_frame_this_id() to fail.  So for inline frames (and only
+   for inline frames), the previous frame will always be returned, even when it
+   has a duplicate frame_id.  We're not worried about cycles in the frame
+   chain as, if the previous frame returned here has a duplicate frame_id,
+   then the frame_id of the inline frame, calculated based off the frame_id
+   of the previous frame, should also be a duplicate.  */
 
-  prev_frame = get_prev_frame_raw (this_frame);
+static frame_info_ptr
+get_prev_frame_maybe_check_cycle (frame_info_ptr this_frame)
+{
+  frame_info_ptr prev_frame = get_prev_frame_raw (this_frame);
 
   /* Don't compute the frame id of the current frame yet.  Unwinding
      the sentinel frame can fail (e.g., if the thread is gone and we
@@ -1922,19 +2204,52 @@ get_prev_frame_if_no_cycle (struct frame_info *this_frame)
   if (prev_frame->level == 0)
     return prev_frame;
 
+  unsigned int entry_generation = get_frame_cache_generation ();
+
   try
     {
       compute_frame_id (prev_frame);
-      if (!frame_stash_add (prev_frame))
+
+      bool cycle_detection_p = get_frame_type (this_frame) != INLINE_FRAME;
+
+      /* This assert checks GDB's state with respect to calculating the
+        frame-id of THIS_FRAME, in the case where THIS_FRAME is an inline
+        frame.
+
+        If THIS_FRAME is frame #0, and is an inline frame, then we put off
+        calculating the frame_id until we specifically make a call to
+        get_frame_id().  As a result we can enter this function in two
+        possible states.  If GDB asked for the previous frame of frame #0
+        then THIS_FRAME will be frame #0 (an inline frame), and the
+        frame_id will be in the NOT_COMPUTED state.  However, if GDB asked
+        for the frame_id of frame #0, then, as getting the frame_id of an
+        inline frame requires us to get the frame_id of the previous
+        frame, we will still end up in here, and the frame_id status will
+        be COMPUTING.
+
+        If, instead, THIS_FRAME is at a level greater than #0 then things
+        are simpler.  For these frames we immediately compute the frame_id
+        when the frame is initially created, and so, for those frames, we
+        will always enter this function with the frame_id status of
+        COMPUTING.  */
+      gdb_assert (cycle_detection_p
+                 || (this_frame->level > 0
+                     && (this_frame->this_id.p
+                         == frame_id_status::COMPUTING))
+                 || (this_frame->level == 0
+                     && (this_frame->this_id.p
+                         != frame_id_status::COMPUTED)));
+
+      /* We must do the CYCLE_DETECTION_P check after attempting to add
+        PREV_FRAME into the cache; if PREV_FRAME is unique then we do want
+        it in the cache, but if it is a duplicate and CYCLE_DETECTION_P is
+        false, then we don't want to unlink it.  */
+      if (!frame_stash_add (prev_frame.get ()) && cycle_detection_p)
        {
          /* Another frame with the same id was already in the stash.  We just
             detected a cycle.  */
-         if (frame_debug)
-           {
-             fprintf_unfiltered (gdb_stdlog, "-> ");
-             fprint_frame (gdb_stdlog, NULL);
-             fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
-           }
+         frame_debug_printf ("  -> nullptr // this frame has same ID");
+
          this_frame->stop_reason = UNWIND_SAME_ID;
          /* Unlink.  */
          prev_frame->next = NULL;
@@ -1944,8 +2259,11 @@ get_prev_frame_if_no_cycle (struct frame_info *this_frame)
     }
   catch (const gdb_exception &ex)
     {
-      prev_frame->next = NULL;
-      this_frame->prev = NULL;
+      if (get_frame_cache_generation () == entry_generation)
+       {
+         prev_frame->next = NULL;
+         this_frame->prev = NULL;
+       }
 
       throw;
     }
@@ -1957,34 +2275,34 @@ get_prev_frame_if_no_cycle (struct frame_info *this_frame)
    TRY_CATCH block.  Return the frame that called THIS_FRAME or NULL if
    there is no such frame.  This may throw an exception.  */
 
-static struct frame_info *
-get_prev_frame_always_1 (struct frame_info *this_frame)
+static frame_info_ptr
+get_prev_frame_always_1 (frame_info_ptr this_frame)
 {
-  struct gdbarch *gdbarch;
+  FRAME_SCOPED_DEBUG_ENTER_EXIT;
 
   gdb_assert (this_frame != NULL);
-  gdbarch = get_frame_arch (this_frame);
 
   if (frame_debug)
     {
-      fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_always (this_frame=");
       if (this_frame != NULL)
-       fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
+       frame_debug_printf ("this_frame=%d", this_frame->level);
       else
-       fprintf_unfiltered (gdb_stdlog, "<NULL>");
-      fprintf_unfiltered (gdb_stdlog, ") ");
+       frame_debug_printf ("this_frame=nullptr");
     }
 
+  struct gdbarch *gdbarch = get_frame_arch (this_frame);
+
   /* Only try to do the unwind once.  */
   if (this_frame->prev_p)
     {
-      if (frame_debug)
-       {
-         fprintf_unfiltered (gdb_stdlog, "-> ");
-         fprint_frame (gdb_stdlog, this_frame->prev);
-         fprintf_unfiltered (gdb_stdlog, " // cached \n");
-       }
-      return this_frame->prev;
+      if (this_frame->prev != nullptr)
+       frame_debug_printf ("  -> %s // cached",
+                           this_frame->prev->to_string ().c_str ());
+      else
+       frame_debug_printf
+         ("  -> nullptr // %s // cached",
+          frame_stop_reason_symbol_string (this_frame->stop_reason));
+      return frame_info_ptr (this_frame->prev);
     }
 
   /* If the frame unwinder hasn't been selected yet, we must do so
@@ -1994,7 +2312,7 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
   if (this_frame->unwind == NULL)
     frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
 
-  this_frame->prev_p = 1;
+  this_frame->prev_p = true;
   this_frame->stop_reason = UNWIND_NO_REASON;
 
   /* If we are unwinding from an inline frame, all of the below tests
@@ -2003,7 +2321,24 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
      until we have unwound all the way down to the previous non-inline
      frame.  */
   if (get_frame_type (this_frame) == INLINE_FRAME)
-    return get_prev_frame_if_no_cycle (this_frame);
+    return get_prev_frame_maybe_check_cycle (this_frame);
+
+  /* If this_frame is the current frame, then compute and stash its
+     frame id prior to fetching and computing the frame id of the
+     previous frame.  Otherwise, the cycle detection code in
+     get_prev_frame_if_no_cycle() will not work correctly.  When
+     get_frame_id() is called later on, an assertion error will be
+     triggered in the event of a cycle between the current frame and
+     its previous frame.
+
+     Note we do this after the INLINE_FRAME check above.  That is
+     because the inline frame's frame id computation needs to fetch
+     the frame id of its previous real stack frame.  I.e., we need to
+     avoid recursion in that case.  This is OK since we're sure the
+     inline frame won't create a cycle with the real stack frame.  See
+     inline_frame_this_id.  */
+  if (this_frame->level == 0)
+    get_frame_id (this_frame);
 
   /* Check that this frame is unwindable.  If it isn't, don't try to
      unwind to the prev frame.  */
@@ -2013,15 +2348,9 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
 
   if (this_frame->stop_reason != UNWIND_NO_REASON)
     {
-      if (frame_debug)
-       {
-         enum unwind_stop_reason reason = this_frame->stop_reason;
-
-         fprintf_unfiltered (gdb_stdlog, "-> ");
-         fprint_frame (gdb_stdlog, NULL);
-         fprintf_unfiltered (gdb_stdlog, " // %s }\n",
-                             frame_stop_reason_symbol_string (reason));
-       }
+      frame_debug_printf
+       ("  -> nullptr // %s",
+        frame_stop_reason_symbol_string (this_frame->stop_reason));
       return NULL;
     }
 
@@ -2031,14 +2360,14 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
      See the comment at frame_id_inner for details.  */
   if (get_frame_type (this_frame) == NORMAL_FRAME
       && this_frame->next->unwind->type == NORMAL_FRAME
-      && frame_id_inner (get_frame_arch (this_frame->next),
+      && frame_id_inner (get_frame_arch (frame_info_ptr (this_frame->next)),
                         get_frame_id (this_frame),
-                        get_frame_id (this_frame->next)))
+                        get_frame_id (frame_info_ptr (this_frame->next))))
     {
       CORE_ADDR this_pc_in_block;
       struct minimal_symbol *morestack_msym;
       const char *morestack_name = NULL;
-      
+
       /* gcc -fsplit-stack __morestack can continue the stack anywhere.  */
       this_pc_in_block = get_frame_address_in_block (this_frame);
       morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
@@ -2046,13 +2375,7 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
        morestack_name = morestack_msym->linkage_name ();
       if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
        {
-         if (frame_debug)
-           {
-             fprintf_unfiltered (gdb_stdlog, "-> ");
-             fprint_frame (gdb_stdlog, NULL);
-             fprintf_unfiltered (gdb_stdlog,
-                                 " // this frame ID is inner }\n");
-           }
+         frame_debug_printf ("  -> nullptr // this frame ID is inner");
          this_frame->stop_reason = UNWIND_INNER_ID;
          return NULL;
        }
@@ -2075,8 +2398,8 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
   if (this_frame->level > 0
       && gdbarch_pc_regnum (gdbarch) >= 0
       && get_frame_type (this_frame) == NORMAL_FRAME
-      && (get_frame_type (this_frame->next) == NORMAL_FRAME
-         || get_frame_type (this_frame->next) == INLINE_FRAME))
+      && (get_frame_type (frame_info_ptr (this_frame->next)) == NORMAL_FRAME
+         || get_frame_type (frame_info_ptr (this_frame->next)) == INLINE_FRAME))
     {
       int optimized, realnum, nrealnum;
       enum lval_type lval, nlval;
@@ -2092,20 +2415,14 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
       if ((lval == lval_memory && lval == nlval && addr == naddr)
          || (lval == lval_register && lval == nlval && realnum == nrealnum))
        {
-         if (frame_debug)
-           {
-             fprintf_unfiltered (gdb_stdlog, "-> ");
-             fprint_frame (gdb_stdlog, NULL);
-             fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
-           }
-
+         frame_debug_printf ("  -> nullptr // no saved PC");
          this_frame->stop_reason = UNWIND_NO_SAVED_PC;
          this_frame->prev = NULL;
          return NULL;
        }
     }
 
-  return get_prev_frame_if_no_cycle (this_frame);
+  return get_prev_frame_maybe_check_cycle (this_frame);
 }
 
 /* Return a "struct frame_info" corresponding to the frame that called
@@ -2114,10 +2431,10 @@ get_prev_frame_always_1 (struct frame_info *this_frame)
    Unlike get_prev_frame, this function always tries to unwind the
    frame.  */
 
-struct frame_info *
-get_prev_frame_always (struct frame_info *this_frame)
+frame_info_ptr
+get_prev_frame_always (frame_info_ptr this_frame)
 {
-  struct frame_info *prev_frame = NULL;
+  frame_info_ptr prev_frame = NULL;
 
   try
     {
@@ -2134,9 +2451,9 @@ get_prev_frame_always (struct frame_info *this_frame)
              size_t size;
 
              /* The error needs to live as long as the frame does.
-                Allocate using stack local STOP_STRING then assign the
-                pointer to the frame, this allows the STOP_STRING on the
-                frame to be of type 'const char *'.  */
+                Allocate using stack local STOP_STRING then assign the
+                pointer to the frame, this allows the STOP_STRING on the
+                frame to be of type 'const char *'.  */
              size = ex.message->size () + 1;
              stop_string = (char *) frame_obstack_zalloc (size);
              memcpy (stop_string, ex.what (), size);
@@ -2154,10 +2471,10 @@ get_prev_frame_always (struct frame_info *this_frame)
 /* Construct a new "struct frame_info" and link it previous to
    this_frame.  */
 
-static struct frame_info *
-get_prev_frame_raw (struct frame_info *this_frame)
+static frame_info_ptr
+get_prev_frame_raw (frame_info_ptr this_frame)
 {
-  struct frame_info *prev_frame;
+  frame_info *prev_frame;
 
   /* Allocate the new frame but do not wire it in to the frame chain.
      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
@@ -2198,71 +2515,94 @@ get_prev_frame_raw (struct frame_info *this_frame)
 
   /* Link it in.  */
   this_frame->prev = prev_frame;
-  prev_frame->next = this_frame;
+  prev_frame->next = this_frame.get ();
 
-  if (frame_debug)
-    {
-      fprintf_unfiltered (gdb_stdlog, "-> ");
-      fprint_frame (gdb_stdlog, prev_frame);
-      fprintf_unfiltered (gdb_stdlog, " }\n");
-    }
+  frame_debug_printf ("  -> %s", prev_frame->to_string ().c_str ());
 
-  return prev_frame;
+  return frame_info_ptr (prev_frame);
 }
 
 /* Debug routine to print a NULL frame being returned.  */
 
 static void
-frame_debug_got_null_frame (struct frame_info *this_frame,
+frame_debug_got_null_frame (frame_info_ptr this_frame,
                            const char *reason)
 {
   if (frame_debug)
     {
-      fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
       if (this_frame != NULL)
-       fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
+       frame_debug_printf ("this_frame=%d -> %s", this_frame->level, reason);
       else
-       fprintf_unfiltered (gdb_stdlog, "<NULL>");
-      fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
+       frame_debug_printf ("this_frame=nullptr -> %s", reason);
     }
 }
 
 /* Is this (non-sentinel) frame in the "main"() function?  */
 
-static int
-inside_main_func (struct frame_info *this_frame)
-{
-  struct bound_minimal_symbol msymbol;
-  CORE_ADDR maddr;
+static bool
+inside_main_func (frame_info_ptr this_frame)
+{
+  if (current_program_space->symfile_object_file == nullptr)
+    return false;
+
+  CORE_ADDR sym_addr = 0;
+  const char *name = main_name ();
+  bound_minimal_symbol msymbol
+    = lookup_minimal_symbol (name, NULL,
+                            current_program_space->symfile_object_file);
+
+  if (msymbol.minsym != nullptr)
+    sym_addr = msymbol.value_address ();
+
+  /* Favor a full symbol in Fortran, for the case where the Fortran main
+     is also called "main".  */
+  if (msymbol.minsym == nullptr
+      || get_frame_language (this_frame) == language_fortran)
+    {
+      /* In some language (for example Fortran) there will be no minimal
+        symbol with the name of the main function.  In this case we should
+        search the full symbols to see if we can find a match.  */
+      struct block_symbol bs = lookup_symbol (name, NULL, VAR_DOMAIN, 0);
+
+      /* We might have found some unrelated symbol.  For example, the
+        Rust compiler can emit both a subprogram and a namespace with
+        the same name in the same scope; and due to how gdb's symbol
+        tables currently work, we can't request the one we'd
+        prefer.  */
+      if (bs.symbol != nullptr && bs.symbol->aclass () == LOC_BLOCK)
+       {
+         const struct block *block = bs.symbol->value_block ();
+         gdb_assert (block != nullptr);
+         sym_addr = block->start ();
+       }
+      else if (msymbol.minsym == nullptr)
+       return false;
+    }
 
-  if (symfile_objfile == 0)
-    return 0;
-  msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
-  if (msymbol.minsym == NULL)
-    return 0;
-  /* Make certain that the code, and not descriptor, address is
-     returned.  */
-  maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
-                                             BMSYMBOL_VALUE_ADDRESS (msymbol),
-                                             current_top_target ());
-  return maddr == get_frame_func (this_frame);
+  /* Convert any function descriptor addresses into the actual function
+     code address.  */
+  sym_addr = (gdbarch_convert_from_func_ptr_addr
+             (get_frame_arch (this_frame), sym_addr,
+              current_inferior ()->top_target ()));
+
+  return sym_addr == get_frame_func (this_frame);
 }
 
 /* Test whether THIS_FRAME is inside the process entry point function.  */
 
-static int
-inside_entry_func (struct frame_info *this_frame)
+static bool
+inside_entry_func (frame_info_ptr this_frame)
 {
   CORE_ADDR entry_point;
 
   if (!entry_point_address_query (&entry_point))
-    return 0;
+    return false;
 
   return get_frame_func (this_frame) == entry_point;
 }
 
 /* Return a structure containing various interesting information about
-   the frame that called THIS_FRAME.  Returns NULL if there is entier
+   the frame that called THIS_FRAME.  Returns NULL if there is either
    no such frame or the frame fails any of a set of target-independent
    condition that should terminate the frame chain (e.g., as unwinding
    past main()).
@@ -2270,9 +2610,11 @@ inside_entry_func (struct frame_info *this_frame)
    This function should not contain target-dependent tests, such as
    checking whether the program-counter is zero.  */
 
-struct frame_info *
-get_prev_frame (struct frame_info *this_frame)
+frame_info_ptr
+get_prev_frame (frame_info_ptr this_frame)
 {
+  FRAME_SCOPED_DEBUG_ENTER_EXIT;
+
   CORE_ADDR frame_pc;
   int frame_pc_p;
 
@@ -2280,16 +2622,6 @@ get_prev_frame (struct frame_info *this_frame)
      something should be calling get_selected_frame() or
      get_current_frame().  */
   gdb_assert (this_frame != NULL);
-  
-  /* If this_frame is the current frame, then compute and stash
-     its frame id prior to fetching and computing the frame id of the
-     previous frame.  Otherwise, the cycle detection code in
-     get_prev_frame_if_no_cycle() will not work correctly.  When
-     get_frame_id() is called later on, an assertion error will
-     be triggered in the event of a cycle between the current
-     frame and its previous frame.  */
-  if (this_frame->level == 0)
-    get_frame_id (this_frame);
 
   frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
 
@@ -2299,9 +2631,9 @@ get_prev_frame (struct frame_info *this_frame)
      pcsqh register (space register for the instruction at the head of the
      instruction queue) cannot be written directly; the only way to set it
      is to branch to code that is in the target space.  In order to implement
-     frame dummies on HPUX, the called function is made to jump back to where 
-     the inferior was when the user function was called.  If gdb was inside 
-     the main function when we created the dummy frame, the dummy frame will 
+     frame dummies on HPUX, the called function is made to jump back to where
+     the inferior was when the user function was called.  If gdb was inside
+     the main function when we created the dummy frame, the dummy frame will
      point inside the main function.  */
   if (this_frame->level >= 0
       && get_frame_type (this_frame) == NORMAL_FRAME
@@ -2346,7 +2678,7 @@ get_prev_frame (struct frame_info *this_frame)
      That should provide a far better stopper than the current
      heuristics.  */
   /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
-     applied tail-call optimizations to main so that a function called 
+     applied tail-call optimizations to main so that a function called
      from main returns directly to the caller of main.  Since we don't
      stop at main, we should at least stop at the entry point of the
      application.  */
@@ -2376,59 +2708,43 @@ get_prev_frame (struct frame_info *this_frame)
   return get_prev_frame_always (this_frame);
 }
 
-struct frame_id
-get_prev_frame_id_by_id (struct frame_id id)
-{
-  struct frame_id prev_id;
-  struct frame_info *frame;
-  
-  frame = frame_find_by_id (id);
-
-  if (frame != NULL)
-    prev_id = get_frame_id (get_prev_frame (frame));
-  else
-    prev_id = null_frame_id;
-
-  return prev_id;
-}
-
 CORE_ADDR
-get_frame_pc (struct frame_info *frame)
+get_frame_pc (frame_info_ptr frame)
 {
   gdb_assert (frame->next != NULL);
-  return frame_unwind_pc (frame->next);
+  return frame_unwind_pc (frame_info_ptr (frame->next));
 }
 
-int
-get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
+bool
+get_frame_pc_if_available (frame_info_ptr frame, CORE_ADDR *pc)
 {
 
   gdb_assert (frame->next != NULL);
 
   try
     {
-      *pc = frame_unwind_pc (frame->next);
+      *pc = frame_unwind_pc (frame_info_ptr (frame->next));
     }
   catch (const gdb_exception_error &ex)
     {
       if (ex.error == NOT_AVAILABLE_ERROR)
-       return 0;
+       return false;
       else
        throw;
     }
 
-  return 1;
+  return true;
 }
 
 /* Return an address that falls within THIS_FRAME's code block.  */
 
 CORE_ADDR
-get_frame_address_in_block (struct frame_info *this_frame)
+get_frame_address_in_block (frame_info_ptr this_frame)
 {
   /* A draft address.  */
   CORE_ADDR pc = get_frame_pc (this_frame);
 
-  struct frame_info *next_frame = this_frame->next;
+  frame_info_ptr next_frame (this_frame->next);
 
   /* Calling get_frame_pc returns the resume address for THIS_FRAME.
      Normally the resume address is inside the body of the function
@@ -2470,7 +2786,7 @@ get_frame_address_in_block (struct frame_info *this_frame)
      "calling" normal function should not be adjusted either.  */
 
   while (get_frame_type (next_frame) == INLINE_FRAME)
-    next_frame = next_frame->next;
+    next_frame = frame_info_ptr (next_frame->next);
 
   if ((get_frame_type (next_frame) == NORMAL_FRAME
        || get_frame_type (next_frame) == TAILCALL_FRAME)
@@ -2482,8 +2798,8 @@ get_frame_address_in_block (struct frame_info *this_frame)
   return pc;
 }
 
-int
-get_frame_address_in_block_if_available (struct frame_info *this_frame,
+bool
+get_frame_address_in_block_if_available (frame_info_ptr this_frame,
                                         CORE_ADDR *pc)
 {
 
@@ -2494,28 +2810,29 @@ get_frame_address_in_block_if_available (struct frame_info *this_frame,
   catch (const gdb_exception_error &ex)
     {
       if (ex.error == NOT_AVAILABLE_ERROR)
-       return 0;
+       return false;
       throw;
     }
 
-  return 1;
+  return true;
 }
 
 symtab_and_line
-find_frame_sal (frame_info *frame)
+find_frame_sal (frame_info_ptr frame)
 {
-  struct frame_info *next_frame;
+  frame_info_ptr next_frame;
   int notcurrent;
   CORE_ADDR pc;
 
-  /* If the next frame represents an inlined function call, this frame's
-     sal is the "call site" of that inlined function, which can not
-     be inferred from get_frame_pc.  */
-  next_frame = get_next_frame (frame);
   if (frame_inlined_callees (frame) > 0)
     {
       struct symbol *sym;
 
+      /* If the current frame has some inlined callees, and we have a next
+        frame, then that frame must be an inlined frame.  In this case
+        this frame's sal is the "call site" of the next frame's inlined
+        function, which can not be inferred from get_frame_pc.  */
+      next_frame = get_next_frame (frame);
       if (next_frame)
        sym = get_frame_function (next_frame);
       else
@@ -2525,10 +2842,10 @@ find_frame_sal (frame_info *frame)
       gdb_assert (sym);
 
       symtab_and_line sal;
-      if (SYMBOL_LINE (sym) != 0)
+      if (sym->line () != 0)
        {
-         sal.symtab = symbol_symtab (sym);
-         sal.line = SYMBOL_LINE (sym);
+         sal.symtab = sym->symtab ();
+         sal.line = sym->line ();
        }
       else
        /* If the symbol does not have a location, we don't know where
@@ -2559,7 +2876,7 @@ find_frame_sal (frame_info *frame)
 /* Per "frame.h", return the ``address'' of the frame.  Code should
    really be using get_frame_id().  */
 CORE_ADDR
-get_frame_base (struct frame_info *fi)
+get_frame_base (frame_info_ptr fi)
 {
   return get_frame_id (fi).stack_addr;
 }
@@ -2567,7 +2884,7 @@ get_frame_base (struct frame_info *fi)
 /* High-level offsets into the frame.  Used by the debug info.  */
 
 CORE_ADDR
-get_frame_base_address (struct frame_info *fi)
+get_frame_base_address (frame_info_ptr fi)
 {
   if (get_frame_type (fi) != NORMAL_FRAME)
     return 0;
@@ -2581,7 +2898,7 @@ get_frame_base_address (struct frame_info *fi)
 }
 
 CORE_ADDR
-get_frame_locals_address (struct frame_info *fi)
+get_frame_locals_address (frame_info_ptr fi)
 {
   if (get_frame_type (fi) != NORMAL_FRAME)
     return 0;
@@ -2596,7 +2913,7 @@ get_frame_locals_address (struct frame_info *fi)
 }
 
 CORE_ADDR
-get_frame_args_address (struct frame_info *fi)
+get_frame_args_address (frame_info_ptr fi)
 {
   if (get_frame_type (fi) != NORMAL_FRAME)
     return 0;
@@ -2613,11 +2930,12 @@ get_frame_args_address (struct frame_info *fi)
 /* Return true if the frame unwinder for frame FI is UNWINDER; false
    otherwise.  */
 
-int
-frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
+bool
+frame_unwinder_is (frame_info_ptr fi, const frame_unwind *unwinder)
 {
-  if (fi->unwind == NULL)
+  if (fi->unwind == nullptr)
     frame_unwind_find_by_frame (fi, &fi->prologue_cache);
+
   return fi->unwind == unwinder;
 }
 
@@ -2625,7 +2943,7 @@ frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
    or -1 for a NULL frame.  */
 
 int
-frame_relative_level (struct frame_info *fi)
+frame_relative_level (frame_info_ptr fi)
 {
   if (fi == NULL)
     return -1;
@@ -2634,7 +2952,7 @@ frame_relative_level (struct frame_info *fi)
 }
 
 enum frame_type
-get_frame_type (struct frame_info *frame)
+get_frame_type (frame_info_ptr frame)
 {
   if (frame->unwind == NULL)
     /* Initialize the frame's unwinder because that's what
@@ -2644,13 +2962,13 @@ get_frame_type (struct frame_info *frame)
 }
 
 struct program_space *
-get_frame_program_space (struct frame_info *frame)
+get_frame_program_space (frame_info_ptr frame)
 {
   return frame->pspace;
 }
 
 struct program_space *
-frame_unwind_program_space (struct frame_info *this_frame)
+frame_unwind_program_space (frame_info_ptr this_frame)
 {
   gdb_assert (this_frame);
 
@@ -2661,7 +2979,7 @@ frame_unwind_program_space (struct frame_info *this_frame)
 }
 
 const address_space *
-get_frame_address_space (struct frame_info *frame)
+get_frame_address_space (frame_info_ptr frame)
 {
   return frame->aspace;
 }
@@ -2669,14 +2987,14 @@ get_frame_address_space (struct frame_info *frame)
 /* Memory access methods.  */
 
 void
-get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
-                 gdb_byte *buf, int len)
+get_frame_memory (frame_info_ptr this_frame, CORE_ADDR addr,
+                 gdb::array_view<gdb_byte> buffer)
 {
-  read_memory (addr, buf, len);
+  read_memory (addr, buffer.data (), buffer.size ());
 }
 
 LONGEST
-get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
+get_frame_memory_signed (frame_info_ptr this_frame, CORE_ADDR addr,
                         int len)
 {
   struct gdbarch *gdbarch = get_frame_arch (this_frame);
@@ -2686,7 +3004,7 @@ get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
 }
 
 ULONGEST
-get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
+get_frame_memory_unsigned (frame_info_ptr this_frame, CORE_ADDR addr,
                           int len)
 {
   struct gdbarch *gdbarch = get_frame_arch (this_frame);
@@ -2695,24 +3013,24 @@ get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
   return read_memory_unsigned_integer (addr, len, byte_order);
 }
 
-int
-safe_frame_unwind_memory (struct frame_info *this_frame,
-                         CORE_ADDR addr, gdb_byte *buf, int len)
+bool
+safe_frame_unwind_memory (frame_info_ptr this_frame,
+                         CORE_ADDR addr, gdb::array_view<gdb_byte> buffer)
 {
   /* NOTE: target_read_memory returns zero on success!  */
-  return !target_read_memory (addr, buf, len);
+  return target_read_memory (addr, buffer.data (), buffer.size ()) == 0;
 }
 
 /* Architecture methods.  */
 
 struct gdbarch *
-get_frame_arch (struct frame_info *this_frame)
+get_frame_arch (frame_info_ptr this_frame)
 {
-  return frame_unwind_arch (this_frame->next);
+  return frame_unwind_arch (frame_info_ptr (this_frame->next));
 }
 
 struct gdbarch *
-frame_unwind_arch (struct frame_info *next_frame)
+frame_unwind_arch (frame_info_ptr next_frame)
 {
   if (!next_frame->prev_arch.p)
     {
@@ -2728,19 +3046,17 @@ frame_unwind_arch (struct frame_info *next_frame)
        arch = get_frame_arch (next_frame);
 
       next_frame->prev_arch.arch = arch;
-      next_frame->prev_arch.p = 1;
-      if (frame_debug)
-       fprintf_unfiltered (gdb_stdlog,
-                           "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
-                           next_frame->level,
-                           gdbarch_bfd_arch_info (arch)->printable_name);
+      next_frame->prev_arch.p = true;
+      frame_debug_printf ("next_frame=%d -> %s",
+                         next_frame->level,
+                         gdbarch_bfd_arch_info (arch)->printable_name);
     }
 
   return next_frame->prev_arch.arch;
 }
 
 struct gdbarch *
-frame_unwind_caller_arch (struct frame_info *next_frame)
+frame_unwind_caller_arch (frame_info_ptr next_frame)
 {
   next_frame = skip_artificial_frames (next_frame);
 
@@ -2755,10 +3071,10 @@ frame_unwind_caller_arch (struct frame_info *next_frame)
 /* Gets the language of FRAME.  */
 
 enum language
-get_frame_language (struct frame_info *frame)
+get_frame_language (frame_info_ptr frame)
 {
   CORE_ADDR pc = 0;
-  int pc_p = 0;
+  bool pc_p = false;
 
   gdb_assert (frame!= NULL);
 
@@ -2774,7 +3090,7 @@ get_frame_language (struct frame_info *frame)
   try
     {
       pc = get_frame_address_in_block (frame);
-      pc_p = 1;
+      pc_p = true;
     }
   catch (const gdb_exception_error &ex)
     {
@@ -2787,7 +3103,7 @@ get_frame_language (struct frame_info *frame)
       struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
 
       if (cust != NULL)
-       return compunit_language (cust);
+       return cust->language ();
     }
 
   return language_unknown;
@@ -2796,19 +3112,58 @@ get_frame_language (struct frame_info *frame)
 /* Stack pointer methods.  */
 
 CORE_ADDR
-get_frame_sp (struct frame_info *this_frame)
+get_frame_sp (frame_info_ptr this_frame)
 {
   struct gdbarch *gdbarch = get_frame_arch (this_frame);
 
   /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
      operate on THIS_FRAME now.  */
-  return gdbarch_unwind_sp (gdbarch, this_frame->next);
+  return gdbarch_unwind_sp (gdbarch, frame_info_ptr (this_frame->next));
+}
+
+/* See frame.h.  */
+
+frame_info_ptr
+frame_follow_static_link (frame_info_ptr frame)
+{
+  const block *frame_block = get_frame_block (frame, nullptr);
+  frame_block = frame_block->function_block ();
+
+  const struct dynamic_prop *static_link = frame_block->static_link ();
+  if (static_link == nullptr)
+    return {};
+
+  CORE_ADDR upper_frame_base;
+
+  if (!dwarf2_evaluate_property (static_link, frame, NULL, &upper_frame_base))
+    return {};
+
+  /* Now climb up the stack frame until we reach the frame we are interested
+     in.  */
+  for (; frame != nullptr; frame = get_prev_frame (frame))
+    {
+      struct symbol *framefunc = get_frame_function (frame);
+
+      /* Stacks can be quite deep: give the user a chance to stop this.  */
+      QUIT;
+
+      /* If we don't know how to compute FRAME's base address, don't give up:
+        maybe the frame we are looking for is upper in the stack frame.  */
+      if (framefunc != NULL
+         && SYMBOL_BLOCK_OPS (framefunc) != NULL
+         && SYMBOL_BLOCK_OPS (framefunc)->get_frame_base != NULL
+         && (SYMBOL_BLOCK_OPS (framefunc)->get_frame_base (framefunc, frame)
+             == upper_frame_base))
+       break;
+    }
+
+  return frame;
 }
 
 /* Return the reason why we can't unwind past FRAME.  */
 
 enum unwind_stop_reason
-get_frame_unwind_stop_reason (struct frame_info *frame)
+get_frame_unwind_stop_reason (frame_info_ptr frame)
 {
   /* Fill-in STOP_REASON.  */
   get_prev_frame_always (frame);
@@ -2830,13 +3185,12 @@ unwind_stop_reason_to_string (enum unwind_stop_reason reason)
 #undef SET
 
     default:
-      internal_error (__FILE__, __LINE__,
-                     "Invalid frame stop reason");
+      internal_error ("Invalid frame stop reason");
     }
 }
 
 const char *
-frame_stop_reason_string (struct frame_info *fi)
+frame_stop_reason_string (frame_info_ptr fi)
 {
   gdb_assert (fi->prev_p);
   gdb_assert (fi->prev == NULL);
@@ -2863,8 +3217,7 @@ frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
 #undef SET
 
     default:
-      internal_error (__FILE__, __LINE__,
-                     "Invalid frame stop reason");
+      internal_error ("Invalid frame stop reason");
     }
 }
 
@@ -2872,7 +3225,7 @@ frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
    FRAME.  */
 
 void
-frame_cleanup_after_sniffer (struct frame_info *frame)
+frame_cleanup_after_sniffer (frame_info_ptr frame)
 {
   /* The sniffer should not allocate a prologue cache if it did not
      match this frame.  */
@@ -2883,13 +3236,13 @@ frame_cleanup_after_sniffer (struct frame_info *frame)
   gdb_assert (!frame->prev_p);
 
   /* The sniffer should not check the frame's ID; that's circular.  */
-  gdb_assert (!frame->this_id.p);
+  gdb_assert (frame->this_id.p != frame_id_status::COMPUTED);
 
   /* Clear cached fields dependent on the unwinder.
 
      The previous PC is independent of the unwinder, but the previous
      function is not (see get_frame_address_in_block).  */
-  frame->prev_func.p = 0;
+  frame->prev_func.status = CC_UNKNOWN;
   frame->prev_func.addr = 0;
 
   /* Discard the unwinder last, so that we can easily find it if an assertion
@@ -2902,7 +3255,7 @@ frame_cleanup_after_sniffer (struct frame_info *frame)
    frame_cleanup_after_sniffer.  */
 
 void
-frame_prepare_for_sniffer (struct frame_info *frame,
+frame_prepare_for_sniffer (frame_info_ptr frame,
                           const struct frame_unwind *unwind)
 {
   gdb_assert (frame->unwind == NULL);
@@ -2912,26 +3265,11 @@ frame_prepare_for_sniffer (struct frame_info *frame,
 static struct cmd_list_element *set_backtrace_cmdlist;
 static struct cmd_list_element *show_backtrace_cmdlist;
 
-static void
-set_backtrace_cmd (const char *args, int from_tty)
-{
-  help_list (set_backtrace_cmdlist, "set backtrace ", all_commands,
-            gdb_stdout);
-}
-
-static void
-show_backtrace_cmd (const char *args, int from_tty)
-{
-  cmd_show_list (show_backtrace_cmdlist, from_tty, "");
-}
-
 /* Definition of the "set backtrace" settings that are exposed as
    "backtrace" command options.  */
 
 using boolean_option_def
   = gdb::option::boolean_option_def<set_backtrace_options>;
-using uinteger_option_def
-  = gdb::option::uinteger_option_def<set_backtrace_options>;
 
 const gdb::option::option_def set_backtrace_option_defs[] = {
 
@@ -2958,25 +3296,102 @@ the rest of the stack trace."),
   },
 };
 
+/* Implement the 'maintenance print frame-id' command.  */
+
+static void
+maintenance_print_frame_id (const char *args, int from_tty)
+{
+  frame_info_ptr frame;
+
+  /* Use the currently selected frame, or select a frame based on the level
+     number passed by the user.  */
+  if (args == nullptr)
+    frame = get_selected_frame ("No frame selected");
+  else
+    {
+      int level = value_as_long (parse_and_eval (args));
+      frame = find_relative_frame (get_current_frame (), &level);
+    }
+
+  /* Print the frame-id.  */
+  gdb_assert (frame != nullptr);
+  gdb_printf ("frame-id for frame #%d: %s\n",
+             frame_relative_level (frame),
+             get_frame_id (frame).to_string ().c_str ());
+}
+
+/* See frame-info-ptr.h.  */
+
+frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
+  : m_ptr (ptr)
+{
+  frame_list.push_back (*this);
+
+  if (m_ptr == nullptr)
+    return;
+
+  m_cached_level = ptr->level;
+
+  if (m_cached_level != 0 || m_ptr->this_id.value.user_created_p)
+    m_cached_id = m_ptr->this_id.value;
+}
+
+/* See frame-info-ptr.h.  */
+
+frame_info *
+frame_info_ptr::reinflate () const
+{
+  /* Ensure we have a valid frame level (sentinel frame or above).  */
+  gdb_assert (m_cached_level >= -1);
+
+  if (m_ptr != nullptr)
+    {
+      /* The frame_info wasn't invalidated, no need to reinflate.  */
+      return m_ptr;
+    }
+
+  if (m_cached_id.user_created_p)
+    m_ptr = create_new_frame (m_cached_id).get ();
+  else
+    {
+      /* Frame #0 needs special handling, see comment in select_frame.  */
+      if (m_cached_level == 0)
+       m_ptr = get_current_frame ().get ();
+      else
+       {
+         /* If we reach here without a valid frame id, it means we are trying
+            to reinflate a frame whose id was not know at construction time.
+            We're probably trying to reinflate a frame while computing its id
+            which is not possible, and would indicate a problem with GDB.  */
+         gdb_assert (frame_id_p (m_cached_id));
+         m_ptr = frame_find_by_id (m_cached_id).get ();
+       }
+    }
+
+  gdb_assert (m_ptr != nullptr);
+  return m_ptr;
+}
+
+void _initialize_frame ();
 void
-_initialize_frame (void)
+_initialize_frame ()
 {
   obstack_init (&frame_cache_obstack);
 
   frame_stash_create ();
 
-  gdb::observers::target_changed.attach (frame_observer_target_changed);
+  gdb::observers::target_changed.attach (frame_observer_target_changed,
+                                        "frame");
 
-  add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
+  add_setshow_prefix_cmd ("backtrace", class_maintenance,
+                         _("\
 Set backtrace specific variables.\n\
 Configure backtrace variables such as the backtrace limit"),
-                 &set_backtrace_cmdlist, "set backtrace ",
-                 0/*allow-unknown*/, &setlist);
-  add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
+                         _("\
 Show backtrace specific variables.\n\
 Show backtrace variables such as the backtrace limit."),
-                 &show_backtrace_cmdlist, "show backtrace ",
-                 0/*allow-unknown*/, &showlist);
+                         &set_backtrace_cmdlist, &show_backtrace_cmdlist,
+                         &setlist, &showlist);
 
   add_setshow_uinteger_cmd ("limit", class_obscure,
                            &user_set_backtrace_options.backtrace_limit, _("\
@@ -2994,11 +3409,15 @@ Literal \"unlimited\" or zero means no limit."),
      set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist);
 
   /* Debug this files internals.  */
-  add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug,  _("\
+  add_setshow_boolean_cmd ("frame", class_maintenance, &frame_debug,  _("\
 Set frame debugging."), _("\
 Show frame debugging."), _("\
 When non-zero, frame specific internal debugging is enabled."),
-                            NULL,
-                            show_frame_debug,
-                            &setdebuglist, &showdebuglist);
+                          NULL,
+                          show_frame_debug,
+                          &setdebuglist, &showdebuglist);
+
+  add_cmd ("frame-id", class_maintenance, maintenance_print_frame_id,
+          _("Print the current frame-id."),
+          &maintenanceprintlist);
 }