pahole: Describe expected use of 'default' in the man page
[dwarves.git] / dwarves.c
blob1ec259f50dbd37788f2cde9f14a9a5589ee800d5
1 /*
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>
8 */
10 #include <assert.h>
11 #include <dirent.h>
12 #include <dwarf.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <fnmatch.h>
16 #include <libelf.h>
17 #include <limits.h>
18 #include <pthread.h>
19 #include <search.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/utsname.h>
28 #include "config.h"
29 #include "list.h"
30 #include "dwarves.h"
31 #include "dutil.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);
42 if (o)
43 memset(o, 0, size);
44 return o;
47 void *cu__zalloc(struct cu *cu, size_t size)
49 if (cu->use_obstack)
50 return obstack_zalloc(&cu->obstack, size);
52 return zalloc(size);
55 void *cu__malloc(struct cu *cu, size_t size)
57 if (cu->use_obstack)
58 return obstack_alloc(&cu->obstack, size);
60 return malloc(size);
63 void cu__free(struct cu *cu, void *ptr)
65 if (!cu->use_obstack)
66 free(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)
73 switch (tag->tag) {
74 case DW_TAG_base_type:
75 return 1;
77 case DW_TAG_typedef: {
78 const struct tag *type = cu__type(cu, tag->type);
80 if (type == NULL)
81 return 0;
82 return tag__is_base_type(type, cu);
85 return 0;
88 bool tag__is_array(const struct tag *tag, const struct cu *cu)
90 switch (tag->tag) {
91 case DW_TAG_array_type:
92 return true;
94 case DW_TAG_const_type:
95 case DW_TAG_typedef: {
96 const struct tag *type = cu__type(cu, tag->type);
98 if (type == NULL)
99 return 0;
100 return tag__is_array(type, cu);
103 return 0;
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;
112 if (type == NULL)
113 return 0;
115 if (tag->type == type->type) {
116 int printed;
118 if (bf != NULL)
119 abf = bf;
120 else
121 len = sizeof(bbf);
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));
124 if (bf == NULL)
125 printed = fprintf(fp ?: stderr, "%s\n", abf);
126 return printed;
129 return 0;
132 static void lexblock__delete_tags(struct tag *tag)
134 struct lexblock *block = tag__lexblock(tag);
135 struct tag *pos, *n;
137 list_for_each_entry_safe_reverse(pos, n, &block->tags, node) {
138 list_del_init(&pos->node);
139 tag__delete(pos);
143 void lexblock__delete(struct lexblock *block)
145 if (block == NULL)
146 return;
148 lexblock__delete_tags(&block->ip.tag);
149 free(block);
152 void tag__delete(struct tag *tag)
154 if (tag == NULL)
155 return;
157 assert(list_empty(&tag->node));
159 switch (tag->tag) {
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;
173 default:
174 free(tag);
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);
182 exit(1);
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);
192 return type;
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);
202 return 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 {
212 const char *name;
213 size_t 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, },
250 { .name = NULL },
253 bool base_type__language_defined(struct base_type *bt)
255 int i = 0;
256 char bf[64];
257 const char *name;
259 if (bt->name_has_encoding)
260 name = bt->name;
261 else
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)
267 return true;
268 } else if (strcmp(base_type_name_to_size_table[i].name, name) == 0)
269 return true;
270 ++i;
273 return false;
276 size_t base_type__name_to_size(struct base_type *bt, struct cu *cu)
278 int i = 0;
279 char bf[64];
280 const char *name, *orig_name;
282 if (bt->name_has_encoding)
283 name = bt->name;
284 else
285 name = base_type__name(bt, bf, sizeof(bf));
286 orig_name = name;
287 try_again:
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) {
291 size_t size;
292 found:
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)
298 goto found;
299 ++i;
302 if (strstarts(name, "signed ")) {
303 i = 0;
304 name += sizeof("signed");
305 goto try_again;
308 fprintf(stderr, "%s: %s %s\n",
309 __func__, dwarf_tag_name(bt->tag.tag), orig_name);
310 return 0;
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)
330 return bt->name;
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);
338 if (bt->float_type)
339 snprintf(bf, len, "%s %s", base_type_fp_type_str[bt->float_type], bt->name);
340 else
341 snprintf(bf, len, "%s%s%s", bt->is_bool ? "bool " : "", bt->is_varargs ? "... " : "", bt->name);
342 return bf;
345 void namespace__delete(struct namespace *space)
347 struct tag *pos, *n;
349 if (space == NULL)
350 return;
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));
358 tag__delete(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,
376 const struct cu *cu)
378 struct class_member *pos, *result = NULL;
379 size_t result_size = 0;
381 type__for_each_data_member(type, pos) {
382 if (pos->is_static)
383 continue;
385 struct tag *type = cu__type(cu, pos->tag.type);
386 size_t member_size = 0, power2;
387 struct class_member *inner = NULL;
389 if (type == NULL) {
390 tag__id_not_found_fprintf(stderr, pos->tag.type);
391 continue;
393 reevaluate:
394 switch (type->tag) {
395 case DW_TAG_base_type:
396 member_size = base_type__size(type);
397 break;
398 case DW_TAG_pointer_type:
399 case DW_TAG_reference_type:
400 member_size = cu->addr_size;
401 break;
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)
406 continue;
407 inner = type__find_first_biggest_size_base_type_member(tag__type(type), cu);
408 member_size = inner->byte_size;
409 break;
410 case DW_TAG_array_type:
411 case DW_TAG_const_type:
412 case DW_TAG_typedef:
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);
417 if (tag == NULL) {
418 tag__id_not_found_fprintf(stderr, type->type);
419 continue;
421 type = tag;
423 goto reevaluate;
424 case DW_TAG_enumeration_type:
425 member_size = tag__type(type)->size / 8;
426 break;
429 /* long long */
430 if (member_size > cu->addr_size)
431 return pos;
433 for (power2 = cu->addr_size; power2 > result_size; power2 /= 2)
434 if (member_size >= power2) {
435 if (power2 == cu->addr_size)
436 return inner ?: pos;
437 result_size = power2;
438 result = inner ?: pos;
442 return result;
445 static void cu__find_class_holes(struct cu *cu)
447 uint32_t id;
448 struct class *pos;
450 cu__for_each_struct(cu, id, pos)
451 class__find_holes(pos);
454 struct cus {
455 uint32_t nr_entries;
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)
474 cus__lock(cus);
475 cu->state = state;
476 cus__unlock(cus);
479 // Used only when reproducible builds are desired
480 struct cu *cus__get_next_processable_cu(struct cus *cus)
482 struct cu *cu;
484 cus__lock(cus);
486 list_for_each_entry(cu, &cus->cus, node) {
487 switch (cu->state) {
488 case CU__LOADED:
489 cu->state = CU__PROCESSING;
490 goto found;
491 case 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.
498 continue;
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.
503 goto out;
506 out:
507 cu = NULL;
508 found:
509 cus__unlock(cus);
511 return cu;
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)
526 cus->nr_entries--;
527 list_del_init(&cu->node);
530 void cus__remove(struct cus *cus, struct cu *cu)
532 cus__lock(cus);
533 __cus__remove(cus, cu);
534 cus__unlock(cus);
537 void __cus__add(struct cus *cus, struct cu *cu)
539 cus->nr_entries++;
540 list_add_tail(&cu->node, &cus->cus);
543 void cus__add(struct cus *cus, struct cu *cu)
545 cus__lock(cus);
546 __cus__add(cus, cu);
547 cus__unlock(cus);
549 cu__find_class_holes(cu);
552 static void ptr_table__init(struct ptr_table *pt)
554 pt->entries = NULL;
555 pt->nr_entries = pt->allocated_entries = 0;
558 static void ptr_table__exit(struct ptr_table *pt)
560 zfree(&pt->entries);
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);
572 if (entries == NULL)
573 return -ENOMEM;
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;
585 *idxp = rc;
586 return 0;
589 static int ptr_table__add_with_id(struct ptr_table *pt, void *ptr,
590 uint32_t id)
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);
597 if (entries == NULL)
598 return -ENOMEM;
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;
611 return 0;
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;
624 struct function *f;
626 while (*p != NULL) {
627 parent = *p;
628 f = rb_entry(parent, struct function, rb_node);
629 if (function->lexblock.ip.addr < f->lexblock.ip.addr)
630 p = &(*p)->rb_left;
631 else
632 p = &(*p)->rb_right;
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);
661 if (err == 0)
662 list_add_tail(&tag->node, &cu->tags);
664 return err;
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);
685 if (err == 0)
686 list_add_tail(&tag->node, &cu->tags);
688 return err;
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);
703 return printed;
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);
712 if (cu != NULL) {
713 uint32_t void_id;
715 cu->use_obstack = use_obstack;
716 if (cu->use_obstack)
717 obstack_init(&cu->obstack);
719 cu->name = strdup(name);
720 if (cu->name == NULL)
721 goto out_free;
723 cu->filename = strdup(filename);
724 if (cu->filename == NULL)
725 goto out_free_name;
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;
739 cu->dfops = NULL;
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);
758 cu->priv = NULL;
761 return cu;
763 out_free_filename:
764 zfree(&cu->filename);
765 out_free_name:
766 zfree(&cu->name);
767 out_free:
768 free(cu);
769 return NULL;
772 void cu__delete(struct cu *cu)
774 if (cu == NULL)
775 return;
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);
783 if (cu->use_obstack)
784 obstack_free(&cu->obstack, NULL);
786 zfree(&cu->filename);
787 zfree(&cu->name);
788 free(cu);
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)
816 uint32_t id;
817 struct tag *pos;
819 if (cu == NULL || type == 0)
820 return NULL;
822 cu__for_each_type(cu, id, pos)
823 if (tag__is_typedef(pos) && pos->type == type)
824 return pos;
826 return NULL;
829 struct tag *cu__find_base_type_by_name(const struct cu *cu,
830 const char *name, type_id_t *idp)
832 uint32_t id;
833 struct tag *pos;
835 if (cu == NULL || name == NULL)
836 return NULL;
838 cu__for_each_type(cu, id, pos) {
839 if (pos->tag != DW_TAG_base_type)
840 continue;
842 const struct base_type *bt = tag__base_type(pos);
843 char bf[64];
844 const char *bname = base_type__name(bt, bf, sizeof(bf));
845 if (!bname || strcmp(bname, name) != 0)
846 continue;
848 if (idp != NULL)
849 *idp = id;
850 return pos;
853 return NULL;
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)
859 uint32_t id;
860 struct tag *pos;
862 if (name == NULL)
863 return NULL;
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);
868 char bf[64];
870 if (bt->bit_size == bit_size &&
871 strcmp(base_type__name(bt, bf, sizeof(bf)), name) == 0) {
872 if (idp != NULL)
873 *idp = id;
874 return pos;
879 return NULL;
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)
885 uint32_t id;
886 struct tag *pos;
888 if (name == NULL)
889 return NULL;
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) {
897 if (idp != NULL)
898 *idp = id;
899 return pos;
904 return NULL;
907 struct tag *cu__find_enumeration_by_name(const struct cu *cu, const char *name, type_id_t *idp)
909 uint32_t id;
910 struct tag *pos;
912 if (name == NULL)
913 return NULL;
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) {
921 if (idp != NULL)
922 *idp = id;
923 return pos;
928 return NULL;
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)
934 return NULL;
936 uint32_t id;
937 struct tag *pos;
938 cu__for_each_type(cu, id, pos) {
939 struct type *type;
941 if (!tag__is_type(pos))
942 continue;
944 type = tag__type(pos);
945 const char *tname = type__name(type);
946 if (tname && strcmp(tname, name) == 0) {
947 if (!type->declaration)
948 goto found;
950 if (include_decls)
951 goto found;
955 return NULL;
956 found:
957 if (idp != NULL)
958 *idp = id;
959 return pos;
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)
965 struct cu *pos;
966 struct tag *tag = NULL;
968 cus__lock(cus);
970 list_for_each_entry(pos, &cus->cus, node) {
971 tag = cu__find_type_by_name(pos, name, include_decls, id);
972 if (tag != NULL) {
973 if (cu != NULL)
974 *cu = pos;
975 break;
979 cus__unlock(cus);
981 return tag;
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)
988 return NULL;
990 uint32_t id;
991 struct tag *pos;
992 cu__for_each_type(cu, id, pos) {
993 struct type *type;
995 if (!(tag__is_struct(pos) || (unions && tag__is_union(pos))))
996 continue;
998 type = tag__type(pos);
999 const char *tname = type__name(type);
1000 if (tname && strcmp(tname, name) == 0) {
1001 if (!type->declaration)
1002 goto found;
1004 if (include_decls)
1005 goto found;
1009 return NULL;
1010 found:
1011 if (idp != NULL)
1012 *idp = id;
1013 return pos;
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;
1032 struct cu *pos;
1034 cus__lock(cus);
1036 list_for_each_entry(pos, &cus->cus, node) {
1037 struct tag *tag = __cu__find_struct_by_name(pos, name, include_decls, unions, id);
1038 if (tag != NULL) {
1039 if (cu != NULL)
1040 *cu = pos;
1041 break;
1045 cus__unlock(cus);
1047 return tag;
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,
1063 uint64_t addr)
1065 struct rb_node *n;
1067 if (cu == NULL)
1068 return NULL;
1070 n = cu->functions.rb_node;
1072 while (n) {
1073 struct function *f = rb_entry(n, struct function, rb_node);
1075 if (addr < f->lexblock.ip.addr)
1076 n = n->rb_left;
1077 else if (addr >= f->lexblock.ip.addr + f->lexblock.size)
1078 n = n->rb_right;
1079 else
1080 return f;
1083 return NULL;
1087 struct function *cus__find_function_at_addr(struct cus *cus, uint64_t addr, struct cu **cu)
1089 struct function *f = NULL;
1090 struct cu *pos;
1092 cus__lock(cus);
1094 list_for_each_entry(pos, &cus->cus, node) {
1095 f = cu__find_function_at_addr(pos, addr);
1097 if (f != NULL) {
1098 if (cu != NULL)
1099 *cu = pos;
1100 break;
1104 cus__unlock(cus);
1106 return f;
1109 static struct cu *__cus__find_cu_by_name(struct cus *cus, const char *name)
1111 struct cu *pos;
1113 list_for_each_entry(pos, &cus->cus, node)
1114 if (pos->name && strcmp(pos->name, name) == 0)
1115 goto out;
1117 pos = NULL;
1118 out:
1119 return pos;
1122 struct cu *cus__find_cu_by_name(struct cus *cus, const char *name)
1124 struct cu *pos;
1126 cus__lock(cus);
1128 pos = __cus__find_cu_by_name(cus, name);
1130 cus__unlock(cus);
1132 return pos;
1135 struct cu *cus__find_pair(struct cus *cus, const char *name)
1137 struct cu *cu;
1139 cus__lock(cus);
1141 if (cus->nr_entries == 1)
1142 cu = list_first_entry(&cus->cus, struct cu, node);
1143 else
1144 cu = __cus__find_cu_by_name(cus, name);
1146 cus__unlock(cus);
1148 return cu;
1151 struct tag *cu__find_function_by_name(const struct cu *cu, const char *name)
1153 if (cu == NULL || name == NULL)
1154 return NULL;
1156 uint32_t id;
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);
1164 return NULL;
1167 static size_t array_type__nr_entries(const struct array_type *at)
1169 int i;
1170 size_t nr_entries = 1;
1172 for (i = 0; i < at->dimensions; ++i)
1173 nr_entries *= at->nr_entries[i];
1175 return nr_entries;
1178 size_t tag__size(const struct tag *tag, const struct cu *cu)
1180 size_t size;
1182 switch (tag->tag) {
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)
1188 return 0;
1189 /* Is it cached already? */
1190 size = member->byte_size;
1191 if (size != 0)
1192 return size;
1193 break;
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)
1207 size = 0;
1208 else
1209 size = tag__type(tag)->size;
1210 } else {
1211 const struct tag *type = cu__type(cu, tag->type);
1213 if (type == NULL) {
1214 tag__id_not_found_fprintf(stderr, tag->type);
1215 return -1;
1216 } else if (tag__has_type_loop(tag, type, NULL, 0, NULL))
1217 return -1;
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));
1224 return size;
1227 const char *variable__name(const struct variable *var)
1229 return var->name;
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)
1242 free(member);
1245 static struct class_member *class_member__clone(const struct class_member *from)
1247 struct class_member *member = malloc(sizeof(*member));
1249 if (member != NULL)
1250 memcpy(member, from, sizeof(*member));
1252 return 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)
1267 if (class == NULL)
1268 return;
1270 type__delete_class_members(&class->type);
1271 free(class);
1274 void type__delete(struct type *type)
1276 if (type == NULL)
1277 return;
1279 type__delete_class_members(type);
1281 if (type->suffix_disambiguation)
1282 zfree(&type->namespace.name);
1284 free(type);
1287 static void enumerator__delete(struct enumerator *enumerator)
1289 free(enumerator);
1292 void enumeration__delete(struct type *type)
1294 struct enumerator *pos, *n;
1296 if (type == NULL)
1297 return;
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);
1307 free(type);
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)
1318 ++space->nr_tags;
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;
1326 else
1327 ++type->nr_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)
1337 return pos;
1338 return NULL;
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);
1351 if (clone == NULL)
1352 return -1;
1353 type__add_member(type, clone);
1356 return 0;
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) {
1368 free(class);
1369 return NULL;
1372 if (type__clone_members(&class->type, &from->type) != 0) {
1373 class__delete(class);
1374 class = NULL;
1378 return class;
1381 void enumeration__add(struct type *type, struct enumerator *enumerator)
1383 ++type->nr_members;
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)
1395 return func->name;
1398 static void parameter__delete(struct parameter *parm)
1400 free(parm);
1403 void ftype__delete(struct ftype *type)
1405 struct parameter *pos, *n;
1407 if (type == NULL)
1408 return;
1410 ftype__for_each_parameter_safe_reverse(type, pos, n) {
1411 list_del_init(&pos->tag.node);
1412 parameter__delete(pos);
1414 free(type);
1417 void function__delete(struct function *func)
1419 if (func == NULL)
1420 return;
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;
1434 if (func->btf)
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)
1443 return 1;
1446 return 0;
1449 void ftype__add_parameter(struct ftype *ftype, struct parameter *parm)
1451 ++ftype->nr_parms;
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)
1476 ++block->nr_labels;
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)
1488 if (pos == trailer)
1489 break;
1490 else if (pos->hole >= byte_hole_size ||
1491 pos->bit_hole >= bit_hole_size)
1492 return pos;
1494 return NULL;
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)))
1507 return;
1509 if (class->holes_searched)
1510 return;
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)
1519 continue;
1521 if (pos->is_static)
1522 continue;
1524 pos->bit_hole = 0;
1525 pos->hole = 0;
1527 bit_start = pos->bit_offset;
1528 if (pos->bitfield_size) {
1529 bit_end = bit_start + pos->bitfield_size;
1530 } else {
1531 bit_end = bit_start + pos->byte_size * 8;
1534 bit_holes = 0;
1535 byte_holes = 0;
1536 if (in_bitfield) {
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;
1552 } else {
1553 byte_holes = bit_start/8 - last_seen_bit/8;
1555 last_seen_bit = bit_end;
1557 if (pos->bitfield_size) {
1558 in_bitfield = true;
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;
1576 } else {
1577 in_bitfield = false;
1578 cur_bitfield_size = 0;
1579 cur_bitfield_end = bit_end;
1582 if (last) {
1583 last->hole = byte_holes;
1584 last->bit_hole = bit_holes;
1585 } else {
1586 class->pre_hole = byte_holes;
1587 class->pre_bit_hole = bit_holes;
1589 if (bit_holes)
1590 class->nr_bit_holes++;
1591 if (byte_holes > 0)
1592 class->nr_holes++;
1594 last = pos;
1597 if (in_bitfield) {
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;
1601 } else {
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)
1637 * seqcount_t seq;
1638 * #endif
1639 * };
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)
1656 continue;
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
1662 continue;
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
1690 continue;
1693 if (!tag__is_struct(member_type))
1694 continue;
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)))
1715 return false;
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;
1725 goto out;
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)
1732 continue;
1734 if (pos->is_static)
1735 continue;
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))
1742 continue;
1744 if (max_natural_alignment < natural_alignment)
1745 max_natural_alignment = natural_alignment;
1747 if ((pos->byte_offset % natural_alignment) == 0)
1748 continue;
1750 cls->is_packed = true;
1751 goto out;
1754 if ((max_natural_alignment != 1 && ctype->alignment == 1) ||
1755 (class__size(cls) % max_natural_alignment) != 0)
1756 cls->is_packed = true;
1758 out:
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)
1776 return;
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))
1782 continue;
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)
1807 return 0;
1809 type__for_each_data_member(&class->type, pos)
1810 if (pos->hole >= size)
1811 return 1;
1813 return 0;
1816 struct class_member *type__find_member_by_name(const struct type *type, const char *name)
1818 if (name == NULL)
1819 return NULL;
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)
1825 return pos;
1828 return NULL;
1831 static int strcommon(const char *a, const char *b)
1833 int i = 0;
1835 while (*a != '\0' && *a == *b) {
1836 ++a;
1837 ++b;
1838 ++i;
1841 return i;
1844 static void enumeration__calc_prefix(struct type *enumeration)
1846 if (enumeration->member_prefix)
1847 return;
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)
1902 return;
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),
1907 cu);
1908 continue;
1909 } else if (pos->tag != DW_TAG_inlined_subroutine)
1910 continue;
1912 type = cu__function(cu, pos->type);
1913 if (type != NULL) {
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)
1926 struct tag *pos;
1927 struct function *fpos;
1929 list_for_each_entry(pos, &cu->tags, node) {
1930 if (!tag__is_function(pos))
1931 continue;
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),
1942 void *cookie)
1944 struct tag *pos, *n;
1946 if (list_empty(list) || !list->next)
1947 return 0;
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,
1960 iterator, cookie))
1961 return 1;
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))
1968 return 1;
1969 if (list__for_all_tags(&tag__function(pos)->lexblock.tags,
1970 cu, iterator, cookie))
1971 return 1;
1972 } else if (pos->tag == DW_TAG_subroutine_type) {
1973 if (list__for_all_tags(&tag__ftype(pos)->parms,
1974 cu, iterator, cookie))
1975 return 1;
1976 } else if (pos->tag == DW_TAG_lexical_block) {
1977 if (list__for_all_tags(&tag__lexblock(pos)->tags,
1978 cu, iterator, cookie))
1979 return 1;
1982 if (iterator(pos, cu, cookie))
1983 return 1;
1985 return 0;
1988 int cu__for_all_tags(struct cu *cu,
1989 int (*iterator)(struct tag *tag,
1990 struct cu *cu, void *cookie),
1991 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),
1998 void *cookie,
1999 struct cu *(*filter)(struct cu *cu))
2001 struct cu *pos;
2003 cus__lock(cus);
2005 list_for_each_entry(pos, &cus->cus, node) {
2006 struct cu *cu = pos;
2007 if (filter != NULL) {
2008 cu = filter(pos);
2009 if (cu == NULL)
2010 continue;
2012 if (iterator(cu, cookie))
2013 break;
2016 cus__unlock(cus);
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;
2024 int err = -1;
2025 DIR *dir = opendir(dirname);
2027 if (dir == NULL)
2028 goto out;
2030 err = 0;
2031 while ((entry = readdir(dir)) != NULL) {
2032 char pathname[PATH_MAX];
2033 struct stat st;
2035 if (strcmp(entry->d_name, ".") == 0 ||
2036 strcmp(entry->d_name, "..") == 0)
2037 continue;
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);
2043 if (err != 0)
2044 break;
2046 if (S_ISDIR(st.st_mode)) {
2047 if (!recursive)
2048 continue;
2050 err = cus__load_dir(cus, conf, pathname,
2051 filename_mask, recursive);
2052 if (err != 0)
2053 break;
2054 } else if (fnmatch(filename_mask, entry->d_name, 0) == 0) {
2055 err = cus__load_file(cus, conf, pathname);
2056 if (err != 0)
2057 break;
2061 if (err == -1)
2062 puts(dirname);
2063 closedir(dir);
2064 out:
2065 return err;
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[] = {
2074 &dwarf__ops,
2075 &btf__ops,
2076 &ctf__ops,
2077 NULL,
2080 static int debugging_formats__loader(const char *name)
2082 int i = 0;
2083 while (debug_fmt_table[i] != NULL) {
2084 if (strcmp(debug_fmt_table[i]->name, name) == 0)
2085 return i;
2086 ++i;
2088 return -1;
2091 int cus__load_file(struct cus *cus, struct conf_load *conf,
2092 const char *filename)
2094 int i = 0, err = 0;
2095 int loader;
2097 if (conf && conf->format_path != NULL) {
2098 char *fpath = strdup(conf->format_path);
2099 if (fpath == NULL)
2100 return -ENOMEM;
2101 char *fp = fpath;
2102 while (1) {
2103 char *sep = strchr(fp, ',');
2105 if (sep != NULL)
2106 *sep = '\0';
2108 err = -ENOTSUP;
2109 loader = debugging_formats__loader(fp);
2110 if (loader == -1)
2111 break;
2113 if (conf->conf_fprintf)
2114 conf->conf_fprintf->has_alignment_info = debug_fmt_table[loader]->has_alignment_info;
2116 err = 0;
2117 if (debug_fmt_table[loader]->load_file(cus, conf,
2118 filename) == 0)
2119 break;
2121 err = -EINVAL;
2122 if (sep == NULL)
2123 break;
2125 fp = sep + 1;
2127 free(fpath);
2128 return err;
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)
2135 return 0;
2136 ++i;
2139 return -EINVAL;
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
2149 #ifndef min
2150 #define min(x, y) ({ \
2151 typeof(x) _min1 = (x); \
2152 typeof(y) _min2 = (y); \
2153 (void) (&_min1 == &_min2); \
2154 _min1 < _min2 ? _min1 : _min2; })
2155 #endif
2157 #ifndef DW_LANG_C89
2158 #define DW_LANG_C89 0x0001
2159 #endif
2160 #ifndef DW_LANG_C
2161 #define DW_LANG_C 0x0002
2162 #endif
2163 #ifndef DW_LANG_Ada83
2164 #define DW_LANG_Ada83 0x0003
2165 #endif
2166 #ifndef DW_LANG_C_plus_plus
2167 #define DW_LANG_C_plus_plus 0x0004
2168 #endif
2169 #ifndef DW_LANG_Cobol74
2170 #define DW_LANG_Cobol74 0x0005
2171 #endif
2172 #ifndef DW_LANG_Cobol85
2173 #define DW_LANG_Cobol85 0x0006
2174 #endif
2175 #ifndef DW_LANG_Fortran77
2176 #define DW_LANG_Fortran77 0x0007
2177 #endif
2178 #ifndef DW_LANG_Fortran90
2179 #define DW_LANG_Fortran90 0x0008
2180 #endif
2181 #ifndef DW_LANG_Pascal83
2182 #define DW_LANG_Pascal83 0x0009
2183 #endif
2184 #ifndef DW_LANG_Modula2
2185 #define DW_LANG_Modula2 0x000a
2186 #endif
2187 #ifndef DW_LANG_Java
2188 #define DW_LANG_Java 0x000b
2189 #endif
2190 #ifndef DW_LANG_C99
2191 #define DW_LANG_C99 0x000c
2192 #endif
2193 #ifndef DW_LANG_Ada95
2194 #define DW_LANG_Ada95 0x000d
2195 #endif
2196 #ifndef DW_LANG_Fortran95
2197 #define DW_LANG_Fortran95 0x000e
2198 #endif
2199 #ifndef DW_LANG_PLI
2200 #define DW_LANG_PLI 0x000f
2201 #endif
2202 #ifndef DW_LANG_ObjC
2203 #define DW_LANG_ObjC 0x0010
2204 #endif
2205 #ifndef DW_LANG_ObjC_plus_plus
2206 #define DW_LANG_ObjC_plus_plus 0x0011
2207 #endif
2208 #ifndef DW_LANG_UPC
2209 #define DW_LANG_UPC 0x0012
2210 #endif
2211 #ifndef DW_LANG_D
2212 #define DW_LANG_D 0x0013
2213 #endif
2214 #ifndef DW_LANG_Python
2215 #define DW_LANG_Python 0x0014
2216 #endif
2217 #ifndef DW_LANG_OpenCL
2218 #define DW_LANG_OpenCL 0x0015
2219 #endif
2220 #ifndef DW_LANG_Go
2221 #define DW_LANG_Go 0x0016
2222 #endif
2223 #ifndef DW_LANG_Modula3
2224 #define DW_LANG_Modula3 0x0017
2225 #endif
2226 #ifndef DW_LANG_Haskell
2227 #define DW_LANG_Haskell 0x0018
2228 #endif
2229 #ifndef DW_LANG_C_plus_plus_03
2230 #define DW_LANG_C_plus_plus_03 0x0019
2231 #endif
2232 #ifndef DW_LANG_C_plus_plus_11
2233 #define DW_LANG_C_plus_plus_11 0x001a
2234 #endif
2235 #ifndef DW_LANG_OCaml
2236 #define DW_LANG_OCaml 0x001b
2237 #endif
2238 #ifndef DW_LANG_Rust
2239 #define DW_LANG_Rust 0x001c
2240 #endif
2241 #ifndef DW_LANG_C11
2242 #define DW_LANG_C11 0x001d
2243 #endif
2244 #ifndef DW_LANG_Swift
2245 #define DW_LANG_Swift 0x001e
2246 #endif
2247 #ifndef DW_LANG_Julia
2248 #define DW_LANG_Julia 0x001f
2249 #endif
2250 #ifndef DW_LANG_Dylan
2251 #define DW_LANG_Dylan 0x0020
2252 #endif
2253 #ifndef DW_LANG_C_plus_plus_14
2254 #define DW_LANG_C_plus_plus_14 0x0021
2255 #endif
2256 #ifndef DW_LANG_Fortran03
2257 #define DW_LANG_Fortran03 0x0022
2258 #endif
2259 #ifndef DW_LANG_Fortran08
2260 #define DW_LANG_Fortran08 0x0023
2261 #endif
2262 #ifndef DW_LANG_RenderScript
2263 #define DW_LANG_RenderScript 0x0024
2264 #endif
2265 #ifndef DW_LANG_BLISS
2266 #define DW_LANG_BLISS 0x0025
2267 #endif
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",
2278 [DW_LANG_C] = "c",
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++",
2285 [DW_LANG_D] = "d",
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)
2317 return id;
2319 return -1;
2323 static int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
2325 int fd, err = -1;
2327 if (size < BUILD_ID_SIZE)
2328 goto out;
2330 fd = open(filename, O_RDONLY);
2331 if (fd < 0)
2332 goto out;
2334 while (1) {
2335 char bf[BUFSIZ];
2336 GElf_Nhdr nhdr;
2337 size_t namesz, descsz;
2339 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
2340 break;
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)
2347 break;
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);
2352 err = 0;
2353 break;
2355 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
2356 break;
2357 } else {
2358 int n = namesz + descsz;
2360 if (n > (int)sizeof(bf)) {
2361 n = 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)
2366 break;
2369 close(fd);
2370 out:
2371 return err;
2374 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
2376 int err = -1;
2377 GElf_Ehdr ehdr;
2378 GElf_Shdr shdr;
2379 Elf_Data *data;
2380 Elf_Scn *sec;
2381 Elf_Kind ek;
2382 void *ptr;
2384 if (size < BUILD_ID_SIZE)
2385 goto out;
2387 ek = elf_kind(elf);
2388 if (ek != ELF_K_ELF)
2389 goto out;
2391 if (gelf_getehdr(elf, &ehdr) == NULL) {
2392 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
2393 goto out;
2397 * Check following sections for notes:
2398 * '.note.gnu.build-id'
2399 * '.notes'
2400 * '.note' (VDSO specific)
2402 do {
2403 sec = elf_section_by_name(elf, &shdr, ".note.gnu.build-id", NULL);
2404 if (sec)
2405 break;
2407 sec = elf_section_by_name(elf, &shdr, ".notes", NULL);
2408 if (sec)
2409 break;
2411 sec = elf_section_by_name(elf, &shdr, ".note", NULL);
2412 if (sec)
2413 break;
2415 return err;
2417 } while (0);
2419 data = elf_getdata(sec, NULL);
2420 if (data == NULL)
2421 goto out;
2423 ptr = data->d_buf;
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);
2428 const char *name;
2430 ptr += sizeof(*nhdr);
2431 name = ptr;
2432 ptr += namesz;
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);
2439 err = descsz;
2440 break;
2443 ptr += descsz;
2446 out:
2447 return err;
2450 static int filename__read_build_id(const char *filename, void *bf, size_t size)
2452 int fd, err = -1;
2453 Elf *elf;
2455 if (size < BUILD_ID_SIZE)
2456 goto out;
2458 fd = open(filename, O_RDONLY);
2459 if (fd < 0)
2460 goto out;
2462 elf = elf_begin(fd, ELF_C_READ, NULL);
2463 if (elf == NULL) {
2464 fprintf(stderr, "%s: cannot read %s ELF file.\n", __func__, filename);
2465 goto out_close;
2468 err = elf_read_build_id(elf, bf, size);
2470 elf_end(elf);
2471 out_close:
2472 close(fd);
2473 out:
2474 return err;
2477 static int build_id__sprintf(const unsigned char *build_id, int len, char *bf)
2479 char *bid = bf;
2480 const unsigned char *raw = build_id;
2481 int i;
2483 for (i = 0; i < len; ++i) {
2484 sprintf(bid, "%02x", *raw);
2485 ++raw;
2486 bid += 2;
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];
2496 int ret;
2498 if (!root_dir)
2499 root_dir = "";
2501 snprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
2503 ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
2504 if (ret < 0)
2505 return ret;
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];
2513 int ret;
2515 ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
2516 if (ret < 0)
2517 return ret;
2518 else if (ret != sizeof(build_id))
2519 return -EINVAL;
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[] = {
2537 "vmlinux",
2538 "/boot/vmlinux"
2541 static const char * const vmlinux_paths_upd[] = {
2542 "/boot/vmlinux-%s",
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)
2553 return -1;
2554 ++vmlinux_path__nr_entries;
2556 return 0;
2559 static int vmlinux_path__init(void)
2561 struct utsname uts;
2562 char bf[PATH_MAX];
2563 char *kernel_version;
2564 unsigned int i;
2566 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2567 ARRAY_SIZE(vmlinux_paths_upd)));
2568 if (vmlinux_path == NULL)
2569 return -1;
2571 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2572 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2573 goto out_fail;
2575 if (uname(&uts) < 0)
2576 goto out_fail;
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)
2583 goto out_fail;
2586 return 0;
2588 out_fail:
2589 vmlinux_path__exit();
2590 return -1;
2593 static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
2595 int i, err = 0;
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");
2601 if (loader == -1)
2602 goto try_elf;
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)
2608 return 0;
2610 try_elf:
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]);
2622 break;
2626 vmlinux_path__exit();
2628 return err;
2631 int cus__load_files(struct cus *cus, struct conf_load *conf,
2632 char *filenames[])
2634 int i = 0;
2636 while (filenames[i] != NULL) {
2637 int err = cus__load_file(cus, conf, filenames[i]);
2638 if (err) {
2639 errno = -err;
2640 return -++i;
2642 ++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));
2659 if (cus != NULL) {
2660 INIT_LIST_HEAD(&cus->cus);
2661 pthread_mutex_init(&cus->mutex, NULL);
2664 return cus;
2667 void cus__delete(struct cus *cus)
2669 struct cu *pos, *n;
2671 if (cus == NULL)
2672 return;
2674 cus__lock(cus);
2676 list_for_each_entry_safe(pos, n, &cus->cus, node) {
2677 list_del_init(&pos->node);
2678 cu__delete(pos);
2681 if (cus->loader_exit)
2682 cus->loader_exit(cus);
2684 cus__unlock(cus);
2686 free(cus);
2689 void cus__set_priv(struct cus *cus, void *priv)
2691 cus->priv = priv;
2694 void *cus__priv(struct cus *cus)
2696 return cus->priv;
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)
2706 int i = 0;
2707 int err = 0;
2709 while (debug_fmt_table[i] != NULL) {
2710 if (debug_fmt_table[i]->init) {
2711 err = debug_fmt_table[i]->init();
2712 if (err)
2713 goto out_fail;
2715 ++i;
2718 return 0;
2719 out_fail:
2720 while (i-- != 0)
2721 if (debug_fmt_table[i]->exit)
2722 debug_fmt_table[i]->exit();
2723 return err;
2726 void dwarves__exit(void)
2728 int i = 0;
2730 while (debug_fmt_table[i] != NULL) {
2731 if (debug_fmt_table[i]->exit)
2732 debug_fmt_table[i]->exit();
2733 ++i;
2737 struct argp_state;
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);