Skip to content

Commit 351131b

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: add btf_dump API for BTF-to-C conversion
BTF contains enough type information to allow generating valid compilable C header w/ correct layout of structs/unions and all the typedef/enum definitions. This patch adds a new "object" - btf_dump to facilitate dumping BTF as valid C. btf_dump__dump_type() is the main API which takes care of dumping out (through user-provided printf-like callback function) C definitions for given type ID and it's required dependencies. This allows for not just dumping out entirety of BTF types, but also selective filtering based on user-provided criterias w/ minimal set of dependent types. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2fc3fc0 commit 351131b

File tree

4 files changed

+1359
-1
lines changed

4 files changed

+1359
-1
lines changed

tools/lib/bpf/Build

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o netlink.o bpf_prog_linfo.o libbpf_probes.o xsk.o hashmap.o
1+
libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \
2+
netlink.o bpf_prog_linfo.o libbpf_probes.o xsk.o hashmap.o \
3+
btf_dump.o

tools/lib/bpf/btf.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#ifndef __LIBBPF_BTF_H
55
#define __LIBBPF_BTF_H
66

7+
#include <stdarg.h>
78
#include <linux/types.h>
89

910
#ifdef __cplusplus
@@ -102,6 +103,22 @@ struct btf_dedup_opts {
102103
LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
103104
const struct btf_dedup_opts *opts);
104105

106+
struct btf_dump;
107+
108+
struct btf_dump_opts {
109+
void *ctx;
110+
};
111+
112+
typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
113+
114+
LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
115+
const struct btf_ext *btf_ext,
116+
const struct btf_dump_opts *opts,
117+
btf_dump_printf_fn_t printf_fn);
118+
LIBBPF_API void btf_dump__free(struct btf_dump *d);
119+
120+
LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
121+
105122
#ifdef __cplusplus
106123
} /* extern "C" */
107124
#endif

0 commit comments

Comments
 (0)