Commit 188a4866 authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov

bpftool: Fix missing BTF output for json during map dump

The btf availability check is only done for plain text output.
It causes the whole BTF output went missing when json_output
is used.

This patch simplifies the logic a little by avoiding passing "int btf" to
map_dump().

For plain text output, the btf_wtr is only created when the map has
BTF (i.e. info->btf_id != 0).  The nullness of "json_writer_t *wtr"
in map_dump() alone can decide if dumping BTF output is needed.
As long as wtr is not NULL, map_dump() will print out the BTF-described
data whenever a map has BTF available (i.e. info->btf_id != 0)
regardless of json or plain-text output.

In do_dump(), the "int btf" is also renamed to "int do_plain_btf".

Fixes: 99f9863a ("bpftool: Match maps by name")
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>
Cc: Paul Chaignon <paul.chaignon@orange.com>
Link: https://lore.kernel.org/bpf/20200115230025.1101828-1-kafai@fb.com
parent d7de7267
...@@ -933,7 +933,7 @@ static int maps_have_btf(int *fds, int nb_fds) ...@@ -933,7 +933,7 @@ static int maps_have_btf(int *fds, int nb_fds)
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 enable_btf, bool show_header) bool show_header)
{ {
void *key, *value, *prev_key; void *key, *value, *prev_key;
unsigned int num_elems = 0; unsigned int num_elems = 0;
...@@ -950,18 +950,16 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, ...@@ -950,18 +950,16 @@ map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
prev_key = NULL; prev_key = NULL;
if (enable_btf) { if (wtr) {
if (info->btf_id) {
err = btf__get_from_id(info->btf_id, &btf); err = btf__get_from_id(info->btf_id, &btf);
if (err || !btf) { if (err || !btf) {
/* enable_btf is true only if we've already checked err = err ? : -ESRCH;
* that all maps have BTF information.
*/
p_err("failed to get btf"); p_err("failed to get btf");
goto exit_free; goto exit_free;
} }
} }
if (wtr) {
if (show_header) { if (show_header) {
jsonw_start_object(wtr); /* map object */ jsonw_start_object(wtr); /* map object */
show_map_header_json(info, wtr); show_map_header_json(info, wtr);
...@@ -1009,7 +1007,7 @@ static int do_dump(int argc, char **argv) ...@@ -1009,7 +1007,7 @@ static int do_dump(int argc, char **argv)
{ {
json_writer_t *wtr = NULL, *btf_wtr = NULL; json_writer_t *wtr = NULL, *btf_wtr = NULL;
struct bpf_map_info info = {}; struct bpf_map_info info = {};
int nb_fds, i = 0, btf = 0; int nb_fds, i = 0;
__u32 len = sizeof(info); __u32 len = sizeof(info);
int *fds = NULL; int *fds = NULL;
int err = -1; int err = -1;
...@@ -1029,17 +1027,17 @@ static int do_dump(int argc, char **argv) ...@@ -1029,17 +1027,17 @@ static int do_dump(int argc, char **argv)
if (json_output) { if (json_output) {
wtr = json_wtr; wtr = json_wtr;
} else { } else {
btf = maps_have_btf(fds, nb_fds); int do_plain_btf;
if (btf < 0)
do_plain_btf = maps_have_btf(fds, nb_fds);
if (do_plain_btf < 0)
goto exit_close; goto exit_close;
if (btf) {
if (do_plain_btf) {
btf_wtr = get_btf_writer(); btf_wtr = get_btf_writer();
if (btf_wtr) {
wtr = btf_wtr; wtr = btf_wtr;
} else { if (!btf_wtr)
p_info("failed to create json writer for btf. falling back to plain output"); p_info("failed to create json writer for btf. falling back to plain output");
btf = 0;
}
} }
} }
...@@ -1050,7 +1048,7 @@ static int do_dump(int argc, char **argv) ...@@ -1050,7 +1048,7 @@ static int do_dump(int argc, char **argv)
p_err("can't get map info: %s", strerror(errno)); p_err("can't get map info: %s", strerror(errno));
break; break;
} }
err = map_dump(fds[i], &info, wtr, btf, nb_fds > 1); err = map_dump(fds[i], &info, wtr, nb_fds > 1);
if (!wtr && i != nb_fds - 1) if (!wtr && i != nb_fds - 1)
printf("\n"); printf("\n");
...@@ -1061,7 +1059,7 @@ static int do_dump(int argc, char **argv) ...@@ -1061,7 +1059,7 @@ static int do_dump(int argc, char **argv)
if (wtr && nb_fds > 1) if (wtr && nb_fds > 1)
jsonw_end_array(wtr); /* root array */ jsonw_end_array(wtr); /* root array */
if (btf) if (btf_wtr)
jsonw_destroy(&btf_wtr); jsonw_destroy(&btf_wtr);
exit_close: exit_close:
for (; i < nb_fds; i++) for (; i < nb_fds; i++)
......
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