Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools: bpftool: Print optional built-in features along with version

Bpftool has a number of features that can be included or left aside
during compilation. This includes:

- Support for libbfd, providing the disassembler for JIT-compiled
programs.
- Support for BPF skeletons, used for profiling programs or iterating on
the PIDs of processes associated with BPF objects.

In order to make it easy for users to understand what features were
compiled for a given bpftool binary, print the status of the two
features above when showing the version number for bpftool ("bpftool -V"
or "bpftool version"). Document this in the main manual page. Example
invocations:

$ bpftool version
./bpftool v5.9.0-rc1
features: libbfd, skeletons

$ bpftool -p version
{
"version": "5.9.0-rc1",
"features": {
"libbfd": true,
"skeletons": true
}
}

Some other parameters are optional at compilation
("DISASM_FOUR_ARGS_SIGNATURE", LIBCAP support) but they do not impact
significantly bpftool's behaviour from a user's point of view, so their
status is not reported.

Available commands and supported program types depend on the version
number, and are therefore not reported either. Note that they are
already available, albeit without JSON, via bpftool's help messages.

v3:
- Use a simple list instead of boolean values for plain output.

v2:
- Fix JSON (object instead or array for the features).

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200909162500.17010-2-quentin@isovalent.com

authored by

Quentin Monnet and committed by
Alexei Starovoitov
82b8cf0a 41d5c37b

+38 -3
+7 -1
tools/bpf/bpftool/Documentation/bpftool.rst
··· 50 50 Print short help message (similar to **bpftool help**). 51 51 52 52 -V, --version 53 - Print version number (similar to **bpftool version**). 53 + Print version number (similar to **bpftool version**), and 54 + optional features that were included when bpftool was 55 + compiled. Optional features include linking against libbfd to 56 + provide the disassembler for JIT-ted programs (**bpftool prog 57 + dump jited**) and usage of BPF skeletons (some features like 58 + **bpftool prog profile** or showing pids associated to BPF 59 + objects may rely on it). 54 60 55 61 -j, --json 56 62 Generate JSON output. For commands that cannot produce JSON, this
+31 -2
tools/bpf/bpftool/main.c
··· 70 70 71 71 static int do_version(int argc, char **argv) 72 72 { 73 + #ifdef HAVE_LIBBFD_SUPPORT 74 + const bool has_libbfd = true; 75 + #else 76 + const bool has_libbfd = false; 77 + #endif 78 + #ifdef BPFTOOL_WITHOUT_SKELETONS 79 + const bool has_skeletons = false; 80 + #else 81 + const bool has_skeletons = true; 82 + #endif 83 + 73 84 if (json_output) { 74 - jsonw_start_object(json_wtr); 85 + jsonw_start_object(json_wtr); /* root object */ 86 + 75 87 jsonw_name(json_wtr, "version"); 76 88 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION); 77 - jsonw_end_object(json_wtr); 89 + 90 + jsonw_name(json_wtr, "features"); 91 + jsonw_start_object(json_wtr); /* features */ 92 + jsonw_bool_field(json_wtr, "libbfd", has_libbfd); 93 + jsonw_bool_field(json_wtr, "skeletons", has_skeletons); 94 + jsonw_end_object(json_wtr); /* features */ 95 + 96 + jsonw_end_object(json_wtr); /* root object */ 78 97 } else { 98 + unsigned int nb_features = 0; 99 + 79 100 printf("%s v%s\n", bin_name, BPFTOOL_VERSION); 101 + printf("features:"); 102 + if (has_libbfd) { 103 + printf(" libbfd"); 104 + nb_features++; 105 + } 106 + if (has_skeletons) 107 + printf("%s skeletons", nb_features++ ? "," : ""); 108 + printf("\n"); 80 109 } 81 110 return 0; 82 111 }