Bug 14148 - -fdebug-types-section regresses static scope of types
Summary: -fdebug-types-section regresses static scope of types
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-23 21:12 UTC by Jan Kratochvil
Modified: 2020-04-29 11:23 UTC (History)
3 users (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 Jan Kratochvil 2012-05-23 21:12:55 UTC
FAILing new testcase for -fdebug-types-section [Re: [RFC - Python scripting] New methods Symtab.global_block and Symtab.static_block (docs included)]
http://sourceware.org/ml/gdb-patches/2012-05/msg00132.html

PASS:
C="gcc -fno-debug-types-section -g -c -x c -";echo 'struct s { int i; }; extern void f (void); int main (void) { struct s a; f (); return 0; }'|$C -o 36.o;echo 'struct s { int j; }; void f (void) { struct s b; }'|$C -o 36b.o;gcc -o 36 36.o 36b.o;./gdb -readnow ./36 -ex start -ex 'ptype struct s' -ex step -ex 'ptype struct s' -ex c -ex q 2>/dev/null|grep -w int
    int i;
    int j;
FAIL:
C="gcc -fdebug-types-section -g -c -x c -";echo 'struct s { int i; }; extern void f (void); int main (void) { struct s a; f (); return 0; }'|$C -o 36.o;echo 'struct s { int j; }; void f (void) { struct s b; }'|$C -o 36b.o;gcc -o 36 36.o 36b.o;./gdb -readnow ./36 -ex start -ex 'ptype struct s' -ex step -ex 'ptype struct s' -ex c -ex q 2>/dev/null|grep -w int
    int j;
    int j;

In a different more readable form:

==> 36.c <==
struct s { int i; };
extern void f (void);
int main (void) {
  struct s a;
  f ();
  return 0;
}
==> 36b.c <==
struct s { int j; };
void f (void) {
  struct s b;
}
$ gcc -fdebug-types-section -o 36 36.c 36b.c -Wall -g
$ ./gdb -readnow ./36
(gdb) start
(gdb) ptype struct s
type = struct s {
    int j;
}
(gdb) ptype a
type = struct s {
    int i;
}
(gdb) whatis a
type = struct s

That is with -fdebug-types-section GDB no longer provides STATIC_BLOCK scope
for DW_TAG_type_unit types.
Comment 1 dje 2012-05-24 00:15:47 UTC
Cary told me that Jason is changing gcc to always create a skeleton type in the CU with the DW_FORM_ref_sig8 living in the skeleton entry.
It's a space saving optimization.

Whether we need to care about producers that don't do this, I'm not sure.
Expanding scan_partial_symbols to watch for type unit references feels problematic.
Comment 2 dje 2012-05-24 04:34:49 UTC
From Jan:
Jason's patch should be http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53235 which (a) still regresses and (b) it will use / uses DW_AT_declaration so I guess it will not work out of the box, but maybe a GDB fix can be easier on top of it.
Comment 3 Tom de Vries 2019-04-24 15:15:34 UTC
I can reproduce this with trunk gdb and trunk gcc.

One thing I noticed: without -fdebug-types-section, for 36.c we have the struct type s as top-level die, and as type of main variable a:
...
 <0><d2>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <d8>   DW_AT_name        : (indirect string, offset: 0x1f8): 36.c
 <1><f4>: Abbrev Number: 2 (DW_TAG_structure_type)
    <f5>   DW_AT_name        : s
 <1><112>: Abbrev Number: 5 (DW_TAG_subprogram)
    <113>   DW_AT_name        : (indirect string, offset: 0x1fd): main
 <2><130>: Abbrev Number: 6 (DW_TAG_variable)
    <131>   DW_AT_name        : a
    <136>   DW_AT_type        : <0xf4>
...
with the same pattern for 36b.c:
...
 <0><14a>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <150>   DW_AT_name        : (indirect string, offset: 0x261): 36b.c
 <1><16c>: Abbrev Number: 2 (DW_TAG_structure_type)
    <16d>   DW_AT_name        : s
 <1><18a>: Abbrev Number: 5 (DW_TAG_subprogram)
    <18b>   DW_AT_name        : f
 <2><1a2>: Abbrev Number: 6 (DW_TAG_variable)
    <1a3>   DW_AT_name        : b
    <1a8>   DW_AT_type        : <0x16c>
...

With -fdebug-types-section, we have struct s in .debug_types with only a reference from the variable for 36.c:
...
 <1><fb>: Abbrev Number: 6 (DW_TAG_subprogram)
    <fc>   DW_AT_name        : (indirect string, offset: 0x272): main
 <2><119>: Abbrev Number: 7 (DW_TAG_variable)
    <11a>   DW_AT_name        : a
    <11f>   DW_AT_type        : signature: 0xfd1462823bb6f7b7
Contents of the .debug_types section:
   Signature:     0xfd1462823bb6f7b7
 <1><1d>: Abbrev Number: 2 (DW_TAG_structure_type)
    <1e>   DW_AT_name        : s
...
and the same for 36b.c:
...
 <1><160>: Abbrev Number: 6 (DW_TAG_subprogram)
    <161>   DW_AT_name        : f
 <2><178>: Abbrev Number: 7 (DW_TAG_variable)
    <179>   DW_AT_name        : b
    <17e>   DW_AT_type        : signature: 0x534310fbefba324d
Contents of the .debug_types section:
   Signature:     0x534310fbefba324d
 <1><59>: Abbrev Number: 2 (DW_TAG_structure_type)
    <5a>   DW_AT_name        : s
...

So, AFAICT gcc drops the top-level struct s dies. I think this is a gcc bug, I'll file a PR.

I'm not sure if this causes the difference with and without -readnow described in comment 0.
Comment 4 Tom de Vries 2020-04-13 21:33:16 UTC
(In reply to Tom de Vries from comment #3)
> So, AFAICT gcc drops the top-level struct s dies. I think this is a gcc bug,
> I'll file a PR.

PR gcc/90232 - "gcc drops top-level dies with -fdebug-types-section" ( https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90232 ).
Comment 5 Tom de Vries 2020-04-17 13:39:09 UTC
(In reply to Tom de Vries from comment #3)
> I'm not sure if this causes the difference with and without -readnow
> described in comment 0.

With a tentative patch for the gcc PR in place, and this gdb patch:
...
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 4910c9b6fc..cf81092691 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -15458,7 +15458,8 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
      these DIEs are identified by the fact that they have no byte_size
      attribute, and a declaration attribute.  */
   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
-      || !die_is_declaration (die, cu))
+      || !die_is_declaration (die, cu)
+      || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
     {
       struct symbol *sym = new_symbol (die, type, cu);
 
...
we have the expected behaviour.
Comment 7 Sourceware Commits 2020-04-25 15:19:30 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=d472f0fbaac80ed6363f26c3f417b9eee7d5e7fc

commit d472f0fbaac80ed6363f26c3f417b9eee7d5e7fc
Author: Tom de Vries <tdevries@suse.de>
Date:   Sat Apr 25 17:19:26 2020 +0200

    [gdb/testsuite] Add target board debug-types
    
    This patch adds a target board debug-types that switches on
    -fdebug-types-section by default.
    
    This -fdebug-types-section option is a gcc option that enables the generation
    of a .debug_types section, which is only effective for DWARF version 4.
    
    There are two other boards that enable this: dwarf4-gdb-index and fisson, but
    while those test some meaningful combination of options, this board is
    intended to test only -fdebug-types-section.
    
    Current results with gcc 7.5.0 are:
    ...
     === gdb Summary ===
    
     # of expected passes            75832
     # of unexpected failures        2841
     # of expected failures          130
     # of known failures             75
     # of unresolved testcases       22
     # of untested testcases         37
     # of unsupported tests          83
    ...
    
    Related known issues:
    - PR gcc/90232 - "gcc drops top-level dies with -fdebug-types-section"
    - PR gdb/25875 - "segv in ada_discrete_type_low_bound"
    - PR gdb/14148 - "-fdebug-types-section regresses static scope of types"
    
    Tested on x86_64-linux.
    
    gdb/testsuite/ChangeLog:
    
    2020-04-25  Tom de Vries  <tdevries@suse.de>
    
            * boards/debug-types.exp: New file.
Comment 8 Tom de Vries 2020-04-29 11:23:57 UTC
GDB patch with test-case committed ( https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=15cd93d05e8e84644acc8bbeaa3d5f4280cc5159 ).

The testsuite test-cases which fail due to the gcc PR have been xfailed ( https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=6e4e3fe1b6d68bde1f4e022bd0675fe36420e976 ).

Marking resolved-fixed.