perf ftrace: Factor out function write_tracing_file_int()
[linux-2.6-microblaze.git] / tools / perf / builtin-ftrace.c
index 2bfc1b0..4b3fcee 100644 (file)
@@ -33,6 +33,7 @@ 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;
@@ -128,6 +129,57 @@ static int append_tracing_file(const char *name, const char *val)
        return __write_tracing_file(name, val, true);
 }
 
+static int read_tracing_file_to_stdout(const char *name)
+{
+       char buf[4096];
+       char *file;
+       int fd;
+       int ret = -1;
+
+       file = get_tracing_file(name);
+       if (!file) {
+               pr_debug("cannot get tracing file: %s\n", name);
+               return -1;
+       }
+
+       fd = open(file, O_RDONLY);
+       if (fd < 0) {
+               pr_debug("cannot open tracing file: %s: %s\n",
+                        name, str_error_r(errno, buf, sizeof(buf)));
+               goto out;
+       }
+
+       /* read contents to stdout */
+       while (true) {
+               int n = read(fd, buf, sizeof(buf));
+               if (n == 0)
+                       break;
+               else if (n < 0)
+                       goto out_close;
+
+               if (fwrite(buf, n, 1, stdout) != 1)
+                       goto out_close;
+       }
+       ret = 0;
+
+out_close:
+       close(fd);
+out:
+       put_tracing_file(file);
+       return ret;
+}
+
+static int write_tracing_file_int(const char *name, int value)
+{
+       char buf[16];
+
+       snprintf(buf, sizeof(buf), "%d", value);
+       if (write_tracing_file(name, buf) < 0)
+               return -1;
+
+       return 0;
+}
+
 static int reset_tracing_cpu(void);
 static void reset_tracing_filters(void);
 
@@ -258,8 +310,6 @@ static void reset_tracing_filters(void)
 
 static int set_tracing_depth(struct perf_ftrace *ftrace)
 {
-       char buf[16];
-
        if (ftrace->graph_depth == 0)
                return 0;
 
@@ -268,9 +318,7 @@ static int set_tracing_depth(struct perf_ftrace *ftrace)
                return -1;
        }
 
-       snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
-
-       if (write_tracing_file("max_graph_depth", buf) < 0)
+       if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
                return -1;
 
        return 0;
@@ -302,6 +350,9 @@ 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;
@@ -455,6 +506,23 @@ static void delete_filter_func(struct list_head *head)
        }
 }
 
+static void select_tracer(struct perf_ftrace *ftrace)
+{
+       bool graph = !list_empty(&ftrace->graph_funcs) ||
+                    !list_empty(&ftrace->nograph_funcs);
+       bool func = !list_empty(&ftrace->filters) ||
+                   !list_empty(&ftrace->notrace);
+
+       /* The function_graph has priority over function tracer. */
+       if (graph)
+               ftrace->tracer = "function_graph";
+       else if (func)
+               ftrace->tracer = "function";
+       /* Otherwise, the default tracer is used. */
+
+       pr_debug("%s tracer is used\n", ftrace->tracer);
+}
+
 int cmd_ftrace(int argc, const char **argv)
 {
        int ret;
@@ -470,6 +538,8 @@ 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_STRING('p', "pid", &ftrace.target.pid, "pid",
                   "trace on existing process id"),
        OPT_INCR('v', "verbose", &verbose,
@@ -479,11 +549,13 @@ int cmd_ftrace(int argc, const char **argv)
        OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
                    "list of cpus to monitor"),
        OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
-                    "trace given functions only", parse_filter_func),
+                    "trace given functions using function tracer",
+                    parse_filter_func),
        OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
                     "do not trace given functions", parse_filter_func),
        OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
-                    "Set graph filter on given functions", parse_filter_func),
+                    "trace given functions using function_graph tracer",
+                    parse_filter_func),
        OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
                     "Set nograph filter on given functions", parse_filter_func),
        OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
@@ -505,6 +577,8 @@ int cmd_ftrace(int argc, const char **argv)
        if (!argc && target__none(&ftrace.target))
                ftrace.target.system_wide = true;
 
+       select_tracer(&ftrace);
+
        ret = target__validate(&ftrace.target);
        if (ret) {
                char errbuf[512];