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

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

index 363513abc7841b521d5032553f7bd479cc7a40a7..bca9d4d3e462730af72eb8faa9de68b27d2a28e8 100644 (file)
@@ -30,6 +30,7 @@ static bool emit_timestamp = false;
 static pid_t target_pid = 0;
 static bool trace_failed_only = false;
 static bool trace_by_process = true;
+static bool verbose = false;
 
 const char *argp_program_version = "exitsnoop 0.1";
 const char *argp_program_bug_address =
@@ -51,6 +52,7 @@ static const struct argp_option opts[] = {
        { "failed", 'x', NULL, 0, "Trace error exits only." },
        { "pid", 'p', "PID", 0, "Process ID to trace" },
        { "threaded", 'T', NULL, 0, "Trace by thread." },
+       { "verbose", 'v', NULL, 0, "Verbose debug output" },
        { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
        {},
 };
@@ -78,6 +80,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
        case 'T':
                trace_by_process = false;
                break;
+       case 'v':
+               verbose = true;
+               break;
        case 'h':
                argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
                break;
@@ -87,6 +92,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 sig_int(int signo)
 {
        exiting = 1;
@@ -149,6 +161,7 @@ int main(int argc, char **argv)
                return err;
 
        libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
+       libbpf_set_print(libbpf_print_fn);
 
        obj = exitsnoop_bpf__open();
        if (!obj) {