]> git.baikalelectronics.ru Git - kernel.git/commitdiff
perf: ftrace: Add filter support for option -F/--funcs
authorChangbin Du <changbin.du@gmail.com>
Fri, 4 Sep 2020 15:23:57 +0000 (23:23 +0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 4 Sep 2020 19:11:16 +0000 (16:11 -0300)
Same as 'perf probe -F', this patch adds filter support for the ftrace
subcommand option '-F, --funcs <[FILTER]>'.

Here is an example that only lists functions which start with 'vfs_':

  $ sudo perf ftrace -F vfs_*
  vfs_fadvise
  vfs_fallocate
  vfs_truncate
  vfs_open
  vfs_setpos
  vfs_llseek
  vfs_readf
  vfs_writef
  ...

Suggested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Changbin Du <changbin.du@gmail.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20200904152357.6053-1-changbin.du@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-ftrace.txt
tools/perf/builtin-ftrace.c

index 78358af9a1c4fc975972cf8ee3ae5f13dc5e9c48..1e91121bac0f3815c3f3f9ba19e36ef8a5fca170 100644 (file)
@@ -33,7 +33,8 @@ OPTIONS
 
 -F::
 --funcs::
-        List all available functions to trace.
+        List available functions to trace. It accepts a pattern to
+        only list interested functions.
 
 -p::
 --pid=::
index 1d44bc2f63d85d386edef81563a3ed5be51083ca..9366fad591dcc2dd994211f82de09d0aaa7aa27b 100644 (file)
@@ -25,6 +25,7 @@
 #include "target.h"
 #include "cpumap.h"
 #include "thread_map.h"
+#include "strfilter.h"
 #include "util/cap.h"
 #include "util/config.h"
 #include "util/units.h"
@@ -36,7 +37,6 @@ struct perf_ftrace {
        struct evlist           *evlist;
        struct target           target;
        const char              *tracer;
-       bool                    list_avail_functions;
        struct list_head        filters;
        struct list_head        notrace;
        struct list_head        graph_funcs;
@@ -181,6 +181,40 @@ out:
        return ret;
 }
 
+static int read_tracing_file_by_line(const char *name,
+                                    void (*cb)(char *str, void *arg),
+                                    void *cb_arg)
+{
+       char *line = NULL;
+       size_t len = 0;
+       char *file;
+       FILE *fp;
+
+       file = get_tracing_file(name);
+       if (!file) {
+               pr_debug("cannot get tracing file: %s\n", name);
+               return -1;
+       }
+
+       fp = fopen(file, "r");
+       if (fp == NULL) {
+               pr_debug("cannot open tracing file: %s\n", name);
+               put_tracing_file(file);
+               return -1;
+       }
+
+       while (getline(&line, &len, fp) != -1) {
+               cb(line, cb_arg);
+       }
+
+       if (line)
+               free(line);
+
+       fclose(fp);
+       put_tracing_file(file);
+       return 0;
+}
+
 static int write_tracing_file_int(const char *name, int value)
 {
        char buf[16];
@@ -557,9 +591,6 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
        signal(SIGCHLD, sig_handler);
        signal(SIGPIPE, sig_handler);
 
-       if (ftrace->list_avail_functions)
-               return read_tracing_file_to_stdout("available_filter_functions");
-
        if (reset_tracing_files(ftrace) < 0) {
                pr_err("failed to reset ftrace\n");
                goto out;
@@ -683,6 +714,46 @@ static int perf_ftrace_config(const char *var, const char *value, void *cb)
        return -1;
 }
 
+static void list_function_cb(char *str, void *arg)
+{
+       struct strfilter *filter = (struct strfilter *)arg;
+
+       if (strfilter__compare(filter, str))
+               printf("%s", str);
+}
+
+static int opt_list_avail_functions(const struct option *opt __maybe_unused,
+                                   const char *str, int unset)
+{
+       struct strfilter *filter;
+       const char *err = NULL;
+       int ret;
+
+       if (unset || !str)
+               return -1;
+
+       filter = strfilter__new(str, &err);
+       if (!filter)
+               return err ? -EINVAL : -ENOMEM;
+
+       ret = strfilter__or(filter, str, &err);
+       if (ret == -EINVAL) {
+               pr_err("Filter parse error at %td.\n", err - str + 1);
+               pr_err("Source: \"%s\"\n", str);
+               pr_err("         %*c\n", (int)(err - str + 1), '^');
+               strfilter__delete(filter);
+               return ret;
+       }
+
+       ret = read_tracing_file_by_line("available_filter_functions",
+                                       list_function_cb, filter);
+       strfilter__delete(filter);
+       if (ret < 0)
+               return ret;
+
+       exit(0);
+}
+
 static int parse_filter_func(const struct option *opt, const char *str,
                             int unset __maybe_unused)
 {
@@ -817,8 +888,9 @@ int cmd_ftrace(int argc, const char **argv)
        const struct option ftrace_options[] = {
        OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
                   "Tracer to use: function_graph(default) or function"),
-       OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
-                   "Show available functions to filter"),
+       OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
+                            "Show available functions to filter",
+                            opt_list_avail_functions, "*"),
        OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
                   "Trace on existing process id"),
        /* TODO: Add short option -t after -t/--tracer can be removed. */