Skip to content

Commit f76d850

Browse files
olsajirianakryiko
authored andcommitted
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. Fixes: e504389 ("bpftool: Use libbpf_get_error() to check error") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 477bb4c commit f76d850

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

tools/bpf/bpftool/map.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -805,29 +805,30 @@ 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-
}
827+
} else {
828+
*btf = NULL;
828829
}
829830

830-
return btf;
831+
return err;
831832
}
832833

833834
static void free_map_kv_btf(struct btf *btf)
@@ -862,8 +863,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
862863
prev_key = NULL;
863864

864865
if (wtr) {
865-
btf = get_map_kv_btf(info);
866-
err = libbpf_get_error(btf);
866+
err = get_map_kv_btf(info, &btf);
867867
if (err) {
868868
goto exit_free;
869869
}
@@ -1054,8 +1054,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
10541054
json_writer_t *btf_wtr;
10551055
struct btf *btf;
10561056

1057-
btf = get_map_kv_btf(info);
1058-
if (libbpf_get_error(btf))
1057+
if (get_map_kv_btf(info, &btf))
10591058
return;
10601059

10611060
if (json_output) {

0 commit comments

Comments
 (0)