1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3 /*
4 * BTF-to-C type converter.
5 *
6 * Copyright (c) 2019 Facebook
7 */
8
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <endian.h>
15 #include <errno.h>
16 #include <limits.h>
17 #include <linux/err.h>
18 #include <linux/btf.h>
19 #include <linux/kernel.h>
20 #include "btf.h"
21 #include "hashmap.h"
22 #include "libbpf.h"
23 #include "libbpf_internal.h"
24 #include "str_error.h"
25
26 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
27 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
28
pfx(int lvl)29 static const char *pfx(int lvl)
30 {
31 return lvl >= PREFIX_CNT ? PREFIXES : &PREFIXES[PREFIX_CNT - lvl];
32 }
33
34 enum btf_dump_type_order_state {
35 NOT_ORDERED,
36 ORDERING,
37 ORDERED,
38 };
39
40 enum btf_dump_type_emit_state {
41 NOT_EMITTED,
42 EMITTING,
43 EMITTED,
44 };
45
46 /* per-type auxiliary state */
47 struct btf_dump_type_aux_state {
48 /* topological sorting state */
49 enum btf_dump_type_order_state order_state: 2;
50 /* emitting state used to determine the need for forward declaration */
51 enum btf_dump_type_emit_state emit_state: 2;
52 /* whether forward declaration was already emitted */
53 __u8 fwd_emitted: 1;
54 /* whether unique non-duplicate name was already assigned */
55 __u8 name_resolved: 1;
56 /* whether type is referenced from any other type */
57 __u8 referenced: 1;
58 };
59
60 /* indent string length; one indent string is added for each indent level */
61 #define BTF_DATA_INDENT_STR_LEN 32
62
63 /*
64 * Common internal data for BTF type data dump operations.
65 */
66 struct btf_dump_data {
67 const void *data_end; /* end of valid data to show */
68 bool compact;
69 bool skip_names;
70 bool emit_zeroes;
71 __u8 indent_lvl; /* base indent level */
72 char indent_str[BTF_DATA_INDENT_STR_LEN];
73 /* below are used during iteration */
74 int depth;
75 bool is_array_member;
76 bool is_array_terminated;
77 bool is_array_char;
78 };
79
80 struct btf_dump {
81 const struct btf *btf;
82 btf_dump_printf_fn_t printf_fn;
83 void *cb_ctx;
84 int ptr_sz;
85 bool strip_mods;
86 bool skip_anon_defs;
87 int last_id;
88
89 /* per-type auxiliary state */
90 struct btf_dump_type_aux_state *type_states;
91 size_t type_states_cap;
92 /* per-type optional cached unique name, must be freed, if present */
93 const char **cached_names;
94 size_t cached_names_cap;
95
96 /* topo-sorted list of dependent type definitions */
97 __u32 *emit_queue;
98 int emit_queue_cap;
99 int emit_queue_cnt;
100
101 /*
102 * stack of type declarations (e.g., chain of modifiers, arrays,
103 * funcs, etc)
104 */
105 __u32 *decl_stack;
106 int decl_stack_cap;
107 int decl_stack_cnt;
108
109 /* maps struct/union/enum name to a number of name occurrences */
110 struct hashmap *type_names;
111 /*
112 * maps typedef identifiers and enum value names to a number of such
113 * name occurrences
114 */
115 struct hashmap *ident_names;
116 /*
117 * data for typed display; allocated if needed.
118 */
119 struct btf_dump_data *typed_dump;
120 };
121
str_hash_fn(long key,void * ctx)122 static size_t str_hash_fn(long key, void *ctx)
123 {
124 return str_hash((void *)key);
125 }
126
str_equal_fn(long a,long b,void * ctx)127 static bool str_equal_fn(long a, long b, void *ctx)
128 {
129 return strcmp((void *)a, (void *)b) == 0;
130 }
131
btf_name_of(const struct btf_dump * d,__u32 name_off)132 static const char *btf_name_of(const struct btf_dump *d, __u32 name_off)
133 {
134 return btf__name_by_offset(d->btf, name_off);
135 }
136
btf_dump_printf(const struct btf_dump * d,const char * fmt,...)137 static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
138 {
139 va_list args;
140
141 va_start(args, fmt);
142 d->printf_fn(d->cb_ctx, fmt, args);
143 va_end(args);
144 }
145
146 static int btf_dump_mark_referenced(struct btf_dump *d);
147 static int btf_dump_resize(struct btf_dump *d);
148
btf_dump__new(const struct btf * btf,btf_dump_printf_fn_t printf_fn,void * ctx,const struct btf_dump_opts * opts)149 struct btf_dump *btf_dump__new(const struct btf *btf,
150 btf_dump_printf_fn_t printf_fn,
151 void *ctx,
152 const struct btf_dump_opts *opts)
153 {
154 struct btf_dump *d;
155 int err;
156
157 if (!OPTS_VALID(opts, btf_dump_opts))
158 return libbpf_err_ptr(-EINVAL);
159
160 if (!printf_fn)
161 return libbpf_err_ptr(-EINVAL);
162
163 d = calloc(1, sizeof(struct btf_dump));
164 if (!d)
165 return libbpf_err_ptr(-ENOMEM);
166
167 d->btf = btf;
168 d->printf_fn = printf_fn;
169 d->cb_ctx = ctx;
170 d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *);
171
172 d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
173 if (IS_ERR(d->type_names)) {
174 err = PTR_ERR(d->type_names);
175 d->type_names = NULL;
176 goto err;
177 }
178 d->ident_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
179 if (IS_ERR(d->ident_names)) {
180 err = PTR_ERR(d->ident_names);
181 d->ident_names = NULL;
182 goto err;
183 }
184
185 err = btf_dump_resize(d);
186 if (err)
187 goto err;
188
189 return d;
190 err:
191 btf_dump__free(d);
192 return libbpf_err_ptr(err);
193 }
194
btf_dump_resize(struct btf_dump * d)195 static int btf_dump_resize(struct btf_dump *d)
196 {
197 int err, last_id = btf__type_cnt(d->btf) - 1;
198
199 if (last_id <= d->last_id)
200 return 0;
201
202 if (libbpf_ensure_mem((void **)&d->type_states, &d->type_states_cap,
203 sizeof(*d->type_states), last_id + 1))
204 return -ENOMEM;
205 if (libbpf_ensure_mem((void **)&d->cached_names, &d->cached_names_cap,
206 sizeof(*d->cached_names), last_id + 1))
207 return -ENOMEM;
208
209 if (d->last_id == 0) {
210 /* VOID is special */
211 d->type_states[0].order_state = ORDERED;
212 d->type_states[0].emit_state = EMITTED;
213 }
214
215 /* eagerly determine referenced types for anon enums */
216 err = btf_dump_mark_referenced(d);
217 if (err)
218 return err;
219
220 d->last_id = last_id;
221 return 0;
222 }
223
btf_dump_free_names(struct hashmap * map)224 static void btf_dump_free_names(struct hashmap *map)
225 {
226 size_t bkt;
227 struct hashmap_entry *cur;
228
229 if (!map)
230 return;
231
232 hashmap__for_each_entry(map, cur, bkt)
233 free((void *)cur->pkey);
234
235 hashmap__free(map);
236 }
237
btf_dump__free(struct btf_dump * d)238 void btf_dump__free(struct btf_dump *d)
239 {
240 int i;
241
242 if (IS_ERR_OR_NULL(d))
243 return;
244
245 free(d->type_states);
246 if (d->cached_names) {
247 /* any set cached name is owned by us and should be freed */
248 for (i = 0; i <= d->last_id; i++) {
249 if (d->cached_names[i])
250 free((void *)d->cached_names[i]);
251 }
252 }
253 free(d->cached_names);
254 free(d->emit_queue);
255 free(d->decl_stack);
256 btf_dump_free_names(d->type_names);
257 btf_dump_free_names(d->ident_names);
258
259 free(d);
260 }
261
262 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr);
263 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id);
264
265 /*
266 * Dump BTF type in a compilable C syntax, including all the necessary
267 * dependent types, necessary for compilation. If some of the dependent types
268 * were already emitted as part of previous btf_dump__dump_type() invocation
269 * for another type, they won't be emitted again. This API allows callers to
270 * filter out BTF types according to user-defined criterias and emitted only
271 * minimal subset of types, necessary to compile everything. Full struct/union
272 * definitions will still be emitted, even if the only usage is through
273 * pointer and could be satisfied with just a forward declaration.
274 *
275 * Dumping is done in two high-level passes:
276 * 1. Topologically sort type definitions to satisfy C rules of compilation.
277 * 2. Emit type definitions in C syntax.
278 *
279 * Returns 0 on success; <0, otherwise.
280 */
btf_dump__dump_type(struct btf_dump * d,__u32 id)281 int btf_dump__dump_type(struct btf_dump *d, __u32 id)
282 {
283 int err, i;
284
285 if (id >= btf__type_cnt(d->btf))
286 return libbpf_err(-EINVAL);
287
288 err = btf_dump_resize(d);
289 if (err)
290 return libbpf_err(err);
291
292 d->emit_queue_cnt = 0;
293 err = btf_dump_order_type(d, id, false);
294 if (err < 0)
295 return libbpf_err(err);
296
297 for (i = 0; i < d->emit_queue_cnt; i++)
298 btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
299
300 return 0;
301 }
302
303 /*
304 * Mark all types that are referenced from any other type. This is used to
305 * determine top-level anonymous enums that need to be emitted as an
306 * independent type declarations.
307 * Anonymous enums come in two flavors: either embedded in a struct's field
308 * definition, in which case they have to be declared inline as part of field
309 * type declaration; or as a top-level anonymous enum, typically used for
310 * declaring global constants. It's impossible to distinguish between two
311 * without knowing whether given enum type was referenced from other type:
312 * top-level anonymous enum won't be referenced by anything, while embedded
313 * one will.
314 */
btf_dump_mark_referenced(struct btf_dump * d)315 static int btf_dump_mark_referenced(struct btf_dump *d)
316 {
317 int i, j, n = btf__type_cnt(d->btf);
318 const struct btf_type *t;
319 __u16 vlen;
320
321 for (i = d->last_id + 1; i < n; i++) {
322 t = btf__type_by_id(d->btf, i);
323 vlen = btf_vlen(t);
324
325 switch (btf_kind(t)) {
326 case BTF_KIND_INT:
327 case BTF_KIND_ENUM:
328 case BTF_KIND_ENUM64:
329 case BTF_KIND_FWD:
330 case BTF_KIND_FLOAT:
331 break;
332
333 case BTF_KIND_VOLATILE:
334 case BTF_KIND_CONST:
335 case BTF_KIND_RESTRICT:
336 case BTF_KIND_PTR:
337 case BTF_KIND_TYPEDEF:
338 case BTF_KIND_FUNC:
339 case BTF_KIND_VAR:
340 case BTF_KIND_DECL_TAG:
341 case BTF_KIND_TYPE_TAG:
342 d->type_states[t->type].referenced = 1;
343 break;
344
345 case BTF_KIND_ARRAY: {
346 const struct btf_array *a = btf_array(t);
347
348 d->type_states[a->index_type].referenced = 1;
349 d->type_states[a->type].referenced = 1;
350 break;
351 }
352 case BTF_KIND_STRUCT:
353 case BTF_KIND_UNION: {
354 const struct btf_member *m = btf_members(t);
355
356 for (j = 0; j < vlen; j++, m++)
357 d->type_states[m->type].referenced = 1;
358 break;
359 }
360 case BTF_KIND_FUNC_PROTO: {
361 const struct btf_param *p = btf_params(t);
362
363 for (j = 0; j < vlen; j++, p++)
364 d->type_states[p->type].referenced = 1;
365 break;
366 }
367 case BTF_KIND_DATASEC: {
368 const struct btf_var_secinfo *v = btf_var_secinfos(t);
369
370 for (j = 0; j < vlen; j++, v++)
371 d->type_states[v->type].referenced = 1;
372 break;
373 }
374 default:
375 return -EINVAL;
376 }
377 }
378 return 0;
379 }
380
btf_dump_add_emit_queue_id(struct btf_dump * d,__u32 id)381 static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id)
382 {
383 __u32 *new_queue;
384 size_t new_cap;
385
386 if (d->emit_queue_cnt >= d->emit_queue_cap) {
387 new_cap = max(16, d->emit_queue_cap * 3 / 2);
388 new_queue = libbpf_reallocarray(d->emit_queue, new_cap, sizeof(new_queue[0]));
389 if (!new_queue)
390 return -ENOMEM;
391 d->emit_queue = new_queue;
392 d->emit_queue_cap = new_cap;
393 }
394
395 d->emit_queue[d->emit_queue_cnt++] = id;
396 return 0;
397 }
398
399 /*
400 * Determine order of emitting dependent types and specified type to satisfy
401 * C compilation rules. This is done through topological sorting with an
402 * additional complication which comes from C rules. The main idea for C is
403 * that if some type is "embedded" into a struct/union, it's size needs to be
404 * known at the time of definition of containing type. E.g., for:
405 *
406 * struct A {};
407 * struct B { struct A x; }
408 *
409 * struct A *HAS* to be defined before struct B, because it's "embedded",
410 * i.e., it is part of struct B layout. But in the following case:
411 *
412 * struct A;
413 * struct B { struct A *x; }
414 * struct A {};
415 *
416 * it's enough to just have a forward declaration of struct A at the time of
417 * struct B definition, as struct B has a pointer to struct A, so the size of
418 * field x is known without knowing struct A size: it's sizeof(void *).
419 *
420 * Unfortunately, there are some trickier cases we need to handle, e.g.:
421 *
422 * struct A {}; // if this was forward-declaration: compilation error
423 * struct B {
424 * struct { // anonymous struct
425 * struct A y;
426 * } *x;
427 * };
428 *
429 * In this case, struct B's field x is a pointer, so it's size is known
430 * regardless of the size of (anonymous) struct it points to. But because this
431 * struct is anonymous and thus defined inline inside struct B, *and* it
432 * embeds struct A, compiler requires full definition of struct A to be known
433 * before struct B can be defined. This creates a transitive dependency
434 * between struct A and struct B. If struct A was forward-declared before
435 * struct B definition and fully defined after struct B definition, that would
436 * trigger compilation error.
437 *
438 * All this means that while we are doing topological sorting on BTF type
439 * graph, we need to determine relationships between different types (graph
440 * nodes):
441 * - weak link (relationship) between X and Y, if Y *CAN* be
442 * forward-declared at the point of X definition;
443 * - strong link, if Y *HAS* to be fully-defined before X can be defined.
444 *
445 * The rule is as follows. Given a chain of BTF types from X to Y, if there is
446 * BTF_KIND_PTR type in the chain and at least one non-anonymous type
447 * Z (excluding X, including Y), then link is weak. Otherwise, it's strong.
448 * Weak/strong relationship is determined recursively during DFS traversal and
449 * is returned as a result from btf_dump_order_type().
450 *
451 * btf_dump_order_type() is trying to avoid unnecessary forward declarations,
452 * but it is not guaranteeing that no extraneous forward declarations will be
453 * emitted.
454 *
455 * To avoid extra work, algorithm marks some of BTF types as ORDERED, when
456 * it's done with them, but not for all (e.g., VOLATILE, CONST, RESTRICT,
457 * ARRAY, FUNC_PROTO), as weak/strong semantics for those depends on the
458 * entire graph path, so depending where from one came to that BTF type, it
459 * might cause weak or strong ordering. For types like STRUCT/UNION/INT/ENUM,
460 * once they are processed, there is no need to do it again, so they are
461 * marked as ORDERED. We can mark PTR as ORDERED as well, as it semi-forces
462 * weak link, unless subsequent referenced STRUCT/UNION/ENUM is anonymous. But
463 * in any case, once those are processed, no need to do it again, as the
464 * result won't change.
465 *
466 * Returns:
467 * - 1, if type is part of strong link (so there is strong topological
468 * ordering requirements);
469 * - 0, if type is part of weak link (so can be satisfied through forward
470 * declaration);
471 * - <0, on error (e.g., unsatisfiable type loop detected).
472 */
btf_dump_order_type(struct btf_dump * d,__u32 id,bool through_ptr)473 static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
474 {
475 /*
476 * Order state is used to detect strong link cycles, but only for BTF
477 * kinds that are or could be an independent definition (i.e.,
478 * stand-alone fwd decl, enum, typedef, struct, union). Ptrs, arrays,
479 * func_protos, modifiers are just means to get to these definitions.
480 * Int/void don't need definitions, they are assumed to be always
481 * properly defined. We also ignore datasec, var, and funcs for now.
482 * So for all non-defining kinds, we never even set ordering state,
483 * for defining kinds we set ORDERING and subsequently ORDERED if it
484 * forms a strong link.
485 */
486 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
487 const struct btf_type *t;
488 __u16 vlen;
489 int err, i;
490
491 /* return true, letting typedefs know that it's ok to be emitted */
492 if (tstate->order_state == ORDERED)
493 return 1;
494
495 t = btf__type_by_id(d->btf, id);
496
497 if (tstate->order_state == ORDERING) {
498 /* type loop, but resolvable through fwd declaration */
499 if (btf_is_composite(t) && through_ptr && t->name_off != 0)
500 return 0;
501 pr_warn("unsatisfiable type cycle, id:[%u]\n", id);
502 return -ELOOP;
503 }
504
505 switch (btf_kind(t)) {
506 case BTF_KIND_INT:
507 case BTF_KIND_FLOAT:
508 tstate->order_state = ORDERED;
509 return 0;
510
511 case BTF_KIND_PTR:
512 err = btf_dump_order_type(d, t->type, true);
513 tstate->order_state = ORDERED;
514 return err;
515
516 case BTF_KIND_ARRAY:
517 return btf_dump_order_type(d, btf_array(t)->type, false);
518
519 case BTF_KIND_STRUCT:
520 case BTF_KIND_UNION: {
521 const struct btf_member *m = btf_members(t);
522 /*
523 * struct/union is part of strong link, only if it's embedded
524 * (so no ptr in a path) or it's anonymous (so has to be
525 * defined inline, even if declared through ptr)
526 */
527 if (through_ptr && t->name_off != 0)
528 return 0;
529
530 tstate->order_state = ORDERING;
531
532 vlen = btf_vlen(t);
533 for (i = 0; i < vlen; i++, m++) {
534 err = btf_dump_order_type(d, m->type, false);
535 if (err < 0)
536 return err;
537 }
538
539 if (t->name_off != 0) {
540 err = btf_dump_add_emit_queue_id(d, id);
541 if (err < 0)
542 return err;
543 }
544
545 tstate->order_state = ORDERED;
546 return 1;
547 }
548 case BTF_KIND_ENUM:
549 case BTF_KIND_ENUM64:
550 case BTF_KIND_FWD:
551 /*
552 * non-anonymous or non-referenced enums are top-level
553 * declarations and should be emitted. Same logic can be
554 * applied to FWDs, it won't hurt anyways.
555 */
556 if (t->name_off != 0 || !tstate->referenced) {
557 err = btf_dump_add_emit_queue_id(d, id);
558 if (err)
559 return err;
560 }
561 tstate->order_state = ORDERED;
562 return 1;
563
564 case BTF_KIND_TYPEDEF: {
565 int is_strong;
566
567 is_strong = btf_dump_order_type(d, t->type, through_ptr);
568 if (is_strong < 0)
569 return is_strong;
570
571 /* typedef is similar to struct/union w.r.t. fwd-decls */
572 if (through_ptr && !is_strong)
573 return 0;
574
575 /* typedef is always a named definition */
576 err = btf_dump_add_emit_queue_id(d, id);
577 if (err)
578 return err;
579
580 d->type_states[id].order_state = ORDERED;
581 return 1;
582 }
583 case BTF_KIND_VOLATILE:
584 case BTF_KIND_CONST:
585 case BTF_KIND_RESTRICT:
586 case BTF_KIND_TYPE_TAG:
587 return btf_dump_order_type(d, t->type, through_ptr);
588
589 case BTF_KIND_FUNC_PROTO: {
590 const struct btf_param *p = btf_params(t);
591 bool is_strong;
592
593 err = btf_dump_order_type(d, t->type, through_ptr);
594 if (err < 0)
595 return err;
596 is_strong = err > 0;
597
598 vlen = btf_vlen(t);
599 for (i = 0; i < vlen; i++, p++) {
600 err = btf_dump_order_type(d, p->type, through_ptr);
601 if (err < 0)
602 return err;
603 if (err > 0)
604 is_strong = true;
605 }
606 return is_strong;
607 }
608 case BTF_KIND_FUNC:
609 case BTF_KIND_VAR:
610 case BTF_KIND_DATASEC:
611 case BTF_KIND_DECL_TAG:
612 d->type_states[id].order_state = ORDERED;
613 return 0;
614
615 default:
616 return -EINVAL;
617 }
618 }
619
620 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
621 const struct btf_type *t);
622
623 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
624 const struct btf_type *t);
625 static void btf_dump_emit_struct_def(struct btf_dump *d, __u32 id,
626 const struct btf_type *t, int lvl);
627
628 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
629 const struct btf_type *t);
630 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
631 const struct btf_type *t, int lvl);
632
633 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
634 const struct btf_type *t);
635
636 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
637 const struct btf_type *t, int lvl);
638
639 /* a local view into a shared stack */
640 struct id_stack {
641 const __u32 *ids;
642 int cnt;
643 };
644
645 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
646 const char *fname, int lvl);
647 static void btf_dump_emit_type_chain(struct btf_dump *d,
648 struct id_stack *decl_stack,
649 const char *fname, int lvl);
650
651 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id);
652 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id);
653 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
654 const char *orig_name);
655
btf_dump_is_blacklisted(struct btf_dump * d,__u32 id)656 static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id)
657 {
658 const struct btf_type *t = btf__type_by_id(d->btf, id);
659
660 /* __builtin_va_list is a compiler built-in, which causes compilation
661 * errors, when compiling w/ different compiler, then used to compile
662 * original code (e.g., GCC to compile kernel, Clang to use generated
663 * C header from BTF). As it is built-in, it should be already defined
664 * properly internally in compiler.
665 */
666 if (t->name_off == 0)
667 return false;
668 return strcmp(btf_name_of(d, t->name_off), "__builtin_va_list") == 0;
669 }
670
671 /*
672 * Emit C-syntax definitions of types from chains of BTF types.
673 *
674 * High-level handling of determining necessary forward declarations are handled
675 * by btf_dump_emit_type() itself, but all nitty-gritty details of emitting type
676 * declarations/definitions in C syntax are handled by a combo of
677 * btf_dump_emit_type_decl()/btf_dump_emit_type_chain() w/ delegation to
678 * corresponding btf_dump_emit_*_{def,fwd}() functions.
679 *
680 * We also keep track of "containing struct/union type ID" to determine when
681 * we reference it from inside and thus can avoid emitting unnecessary forward
682 * declaration.
683 *
684 * This algorithm is designed in such a way, that even if some error occurs
685 * (either technical, e.g., out of memory, or logical, i.e., malformed BTF
686 * that doesn't comply to C rules completely), algorithm will try to proceed
687 * and produce as much meaningful output as possible.
688 */
btf_dump_emit_type(struct btf_dump * d,__u32 id,__u32 cont_id)689 static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id)
690 {
691 struct btf_dump_type_aux_state *tstate = &d->type_states[id];
692 bool top_level_def = cont_id == 0;
693 const struct btf_type *t;
694 __u16 kind;
695
696 if (tstate->emit_state == EMITTED)
697 return;
698
699 t = btf__type_by_id(d->btf, id);
700 kind = btf_kind(t);
701
702 if (tstate->emit_state == EMITTING) {
703 if (tstate->fwd_emitted)
704 return;
705
706 switch (kind) {
707 case BTF_KIND_STRUCT:
708 case BTF_KIND_UNION:
709 /*
710 * if we are referencing a struct/union that we are
711 * part of - then no need for fwd declaration
712 */
713 if (id == cont_id)
714 return;
715 if (t->name_off == 0) {
716 pr_warn("anonymous struct/union loop, id:[%u]\n",
717 id);
718 return;
719 }
720 btf_dump_emit_struct_fwd(d, id, t);
721 btf_dump_printf(d, ";\n\n");
722 tstate->fwd_emitted = 1;
723 break;
724 case BTF_KIND_TYPEDEF:
725 /*
726 * for typedef fwd_emitted means typedef definition
727 * was emitted, but it can be used only for "weak"
728 * references through pointer only, not for embedding
729 */
730 if (!btf_dump_is_blacklisted(d, id)) {
731 btf_dump_emit_typedef_def(d, id, t, 0);
732 btf_dump_printf(d, ";\n\n");
733 }
734 tstate->fwd_emitted = 1;
735 break;
736 default:
737 break;
738 }
739
740 return;
741 }
742
743 switch (kind) {
744 case BTF_KIND_INT:
745 /* Emit type alias definitions if necessary */
746 btf_dump_emit_missing_aliases(d, id, t);
747
748 tstate->emit_state = EMITTED;
749 break;
750 case BTF_KIND_ENUM:
751 case BTF_KIND_ENUM64:
752 if (top_level_def) {
753 btf_dump_emit_enum_def(d, id, t, 0);
754 btf_dump_printf(d, ";\n\n");
755 }
756 tstate->emit_state = EMITTED;
757 break;
758 case BTF_KIND_PTR:
759 case BTF_KIND_VOLATILE:
760 case BTF_KIND_CONST:
761 case BTF_KIND_RESTRICT:
762 case BTF_KIND_TYPE_TAG:
763 btf_dump_emit_type(d, t->type, cont_id);
764 break;
765 case BTF_KIND_ARRAY:
766 btf_dump_emit_type(d, btf_array(t)->type, cont_id);
767 break;
768 case BTF_KIND_FWD:
769 btf_dump_emit_fwd_def(d, id, t);
770 btf_dump_printf(d, ";\n\n");
771 tstate->emit_state = EMITTED;
772 break;
773 case BTF_KIND_TYPEDEF:
774 tstate->emit_state = EMITTING;
775 btf_dump_emit_type(d, t->type, id);
776 /*
777 * typedef can server as both definition and forward
778 * declaration; at this stage someone depends on
779 * typedef as a forward declaration (refers to it
780 * through pointer), so unless we already did it,
781 * emit typedef as a forward declaration
782 */
783 if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) {
784 btf_dump_emit_typedef_def(d, id, t, 0);
785 btf_dump_printf(d, ";\n\n");
786 }
787 tstate->emit_state = EMITTED;
788 break;
789 case BTF_KIND_STRUCT:
790 case BTF_KIND_UNION:
791 tstate->emit_state = EMITTING;
792 /* if it's a top-level struct/union definition or struct/union
793 * is anonymous, then in C we'll be emitting all fields and
794 * their types (as opposed to just `struct X`), so we need to
795 * make sure that all types, referenced from struct/union
796 * members have necessary forward-declarations, where
797 * applicable
798 */
799 if (top_level_def || t->name_off == 0) {
800 const struct btf_member *m = btf_members(t);
801 __u16 vlen = btf_vlen(t);
802 int i, new_cont_id;
803
804 new_cont_id = t->name_off == 0 ? cont_id : id;
805 for (i = 0; i < vlen; i++, m++)
806 btf_dump_emit_type(d, m->type, new_cont_id);
807 } else if (!tstate->fwd_emitted && id != cont_id) {
808 btf_dump_emit_struct_fwd(d, id, t);
809 btf_dump_printf(d, ";\n\n");
810 tstate->fwd_emitted = 1;
811 }
812
813 if (top_level_def) {
814 btf_dump_emit_struct_def(d, id, t, 0);
815 btf_dump_printf(d, ";\n\n");
816 tstate->emit_state = EMITTED;
817 } else {
818 tstate->emit_state = NOT_EMITTED;
819 }
820 break;
821 case BTF_KIND_FUNC_PROTO: {
822 const struct btf_param *p = btf_params(t);
823 __u16 n = btf_vlen(t);
824 int i;
825
826 btf_dump_emit_type(d, t->type, cont_id);
827 for (i = 0; i < n; i++, p++)
828 btf_dump_emit_type(d, p->type, cont_id);
829
830 break;
831 }
832 default:
833 break;
834 }
835 }
836
btf_is_struct_packed(const struct btf * btf,__u32 id,const struct btf_type * t)837 static bool btf_is_struct_packed(const struct btf *btf, __u32 id,
838 const struct btf_type *t)
839 {
840 const struct btf_member *m;
841 int max_align = 1, align, i, bit_sz;
842 __u16 vlen;
843
844 m = btf_members(t);
845 vlen = btf_vlen(t);
846 /* all non-bitfield fields have to be naturally aligned */
847 for (i = 0; i < vlen; i++, m++) {
848 align = btf__align_of(btf, m->type);
849 bit_sz = btf_member_bitfield_size(t, i);
850 if (align && bit_sz == 0 && m->offset % (8 * align) != 0)
851 return true;
852 max_align = max(align, max_align);
853 }
854 /* size of a non-packed struct has to be a multiple of its alignment */
855 if (t->size % max_align != 0)
856 return true;
857 /*
858 * if original struct was marked as packed, but its layout is
859 * naturally aligned, we'll detect that it's not packed
860 */
861 return false;
862 }
863
btf_dump_emit_bit_padding(const struct btf_dump * d,int cur_off,int next_off,int next_align,bool in_bitfield,int lvl)864 static void btf_dump_emit_bit_padding(const struct btf_dump *d,
865 int cur_off, int next_off, int next_align,
866 bool in_bitfield, int lvl)
867 {
868 const struct {
869 const char *name;
870 int bits;
871 } pads[] = {
872 {"long", d->ptr_sz * 8}, {"int", 32}, {"short", 16}, {"char", 8}
873 };
874 int new_off = 0, pad_bits = 0, bits, i;
875 const char *pad_type = NULL;
876
877 if (cur_off >= next_off)
878 return; /* no gap */
879
880 /* For filling out padding we want to take advantage of
881 * natural alignment rules to minimize unnecessary explicit
882 * padding. First, we find the largest type (among long, int,
883 * short, or char) that can be used to force naturally aligned
884 * boundary. Once determined, we'll use such type to fill in
885 * the remaining padding gap. In some cases we can rely on
886 * compiler filling some gaps, but sometimes we need to force
887 * alignment to close natural alignment with markers like
888 * `long: 0` (this is always the case for bitfields). Note
889 * that even if struct itself has, let's say 4-byte alignment
890 * (i.e., it only uses up to int-aligned types), using `long:
891 * X;` explicit padding doesn't actually change struct's
892 * overall alignment requirements, but compiler does take into
893 * account that type's (long, in this example) natural
894 * alignment requirements when adding implicit padding. We use
895 * this fact heavily and don't worry about ruining correct
896 * struct alignment requirement.
897 */
898 for (i = 0; i < ARRAY_SIZE(pads); i++) {
899 pad_bits = pads[i].bits;
900 pad_type = pads[i].name;
901
902 new_off = roundup(cur_off, pad_bits);
903 if (new_off <= next_off)
904 break;
905 }
906
907 if (new_off > cur_off && new_off <= next_off) {
908 /* We need explicit `<type>: 0` aligning mark if next
909 * field is right on alignment offset and its
910 * alignment requirement is less strict than <type>'s
911 * alignment (so compiler won't naturally align to the
912 * offset we expect), or if subsequent `<type>: X`,
913 * will actually completely fit in the remaining hole,
914 * making compiler basically ignore `<type>: X`
915 * completely.
916 */
917 if (in_bitfield ||
918 (new_off == next_off && roundup(cur_off, next_align * 8) != new_off) ||
919 (new_off != next_off && next_off - new_off <= new_off - cur_off))
920 /* but for bitfields we'll emit explicit bit count */
921 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type,
922 in_bitfield ? new_off - cur_off : 0);
923 cur_off = new_off;
924 }
925
926 /* Now we know we start at naturally aligned offset for a chosen
927 * padding type (long, int, short, or char), and so the rest is just
928 * a straightforward filling of remaining padding gap with full
929 * `<type>: sizeof(<type>);` markers, except for the last one, which
930 * might need smaller than sizeof(<type>) padding.
931 */
932 while (cur_off != next_off) {
933 bits = min(next_off - cur_off, pad_bits);
934 if (bits == pad_bits) {
935 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, pad_bits);
936 cur_off += bits;
937 continue;
938 }
939 /* For the remainder padding that doesn't cover entire
940 * pad_type bit length, we pick the smallest necessary type.
941 * This is pure aesthetics, we could have just used `long`,
942 * but having smallest necessary one communicates better the
943 * scale of the padding gap.
944 */
945 for (i = ARRAY_SIZE(pads) - 1; i >= 0; i--) {
946 pad_type = pads[i].name;
947 pad_bits = pads[i].bits;
948 if (pad_bits < bits)
949 continue;
950
951 btf_dump_printf(d, "\n%s%s: %d;", pfx(lvl), pad_type, bits);
952 cur_off += bits;
953 break;
954 }
955 }
956 }
957
btf_dump_emit_struct_fwd(struct btf_dump * d,__u32 id,const struct btf_type * t)958 static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id,
959 const struct btf_type *t)
960 {
961 btf_dump_printf(d, "%s%s%s",
962 btf_is_struct(t) ? "struct" : "union",
963 t->name_off ? " " : "",
964 btf_dump_type_name(d, id));
965 }
966
btf_dump_emit_struct_def(struct btf_dump * d,__u32 id,const struct btf_type * t,int lvl)967 static void btf_dump_emit_struct_def(struct btf_dump *d,
968 __u32 id,
969 const struct btf_type *t,
970 int lvl)
971 {
972 const struct btf_member *m = btf_members(t);
973 bool is_struct = btf_is_struct(t);
974 bool packed, prev_bitfield = false;
975 int align, i, off = 0;
976 __u16 vlen = btf_vlen(t);
977
978 align = btf__align_of(d->btf, id);
979 packed = is_struct ? btf_is_struct_packed(d->btf, id, t) : 0;
980
981 btf_dump_printf(d, "%s%s%s {",
982 is_struct ? "struct" : "union",
983 t->name_off ? " " : "",
984 btf_dump_type_name(d, id));
985
986 for (i = 0; i < vlen; i++, m++) {
987 const char *fname;
988 int m_off, m_sz, m_align;
989 bool in_bitfield;
990
991 fname = btf_name_of(d, m->name_off);
992 m_sz = btf_member_bitfield_size(t, i);
993 m_off = btf_member_bit_offset(t, i);
994 m_align = packed ? 1 : btf__align_of(d->btf, m->type);
995
996 in_bitfield = prev_bitfield && m_sz != 0;
997
998 btf_dump_emit_bit_padding(d, off, m_off, m_align, in_bitfield, lvl + 1);
999 btf_dump_printf(d, "\n%s", pfx(lvl + 1));
1000 btf_dump_emit_type_decl(d, m->type, fname, lvl + 1);
1001
1002 if (m_sz) {
1003 btf_dump_printf(d, ": %d", m_sz);
1004 off = m_off + m_sz;
1005 prev_bitfield = true;
1006 } else {
1007 m_sz = max((__s64)0, btf__resolve_size(d->btf, m->type));
1008 off = m_off + m_sz * 8;
1009 prev_bitfield = false;
1010 }
1011
1012 btf_dump_printf(d, ";");
1013 }
1014
1015 /* pad at the end, if necessary */
1016 if (is_struct)
1017 btf_dump_emit_bit_padding(d, off, t->size * 8, align, false, lvl + 1);
1018
1019 /*
1020 * Keep `struct empty {}` on a single line,
1021 * only print newline when there are regular or padding fields.
1022 */
1023 if (vlen || t->size) {
1024 btf_dump_printf(d, "\n");
1025 btf_dump_printf(d, "%s}", pfx(lvl));
1026 } else {
1027 btf_dump_printf(d, "}");
1028 }
1029 if (packed)
1030 btf_dump_printf(d, " __attribute__((packed))");
1031 }
1032
1033 static const char *missing_base_types[][2] = {
1034 /*
1035 * GCC emits typedefs to its internal __PolyX_t types when compiling Arm
1036 * SIMD intrinsics. Alias them to standard base types.
1037 */
1038 { "__Poly8_t", "unsigned char" },
1039 { "__Poly16_t", "unsigned short" },
1040 { "__Poly64_t", "unsigned long long" },
1041 { "__Poly128_t", "unsigned __int128" },
1042 };
1043
btf_dump_emit_missing_aliases(struct btf_dump * d,__u32 id,const struct btf_type * t)1044 static void btf_dump_emit_missing_aliases(struct btf_dump *d, __u32 id,
1045 const struct btf_type *t)
1046 {
1047 const char *name = btf_dump_type_name(d, id);
1048 int i;
1049
1050 for (i = 0; i < ARRAY_SIZE(missing_base_types); i++) {
1051 if (strcmp(name, missing_base_types[i][0]) == 0) {
1052 btf_dump_printf(d, "typedef %s %s;\n\n",
1053 missing_base_types[i][1], name);
1054 break;
1055 }
1056 }
1057 }
1058
btf_dump_emit_enum_fwd(struct btf_dump * d,__u32 id,const struct btf_type * t)1059 static void btf_dump_emit_enum_fwd(struct btf_dump *d, __u32 id,
1060 const struct btf_type *t)
1061 {
1062 btf_dump_printf(d, "enum %s", btf_dump_type_name(d, id));
1063 }
1064
btf_dump_emit_enum32_val(struct btf_dump * d,const struct btf_type * t,int lvl,__u16 vlen)1065 static void btf_dump_emit_enum32_val(struct btf_dump *d,
1066 const struct btf_type *t,
1067 int lvl, __u16 vlen)
1068 {
1069 const struct btf_enum *v = btf_enum(t);
1070 bool is_signed = btf_kflag(t);
1071 const char *fmt_str;
1072 const char *name;
1073 size_t dup_cnt;
1074 int i;
1075
1076 for (i = 0; i < vlen; i++, v++) {
1077 name = btf_name_of(d, v->name_off);
1078 /* enumerators share namespace with typedef idents */
1079 dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1080 if (dup_cnt > 1) {
1081 fmt_str = is_signed ? "\n%s%s___%zd = %d," : "\n%s%s___%zd = %u,";
1082 btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, dup_cnt, v->val);
1083 } else {
1084 fmt_str = is_signed ? "\n%s%s = %d," : "\n%s%s = %u,";
1085 btf_dump_printf(d, fmt_str, pfx(lvl + 1), name, v->val);
1086 }
1087 }
1088 }
1089
btf_dump_emit_enum64_val(struct btf_dump * d,const struct btf_type * t,int lvl,__u16 vlen)1090 static void btf_dump_emit_enum64_val(struct btf_dump *d,
1091 const struct btf_type *t,
1092 int lvl, __u16 vlen)
1093 {
1094 const struct btf_enum64 *v = btf_enum64(t);
1095 bool is_signed = btf_kflag(t);
1096 const char *fmt_str;
1097 const char *name;
1098 size_t dup_cnt;
1099 __u64 val;
1100 int i;
1101
1102 for (i = 0; i < vlen; i++, v++) {
1103 name = btf_name_of(d, v->name_off);
1104 dup_cnt = btf_dump_name_dups(d, d->ident_names, name);
1105 val = btf_enum64_value(v);
1106 if (dup_cnt > 1) {
1107 fmt_str = is_signed ? "\n%s%s___%zd = %lldLL,"
1108 : "\n%s%s___%zd = %lluULL,";
1109 btf_dump_printf(d, fmt_str,
1110 pfx(lvl + 1), name, dup_cnt,
1111 (unsigned long long)val);
1112 } else {
1113 fmt_str = is_signed ? "\n%s%s = %lldLL,"
1114 : "\n%s%s = %lluULL,";
1115 btf_dump_printf(d, fmt_str,
1116 pfx(lvl + 1), name,
1117 (unsigned long long)val);
1118 }
1119 }
1120 }
btf_dump_emit_enum_def(struct btf_dump * d,__u32 id,const struct btf_type * t,int lvl)1121 static void btf_dump_emit_enum_def(struct btf_dump *d, __u32 id,
1122 const struct btf_type *t,
1123 int lvl)
1124 {
1125 __u16 vlen = btf_vlen(t);
1126
1127 btf_dump_printf(d, "enum%s%s",
1128 t->name_off ? " " : "",
1129 btf_dump_type_name(d, id));
1130
1131 if (!vlen)
1132 return;
1133
1134 btf_dump_printf(d, " {");
1135 if (btf_is_enum(t))
1136 btf_dump_emit_enum32_val(d, t, lvl, vlen);
1137 else
1138 btf_dump_emit_enum64_val(d, t, lvl, vlen);
1139 btf_dump_printf(d, "\n%s}", pfx(lvl));
1140
1141 /* special case enums with special sizes */
1142 if (t->size == 1) {
1143 /* one-byte enums can be forced with mode(byte) attribute */
1144 btf_dump_printf(d, " __attribute__((mode(byte)))");
1145 } else if (t->size == 8 && d->ptr_sz == 8) {
1146 /* enum can be 8-byte sized if one of the enumerator values
1147 * doesn't fit in 32-bit integer, or by adding mode(word)
1148 * attribute (but probably only on 64-bit architectures); do
1149 * our best here to try to satisfy the contract without adding
1150 * unnecessary attributes
1151 */
1152 bool needs_word_mode;
1153
1154 if (btf_is_enum(t)) {
1155 /* enum can't represent 64-bit values, so we need word mode */
1156 needs_word_mode = true;
1157 } else {
1158 /* enum64 needs mode(word) if none of its values has
1159 * non-zero upper 32-bits (which means that all values
1160 * fit in 32-bit integers and won't cause compiler to
1161 * bump enum to be 64-bit naturally
1162 */
1163 int i;
1164
1165 needs_word_mode = true;
1166 for (i = 0; i < vlen; i++) {
1167 if (btf_enum64(t)[i].val_hi32 != 0) {
1168 needs_word_mode = false;
1169 break;
1170 }
1171 }
1172 }
1173 if (needs_word_mode)
1174 btf_dump_printf(d, " __attribute__((mode(word)))");
1175 }
1176
1177 }
1178
btf_dump_emit_fwd_def(struct btf_dump * d,__u32 id,const struct btf_type * t)1179 static void btf_dump_emit_fwd_def(struct btf_dump *d, __u32 id,
1180 const struct btf_type *t)
1181 {
1182 const char *name = btf_dump_type_name(d, id);
1183
1184 if (btf_kflag(t))
1185 btf_dump_printf(d, "union %s", name);
1186 else
1187 btf_dump_printf(d, "struct %s", name);
1188 }
1189
btf_dump_emit_typedef_def(struct btf_dump * d,__u32 id,const struct btf_type * t,int lvl)1190 static void btf_dump_emit_typedef_def(struct btf_dump *d, __u32 id,
1191 const struct btf_type *t, int lvl)
1192 {
1193 const char *name = btf_dump_ident_name(d, id);
1194
1195 /*
1196 * Old GCC versions are emitting invalid typedef for __gnuc_va_list
1197 * pointing to VOID. This generates warnings from btf_dump() and
1198 * results in uncompilable header file, so we are fixing it up here
1199 * with valid typedef into __builtin_va_list.
1200 */
1201 if (t->type == 0 && strcmp(name, "__gnuc_va_list") == 0) {
1202 btf_dump_printf(d, "typedef __builtin_va_list __gnuc_va_list");
1203 return;
1204 }
1205
1206 btf_dump_printf(d, "typedef ");
1207 btf_dump_emit_type_decl(d, t->type, name, lvl);
1208 }
1209
btf_dump_push_decl_stack_id(struct btf_dump * d,__u32 id)1210 static int btf_dump_push_decl_stack_id(struct btf_dump *d, __u32 id)
1211 {
1212 __u32 *new_stack;
1213 size_t new_cap;
1214
1215 if (d->decl_stack_cnt >= d->decl_stack_cap) {
1216 new_cap = max(16, d->decl_stack_cap * 3 / 2);
1217 new_stack = libbpf_reallocarray(d->decl_stack, new_cap, sizeof(new_stack[0]));
1218 if (!new_stack)
1219 return -ENOMEM;
1220 d->decl_stack = new_stack;
1221 d->decl_stack_cap = new_cap;
1222 }
1223
1224 d->decl_stack[d->decl_stack_cnt++] = id;
1225
1226 return 0;
1227 }
1228
1229 /*
1230 * Emit type declaration (e.g., field type declaration in a struct or argument
1231 * declaration in function prototype) in correct C syntax.
1232 *
1233 * For most types it's trivial, but there are few quirky type declaration
1234 * cases worth mentioning:
1235 * - function prototypes (especially nesting of function prototypes);
1236 * - arrays;
1237 * - const/volatile/restrict for pointers vs other types.
1238 *
1239 * For a good discussion of *PARSING* C syntax (as a human), see
1240 * Peter van der Linden's "Expert C Programming: Deep C Secrets",
1241 * Ch.3 "Unscrambling Declarations in C".
1242 *
1243 * It won't help with BTF to C conversion much, though, as it's an opposite
1244 * problem. So we came up with this algorithm in reverse to van der Linden's
1245 * parsing algorithm. It goes from structured BTF representation of type
1246 * declaration to a valid compilable C syntax.
1247 *
1248 * For instance, consider this C typedef:
1249 * typedef const int * const * arr[10] arr_t;
1250 * It will be represented in BTF with this chain of BTF types:
1251 * [typedef] -> [array] -> [ptr] -> [const] -> [ptr] -> [const] -> [int]
1252 *
1253 * Notice how [const] modifier always goes before type it modifies in BTF type
1254 * graph, but in C syntax, const/volatile/restrict modifiers are written to
1255 * the right of pointers, but to the left of other types. There are also other
1256 * quirks, like function pointers, arrays of them, functions returning other
1257 * functions, etc.
1258 *
1259 * We handle that by pushing all the types to a stack, until we hit "terminal"
1260 * type (int/enum/struct/union/fwd). Then depending on the kind of a type on
1261 * top of a stack, modifiers are handled differently. Array/function pointers
1262 * have also wildly different syntax and how nesting of them are done. See
1263 * code for authoritative definition.
1264 *
1265 * To avoid allocating new stack for each independent chain of BTF types, we
1266 * share one bigger stack, with each chain working only on its own local view
1267 * of a stack frame. Some care is required to "pop" stack frames after
1268 * processing type declaration chain.
1269 */
btf_dump__emit_type_decl(struct btf_dump * d,__u32 id,const struct btf_dump_emit_type_decl_opts * opts)1270 int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
1271 const struct btf_dump_emit_type_decl_opts *opts)
1272 {
1273 const char *fname;
1274 int lvl, err;
1275
1276 if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
1277 return libbpf_err(-EINVAL);
1278
1279 err = btf_dump_resize(d);
1280 if (err)
1281 return libbpf_err(err);
1282
1283 fname = OPTS_GET(opts, field_name, "");
1284 lvl = OPTS_GET(opts, indent_level, 0);
1285 d->strip_mods = OPTS_GET(opts, strip_mods, false);
1286 btf_dump_emit_type_decl(d, id, fname, lvl);
1287 d->strip_mods = false;
1288 return 0;
1289 }
1290
btf_dump_emit_type_decl(struct btf_dump * d,__u32 id,const char * fname,int lvl)1291 static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
1292 const char *fname, int lvl)
1293 {
1294 struct id_stack decl_stack;
1295 const struct btf_type *t;
1296 int err, stack_start;
1297
1298 stack_start = d->decl_stack_cnt;
1299 for (;;) {
1300 t = btf__type_by_id(d->btf, id);
1301 if (d->strip_mods && btf_is_mod(t))
1302 goto skip_mod;
1303
1304 err = btf_dump_push_decl_stack_id(d, id);
1305 if (err < 0) {
1306 /*
1307 * if we don't have enough memory for entire type decl
1308 * chain, restore stack, emit warning, and try to
1309 * proceed nevertheless
1310 */
1311 pr_warn("not enough memory for decl stack: %s\n", errstr(err));
1312 d->decl_stack_cnt = stack_start;
1313 return;
1314 }
1315 skip_mod:
1316 /* VOID */
1317 if (id == 0)
1318 break;
1319
1320 switch (btf_kind(t)) {
1321 case BTF_KIND_PTR:
1322 case BTF_KIND_VOLATILE:
1323 case BTF_KIND_CONST:
1324 case BTF_KIND_RESTRICT:
1325 case BTF_KIND_FUNC_PROTO:
1326 case BTF_KIND_TYPE_TAG:
1327 id = t->type;
1328 break;
1329 case BTF_KIND_ARRAY:
1330 id = btf_array(t)->type;
1331 break;
1332 case BTF_KIND_INT:
1333 case BTF_KIND_ENUM:
1334 case BTF_KIND_ENUM64:
1335 case BTF_KIND_FWD:
1336 case BTF_KIND_STRUCT:
1337 case BTF_KIND_UNION:
1338 case BTF_KIND_TYPEDEF:
1339 case BTF_KIND_FLOAT:
1340 goto done;
1341 default:
1342 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1343 btf_kind(t), id);
1344 goto done;
1345 }
1346 }
1347 done:
1348 /*
1349 * We might be inside a chain of declarations (e.g., array of function
1350 * pointers returning anonymous (so inlined) structs, having another
1351 * array field). Each of those needs its own "stack frame" to handle
1352 * emitting of declarations. Those stack frames are non-overlapping
1353 * portions of shared btf_dump->decl_stack. To make it a bit nicer to
1354 * handle this set of nested stacks, we create a view corresponding to
1355 * our own "stack frame" and work with it as an independent stack.
1356 * We'll need to clean up after emit_type_chain() returns, though.
1357 */
1358 decl_stack.ids = d->decl_stack + stack_start;
1359 decl_stack.cnt = d->decl_stack_cnt - stack_start;
1360 btf_dump_emit_type_chain(d, &decl_stack, fname, lvl);
1361 /*
1362 * emit_type_chain() guarantees that it will pop its entire decl_stack
1363 * frame before returning. But it works with a read-only view into
1364 * decl_stack, so it doesn't actually pop anything from the
1365 * perspective of shared btf_dump->decl_stack, per se. We need to
1366 * reset decl_stack state to how it was before us to avoid it growing
1367 * all the time.
1368 */
1369 d->decl_stack_cnt = stack_start;
1370 }
1371
btf_dump_emit_mods(struct btf_dump * d,struct id_stack * decl_stack)1372 static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
1373 {
1374 const struct btf_type *t;
1375 __u32 id;
1376
1377 while (decl_stack->cnt) {
1378 id = decl_stack->ids[decl_stack->cnt - 1];
1379 t = btf__type_by_id(d->btf, id);
1380
1381 switch (btf_kind(t)) {
1382 case BTF_KIND_VOLATILE:
1383 btf_dump_printf(d, "volatile ");
1384 break;
1385 case BTF_KIND_CONST:
1386 btf_dump_printf(d, "const ");
1387 break;
1388 case BTF_KIND_RESTRICT:
1389 btf_dump_printf(d, "restrict ");
1390 break;
1391 default:
1392 return;
1393 }
1394 decl_stack->cnt--;
1395 }
1396 }
1397
btf_dump_drop_mods(struct btf_dump * d,struct id_stack * decl_stack)1398 static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1399 {
1400 const struct btf_type *t;
1401 __u32 id;
1402
1403 while (decl_stack->cnt) {
1404 id = decl_stack->ids[decl_stack->cnt - 1];
1405 t = btf__type_by_id(d->btf, id);
1406 if (!btf_is_mod(t))
1407 return;
1408 decl_stack->cnt--;
1409 }
1410 }
1411
btf_dump_emit_name(const struct btf_dump * d,const char * name,bool last_was_ptr)1412 static void btf_dump_emit_name(const struct btf_dump *d,
1413 const char *name, bool last_was_ptr)
1414 {
1415 bool separate = name[0] && !last_was_ptr;
1416
1417 btf_dump_printf(d, "%s%s", separate ? " " : "", name);
1418 }
1419
btf_dump_emit_type_chain(struct btf_dump * d,struct id_stack * decls,const char * fname,int lvl)1420 static void btf_dump_emit_type_chain(struct btf_dump *d,
1421 struct id_stack *decls,
1422 const char *fname, int lvl)
1423 {
1424 /*
1425 * last_was_ptr is used to determine if we need to separate pointer
1426 * asterisk (*) from previous part of type signature with space, so
1427 * that we get `int ***`, instead of `int * * *`. We default to true
1428 * for cases where we have single pointer in a chain. E.g., in ptr ->
1429 * func_proto case. func_proto will start a new emit_type_chain call
1430 * with just ptr, which should be emitted as (*) or (*<fname>), so we
1431 * don't want to prepend space for that last pointer.
1432 */
1433 bool last_was_ptr = true;
1434 const struct btf_type *t;
1435 const char *name;
1436 __u16 kind;
1437 __u32 id;
1438
1439 while (decls->cnt) {
1440 id = decls->ids[--decls->cnt];
1441 if (id == 0) {
1442 /* VOID is a special snowflake */
1443 btf_dump_emit_mods(d, decls);
1444 btf_dump_printf(d, "void");
1445 last_was_ptr = false;
1446 continue;
1447 }
1448
1449 t = btf__type_by_id(d->btf, id);
1450 kind = btf_kind(t);
1451
1452 switch (kind) {
1453 case BTF_KIND_INT:
1454 case BTF_KIND_FLOAT:
1455 btf_dump_emit_mods(d, decls);
1456 name = btf_name_of(d, t->name_off);
1457 btf_dump_printf(d, "%s", name);
1458 break;
1459 case BTF_KIND_STRUCT:
1460 case BTF_KIND_UNION:
1461 btf_dump_emit_mods(d, decls);
1462 /* inline anonymous struct/union */
1463 if (t->name_off == 0 && !d->skip_anon_defs)
1464 btf_dump_emit_struct_def(d, id, t, lvl);
1465 else
1466 btf_dump_emit_struct_fwd(d, id, t);
1467 break;
1468 case BTF_KIND_ENUM:
1469 case BTF_KIND_ENUM64:
1470 btf_dump_emit_mods(d, decls);
1471 /* inline anonymous enum */
1472 if (t->name_off == 0 && !d->skip_anon_defs)
1473 btf_dump_emit_enum_def(d, id, t, lvl);
1474 else
1475 btf_dump_emit_enum_fwd(d, id, t);
1476 break;
1477 case BTF_KIND_FWD:
1478 btf_dump_emit_mods(d, decls);
1479 btf_dump_emit_fwd_def(d, id, t);
1480 break;
1481 case BTF_KIND_TYPEDEF:
1482 btf_dump_emit_mods(d, decls);
1483 btf_dump_printf(d, "%s", btf_dump_ident_name(d, id));
1484 break;
1485 case BTF_KIND_PTR:
1486 btf_dump_printf(d, "%s", last_was_ptr ? "*" : " *");
1487 break;
1488 case BTF_KIND_VOLATILE:
1489 btf_dump_printf(d, " volatile");
1490 break;
1491 case BTF_KIND_CONST:
1492 btf_dump_printf(d, " const");
1493 break;
1494 case BTF_KIND_RESTRICT:
1495 btf_dump_printf(d, " restrict");
1496 break;
1497 case BTF_KIND_TYPE_TAG:
1498 btf_dump_emit_mods(d, decls);
1499 name = btf_name_of(d, t->name_off);
1500 if (btf_kflag(t))
1501 btf_dump_printf(d, " __attribute__((%s))", name);
1502 else
1503 btf_dump_printf(d, " __attribute__((btf_type_tag(\"%s\")))", name);
1504 break;
1505 case BTF_KIND_ARRAY: {
1506 const struct btf_array *a = btf_array(t);
1507 const struct btf_type *next_t;
1508 __u32 next_id;
1509 bool multidim;
1510 /*
1511 * GCC has a bug
1512 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=8354)
1513 * which causes it to emit extra const/volatile
1514 * modifiers for an array, if array's element type has
1515 * const/volatile modifiers. Clang doesn't do that.
1516 * In general, it doesn't seem very meaningful to have
1517 * a const/volatile modifier for array, so we are
1518 * going to silently skip them here.
1519 */
1520 btf_dump_drop_mods(d, decls);
1521
1522 if (decls->cnt == 0) {
1523 btf_dump_emit_name(d, fname, last_was_ptr);
1524 btf_dump_printf(d, "[%u]", a->nelems);
1525 return;
1526 }
1527
1528 next_id = decls->ids[decls->cnt - 1];
1529 next_t = btf__type_by_id(d->btf, next_id);
1530 multidim = btf_is_array(next_t);
1531 /* we need space if we have named non-pointer */
1532 if (fname[0] && !last_was_ptr)
1533 btf_dump_printf(d, " ");
1534 /* no parentheses for multi-dimensional array */
1535 if (!multidim)
1536 btf_dump_printf(d, "(");
1537 btf_dump_emit_type_chain(d, decls, fname, lvl);
1538 if (!multidim)
1539 btf_dump_printf(d, ")");
1540 btf_dump_printf(d, "[%u]", a->nelems);
1541 return;
1542 }
1543 case BTF_KIND_FUNC_PROTO: {
1544 const struct btf_param *p = btf_params(t);
1545 __u16 vlen = btf_vlen(t);
1546 int i;
1547
1548 /*
1549 * GCC emits extra volatile qualifier for
1550 * __attribute__((noreturn)) function pointers. Clang
1551 * doesn't do it. It's a GCC quirk for backwards
1552 * compatibility with code written for GCC <2.5. So,
1553 * similarly to extra qualifiers for array, just drop
1554 * them, instead of handling them.
1555 */
1556 btf_dump_drop_mods(d, decls);
1557 if (decls->cnt) {
1558 btf_dump_printf(d, " (");
1559 btf_dump_emit_type_chain(d, decls, fname, lvl);
1560 btf_dump_printf(d, ")");
1561 } else {
1562 btf_dump_emit_name(d, fname, last_was_ptr);
1563 }
1564 btf_dump_printf(d, "(");
1565 /*
1566 * Clang for BPF target generates func_proto with no
1567 * args as a func_proto with a single void arg (e.g.,
1568 * `int (*f)(void)` vs just `int (*f)()`). We are
1569 * going to emit valid empty args (void) syntax for
1570 * such case. Similarly and conveniently, valid
1571 * no args case can be special-cased here as well.
1572 */
1573 if (vlen == 0 || (vlen == 1 && p->type == 0)) {
1574 btf_dump_printf(d, "void)");
1575 return;
1576 }
1577
1578 for (i = 0; i < vlen; i++, p++) {
1579 if (i > 0)
1580 btf_dump_printf(d, ", ");
1581
1582 /* last arg of type void is vararg */
1583 if (i == vlen - 1 && p->type == 0) {
1584 btf_dump_printf(d, "...");
1585 break;
1586 }
1587
1588 name = btf_name_of(d, p->name_off);
1589 btf_dump_emit_type_decl(d, p->type, name, lvl);
1590 }
1591
1592 btf_dump_printf(d, ")");
1593 return;
1594 }
1595 default:
1596 pr_warn("unexpected type in decl chain, kind:%u, id:[%u]\n",
1597 kind, id);
1598 return;
1599 }
1600
1601 last_was_ptr = kind == BTF_KIND_PTR;
1602 }
1603
1604 btf_dump_emit_name(d, fname, last_was_ptr);
1605 }
1606
1607 /* show type name as (type_name) */
btf_dump_emit_type_cast(struct btf_dump * d,__u32 id,bool top_level)1608 static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id,
1609 bool top_level)
1610 {
1611 const struct btf_type *t;
1612
1613 /* for array members, we don't bother emitting type name for each
1614 * member to avoid the redundancy of
1615 * .name = (char[4])[(char)'f',(char)'o',(char)'o',]
1616 */
1617 if (d->typed_dump->is_array_member)
1618 return;
1619
1620 /* avoid type name specification for variable/section; it will be done
1621 * for the associated variable value(s).
1622 */
1623 t = btf__type_by_id(d->btf, id);
1624 if (btf_is_var(t) || btf_is_datasec(t))
1625 return;
1626
1627 if (top_level)
1628 btf_dump_printf(d, "(");
1629
1630 d->skip_anon_defs = true;
1631 d->strip_mods = true;
1632 btf_dump_emit_type_decl(d, id, "", 0);
1633 d->strip_mods = false;
1634 d->skip_anon_defs = false;
1635
1636 if (top_level)
1637 btf_dump_printf(d, ")");
1638 }
1639
1640 /* return number of duplicates (occurrences) of a given name */
btf_dump_name_dups(struct btf_dump * d,struct hashmap * name_map,const char * orig_name)1641 static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map,
1642 const char *orig_name)
1643 {
1644 char *old_name, *new_name;
1645 size_t dup_cnt = 0;
1646 int err;
1647
1648 new_name = strdup(orig_name);
1649 if (!new_name)
1650 return 1;
1651
1652 (void)hashmap__find(name_map, orig_name, &dup_cnt);
1653 dup_cnt++;
1654
1655 err = hashmap__set(name_map, new_name, dup_cnt, &old_name, NULL);
1656 if (err)
1657 free(new_name);
1658
1659 free(old_name);
1660
1661 return dup_cnt;
1662 }
1663
btf_dump_resolve_name(struct btf_dump * d,__u32 id,struct hashmap * name_map)1664 static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
1665 struct hashmap *name_map)
1666 {
1667 struct btf_dump_type_aux_state *s = &d->type_states[id];
1668 const struct btf_type *t = btf__type_by_id(d->btf, id);
1669 const char *orig_name = btf_name_of(d, t->name_off);
1670 const char **cached_name = &d->cached_names[id];
1671 size_t dup_cnt;
1672
1673 if (t->name_off == 0)
1674 return "";
1675
1676 if (s->name_resolved)
1677 return *cached_name ? *cached_name : orig_name;
1678
1679 if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
1680 s->name_resolved = 1;
1681 return orig_name;
1682 }
1683
1684 dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
1685 if (dup_cnt > 1) {
1686 const size_t max_len = 256;
1687 char new_name[max_len];
1688
1689 snprintf(new_name, max_len, "%s___%zu", orig_name, dup_cnt);
1690 *cached_name = strdup(new_name);
1691 }
1692
1693 s->name_resolved = 1;
1694 return *cached_name ? *cached_name : orig_name;
1695 }
1696
btf_dump_type_name(struct btf_dump * d,__u32 id)1697 static const char *btf_dump_type_name(struct btf_dump *d, __u32 id)
1698 {
1699 return btf_dump_resolve_name(d, id, d->type_names);
1700 }
1701
btf_dump_ident_name(struct btf_dump * d,__u32 id)1702 static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id)
1703 {
1704 return btf_dump_resolve_name(d, id, d->ident_names);
1705 }
1706
1707 static int btf_dump_dump_type_data(struct btf_dump *d,
1708 const char *fname,
1709 const struct btf_type *t,
1710 __u32 id,
1711 const void *data,
1712 __u8 bits_offset,
1713 __u8 bit_sz);
1714
btf_dump_data_newline(struct btf_dump * d)1715 static const char *btf_dump_data_newline(struct btf_dump *d)
1716 {
1717 return d->typed_dump->compact || d->typed_dump->depth == 0 ? "" : "\n";
1718 }
1719
btf_dump_data_delim(struct btf_dump * d)1720 static const char *btf_dump_data_delim(struct btf_dump *d)
1721 {
1722 return d->typed_dump->depth == 0 ? "" : ",";
1723 }
1724
btf_dump_data_pfx(struct btf_dump * d)1725 static void btf_dump_data_pfx(struct btf_dump *d)
1726 {
1727 int i, lvl = d->typed_dump->indent_lvl + d->typed_dump->depth;
1728
1729 if (d->typed_dump->compact)
1730 return;
1731
1732 for (i = 0; i < lvl; i++)
1733 btf_dump_printf(d, "%s", d->typed_dump->indent_str);
1734 }
1735
1736 /* A macro is used here as btf_type_value[s]() appends format specifiers
1737 * to the format specifier passed in; these do the work of appending
1738 * delimiters etc while the caller simply has to specify the type values
1739 * in the format specifier + value(s).
1740 */
1741 #define btf_dump_type_values(d, fmt, ...) \
1742 btf_dump_printf(d, fmt "%s%s", \
1743 ##__VA_ARGS__, \
1744 btf_dump_data_delim(d), \
1745 btf_dump_data_newline(d))
1746
btf_dump_unsupported_data(struct btf_dump * d,const struct btf_type * t,__u32 id)1747 static int btf_dump_unsupported_data(struct btf_dump *d,
1748 const struct btf_type *t,
1749 __u32 id)
1750 {
1751 btf_dump_printf(d, "<unsupported kind:%u>", btf_kind(t));
1752 return -ENOTSUP;
1753 }
1754
btf_dump_get_bitfield_value(struct btf_dump * d,const struct btf_type * t,const void * data,__u8 bits_offset,__u8 bit_sz,__u64 * value)1755 static int btf_dump_get_bitfield_value(struct btf_dump *d,
1756 const struct btf_type *t,
1757 const void *data,
1758 __u8 bits_offset,
1759 __u8 bit_sz,
1760 __u64 *value)
1761 {
1762 __u16 left_shift_bits, right_shift_bits;
1763 const __u8 *bytes = data;
1764 __u8 nr_copy_bits;
1765 __u64 num = 0;
1766 int i;
1767
1768 /* Maximum supported bitfield size is 64 bits */
1769 if (t->size > 8) {
1770 pr_warn("unexpected bitfield size %d\n", t->size);
1771 return -EINVAL;
1772 }
1773
1774 /* Bitfield value retrieval is done in two steps; first relevant bytes are
1775 * stored in num, then we left/right shift num to eliminate irrelevant bits.
1776 */
1777 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1778 for (i = t->size - 1; i >= 0; i--)
1779 num = num * 256 + bytes[i];
1780 nr_copy_bits = bit_sz + bits_offset;
1781 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1782 for (i = 0; i < t->size; i++)
1783 num = num * 256 + bytes[i];
1784 nr_copy_bits = t->size * 8 - bits_offset;
1785 #else
1786 # error "Unrecognized __BYTE_ORDER__"
1787 #endif
1788 left_shift_bits = 64 - nr_copy_bits;
1789 right_shift_bits = 64 - bit_sz;
1790
1791 *value = (num << left_shift_bits) >> right_shift_bits;
1792
1793 return 0;
1794 }
1795
btf_dump_bitfield_check_zero(struct btf_dump * d,const struct btf_type * t,const void * data,__u8 bits_offset,__u8 bit_sz)1796 static int btf_dump_bitfield_check_zero(struct btf_dump *d,
1797 const struct btf_type *t,
1798 const void *data,
1799 __u8 bits_offset,
1800 __u8 bit_sz)
1801 {
1802 __u64 check_num;
1803 int err;
1804
1805 err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &check_num);
1806 if (err)
1807 return err;
1808 if (check_num == 0)
1809 return -ENODATA;
1810 return 0;
1811 }
1812
btf_dump_bitfield_data(struct btf_dump * d,const struct btf_type * t,const void * data,__u8 bits_offset,__u8 bit_sz)1813 static int btf_dump_bitfield_data(struct btf_dump *d,
1814 const struct btf_type *t,
1815 const void *data,
1816 __u8 bits_offset,
1817 __u8 bit_sz)
1818 {
1819 __u64 print_num;
1820 int err;
1821
1822 err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz, &print_num);
1823 if (err)
1824 return err;
1825
1826 btf_dump_type_values(d, "0x%llx", (unsigned long long)print_num);
1827
1828 return 0;
1829 }
1830
1831 /* ints, floats and ptrs */
btf_dump_base_type_check_zero(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)1832 static int btf_dump_base_type_check_zero(struct btf_dump *d,
1833 const struct btf_type *t,
1834 __u32 id,
1835 const void *data)
1836 {
1837 static __u8 bytecmp[16] = {};
1838 int nr_bytes;
1839
1840 /* For pointer types, pointer size is not defined on a per-type basis.
1841 * On dump creation however, we store the pointer size.
1842 */
1843 if (btf_kind(t) == BTF_KIND_PTR)
1844 nr_bytes = d->ptr_sz;
1845 else
1846 nr_bytes = t->size;
1847
1848 if (nr_bytes < 1 || nr_bytes > 16) {
1849 pr_warn("unexpected size %d for id [%u]\n", nr_bytes, id);
1850 return -EINVAL;
1851 }
1852
1853 if (memcmp(data, bytecmp, nr_bytes) == 0)
1854 return -ENODATA;
1855 return 0;
1856 }
1857
ptr_is_aligned(const struct btf * btf,__u32 type_id,const void * data)1858 static bool ptr_is_aligned(const struct btf *btf, __u32 type_id,
1859 const void *data)
1860 {
1861 int alignment = btf__align_of(btf, type_id);
1862
1863 if (alignment == 0)
1864 return false;
1865
1866 return ((uintptr_t)data) % alignment == 0;
1867 }
1868
btf_dump_int_data(struct btf_dump * d,const struct btf_type * t,__u32 type_id,const void * data,__u8 bits_offset)1869 static int btf_dump_int_data(struct btf_dump *d,
1870 const struct btf_type *t,
1871 __u32 type_id,
1872 const void *data,
1873 __u8 bits_offset)
1874 {
1875 __u8 encoding = btf_int_encoding(t);
1876 bool sign = encoding & BTF_INT_SIGNED;
1877 char buf[16] __attribute__((aligned(16)));
1878 int sz = t->size;
1879
1880 if (sz == 0 || sz > sizeof(buf)) {
1881 pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1882 return -EINVAL;
1883 }
1884
1885 /* handle packed int data - accesses of integers not aligned on
1886 * int boundaries can cause problems on some platforms.
1887 */
1888 if (!ptr_is_aligned(d->btf, type_id, data)) {
1889 memcpy(buf, data, sz);
1890 data = buf;
1891 }
1892
1893 switch (sz) {
1894 case 16: {
1895 const __u64 *ints = data;
1896 __u64 lsi, msi;
1897
1898 /* avoid use of __int128 as some 32-bit platforms do not
1899 * support it.
1900 */
1901 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1902 lsi = ints[0];
1903 msi = ints[1];
1904 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1905 lsi = ints[1];
1906 msi = ints[0];
1907 #else
1908 # error "Unrecognized __BYTE_ORDER__"
1909 #endif
1910 if (msi == 0)
1911 btf_dump_type_values(d, "0x%llx", (unsigned long long)lsi);
1912 else
1913 btf_dump_type_values(d, "0x%llx%016llx", (unsigned long long)msi,
1914 (unsigned long long)lsi);
1915 break;
1916 }
1917 case 8:
1918 if (sign)
1919 btf_dump_type_values(d, "%lld", *(long long *)data);
1920 else
1921 btf_dump_type_values(d, "%llu", *(unsigned long long *)data);
1922 break;
1923 case 4:
1924 if (sign)
1925 btf_dump_type_values(d, "%d", *(__s32 *)data);
1926 else
1927 btf_dump_type_values(d, "%u", *(__u32 *)data);
1928 break;
1929 case 2:
1930 if (sign)
1931 btf_dump_type_values(d, "%d", *(__s16 *)data);
1932 else
1933 btf_dump_type_values(d, "%u", *(__u16 *)data);
1934 break;
1935 case 1:
1936 if (d->typed_dump->is_array_char) {
1937 /* check for null terminator */
1938 if (d->typed_dump->is_array_terminated)
1939 break;
1940 if (*(char *)data == '\0') {
1941 btf_dump_type_values(d, "'\\0'");
1942 d->typed_dump->is_array_terminated = true;
1943 break;
1944 }
1945 if (isprint(*(char *)data)) {
1946 btf_dump_type_values(d, "'%c'", *(char *)data);
1947 break;
1948 }
1949 }
1950 if (sign)
1951 btf_dump_type_values(d, "%d", *(__s8 *)data);
1952 else
1953 btf_dump_type_values(d, "%u", *(__u8 *)data);
1954 break;
1955 default:
1956 pr_warn("unexpected sz %d for id [%u]\n", sz, type_id);
1957 return -EINVAL;
1958 }
1959 return 0;
1960 }
1961
1962 union float_data {
1963 long double ld;
1964 double d;
1965 float f;
1966 };
1967
btf_dump_float_data(struct btf_dump * d,const struct btf_type * t,__u32 type_id,const void * data)1968 static int btf_dump_float_data(struct btf_dump *d,
1969 const struct btf_type *t,
1970 __u32 type_id,
1971 const void *data)
1972 {
1973 const union float_data *flp = data;
1974 union float_data fl;
1975 int sz = t->size;
1976
1977 /* handle unaligned data; copy to local union */
1978 if (!ptr_is_aligned(d->btf, type_id, data)) {
1979 memcpy(&fl, data, sz);
1980 flp = &fl;
1981 }
1982
1983 switch (sz) {
1984 case 16:
1985 btf_dump_type_values(d, "%Lf", flp->ld);
1986 break;
1987 case 8:
1988 btf_dump_type_values(d, "%lf", flp->d);
1989 break;
1990 case 4:
1991 btf_dump_type_values(d, "%f", flp->f);
1992 break;
1993 default:
1994 pr_warn("unexpected size %d for id [%u]\n", sz, type_id);
1995 return -EINVAL;
1996 }
1997 return 0;
1998 }
1999
btf_dump_var_data(struct btf_dump * d,const struct btf_type * v,__u32 id,const void * data)2000 static int btf_dump_var_data(struct btf_dump *d,
2001 const struct btf_type *v,
2002 __u32 id,
2003 const void *data)
2004 {
2005 enum btf_func_linkage linkage = btf_var(v)->linkage;
2006 const struct btf_type *t;
2007 const char *l;
2008 __u32 type_id;
2009
2010 switch (linkage) {
2011 case BTF_FUNC_STATIC:
2012 l = "static ";
2013 break;
2014 case BTF_FUNC_EXTERN:
2015 l = "extern ";
2016 break;
2017 case BTF_FUNC_GLOBAL:
2018 default:
2019 l = "";
2020 break;
2021 }
2022
2023 /* format of output here is [linkage] [type] [varname] = (type)value,
2024 * for example "static int cpu_profile_flip = (int)1"
2025 */
2026 btf_dump_printf(d, "%s", l);
2027 type_id = v->type;
2028 t = btf__type_by_id(d->btf, type_id);
2029 btf_dump_emit_type_cast(d, type_id, false);
2030 btf_dump_printf(d, " %s = ", btf_name_of(d, v->name_off));
2031 return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0);
2032 }
2033
btf_dump_array_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)2034 static int btf_dump_array_data(struct btf_dump *d,
2035 const struct btf_type *t,
2036 __u32 id,
2037 const void *data)
2038 {
2039 const struct btf_array *array = btf_array(t);
2040 const struct btf_type *elem_type;
2041 __u32 i, elem_type_id;
2042 __s64 elem_size;
2043 bool is_array_member;
2044 bool is_array_terminated;
2045
2046 elem_type_id = array->type;
2047 elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2048 elem_size = btf__resolve_size(d->btf, elem_type_id);
2049 if (elem_size <= 0) {
2050 pr_warn("unexpected elem size %zd for array type [%u]\n",
2051 (ssize_t)elem_size, id);
2052 return -EINVAL;
2053 }
2054
2055 if (btf_is_int(elem_type)) {
2056 /*
2057 * BTF_INT_CHAR encoding never seems to be set for
2058 * char arrays, so if size is 1 and element is
2059 * printable as a char, we'll do that.
2060 */
2061 if (elem_size == 1)
2062 d->typed_dump->is_array_char = true;
2063 }
2064
2065 /* note that we increment depth before calling btf_dump_print() below;
2066 * this is intentional. btf_dump_data_newline() will not print a
2067 * newline for depth 0 (since this leaves us with trailing newlines
2068 * at the end of typed display), so depth is incremented first.
2069 * For similar reasons, we decrement depth before showing the closing
2070 * parenthesis.
2071 */
2072 d->typed_dump->depth++;
2073 btf_dump_printf(d, "[%s", btf_dump_data_newline(d));
2074
2075 /* may be a multidimensional array, so store current "is array member"
2076 * status so we can restore it correctly later.
2077 */
2078 is_array_member = d->typed_dump->is_array_member;
2079 d->typed_dump->is_array_member = true;
2080 is_array_terminated = d->typed_dump->is_array_terminated;
2081 d->typed_dump->is_array_terminated = false;
2082 for (i = 0; i < array->nelems; i++, data += elem_size) {
2083 if (d->typed_dump->is_array_terminated)
2084 break;
2085 btf_dump_dump_type_data(d, NULL, elem_type, elem_type_id, data, 0, 0);
2086 }
2087 d->typed_dump->is_array_member = is_array_member;
2088 d->typed_dump->is_array_terminated = is_array_terminated;
2089 d->typed_dump->depth--;
2090 btf_dump_data_pfx(d);
2091 btf_dump_type_values(d, "]");
2092
2093 return 0;
2094 }
2095
btf_dump_struct_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)2096 static int btf_dump_struct_data(struct btf_dump *d,
2097 const struct btf_type *t,
2098 __u32 id,
2099 const void *data)
2100 {
2101 const struct btf_member *m = btf_members(t);
2102 __u16 n = btf_vlen(t);
2103 int i, err = 0;
2104
2105 /* note that we increment depth before calling btf_dump_print() below;
2106 * this is intentional. btf_dump_data_newline() will not print a
2107 * newline for depth 0 (since this leaves us with trailing newlines
2108 * at the end of typed display), so depth is incremented first.
2109 * For similar reasons, we decrement depth before showing the closing
2110 * parenthesis.
2111 */
2112 d->typed_dump->depth++;
2113 btf_dump_printf(d, "{%s", btf_dump_data_newline(d));
2114
2115 for (i = 0; i < n; i++, m++) {
2116 const struct btf_type *mtype;
2117 const char *mname;
2118 __u32 moffset;
2119 __u8 bit_sz;
2120
2121 mtype = btf__type_by_id(d->btf, m->type);
2122 mname = btf_name_of(d, m->name_off);
2123 moffset = btf_member_bit_offset(t, i);
2124
2125 bit_sz = btf_member_bitfield_size(t, i);
2126 err = btf_dump_dump_type_data(d, mname, mtype, m->type, data + moffset / 8,
2127 moffset % 8, bit_sz);
2128 if (err < 0)
2129 return err;
2130 }
2131 d->typed_dump->depth--;
2132 btf_dump_data_pfx(d);
2133 btf_dump_type_values(d, "}");
2134 return err;
2135 }
2136
2137 union ptr_data {
2138 unsigned int p;
2139 unsigned long long lp;
2140 };
2141
btf_dump_ptr_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)2142 static int btf_dump_ptr_data(struct btf_dump *d,
2143 const struct btf_type *t,
2144 __u32 id,
2145 const void *data)
2146 {
2147 if (ptr_is_aligned(d->btf, id, data) && d->ptr_sz == sizeof(void *)) {
2148 btf_dump_type_values(d, "%p", *(void **)data);
2149 } else {
2150 union ptr_data pt;
2151
2152 memcpy(&pt, data, d->ptr_sz);
2153 if (d->ptr_sz == 4)
2154 btf_dump_type_values(d, "0x%x", pt.p);
2155 else
2156 btf_dump_type_values(d, "0x%llx", pt.lp);
2157 }
2158 return 0;
2159 }
2160
btf_dump_get_enum_value(struct btf_dump * d,const struct btf_type * t,const void * data,__u32 id,__s64 * value)2161 static int btf_dump_get_enum_value(struct btf_dump *d,
2162 const struct btf_type *t,
2163 const void *data,
2164 __u32 id,
2165 __s64 *value)
2166 {
2167 bool is_signed = btf_kflag(t);
2168
2169 if (!ptr_is_aligned(d->btf, id, data)) {
2170 __u64 val;
2171 int err;
2172
2173 err = btf_dump_get_bitfield_value(d, t, data, 0, 0, &val);
2174 if (err)
2175 return err;
2176 *value = (__s64)val;
2177 return 0;
2178 }
2179
2180 switch (t->size) {
2181 case 8:
2182 *value = *(__s64 *)data;
2183 return 0;
2184 case 4:
2185 *value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;
2186 return 0;
2187 case 2:
2188 *value = is_signed ? *(__s16 *)data : *(__u16 *)data;
2189 return 0;
2190 case 1:
2191 *value = is_signed ? *(__s8 *)data : *(__u8 *)data;
2192 return 0;
2193 default:
2194 pr_warn("unexpected size %d for enum, id:[%u]\n", t->size, id);
2195 return -EINVAL;
2196 }
2197 }
2198
btf_dump_enum_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)2199 static int btf_dump_enum_data(struct btf_dump *d,
2200 const struct btf_type *t,
2201 __u32 id,
2202 const void *data)
2203 {
2204 bool is_signed;
2205 __s64 value;
2206 int i, err;
2207
2208 err = btf_dump_get_enum_value(d, t, data, id, &value);
2209 if (err)
2210 return err;
2211
2212 is_signed = btf_kflag(t);
2213 if (btf_is_enum(t)) {
2214 const struct btf_enum *e;
2215
2216 for (i = 0, e = btf_enum(t); i < btf_vlen(t); i++, e++) {
2217 if (value != e->val)
2218 continue;
2219 btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2220 return 0;
2221 }
2222
2223 btf_dump_type_values(d, is_signed ? "%d" : "%u", value);
2224 } else {
2225 const struct btf_enum64 *e;
2226
2227 for (i = 0, e = btf_enum64(t); i < btf_vlen(t); i++, e++) {
2228 if (value != btf_enum64_value(e))
2229 continue;
2230 btf_dump_type_values(d, "%s", btf_name_of(d, e->name_off));
2231 return 0;
2232 }
2233
2234 btf_dump_type_values(d, is_signed ? "%lldLL" : "%lluULL",
2235 (unsigned long long)value);
2236 }
2237 return 0;
2238 }
2239
btf_dump_datasec_data(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data)2240 static int btf_dump_datasec_data(struct btf_dump *d,
2241 const struct btf_type *t,
2242 __u32 id,
2243 const void *data)
2244 {
2245 const struct btf_var_secinfo *vsi;
2246 const struct btf_type *var;
2247 __u32 i;
2248 int err;
2249
2250 btf_dump_type_values(d, "SEC(\"%s\") ", btf_name_of(d, t->name_off));
2251
2252 for (i = 0, vsi = btf_var_secinfos(t); i < btf_vlen(t); i++, vsi++) {
2253 var = btf__type_by_id(d->btf, vsi->type);
2254 err = btf_dump_dump_type_data(d, NULL, var, vsi->type, data + vsi->offset, 0, 0);
2255 if (err < 0)
2256 return err;
2257 btf_dump_printf(d, ";");
2258 }
2259 return 0;
2260 }
2261
2262 /* return size of type, or if base type overflows, return -E2BIG. */
btf_dump_type_data_check_overflow(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data,__u8 bits_offset,__u8 bit_sz)2263 static int btf_dump_type_data_check_overflow(struct btf_dump *d,
2264 const struct btf_type *t,
2265 __u32 id,
2266 const void *data,
2267 __u8 bits_offset,
2268 __u8 bit_sz)
2269 {
2270 __s64 size;
2271
2272 if (bit_sz) {
2273 /* bits_offset is at most 7. bit_sz is at most 128. */
2274 __u8 nr_bytes = (bits_offset + bit_sz + 7) / 8;
2275
2276 /* When bit_sz is non zero, it is called from
2277 * btf_dump_struct_data() where it only cares about
2278 * negative error value.
2279 * Return nr_bytes in success case to make it
2280 * consistent as the regular integer case below.
2281 */
2282 return data + nr_bytes > d->typed_dump->data_end ? -E2BIG : nr_bytes;
2283 }
2284
2285 size = btf__resolve_size(d->btf, id);
2286
2287 if (size < 0 || size >= INT_MAX) {
2288 pr_warn("unexpected size [%zu] for id [%u]\n",
2289 (size_t)size, id);
2290 return -EINVAL;
2291 }
2292
2293 /* Only do overflow checking for base types; we do not want to
2294 * avoid showing part of a struct, union or array, even if we
2295 * do not have enough data to show the full object. By
2296 * restricting overflow checking to base types we can ensure
2297 * that partial display succeeds, while avoiding overflowing
2298 * and using bogus data for display.
2299 */
2300 t = skip_mods_and_typedefs(d->btf, id, NULL);
2301 if (!t) {
2302 pr_warn("unexpected error skipping mods/typedefs for id [%u]\n",
2303 id);
2304 return -EINVAL;
2305 }
2306
2307 switch (btf_kind(t)) {
2308 case BTF_KIND_INT:
2309 case BTF_KIND_FLOAT:
2310 case BTF_KIND_PTR:
2311 case BTF_KIND_ENUM:
2312 case BTF_KIND_ENUM64:
2313 if (data + bits_offset / 8 + size > d->typed_dump->data_end)
2314 return -E2BIG;
2315 break;
2316 default:
2317 break;
2318 }
2319 return (int)size;
2320 }
2321
btf_dump_type_data_check_zero(struct btf_dump * d,const struct btf_type * t,__u32 id,const void * data,__u8 bits_offset,__u8 bit_sz)2322 static int btf_dump_type_data_check_zero(struct btf_dump *d,
2323 const struct btf_type *t,
2324 __u32 id,
2325 const void *data,
2326 __u8 bits_offset,
2327 __u8 bit_sz)
2328 {
2329 __s64 value;
2330 int i, err;
2331
2332 /* toplevel exceptions; we show zero values if
2333 * - we ask for them (emit_zeros)
2334 * - if we are at top-level so we see "struct empty { }"
2335 * - or if we are an array member and the array is non-empty and
2336 * not a char array; we don't want to be in a situation where we
2337 * have an integer array 0, 1, 0, 1 and only show non-zero values.
2338 * If the array contains zeroes only, or is a char array starting
2339 * with a '\0', the array-level check_zero() will prevent showing it;
2340 * we are concerned with determining zero value at the array member
2341 * level here.
2342 */
2343 if (d->typed_dump->emit_zeroes || d->typed_dump->depth == 0 ||
2344 (d->typed_dump->is_array_member &&
2345 !d->typed_dump->is_array_char))
2346 return 0;
2347
2348 t = skip_mods_and_typedefs(d->btf, id, NULL);
2349
2350 switch (btf_kind(t)) {
2351 case BTF_KIND_INT:
2352 if (bit_sz)
2353 return btf_dump_bitfield_check_zero(d, t, data, bits_offset, bit_sz);
2354 return btf_dump_base_type_check_zero(d, t, id, data);
2355 case BTF_KIND_FLOAT:
2356 case BTF_KIND_PTR:
2357 return btf_dump_base_type_check_zero(d, t, id, data);
2358 case BTF_KIND_ARRAY: {
2359 const struct btf_array *array = btf_array(t);
2360 const struct btf_type *elem_type;
2361 __u32 elem_type_id, elem_size;
2362 bool ischar;
2363
2364 elem_type_id = array->type;
2365 elem_size = btf__resolve_size(d->btf, elem_type_id);
2366 elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
2367
2368 ischar = btf_is_int(elem_type) && elem_size == 1;
2369
2370 /* check all elements; if _any_ element is nonzero, all
2371 * of array is displayed. We make an exception however
2372 * for char arrays where the first element is 0; these
2373 * are considered zeroed also, even if later elements are
2374 * non-zero because the string is terminated.
2375 */
2376 for (i = 0; i < array->nelems; i++) {
2377 if (i == 0 && ischar && *(char *)data == 0)
2378 return -ENODATA;
2379 err = btf_dump_type_data_check_zero(d, elem_type,
2380 elem_type_id,
2381 data +
2382 (i * elem_size),
2383 bits_offset, 0);
2384 if (err != -ENODATA)
2385 return err;
2386 }
2387 return -ENODATA;
2388 }
2389 case BTF_KIND_STRUCT:
2390 case BTF_KIND_UNION: {
2391 const struct btf_member *m = btf_members(t);
2392 __u16 n = btf_vlen(t);
2393
2394 /* if any struct/union member is non-zero, the struct/union
2395 * is considered non-zero and dumped.
2396 */
2397 for (i = 0; i < n; i++, m++) {
2398 const struct btf_type *mtype;
2399 __u32 moffset;
2400
2401 mtype = btf__type_by_id(d->btf, m->type);
2402 moffset = btf_member_bit_offset(t, i);
2403
2404 /* btf_int_bits() does not store member bitfield size;
2405 * bitfield size needs to be stored here so int display
2406 * of member can retrieve it.
2407 */
2408 bit_sz = btf_member_bitfield_size(t, i);
2409 err = btf_dump_type_data_check_zero(d, mtype, m->type, data + moffset / 8,
2410 moffset % 8, bit_sz);
2411 if (err != ENODATA)
2412 return err;
2413 }
2414 return -ENODATA;
2415 }
2416 case BTF_KIND_ENUM:
2417 case BTF_KIND_ENUM64:
2418 err = btf_dump_get_enum_value(d, t, data, id, &value);
2419 if (err)
2420 return err;
2421 if (value == 0)
2422 return -ENODATA;
2423 return 0;
2424 default:
2425 return 0;
2426 }
2427 }
2428
2429 /* returns size of data dumped, or error. */
btf_dump_dump_type_data(struct btf_dump * d,const char * fname,const struct btf_type * t,__u32 id,const void * data,__u8 bits_offset,__u8 bit_sz)2430 static int btf_dump_dump_type_data(struct btf_dump *d,
2431 const char *fname,
2432 const struct btf_type *t,
2433 __u32 id,
2434 const void *data,
2435 __u8 bits_offset,
2436 __u8 bit_sz)
2437 {
2438 int size, err = 0;
2439
2440 size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz);
2441 if (size < 0)
2442 return size;
2443 err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);
2444 if (err) {
2445 /* zeroed data is expected and not an error, so simply skip
2446 * dumping such data. Record other errors however.
2447 */
2448 if (err == -ENODATA)
2449 return size;
2450 return err;
2451 }
2452 btf_dump_data_pfx(d);
2453
2454 if (!d->typed_dump->skip_names) {
2455 if (fname && strlen(fname) > 0)
2456 btf_dump_printf(d, ".%s = ", fname);
2457 btf_dump_emit_type_cast(d, id, true);
2458 }
2459
2460 t = skip_mods_and_typedefs(d->btf, id, NULL);
2461
2462 switch (btf_kind(t)) {
2463 case BTF_KIND_UNKN:
2464 case BTF_KIND_FWD:
2465 case BTF_KIND_FUNC:
2466 case BTF_KIND_FUNC_PROTO:
2467 case BTF_KIND_DECL_TAG:
2468 err = btf_dump_unsupported_data(d, t, id);
2469 break;
2470 case BTF_KIND_INT:
2471 if (bit_sz)
2472 err = btf_dump_bitfield_data(d, t, data, bits_offset, bit_sz);
2473 else
2474 err = btf_dump_int_data(d, t, id, data, bits_offset);
2475 break;
2476 case BTF_KIND_FLOAT:
2477 err = btf_dump_float_data(d, t, id, data);
2478 break;
2479 case BTF_KIND_PTR:
2480 err = btf_dump_ptr_data(d, t, id, data);
2481 break;
2482 case BTF_KIND_ARRAY:
2483 err = btf_dump_array_data(d, t, id, data);
2484 break;
2485 case BTF_KIND_STRUCT:
2486 case BTF_KIND_UNION:
2487 err = btf_dump_struct_data(d, t, id, data);
2488 break;
2489 case BTF_KIND_ENUM:
2490 case BTF_KIND_ENUM64:
2491 /* handle bitfield and int enum values */
2492 if (bit_sz) {
2493 __u64 print_num;
2494 __s64 enum_val;
2495
2496 err = btf_dump_get_bitfield_value(d, t, data, bits_offset, bit_sz,
2497 &print_num);
2498 if (err)
2499 break;
2500 enum_val = (__s64)print_num;
2501 err = btf_dump_enum_data(d, t, id, &enum_val);
2502 } else
2503 err = btf_dump_enum_data(d, t, id, data);
2504 break;
2505 case BTF_KIND_VAR:
2506 err = btf_dump_var_data(d, t, id, data);
2507 break;
2508 case BTF_KIND_DATASEC:
2509 err = btf_dump_datasec_data(d, t, id, data);
2510 break;
2511 default:
2512 pr_warn("unexpected kind [%u] for id [%u]\n",
2513 BTF_INFO_KIND(t->info), id);
2514 return -EINVAL;
2515 }
2516 if (err < 0)
2517 return err;
2518 return size;
2519 }
2520
btf_dump__dump_type_data(struct btf_dump * d,__u32 id,const void * data,size_t data_sz,const struct btf_dump_type_data_opts * opts)2521 int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
2522 const void *data, size_t data_sz,
2523 const struct btf_dump_type_data_opts *opts)
2524 {
2525 struct btf_dump_data typed_dump = {};
2526 const struct btf_type *t;
2527 int ret;
2528
2529 if (!OPTS_VALID(opts, btf_dump_type_data_opts))
2530 return libbpf_err(-EINVAL);
2531
2532 t = btf__type_by_id(d->btf, id);
2533 if (!t)
2534 return libbpf_err(-ENOENT);
2535
2536 d->typed_dump = &typed_dump;
2537 d->typed_dump->data_end = data + data_sz;
2538 d->typed_dump->indent_lvl = OPTS_GET(opts, indent_level, 0);
2539
2540 /* default indent string is a tab */
2541 if (!OPTS_GET(opts, indent_str, NULL))
2542 d->typed_dump->indent_str[0] = '\t';
2543 else
2544 libbpf_strlcpy(d->typed_dump->indent_str, opts->indent_str,
2545 sizeof(d->typed_dump->indent_str));
2546
2547 d->typed_dump->compact = OPTS_GET(opts, compact, false);
2548 d->typed_dump->skip_names = OPTS_GET(opts, skip_names, false);
2549 d->typed_dump->emit_zeroes = OPTS_GET(opts, emit_zeroes, false);
2550
2551 ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
2552
2553 d->typed_dump = NULL;
2554
2555 return libbpf_err(ret);
2556 }
2557