Commit f76d8507 authored by Jiri Olsa's avatar Jiri Olsa Committed by Andrii Nakryiko

bpftool: Fix pretty print dump for maps without BTF loaded

The commit e5043894 ("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: e5043894 ("bpftool: Use libbpf_get_error() to check error")
Suggested-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarJiri Olsa <jolsa@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220216092102.125448-1-jolsa@kernel.org
parent 477bb4c1
...@@ -805,29 +805,30 @@ static int maps_have_btf(int *fds, int nb_fds) ...@@ -805,29 +805,30 @@ static int maps_have_btf(int *fds, int nb_fds)
static struct btf *btf_vmlinux; static struct btf *btf_vmlinux;
static struct btf *get_map_kv_btf(const struct bpf_map_info *info) static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)
{ {
struct btf *btf = NULL; int err = 0;
if (info->btf_vmlinux_value_type_id) { if (info->btf_vmlinux_value_type_id) {
if (!btf_vmlinux) { if (!btf_vmlinux) {
btf_vmlinux = libbpf_find_kernel_btf(); btf_vmlinux = libbpf_find_kernel_btf();
if (libbpf_get_error(btf_vmlinux)) err = libbpf_get_error(btf_vmlinux);
if (err) {
p_err("failed to get kernel btf"); p_err("failed to get kernel btf");
return err;
} }
return btf_vmlinux; }
*btf = btf_vmlinux;
} else if (info->btf_value_type_id) { } else if (info->btf_value_type_id) {
int err; *btf = btf__load_from_kernel_by_id(info->btf_id);
err = libbpf_get_error(*btf);
btf = btf__load_from_kernel_by_id(info->btf_id); if (err)
err = libbpf_get_error(btf);
if (err) {
p_err("failed to get btf"); p_err("failed to get btf");
btf = ERR_PTR(err); } else {
} *btf = NULL;
} }
return btf; return err;
} }
static void free_map_kv_btf(struct btf *btf) 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, ...@@ -862,8 +863,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
prev_key = NULL; prev_key = NULL;
if (wtr) { if (wtr) {
btf = get_map_kv_btf(info); err = get_map_kv_btf(info, &btf);
err = libbpf_get_error(btf);
if (err) { if (err) {
goto exit_free; goto exit_free;
} }
...@@ -1054,8 +1054,7 @@ static void print_key_value(struct bpf_map_info *info, void *key, ...@@ -1054,8 +1054,7 @@ static void print_key_value(struct bpf_map_info *info, void *key,
json_writer_t *btf_wtr; json_writer_t *btf_wtr;
struct btf *btf; struct btf *btf;
btf = get_map_kv_btf(info); if (get_map_kv_btf(info, &btf))
if (libbpf_get_error(btf))
return; return;
if (json_output) { if (json_output) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment