From patchwork Thu Dec 7 19:53:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Domani X-Patchwork-Id: 81705 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 07DA43858416 for ; Thu, 7 Dec 2023 19:54:16 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from sonic311-31.consmr.mail.ir2.yahoo.com (sonic311-31.consmr.mail.ir2.yahoo.com [77.238.176.163]) by sourceware.org (Postfix) with ESMTPS id AC01F3858D20 for ; Thu, 7 Dec 2023 19:53:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC01F3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=yahoo.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=yahoo.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AC01F3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=77.238.176.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701978842; cv=none; b=p1nYCQG3NpdHmWZlpZ0BXZ5eWmQ0ufbW9TLe9RuAHnIOL0fsDvI3QWEtnQQZUzSWDNnbH1CkkuCBAIQSqSHy0bcqXTG2OMEy8pHcgbJvF4YnDQscJpNFmcMOPXdm2iGZQhazapBfe2AKObrCKt5qvO5kgzSUli/Sp/AFkHkKLFA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701978842; c=relaxed/simple; bh=mdaUuSQB45PrXkq/VyT+b+v/IEKsf/A9WqhUJ1iyAZ4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=JIT/HBk+4NpUbH4SOnhngSzWGktAFgc1k9nE2agolUQnXVXb0Y7u6RyZAdl3DmQWdVM/wCgNPQ13IOQtijCUUhgzoZkodFQ7zTpSEzTmmmMW4yVJk1OMyE7tEbDyRwxQWBa+CvcHg1gG4+5jV8NPxuEZgkL+AulaCfCTd+XL9kc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.de; s=s2048; t=1701978837; bh=A2nTazprzOCSOgdjf9m3E6xNicZQP9hYeeLlcqUe4Rc=; h=From:To:Subject:Date:References:From:Subject:Reply-To; b=ctZOYVs1+CpF1MFgeABRDEvXAtKeNCSges9e25wNJ5SeBv4kfn8pKNqarNNElXdnQq1OE1SgTtPgqO4EYeUr9v/5bE8R+W1SsdC7vH41dqvO+B/SgZAIS8We1xv+/GpcUNhO0c6p3+dI/l6BTQQEeLf8a9gCZgA6qEMg5DCP7D/mb2lzKSe4lfPcBx2Ta+hNheOnIVWgkZyJ0hAr0jEHGddpuKikS/lCCGmZZzodx7GfJ69gHN6+J+nzfmzUGrCZvbuQ1jE2Bt0gEoKJqkMs1Mo4HGMRtqYyIh7lUSjCNDbeb1MjjNvJrKnu3Sa51o8tC5YdUz4qA7w+ZFXTrznJZQ== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1701978837; bh=mOH43Zqao4nGVHfNzZA18YAMGnw7cxbwh8G0YxZ585r=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=Ygy2io9sykwhpD1W3pQyNZipALRttnbSt39tcHAu61P3mXshRy7paTmyw4+mb0L3lUvYzrKNi/bxkLQpUC4ruQSMhS/oF+NrT3WzJdVgc/qadOeAn/R91cC5L4YXdsxLiDrrke0tRokY/iSnrpmwEV4kR73PKmIFU0zuPVVXNCfbAv5/gX45cZDfn/97VPF/plJ5Ggfaw9D3SSw3w2COv0IZ/5Xpxr9Hc2Ng2/8hQZ9saev69NDR0HOs4WyLay89LiFTR0v5ACGc1gY8RDcEVYOZE06Get827K42/9rcsixUU28bJbA1v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du73bF6TrVpYdJU.FM _phZQvaqJ8gnunUw4ubSJQEq.bt0KnMAWMrruZwOKpFMDTQY3ecsDR7uqVnaWV2JcCAyTfZNBW9I uJfHnmv8G4S0HQ4B7TkxyWPMcDO1KjInQ1Rj_Q700xWUYay8YDXyvab9WJDqUmJPSP5ECjIGKrD8 IZEo4TJdK8mIUJZ.uSPCCFWVSP0sKTqbLdPBS X-Sonic-MF: X-Sonic-ID: f9d06381-3de9-430c-ae82-769095dd76b9 Received: from sonic.gate.mail.ne1.yahoo.com by sonic311.consmr.mail.ir2.yahoo.com with HTTP; Thu, 7 Dec 2023 19:53:57 +0000 Received: by hermes--production-ir2-95776f84c-xx55z (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID e9b5aa3467c38afb6e3ff72faf1fd685; Thu, 07 Dec 2023 19:53:53 +0000 (UTC) From: Hannes Domani To: gdb-patches@sourceware.org Subject: [PATCH] Use pretty printers for struct member stubs Date: Thu, 7 Dec 2023 20:53:31 +0100 Message-Id: <20231207195331.3250-1-ssbssa@yahoo.de> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Antivirus: Avast (VPS 231207-2, 12/07/2023), Outbound message X-Antivirus-Status: Clean References: <20231207195331.3250-1-ssbssa.ref@yahoo.de> X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org PR29079 shows that pretty printers can be used for an incomplete type (stub), but only when printing it directly, not if it's part of another struct: ``` (gdb) p s $1 = {pp m_i = 5} (gdb) p s2 $2 = {m_s = , m_l = 20} ``` The reason is simply that in common_val_print the check for stubs is before any pretty printer is tried. It works if the pretty printer is tried before the stub check: ``` (gdb) p s $1 = {pp m_i = 5} (gdb) p s2 $2 = {m_s = {pp m_i = 10}, m_l = 20} ``` Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29079 --- .../gdb.python/py-prettyprint-stub-2.cc | 26 ++++++++ .../gdb.python/py-prettyprint-stub.cc | 36 +++++++++++ .../gdb.python/py-prettyprint-stub.exp | 59 +++++++++++++++++++ .../gdb.python/py-prettyprint-stub.h | 24 ++++++++ .../gdb.python/py-prettyprint-stub.py | 38 ++++++++++++ gdb/valprint.c | 20 +++---- 6 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 gdb/testsuite/gdb.python/py-prettyprint-stub-2.cc create mode 100644 gdb/testsuite/gdb.python/py-prettyprint-stub.cc create mode 100644 gdb/testsuite/gdb.python/py-prettyprint-stub.exp create mode 100644 gdb/testsuite/gdb.python/py-prettyprint-stub.h create mode 100644 gdb/testsuite/gdb.python/py-prettyprint-stub.py diff --git a/gdb/testsuite/gdb.python/py-prettyprint-stub-2.cc b/gdb/testsuite/gdb.python/py-prettyprint-stub-2.cc new file mode 100644 index 00000000000..41a949d4425 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-prettyprint-stub-2.cc @@ -0,0 +1,26 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "py-prettyprint-stub.h" + +S::S (int i) : m_i (i) +{ +} + +S::~S () +{ +} diff --git a/gdb/testsuite/gdb.python/py-prettyprint-stub.cc b/gdb/testsuite/gdb.python/py-prettyprint-stub.cc new file mode 100644 index 00000000000..9b0aef3bf8e --- /dev/null +++ b/gdb/testsuite/gdb.python/py-prettyprint-stub.cc @@ -0,0 +1,36 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "py-prettyprint-stub.h" + +struct S2 +{ + S2 (int i, long l) : m_s (i), m_l (l) + { + } + + S m_s; + long m_l; +}; + +int main () +{ + S s (5); + S2 s2 (10, 20); + + return 0; /* Break here. */ +} diff --git a/gdb/testsuite/gdb.python/py-prettyprint-stub.exp b/gdb/testsuite/gdb.python/py-prettyprint-stub.exp new file mode 100644 index 00000000000..79492ec5e5d --- /dev/null +++ b/gdb/testsuite/gdb.python/py-prettyprint-stub.exp @@ -0,0 +1,59 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. +# It tests Python-based pretty-printing of stubs. + +load_lib gdb-python.exp + +require allow_python_tests + +standard_testfile .cc py-prettyprint-stub-2.cc + +set srcfiles [list $srcfile $srcfile2] + +if { [build_executable_from_specs \ + "failed to prepare" \ + $testfile {c++} \ + $srcfile {c++ debug} \ + $srcfile2 {c++}] == -1 } { + return -1 +} + +# Start with a fresh gdb. +clean_restart $testfile + +if {![runto_main]} { + return +} + +set remote_python_file [gdb_remote_download host \ + ${srcdir}/${subdir}/${testfile}.py] + +gdb_breakpoint [gdb_get_line_number "Break here."] +gdb_continue_to_breakpoint "Break here" ".*Break here.*" + +# First test without pretty printer. +gdb_test "print s" " = " +gdb_test "print s2" " = {m_s = , m_l = 20}" + +# Load pretty printer. +gdb_test_no_output "source ${remote_python_file}" "load python file" + +# Test with pretty printer. +with_test_prefix pp { + gdb_test "print s" " = {pp m_i = 5}" + gdb_test "print s2" " = {m_s = {pp m_i = 10}, m_l = 20}" +} diff --git a/gdb/testsuite/gdb.python/py-prettyprint-stub.h b/gdb/testsuite/gdb.python/py-prettyprint-stub.h new file mode 100644 index 00000000000..1bb56c7c65a --- /dev/null +++ b/gdb/testsuite/gdb.python/py-prettyprint-stub.h @@ -0,0 +1,24 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +struct S +{ + S (int); + virtual ~S (); + + int m_i; +}; diff --git a/gdb/testsuite/gdb.python/py-prettyprint-stub.py b/gdb/testsuite/gdb.python/py-prettyprint-stub.py new file mode 100644 index 00000000000..2315494c121 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-prettyprint-stub.py @@ -0,0 +1,38 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. +# It tests Python-based pretty-printing of stubs. + + +class SPrinter: + def __init__(self, val): + self.val = val + + def to_string(self): + char_ptr = gdb.lookup_type("char").pointer() + int_ptr = gdb.lookup_type("int").pointer() + # m_i should be after the vtable, which has the size of a pointer + i = ( + (self.val.address.cast(char_ptr) + int_ptr.sizeof) + .cast(int_ptr) + .dereference() + ) + return "{pp m_i = %d}" % int(i) + + +pp = gdb.printing.RegexpCollectionPrettyPrinter("S") +pp.add_printer("S", "^S$", SPrinter) +gdb.printing.register_pretty_printer(gdb.current_objfile(), pp, True) diff --git a/gdb/valprint.c b/gdb/valprint.c index d48c599c832..00943c1111a 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -1054,16 +1054,6 @@ common_val_print (struct value *value, struct ui_file *stream, int recurse, QUIT; - /* Ensure that the type is complete and not just a stub. If the type is - only a stub and we can't find and substitute its complete type, then - print appropriate string and return. */ - - if (real_type->is_stub ()) - { - fprintf_styled (stream, metadata_style.style (), _("")); - return; - } - if (!valprint_check_validity (stream, real_type, 0, value)) return; @@ -1074,6 +1064,16 @@ common_val_print (struct value *value, struct ui_file *stream, int recurse, return; } + /* Ensure that the type is complete and not just a stub. If the type is + only a stub and we can't find and substitute its complete type, then + print appropriate string and return. */ + + if (real_type->is_stub ()) + { + fprintf_styled (stream, metadata_style.style (), _("")); + return; + } + /* Handle summary mode. If the value is a scalar, print it; otherwise, print an ellipsis. */ if (options->summary && !val_print_scalar_type_p (type))