tools: bpftool: add -d option to get debug output from libbpf
authorQuentin Monnet <quentin.monnet@netronome.com>
Fri, 24 May 2019 10:36:46 +0000 (11:36 +0100)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 28 May 2019 09:02:21 +0000 (11:02 +0200)
libbpf has three levels of priority for output messages: warn, info,
debug. By default, debug output is not printed to the console.

Add a new "--debug" (short name: "-d") option to bpftool to print libbpf
logs for all three levels.

Internally, we simply use the function provided by libbpf to replace the
default printing function by one that prints logs regardless of their
level.

v2:
- Remove the possibility to select the log-levels to use (v1 offered a
  combination of "warn", "info" and "debug").
- Rename option and offer a short name: -d|--debug.
- Add option description to all bpftool manual pages (instead of
  bpftool-prog.rst only), as all commands use libbpf.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/bpf/bpftool/Documentation/bpftool-btf.rst
tools/bpf/bpftool/Documentation/bpftool-cgroup.rst
tools/bpf/bpftool/Documentation/bpftool-feature.rst
tools/bpf/bpftool/Documentation/bpftool-map.rst
tools/bpf/bpftool/Documentation/bpftool-net.rst
tools/bpf/bpftool/Documentation/bpftool-perf.rst
tools/bpf/bpftool/Documentation/bpftool-prog.rst
tools/bpf/bpftool/Documentation/bpftool.rst
tools/bpf/bpftool/bash-completion/bpftool
tools/bpf/bpftool/main.c

index 3daed9e..6694a0f 100644 (file)
@@ -72,6 +72,10 @@ OPTIONS
        -p, --pretty
                  Generate human-readable JSON output. Implies **-j**.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 EXAMPLES
 ========
 **# bpftool btf dump id 1226**
index ac26876..3680773 100644 (file)
@@ -113,6 +113,10 @@ OPTIONS
        -f, --bpffs
                  Show file names of pinned programs.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 EXAMPLES
 ========
 |
index 14180e8..4d08f35 100644 (file)
@@ -73,6 +73,10 @@ OPTIONS
        -p, --pretty
                  Generate human-readable JSON output. Implies **-j**.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 SEE ALSO
 ========
        **bpf**\ (2),
index 13ef27b..490b450 100644 (file)
@@ -152,6 +152,10 @@ OPTIONS
                  Do not automatically attempt to mount any virtual file system
                  (such as tracefs or BPF virtual file system) when necessary.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 EXAMPLES
 ========
 **# bpftool map show**
index 9345808..d8e5237 100644 (file)
@@ -65,6 +65,10 @@ OPTIONS
        -p, --pretty
                  Generate human-readable JSON output. Implies **-j**.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 EXAMPLES
 ========
 
index 0c75765..e252bd0 100644 (file)
@@ -53,6 +53,10 @@ OPTIONS
        -p, --pretty
                  Generate human-readable JSON output. Implies **-j**.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 EXAMPLES
 ========
 
index e811854..9a92614 100644 (file)
@@ -174,6 +174,10 @@ OPTIONS
                  Do not automatically attempt to mount any virtual file system
                  (such as tracefs or BPF virtual file system) when necessary.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
+
 EXAMPLES
 ========
 **# bpftool prog show**
index 3e562d7..43dba07 100644 (file)
@@ -66,6 +66,9 @@ OPTIONS
                  Do not automatically attempt to mount any virtual file system
                  (such as tracefs or BPF virtual file system) when necessary.
 
+       -d, --debug
+                 Print all logs available from libbpf, including debug-level
+                 information.
 
 SEE ALSO
 ========
index fbbad0e..2725e27 100644 (file)
@@ -187,7 +187,7 @@ _bpftool()
 
     # Deal with options
     if [[ ${words[cword]} == -* ]]; then
-        local c='--version --json --pretty --bpffs --mapcompat'
+        local c='--version --json --pretty --bpffs --mapcompat --debug'
         COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
         return 0
     fi
index 1ac1fc5..d742939 100644 (file)
@@ -10,6 +10,7 @@
 #include <string.h>
 
 #include <bpf.h>
+#include <libbpf.h>
 
 #include "main.h"
 
@@ -77,6 +78,13 @@ static int do_version(int argc, char **argv)
        return 0;
 }
 
+static int __printf(2, 0)
+print_all_levels(__maybe_unused enum libbpf_print_level level,
+                const char *format, va_list args)
+{
+       return vfprintf(stderr, format, args);
+}
+
 int cmd_select(const struct cmd *cmds, int argc, char **argv,
               int (*help)(int argc, char **argv))
 {
@@ -317,6 +325,7 @@ int main(int argc, char **argv)
                { "bpffs",      no_argument,    NULL,   'f' },
                { "mapcompat",  no_argument,    NULL,   'm' },
                { "nomount",    no_argument,    NULL,   'n' },
+               { "debug",      no_argument,    NULL,   'd' },
                { 0 }
        };
        int opt, ret;
@@ -332,7 +341,7 @@ int main(int argc, char **argv)
        hash_init(map_table.table);
 
        opterr = 0;
-       while ((opt = getopt_long(argc, argv, "Vhpjfmn",
+       while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
                                  options, NULL)) >= 0) {
                switch (opt) {
                case 'V':
@@ -362,6 +371,9 @@ int main(int argc, char **argv)
                case 'n':
                        block_mount = true;
                        break;
+               case 'd':
+                       libbpf_set_print(print_all_levels);
+                       break;
                default:
                        p_err("unrecognized option '%s'", argv[optind - 1]);
                        if (json_output)