1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
16 #include <linux/capability.h>
17 #include <linux/string.h>
20 #include <subcmd/pager.h>
21 #include <subcmd/parse-options.h>
22 #include <api/fs/tracing_path.h>
26 #include "thread_map.h"
28 #include "util/config.h"
30 #define DEFAULT_TRACER "function_graph"
33 struct evlist *evlist;
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;
45 struct list_head list;
49 static volatile int workload_exec_errno;
52 static void sig_handler(int sig __maybe_unused)
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.
62 * XXX We need to handle this more appropriately, emitting an error, etc.
64 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
65 siginfo_t *info __maybe_unused,
66 void *ucontext __maybe_unused)
68 workload_exec_errno = info->si_value.sival_int;
72 static int __write_tracing_file(const char *name, const char *val, bool append)
76 ssize_t size = strlen(val);
81 file = get_tracing_file(name);
83 pr_debug("cannot get tracing file: %s\n", name);
92 fd = open(file, flags);
94 pr_debug("cannot open tracing file: %s: %s\n",
95 name, str_error_r(errno, errbuf, sizeof(errbuf)));
100 * Copy the original value and append a '\n'. Without this,
101 * the kernel can hide possible errors.
103 val_copy = strdup(val);
106 val_copy[size] = '\n';
108 if (write(fd, val_copy, size + 1) == size + 1)
111 pr_debug("write '%s' to tracing/%s failed: %s\n",
112 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
118 put_tracing_file(file);
122 static int write_tracing_file(const char *name, const char *val)
124 return __write_tracing_file(name, val, false);
127 static int append_tracing_file(const char *name, const char *val)
129 return __write_tracing_file(name, val, true);
132 static int read_tracing_file_to_stdout(const char *name)
139 file = get_tracing_file(name);
141 pr_debug("cannot get tracing file: %s\n", name);
145 fd = open(file, O_RDONLY);
147 pr_debug("cannot open tracing file: %s: %s\n",
148 name, str_error_r(errno, buf, sizeof(buf)));
152 /* read contents to stdout */
154 int n = read(fd, buf, sizeof(buf));
160 if (fwrite(buf, n, 1, stdout) != 1)
168 put_tracing_file(file);
172 static int write_tracing_file_int(const char *name, int value)
176 snprintf(buf, sizeof(buf), "%d", value);
177 if (write_tracing_file(name, buf) < 0)
183 static int reset_tracing_cpu(void);
184 static void reset_tracing_filters(void);
186 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
188 if (write_tracing_file("tracing_on", "0") < 0)
191 if (write_tracing_file("current_tracer", "nop") < 0)
194 if (write_tracing_file("set_ftrace_pid", " ") < 0)
197 if (reset_tracing_cpu() < 0)
200 if (write_tracing_file("max_graph_depth", "0") < 0)
203 reset_tracing_filters();
207 static int set_tracing_pid(struct perf_ftrace *ftrace)
212 if (target__has_cpu(&ftrace->target))
215 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
216 scnprintf(buf, sizeof(buf), "%d",
217 ftrace->evlist->core.threads->map[i]);
218 if (append_tracing_file("set_ftrace_pid", buf) < 0)
224 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
231 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
232 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
233 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
235 cpumask = malloc(mask_size);
236 if (cpumask == NULL) {
237 pr_debug("failed to allocate cpu mask\n");
241 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
243 ret = write_tracing_file("tracing_cpumask", cpumask);
249 static int set_tracing_cpu(struct perf_ftrace *ftrace)
251 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
253 if (!target__has_cpu(&ftrace->target))
256 return set_tracing_cpumask(cpumap);
259 static int reset_tracing_cpu(void)
261 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
264 ret = set_tracing_cpumask(cpumap);
265 perf_cpu_map__put(cpumap);
269 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
271 struct filter_entry *pos;
273 list_for_each_entry(pos, funcs, list) {
274 if (append_tracing_file(filter_file, pos->name) < 0)
281 static int set_tracing_filters(struct perf_ftrace *ftrace)
285 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
289 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
293 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
297 /* old kernels do not have this filter */
298 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
303 static void reset_tracing_filters(void)
305 write_tracing_file("set_ftrace_filter", " ");
306 write_tracing_file("set_ftrace_notrace", " ");
307 write_tracing_file("set_graph_function", " ");
308 write_tracing_file("set_graph_notrace", " ");
311 static int set_tracing_depth(struct perf_ftrace *ftrace)
313 if (ftrace->graph_depth == 0)
316 if (ftrace->graph_depth < 0) {
317 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
321 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
327 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
332 struct pollfd pollfd = {
336 if (!(perf_cap__capable(CAP_PERFMON) ||
337 perf_cap__capable(CAP_SYS_ADMIN))) {
338 pr_err("ftrace only works for %s!\n",
339 #ifdef HAVE_LIBCAP_SUPPORT
340 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
348 signal(SIGINT, sig_handler);
349 signal(SIGUSR1, sig_handler);
350 signal(SIGCHLD, sig_handler);
351 signal(SIGPIPE, sig_handler);
353 if (ftrace->list_avail_functions)
354 return read_tracing_file_to_stdout("available_filter_functions");
356 if (reset_tracing_files(ftrace) < 0) {
357 pr_err("failed to reset ftrace\n");
361 /* reset ftrace buffer */
362 if (write_tracing_file("trace", "0") < 0)
365 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
366 &ftrace->target, argv, false,
367 ftrace__workload_exec_failed_signal) < 0) {
371 if (set_tracing_pid(ftrace) < 0) {
372 pr_err("failed to set ftrace pid\n");
376 if (set_tracing_cpu(ftrace) < 0) {
377 pr_err("failed to set tracing cpumask\n");
381 if (set_tracing_filters(ftrace) < 0) {
382 pr_err("failed to set tracing filters\n");
386 if (set_tracing_depth(ftrace) < 0) {
387 pr_err("failed to set graph depth\n");
391 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
392 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
398 trace_file = get_tracing_file("trace_pipe");
400 pr_err("failed to open trace_pipe\n");
404 trace_fd = open(trace_file, O_RDONLY);
406 put_tracing_file(trace_file);
409 pr_err("failed to open trace_pipe\n");
413 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
414 pollfd.fd = trace_fd;
416 if (write_tracing_file("tracing_on", "1") < 0) {
417 pr_err("can't enable tracing\n");
421 perf_evlist__start_workload(ftrace->evlist);
424 if (poll(&pollfd, 1, -1) < 0)
427 if (pollfd.revents & POLLIN) {
428 int n = read(trace_fd, buf, sizeof(buf));
431 if (fwrite(buf, n, 1, stdout) != 1)
436 write_tracing_file("tracing_on", "0");
438 if (workload_exec_errno) {
439 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
440 /* flush stdout first so below error msg appears at the end. */
442 pr_err("workload failed: %s\n", emsg);
446 /* read remaining buffer contents */
448 int n = read(trace_fd, buf, sizeof(buf));
451 if (fwrite(buf, n, 1, stdout) != 1)
458 reset_tracing_files(ftrace);
460 return (done && !workload_exec_errno) ? 0 : -1;
463 static int perf_ftrace_config(const char *var, const char *value, void *cb)
465 struct perf_ftrace *ftrace = cb;
467 if (!strstarts(var, "ftrace."))
470 if (strcmp(var, "ftrace.tracer"))
473 if (!strcmp(value, "function_graph") ||
474 !strcmp(value, "function")) {
475 ftrace->tracer = value;
479 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
483 static int parse_filter_func(const struct option *opt, const char *str,
484 int unset __maybe_unused)
486 struct list_head *head = opt->value;
487 struct filter_entry *entry;
489 entry = malloc(sizeof(*entry) + strlen(str) + 1);
493 strcpy(entry->name, str);
494 list_add_tail(&entry->list, head);
499 static void delete_filter_func(struct list_head *head)
501 struct filter_entry *pos, *tmp;
503 list_for_each_entry_safe(pos, tmp, head, list) {
504 list_del_init(&pos->list);
509 static void select_tracer(struct perf_ftrace *ftrace)
511 bool graph = !list_empty(&ftrace->graph_funcs) ||
512 !list_empty(&ftrace->nograph_funcs);
513 bool func = !list_empty(&ftrace->filters) ||
514 !list_empty(&ftrace->notrace);
516 /* The function_graph has priority over function tracer. */
518 ftrace->tracer = "function_graph";
520 ftrace->tracer = "function";
521 /* Otherwise, the default tracer is used. */
523 pr_debug("%s tracer is used\n", ftrace->tracer);
526 int cmd_ftrace(int argc, const char **argv)
529 struct perf_ftrace ftrace = {
530 .tracer = DEFAULT_TRACER,
531 .target = { .uid = UINT_MAX, },
533 const char * const ftrace_usage[] = {
534 "perf ftrace [<options>] [<command>]",
535 "perf ftrace [<options>] -- <command> [<options>]",
538 const struct option ftrace_options[] = {
539 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
540 "tracer to use: function_graph(default) or function"),
541 OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
542 "Show available functions to filter"),
543 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
544 "trace on existing process id"),
545 OPT_INCR('v', "verbose", &verbose,
547 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
548 "system-wide collection from all CPUs"),
549 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
550 "list of cpus to monitor"),
551 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
552 "trace given functions using function tracer",
554 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
555 "do not trace given functions", parse_filter_func),
556 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
557 "trace given functions using function_graph tracer",
559 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
560 "Set nograph filter on given functions", parse_filter_func),
561 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
562 "Max depth for function graph tracer"),
566 INIT_LIST_HEAD(&ftrace.filters);
567 INIT_LIST_HEAD(&ftrace.notrace);
568 INIT_LIST_HEAD(&ftrace.graph_funcs);
569 INIT_LIST_HEAD(&ftrace.nograph_funcs);
571 ret = perf_config(perf_ftrace_config, &ftrace);
575 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
576 PARSE_OPT_STOP_AT_NON_OPTION);
577 if (!argc && target__none(&ftrace.target))
578 ftrace.target.system_wide = true;
580 select_tracer(&ftrace);
582 ret = target__validate(&ftrace.target);
586 target__strerror(&ftrace.target, ret, errbuf, 512);
587 pr_err("%s\n", errbuf);
588 goto out_delete_filters;
591 ftrace.evlist = evlist__new();
592 if (ftrace.evlist == NULL) {
594 goto out_delete_filters;
597 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
599 goto out_delete_evlist;
601 ret = __cmd_ftrace(&ftrace, argc, argv);
604 evlist__delete(ftrace.evlist);
607 delete_filter_func(&ftrace.filters);
608 delete_filter_func(&ftrace.notrace);
609 delete_filter_func(&ftrace.graph_funcs);
610 delete_filter_func(&ftrace.nograph_funcs);