From 4005cc605254f55b9f4ba526463b660abb7016d8 Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Sat, 25 Dec 2021 12:10:55 +0800 Subject: [PATCH] libbpf-tools: Add verbose option to solisten Support verbose mode and set custom libbpf print callback in solisten. Signed-off-by: Hengqi Chen --- libbpf-tools/solisten.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/libbpf-tools/solisten.c b/libbpf-tools/solisten.c index bf340b51..adaa668d 100644 --- a/libbpf-tools/solisten.c +++ b/libbpf-tools/solisten.c @@ -29,6 +29,7 @@ static volatile sig_atomic_t exiting = 0; static pid_t target_pid = 0; static bool emit_timestamp = false; +static bool verbose = false; const char *argp_program_version = "solisten 0.1"; const char *argp_program_bug_address = @@ -44,9 +45,10 @@ const char argp_program_doc[] = " solisten -p 1216 # only trace PID 1216\n"; static const struct argp_option opts[] = { - {"pid", 'p', "PID", 0, "Process ID to trace"}, - {"timestamp", 't', NULL, 0, "Include timestamp on output"}, - {NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"}, + { "pid", 'p', "PID", 0, "Process ID to trace" }, + { "timestamp", 't', NULL, 0, "Include timestamp on output" }, + { "verbose", 'v', NULL, 0, "Verbose debug output" }, + { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, {}, }; @@ -67,6 +69,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) case 't': emit_timestamp = true; break; + case 'v': + verbose = true; + break; case 'h': argp_state_help(state, stderr, ARGP_HELP_STD_HELP); break; @@ -76,6 +81,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; @@ -134,6 +146,7 @@ int main(int argc, char **argv) return err; libbpf_set_strict_mode(LIBBPF_STRICT_ALL); + libbpf_set_print(libbpf_print_fn); obj = solisten_bpf__open(); if (!obj) { -- 2.34.1