public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: add HtabPrinter to gdb-gdb.py.in
@ 2023-02-09 19:50 Simon Marchi
  2023-02-09 19:52 ` Simon Marchi
  2023-02-27 18:10 ` Tom Tromey
  0 siblings, 2 replies; 6+ messages in thread
From: Simon Marchi @ 2023-02-09 19:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

When debugging GDB, I find it a bit tedious to inspect htab_t objects.
It is possible to find the entries by poking at the fields, but it's
annoying to do each time.  I think a pretty printer would help.  Add a
basic one to gdb-gdb.py.

The pretty printer advertises itself as "array-like", and the result
looks like:

    (top-gdb) p bfcache
    $3 = htab_t with 3 elements = {0x6210003252a0, 0x62100032caa0, 0x62100033baa0}

The htab_t itself doesn't know about the type of pointed objects.  But
it's easy enough to cast the addresses to the right type to use them:

    (top-gdb) print *((btrace_frame_cache *) 0x6210003252a0)
    $6 = {tp = 0x61700002ed80, frame = 0x6210003251e0, bfun = 0x62000000b390}

Change-Id: Ia692e3555fe7a117b7ec087840246b1260a704c6
---
 gdb/gdb-gdb.py.in | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in
index 95b7d84966f0..31ce80bb1e44 100644
--- a/gdb/gdb-gdb.py.in
+++ b/gdb/gdb-gdb.py.in
@@ -369,6 +369,40 @@ class IntrusiveListPrinter:
         return self._children_generator()
 
 
+class HtabPrinter:
+    """Pretty-printer for htab_t hash tables."""
+
+    def __init__(self, val):
+        self._val = val
+        self._entries = val["entries"]
+        self._size = int(val["size"])
+
+    def display_hint(self):
+        return "array"
+
+    def to_string(self):
+        n = int(self._val["n_elements"]) - int(self._val["n_deleted"])
+        return "htab_t with {} elements".format(n)
+
+    def _children_generator(self):
+        size = int(self._val["size"])
+        entries = self._val["entries"]
+
+        child_i = 0
+        for entries_i in range(size):
+            entry = entries[entries_i]
+            # 0 (NULL pointer) means there's nothing, 1 (HTAB_DELETED_ENTRY)
+            # means there was something, but is now deleted.
+            if int(entry) in (0, 1):
+                continue
+
+            yield (str(child_i), entry)
+            child_i += 1
+
+    def children(self):
+        return self._children_generator()
+
+
 def type_lookup_function(val):
     """A routine that returns the correct pretty printer for VAL
     if appropriate.  Returns None otherwise.
@@ -383,6 +417,8 @@ def type_lookup_function(val):
         return CoreAddrPrettyPrinter(val)
     elif tag is not None and tag.startswith("intrusive_list<"):
         return IntrusiveListPrinter(val)
+    elif name == "htab_t":
+        return HtabPrinter(val)
     return None
 
 
-- 
2.39.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-02-28 16:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09 19:50 [PATCH] gdb: add HtabPrinter to gdb-gdb.py.in Simon Marchi
2023-02-09 19:52 ` Simon Marchi
2023-02-27 18:10 ` Tom Tromey
2023-02-28  4:23   ` Simon Marchi
2023-02-28 14:30     ` Tom Tromey
2023-02-28 16:08       ` Simon Marchi

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).