[PATCH] gdb: track parents of nameless DIEs in cooked indexer

Andrew Burgess <[email protected]>
Newsgroups gmane.comp.gdb.patches
Message-ID <4c16f81081452e05f47741be7a2d15a250827c52.1787152777.git.aburgess@redhat.com>
From: Tom de Vries <[email protected]>

This patch is being posted by Andrew, but I have left the author
attribution as Tom de Vries as the changes I've made here are not
significant.

The version of this patch I picked up can be found here:

  https://inbox.sourceware.org/gdb-patches/[email protected]

I found this patch while looking at PR gdb/33447, Tom Tromey had
suggest this patch might fix that bug, so I took the patch from the
mailing list, rebased it to current HEAD, checked the test still
passed, and then checked to see if it fixed PR gdb/33447.

Turns out this doesn't fix that bug, but by that point I'd already
done most of the work to update this patch, so it seemed a shame to
just discard it.

Things I have changed:

  1. The code adds additional DIEs to the parent map, but the bug is
     specifically about nameless DIEs not being tracked.  I tweaked
     the code so that only nameless DIEs are being added to the map
     (by this patch).  The test still passes, so I think this confirms
     my understanding of what's going on here.  This helps keep the
     map smaller, which is a very minor memory saving, and possibly
     the lookups are faster too.

  2. I renamed the test.  I'm not a fan of names like 'dwz-2.exp',
     what's next?  dwz-3.exp, dwz-4.exp, ... I tried to find a name
     which hinted at what was being tested.

  3. I added a comment on the new code to try an explain what's going
     on.

  4. I reworded some parts of the commit message to make things
     clearer.

Any errors introduced by my changes are all my own fault.

Thanks,
Andrew

---

This patch ensures the cooked index correctly resolves parents for
DIEs whose DW_AT_specification chains pass through unnamed
intermediate DIEs in a partial unit.

The test-case is based on the DWARF info generated for test-case
gdb.cp/breakpoint-locs.exp with target board cc-with-dwz.

The difference is that the test-case does not contain
DW_TAG_inlined_subroutine, which makes it possible to submit this patch
independently from the patch series:

  https://sourceware.org/pipermail/gdb-patches/2023-December/205056.html

A consequence of this difference is that the relevant DIEs in the
Partial Unit (PU) are not named, and consequently have no entries in
the cooked index, which meant that the original fix didn't work, so
the fix has been updated to handle this case as well.

Consider the new DWARF assembly test-case
gdb.dwarf2/dwz-parent-spec-chain.exp.  With readnow, we have:

  $ gdb -q -batch -readnow outputs/gdb.dwarf2/dwz-parent-spec-chain/dwz-parent-spec-chain \
          -ex "print ns::foo" \
          -ex "print ns::bar"
  $1 = {int (void)} 0x4101ac <ns::foo()>
  $2 = {int (void)} 0x4101b4 <ns::bar()>

but with the cooked index we have either:

  $1 = {int (void)} 0x4101ac <ns::foo()>
  No symbol "bar" in namespace "ns".

or:

  No symbol "foo" in namespace "ns".
  $1 = {int (void)} 0x4101b4 <ns::bar()>

The problem is that both the entries for 'foo' and 'bar' don't have the correct
parent:

  $ gdb -q -batch outputs/gdb.dwarf2/dwz-parent-spec-chain/dwz-parent-spec-chain \
          -ex "maint print objfiles" \
          | egrep "qualified:.*(foo|bar)$"
      qualified:  foo
      qualified:  bar

so gdb ends up expanding the first CU that contains namespace ns,
which is the CU that won the import race for the PU, which is why
we're getting either ns::foo or ns::bar.

So why are the 'foo' and 'bar' entries not getting the correct parent?

The DWARF representation of ns::foo and ns::bar is as follows; we have the foo
DIE (in CU1):

  <1><42>: Abbrev Number: 3 (DW_TAG_subprogram)
     <43>   DW_AT_specification: <0x2a>
     <47>   DW_AT_name        : foo
     <4b>   DW_AT_low_pc      : 0x4101ac
     <53>   DW_AT_high_pc     : 0x4101b4
     <5b>   DW_AT_linkage_name: _ZN2ns3fooEv

and the bar DIE (in CU2):

  <1><7b>: Abbrev Number: 3 (DW_TAG_subprogram)
     <7c>   DW_AT_specification: <0x2a>
     <80>   DW_AT_name        : bar
     <84>   DW_AT_low_pc      : 0x4101b4
     <8c>   DW_AT_high_pc     : 0x4101bc
     <94>   DW_AT_linkage_name: _ZN2ns3barEv

both referring to this DIE (in the PU):

  <1><2a>: Abbrev Number: 5 (DW_TAG_subprogram)
     <2b>   DW_AT_specification: <0x23>

which refers to this DIE (also in the PU):

  <1><1f>: Abbrev Number: 3 (DW_TAG_namespace)
     <20>   DW_AT_name        : ns
  <2><23>: Abbrev Number: 4 (DW_TAG_subprogram)
     <24>   DW_AT_type        : <0x18>
     <28>   DW_AT_external    : 1

When processing both the 'foo' and 'bar' DIEs, finding the parent is deferred, but
when the deferred parents are resolved there is no entry at 0x2a in the parent
map:

  map start:
    0x0000000000000000 0x0
    0x0000000000000020 0x31f497c0 (0x1f: ns)
    0x000000000000002a 0x0

Fix this by adding an entry at 0x2a in the parent map, such that we have
instead:

  map start:
    0x0000000000000000 0x0
    0x0000000000000020 0x321117c0 (0x1f: ns)
    0x000000000000002b 0x0
    0x0000000000000042 0x321117c0 (0x1f: ns)
    0x0000000000000043 0x0
    0x000000000000007b 0x321117c0 (0x1f: ns)
    0x000000000000007c 0x0

and:

  $ gdb -q -batch outputs/gdb.dwarf2/dwz-parent-spec-chain/dwz-parent-spec-chain \
            -ex "maint print objfiles" \
            | egrep "qualified:.*(foo|bar)$"
      qualified:  ns::foo
      qualified:  ns::bar

and:

  $ gdb -q -batch outputs/gdb.dwarf2/dwz-parent-spec-chain/dwz-parent-spec-chain \
          -ex "print ns::foo" \
          -ex "print ns::bar"
  $1 = {int (void)} 0x4101ac <ns::foo()>
  $2 = {int (void)} 0x4101b4 <ns::bar()>

Tested on aarch64-linux and x86-64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32299
---
 gdb/dwarf2/cooked-indexer.c                   |  11 ++
 .../gdb.dwarf2/dwz-parent-spec-chain.c        |  37 ++++++
 .../gdb.dwarf2/dwz-parent-spec-chain.exp      | 125 ++++++++++++++++++
 3 files changed, 173 insertions(+)
 create mode 100644 gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.c
 create mode 100644 gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.exp

diff --git a/gdb/dwarf2/cooked-indexer.c b/gdb/dwarf2/cooked-indexer.c
index f55efc5e3a0..581c0eb0763 100644
--- a/gdb/dwarf2/cooked-indexer.c
+++ b/gdb/dwarf2/cooked-indexer.c
@@ -625,6 +625,17 @@ cooked_indexer::index_dies (cutu_reader *reader,
 				      m_language, name,
 				      this_parent_entry, cu_for_entry);
 	}
+      else if (this_parent_entry != nullptr)
+	{
+	  /* Record this DIE's parent in the map so that deferred
+	     parent lookups through DW_AT_specification chains can
+	     find the parent even for DIEs that have no name and thus
+	     no cooked_index_entry.  */
+	  parent_map::addr_type addr
+	    = parent_map::form_addr (reader->buffer ()
+				     + to_underlying (this_die));
+	  m_die_range_map->add_entry (addr, addr, this_parent_entry);
+	}
 
       if (linkage_name != nullptr)
 	{
diff --git a/gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.c b/gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.c
new file mode 100644
index 00000000000..ffaf1e2b660
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.c
@@ -0,0 +1,37 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2024-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/>.  */
+
+int
+foo (void)
+{
+  asm ("foo_label: .globl foo_label");
+  return 1;
+}
+
+int
+bar (void)
+{
+  asm ("bar_label: .globl bar_label");
+  return 2;
+}
+
+int
+main (void)
+{
+  asm ("main_label: .globl main_label");
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.exp b/gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.exp
new file mode 100644
index 00000000000..6de09fb0b07
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dwz-parent-spec-chain.exp
@@ -0,0 +1,125 @@
+# Copyright 2024-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/>.
+
+# Check that the cooked index gets the correct parents for some
+# entries with the parents in a partial unit.
+#
+# The structure is similar to what's generated for test-case
+# gdb.cp/breakpoint-locs.exp with gcc and target board cc-with-dwz,
+# only in that case we have:
+#
+#   DW_TAG_inlined_subroutine -ao-> decl DW_TAG_subprogram -s->
+#     (DW_TAG_namespace::DW_TAG_class_type::decl DW_TAG_subprogram)
+#
+# and here we use:
+#
+#   DW_TAG_subprogram -s-> decl DW_TAG_subprogram -s->
+#     (DW_TAG_namespace::decl DW_TAG_subprogram)
+#
+# In the above '-s->' is a DW_AT_specification link and '-ao->' is a
+# DW_AT_abstract_origin link.
+
+load_lib dwarf.exp
+
+require dwarf2_support
+
+standard_testfile .c dwz.S
+
+# Create the DWARF.
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    get_func_info foo
+    get_func_info bar
+    get_func_info main
+
+    declare_labels partial_label int_label decl1 decl2
+
+    cu {} {
+	partial_label: DW_TAG_partial_unit {} {
+	    int_label: DW_TAG_base_type {
+		DW_AT_name int
+		DW_AT_byte_size 4 sdata
+		DW_AT_encoding @DW_ATE_signed
+	    }
+
+	    DW_TAG_namespace {
+		DW_AT_name ns
+	    } {
+		decl1: DW_TAG_subprogram {
+		    DW_AT_type :$int_label
+		    DW_AT_external 1 flag
+		}
+	    }
+
+	    decl2: DW_TAG_subprogram {
+		DW_AT_specification %$decl1
+	    }
+	}
+    }
+
+    cu {} {
+	DW_TAG_compile_unit {
+	    DW_AT_language @DW_LANG_C_plus_plus
+	} {
+	    DW_TAG_imported_unit {
+		DW_AT_import $partial_label ref_addr
+	    }
+
+	    DW_TAG_subprogram {
+		DW_AT_specification %$decl2
+		DW_AT_name foo
+		DW_AT_low_pc $foo_start DW_FORM_addr
+		DW_AT_high_pc $foo_end DW_FORM_addr
+		DW_AT_linkage_name _ZN2ns3fooEv
+	    }
+	}
+    }
+
+    cu {} {
+	DW_TAG_compile_unit {
+	    DW_AT_language @DW_LANG_C_plus_plus
+	} {
+	    DW_TAG_imported_unit {
+		DW_AT_import $partial_label ref_addr
+	    }
+
+	    DW_TAG_subprogram {
+		DW_AT_specification %$decl2
+		DW_AT_name bar
+		DW_AT_low_pc $bar_start DW_FORM_addr
+		DW_AT_high_pc $bar_end DW_FORM_addr
+		DW_AT_linkage_name _ZN2ns3barEv
+	    }
+
+	    DW_TAG_subprogram {
+		DW_AT_name main
+		DW_AT_low_pc $main_start DW_FORM_addr
+		DW_AT_high_pc $main_end DW_FORM_addr
+	    }
+	}
+    }
+}
+
+if { [prepare_for_testing "failed to prepare" $testfile \
+	  [list $asm_file $srcfile] {nodebug}] } {
+    return
+}
+
+# Regression test for PR32299.  These should both pass, but before the fix
+# only one of them passed.
+gdb_test "p ns::foo" \
+    [string_to_regexp " <ns::foo()>"]
+gdb_test "p ns::bar" \
+    [string_to_regexp " <ns::bar()>"]

base-commit: afa6db16e6508d8ea269557085bc7c301e824382
-- 
2.25.4
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.