From patchwork Wed Feb 26 11:06:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Vrany X-Patchwork-Id: 107123 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 E2863385843F for ; Wed, 26 Feb 2025 11:09:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2863385843F X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-114.mimecast.com (us-smtp-delivery-114.mimecast.com [170.10.129.114]) by sourceware.org (Postfix) with ESMTP id 31E923858D37 for ; Wed, 26 Feb 2025 11:07:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31E923858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=labware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=labware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 31E923858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.114 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740568067; cv=none; b=Mv9RJ35gkg/cvHLOsGFNzOVAMdjBnDHBwBMeOgRl5DU/toS09UtmBY3BfvOjiOGnhduDqzlTh4GCWSohs//jYvg1ldk1uCJzmBum6KgkcBiBkhjf7NlR/OIZSQfMh9q/8blDEtX31SMaeSTDtb0d+1p1KdPeAn11CjnsKHvPUs4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1740568067; c=relaxed/simple; bh=CzXhejLWKlbp8fjNeSo+qF/tR1QR01DKikPvY4NYLCA=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=oGRs8vrEzxhbPgwzSCa/c1DpfI7LdsB92ud+M5HFDoek6lt4VxV5q1RsW2YV9fTmZG2SaXD0XMJ3Cj91tL9eZZizdmomzkwkK0mcSnnup8h87gTmOZ7bAK9Bu9uWEv5f4hUDC8xGqycYDqFlH/H3Jw2PYMJmAHGY7tagFkDkoeI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 31E923858D37 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2040.outbound.protection.outlook.com [104.47.55.40]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695-UT_4lAupPJmzEY5Cq6VRig-1; Wed, 26 Feb 2025 06:07:46 -0500 X-MC-Unique: UT_4lAupPJmzEY5Cq6VRig-1 X-Mimecast-MFC-AGG-ID: UT_4lAupPJmzEY5Cq6VRig_1740568065 Received: from SA0PR17MB4314.namprd17.prod.outlook.com (2603:10b6:806:e7::16) by PH7PR17MB6156.namprd17.prod.outlook.com (2603:10b6:510:1f0::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.18; Wed, 26 Feb 2025 11:07:44 +0000 Received: from SA0PR17MB4314.namprd17.prod.outlook.com ([fe80::38a7:a6f2:3b95:bc26]) by SA0PR17MB4314.namprd17.prod.outlook.com ([fe80::38a7:a6f2:3b95:bc26%7]) with mapi id 15.20.8466.016; Wed, 26 Feb 2025 11:07:44 +0000 From: Jan Vrany To: gdb-patches@sourceware.org CC: Jan Vrany Subject: [RFC v2 04/10] gdb/python: preserve identity for gdb.Type objects Date: Wed, 26 Feb 2025 11:06:47 +0000 Message-ID: <20250226110653.1200260-5-jan.vrany@labware.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250226110653.1200260-1-jan.vrany@labware.com> References: <20250226110653.1200260-1-jan.vrany@labware.com> X-ClientProxiedBy: LO4P123CA0015.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:150::20) To SA0PR17MB4314.namprd17.prod.outlook.com (2603:10b6:806:e7::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA0PR17MB4314:EE_|PH7PR17MB6156:EE_ X-MS-Office365-Filtering-Correlation-Id: ea846cd2-7661-4561-e796-08dd5655cb62 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014 X-Microsoft-Antispam-Message-Info: 5zwlgvcKMmyO58jXdlDz+7gxOZmzKq2HPXb2RG2cGMPOtg4nXbhwtUeCNO+hG1flsdKmK4qhmTttvuYPIvZa43XQnqr681cH4miHuLX7CYfh7x1oOUZ/MDFndyoFxnB4WUfEHUcDMxftGEFxffiWHpXnOHawnwfd2U/Ay0C1Q1BXp5qBUlEmAj7hlMACegWB4T6nNAGhR8zKd5v4ztJsE98H/5gLWJoJCzyAyXxOiy1b/U2fuHP8hNZp5eIdzQfDX937rMTWKxXubU4SCSXYPpIV1tcQul9P3wB0SaCv13GgHtbapA+rxoa/zaxWHaKr/dmZn35Mc18NcwWKVK1/EE/qUpWY99WiwPFzG/+vy5UxlKX2DgQVKtm/VLmku/NocmOuuWgO2WZH3HsnsFG5N2BDIkg5BqyV0Nx4lLDBoHgBNQDXFmv4zUPPlGww1kWVNgMGnkXmWqMKxnalhQV4wwkiqYIkhe1zYNHR2ixvNHsB2xACKf5L4OI+5SlDrSlW/JdCDDh6+RdGxsQB1yEPjPjW/FMP+RA0WWCVKYaQ5H++ZCr2G498sZSS+gbthFnG62Tl2xxrcXBDWzMOcJPOR8WoLwYS1CwTo0wc59rwFnGtl0BjnSUaTwWmAhk/HaB0vDczQ6dOuns3lOqTO5jjHYKOr+iDYHFZIjywOHLMPIcM3x+jIj+nj8NTo3KBJ+FVV0mWNfE/as3DqMORCiIbVqmqsQuObIcxENhfQSjYgNCqh1YXKwp8UVh/OCRCk46sDNv5U0ag2wft5T4s1dv1qzmTNI2Nq02KuNddCgDlTDofXB3Qp0NwfGGwXxqL/Qb59QIfaWIn+wIR48dbp4Rgmp0K1r2Vsjil/4Z7MhLWVbagXf1PGgkzJKjBQQ1wbqjnZuUI4DI9zOGwqy3p4myzp1R6c5Fle0U00dfFY9N8uxEAeyb7s47vDFcJl8debyBueDKuEQ+V/65FZaKYcSuxLDQZT8TTEuhLSMR59ioLC3Bk9ob7v4Oz+f/zz6IPNhWPqn02xStZQjlUuyPB0Sx++Srotn9a2ieNlFfY+Gbd0zTUrcfupsDEexL0rU2KoRCbeGxIFWbLA2AauM0U4A5cJnZclijsSvqTb34PEGLmVbMTs+QN8TzALl/i4rY3R/KFNER8XoZw5SNyYhWOPB0S8rYu4Fduf5kgUkWn549fsI5fC8C4qUR3iWy4P18ahVpbgO8NVbihlgXuwu9tdU+ae2H7Qt9dGdtwvuttrVB0SIpqfKsyzOOD7Zg4O38WbgswfkU9pO5Y8e1oTA6gkCG2kR/TQoRQaOYZXCqy16Tdgbhbc0z7fflgIKIDVoygEs3GFGi72+T8Otpm17cBXBXiYVkDKSimeuOgslyOxZUWWu9Q+JTOENRXVnB+HMxjuDjR X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA0PR17MB4314.namprd17.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(376014); DIR:OUT; SFP:1102 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: phXMfK0stvk3Z16UWbinx0SSdoOrryUFj7zvM914Za7VIHD18GvDPbGYxiKWWrCOAijEKbFkEYclhhODA2s3zmsNfDuEFQj3B15DeeWBmzBXlkFrJbx4GmOK+h81ng0JaFeMDkRuRaCDjR/DRG69oj5fdoc8KP6WAagJV9fgjGvZ5XnE0t1o0f+avnTki3JMdxi90fGzTMWiRZjlCQ9Oe8h3kBpy44u8Iz7TrPAO2Si9adCy5YYAyi/fLG0X9B77b7d9wG6+mNVhaXR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DfXH23/uXYLpCxGcD3HMk4TKRQfmHRYloAWXzfdTCjQQrtnkdwFe7VHf3lKbcaQieFP2HaMJ1LNc1BPKVxpefmGd/gxRKI2wnAqYxGlXrl/2IfqXDre6zPoD9BWSrAJKRjpaJPEPGHfAejmTnJevHyh0k/8QYtWohmLoTNC6q3AZ1+VPC9UkPc89R9glgl1PlwIZGo386l9eHws++1nxID2LwyFk7UzXcixpGr1H6SET5iRPi2X+rpKW+5fUrmOKYC X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: ea846cd2-7661-4561-e796-08dd5655cb62 X-MS-Exchange-CrossTenant-AuthSource: SA0PR17MB4314.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Feb 2025 11:07:44.1727 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b5db0322-1aa0-4c0a-859c-ad0f96966f4c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: p5c0aqSdwhJoIedyeEt8Cove5+df88Zz7BQkwzxcJ9SlslurElBhzKgui6VY0aq5jb3ZMw/AuZtwRAb5711nYw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR17MB6156 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: wEeEKa-1IVfKR92jFtRU8pT83xEycpQ6ojfAtsa2IeQ_1740568065 X-Mimecast-Originator: labware.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, 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 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 This commit changes type_to_type_object() so that each it is called with a particular struct type * it returns the very same gdb.Type object. This is done in the same way as for gdb.Symtab objects in earlier commit ("gdb/python: preserve identity for gdb.Symtab objects") except that types may be either objfile-owned or arch-owned. Prior this commit, arch-owned objects we not put into any list (like objfile-owned ones) so they could not be easily looked up. This commit changes the code so arch-owned list are put into per-architecture list which is then used (solely) for looking up arch-owned gdb.Type. Another complication comes from the fact that when objfile is about to be freed, associated gdb.Type instances are not merely invalidated (like it is done with gdb.Symtab or gdb.Symbol objects) but instead the type is copied and the copy is arch-owned. So we need two different "deleters", one for objfile-owned types that copies the type (as before) and then insert the object to per-architecture list and another one for arch-owned types. --- gdb/python/py-type.c | 104 ++++++++++++++++++++++----- gdb/testsuite/gdb.python/py-arch.exp | 5 ++ gdb/testsuite/gdb.python/py-type.exp | 15 ++++ 3 files changed, 105 insertions(+), 19 deletions(-) diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index 11a96d52c2e..c9fa6a37250 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -1162,8 +1162,12 @@ typy_richcompare (PyObject *self, PyObject *other, int op) +/* Forward declaration, see below. */ +static void +set_type (type_object *obj, struct type *type); + /* Deleter that saves types when an objfile is being destroyed. */ -struct typy_deleter +struct typy_deleter_for_objfile_owned { void operator() (type_object *obj) { @@ -1181,35 +1185,74 @@ struct typy_deleter type_object *next = obj->next; copied_types.clear (); - obj->type = copy_type_recursive (obj->type, copied_types); - obj->next = NULL; - obj->prev = NULL; + /* Set a copied (now arch-owned) type. As a side-effect this + adds OBJ to per-arch list. We do not need to remove it from + per-objfile list since the objfile is going to go completely + anyway. */ + set_type (obj, copy_type_recursive (obj->type, copied_types)); obj = next; } } }; -static const registry::key +/* Deleter that is used when an arch is is about to be freed. */ +struct typy_deleter_for_arch_owned +{ + void operator() (type_object *obj) + { + while (obj) + { + type_object *next = obj->next; + + obj->type = nullptr; + + obj->next = nullptr; + obj->prev = nullptr; + + obj = next; + } + } +}; + + + +static const registry::key typy_objfile_data_key; +static const registry::key + typy_gdbarch_data_key; static void set_type (type_object *obj, struct type *type) { obj->type = type; - obj->prev = NULL; - if (type != nullptr && type->objfile_owner () != nullptr) + obj->prev = nullptr; + + /* Can it really happen that type is NULL? */ + if (type != nullptr) { - struct objfile *objfile = type->objfile_owner (); + if (type->objfile_owner () != nullptr) + { + struct objfile *objfile = type->objfile_owner (); + + obj->next = typy_objfile_data_key.get (objfile); + if (obj->next) + obj->next->prev = obj; + typy_objfile_data_key.set (objfile, obj); + } + else + { + struct gdbarch *arch = type->arch_owner (); - obj->next = typy_objfile_data_key.get (objfile); - if (obj->next) - obj->next->prev = obj; - typy_objfile_data_key.set (objfile, obj); + obj->next = typy_gdbarch_data_key.get (arch); + if (obj->next) + obj->next->prev = obj; + typy_gdbarch_data_key.set (arch, obj); + } } else - obj->next = NULL; + obj->next = nullptr; } static void @@ -1219,13 +1262,19 @@ typy_dealloc (PyObject *obj) if (type->prev) type->prev->next = type->next; - else if (type->type != nullptr && type->type->objfile_owner () != nullptr) + else if (type->type != nullptr) { - /* Must reset head of list. */ - struct objfile *objfile = type->type->objfile_owner (); - - if (objfile) - typy_objfile_data_key.set (objfile, type->next); + if (type->type->is_objfile_owned ()) + { + /* Must reset head of list. */ + struct objfile *objfile = type->type->objfile_owner (); + typy_objfile_data_key.set (objfile, type->next); + } + else + { + struct gdbarch *arch = type->type->arch_owner (); + typy_gdbarch_data_key.set (arch, type->next); + } } if (type->next) type->next->prev = type->prev; @@ -1473,6 +1522,23 @@ type_to_type_object (struct type *type) return gdbpy_handle_gdb_exception (nullptr, except); } + /* Look if there's already a gdb.Type object for given TYPE + and if so, return it. */ + if (type->is_objfile_owned ()) + type_obj = typy_objfile_data_key.get (type->objfile_owner ()); + else + type_obj = typy_gdbarch_data_key.get (type->arch_owner ()); + + while (type_obj != nullptr) + { + if (type_obj->type == type) + { + Py_INCREF (type_obj); + return (PyObject*)type_obj; + } + type_obj = type_obj->next; + } + type_obj = PyObject_New (type_object, &type_object_type); if (type_obj) set_type (type_obj, type); diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp index c76fc778117..c294011ac69 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -108,6 +108,11 @@ gdb_test "python print(arch.void_type())" \ "void" \ "get void type" +# Test type identity +gdb_test "python print(arch.integer_type(32) is arch.integer_type(32))" \ + "True" \ + "arch.integer_type(32) always return the same Python object" + # Test for gdb.architecture_names(). First we're going to grab the # complete list of architecture names using the 'complete' command. set arch_names [] diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp index 7e469c93c35..c9d4353e488 100644 --- a/gdb/testsuite/gdb.python/py-type.exp +++ b/gdb/testsuite/gdb.python/py-type.exp @@ -324,6 +324,19 @@ proc test_type_equality {} { } } +# Test type identity +proc test_type_identity {} { + gdb_test_no_output "python v1 = gdb.parse_and_eval('global_unsigned_int')" + gdb_test_no_output "python v2 = gdb.parse_and_eval('global_unsigned_int')" + + gdb_test "python print(v1.type is v2.type)" "True" + + gdb_test_no_output "python t1 = gdb.lookup_type ('char')" + gdb_test_no_output "python t2 = gdb.lookup_type ('char')" + + gdb_test "python print(t1 is t2)" "True" +} + # Test the gdb.Type.is_scalar property. proc test_is_scalar { lang } { if {$lang == "c++"} { @@ -376,6 +389,7 @@ if { [build_inferior "${binfile}" "c"] == 0 } { test_is_scalar "c" test_is_signed "c" test_type_equality + test_type_identity } } @@ -392,6 +406,7 @@ if { [build_inferior "${binfile}-cxx" "c++"] == 0 } { test_is_scalar "c++" test_is_signed "c++" test_type_equality + test_type_identity } }