On Tue, Nov 16, 2021 at 9:25 PM Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: > > On Tue, Nov 16, 2021 at 8:42 AM Mauricio Vásquez <mauricio@xxxxxxxxxx> wrote: > > > > Implement helper function to save the contents of a BTF object to a > > file. > > > > Signed-off-by: Mauricio Vásquez <mauricio@xxxxxxxxxx> > > Signed-off-by: Rafael David Tinoco <rafael.tinoco@xxxxxxxxxxx> > > Signed-off-by: Lorenzo Fontana <lorenzo.fontana@xxxxxxxxxx> > > Signed-off-by: Leonardo Di Donato <leonardo.didonato@xxxxxxxxxx> > > --- > > tools/lib/bpf/btf.c | 30 ++++++++++++++++++++++++++++++ > > tools/lib/bpf/btf.h | 2 ++ > > tools/lib/bpf/libbpf.map | 1 + > > 3 files changed, 33 insertions(+) > > > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > > index fadf089ae8fe..96a242f91832 100644 > > --- a/tools/lib/bpf/btf.c > > +++ b/tools/lib/bpf/btf.c > > @@ -1121,6 +1121,36 @@ struct btf *btf__parse_split(const char *path, struct btf *base_btf) > > return libbpf_ptr(btf_parse(path, base_btf, NULL)); > > } > > > > +int btf__save_raw(const struct btf *btf, const char *path) > > +{ > > + const void *data; > > + FILE *f = NULL; > > + __u32 data_sz; > > + int err = 0; > > + > > + data = btf__raw_data(btf, &data_sz); > > + if (!data) { > > + err = -ENOMEM; > > + goto out; > > + } > > + > > + f = fopen(path, "wb"); > > + if (!f) { > > + err = -errno; > > + goto out; > > + } > > + > > + if (fwrite(data, 1, data_sz, f) != data_sz) { > > + err = -errno; > > + goto out; > > + } > > + > > +out: > > + if (f) > > + fclose(f); > > + return libbpf_err(err); > > +} > > + > > static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian); > > > > int btf__load_into_kernel(struct btf *btf) > > diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h > > index 5c73a5b0a044..4f8d3f303aa6 100644 > > --- a/tools/lib/bpf/btf.h > > +++ b/tools/lib/bpf/btf.h > > @@ -114,6 +114,8 @@ LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_b > > LIBBPF_API struct btf *btf__parse_raw(const char *path); > > LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf); > > > > +LIBBPF_API int btf__save_raw(const struct btf *btf, const char *path); Thinking about this some more, I don't feel it's necessary to have this kind of API in libbpf. Libbpf provides raw bytes and it's not hard to write dumping byte array to file. We don't have btf__save_elf() and we probably shouldn't, so I don't see the need for btf__save_raw() either. It's neither complicated nor frequently used code to write. > > + > > LIBBPF_API struct btf *btf__load_vmlinux_btf(void); > > LIBBPF_API struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf); > > LIBBPF_API struct btf *libbpf_find_kernel_btf(void); > > diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map > > index 6a59514a48cf..c9555f8655af 100644 > > --- a/tools/lib/bpf/libbpf.map > > +++ b/tools/lib/bpf/libbpf.map > > @@ -414,4 +414,5 @@ LIBBPF_0.6.0 { > > perf_buffer__new_deprecated; > > perf_buffer__new_raw; > > perf_buffer__new_raw_deprecated; > > + btf__save_raw; > > this is a sorted list, please keep it so > > > } LIBBPF_0.5.0; > > -- > > 2.25.1 > >