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"
29 #include "util/units.h"
30 #include "util/parse-sublevel-options.h"
32 #define DEFAULT_TRACER "function_graph"
35 struct evlist *evlist;
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;
44 unsigned long percpu_buffer_size;
48 int graph_nosleep_time;
53 struct list_head list;
57 static volatile int workload_exec_errno;
60 static void sig_handler(int sig __maybe_unused)
66 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
67 * we asked by setting its exec_error to the function below,
68 * ftrace__workload_exec_failed_signal.
70 * XXX We need to handle this more appropriately, emitting an error, etc.
72 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
73 siginfo_t *info __maybe_unused,
74 void *ucontext __maybe_unused)
76 workload_exec_errno = info->si_value.sival_int;
80 static int __write_tracing_file(const char *name, const char *val, bool append)
84 ssize_t size = strlen(val);
89 file = get_tracing_file(name);
91 pr_debug("cannot get tracing file: %s\n", name);
100 fd = open(file, flags);
102 pr_debug("cannot open tracing file: %s: %s\n",
103 name, str_error_r(errno, errbuf, sizeof(errbuf)));
108 * Copy the original value and append a '\n'. Without this,
109 * the kernel can hide possible errors.
111 val_copy = strdup(val);
114 val_copy[size] = '\n';
116 if (write(fd, val_copy, size + 1) == size + 1)
119 pr_debug("write '%s' to tracing/%s failed: %s\n",
120 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
126 put_tracing_file(file);
130 static int write_tracing_file(const char *name, const char *val)
132 return __write_tracing_file(name, val, false);
135 static int append_tracing_file(const char *name, const char *val)
137 return __write_tracing_file(name, val, true);
140 static int read_tracing_file_to_stdout(const char *name)
147 file = get_tracing_file(name);
149 pr_debug("cannot get tracing file: %s\n", name);
153 fd = open(file, O_RDONLY);
155 pr_debug("cannot open tracing file: %s: %s\n",
156 name, str_error_r(errno, buf, sizeof(buf)));
160 /* read contents to stdout */
162 int n = read(fd, buf, sizeof(buf));
168 if (fwrite(buf, n, 1, stdout) != 1)
176 put_tracing_file(file);
180 static int write_tracing_file_int(const char *name, int value)
184 snprintf(buf, sizeof(buf), "%d", value);
185 if (write_tracing_file(name, buf) < 0)
191 static int write_tracing_option_file(const char *name, const char *val)
196 if (asprintf(&file, "options/%s", name) < 0)
199 ret = __write_tracing_file(file, val, false);
204 static int reset_tracing_cpu(void);
205 static void reset_tracing_filters(void);
207 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
209 write_tracing_option_file("function-fork", "0");
210 write_tracing_option_file("func_stack_trace", "0");
211 write_tracing_option_file("sleep-time", "1");
212 write_tracing_option_file("funcgraph-irqs", "1");
213 write_tracing_option_file("irq-info", "0");
216 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
218 if (write_tracing_file("tracing_on", "0") < 0)
221 if (write_tracing_file("current_tracer", "nop") < 0)
224 if (write_tracing_file("set_ftrace_pid", " ") < 0)
227 if (reset_tracing_cpu() < 0)
230 if (write_tracing_file("max_graph_depth", "0") < 0)
233 reset_tracing_filters();
234 reset_tracing_options(ftrace);
238 static int set_tracing_pid(struct perf_ftrace *ftrace)
243 if (target__has_cpu(&ftrace->target))
246 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
247 scnprintf(buf, sizeof(buf), "%d",
248 ftrace->evlist->core.threads->map[i]);
249 if (append_tracing_file("set_ftrace_pid", buf) < 0)
255 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
262 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
263 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
264 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
266 cpumask = malloc(mask_size);
267 if (cpumask == NULL) {
268 pr_debug("failed to allocate cpu mask\n");
272 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
274 ret = write_tracing_file("tracing_cpumask", cpumask);
280 static int set_tracing_cpu(struct perf_ftrace *ftrace)
282 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
284 if (!target__has_cpu(&ftrace->target))
287 return set_tracing_cpumask(cpumap);
290 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
292 if (!ftrace->func_stack_trace)
295 if (write_tracing_option_file("func_stack_trace", "1") < 0)
301 static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
303 if (!ftrace->func_irq_info)
306 if (write_tracing_option_file("irq-info", "1") < 0)
312 static int reset_tracing_cpu(void)
314 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
317 ret = set_tracing_cpumask(cpumap);
318 perf_cpu_map__put(cpumap);
322 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
324 struct filter_entry *pos;
326 list_for_each_entry(pos, funcs, list) {
327 if (append_tracing_file(filter_file, pos->name) < 0)
334 static int set_tracing_filters(struct perf_ftrace *ftrace)
338 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
342 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
346 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
350 /* old kernels do not have this filter */
351 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
356 static void reset_tracing_filters(void)
358 write_tracing_file("set_ftrace_filter", " ");
359 write_tracing_file("set_ftrace_notrace", " ");
360 write_tracing_file("set_graph_function", " ");
361 write_tracing_file("set_graph_notrace", " ");
364 static int set_tracing_depth(struct perf_ftrace *ftrace)
366 if (ftrace->graph_depth == 0)
369 if (ftrace->graph_depth < 0) {
370 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
374 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
380 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
384 if (ftrace->percpu_buffer_size == 0)
387 ret = write_tracing_file_int("buffer_size_kb",
388 ftrace->percpu_buffer_size / 1024);
395 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
397 if (!ftrace->inherit)
400 if (write_tracing_option_file("function-fork", "1") < 0)
406 static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
408 if (!ftrace->graph_nosleep_time)
411 if (write_tracing_option_file("sleep-time", "0") < 0)
417 static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
419 if (!ftrace->graph_noirqs)
422 if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
428 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
433 struct pollfd pollfd = {
437 if (!(perf_cap__capable(CAP_PERFMON) ||
438 perf_cap__capable(CAP_SYS_ADMIN))) {
439 pr_err("ftrace only works for %s!\n",
440 #ifdef HAVE_LIBCAP_SUPPORT
441 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
449 signal(SIGINT, sig_handler);
450 signal(SIGUSR1, sig_handler);
451 signal(SIGCHLD, sig_handler);
452 signal(SIGPIPE, sig_handler);
454 if (ftrace->list_avail_functions)
455 return read_tracing_file_to_stdout("available_filter_functions");
457 if (reset_tracing_files(ftrace) < 0) {
458 pr_err("failed to reset ftrace\n");
462 /* reset ftrace buffer */
463 if (write_tracing_file("trace", "0") < 0)
466 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
467 &ftrace->target, argv, false,
468 ftrace__workload_exec_failed_signal) < 0) {
472 if (set_tracing_pid(ftrace) < 0) {
473 pr_err("failed to set ftrace pid\n");
477 if (set_tracing_cpu(ftrace) < 0) {
478 pr_err("failed to set tracing cpumask\n");
482 if (set_tracing_func_stack_trace(ftrace) < 0) {
483 pr_err("failed to set tracing option func_stack_trace\n");
487 if (set_tracing_func_irqinfo(ftrace) < 0) {
488 pr_err("failed to set tracing option irq-info\n");
492 if (set_tracing_filters(ftrace) < 0) {
493 pr_err("failed to set tracing filters\n");
497 if (set_tracing_depth(ftrace) < 0) {
498 pr_err("failed to set graph depth\n");
502 if (set_tracing_percpu_buffer_size(ftrace) < 0) {
503 pr_err("failed to set tracing per-cpu buffer size\n");
507 if (set_tracing_trace_inherit(ftrace) < 0) {
508 pr_err("failed to set tracing option function-fork\n");
512 if (set_tracing_sleep_time(ftrace) < 0) {
513 pr_err("failed to set tracing option sleep-time\n");
517 if (set_tracing_funcgraph_irqs(ftrace) < 0) {
518 pr_err("failed to set tracing option funcgraph-irqs\n");
522 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
523 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
529 trace_file = get_tracing_file("trace_pipe");
531 pr_err("failed to open trace_pipe\n");
535 trace_fd = open(trace_file, O_RDONLY);
537 put_tracing_file(trace_file);
540 pr_err("failed to open trace_pipe\n");
544 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
545 pollfd.fd = trace_fd;
547 /* display column headers */
548 read_tracing_file_to_stdout("trace");
550 if (write_tracing_file("tracing_on", "1") < 0) {
551 pr_err("can't enable tracing\n");
555 perf_evlist__start_workload(ftrace->evlist);
558 if (poll(&pollfd, 1, -1) < 0)
561 if (pollfd.revents & POLLIN) {
562 int n = read(trace_fd, buf, sizeof(buf));
565 if (fwrite(buf, n, 1, stdout) != 1)
570 write_tracing_file("tracing_on", "0");
572 if (workload_exec_errno) {
573 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
574 /* flush stdout first so below error msg appears at the end. */
576 pr_err("workload failed: %s\n", emsg);
580 /* read remaining buffer contents */
582 int n = read(trace_fd, buf, sizeof(buf));
585 if (fwrite(buf, n, 1, stdout) != 1)
592 reset_tracing_files(ftrace);
594 return (done && !workload_exec_errno) ? 0 : -1;
597 static int perf_ftrace_config(const char *var, const char *value, void *cb)
599 struct perf_ftrace *ftrace = cb;
601 if (!strstarts(var, "ftrace."))
604 if (strcmp(var, "ftrace.tracer"))
607 if (!strcmp(value, "function_graph") ||
608 !strcmp(value, "function")) {
609 ftrace->tracer = value;
613 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
617 static int parse_filter_func(const struct option *opt, const char *str,
618 int unset __maybe_unused)
620 struct list_head *head = opt->value;
621 struct filter_entry *entry;
623 entry = malloc(sizeof(*entry) + strlen(str) + 1);
627 strcpy(entry->name, str);
628 list_add_tail(&entry->list, head);
633 static void delete_filter_func(struct list_head *head)
635 struct filter_entry *pos, *tmp;
637 list_for_each_entry_safe(pos, tmp, head, list) {
638 list_del_init(&pos->list);
643 static int parse_buffer_size(const struct option *opt,
644 const char *str, int unset)
646 unsigned long *s = (unsigned long *)opt->value;
647 static struct parse_tag tags_size[] = {
648 { .tag = 'B', .mult = 1 },
649 { .tag = 'K', .mult = 1 << 10 },
650 { .tag = 'M', .mult = 1 << 20 },
651 { .tag = 'G', .mult = 1 << 30 },
661 val = parse_tag_value(str, tags_size);
662 if (val != (unsigned long) -1) {
664 pr_err("buffer size too small, must larger than 1KB.");
674 static int parse_func_tracer_opts(const struct option *opt,
675 const char *str, int unset)
678 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
679 struct sublevel_option func_tracer_opts[] = {
680 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
681 { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
688 ret = perf_parse_sublevel_options(str, func_tracer_opts);
695 static int parse_graph_tracer_opts(const struct option *opt,
696 const char *str, int unset)
699 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
700 struct sublevel_option graph_tracer_opts[] = {
701 { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
702 { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
709 ret = perf_parse_sublevel_options(str, graph_tracer_opts);
716 static void select_tracer(struct perf_ftrace *ftrace)
718 bool graph = !list_empty(&ftrace->graph_funcs) ||
719 !list_empty(&ftrace->nograph_funcs);
720 bool func = !list_empty(&ftrace->filters) ||
721 !list_empty(&ftrace->notrace);
723 /* The function_graph has priority over function tracer. */
725 ftrace->tracer = "function_graph";
727 ftrace->tracer = "function";
728 /* Otherwise, the default tracer is used. */
730 pr_debug("%s tracer is used\n", ftrace->tracer);
733 int cmd_ftrace(int argc, const char **argv)
736 struct perf_ftrace ftrace = {
737 .tracer = DEFAULT_TRACER,
738 .target = { .uid = UINT_MAX, },
740 const char * const ftrace_usage[] = {
741 "perf ftrace [<options>] [<command>]",
742 "perf ftrace [<options>] -- <command> [<options>]",
745 const struct option ftrace_options[] = {
746 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
747 "tracer to use: function_graph(default) or function"),
748 OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
749 "Show available functions to filter"),
750 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
751 "trace on existing process id"),
752 OPT_INCR('v', "verbose", &verbose,
754 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
755 "system-wide collection from all CPUs"),
756 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
757 "list of cpus to monitor"),
758 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
759 "trace given functions using function tracer",
761 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
762 "do not trace given functions", parse_filter_func),
763 OPT_CALLBACK(0, "func-opts", &ftrace, "options",
764 "function tracer options, available options: call-graph,irq-info",
765 parse_func_tracer_opts),
766 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
767 "trace given functions using function_graph tracer",
769 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
770 "Set nograph filter on given functions", parse_filter_func),
771 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
772 "Max depth for function graph tracer"),
773 OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
774 "graph tracer options, available options: nosleep-time,noirqs",
775 parse_graph_tracer_opts),
776 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
777 "size of per cpu buffer", parse_buffer_size),
778 OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
779 "trace children processes"),
783 INIT_LIST_HEAD(&ftrace.filters);
784 INIT_LIST_HEAD(&ftrace.notrace);
785 INIT_LIST_HEAD(&ftrace.graph_funcs);
786 INIT_LIST_HEAD(&ftrace.nograph_funcs);
788 ret = perf_config(perf_ftrace_config, &ftrace);
792 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
793 PARSE_OPT_STOP_AT_NON_OPTION);
794 if (!argc && target__none(&ftrace.target))
795 ftrace.target.system_wide = true;
797 select_tracer(&ftrace);
799 ret = target__validate(&ftrace.target);
803 target__strerror(&ftrace.target, ret, errbuf, 512);
804 pr_err("%s\n", errbuf);
805 goto out_delete_filters;
808 ftrace.evlist = evlist__new();
809 if (ftrace.evlist == NULL) {
811 goto out_delete_filters;
814 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
816 goto out_delete_evlist;
818 ret = __cmd_ftrace(&ftrace, argc, argv);
821 evlist__delete(ftrace.evlist);
824 delete_filter_func(&ftrace.filters);
825 delete_filter_func(&ftrace.notrace);
826 delete_filter_func(&ftrace.graph_funcs);
827 delete_filter_func(&ftrace.nograph_funcs);