File Index
Files and directories
Sign in
FILE

btf_dump.c

File preview with syntax highlighting.

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