From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: "Mauricio Vásquez" <mauricio@kinvolk.io>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Rafael David Tinoco <rafaeldtinoco@gmail.com>,
Rafael David Tinoco <rafael.tinoco@aquasec.com>,
Lorenzo Fontana <lorenzo.fontana@elastic.co>
Subject: Re: [PATCH bpf-next 1/2] libbpf: Implement btf__save_to_file()
Date: Thu, 28 Oct 2021 11:36:28 -0700 [thread overview]
Message-ID: <CAEf4BzZ2xxSSaLuvV=uLa0trom8_RPx8XR=KxeP5WF+2z3DMBA@mail.gmail.com> (raw)
In-Reply-To: <20211027203727.208847-2-mauricio@kinvolk.io>
On Wed, Oct 27, 2021 at 1:37 PM Mauricio Vásquez <mauricio@kinvolk.io> wrote:
>
> Implement helper function to save the contents of a BTF object to a
> file.
>
> Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
> Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
> Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
> ---
> tools/lib/bpf/btf.c | 22 ++++++++++++++++++++++
> tools/lib/bpf/btf.h | 2 ++
> tools/lib/bpf/libbpf.map | 1 +
> 3 files changed, 25 insertions(+)
>
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 0c628c33e23b..087035574dba 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -4773,3 +4773,25 @@ int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void
>
> return 0;
> }
> +
> +int btf__save_to_file(struct btf *btf, const char *path)
given we have its loading counterpart as btf__parse_raw(), let's call
this one btf__save_raw()?
> +{
> + const void *data;
> + __u32 data_sz;
> + FILE *file;
> +
> + data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
use btf__raw_data() instead? no need to think about btf->swapped_endian here
> + if (!data)
> + return -ENOMEM;
please use libbpf_err() helper for returning errors, see other use cases
> +
> + file = fopen(path, "wb");
> + if (!file)
> + return -errno;
> +
> + if (fwrite(data, 1, data_sz, file) != data_sz) {
> + fclose(file);
> + return -EIO;
why not propagating original errno? make sure you save it before
fclose(), though
> + }
> +
> + return fclose(file);
hm... I'd just do fclose(file) separately and return 0 (because
success). If file closing fails, there isn't anything that can be done
(but it also shouldn't fail in any normal situation).
> +}
> diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
> index bc005ba3ceec..300ad498c615 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_to_file(struct btf *btf, const char *path);
const struct btf? btf__raw_data() (even though it internally modifies
btf) accepts `const struct btf*`, because this is conceptually
read-only operation
> +
> 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 15239c05659c..0e9bed7c9b9e 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -399,4 +399,5 @@ LIBBPF_0.6.0 {
> btf__add_decl_tag;
> btf__raw_data;
> btf__type_cnt;
> + btf__save_to_file;
> } LIBBPF_0.5.0;
> --
> 2.25.1
>
next prev parent reply other threads:[~2021-10-28 18:36 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-27 20:37 [PATCH bpf-next 0/2] libbpf: Implement BTF Generator API Mauricio Vásquez
2021-10-27 20:37 ` [PATCH bpf-next 1/2] libbpf: Implement btf__save_to_file() Mauricio Vásquez
2021-10-28 18:36 ` Andrii Nakryiko [this message]
2021-10-27 20:37 ` [PATCH bpf-next 2/2] libbpf: Implement API for generating BTF for ebpf objects Mauricio Vásquez
2021-10-28 18:45 ` Andrii Nakryiko
2021-10-28 22:42 ` Mauricio Vásquez Bernal
2021-10-28 22:48 ` Andrii Nakryiko
2021-10-29 2:33 ` [PATCH bpf-next 0/2] libbpf: Implement BTF Generator API Alexei Starovoitov
2021-10-29 5:41 ` Rafael David Tinoco
2021-10-29 5:51 ` Rafael David Tinoco
2021-10-29 16:12 ` Mauricio Vásquez Bernal
2021-11-02 5:53 ` Andrii Nakryiko
2021-11-02 10:58 ` Leonardo Di Donato
2021-11-02 17:12 ` Andrii Nakryiko
2021-11-02 21:26 ` Mauricio Vásquez Bernal
2021-11-03 5:26 ` Andrii Nakryiko
2021-11-04 14:58 ` Mauricio Vásquez Bernal
2021-11-04 17:34 ` Andrii Nakryiko
2021-11-03 23:40 ` Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAEf4BzZ2xxSSaLuvV=uLa0trom8_RPx8XR=KxeP5WF+2z3DMBA@mail.gmail.com' \
--to=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=lorenzo.fontana@elastic.co \
--cc=mauricio@kinvolk.io \
--cc=netdev@vger.kernel.org \
--cc=rafael.tinoco@aquasec.com \
--cc=rafaeldtinoco@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).