From 82aacde397bd519a4f0e865881ff150ed34680ed Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Sat, 25 Dec 2021 12:06:39 +0800 Subject: [PATCH] libbpf-tools: Add verbose option to bindsnoop Support verbose mode and set custom libbpf print callback in bindsnoop. Signed-off-by: Hengqi Chen --- libbpf-tools/bindsnoop.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libbpf-tools/bindsnoop.c b/libbpf-tools/bindsnoop.c index 827bff39..5d87d484 100644 --- a/libbpf-tools/bindsnoop.c +++ b/libbpf-tools/bindsnoop.c @@ -30,6 +30,7 @@ static bool emit_timestamp = false; static pid_t target_pid = 0; static bool ignore_errors = true; static char *target_ports = NULL; +static bool verbose = false; const char *argp_program_version = "bindsnoop 0.1"; const char *argp_program_bug_address = @@ -58,6 +59,7 @@ static const struct argp_option opts[] = { { "failed", 'x', NULL, 0, "Include errors on output." }, { "pid", 'p', "PID", 0, "Process ID to trace" }, { "ports", 'P', "PORTS", 0, "Comma-separated list of ports to trace." }, + { "verbose", 'v', NULL, 0, "Verbose debug output" }, { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, {}, }; @@ -99,6 +101,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; @@ -108,6 +113,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; @@ -173,6 +185,7 @@ int main(int argc, char **argv) return err; libbpf_set_strict_mode(LIBBPF_STRICT_ALL); + libbpf_set_print(libbpf_print_fn); obj = bindsnoop_bpf__open(); if (!obj) { -- 2.34.1