[debug] Add -gforce-named-dies

Message ID 077f3e77-79b8-4f33-a135-990a885cf999@suse.de
State New
Headers show
  • [debug] Add -gforce-named-dies
Related show

Commit Message

Tom de Vries Aug. 22, 2018, 6:56 a.m.
[ was: Re: [RFC][debug] Add -greadable-dwarf ]

On 08/21/2018 02:22 PM, Tom de Vries wrote:
> Currently doing a bootstrap and reg-test on x86_64 of attached patch

> with -gforce-named-dies enabled by default.

That went well. There are some understandable failures in
libstdc++-prettyprinters/whatis.cc, explained in the rationale below.

OK for trunk?

- Tom


Tom de Vries Aug. 22, 2018, 9:46 a.m. | #1
On 08/22/2018 08:56 AM, Tom de Vries wrote:
> This is an undocumented developer-only option, because using this option may

> change behaviour of dwarf consumers, f.i., gdb shows the artificial variables:

> ...

> (gdb) info locals

> a = 0x7fffffffda90 "\005"

> D.4278 = <optimized out>

> ...

I just found in the dwarf 5 spec the attribute DW_AT_description
(present since version 3):
2.20 Entity Descriptions
Some debugging information entries may describe entities in the program
that are artificial, or which otherwise have a “name” that is not a
valid identifier in the programming language.

This attribute provides a means for the producer to indicate the purpose
or usage of the containing debugging infor

Generally, any debugging information entry that has, or may have, a
DW_AT_name attribute, may also have a DW_AT_description attribute whose
value is a null-terminated string providing a description of the entity.

It is expected that a debugger will display these descriptions as part
of displaying other properties of an entity.

AFAICT, gdb currently does not explicitly handle this attribute, which I
think means it's ignored.

It seems applicable to use DW_AT_description at least for the artificial

Perhaps even for all cases that are added by the patch?

I'll rewrite the patch.

- Tom


[debug] Add -gforce-named-dies

This patch adds option -gforce-named-dies.  It sets the DW_AT_name attribute
of dies that otherwise do not get that attribute, to make it easier to figure
out what the die is describing.

The option exports the names of artificial variables:
 DIE    0: DW_TAG_variable (0x7fa934dd54b0)
+  DW_AT_name: "D.1922"
   DW_AT_type: die -> 0 (0x7fa934dd0d70)
   DW_AT_artificial: 1

which can be traced back to gimple dumps:
  char a[0:D.1922] [value-expr: *a.0];

Furthermore, it adds names to external references:
 DIE    0: DW_TAG_subprogram (0x7fa88b9650f0)
+DW_AT_name: "main"
 DW_AT_abstract_origin: die -> label: vla_1.c.6719312a + 29 (0x7fa88b965140)

This is an undocumented developer-only option, because using this option may
change behaviour of dwarf consumers, f.i., gdb shows the artificial variables:
(gdb) info locals
a = 0x7fffffffda90 "\005"
D.4278 = <optimized out>

Bootstrapped and reg-tested on x86_64, in combination with a patch that
switches the option on by default.  The only failures are in
libstdc++-prettyprinters/whatis.cc, where we get failures of the following
type: we expect to gdb to print
type = holder<std::ios>
but instead we get
  type = holder<std::basic_ios<char, std::char_traits<char> > >
where std::ios is defined as:
 typedef basic_ios<char>               ios;
 template<typename _CharT, typename _Traits = char_traits<_CharT> > class basic_ios;

2018-08-15  Tom de Vries  <tdevries@suse.de>

	* common.opt (gforce-named-dies): Add option.
	* dwarf2out.c (add_name_and_src_coords_attributes): Add param. Add name
	for artifical and nameless decls.
	(dwarf2out_register_external_die): Add name to external reference die.
	(gen_subprogram_die): Add name to DW_TAG_call_site_parameter.

 gcc/common.opt  |  5 +++++
 gcc/dwarf2out.c | 27 +++++++++++++++++++++++----
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index ebc3ef43ce2..76032f9bb1d 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -2976,6 +2976,11 @@  gstrict-dwarf
 Common Driver Report Var(dwarf_strict) Init(0)
 Don't emit DWARF additions beyond selected version.
+Common Driver Undocumented Report Var(flag_force_named_dies) Init(0)
+Force DWARF DIEs to have a name attribute.  Undocumented because it exposes
+compiler internals to the DWARF consumer.
 Common Driver Report Var(flag_gtoggle)
 Toggle debug information generation.
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index fb71ff349fa..dd8f438dfd3 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -3824,7 +3824,8 @@  static void add_prototyped_attribute (dw_die_ref, tree);
 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
 static void add_src_coords_attributes (dw_die_ref, tree);
-static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
+static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false,
+						bool = false);
 static void add_discr_value (dw_die_ref, dw_discr_value *);
 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
@@ -6022,6 +6023,8 @@  dwarf2out_register_external_die (tree decl, const char *sym,
     equate_decl_number_to_die (decl, die);
+  if (flag_force_named_dies && DECL_P (decl))
+    add_name_and_src_coords_attributes (die, decl, true, true);
   /* Add a reference to the DIE providing early debug at $sym + off.  */
   add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
@@ -21277,22 +21280,33 @@  add_linkage_name (dw_die_ref die, tree decl)
 static void
 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
-				    bool no_linkage_name)
+				    bool no_linkage_name,
+				    bool no_src_coords_attributes)
   tree decl_name;
   decl_name = DECL_NAME (decl);
   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
-      const char *name = dwarf2_name (decl, 0);
+      const char *name = (flag_force_named_dies && DECL_NAMELESS (decl)
+			  ? IDENTIFIER_POINTER (decl_name)
+			  : dwarf2_name (decl, 0));
       if (name)
 	add_name_attribute (die, name);
-      if (! DECL_ARTIFICIAL (decl))
+      if (! no_src_coords_attributes && ! DECL_ARTIFICIAL (decl))
 	add_src_coords_attributes (die, decl);
       if (!no_linkage_name)
 	add_linkage_name (die, decl);
+  else if (flag_force_named_dies && decl_name == NULL
+	   && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL))
+    {
+      char buf[32];
+      char decl_letter = TREE_CODE (decl) == CONST_DECL ? 'C' : 'D';
+      sprintf (buf, "%c.%u", decl_letter, DECL_UID (decl));
+      add_name_attribute (die, buf);
+    }
   /* Get the function's name, as described by its RTL.  This may be different
@@ -23265,6 +23279,7 @@  gen_subprogram_die (tree decl, dw_die_ref context_die)
 	      dw_die_ref die = NULL;
 	      rtx tloc = NULL_RTX, tlocc = NULL_RTX;
 	      rtx arg, next_arg;
+	      tree arg_decl = NULL_TREE;
 	      for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
 			  ? XEXP (ca_loc->call_arg_loc_note, 0)
@@ -23329,6 +23344,7 @@  gen_subprogram_die (tree decl, dw_die_ref context_die)
 		      tdie = lookup_decl_die (tdecl);
 		      if (tdie == NULL)
+		      arg_decl = tdecl;
@@ -23345,6 +23361,9 @@  gen_subprogram_die (tree decl, dw_die_ref context_die)
 		    die = gen_call_site_die (decl, subr_die, ca_loc);
 		  cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
+		  if (flag_force_named_dies && arg_decl)
+		    add_name_and_src_coords_attributes (cdie, arg_decl, true,
+							true);
 		  if (reg != NULL)
 		    add_AT_loc (cdie, DW_AT_location, reg);
 		  else if (tdie != NULL)