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