Skip to content

Commit 8a857df

Browse files
olsajiriNobody
authored and
Nobody
committed
bpftool: Fix pretty print dump for maps without BTF loaded
The commit e504389 ("bpftool: Use libbpf_get_error() to check error") fails to dump map without BTF loaded in pretty mode (-p option). Fixing this by making sure get_map_kv_btf won't fail in case there's no BTF available for the map. Cc: Yinjun Zhang <[email protected]> Fixes: e504389 ("bpftool: Use libbpf_get_error() to check error") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Jiri Olsa <[email protected]>
1 parent 7567200 commit 8a857df

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

tools/bpf/bpftool/map.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -805,29 +805,28 @@ static int maps_have_btf(int *fds, int nb_fds)
805805

806806
static struct btf *btf_vmlinux;
807807

808-
static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
808+
static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
809809
{
810-
struct btf *btf = NULL;
810+
int err = 0;
811811

812812
if (info->btf_vmlinux_value_type_id) {
813813
if (!btf_vmlinux) {
814814
btf_vmlinux = libbpf_find_kernel_btf();
815-
if (libbpf_get_error(btf_vmlinux))
815+
err = libbpf_get_error(btf_vmlinux);
816+
if (err) {
816817
p_err("failed to get kernel btf");
818+
return err;
819+
}
817820
}
818-
return btf_vmlinux;
821+
*btf = btf_vmlinux;
819822
} else if (info->btf_value_type_id) {
820-
int err;
821-
822-
btf = btf__load_from_kernel_by_id(info->btf_id);
823-
err = libbpf_get_error(btf);
824-
if (err) {
823+
*btf = btf__load_from_kernel_by_id(info->btf_id);
824+
err = libbpf_get_error(*btf);
825+
if (err)
825826
p_err("failed to get btf");
826-
btf = ERR_PTR(err);
827-
}
828827
}
829828

830-
return btf;
829+
return err;
831830
}
832831

833832
static void free_map_kv_btf(struct btf *btf)
@@ -862,8 +861,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
862861
prev_key = NULL;
863862

864863
if (wtr) {
865-
btf = get_map_kv_btf(info);
866-
err = libbpf_get_error(btf);
864+
err = get_map_kv_btf(info, &btf);
867865
if (err) {
868866
goto exit_free;
869867
}
@@ -1054,8 +1052,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
10541052
json_writer_t *btf_wtr;
10551053
struct btf *btf;
10561054

1057-
btf = get_map_kv_btf(info);
1058-
if (libbpf_get_error(btf))
1055+
if (get_map_kv_btf(info, &btf))
10591056
return;
10601057

10611058
if (json_output) {

0 commit comments

Comments
 (0)