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

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

index 9e456db900585fe19dd0d1670de172f0a2a573e4..3ea0fec39161dc849e26341a9f9704f305fefb6d 100644 (file)
@@ -35,6 +35,7 @@ static struct prog_env {
        unsigned int iterations;
        bool timestamp;
        char *funcname;
+       bool verbose;
 } env = {
        .interval = 99999999,
        .iterations = 99999999,
@@ -74,6 +75,7 @@ static const struct argp_option opts[] = {
        { "interval", 'i', "INTERVAL", 0, "Summary interval in seconds"},
        { "duration", 'd', "DURATION", 0, "Duration to trace"},
        { "timestamp", 'T', NULL, 0, "Print timestamp"},
+       { "verbose", 'v', NULL, 0, "Verbose debug output" },
        { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"},
        {},
 };
@@ -128,6 +130,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
        case 'T':
                env->timestamp = true;
                break;
+       case 'v':
+               env->verbose = true;
+               break;
        case 'h':
                argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
                break;
@@ -155,6 +160,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 && !env.verbose)
+               return 0;
+       return vfprintf(stderr, format, args);
+}
+
 static const char *unit_str(void)
 {
        switch (env.units) {
@@ -285,6 +297,7 @@ int main(int argc, char **argv)
        sigaction(SIGINT, &sigact, 0);
 
        libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
+       libbpf_set_print(libbpf_print_fn);
 
        obj = funclatency_bpf__open();
        if (!obj) {