aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@efficios.com>2023-02-09 14:50:56 -0500
committerSimon Marchi <simon.marchi@efficios.com>2023-02-28 11:05:57 -0500
commit139f66c7287806a50dbee598e194ba869b7955d4 (patch)
tree2d44997ace353e8cf3224ee8131ab1b4d653aae3
parent0c132dac7f21062f19d47982697f0a223ad430ed (diff)
downloadbinutils-139f66c7287806a50dbee598e194ba869b7955d4.zip
binutils-139f66c7287806a50dbee598e194ba869b7955d4.tar.gz
binutils-139f66c7287806a50dbee598e194ba869b7955d4.tar.bz2
gdb: add HtabPrinter to gdb-gdb.py.in
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 Reviewed-By: Tom Tromey <tom@tromey.com>
-rw-r--r--gdb/gdb-gdb.py.in31
-rw-r--r--gdb/testsuite/gdb.gdb/python-helper.exp3
2 files changed, 34 insertions, 0 deletions
diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in
index 95b7d84..56d063c 100644
--- a/gdb/gdb-gdb.py.in
+++ b/gdb/gdb-gdb.py.in
@@ -369,6 +369,35 @@ class IntrusiveListPrinter:
return self._children_generator()
+class HtabPrinter:
+ """Pretty-printer for htab_t hash tables."""
+
+ def __init__(self, val):
+ self._val = val
+
+ 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(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 type_lookup_function(val):
"""A routine that returns the correct pretty printer for VAL
if appropriate. Returns None otherwise.
@@ -383,6 +412,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
diff --git a/gdb/testsuite/gdb.gdb/python-helper.exp b/gdb/testsuite/gdb.gdb/python-helper.exp
index f1e95fb..16b4199 100644
--- a/gdb/testsuite/gdb.gdb/python-helper.exp
+++ b/gdb/testsuite/gdb.gdb/python-helper.exp
@@ -208,6 +208,9 @@ proc test_python_helper {} {
" cplus_stuff = $hex}"]
gdb_test -prompt $outer_prompt_re "print *type->main_type" $answer
+ # Test the htab_t pretty-printer.
+ gdb_test -prompt $outer_prompt_re "print all_bfds" "htab_t with ${::decimal} elements = \\{${::hex}.*\\}"
+
return 0
}