diff mbox series

[debug] Add -gdescriptive-dies

Message ID 516e20a1-0a30-958c-7cd2-848a4879dc4a@suse.de
State New
Headers show
Series [debug] Add -gdescriptive-dies | expand

Commit Message

Tom de Vries Aug. 22, 2018, 1:23 p.m. UTC
[ was: Re: [PATCH][debug] Add -gforce-named-dies ]

On 08/22/2018 11:46 AM, Tom de Vries wrote:
> 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
> decls.
> 
> Perhaps even for all cases that are added by the patch?
> 

I've chosen for this option. Using DW_AT_desciption instead of
DW_AT_name should minimize difference in gdb behaviour with and without
-gdescriptive-dies.

> I'll rewrite the patch.

OK for trunk?

Thanks,
- Tom

Comments

Richard Biener Aug. 24, 2018, 10:44 a.m. UTC | #1
On Wed, 22 Aug 2018, Tom de Vries wrote:

> [ was: Re: [PATCH][debug] Add -gforce-named-dies ]
> 
> On 08/22/2018 11:46 AM, Tom de Vries wrote:
> > 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
> > decls.
> > 
> > Perhaps even for all cases that are added by the patch?
> > 
> 
> I've chosen for this option. Using DW_AT_desciption instead of
> DW_AT_name should minimize difference in gdb behaviour with and without
> -gdescriptive-dies.

-gdescribe-dies?

> > I'll rewrite the patch.
> 
> OK for trunk?

Few comments:

+static void
+add_desc_attribute (dw_die_ref die, tree decl)
+{
+  tree decl_name;
+
+  if (!flag_descriptive_dies || dwarf_version < 3)
+    return;
+

you said this is a DWARF5 "feature", I'd suggest changing the
check to

  if (!flag_desctiprive_dies || (dwarf_version < 5 && dwarf_strict))

given -gdescribe-dies is enough of a user request to enable the
feature.  Not sure if we should warn about -gstrict-dwarf
combination with it and -gdwarf-N < 5.

+  else if (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_desc_attribute (die, buf);
+    }

I wondered before if we can use pretty-printing of decl here.  At
least I wouldn't restrict it to VAR_DECLs, FUNCTION_DECLs can
certainly appear here I think.

+@item -gdescriptive-dies
+@opindex gdescriptive-dies
+Add description attribute to DWARF DIEs that have no name attribute.
+

Either "description attributes" or "a description attribute", not
100% sure being a non-native speaker.

Otherwise the patch looks OK to me but please leave Jason time
to comment.

Richard.
diff mbox series

Patch

[debug] Add -gdescriptive-dies

This patch adds option -gdescriptive-dies.  It sets the DW_AT_description
attribute of dies that do not get a DW_AT_name 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_description: "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_description: "main"
 DW_AT_abstract_origin: die -> label: vla_1.c.6719312a + 29 (0x7fa88b965140)
...

Bootstrapped and reg-tested on x86_64, in combination with a patch that
switches the option on by default.

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

	* common.opt (gdescriptive-dies): Add option.
	* dwarf2out.c (add_name_and_src_coords_attributes): Add description
	attribute for artifical and nameless decls.
	(dwarf2out_register_external_die): Add description attribute to
	external reference die.
	(add_desc_attribute): New functions.
	(gen_subprogram_die): Add description attribute to
	DW_TAG_call_site_parameter.
	* doc/invoke.texi (@item Debugging Options): Add -gdescriptive-dies and
	-gno-descriptive-dies.
	(@item -gdescriptive-dies): Add.

---
 gcc/common.opt      |  4 ++++
 gcc/doc/invoke.texi |  6 +++++-
 gcc/dwarf2out.c     | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index ebc3ef43ce2..c2ba80c323f 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -2976,6 +2976,10 @@  gstrict-dwarf
 Common Driver Report Var(dwarf_strict) Init(0)
 Don't emit DWARF additions beyond selected version.
 
+gdescriptive-dies
+Common Driver Report Var(flag_descriptive_dies) Init(0)
+Add description attribute to DWARF DIEs that have no name attribute.
+
 gtoggle
 Common Driver Report Var(flag_gtoggle)
 Toggle debug information generation.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 99849ec6467..5a9c5b49481 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -372,7 +372,7 @@  Objective-C and Objective-C++ Dialects}.
 -ginternal-reset-location-views  -gno-internal-reset-location-views @gol
 -ginline-points  -gno-inline-points @gol
 -gvms  -gxcoff  -gxcoff+  -gz@r{[}=@var{type}@r{]} @gol
--gsplit-dwarf @gol
+-gsplit-dwarf -gdescriptive-dies -gno-descriptive-dies @gol
 -fdebug-prefix-map=@var{old}=@var{new}  -fdebug-types-section @gol
 -fno-eliminate-unused-debug-types @gol
 -femit-struct-debug-baseonly  -femit-struct-debug-reduced @gol
@@ -7395,6 +7395,10 @@  the build system to avoid linking files with debug information.  To
 be useful, this option requires a debugger capable of reading @file{.dwo}
 files.
 
+@item -gdescriptive-dies
+@opindex gdescriptive-dies
+Add description attribute to DWARF DIEs that have no name attribute.
+
 @item -gpubnames
 @opindex gpubnames
 Generate DWARF @code{.debug_pubnames} and @code{.debug_pubtypes} sections.
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index fb71ff349fa..e1d4ea50695 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -3808,6 +3808,7 @@  static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
 static bool tree_add_const_value_attribute (dw_die_ref, tree);
 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
 static void add_name_attribute (dw_die_ref, const char *);
+static void add_desc_attribute (dw_die_ref, tree);
 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
 static void add_comp_dir_attribute (dw_die_ref);
 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
@@ -6022,6 +6023,8 @@  dwarf2out_register_external_die (tree decl, const char *sym,
   else
     equate_decl_number_to_die (decl, die);
 
+  add_desc_attribute (die, decl);
+
   /* Add a reference to the DIE providing early debug at $sym + off.  */
   add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
 }
@@ -20529,6 +20532,53 @@  add_name_attribute (dw_die_ref die, const char *name_string)
     }
 }
 
+/* Generate a DW_AT_description attribute given some string value to be included
+   as the value of the attribute.  */
+
+static void
+add_desc_attribute (dw_die_ref die, const char *name_string)
+{
+  if (!flag_descriptive_dies || dwarf_version < 3)
+    return;
+
+  if (name_string != NULL && *name_string != 0)
+    {
+      if (demangle_name_func)
+	name_string = (*demangle_name_func) (name_string);
+
+      add_AT_string (die, DW_AT_description, name_string);
+    }
+}
+
+/* Generate a DW_AT_description attribute given some decl to be included
+   as the value of the attribute.  */
+
+static void
+add_desc_attribute (dw_die_ref die, tree decl)
+{
+  tree decl_name;
+
+  if (!flag_descriptive_dies || dwarf_version < 3)
+    return;
+
+  if (decl == NULL_TREE || !DECL_P (decl))
+    return;
+  decl_name = DECL_NAME (decl);
+
+  if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
+    {
+      const char *name = dwarf2_name (decl, 0);
+      add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
+    }
+  else if (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_desc_attribute (die, buf);
+    }
+}
+
 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
    of TYPE accordingly.
@@ -21287,12 +21337,17 @@  add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
       const char *name = dwarf2_name (decl, 0);
       if (name)
 	add_name_attribute (die, name);
+      else
+	add_desc_attribute (die, decl);
+
       if (! DECL_ARTIFICIAL (decl))
 	add_src_coords_attributes (die, decl);
 
       if (!no_linkage_name)
 	add_linkage_name (die, decl);
     }
+  else
+    add_desc_attribute (die, decl);
 
 #ifdef VMS_DEBUGGING_INFO
   /* Get the function's name, as described by its RTL.  This may be different
@@ -23265,6 +23320,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 +23385,7 @@  gen_subprogram_die (tree decl, dw_die_ref context_die)
 		      tdie = lookup_decl_die (tdecl);
 		      if (tdie == NULL)
 			continue;
+		      arg_decl = tdecl;
 		    }
 		  else
 		    continue;
@@ -23345,6 +23402,7 @@  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,
 				  NULL_TREE);
+		  add_desc_attribute (cdie, arg_decl);
 		  if (reg != NULL)
 		    add_AT_loc (cdie, DW_AT_location, reg);
 		  else if (tdie != NULL)