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