162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright (c) 2020 Facebook */
362306a36Sopenharmony_ci#include <stdio.h>
462306a36Sopenharmony_ci#include <errno.h>
562306a36Sopenharmony_ci#include <bpf/btf.h>
662306a36Sopenharmony_ci#include <bpf/libbpf.h>
762306a36Sopenharmony_ci#include "test_progs.h"
862306a36Sopenharmony_ci
962306a36Sopenharmony_cistatic const char * const btf_kind_str_mapping[] = {
1062306a36Sopenharmony_ci	[BTF_KIND_UNKN]		= "UNKNOWN",
1162306a36Sopenharmony_ci	[BTF_KIND_INT]		= "INT",
1262306a36Sopenharmony_ci	[BTF_KIND_PTR]		= "PTR",
1362306a36Sopenharmony_ci	[BTF_KIND_ARRAY]	= "ARRAY",
1462306a36Sopenharmony_ci	[BTF_KIND_STRUCT]	= "STRUCT",
1562306a36Sopenharmony_ci	[BTF_KIND_UNION]	= "UNION",
1662306a36Sopenharmony_ci	[BTF_KIND_ENUM]		= "ENUM",
1762306a36Sopenharmony_ci	[BTF_KIND_FWD]		= "FWD",
1862306a36Sopenharmony_ci	[BTF_KIND_TYPEDEF]	= "TYPEDEF",
1962306a36Sopenharmony_ci	[BTF_KIND_VOLATILE]	= "VOLATILE",
2062306a36Sopenharmony_ci	[BTF_KIND_CONST]	= "CONST",
2162306a36Sopenharmony_ci	[BTF_KIND_RESTRICT]	= "RESTRICT",
2262306a36Sopenharmony_ci	[BTF_KIND_FUNC]		= "FUNC",
2362306a36Sopenharmony_ci	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
2462306a36Sopenharmony_ci	[BTF_KIND_VAR]		= "VAR",
2562306a36Sopenharmony_ci	[BTF_KIND_DATASEC]	= "DATASEC",
2662306a36Sopenharmony_ci	[BTF_KIND_FLOAT]	= "FLOAT",
2762306a36Sopenharmony_ci	[BTF_KIND_DECL_TAG]	= "DECL_TAG",
2862306a36Sopenharmony_ci	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",
2962306a36Sopenharmony_ci	[BTF_KIND_ENUM64]	= "ENUM64",
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic const char *btf_kind_str(__u16 kind)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	if (kind > BTF_KIND_ENUM64)
3562306a36Sopenharmony_ci		return "UNKNOWN";
3662306a36Sopenharmony_ci	return btf_kind_str_mapping[kind];
3762306a36Sopenharmony_ci}
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistatic const char *btf_int_enc_str(__u8 encoding)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	switch (encoding) {
4262306a36Sopenharmony_ci	case 0:
4362306a36Sopenharmony_ci		return "(none)";
4462306a36Sopenharmony_ci	case BTF_INT_SIGNED:
4562306a36Sopenharmony_ci		return "SIGNED";
4662306a36Sopenharmony_ci	case BTF_INT_CHAR:
4762306a36Sopenharmony_ci		return "CHAR";
4862306a36Sopenharmony_ci	case BTF_INT_BOOL:
4962306a36Sopenharmony_ci		return "BOOL";
5062306a36Sopenharmony_ci	default:
5162306a36Sopenharmony_ci		return "UNKN";
5262306a36Sopenharmony_ci	}
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic const char *btf_var_linkage_str(__u32 linkage)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	switch (linkage) {
5862306a36Sopenharmony_ci	case BTF_VAR_STATIC:
5962306a36Sopenharmony_ci		return "static";
6062306a36Sopenharmony_ci	case BTF_VAR_GLOBAL_ALLOCATED:
6162306a36Sopenharmony_ci		return "global-alloc";
6262306a36Sopenharmony_ci	default:
6362306a36Sopenharmony_ci		return "(unknown)";
6462306a36Sopenharmony_ci	}
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic const char *btf_func_linkage_str(const struct btf_type *t)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	switch (btf_vlen(t)) {
7062306a36Sopenharmony_ci	case BTF_FUNC_STATIC:
7162306a36Sopenharmony_ci		return "static";
7262306a36Sopenharmony_ci	case BTF_FUNC_GLOBAL:
7362306a36Sopenharmony_ci		return "global";
7462306a36Sopenharmony_ci	case BTF_FUNC_EXTERN:
7562306a36Sopenharmony_ci		return "extern";
7662306a36Sopenharmony_ci	default:
7762306a36Sopenharmony_ci		return "(unknown)";
7862306a36Sopenharmony_ci	}
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cistatic const char *btf_str(const struct btf *btf, __u32 off)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	if (!off)
8462306a36Sopenharmony_ci		return "(anon)";
8562306a36Sopenharmony_ci	return btf__str_by_offset(btf, off) ?: "(invalid)";
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciint fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	const struct btf_type *t;
9162306a36Sopenharmony_ci	int kind, i;
9262306a36Sopenharmony_ci	__u32 vlen;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	t = btf__type_by_id(btf, id);
9562306a36Sopenharmony_ci	if (!t)
9662306a36Sopenharmony_ci		return -EINVAL;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	vlen = btf_vlen(t);
9962306a36Sopenharmony_ci	kind = btf_kind(t);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	fprintf(out, "[%u] %s '%s'", id, btf_kind_str(kind), btf_str(btf, t->name_off));
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	switch (kind) {
10462306a36Sopenharmony_ci	case BTF_KIND_INT:
10562306a36Sopenharmony_ci		fprintf(out, " size=%u bits_offset=%u nr_bits=%u encoding=%s",
10662306a36Sopenharmony_ci			t->size, btf_int_offset(t), btf_int_bits(t),
10762306a36Sopenharmony_ci			btf_int_enc_str(btf_int_encoding(t)));
10862306a36Sopenharmony_ci		break;
10962306a36Sopenharmony_ci	case BTF_KIND_PTR:
11062306a36Sopenharmony_ci	case BTF_KIND_CONST:
11162306a36Sopenharmony_ci	case BTF_KIND_VOLATILE:
11262306a36Sopenharmony_ci	case BTF_KIND_RESTRICT:
11362306a36Sopenharmony_ci	case BTF_KIND_TYPEDEF:
11462306a36Sopenharmony_ci	case BTF_KIND_TYPE_TAG:
11562306a36Sopenharmony_ci		fprintf(out, " type_id=%u", t->type);
11662306a36Sopenharmony_ci		break;
11762306a36Sopenharmony_ci	case BTF_KIND_ARRAY: {
11862306a36Sopenharmony_ci		const struct btf_array *arr = btf_array(t);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci		fprintf(out, " type_id=%u index_type_id=%u nr_elems=%u",
12162306a36Sopenharmony_ci			arr->type, arr->index_type, arr->nelems);
12262306a36Sopenharmony_ci		break;
12362306a36Sopenharmony_ci	}
12462306a36Sopenharmony_ci	case BTF_KIND_STRUCT:
12562306a36Sopenharmony_ci	case BTF_KIND_UNION: {
12662306a36Sopenharmony_ci		const struct btf_member *m = btf_members(t);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci		fprintf(out, " size=%u vlen=%u", t->size, vlen);
12962306a36Sopenharmony_ci		for (i = 0; i < vlen; i++, m++) {
13062306a36Sopenharmony_ci			__u32 bit_off, bit_sz;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci			bit_off = btf_member_bit_offset(t, i);
13362306a36Sopenharmony_ci			bit_sz = btf_member_bitfield_size(t, i);
13462306a36Sopenharmony_ci			fprintf(out, "\n\t'%s' type_id=%u bits_offset=%u",
13562306a36Sopenharmony_ci				btf_str(btf, m->name_off), m->type, bit_off);
13662306a36Sopenharmony_ci			if (bit_sz)
13762306a36Sopenharmony_ci				fprintf(out, " bitfield_size=%u", bit_sz);
13862306a36Sopenharmony_ci		}
13962306a36Sopenharmony_ci		break;
14062306a36Sopenharmony_ci	}
14162306a36Sopenharmony_ci	case BTF_KIND_ENUM: {
14262306a36Sopenharmony_ci		const struct btf_enum *v = btf_enum(t);
14362306a36Sopenharmony_ci		const char *fmt_str;
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci		fmt_str = btf_kflag(t) ? "\n\t'%s' val=%d" : "\n\t'%s' val=%u";
14662306a36Sopenharmony_ci		fprintf(out, " encoding=%s size=%u vlen=%u",
14762306a36Sopenharmony_ci			btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);
14862306a36Sopenharmony_ci		for (i = 0; i < vlen; i++, v++) {
14962306a36Sopenharmony_ci			fprintf(out, fmt_str,
15062306a36Sopenharmony_ci				btf_str(btf, v->name_off), v->val);
15162306a36Sopenharmony_ci		}
15262306a36Sopenharmony_ci		break;
15362306a36Sopenharmony_ci	}
15462306a36Sopenharmony_ci	case BTF_KIND_ENUM64: {
15562306a36Sopenharmony_ci		const struct btf_enum64 *v = btf_enum64(t);
15662306a36Sopenharmony_ci		const char *fmt_str;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci		fmt_str = btf_kflag(t) ? "\n\t'%s' val=%lld" : "\n\t'%s' val=%llu";
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci		fprintf(out, " encoding=%s size=%u vlen=%u",
16162306a36Sopenharmony_ci			btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);
16262306a36Sopenharmony_ci		for (i = 0; i < vlen; i++, v++) {
16362306a36Sopenharmony_ci			fprintf(out, fmt_str,
16462306a36Sopenharmony_ci				btf_str(btf, v->name_off),
16562306a36Sopenharmony_ci				((__u64)v->val_hi32 << 32) | v->val_lo32);
16662306a36Sopenharmony_ci		}
16762306a36Sopenharmony_ci		break;
16862306a36Sopenharmony_ci	}
16962306a36Sopenharmony_ci	case BTF_KIND_FWD:
17062306a36Sopenharmony_ci		fprintf(out, " fwd_kind=%s", btf_kflag(t) ? "union" : "struct");
17162306a36Sopenharmony_ci		break;
17262306a36Sopenharmony_ci	case BTF_KIND_FUNC:
17362306a36Sopenharmony_ci		fprintf(out, " type_id=%u linkage=%s", t->type, btf_func_linkage_str(t));
17462306a36Sopenharmony_ci		break;
17562306a36Sopenharmony_ci	case BTF_KIND_FUNC_PROTO: {
17662306a36Sopenharmony_ci		const struct btf_param *p = btf_params(t);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci		fprintf(out, " ret_type_id=%u vlen=%u", t->type, vlen);
17962306a36Sopenharmony_ci		for (i = 0; i < vlen; i++, p++) {
18062306a36Sopenharmony_ci			fprintf(out, "\n\t'%s' type_id=%u",
18162306a36Sopenharmony_ci				btf_str(btf, p->name_off), p->type);
18262306a36Sopenharmony_ci		}
18362306a36Sopenharmony_ci		break;
18462306a36Sopenharmony_ci	}
18562306a36Sopenharmony_ci	case BTF_KIND_VAR:
18662306a36Sopenharmony_ci		fprintf(out, " type_id=%u, linkage=%s",
18762306a36Sopenharmony_ci			t->type, btf_var_linkage_str(btf_var(t)->linkage));
18862306a36Sopenharmony_ci		break;
18962306a36Sopenharmony_ci	case BTF_KIND_DATASEC: {
19062306a36Sopenharmony_ci		const struct btf_var_secinfo *v = btf_var_secinfos(t);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci		fprintf(out, " size=%u vlen=%u", t->size, vlen);
19362306a36Sopenharmony_ci		for (i = 0; i < vlen; i++, v++) {
19462306a36Sopenharmony_ci			fprintf(out, "\n\ttype_id=%u offset=%u size=%u",
19562306a36Sopenharmony_ci				v->type, v->offset, v->size);
19662306a36Sopenharmony_ci		}
19762306a36Sopenharmony_ci		break;
19862306a36Sopenharmony_ci	}
19962306a36Sopenharmony_ci	case BTF_KIND_FLOAT:
20062306a36Sopenharmony_ci		fprintf(out, " size=%u", t->size);
20162306a36Sopenharmony_ci		break;
20262306a36Sopenharmony_ci	case BTF_KIND_DECL_TAG:
20362306a36Sopenharmony_ci		fprintf(out, " type_id=%u component_idx=%d",
20462306a36Sopenharmony_ci			t->type, btf_decl_tag(t)->component_idx);
20562306a36Sopenharmony_ci		break;
20662306a36Sopenharmony_ci	default:
20762306a36Sopenharmony_ci		break;
20862306a36Sopenharmony_ci	}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	return 0;
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/* Print raw BTF type dump into a local buffer and return string pointer back.
21462306a36Sopenharmony_ci * Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls
21562306a36Sopenharmony_ci */
21662306a36Sopenharmony_ciconst char *btf_type_raw_dump(const struct btf *btf, int type_id)
21762306a36Sopenharmony_ci{
21862306a36Sopenharmony_ci	static char buf[16 * 1024];
21962306a36Sopenharmony_ci	FILE *buf_file;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
22262306a36Sopenharmony_ci	if (!buf_file) {
22362306a36Sopenharmony_ci		fprintf(stderr, "Failed to open memstream: %d\n", errno);
22462306a36Sopenharmony_ci		return NULL;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	fprintf_btf_type_raw(buf_file, btf, type_id);
22862306a36Sopenharmony_ci	fflush(buf_file);
22962306a36Sopenharmony_ci	fclose(buf_file);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	return buf;
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciint btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	int i;
23762306a36Sopenharmony_ci	bool ok = true;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	ASSERT_EQ(btf__type_cnt(btf) - 1, nr_types, "btf_nr_types");
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	for (i = 1; i <= nr_types; i++) {
24262306a36Sopenharmony_ci		if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))
24362306a36Sopenharmony_ci			ok = false;
24462306a36Sopenharmony_ci	}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	return ok;
24762306a36Sopenharmony_ci}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_cistatic void btf_dump_printf(void *ctx, const char *fmt, va_list args)
25062306a36Sopenharmony_ci{
25162306a36Sopenharmony_ci	vfprintf(ctx, fmt, args);
25262306a36Sopenharmony_ci}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci/* Print BTF-to-C dump into a local buffer and return string pointer back.
25562306a36Sopenharmony_ci * Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls
25662306a36Sopenharmony_ci */
25762306a36Sopenharmony_ciconst char *btf_type_c_dump(const struct btf *btf)
25862306a36Sopenharmony_ci{
25962306a36Sopenharmony_ci	static char buf[16 * 1024];
26062306a36Sopenharmony_ci	FILE *buf_file;
26162306a36Sopenharmony_ci	struct btf_dump *d = NULL;
26262306a36Sopenharmony_ci	int err, i;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
26562306a36Sopenharmony_ci	if (!buf_file) {
26662306a36Sopenharmony_ci		fprintf(stderr, "Failed to open memstream: %d\n", errno);
26762306a36Sopenharmony_ci		return NULL;
26862306a36Sopenharmony_ci	}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	d = btf_dump__new(btf, btf_dump_printf, buf_file, NULL);
27162306a36Sopenharmony_ci	if (libbpf_get_error(d)) {
27262306a36Sopenharmony_ci		fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
27362306a36Sopenharmony_ci		goto err_out;
27462306a36Sopenharmony_ci	}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	for (i = 1; i < btf__type_cnt(btf); i++) {
27762306a36Sopenharmony_ci		err = btf_dump__dump_type(d, i);
27862306a36Sopenharmony_ci		if (err) {
27962306a36Sopenharmony_ci			fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
28062306a36Sopenharmony_ci			goto err_out;
28162306a36Sopenharmony_ci		}
28262306a36Sopenharmony_ci	}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	btf_dump__free(d);
28562306a36Sopenharmony_ci	fflush(buf_file);
28662306a36Sopenharmony_ci	fclose(buf_file);
28762306a36Sopenharmony_ci	return buf;
28862306a36Sopenharmony_cierr_out:
28962306a36Sopenharmony_ci	btf_dump__free(d);
29062306a36Sopenharmony_ci	fclose(buf_file);
29162306a36Sopenharmony_ci	return NULL;
29262306a36Sopenharmony_ci}
293