Commit 4e1ea332 authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov

bpftool: Support dumping a map with btf_vmlinux_value_type_id

This patch makes bpftool support dumping a map's value properly
when the map's value type is a type of the running kernel's btf.
(i.e. map_info.btf_vmlinux_value_type_id is set instead of
map_info.btf_value_type_id).  The first usecase is for the
BPF_MAP_TYPE_STRUCT_OPS.
Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200115230044.1103008-1-kafai@fb.com
parent 84c72cee
...@@ -252,6 +252,7 @@ static int do_dump_btf(const struct btf_dumper *d, ...@@ -252,6 +252,7 @@ static int do_dump_btf(const struct btf_dumper *d,
struct bpf_map_info *map_info, void *key, struct bpf_map_info *map_info, void *key,
void *value) void *value)
{ {
__u32 value_id;
int ret; int ret;
/* start of key-value pair */ /* start of key-value pair */
...@@ -265,9 +266,12 @@ static int do_dump_btf(const struct btf_dumper *d, ...@@ -265,9 +266,12 @@ static int do_dump_btf(const struct btf_dumper *d,
goto err_end_obj; goto err_end_obj;
} }
value_id = map_info->btf_vmlinux_value_type_id ?
: map_info->btf_value_type_id;
if (!map_is_per_cpu(map_info->type)) { if (!map_is_per_cpu(map_info->type)) {
jsonw_name(d->jw, "value"); jsonw_name(d->jw, "value");
ret = btf_dumper_type(d, map_info->btf_value_type_id, value); ret = btf_dumper_type(d, value_id, value);
} else { } else {
unsigned int i, n, step; unsigned int i, n, step;
...@@ -279,8 +283,7 @@ static int do_dump_btf(const struct btf_dumper *d, ...@@ -279,8 +283,7 @@ static int do_dump_btf(const struct btf_dumper *d,
jsonw_start_object(d->jw); jsonw_start_object(d->jw);
jsonw_int_field(d->jw, "cpu", i); jsonw_int_field(d->jw, "cpu", i);
jsonw_name(d->jw, "value"); jsonw_name(d->jw, "value");
ret = btf_dumper_type(d, map_info->btf_value_type_id, ret = btf_dumper_type(d, value_id, value + i * step);
value + i * step);
jsonw_end_object(d->jw); jsonw_end_object(d->jw);
if (ret) if (ret)
break; break;
...@@ -932,6 +935,44 @@ static int maps_have_btf(int *fds, int nb_fds) ...@@ -932,6 +935,44 @@ static int maps_have_btf(int *fds, int nb_fds)
return 1; return 1;
} }
static struct btf *btf_vmlinux;
static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
{
struct btf *btf = NULL;
if (info->btf_vmlinux_value_type_id) {
if (!btf_vmlinux) {
btf_vmlinux = libbpf_find_kernel_btf();
if (IS_ERR(btf_vmlinux))
p_err("failed to get kernel btf");
}
return btf_vmlinux;
} else if (info->btf_value_type_id) {
int err;
err = btf__get_from_id(info->btf_id, &btf);
if (err || !btf) {
p_err("failed to get btf");
btf = err ? ERR_PTR(err) : ERR_PTR(-ESRCH);
}
}
return btf;
}
static void free_map_kv_btf(struct btf *btf)
{
if (!IS_ERR(btf) && btf != btf_vmlinux)
btf__free(btf);
}
static void free_btf_vmlinux(void)
{
if (!IS_ERR(btf_vmlinux))
btf__free(btf_vmlinux);
}
static int static int
map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
bool show_header) bool show_header)
...@@ -952,14 +993,11 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, ...@@ -952,14 +993,11 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
prev_key = NULL; prev_key = NULL;
if (wtr) { if (wtr) {
if (info->btf_id) { btf = get_map_kv_btf(info);
err = btf__get_from_id(info->btf_id, &btf); if (IS_ERR(btf)) {
if (err || !btf) { err = PTR_ERR(btf);
err = err ? : -ESRCH;
p_err("failed to get btf");
goto exit_free; goto exit_free;
} }
}
if (show_header) { if (show_header) {
jsonw_start_object(wtr); /* map object */ jsonw_start_object(wtr); /* map object */
...@@ -999,7 +1037,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, ...@@ -999,7 +1037,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
free(key); free(key);
free(value); free(value);
close(fd); close(fd);
btf__free(btf); free_map_kv_btf(btf);
return err; return err;
} }
...@@ -1067,6 +1105,7 @@ static int do_dump(int argc, char **argv) ...@@ -1067,6 +1105,7 @@ static int do_dump(int argc, char **argv)
close(fds[i]); close(fds[i]);
exit_free: exit_free:
free(fds); free(fds);
free_btf_vmlinux();
return err; return err;
} }
......
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