Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Copyright (C) 2019 Facebook */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <bpf/btf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/btf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "json_writer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static const char * const btf_kind_str[NR_BTF_KINDS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	[BTF_KIND_UNKN]		= "UNKNOWN",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	[BTF_KIND_INT]		= "INT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	[BTF_KIND_PTR]		= "PTR",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	[BTF_KIND_ARRAY]	= "ARRAY",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	[BTF_KIND_STRUCT]	= "STRUCT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	[BTF_KIND_UNION]	= "UNION",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	[BTF_KIND_ENUM]		= "ENUM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	[BTF_KIND_FWD]		= "FWD",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	[BTF_KIND_TYPEDEF]	= "TYPEDEF",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	[BTF_KIND_VOLATILE]	= "VOLATILE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	[BTF_KIND_CONST]	= "CONST",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	[BTF_KIND_RESTRICT]	= "RESTRICT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	[BTF_KIND_FUNC]		= "FUNC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	[BTF_KIND_VAR]		= "VAR",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	[BTF_KIND_DATASEC]	= "DATASEC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct btf_attach_table {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	DECLARE_HASHTABLE(table, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct btf_attach_point {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	__u32 obj_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	__u32 btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct hlist_node hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static const char *btf_int_enc_str(__u8 encoding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	switch (encoding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return "(none)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	case BTF_INT_SIGNED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return "SIGNED";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	case BTF_INT_CHAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return "CHAR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	case BTF_INT_BOOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return "BOOL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return "UNKN";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static const char *btf_var_linkage_str(__u32 linkage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	switch (linkage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	case BTF_VAR_STATIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return "static";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	case BTF_VAR_GLOBAL_ALLOCATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return "global-alloc";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return "(unknown)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static const char *btf_func_linkage_str(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	switch (btf_vlen(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	case BTF_FUNC_STATIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return "static";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	case BTF_FUNC_GLOBAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return "global";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	case BTF_FUNC_EXTERN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return "extern";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return "(unknown)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static const char *btf_str(const struct btf *btf, __u32 off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return "(anon)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return btf__name_by_offset(btf, off) ? : "(invalid)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int dump_btf_type(const struct btf *btf, __u32 id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			 const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	json_writer_t *w = json_wtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	int kind, safe_kind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	kind = BTF_INFO_KIND(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	safe_kind = kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		jsonw_start_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		jsonw_uint_field(w, "id", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		jsonw_string_field(w, "kind", btf_kind_str[safe_kind]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		jsonw_string_field(w, "name", btf_str(btf, t->name_off));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		printf("[%u] %s '%s'", id, btf_kind_str[safe_kind],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		       btf_str(btf, t->name_off));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	switch (BTF_INFO_KIND(t->info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	case BTF_KIND_INT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		__u32 v = *(__u32 *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		const char *enc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		enc = btf_int_enc_str(BTF_INT_ENCODING(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			jsonw_uint_field(w, "size", t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			jsonw_string_field(w, "encoding", enc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			       t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			       enc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	case BTF_KIND_PTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	case BTF_KIND_CONST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	case BTF_KIND_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	case BTF_KIND_RESTRICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	case BTF_KIND_TYPEDEF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			jsonw_uint_field(w, "type_id", t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			printf(" type_id=%u", t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	case BTF_KIND_ARRAY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		const struct btf_array *arr = (const void *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			jsonw_uint_field(w, "type_id", arr->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			jsonw_uint_field(w, "index_type_id", arr->index_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			jsonw_uint_field(w, "nr_elems", arr->nelems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			printf(" type_id=%u index_type_id=%u nr_elems=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			       arr->type, arr->index_type, arr->nelems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	case BTF_KIND_STRUCT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	case BTF_KIND_UNION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		const struct btf_member *m = (const void *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		__u16 vlen = BTF_INFO_VLEN(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			jsonw_uint_field(w, "size", t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			jsonw_uint_field(w, "vlen", vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			jsonw_name(w, "members");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			jsonw_start_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			printf(" size=%u vlen=%u", t->size, vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		for (i = 0; i < vlen; i++, m++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			const char *name = btf_str(btf, m->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			__u32 bit_off, bit_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			if (BTF_INFO_KFLAG(t->info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				bit_off = BTF_MEMBER_BIT_OFFSET(m->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				bit_off = m->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				bit_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				jsonw_start_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 				jsonw_string_field(w, "name", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				jsonw_uint_field(w, "type_id", m->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				jsonw_uint_field(w, "bits_offset", bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				if (bit_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 					jsonw_uint_field(w, "bitfield_size",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 							 bit_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				jsonw_end_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				printf("\n\t'%s' type_id=%u bits_offset=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				       name, m->type, bit_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				if (bit_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 					printf(" bitfield_size=%u", bit_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			jsonw_end_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	case BTF_KIND_ENUM: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		const struct btf_enum *v = (const void *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		__u16 vlen = BTF_INFO_VLEN(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			jsonw_uint_field(w, "size", t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			jsonw_uint_field(w, "vlen", vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			jsonw_name(w, "values");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			jsonw_start_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			printf(" size=%u vlen=%u", t->size, vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		for (i = 0; i < vlen; i++, v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			const char *name = btf_str(btf, v->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				jsonw_start_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				jsonw_string_field(w, "name", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				jsonw_uint_field(w, "val", v->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				jsonw_end_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				printf("\n\t'%s' val=%u", name, v->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			jsonw_end_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	case BTF_KIND_FWD: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 							       : "struct";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			jsonw_string_field(w, "fwd_kind", fwd_kind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			printf(" fwd_kind=%s", fwd_kind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	case BTF_KIND_FUNC: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		const char *linkage = btf_func_linkage_str(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			jsonw_uint_field(w, "type_id", t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			jsonw_string_field(w, "linkage", linkage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			printf(" type_id=%u linkage=%s", t->type, linkage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	case BTF_KIND_FUNC_PROTO: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		const struct btf_param *p = (const void *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		__u16 vlen = BTF_INFO_VLEN(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			jsonw_uint_field(w, "ret_type_id", t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			jsonw_uint_field(w, "vlen", vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			jsonw_name(w, "params");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			jsonw_start_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			printf(" ret_type_id=%u vlen=%u", t->type, vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		for (i = 0; i < vlen; i++, p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			const char *name = btf_str(btf, p->name_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 				jsonw_start_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				jsonw_string_field(w, "name", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				jsonw_uint_field(w, "type_id", p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				jsonw_end_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				printf("\n\t'%s' type_id=%u", name, p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			jsonw_end_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	case BTF_KIND_VAR: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		const struct btf_var *v = (const void *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		const char *linkage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		linkage = btf_var_linkage_str(v->linkage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			jsonw_uint_field(w, "type_id", t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			jsonw_string_field(w, "linkage", linkage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			printf(" type_id=%u, linkage=%s", t->type, linkage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	case BTF_KIND_DATASEC: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		const struct btf_var_secinfo *v = (const void *)(t+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		__u16 vlen = BTF_INFO_VLEN(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			jsonw_uint_field(w, "size", t->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			jsonw_uint_field(w, "vlen", vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			jsonw_name(w, "vars");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			jsonw_start_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			printf(" size=%u vlen=%u", t->size, vlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		for (i = 0; i < vlen; i++, v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				jsonw_start_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				jsonw_uint_field(w, "type_id", v->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 				jsonw_uint_field(w, "offset", v->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				jsonw_uint_field(w, "size", v->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				jsonw_end_object(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				printf("\n\ttype_id=%u offset=%u size=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				       v->type, v->offset, v->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			jsonw_end_array(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		jsonw_end_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static int dump_btf_raw(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			__u32 *root_type_ids, int root_type_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	const struct btf_type *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		jsonw_start_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		jsonw_name(json_wtr, "types");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		jsonw_start_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (root_type_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		for (i = 0; i < root_type_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			t = btf__type_by_id(btf, root_type_ids[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			dump_btf_type(btf, root_type_ids[i], t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		int cnt = btf__get_nr_types(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		for (i = 1; i <= cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			t = btf__type_by_id(btf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			dump_btf_type(btf, i, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		jsonw_end_array(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		jsonw_end_object(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static void __printf(2, 0) btf_dump_printf(void *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 					   const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	vfprintf(stdout, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int dump_btf_c(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		      __u32 *root_type_ids, int root_type_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct btf_dump *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	int err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	d = btf_dump__new(btf, NULL, NULL, btf_dump_printf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (IS_ERR(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return PTR_ERR(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	printf("#ifndef __VMLINUX_H__\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	printf("#define __VMLINUX_H__\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	printf("#endif\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (root_type_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		for (i = 0; i < root_type_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			err = btf_dump__dump_type(d, root_type_ids[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		int cnt = btf__get_nr_types(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		for (i = 1; i <= cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			err = btf_dump__dump_type(d, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	printf("#pragma clang attribute pop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	printf("#endif\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	printf("#endif /* __VMLINUX_H__ */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	btf_dump__free(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static int do_dump(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct btf *btf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	__u32 root_type_ids[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	int root_type_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	bool dump_c = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	__u32 btf_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	const char *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	int fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (!REQ_ARGS(2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	src = GET_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (is_prefix(src, "map")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		struct bpf_map_info info = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		__u32 len = sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		if (!REQ_ARGS(2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		btf_id = info.btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		if (argc && is_prefix(*argv, "key")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			root_type_ids[root_type_cnt++] = info.btf_key_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		} else if (argc && is_prefix(*argv, "value")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			root_type_ids[root_type_cnt++] = info.btf_value_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		} else if (argc && is_prefix(*argv, "all")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		} else if (argc && is_prefix(*argv, "kv")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			root_type_ids[root_type_cnt++] = info.btf_key_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			root_type_ids[root_type_cnt++] = info.btf_value_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			root_type_ids[root_type_cnt++] = info.btf_key_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			root_type_ids[root_type_cnt++] = info.btf_value_type_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	} else if (is_prefix(src, "prog")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		struct bpf_prog_info info = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		__u32 len = sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (!REQ_ARGS(2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		fd = prog_parse_fd(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		err = bpf_obj_get_info_by_fd(fd, &info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			p_err("can't get prog info: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		btf_id = info.btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	} else if (is_prefix(src, "id")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		char *endptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		btf_id = strtoul(*argv, &endptr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		if (*endptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			p_err("can't parse %s as ID", *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	} else if (is_prefix(src, "file")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		btf = btf__parse(*argv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		if (IS_ERR(btf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			err = -PTR_ERR(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			btf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			p_err("failed to load BTF from %s: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			      *argv, strerror(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		p_err("unrecognized BTF source specifier: '%s'", src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	while (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		if (is_prefix(*argv, "format")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			if (argc < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 				p_err("expecting value for 'format' option\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 				err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			if (strcmp(*argv, "c") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 				dump_c = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			} else if (strcmp(*argv, "raw") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 				dump_c = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 				p_err("unrecognized format specifier: '%s', possible values: raw, c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 				      *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 				err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			NEXT_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			p_err("unrecognized option: '%s'", *argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	if (!btf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		err = btf__get_from_id(btf_id, &btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 			p_err("get btf by id (%u): %s", btf_id, strerror(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		if (!btf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			p_err("can't find btf with ID (%u)", btf_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (dump_c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			p_err("JSON output for C-syntax dump is not supported");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			err = -ENOTSUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		err = dump_btf_c(btf, root_type_ids, root_type_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		err = dump_btf_raw(btf, root_type_ids, root_type_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	btf__free(btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) static int btf_parse_fd(int *argc, char ***argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	char *endptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (!is_prefix(*argv[0], "id")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		p_err("expected 'id', got: '%s'?", **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	id = strtoul(**argv, &endptr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	if (*endptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		p_err("can't parse %s as ID", **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	NEXT_ARGP();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	fd = bpf_btf_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		p_err("can't get BTF object by id (%u): %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		      id, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static void delete_btf_table(struct btf_attach_table *tab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct btf_attach_point *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	struct hlist_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	unsigned int bkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		hash_del(&obj->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		free(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) build_btf_type_table(struct btf_attach_table *tab, enum bpf_obj_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		     void *info, __u32 *len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	static const char * const names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		[BPF_OBJ_UNKNOWN]	= "unknown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		[BPF_OBJ_PROG]		= "prog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		[BPF_OBJ_MAP]		= "map",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	struct btf_attach_point *obj_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	__u32 btf_id, id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		case BPF_OBJ_PROG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 			err = bpf_prog_get_next_id(id, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		case BPF_OBJ_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 			err = bpf_map_get_next_id(id, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			p_err("unexpected object type: %d", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			if (errno == ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			p_err("can't get next %s: %s%s", names[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 			      strerror(errno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			      errno == EINVAL ? " -- kernel too old?" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		case BPF_OBJ_PROG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			fd = bpf_prog_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		case BPF_OBJ_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			fd = bpf_map_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			p_err("unexpected object type: %d", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 			if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			p_err("can't get %s by id (%u): %s", names[type], id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 			      strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		memset(info, 0, *len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		err = bpf_obj_get_info_by_fd(fd, info, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 			p_err("can't get %s info: %s", names[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			      strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		case BPF_OBJ_PROG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 			btf_id = ((struct bpf_prog_info *)info)->btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		case BPF_OBJ_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 			btf_id = ((struct bpf_map_info *)info)->btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 			p_err("unexpected object type: %d", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		if (!btf_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		obj_node = calloc(1, sizeof(*obj_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		if (!obj_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 			p_err("failed to allocate memory: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		obj_node->obj_id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		obj_node->btf_id = btf_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		hash_add(tab->table, &obj_node->hash, obj_node->btf_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	delete_btf_table(tab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) build_btf_tables(struct btf_attach_table *btf_prog_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		 struct btf_attach_table *btf_map_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	struct bpf_prog_info prog_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	__u32 prog_len = sizeof(prog_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	struct bpf_map_info map_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	__u32 map_len = sizeof(map_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	err = build_btf_type_table(btf_prog_table, BPF_OBJ_PROG, &prog_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 				   &prog_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	err = build_btf_type_table(btf_map_table, BPF_OBJ_MAP, &map_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 				   &map_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		delete_btf_table(btf_prog_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) show_btf_plain(struct bpf_btf_info *info, int fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	       struct btf_attach_table *btf_prog_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	       struct btf_attach_table *btf_map_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	struct btf_attach_point *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	printf("%u: ", info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	printf("size %uB", info->btf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	hash_for_each_possible(btf_prog_table->table, obj, hash, info->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		if (obj->btf_id == info->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 			printf("%s%u", n++ == 0 ? "  prog_ids " : ",",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 			       obj->obj_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	hash_for_each_possible(btf_map_table->table, obj, hash, info->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		if (obj->btf_id == info->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 			printf("%s%u", n++ == 0 ? "  map_ids " : ",",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 			       obj->obj_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) show_btf_json(struct bpf_btf_info *info, int fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	      struct btf_attach_table *btf_prog_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	      struct btf_attach_table *btf_map_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	struct btf_attach_point *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	jsonw_start_object(json_wtr);	/* btf object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	jsonw_uint_field(json_wtr, "id", info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	jsonw_uint_field(json_wtr, "size", info->btf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	jsonw_name(json_wtr, "prog_ids");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	jsonw_start_array(json_wtr);	/* prog_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	hash_for_each_possible(btf_prog_table->table, obj, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 			       info->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		if (obj->btf_id == info->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 			jsonw_uint(json_wtr, obj->obj_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	jsonw_end_array(json_wtr);	/* prog_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	jsonw_name(json_wtr, "map_ids");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	jsonw_start_array(json_wtr);	/* map_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	hash_for_each_possible(btf_map_table->table, obj, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 			       info->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 		if (obj->btf_id == info->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 			jsonw_uint(json_wtr, obj->obj_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	jsonw_end_array(json_wtr);	/* map_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	emit_obj_refs_json(&refs_table, info->id, json_wtr); /* pids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	jsonw_end_object(json_wtr);	/* btf object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) show_btf(int fd, struct btf_attach_table *btf_prog_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	 struct btf_attach_table *btf_map_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	struct bpf_btf_info info = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	__u32 len = sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		p_err("can't get BTF object info: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 		show_btf_json(&info, fd, btf_prog_table, btf_map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 		show_btf_plain(&info, fd, btf_prog_table, btf_map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) static int do_show(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	struct btf_attach_table btf_prog_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	struct btf_attach_table btf_map_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	int err, fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	__u32 id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	if (argc == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 		fd = btf_parse_fd(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		if (fd >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 			close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		return BAD_ARG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	hash_init(btf_prog_table.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	hash_init(btf_map_table.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	err = build_btf_tables(&btf_prog_table, &btf_map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		if (fd >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 			close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	build_obj_refs_table(&refs_table, BPF_OBJ_BTF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 		err = show_btf(fd, &btf_prog_table, &btf_map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 		goto exit_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 		jsonw_start_array(json_wtr);	/* root array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 		err = bpf_btf_get_next_id(id, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 			if (errno == ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 			p_err("can't get next BTF object: %s%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 			      strerror(errno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 			      errno == EINVAL ? " -- kernel too old?" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		fd = bpf_btf_get_fd_by_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 			if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 			p_err("can't get BTF object by id (%u): %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 			      id, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 		err = show_btf(fd, &btf_prog_table, &btf_map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	if (json_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 		jsonw_end_array(json_wtr);	/* root array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) exit_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	delete_btf_table(&btf_prog_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	delete_btf_table(&btf_map_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	delete_obj_refs_table(&refs_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) static int do_help(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	if (json_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 		jsonw_null(json_wtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		"Usage: %1$s %2$s { show | list } [id BTF_ID]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 		"       %1$s %2$s dump BTF_SRC [format FORMAT]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 		"       %1$s %2$s help\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 		"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 		"       BTF_SRC := { id BTF_ID | prog PROG | map MAP [{key | value | kv | all}] | file FILE }\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 		"       FORMAT  := { raw | c }\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 		"       " HELP_SPEC_MAP "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 		"       " HELP_SPEC_PROGRAM "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 		"       " HELP_SPEC_OPTIONS "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 		"",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 		bin_name, "btf");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) static const struct cmd cmds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	{ "show",	do_show },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	{ "list",	do_show },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	{ "help",	do_help },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	{ "dump",	do_dump },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	{ 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) int do_btf(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	return cmd_select(cmds, argc, argv, do_help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }