Thread (15 messages) flat view 15 messages, 3 authors, 2021-12-07

Re: [PATCH bpf-next v2 1/4] libbpf: Implement btf__save_raw()

From: Andrii Nakryiko <hidden>
Date: 2021-11-19 17:25:24
Also in: bpf

On Tue, Nov 16, 2021 at 9:25 PM Andrii Nakryiko
[off-list ref] wrote:
On Tue, Nov 16, 2021 at 8:42 AM Mauricio Vásquez [off-list ref] wrote:
quoted
Implement helper function to save the contents of a BTF object to a
file.

Signed-off-by: Mauricio Vásquez <redacted>
Signed-off-by: Rafael David Tinoco <redacted>
Signed-off-by: Lorenzo Fontana <redacted>
Signed-off-by: Leonardo Di Donato <redacted>
---
 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.

quoted
+
 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
quoted
 } LIBBPF_0.5.0;
--
2.25.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help