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

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

index f653637a2bb35f607bbe911a2b4bd5a92bdd1c69..9b3ebc28dcc00c76a582d6afd67e822dc89a06ed 100644 (file)
@@ -28,6 +28,7 @@ static volatile sig_atomic_t exiting = 0;
 
 static pid_t target_pid = 0;
 static const char *libc_path = NULL;
+static bool verbose = false;
 
 const char *argp_program_version = "gethostlatency 0.1";
 const char *argp_program_bug_address =
@@ -44,6 +45,7 @@ const char argp_program_doc[] =
 static const struct argp_option opts[] = {
        { "pid", 'p', "PID", 0, "Process ID to trace" },
        { "libc", 'l', "LIBC", 0, "Specify which libc.so to use" },
+       { "verbose", 'v', NULL, 0, "Verbose debug output" },
        { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
        {},
 };
@@ -69,6 +71,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
                        argp_usage(state);
                }
                break;
+       case 'v':
+               verbose = true;
+               break;
        case 'h':
                argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
                break;
@@ -78,6 +83,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;
@@ -219,6 +231,7 @@ int main(int argc, char **argv)
                return err;
 
        libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
+       libbpf_set_print(libbpf_print_fn);
 
        obj = gethostlatency_bpf__open();
        if (!obj) {