Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Use fully-qualified name in var_decl_name
@ 2026-04-23 19:01 Tom Tromey
  2026-04-24 13:33 ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2026-04-23 19:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

While testing gdb against a version of gnat-llvm that emits
unqualified names (in a hierarchical structure), I found that
gdb.ada/array_of_symbolic_length.exp would fail.

The DWARF in question looks like this:

 <1><11a1>: Abbrev Number: 2 (DW_TAG_module)
    <11a2>   DW_AT_name        : (indirect string, offset: 0xe65): pck
 <2><11a6>: Abbrev Number: 3 (DW_TAG_variable)
    <11a7>   DW_AT_name        : (indirect string, offset: 0xe69): my_length_LAST
[...]
 <3><11df>: Abbrev Number: 5 (DW_TAG_subrange_type)
    <11e0>   DW_AT_type        : <0x12ab>
    <11e4>   DW_AT_upper_bound : <0x11a6>

That is, an array's upper bound refers to my_length_LAST.  gdb
represents this as a PROP_VARIABLE_NAME -- but only the local name of
the variable was used in this case.

This patch changes var_decl_name to use the correct full name for the
variable.
---
 gdb/dwarf2/read.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index bc7b8b46d87..9fa8b704182 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -13397,10 +13397,7 @@ var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
   if (attr == nullptr || !attr->as_boolean ())
     return nullptr;
 
-  attr = dwarf2_attr (die, DW_AT_name, cu);
-  if (attr == nullptr)
-    return nullptr;
-  return attr->as_string ();
+  return dwarf2_full_name (nullptr, die, cu);
 }
 
 /* Parse dwarf attribute if it's a block, reference or constant and put the

base-commit: f797b25fdc7ca4a48c09802082426b71e56898aa
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Use fully-qualified name in var_decl_name
  2026-04-23 19:01 [PATCH] Use fully-qualified name in var_decl_name Tom Tromey
@ 2026-04-24 13:33 ` Andrew Burgess
  2026-05-01 17:43   ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2026-04-24 13:33 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches; +Cc: Tom Tromey

Tom Tromey <tromey@adacore.com> writes:

> While testing gdb against a version of gnat-llvm that emits
> unqualified names (in a hierarchical structure), I found that
> gdb.ada/array_of_symbolic_length.exp would fail.
>
> The DWARF in question looks like this:
>
>  <1><11a1>: Abbrev Number: 2 (DW_TAG_module)
>     <11a2>   DW_AT_name        : (indirect string, offset: 0xe65): pck
>  <2><11a6>: Abbrev Number: 3 (DW_TAG_variable)
>     <11a7>   DW_AT_name        : (indirect string, offset: 0xe69): my_length_LAST
> [...]
>  <3><11df>: Abbrev Number: 5 (DW_TAG_subrange_type)
>     <11e0>   DW_AT_type        : <0x12ab>
>     <11e4>   DW_AT_upper_bound : <0x11a6>
>
> That is, an array's upper bound refers to my_length_LAST.  gdb
> represents this as a PROP_VARIABLE_NAME -- but only the local name of
> the variable was used in this case.
>
> This patch changes var_decl_name to use the correct full name for the
> variable.
> ---
>  gdb/dwarf2/read.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> index bc7b8b46d87..9fa8b704182 100644
> --- a/gdb/dwarf2/read.c
> +++ b/gdb/dwarf2/read.c
> @@ -13397,10 +13397,7 @@ var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
>    if (attr == nullptr || !attr->as_boolean ())
>      return nullptr;
>  
> -  attr = dwarf2_attr (die, DW_AT_name, cu);
> -  if (attr == nullptr)
> -    return nullptr;
> -  return attr->as_string ();
> +  return dwarf2_full_name (nullptr, die, cu);
>  }

I took a crack at writing a DWARF assembler test for this, which I've
included below.

I think it would be a good idea if a test was included with this patch.

Feel free to use what I've written, or change it as needed.  Or write
your own if this isn't suitable.

If the test below is OK, then:

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew

---

diff --git a/gdb/testsuite/gdb.dwarf2/ada-var-in-module.c b/gdb/testsuite/gdb.dwarf2/ada-var-in-module.c
new file mode 100644
index 00000000000..cbc388b0346
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/ada-var-in-module.c
@@ -0,0 +1,24 @@
+/* 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/>.  */
+
+int the_length = 5;
+int wrong_length = 9;
+int global_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/ada-var-in-module.exp b/gdb/testsuite/gdb.dwarf2/ada-var-in-module.exp
new file mode 100644
index 00000000000..b97e81b65c2
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/ada-var-in-module.exp
@@ -0,0 +1,151 @@
+# 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 that an array whose upper bound references a variable
+# declaration inside a DW_TAG_module gets the correct fully qualified
+# name.  This tests that var_decl_name returns the fully qualified
+# name.
+#
+# Without the var_decl_name fix, only the non-qualified name was being
+# returned, GDB would then lookup based on this partial name, and
+# could find the wrong variable.
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+require dwarf2_support
+
+standard_testfile .c -dw.S
+
+# Make some DWARF for the test.
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    cu {} {
+	DW_TAG_compile_unit {
+	    DW_AT_language @DW_LANG_Ada95
+	    DW_AT_name	    foo.adb
+	    DW_AT_comp_dir /tmp
+	} {
+	    declare_labels integer_label array_label \
+		length_label
+
+	    # Basic integer type.
+	    integer_label: DW_TAG_base_type {
+		DW_AT_byte_size 4 DW_FORM_sdata
+		DW_AT_encoding	 @DW_ATE_signed
+		DW_AT_name	 integer
+	    }
+
+	    # A module with a variable called 'my_length', this does
+	    # not point at the array length though.  This exists to
+	    # confuse Ada's wild card variable lookup.
+	    DW_TAG_module {
+		DW_AT_name aaa
+	    } {
+		DW_TAG_variable {
+		    DW_AT_name my_length
+		    DW_AT_type :$integer_label
+		    DW_AT_external 1 flag
+		    DW_AT_location {
+			DW_OP_addr [gdb_target_symbol wrong_length]
+		    } SPECIAL_expr
+		}
+	    }
+
+	    # The 'bbb' module contains the variable that holds the
+	    # array length (the array itself is defined below).  There
+	    # MUST be a separate declaration and definition for the
+	    # variable, the bug we are testing for is when the array
+	    # bound points at the declaration.
+	    DW_TAG_module {
+		DW_AT_name bbb
+	    } {
+		length_label: DW_TAG_variable {
+		    DW_AT_name my_length
+		    DW_AT_type :$integer_label
+		    DW_AT_declaration 1 flag
+		}
+
+		DW_TAG_variable {
+		    DW_AT_name my_length
+		    DW_AT_type :$integer_label
+		    DW_AT_external 1 flag
+		    DW_AT_location {
+			DW_OP_addr [gdb_target_symbol the_length]
+		    } SPECIAL_expr
+		}
+	    }
+
+	    # Another module with a variable called 'my_length', this
+	    # also does not point at the array length.  As with the
+	    # 'aaa' module, this exists to confuse Ada's wild card
+	    # variable lookup.
+	    DW_TAG_module {
+		DW_AT_name ccc
+	    } {
+		DW_TAG_variable {
+		    DW_AT_name my_length
+		    DW_AT_type :$integer_label
+		    DW_AT_external 1 flag
+		    DW_AT_location {
+			DW_OP_addr [gdb_target_symbol wrong_length]
+		    } SPECIAL_expr
+		}
+	    }
+
+	    # Global array type.  The upper bound is held in the
+	    # 'bbb.my_length' variable.  The upper bound MUST
+	    # reference the variable declaration in order to test the
+	    # bug we are interested in.
+	    array_label: DW_TAG_array_type {
+		DW_AT_name the_array_type
+		DW_AT_type :$integer_label
+	    } {
+		DW_TAG_subrange_type {
+		    DW_AT_type	       :$integer_label
+		    DW_AT_lower_bound 1 DW_FORM_sdata
+		    DW_AT_upper_bound :$length_label
+		}
+	    }
+
+	    # Global array variable.
+	    DW_TAG_variable {
+		DW_AT_name the_array
+		DW_AT_type :$array_label
+		DW_AT_location {
+		    DW_OP_addr [gdb_target_symbol global_array]
+		} SPECIAL_expr
+		DW_AT_external 1 flag
+	    }
+	}
+    }
+}
+
+if {[prepare_for_testing "failed to prepare" ${testfile} \
+	 [list $srcfile $asm_file] {nodebug}]} {
+    return
+}
+
+# Print 'the_array'.  To establish the upper bound GDB must lookup the
+# correct variable by name.  With the bug fix in place GDB should look
+# for 'bbb.my_length' and find the correct variable.  Without the fix
+# GDB would look for just 'my_length' which is ambiguous.  The hope of
+# this test is that by having an 'aaa.my_length' and 'ccc.my_length'
+# that, without the fix, GDB will find the wrong length and use that.
+gdb_test_no_output "set language ada"
+gdb_test "print aaa.my_length" " = 9"
+gdb_test "print bbb.my_length" " = 5"
+gdb_test "print ccc.my_length" " = 9"
+gdb_test "print the_array" " = \\(1, 2, 3, 4, 5\\)"


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Use fully-qualified name in var_decl_name
  2026-04-24 13:33 ` Andrew Burgess
@ 2026-05-01 17:43   ` Tom Tromey
  0 siblings, 0 replies; 3+ messages in thread
From: Tom Tromey @ 2026-05-01 17:43 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: Tom Tromey, gdb-patches

>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

>> This patch changes var_decl_name to use the correct full name for the
>> variable.

Andrew> I took a crack at writing a DWARF assembler test for this, which I've
Andrew> included below.

Andrew> I think it would be a good idea if a test was included with this patch.

Ok, I feel a little bad that you did this.  I didn't write a test for
the initial patch because I felt it was already covered by gnat-llvm
testing.

However I get that it's better to have a pure test for this.  Anyway
it's always fine to push back -- I would be fine writing a test.

So, thanks, and also sorry.

Andrew> Feel free to use what I've written, or change it as needed.  Or write
Andrew> your own if this isn't suitable.

Andrew> If the test below is OK, then:

I verified that the test fails without the patch and passes with it, and
also read the test & think it's fine.  So I'm including it.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-05-01 17:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-23 19:01 [PATCH] Use fully-qualified name in var_decl_name Tom Tromey
2026-04-24 13:33 ` Andrew Burgess
2026-05-01 17:43   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox