public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tom@tromey.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Fix htab_t pretty-printer for NULL values
Date: Fri, 10 Mar 2023 22:15:50 -0500	[thread overview]
Message-ID: <41ac36c3-2c31-c78b-9d48-57be9402bf14@simark.ca> (raw)
In-Reply-To: <20230308205628.4021427-1-tom@tromey.com>



On 3/8/23 15:56, Tom Tromey wrote:
> I was debugging gdb and was surprised to see this in a
> compunit_symtab:
> 
>   m_call_site_htab = <error reading variable: Cannot access memory at address 0x28>,
> 
> Debugging showed that the field was not uninitialized; rather, the
> htab_t pretty-printer did not cope with NULL.
> ---
>  gdb/gdb-gdb.py.in                       | 5 +++++
>  gdb/testsuite/gdb.gdb/python-helper.exp | 2 ++
>  2 files changed, 7 insertions(+)
> 
> diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in
> index 56d063cd506..567ec41b305 100644
> --- a/gdb/gdb-gdb.py.in
> +++ b/gdb/gdb-gdb.py.in
> @@ -379,10 +379,15 @@ class HtabPrinter:
>          return "array"
>  
>      def to_string(self):
> +        if int(self._val) == 0:
> +            return "(null)"
>          n = int(self._val["n_elements"]) - int(self._val["n_deleted"])
>          return "htab_t with {} elements".format(n)

It would make me tremendously happy if you put a blank line after the
return null :).

>  
>      def children(self):
> +        if int(self._val) == 0:
> +            return
> +
>          size = int(self._val["size"])
>          entries = self._val["entries"]
>  
> diff --git a/gdb/testsuite/gdb.gdb/python-helper.exp b/gdb/testsuite/gdb.gdb/python-helper.exp
> index 16b419984cf..be0b311e8ca 100644
> --- a/gdb/testsuite/gdb.gdb/python-helper.exp
> +++ b/gdb/testsuite/gdb.gdb/python-helper.exp
> @@ -211,6 +211,8 @@ proc test_python_helper {} {
>      # Test the htab_t pretty-printer.
>      gdb_test -prompt $outer_prompt_re "print all_bfds" "htab_t with ${::decimal} elements = \\{${::hex}.*\\}"
>  
> +    gdb_test -prompt $outer_prompt_re "print (htab_t) 0x0" \
> +	[string_to_regexp " = (null)"]

Instead of hardcoding (null), what about not selecting the pretty
printer when the value is nullptr?  Like this:


diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in
index 567ec41b305b..5092512eaca9 100644
--- a/gdb/gdb-gdb.py.in
+++ b/gdb/gdb-gdb.py.in
@@ -379,15 +379,10 @@ class HtabPrinter:
         return "array"
 
     def to_string(self):
-        if int(self._val) == 0:
-            return "(null)"
         n = int(self._val["n_elements"]) - int(self._val["n_deleted"])
         return "htab_t with {} elements".format(n)
 
     def children(self):
-        if int(self._val) == 0:
-            return
-
         size = int(self._val["size"])
         entries = self._val["entries"]
 
@@ -418,7 +413,9 @@ def type_lookup_function(val):
     elif tag is not None and tag.startswith("intrusive_list<"):
         return IntrusiveListPrinter(val)
     elif name == "htab_t":
-        return HtabPrinter(val)
+        if int(val) != 0:
+            return HtabPrinter(val)
+
     return None
 
Can the value's value change after a pretty printer was selected for it?
In that case, my idea wouldn't work, because the value could become
nullptr under the pretty printer's feet.

Simon

  reply	other threads:[~2023-03-11  3:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-08 20:56 Tom Tromey
2023-03-11  3:15 ` Simon Marchi [this message]
2023-03-11 15:09   ` 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=41ac36c3-2c31-c78b-9d48-57be9402bf14@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /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).