ca61f8b8bbf627dad149d0c4236cec8db5a8c4a1
[linux-2.6-microblaze.git] / tools / perf / builtin-ftrace.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * builtin-ftrace.c
4  *
5  * Copyright (c) 2013  LG Electronics,  Namhyung Kim <namhyung@kernel.org>
6  */
7
8 #include "builtin.h"
9
10 #include <errno.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <poll.h>
16 #include <linux/capability.h>
17 #include <linux/string.h>
18
19 #include "debug.h"
20 #include <subcmd/pager.h>
21 #include <subcmd/parse-options.h>
22 #include <api/fs/tracing_path.h>
23 #include "evlist.h"
24 #include "target.h"
25 #include "cpumap.h"
26 #include "thread_map.h"
27 #include "util/cap.h"
28 #include "util/config.h"
29 #include "util/units.h"
30 #include "util/parse-sublevel-options.h"
31
32 #define DEFAULT_TRACER  "function_graph"
33
34 struct perf_ftrace {
35         struct evlist           *evlist;
36         struct target           target;
37         const char              *tracer;
38         bool                    list_avail_functions;
39         struct list_head        filters;
40         struct list_head        notrace;
41         struct list_head        graph_funcs;
42         struct list_head        nograph_funcs;
43         int                     graph_depth;
44         unsigned long           percpu_buffer_size;
45         bool                    inherit;
46         int                     func_stack_trace;
47         int                     func_irq_info;
48         int                     graph_nosleep_time;
49         int                     graph_noirqs;
50         int                     graph_verbose;
51         int                     graph_thresh;
52 };
53
54 struct filter_entry {
55         struct list_head        list;
56         char                    name[];
57 };
58
59 static volatile int workload_exec_errno;
60 static bool done;
61
62 static void sig_handler(int sig __maybe_unused)
63 {
64         done = true;
65 }
66
67 /*
68  * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
69  * we asked by setting its exec_error to the function below,
70  * ftrace__workload_exec_failed_signal.
71  *
72  * XXX We need to handle this more appropriately, emitting an error, etc.
73  */
74 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
75                                                 siginfo_t *info __maybe_unused,
76                                                 void *ucontext __maybe_unused)
77 {
78         workload_exec_errno = info->si_value.sival_int;
79         done = true;
80 }
81
82 static int __write_tracing_file(const char *name, const char *val, bool append)
83 {
84         char *file;
85         int fd, ret = -1;
86         ssize_t size = strlen(val);
87         int flags = O_WRONLY;
88         char errbuf[512];
89         char *val_copy;
90
91         file = get_tracing_file(name);
92         if (!file) {
93                 pr_debug("cannot get tracing file: %s\n", name);
94                 return -1;
95         }
96
97         if (append)
98                 flags |= O_APPEND;
99         else
100                 flags |= O_TRUNC;
101
102         fd = open(file, flags);
103         if (fd < 0) {
104                 pr_debug("cannot open tracing file: %s: %s\n",
105                          name, str_error_r(errno, errbuf, sizeof(errbuf)));
106                 goto out;
107         }
108
109         /*
110          * Copy the original value and append a '\n'. Without this,
111          * the kernel can hide possible errors.
112          */
113         val_copy = strdup(val);
114         if (!val_copy)
115                 goto out_close;
116         val_copy[size] = '\n';
117
118         if (write(fd, val_copy, size + 1) == size + 1)
119                 ret = 0;
120         else
121                 pr_debug("write '%s' to tracing/%s failed: %s\n",
122                          val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
123
124         free(val_copy);
125 out_close:
126         close(fd);
127 out:
128         put_tracing_file(file);
129         return ret;
130 }
131
132 static int write_tracing_file(const char *name, const char *val)
133 {
134         return __write_tracing_file(name, val, false);
135 }
136
137 static int append_tracing_file(const char *name, const char *val)
138 {
139         return __write_tracing_file(name, val, true);
140 }
141
142 static int read_tracing_file_to_stdout(const char *name)
143 {
144         char buf[4096];
145         char *file;
146         int fd;
147         int ret = -1;
148
149         file = get_tracing_file(name);
150         if (!file) {
151                 pr_debug("cannot get tracing file: %s\n", name);
152                 return -1;
153         }
154
155         fd = open(file, O_RDONLY);
156         if (fd < 0) {
157                 pr_debug("cannot open tracing file: %s: %s\n",
158                          name, str_error_r(errno, buf, sizeof(buf)));
159                 goto out;
160         }
161
162         /* read contents to stdout */
163         while (true) {
164                 int n = read(fd, buf, sizeof(buf));
165                 if (n == 0)
166                         break;
167                 else if (n < 0)
168                         goto out_close;
169
170                 if (fwrite(buf, n, 1, stdout) != 1)
171                         goto out_close;
172         }
173         ret = 0;
174
175 out_close:
176         close(fd);
177 out:
178         put_tracing_file(file);
179         return ret;
180 }
181
182 static int write_tracing_file_int(const char *name, int value)
183 {
184         char buf[16];
185
186         snprintf(buf, sizeof(buf), "%d", value);
187         if (write_tracing_file(name, buf) < 0)
188                 return -1;
189
190         return 0;
191 }
192
193 static int write_tracing_option_file(const char *name, const char *val)
194 {
195         char *file;
196         int ret;
197
198         if (asprintf(&file, "options/%s", name) < 0)
199                 return -1;
200
201         ret = __write_tracing_file(file, val, false);
202         free(file);
203         return ret;
204 }
205
206 static int reset_tracing_cpu(void);
207 static void reset_tracing_filters(void);
208
209 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
210 {
211         write_tracing_option_file("function-fork", "0");
212         write_tracing_option_file("func_stack_trace", "0");
213         write_tracing_option_file("sleep-time", "1");
214         write_tracing_option_file("funcgraph-irqs", "1");
215         write_tracing_option_file("funcgraph-proc", "0");
216         write_tracing_option_file("funcgraph-abstime", "0");
217         write_tracing_option_file("latency-format", "0");
218         write_tracing_option_file("irq-info", "0");
219 }
220
221 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
222 {
223         if (write_tracing_file("tracing_on", "0") < 0)
224                 return -1;
225
226         if (write_tracing_file("current_tracer", "nop") < 0)
227                 return -1;
228
229         if (write_tracing_file("set_ftrace_pid", " ") < 0)
230                 return -1;
231
232         if (reset_tracing_cpu() < 0)
233                 return -1;
234
235         if (write_tracing_file("max_graph_depth", "0") < 0)
236                 return -1;
237
238         if (write_tracing_file("tracing_thresh", "0") < 0)
239                 return -1;
240
241         reset_tracing_filters();
242         reset_tracing_options(ftrace);
243         return 0;
244 }
245
246 static int set_tracing_pid(struct perf_ftrace *ftrace)
247 {
248         int i;
249         char buf[16];
250
251         if (target__has_cpu(&ftrace->target))
252                 return 0;
253
254         for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
255                 scnprintf(buf, sizeof(buf), "%d",
256                           ftrace->evlist->core.threads->map[i]);
257                 if (append_tracing_file("set_ftrace_pid", buf) < 0)
258                         return -1;
259         }
260         return 0;
261 }
262
263 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
264 {
265         char *cpumask;
266         size_t mask_size;
267         int ret;
268         int last_cpu;
269
270         last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
271         mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
272         mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
273
274         cpumask = malloc(mask_size);
275         if (cpumask == NULL) {
276                 pr_debug("failed to allocate cpu mask\n");
277                 return -1;
278         }
279
280         cpu_map__snprint_mask(cpumap, cpumask, mask_size);
281
282         ret = write_tracing_file("tracing_cpumask", cpumask);
283
284         free(cpumask);
285         return ret;
286 }
287
288 static int set_tracing_cpu(struct perf_ftrace *ftrace)
289 {
290         struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
291
292         if (!target__has_cpu(&ftrace->target))
293                 return 0;
294
295         return set_tracing_cpumask(cpumap);
296 }
297
298 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
299 {
300         if (!ftrace->func_stack_trace)
301                 return 0;
302
303         if (write_tracing_option_file("func_stack_trace", "1") < 0)
304                 return -1;
305
306         return 0;
307 }
308
309 static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
310 {
311         if (!ftrace->func_irq_info)
312                 return 0;
313
314         if (write_tracing_option_file("irq-info", "1") < 0)
315                 return -1;
316
317         return 0;
318 }
319
320 static int reset_tracing_cpu(void)
321 {
322         struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
323         int ret;
324
325         ret = set_tracing_cpumask(cpumap);
326         perf_cpu_map__put(cpumap);
327         return ret;
328 }
329
330 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
331 {
332         struct filter_entry *pos;
333
334         list_for_each_entry(pos, funcs, list) {
335                 if (append_tracing_file(filter_file, pos->name) < 0)
336                         return -1;
337         }
338
339         return 0;
340 }
341
342 static int set_tracing_filters(struct perf_ftrace *ftrace)
343 {
344         int ret;
345
346         ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
347         if (ret < 0)
348                 return ret;
349
350         ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
351         if (ret < 0)
352                 return ret;
353
354         ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
355         if (ret < 0)
356                 return ret;
357
358         /* old kernels do not have this filter */
359         __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
360
361         return ret;
362 }
363
364 static void reset_tracing_filters(void)
365 {
366         write_tracing_file("set_ftrace_filter", " ");
367         write_tracing_file("set_ftrace_notrace", " ");
368         write_tracing_file("set_graph_function", " ");
369         write_tracing_file("set_graph_notrace", " ");
370 }
371
372 static int set_tracing_depth(struct perf_ftrace *ftrace)
373 {
374         if (ftrace->graph_depth == 0)
375                 return 0;
376
377         if (ftrace->graph_depth < 0) {
378                 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
379                 return -1;
380         }
381
382         if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
383                 return -1;
384
385         return 0;
386 }
387
388 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
389 {
390         int ret;
391
392         if (ftrace->percpu_buffer_size == 0)
393                 return 0;
394
395         ret = write_tracing_file_int("buffer_size_kb",
396                                      ftrace->percpu_buffer_size / 1024);
397         if (ret < 0)
398                 return ret;
399
400         return 0;
401 }
402
403 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
404 {
405         if (!ftrace->inherit)
406                 return 0;
407
408         if (write_tracing_option_file("function-fork", "1") < 0)
409                 return -1;
410
411         return 0;
412 }
413
414 static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
415 {
416         if (!ftrace->graph_nosleep_time)
417                 return 0;
418
419         if (write_tracing_option_file("sleep-time", "0") < 0)
420                 return -1;
421
422         return 0;
423 }
424
425 static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
426 {
427         if (!ftrace->graph_noirqs)
428                 return 0;
429
430         if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
431                 return -1;
432
433         return 0;
434 }
435
436 static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
437 {
438         if (!ftrace->graph_verbose)
439                 return 0;
440
441         if (write_tracing_option_file("funcgraph-proc", "1") < 0)
442                 return -1;
443
444         if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
445                 return -1;
446
447         if (write_tracing_option_file("latency-format", "1") < 0)
448                 return -1;
449
450         return 0;
451 }
452
453 static int set_tracing_thresh(struct perf_ftrace *ftrace)
454 {
455         int ret;
456
457         if (ftrace->graph_thresh == 0)
458                 return 0;
459
460         ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
461         if (ret < 0)
462                 return ret;
463
464         return 0;
465 }
466
467 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
468 {
469         char *trace_file;
470         int trace_fd;
471         char buf[4096];
472         struct pollfd pollfd = {
473                 .events = POLLIN,
474         };
475
476         if (!(perf_cap__capable(CAP_PERFMON) ||
477               perf_cap__capable(CAP_SYS_ADMIN))) {
478                 pr_err("ftrace only works for %s!\n",
479 #ifdef HAVE_LIBCAP_SUPPORT
480                 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
481 #else
482                 "root"
483 #endif
484                 );
485                 return -1;
486         }
487
488         signal(SIGINT, sig_handler);
489         signal(SIGUSR1, sig_handler);
490         signal(SIGCHLD, sig_handler);
491         signal(SIGPIPE, sig_handler);
492
493         if (ftrace->list_avail_functions)
494                 return read_tracing_file_to_stdout("available_filter_functions");
495
496         if (reset_tracing_files(ftrace) < 0) {
497                 pr_err("failed to reset ftrace\n");
498                 goto out;
499         }
500
501         /* reset ftrace buffer */
502         if (write_tracing_file("trace", "0") < 0)
503                 goto out;
504
505         if (argc && perf_evlist__prepare_workload(ftrace->evlist,
506                                 &ftrace->target, argv, false,
507                                 ftrace__workload_exec_failed_signal) < 0) {
508                 goto out;
509         }
510
511         if (set_tracing_pid(ftrace) < 0) {
512                 pr_err("failed to set ftrace pid\n");
513                 goto out_reset;
514         }
515
516         if (set_tracing_cpu(ftrace) < 0) {
517                 pr_err("failed to set tracing cpumask\n");
518                 goto out_reset;
519         }
520
521         if (set_tracing_func_stack_trace(ftrace) < 0) {
522                 pr_err("failed to set tracing option func_stack_trace\n");
523                 goto out_reset;
524         }
525
526         if (set_tracing_func_irqinfo(ftrace) < 0) {
527                 pr_err("failed to set tracing option irq-info\n");
528                 goto out_reset;
529         }
530
531         if (set_tracing_filters(ftrace) < 0) {
532                 pr_err("failed to set tracing filters\n");
533                 goto out_reset;
534         }
535
536         if (set_tracing_depth(ftrace) < 0) {
537                 pr_err("failed to set graph depth\n");
538                 goto out_reset;
539         }
540
541         if (set_tracing_percpu_buffer_size(ftrace) < 0) {
542                 pr_err("failed to set tracing per-cpu buffer size\n");
543                 goto out_reset;
544         }
545
546         if (set_tracing_trace_inherit(ftrace) < 0) {
547                 pr_err("failed to set tracing option function-fork\n");
548                 goto out_reset;
549         }
550
551         if (set_tracing_sleep_time(ftrace) < 0) {
552                 pr_err("failed to set tracing option sleep-time\n");
553                 goto out_reset;
554         }
555
556         if (set_tracing_funcgraph_irqs(ftrace) < 0) {
557                 pr_err("failed to set tracing option funcgraph-irqs\n");
558                 goto out_reset;
559         }
560
561         if (set_tracing_funcgraph_verbose(ftrace) < 0) {
562                 pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
563                 goto out_reset;
564         }
565
566         if (set_tracing_thresh(ftrace) < 0) {
567                 pr_err("failed to set tracing thresh\n");
568                 goto out_reset;
569         }
570
571         if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
572                 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
573                 goto out_reset;
574         }
575
576         setup_pager();
577
578         trace_file = get_tracing_file("trace_pipe");
579         if (!trace_file) {
580                 pr_err("failed to open trace_pipe\n");
581                 goto out_reset;
582         }
583
584         trace_fd = open(trace_file, O_RDONLY);
585
586         put_tracing_file(trace_file);
587
588         if (trace_fd < 0) {
589                 pr_err("failed to open trace_pipe\n");
590                 goto out_reset;
591         }
592
593         fcntl(trace_fd, F_SETFL, O_NONBLOCK);
594         pollfd.fd = trace_fd;
595
596         /* display column headers */
597         read_tracing_file_to_stdout("trace");
598
599         if (write_tracing_file("tracing_on", "1") < 0) {
600                 pr_err("can't enable tracing\n");
601                 goto out_close_fd;
602         }
603
604         perf_evlist__start_workload(ftrace->evlist);
605
606         while (!done) {
607                 if (poll(&pollfd, 1, -1) < 0)
608                         break;
609
610                 if (pollfd.revents & POLLIN) {
611                         int n = read(trace_fd, buf, sizeof(buf));
612                         if (n < 0)
613                                 break;
614                         if (fwrite(buf, n, 1, stdout) != 1)
615                                 break;
616                 }
617         }
618
619         write_tracing_file("tracing_on", "0");
620
621         if (workload_exec_errno) {
622                 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
623                 /* flush stdout first so below error msg appears at the end. */
624                 fflush(stdout);
625                 pr_err("workload failed: %s\n", emsg);
626                 goto out_close_fd;
627         }
628
629         /* read remaining buffer contents */
630         while (true) {
631                 int n = read(trace_fd, buf, sizeof(buf));
632                 if (n <= 0)
633                         break;
634                 if (fwrite(buf, n, 1, stdout) != 1)
635                         break;
636         }
637
638 out_close_fd:
639         close(trace_fd);
640 out_reset:
641         reset_tracing_files(ftrace);
642 out:
643         return (done && !workload_exec_errno) ? 0 : -1;
644 }
645
646 static int perf_ftrace_config(const char *var, const char *value, void *cb)
647 {
648         struct perf_ftrace *ftrace = cb;
649
650         if (!strstarts(var, "ftrace."))
651                 return 0;
652
653         if (strcmp(var, "ftrace.tracer"))
654                 return -1;
655
656         if (!strcmp(value, "function_graph") ||
657             !strcmp(value, "function")) {
658                 ftrace->tracer = value;
659                 return 0;
660         }
661
662         pr_err("Please select \"function_graph\" (default) or \"function\"\n");
663         return -1;
664 }
665
666 static int parse_filter_func(const struct option *opt, const char *str,
667                              int unset __maybe_unused)
668 {
669         struct list_head *head = opt->value;
670         struct filter_entry *entry;
671
672         entry = malloc(sizeof(*entry) + strlen(str) + 1);
673         if (entry == NULL)
674                 return -ENOMEM;
675
676         strcpy(entry->name, str);
677         list_add_tail(&entry->list, head);
678
679         return 0;
680 }
681
682 static void delete_filter_func(struct list_head *head)
683 {
684         struct filter_entry *pos, *tmp;
685
686         list_for_each_entry_safe(pos, tmp, head, list) {
687                 list_del_init(&pos->list);
688                 free(pos);
689         }
690 }
691
692 static int parse_buffer_size(const struct option *opt,
693                              const char *str, int unset)
694 {
695         unsigned long *s = (unsigned long *)opt->value;
696         static struct parse_tag tags_size[] = {
697                 { .tag  = 'B', .mult = 1       },
698                 { .tag  = 'K', .mult = 1 << 10 },
699                 { .tag  = 'M', .mult = 1 << 20 },
700                 { .tag  = 'G', .mult = 1 << 30 },
701                 { .tag  = 0 },
702         };
703         unsigned long val;
704
705         if (unset) {
706                 *s = 0;
707                 return 0;
708         }
709
710         val = parse_tag_value(str, tags_size);
711         if (val != (unsigned long) -1) {
712                 if (val < 1024) {
713                         pr_err("buffer size too small, must larger than 1KB.");
714                         return -1;
715                 }
716                 *s = val;
717                 return 0;
718         }
719
720         return -1;
721 }
722
723 static int parse_func_tracer_opts(const struct option *opt,
724                                   const char *str, int unset)
725 {
726         int ret;
727         struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
728         struct sublevel_option func_tracer_opts[] = {
729                 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
730                 { .name = "irq-info",   .value_ptr = &ftrace->func_irq_info },
731                 { .name = NULL, }
732         };
733
734         if (unset)
735                 return 0;
736
737         ret = perf_parse_sublevel_options(str, func_tracer_opts);
738         if (ret)
739                 return ret;
740
741         return 0;
742 }
743
744 static int parse_graph_tracer_opts(const struct option *opt,
745                                   const char *str, int unset)
746 {
747         int ret;
748         struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
749         struct sublevel_option graph_tracer_opts[] = {
750                 { .name = "nosleep-time",       .value_ptr = &ftrace->graph_nosleep_time },
751                 { .name = "noirqs",             .value_ptr = &ftrace->graph_noirqs },
752                 { .name = "verbose",            .value_ptr = &ftrace->graph_verbose },
753                 { .name = "thresh",             .value_ptr = &ftrace->graph_thresh },
754                 { .name = NULL, }
755         };
756
757         if (unset)
758                 return 0;
759
760         ret = perf_parse_sublevel_options(str, graph_tracer_opts);
761         if (ret)
762                 return ret;
763
764         return 0;
765 }
766
767 static void select_tracer(struct perf_ftrace *ftrace)
768 {
769         bool graph = !list_empty(&ftrace->graph_funcs) ||
770                      !list_empty(&ftrace->nograph_funcs);
771         bool func = !list_empty(&ftrace->filters) ||
772                     !list_empty(&ftrace->notrace);
773
774         /* The function_graph has priority over function tracer. */
775         if (graph)
776                 ftrace->tracer = "function_graph";
777         else if (func)
778                 ftrace->tracer = "function";
779         /* Otherwise, the default tracer is used. */
780
781         pr_debug("%s tracer is used\n", ftrace->tracer);
782 }
783
784 int cmd_ftrace(int argc, const char **argv)
785 {
786         int ret;
787         struct perf_ftrace ftrace = {
788                 .tracer = DEFAULT_TRACER,
789                 .target = { .uid = UINT_MAX, },
790         };
791         const char * const ftrace_usage[] = {
792                 "perf ftrace [<options>] [<command>]",
793                 "perf ftrace [<options>] -- <command> [<options>]",
794                 NULL
795         };
796         const struct option ftrace_options[] = {
797         OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
798                    "tracer to use: function_graph(default) or function"),
799         OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
800                     "Show available functions to filter"),
801         OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
802                    "trace on existing process id"),
803         OPT_INCR('v', "verbose", &verbose,
804                  "be more verbose"),
805         OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
806                     "system-wide collection from all CPUs"),
807         OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
808                     "list of cpus to monitor"),
809         OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
810                      "trace given functions using function tracer",
811                      parse_filter_func),
812         OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
813                      "do not trace given functions", parse_filter_func),
814         OPT_CALLBACK(0, "func-opts", &ftrace, "options",
815                      "function tracer options, available options: call-graph,irq-info",
816                      parse_func_tracer_opts),
817         OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
818                      "trace given functions using function_graph tracer",
819                      parse_filter_func),
820         OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
821                      "Set nograph filter on given functions", parse_filter_func),
822         OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
823                     "Max depth for function graph tracer"),
824         OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
825                      "graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>",
826                      parse_graph_tracer_opts),
827         OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
828                      "size of per cpu buffer", parse_buffer_size),
829         OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
830                     "trace children processes"),
831         OPT_END()
832         };
833
834         INIT_LIST_HEAD(&ftrace.filters);
835         INIT_LIST_HEAD(&ftrace.notrace);
836         INIT_LIST_HEAD(&ftrace.graph_funcs);
837         INIT_LIST_HEAD(&ftrace.nograph_funcs);
838
839         ret = perf_config(perf_ftrace_config, &ftrace);
840         if (ret < 0)
841                 return -1;
842
843         argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
844                             PARSE_OPT_STOP_AT_NON_OPTION);
845         if (!argc && target__none(&ftrace.target))
846                 ftrace.target.system_wide = true;
847
848         select_tracer(&ftrace);
849
850         ret = target__validate(&ftrace.target);
851         if (ret) {
852                 char errbuf[512];
853
854                 target__strerror(&ftrace.target, ret, errbuf, 512);
855                 pr_err("%s\n", errbuf);
856                 goto out_delete_filters;
857         }
858
859         ftrace.evlist = evlist__new();
860         if (ftrace.evlist == NULL) {
861                 ret = -ENOMEM;
862                 goto out_delete_filters;
863         }
864
865         ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
866         if (ret < 0)
867                 goto out_delete_evlist;
868
869         ret = __cmd_ftrace(&ftrace, argc, argv);
870
871 out_delete_evlist:
872         evlist__delete(ftrace.evlist);
873
874 out_delete_filters:
875         delete_filter_func(&ftrace.filters);
876         delete_filter_func(&ftrace.notrace);
877         delete_filter_func(&ftrace.graph_funcs);
878         delete_filter_func(&ftrace.nograph_funcs);
879
880         return ret;
881 }