public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 3/7] Move follow_static_link to frame.c
Date: Wed, 01 Nov 2023 11:09:35 -0600	[thread overview]
Message-ID: <20231101-dap-nested-function-v1-3-0b0c3b228ac7@adacore.com> (raw)
In-Reply-To: <20231101-dap-nested-function-v1-0-0b0c3b228ac7@adacore.com>

This moves the follow_static_link function to frame.c and exports it
for use elsewhere.  The API is changed slightly to make it more
generically useful.
---
 gdb/findvar.c | 59 ++++++-----------------------------------------------------
 gdb/frame.c   | 40 ++++++++++++++++++++++++++++++++++++++++
 gdb/frame.h   |  7 +++++++
 3 files changed, 53 insertions(+), 53 deletions(-)

diff --git a/gdb/findvar.c b/gdb/findvar.c
index 1079b85df82..952ec20c0b7 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -31,7 +31,6 @@
 #include "block.h"
 #include "objfiles.h"
 #include "language.h"
-#include "dwarf2/loc.h"
 #include "gdbsupport/selftest.h"
 
 /* Basic byte-swapping routines.  All 'extract' functions return a
@@ -391,41 +390,6 @@ symbol_read_needs_frame (struct symbol *sym)
   return symbol_read_needs (sym) == SYMBOL_NEEDS_FRAME;
 }
 
-/* Given static link expression and the frame it lives in, look for the frame
-   the static links points to and return it.  Return NULL if we could not find
-   such a frame.   */
-
-static frame_info_ptr
-follow_static_link (frame_info_ptr frame,
-		    const struct dynamic_prop *static_link)
-{
-  CORE_ADDR upper_frame_base;
-
-  if (!dwarf2_evaluate_property (static_link, frame, NULL, &upper_frame_base))
-    return NULL;
-
-  /* Now climb up the stack frame until we reach the frame we are interested
-     in.  */
-  for (; frame != NULL; 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;
-}
-
 /* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical
    rules, look for the frame that is actually hosting VAR and return it.  If,
    for some reason, we found no such frame, return NULL.
@@ -494,25 +458,14 @@ get_hosting_frame (struct symbol *var, const struct block *var_block,
       /* Assuming we have a block for this frame: if we are at the function
 	 level, the immediate upper lexical block is in an outer function:
 	 follow the static link.  */
-      else if (frame_block->function ())
+      else if (frame_block->function () != nullptr)
 	{
-	  const struct dynamic_prop *static_link
-	    = frame_block->static_link ();
-	  int could_climb_up = 0;
-
-	  if (static_link != NULL)
-	    {
-	      frame = follow_static_link (frame, static_link);
-	      if (frame != NULL)
-		{
-		  frame_block = get_frame_block (frame, NULL);
-		  could_climb_up = frame_block != NULL;
-		}
-	    }
-	  if (!could_climb_up)
+	  frame = frame_follow_static_link (frame);
+	  if (frame != nullptr)
 	    {
-	      frame = NULL;
-	      break;
+	      frame_block = get_frame_block (frame, nullptr);
+	      if (frame_block == nullptr)
+		frame = nullptr;
 	    }
 	}
 
diff --git a/gdb/frame.c b/gdb/frame.c
index 7077016ccba..4a46ccb9abc 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -43,6 +43,7 @@
 #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
@@ -3120,6 +3121,45 @@ get_frame_sp (frame_info_ptr this_frame)
   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
diff --git a/gdb/frame.h b/gdb/frame.h
index 1d7422cac32..19bf8176682 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -640,6 +640,13 @@ class address_space;
 /* Return the frame's address space.  */
 extern const address_space *get_frame_address_space (frame_info_ptr);
 
+/* A frame may have a "static link".  That is, in some languages, a
+   nested function may have access to variables from the enclosing
+   block and frame.  This function looks for a frame's static link.
+   If found, returns the corresponding frame; otherwise, returns a
+   null frame_info_ptr.  */
+extern frame_info_ptr frame_follow_static_link (frame_info_ptr frame);
+
 /* For frames where we can not unwind further, describe why.  */
 
 enum unwind_stop_reason

-- 
2.40.1


  parent reply	other threads:[~2023-11-01 17:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-01 17:09 [PATCH 0/7] Handle nested functions in DAP Tom Tromey
2023-11-01 17:09 ` [PATCH 1/7] Add two convenience methods to block Tom Tromey
2023-11-01 17:09 ` [PATCH 2/7] Add block::function_block Tom Tromey
2023-11-01 17:09 ` Tom Tromey [this message]
2023-11-01 17:09 ` [PATCH 4/7] Add gdb.Frame.static_link method Tom Tromey
2023-11-01 17:17   ` Eli Zaretskii
2023-11-01 17:09 ` [PATCH 5/7] Fix a bug in DAP scopes code Tom Tromey
2023-11-01 17:09 ` [PATCH 6/7] Handle the static link in FrameDecorator Tom Tromey
2023-11-15 12:31   ` Tom de Vries
2023-11-15 13:58     ` Tom Tromey
2023-11-15 14:07       ` Tom de Vries
2023-11-15 14:58         ` Tom Tromey
2023-11-01 17:09 ` [PATCH 7/7] Update gdb.Symbol.is_variable documentation Tom Tromey
2023-11-01 17:15   ` Eli Zaretskii
2023-11-14 15:39 ` [PATCH 0/7] Handle nested functions in DAP Tom Tromey
2023-11-14 16:11   ` Tom Tromey

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=20231101-dap-nested-function-v1-3-0b0c3b228ac7@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /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).