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 2/2] Add two new pretty-printer methods
Date: Mon, 11 Sep 2023 11:28:26 -0600	[thread overview]
Message-ID: <20230911-pp-v2-v1-2-c4e0d40c8b63@adacore.com> (raw)
In-Reply-To: <20230911-pp-v2-v1-0-c4e0d40c8b63@adacore.com>

This adds two new pretty-printer methods, to support random access to
children.  The methods are implemented for the no-op array printer,
and DAP is updated to use this.
---
 gdb/doc/python.texi              | 16 ++++++++++++++++
 gdb/python/lib/gdb/dap/varref.py | 22 +++++++++++++++-------
 gdb/python/lib/gdb/printing.py   |  9 ++++++---
 3 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index d09f5e03997..79438baf477 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1828,6 +1828,22 @@ are peformed in this method and nothing is printed.
 If the result is not one of these types, an exception is raised.
 @end defun
 
+@defun pretty_printer.num_children ()
+This is not a basic method, so @value{GDBN} will only ever call it for
+objects derived from @code{gdb.ValuePrinter}.
+
+If available, this method should return the number of children.
+@code{None} may be returned if the number can't readily be computed.
+@end defun
+
+@defun pretty_printer.child (n)
+This is not a basic method, so @value{GDBN} will only ever call it for
+objects derived from @code{gdb.ValuePrinter}.
+
+If available, this method should return the child value indicated by
+@var{n}.  Indices start at zero.
+@end defun
+
 @value{GDBN} provides a function which can be used to look up the
 default pretty-printer for a @code{gdb.Value}:
 
diff --git a/gdb/python/lib/gdb/dap/varref.py b/gdb/python/lib/gdb/dap/varref.py
index c1e5ba8686d..768d768c7b3 100644
--- a/gdb/python/lib/gdb/dap/varref.py
+++ b/gdb/python/lib/gdb/dap/varref.py
@@ -151,9 +151,9 @@ class VariableReference(BaseReference):
             # This discards all laziness.  This could be improved
             # slightly by lazily evaluating children, but because this
             # code also generally needs to know the number of
-            # children, it probably wouldn't help much.  A real fix
-            # would require an update to gdb's pretty-printer protocol
-            # (though of course that is probably also inadvisable).
+            # children, it probably wouldn't help much.  Note that
+            # this is only needed with legacy (non-ValuePrinter)
+            # printers.
             self.child_cache = list(self.printer.children())
         return self.child_cache
 
@@ -161,9 +161,12 @@ class VariableReference(BaseReference):
         if self.count is None:
             return None
         if self.count == -1:
-            if hasattr(self.printer, "num_children"):
-                num_children = self.printer.num_children
-            else:
+            num_children = None
+            if isinstance(self.printer, gdb.ValuePrinter) and hasattr(
+                self.printer, "num_children"
+            ):
+                num_children = self.printer.num_children()
+            if num_children is None:
                 num_children = len(self.cache_children())
             self.count = num_children
         return self.count
@@ -193,7 +196,12 @@ class VariableReference(BaseReference):
 
     @in_gdb_thread
     def fetch_one_child(self, idx):
-        return self.cache_children()[idx]
+        if isinstance(self.printer, gdb.ValuePrinter) and hasattr(
+                self.printer, "child"
+        ):
+            return self.printer.child(idx)
+        else:
+            return self.cache_children()[idx]
 
 
 @in_gdb_thread
diff --git a/gdb/python/lib/gdb/printing.py b/gdb/python/lib/gdb/printing.py
index 80851590134..4982abc3f77 100644
--- a/gdb/python/lib/gdb/printing.py
+++ b/gdb/python/lib/gdb/printing.py
@@ -299,9 +299,6 @@ class NoOpArrayPrinter(gdb.ValuePrinter):
             e_values = itertools.takewhile(lambda x: x.enumval <= high, e_values)
             low = 0
             high = len(list(e_values)) - 1
-        # This is a convenience to the DAP code and perhaps other
-        # users.
-        self.num_children = high - low + 1
         self.__low = low
         self.__high = high
 
@@ -311,6 +308,12 @@ class NoOpArrayPrinter(gdb.ValuePrinter):
     def display_hint(self):
         return "array"
 
+    def num_children(self):
+        return self.__high - self.__low + 1
+
+    def child(self, i):
+        return (self.__low + i, self.__value[self.__low + i])
+
     def children(self):
         for i in range(self.__low, self.__high + 1):
             yield (i, self.__value[i])

-- 
2.40.1


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

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-11 17:28 [PATCH 0/2] Add pretty-printer base class and new methods Tom Tromey
2023-09-11 17:28 ` [PATCH 1/2] Introduce gdb.ValuePrinter Tom Tromey
2023-09-11 19:04   ` Eli Zaretskii
2023-09-26 14:08     ` Tom Tromey
2023-09-11 17:28 ` Tom Tromey [this message]
2023-09-11 19:00   ` [PATCH 2/2] Add two new pretty-printer methods Eli Zaretskii
2023-09-26 14:09 ` [PATCH 0/2] Add pretty-printer base class and new methods 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=20230911-pp-v2-v1-2-c4e0d40c8b63@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).