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