From patchwork Wed Aug 16 20:11:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 22168 Received: (qmail 98901 invoked by alias); 16 Aug 2017 20:11:42 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 98886 invoked by uid 89); 16 Aug 2017 20:11:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=123456789, 3097, 2636, kicking X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 16 Aug 2017 20:11:39 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BA87326AC45 for ; Wed, 16 Aug 2017 20:11:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BA87326AC45 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=palves@redhat.com Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2F7E600CA; Wed, 16 Aug 2017 20:11:36 +0000 (UTC) Subject: Re: [PATCH] Fix type casts losing typedefs and reimplement "whatis" typedef stripping To: Phil Muldoon References: <1498837699-20897-1-git-send-email-palves@redhat.com> Cc: GDB Patches From: Pedro Alves Message-ID: Date: Wed, 16 Aug 2017 21:11:35 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 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 * 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)