Bug 29335 - [gdb/ada, debug-types] FAIL: gdb.ada/local-enum.exp: print v1 element
Summary: [gdb/ada, debug-types] FAIL: gdb.ada/local-enum.exp: print v1 element
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: ada (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 14.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-08 08:00 UTC by Tom de Vries
Modified: 2023-09-07 19:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2022-07-08 08:00:39 UTC
With test-case gdb.ada/local-enum.exp and target board debug-types, I run into:
...
(gdb) print v1(three)^M
No name 'three' in enumeration type 'local__e1'^M
(gdb) FAIL: gdb.ada/local-enum.exp: print v1 element
print v2(three)^M
No name 'three' in enumeration type 'local__e2'^M
(gdb) FAIL: gdb.ada/local-enum.exp: print v2 element
...
Comment 1 Tom Tromey 2023-04-19 18:24:51 UTC
FWIW this passes for me on x86-64 Fedora 36:

murgatroyd. runtest --target_board=debug-types gdb.ada/local-enum.exp
...
		=== gdb Summary ===

# of expected passes		7
Comment 2 Tom de Vries 2023-04-20 08:03:00 UTC
(In reply to Tom Tromey from comment #1)
> FWIW this passes for me on x86-64 Fedora 36:
> 
> murgatroyd. runtest --target_board=debug-types gdb.ada/local-enum.exp
> ...
> 		=== gdb Summary ===
> 
> # of expected passes		7

Fails for me with GNATMAKE 11.3.0 (and all the way back to 7.5.0).

Passes for me with GNATMAKE 12.2.1.

Fedora 36 uses gcc 12 AFAICT ( https://fedoraproject.org/wiki/Changes/GNUToolchainF36 ) so that makes sense.
Comment 3 Tom de Vries 2023-04-20 08:57:20 UTC
With gnatmake-11:
...
$ readelf -WS leap-15-4/build/gdb/testsuite/outputs/gdb.ada/local-enum/local | grep debug | sed 's/.*\.//' | awk '{print $1}'
debug_aranges
debug_info
debug_abbrev
debug_line
debug_str
debug_loc
debug_ranges
debug_types
...

With gnatmake-12:
...
$ readelf -WS leap-15-4/build/gdb/testsuite/outputs/gdb.ada/local-enum/local | grep debug | sed 's/.*\.//' | awk '{print $1}'
debug_aranges
debug_info
debug_abbrev
debug_line
debug_str
debug_loc
debug_ranges
...

So, the test-case passes with gnatmake-12 because .debug_types is no longer generated.

I wrote some C program that has an enum:
...
$ cat test.c
enum a {
  b, c ,d
};

enum a
foo (enum a e)
{
  return e;
}

int
main (void)
{
  foo (b);
  return 0;
}
...
and tried:
...
$ gcc-11 test.c -g -fdebug-types-section; readelf -S -W a.out | grep -c debug_types
1
$ gcc-12 test.c -g -fdebug-types-section; readelf -S -W a.out | grep -c debug_types
1
...
so it's not that support for fdebug-types-section disappeared.

It's just that the ada compiler no longer generates the .debug_types section for a DW_TAG_type_unit which is used here:
...
 <1><15f2>: Abbrev Number: 19 (DW_TAG_array_type)
    <15f3>   DW_AT_name        : (indirect string, offset: 0x1e5a): local__a2
    <15f7>   DW_AT_GNAT_descriptive_type: signature: 0x76b135b6e23e0b79
    <15ff>   DW_AT_type        : <0x15ce>
...
referenced using a DW_AT_GNAT_descriptive_type.

The FAIL is back if we add additional_flags=-fgnat-encodings=all.
Comment 4 Tom Tromey 2023-04-27 16:30:30 UTC
(In reply to Tom de Vries from comment #3)

> The FAIL is back if we add additional_flags=-fgnat-encodings=all.

FWIW with new-enough versions of gcc, I encourage people
to use "minimal" encodings and not GNAT encodings.
I'd like to deprecate these eventually.
Comment 5 Tom de Vries 2023-09-07 10:27:26 UTC
Tentative patch:
...
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 2e1b9664fdc..2496c099f19 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -204,6 +204,8 @@ static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
 
 static int symbols_are_identical_enums
   (const std::vector<struct block_symbol> &syms);
+
+static int ada_identical_enum_types_p (struct type *type1, struct type *type2);
 

 
 /* The character set used for source files.  */
@@ -3801,11 +3803,24 @@ ada_resolve_enum (std::vector<struct block_symbol> &syms,
   gdb_assert (context_type->code () == TYPE_CODE_ENUM);
   context_type = ada_check_typedef (context_type);
 
+  /* We already know the name matches, so we're just looking for
+     an element of the correct enum type.  */
+  struct type *type1 = context_type;
+  for (int i = 0; i < syms.size (); ++i)
+    {
+      struct type *type2 = ada_check_typedef (syms[i].symbol->type ());
+      if (type1 == type2)
+	return i;
+    }
+
   for (int i = 0; i < syms.size (); ++i)
     {
-      /* We already know the name matches, so we're just looking for
-	 an element of the correct enum type.  */
-      if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
+      struct type *type2 = ada_check_typedef (syms[i].symbol->type ());
+      if (type1->num_fields () != type2->num_fields ())
+	continue;
+      if (strcmp (type1->name (), type2->name ()) != 0)
+	continue;
+      if (ada_identical_enum_types_p (type1, type2))
 	return i;
     }
 
...
Comment 7 Sourceware Commits 2023-09-07 19:53:24 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=74c36641b0e997ce81294e5bd4fe2d2ce017ea57

commit 74c36641b0e997ce81294e5bd4fe2d2ce017ea57
Author: Tom de Vries <tdevries@suse.de>
Date:   Thu Sep 7 21:53:17 2023 +0200

    [gdb/ada] Extend type equivalence test in ada_resolve_enum
    
    When running test-case gdb.ada/local-enum.exp with target board debug-types, I
    run into:
    ...
    (gdb) print v1(three)^M
    No name 'three' in enumeration type 'local__e1'^M
    (gdb) FAIL: gdb.ada/local-enum.exp: print v1 element
    ...
    
    The array V1 is of type A1 which is an array with index type E1, containing
    "three" as enumerator:
    ...
      type E1 is (one, two, three);
      type A1 is array (E1) of Integer;
      V1 : A1 := (0, 1, 2);
    ...
    
    There's also a type E2 that contains three as enumerator:
    ...
      type E2 is (three, four, five);
    ...
    
    When doing "print v1(three)", it's the job of ada_resolve_enum to resolve
    "three" to type E1 rather than type E2.
    
    When using target board debug-types, the enums E1 and E2 are replicated in the
    .debug_types section, and consequently in ada_resolve_enum the type
    equivalence check using a pointer comparison fails:
    ...
      for (int i = 0; i < syms.size (); ++i)
        {
          /* We already know the name matches, so we're just looking for
             an element of the correct enum type.  */
          if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
            return i;
        }
    ...
    
    Fix this by also trying a structural comparison using
    ada_identical_enum_types_p.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    PR ada/29335
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29335
Comment 8 Tom de Vries 2023-09-07 19:54:52 UTC
Fixed.