From: Pedro Alves <palves@redhat.com>
To: Phil Muldoon <pmuldoon@redhat.com>
Cc: GDB Patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH] Fix type casts losing typedefs and reimplement "whatis" typedef stripping
Date: Wed, 16 Aug 2017 20:11:00 -0000 [thread overview]
Message-ID: <d727c2c0-9e19-9e7e-c1c0-38b3345f0499@redhat.com> (raw)
In-Reply-To: <1498837699-20897-1-git-send-email-palves@redhat.com>
On 06/30/2017 04:48 PM, Pedro Alves wrote:
> This prevents a type printer for "int_t" kicking in, with e.g.:
I've finally written a pretty printer test that actually tests
this.
Phil, WDYT?
gdb/testsuite/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>
* gdb.python/py-prettyprint.c (int_type, int_type2): New typedefs.
(an_int, an_int_type, an_int_type2): New globals.
* gdb.python/py-prettyprint.exp (run_lang_tests): Add tests
involving typedefs and cast expressions.
* gdb.python/py-prettyprint.py (class pp_int_type): New.
(lookup_typedefs_function): New.
(typedefs_pretty_printers_dict): New.
(top level): Register lookup_typedefs_function in gdb.pretty_printers.
---
gdb/testsuite/gdb.python/py-prettyprint.c | 9 ++++++
gdb/testsuite/gdb.python/py-prettyprint.exp | 13 +++++++++
gdb/testsuite/gdb.python/py-prettyprint.py | 40 +++++++++++++++++++++++++++
3 files changed, 62 insertions(+)
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.c b/gdb/testsuite/gdb.python/py-prettyprint.c
index fd58358..82f9fe7 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.c
+++ b/gdb/testsuite/gdb.python/py-prettyprint.c
@@ -257,6 +257,15 @@ bug_14741()
set_item(&c, 0, 5);
}
+/* Some typedefs/variables for checking that GDB doesn't lose typedefs
+ when looking for a printer. */
+typedef int int_type;
+typedef int_type int_type2;
+
+int an_int = -1;
+int_type an_int_type = 1;
+int_type2 an_int_type2 = 2;
+
int
main ()
{
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.exp b/gdb/testsuite/gdb.python/py-prettyprint.exp
index b0a9e32..9c52e9c 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.exp
+++ b/gdb/testsuite/gdb.python/py-prettyprint.exp
@@ -110,6 +110,19 @@ proc run_lang_tests {exefile lang} {
gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \
"print nstype on one line"
+ # Check that GDB doesn't lose typedefs when looking for a printer.
+ gdb_test "print an_int" " = -1"
+ gdb_test "print (int) an_int" " = -1"
+ gdb_test "print (int_type) an_int" " = an int_type, val=-1"
+
+ gdb_test "print an_int_type" " = an int_type, val=1"
+ gdb_test "print (int_type) an_int_type" " = an int_type, val=1"
+
+ gdb_test "print an_int_type2" " = an int_type, val=2"
+ gdb_test "print (int) an_int_type2" " = 2"
+ gdb_test "print (int_type) an_int_type2" " = an int_type, val=2"
+ gdb_test "print (int_type2) an_int_type2" " = an int_type, val=2"
+
gdb_continue_to_end
}
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.py b/gdb/testsuite/gdb.python/py-prettyprint.py
index c56f564..96ad0f9 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.py
+++ b/gdb/testsuite/gdb.python/py-prettyprint.py
@@ -227,6 +227,13 @@ class pp_eval_type (object):
gdb.execute("bt", to_string=True)
return "eval=<" + str(gdb.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
+class pp_int_type (object):
+ def __init__(self, val):
+ self.val = val
+
+ def to_string(self):
+ return "an int_type, val=%s" % int(self.val)
+
def lookup_function (val):
"Look-up and return a pretty-printer that can print val."
@@ -263,6 +270,33 @@ def disable_lookup_function ():
def enable_lookup_function ():
lookup_function.enabled = True
+# Lookup a printer for VAL in the typedefs dict.
+def lookup_typedefs_function (val):
+ "Look-up and return a pretty-printer that can print val (typedefs)."
+
+ # Get the type.
+ type = val.type
+
+ # Get the unqualified type.
+ type = type.unqualified ()
+
+ # Iterate over local dictionary of types to determine if a printer
+ # is registered for that type. Return an instantiation of the
+ # printer if found.
+ for function in typedefs_pretty_printers_dict:
+ # Compare the type name, stripping typedefs, one layer at a
+ # time.
+ while type != None:
+ if type.name != None and function.match (type.name):
+ return typedefs_pretty_printers_dict[function] (val)
+ if type.code != gdb.TYPE_CODE_TYPEDEF:
+ break
+ type = type.target ()
+
+ # Cannot find a pretty printer. Return None.
+
+ return None
+
def register_pretty_printers ():
pretty_printers_dict[re.compile ('^struct s$')] = pp_s
pretty_printers_dict[re.compile ('^s$')] = pp_s
@@ -309,7 +343,13 @@ def register_pretty_printers ():
pretty_printers_dict[re.compile ('^eval_type_s$')] = pp_eval_type
+ typedefs_pretty_printers_dict[re.compile ('^int_type$')] = pp_int_type
+
+# Dict for struct types with typedefs fully stripped.
pretty_printers_dict = {}
+# Dict for typedef types.
+typedefs_pretty_printers_dict = {}
register_pretty_printers ()
gdb.pretty_printers.append (lookup_function)
+gdb.pretty_printers.append (lookup_typedefs_function)
next prev parent reply other threads:[~2017-08-16 20:11 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-30 15:48 Pedro Alves
2017-08-16 20:11 ` Pedro Alves [this message]
2017-08-21 10:39 ` Pedro Alves
2017-11-18 20:57 ` Yao Qi
2017-11-18 22:58 ` Simon Marchi
2017-11-20 16:42 ` Pedro Alves
2017-11-20 17:01 ` Simon Marchi
2017-11-20 22:35 ` Yao Qi
2017-11-20 23:11 ` [pushed] Fix gdb.base/whatis-ptype-typedefs.exp on 32-bit archs (Re: [PATCH] Fix type casts losing typedefs and reimplement "whatis" typedef stripping) Pedro Alves
2017-11-20 23:06 ` [PATCH] Fix type casts losing typedefs and reimplement "whatis" typedef stripping Andreas Schwab
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=d727c2c0-9e19-9e7e-c1c0-38b3345f0499@redhat.com \
--to=palves@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=pmuldoon@redhat.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