From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 744603858D39 for ; Tue, 28 Feb 2023 04:23:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 744603858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca Received: from [10.0.0.11] (unknown [217.28.27.60]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id E12861E128; Mon, 27 Feb 2023 23:23:55 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1677558236; bh=jDwzVBZHGNfI4UqRnhysOAqOkxirUbsm+IZEa0ctccQ=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=UrJ50jmU7onkhmd0h9KZqHSnHUWAudXwA48LKoak8pCVy8hU+RlfEmru4byNlpFgl 00lx+N0Ngo9Lo8AFjTPYj4s3JO4qVi4WB9HK3bYwDfRDrJz6iVYWbHuDStA9COx2yS atytTZBwzsevBRp7UxSd3iz+wOjJJbVdM2XYv/EM= Message-ID: <62de3d6a-600b-4bd0-2bf0-73feff460fb2@simark.ca> Date: Mon, 27 Feb 2023 23:23:55 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH] gdb: add HtabPrinter to gdb-gdb.py.in Content-Language: en-US To: Tom Tromey , Simon Marchi via Gdb-patches Cc: Simon Marchi References: <20230209195056.100681-1-simon.marchi@efficios.com> <87edqbdlo1.fsf@tromey.com> From: Simon Marchi In-Reply-To: <87edqbdlo1.fsf@tromey.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2/27/23 13:10, Tom Tromey wrote: >>>>>> "Simon" == Simon Marchi via Gdb-patches writes: > > Simon> When debugging GDB, I find it a bit tedious to inspect htab_t objects. > Simon> It is possible to find the entries by poking at the fields, but it's > Simon> annoying to do each time. I think a pretty printer would help. Add a > Simon> basic one to gdb-gdb.py. > > I could have used this today :-} > > Simon> + def _children_generator(self): > Simon> + size = int(self._val["size"]) > Simon> + entries = self._val["entries"] > Simon> + > Simon> + child_i = 0 > Simon> + for entries_i in range(size): > Simon> + entry = entries[entries_i] > Simon> + # 0 (NULL pointer) means there's nothing, 1 (HTAB_DELETED_ENTRY) > Simon> + # means there was something, but is now deleted. > Simon> + if int(entry) in (0, 1): > Simon> + continue > Simon> + > Simon> + yield (str(child_i), entry) > Simon> + child_i += 1 > Simon> + > Simon> + def children(self): > Simon> + return self._children_generator() > > I don't think the indirection here is needed, you can just have children > yield. But, either way seems fine to me. > > Reviewed-By: Tom Tromey Ah, you're right. Well, here's an updated version that also adds a test in gdb.gdb/python-helper.exp. >From 69259c470aaad72b28b4d34a5f1d0a999f660582 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 9 Feb 2023 14:50:56 -0500 Subject: [PATCH] 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 --- gdb/gdb-gdb.py.in | 31 +++++++++++++++++++++++++ gdb/testsuite/gdb.gdb/python-helper.exp | 3 +++ 2 files changed, 34 insertions(+) diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in index 95b7d84966f0..56d063cd506b 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 f1e95fbe5ee7..16b419984cf3 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 } base-commit: 26c294bd1b8d95b421487294863eb1560b65580d -- 2.39.2