public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/dfaust/heads/btf-type-tag-new-rebase)] dwarf2ctf: convert tag DIEs to CTF types
@ 2022-10-07 18:34 David Faust
  0 siblings, 0 replies; only message in thread
From: David Faust @ 2022-10-07 18:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:71950a45356a5c24449ea3fb15f3e335417c9ff1

commit 71950a45356a5c24449ea3fb15f3e335417c9ff1
Author: David Faust <david.faust@oracle.com>
Date:   Wed Mar 2 15:12:55 2022 -0800

    dwarf2ctf: convert tag DIEs to CTF types
    
    This patch makes the DWARF-to-CTF conversion process aware of the new
    DW_TAG_GNU_annotation DIEs. The DIEs are converted to CTF_K_DECL_TAG or
    CTF_K_TYPE_TAG types as approprate and added to the compilation unit CTF
    container.
    
    gcc/
    
            * dwarf2ctf.cc (handle_btf_tags): New function.
            (gen_ctf_sou_type): Call it here, if appropriate. Don't try to
            create member types for children that are not DW_TAG_member.
            (gen_ctf_function_type): Call handle_btf_tags if appropriate.
            (gen_ctf_variable): Likewise.
            (gen_ctf_function): Likewise.
            (gen_ctf_type): Likewise.

Diff:
---
 gcc/dwarf2ctf.cc | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 112 insertions(+), 1 deletion(-)

diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
index 83fc19185b5..94fc9dc7b05 100644
--- a/gcc/dwarf2ctf.cc
+++ b/gcc/dwarf2ctf.cc
@@ -32,6 +32,12 @@ along with GCC; see the file COPYING3.  If not see
 static ctf_id_t
 gen_ctf_type (ctf_container_ref, dw_die_ref);
 
+static void
+gen_ctf_variable (ctf_container_ref, dw_die_ref);
+
+static void
+handle_btf_tags (ctf_container_ref, dw_die_ref, ctf_id_t, int);
+
 /* All the DIE structures we handle come from the DWARF information
    generated by GCC.  However, there are three situations where we need
    to create our own created DIE structures because GCC doesn't
@@ -547,6 +553,7 @@ gen_ctf_sou_type (ctf_container_ref ctfc, dw_die_ref sou, uint32_t kind)
   /* Now process the struct members.  */
   {
     dw_die_ref c;
+    int idx = 0;
 
     c = dw_get_die_child (sou);
     if (c)
@@ -559,6 +566,12 @@ gen_ctf_sou_type (ctf_container_ref ctfc, dw_die_ref sou, uint32_t kind)
 
 	  c = dw_get_die_sib (c);
 
+	  if (dw_get_die_tag (c) != DW_TAG_member)
+	    continue;
+
+	  if (c == dw_get_die_child (sou))
+	    idx = 0;
+
 	  field_name = get_AT_string (c, DW_AT_name);
 	  field_type = ctf_get_AT_type (c);
 	  field_location = ctf_get_AT_data_member_location (c);
@@ -626,6 +639,12 @@ gen_ctf_sou_type (ctf_container_ref ctfc, dw_die_ref sou, uint32_t kind)
 				 field_name,
 				 field_type_id,
 				 field_location);
+
+	  /* Handle BTF tags on the member.  */
+	  if (btf_debuginfo_p ())
+	    handle_btf_tags (ctfc, c, sou_type_id, idx);
+
+	  idx++;
 	}
       while (c != dw_get_die_child (sou));
   }
@@ -718,6 +737,9 @@ gen_ctf_function_type (ctf_container_ref ctfc, dw_die_ref function,
 	      arg_type = gen_ctf_type (ctfc, ctf_get_AT_type (c));
 	      /* Add the argument to the existing CTF function type.  */
 	      ctf_add_function_arg (ctfc, function, arg_name, arg_type);
+
+	      if (btf_debuginfo_p ())
+		handle_btf_tags (ctfc, c, function_type_id, i - 1);
 	    }
 	  else
 	    /* This is a local variable.  Ignore.  */
@@ -830,6 +852,11 @@ gen_ctf_variable (ctf_container_ref ctfc, dw_die_ref die)
   /* Skip updating the number of global objects at this time.  This is updated
      later after pre-processing as some CTF variable records although
      generated now, will not be emitted later.  [PR105089].  */
+
+  /* Handle any BTF tags on the variable.  */
+  if (btf_debuginfo_p ())
+    handle_btf_tags (ctfc, die, CTF_NULL_TYPEID, -1);
+
 }
 
 /* Add a CTF function record for the given input DWARF DIE.  */
@@ -847,8 +874,12 @@ gen_ctf_function (ctf_container_ref ctfc, dw_die_ref die)
      counter.  Note that DWARF encodes function types in both
      DW_TAG_subroutine_type and DW_TAG_subprogram in exactly the same
      way.  */
-  (void) gen_ctf_function_type (ctfc, die, true /* from_global_func */);
+  function_type_id = gen_ctf_function_type (ctfc, die, true /* from_global_func */);
   ctfc->ctfc_num_global_funcs += 1;
+
+  /* Handle any BTF tags on the function itself.  */
+  if (btf_debuginfo_p ())
+    handle_btf_tags (ctfc, die, function_type_id, -1);
 }
 
 /* Add CTF type record(s) for the given input DWARF DIE and return its type id.
@@ -925,6 +956,10 @@ gen_ctf_type (ctf_container_ref ctfc, dw_die_ref die)
       break;
     }
 
+  /* Handle any BTF tags on the type.  */
+  if (btf_debuginfo_p () && !unrecog_die)
+    handle_btf_tags (ctfc, die, type_id, -1);
+
   /* For all types unrepresented in CTF, use an explicit CTF type of kind
      CTF_K_UNKNOWN.  */
   if ((type_id == CTF_NULL_TYPEID) && (!unrecog_die))
@@ -933,6 +968,82 @@ gen_ctf_type (ctf_container_ref ctfc, dw_die_ref die)
   return type_id;
 }
 
+/* BTF support. Handle any BTF tags attached to a given DIE, and generate
+   intermediate CTF types for them. Type tags are inserted into the type chain
+   at this point. The return value is the CTF type ID of the last type tag
+   created (for type chaining), or the same as the argument TYPE_ID if there are
+   no type tags.
+   Note that despite the name, the BTF spec seems to allow decl tags on types
+   as well as declarations.  */
+
+static void
+handle_btf_tags (ctf_container_ref ctfc, dw_die_ref die, ctf_id_t type_id,
+		 int component_idx)
+{
+  dw_die_ref c;
+  const char * name = NULL;
+  const char * value = NULL;
+  ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die);
+  ctf_id_t target_id, tag_id;
+
+  if (dtd)
+    target_id = dtd->dtd_data.ctti_type;
+  else
+    target_id = CTF_NULL_TYPEID;
+
+  c = dw_get_die_child (die);
+  if (c)
+    do
+      {
+	if (dw_get_die_tag (c) != DW_TAG_GNU_annotation)
+	  {
+	    c = dw_get_die_sib (c);
+	    continue;
+	  }
+
+	name = get_AT_string (c, DW_AT_name);
+
+	/* BTF decl tags add an arbitrary annotation to the thing they
+	   annotate. The annotated thing could be a variable or a type.  */
+	if (strcmp (name, "btf_decl_tag") == 0)
+	  {
+	    value = get_AT_string (c, DW_AT_const_value);
+	    if (!ctf_type_exists (ctfc, c, &tag_id))
+	      (void) ctf_add_reftype (ctfc, CTF_ADD_ROOT, value,
+				      type_id, CTF_K_DECL_TAG, c);
+	    ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, c);
+	    dtd->dtd_u.dtu_btfnote.component_idx = component_idx;
+	  }
+
+	/* BTF type tags are part of the type chain similar to cvr quals.
+	   But the type tag DIEs are children of the DIEs they annotate.
+
+	   For each type tag on this type, create a CTF type for it and
+	   insert it into the type chain:
+	   - The first tag refers to the type referred to by the parent.
+	   - Each subsequent tag refers to the prior tag.
+	   - The parent type is updated to refer to the last tag.  */
+
+	/* TODO: given this type chain requirement, the representation of type
+	   tags in BTF only makes sense for pointer types. Should this be
+	   enforced here?  */
+	else if (strcmp (name, "btf_type_tag") == 0)
+	  {
+	    gcc_assert (dtd);
+	    value = get_AT_string (c, DW_AT_const_value);
+
+	    if (!ctf_type_exists (ctfc, c, &tag_id))
+	      tag_id = ctf_add_reftype (ctfc, CTF_ADD_ROOT, value,
+					target_id, CTF_K_TYPE_TAG, c);
+
+	    dtd->dtd_data.ctti_type = tag_id;
+	    target_id = tag_id;
+	  }
+	c = dw_get_die_sib (c);
+      }
+    while (c != dw_get_die_child (die));
+}
+
 /* Prepare for output and write out the CTF debug information.  */
 
 static void

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-07 18:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-07 18:34 [gcc(refs/users/dfaust/heads/btf-type-tag-new-rebase)] dwarf2ctf: convert tag DIEs to CTF types David Faust

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).