libbpf-tools: Add verbose option to bashreadline
authorHengqi Chen <chenhengqi@outlook.com>
Sat, 25 Dec 2021 04:05:54 +0000 (12:05 +0800)
committerHengqi Chen <chenhengqi@outlook.com>
Sat, 25 Dec 2021 04:05:54 +0000 (12:05 +0800)
Support verbose mode and set custom libbpf print callback
in bashreadline.

Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
libbpf-tools/bashreadline.c

index 3fb7fea84463c87bd2d106a628a9deeff55ffa0f..2fcb2e2cadf717e798a4b5b8e720b7a26187d33e 100644 (file)
@@ -35,11 +35,13 @@ const char argp_program_doc[] =
 
 static const struct argp_option opts[] = {
        { "shared", 's', "PATH", 0, "the location of libreadline.so library" },
+       { "verbose", 'v', NULL, 0, "Verbose debug output" },
        { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
        {},
 };
 
 static char *libreadline_path = NULL;
+static bool verbose = false;
 
 static error_t parse_arg(int key, char *arg, struct argp_state *state)
 {
@@ -49,6 +51,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
                if (libreadline_path == NULL)
                        return ARGP_ERR_UNKNOWN;
                break;
+       case 'v':
+               verbose = true;
+               break;
        case 'h':
                argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
                break;
@@ -58,6 +63,13 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
        return 0;
 }
 
+static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
+{
+       if (level == LIBBPF_DEBUG && !verbose)
+               return 0;
+       return vfprintf(stderr, format, args);
+}
+
 static void handle_event(void *ctx, int cpu, void *data, __u32 data_size)
 {
        struct str_t *e = data;
@@ -154,6 +166,7 @@ int main(int argc, char **argv)
        }
 
        libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
+       libbpf_set_print(libbpf_print_fn);
 
        obj = bashreadline_bpf__open_and_load();
        if (!obj) {