* [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses
@ 2026-11-20 9:18 Sendao Yan
2026-11-20 18:26 ` Andrew Burgess
2026-11-21 14:59 ` [PATCH v2] " Sendao Yan
0 siblings, 2 replies; 12+ messages in thread
From: Sendao Yan @ 2026-11-20 9:18 UTC (permalink / raw)
To: gdb-patches; +Cc: tom, yansendao
lookup_symbol_in_objfile has only searched the cooked index since commit
86ac8c546235 ("Convert lookup_symbol_in_objfile"). The index stores the
DWARF spelling of a name, so a DWARF5 static constexpr member of a
template instantiation can be missed when the C++ expression evaluator
looks it up using canonicalised template arguments: GCC records
std::ratio<1, 1> but the lookup uses std::ratio<1l, 1l>.
This makes libstdc++'s chrono pretty-printer fail with
"gdb.error: value has been optimized out" when printing
std::chrono::hh_mm_ss (PR symtab/33533, GCC PR 128817).
Walk the expanded symtabs as a fallback when the quick lookup returns
nothing, restoring the behaviour that existed before 86ac8c546235 while
keeping the quick lookup as the fast path.
Add gdb.cp/static-const-template.exp, which prints a static constexpr
member of a class template with a long non-type template parameter. With
the unpatched gdb it fails with <optimized out>; with the fix it passes.
Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc
(print hms/hms_nano, also with -flto), the new test, and the existing
gdb.dwarf2/static-const-member.exp, gdb.cp/m-static.exp,
gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533
---
gdb/symtab.c | 20 +++++++++-
gdb/testsuite/gdb.cp/static-const-template.cc | 33 +++++++++++++++
.../gdb.cp/static-const-template.exp | 40 +++++++++++++++++++
3 files changed, 91 insertions(+), 2 deletions(-)
create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc
create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp
diff --git a/gdb/symtab.c b/gdb/symtab.c
index dc40fbfadf6..13a275c3fcc 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2501,8 +2501,9 @@ lookup_symbol_in_static_block (const char *name,
}
/* Perform the standard symbol lookup of NAME in OBJFILE:
- 1) First search expanded symtabs, and if not found
- 2) Search the "quick" symtabs (partial or .gdb_index).
+ 1) First search the "quick" symtabs (partial or .gdb_index), and if not
+ found
+ 2) Search the expanded symtabs.
BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */
static struct block_symbol
@@ -2521,6 +2522,21 @@ lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index,
result = lookup_symbol_via_quick_fns (objfile, block_index,
name, domain);
+ if (result.symbol == NULL)
+ {
+ /* The cooked index does not necessarily contain every symbol that
+ the expanded symtabs do. In particular, GCC emits a DWARF5
+ DW_TAG_variable for a static constexpr member using the DWARF
+ spelling of the enclosing template arguments (e.g.
+ std::ratio<1, 1>), while the C++ expression evaluator looks the
+ member up using the canonical spelling (std::ratio<1l, 1l>), so
+ the quick lookup misses it. Fall back to walking the symtabs,
+ which was the behaviour before commit 86ac8c546235.
+
+ PR symtab/33533. */
+ result = lookup_symbol_in_objfile_symtabs (objfile, block_index,
+ name, domain);
+ }
symbol_lookup_debug_printf ("lookup_symbol_in_objfile (...) = %s",
result.symbol != NULL
? host_address_to_string (result.symbol)
diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc
new file mode 100644
index 00000000000..036e6b3b593
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/static-const-template.cc
@@ -0,0 +1,33 @@
+/* Copyright 2026 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 <http://www.gnu.org/licenses/>. */
+
+/* A static constexpr member of a class template with a non-type template
+ parameter. The reference from the member typedef forces GCC to emit the
+ declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5. */
+
+template<long N>
+struct S
+{
+ static constexpr unsigned v = N;
+ using P = int[v];
+};
+
+S<1> s;
+
+int
+main ()
+{
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp
new file mode 100644
index 00000000000..af8477dba4e
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/static-const-template.exp
@@ -0,0 +1,40 @@
+# Copyright 2026 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 <http://www.gnu.org/licenses/>.
+
+# Test printing a static constexpr member of a class template with a
+# non-type template parameter.
+#
+# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression
+# evaluator looks the member up using the canonical spelling S<1l>. The
+# cooked index stores the DWARF spelling, so before the fix for PR
+# symtab/33533 the lookup missed and gdb printed <optimized out>.
+
+standard_testfile .cc
+
+set opts {debug c++ dwarf5}
+lappend opts additional_flags=-std=c++11
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} {
+ return -1
+}
+
+if {![runto_main]} {
+ return -1
+}
+
+gdb_test "print s.v" "= 1" \
+ "print static constexpr member of a template"
+
+gdb_test "print 'S<1l>::v'" "= 1" \
+ "print static constexpr member with canonical template arguments"
--
2.34.1
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses 2026-11-20 9:18 [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses Sendao Yan @ 2026-11-20 18:26 ` Andrew Burgess 2026-11-20 19:46 ` Keith Seitz 2026-11-21 0:50 ` Tom Tromey 2026-11-21 14:59 ` [PATCH v2] " Sendao Yan 1 sibling, 2 replies; 12+ messages in thread From: Andrew Burgess @ 2026-11-20 18:26 UTC (permalink / raw) To: Sendao Yan, gdb-patches; +Cc: tom, yansendao Sendao Yan <yansendao@126.com> writes: > lookup_symbol_in_objfile has only searched the cooked index since commit > 86ac8c546235 ("Convert lookup_symbol_in_objfile"). The index stores the > DWARF spelling of a name, so a DWARF5 static constexpr member of a > template instantiation can be missed when the C++ expression evaluator > looks it up using canonicalised template arguments: GCC records > std::ratio<1, 1> but the lookup uses std::ratio<1l, 1l>. > > This makes libstdc++'s chrono pretty-printer fail with > "gdb.error: value has been optimized out" when printing > std::chrono::hh_mm_ss (PR symtab/33533, GCC PR 128817). > > Walk the expanded symtabs as a fallback when the quick lookup returns > nothing, restoring the behaviour that existed before 86ac8c546235 while > keeping the quick lookup as the fast path. > > Add gdb.cp/static-const-template.exp, which prints a static constexpr > member of a class template with a long non-type template parameter. With > the unpatched gdb it fails with <optimized out>; with the fix it passes. > > Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc > (print hms/hms_nano, also with -flto), the new test, and the existing > gdb.dwarf2/static-const-member.exp, gdb.cp/m-static.exp, > gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp. > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533 > --- > gdb/symtab.c | 20 +++++++++- > gdb/testsuite/gdb.cp/static-const-template.cc | 33 +++++++++++++++ > .../gdb.cp/static-const-template.exp | 40 +++++++++++++++++++ > 3 files changed, 91 insertions(+), 2 deletions(-) > create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc > create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp > > diff --git a/gdb/symtab.c b/gdb/symtab.c > index dc40fbfadf6..13a275c3fcc 100644 > --- a/gdb/symtab.c > +++ b/gdb/symtab.c > @@ -2501,8 +2501,9 @@ lookup_symbol_in_static_block (const char *name, > } > > /* Perform the standard symbol lookup of NAME in OBJFILE: > - 1) First search expanded symtabs, and if not found > - 2) Search the "quick" symtabs (partial or .gdb_index). > + 1) First search the "quick" symtabs (partial or .gdb_index), and if not > + found > + 2) Search the expanded symtabs. > BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK. */ > > static struct block_symbol > @@ -2521,6 +2522,21 @@ lookup_symbol_in_objfile (struct objfile *objfile, enum block_enum block_index, > > result = lookup_symbol_via_quick_fns (objfile, block_index, > name, domain); > + if (result.symbol == NULL) GDB uses 'nullptr' not NULL for new code. > + { > + /* The cooked index does not necessarily contain every symbol that > + the expanded symtabs do. In particular, GCC emits a DWARF5 > + DW_TAG_variable for a static constexpr member using the DWARF > + spelling of the enclosing template arguments (e.g. > + std::ratio<1, 1>), while the C++ expression evaluator looks the > + member up using the canonical spelling (std::ratio<1l, 1l>), so > + the quick lookup misses it. Fall back to walking the symtabs, > + which was the behaviour before commit 86ac8c546235. > + > + PR symtab/33533. */ > + result = lookup_symbol_in_objfile_symtabs (objfile, block_index, > + name, domain); > + } > symbol_lookup_debug_printf ("lookup_symbol_in_objfile (...) = %s", > result.symbol != NULL > ? host_address_to_string (result.symbol) > diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc > new file mode 100644 > index 00000000000..036e6b3b593 > --- /dev/null > +++ b/gdb/testsuite/gdb.cp/static-const-template.cc > @@ -0,0 +1,33 @@ > +/* Copyright 2026 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 <http://www.gnu.org/licenses/>. */ > + > +/* A static constexpr member of a class template with a non-type template > + parameter. The reference from the member typedef forces GCC to emit the > + declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5. */ > + > +template<long N> > +struct S > +{ > + static constexpr unsigned v = N; > + using P = int[v]; > +}; > + > +S<1> s; > + > +int > +main () > +{ > + return 0; > +} > diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp > new file mode 100644 > index 00000000000..af8477dba4e > --- /dev/null > +++ b/gdb/testsuite/gdb.cp/static-const-template.exp > @@ -0,0 +1,40 @@ > +# Copyright 2026 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 <http://www.gnu.org/licenses/>. > + > +# Test printing a static constexpr member of a class template with a > +# non-type template parameter. > +# > +# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression > +# evaluator looks the member up using the canonical spelling S<1l>. The > +# cooked index stores the DWARF spelling, so before the fix for PR > +# symtab/33533 the lookup missed and gdb printed <optimized out>. > + > +standard_testfile .cc > + > +set opts {debug c++ dwarf5} > +lappend opts additional_flags=-std=c++11 > + > +if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} { > + return -1 > +} > + > +if {![runto_main]} { > + return -1 > +} The -1 on both these returns is unnecessary. Please remove. > + > +gdb_test "print s.v" "= 1" \ > + "print static constexpr member of a template" > + > +gdb_test "print 'S<1l>::v'" "= 1" \ > + "print static constexpr member with canonical template arguments" What happens if you remove the 'print s.v' line test? Does this second test still pass? My concern here is that if you *rely* on finding symbols in the expanded symtabs, then the lookup only works if something else first expands the symtab. This means we have random bugs where things don't work, then they'll suddenly start working (once the symtab has expanded). Can you talk more about why this bug cannot be fixed in the quick syms lookup path. At the very least, if we are baking in a known limitation (reliance on expanded symtabs), then this should be documented in both the commit message and code comments. Thanks, Andrew ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses 2026-11-20 18:26 ` Andrew Burgess @ 2026-11-20 19:46 ` Keith Seitz 2026-11-21 0:50 ` Tom Tromey 1 sibling, 0 replies; 12+ messages in thread From: Keith Seitz @ 2026-11-20 19:46 UTC (permalink / raw) To: Sendao Yan, gdb-patches; +Cc: Andrew Burgess Hi, I'd also like to ask a question or two... On 11/20/26 10:26 AM, Andrew Burgess wrote: > Sendao Yan <yansendao@126.com> writes: > >> lookup_symbol_in_objfile has only searched the cooked index since commit >> 86ac8c546235 ("Convert lookup_symbol_in_objfile"). The index stores the >> DWARF spelling of a name, so a DWARF5 static constexpr member of a >> template instantiation can be missed when the C++ expression evaluator >> looks it up using canonicalised template arguments: GCC records >> std::ratio<1, 1> but the lookup uses std::ratio<1l, 1l>. This "std::ratio<1, 1>" vs "std::ratio<1l, 1l>" is exactly what canonicalization is supposed to address. Both symbol names from the compiler and user input should be canonicalized. > Can you talk more about why this bug cannot be fixed in the quick syms > lookup path. At the very least, if we are baking in a known limitation > (reliance on expanded symtabs), then this should be documented in both > the commit message and code comments. I really wonder whether there is some canonicalization that isn't happening. From the description, it sounds like the compiler's name for the symbol was not canonicalized. Keith ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses 2026-11-20 18:26 ` Andrew Burgess 2026-11-20 19:46 ` Keith Seitz @ 2026-11-21 0:50 ` Tom Tromey 2026-11-21 3:53 ` Simon Marchi 1 sibling, 1 reply; 12+ messages in thread From: Tom Tromey @ 2026-11-21 0:50 UTC (permalink / raw) To: Andrew Burgess; +Cc: Sendao Yan, gdb-patches, tom >>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: >> + /* The cooked index does not necessarily contain every symbol that >> + the expanded symtabs do. In particular, GCC emits a DWARF5 >> + DW_TAG_variable for a static constexpr member using the DWARF >> + spelling of the enclosing template arguments (e.g. >> + std::ratio<1, 1>), while the C++ expression evaluator looks the >> + member up using the canonical spelling (std::ratio<1l, 1l>), so >> + the quick lookup misses it. Fall back to walking the symtabs, >> + which was the behaviour before commit 86ac8c546235. >> + >> + PR symtab/33533. */ >> + result = lookup_symbol_in_objfile_symtabs (objfile, block_index, >> + name, domain); I don't think we want to go back to doing this. I hate to NAK a patch but I think I will have to for this one. Andrew> My concern here is that if you *rely* on finding symbols in the expanded Andrew> symtabs, then the lookup only works if something else first expands the Andrew> symtab. This means we have random bugs where things don't work, then Andrew> they'll suddenly start working (once the symtab has expanded). ... and this is why. This was basically the reason for commit 86ac8c546235. I think the fix has to involve the lookup succeeding in the cooked index. One question I have is why the name isn't canonicalized in the cooked index, or to put it another way, why the C++ expression lookup and the cooked index disagree -- since the idea behind canonicalization is that there's a single spelling. thanks Tom ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses 2026-11-21 0:50 ` Tom Tromey @ 2026-11-21 3:53 ` Simon Marchi 2026-11-21 16:31 ` Tom Tromey 0 siblings, 1 reply; 12+ messages in thread From: Simon Marchi @ 2026-11-21 3:53 UTC (permalink / raw) To: Tom Tromey, Andrew Burgess; +Cc: Sendao Yan, gdb-patches On 11/20/26 7:50 PM, Tom Tromey wrote: >>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: > >>> + /* The cooked index does not necessarily contain every symbol that >>> + the expanded symtabs do. In particular, GCC emits a DWARF5 >>> + DW_TAG_variable for a static constexpr member using the DWARF >>> + spelling of the enclosing template arguments (e.g. >>> + std::ratio<1, 1>), while the C++ expression evaluator looks the >>> + member up using the canonical spelling (std::ratio<1l, 1l>), so >>> + the quick lookup misses it. Fall back to walking the symtabs, >>> + which was the behaviour before commit 86ac8c546235. >>> + >>> + PR symtab/33533. */ >>> + result = lookup_symbol_in_objfile_symtabs (objfile, block_index, >>> + name, domain); > > I don't think we want to go back to doing this. I hate to NAK a patch > but I think I will have to for this one. > > Andrew> My concern here is that if you *rely* on finding symbols in the expanded > Andrew> symtabs, then the lookup only works if something else first expands the > Andrew> symtab. This means we have random bugs where things don't work, then > Andrew> they'll suddenly start working (once the symtab has expanded). > > ... and this is why. This was basically the reason for commit > 86ac8c546235. > > I think the fix has to involve the lookup succeeding in the cooked > index. > > One question I have is why the name isn't canonicalized in the cooked > index, or to put it another way, why the C++ expression lookup and the > cooked index disagree -- since the idea behind canonicalization is that > there's a single spelling. I don't know the whole story behind this particular bug, but the std::ratio<1, 1> vs std::ratio<1l, 1l> thing is: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81932 Strictly speaking, S<1> and S<1l> mean two different types. One has an "int" template parameter and the other a "long int" template parameter. I don't think it would make sense for them to canonicalize to the same thing, as (1) the conversion would be lossy and (2) the type names would become ambiguous. I think that what we want is... more approximate/fuzzy matching. To illustrate the problem again, using the type from the test: template<long N> struct S {}; S<1> s; - gcc does not put the integer suffix, so gives it DW_AT_name "S<1>" - clang puts the integer suffix, so gives it DW_AT_name "S<1L>" So the cooked index contains either "S<1>" or "S<1L>", depending on the compiler. Knowing this, we want lookups of either "S<1>" or "S<1L>" to work, because they are both legitimate queries. How do we achieve that? It does sound tempting to just canonicalize everything to "S<1>" (i.e. drop the suffixes), but then what if you have both "S<1>" and "S<1L>" in your program, how do you differentiate them? Perhaps it's fine for the index to store "simplified" names that don't include the suffixes, but I wouldn't call that a "canonical" name. If the program contains both "S<1>" and "S<1L>", then I'd want a lookup of "S<1>" to find the former and a lookup of "S<1L>" to find the latter. To achieve this, it sounds like we need some ranking system ("this match is better than this match"), a bit like when looking for the best function overload to call. We would therefore need a way to tell the two types apart. Admittedly, this is a bit of a corner case, but I believe that if we try to fix the problem generally, we'll end up with a more robust solution and less trouble in the long term. Regarding the DW_AT_names given by gcc: given that they lack potentially crucial information to know the type of the value parameters, maybe we should consider rebuilding the names from the DW_TAG_template_value_parameter DIEs. Those DIEs point to the exact type of each parameter, so we could reconstruct "S<1L>". I think the path already exists in the DWARF reader, we just don't take it because it is more expensive and we trust that DW_AT_name is correct. Simon ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses 2026-11-21 3:53 ` Simon Marchi @ 2026-11-21 16:31 ` Tom Tromey 2026-11-22 12:03 ` [PATCH v3 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members Sendao Yan 0 siblings, 1 reply; 12+ messages in thread From: Tom Tromey @ 2026-11-21 16:31 UTC (permalink / raw) To: Simon Marchi; +Cc: Tom Tromey, Andrew Burgess, Sendao Yan, gdb-patches >>>>> "Simon" == Simon Marchi <simark@simark.ca> writes: Simon> Strictly speaking, S<1> and S<1l> mean two different types. One has Simon> an "int" template parameter and the other a "long int" template Simon> parameter. I don't think it would make sense for them to canonicalize Simon> to the same thing, as (1) the conversion would be lossy and (2) the Simon> type names would become ambiguous. I think that what we want is... more Simon> approximate/fuzzy matching. I don't truly dislike fuzzy matching; but what I do strongly dislike is strcmp_iw, which is basically C++-isms getting into the gdb core. I'd prefer that the symbol tables be dumber and, if necessary, the logic be pushed into the C++ parser or some other place where it doesn't require the core of gdb to have language-specific treatment. I tend to think that instead of "lookup", gdb would be better if access to the symtabs looked more like iteration. Then the C++ parser could do whatever weird stuff it needs -- in the C++ support files. This could maybe be done with some kind of callback thing instead. This would solve the best_symbol thing as well. Simon> - gcc does not put the integer suffix, so gives it DW_AT_name "S<1>" Simon> - clang puts the integer suffix, so gives it DW_AT_name "S<1L>" Simon> So the cooked index contains either "S<1>" or "S<1L>", depending on the Simon> compiler. Knowing this, we want lookups of either "S<1>" or "S<1L>" to Simon> work, because they are both legitimate queries. How do we achieve that? Simon> It does sound tempting to just canonicalize everything to "S<1>" (i.e. Simon> drop the suffixes), but then what if you have both "S<1>" and "S<1L>" in Simon> your program, how do you differentiate them? Perhaps it's fine for the Simon> index to store "simplified" names that don't include the suffixes, Simon> but I wouldn't call that a "canonical" name. Yeah, that's a real problem. I think you hit on a solution below. Simon> Regarding the DW_AT_names given by gcc: given that they lack potentially Simon> crucial information to know the type of the value parameters, maybe we Simon> should consider rebuilding the names from the Simon> DW_TAG_template_value_parameter DIEs. We already need to do this due to -gsimple-template-names. Relevant bugs: https://sourceware.org/bugzilla/show_bug.cgi?id=30023 https://sourceware.org/bugzilla/show_bug.cgi?id=32421 Tom ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members 2026-11-21 16:31 ` Tom Tromey @ 2026-11-22 12:03 ` Sendao Yan 2026-11-22 12:03 ` [PATCH v3 1/1] " Sendao Yan 2026-11-23 2:09 ` [PATCH v4 0/1] " Sendao Yan 0 siblings, 2 replies; 12+ messages in thread From: Sendao Yan @ 2026-11-22 12:03 UTC (permalink / raw) To: gdb-patches Cc: Tom Tromey, Andrew Burgess, Keith Seitz, Simon Marchi, yansendao Hi Tom, Simon, Andrew, Keith, This is v3 of the fix for PR symtab/33533 (GCC PR 128817), where the libstdc++ chrono pretty-printer reports a static constexpr member as <optimized out>. Tom, thanks for the pointer to PR 30023/32421 and -gsimple-template-names. I agree that rebuilding names from the DW_TAG_template_value_parameter DIEs is the right general direction, and that it is needed anyway for -gsimple-template-names. This version does not attempt that general reconstruction. It is a targeted fix for the quick-lookup miss, and it is not fuzzy matching (no strcmp_iw). What v3 does: for a C++ static data member, the cooked index contains the DW_AT_name spelling (with GCC, S<1>::v) and a separate entry for the linkage name. v3 adds one synthesized index entry whose name is the demangled linkage name (S<1l>::v), so the quick lookup finds the member without expanding symtabs. Each alias is derived from the symbol's own linkage name, so T<1> (int) and T<1L> (long) get different names and are not conflated. The alias is skipped when the entry built from DW_AT_name already has the canonical spelling, as with a clang-built binary. Only entries whose parent is a type or namespace are considered, which excludes function-local statics. If the DW_AT_name entry is not adjacent (the index came from .debug_names), the alias is skipped rather than added without these checks. Only Itanium-mangled (_Z) names with an I...E argument block are considered. The alias name is produced with the v3 demangler directly (still through gdb_demangle's crash handler), so index construction does not depend on the user's current demangle-style. The demangling and filtering live in cp-support.c, so the index code stays language-agnostic apart from the language_cplus check. The comparison uses the DWARF spelling of the DW_AT_name entry (a new FOR_DWARF_NAME flag) rather than its canonical spelling, because canonicalize_names runs per shard concurrently and a deferred parent can belong to a shard that has not been canonicalized yet. The aliases are written to a saved .gdb_index: the synthesized entry does not carry IS_LINKAGE or IS_SYNTHESIZED, and an entry is also added for the canonical spelling of each enclosing template scope (S<1l> for S<1l>::v), because the .gdb_index reader rebuilds qualified names from their components. Those scope entries are parentless and store the full prefix; they are only there for the reader, which re-splits the name itself. Because the alias is not IS_SYNTHESIZED, it is also written by save gdb-index -dwarf-5 and survives a .debug_names reload. Why not reconstruct from the DIEs here: the cooked index is built by scanning raw DWARF with cutu_reader, before die_info trees are created; dwarf2_compute_name is only usable at symtab-expansion time. Rebuilding names in the indexer would mean parsing template parameter DIEs there and printing values with their C++ literal type (the current dwarf2_compute_name prints "1" rather than "1l"), which is the larger -gsimple-template-names work. I am happy to prototype that as a follow-up, but it is a much bigger change than this bug fix. If you would prefer to hold this patch until the general reconstruction lands, please say so; otherwise I think the guarded alias is a small fix that keeps the quick lookup authoritative and does not change lookup semantics. Testing: - new test gdb.cp/static-const-template.exp: 18 PASS; it checks the long member, the canonical spelling, that T<1> (int) and T<1L> (long) remain distinct (whatis reports const int vs const long), a member of a template nested in a namespace, that the canonical spellings survive saving and reloading a .gdb_index, and that the alias is emitted in a DWARF-5 .debug_names index; - GCC libstdc++-prettyprinters/chrono.cc: 54/54 PASS, including -flto; - gdb.cp/static-const-template.exp, gdb.dwarf2/static-const-member.exp, gdb.dwarf2/forward-spec-inter-cu.exp, gdb.cp/m-static.exp, gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp: 244 PASS, 0 FAIL; - gdb.dwarf2/debug-names*.exp: 27 PASS, 0 FAIL; - full gdb.cp suite: 8779 PASS and 70 failures, all in gdb.cp/step-and-next-inline.exp. Unpatched gdb 16.3 fails the same way, so this is a testsuite/compiler environment issue. Sendao Yan (1): [gdb/symtab] Add canonical-name aliases for C++ template static members gdb/cp-support.c | 40 +++++- gdb/cp-support.h | 12 +- gdb/dwarf2/cooked-index-entry.c | 14 +- gdb/dwarf2/cooked-index-entry.h | 9 ++ gdb/dwarf2/cooked-index-shard.c | 124 ++++++++++++++++++ gdb/testsuite/gdb.cp/static-const-template.cc | 58 ++++++++ .../gdb.cp/static-const-template.exp | 118 +++++++++++++++++ 7 files changed, 365 insertions(+), 10 deletions(-) create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp -- 2.34.1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 1/1] [gdb/symtab] Add canonical-name aliases for C++ template static members 2026-11-22 12:03 ` [PATCH v3 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members Sendao Yan @ 2026-11-22 12:03 ` Sendao Yan 2026-11-23 2:09 ` [PATCH v4 0/1] " Sendao Yan 1 sibling, 0 replies; 12+ messages in thread From: Sendao Yan @ 2026-11-22 12:03 UTC (permalink / raw) To: gdb-patches Cc: Tom Tromey, Andrew Burgess, Keith Seitz, Simon Marchi, yansendao The cooked index stores names as they appear in the DWARF. For a static data member of a class template, GCC's DW_AT_name omits the integer type suffix (S<1>::v), while the C++ expression evaluator looks the member up using the canonical spelling from the demangled linkage name (S<1l>::v). Since commit 86ac8c546235 lookup_symbol_in_objfile only searches the cooked index, so the lookup misses and GDB reports the member as optimized out. This makes libstdc++'s chrono pretty-printer fail (PR symtab/33533, GCC PR 128817). Add a synthesized cooked-index alias for C++ static data members using the demangled linkage name. The alias is derived from the linkage name of this particular symbol, which encodes the exact template argument type, so T<1> (int) and T<1L> (long) get distinct spellings and are not conflated. The alias is skipped when the entry built from DW_AT_name already has the canonical spelling, so a clang-built binary (whose DWARF name already has the suffix) is unaffected. Only entries whose parent is a type or namespace are considered, which excludes function-local statics. Only Itanium-mangled (_Z) names with an I...E argument block are considered. The alias name is produced with the v3 demangler directly (still through gdb_demangle's crash handler), so index construction does not depend on the user's current demangle-style. This keeps the quick lookup authoritative; no symtab-expansion fallback is needed. The DWARF-spelling comparison uses a new FOR_DWARF_NAME flag instead of the default canonical spelling. canonicalize_names() runs per shard concurrently, and a deferred parent may belong to a shard whose canonical field has not been set yet; FOR_DWARF_NAME builds the name from the immutable DWARF names and avoids that race. The comparison allocates its name on a temporary obstack. If the DW_AT_name entry is not adjacent (the index was not built by the DIE scanner, e.g. it came from .debug_names), skip the alias rather than bypassing the checks. The alias does not carry IS_LINKAGE or IS_SYNTHESIZED, so it is written to a saved .gdb_index and .debug_names; C++ linkage names are otherwise omitted from .gdb_index, and synthesized entries are otherwise omitted from .debug_names. For the same reason, synthesize an entry for the canonical spelling of each enclosing template scope (S<1l> for S<1l>::v), because the .gdb_index reader rebuilds qualified names from their components and needs each prefix to be present. Add gdb.cp/static-const-template.exp, which prints a static constexpr member of a class template with a long non-type template parameter, checks that int and long instantiations of the same template remain distinct, covers a member of a template nested in a namespace, checks that the canonical spellings survive saving and reloading a .gdb_index, and checks that the alias is emitted in a DWARF-5 .debug_names index. With the unpatched gdb the long lookup fails with <optimized out>; with the fix it passes. Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc (54/54 PASS, including the -flto variants), the new test, and the existing gdb.dwarf2/static-const-member.exp, gdb.dwarf2/forward-spec-inter-cu.exp, gdb.cp/m-static.exp, gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp (241 PASS); gdb.dwarf2/debug-names*.exp: 27 PASS. Also verified by hand that save gdb-index -dwarf-5 / reload finds S<1l>::v and reports its type as const long. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533 --- gdb/cp-support.c | 40 +++++- gdb/cp-support.h | 12 +- gdb/dwarf2/cooked-index-entry.c | 14 +- gdb/dwarf2/cooked-index-entry.h | 9 ++ gdb/dwarf2/cooked-index-shard.c | 124 ++++++++++++++++++ gdb/testsuite/gdb.cp/static-const-template.cc | 58 ++++++++ .../gdb.cp/static-const-template.exp | 118 +++++++++++++++++ 7 files changed, 365 insertions(+), 10 deletions(-) create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp diff --git a/gdb/cp-support.c b/gdb/cp-support.c index 51c836a06d0..df560186161 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -665,6 +665,37 @@ cp_canonicalize_string (const char *string) return us; } +/* See cp-support.h. */ + +gdb::unique_xmalloc_ptr<char> +cp_demangle_template_linkage_name (const char *linkage_name) +{ + /* Only an Itanium-mangled (_Z) C++ name can be a template + instantiation. An Itanium template instantiation always has an + I...E argument block, so avoid demangling names that cannot have + one. The _Z check also keeps plain identifiers that happen to be + valid type encodings out of the index. */ + if (strncmp (linkage_name, "_Z", 2) != 0 + || strchr (linkage_name, 'I') == nullptr) + return nullptr; + + /* Force the v3 demangler: cplus_demangle() returns the mangled name + unchanged when the user has set demangle-style to none, even if + DMGL_GNU_V3 is requested, and the cooked index must not depend on the + user's display preference. This still goes through the demangler + crash handler in gdb_demangle. */ + gdb::unique_xmalloc_ptr<char> demangled + = gdb_demangle (linkage_name, + DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES, true); + + if (demangled == nullptr + || strchr (demangled.get (), ':') == nullptr + || strchr (demangled.get (), '<') == nullptr) + return nullptr; + + return demangled; +} + /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the block of used memory that should be freed when finished with the tree. DEMANGLED_P is set to the char * that should be @@ -1586,7 +1617,7 @@ report_failed_demangle (const char *name, bool core_dump_allowed, /* A wrapper for bfd_demangle. */ gdb::unique_xmalloc_ptr<char> -gdb_demangle (const char *name, int options) +gdb_demangle (const char *name, int options, bool force_v3) { gdb::unique_xmalloc_ptr<char> result; int crash_signal = 0; @@ -1617,7 +1648,12 @@ gdb_demangle (const char *name, int options) #endif if (crash_signal == 0) - result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); + { + if (force_v3) + result.reset (cplus_demangle_v3 (name, options | DMGL_VERBOSE)); + else + result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); + } #ifdef HAVE_WORKING_FORK if (catch_demangler_crashes) diff --git a/gdb/cp-support.h b/gdb/cp-support.h index b3040506c8a..7bc1f5566a2 100644 --- a/gdb/cp-support.h +++ b/gdb/cp-support.h @@ -82,6 +82,12 @@ struct demangle_parse_info extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string (const char *string); +/* Return the demangled spelling of LINKAGE_NAME when it names a scoped + template instantiation, or NULL otherwise. */ + +extern gdb::unique_xmalloc_ptr<char> cp_demangle_template_linkage_name + (const char *linkage_name); + extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string_no_typedefs (const char *string); @@ -194,9 +200,11 @@ extern struct cmd_list_element *maint_cplus_cmd_list; libiberty's demangler to expand standard substitutions to their full template name. */ -/* A wrapper for bfd_demangle. */ +/* A wrapper for bfd_demangle. When FORCE_V3 is true, use the v3 + demangler directly, ignoring the user's demangle-style setting. */ -gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options); +gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options, + bool force_v3 = false); /* A wrapper for cplus_demangle_print. */ diff --git a/gdb/dwarf2/cooked-index-entry.c b/gdb/dwarf2/cooked-index-entry.c index 78a74273616..0a688ed453c 100644 --- a/gdb/dwarf2/cooked-index-entry.c +++ b/gdb/dwarf2/cooked-index-entry.c @@ -175,7 +175,8 @@ cooked_index_entry::full_name (struct obstack *storage, cooked_index_full_name_flag name_flags, const char *default_sep) const { - const char *local_name = ((name_flags & FOR_MAIN) != 0) ? name () : canonical; + const char *local_name + = ((name_flags & (FOR_MAIN | FOR_DWARF_NAME)) != 0) ? name () : canonical; if ((flags & IS_LINKAGE) != 0 || get_parent () == nullptr) return local_name; @@ -238,11 +239,12 @@ cooked_index_entry::write_scope (struct obstack *storage, cooked_index_shard::prune_nameless_entries. */ gdb_assert (!name_is_deferred ()); - /* When computing the Ada linkage name, the entry might not have - been canonicalized yet, so use the 'name'. */ - const char *local_name = ((flags & (FOR_MAIN | FOR_ADA_LINKAGE_NAME)) != 0 - ? name () - : canonical); + /* When computing the Ada linkage name or the DWARF spelling, the + entry might not have been canonicalized yet, so use the 'name'. */ + const char *local_name + = ((flags & (FOR_MAIN | FOR_ADA_LINKAGE_NAME | FOR_DWARF_NAME)) != 0 + ? name () + : canonical); obstack_grow (storage, local_name, strlen (local_name)); obstack_grow (storage, sep, strlen (sep)); } diff --git a/gdb/dwarf2/cooked-index-entry.h b/gdb/dwarf2/cooked-index-entry.h index 462ec0640a9..d9a8f001e55 100644 --- a/gdb/dwarf2/cooked-index-entry.h +++ b/gdb/dwarf2/cooked-index-entry.h @@ -60,6 +60,10 @@ enum cooked_index_full_name_enum : unsigned char FOR_MAIN = 1, /* Set when requesting the linkage name for an Ada entry. */ FOR_ADA_LINKAGE_NAME = 2, + /* Set when requesting the name as spelled in the DWARF, rather than + the canonical name. This does not read the canonical field, so it + is safe to use before all shards have been canonicalized. */ + FOR_DWARF_NAME = 4, }; DEF_ENUM_FLAGS_TYPE (enum cooked_index_full_name_enum, cooked_index_full_name_flag); @@ -168,6 +172,11 @@ struct cooked_index_entry : public allocate_on_obstack<cooked_index_entry> symbols will have their "linkage-style" name computed. The default is source-style. + If the FOR_DWARF_NAME flag is set, the name is built from the + entries' names as spelled in the DWARF; the canonical field is not + consulted. This is safe to use while shards are still being + canonicalized concurrently. + If the language doesn't prescribe a separator, one can be specified using DEFAULT_SEP. */ const char *full_name (struct obstack *storage, diff --git a/gdb/dwarf2/cooked-index-shard.c b/gdb/dwarf2/cooked-index-shard.c index 206fe4483a4..ebb8f2200b1 100644 --- a/gdb/dwarf2/cooked-index-shard.c +++ b/gdb/dwarf2/cooked-index-shard.c @@ -23,8 +23,10 @@ #include "dwarf2/read.h" #include "dwarf2/error.h" #include "cp-support.h" +#include "split-name.h" #include "c-lang.h" #include "ada-lang.h" +#include <unordered_set> /* Return true if a plain "main" could be the main program for this language. Languages that are known to use some other mechanism are @@ -304,6 +306,7 @@ cooked_index_shard::canonicalize_names () htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, nullptr, xcalloc, xfree)); std::vector<cooked_index_entry *> new_gnat_entries; + std::vector<cooked_index_entry *> new_cxx_entries; for (cooked_index_entry *entry : m_entries) { @@ -393,11 +396,132 @@ cooked_index_shard::canonicalize_names () entry->canonical = entry->name (); } + /* For a C++ static data member, the DWARF name of the enclosing + template can use a non-canonical spelling of the template + arguments, while lookups use the canonical spelling from the + demangled linkage name. For example, GCC emits S<1>::v while the + demangled linkage name is S<1l>::v. The cooked index therefore + needs an entry for the canonical spelling as well. + + The alias is derived from the linkage name of this particular + symbol, so S<1> (int) and S<1L> (long) stay distinct: each symbol + gets the spelling encoded by its own linkage name. Skip the alias + when the entry built from DW_AT_name already uses the canonical + spelling (for instance a clang-built binary whose DWARF name + already has the integer suffix). PR symtab/33533. */ + + /* Names of the enclosing scopes whose canonical spelling has already + been added, to avoid duplicates within this shard. */ + std::unordered_set<std::string> enclosing_names; + + for (size_t i = 0; i < m_entries.size (); ++i) + { + cooked_index_entry *entry = m_entries[i]; + + if ((entry->flags & IS_LINKAGE) == 0 + || entry->lang != language_cplus + || (entry->tag != DW_TAG_variable + && entry->tag != DW_TAG_member)) + continue; + + gdb::unique_xmalloc_ptr<char> demangled + = cp_demangle_template_linkage_name (entry->name ()); + if (demangled == nullptr) + continue; + + /* The index entry built from DW_AT_name is added directly before + the linkage-name entry for the same DIE. */ + const cooked_index_entry *dwarf_entry = nullptr; + if (i > 0) + { + const cooked_index_entry *prev = m_entries[i - 1]; + if (prev->die_offset == entry->die_offset + && prev->per_cu == entry->per_cu + && (prev->flags & (IS_LINKAGE | IS_SYNTHESIZED)) == 0) + dwarf_entry = prev; + } + + /* If the DW_AT_name entry is not adjacent, the index was not + built by the DIE scanner (for example, it came from + .debug_names), so the checks below cannot be applied. Skip + the alias rather than adding one without them. */ + if (dwarf_entry == nullptr) + continue; + + /* Use a temporary obstack for this probe. Use the DWARF + spelling: this shard may run before the shard owning an + ancestor has canonicalized its entries. */ + auto_obstack temp; + const char *full = dwarf_entry->full_name (&temp, FOR_DWARF_NAME); + + /* If the full name already has the canonical spelling, there is + nothing to add; this is the case for clang, which includes the + integer suffix in DW_AT_name. */ + if (strcmp (demangled.get (), full) == 0) + continue; + + /* Only add an alias when the parent is a type or namespace. + This excludes function-local statics, whose parent is a + subprogram. */ + const cooked_index_entry *parent = dwarf_entry->get_parent (); + if (parent != nullptr && !tag_is_type (parent->tag)) + continue; + + /* Clear IS_LINKAGE so that the alias is written to a saved + .gdb_index; C++ linkage names are otherwise omitted from the + cache. The alias has no parent, so full_name() still returns + its canonical name. Do not mark it IS_SYNTHESIZED: unlike the + Ada package entries, this alias names a real DIE and should be + written to .debug_names as well. */ + const char *canon = m_names.insert (std::move (demangled)); + cooked_index_entry *alias + = create (entry->die_offset, entry->tag, + entry->flags & ~IS_LINKAGE, + entry->lang, canon, nullptr, entry->per_cu); + alias->canonical = canon; + new_cxx_entries.push_back (alias); + + /* Also add the canonical spelling of each enclosing template + scope. A .gdb_index reader splits a symbol name into its + components and re-links them, but it can only do that if each + prefix has an entry too. These entries are deliberately + parentless and store the whole prefix: the reader re-splits the + name itself, and they are not intended for direct scope lookup. + A lookup for a scope-only spelling can still match one of them + and expand its CU before missing; that is the cost of making + saved indexes round-trip. */ + std::vector<std::string_view> components + = split_name (canon, split_style::CXX); + std::string prefix; + for (size_t j = 0; j + 1 < components.size (); ++j) + { + if (!prefix.empty ()) + prefix += "::"; + prefix.append (components[j].data (), components[j].size ()); + + if (prefix.find ('<') == std::string::npos) + continue; + if (!enclosing_names.insert (prefix).second) + continue; + + const char *enclosing_name + = m_names.insert (std::string_view (prefix)); + cooked_index_entry *enclosing + = create (entry->die_offset, DW_TAG_structure_type, + IS_SYNTHESIZED, entry->lang, enclosing_name, nullptr, + entry->per_cu); + enclosing->canonical = enclosing_name; + new_cxx_entries.push_back (enclosing); + } + } + /* Make sure any new Ada entries end up in the results. This isn't done when creating these new entries to avoid invalidating the m_entries iterator used in the foreach above. */ m_entries.insert (m_entries.end (), new_gnat_entries.begin (), new_gnat_entries.end ()); + m_entries.insert (m_entries.end (), new_cxx_entries.begin (), + new_cxx_entries.end ()); m_entries.shrink_to_fit (); std::sort (m_entries.begin (), m_entries.end (), diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc new file mode 100644 index 00000000000..f359f265406 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.cc @@ -0,0 +1,58 @@ +/* Copyright 2026 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 <http://www.gnu.org/licenses/>. */ + +/* A static constexpr member of a class template with a non-type template + parameter. The reference from the member typedef forces GCC to emit the + declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5. */ + +template<long N> +struct S +{ + static constexpr unsigned v = N; + using P = int[v]; +}; + +S<1> s; + +/* The same template name instantiated with both int and long non-type + arguments. A canonical-name alias must not conflate the two. */ +template<auto N> +struct T +{ + static constexpr auto v = N; +}; + +T<1> t_int; +T<1L> t_long; + +/* A member of a template in a namespace: the canonical spelling has + multiple scope components. */ +namespace N +{ + template<long M> + struct U + { + static constexpr unsigned v = M; + using P = int[v]; + }; +} + +N::U<1> u; + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp new file mode 100644 index 00000000000..c414286f553 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.exp @@ -0,0 +1,118 @@ +# Copyright 2026 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 <http://www.gnu.org/licenses/>. + +# Test printing a static constexpr member of a class template with a +# non-type template parameter. +# +# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression +# evaluator looks the member up using the canonical spelling S<1l>. The +# cooked index stores the DWARF spelling, so before the fix for PR +# symtab/33533 the lookup missed and gdb printed <optimized out>. + +standard_testfile .cc + +# Can't save an index with readnow. +require !readnow + +set opts {debug c++ dwarf5} +lappend opts additional_flags=-std=c++17 + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} { + return +} + +# Save a .gdb_index now, before the inferior is started, so that only +# the main objfile is indexed. The aliases must survive this round +# trip; the reload is checked below. +set filename [file tail $binfile] +set host_program [gdb_remote_download host $binfile] +set index_file [host_standard_output_file $filename.gdb-index] +remote_file host delete $index_file +gdb_test_no_output "save gdb-index [file dirname $index_file]" \ + "save gdb-index for $filename" + +if {![remote_file host exists $index_file]} { + unsupported "gdb-index file not created" + return +} +pass "gdb-index file created" + +# Also check that the alias is written to a DWARF-5 .debug_names index. +# Names that are not already in .debug_str are emitted in the .debug_str +# addition file, so check that file. +set debug_str_file [host_standard_output_file $filename.debug_str] +remote_file host delete $debug_str_file +gdb_test_no_output "save gdb-index -dwarf-5 [file dirname $index_file]" \ + "save .debug_names index for $filename" + +if {[remote_file host exists $debug_str_file]} { + pass ".debug_str addition created" + set fp [open $debug_str_file rb] + set contents [read $fp] + close $fp + gdb_assert {[string first "S<1l>::v" $contents] != -1} \ + "canonical alias present in .debug_names" +} else { + unsupported ".debug_str addition not created" +} + +if {![runto_main]} { + return +} + +gdb_test "print s.v" "= 1" \ + "print static constexpr member of a template" + +gdb_test "print 'S<1l>::v'" "= 1" \ + "print static constexpr member with canonical template arguments" + +# Both T<1> and T<1L> are instantiated. Their DWARF names are identical +# (T<1>), but the canonical spellings differ. Check that the long +# instantiation is not conflated with the int one. +gdb_test "print t_int.v" "= 1" +gdb_test "print t_long.v" "= 1" +gdb_test "whatis 'T<1>::v'" "type = (const )?int" +gdb_test "whatis 'T<1l>::v'" "type = (const )?long( int)?" + +gdb_test "print 'N::U<1l>::v'" "= 1" \ + "print member of a template in a namespace" +gdb_test "whatis 'N::U<1l>::v'" "type = (const )?unsigned int" + +# The synthesized aliases must also survive saving and reloading a +# .gdb_index. Embed the saved index in a copy of the program; the +# index reader rebuilds qualified names from their components, so this +# also checks that the enclosing scope aliases were emitted. +set host_program_with_index $host_program.with-index +set program_with_index $binfile.with-index +remote_file host delete $host_program_with_index +if {[run_on_host "objcopy" [gdb_find_objcopy] \ + [join [list \ + "--remove-section .gdb_index" \ + "--add-section .gdb_index=$index_file" \ + "--set-section-flags .gdb_index=readonly" \ + "$host_program" "$host_program_with_index"]]]} { + return +} + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir +gdb_test "file $program_with_index" "Reading symbols from .*" \ + "load program with embedded .gdb_index" +gdb_test "print 'S<1l>::v'" "= 1" \ + "canonical member after .gdb_index reload" +gdb_test "print 'T<1l>::v'" "= 1" \ + "canonical auto member after .gdb_index reload" +gdb_test "print 'N::U<1l>::v'" "= 1" \ + "canonical namespaced member after .gdb_index reload" -- 2.34.1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v4 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members 2026-11-22 12:03 ` [PATCH v3 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members Sendao Yan 2026-11-22 12:03 ` [PATCH v3 1/1] " Sendao Yan @ 2026-11-23 2:09 ` Sendao Yan 2026-11-23 2:09 ` [PATCH v4 1/1] " Sendao Yan 1 sibling, 1 reply; 12+ messages in thread From: Sendao Yan @ 2026-11-23 2:09 UTC (permalink / raw) To: gdb-patches Cc: Tom Tromey, Andrew Burgess, Keith Seitz, Simon Marchi, yansendao Hi Tom, Simon, Andrew, Keith, This is v4 of the fix for PR symtab/33533 (GCC PR 128817). v3 was sent as [PATCH v3 0/1]; v4 supersedes it. I sent v4 before any human review of v3 because an automated review of v3 flagged that static member functions have the same spelling mismatch, and I confirmed that gdb.lookup_global_symbol("S<1l>::f") missed in v3. Sorry for the extra traffic; the delta is small and is the only change from v3. Changes since v3: - canonicalize_names() now also accepts DW_TAG_subprogram linkage entries, so static member functions get a canonical alias; - functions are demangled without DMGL_PARAMS, so the alias is S<1l>::f rather than S<1l>::f() and matches the parameter-stripped lookup name; - the test covers a class template with only a static member function (call and type lookup, .gdb_index reload, .debug_names emission). Everything else is unchanged from v3. Static member functions have the same DWARF/canonical mismatch as data members: the index has S<1>::f while the canonical lookup uses S<1l>::f. In v3 the expression forms happened to find the function through symtab expansion, but the quick index returned no symbol for gdb.lookup_global_symbol("S<1l>::f"). Tom, thanks for the pointer to PR 30023/32421 and -gsimple-template-names. I agree that rebuilding names from the DW_TAG_template_value_parameter DIEs is the right general direction, and that it is needed anyway for -gsimple-template-names. This version does not attempt that general reconstruction. It is a targeted fix for the quick-lookup miss, and it is not fuzzy matching (no strcmp_iw). What v4 does: for a C++ static data member or static member function, the cooked index contains the DW_AT_name spelling (with GCC, S<1>::v) and a separate entry for the linkage name. v4 adds one synthesized index entry whose name is the demangled linkage name (S<1l>::v), so the quick lookup finds the member without expanding symtabs. Each alias is derived from the symbol's own linkage name, so T<1> (int) and T<1L> (long) get different names and are not conflated. The alias is skipped when the entry built from DW_AT_name already has the canonical spelling, as with a clang-built binary. Only entries whose parent is a type or namespace are considered. Only Itanium-mangled (_Z) names with an I...E argument block are considered. The alias name is produced with the v3 demangler directly (still through gdb_demangle's crash handler), so index construction does not depend on the user's current demangle-style. The demangling and filtering live in cp-support.c, so the index code stays language-agnostic apart from the language_cplus check. The comparison uses the DWARF spelling of the DW_AT_name entry (a new FOR_DWARF_NAME flag) rather than its canonical spelling, because canonicalize_names runs per shard concurrently and a deferred parent can belong to a shard that has not been canonicalized yet. If the DW_AT_name entry is not adjacent (the index came from .debug_names), the alias is skipped rather than added without these checks. The aliases are written to a saved .gdb_index and .debug_names: the synthesized entry does not carry IS_LINKAGE or IS_SYNTHESIZED, and an entry is also added for the canonical spelling of each enclosing template scope (S<1l> for S<1l>::v), because the .gdb_index reader rebuilds qualified names from their components. Those scope entries are parentless and store the full prefix; they are only there for the reader, which re-splits the name itself. Why not reconstruct from the DIEs here: the cooked index is built by scanning raw DWARF with cutu_reader, before die_info trees are created; dwarf2_compute_name is only usable at symtab-expansion time. Rebuilding names in the indexer would mean parsing template parameter DIEs there and printing values with their C++ literal type (the current dwarf2_compute_name prints "1" rather than "1l"), which is the larger -gsimple-template-names work. I am happy to prototype that as a follow-up, but it is a much bigger change than this bug fix. If you would prefer to hold this patch until the general reconstruction lands, please say so; otherwise I think the guarded alias is a small fix that keeps the quick lookup authoritative and does not change lookup semantics. Testing (x86_64 and aarch64): - new test gdb.cp/static-const-template.exp: 22 PASS on both; it checks the long data member, the canonical spelling, that T<1> (int) and T<1L> (long) remain distinct, a member of a template nested in a namespace, a static member function found by its canonical spelling, saving and reloading a .gdb_index, and alias emission in a DWARF-5 .debug_names index; - GCC libstdc++-prettyprinters/chrono.cc: 54/54 PASS on x86_64, including -flto (the aarch64 build has no Python, so the pretty-printer test was not run there); - the six targeted .exp files: 248 PASS, 0 FAIL on both; - gdb.dwarf2/debug-names*.exp: 27 PASS, 0 FAIL on both; - full gdb.cp suite: x86_64 8783 PASS with 70 pre-existing failures in gdb.cp/step-and-next-inline.exp (also with unpatched gdb 16.3); aarch64 8914 PASS, 0 unexpected failures (2 unsupported because that build has no Python). Sendao Yan (1): [gdb/symtab] Add canonical-name aliases for C++ template static members gdb/cp-support.c | 41 +++++- gdb/cp-support.h | 12 +- gdb/dwarf2/cooked-index-entry.c | 14 +- gdb/dwarf2/cooked-index-entry.h | 9 ++ gdb/dwarf2/cooked-index-shard.c | 125 +++++++++++++++++ gdb/testsuite/gdb.cp/static-const-template.cc | 69 ++++++++++ .../gdb.cp/static-const-template.exp | 129 ++++++++++++++++++ 7 files changed, 389 insertions(+), 10 deletions(-) create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp -- 2.34.1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v4 1/1] [gdb/symtab] Add canonical-name aliases for C++ template static members 2026-11-23 2:09 ` [PATCH v4 0/1] " Sendao Yan @ 2026-11-23 2:09 ` Sendao Yan 0 siblings, 0 replies; 12+ messages in thread From: Sendao Yan @ 2026-11-23 2:09 UTC (permalink / raw) To: gdb-patches Cc: Tom Tromey, Andrew Burgess, Keith Seitz, Simon Marchi, yansendao The cooked index stores names as they appear in the DWARF. For a static data member of a class template, GCC's DW_AT_name omits the integer type suffix (S<1>::v), while the C++ expression evaluator looks the member up using the canonical spelling from the demangled linkage name (S<1l>::v). Since commit 86ac8c546235 lookup_symbol_in_objfile only searches the cooked index, so the lookup misses and GDB reports the member as optimized out. This makes libstdc++'s chrono pretty-printer fail (PR symtab/33533, GCC PR 128817). Static member functions have the same problem: their quick lookup uses S<1l>::f while the index only has S<1>::f. Add a synthesized cooked-index alias for C++ static data members and static member functions using the demangled linkage name. The alias is derived from the linkage name of this particular symbol, which encodes the exact template argument type, so T<1> (int) and T<1L> (long) get distinct spellings and are not conflated. The alias is skipped when the entry built from DW_AT_name already has the canonical spelling, so a clang-built binary (whose DWARF name already has the suffix) is unaffected. Only entries whose parent is a type or namespace are considered. Only Itanium-mangled (_Z) names with an I...E argument block are considered. The alias name is produced with the v3 demangler directly (still through gdb_demangle's crash handler), so index construction does not depend on the user's current demangle-style. Functions are demangled without DMGL_PARAMS, since index lookup ignores parameter lists, so the alias is S<1l>::f rather than S<1l>::f(). This keeps the quick lookup authoritative; no symtab-expansion fallback is needed. The DWARF-spelling comparison uses a new FOR_DWARF_NAME flag instead of the default canonical spelling. canonicalize_names() runs per shard concurrently, and a deferred parent may belong to a shard whose canonical field has not been set yet; FOR_DWARF_NAME builds the name from the immutable DWARF names and avoids that race. The comparison allocates its name on a temporary obstack. If the DW_AT_name entry is not adjacent (the index was not built by the DIE scanner, e.g. it came from .debug_names), skip the alias rather than bypassing the checks. The alias does not carry IS_LINKAGE or IS_SYNTHESIZED, so it is written to a saved .gdb_index and .debug_names; C++ linkage names are otherwise omitted from .gdb_index, and synthesized entries are otherwise omitted from .debug_names. For the same reason, synthesize an entry for the canonical spelling of each enclosing template scope (S<1l> for S<1l>::v), because the .gdb_index reader rebuilds qualified names from their components and needs each prefix to be present. Add gdb.cp/static-const-template.exp, which prints a static constexpr member of a class template with a long non-type template parameter, checks that int and long instantiations of the same template remain distinct, covers a member of a template nested in a namespace, checks that a static member function is found with its canonical template arguments, checks that the canonical spellings survive saving and reloading a .gdb_index, and checks that the aliases are emitted in a DWARF-5 .debug_names index. With the unpatched gdb the long lookups fail with <optimized out>; with the fix they pass. Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc (54/54 PASS, including the -flto variants), the new test, and the existing gdb.dwarf2/static-const-member.exp, gdb.dwarf2/forward-spec-inter-cu.exp, gdb.cp/m-static.exp, gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp (248 PASS); gdb.dwarf2/debug-names*.exp: 27 PASS. Full gdb.cp: 8783 PASS, with only the pre-existing environmental failures in gdb.cp/step-and-next-inline.exp. Also verified by hand that save gdb-index -dwarf-5 / reload finds S<1l>::v and reports its type as const long. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533 --- gdb/cp-support.c | 41 +++++- gdb/cp-support.h | 12 +- gdb/dwarf2/cooked-index-entry.c | 14 +- gdb/dwarf2/cooked-index-entry.h | 9 ++ gdb/dwarf2/cooked-index-shard.c | 125 +++++++++++++++++ gdb/testsuite/gdb.cp/static-const-template.cc | 69 ++++++++++ .../gdb.cp/static-const-template.exp | 129 ++++++++++++++++++ 7 files changed, 389 insertions(+), 10 deletions(-) create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp diff --git a/gdb/cp-support.c b/gdb/cp-support.c index 51c836a06d0..d5f6b643797 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -665,6 +665,38 @@ cp_canonicalize_string (const char *string) return us; } +/* See cp-support.h. */ + +gdb::unique_xmalloc_ptr<char> +cp_demangle_template_linkage_name (const char *linkage_name) +{ + /* Only an Itanium-mangled (_Z) C++ name can be a template + instantiation. An Itanium template instantiation always has an + I...E argument block, so avoid demangling names that cannot have + one. The _Z check also keeps plain identifiers that happen to be + valid type encodings out of the index. */ + if (strncmp (linkage_name, "_Z", 2) != 0 + || strchr (linkage_name, 'I') == nullptr) + return nullptr; + + /* Force the v3 demangler: cplus_demangle() returns the mangled name + unchanged when the user has set demangle-style to none, even if + DMGL_GNU_V3 is requested, and the cooked index must not depend on the + user's display preference. This still goes through the demangler + crash handler in gdb_demangle. Do not pass DMGL_PARAMS: index + lookup ignores parameter lists, so a function alias should be + S<1l>::f rather than S<1l>::f(). */ + gdb::unique_xmalloc_ptr<char> demangled + = gdb_demangle (linkage_name, DMGL_ANSI | DMGL_TYPES, true); + + if (demangled == nullptr + || strchr (demangled.get (), ':') == nullptr + || strchr (demangled.get (), '<') == nullptr) + return nullptr; + + return demangled; +} + /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the block of used memory that should be freed when finished with the tree. DEMANGLED_P is set to the char * that should be @@ -1586,7 +1618,7 @@ report_failed_demangle (const char *name, bool core_dump_allowed, /* A wrapper for bfd_demangle. */ gdb::unique_xmalloc_ptr<char> -gdb_demangle (const char *name, int options) +gdb_demangle (const char *name, int options, bool force_v3) { gdb::unique_xmalloc_ptr<char> result; int crash_signal = 0; @@ -1617,7 +1649,12 @@ gdb_demangle (const char *name, int options) #endif if (crash_signal == 0) - result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); + { + if (force_v3) + result.reset (cplus_demangle_v3 (name, options | DMGL_VERBOSE)); + else + result.reset (bfd_demangle (NULL, name, options | DMGL_VERBOSE)); + } #ifdef HAVE_WORKING_FORK if (catch_demangler_crashes) diff --git a/gdb/cp-support.h b/gdb/cp-support.h index b3040506c8a..7bc1f5566a2 100644 --- a/gdb/cp-support.h +++ b/gdb/cp-support.h @@ -82,6 +82,12 @@ struct demangle_parse_info extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string (const char *string); +/* Return the demangled spelling of LINKAGE_NAME when it names a scoped + template instantiation, or NULL otherwise. */ + +extern gdb::unique_xmalloc_ptr<char> cp_demangle_template_linkage_name + (const char *linkage_name); + extern gdb::unique_xmalloc_ptr<char> cp_canonicalize_string_no_typedefs (const char *string); @@ -194,9 +200,11 @@ extern struct cmd_list_element *maint_cplus_cmd_list; libiberty's demangler to expand standard substitutions to their full template name. */ -/* A wrapper for bfd_demangle. */ +/* A wrapper for bfd_demangle. When FORCE_V3 is true, use the v3 + demangler directly, ignoring the user's demangle-style setting. */ -gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options); +gdb::unique_xmalloc_ptr<char> gdb_demangle (const char *name, int options, + bool force_v3 = false); /* A wrapper for cplus_demangle_print. */ diff --git a/gdb/dwarf2/cooked-index-entry.c b/gdb/dwarf2/cooked-index-entry.c index 78a74273616..0a688ed453c 100644 --- a/gdb/dwarf2/cooked-index-entry.c +++ b/gdb/dwarf2/cooked-index-entry.c @@ -175,7 +175,8 @@ cooked_index_entry::full_name (struct obstack *storage, cooked_index_full_name_flag name_flags, const char *default_sep) const { - const char *local_name = ((name_flags & FOR_MAIN) != 0) ? name () : canonical; + const char *local_name + = ((name_flags & (FOR_MAIN | FOR_DWARF_NAME)) != 0) ? name () : canonical; if ((flags & IS_LINKAGE) != 0 || get_parent () == nullptr) return local_name; @@ -238,11 +239,12 @@ cooked_index_entry::write_scope (struct obstack *storage, cooked_index_shard::prune_nameless_entries. */ gdb_assert (!name_is_deferred ()); - /* When computing the Ada linkage name, the entry might not have - been canonicalized yet, so use the 'name'. */ - const char *local_name = ((flags & (FOR_MAIN | FOR_ADA_LINKAGE_NAME)) != 0 - ? name () - : canonical); + /* When computing the Ada linkage name or the DWARF spelling, the + entry might not have been canonicalized yet, so use the 'name'. */ + const char *local_name + = ((flags & (FOR_MAIN | FOR_ADA_LINKAGE_NAME | FOR_DWARF_NAME)) != 0 + ? name () + : canonical); obstack_grow (storage, local_name, strlen (local_name)); obstack_grow (storage, sep, strlen (sep)); } diff --git a/gdb/dwarf2/cooked-index-entry.h b/gdb/dwarf2/cooked-index-entry.h index 462ec0640a9..d9a8f001e55 100644 --- a/gdb/dwarf2/cooked-index-entry.h +++ b/gdb/dwarf2/cooked-index-entry.h @@ -60,6 +60,10 @@ enum cooked_index_full_name_enum : unsigned char FOR_MAIN = 1, /* Set when requesting the linkage name for an Ada entry. */ FOR_ADA_LINKAGE_NAME = 2, + /* Set when requesting the name as spelled in the DWARF, rather than + the canonical name. This does not read the canonical field, so it + is safe to use before all shards have been canonicalized. */ + FOR_DWARF_NAME = 4, }; DEF_ENUM_FLAGS_TYPE (enum cooked_index_full_name_enum, cooked_index_full_name_flag); @@ -168,6 +172,11 @@ struct cooked_index_entry : public allocate_on_obstack<cooked_index_entry> symbols will have their "linkage-style" name computed. The default is source-style. + If the FOR_DWARF_NAME flag is set, the name is built from the + entries' names as spelled in the DWARF; the canonical field is not + consulted. This is safe to use while shards are still being + canonicalized concurrently. + If the language doesn't prescribe a separator, one can be specified using DEFAULT_SEP. */ const char *full_name (struct obstack *storage, diff --git a/gdb/dwarf2/cooked-index-shard.c b/gdb/dwarf2/cooked-index-shard.c index 206fe4483a4..b966b88c7f3 100644 --- a/gdb/dwarf2/cooked-index-shard.c +++ b/gdb/dwarf2/cooked-index-shard.c @@ -23,8 +23,10 @@ #include "dwarf2/read.h" #include "dwarf2/error.h" #include "cp-support.h" +#include "split-name.h" #include "c-lang.h" #include "ada-lang.h" +#include <unordered_set> /* Return true if a plain "main" could be the main program for this language. Languages that are known to use some other mechanism are @@ -304,6 +306,7 @@ cooked_index_shard::canonicalize_names () htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, nullptr, xcalloc, xfree)); std::vector<cooked_index_entry *> new_gnat_entries; + std::vector<cooked_index_entry *> new_cxx_entries; for (cooked_index_entry *entry : m_entries) { @@ -393,11 +396,133 @@ cooked_index_shard::canonicalize_names () entry->canonical = entry->name (); } + /* For a C++ static data member or member function, the DWARF name of + the enclosing template can use a non-canonical spelling of the + template arguments, while lookups use the canonical spelling from + the demangled linkage name. For example, GCC emits S<1>::v while + the demangled linkage name is S<1l>::v. The cooked index therefore + needs an entry for the canonical spelling as well. + + The alias is derived from the linkage name of this particular + symbol, so S<1> (int) and S<1L> (long) stay distinct: each symbol + gets the spelling encoded by its own linkage name. Skip the alias + when the entry built from DW_AT_name already uses the canonical + spelling (for instance a clang-built binary whose DWARF name + already has the integer suffix). PR symtab/33533. */ + + /* Names of the enclosing scopes whose canonical spelling has already + been added, to avoid duplicates within this shard. */ + std::unordered_set<std::string> enclosing_names; + + for (size_t i = 0; i < m_entries.size (); ++i) + { + cooked_index_entry *entry = m_entries[i]; + + if ((entry->flags & IS_LINKAGE) == 0 + || entry->lang != language_cplus + || (entry->tag != DW_TAG_variable + && entry->tag != DW_TAG_member + && entry->tag != DW_TAG_subprogram)) + continue; + + gdb::unique_xmalloc_ptr<char> demangled + = cp_demangle_template_linkage_name (entry->name ()); + if (demangled == nullptr) + continue; + + /* The index entry built from DW_AT_name is added directly before + the linkage-name entry for the same DIE. */ + const cooked_index_entry *dwarf_entry = nullptr; + if (i > 0) + { + const cooked_index_entry *prev = m_entries[i - 1]; + if (prev->die_offset == entry->die_offset + && prev->per_cu == entry->per_cu + && (prev->flags & (IS_LINKAGE | IS_SYNTHESIZED)) == 0) + dwarf_entry = prev; + } + + /* If the DW_AT_name entry is not adjacent, the index was not + built by the DIE scanner (for example, it came from + .debug_names), so the checks below cannot be applied. Skip + the alias rather than adding one without them. */ + if (dwarf_entry == nullptr) + continue; + + /* Use a temporary obstack for this probe. Use the DWARF + spelling: this shard may run before the shard owning an + ancestor has canonicalized its entries. */ + auto_obstack temp; + const char *full = dwarf_entry->full_name (&temp, FOR_DWARF_NAME); + + /* If the full name already has the canonical spelling, there is + nothing to add; this is the case for clang, which includes the + integer suffix in DW_AT_name. */ + if (strcmp (demangled.get (), full) == 0) + continue; + + /* Only add an alias when the parent is a type or namespace. + This excludes function-local statics, whose parent is a + subprogram. */ + const cooked_index_entry *parent = dwarf_entry->get_parent (); + if (parent != nullptr && !tag_is_type (parent->tag)) + continue; + + /* Clear IS_LINKAGE so that the alias is written to a saved + .gdb_index; C++ linkage names are otherwise omitted from the + cache. The alias has no parent, so full_name() still returns + its canonical name. Do not mark it IS_SYNTHESIZED: unlike the + Ada package entries, this alias names a real DIE and should be + written to .debug_names as well. */ + const char *canon = m_names.insert (std::move (demangled)); + cooked_index_entry *alias + = create (entry->die_offset, entry->tag, + entry->flags & ~IS_LINKAGE, + entry->lang, canon, nullptr, entry->per_cu); + alias->canonical = canon; + new_cxx_entries.push_back (alias); + + /* Also add the canonical spelling of each enclosing template + scope. A .gdb_index reader splits a symbol name into its + components and re-links them, but it can only do that if each + prefix has an entry too. These entries are deliberately + parentless and store the whole prefix: the reader re-splits the + name itself, and they are not intended for direct scope lookup. + A lookup for a scope-only spelling can still match one of them + and expand its CU before missing; that is the cost of making + saved indexes round-trip. */ + std::vector<std::string_view> components + = split_name (canon, split_style::CXX); + std::string prefix; + for (size_t j = 0; j + 1 < components.size (); ++j) + { + if (!prefix.empty ()) + prefix += "::"; + prefix.append (components[j].data (), components[j].size ()); + + if (prefix.find ('<') == std::string::npos) + continue; + if (!enclosing_names.insert (prefix).second) + continue; + + const char *enclosing_name + = m_names.insert (std::string_view (prefix)); + cooked_index_entry *enclosing + = create (entry->die_offset, DW_TAG_structure_type, + IS_SYNTHESIZED, entry->lang, enclosing_name, nullptr, + entry->per_cu); + enclosing->canonical = enclosing_name; + new_cxx_entries.push_back (enclosing); + } + } + /* Make sure any new Ada entries end up in the results. This isn't done when creating these new entries to avoid invalidating the m_entries iterator used in the foreach above. */ m_entries.insert (m_entries.end (), new_gnat_entries.begin (), new_gnat_entries.end ()); + m_entries.insert (m_entries.end (), new_cxx_entries.begin (), + new_cxx_entries.end ()); m_entries.shrink_to_fit (); std::sort (m_entries.begin (), m_entries.end (), diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc new file mode 100644 index 00000000000..1b1b8679c82 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.cc @@ -0,0 +1,69 @@ +/* Copyright 2026 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 <http://www.gnu.org/licenses/>. */ + +/* A static constexpr member of a class template with a non-type template + parameter. The reference from the member typedef forces GCC to emit the + declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5. */ + +template<long N> +struct S +{ + static constexpr unsigned v = N; + using P = int[v]; +}; + +S<1> s; + +/* The same template name instantiated with both int and long non-type + arguments. A canonical-name alias must not conflate the two. */ +template<auto N> +struct T +{ + static constexpr auto v = N; +}; + +T<1> t_int; +T<1L> t_long; + +/* A member of a template in a namespace: the canonical spelling has + multiple scope components. */ +namespace N +{ + template<long M> + struct U + { + static constexpr unsigned v = M; + using P = int[v]; + }; +} + +N::U<1> u; + +/* A class template with only a static member function. */ +template<long N> +struct F +{ + static long f () { return N; } + using P = int[N]; +}; + +F<1> f; +long fv = F<1>::f (); + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp new file mode 100644 index 00000000000..55ef14aefe6 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.exp @@ -0,0 +1,129 @@ +# Copyright 2026 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 <http://www.gnu.org/licenses/>. + +# Test printing a static constexpr member of a class template with a +# non-type template parameter. +# +# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression +# evaluator looks the member up using the canonical spelling S<1l>. The +# cooked index stores the DWARF spelling, so before the fix for PR +# symtab/33533 the lookup missed and gdb printed <optimized out>. + +standard_testfile .cc + +# Can't save an index with readnow. +require !readnow + +set opts {debug c++ dwarf5} +lappend opts additional_flags=-std=c++17 + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} { + return +} + +# Save a .gdb_index now, before the inferior is started, so that only +# the main objfile is indexed. The aliases must survive this round +# trip; the reload is checked below. +set filename [file tail $binfile] +set host_program [gdb_remote_download host $binfile] +set index_file [host_standard_output_file $filename.gdb-index] +remote_file host delete $index_file +gdb_test_no_output "save gdb-index [file dirname $index_file]" \ + "save gdb-index for $filename" + +if {![remote_file host exists $index_file]} { + unsupported "gdb-index file not created" + return +} +pass "gdb-index file created" + +# Also check that the alias is written to a DWARF-5 .debug_names index. +# Names that are not already in .debug_str are emitted in the .debug_str +# addition file, so check that file. +set debug_str_file [host_standard_output_file $filename.debug_str] +remote_file host delete $debug_str_file +gdb_test_no_output "save gdb-index -dwarf-5 [file dirname $index_file]" \ + "save .debug_names index for $filename" + +if {[remote_file host exists $debug_str_file]} { + pass ".debug_str addition created" + set fp [open $debug_str_file rb] + set contents [read $fp] + close $fp + gdb_assert {[string first "S<1l>::v" $contents] != -1} \ + "canonical alias present in .debug_names" + gdb_assert {[string first "F<1l>::f" $contents] != -1} \ + "canonical function alias present in .debug_names" +} else { + unsupported ".debug_str addition not created" +} + +if {![runto_main]} { + return +} + +gdb_test "print s.v" "= 1" \ + "print static constexpr member of a template" + +gdb_test "print 'S<1l>::v'" "= 1" \ + "print static constexpr member with canonical template arguments" + +# Both T<1> and T<1L> are instantiated. Their DWARF names are identical +# (T<1>), but the canonical spellings differ. Check that the long +# instantiation is not conflated with the int one. +gdb_test "print t_int.v" "= 1" +gdb_test "print t_long.v" "= 1" +gdb_test "whatis 'T<1>::v'" "type = (const )?int" +gdb_test "whatis 'T<1l>::v'" "type = (const )?long( int)?" + +gdb_test "print 'N::U<1l>::v'" "= 1" \ + "print member of a template in a namespace" +gdb_test "whatis 'N::U<1l>::v'" "type = (const )?unsigned int" + +# A static member function of a class template has the same spelling +# mismatch. +gdb_test "print 'F<1l>::f'()" "= 1" \ + "call member function with canonical template arguments" +gdb_test "whatis 'F<1l>::f'" "type = long \\(void\\)" \ + "member function type with canonical template arguments" + +# The synthesized aliases must also survive saving and reloading a +# .gdb_index. Embed the saved index in a copy of the program; the +# index reader rebuilds qualified names from their components, so this +# also checks that the enclosing scope aliases were emitted. +set host_program_with_index $host_program.with-index +set program_with_index $binfile.with-index +remote_file host delete $host_program_with_index +if {[run_on_host "objcopy" [gdb_find_objcopy] \ + [join [list \ + "--remove-section .gdb_index" \ + "--add-section .gdb_index=$index_file" \ + "--set-section-flags .gdb_index=readonly" \ + "$host_program" "$host_program_with_index"]]]} { + return +} + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir +gdb_test "file $program_with_index" "Reading symbols from .*" \ + "load program with embedded .gdb_index" +gdb_test "print 'S<1l>::v'" "= 1" \ + "canonical member after .gdb_index reload" +gdb_test "print 'T<1l>::v'" "= 1" \ + "canonical auto member after .gdb_index reload" +gdb_test "print 'N::U<1l>::v'" "= 1" \ + "canonical namespaced member after .gdb_index reload" +gdb_test "whatis 'F<1l>::f'" "type = long \\(void\\)" \ + "canonical member function after .gdb_index reload" -- 2.34.1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2] [gdb/symtab] Add canonical-name aliases for C++ template static members 2026-11-20 9:18 [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses Sendao Yan 2026-11-20 18:26 ` Andrew Burgess @ 2026-11-21 14:59 ` Sendao Yan 2026-11-21 15:07 ` Sendao Yan 1 sibling, 1 reply; 12+ messages in thread From: Sendao Yan @ 2026-11-21 14:59 UTC (permalink / raw) To: gdb-patches; +Cc: tom, aburgess, keiths, yansendao The cooked index stores names as they appear in the DWARF. For a static data member of a class template, GCC's DW_AT_name uses the DWARF spelling of the template arguments (e.g. S<1>::v), while the C++ expression evaluator looks the member up using the canonical spelling from the demangled linkage name (S<1l>::v). Since commit 86ac8c546235 lookup_symbol_in_objfile only searches the cooked index, the lookup misses and GDB reports the member as optimized out. This makes libstdc++'s chrono pretty-printer fail (PR symtab/33533, GCC PR 128817). Fix this in the index: for a C++ static data member (DW_TAG_variable or DW_TAG_member with a linkage name) whose canonical name contains template arguments, add a synthesized cooked-index entry using the demangled linkage name. The normal quick lookup then finds the symbol, so no symtab-expansion fallback is needed. Add gdb.cp/static-const-template.exp, which prints a static constexpr member of a class template with a long non-type template parameter. With the unpatched gdb it fails with <optimized out>; with the fix it passes. Verified with the GCC testcase libstdc++-prettyprinters/chrono.cc (print hms/hms_nano, also with -flto), the new test, and the existing gdb.dwarf2/static-const-member.exp, gdb.cp/m-static.exp, gdb.cp/namespace.exp and gdb.base/condbreak-multi-context.exp. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33533 --- gdb/dwarf2/cooked-index-shard.c | 38 +++++++++++++++++- gdb/testsuite/gdb.cp/static-const-template.cc | 33 +++++++++++++++ .../gdb.cp/static-const-template.exp | 40 +++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.cp/static-const-template.cc create mode 100644 gdb/testsuite/gdb.cp/static-const-template.exp diff --git a/gdb/dwarf2/cooked-index-shard.c b/gdb/dwarf2/cooked-index-shard.c index 206fe4483a4..ad4a0cb6c87 100644 --- a/gdb/dwarf2/cooked-index-shard.c +++ b/gdb/dwarf2/cooked-index-shard.c @@ -23,6 +23,7 @@ #include "dwarf2/read.h" #include "dwarf2/error.h" #include "cp-support.h" +#include "demangle.h" #include "c-lang.h" #include "ada-lang.h" @@ -304,6 +305,7 @@ cooked_index_shard::canonicalize_names () htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, nullptr, xcalloc, xfree)); std::vector<cooked_index_entry *> new_gnat_entries; + std::vector<cooked_index_entry *> new_cxx_entries; for (cooked_index_entry *entry : m_entries) { @@ -319,7 +321,39 @@ cooked_index_shard::canonicalize_names () here, then a check might be required there. */ gdb_assert (entry->canonical == nullptr); if ((entry->flags & IS_LINKAGE) != 0) - entry->canonical = entry->name (); + { + entry->canonical = entry->name (); + + /* For a C++ static data member, the DWARF name of the + enclosing template can use a non-canonical spelling of the + template arguments, while lookups use the canonical + spelling from the demangled linkage name. For example, + GCC emits S<1>::v while the demangled linkage name is + S<1l>::v. Add an alias entry using the canonical + demangled name so that the cooked-index lookup succeeds. + PR symtab/33533. */ + if (entry->lang == language_cplus + && (entry->tag == DW_TAG_variable + || entry->tag == DW_TAG_member)) + { + gdb::unique_xmalloc_ptr<char> demangled + = gdb_demangle (entry->name (), + DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES); + if (demangled != nullptr + && strchr (demangled.get (), ':') != nullptr + && strchr (demangled.get (), '<') != nullptr + && strcmp (demangled.get (), entry->name ()) != 0) + { + const char *canon = m_names.insert (std::move (demangled)); + cooked_index_entry *alias + = create (entry->die_offset, entry->tag, + entry->flags | IS_SYNTHESIZED, entry->lang, + canon, nullptr, entry->per_cu); + alias->canonical = canon; + new_cxx_entries.push_back (alias); + } + } + } else if (entry->lang == language_ada) { /* Newer versions of GNAT emit DW_TAG_module and use a @@ -398,6 +432,8 @@ cooked_index_shard::canonicalize_names () m_entries iterator used in the foreach above. */ m_entries.insert (m_entries.end (), new_gnat_entries.begin (), new_gnat_entries.end ()); + m_entries.insert (m_entries.end (), new_cxx_entries.begin (), + new_cxx_entries.end ()); m_entries.shrink_to_fit (); std::sort (m_entries.begin (), m_entries.end (), diff --git a/gdb/testsuite/gdb.cp/static-const-template.cc b/gdb/testsuite/gdb.cp/static-const-template.cc new file mode 100644 index 00000000000..036e6b3b593 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.cc @@ -0,0 +1,33 @@ +/* Copyright 2026 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 <http://www.gnu.org/licenses/>. */ + +/* A static constexpr member of a class template with a non-type template + parameter. The reference from the member typedef forces GCC to emit the + declaration DIE (DW_TAG_variable with DW_AT_const_value) in DWARF5. */ + +template<long N> +struct S +{ + static constexpr unsigned v = N; + using P = int[v]; +}; + +S<1> s; + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.cp/static-const-template.exp b/gdb/testsuite/gdb.cp/static-const-template.exp new file mode 100644 index 00000000000..b68d646cd09 --- /dev/null +++ b/gdb/testsuite/gdb.cp/static-const-template.exp @@ -0,0 +1,40 @@ +# Copyright 2026 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 <http://www.gnu.org/licenses/>. + +# Test printing a static constexpr member of a class template with a +# non-type template parameter. +# +# In DWARF5, GCC spells the enclosing type S<1> while the C++ expression +# evaluator looks the member up using the canonical spelling S<1l>. The +# cooked index stores the DWARF spelling, so before the fix for PR +# symtab/33533 the lookup missed and gdb printed <optimized out>. + +standard_testfile .cc + +set opts {debug c++ dwarf5} +lappend opts additional_flags=-std=c++11 + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile $opts]} { + return +} + +if {![runto_main]} { + return +} + +gdb_test "print s.v" "= 1" \ + "print static constexpr member of a template" + +gdb_test "print 'S<1l>::v'" "= 1" \ + "print static constexpr member with canonical template arguments" -- 2.34.1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2] [gdb/symtab] Add canonical-name aliases for C++ template static members 2026-11-21 14:59 ` [PATCH v2] " Sendao Yan @ 2026-11-21 15:07 ` Sendao Yan 0 siblings, 0 replies; 12+ messages in thread From: Sendao Yan @ 2026-11-21 15:07 UTC (permalink / raw) To: gdb-patches; +Cc: tom, aburgess, keiths, simark, yansendao Hi Simon, Andrew, Tom, Keith, Sorry for the delayed reply -- v2 was sent before I saw Simon's message, and the cover text did not make it into the v2 email (git send-email sent the patch file as the body). Andrew asked why this cannot be fixed in the quick lookup path, and whether a fallback bakes in a known limitation. v2 answers that: the quick lookup path can be fixed, and v2 does fix it there. v1's symtab fallback was not needed. The root cause is that the cooked index only contained the DWARF spelling (S<1>::v), while the lookup uses the canonical spelling from the demangled linkage name (S<1l>::v). The lookup algorithm itself is fine; it misses because the index does not contain the spelling being looked up. v2 adds that spelling as a synthesized cooked-index entry, so there is no reliance on expanded symtabs and no known limitation to document. Simon: you are right that S<1> and S<1L> are different types, and that a plain alias would be wrong if both types exist. v2 only adds the alias for the DWARF spelling that lacks the integer suffix; I agree it needs to be disambiguated. Some options: 1. Only add the alias when no exact canonical entry for S<1L> exists in the index. A real S<1L> entry then always wins; the alias is only a fallback for compilers that omit the suffix. This is a small change, but not a general fuzzy match. 2. Reconstruct the type name from the DW_TAG_template_value_parameter DIEs using the parameter type's spelling, as you suggested. The existing dwarf2_compute_name path does something similar for functions, but it prints the value without the integer suffix; we would need to print it using the C++ type spelling (S<1L>) and use that name for the class type. 3. Implement fuzzy matching with ranking in the index lookup. I think (2) is the most robust. Before I rework the patch: would you prefer (2), or is a guarded (1) acceptable as a first step? I can test both with GCC- and clang-built binaries (including a program that instantiates both S<1> and S<1L>). For the other comments: - Tom: v2 no longer touches lookup_symbol_in_objfile; the symtab fallback is gone. - Andrew: `return -1` removed; no new NULL; `print s.v` is the actual regression test, and the alias lives in the cooked index, so it does not depend on symtab expansion. - Keith: the canonical spelling comes from the demangled linkage name; GCC's DW_AT_name omits the integer suffix. Thanks, Sendao ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-11-23 2:10 UTC | newest] Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-20 9:18 [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses Sendao Yan 2026-11-20 18:26 ` Andrew Burgess 2026-11-20 19:46 ` Keith Seitz 2026-11-21 0:50 ` Tom Tromey 2026-11-21 3:53 ` Simon Marchi 2026-11-21 16:31 ` Tom Tromey 2026-11-22 12:03 ` [PATCH v3 0/1] [gdb/symtab] Add canonical-name aliases for C++ template static members Sendao Yan 2026-11-22 12:03 ` [PATCH v3 1/1] " Sendao Yan 2026-11-23 2:09 ` [PATCH v4 0/1] " Sendao Yan 2026-11-23 2:09 ` [PATCH v4 1/1] " Sendao Yan 2026-11-21 14:59 ` [PATCH v2] " Sendao Yan 2026-11-21 15:07 ` Sendao Yan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox