2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2006 Mandriva Conectiva S.A.
5 Copyright (C) 2006 Arnaldo Carvalho de Melo <acme@mandriva.com>
6 Copyright (C) 2007 Red Hat Inc.
7 Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
26 #include <sys/utsname.h>
33 #define min(x, y) ((x) < (y) ? (x) : (y))
35 #define obstack_chunk_alloc malloc
36 #define obstack_chunk_free free
38 static void *obstack_zalloc(struct obstack
*obstack
, size_t size
)
40 void *o
= obstack_alloc(obstack
, size
);
47 void *cu__zalloc(struct cu
*cu
, size_t size
)
50 return obstack_zalloc(&cu
->obstack
, size
);
55 void *cu__malloc(struct cu
*cu
, size_t size
)
58 return obstack_alloc(&cu
->obstack
, size
);
63 void cu__free(struct cu
*cu
, void *ptr
)
68 // When using an obstack we'll free everything in cu__delete()
71 int tag__is_base_type(const struct tag
*tag
, const struct cu
*cu
)
74 case DW_TAG_base_type
:
77 case DW_TAG_typedef
: {
78 const struct tag
*type
= cu__type(cu
, tag
->type
);
82 return tag__is_base_type(type
, cu
);
88 bool tag__is_array(const struct tag
*tag
, const struct cu
*cu
)
91 case DW_TAG_array_type
:
94 case DW_TAG_const_type
:
95 case DW_TAG_typedef
: {
96 const struct tag
*type
= cu__type(cu
, tag
->type
);
100 return tag__is_array(type
, cu
);
106 int __tag__has_type_loop(const struct tag
*tag
, const struct tag
*type
,
107 char *bf
, size_t len
, FILE *fp
,
108 const char *fn
, int line
)
110 char bbf
[2048], *abf
= bbf
;
115 if (tag
->type
== type
->type
) {
122 printed
= snprintf(abf
, len
, "<ERROR(%s:%d): detected type loop: type=%d, tag=%s>",
123 fn
, line
, tag
->type
, dwarf_tag_name(tag
->tag
));
125 printed
= fprintf(fp
?: stderr
, "%s\n", abf
);
132 static void lexblock__delete_tags(struct tag
*tag
)
134 struct lexblock
*block
= tag__lexblock(tag
);
137 list_for_each_entry_safe_reverse(pos
, n
, &block
->tags
, node
) {
138 list_del_init(&pos
->node
);
143 void lexblock__delete(struct lexblock
*block
)
148 lexblock__delete_tags(&block
->ip
.tag
);
152 void tag__delete(struct tag
*tag
)
157 assert(list_empty(&tag
->node
));
160 case DW_TAG_union_type
:
161 type__delete(tag__type(tag
)); break;
162 case DW_TAG_class_type
:
163 case DW_TAG_structure_type
:
164 class__delete(tag__class(tag
)); break;
165 case DW_TAG_enumeration_type
:
166 enumeration__delete(tag__type(tag
)); break;
167 case DW_TAG_subroutine_type
:
168 ftype__delete(tag__ftype(tag
)); break;
169 case DW_TAG_subprogram
:
170 function__delete(tag__function(tag
)); break;
171 case DW_TAG_lexical_block
:
172 lexblock__delete(tag__lexblock(tag
)); break;
178 void tag__not_found_die(const char *file
, int line
, const char *func
, int tag
, const char *name
)
180 fprintf(stderr
, "%s::%s(%d, related to the type of tag DW_TAG_%s \"%s\"): tag not found, please report to "
181 "acme@kernel.org\n", file
, func
, line
, dwarf_tag_name(tag
), name
);
185 struct tag
*tag__follow_typedef(const struct tag
*tag
, const struct cu
*cu
)
187 struct tag
*type
= cu__type(cu
, tag
->type
);
189 if (type
!= NULL
&& tag__is_typedef(type
))
190 return tag__follow_typedef(type
, cu
);
195 struct tag
*tag__strip_typedefs_and_modifiers(const struct tag
*tag
, const struct cu
*cu
)
197 struct tag
*type
= cu__type(cu
, tag
->type
);
199 while (type
!= NULL
&& (tag__is_typedef(type
) || tag__is_modifier(type
)))
200 type
= cu__type(cu
, type
->type
);
205 size_t __tag__id_not_found_fprintf(FILE *fp
, type_id_t id
,
206 const char *fn
, int line
)
208 return fprintf(fp
, "<ERROR(%s:%d): %d not found!>\n", fn
, line
, id
);
211 static struct ase_type_name_to_size
{
214 } base_type_name_to_size_table
[] = {
215 { .name
= "unsigned", .size
= 32, },
216 { .name
= "signed int", .size
= 32, },
217 { .name
= "unsigned int", .size
= 32, },
218 { .name
= "int", .size
= 32, },
219 { .name
= "short unsigned int", .size
= 16, },
220 { .name
= "signed short", .size
= 16, },
221 { .name
= "unsigned short", .size
= 16, },
222 { .name
= "short int", .size
= 16, },
223 { .name
= "short", .size
= 16, },
224 { .name
= "char", .size
= 8, },
225 { .name
= "signed char", .size
= 8, },
226 { .name
= "unsigned char", .size
= 8, },
227 { .name
= "signed long", .size
= 0, },
228 { .name
= "long int", .size
= 0, },
229 { .name
= "long", .size
= 0, },
230 { .name
= "signed long", .size
= 0, },
231 { .name
= "unsigned long", .size
= 0, },
232 { .name
= "long unsigned int", .size
= 0, },
233 { .name
= "bool", .size
= 8, },
234 { .name
= "_Bool", .size
= 8, },
235 { .name
= "long long unsigned int", .size
= 64, },
236 { .name
= "long long int", .size
= 64, },
237 { .name
= "long long", .size
= 64, },
238 { .name
= "signed long long", .size
= 64, },
239 { .name
= "unsigned long long", .size
= 64, },
240 { .name
= "double", .size
= 64, },
241 { .name
= "double double", .size
= 64, },
242 { .name
= "single float", .size
= 32, },
243 { .name
= "float", .size
= 32, },
244 { .name
= "long double", .size
= sizeof(long double) * 8, },
245 { .name
= "long double long double", .size
= sizeof(long double) * 8, },
246 { .name
= "__int128", .size
= 128, },
247 { .name
= "unsigned __int128", .size
= 128, },
248 { .name
= "__int128 unsigned", .size
= 128, },
249 { .name
= "_Float128", .size
= 128, },
253 bool base_type__language_defined(struct base_type
*bt
)
259 if (bt
->name_has_encoding
)
262 name
= base_type__name(bt
, bf
, sizeof(bf
));
264 while (base_type_name_to_size_table
[i
].name
!= NULL
) {
265 if (bt
->name_has_encoding
) {
266 if (strcmp(base_type_name_to_size_table
[i
].name
, bt
->name
) == 0)
268 } else if (strcmp(base_type_name_to_size_table
[i
].name
, name
) == 0)
276 size_t base_type__name_to_size(struct base_type
*bt
, struct cu
*cu
)
280 const char *name
, *orig_name
;
282 if (bt
->name_has_encoding
)
285 name
= base_type__name(bt
, bf
, sizeof(bf
));
288 while (base_type_name_to_size_table
[i
].name
!= NULL
) {
289 if (bt
->name_has_encoding
) {
290 if (strcmp(base_type_name_to_size_table
[i
].name
, bt
->name
) == 0) {
293 size
= base_type_name_to_size_table
[i
].size
;
295 return size
?: ((size_t)cu
->addr_size
* 8);
297 } else if (strcmp(base_type_name_to_size_table
[i
].name
, name
) == 0)
302 if (strstarts(name
, "signed ")) {
304 name
+= sizeof("signed");
308 fprintf(stderr
, "%s: %s %s\n",
309 __func__
, dwarf_tag_name(bt
->tag
.tag
), orig_name
);
313 static const char *base_type_fp_type_str
[] = {
314 [BT_FP_SINGLE
] = "single",
315 [BT_FP_DOUBLE
] = "double",
316 [BT_FP_CMPLX
] = "complex",
317 [BT_FP_CMPLX_DBL
] = "complex double",
318 [BT_FP_CMPLX_LDBL
] = "complex long double",
319 [BT_FP_LDBL
] = "long double",
320 [BT_FP_INTVL
] = "interval",
321 [BT_FP_INTVL_DBL
] = "interval double",
322 [BT_FP_INTVL_LDBL
] = "interval long double",
323 [BT_FP_IMGRY
] = "imaginary",
324 [BT_FP_IMGRY_DBL
] = "imaginary double",
325 [BT_FP_IMGRY_LDBL
] = "imaginary long double",
328 const char *__base_type__name(const struct base_type
*bt
)
333 const char *base_type__name(const struct base_type
*bt
, char *bf
, size_t len
)
335 if (bt
->name_has_encoding
)
336 return __base_type__name(bt
);
339 snprintf(bf
, len
, "%s %s", base_type_fp_type_str
[bt
->float_type
], bt
->name
);
341 snprintf(bf
, len
, "%s%s%s", bt
->is_bool
? "bool " : "", bt
->is_varargs
? "... " : "", bt
->name
);
345 void namespace__delete(struct namespace *space
)
352 namespace__for_each_tag_safe_reverse(space
, pos
, n
) {
353 list_del_init(&pos
->node
);
355 /* Look for nested namespaces */
356 if (tag__has_namespace(pos
))
357 namespace__delete(tag__namespace(pos
));
361 tag__delete(&space
->tag
);
364 void __type__init(struct type
*type
)
366 INIT_LIST_HEAD(&type
->node
);
367 INIT_LIST_HEAD(&type
->type_enum
);
368 type
->sizeof_member
= NULL
;
369 type
->member_prefix
= NULL
;
370 type
->member_prefix_len
= 0;
371 type
->suffix_disambiguation
= 0;
374 struct class_member
*
375 type__find_first_biggest_size_base_type_member(struct type
*type
,
378 struct class_member
*pos
, *result
= NULL
;
379 size_t result_size
= 0;
381 type__for_each_data_member(type
, pos
) {
385 struct tag
*type
= cu__type(cu
, pos
->tag
.type
);
386 size_t member_size
= 0, power2
;
387 struct class_member
*inner
= NULL
;
390 tag__id_not_found_fprintf(stderr
, pos
->tag
.type
);
395 case DW_TAG_base_type
:
396 member_size
= base_type__size(type
);
398 case DW_TAG_pointer_type
:
399 case DW_TAG_reference_type
:
400 member_size
= cu
->addr_size
;
402 case DW_TAG_class_type
:
403 case DW_TAG_union_type
:
404 case DW_TAG_structure_type
:
405 if (tag__type(type
)->nr_members
== 0)
407 inner
= type__find_first_biggest_size_base_type_member(tag__type(type
), cu
);
408 member_size
= inner
->byte_size
;
410 case DW_TAG_array_type
:
411 case DW_TAG_const_type
:
413 case DW_TAG_rvalue_reference_type
:
414 case DW_TAG_volatile_type
:
415 case DW_TAG_atomic_type
: {
416 struct tag
*tag
= cu__type(cu
, type
->type
);
418 tag__id_not_found_fprintf(stderr
, type
->type
);
424 case DW_TAG_enumeration_type
:
425 member_size
= tag__type(type
)->size
/ 8;
430 if (member_size
> cu
->addr_size
)
433 for (power2
= cu
->addr_size
; power2
> result_size
; power2
/= 2)
434 if (member_size
>= power2
) {
435 if (power2
== cu
->addr_size
)
437 result_size
= power2
;
438 result
= inner
?: pos
;
445 static void cu__find_class_holes(struct cu
*cu
)
450 cu__for_each_struct(cu
, id
, pos
)
451 class__find_holes(pos
);
456 struct list_head cus
;
457 pthread_mutex_t mutex
;
458 void (*loader_exit
)(struct cus
*cus
);
459 void *priv
; // Used in dwarf_loader__exit()
462 void cus__lock(struct cus
*cus
)
464 pthread_mutex_lock(&cus
->mutex
);
467 void cus__unlock(struct cus
*cus
)
469 pthread_mutex_unlock(&cus
->mutex
);
472 void cus__set_cu_state(struct cus
*cus
, struct cu
*cu
, enum cu_state state
)
479 // Used only when reproducible builds are desired
480 struct cu
*cus__get_next_processable_cu(struct cus
*cus
)
486 list_for_each_entry(cu
, &cus
->cus
, node
) {
489 cu
->state
= CU__PROCESSING
;
492 // This will happen when we get to parallel
493 // reproducible BTF encoding, libbpf dedup work needed
494 // here. The other possibility is when we're flushing
495 // the DWARF processed CUs when the parallel DWARF
496 // loading stoped and we still have CUs to encode to
497 // BTF because of ordering requirements.
499 case CU__UNPROCESSED
:
500 // The first entry isn't loaded, signal the
501 // caller to return and try another day, as we
502 // need to respect the original DWARF CU ordering.
514 bool cus__empty(const struct cus
*cus
)
516 return list_empty(&cus
->cus
);
519 uint32_t cus__nr_entries(const struct cus
*cus
)
521 return cus
->nr_entries
;
524 void __cus__remove(struct cus
*cus
, struct cu
*cu
)
527 list_del_init(&cu
->node
);
530 void cus__remove(struct cus
*cus
, struct cu
*cu
)
533 __cus__remove(cus
, cu
);
537 void __cus__add(struct cus
*cus
, struct cu
*cu
)
540 list_add_tail(&cu
->node
, &cus
->cus
);
543 void cus__add(struct cus
*cus
, struct cu
*cu
)
549 cu__find_class_holes(cu
);
552 static void ptr_table__init(struct ptr_table
*pt
)
555 pt
->nr_entries
= pt
->allocated_entries
= 0;
558 static void ptr_table__exit(struct ptr_table
*pt
)
563 static int ptr_table__add(struct ptr_table
*pt
, void *ptr
, uint32_t *idxp
)
565 const uint32_t nr_entries
= pt
->nr_entries
+ 1;
566 const uint32_t rc
= pt
->nr_entries
;
568 if (nr_entries
> pt
->allocated_entries
) {
569 uint32_t allocated_entries
= pt
->allocated_entries
+ 2048;
570 void *entries
= realloc(pt
->entries
,
571 sizeof(void *) * allocated_entries
);
575 /* Zero out the new range */
576 memset(entries
+ pt
->allocated_entries
* sizeof(void *), 0,
577 (allocated_entries
- pt
->allocated_entries
) * sizeof(void *));
579 pt
->allocated_entries
= allocated_entries
;
580 pt
->entries
= entries
;
583 pt
->entries
[rc
] = ptr
;
584 pt
->nr_entries
= nr_entries
;
589 static int ptr_table__add_with_id(struct ptr_table
*pt
, void *ptr
,
592 /* Assume we won't be fed with the same id more than once */
593 if (id
>= pt
->allocated_entries
) {
594 uint32_t allocated_entries
= roundup(id
+ 1, 2048);
595 void *entries
= realloc(pt
->entries
,
596 sizeof(void *) * allocated_entries
);
600 /* Zero out the new range */
601 memset(entries
+ pt
->allocated_entries
* sizeof(void *), 0,
602 (allocated_entries
- pt
->allocated_entries
) * sizeof(void *));
604 pt
->allocated_entries
= allocated_entries
;
605 pt
->entries
= entries
;
608 pt
->entries
[id
] = ptr
;
609 if (id
>= pt
->nr_entries
)
610 pt
->nr_entries
= id
+ 1;
614 static void *ptr_table__entry(const struct ptr_table
*pt
, uint32_t id
)
616 return id
>= pt
->nr_entries
? NULL
: pt
->entries
[id
];
619 static void cu__insert_function(struct cu
*cu
, struct tag
*tag
)
621 struct function
*function
= tag__function(tag
);
622 struct rb_node
**p
= &cu
->functions
.rb_node
;
623 struct rb_node
*parent
= NULL
;
628 f
= rb_entry(parent
, struct function
, rb_node
);
629 if (function
->lexblock
.ip
.addr
< f
->lexblock
.ip
.addr
)
634 rb_link_node(&function
->rb_node
, parent
, p
);
635 rb_insert_color(&function
->rb_node
, &cu
->functions
);
638 int cu__table_add_tag(struct cu
*cu
, struct tag
*tag
, uint32_t *type_id
)
640 struct ptr_table
*pt
= &cu
->tags_table
;
642 if (tag__is_tag_type(tag
))
643 pt
= &cu
->types_table
;
644 else if (tag__is_function(tag
)) {
645 pt
= &cu
->functions_table
;
646 cu__insert_function(cu
, tag
);
649 return ptr_table__add(pt
, tag
, type_id
) ? -ENOMEM
: 0;
652 int cu__table_nullify_type_entry(struct cu
*cu
, uint32_t id
)
654 return ptr_table__add_with_id(&cu
->types_table
, NULL
, id
);
657 int cu__add_tag(struct cu
*cu
, struct tag
*tag
, uint32_t *id
)
659 int err
= cu__table_add_tag(cu
, tag
, id
);
662 list_add_tail(&tag
->node
, &cu
->tags
);
667 int cu__table_add_tag_with_id(struct cu
*cu
, struct tag
*tag
, uint32_t id
)
669 struct ptr_table
*pt
= &cu
->tags_table
;
671 if (tag__is_tag_type(tag
)) {
672 pt
= &cu
->types_table
;
673 } else if (tag__is_function(tag
)) {
674 pt
= &cu
->functions_table
;
675 cu__insert_function(cu
, tag
);
678 return ptr_table__add_with_id(pt
, tag
, id
);
681 int cu__add_tag_with_id(struct cu
*cu
, struct tag
*tag
, uint32_t id
)
683 int err
= cu__table_add_tag_with_id(cu
, tag
, id
);
686 list_add_tail(&tag
->node
, &cu
->tags
);
691 int cus__fprintf_ptr_table_stats_csv_header(FILE *fp
)
693 return fprintf(fp
, "# cu,tags,allocated_tags,types,allocated_types,functions,allocated_functions\n");
696 int cu__fprintf_ptr_table_stats_csv(struct cu
*cu
, FILE *fp
)
698 int printed
= fprintf(fp
, "%s,%u,%u,%u,%u,%u,%u\n", cu
->name
,
699 cu
->tags_table
.nr_entries
, cu
->tags_table
.allocated_entries
,
700 cu
->types_table
.nr_entries
, cu
->types_table
.allocated_entries
,
701 cu
->functions_table
.nr_entries
, cu
->functions_table
.allocated_entries
);
706 struct cu
*cu__new(const char *name
, uint8_t addr_size
,
707 const unsigned char *build_id
, int build_id_len
,
708 const char *filename
, bool use_obstack
)
710 struct cu
*cu
= zalloc(sizeof(*cu
) + build_id_len
);
715 cu
->use_obstack
= use_obstack
;
717 obstack_init(&cu
->obstack
);
719 cu
->name
= strdup(name
);
720 if (cu
->name
== NULL
)
723 cu
->filename
= strdup(filename
);
724 if (cu
->filename
== NULL
)
727 ptr_table__init(&cu
->tags_table
);
728 ptr_table__init(&cu
->types_table
);
729 ptr_table__init(&cu
->functions_table
);
731 * the first entry is historically associated with void,
732 * so make sure we don't use it
734 if (ptr_table__add(&cu
->types_table
, NULL
, &void_id
) < 0)
735 goto out_free_filename
;
737 cu
->functions
= RB_ROOT
;
740 INIT_LIST_HEAD(&cu
->tags
);
741 INIT_LIST_HEAD(&cu
->tool_list
);
743 cu
->addr_size
= addr_size
;
744 cu
->extra_dbg_info
= 0;
746 cu
->state
= CU__UNPROCESSED
;
748 cu
->nr_inline_expansions
= 0;
749 cu
->size_inline_expansions
= 0;
750 cu
->nr_structures_changed
= 0;
751 cu
->nr_functions_changed
= 0;
752 cu
->max_len_changed_item
= 0;
753 cu
->function_bytes_added
= 0;
754 cu
->function_bytes_removed
= 0;
755 cu
->build_id_len
= build_id_len
;
756 if (build_id_len
> 0)
757 memcpy(cu
->build_id
, build_id
, build_id_len
);
764 zfree(&cu
->filename
);
772 void cu__delete(struct cu
*cu
)
777 ptr_table__exit(&cu
->tags_table
);
778 ptr_table__exit(&cu
->types_table
);
779 ptr_table__exit(&cu
->functions_table
);
780 if (cu
->dfops
&& cu
->dfops
->cu__delete
)
781 cu
->dfops
->cu__delete(cu
);
784 obstack_free(&cu
->obstack
, NULL
);
786 zfree(&cu
->filename
);
791 bool cu__same_build_id(const struct cu
*cu
, const struct cu
*other
)
793 return cu
->build_id_len
!= 0 &&
794 cu
->build_id_len
== other
->build_id_len
&&
795 memcmp(cu
->build_id
, other
->build_id
, cu
->build_id_len
) == 0;
798 struct tag
*cu__function(const struct cu
*cu
, const uint32_t id
)
800 return cu
? ptr_table__entry(&cu
->functions_table
, id
) : NULL
;
803 struct tag
*cu__tag(const struct cu
*cu
, const uint32_t id
)
805 return cu
? ptr_table__entry(&cu
->tags_table
, id
) : NULL
;
808 struct tag
*cu__type(const struct cu
*cu
, const type_id_t id
)
810 return cu
? ptr_table__entry(&cu
->types_table
, id
) : NULL
;
813 struct tag
*cu__find_first_typedef_of_type(const struct cu
*cu
,
814 const type_id_t type
)
819 if (cu
== NULL
|| type
== 0)
822 cu__for_each_type(cu
, id
, pos
)
823 if (tag__is_typedef(pos
) && pos
->type
== type
)
829 struct tag
*cu__find_base_type_by_name(const struct cu
*cu
,
830 const char *name
, type_id_t
*idp
)
835 if (cu
== NULL
|| name
== NULL
)
838 cu__for_each_type(cu
, id
, pos
) {
839 if (pos
->tag
!= DW_TAG_base_type
)
842 const struct base_type
*bt
= tag__base_type(pos
);
844 const char *bname
= base_type__name(bt
, bf
, sizeof(bf
));
845 if (!bname
|| strcmp(bname
, name
) != 0)
856 struct tag
*cu__find_base_type_by_name_and_size(const struct cu
*cu
, const char *name
,
857 uint16_t bit_size
, type_id_t
*idp
)
865 cu__for_each_type(cu
, id
, pos
) {
866 if (pos
->tag
== DW_TAG_base_type
) {
867 const struct base_type
*bt
= tag__base_type(pos
);
870 if (bt
->bit_size
== bit_size
&&
871 strcmp(base_type__name(bt
, bf
, sizeof(bf
)), name
) == 0) {
882 struct tag
*cu__find_enumeration_by_name_and_size(const struct cu
*cu
, const char *name
,
883 uint16_t bit_size
, type_id_t
*idp
)
891 cu__for_each_type(cu
, id
, pos
) {
892 if (pos
->tag
== DW_TAG_enumeration_type
) {
893 const struct type
*t
= tag__type(pos
);
895 if (t
->size
== bit_size
&&
896 strcmp(type__name(t
), name
) == 0) {
907 struct tag
*cu__find_enumeration_by_name(const struct cu
*cu
, const char *name
, type_id_t
*idp
)
915 cu__for_each_type(cu
, id
, pos
) {
916 if (pos
->tag
== DW_TAG_enumeration_type
) {
917 const struct type
*type
= tag__type(pos
);
918 const char *tname
= type__name(type
);
920 if (tname
&& strcmp(tname
, name
) == 0) {
931 struct tag
*cu__find_type_by_name(const struct cu
*cu
, const char *name
, const int include_decls
, type_id_t
*idp
)
933 if (cu
== NULL
|| name
== NULL
)
938 cu__for_each_type(cu
, id
, pos
) {
941 if (!tag__is_type(pos
))
944 type
= tag__type(pos
);
945 const char *tname
= type__name(type
);
946 if (tname
&& strcmp(tname
, name
) == 0) {
947 if (!type
->declaration
)
962 struct tag
*cus__find_type_by_name(struct cus
*cus
, struct cu
**cu
, const char *name
,
963 const int include_decls
, type_id_t
*id
)
966 struct tag
*tag
= NULL
;
970 list_for_each_entry(pos
, &cus
->cus
, node
) {
971 tag
= cu__find_type_by_name(pos
, name
, include_decls
, id
);
984 static struct tag
*__cu__find_struct_by_name(const struct cu
*cu
, const char *name
,
985 const int include_decls
, bool unions
, type_id_t
*idp
)
987 if (cu
== NULL
|| name
== NULL
)
992 cu__for_each_type(cu
, id
, pos
) {
995 if (!(tag__is_struct(pos
) || (unions
&& tag__is_union(pos
))))
998 type
= tag__type(pos
);
999 const char *tname
= type__name(type
);
1000 if (tname
&& strcmp(tname
, name
) == 0) {
1001 if (!type
->declaration
)
1016 struct tag
*cu__find_struct_by_name(const struct cu
*cu
, const char *name
,
1017 const int include_decls
, type_id_t
*idp
)
1019 return __cu__find_struct_by_name(cu
, name
, include_decls
, false, idp
);
1022 struct tag
*cu__find_struct_or_union_by_name(const struct cu
*cu
, const char *name
,
1023 const int include_decls
, type_id_t
*idp
)
1025 return __cu__find_struct_by_name(cu
, name
, include_decls
, true, idp
);
1028 static struct tag
*__cus__find_struct_by_name(struct cus
*cus
, struct cu
**cu
, const char *name
,
1029 const int include_decls
, bool unions
, type_id_t
*id
)
1031 struct tag
*tag
= NULL
;
1036 list_for_each_entry(pos
, &cus
->cus
, node
) {
1037 struct tag
*tag
= __cu__find_struct_by_name(pos
, name
, include_decls
, unions
, id
);
1050 struct tag
*cus__find_struct_by_name(struct cus
*cus
, struct cu
**cu
, const char *name
,
1051 const int include_decls
, type_id_t
*idp
)
1053 return __cus__find_struct_by_name(cus
, cu
, name
, include_decls
, false, idp
);
1056 struct tag
*cus__find_struct_or_union_by_name(struct cus
*cus
, struct cu
**cu
, const char *name
,
1057 const int include_decls
, type_id_t
*idp
)
1059 return __cus__find_struct_by_name(cus
, cu
, name
, include_decls
, true, idp
);
1062 struct function
*cu__find_function_at_addr(const struct cu
*cu
,
1070 n
= cu
->functions
.rb_node
;
1073 struct function
*f
= rb_entry(n
, struct function
, rb_node
);
1075 if (addr
< f
->lexblock
.ip
.addr
)
1077 else if (addr
>= f
->lexblock
.ip
.addr
+ f
->lexblock
.size
)
1087 struct function
*cus__find_function_at_addr(struct cus
*cus
, uint64_t addr
, struct cu
**cu
)
1089 struct function
*f
= NULL
;
1094 list_for_each_entry(pos
, &cus
->cus
, node
) {
1095 f
= cu__find_function_at_addr(pos
, addr
);
1109 static struct cu
*__cus__find_cu_by_name(struct cus
*cus
, const char *name
)
1113 list_for_each_entry(pos
, &cus
->cus
, node
)
1114 if (pos
->name
&& strcmp(pos
->name
, name
) == 0)
1122 struct cu
*cus__find_cu_by_name(struct cus
*cus
, const char *name
)
1128 pos
= __cus__find_cu_by_name(cus
, name
);
1135 struct cu
*cus__find_pair(struct cus
*cus
, const char *name
)
1141 if (cus
->nr_entries
== 1)
1142 cu
= list_first_entry(&cus
->cus
, struct cu
, node
);
1144 cu
= __cus__find_cu_by_name(cus
, name
);
1151 struct tag
*cu__find_function_by_name(const struct cu
*cu
, const char *name
)
1153 if (cu
== NULL
|| name
== NULL
)
1157 struct function
*pos
;
1158 cu__for_each_function(cu
, id
, pos
) {
1159 const char *fname
= function__name(pos
);
1160 if (fname
&& strcmp(fname
, name
) == 0)
1161 return function__tag(pos
);
1167 static size_t array_type__nr_entries(const struct array_type
*at
)
1170 size_t nr_entries
= 1;
1172 for (i
= 0; i
< at
->dimensions
; ++i
)
1173 nr_entries
*= at
->nr_entries
[i
];
1178 size_t tag__size(const struct tag
*tag
, const struct cu
*cu
)
1183 case DW_TAG_string_type
:
1184 return tag__string_type(tag
)->nr_entries
;
1185 case DW_TAG_member
: {
1186 struct class_member
*member
= tag__class_member(tag
);
1187 if (member
->is_static
)
1189 /* Is it cached already? */
1190 size
= member
->byte_size
;
1195 case DW_TAG_pointer_type
:
1196 case DW_TAG_reference_type
: return cu
->addr_size
;
1197 case DW_TAG_base_type
: return base_type__size(tag
);
1198 case DW_TAG_enumeration_type
: return tag__type(tag
)->size
/ 8;
1199 case DW_TAG_subroutine_type
: return tag__ftype(tag
)->byte_size
?: cu
->addr_size
;
1202 if (tag
->type
== 0) { /* struct class: unions, structs */
1203 struct type
*type
= tag__type(tag
);
1205 /* empty base optimization trick */
1206 if (type
->size
== 1 && type
->nr_members
== 0)
1209 size
= tag__type(tag
)->size
;
1211 const struct tag
*type
= cu__type(cu
, tag
->type
);
1214 tag__id_not_found_fprintf(stderr
, tag
->type
);
1216 } else if (tag__has_type_loop(tag
, type
, NULL
, 0, NULL
))
1218 size
= tag__size(type
, cu
);
1221 if (tag
->tag
== DW_TAG_array_type
)
1222 return size
* array_type__nr_entries(tag__array_type(tag
));
1227 const char *variable__name(const struct variable
*var
)
1232 const char *variable__type_name(const struct variable
*var
,
1233 const struct cu
*cu
,
1234 char *bf
, size_t len
)
1236 const struct tag
*tag
= cu__type(cu
, var
->ip
.tag
.type
);
1237 return tag
!= NULL
? tag__name(tag
, cu
, bf
, len
, NULL
) : NULL
;
1240 void class_member__delete(struct class_member
*member
)
1245 static struct class_member
*class_member__clone(const struct class_member
*from
)
1247 struct class_member
*member
= malloc(sizeof(*member
));
1250 memcpy(member
, from
, sizeof(*member
));
1255 static void type__delete_class_members(struct type
*type
)
1257 struct class_member
*pos
, *next
;
1259 type__for_each_tag_safe_reverse(type
, pos
, next
) {
1260 list_del_init(&pos
->tag
.node
);
1261 class_member__delete(pos
);
1265 void class__delete(struct class *class)
1270 type__delete_class_members(&class->type
);
1274 void type__delete(struct type
*type
)
1279 type__delete_class_members(type
);
1281 if (type
->suffix_disambiguation
)
1282 zfree(&type
->namespace.name
);
1287 static void enumerator__delete(struct enumerator
*enumerator
)
1292 void enumeration__delete(struct type
*type
)
1294 struct enumerator
*pos
, *n
;
1299 type__for_each_enumerator_safe_reverse(type
, pos
, n
) {
1300 list_del_init(&pos
->tag
.node
);
1301 enumerator__delete(pos
);
1304 if (type
->suffix_disambiguation
)
1305 zfree(&type
->namespace.name
);
1310 void class__add_vtable_entry(struct class *class, struct function
*vtable_entry
)
1312 ++class->nr_vtable_entries
;
1313 list_add_tail(&vtable_entry
->vtable_node
, &class->vtable
);
1316 void namespace__add_tag(struct namespace *space
, struct tag
*tag
)
1319 list_add_tail(&tag
->node
, &space
->tags
);
1322 void type__add_member(struct type
*type
, struct class_member
*member
)
1324 if (member
->is_static
)
1325 ++type
->nr_static_members
;
1328 namespace__add_tag(&type
->namespace, &member
->tag
);
1331 struct class_member
*type__last_member(struct type
*type
)
1333 struct class_member
*pos
;
1335 list_for_each_entry_reverse(pos
, &type
->namespace.tags
, tag
.node
)
1336 if (pos
->tag
.tag
== DW_TAG_member
)
1341 static int type__clone_members(struct type
*type
, const struct type
*from
)
1343 struct class_member
*pos
;
1345 type
->nr_members
= type
->nr_static_members
= 0;
1346 INIT_LIST_HEAD(&type
->namespace.tags
);
1348 type__for_each_member(from
, pos
) {
1349 struct class_member
*clone
= class_member__clone(pos
);
1353 type__add_member(type
, clone
);
1359 struct class *class__clone(const struct class *from
, const char *new_class_name
)
1361 struct class *class = malloc(sizeof(*class));
1363 if (class != NULL
) {
1364 memcpy(class, from
, sizeof(*class));
1365 if (new_class_name
!= NULL
) {
1366 class->type
.namespace.name
= strdup(new_class_name
);
1367 if (class->type
.namespace.name
== NULL
) {
1372 if (type__clone_members(&class->type
, &from
->type
) != 0) {
1373 class__delete(class);
1381 void enumeration__add(struct type
*type
, struct enumerator
*enumerator
)
1384 namespace__add_tag(&type
->namespace, &enumerator
->tag
);
1387 void lexblock__add_lexblock(struct lexblock
*block
, struct lexblock
*child
)
1389 ++block
->nr_lexblocks
;
1390 list_add_tail(&child
->ip
.tag
.node
, &block
->tags
);
1393 const char *function__name(struct function
*func
)
1398 static void parameter__delete(struct parameter
*parm
)
1403 void ftype__delete(struct ftype
*type
)
1405 struct parameter
*pos
, *n
;
1410 ftype__for_each_parameter_safe_reverse(type
, pos
, n
) {
1411 list_del_init(&pos
->tag
.node
);
1412 parameter__delete(pos
);
1417 void function__delete(struct function
*func
)
1422 lexblock__delete_tags(&func
->lexblock
.ip
.tag
);
1423 ftype__delete(&func
->proto
);
1426 int ftype__has_parm_of_type(const struct ftype
*ftype
, const type_id_t target
,
1427 const struct cu
*cu
)
1429 struct parameter
*pos
;
1431 if (ftype
->tag
.tag
== DW_TAG_subprogram
) {
1432 struct function
*func
= (struct function
*)ftype
;
1435 ftype
= tag__ftype(cu__type(cu
, ftype
->tag
.type
));
1438 ftype__for_each_parameter(ftype
, pos
) {
1439 struct tag
*type
= cu__type(cu
, pos
->tag
.type
);
1441 if (type
!= NULL
&& tag__is_pointer(type
)) {
1442 if (type
->type
== target
)
1449 void ftype__add_parameter(struct ftype
*ftype
, struct parameter
*parm
)
1452 list_add_tail(&parm
->tag
.node
, &ftype
->parms
);
1455 void lexblock__add_tag(struct lexblock
*block
, struct tag
*tag
)
1457 list_add_tail(&tag
->node
, &block
->tags
);
1460 void lexblock__add_inline_expansion(struct lexblock
*block
,
1461 struct inline_expansion
*exp
)
1463 ++block
->nr_inline_expansions
;
1464 block
->size_inline_expansions
+= exp
->size
;
1465 lexblock__add_tag(block
, &exp
->ip
.tag
);
1468 void lexblock__add_variable(struct lexblock
*block
, struct variable
*var
)
1470 ++block
->nr_variables
;
1471 lexblock__add_tag(block
, &var
->ip
.tag
);
1474 void lexblock__add_label(struct lexblock
*block
, struct label
*label
)
1477 lexblock__add_tag(block
, &label
->ip
.tag
);
1480 const struct class_member
*class__find_bit_hole(const struct class *class,
1481 const struct class_member
*trailer
,
1482 const uint16_t bit_hole_size
)
1484 struct class_member
*pos
;
1485 const size_t byte_hole_size
= bit_hole_size
/ 8;
1487 type__for_each_data_member(&class->type
, pos
)
1490 else if (pos
->hole
>= byte_hole_size
||
1491 pos
->bit_hole
>= bit_hole_size
)
1497 void class__find_holes(struct class *class)
1499 const struct type
*ctype
= &class->type
;
1500 struct class_member
*pos
, *last
= NULL
;
1501 uint32_t cur_bitfield_end
= ctype
->size
* 8, cur_bitfield_size
= 0;
1502 int bit_holes
= 0, byte_holes
= 0;
1503 uint32_t bit_start
, bit_end
, last_seen_bit
= 0;
1504 bool in_bitfield
= false;
1506 if (!tag__is_struct(class__tag(class)))
1509 if (class->holes_searched
)
1512 class->nr_holes
= 0;
1513 class->nr_bit_holes
= 0;
1515 type__for_each_member(ctype
, pos
) {
1516 /* XXX for now just skip these */
1517 if (pos
->tag
.tag
== DW_TAG_inheritance
&&
1518 pos
->virtuality
== DW_VIRTUALITY_virtual
)
1527 bit_start
= pos
->bit_offset
;
1528 if (pos
->bitfield_size
) {
1529 bit_end
= bit_start
+ pos
->bitfield_size
;
1531 bit_end
= bit_start
+ pos
->byte_size
* 8;
1537 /* check if we have some trailing bitfield bits left */
1538 int bitfield_end
= min(bit_start
, cur_bitfield_end
);
1539 bit_holes
= bitfield_end
- last_seen_bit
;
1540 last_seen_bit
= bitfield_end
;
1542 if (pos
->bitfield_size
) {
1543 uint32_t aligned_start
= pos
->byte_offset
* 8;
1544 /* we can have some alignment byte padding left,
1545 * but we need to be careful about bitfield spanning
1546 * multiple aligned boundaries */
1547 if (last_seen_bit
< aligned_start
&& aligned_start
<= bit_start
) {
1548 byte_holes
= pos
->byte_offset
- last_seen_bit
/ 8;
1549 last_seen_bit
= aligned_start
;
1551 bit_holes
+= bit_start
- last_seen_bit
;
1553 byte_holes
= bit_start
/8 - last_seen_bit
/8;
1555 last_seen_bit
= bit_end
;
1557 if (pos
->bitfield_size
) {
1559 /* if it's a new bitfield set or same, but with
1560 * bigger-sized type, readjust size and end bit */
1561 if (bit_end
> cur_bitfield_end
|| pos
->bit_size
> cur_bitfield_size
) {
1562 cur_bitfield_size
= pos
->bit_size
;
1563 cur_bitfield_end
= pos
->byte_offset
* 8 + cur_bitfield_size
;
1565 * if current bitfield "borrowed" bits from
1566 * previous bitfield, it will have byte_offset
1567 * of previous bitfield's backing integral
1568 * type, but its end bit will be in a new
1569 * bitfield "area", so we need to adjust
1570 * bitfield end appropriately
1572 if (bit_end
> cur_bitfield_end
) {
1573 cur_bitfield_end
+= cur_bitfield_size
;
1577 in_bitfield
= false;
1578 cur_bitfield_size
= 0;
1579 cur_bitfield_end
= bit_end
;
1583 last
->hole
= byte_holes
;
1584 last
->bit_hole
= bit_holes
;
1586 class->pre_hole
= byte_holes
;
1587 class->pre_bit_hole
= bit_holes
;
1590 class->nr_bit_holes
++;
1598 int bitfield_end
= min(ctype
->size
* 8, cur_bitfield_end
);
1599 class->bit_padding
= bitfield_end
- last_seen_bit
;
1600 last_seen_bit
= bitfield_end
;
1602 class->bit_padding
= 0;
1604 class->padding
= ctype
->size
- last_seen_bit
/ 8;
1606 class->holes_searched
= true;
1609 static size_t type__natural_alignment(struct type
*type
, const struct cu
*cu
);
1611 size_t tag__natural_alignment(struct tag
*tag
, const struct cu
*cu
)
1613 size_t natural_alignment
= 1;
1615 if (tag
== NULL
) // Maybe its a non supported type, like DW_TAG_subrange_type, ADA stuff
1616 return natural_alignment
;
1618 if (tag__is_pointer(tag
)) {
1619 natural_alignment
= cu
->addr_size
;
1620 } else if (tag
->tag
== DW_TAG_base_type
) {
1621 natural_alignment
= base_type__size(tag
);
1622 } else if (tag__is_enumeration(tag
)) {
1623 natural_alignment
= tag__type(tag
)->size
/ 8;
1624 } else if (tag__is_struct(tag
) || tag__is_union(tag
)) {
1625 natural_alignment
= type__natural_alignment(tag__type(tag
), cu
);
1626 } else if (tag
->tag
== DW_TAG_array_type
) {
1627 tag
= tag__strip_typedefs_and_modifiers(tag
, cu
);
1628 if (tag
!= NULL
) // Maybe its a non supported type, like DW_TAG_subrange_type, ADA stuff
1629 natural_alignment
= tag__natural_alignment(tag
, cu
);
1633 * Cope with zero sized types, like:
1635 * struct u64_stats_sync {
1636 * #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1642 return natural_alignment
?: 1;
1645 static size_t type__natural_alignment(struct type
*type
, const struct cu
*cu
)
1647 struct class_member
*member
;
1649 if (type
->natural_alignment
!= 0)
1650 return type
->natural_alignment
;
1652 type__for_each_member(type
, member
) {
1653 /* XXX for now just skip these */
1654 if (member
->tag
.tag
== DW_TAG_inheritance
&&
1655 member
->virtuality
== DW_VIRTUALITY_virtual
)
1657 if (member
->is_static
) continue;
1659 struct tag
*member_type
= tag__strip_typedefs_and_modifiers(&member
->tag
, cu
);
1661 if (member_type
== NULL
) // Maybe its a DW_TAG_subrange_type, ADA stuff still not supported
1664 size_t member_natural_alignment
= tag__natural_alignment(member_type
, cu
);
1666 if (type
->natural_alignment
< member_natural_alignment
)
1667 type
->natural_alignment
= member_natural_alignment
;
1670 return type
->natural_alignment
;
1674 * Sometimes the only indication that a struct is __packed__ is for it to
1675 * appear embedded in another and at an offset that is not natural for it,
1676 * so, in !__packed__ parked struct, check for that and mark the types of
1677 * members at unnatural alignments.
1679 void type__check_structs_at_unnatural_alignments(struct type
*type
, const struct cu
*cu
)
1681 struct class_member
*member
;
1683 type__for_each_member(type
, member
) {
1684 struct tag
*member_type
= tag__strip_typedefs_and_modifiers(&member
->tag
, cu
);
1686 if (member_type
== NULL
) {
1687 // just be conservative and ignore
1688 // Found first when a FORTRAN95 DWARF file was processed
1689 // and the DW_TAG_string_type wasn't yet supported
1693 if (!tag__is_struct(member_type
))
1696 size_t natural_alignment
= tag__natural_alignment(member_type
, cu
);
1698 /* Would this break the natural alignment */
1699 if ((member
->byte_offset
% natural_alignment
) != 0) {
1700 struct class *cls
= tag__class(member_type
);
1702 cls
->is_packed
= true;
1703 cls
->type
.packed_attributes_inferred
= 1;
1708 bool class__infer_packed_attributes(struct class *cls
, const struct cu
*cu
)
1710 struct type
*ctype
= &cls
->type
;
1711 struct class_member
*pos
;
1712 uint16_t max_natural_alignment
= 1;
1714 if (!tag__is_struct(class__tag(cls
)))
1717 if (ctype
->packed_attributes_inferred
)
1718 return cls
->is_packed
;
1720 class__find_holes(cls
);
1722 if (cls
->padding
!= 0 || cls
->nr_holes
!= 0) {
1723 type__check_structs_at_unnatural_alignments(ctype
, cu
);
1724 cls
->is_packed
= false;
1728 type__for_each_member(ctype
, pos
) {
1729 /* XXX for now just skip these */
1730 if (pos
->tag
.tag
== DW_TAG_inheritance
&&
1731 pos
->virtuality
== DW_VIRTUALITY_virtual
)
1737 struct tag
*member_type
= tag__strip_typedefs_and_modifiers(&pos
->tag
, cu
);
1738 size_t natural_alignment
= tag__natural_alignment(member_type
, cu
);
1740 /* Always aligned: */
1741 if (natural_alignment
== sizeof(char))
1744 if (max_natural_alignment
< natural_alignment
)
1745 max_natural_alignment
= natural_alignment
;
1747 if ((pos
->byte_offset
% natural_alignment
) == 0)
1750 cls
->is_packed
= true;
1754 if ((max_natural_alignment
!= 1 && ctype
->alignment
== 1) ||
1755 (class__size(cls
) % max_natural_alignment
) != 0)
1756 cls
->is_packed
= true;
1759 ctype
->packed_attributes_inferred
= 1;
1761 return cls
->is_packed
;
1765 * If structs embedded in unions, nameless or not, have a size which isn't
1766 * isn't a multiple of the union size, then it must be packed, even if
1767 * it has no holes nor padding, as an array of such unions would have the
1768 * natural alignments of non-multiple structs inside it broken.
1770 void union__infer_packed_attributes(struct type
*type
, const struct cu
*cu
)
1772 const uint32_t union_size
= type
->size
;
1773 struct class_member
*member
;
1775 if (type
->packed_attributes_inferred
)
1778 type__for_each_member(type
, member
) {
1779 struct tag
*member_type
= tag__strip_typedefs_and_modifiers(&member
->tag
, cu
);
1781 if (!tag__is_struct(member_type
))
1784 size_t natural_alignment
= tag__natural_alignment(member_type
, cu
);
1786 /* Would this break the natural alignment */
1787 if ((union_size
% natural_alignment
) != 0) {
1788 struct class *cls
= tag__class(member_type
);
1790 cls
->is_packed
= true;
1791 cls
->type
.packed_attributes_inferred
= 1;
1795 type
->packed_attributes_inferred
= 1;
1798 /** class__has_hole_ge - check if class has a hole greater or equal to @size
1799 * @class - class instance
1800 * @size - hole size to check
1802 int class__has_hole_ge(const struct class *class, const uint16_t size
)
1804 struct class_member
*pos
;
1806 if (class->nr_holes
== 0)
1809 type__for_each_data_member(&class->type
, pos
)
1810 if (pos
->hole
>= size
)
1816 struct class_member
*type__find_member_by_name(const struct type
*type
, const char *name
)
1821 struct class_member
*pos
;
1822 type__for_each_data_member(type
, pos
) {
1823 const char *curr_name
= class_member__name(pos
);
1824 if (curr_name
&& strcmp(curr_name
, name
) == 0)
1831 static int strcommon(const char *a
, const char *b
)
1835 while (*a
!= '\0' && *a
== *b
) {
1844 static void enumeration__calc_prefix(struct type
*enumeration
)
1846 if (enumeration
->member_prefix
)
1849 const char *previous_name
= NULL
, *curr_name
= "";
1850 int common_part
= INT32_MAX
;
1851 struct enumerator
*entry
;
1853 type__for_each_enumerator(enumeration
, entry
) {
1854 const char *curr_name
= enumerator__name(entry
);
1856 if (previous_name
) {
1857 int curr_common_part
= strcommon(curr_name
, previous_name
);
1858 if (common_part
> curr_common_part
)
1859 common_part
= curr_common_part
;
1863 previous_name
= curr_name
;
1866 enumeration
->member_prefix
= NULL
;
1867 enumeration
->member_prefix_len
= 0;
1869 if (common_part
!= INT32_MAX
) {
1870 enumeration
->member_prefix
= strndup(curr_name
, common_part
);
1871 if (enumeration
->member_prefix
!= NULL
)
1872 enumeration
->member_prefix_len
= common_part
;
1876 void enumerations__calc_prefix(struct list_head
*enumerations
)
1878 struct tag_cu_node
*pos
;
1880 list_for_each_entry(pos
, enumerations
, node
)
1881 enumeration__calc_prefix(tag__type(pos
->tc
.tag
));
1884 uint32_t type__nr_members_of_type(const struct type
*type
, const type_id_t type_id
)
1886 struct class_member
*pos
;
1887 uint32_t nr_members_of_type
= 0;
1889 type__for_each_member(type
, pos
)
1890 if (pos
->tag
.type
== type_id
)
1891 ++nr_members_of_type
;
1893 return nr_members_of_type
;
1896 static void lexblock__account_inline_expansions(struct lexblock
*block
,
1897 const struct cu
*cu
)
1899 struct tag
*pos
, *type
;
1901 if (block
->nr_inline_expansions
== 0)
1904 list_for_each_entry(pos
, &block
->tags
, node
) {
1905 if (pos
->tag
== DW_TAG_lexical_block
) {
1906 lexblock__account_inline_expansions(tag__lexblock(pos
),
1909 } else if (pos
->tag
!= DW_TAG_inlined_subroutine
)
1912 type
= cu__function(cu
, pos
->type
);
1914 struct function
*ftype
= tag__function(type
);
1916 ftype
->cu_total_nr_inline_expansions
++;
1917 ftype
->cu_total_size_inline_expansions
+=
1918 tag__inline_expansion(pos
)->size
;
1924 void cu__account_inline_expansions(struct cu
*cu
)
1927 struct function
*fpos
;
1929 list_for_each_entry(pos
, &cu
->tags
, node
) {
1930 if (!tag__is_function(pos
))
1932 fpos
= tag__function(pos
);
1933 lexblock__account_inline_expansions(&fpos
->lexblock
, cu
);
1934 cu
->nr_inline_expansions
+= fpos
->lexblock
.nr_inline_expansions
;
1935 cu
->size_inline_expansions
+= fpos
->lexblock
.size_inline_expansions
;
1939 static int list__for_all_tags(struct list_head
*list
, struct cu
*cu
,
1940 int (*iterator
)(struct tag
*tag
,
1941 struct cu
*cu
, void *cookie
),
1944 struct tag
*pos
, *n
;
1946 if (list_empty(list
) || !list
->next
)
1949 list_for_each_entry_safe_reverse(pos
, n
, list
, node
) {
1950 if (tag__has_namespace(pos
)) {
1951 struct namespace *space
= tag__namespace(pos
);
1954 * See comment in type__for_each_enumerator, the
1955 * enumerators (enum entries) are shared, but the
1956 * enumeration tag must be deleted.
1958 if (!space
->shared_tags
&&
1959 list__for_all_tags(&space
->tags
, cu
,
1963 * vtable functions are already in the class tags list
1965 } else if (tag__is_function(pos
)) {
1966 if (list__for_all_tags(&tag__ftype(pos
)->parms
,
1967 cu
, iterator
, cookie
))
1969 if (list__for_all_tags(&tag__function(pos
)->lexblock
.tags
,
1970 cu
, iterator
, cookie
))
1972 } else if (pos
->tag
== DW_TAG_subroutine_type
) {
1973 if (list__for_all_tags(&tag__ftype(pos
)->parms
,
1974 cu
, iterator
, cookie
))
1976 } else if (pos
->tag
== DW_TAG_lexical_block
) {
1977 if (list__for_all_tags(&tag__lexblock(pos
)->tags
,
1978 cu
, iterator
, cookie
))
1982 if (iterator(pos
, cu
, cookie
))
1988 int cu__for_all_tags(struct cu
*cu
,
1989 int (*iterator
)(struct tag
*tag
,
1990 struct cu
*cu
, void *cookie
),
1993 return list__for_all_tags(&cu
->tags
, cu
, iterator
, cookie
);
1996 void cus__for_each_cu(struct cus
*cus
,
1997 int (*iterator
)(struct cu
*cu
, void *cookie
),
1999 struct cu
*(*filter
)(struct cu
*cu
))
2005 list_for_each_entry(pos
, &cus
->cus
, node
) {
2006 struct cu
*cu
= pos
;
2007 if (filter
!= NULL
) {
2012 if (iterator(cu
, cookie
))
2019 int cus__load_dir(struct cus
*cus
, struct conf_load
*conf
,
2020 const char *dirname
, const char *filename_mask
,
2021 const int recursive
)
2023 struct dirent
*entry
;
2025 DIR *dir
= opendir(dirname
);
2031 while ((entry
= readdir(dir
)) != NULL
) {
2032 char pathname
[PATH_MAX
];
2035 if (strcmp(entry
->d_name
, ".") == 0 ||
2036 strcmp(entry
->d_name
, "..") == 0)
2039 snprintf(pathname
, sizeof(pathname
), "%.*s/%s",
2040 (int)(sizeof(pathname
) - sizeof(entry
->d_name
) - 1), dirname
, entry
->d_name
);
2042 err
= lstat(pathname
, &st
);
2046 if (S_ISDIR(st
.st_mode
)) {
2050 err
= cus__load_dir(cus
, conf
, pathname
,
2051 filename_mask
, recursive
);
2054 } else if (fnmatch(filename_mask
, entry
->d_name
, 0) == 0) {
2055 err
= cus__load_file(cus
, conf
, pathname
);
2069 * This should really do demand loading of DSOs, STABS anyone? 8-)
2071 extern struct debug_fmt_ops dwarf__ops
, ctf__ops
, btf__ops
;
2073 static struct debug_fmt_ops
*debug_fmt_table
[] = {
2080 static int debugging_formats__loader(const char *name
)
2083 while (debug_fmt_table
[i
] != NULL
) {
2084 if (strcmp(debug_fmt_table
[i
]->name
, name
) == 0)
2091 int cus__load_file(struct cus
*cus
, struct conf_load
*conf
,
2092 const char *filename
)
2097 if (conf
&& conf
->format_path
!= NULL
) {
2098 char *fpath
= strdup(conf
->format_path
);
2103 char *sep
= strchr(fp
, ',');
2109 loader
= debugging_formats__loader(fp
);
2113 if (conf
->conf_fprintf
)
2114 conf
->conf_fprintf
->has_alignment_info
= debug_fmt_table
[loader
]->has_alignment_info
;
2117 if (debug_fmt_table
[loader
]->load_file(cus
, conf
,
2131 while (debug_fmt_table
[i
] != NULL
) {
2132 if (conf
&& conf
->conf_fprintf
)
2133 conf
->conf_fprintf
->has_alignment_info
= debug_fmt_table
[i
]->has_alignment_info
;
2134 if (debug_fmt_table
[i
]->load_file(cus
, conf
, filename
) == 0)
2142 #define BUILD_ID_SIZE 20
2143 #define SBUILD_ID_SIZE (BUILD_ID_SIZE * 2 + 1)
2145 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
2147 #define NT_GNU_BUILD_ID 3
2150 #define min(x, y) ({ \
2151 typeof(x) _min1 = (x); \
2152 typeof(y) _min2 = (y); \
2153 (void) (&_min1 == &_min2); \
2154 _min1 < _min2 ? _min1 : _min2; })
2158 #define DW_LANG_C89 0x0001
2161 #define DW_LANG_C 0x0002
2163 #ifndef DW_LANG_Ada83
2164 #define DW_LANG_Ada83 0x0003
2166 #ifndef DW_LANG_C_plus_plus
2167 #define DW_LANG_C_plus_plus 0x0004
2169 #ifndef DW_LANG_Cobol74
2170 #define DW_LANG_Cobol74 0x0005
2172 #ifndef DW_LANG_Cobol85
2173 #define DW_LANG_Cobol85 0x0006
2175 #ifndef DW_LANG_Fortran77
2176 #define DW_LANG_Fortran77 0x0007
2178 #ifndef DW_LANG_Fortran90
2179 #define DW_LANG_Fortran90 0x0008
2181 #ifndef DW_LANG_Pascal83
2182 #define DW_LANG_Pascal83 0x0009
2184 #ifndef DW_LANG_Modula2
2185 #define DW_LANG_Modula2 0x000a
2187 #ifndef DW_LANG_Java
2188 #define DW_LANG_Java 0x000b
2191 #define DW_LANG_C99 0x000c
2193 #ifndef DW_LANG_Ada95
2194 #define DW_LANG_Ada95 0x000d
2196 #ifndef DW_LANG_Fortran95
2197 #define DW_LANG_Fortran95 0x000e
2200 #define DW_LANG_PLI 0x000f
2202 #ifndef DW_LANG_ObjC
2203 #define DW_LANG_ObjC 0x0010
2205 #ifndef DW_LANG_ObjC_plus_plus
2206 #define DW_LANG_ObjC_plus_plus 0x0011
2209 #define DW_LANG_UPC 0x0012
2212 #define DW_LANG_D 0x0013
2214 #ifndef DW_LANG_Python
2215 #define DW_LANG_Python 0x0014
2217 #ifndef DW_LANG_OpenCL
2218 #define DW_LANG_OpenCL 0x0015
2221 #define DW_LANG_Go 0x0016
2223 #ifndef DW_LANG_Modula3
2224 #define DW_LANG_Modula3 0x0017
2226 #ifndef DW_LANG_Haskell
2227 #define DW_LANG_Haskell 0x0018
2229 #ifndef DW_LANG_C_plus_plus_03
2230 #define DW_LANG_C_plus_plus_03 0x0019
2232 #ifndef DW_LANG_C_plus_plus_11
2233 #define DW_LANG_C_plus_plus_11 0x001a
2235 #ifndef DW_LANG_OCaml
2236 #define DW_LANG_OCaml 0x001b
2238 #ifndef DW_LANG_Rust
2239 #define DW_LANG_Rust 0x001c
2242 #define DW_LANG_C11 0x001d
2244 #ifndef DW_LANG_Swift
2245 #define DW_LANG_Swift 0x001e
2247 #ifndef DW_LANG_Julia
2248 #define DW_LANG_Julia 0x001f
2250 #ifndef DW_LANG_Dylan
2251 #define DW_LANG_Dylan 0x0020
2253 #ifndef DW_LANG_C_plus_plus_14
2254 #define DW_LANG_C_plus_plus_14 0x0021
2256 #ifndef DW_LANG_Fortran03
2257 #define DW_LANG_Fortran03 0x0022
2259 #ifndef DW_LANG_Fortran08
2260 #define DW_LANG_Fortran08 0x0023
2262 #ifndef DW_LANG_RenderScript
2263 #define DW_LANG_RenderScript 0x0024
2265 #ifndef DW_LANG_BLISS
2266 #define DW_LANG_BLISS 0x0025
2269 int lang__str2int(const char *lang
)
2271 static const char *languages
[] = {
2272 [DW_LANG_Ada83
] = "ada83",
2273 [DW_LANG_Ada95
] = "ada95",
2274 [DW_LANG_BLISS
] = "bliss",
2275 [DW_LANG_C11
] = "c11",
2276 [DW_LANG_C89
] = "c89",
2277 [DW_LANG_C99
] = "c99",
2279 [DW_LANG_Cobol74
] = "cobol74",
2280 [DW_LANG_Cobol85
] = "cobol85",
2281 [DW_LANG_C_plus_plus_03
] = "c++03",
2282 [DW_LANG_C_plus_plus_11
] = "c++11",
2283 [DW_LANG_C_plus_plus_14
] = "c++14",
2284 [DW_LANG_C_plus_plus
] = "c++",
2286 [DW_LANG_Dylan
] = "dylan",
2287 [DW_LANG_Fortran03
] = "fortran03",
2288 [DW_LANG_Fortran08
] = "fortran08",
2289 [DW_LANG_Fortran77
] = "fortran77",
2290 [DW_LANG_Fortran90
] = "fortran90",
2291 [DW_LANG_Fortran95
] = "fortran95",
2292 [DW_LANG_Go
] = "go",
2293 [DW_LANG_Haskell
] = "haskell",
2294 [DW_LANG_Java
] = "java",
2295 [DW_LANG_Julia
] = "julia",
2296 [DW_LANG_Modula2
] = "modula2",
2297 [DW_LANG_Modula3
] = "modula3",
2298 [DW_LANG_ObjC
] = "objc",
2299 [DW_LANG_ObjC_plus_plus
] = "objc++",
2300 [DW_LANG_OCaml
] = "ocaml",
2301 [DW_LANG_OpenCL
] = "opencl",
2302 [DW_LANG_Pascal83
] = "pascal83",
2303 [DW_LANG_PLI
] = "pli",
2304 [DW_LANG_Python
] = "python",
2305 [DW_LANG_RenderScript
] = "renderscript",
2306 [DW_LANG_Rust
] = "rust",
2307 [DW_LANG_Swift
] = "swift",
2308 [DW_LANG_UPC
] = "upc",
2311 if (strcasecmp(lang
, "asm") == 0)
2312 return DW_LANG_Mips_Assembler
;
2314 // c89 is the first, bliss is the last, see /usr/include/dwarf.h
2315 for (int id
= DW_LANG_C89
; id
<= DW_LANG_BLISS
; ++id
)
2316 if (languages
[id
] && strcasecmp(lang
, languages
[id
]) == 0)
2323 static int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
2327 if (size
< BUILD_ID_SIZE
)
2330 fd
= open(filename
, O_RDONLY
);
2337 size_t namesz
, descsz
;
2339 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
2342 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
2343 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
2344 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
2345 nhdr
.n_namesz
== sizeof("GNU")) {
2346 if (read(fd
, bf
, namesz
) != (ssize_t
)namesz
)
2348 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
2349 size_t sz
= min(descsz
, size
);
2350 if (read(fd
, build_id
, sz
) == (ssize_t
)sz
) {
2351 memset(build_id
+ sz
, 0, size
- sz
);
2355 } else if (read(fd
, bf
, descsz
) != (ssize_t
)descsz
)
2358 int n
= namesz
+ descsz
;
2360 if (n
> (int)sizeof(bf
)) {
2362 fprintf(stderr
, "%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
2363 __func__
, filename
, nhdr
.n_namesz
, nhdr
.n_descsz
);
2365 if (read(fd
, bf
, n
) != n
)
2374 static int elf_read_build_id(Elf
*elf
, void *bf
, size_t size
)
2384 if (size
< BUILD_ID_SIZE
)
2388 if (ek
!= ELF_K_ELF
)
2391 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
2392 fprintf(stderr
, "%s: cannot get elf header.\n", __func__
);
2397 * Check following sections for notes:
2398 * '.note.gnu.build-id'
2400 * '.note' (VDSO specific)
2403 sec
= elf_section_by_name(elf
, &shdr
, ".note.gnu.build-id", NULL
);
2407 sec
= elf_section_by_name(elf
, &shdr
, ".notes", NULL
);
2411 sec
= elf_section_by_name(elf
, &shdr
, ".note", NULL
);
2419 data
= elf_getdata(sec
, NULL
);
2424 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
2425 GElf_Nhdr
*nhdr
= ptr
;
2426 size_t namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
2427 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
2430 ptr
+= sizeof(*nhdr
);
2433 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
2434 nhdr
->n_namesz
== sizeof("GNU")) {
2435 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
2436 size_t sz
= min(size
, descsz
);
2437 memcpy(bf
, ptr
, sz
);
2438 memset(bf
+ sz
, 0, size
- sz
);
2450 static int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
2455 if (size
< BUILD_ID_SIZE
)
2458 fd
= open(filename
, O_RDONLY
);
2462 elf
= elf_begin(fd
, ELF_C_READ
, NULL
);
2464 fprintf(stderr
, "%s: cannot read %s ELF file.\n", __func__
, filename
);
2468 err
= elf_read_build_id(elf
, bf
, size
);
2477 static int build_id__sprintf(const unsigned char *build_id
, int len
, char *bf
)
2480 const unsigned char *raw
= build_id
;
2483 for (i
= 0; i
< len
; ++i
) {
2484 sprintf(bid
, "%02x", *raw
);
2489 return (bid
- bf
) + 1;
2492 static int sysfs__sprintf_build_id(const char *root_dir
, char *sbuild_id
)
2494 char notes
[PATH_MAX
];
2495 unsigned char build_id
[BUILD_ID_SIZE
];
2501 snprintf(notes
, sizeof(notes
), "%s/sys/kernel/notes", root_dir
);
2503 ret
= sysfs__read_build_id(notes
, build_id
, sizeof(build_id
));
2507 return build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
2510 static int filename__sprintf_build_id(const char *pathname
, char *sbuild_id
)
2512 unsigned char build_id
[BUILD_ID_SIZE
];
2515 ret
= filename__read_build_id(pathname
, build_id
, sizeof(build_id
));
2518 else if (ret
!= sizeof(build_id
))
2521 return build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
2524 static int vmlinux_path__nr_entries
;
2525 static char **vmlinux_path
;
2527 static void vmlinux_path__exit(void)
2529 while (--vmlinux_path__nr_entries
>= 0)
2530 zfree(&vmlinux_path
[vmlinux_path__nr_entries
]);
2531 vmlinux_path__nr_entries
= 0;
2533 zfree(&vmlinux_path
);
2536 static const char * const vmlinux_paths
[] = {
2541 static const char * const vmlinux_paths_upd
[] = {
2543 "/usr/lib/debug/boot/vmlinux-%s",
2544 "/lib/modules/%s/build/vmlinux",
2545 "/usr/lib/debug/lib/modules/%s/vmlinux",
2546 "/usr/lib/debug/boot/vmlinux-%s.debug"
2549 static int vmlinux_path__add(const char *new_entry
)
2551 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(new_entry
);
2552 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
2554 ++vmlinux_path__nr_entries
;
2559 static int vmlinux_path__init(void)
2563 char *kernel_version
;
2566 vmlinux_path
= malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths
) +
2567 ARRAY_SIZE(vmlinux_paths_upd
)));
2568 if (vmlinux_path
== NULL
)
2571 for (i
= 0; i
< ARRAY_SIZE(vmlinux_paths
); i
++)
2572 if (vmlinux_path__add(vmlinux_paths
[i
]) < 0)
2575 if (uname(&uts
) < 0)
2578 kernel_version
= uts
.release
;
2580 for (i
= 0; i
< ARRAY_SIZE(vmlinux_paths_upd
); i
++) {
2581 snprintf(bf
, sizeof(bf
), vmlinux_paths_upd
[i
], kernel_version
);
2582 if (vmlinux_path__add(bf
) < 0)
2589 vmlinux_path__exit();
2593 static int cus__load_running_kernel(struct cus
*cus
, struct conf_load
*conf
)
2596 char running_sbuild_id
[SBUILD_ID_SIZE
];
2598 if ((!conf
|| conf
->format_path
== NULL
|| strncmp(conf
->format_path
, "btf", 3) == 0) &&
2599 access("/sys/kernel/btf/vmlinux", R_OK
) == 0) {
2600 int loader
= debugging_formats__loader("btf");
2604 if (conf
&& conf
->conf_fprintf
)
2605 conf
->conf_fprintf
->has_alignment_info
= debug_fmt_table
[loader
]->has_alignment_info
;
2607 if (debug_fmt_table
[loader
]->load_file(cus
, conf
, "/sys/kernel/btf/vmlinux") == 0)
2611 elf_version(EV_CURRENT
);
2612 vmlinux_path__init();
2614 sysfs__sprintf_build_id(NULL
, running_sbuild_id
);
2616 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
2617 char sbuild_id
[SBUILD_ID_SIZE
];
2619 if (filename__sprintf_build_id(vmlinux_path
[i
], sbuild_id
) > 0 &&
2620 strcmp(sbuild_id
, running_sbuild_id
) == 0) {
2621 err
= cus__load_file(cus
, conf
, vmlinux_path
[i
]);
2626 vmlinux_path__exit();
2631 int cus__load_files(struct cus
*cus
, struct conf_load
*conf
,
2636 while (filenames
[i
] != NULL
) {
2637 int err
= cus__load_file(cus
, conf
, filenames
[i
]);
2645 return i
? 0 : cus__load_running_kernel(cus
, conf
);
2648 int cus__fprintf_load_files_err(struct cus
*cus __maybe_unused
, const char *tool
, char *argv
[], int err
, FILE *output
)
2650 /* errno is not properly preserved in some cases, sigh */
2651 return fprintf(output
, "%s: %s: %s\n", tool
, argv
[-err
- 1],
2652 errno
? strerror(errno
) : "No debugging information found");
2655 struct cus
*cus__new(void)
2657 struct cus
*cus
= zalloc(sizeof(*cus
));
2660 INIT_LIST_HEAD(&cus
->cus
);
2661 pthread_mutex_init(&cus
->mutex
, NULL
);
2667 void cus__delete(struct cus
*cus
)
2676 list_for_each_entry_safe(pos
, n
, &cus
->cus
, node
) {
2677 list_del_init(&pos
->node
);
2681 if (cus
->loader_exit
)
2682 cus
->loader_exit(cus
);
2689 void cus__set_priv(struct cus
*cus
, void *priv
)
2694 void *cus__priv(struct cus
*cus
)
2699 void cus__set_loader_exit(struct cus
*cus
, void (*loader_exit
)(struct cus
*cus
))
2701 cus
->loader_exit
= loader_exit
;
2704 int dwarves__init(void)
2709 while (debug_fmt_table
[i
] != NULL
) {
2710 if (debug_fmt_table
[i
]->init
) {
2711 err
= debug_fmt_table
[i
]->init();
2721 if (debug_fmt_table
[i
]->exit
)
2722 debug_fmt_table
[i
]->exit();
2726 void dwarves__exit(void)
2730 while (debug_fmt_table
[i
] != NULL
) {
2731 if (debug_fmt_table
[i
]->exit
)
2732 debug_fmt_table
[i
]->exit();
2739 void dwarves_print_version(FILE *fp
, struct argp_state
*state __maybe_unused
)
2741 fprintf(fp
, "v%u.%u\n", DWARVES_MAJOR_VERSION
, DWARVES_MINOR_VERSION
);
2744 bool print_numeric_version
;
2746 void dwarves_print_numeric_version(FILE *fp
)
2748 fprintf(fp
, "%u%u\n", DWARVES_MAJOR_VERSION
, DWARVES_MINOR_VERSION
);