public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH v2 13/13] gdb: make frame_info_ptr auto-reinflatable
Date: Tue, 13 Dec 2022 22:34:41 -0500	[thread overview]
Message-ID: <20221214033441.499512-14-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20221214033441.499512-1-simon.marchi@polymtl.ca>

From: Simon Marchi <simon.marchi@efficios.com>

This is the second step of making frame_info_ptr automatic, reinflate on
demand whenever trying to obtain the wrapper frame_info pointer, either
through the get method or operator->.  Make the reinflate method
private, it is used as a convenience method in those two.

Add an "is_null" method, because it is often needed to know whether the
frame_info_ptr wraps an frame_info or is empty.

Make m_ptr mutable, so that it's possible to reinflate const
frame_info_ptr objects.  Whether m_ptr is nullptr or not does not change
the logical state of the object, because we re-create it on demand.  I
believe this is the right use case for mutable.

Change-Id: Icb0552d0035e227f81eb3c121d8a9bb2f9d25794
---
 gdb/frame.c                              |  7 ++--
 gdb/frame.h                              | 45 +++++++++++++++++-------
 gdb/infcmd.c                             |  1 -
 gdb/mi/mi-cmd-stack.c                    |  1 -
 gdb/stack.c                              |  6 ----
 gdb/unittests/frame_info_ptr-selftests.c |  2 --
 6 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/gdb/frame.c b/gdb/frame.c
index 4755c8e9aa7f..ef58253312e8 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -3224,8 +3224,8 @@ frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
 
 /* See frame-info-ptr.h.  */
 
-void
-frame_info_ptr::reinflate ()
+frame_info *
+frame_info_ptr::reinflate () const
 {
   /* Ensure we have a valid frame level (sentinel frame or above).  */
   gdb_assert (m_cached_level >= -1);
@@ -3233,7 +3233,7 @@ frame_info_ptr::reinflate ()
   if (m_ptr != nullptr)
     {
       /* The frame_info wasn't invalidated, no need to reinflate.  */
-      return;
+      return m_ptr;
     }
 
   if (m_cached_id.user_created_p)
@@ -3255,6 +3255,7 @@ frame_info_ptr::reinflate ()
     }
 
   gdb_assert (m_ptr != nullptr);
+  return m_ptr;
 }
 
 void _initialize_frame ();
diff --git a/gdb/frame.h b/gdb/frame.h
index f8ca57815bf6..e02635c21408 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -275,29 +275,38 @@ class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
   }
 
   frame_info *operator-> () const
-  {
-    return m_ptr;
-  }
+  { return this->reinflate (); }
 
   /* Fetch the underlying pointer.  Note that new code should
      generally not use this -- avoid it if at all possible.  */
   frame_info *get () const
   {
-    return m_ptr;
+    if (this->is_null ())
+      return nullptr;
+
+    return this->reinflate ();
   }
 
+  /* Return true if this object is empty (does not wrap a frame_info
+     object).  */
+
+  bool is_null () const
+  {
+    return m_cached_level == this->invalid_level;
+  };
+
   /* This exists for compatibility with pre-existing code that checked
      a "frame_info *" using "!".  */
   bool operator! () const
   {
-    return m_ptr == nullptr;
+    return this->is_null ();
   }
 
   /* This exists for compatibility with pre-existing code that checked
      a "frame_info *" like "if (ptr)".  */
   explicit operator bool () const
   {
-    return m_ptr != nullptr;
+    return !this->is_null ();
   }
 
   /* Invalidate this pointer.  */
@@ -306,17 +315,18 @@ class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
     m_ptr = nullptr;
   }
 
-  /* Use the cached frame_id to reinflate the pointer.  */
-  void reinflate ();
-
 private:
   /* We sometimes need to construct frame_info_ptr objects around the
      sentinel_frame, which has level -1.  Therefore, make the invalid frame
      level value -2.  */
   static constexpr int invalid_level = -2;
 
+  /* Use the cached frame level and id to reinflate the pointer, and return
+     it.  */
+  frame_info *reinflate () const;
+
   /* The underlying pointer.  */
-  frame_info *m_ptr = nullptr;
+  mutable frame_info *m_ptr = nullptr;
 
   /* The frame_id of the underlying pointer.
 
@@ -341,37 +351,46 @@ class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
 static inline bool
 operator== (const frame_info *self, const frame_info_ptr &other)
 {
+  if (self == nullptr || other.is_null ())
+    return self == nullptr && other.is_null ();
+
   return self == other.get ();
 }
 
 static inline bool
 operator== (const frame_info_ptr &self, const frame_info_ptr &other)
 {
+  if (self.is_null () || other.is_null ())
+    return self.is_null () && other.is_null ();
+
   return self.get () == other.get ();
 }
 
 static inline bool
 operator== (const frame_info_ptr &self, const frame_info *other)
 {
+  if (self.is_null () || other == nullptr)
+    return self.is_null () && other == nullptr;
+
   return self.get () == other;
 }
 
 static inline bool
 operator!= (const frame_info *self, const frame_info_ptr &other)
 {
-  return self != other.get ();
+  return !(self == other);
 }
 
 static inline bool
 operator!= (const frame_info_ptr &self, const frame_info_ptr &other)
 {
-  return self.get () != other.get ();
+  return !(self == other);
 }
 
 static inline bool
 operator!= (const frame_info_ptr &self, const frame_info *other)
 {
-  return self.get () != other;
+  return !(self == other);
 }
 
 /* For every stopped thread, GDB tracks two frames: current and
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index ee3d97b585dd..d1f634f86b1d 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -1917,7 +1917,6 @@ finish_command (const char *arg, int from_tty)
 
       print_stack_frame (callee_frame, 1, LOCATION, 0);
     }
-  frame.reinflate ();
 
   if (execution_direction == EXEC_REVERSE)
     finish_backward (sm);
diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c
index 186d53156730..16be1938c033 100644
--- a/gdb/mi/mi-cmd-stack.c
+++ b/gdb/mi/mi-cmd-stack.c
@@ -180,7 +180,6 @@ mi_cmd_stack_list_frames (const char *command, char **argv, int argc)
 	     If args is 0, don't print the arguments.  */
 	  print_frame_info (user_frame_print_options,
 			    fi, 1, LOC_AND_ADDRESS, 0 /* args */, 0);
-	  fi.reinflate ();
 	}
     }
 }
diff --git a/gdb/stack.c b/gdb/stack.c
index 126ce0b8cc2a..5bc5b2d26cfe 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -367,7 +367,6 @@ print_stack_frame (frame_info_ptr frame, int print_level,
       print_frame_info (user_frame_print_options,
 			frame, print_level, print_what, 1 /* print_args */,
 			set_current_sal);
-      frame.reinflate ();
       if (set_current_sal)
 	set_current_sal_from_frame (frame);
     }
@@ -903,7 +902,6 @@ print_frame_args (const frame_print_options &fp_opts,
 	    }
 
 	  first = 0;
-	  frame.reinflate ();
 	}
     }
 
@@ -1173,7 +1171,6 @@ print_frame_info (const frame_print_options &fp_opts,
 
 	  print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
 	}
-      frame.reinflate ();
 
       /* If disassemble-next-line is set to on and there is line debug
 	 messages, output assembly codes for next line.  */
@@ -1676,8 +1673,6 @@ info_frame_command_core (frame_info_ptr fi, bool selected_frame_p)
 
 	print_frame_args (user_frame_print_options,
 			  func, fi, numargs, gdb_stdout);
-	fi.reinflate ();
-
 	gdb_puts ("\n");
       }
   }
@@ -2077,7 +2072,6 @@ backtrace_command_1 (const frame_print_options &fp_opts,
 	    print_frame_local_vars (fi, false, NULL, NULL, 1, gdb_stdout);
 
 	  /* Save the last frame to check for error conditions.  */
-	  fi.reinflate ();
 	  trailing = fi;
 	}
 
diff --git a/gdb/unittests/frame_info_ptr-selftests.c b/gdb/unittests/frame_info_ptr-selftests.c
index 47306113c7aa..fe1e7be35daa 100644
--- a/gdb/unittests/frame_info_ptr-selftests.c
+++ b/gdb/unittests/frame_info_ptr-selftests.c
@@ -41,7 +41,6 @@ user_created_frame_callee (frame_info_ptr frame)
   validate_user_created_frame (get_frame_id (frame));
 
   reinit_frame_cache ();
-  frame.reinflate ();
 
   validate_user_created_frame (get_frame_id (frame));
 
@@ -61,7 +60,6 @@ test_user_created_frame ()
      validate that the reinflation in both the callee and caller restore the
      same frame_info object.  */
   frame_info_ptr callees_frame_info = user_created_frame_callee (frame);
-  frame.reinflate ();
 
   validate_user_created_frame (get_frame_id (frame));
   SELF_CHECK (frame.get () == callees_frame_info.get ());
-- 
2.38.1


  parent reply	other threads:[~2022-12-14  3:46 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14  3:34 [PATCH v2 00/13] Make frame_info_ptr automatic Simon Marchi
2022-12-14  3:34 ` [PATCH v2 01/13] gdb: move type_map_instance to compile/compile.c Simon Marchi
2022-12-14  3:34 ` [PATCH v2 02/13] gdb: move compile_instance to compile/compile.h Simon Marchi
2022-12-14  3:34 ` [PATCH v2 03/13] gdb: remove language.h include from frame.h Simon Marchi
2022-12-14  3:34 ` [PATCH v2 04/13] gdb: move sect_offset and cu_offset to dwarf2/types.h Simon Marchi
2022-12-14  3:34 ` [PATCH v2 05/13] gdb: move call site types to call-site.h Simon Marchi
2022-12-14  3:34 ` [PATCH v2 06/13] gdb: move frame_info_ptr to frame.{c,h} Simon Marchi
2022-12-20 17:01   ` Bruno Larsen
2023-01-03 18:59     ` Simon Marchi
2022-12-14  3:34 ` [PATCH v2 07/13] gdb: add frame_id::user_created_p Simon Marchi
2022-12-14  3:34 ` [PATCH v2 08/13] gdb: add user-created frames to stash Simon Marchi
2022-12-14  3:34 ` [PATCH v2 09/13] gdb: add create_new_frame(frame_id) overload Simon Marchi
2022-12-14  3:34 ` [PATCH v2 10/13] gdb: make it possible to restore selected user-created frames Simon Marchi
2022-12-14  3:34 ` [PATCH v2 11/13] gdb: make user-created frames reinflatable Simon Marchi
2023-01-23 12:57   ` Tom de Vries
2023-01-23 14:34     ` Luis Machado
2023-01-24  3:55       ` Simon Marchi
2023-01-24  8:22         ` Luis Machado
2023-01-25  3:45           ` Simon Marchi
2023-01-30  8:49             ` Luis Machado
2023-01-30 16:20               ` Simon Marchi
2022-12-14  3:34 ` [PATCH v2 12/13] gdb: make frame_info_ptr grab frame level and id on construction Simon Marchi
2022-12-14  3:34 ` Simon Marchi [this message]
2022-12-20 16:57 ` [PATCH v2 00/13] Make frame_info_ptr automatic Bruno Larsen
2023-01-03 19:00   ` Simon Marchi
2023-01-03 19:09 ` Simon Marchi
2023-01-18 18:10 ` Tom Tromey
2023-01-19  3:40   ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221214033441.499512-14-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).