0001
0002
0003 #include <stdio.h>
0004 #include <errno.h>
0005 #include <bpf/btf.h>
0006 #include <bpf/libbpf.h>
0007 #include "test_progs.h"
0008
0009 static const char * const btf_kind_str_mapping[] = {
0010 [BTF_KIND_UNKN] = "UNKNOWN",
0011 [BTF_KIND_INT] = "INT",
0012 [BTF_KIND_PTR] = "PTR",
0013 [BTF_KIND_ARRAY] = "ARRAY",
0014 [BTF_KIND_STRUCT] = "STRUCT",
0015 [BTF_KIND_UNION] = "UNION",
0016 [BTF_KIND_ENUM] = "ENUM",
0017 [BTF_KIND_FWD] = "FWD",
0018 [BTF_KIND_TYPEDEF] = "TYPEDEF",
0019 [BTF_KIND_VOLATILE] = "VOLATILE",
0020 [BTF_KIND_CONST] = "CONST",
0021 [BTF_KIND_RESTRICT] = "RESTRICT",
0022 [BTF_KIND_FUNC] = "FUNC",
0023 [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",
0024 [BTF_KIND_VAR] = "VAR",
0025 [BTF_KIND_DATASEC] = "DATASEC",
0026 [BTF_KIND_FLOAT] = "FLOAT",
0027 [BTF_KIND_DECL_TAG] = "DECL_TAG",
0028 [BTF_KIND_TYPE_TAG] = "TYPE_TAG",
0029 [BTF_KIND_ENUM64] = "ENUM64",
0030 };
0031
0032 static const char *btf_kind_str(__u16 kind)
0033 {
0034 if (kind > BTF_KIND_ENUM64)
0035 return "UNKNOWN";
0036 return btf_kind_str_mapping[kind];
0037 }
0038
0039 static const char *btf_int_enc_str(__u8 encoding)
0040 {
0041 switch (encoding) {
0042 case 0:
0043 return "(none)";
0044 case BTF_INT_SIGNED:
0045 return "SIGNED";
0046 case BTF_INT_CHAR:
0047 return "CHAR";
0048 case BTF_INT_BOOL:
0049 return "BOOL";
0050 default:
0051 return "UNKN";
0052 }
0053 }
0054
0055 static const char *btf_var_linkage_str(__u32 linkage)
0056 {
0057 switch (linkage) {
0058 case BTF_VAR_STATIC:
0059 return "static";
0060 case BTF_VAR_GLOBAL_ALLOCATED:
0061 return "global-alloc";
0062 default:
0063 return "(unknown)";
0064 }
0065 }
0066
0067 static const char *btf_func_linkage_str(const struct btf_type *t)
0068 {
0069 switch (btf_vlen(t)) {
0070 case BTF_FUNC_STATIC:
0071 return "static";
0072 case BTF_FUNC_GLOBAL:
0073 return "global";
0074 case BTF_FUNC_EXTERN:
0075 return "extern";
0076 default:
0077 return "(unknown)";
0078 }
0079 }
0080
0081 static const char *btf_str(const struct btf *btf, __u32 off)
0082 {
0083 if (!off)
0084 return "(anon)";
0085 return btf__str_by_offset(btf, off) ?: "(invalid)";
0086 }
0087
0088 int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
0089 {
0090 const struct btf_type *t;
0091 int kind, i;
0092 __u32 vlen;
0093
0094 t = btf__type_by_id(btf, id);
0095 if (!t)
0096 return -EINVAL;
0097
0098 vlen = btf_vlen(t);
0099 kind = btf_kind(t);
0100
0101 fprintf(out, "[%u] %s '%s'", id, btf_kind_str(kind), btf_str(btf, t->name_off));
0102
0103 switch (kind) {
0104 case BTF_KIND_INT:
0105 fprintf(out, " size=%u bits_offset=%u nr_bits=%u encoding=%s",
0106 t->size, btf_int_offset(t), btf_int_bits(t),
0107 btf_int_enc_str(btf_int_encoding(t)));
0108 break;
0109 case BTF_KIND_PTR:
0110 case BTF_KIND_CONST:
0111 case BTF_KIND_VOLATILE:
0112 case BTF_KIND_RESTRICT:
0113 case BTF_KIND_TYPEDEF:
0114 case BTF_KIND_TYPE_TAG:
0115 fprintf(out, " type_id=%u", t->type);
0116 break;
0117 case BTF_KIND_ARRAY: {
0118 const struct btf_array *arr = btf_array(t);
0119
0120 fprintf(out, " type_id=%u index_type_id=%u nr_elems=%u",
0121 arr->type, arr->index_type, arr->nelems);
0122 break;
0123 }
0124 case BTF_KIND_STRUCT:
0125 case BTF_KIND_UNION: {
0126 const struct btf_member *m = btf_members(t);
0127
0128 fprintf(out, " size=%u vlen=%u", t->size, vlen);
0129 for (i = 0; i < vlen; i++, m++) {
0130 __u32 bit_off, bit_sz;
0131
0132 bit_off = btf_member_bit_offset(t, i);
0133 bit_sz = btf_member_bitfield_size(t, i);
0134 fprintf(out, "\n\t'%s' type_id=%u bits_offset=%u",
0135 btf_str(btf, m->name_off), m->type, bit_off);
0136 if (bit_sz)
0137 fprintf(out, " bitfield_size=%u", bit_sz);
0138 }
0139 break;
0140 }
0141 case BTF_KIND_ENUM: {
0142 const struct btf_enum *v = btf_enum(t);
0143 const char *fmt_str;
0144
0145 fmt_str = btf_kflag(t) ? "\n\t'%s' val=%d" : "\n\t'%s' val=%u";
0146 fprintf(out, " encoding=%s size=%u vlen=%u",
0147 btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);
0148 for (i = 0; i < vlen; i++, v++) {
0149 fprintf(out, fmt_str,
0150 btf_str(btf, v->name_off), v->val);
0151 }
0152 break;
0153 }
0154 case BTF_KIND_ENUM64: {
0155 const struct btf_enum64 *v = btf_enum64(t);
0156 const char *fmt_str;
0157
0158 fmt_str = btf_kflag(t) ? "\n\t'%s' val=%lld" : "\n\t'%s' val=%llu";
0159
0160 fprintf(out, " encoding=%s size=%u vlen=%u",
0161 btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);
0162 for (i = 0; i < vlen; i++, v++) {
0163 fprintf(out, fmt_str,
0164 btf_str(btf, v->name_off),
0165 ((__u64)v->val_hi32 << 32) | v->val_lo32);
0166 }
0167 break;
0168 }
0169 case BTF_KIND_FWD:
0170 fprintf(out, " fwd_kind=%s", btf_kflag(t) ? "union" : "struct");
0171 break;
0172 case BTF_KIND_FUNC:
0173 fprintf(out, " type_id=%u linkage=%s", t->type, btf_func_linkage_str(t));
0174 break;
0175 case BTF_KIND_FUNC_PROTO: {
0176 const struct btf_param *p = btf_params(t);
0177
0178 fprintf(out, " ret_type_id=%u vlen=%u", t->type, vlen);
0179 for (i = 0; i < vlen; i++, p++) {
0180 fprintf(out, "\n\t'%s' type_id=%u",
0181 btf_str(btf, p->name_off), p->type);
0182 }
0183 break;
0184 }
0185 case BTF_KIND_VAR:
0186 fprintf(out, " type_id=%u, linkage=%s",
0187 t->type, btf_var_linkage_str(btf_var(t)->linkage));
0188 break;
0189 case BTF_KIND_DATASEC: {
0190 const struct btf_var_secinfo *v = btf_var_secinfos(t);
0191
0192 fprintf(out, " size=%u vlen=%u", t->size, vlen);
0193 for (i = 0; i < vlen; i++, v++) {
0194 fprintf(out, "\n\ttype_id=%u offset=%u size=%u",
0195 v->type, v->offset, v->size);
0196 }
0197 break;
0198 }
0199 case BTF_KIND_FLOAT:
0200 fprintf(out, " size=%u", t->size);
0201 break;
0202 case BTF_KIND_DECL_TAG:
0203 fprintf(out, " type_id=%u component_idx=%d",
0204 t->type, btf_decl_tag(t)->component_idx);
0205 break;
0206 default:
0207 break;
0208 }
0209
0210 return 0;
0211 }
0212
0213
0214
0215
0216 const char *btf_type_raw_dump(const struct btf *btf, int type_id)
0217 {
0218 static char buf[16 * 1024];
0219 FILE *buf_file;
0220
0221 buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
0222 if (!buf_file) {
0223 fprintf(stderr, "Failed to open memstream: %d\n", errno);
0224 return NULL;
0225 }
0226
0227 fprintf_btf_type_raw(buf_file, btf, type_id);
0228 fflush(buf_file);
0229 fclose(buf_file);
0230
0231 return buf;
0232 }
0233
0234 int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])
0235 {
0236 int i;
0237 bool ok = true;
0238
0239 ASSERT_EQ(btf__type_cnt(btf) - 1, nr_types, "btf_nr_types");
0240
0241 for (i = 1; i <= nr_types; i++) {
0242 if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))
0243 ok = false;
0244 }
0245
0246 return ok;
0247 }
0248
0249 static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
0250 {
0251 vfprintf(ctx, fmt, args);
0252 }
0253
0254
0255
0256
0257 const char *btf_type_c_dump(const struct btf *btf)
0258 {
0259 static char buf[16 * 1024];
0260 FILE *buf_file;
0261 struct btf_dump *d = NULL;
0262 int err, i;
0263
0264 buf_file = fmemopen(buf, sizeof(buf) - 1, "w");
0265 if (!buf_file) {
0266 fprintf(stderr, "Failed to open memstream: %d\n", errno);
0267 return NULL;
0268 }
0269
0270 d = btf_dump__new(btf, btf_dump_printf, buf_file, NULL);
0271 if (libbpf_get_error(d)) {
0272 fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
0273 goto err_out;
0274 }
0275
0276 for (i = 1; i < btf__type_cnt(btf); i++) {
0277 err = btf_dump__dump_type(d, i);
0278 if (err) {
0279 fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
0280 goto err_out;
0281 }
0282 }
0283
0284 btf_dump__free(d);
0285 fflush(buf_file);
0286 fclose(buf_file);
0287 return buf;
0288 err_out:
0289 btf_dump__free(d);
0290 fclose(buf_file);
0291 return NULL;
0292 }