On Tue, Nov 8, 2022 at 11:46 PM Sahid Orentino Ferdjaoui <sahid.ferdjaoui@xxxxxxxxxxxxxxxxxxxxxxxx> wrote: > > bpftool is now totally compliant with libbpf 1.0 mode and is not > expected to be compiled with pre-1.0, let's clean-up the usage of > libbpf_get_error(). > > In some places functions that are returning result of > libbpf_get_error() will now return errno. This won't impact the > functionality as the caller checks for 0/non-0 for success/failure. > > sidenode: We can remove the checks on NULL pointers before calling > btf__free() because that function already does the check. > > Signed-off-by: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@xxxxxxxxxxxxxxxxxxxxxxxx> > --- > tools/bpf/bpftool/btf.c | 17 +++++++---------- > tools/bpf/bpftool/btf_dumper.c | 2 +- > tools/bpf/bpftool/gen.c | 11 ++++------- > tools/bpf/bpftool/iter.c | 6 ++---- > tools/bpf/bpftool/main.c | 7 +++---- > tools/bpf/bpftool/map.c | 15 +++++++-------- > tools/bpf/bpftool/prog.c | 10 +++++----- > tools/bpf/bpftool/struct_ops.c | 15 +++++++-------- > 8 files changed, 36 insertions(+), 47 deletions(-) > > diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c > index 68a70ac03c80..ed39b24e39d2 100644 > --- a/tools/bpf/bpftool/btf.c > +++ b/tools/bpf/bpftool/btf.c > @@ -467,9 +467,8 @@ static int dump_btf_c(const struct btf *btf, > int err = 0, i; > > d = btf_dump__new(btf, btf_dump_printf, NULL, NULL); > - err = libbpf_get_error(d); > - if (err) > - return err; > + if (!d) > + return errno; -errno > > printf("#ifndef __VMLINUX_H__\n"); > printf("#define __VMLINUX_H__\n"); > @@ -512,11 +511,9 @@ static struct btf *get_vmlinux_btf_from_sysfs(void) > struct btf *base; > > base = btf__parse(sysfs_vmlinux, NULL); > - if (libbpf_get_error(base)) { > - p_err("failed to parse vmlinux BTF at '%s': %ld\n", > - sysfs_vmlinux, libbpf_get_error(base)); > - base = NULL; > - } > + if (!base) > + p_err("failed to parse vmlinux BTF at '%s': %d\n", > + sysfs_vmlinux, errno); to be exactly the same: -errno (errno is always positive, while returned error is always negative) > > return base; > } > @@ -634,8 +631,8 @@ static int do_dump(int argc, char **argv) > base = get_vmlinux_btf_from_sysfs(); > > btf = btf__parse_split(*argv, base ?: base_btf); > - err = libbpf_get_error(btf); you are removing setting err to actual value returned by libbpf. I don't know if that matters, but it's certainly a change. Now on error you'll always return -1. Please check if that has any unexpected effect on calling code. > if (!btf) { > + err = errno; -errno > p_err("failed to load BTF from %s: %s", > *argv, strerror(errno)); > goto done; > @@ -681,8 +678,8 @@ static int do_dump(int argc, char **argv) > } > > btf = btf__load_from_kernel_by_id_split(btf_id, base_btf); > - err = libbpf_get_error(btf); > if (!btf) { > + err = errno; same > p_err("get btf by id (%u): %s", btf_id, strerror(errno)); > goto done; > } > diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c > index 19924b6ce796..eda71fdfe95a 100644 > --- a/tools/bpf/bpftool/btf_dumper.c > +++ b/tools/bpf/bpftool/btf_dumper.c > @@ -75,7 +75,7 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, > goto print; > > prog_btf = btf__load_from_kernel_by_id(info.btf_id); > - if (libbpf_get_error(prog_btf)) > + if (!prog_btf) > goto print; > func_type = btf__type_by_id(prog_btf, finfo.type_id); > if (!func_type || !btf_is_func(func_type)) > diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c > index cf8b4e525c88..d00b4e1b99ba 100644 > --- a/tools/bpf/bpftool/gen.c > +++ b/tools/bpf/bpftool/gen.c > @@ -252,9 +252,8 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) > int err = 0; > > d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL); > - err = libbpf_get_error(d); > - if (err) > - return err; > + if (!d) > + return errno; -errno > > bpf_object__for_each_map(map, obj) { > /* only generate definitions for memory-mapped internal maps */ > @@ -976,13 +975,11 @@ static int do_skeleton(int argc, char **argv) > /* log_level1 + log_level2 + stats, but not stable UAPI */ > opts.kernel_log_level = 1 + 2 + 4; > obj = bpf_object__open_mem(obj_data, file_sz, &opts); > - err = libbpf_get_error(obj); > - if (err) { > + if (!obj) { > char err_buf[256]; > > - libbpf_strerror(err, err_buf, sizeof(err_buf)); > + libbpf_strerror(errno, err_buf, sizeof(err_buf)); in this case doesn't matter, as libbpf_strerror drops the sign anyway > p_err("failed to open BPF object file: %s", err_buf); > - obj = NULL; > goto out; > } > [...]