Skip to content

Commit 9c862a5

Browse files
committed
ctf: translate annotation DIEs to internal ctf
Translate DW_TAG_GNU_annotation DIEs created for C attributes btf_decl_tag and btf_type_tag into an in-memory representation in the CTF/BTF container. They will be output in BTF as BTF_KIND_DECL_TAG and BTF_KIND_TYPE_TAG records. The new CTF kinds used to represent these annotations, CTF_K_DECL_TAG and CTF_K_TYPE_TAG, are expected to be formalized in the next version of the CTF specification. For now they only exist in memory as a translation step to BTF, and are not emitted when generating CTF information. gcc/ * ctfc.cc (ctf_dtu_d_union_selector): Handle CTF_K_DECL_TAG and CTF_K_TYPE_TAG. (ctf_add_type_tag, ctf_add_decl_tag): New. (ctf_add_variable): Return the new ctf_dvdef_ref rather than zero. (new_ctf_container): Initialize new members. (ctfc_delete_container): Deallocate new members. * ctfc.h (ctf_dvdef, ctf_dvdef_t, ctf_dvdef_ref): Move forward declarations earlier in file. (ctf_decl_tag_t): New typedef. (ctf_dtdef): Add ctf_decl_tag_t member to dtd_u union. (ctf_dtu_d_union_enum): Add new CTF_DTU_D_TAG enumerator. (ctf_container): Add ctfc_tags vector and ctfc_type_tags_map hash_map members. (ctf_add_type_tag, ctf_add_decl_tag): New function protos. (ctf_add_variable): Change prototype return type to ctf_dvdef_ref. * dwarf2ctf.cc (gen_ctf_type_tags, gen_ctf_decl_tags) (gen_ctf_decl_tags_for_var): New static functions. (gen_ctf_pointer_type): Handle type tags. (gen_ctf_sou_type): Handle decl tags. (gen_ctf_function_type): Likewise. (gen_ctf_variable): Likewise. (gen_ctf_function): Likewise. (gen_ctf_type): Handle TAG_GNU_annotation DIEs. gcc/testsuite/ * gcc.dg/debug/ctf/ctf-decl-tag-1.c: New test. * gcc.dg/debug/ctf/ctf-type-tag-1.c: New test. include/ * ctf.h (CTF_K_DECL_TAG, CTF_K_TYPE_TAG): New defines.
1 parent ac7027f commit 9c862a5

File tree

6 files changed

+299
-13
lines changed

6 files changed

+299
-13
lines changed

gcc/ctfc.cc

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ ctf_dtu_d_union_selector (ctf_dtdef_ref ctftype)
107107
return CTF_DTU_D_ARGUMENTS;
108108
case CTF_K_SLICE:
109109
return CTF_DTU_D_SLICE;
110+
case CTF_K_DECL_TAG:
111+
case CTF_K_TYPE_TAG:
112+
return CTF_DTU_D_TAG;
110113
default:
111114
/* The largest member as default. */
112115
return CTF_DTU_D_ARRAY;
@@ -445,6 +448,68 @@ ctf_add_reftype (ctf_container_ref ctfc, uint32_t flag, ctf_dtdef_ref ref,
445448
return dtd;
446449
}
447450

451+
ctf_dtdef_ref
452+
ctf_add_type_tag (ctf_container_ref ctfc, uint32_t flag, const char *value,
453+
ctf_dtdef_ref ref_dtd)
454+
{
455+
ctf_dtdef_ref dtd;
456+
/* Create a DTD for the tag, but do not place it in the regular types list;
457+
CTF format does not (yet) encode tags. */
458+
dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
459+
460+
dtd->dtd_name = ctf_add_string (ctfc, value, &(dtd->dtd_data.ctti_name),
461+
CTF_AUX_STRTAB);
462+
/* A single DW_TAG_GNU_annotation DIE may be referenced by multiple DIEs,
463+
e.g. when multiple distinct types specify the same type tag. We will
464+
synthesize multiple CTF DTD records in that case, so we cannot tie them
465+
all to the same key (the DW_TAG_GNU_annotation DIE) in ctfc_types. */
466+
dtd->dtd_key = NULL;
467+
dtd->ref_type = ref_dtd;
468+
dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_TYPE_TAG, flag, 0);
469+
dtd->dtd_u.dtu_tag.ref_var = NULL; /* Not used for type tags. */
470+
dtd->dtd_u.dtu_tag.component_idx = 0; /* Not used for type tags. */
471+
472+
/* Insert tag directly into the tag list. Type ID will be assigned later. */
473+
vec_safe_push (ctfc->ctfc_tags, dtd);
474+
475+
/* Keep ctfc_aux_strlen updated. */
476+
if ((value != NULL) && strcmp (value, ""))
477+
ctfc->ctfc_aux_strlen += strlen (value) + 1;
478+
479+
return dtd;
480+
}
481+
482+
ctf_dtdef_ref
483+
ctf_add_decl_tag (ctf_container_ref ctfc, uint32_t flag, const char *value,
484+
ctf_dtdef_ref ref_dtd, uint32_t comp_idx)
485+
{
486+
ctf_dtdef_ref dtd;
487+
/* Create a DTD for the tag, but do not place it in the regular types list;
488+
ctf format does not (yet) encode tags. */
489+
dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
490+
491+
dtd->dtd_name = ctf_add_string (ctfc, value, &(dtd->dtd_data.ctti_name),
492+
CTF_AUX_STRTAB);
493+
/* A single DW_TAG_GNU_annotation DIE may be referenced by multiple DIEs,
494+
e.g. when multiple distinct declarations specify the same decl tag.
495+
We will synthesize multiple CTF DTD records in that case, so we cannot tie
496+
them all to the same key (the DW_TAG_GNU_annotation DIE) in ctfc_types. */
497+
dtd->dtd_key = NULL;
498+
dtd->ref_type = ref_dtd;
499+
dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_DECL_TAG, flag, 0);
500+
dtd->dtd_u.dtu_tag.ref_var = NULL;
501+
dtd->dtd_u.dtu_tag.component_idx = comp_idx;
502+
503+
/* Insert tag directly into the tag list. Type ID will be assigned later. */
504+
vec_safe_push (ctfc->ctfc_tags, dtd);
505+
506+
/* Keep ctfc_aux_strlen updated. */
507+
if ((value != NULL) && strcmp (value, ""))
508+
ctfc->ctfc_aux_strlen += strlen (value) + 1;
509+
510+
return dtd;
511+
}
512+
448513
ctf_dtdef_ref
449514
ctf_add_forward (ctf_container_ref ctfc, uint32_t flag, const char * name,
450515
uint32_t kind, dw_die_ref die)
@@ -691,12 +756,12 @@ ctf_add_member_offset (ctf_container_ref ctfc, dw_die_ref sou,
691756
return 0;
692757
}
693758

694-
int
759+
ctf_dvdef_ref
695760
ctf_add_variable (ctf_container_ref ctfc, const char * name, ctf_dtdef_ref ref,
696761
dw_die_ref die, unsigned int external_vis,
697762
dw_die_ref die_var_decl)
698763
{
699-
ctf_dvdef_ref dvd, dvd_ignore;
764+
ctf_dvdef_ref dvd = NULL, dvd_ignore;
700765

701766
gcc_assert (name);
702767

@@ -732,7 +797,7 @@ ctf_add_variable (ctf_container_ref ctfc, const char * name, ctf_dtdef_ref ref,
732797
ctfc->ctfc_strlen += strlen (name) + 1;
733798
}
734799

735-
return 0;
800+
return dvd;
736801
}
737802

738803
int
@@ -949,6 +1014,10 @@ new_ctf_container (void)
9491014
tu_ctfc->ctfc_ignore_vars
9501015
= hash_table<ctfc_dvd_hasher>::create_ggc (10);
9511016

1017+
vec_alloc (tu_ctfc->ctfc_tags, 100);
1018+
tu_ctfc->ctfc_type_tags_map
1019+
= hash_map<ctf_dtdef_ref, ctf_dtdef_ref>::create_ggc (100);
1020+
9521021
return tu_ctfc;
9531022
}
9541023

@@ -1003,6 +1072,11 @@ ctfc_delete_container (ctf_container_ref ctfc)
10031072
ctfc->ctfc_ignore_vars->empty ();
10041073
ctfc->ctfc_ignore_vars = NULL;
10051074

1075+
ctfc->ctfc_tags = NULL;
1076+
1077+
ctfc->ctfc_type_tags_map->empty ();
1078+
ctfc->ctfc_type_tags_map = NULL;
1079+
10061080
ctfc_delete_strtab (&ctfc->ctfc_strtable);
10071081
ctfc_delete_strtab (&ctfc->ctfc_aux_strtable);
10081082
if (ctfc->ctfc_vars_list)

gcc/ctfc.h

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ struct ctf_dtdef;
5252
typedef struct ctf_dtdef ctf_dtdef_t;
5353
typedef ctf_dtdef_t * ctf_dtdef_ref;
5454

55+
struct ctf_dvdef;
56+
typedef struct ctf_dvdef ctf_dvdef_t;
57+
typedef ctf_dvdef_t * ctf_dvdef_ref;
58+
5559
/* CTF string table element (list node). */
5660

5761
typedef struct GTY ((chain_next ("%h.cts_next"))) ctf_string
@@ -155,6 +159,16 @@ typedef struct GTY (()) ctf_func_arg
155159

156160
#define ctf_farg_list_next(elem) ((ctf_func_arg_t *)((elem)->farg_next))
157161

162+
/* Declaration Tag.
163+
May be used to annotate struct/union members, variable declarations,
164+
function declarations, and function arguments. */
165+
166+
typedef struct GTY (()) ctf_decl_tag
167+
{
168+
uint32_t component_idx; /* Index of component to which tag applies. */
169+
ctf_dvdef_ref ref_var; /* Non-null iff this tag applies to a variable. */
170+
} ctf_decl_tag_t;
171+
158172
/* Type definition for CTF generation. */
159173

160174
struct GTY ((for_user)) ctf_dtdef
@@ -184,6 +198,8 @@ struct GTY ((for_user)) ctf_dtdef
184198
ctf_func_arg_t * GTY ((tag ("CTF_DTU_D_ARGUMENTS"))) dtu_argv;
185199
/* slice. */
186200
ctf_sliceinfo_t GTY ((tag ("CTF_DTU_D_SLICE"))) dtu_slice;
201+
/* decl tag. */
202+
ctf_decl_tag_t GTY ((tag ("CTF_DTU_D_TAG"))) dtu_tag;
187203
} dtd_u;
188204
};
189205

@@ -201,9 +217,6 @@ struct GTY ((for_user)) ctf_dvdef
201217
ctf_id_t dvd_id; /* ID of this variable. Only used for BTF. */
202218
};
203219

204-
typedef struct ctf_dvdef ctf_dvdef_t;
205-
typedef ctf_dvdef_t * ctf_dvdef_ref;
206-
207220
/* Location information for CTF Types and CTF Variables. */
208221

209222
typedef struct GTY (()) ctf_srcloc
@@ -222,7 +235,8 @@ enum ctf_dtu_d_union_enum {
222235
CTF_DTU_D_ARRAY,
223236
CTF_DTU_D_ENCODING,
224237
CTF_DTU_D_ARGUMENTS,
225-
CTF_DTU_D_SLICE
238+
CTF_DTU_D_SLICE,
239+
CTF_DTU_D_TAG,
226240
};
227241

228242
enum ctf_dtu_d_union_enum
@@ -287,6 +301,17 @@ typedef struct GTY (()) ctf_container
287301
/* CTF variables to be ignored. */
288302
hash_table <ctfc_dvd_hasher> * GTY (()) ctfc_ignore_vars;
289303

304+
/* BTF type and decl tags. Not yet represented in CTF. These tags also
305+
uniquely have a one-to-many relation with DIEs, meaning a single DIE
306+
may translate to multiple CTF/BTF records. For both of these reasons,
307+
they cannot be stored in the regular types table. */
308+
vec <ctf_dtdef_ref, va_gc> * GTY (()) ctfc_tags;
309+
/* Type tags logically form part of the type chain similar to cv-quals.
310+
Therefore references to types need to know if the referred-to type has
311+
any type tags, and if so to refer to the outermost type tag. This map
312+
maps a type to the outermost type tag created for it, if any. */
313+
hash_map <ctf_dtdef_ref, ctf_dtdef_ref> * GTY (()) ctfc_type_tags_map;
314+
290315
/* CTF string table. */
291316
ctf_strtable_t ctfc_strtable;
292317
/* Auxilliary string table. At this time, used for keeping func arg names
@@ -440,15 +465,19 @@ extern ctf_dtdef_ref ctf_add_function (ctf_container_ref, uint32_t,
440465
dw_die_ref, bool, int);
441466
extern ctf_dtdef_ref ctf_add_sou (ctf_container_ref, uint32_t, const char *,
442467
uint32_t, unsigned HOST_WIDE_INT, dw_die_ref);
443-
468+
extern ctf_dtdef_ref ctf_add_type_tag (ctf_container_ref, uint32_t,
469+
const char *, ctf_dtdef_ref);
470+
extern ctf_dtdef_ref ctf_add_decl_tag (ctf_container_ref, uint32_t,
471+
const char *, ctf_dtdef_ref, uint32_t);
444472
extern int ctf_add_enumerator (ctf_container_ref, ctf_dtdef_ref, const char *,
445473
HOST_WIDE_INT, dw_die_ref);
446474
extern int ctf_add_member_offset (ctf_container_ref, dw_die_ref, const char *,
447475
ctf_dtdef_ref, uint64_t);
448476
extern int ctf_add_function_arg (ctf_container_ref, dw_die_ref,
449477
const char *, ctf_dtdef_ref);
450-
extern int ctf_add_variable (ctf_container_ref, const char *, ctf_dtdef_ref,
451-
dw_die_ref, unsigned int, dw_die_ref);
478+
extern ctf_dvdef_ref ctf_add_variable (ctf_container_ref, const char *,
479+
ctf_dtdef_ref, dw_die_ref, unsigned int,
480+
dw_die_ref);
452481

453482
extern ctf_dtdef_ref ctf_lookup_tree_type (ctf_container_ref, const tree);
454483

0 commit comments

Comments
 (0)