perf ftrace: Add option '-F/--funcs' to list available functions
[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
30 #define DEFAULT_TRACER  "function_graph"
31
32 struct perf_ftrace {
33         struct evlist           *evlist;
34         struct target           target;
35         const char              *tracer;
36         bool                    list_avail_functions;
37         struct list_head        filters;
38         struct list_head        notrace;
39         struct list_head        graph_funcs;
40         struct list_head        nograph_funcs;
41         int                     graph_depth;
42 };
43
44 struct filter_entry {
45         struct list_head        list;
46         char                    name[];
47 };
48
49 static volatile int workload_exec_errno;
50 static bool done;
51
52 static void sig_handler(int sig __maybe_unused)
53 {
54         done = true;
55 }
56
57 /*
58  * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
59  * we asked by setting its exec_error to the function below,
60  * ftrace__workload_exec_failed_signal.
61  *
62  * XXX We need to handle this more appropriately, emitting an error, etc.
63  */
64 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
65                                                 siginfo_t *info __maybe_unused,
66                                                 void *ucontext __maybe_unused)
67 {
68         workload_exec_errno = info->si_value.sival_int;
69         done = true;
70 }
71
72 static int __write_tracing_file(const char *name, const char *val, bool append)
73 {
74         char *file;
75         int fd, ret = -1;
76         ssize_t size = strlen(val);
77         int flags = O_WRONLY;
78         char errbuf[512];
79         char *val_copy;
80
81         file = get_tracing_file(name);
82         if (!file) {
83                 pr_debug("cannot get tracing file: %s\n", name);
84                 return -1;
85         }
86
87         if (append)
88                 flags |= O_APPEND;
89         else
90                 flags |= O_TRUNC;
91
92         fd = open(file, flags);
93         if (fd < 0) {
94                 pr_debug("cannot open tracing file: %s: %s\n",
95                          name, str_error_r(errno, errbuf, sizeof(errbuf)));
96                 goto out;
97         }
98
99         /*
100          * Copy the original value and append a '\n'. Without this,
101          * the kernel can hide possible errors.
102          */
103         val_copy = strdup(val);
104         if (!val_copy)
105                 goto out_close;
106         val_copy[size] = '\n';
107
108         if (write(fd, val_copy, size + 1) == size + 1)
109                 ret = 0;
110         else
111                 pr_debug("write '%s' to tracing/%s failed: %s\n",
112                          val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
113
114         free(val_copy);
115 out_close:
116         close(fd);
117 out:
118         put_tracing_file(file);
119         return ret;
120 }
121
122 static int write_tracing_file(const char *name, const char *val)
123 {
124         return __write_tracing_file(name, val, false);
125 }
126
127 static int append_tracing_file(const char *name, const char *val)
128 {
129         return __write_tracing_file(name, val, true);
130 }
131
132 static int read_tracing_file_to_stdout(const char *name)
133 {
134         char buf[4096];
135         char *file;
136         int fd;
137         int ret = -1;
138
139         file = get_tracing_file(name);
140         if (!file) {
141                 pr_debug("cannot get tracing file: %s\n", name);
142                 return -1;
143         }
144
145         fd = open(file, O_RDONLY);
146         if (fd < 0) {
147                 pr_debug("cannot open tracing file: %s: %s\n",
148                          name, str_error_r(errno, buf, sizeof(buf)));
149                 goto out;
150         }
151
152         /* read contents to stdout */
153         while (true) {
154                 int n = read(fd, buf, sizeof(buf));
155                 if (n == 0)
156                         break;
157                 else if (n < 0)
158                         goto out_close;
159
160                 if (fwrite(buf, n, 1, stdout) != 1)
161                         goto out_close;
162         }
163         ret = 0;
164
165 out_close:
166         close(fd);
167 out:
168         put_tracing_file(file);
169         return ret;
170 }
171
172 static int reset_tracing_cpu(void);
173 static void reset_tracing_filters(void);
174
175 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
176 {
177         if (write_tracing_file("tracing_on", "0") < 0)
178                 return -1;
179
180         if (write_tracing_file("current_tracer", "nop") < 0)
181                 return -1;
182
183         if (write_tracing_file("set_ftrace_pid", " ") < 0)
184                 return -1;
185
186         if (reset_tracing_cpu() < 0)
187                 return -1;
188
189         if (write_tracing_file("max_graph_depth", "0") < 0)
190                 return -1;
191
192         reset_tracing_filters();
193         return 0;
194 }
195
196 static int set_tracing_pid(struct perf_ftrace *ftrace)
197 {
198         int i;
199         char buf[16];
200
201         if (target__has_cpu(&ftrace->target))
202                 return 0;
203
204         for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
205                 scnprintf(buf, sizeof(buf), "%d",
206                           ftrace->evlist->core.threads->map[i]);
207                 if (append_tracing_file("set_ftrace_pid", buf) < 0)
208                         return -1;
209         }
210         return 0;
211 }
212
213 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
214 {
215         char *cpumask;
216         size_t mask_size;
217         int ret;
218         int last_cpu;
219
220         last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
221         mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
222         mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
223
224         cpumask = malloc(mask_size);
225         if (cpumask == NULL) {
226                 pr_debug("failed to allocate cpu mask\n");
227                 return -1;
228         }
229
230         cpu_map__snprint_mask(cpumap, cpumask, mask_size);
231
232         ret = write_tracing_file("tracing_cpumask", cpumask);
233
234         free(cpumask);
235         return ret;
236 }
237
238 static int set_tracing_cpu(struct perf_ftrace *ftrace)
239 {
240         struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
241
242         if (!target__has_cpu(&ftrace->target))
243                 return 0;
244
245         return set_tracing_cpumask(cpumap);
246 }
247
248 static int reset_tracing_cpu(void)
249 {
250         struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
251         int ret;
252
253         ret = set_tracing_cpumask(cpumap);
254         perf_cpu_map__put(cpumap);
255         return ret;
256 }
257
258 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
259 {
260         struct filter_entry *pos;
261
262         list_for_each_entry(pos, funcs, list) {
263                 if (append_tracing_file(filter_file, pos->name) < 0)
264                         return -1;
265         }
266
267         return 0;
268 }
269
270 static int set_tracing_filters(struct perf_ftrace *ftrace)
271 {
272         int ret;
273
274         ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
275         if (ret < 0)
276                 return ret;
277
278         ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
279         if (ret < 0)
280                 return ret;
281
282         ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
283         if (ret < 0)
284                 return ret;
285
286         /* old kernels do not have this filter */
287         __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
288
289         return ret;
290 }
291
292 static void reset_tracing_filters(void)
293 {
294         write_tracing_file("set_ftrace_filter", " ");
295         write_tracing_file("set_ftrace_notrace", " ");
296         write_tracing_file("set_graph_function", " ");
297         write_tracing_file("set_graph_notrace", " ");
298 }
299
300 static int set_tracing_depth(struct perf_ftrace *ftrace)
301 {
302         char buf[16];
303
304         if (ftrace->graph_depth == 0)
305                 return 0;
306
307         if (ftrace->graph_depth < 0) {
308                 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
309                 return -1;
310         }
311
312         snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
313
314         if (write_tracing_file("max_graph_depth", buf) < 0)
315                 return -1;
316
317         return 0;
318 }
319
320 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
321 {
322         char *trace_file;
323         int trace_fd;
324         char buf[4096];
325         struct pollfd pollfd = {
326                 .events = POLLIN,
327         };
328
329         if (!(perf_cap__capable(CAP_PERFMON) ||
330               perf_cap__capable(CAP_SYS_ADMIN))) {
331                 pr_err("ftrace only works for %s!\n",
332 #ifdef HAVE_LIBCAP_SUPPORT
333                 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
334 #else
335                 "root"
336 #endif
337                 );
338                 return -1;
339         }
340
341         signal(SIGINT, sig_handler);
342         signal(SIGUSR1, sig_handler);
343         signal(SIGCHLD, sig_handler);
344         signal(SIGPIPE, sig_handler);
345
346         if (ftrace->list_avail_functions)
347                 return read_tracing_file_to_stdout("available_filter_functions");
348
349         if (reset_tracing_files(ftrace) < 0) {
350                 pr_err("failed to reset ftrace\n");
351                 goto out;
352         }
353
354         /* reset ftrace buffer */
355         if (write_tracing_file("trace", "0") < 0)
356                 goto out;
357
358         if (argc && perf_evlist__prepare_workload(ftrace->evlist,
359                                 &ftrace->target, argv, false,
360                                 ftrace__workload_exec_failed_signal) < 0) {
361                 goto out;
362         }
363
364         if (set_tracing_pid(ftrace) < 0) {
365                 pr_err("failed to set ftrace pid\n");
366                 goto out_reset;
367         }
368
369         if (set_tracing_cpu(ftrace) < 0) {
370                 pr_err("failed to set tracing cpumask\n");
371                 goto out_reset;
372         }
373
374         if (set_tracing_filters(ftrace) < 0) {
375                 pr_err("failed to set tracing filters\n");
376                 goto out_reset;
377         }
378
379         if (set_tracing_depth(ftrace) < 0) {
380                 pr_err("failed to set graph depth\n");
381                 goto out_reset;
382         }
383
384         if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
385                 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
386                 goto out_reset;
387         }
388
389         setup_pager();
390
391         trace_file = get_tracing_file("trace_pipe");
392         if (!trace_file) {
393                 pr_err("failed to open trace_pipe\n");
394                 goto out_reset;
395         }
396
397         trace_fd = open(trace_file, O_RDONLY);
398
399         put_tracing_file(trace_file);
400
401         if (trace_fd < 0) {
402                 pr_err("failed to open trace_pipe\n");
403                 goto out_reset;
404         }
405
406         fcntl(trace_fd, F_SETFL, O_NONBLOCK);
407         pollfd.fd = trace_fd;
408
409         if (write_tracing_file("tracing_on", "1") < 0) {
410                 pr_err("can't enable tracing\n");
411                 goto out_close_fd;
412         }
413
414         perf_evlist__start_workload(ftrace->evlist);
415
416         while (!done) {
417                 if (poll(&pollfd, 1, -1) < 0)
418                         break;
419
420                 if (pollfd.revents & POLLIN) {
421                         int n = read(trace_fd, buf, sizeof(buf));
422                         if (n < 0)
423                                 break;
424                         if (fwrite(buf, n, 1, stdout) != 1)
425                                 break;
426                 }
427         }
428
429         write_tracing_file("tracing_on", "0");
430
431         if (workload_exec_errno) {
432                 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
433                 /* flush stdout first so below error msg appears at the end. */
434                 fflush(stdout);
435                 pr_err("workload failed: %s\n", emsg);
436                 goto out_close_fd;
437         }
438
439         /* read remaining buffer contents */
440         while (true) {
441                 int n = read(trace_fd, buf, sizeof(buf));
442                 if (n <= 0)
443                         break;
444                 if (fwrite(buf, n, 1, stdout) != 1)
445                         break;
446         }
447
448 out_close_fd:
449         close(trace_fd);
450 out_reset:
451         reset_tracing_files(ftrace);
452 out:
453         return (done && !workload_exec_errno) ? 0 : -1;
454 }
455
456 static int perf_ftrace_config(const char *var, const char *value, void *cb)
457 {
458         struct perf_ftrace *ftrace = cb;
459
460         if (!strstarts(var, "ftrace."))
461                 return 0;
462
463         if (strcmp(var, "ftrace.tracer"))
464                 return -1;
465
466         if (!strcmp(value, "function_graph") ||
467             !strcmp(value, "function")) {
468                 ftrace->tracer = value;
469                 return 0;
470         }
471
472         pr_err("Please select \"function_graph\" (default) or \"function\"\n");
473         return -1;
474 }
475
476 static int parse_filter_func(const struct option *opt, const char *str,
477                              int unset __maybe_unused)
478 {
479         struct list_head *head = opt->value;
480         struct filter_entry *entry;
481
482         entry = malloc(sizeof(*entry) + strlen(str) + 1);
483         if (entry == NULL)
484                 return -ENOMEM;
485
486         strcpy(entry->name, str);
487         list_add_tail(&entry->list, head);
488
489         return 0;
490 }
491
492 static void delete_filter_func(struct list_head *head)
493 {
494         struct filter_entry *pos, *tmp;
495
496         list_for_each_entry_safe(pos, tmp, head, list) {
497                 list_del_init(&pos->list);
498                 free(pos);
499         }
500 }
501
502 static void select_tracer(struct perf_ftrace *ftrace)
503 {
504         bool graph = !list_empty(&ftrace->graph_funcs) ||
505                      !list_empty(&ftrace->nograph_funcs);
506         bool func = !list_empty(&ftrace->filters) ||
507                     !list_empty(&ftrace->notrace);
508
509         /* The function_graph has priority over function tracer. */
510         if (graph)
511                 ftrace->tracer = "function_graph";
512         else if (func)
513                 ftrace->tracer = "function";
514         /* Otherwise, the default tracer is used. */
515
516         pr_debug("%s tracer is used\n", ftrace->tracer);
517 }
518
519 int cmd_ftrace(int argc, const char **argv)
520 {
521         int ret;
522         struct perf_ftrace ftrace = {
523                 .tracer = DEFAULT_TRACER,
524                 .target = { .uid = UINT_MAX, },
525         };
526         const char * const ftrace_usage[] = {
527                 "perf ftrace [<options>] [<command>]",
528                 "perf ftrace [<options>] -- <command> [<options>]",
529                 NULL
530         };
531         const struct option ftrace_options[] = {
532         OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
533                    "tracer to use: function_graph(default) or function"),
534         OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
535                     "Show available functions to filter"),
536         OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
537                    "trace on existing process id"),
538         OPT_INCR('v', "verbose", &verbose,
539                  "be more verbose"),
540         OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
541                     "system-wide collection from all CPUs"),
542         OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
543                     "list of cpus to monitor"),
544         OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
545                      "trace given functions using function tracer",
546                      parse_filter_func),
547         OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
548                      "do not trace given functions", parse_filter_func),
549         OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
550                      "trace given functions using function_graph tracer",
551                      parse_filter_func),
552         OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
553                      "Set nograph filter on given functions", parse_filter_func),
554         OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
555                     "Max depth for function graph tracer"),
556         OPT_END()
557         };
558
559         INIT_LIST_HEAD(&ftrace.filters);
560         INIT_LIST_HEAD(&ftrace.notrace);
561         INIT_LIST_HEAD(&ftrace.graph_funcs);
562         INIT_LIST_HEAD(&ftrace.nograph_funcs);
563
564         ret = perf_config(perf_ftrace_config, &ftrace);
565         if (ret < 0)
566                 return -1;
567
568         argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
569                             PARSE_OPT_STOP_AT_NON_OPTION);
570         if (!argc && target__none(&ftrace.target))
571                 ftrace.target.system_wide = true;
572
573         select_tracer(&ftrace);
574
575         ret = target__validate(&ftrace.target);
576         if (ret) {
577                 char errbuf[512];
578
579                 target__strerror(&ftrace.target, ret, errbuf, 512);
580                 pr_err("%s\n", errbuf);
581                 goto out_delete_filters;
582         }
583
584         ftrace.evlist = evlist__new();
585         if (ftrace.evlist == NULL) {
586                 ret = -ENOMEM;
587                 goto out_delete_filters;
588         }
589
590         ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
591         if (ret < 0)
592                 goto out_delete_evlist;
593
594         ret = __cmd_ftrace(&ftrace, argc, argv);
595
596 out_delete_evlist:
597         evlist__delete(ftrace.evlist);
598
599 out_delete_filters:
600         delete_filter_func(&ftrace.filters);
601         delete_filter_func(&ftrace.notrace);
602         delete_filter_func(&ftrace.graph_funcs);
603         delete_filter_func(&ftrace.nograph_funcs);
604
605         return ret;
606 }