Commit 05a06be7 authored by Quentin Monnet's avatar Quentin Monnet Committed by Alexei Starovoitov

bpftool: Return an error on prog dumps if both CFG and JSON are required

We do not support JSON output for control flow graphs of programs with
bpftool. So far, requiring both the CFG and JSON output would result in
producing a null JSON object. It makes more sense to raise an error
directly when parsing command line arguments and options, so that users
know they won't get any output they might expect.

If JSON is required for the graph, we leave it to Graphviz instead:

    # bpftool prog dump xlated <REF> visual | dot -Tjson
Suggested-by: default avatarEduard Zingerman <eddyz87@gmail.com>
Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/r/20230405132120.59886-5-quentin@isovalent.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 9fd49684
...@@ -255,7 +255,7 @@ _bpftool_map_update_get_name() ...@@ -255,7 +255,7 @@ _bpftool_map_update_get_name()
_bpftool() _bpftool()
{ {
local cur prev words objword local cur prev words objword json=0
_init_completion || return _init_completion || return
# Deal with options # Deal with options
...@@ -265,6 +265,9 @@ _bpftool() ...@@ -265,6 +265,9 @@ _bpftool()
COMPREPLY=( $( compgen -W "$c" -- "$cur" ) ) COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
return 0 return 0
fi fi
if _bpftool_search_list -j --json -p --pretty; then
json=1
fi
# Deal with simplest keywords # Deal with simplest keywords
case $prev in case $prev in
...@@ -367,7 +370,7 @@ _bpftool() ...@@ -367,7 +370,7 @@ _bpftool()
;; ;;
*) *)
_bpftool_once_attr 'file' _bpftool_once_attr 'file'
if _bpftool_search_list 'xlated'; then if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then
COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \ COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
"$cur" ) ) "$cur" ) )
else else
......
...@@ -849,9 +849,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -849,9 +849,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
dd.finfo_rec_size = info->func_info_rec_size; dd.finfo_rec_size = info->func_info_rec_size;
dd.prog_linfo = prog_linfo; dd.prog_linfo = prog_linfo;
if (json_output && visual) if (json_output)
jsonw_null(json_wtr);
else if (json_output)
dump_xlated_json(&dd, buf, member_len, opcodes, linum); dump_xlated_json(&dd, buf, member_len, opcodes, linum);
else if (visual) else if (visual)
dump_xlated_cfg(&dd, buf, member_len); dump_xlated_cfg(&dd, buf, member_len);
...@@ -940,6 +938,10 @@ static int do_dump(int argc, char **argv) ...@@ -940,6 +938,10 @@ static int do_dump(int argc, char **argv)
usage(); usage();
goto exit_close; goto exit_close;
} }
if (json_output && visual) {
p_err("'visual' is not compatible with JSON output");
goto exit_close;
}
if (json_output && nb_fds > 1) if (json_output && nb_fds > 1)
jsonw_start_array(json_wtr); /* root array */ jsonw_start_array(json_wtr); /* root array */
......
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