Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Sendao Yan <yansendao@126.com>, gdb-patches@sourceware.org
Cc: tom@tromey.com, yansendao@126.com
Subject: Re: [PATCH] [gdb/symtab] Fall back to symtabs when the quick lookup misses
Date: Fri, 20 Nov 2026 18:26:01 +0000	[thread overview]
Message-ID: <xhuyfse0v2wcm.fsf@aburgess-thinkpadp1gen8.rmtgb.csb> (raw)
In-Reply-To: <20261120091803.89033-1-yansendao@126.com>

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


  reply	other threads:[~2026-11-20 18:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  9:18 Sendao Yan
2026-11-20 18:26 ` Andrew Burgess [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xhuyfse0v2wcm.fsf@aburgess-thinkpadp1gen8.rmtgb.csb \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    --cc=yansendao@126.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox