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