From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EcZZDHQ18Wj9PzkAWB0awg (envelope-from ) for ; Thu, 16 Oct 2025 14:12:04 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bpGhJlJb; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 2C7901E0BA; Thu, 16 Oct 2025 14:12:04 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 2C29A1E047 for ; Thu, 16 Oct 2025 14:12:02 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 49407385842E for ; Thu, 16 Oct 2025 18:12:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 49407385842E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bpGhJlJb Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id C04CE3858428 for ; Thu, 16 Oct 2025 18:11:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C04CE3858428 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C04CE3858428 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1760638277; cv=none; b=AUwshsmtAePgyRjIr+iPNpzOF/1GLI2txeDgvD7fZ0B6L0VexVvQ5V3iGzmngJvFz9qYhiXzM01va4/Wl4z017JXimOtXefilrwv4IroWoPSizPQ5EgCFOvL+Xzf9cO4UjVMQcC+gqTRTDDl3ZygcrNhuk10d8vhZrN5HFPxhbM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1760638277; c=relaxed/simple; bh=CTxzFWSLv+07LwAyvzU0nXf/TUbn3aOWvL+iKVXC7ZI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=sbpmJZs6AjV0VR3wfuuURAx1eMV+tM06CdYajDACD5uVDO0sHMYeJ6g3F+yn78dcjQy21yIp8Cbqk3gtKKkCbiOX+vXtj0OUzDg04CbkjT+yrT9KzNd9MsLNT9r+T54GMNzhmhyLiHFaCYKUZ3+K0QD8L9rWThQp+OIqj2iWIw4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C04CE3858428 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760638277; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1yetDGByMmkAqmG5zrZNV5j7AFCRfkRBe+pOB/5Q7Oc=; b=bpGhJlJbNizbkiK8oDdWTpT6xGaSPeaalvAPRekZPztf7Vcv0gR0blumI+YaxZgF+RlesM Cdg6WNCffPtQK6+4NSe7LOQgcJSv3mLrqQfyenWYGE4a8QNkAioEb3KFRVK18mXsZ0Kdf0 V5zXDEOrLlA1iZI0B/4yWOxZ69tcSPw= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-327-2a43jwKwNIeV6WOiOjhMMQ-1; Thu, 16 Oct 2025 14:11:12 -0400 X-MC-Unique: 2a43jwKwNIeV6WOiOjhMMQ-1 X-Mimecast-MFC-AGG-ID: 2a43jwKwNIeV6WOiOjhMMQ_1760638271 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4711899ab0aso297015e9.2 for ; Thu, 16 Oct 2025 11:11:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760638271; x=1761243071; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1yetDGByMmkAqmG5zrZNV5j7AFCRfkRBe+pOB/5Q7Oc=; b=nYGAuNZO/pCQLAwCM8BHsijQ4We6BRJU250dA49r38+DKeydc2GW2Rj7+lDZtnU07h qXLeMWtZv8K9baxqXOkA1kDrE/0QtxYDlnxbKBgZtnUjEMKPES81Cx4/oU3qrJFVGaUQ O6aSK+Fg3bjJGZ6r7EOQHdPksn6dVq/etwSr0l6DCzlRrU6qGuIHbGmRNxX7pIy90cfs Lz+WEuNOg+xPPm3aKp5KlBWefE/8myarGR+XusNavUk+t1SrXz6MfEm/y0bVJDI0pz33 gdawgSk5WDKKEf6o7OAOG852JLpVzAJJvm31agV/NKermPyDvwliDmRJ+v+4hyJPuPti pBWA== X-Gm-Message-State: AOJu0YwwrtXc++r+t5fUy1t5IN2twJc5susZCzeomaDWRRNMKxYIIH71 VBnEdkJyk3+zgJRvCo8lkMeYAiTMpdUNxN5P80jcFNMqt/gyVVmJCSiB+Gf3RMXWrBCp+zlUrXn BzF+IC+5obRof0KZAG0YLqd6H6NewCAgf9lpezRSz1/7HpKpH7y0TAsKoT4SbHOHislolYlY0Ij FkCgcW3K2k4G6heRRS/vl1E4rxi7QO6FJBdC3XTW8tvvX9Nw8= X-Gm-Gg: ASbGncuzIyUYZsnjl0PswxnGCSkOZFc5AuIfUGMGefmI5H+L319c7k9dVvThJb9vXsY 4GVLy/fUFsn7WZ+mnXFavCvQxv8I7UtCMmjNO0BtpVw9Fhvup080/yr+/gqGI1g58go8nRGt2rj x4nf3sLCfY3p6e7Nv6FRryZjmZF1ax5tMA/le1DTlK0f1YQMVp/jzgtdSfsmY8EhplkGciv8LZN Pg4IOiZncNorTDhZ/bB6uOn0JpXiP2y9rthjFrOugTP9qo2FiQpK/mPbZCNS81m8Ulw0bZ/z0vg ALgoqo+BHNFJRbWn361CKKry34Jv/Q+d3KO9fim32Tyi4LF5HyU1cCwZNQ0VUVaL8k0vOQ== X-Received: by 2002:a05:600c:458b:b0:46e:4246:c90d with SMTP id 5b1f17b1804b1-471178774bfmr6400005e9.11.1760638270271; Thu, 16 Oct 2025 11:11:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH4QNDiYLksVgisG6rswUQ9cFM4C22OkVp0M2IQhOVF+wKxES/yxxxfo4eceRTwHHikPQJgXA== X-Received: by 2002:a05:600c:458b:b0:46e:4246:c90d with SMTP id 5b1f17b1804b1-471178774bfmr6399695e9.11.1760638269312; Thu, 16 Oct 2025 11:11:09 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47114428dbfsm39042775e9.5.2025.10.16.11.11.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Oct 2025 11:11:08 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2] gdb: remove attempted type de-duplication when building gdb-index Date: Thu, 16 Oct 2025 19:11:06 +0100 Message-ID: <5fb2ed381ab0f96c5057f1644ca67dceebcdf3b7.1760638029.git.aburgess@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: pukZBhwXdpwwRhS8TFcsso81BSbJkjXO6RFvbjwWK1k_1760638271 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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~public-inbox=simark.ca@sourceware.org In v2: - The v1 test would give 9 FAILs when using more recent versions of gcc due to the DW_TAG_typedef and DW_TAG_structure_type for the declaration being included in every CU, even when the type was not otherwise referenced in that CU. Resolved these by ensuring every CU references the type; now even older versions of gcc will emit the types in every CU, and the output will be consistent, this doesn't impact the goal of the test. - Rebased to current upstream HEAD, resolved conflict in NEWS file. --- This commit removes the attempted de-duplication of types when building the gdb-index. This commit is the natural extension of this earlier commit: commit aef36dee93bf194cb0b976a4ae49a79a736a188d Date: Sun Aug 13 14:08:06 2023 +0200 [gdb/symtab] Don't deduplicate variables in gdb-index Which removed the de-duplication of variables. It is worth reading the earlier commit as all the justifications for that patch also apply to this one. Currently, when building the gdb-index we sort the type entries, moving declarations to the end of the entry list, and non-declarations to the front, sorting non-declarations by CU offset. We then emit the first entry for any given type name. There are two problems with this. First, a non-declaration entry could be a definition, but it could also be a typedef. Now sure, a typedef is a definition, but not necessarily a useful one. If we have a header file that contains: typedef struct foo_t foo_t; And a CU which makes use of 'foo_t', then the CU will include both a typedef and a type declaration. The target of the typedef will be the declaration. If we have two CUs, one which only sees the above typedef and declaration, and another which sees the typedef and an actual type definition, then the final list of entries for this type's name will be: 1. A typedef entry that points at the declaration. 2. A typedef entry that points at the definition. 3. A definition. 4. A declaration. Now (4) will get sorted to the end of the entry list. But the order of (1), (2), and (3) will depend on the CU offset. If the CU which containing the typedef and declaration has the smallest offset, then (1) will be sorted to the front of the list of entries for this type name. Due to the de-duplication code this means that only (1) will be added to the gdb-index. Now, after GDB starts and parses the index, if a user references 'foo_t' then GDB looks in the index and finds just (1). GDB loads the CU containing (1) and find both the typedef and the declaration. But GDB does not find the full definition. As a result GDB will display 'foo_t' as an incomplete type. This differs from the behaviour when no index is used. With no index GDB expands every CU containing the 'foo_t' type, which means that GDB finds the type's definition, and prints the type correctly. We could solve this problem by marking typedefs as a distinct sub-category of types, just as we do with declarations. Then we could sort definitions to the front of the list, then typedefs, and finally, declarations after that. This would, I think, mean that we always prefer emitting a definition for a type, which would resolve the first problem. But wouldn't, I think, solve the second problem. The second problem is that the Python API can be used to query type symbols. As such, GDB needs to be able to find all the CUs which contain a given type. Especially as it is possible that a type might be defined differently within different CUs. NOTE: Obviously a program doing this would need to be mindful of the One Definition Rule, but so long as the type doesn't escape outside of a single CU then reusing a type name isn't, as I understand it, wrong. And even if it is, the fact that it compiles, and could be a source of bugs, means (in my opinion) that GDB should handle this case to enable debugging of it. Given all of the above, I think that any attempt to remove type entries from the gdb-index is unsafe and can result in GDB behaving differently when using the gdb-index when compared to using no index. The solution is to remove the de-duplication code, which is what this patch does. Now that we no longer need to sort declarations to the end of the entry list, I've removed all the code related to the special use of GDB_INDEX_SYMBOL_KIND_UNUSED5 (which is how we marked declarations), this cleans things up a little bit. I've also renamed some of the functions away from minimize, now that there's no minimization being done. --- gdb/NEWS | 5 + gdb/dwarf2/index-write.c | 80 ++------- .../gdb.base/gdb-index-many-types-1.c | 48 ++++++ .../gdb.base/gdb-index-many-types-2.c | 56 +++++++ .../gdb.base/gdb-index-many-types-3.c | 56 +++++++ .../gdb.base/gdb-index-many-types.exp | 156 ++++++++++++++++++ gdb/testsuite/gdb.base/gdb-index-many-types.h | 42 +++++ .../gdb.base/gdb-index-many-types.py | 54 ++++++ 8 files changed, 427 insertions(+), 70 deletions(-) create mode 100644 gdb/testsuite/gdb.base/gdb-index-many-types-1.c create mode 100644 gdb/testsuite/gdb.base/gdb-index-many-types-2.c create mode 100644 gdb/testsuite/gdb.base/gdb-index-many-types-3.c create mode 100644 gdb/testsuite/gdb.base/gdb-index-many-types.exp create mode 100644 gdb/testsuite/gdb.base/gdb-index-many-types.h create mode 100644 gdb/testsuite/gdb.base/gdb-index-many-types.py diff --git a/gdb/NEWS b/gdb/NEWS index b0146852c5c..72c4ae93796 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -31,6 +31,11 @@ subsequent runs of the inferior will use the same arguments as the first run. +* GDB now adds all type symbols to the .gdb_index section. The index + version number has not increased as a consequence of this change. + This fixes an issue where GDB could fail to find a type when relying + on the index. Any existing indexes should be regenerated. + * New targets GNU/Linux/MicroBlaze (gdbserver) microblazeel-*linux* diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 52131bef216..5031a9f7669 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -55,7 +55,7 @@ #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \ do { \ gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \ - && (value) <= GDB_INDEX_SYMBOL_KIND_UNUSED5); \ + && (value) < GDB_INDEX_SYMBOL_KIND_UNUSED5); \ GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \ } while (0) @@ -184,9 +184,8 @@ struct symtab_index_entry of this name. */ std::vector cu_indices; - /* Minimize CU_INDICES, sorting them and removing duplicates as - appropriate. */ - void minimize (); + /* Sort CU_INDICES. */ + void sort (); }; /* The symbol table. This is a power-of-2-sized hash table. */ @@ -198,16 +197,16 @@ struct mapped_symtab } /* If there are no elements in the symbol table, then reduce the table - size to zero. Otherwise call symtab_index_entry::minimize each entry + size to zero. Otherwise call symtab_index_entry::sort each entry in the symbol table. */ - void minimize () + void minimize_and_sort () { if (m_element_count == 0) m_data.resize (0); for (symtab_index_entry &item : m_data) - item.minimize (); + item.sort (); } /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX is @@ -417,69 +416,19 @@ mapped_symtab::add_index_entry (const char *name, int is_static, /* See symtab_index_entry. */ void -symtab_index_entry::minimize () +symtab_index_entry::sort () { if (name == nullptr || cu_indices.empty ()) return; - /* We sort the indexes in a funny way: GDB_INDEX_SYMBOL_KIND_UNUSED5 - is always sorted last; then otherwise we sort by numeric value. - This ensures that we prefer the definition when both a definition - and a declaration (stub type) are seen. */ + /* Sort the indexes numerically. */ std::sort (cu_indices.begin (), cu_indices.end (), [] (offset_type vala, offset_type valb) { - auto kinda = GDB_INDEX_SYMBOL_KIND_VALUE (vala); - auto kindb = GDB_INDEX_SYMBOL_KIND_VALUE (valb); - if (kinda != kindb) - { - /* Declaration sorts last. */ - if (kinda == GDB_INDEX_SYMBOL_KIND_UNUSED5) - return false; - if (kindb == GDB_INDEX_SYMBOL_KIND_UNUSED5) - return true; - } return vala < valb; }); auto from = std::unique (cu_indices.begin (), cu_indices.end ()); cu_indices.erase (from, cu_indices.end ()); - - /* Rewrite GDB_INDEX_SYMBOL_KIND_UNUSED5. This ensures that a type - declaration will be deleted by the subsequent squashing step, if - warranted. */ - for (auto &val : cu_indices) - { - gdb_index_symbol_kind kind = GDB_INDEX_SYMBOL_KIND_VALUE (val); - if (kind != GDB_INDEX_SYMBOL_KIND_UNUSED5) - continue; - - offset_type newval = 0; - DW2_GDB_INDEX_CU_SET_VALUE (newval, GDB_INDEX_CU_VALUE (val)); - DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE - (newval, GDB_INDEX_SYMBOL_STATIC_VALUE (val)); - DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (newval, - GDB_INDEX_SYMBOL_KIND_TYPE); - - val = newval; - } - - /* We don't want to enter a type more than once, so - remove any such duplicates from the list as well. When doing - this, we want to keep the entry from the first CU -- but this is - implicit due to the sort. This choice is done because it's - similar to what gdb historically did for partial symbols. */ - gdb::unordered_set seen; - from = std::remove_if (cu_indices.begin (), cu_indices.end (), - [&] (offset_type val) - { - gdb_index_symbol_kind kind = GDB_INDEX_SYMBOL_KIND_VALUE (val); - if (kind != GDB_INDEX_SYMBOL_KIND_TYPE) - return false; - - val &= ~GDB_INDEX_CU_MASK; - return !seen.insert (val).second; - }); - cu_indices.erase (from, cu_indices.end ()); } /* A form of 'const char *' suitable for container keys. Only the @@ -1307,16 +1256,7 @@ write_cooked_index (cooked_index *table, || entry->tag == DW_TAG_enumerator) kind = GDB_INDEX_SYMBOL_KIND_VARIABLE; else if (tag_is_type (entry->tag)) - { - /* If we added a type declaration, we want to note this - fact for later, because we don't want a type declaration - to cause the real definition to be omitted from the - index. GDB_INDEX_SYMBOL_KIND_UNUSED5 is used here, but - rewritten later before the index is written. */ - kind = ((entry->flags & IS_TYPE_DECLARATION) == 0 - ? GDB_INDEX_SYMBOL_KIND_TYPE - : GDB_INDEX_SYMBOL_KIND_UNUSED5); - } + kind = GDB_INDEX_SYMBOL_KIND_TYPE; else kind = GDB_INDEX_SYMBOL_KIND_OTHER; @@ -1458,7 +1398,7 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table, /* Now that we've processed all symbols we can shrink their cu_indices lists. */ - symtab.minimize (); + symtab.minimize_and_sort (); data_buf symtab_vec, constant_pool; diff --git a/gdb/testsuite/gdb.base/gdb-index-many-types-1.c b/gdb/testsuite/gdb.base/gdb-index-many-types-1.c new file mode 100644 index 00000000000..00b793ca4ee --- /dev/null +++ b/gdb/testsuite/gdb.base/gdb-index-many-types-1.c @@ -0,0 +1,48 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 "gdb-index-many-types.h" + +int +main () +{ + foo_func_a (0); + foo_func_b (0); + bar_func_a (0); + bar_func_b (0); + baz_func_a (0); + baz_func_b (0); + return 0; +} + +void +foo_func_c (foo_t *obj) +{ + (void) obj; +} + +void +bar_func_c (bar_t *obj) +{ + (void) obj; +} + +void +baz_func_c (baz_t *obj) +{ + (void) obj; +} diff --git a/gdb/testsuite/gdb.base/gdb-index-many-types-2.c b/gdb/testsuite/gdb.base/gdb-index-many-types-2.c new file mode 100644 index 00000000000..2c0a27f30ba --- /dev/null +++ b/gdb/testsuite/gdb.base/gdb-index-many-types-2.c @@ -0,0 +1,56 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 "gdb-index-many-types.h" + +typedef struct foo_t +{ + int foo_t_1; + int foo_t_2; +} foo_t; + +typedef struct woof_t +{ + int woof_t_1; + int woof_t_2; +} woof_t; + +static void +woof_func (woof_t *obj) +{ + (void) obj; +} + +void +foo_func_a (foo_t *obj) +{ + woof_func (0); + (void) obj; +} + +void +baz_func_a (baz_t *obj) +{ + (void) obj; +} + +void +bar_func_a (bar_t *obj) +{ + woof_func (0); + (void) obj; +} diff --git a/gdb/testsuite/gdb.base/gdb-index-many-types-3.c b/gdb/testsuite/gdb.base/gdb-index-many-types-3.c new file mode 100644 index 00000000000..54425ed5156 --- /dev/null +++ b/gdb/testsuite/gdb.base/gdb-index-many-types-3.c @@ -0,0 +1,56 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 "gdb-index-many-types.h" + +typedef struct woof_t +{ + double woof_t_3; + double woof_t_4; +} woof_t; + +static void +woof_func (woof_t *obj) +{ + (void) obj; +} + +typedef struct bar_t +{ + int bar_t_1; + int bar_t_2; +} bar_t; + +void +bar_func_b (bar_t *obj) +{ + woof_func (0); + (void) obj; +} + +void +baz_func_b (baz_t *obj) +{ + (void) obj; +} + +void +foo_func_b (foo_t *obj) +{ + woof_func (0); + (void) obj; +} diff --git a/gdb/testsuite/gdb.base/gdb-index-many-types.exp b/gdb/testsuite/gdb.base/gdb-index-many-types.exp new file mode 100644 index 00000000000..34524c27aab --- /dev/null +++ b/gdb/testsuite/gdb.base/gdb-index-many-types.exp @@ -0,0 +1,156 @@ +# Copyright 2025 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 . + +# Check that adding an index to an executable (both a gdb index and a +# dwarf-5 index are tested), doesn't prevent GDB from seeing the +# expected types. + +standard_testfile -1.c -2.c -3.c .h + +# One of the tests uses this Python file. The test_* proc checks that +# GDB supports Python tests. Some of the other procs don't use this +# Python file. +set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py] + +if {[build_executable "building" $testfile \ + [list $srcfile $srcfile2 $srcfile3]] == -1} { + return +} + +# Run 'info types TYPENAME', expect to see an entry from FILENAME for +# the line matching PATTERN. +proc check_info_types { testfile typename filename pattern } { + with_test_prefix $filename { + clean_restart $testfile + + set line_num [gdb_get_line_number $pattern $filename] + gdb_test "info types $typename" \ + "File \[^\r\n\]+/${filename}:\r\n${line_num}:\[^\r\n\]+.*" + } +} + +# Start GDB with FILENAME, and examine some of the types. This proc +# might seem to be using clean_restart a little too much, but we need +# to be really careful here. As we examine one type, e.g. foo_t, this +# might cause GDB to fully parse a CU, which then means examining +# bar_t gives the expected result. When, if we'd first looked for +# bar_t, then (due to an index bug) we might not have found the +# correct type definition. +# +# The only way we can be sure that an earlier test isn't going to +# trigger CU expansion is to restart GDB before every query. +proc run_test { filename } { + # Print all the types for which there is only one representation. + foreach type { foo_t bar_t baz_t } { + clean_restart $filename + gdb_test "ptype $type" \ + [multi_line \ + "type = struct $type {" \ + "\\s+int ${type}_1;" \ + "\\s+int ${type}_2;" \ + "}"] + } + + # There are two different versions of woof_t. For now, when using + # `ptype` GDB will just display the first one it finds, which could + # legitimately be either. + set woof_int_re [multi_line \ + "type = struct woof_t {" \ + "\\s+int woof_t_1;" \ + "\\s+int woof_t_2;" \ + "}"] + set woof_double_re [multi_line \ + "type = struct woof_t {" \ + "\\s+double woof_t_3;" \ + "\\s+double woof_t_4;" \ + "}"] + clean_restart $filename + gdb_test_multiple "ptype woof_t" "" { + -re -wrap $woof_int_re { + pass $gdb_test_name + } + -re -wrap $woof_double_re { + pass $gdb_test_name + } + } + + # Check for declarations and definitions of some types. + check_info_types $filename foo_t $::srcfile2 "typedef struct foo_t" + check_info_types $filename foo_t $::srcfile4 "typedef struct foo_t foo_t;" + check_info_types $filename bar_t $::srcfile3 "typedef struct bar_t" + check_info_types $filename bar_t $::srcfile4 "typedef struct bar_t bar_t;" + check_info_types $filename baz_t $::srcfile4 "typedef struct baz_t" + check_info_types $filename woof_t $::srcfile2 "typedef struct woof_t" + check_info_types $filename woof_t $::srcfile3 "typedef struct woof_t" + + # Use Python to look for type symbols. + if { [allow_python_tests] } { + foreach_with_prefix type { foo_t bar_t baz_t } { + clean_restart $filename + gdb_test_no_output "source $::pyfile" "import python scripts" + gdb_test "py-show-type $type" \ + [multi_line \ + "Looking for type '$type':" \ + " Found 3 type symbols" \ + " 1: struct $type \\{ int ${type}_1; int ${type}_2; \\}" \ + " 2: struct $type \\{ int ${type}_1; int ${type}_2; \\}" \ + " 3: struct $type \\{ int ${type}_1; int ${type}_2; \\}"] + } + + clean_restart $filename + gdb_test_no_output "source $::pyfile" "import python scripts" + gdb_test "py-show-type woof_t" \ + [multi_line \ + "Looking for type 'woof_t':" \ + " Found 2 type symbols" \ + " 1: struct woof_t \\{ (?:int|double) woof_t_(?:1|3); (?:int|double) woof_t_(?:2|4); \\}" \ + " 2: struct woof_t \\{ (?:int|double) woof_t_(?:1|3); (?:int|double) woof_t_(?:2|4); \\}"] + + } +} + +with_test_prefix "no index" { + run_test $testfile +} + +# The previous call to 'run_test' will have left GDB active. Check if +# BINFILE already has an index. If it does then we must be running +# with one of the boardfiles that adds an index. We could possibly +# try to remove the index, but for now, just don't run the following +# parts which rely on adding an index. +set index_type [get_index_type $binfile "check debug style"] +if { $index_type ne "cooked" } { + unsupported "cannot test without a cooked index" + return +} + +foreach_with_prefix index_type { gdb dwarf5 } { + set binfile_with_index ${binfile}-idx-${index_type} + + remote_exec build "cp $binfile $binfile_with_index" + + if { $index_type eq "gdb" } { + set style "" + } else { + set style "-dwarf-5" + } + + if {[ensure_gdb_index $binfile_with_index $style] != 1} { + unsupported "couldn't add $index_type index" + return + } + + run_test [file tail $binfile_with_index] +} diff --git a/gdb/testsuite/gdb.base/gdb-index-many-types.h b/gdb/testsuite/gdb.base/gdb-index-many-types.h new file mode 100644 index 00000000000..eb5447ad678 --- /dev/null +++ b/gdb/testsuite/gdb.base/gdb-index-many-types.h @@ -0,0 +1,42 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 . */ + +#ifndef GDB_INDEX_MANY_TYPES_H +#define GDB_INDEX_MANY_TYPES_H + +typedef struct foo_t foo_t; +typedef struct bar_t bar_t; + +extern void foo_func_a (foo_t *obj); +extern void foo_func_b (foo_t *obj); +extern void foo_func_c (foo_t *obj); + +extern void bar_func_a (bar_t *obj); +extern void bar_func_b (bar_t *obj); +extern void bar_func_c (bar_t *obj); + +typedef struct baz_t +{ + int baz_t_1; + int baz_t_2; +} baz_t; + +extern void baz_func_a (baz_t *obj); +extern void baz_func_b (baz_t *obj); +extern void baz_func_c (baz_t *obj); + +#endif /* GDB_INDEX_MANY_TYPES_H */ diff --git a/gdb/testsuite/gdb.base/gdb-index-many-types.py b/gdb/testsuite/gdb.base/gdb-index-many-types.py new file mode 100644 index 00000000000..b1de4f9cbed --- /dev/null +++ b/gdb/testsuite/gdb.base/gdb-index-many-types.py @@ -0,0 +1,54 @@ +# Copyright 2025 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 . + + +class TypeViewer(gdb.Command): + """A command which takes a string and looks up types with that name. + + The types are expected to all be structs. This command prints a + basic representation of the struct. This is only going to work when + used with the types defined in the gdb-index-many-types test source + files.""" + + def __init__(self): + super().__init__("py-show-type", gdb.COMMAND_USER) + + def invoke(self, args, from_tty): + argv = gdb.string_to_argv(args) + if argv[0] == "": + raise gdb.GdbError("missing argument") + print("Looking for type '" + argv[0] + "':") + syms = gdb.lookup_static_symbols(argv[0], gdb.SYMBOL_TYPE_DOMAIN) + count = len(syms) + print(" Found %d type symbol%s" % (count, "" if count == 1 else "s")) + for i, s in enumerate(syms, start=1): + t = s.type + if t is None: + print(" %d: No type." % i) + else: + fields = "struct " + argv[0] + " {" + try: + for f in t.fields(): + if len(fields) > 0: + fields = fields + " " + fields = fields + "%s %s;" % (str(f.type), f.name) + except: + pass + fields = fields + " }" + + print(" %d: %s" % (i, fields)) + + +TypeViewer() base-commit: b6753354fbbe7c2c66ae9f452ba7aa049db0fe0c -- 2.47.1