1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * BTF-to-C type converter.
6 * Copyright (c) 2019 Facebook
17 #include <linux/err.h>
18 #include <linux/btf.h>
19 #include <linux/kernel.h>
23 #include "libbpf_internal.h"
25 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
26 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
28 static const char *pfx(int lvl)
30 return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl];
33 enum btf_dump_type_order_state {
39 enum btf_dump_type_emit_state {
45 /* per-type auxiliary state */
46 struct btf_dump_type_aux_state {
47 /* topological sorting state */
48 enum btf_dump_type_order_state order_state: 2;
49 /* emitting state used to determine the need for forward declaration */
50 enum btf_dump_type_emit_state emit_state: 2;
51 /* whether forward declaration was already emitted */
53 /* whether unique non-duplicate name was already assigned */
54 __u8 name_resolved: 1;
55 /* whether type is referenced from any other type */
59 /* indent string length; one indent string is added for each indent level */
60 #define BTF_DATA_INDENT_STR_LEN 32
63 * Common internal data for BTF type data dump operations.
65 struct btf_dump_data {
66 const void *data_end; /* end of valid data to show */
70 __u8 indent_lvl; /* base indent level */
71 char indent_str[BTF_DATA_INDENT_STR_LEN];
72 /* below are used during iteration */
75 bool is_array_terminated;
80 const struct btf *btf;
81 btf_dump_printf_fn_t printf_fn;
88 /* per-type auxiliary state */
89 struct btf_dump_type_aux_state *type_states;
90 size_t type_states_cap;
91 /* per-type optional cached unique name, must be freed, if present */
92 const char **cached_names;
93 size_t cached_names_cap;
95 /* topo-sorted list of dependent type definitions */
101 * stack of type declarations (e.g., chain of modifiers, arrays,
108 /* maps struct/union/enum name to a number of name occurrences */
109 struct hashmap *type_names;
111 * maps typedef identifiers and enum value names to a number of such
114 struct hashmap *ident_names;
116 * data for typed display; allocated if needed.
118 struct btf_dump_data *typed_dump;
121 static size_t str_hash_fn(long key, void *ctx)
123 return str_hash((void *)key);
126 static bool str_equal_fn(long a, long b, void *ctx)
128 return strcmp((void *)a, (void *)b) == 0;
131 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off)
133 return btf__name_by_offset(d->btf, name_off);
136 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
141 d->printf_fn(d->cb_ctx, fmt, args);
145 static int btf_dump_mark_referenced(struct btf_dump *d);
146 static int btf_dump_resize(struct btf_dump *d);
148 struct btf_dump *btf_dump__new(const struct btf *btf,
149 btf_dump_printf_fn_t printf_fn,
151 const struct btf_dump_opts *opts)
156 if (!OPTS_VALID(opts, btf_dump_opts))
157 return libbpf_err_ptr(-EINVAL);
160 return libbpf_err_ptr(-EINVAL);
162 d = calloc(1, sizeof(struct btf_dump));
164 return libbpf_err_ptr(-ENOMEM);
167 d->printf_fn = printf_fn;
169 d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *);
171 d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
172 if (IS_ERR(d->type_names)) {
173 err = PTR_ERR(d->type_names);
174 d->type_names = NULL;
177 d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
178 if (IS_ERR(d->ident_names)) {
179 err = PTR_ERR(d->ident_names);
180 d->ident_names = NULL;
184 err = btf_dump_resize(d);
191 return libbpf_err_ptr(err);
194 static int btf_dump_resize(struct btf_dump *d)
196 int err, last_id = btf__type_cnt(d->btf) - 1;
198 if (last_id <= d->last_id)
201 if (libbpf_ensure_mem((void **)&d->type_states, &d->type_states_cap,
202 sizeof(*d->type_states), last_id + 1))
204 if (libbpf_ensure_mem((void **)&d->cached_names, &d->cached_names_cap,
205 sizeof(*d->cached_names), last_id + 1))
208 if (d->last_id == 0) {
209 /* VOID is special */
210 d->type_states[0].order_state = ORDERED;
211 d->type_states[0].emit_state = EMITTED;
214 /* eagerly determine referenced types for anon enums */
215 err = btf_dump_mark_referenced(d);
219 d->last_id = last_id;
223 static void btf_dump_free_names(struct hashmap *map)
226 struct hashmap_entry *cur;
228 hashmap__for_each_entry(map, cur, bkt)
229 free((void *)cur->pkey);
234 void btf_dump__free(struct btf_dump *d)
238 if (IS_ERR_OR_NULL(d))
241 free(d->type_states);
242 if (d->cached_names) {
243 /* any set cached name is owned by us and should be freed */
244 for (i = 0; i <= d->last_id; i++) {
245 if (d->cached_names[i])
246 free((void *)d->cached_names[i]);
249 free(d->cached_names);
252 btf_dump_free_names(d->type_names);
253 btf_dump_free_names(d->ident_names);
258 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
259 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
262 * Dump BTF type in a compilable C syntax, including all the necessary
263 * dependent types, necessary for compilation. If some of the dependent types
264 * were already emitted as part of previous btf_dump__dump_type() invocation
265 * for another type, they won't be emitted again. This API allows callers to
266 * filter out BTF types according to user-defined criterias and emitted only
267 * minimal subset of types, necessary to compile everything. Full struct/union
268 * definitions will still be emitted, even if the only usage is through
269 * pointer and could be satisfied with just a forward declaration.
271 * Dumping is done in two high-level passes:
272 * 1. Topologically sort type definitions to satisfy C rules of compilation.
273 * 2. Emit type definitions in C syntax.
275 * Returns 0 on success; <0, otherwise.
277 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
281 if (id >= btf__type_cnt(d->btf))
282 return libbpf_err(-EINVAL);
284 err = btf_dump_resize(d);
286 return libbpf_err(err);
288 d->emit_queue_cnt = 0;
289 err = btf_dump_order_type(d, id, false);
291 return libbpf_err(err);
293 for (i = 0; i < d->emit_queue_cnt; i++)
294 btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
300 * Mark all types that are referenced from any other type. This is used to
301 * determine top-level anonymous enums that need to be emitted as an
302 * independent type declarations.
303 * Anonymous enums come in two flavors: either embedded in a struct's field
304 * definition, in which case they have to be declared inline as part of field
305 * type declaration; or as a top-level anonymous enum, typically used for
306 * declaring global constants. It's impossible to distinguish between two
307 * without knowning whether given enum type was referenced from other type:
308 * top-level anonymous enum won't be referenced by anything, while embedded
311 static int btf_dump_mark_referenced(struct btf_dump *d)
313 int i, j, n = btf__type_cnt(d->btf);
314 const struct btf_type *t;
317 for (i = d->last_id + 1; i < n; i++) {
318 t = btf__type_by_id(d->btf, i);
321 switch (btf_kind(t)) {
324 case BTF_KIND_ENUM64:
329 case BTF_KIND_VOLATILE:
331 case BTF_KIND_RESTRICT:
333 case BTF_KIND_TYPEDEF:
336 case BTF_KIND_DECL_TAG:
337 case BTF_KIND_TYPE_TAG:
338 d->type_states[t->type].referenced = 1;
341 case BTF_KIND_ARRAY: {
342 const struct btf_array *a = btf_array(t);
344 d->type_states[a->index_type].referenced = 1;
345 d->type_states[a->type].referenced = 1;
348 case BTF_KIND_STRUCT:
349 case BTF_KIND_UNION: {
350 const struct btf_member *m = btf_members(t);
352 for (j = 0; j < vlen; j++, m++)
353 d->type_states[m->type].referenced = 1;
356 case BTF_KIND_FUNC_PROTO: {
357 const struct btf_param *p = btf_params(t);
359 for (j = 0; j < vlen; j++, p++)
360 d->type_states[p->type].referenced = 1;
363 case BTF_KIND_DATASEC: {
364 const struct btf_var_secinfo *v = btf_var_secinfos(t);
366 for (j = 0; j < vlen; j++, v++)
367 d->type_states[v->type].referenced = 1;
377 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
382 if (d->emit_queue_cnt >= d->emit_queue_cap) {
383 new_cap = max(16, d->emit_queue_cap * 3 / 2);
384 new_queue = libbpf_reallocarray(d->emit_queue, new_cap, sizeof(new_queue[0]));
387 d->emit_queue = new_queue;
388 d->emit_queue_cap = new_cap;
391 d->emit_queue[d->emit_queue_cnt++] = id;
396 * Determine order of emitting dependent types and specified type to satisfy
397 * C compilation rules. This is done through topological sorting with an
398 * additional complication which comes from C rules. The main idea for C is
399 * that if some type is "embedded" into a struct/union, it's size needs to be
400 * known at the time of definition of containing type. E.g., for:
403 * struct B { struct A x; }
405 * struct A *HAS* to be defined before struct B, because it's "embedded",
406 * i.e., it is part of struct B layout. But in the following case:
409 * struct B { struct A *x; }
412 * it's enough to just have a forward declaration of struct A at the time of
413 * struct B definition, as struct B has a pointer to struct A, so the size of
414 * field x is known without knowing struct A size: it's sizeof(void *).
416 * Unfortunately, there are some trickier cases we need to handle, e.g.:
418 * struct A {}; // if this was forward-declaration: compilation error
420 * struct { // anonymous struct
425 * In this case, struct B's field x is a pointer, so it's size is known
426 * regardless of the size of (anonymous) struct it points to. But because this
427 * struct is anonymous and thus defined inline inside struct B, *and* it
428 * embeds struct A, compiler requires full definition of struct A to be known
429 * before struct B can be defined. This creates a transitive dependency
430 * between struct A and struct B. If struct A was forward-declared before
431 * struct B definition and fully defined after struct B definition, that would
432 * trigger compilation error.
434 * All this means that while we are doing topological sorting on BTF type
435 * graph, we need to determine relationships between different types (graph
437 * - weak link (relationship) between X and Y, if Y *CAN* be
438 * forward-declared at the point of X definition;
439 * - strong link, if Y *HAS* to be fully-defined before X can be defined.
441 * The rule is as follows. Given a chain of BTF types from X to Y, if there is
442 * BTF_KIND_PTR type in the chain and at least one non-anonymous type
443 * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
444 * Weak/strong relationship is determined recursively during DFS traversal and
445 * is returned as a result from btf_dump_order_type().
447 * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
448 * but it is not guaranteeing that no extraneous forward declarations will be
451 * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
452 * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
453 * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
454 * entire graph path, so depending where from one came to that BTF type, it
455 * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
456 * once they are processed, there is no need to do it again, so they are
457 * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
458 * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
459 * in any case, once those are processed, no need to do it again, as the
460 * result won't change.
463 * - 1, if type is part of strong link (so there is strong topological
464 * ordering requirements);
465 * - 0, if type is part of weak link (so can be satisfied through forward
467 * - <0, on error (e.g., unsatisfiable type loop detected).
469 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
472 * Order state is used to detect strong link cycles, but only for BTF
473 * kinds that are or could be an independent definition (i.e.,
474 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
475 * func_protos, modifiers are just means to get to these definitions.
476 * Int/void don't need definitions, they are assumed to be always
477 * properly defined. We also ignore datasec, var, and funcs for now.
478 * So for all non-defining kinds, we never even set ordering state,
479 * for defining kinds we set ORDERING and subsequently ORDERED if it
480 * forms a strong link.
482 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
483 const struct btf_type *t;
487 /* return true, letting typedefs know that it's ok to be emitted */
488 if (tstate->order_state == ORDERED)
491 t = btf__type_by_id(d->btf, id);
493 if (tstate->order_state == ORDERING) {
494 /* type loop, but resolvable through fwd declaration */
495 if (btf_is_composite(t) && through_ptr && t->name_off != 0)
497 pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
501 switch (btf_kind(t)) {
504 tstate->order_state = ORDERED;
508 err = btf_dump_order_type(d, t->type, true);
509 tstate->order_state = ORDERED;
513 return btf_dump_order_type(d, btf_array(t)->type, false);
515 case BTF_KIND_STRUCT:
516 case BTF_KIND_UNION: {
517 const struct btf_member *m = btf_members(t);
519 * struct/union is part of strong link, only if it's embedded
520 * (so no ptr in a path) or it's anonymous (so has to be
521 * defined inline, even if declared through ptr)
523 if (through_ptr && t->name_off != 0)
526 tstate->order_state = ORDERING;
529 for (i = 0; i < vlen; i++, m++) {
530 err = btf_dump_order_type(d, m->type, false);
535 if (t->name_off != 0) {
536 err = btf_dump_add_emit_queue_id(d, id);
541 tstate->order_state = ORDERED;
545 case BTF_KIND_ENUM64:
548 * non-anonymous or non-referenced enums are top-level
549 * declarations and should be emitted. Same logic can be
550 * applied to FWDs, it won't hurt anyways.
552 if (t->name_off != 0 || !tstate->referenced) {
553 err = btf_dump_add_emit_queue_id(d, id);
557 tstate->order_state = ORDERED;
560 case BTF_KIND_TYPEDEF: {
563 is_strong = btf_dump_order_type(d, t->type, through_ptr);
567 /* typedef is similar to struct/union w.r.t. fwd-decls */
568 if (through_ptr && !is_strong)
571 /* typedef is always a named definition */
572 err = btf_dump_add_emit_queue_id(d, id);
576 d->type_states[id].order_state = ORDERED;
579 case BTF_KIND_VOLATILE:
581 case BTF_KIND_RESTRICT:
582 case BTF_KIND_TYPE_TAG:
583 return btf_dump_order_type(d, t->type, through_ptr);
585 case BTF_KIND_FUNC_PROTO: {
586 const struct btf_param *p = btf_params(t);
589 err = btf_dump_order_type(d, t->type, through_ptr);
595 for (i = 0; i < vlen; i++, p++) {
596 err = btf_dump_order_type(d, p->type, through_ptr);
606 case BTF_KIND_DATASEC:
607 case BTF_KIND_DECL_TAG:
608 d->type_states[id].order_state = ORDERED;
616 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
617 const struct btf_type *t);
619 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
620 const struct btf_type *t);
621 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
622 const struct btf_type *t, int lvl);
624 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
625 const struct btf_type *t);
626 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
627 const struct btf_type *t, int lvl);
629 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
630 const struct btf_type *t);
632 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
633 const struct btf_type *t, int lvl);
635 /* a local view into a shared stack */
641 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
642 const char *fname, int lvl);
643 static void btf_dump_emit_type_chain(struct btf_dump *d,
644 struct id_stack *decl_stack,
645 const char *fname, int lvl);
647 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
648 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
649 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
650 const char *orig_name);
652 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
654 const struct btf_type *t = btf__type_by_id(d->btf, id);
656 /* __builtin_va_list is a compiler built-in, which causes compilation
657 * errors, when compiling w/ different compiler, then used to compile
658 * original code (e.g., GCC to compile kernel, Clang to use generated
659 * C header from BTF). As it is built-in, it should be already defined
660 * properly internally in compiler.
662 if (t->name_off == 0)
664 return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
668 * Emit C-syntax definitions of types from chains of BTF types.
670 * High-level handling of determining necessary forward declarations are handled
671 * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
672 * declarations/definitions in C syntax are handled by a combo of
673 * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
674 * corresponding btf_dump_emit_*_{def,fwd}() functions.
676 * We also keep track of "containing struct/union type ID" to determine when
677 * we reference it from inside and thus can avoid emitting unnecessary forward
680 * This algorithm is designed in such a way, that even if some error occurs
681 * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
682 * that doesn't comply to C rules completely), algorithm will try to proceed
683 * and produce as much meaningful output as possible.
685 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
687 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
688 bool top_level_def = cont_id == 0;
689 const struct btf_type *t;
692 if (tstate->emit_state == EMITTED)
695 t = btf__type_by_id(d->btf, id);
698 if (tstate->emit_state == EMITTING) {
699 if (tstate->fwd_emitted)
703 case BTF_KIND_STRUCT:
706 * if we are referencing a struct/union that we are
707 * part of - then no need for fwd declaration
711 if (t->name_off == 0) {
712 pr_warn("anonymous struct/union loop, id:[%u]\n",
716 btf_dump_emit_struct_fwd(d, id, t);
717 btf_dump_printf(d, ";\n\n");
718 tstate->fwd_emitted = 1;
720 case BTF_KIND_TYPEDEF:
722 * for typedef fwd_emitted means typedef definition
723 * was emitted, but it can be used only for "weak"
724 * references through pointer only, not for embedding
726 if (!btf_dump_is_blacklisted(d, id)) {
727 btf_dump_emit_typedef_def(d, id, t, 0);
728 btf_dump_printf(d, ";\n\n");
730 tstate->fwd_emitted = 1;
741 /* Emit type alias definitions if necessary */
742 btf_dump_emit_missing_aliases(d, id, t);
744 tstate->emit_state = EMITTED;
747 case BTF_KIND_ENUM64:
749 btf_dump_emit_enum_def(d, id, t, 0);
750 btf_dump_printf(d, ";\n\n");
752 tstate->emit_state = EMITTED;
755 case BTF_KIND_VOLATILE:
757 case BTF_KIND_RESTRICT:
758 case BTF_KIND_TYPE_TAG:
759 btf_dump_emit_type(d, t->type, cont_id);
762 btf_dump_emit_type(d, btf_array(t)->type, cont_id);
765 btf_dump_emit_fwd_def(d, id, t);
766 btf_dump_printf(d, ";\n\n");
767 tstate->emit_state = EMITTED;
769 case BTF_KIND_TYPEDEF:
770 tstate->emit_state = EMITTING;
771 btf_dump_emit_type(d, t->type, id);
773 * typedef can server as both definition and forward
774 * declaration; at this stage someone depends on
775 * typedef as a forward declaration (refers to it
776 * through pointer), so unless we already did it,
777 * emit typedef as a forward declaration
779 if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
780 btf_dump_emit_typedef_def(d, id, t, 0);
781 btf_dump_printf(d, ";\n\n");
783 tstate->emit_state = EMITTED;
785 case BTF_KIND_STRUCT:
787 tstate->emit_state = EMITTING;
788 /* if it's a top-level struct/union definition or struct/union
789 * is anonymous, then in C we'll be emitting all fields and
790 * their types (as opposed to just `struct X`), so we need to
791 * make sure that all types, referenced from struct/union
792 * members have necessary forward-declarations, where
795 if (top_level_def || t->name_off == 0) {
796 const struct btf_member *m = btf_members(t);
797 __u16 vlen = btf_vlen(t);
800 new_cont_id = t->name_off == 0 ? cont_id : id;
801 for (i = 0; i < vlen; i++, m++)
802 btf_dump_emit_type(d, m->type, new_cont_id);
803 } else if (!tstate->fwd_emitted && id != cont_id) {
804 btf_dump_emit_struct_fwd(d, id, t);
805 btf_dump_printf(d, ";\n\n");
806 tstate->fwd_emitted = 1;
810 btf_dump_emit_struct_def(d, id, t, 0);
811 btf_dump_printf(d, ";\n\n");
812 tstate->emit_state = EMITTED;
814 tstate->emit_state = NOT_EMITTED;
817 case BTF_KIND_FUNC_PROTO: {
818 const struct btf_param *p = btf_params(t);
819 __u16 n = btf_vlen(t);
822 btf_dump_emit_type(d, t->type, cont_id);
823 for (i = 0; i < n; i++, p++)
824 btf_dump_emit_type(d, p->type, cont_id);
833 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
834 const struct btf_type *t)
836 const struct btf_member *m;
837 int max_align = 1, align, i, bit_sz;
842 /* all non-bitfield fields have to be naturally aligned */
843 for (i = 0; i < vlen; i++, m++) {
844 align = btf__align_of(btf, m->type);
845 bit_sz = btf_member_bitfield_size(t, i);
846 if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
848 max_align = max(align, max_align);
850 /* size of a non-packed struct has to be a multiple of its alignment */
851 if (t->size % max_align != 0)
854 * if original struct was marked as packed, but its layout is
855 * naturally aligned, we'll detect that it's not packed
860 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
861 int cur_off, int next_off, int next_align,
862 bool in_bitfield, int lvl)
868 {"long", d->ptr_sz * 8}, {"int", 32}, {"short", 16}, {"char", 8}
870 int new_off, pad_bits, bits, i;
871 const char *pad_type;
873 if (cur_off >= next_off)
876 /* For filling out padding we want to take advantage of
877 * natural alignment rules to minimize unnecessary explicit
878 * padding. First, we find the largest type (among long, int,
879 * short, or char) that can be used to force naturally aligned
880 * boundary. Once determined, we'll use such type to fill in
881 * the remaining padding gap. In some cases we can rely on
882 * compiler filling some gaps, but sometimes we need to force
883 * alignment to close natural alignment with markers like
884 * `long: 0` (this is always the case for bitfields). Note
885 * that even if struct itself has, let's say 4-byte alignment
886 * (i.e., it only uses up to int-aligned types), using `long:
887 * X;` explicit padding doesn't actually change struct's
888 * overall alignment requirements, but compiler does take into
889 * account that type's (long, in this example) natural
890 * alignment requirements when adding implicit padding. We use
891 * this fact heavily and don't worry about ruining correct
892 * struct alignment requirement.
894 for (i = 0; i < ARRAY_SIZE(pads); i++) {
895 pad_bits = pads[i].bits;
896 pad_type = pads[i].name;
898 new_off = roundup(cur_off, pad_bits);
899 if (new_off <= next_off)
903 if (new_off > cur_off && new_off <= next_off) {
904 /* We need explicit `<type>: 0` aligning mark if next
905 * field is right on alignment offset and its
906 * alignment requirement is less strict than <type>'s
907 * alignment (so compiler won't naturally align to the
908 * offset we expect), or if subsequent `<type>: X`,
909 * will actually completely fit in the remaining hole,
910 * making compiler basically ignore `<type>: X`
914 (new_off == next_off && roundup(cur_off, next_align * 8) != new_off) ||
915 (new_off != next_off && next_off - new_off <= new_off - cur_off))
916 /* but for bitfields we'll emit explicit bit count */
917 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type,
918 in_bitfield ? new_off - cur_off : 0);
922 /* Now we know we start at naturally aligned offset for a chosen
923 * padding type (long, int, short, or char), and so the rest is just
924 * a straightforward filling of remaining padding gap with full
925 * `<type>: sizeof(<type>);` markers, except for the last one, which
926 * might need smaller than sizeof(<type>) padding.
928 while (cur_off != next_off) {
929 bits = min(next_off - cur_off, pad_bits);
930 if (bits == pad_bits) {
931 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
935 /* For the remainder padding that doesn't cover entire
936 * pad_type bit length, we pick the smallest necessary type.
937 * This is pure aesthetics, we could have just used `long`,
938 * but having smallest necessary one communicates better the
939 * scale of the padding gap.
941 for (i = ARRAY_SIZE(pads) - 1; i >= 0; i--) {
942 pad_type = pads[i].name;
943 pad_bits = pads[i].bits;
947 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, bits);
954 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
955 const struct btf_type *t)
957 btf_dump_printf(d, "%s%s%s",
958 btf_is_struct(t) ? "struct" : "union",
959 t->name_off ? " " : "",
960 btf_dump_type_name(d, id));
963 static void btf_dump_emit_struct_def(struct btf_dump *d,
965 const struct btf_type *t,
968 const struct btf_member *m = btf_members(t);
969 bool is_struct = btf_is_struct(t);
970 bool packed, prev_bitfield = false;
971 int align, i, off = 0;
972 __u16 vlen = btf_vlen(t);
974 align = btf__align_of(d->btf, id);
975 packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
977 btf_dump_printf(d, "%s%s%s {",
978 is_struct ? "struct" : "union",
979 t->name_off ? " " : "",
980 btf_dump_type_name(d, id));
982 for (i = 0; i < vlen; i++, m++) {
984 int m_off, m_sz, m_align;
987 fname = btf_name_of(d, m->name_off);
988 m_sz = btf_member_bitfield_size(t, i);
989 m_off = btf_member_bit_offset(t, i);
990 m_align = packed ? 1 : btf__align_of(d->btf, m->type);
992 in_bitfield = prev_bitfield && m_sz != 0;
994 btf_dump_emit_bit_padding(d, off, m_off, m_align, in_bitfield, lvl + 1);
995 btf_dump_printf(d, "\n%s", pfx(lvl + 1));
996 btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
999 btf_dump_printf(d, ": %d", m_sz);
1001 prev_bitfield = true;
1003 m_sz = max((__s64)0, btf__resolve_size(d->btf, m->type));
1004 off = m_off + m_sz * 8;
1005 prev_bitfield = false;
1008 btf_dump_printf(d, ";");
1011 /* pad at the end, if necessary */
1013 btf_dump_emit_bit_padding(d, off, t->size * 8, align, false, lvl + 1);
1016 * Keep `struct empty {}` on a single line,
1017 * only print newline when there are regular or padding fields.
1019 if (vlen || t->size) {
1020 btf_dump_printf(d, "\n");
1021 btf_dump_printf(d, "%s}", pfx(lvl));
1023 btf_dump_printf(d, "}");
1026 btf_dump_printf(d, " __attribute__((packed))");
1029 static const char *missing_base_types[][2] = {
1031 * GCC emits typedefs to its internal __PolyX_t types when compiling Arm
1032 * SIMD intrinsics. Alias them to standard base types.
1034 { "__Poly8_t", "unsigned char" },
1035 { "__Poly16_t", "unsigned short" },
1036 { "__Poly64_t", "unsigned long long" },
1037 { "__Poly128_t", "unsigned __int128" },
1040 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
1041 const struct btf_type *t)
1043 const char *name = btf_dump_type_name(d, id);
1046 for (i = 0; i < ARRAY_SIZE(missing_base_types); i++) {
1047 if (strcmp(name, missing_base_types[i][0]) == 0) {
1048 btf_dump_printf(d, "typedef %s %s;\n\n",
1049 missing_base_types[i][1], name);
1055 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
1056 const struct btf_type *t)
1058 btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
1061 static void btf_dump_emit_enum32_val(struct btf_dump *d,
1062 const struct btf_type *t,
1063 int lvl, __u16 vlen)
1065 const struct btf_enum *v = btf_enum(t);
1066 bool is_signed = btf_kflag(t);
1067 const char *fmt_str;
1072 for (i = 0; i < vlen; i++, v++) {
1073 name = btf_name_of(d, v->name_off);
1074 /* enumerators share namespace with typedef idents */
1075 dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1077 fmt_str = is_signed ? "\n%s%s___%zd = %d," : "\n%s%s___%zd = %u,";
1078 btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, dup_cnt, v->val);
1080 fmt_str = is_signed ? "\n%s%s = %d," : "\n%s%s = %u,";
1081 btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, v->val);
1086 static void btf_dump_emit_enum64_val(struct btf_dump *d,
1087 const struct btf_type *t,
1088 int lvl, __u16 vlen)
1090 const struct btf_enum64 *v = btf_enum64(t);
1091 bool is_signed = btf_kflag(t);
1092 const char *fmt_str;
1098 for (i = 0; i < vlen; i++, v++) {
1099 name = btf_name_of(d, v->name_off);
1100 dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1101 val = btf_enum64_value(v);
1103 fmt_str = is_signed ? "\n%s%s___%zd = %lldLL,"
1104 : "\n%s%s___%zd = %lluULL,";
1105 btf_dump_printf(d, fmt_str,
1106 pfx(lvl + 1), name, dup_cnt,
1107 (unsigned long long)val);
1109 fmt_str = is_signed ? "\n%s%s = %lldLL,"
1110 : "\n%s%s = %lluULL,";
1111 btf_dump_printf(d, fmt_str,
1113 (unsigned long long)val);
1117 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
1118 const struct btf_type *t,
1121 __u16 vlen = btf_vlen(t);
1123 btf_dump_printf(d, "enum%s%s",
1124 t->name_off ? " " : "",
1125 btf_dump_type_name(d, id));
1130 btf_dump_printf(d, " {");
1132 btf_dump_emit_enum32_val(d, t, lvl, vlen);
1134 btf_dump_emit_enum64_val(d, t, lvl, vlen);
1135 btf_dump_printf(d, "\n%s}", pfx(lvl));
1137 /* special case enums with special sizes */
1139 /* one-byte enums can be forced with mode(byte) attribute */
1140 btf_dump_printf(d, " __attribute__((mode(byte)))");
1141 } else if (t->size == 8 && d->ptr_sz == 8) {
1142 /* enum can be 8-byte sized if one of the enumerator values
1143 * doesn't fit in 32-bit integer, or by adding mode(word)
1144 * attribute (but probably only on 64-bit architectures); do
1145 * our best here to try to satisfy the contract without adding
1146 * unnecessary attributes
1148 bool needs_word_mode;
1150 if (btf_is_enum(t)) {
1151 /* enum can't represent 64-bit values, so we need word mode */
1152 needs_word_mode = true;
1154 /* enum64 needs mode(word) if none of its values has
1155 * non-zero upper 32-bits (which means that all values
1156 * fit in 32-bit integers and won't cause compiler to
1157 * bump enum to be 64-bit naturally
1161 needs_word_mode = true;
1162 for (i = 0; i < vlen; i++) {
1163 if (btf_enum64(t)[i].val_hi32 != 0) {
1164 needs_word_mode = false;
1169 if (needs_word_mode)
1170 btf_dump_printf(d, " __attribute__((mode(word)))");
1175 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
1176 const struct btf_type *t)
1178 const char *name = btf_dump_type_name(d, id);
1181 btf_dump_printf(d, "union %s", name);
1183 btf_dump_printf(d, "struct %s", name);
1186 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
1187 const struct btf_type *t, int lvl)
1189 const char *name = btf_dump_ident_name(d, id);
1192 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
1193 * pointing to VOID. This generates warnings from btf_dump() and
1194 * results in uncompilable header file, so we are fixing it up here
1195 * with valid typedef into __builtin_va_list.
1197 if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
1198 btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
1202 btf_dump_printf(d, "typedef ");
1203 btf_dump_emit_type_decl(d, t->type, name, lvl);
1206 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
1211 if (d->decl_stack_cnt >= d->decl_stack_cap) {
1212 new_cap = max(16, d->decl_stack_cap * 3 / 2);
1213 new_stack = libbpf_reallocarray(d->decl_stack, new_cap, sizeof(new_stack[0]));
1216 d->decl_stack = new_stack;
1217 d->decl_stack_cap = new_cap;
1220 d->decl_stack[d->decl_stack_cnt++] = id;
1226 * Emit type declaration (e.g., field type declaration in a struct or argument
1227 * declaration in function prototype) in correct C syntax.
1229 * For most types it's trivial, but there are few quirky type declaration
1230 * cases worth mentioning:
1231 * - function prototypes (especially nesting of function prototypes);
1233 * - const/volatile/restrict for pointers vs other types.
1235 * For a good discussion of *PARSING* C syntax (as a human), see
1236 * Peter van der Linden's "Expert C Programming: Deep C Secrets",
1237 * Ch.3 "Unscrambling Declarations in C".
1239 * It won't help with BTF to C conversion much, though, as it's an opposite
1240 * problem. So we came up with this algorithm in reverse to van der Linden's
1241 * parsing algorithm. It goes from structured BTF representation of type
1242 * declaration to a valid compilable C syntax.
1244 * For instance, consider this C typedef:
1245 * typedef const int * const * arr[10] arr_t;
1246 * It will be represented in BTF with this chain of BTF types:
1247 * [typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1249 * Notice how [const] modifier always goes before type it modifies in BTF type
1250 * graph, but in C syntax, const/volatile/restrict modifiers are written to
1251 * the right of pointers, but to the left of other types. There are also other
1252 * quirks, like function pointers, arrays of them, functions returning other
1255 * We handle that by pushing all the types to a stack, until we hit "terminal"
1256 * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1257 * top of a stack, modifiers are handled differently. Array/function pointers
1258 * have also wildly different syntax and how nesting of them are done. See
1259 * code for authoritative definition.
1261 * To avoid allocating new stack for each independent chain of BTF types, we
1262 * share one bigger stack, with each chain working only on its own local view
1263 * of a stack frame. Some care is required to "pop" stack frames after
1264 * processing type declaration chain.
1266 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1267 const struct btf_dump_emit_type_decl_opts *opts)
1272 if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1273 return libbpf_err(-EINVAL);
1275 err = btf_dump_resize(d);
1277 return libbpf_err(err);
1279 fname = OPTS_GET(opts, field_name, "");
1280 lvl = OPTS_GET(opts, indent_level, 0);
1281 d->strip_mods = OPTS_GET(opts, strip_mods, false);
1282 btf_dump_emit_type_decl(d, id, fname, lvl);
1283 d->strip_mods = false;
1287 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1288 const char *fname, int lvl)
1290 struct id_stack decl_stack;
1291 const struct btf_type *t;
1292 int err, stack_start;
1294 stack_start = d->decl_stack_cnt;
1296 t = btf__type_by_id(d->btf, id);
1297 if (d->strip_mods && btf_is_mod(t))
1300 err = btf_dump_push_decl_stack_id(d, id);
1303 * if we don't have enough memory for entire type decl
1304 * chain, restore stack, emit warning, and try to
1305 * proceed nevertheless
1307 pr_warn("not enough memory for decl stack:%d", err);
1308 d->decl_stack_cnt = stack_start;
1316 switch (btf_kind(t)) {
1318 case BTF_KIND_VOLATILE:
1319 case BTF_KIND_CONST:
1320 case BTF_KIND_RESTRICT:
1321 case BTF_KIND_FUNC_PROTO:
1322 case BTF_KIND_TYPE_TAG:
1325 case BTF_KIND_ARRAY:
1326 id = btf_array(t)->type;
1330 case BTF_KIND_ENUM64:
1332 case BTF_KIND_STRUCT:
1333 case BTF_KIND_UNION:
1334 case BTF_KIND_TYPEDEF:
1335 case BTF_KIND_FLOAT:
1338 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1345 * We might be inside a chain of declarations (e.g., array of function
1346 * pointers returning anonymous (so inlined) structs, having another
1347 * array field). Each of those needs its own "stack frame" to handle
1348 * emitting of declarations. Those stack frames are non-overlapping
1349 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1350 * handle this set of nested stacks, we create a view corresponding to
1351 * our own "stack frame" and work with it as an independent stack.
1352 * We'll need to clean up after emit_type_chain() returns, though.
1354 decl_stack.ids = d->decl_stack + stack_start;
1355 decl_stack.cnt = d->decl_stack_cnt - stack_start;
1356 btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1358 * emit_type_chain() guarantees that it will pop its entire decl_stack
1359 * frame before returning. But it works with a read-only view into
1360 * decl_stack, so it doesn't actually pop anything from the
1361 * perspective of shared btf_dump->decl_stack, per se. We need to
1362 * reset decl_stack state to how it was before us to avoid it growing
1365 d->decl_stack_cnt = stack_start;
1368 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1370 const struct btf_type *t;
1373 while (decl_stack->cnt) {
1374 id = decl_stack->ids[decl_stack->cnt - 1];
1375 t = btf__type_by_id(d->btf, id);
1377 switch (btf_kind(t)) {
1378 case BTF_KIND_VOLATILE:
1379 btf_dump_printf(d, "volatile ");
1381 case BTF_KIND_CONST:
1382 btf_dump_printf(d, "const ");
1384 case BTF_KIND_RESTRICT:
1385 btf_dump_printf(d, "restrict ");
1394 static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1396 const struct btf_type *t;
1399 while (decl_stack->cnt) {
1400 id = decl_stack->ids[decl_stack->cnt - 1];
1401 t = btf__type_by_id(d->btf, id);
1408 static void btf_dump_emit_name(const struct btf_dump *d,
1409 const char *name, bool last_was_ptr)
1411 bool separate = name[0] && !last_was_ptr;
1413 btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1416 static void btf_dump_emit_type_chain(struct btf_dump *d,
1417 struct id_stack *decls,
1418 const char *fname, int lvl)
1421 * last_was_ptr is used to determine if we need to separate pointer
1422 * asterisk (*) from previous part of type signature with space, so
1423 * that we get `int ***`, instead of `int * * *`. We default to true
1424 * for cases where we have single pointer in a chain. E.g., in ptr ->
1425 * func_proto case. func_proto will start a new emit_type_chain call
1426 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1427 * don't want to prepend space for that last pointer.
1429 bool last_was_ptr = true;
1430 const struct btf_type *t;
1435 while (decls->cnt) {
1436 id = decls->ids[--decls->cnt];
1438 /* VOID is a special snowflake */
1439 btf_dump_emit_mods(d, decls);
1440 btf_dump_printf(d, "void");
1441 last_was_ptr = false;
1445 t = btf__type_by_id(d->btf, id);
1450 case BTF_KIND_FLOAT:
1451 btf_dump_emit_mods(d, decls);
1452 name = btf_name_of(d, t->name_off);
1453 btf_dump_printf(d, "%s", name);
1455 case BTF_KIND_STRUCT:
1456 case BTF_KIND_UNION:
1457 btf_dump_emit_mods(d, decls);
1458 /* inline anonymous struct/union */
1459 if (t->name_off == 0 && !d->skip_anon_defs)
1460 btf_dump_emit_struct_def(d, id, t, lvl);
1462 btf_dump_emit_struct_fwd(d, id, t);
1465 case BTF_KIND_ENUM64:
1466 btf_dump_emit_mods(d, decls);
1467 /* inline anonymous enum */
1468 if (t->name_off == 0 && !d->skip_anon_defs)
1469 btf_dump_emit_enum_def(d, id, t, lvl);
1471 btf_dump_emit_enum_fwd(d, id, t);
1474 btf_dump_emit_mods(d, decls);
1475 btf_dump_emit_fwd_def(d, id, t);
1477 case BTF_KIND_TYPEDEF:
1478 btf_dump_emit_mods(d, decls);
1479 btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1482 btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1484 case BTF_KIND_VOLATILE:
1485 btf_dump_printf(d, " volatile");
1487 case BTF_KIND_CONST:
1488 btf_dump_printf(d, " const");
1490 case BTF_KIND_RESTRICT:
1491 btf_dump_printf(d, " restrict");
1493 case BTF_KIND_TYPE_TAG:
1494 btf_dump_emit_mods(d, decls);
1495 name = btf_name_of(d, t->name_off);
1496 btf_dump_printf(d, " __attribute__((btf_type_tag(\"%s\")))", name);
1498 case BTF_KIND_ARRAY: {
1499 const struct btf_array *a = btf_array(t);
1500 const struct btf_type *next_t;
1505 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1506 * which causes it to emit extra const/volatile
1507 * modifiers for an array, if array's element type has
1508 * const/volatile modifiers. Clang doesn't do that.
1509 * In general, it doesn't seem very meaningful to have
1510 * a const/volatile modifier for array, so we are
1511 * going to silently skip them here.
1513 btf_dump_drop_mods(d, decls);
1515 if (decls->cnt == 0) {
1516 btf_dump_emit_name(d, fname, last_was_ptr);
1517 btf_dump_printf(d, "[%u]", a->nelems);
1521 next_id = decls->ids[decls->cnt - 1];
1522 next_t = btf__type_by_id(d->btf, next_id);
1523 multidim = btf_is_array(next_t);
1524 /* we need space if we have named non-pointer */
1525 if (fname[0] && !last_was_ptr)
1526 btf_dump_printf(d, " ");
1527 /* no parentheses for multi-dimensional array */
1529 btf_dump_printf(d, "(");
1530 btf_dump_emit_type_chain(d, decls, fname, lvl);
1532 btf_dump_printf(d, ")");
1533 btf_dump_printf(d, "[%u]", a->nelems);
1536 case BTF_KIND_FUNC_PROTO: {
1537 const struct btf_param *p = btf_params(t);
1538 __u16 vlen = btf_vlen(t);
1542 * GCC emits extra volatile qualifier for
1543 * __attribute__((noreturn)) function pointers. Clang
1544 * doesn't do it. It's a GCC quirk for backwards
1545 * compatibility with code written for GCC <2.5. So,
1546 * similarly to extra qualifiers for array, just drop
1547 * them, instead of handling them.
1549 btf_dump_drop_mods(d, decls);
1551 btf_dump_printf(d, " (");
1552 btf_dump_emit_type_chain(d, decls, fname, lvl);
1553 btf_dump_printf(d, ")");
1555 btf_dump_emit_name(d, fname, last_was_ptr);
1557 btf_dump_printf(d, "(");
1559 * Clang for BPF target generates func_proto with no
1560 * args as a func_proto with a single void arg (e.g.,
1561 * `int (*f)(void)` vs just `int (*f)()`). We are
1562 * going to pretend there are no args for such case.
1564 if (vlen == 1 && p->type == 0) {
1565 btf_dump_printf(d, ")");
1569 for (i = 0; i < vlen; i++, p++) {
1571 btf_dump_printf(d, ", ");
1573 /* last arg of type void is vararg */
1574 if (i == vlen - 1 && p->type == 0) {
1575 btf_dump_printf(d, "...");
1579 name = btf_name_of(d, p->name_off);
1580 btf_dump_emit_type_decl(d, p->type, name, lvl);
1583 btf_dump_printf(d, ")");
1587 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1592 last_was_ptr = kind == BTF_KIND_PTR;
1595 btf_dump_emit_name(d, fname, last_was_ptr);
1598 /* show type name as (type_name) */
1599 static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id,
1602 const struct btf_type *t;
1604 /* for array members, we don't bother emitting type name for each
1605 * member to avoid the redundancy of
1606 * .name = (char[4])[(char)'f',(char)'o',(char)'o',]
1608 if (d->typed_dump->is_array_member)
1611 /* avoid type name specification for variable/section; it will be done
1612 * for the associated variable value(s).
1614 t = btf__type_by_id(d->btf, id);
1615 if (btf_is_var(t) || btf_is_datasec(t))
1619 btf_dump_printf(d, "(");
1621 d->skip_anon_defs = true;
1622 d->strip_mods = true;
1623 btf_dump_emit_type_decl(d, id, "", 0);
1624 d->strip_mods = false;
1625 d->skip_anon_defs = false;
1628 btf_dump_printf(d, ")");
1631 /* return number of duplicates (occurrences) of a given name */
1632 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1633 const char *orig_name)
1635 char *old_name, *new_name;
1639 new_name = strdup(orig_name);
1643 (void)hashmap__find(name_map, orig_name, &dup_cnt);
1646 err = hashmap__set(name_map, new_name, dup_cnt, &old_name, NULL);
1655 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1656 struct hashmap *name_map)
1658 struct btf_dump_type_aux_state *s = &d->type_states[id];
1659 const struct btf_type *t = btf__type_by_id(d->btf, id);
1660 const char *orig_name = btf_name_of(d, t->name_off);
1661 const char **cached_name = &d->cached_names[id];
1664 if (t->name_off == 0)
1667 if (s->name_resolved)
1668 return *cached_name ? *cached_name : orig_name;
1670 if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
1671 s->name_resolved = 1;
1675 dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1677 const size_t max_len = 256;
1678 char new_name[max_len];
1680 snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1681 *cached_name = strdup(new_name);
1684 s->name_resolved = 1;
1685 return *cached_name ? *cached_name : orig_name;
1688 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1690 return btf_dump_resolve_name(d, id, d->type_names);
1693 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1695 return btf_dump_resolve_name(d, id, d->ident_names);
1698 static int btf_dump_dump_type_data(struct btf_dump *d,
1700 const struct btf_type *t,
1706 static const char *btf_dump_data_newline(struct btf_dump *d)
1708 return d->typed_dump->compact || d->typed_dump->depth == 0 ? "" : "\n";
1711 static const char *btf_dump_data_delim(struct btf_dump *d)
1713 return d->typed_dump->depth == 0 ? "" : ",";
1716 static void btf_dump_data_pfx(struct btf_dump *d)
1718 int i, lvl = d->typed_dump->indent_lvl + d->typed_dump->depth;
1720 if (d->typed_dump->compact)
1723 for (i = 0; i < lvl; i++)
1724 btf_dump_printf(d, "%s", d->typed_dump->indent_str);
1727 /* A macro is used here as btf_type_value[s]() appends format specifiers
1728 * to the format specifier passed in; these do the work of appending
1729 * delimiters etc while the caller simply has to specify the type values
1730 * in the format specifier + value(s).
1732 #define btf_dump_type_values(d, fmt, ...) \
1733 btf_dump_printf(d, fmt "%s%s", \
1735 btf_dump_data_delim(d), \
1736 btf_dump_data_newline(d))
1738 static int btf_dump_unsupported_data(struct btf_dump *d,
1739 const struct btf_type *t,
1742 btf_dump_printf(d, "<unsupported kind:%u>", btf_kind(t));
1746 static int btf_dump_get_bitfield_value(struct btf_dump *d,
1747 const struct btf_type *t,
1753 __u16 left_shift_bits, right_shift_bits;
1754 const __u8 *bytes = data;
1759 /* Maximum supported bitfield size is 64 bits */
1761 pr_warn("unexpected bitfield size %d\n", t->size);
1765 /* Bitfield value retrieval is done in two steps; first relevant bytes are
1766 * stored in num, then we left/right shift num to eliminate irrelevant bits.
1768 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1769 for (i = t->size - 1; i >= 0; i--)
1770 num = num * 256 + bytes[i];
1771 nr_copy_bits = bit_sz + bits_offset;
1772 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1773 for (i = 0; i < t->size; i++)
1774 num = num * 256 + bytes[i];
1775 nr_copy_bits = t->size * 8 - bits_offset;
1777 # error "Unrecognized __BYTE_ORDER__"
1779 left_shift_bits = 64 - nr_copy_bits;
1780 right_shift_bits = 64 - bit_sz;
1782 *value = (num << left_shift_bits) >> right_shift_bits;
1787 static int btf_dump_bitfield_check_zero(struct btf_dump *d,
1788 const struct btf_type *t,
1796 err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &check_num);
1804 static int btf_dump_bitfield_data(struct btf_dump *d,
1805 const struct btf_type *t,
1813 err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &print_num);
1817 btf_dump_type_values(d, "0x%llx", (unsigned long long)print_num);
1822 /* ints, floats and ptrs */
1823 static int btf_dump_base_type_check_zero(struct btf_dump *d,
1824 const struct btf_type *t,
1828 static __u8 bytecmp[16] = {};
1831 /* For pointer types, pointer size is not defined on a per-type basis.
1832 * On dump creation however, we store the pointer size.
1834 if (btf_kind(t) == BTF_KIND_PTR)
1835 nr_bytes = d->ptr_sz;
1839 if (nr_bytes < 1 || nr_bytes > 16) {
1840 pr_warn("unexpected size %d for id [%u]\n", nr_bytes, id);
1844 if (memcmp(data, bytecmp, nr_bytes) == 0)
1849 static bool ptr_is_aligned(const struct btf *btf, __u32 type_id,
1852 int alignment = btf__align_of(btf, type_id);
1857 return ((uintptr_t)data) % alignment == 0;
1860 static int btf_dump_int_data(struct btf_dump *d,
1861 const struct btf_type *t,
1866 __u8 encoding = btf_int_encoding(t);
1867 bool sign = encoding & BTF_INT_SIGNED;
1868 char buf[16] __attribute__((aligned(16)));
1871 if (sz == 0 || sz > sizeof(buf)) {
1872 pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1876 /* handle packed int data - accesses of integers not aligned on
1877 * int boundaries can cause problems on some platforms.
1879 if (!ptr_is_aligned(d->btf, type_id, data)) {
1880 memcpy(buf, data, sz);
1886 const __u64 *ints = data;
1889 /* avoid use of __int128 as some 32-bit platforms do not
1892 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1895 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1899 # error "Unrecognized __BYTE_ORDER__"
1902 btf_dump_type_values(d, "0x%llx", (unsigned long long)lsi);
1904 btf_dump_type_values(d, "0x%llx%016llx", (unsigned long long)msi,
1905 (unsigned long long)lsi);
1910 btf_dump_type_values(d, "%lld", *(long long *)data);
1912 btf_dump_type_values(d, "%llu", *(unsigned long long *)data);
1916 btf_dump_type_values(d, "%d", *(__s32 *)data);
1918 btf_dump_type_values(d, "%u", *(__u32 *)data);
1922 btf_dump_type_values(d, "%d", *(__s16 *)data);
1924 btf_dump_type_values(d, "%u", *(__u16 *)data);
1927 if (d->typed_dump->is_array_char) {
1928 /* check for null terminator */
1929 if (d->typed_dump->is_array_terminated)
1931 if (*(char *)data == '\0') {
1932 d->typed_dump->is_array_terminated = true;
1935 if (isprint(*(char *)data)) {
1936 btf_dump_type_values(d, "'%c'", *(char *)data);
1941 btf_dump_type_values(d, "%d", *(__s8 *)data);
1943 btf_dump_type_values(d, "%u", *(__u8 *)data);
1946 pr_warn("unexpected sz %d for id [%u]\n", sz, type_id);
1958 static int btf_dump_float_data(struct btf_dump *d,
1959 const struct btf_type *t,
1963 const union float_data *flp = data;
1964 union float_data fl;
1967 /* handle unaligned data; copy to local union */
1968 if (!ptr_is_aligned(d->btf, type_id, data)) {
1969 memcpy(&fl, data, sz);
1975 btf_dump_type_values(d, "%Lf", flp->ld);
1978 btf_dump_type_values(d, "%lf", flp->d);
1981 btf_dump_type_values(d, "%f", flp->f);
1984 pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1990 static int btf_dump_var_data(struct btf_dump *d,
1991 const struct btf_type *v,
1995 enum btf_func_linkage linkage = btf_var(v)->linkage;
1996 const struct btf_type *t;
2001 case BTF_FUNC_STATIC:
2004 case BTF_FUNC_EXTERN:
2007 case BTF_FUNC_GLOBAL:
2013 /* format of output here is [linkage] [type] [varname] = (type)value,
2014 * for example "static int cpu_profile_flip = (int)1"
2016 btf_dump_printf(d, "%s", l);
2018 t = btf__type_by_id(d->btf, type_id);
2019 btf_dump_emit_type_cast(d, type_id, false);
2020 btf_dump_printf(d, " %s = ", btf_name_of(d, v->name_off));
2021 return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0);
2024 static int btf_dump_array_data(struct btf_dump *d,
2025 const struct btf_type *t,
2029 const struct btf_array *array = btf_array(t);
2030 const struct btf_type *elem_type;
2031 __u32 i, elem_type_id;
2033 bool is_array_member;
2035 elem_type_id = array->type;
2036 elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2037 elem_size = btf__resolve_size(d->btf, elem_type_id);
2038 if (elem_size <= 0) {
2039 pr_warn("unexpected elem size %zd for array type [%u]\n",
2040 (ssize_t)elem_size, id);
2044 if (btf_is_int(elem_type)) {
2046 * BTF_INT_CHAR encoding never seems to be set for
2047 * char arrays, so if size is 1 and element is
2048 * printable as a char, we'll do that.
2051 d->typed_dump->is_array_char = true;
2054 /* note that we increment depth before calling btf_dump_print() below;
2055 * this is intentional. btf_dump_data_newline() will not print a
2056 * newline for depth 0 (since this leaves us with trailing newlines
2057 * at the end of typed display), so depth is incremented first.
2058 * For similar reasons, we decrement depth before showing the closing
2061 d->typed_dump->depth++;
2062 btf_dump_printf(d, "[%s", btf_dump_data_newline(d));
2064 /* may be a multidimensional array, so store current "is array member"
2065 * status so we can restore it correctly later.
2067 is_array_member = d->typed_dump->is_array_member;
2068 d->typed_dump->is_array_member = true;
2069 for (i = 0; i < array->nelems; i++, data += elem_size) {
2070 if (d->typed_dump->is_array_terminated)
2072 btf_dump_dump_type_data(d, NULL, elem_type, elem_type_id, data, 0, 0);
2074 d->typed_dump->is_array_member = is_array_member;
2075 d->typed_dump->depth--;
2076 btf_dump_data_pfx(d);
2077 btf_dump_type_values(d, "]");
2082 static int btf_dump_struct_data(struct btf_dump *d,
2083 const struct btf_type *t,
2087 const struct btf_member *m = btf_members(t);
2088 __u16 n = btf_vlen(t);
2091 /* note that we increment depth before calling btf_dump_print() below;
2092 * this is intentional. btf_dump_data_newline() will not print a
2093 * newline for depth 0 (since this leaves us with trailing newlines
2094 * at the end of typed display), so depth is incremented first.
2095 * For similar reasons, we decrement depth before showing the closing
2098 d->typed_dump->depth++;
2099 btf_dump_printf(d, "{%s", btf_dump_data_newline(d));
2101 for (i = 0; i < n; i++, m++) {
2102 const struct btf_type *mtype;
2107 mtype = btf__type_by_id(d->btf, m->type);
2108 mname = btf_name_of(d, m->name_off);
2109 moffset = btf_member_bit_offset(t, i);
2111 bit_sz = btf_member_bitfield_size(t, i);
2112 err = btf_dump_dump_type_data(d, mname, mtype, m->type, data + moffset / 8,
2113 moffset % 8, bit_sz);
2117 d->typed_dump->depth--;
2118 btf_dump_data_pfx(d);
2119 btf_dump_type_values(d, "}");
2125 unsigned long long lp;
2128 static int btf_dump_ptr_data(struct btf_dump *d,
2129 const struct btf_type *t,
2133 if (ptr_is_aligned(d->btf, id, data) && d->ptr_sz == sizeof(void *)) {
2134 btf_dump_type_values(d, "%p", *(void **)data);
2138 memcpy(&pt, data, d->ptr_sz);
2140 btf_dump_type_values(d, "0x%x", pt.p);
2142 btf_dump_type_values(d, "0x%llx", pt.lp);
2147 static int btf_dump_get_enum_value(struct btf_dump *d,
2148 const struct btf_type *t,
2153 bool is_signed = btf_kflag(t);
2155 if (!ptr_is_aligned(d->btf, id, data)) {
2159 err = btf_dump_get_bitfield_value(d, t, data, 0, 0, &val);
2162 *value = (__s64)val;
2168 *value = *(__s64 *)data;
2171 *value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;
2174 *value = is_signed ? *(__s16 *)data : *(__u16 *)data;
2177 *value = is_signed ? *(__s8 *)data : *(__u8 *)data;
2180 pr_warn("unexpected size %d for enum, id:[%u]\n", t->size, id);
2185 static int btf_dump_enum_data(struct btf_dump *d,
2186 const struct btf_type *t,
2194 err = btf_dump_get_enum_value(d, t, data, id, &value);
2198 is_signed = btf_kflag(t);
2199 if (btf_is_enum(t)) {
2200 const struct btf_enum *e;
2202 for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
2203 if (value != e->val)
2205 btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2209 btf_dump_type_values(d, is_signed ? "%d" : "%u", value);
2211 const struct btf_enum64 *e;
2213 for (i = 0, e = btf_enum64(t); i < btf_vlen(t); i++, e++) {
2214 if (value != btf_enum64_value(e))
2216 btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2220 btf_dump_type_values(d, is_signed ? "%lldLL" : "%lluULL",
2221 (unsigned long long)value);
2226 static int btf_dump_datasec_data(struct btf_dump *d,
2227 const struct btf_type *t,
2231 const struct btf_var_secinfo *vsi;
2232 const struct btf_type *var;
2236 btf_dump_type_values(d, "SEC(\"%s\") ", btf_name_of(d, t->name_off));
2238 for (i = 0, vsi = btf_var_secinfos(t); i < btf_vlen(t); i++, vsi++) {
2239 var = btf__type_by_id(d->btf, vsi->type);
2240 err = btf_dump_dump_type_data(d, NULL, var, vsi->type, data + vsi->offset, 0, 0);
2243 btf_dump_printf(d, ";");
2248 /* return size of type, or if base type overflows, return -E2BIG. */
2249 static int btf_dump_type_data_check_overflow(struct btf_dump *d,
2250 const struct btf_type *t,
2259 /* bits_offset is at most 7. bit_sz is at most 128. */
2260 __u8 nr_bytes = (bits_offset + bit_sz + 7) / 8;
2262 /* When bit_sz is non zero, it is called from
2263 * btf_dump_struct_data() where it only cares about
2264 * negative error value.
2265 * Return nr_bytes in success case to make it
2266 * consistent as the regular integer case below.
2268 return data + nr_bytes > d->typed_dump->data_end ? -E2BIG : nr_bytes;
2271 size = btf__resolve_size(d->btf, id);
2273 if (size < 0 || size >= INT_MAX) {
2274 pr_warn("unexpected size [%zu] for id [%u]\n",
2279 /* Only do overflow checking for base types; we do not want to
2280 * avoid showing part of a struct, union or array, even if we
2281 * do not have enough data to show the full object. By
2282 * restricting overflow checking to base types we can ensure
2283 * that partial display succeeds, while avoiding overflowing
2284 * and using bogus data for display.
2286 t = skip_mods_and_typedefs(d->btf, id, NULL);
2288 pr_warn("unexpected error skipping mods/typedefs for id [%u]\n",
2293 switch (btf_kind(t)) {
2295 case BTF_KIND_FLOAT:
2298 case BTF_KIND_ENUM64:
2299 if (data + bits_offset / 8 + size > d->typed_dump->data_end)
2308 static int btf_dump_type_data_check_zero(struct btf_dump *d,
2309 const struct btf_type *t,
2318 /* toplevel exceptions; we show zero values if
2319 * - we ask for them (emit_zeros)
2320 * - if we are at top-level so we see "struct empty { }"
2321 * - or if we are an array member and the array is non-empty and
2322 * not a char array; we don't want to be in a situation where we
2323 * have an integer array 0, 1, 0, 1 and only show non-zero values.
2324 * If the array contains zeroes only, or is a char array starting
2325 * with a '\0', the array-level check_zero() will prevent showing it;
2326 * we are concerned with determining zero value at the array member
2329 if (d->typed_dump->emit_zeroes || d->typed_dump->depth == 0 ||
2330 (d->typed_dump->is_array_member &&
2331 !d->typed_dump->is_array_char))
2334 t = skip_mods_and_typedefs(d->btf, id, NULL);
2336 switch (btf_kind(t)) {
2339 return btf_dump_bitfield_check_zero(d, t, data, bits_offset, bit_sz);
2340 return btf_dump_base_type_check_zero(d, t, id, data);
2341 case BTF_KIND_FLOAT:
2343 return btf_dump_base_type_check_zero(d, t, id, data);
2344 case BTF_KIND_ARRAY: {
2345 const struct btf_array *array = btf_array(t);
2346 const struct btf_type *elem_type;
2347 __u32 elem_type_id, elem_size;
2350 elem_type_id = array->type;
2351 elem_size = btf__resolve_size(d->btf, elem_type_id);
2352 elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2354 ischar = btf_is_int(elem_type) && elem_size == 1;
2356 /* check all elements; if _any_ element is nonzero, all
2357 * of array is displayed. We make an exception however
2358 * for char arrays where the first element is 0; these
2359 * are considered zeroed also, even if later elements are
2360 * non-zero because the string is terminated.
2362 for (i = 0; i < array->nelems; i++) {
2363 if (i == 0 && ischar && *(char *)data == 0)
2365 err = btf_dump_type_data_check_zero(d, elem_type,
2370 if (err != -ENODATA)
2375 case BTF_KIND_STRUCT:
2376 case BTF_KIND_UNION: {
2377 const struct btf_member *m = btf_members(t);
2378 __u16 n = btf_vlen(t);
2380 /* if any struct/union member is non-zero, the struct/union
2381 * is considered non-zero and dumped.
2383 for (i = 0; i < n; i++, m++) {
2384 const struct btf_type *mtype;
2387 mtype = btf__type_by_id(d->btf, m->type);
2388 moffset = btf_member_bit_offset(t, i);
2390 /* btf_int_bits() does not store member bitfield size;
2391 * bitfield size needs to be stored here so int display
2392 * of member can retrieve it.
2394 bit_sz = btf_member_bitfield_size(t, i);
2395 err = btf_dump_type_data_check_zero(d, mtype, m->type, data + moffset / 8,
2396 moffset % 8, bit_sz);
2403 case BTF_KIND_ENUM64:
2404 err = btf_dump_get_enum_value(d, t, data, id, &value);
2415 /* returns size of data dumped, or error. */
2416 static int btf_dump_dump_type_data(struct btf_dump *d,
2418 const struct btf_type *t,
2426 size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz);
2429 err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);
2431 /* zeroed data is expected and not an error, so simply skip
2432 * dumping such data. Record other errors however.
2434 if (err == -ENODATA)
2438 btf_dump_data_pfx(d);
2440 if (!d->typed_dump->skip_names) {
2441 if (fname && strlen(fname) > 0)
2442 btf_dump_printf(d, ".%s = ", fname);
2443 btf_dump_emit_type_cast(d, id, true);
2446 t = skip_mods_and_typedefs(d->btf, id, NULL);
2448 switch (btf_kind(t)) {
2452 case BTF_KIND_FUNC_PROTO:
2453 case BTF_KIND_DECL_TAG:
2454 err = btf_dump_unsupported_data(d, t, id);
2458 err = btf_dump_bitfield_data(d, t, data, bits_offset, bit_sz);
2460 err = btf_dump_int_data(d, t, id, data, bits_offset);
2462 case BTF_KIND_FLOAT:
2463 err = btf_dump_float_data(d, t, id, data);
2466 err = btf_dump_ptr_data(d, t, id, data);
2468 case BTF_KIND_ARRAY:
2469 err = btf_dump_array_data(d, t, id, data);
2471 case BTF_KIND_STRUCT:
2472 case BTF_KIND_UNION:
2473 err = btf_dump_struct_data(d, t, id, data);
2476 case BTF_KIND_ENUM64:
2477 /* handle bitfield and int enum values */
2482 err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz,
2486 enum_val = (__s64)print_num;
2487 err = btf_dump_enum_data(d, t, id, &enum_val);
2489 err = btf_dump_enum_data(d, t, id, data);
2492 err = btf_dump_var_data(d, t, id, data);
2494 case BTF_KIND_DATASEC:
2495 err = btf_dump_datasec_data(d, t, id, data);
2498 pr_warn("unexpected kind [%u] for id [%u]\n",
2499 BTF_INFO_KIND(t->info), id);
2507 int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
2508 const void *data, size_t data_sz,
2509 const struct btf_dump_type_data_opts *opts)
2511 struct btf_dump_data typed_dump = {};
2512 const struct btf_type *t;
2515 if (!OPTS_VALID(opts, btf_dump_type_data_opts))
2516 return libbpf_err(-EINVAL);
2518 t = btf__type_by_id(d->btf, id);
2520 return libbpf_err(-ENOENT);
2522 d->typed_dump = &typed_dump;
2523 d->typed_dump->data_end = data + data_sz;
2524 d->typed_dump->indent_lvl = OPTS_GET(opts, indent_level, 0);
2526 /* default indent string is a tab */
2527 if (!OPTS_GET(opts, indent_str, NULL))
2528 d->typed_dump->indent_str[0] = '\t';
2530 libbpf_strlcpy(d->typed_dump->indent_str, opts->indent_str,
2531 sizeof(d->typed_dump->indent_str));
2533 d->typed_dump->compact = OPTS_GET(opts, compact, false);
2534 d->typed_dump->skip_names = OPTS_GET(opts, skip_names, false);
2535 d->typed_dump->emit_zeroes = OPTS_GET(opts, emit_zeroes, false);
2537 ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
2539 d->typed_dump = NULL;
2541 return libbpf_err(ret);