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 reset_tracing_cpu(void);
173 static void reset_tracing_filters(void);
175 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
177 if (write_tracing_file("tracing_on", "0") < 0)
180 if (write_tracing_file("current_tracer", "nop") < 0)
183 if (write_tracing_file("set_ftrace_pid", " ") < 0)
186 if (reset_tracing_cpu() < 0)
189 if (write_tracing_file("max_graph_depth", "0") < 0)
192 reset_tracing_filters();
196 static int set_tracing_pid(struct perf_ftrace *ftrace)
201 if (target__has_cpu(&ftrace->target))
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)
213 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
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 */
224 cpumask = malloc(mask_size);
225 if (cpumask == NULL) {
226 pr_debug("failed to allocate cpu mask\n");
230 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
232 ret = write_tracing_file("tracing_cpumask", cpumask);
238 static int set_tracing_cpu(struct perf_ftrace *ftrace)
240 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
242 if (!target__has_cpu(&ftrace->target))
245 return set_tracing_cpumask(cpumap);
248 static int reset_tracing_cpu(void)
250 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
253 ret = set_tracing_cpumask(cpumap);
254 perf_cpu_map__put(cpumap);
258 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
260 struct filter_entry *pos;
262 list_for_each_entry(pos, funcs, list) {
263 if (append_tracing_file(filter_file, pos->name) < 0)
270 static int set_tracing_filters(struct perf_ftrace *ftrace)
274 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
278 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
282 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
286 /* old kernels do not have this filter */
287 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
292 static void reset_tracing_filters(void)
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", " ");
300 static int set_tracing_depth(struct perf_ftrace *ftrace)
304 if (ftrace->graph_depth == 0)
307 if (ftrace->graph_depth < 0) {
308 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
312 snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
314 if (write_tracing_file("max_graph_depth", buf) < 0)
320 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
325 struct pollfd pollfd = {
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"
341 signal(SIGINT, sig_handler);
342 signal(SIGUSR1, sig_handler);
343 signal(SIGCHLD, sig_handler);
344 signal(SIGPIPE, sig_handler);
346 if (ftrace->list_avail_functions)
347 return read_tracing_file_to_stdout("available_filter_functions");
349 if (reset_tracing_files(ftrace) < 0) {
350 pr_err("failed to reset ftrace\n");
354 /* reset ftrace buffer */
355 if (write_tracing_file("trace", "0") < 0)
358 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
359 &ftrace->target, argv, false,
360 ftrace__workload_exec_failed_signal) < 0) {
364 if (set_tracing_pid(ftrace) < 0) {
365 pr_err("failed to set ftrace pid\n");
369 if (set_tracing_cpu(ftrace) < 0) {
370 pr_err("failed to set tracing cpumask\n");
374 if (set_tracing_filters(ftrace) < 0) {
375 pr_err("failed to set tracing filters\n");
379 if (set_tracing_depth(ftrace) < 0) {
380 pr_err("failed to set graph depth\n");
384 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
385 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
391 trace_file = get_tracing_file("trace_pipe");
393 pr_err("failed to open trace_pipe\n");
397 trace_fd = open(trace_file, O_RDONLY);
399 put_tracing_file(trace_file);
402 pr_err("failed to open trace_pipe\n");
406 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
407 pollfd.fd = trace_fd;
409 if (write_tracing_file("tracing_on", "1") < 0) {
410 pr_err("can't enable tracing\n");
414 perf_evlist__start_workload(ftrace->evlist);
417 if (poll(&pollfd, 1, -1) < 0)
420 if (pollfd.revents & POLLIN) {
421 int n = read(trace_fd, buf, sizeof(buf));
424 if (fwrite(buf, n, 1, stdout) != 1)
429 write_tracing_file("tracing_on", "0");
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. */
435 pr_err("workload failed: %s\n", emsg);
439 /* read remaining buffer contents */
441 int n = read(trace_fd, buf, sizeof(buf));
444 if (fwrite(buf, n, 1, stdout) != 1)
451 reset_tracing_files(ftrace);
453 return (done && !workload_exec_errno) ? 0 : -1;
456 static int perf_ftrace_config(const char *var, const char *value, void *cb)
458 struct perf_ftrace *ftrace = cb;
460 if (!strstarts(var, "ftrace."))
463 if (strcmp(var, "ftrace.tracer"))
466 if (!strcmp(value, "function_graph") ||
467 !strcmp(value, "function")) {
468 ftrace->tracer = value;
472 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
476 static int parse_filter_func(const struct option *opt, const char *str,
477 int unset __maybe_unused)
479 struct list_head *head = opt->value;
480 struct filter_entry *entry;
482 entry = malloc(sizeof(*entry) + strlen(str) + 1);
486 strcpy(entry->name, str);
487 list_add_tail(&entry->list, head);
492 static void delete_filter_func(struct list_head *head)
494 struct filter_entry *pos, *tmp;
496 list_for_each_entry_safe(pos, tmp, head, list) {
497 list_del_init(&pos->list);
502 static void select_tracer(struct perf_ftrace *ftrace)
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);
509 /* The function_graph has priority over function tracer. */
511 ftrace->tracer = "function_graph";
513 ftrace->tracer = "function";
514 /* Otherwise, the default tracer is used. */
516 pr_debug("%s tracer is used\n", ftrace->tracer);
519 int cmd_ftrace(int argc, const char **argv)
522 struct perf_ftrace ftrace = {
523 .tracer = DEFAULT_TRACER,
524 .target = { .uid = UINT_MAX, },
526 const char * const ftrace_usage[] = {
527 "perf ftrace [<options>] [<command>]",
528 "perf ftrace [<options>] -- <command> [<options>]",
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,
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",
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",
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"),
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);
564 ret = perf_config(perf_ftrace_config, &ftrace);
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;
573 select_tracer(&ftrace);
575 ret = target__validate(&ftrace.target);
579 target__strerror(&ftrace.target, ret, errbuf, 512);
580 pr_err("%s\n", errbuf);
581 goto out_delete_filters;
584 ftrace.evlist = evlist__new();
585 if (ftrace.evlist == NULL) {
587 goto out_delete_filters;
590 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
592 goto out_delete_evlist;
594 ret = __cmd_ftrace(&ftrace, argc, argv);
597 evlist__delete(ftrace.evlist);
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);