Path: blob/master/tools/testing/selftests/bpf/btf_helpers.c
26285 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (c) 2020 Facebook */2#include <stdio.h>3#include <errno.h>4#include <bpf/btf.h>5#include <bpf/libbpf.h>6#include "test_progs.h"78static const char * const btf_kind_str_mapping[] = {9[BTF_KIND_UNKN] = "UNKNOWN",10[BTF_KIND_INT] = "INT",11[BTF_KIND_PTR] = "PTR",12[BTF_KIND_ARRAY] = "ARRAY",13[BTF_KIND_STRUCT] = "STRUCT",14[BTF_KIND_UNION] = "UNION",15[BTF_KIND_ENUM] = "ENUM",16[BTF_KIND_FWD] = "FWD",17[BTF_KIND_TYPEDEF] = "TYPEDEF",18[BTF_KIND_VOLATILE] = "VOLATILE",19[BTF_KIND_CONST] = "CONST",20[BTF_KIND_RESTRICT] = "RESTRICT",21[BTF_KIND_FUNC] = "FUNC",22[BTF_KIND_FUNC_PROTO] = "FUNC_PROTO",23[BTF_KIND_VAR] = "VAR",24[BTF_KIND_DATASEC] = "DATASEC",25[BTF_KIND_FLOAT] = "FLOAT",26[BTF_KIND_DECL_TAG] = "DECL_TAG",27[BTF_KIND_TYPE_TAG] = "TYPE_TAG",28[BTF_KIND_ENUM64] = "ENUM64",29};3031static const char *btf_kind_str(__u16 kind)32{33if (kind > BTF_KIND_ENUM64)34return "UNKNOWN";35return btf_kind_str_mapping[kind];36}3738static const char *btf_int_enc_str(__u8 encoding)39{40switch (encoding) {41case 0:42return "(none)";43case BTF_INT_SIGNED:44return "SIGNED";45case BTF_INT_CHAR:46return "CHAR";47case BTF_INT_BOOL:48return "BOOL";49default:50return "UNKN";51}52}5354static const char *btf_var_linkage_str(__u32 linkage)55{56switch (linkage) {57case BTF_VAR_STATIC:58return "static";59case BTF_VAR_GLOBAL_ALLOCATED:60return "global-alloc";61default:62return "(unknown)";63}64}6566static const char *btf_func_linkage_str(const struct btf_type *t)67{68switch (btf_vlen(t)) {69case BTF_FUNC_STATIC:70return "static";71case BTF_FUNC_GLOBAL:72return "global";73case BTF_FUNC_EXTERN:74return "extern";75default:76return "(unknown)";77}78}7980static const char *btf_str(const struct btf *btf, __u32 off)81{82if (!off)83return "(anon)";84return btf__str_by_offset(btf, off) ?: "(invalid)";85}8687int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)88{89const struct btf_type *t;90int kind, i;91__u32 vlen;9293t = btf__type_by_id(btf, id);94if (!t)95return -EINVAL;9697vlen = btf_vlen(t);98kind = btf_kind(t);99100fprintf(out, "[%u] %s '%s'", id, btf_kind_str(kind), btf_str(btf, t->name_off));101102switch (kind) {103case BTF_KIND_INT:104fprintf(out, " size=%u bits_offset=%u nr_bits=%u encoding=%s",105t->size, btf_int_offset(t), btf_int_bits(t),106btf_int_enc_str(btf_int_encoding(t)));107break;108case BTF_KIND_PTR:109case BTF_KIND_CONST:110case BTF_KIND_VOLATILE:111case BTF_KIND_RESTRICT:112case BTF_KIND_TYPEDEF:113case BTF_KIND_TYPE_TAG:114fprintf(out, " type_id=%u", t->type);115break;116case BTF_KIND_ARRAY: {117const struct btf_array *arr = btf_array(t);118119fprintf(out, " type_id=%u index_type_id=%u nr_elems=%u",120arr->type, arr->index_type, arr->nelems);121break;122}123case BTF_KIND_STRUCT:124case BTF_KIND_UNION: {125const struct btf_member *m = btf_members(t);126127fprintf(out, " size=%u vlen=%u", t->size, vlen);128for (i = 0; i < vlen; i++, m++) {129__u32 bit_off, bit_sz;130131bit_off = btf_member_bit_offset(t, i);132bit_sz = btf_member_bitfield_size(t, i);133fprintf(out, "\n\t'%s' type_id=%u bits_offset=%u",134btf_str(btf, m->name_off), m->type, bit_off);135if (bit_sz)136fprintf(out, " bitfield_size=%u", bit_sz);137}138break;139}140case BTF_KIND_ENUM: {141const struct btf_enum *v = btf_enum(t);142const char *fmt_str;143144fmt_str = btf_kflag(t) ? "\n\t'%s' val=%d" : "\n\t'%s' val=%u";145fprintf(out, " encoding=%s size=%u vlen=%u",146btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);147for (i = 0; i < vlen; i++, v++) {148fprintf(out, fmt_str,149btf_str(btf, v->name_off), v->val);150}151break;152}153case BTF_KIND_ENUM64: {154const struct btf_enum64 *v = btf_enum64(t);155const char *fmt_str;156157fmt_str = btf_kflag(t) ? "\n\t'%s' val=%lld" : "\n\t'%s' val=%llu";158159fprintf(out, " encoding=%s size=%u vlen=%u",160btf_kflag(t) ? "SIGNED" : "UNSIGNED", t->size, vlen);161for (i = 0; i < vlen; i++, v++) {162fprintf(out, fmt_str,163btf_str(btf, v->name_off),164((__u64)v->val_hi32 << 32) | v->val_lo32);165}166break;167}168case BTF_KIND_FWD:169fprintf(out, " fwd_kind=%s", btf_kflag(t) ? "union" : "struct");170break;171case BTF_KIND_FUNC:172fprintf(out, " type_id=%u linkage=%s", t->type, btf_func_linkage_str(t));173break;174case BTF_KIND_FUNC_PROTO: {175const struct btf_param *p = btf_params(t);176177fprintf(out, " ret_type_id=%u vlen=%u", t->type, vlen);178for (i = 0; i < vlen; i++, p++) {179fprintf(out, "\n\t'%s' type_id=%u",180btf_str(btf, p->name_off), p->type);181}182break;183}184case BTF_KIND_VAR:185fprintf(out, " type_id=%u, linkage=%s",186t->type, btf_var_linkage_str(btf_var(t)->linkage));187break;188case BTF_KIND_DATASEC: {189const struct btf_var_secinfo *v = btf_var_secinfos(t);190191fprintf(out, " size=%u vlen=%u", t->size, vlen);192for (i = 0; i < vlen; i++, v++) {193fprintf(out, "\n\ttype_id=%u offset=%u size=%u",194v->type, v->offset, v->size);195}196break;197}198case BTF_KIND_FLOAT:199fprintf(out, " size=%u", t->size);200break;201case BTF_KIND_DECL_TAG:202fprintf(out, " type_id=%u component_idx=%d",203t->type, btf_decl_tag(t)->component_idx);204break;205default:206break;207}208209return 0;210}211212/* Print raw BTF type dump into a local buffer and return string pointer back.213* Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls214*/215const char *btf_type_raw_dump(const struct btf *btf, int type_id)216{217static char buf[16 * 1024];218FILE *buf_file;219220buf_file = fmemopen(buf, sizeof(buf) - 1, "w");221if (!buf_file) {222fprintf(stderr, "Failed to open memstream: %d\n", errno);223return NULL;224}225226fprintf_btf_type_raw(buf_file, btf, type_id);227fflush(buf_file);228fclose(buf_file);229230return buf;231}232233int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])234{235int i;236bool ok = true;237238ASSERT_EQ(btf__type_cnt(btf) - 1, nr_types, "btf_nr_types");239240for (i = 1; i <= nr_types; i++) {241if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))242ok = false;243}244245return ok;246}247248static void btf_dump_printf(void *ctx, const char *fmt, va_list args)249{250vfprintf(ctx, fmt, args);251}252253/* Print BTF-to-C dump into a local buffer and return string pointer back.254* Buffer *will* be overwritten by subsequent btf_type_raw_dump() calls255*/256const char *btf_type_c_dump(const struct btf *btf)257{258static char buf[16 * 1024];259FILE *buf_file;260struct btf_dump *d = NULL;261int err, i;262263buf_file = fmemopen(buf, sizeof(buf) - 1, "w");264if (!buf_file) {265fprintf(stderr, "Failed to open memstream: %d\n", errno);266return NULL;267}268269d = btf_dump__new(btf, btf_dump_printf, buf_file, NULL);270if (libbpf_get_error(d)) {271fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));272goto err_out;273}274275for (i = 1; i < btf__type_cnt(btf); i++) {276err = btf_dump__dump_type(d, i);277if (err) {278fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);279goto err_out;280}281}282283btf_dump__free(d);284fflush(buf_file);285fclose(buf_file);286return buf;287err_out:288btf_dump__free(d);289fclose(buf_file);290return NULL;291}292293294