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;
50 struct list_head list;
54 static volatile int workload_exec_errno;
57 static void sig_handler(int sig __maybe_unused)
63 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
64 * we asked by setting its exec_error to the function below,
65 * ftrace__workload_exec_failed_signal.
67 * XXX We need to handle this more appropriately, emitting an error, etc.
69 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
70 siginfo_t *info __maybe_unused,
71 void *ucontext __maybe_unused)
73 workload_exec_errno = info->si_value.sival_int;
77 static int __write_tracing_file(const char *name, const char *val, bool append)
81 ssize_t size = strlen(val);
86 file = get_tracing_file(name);
88 pr_debug("cannot get tracing file: %s\n", name);
97 fd = open(file, flags);
99 pr_debug("cannot open tracing file: %s: %s\n",
100 name, str_error_r(errno, errbuf, sizeof(errbuf)));
105 * Copy the original value and append a '\n'. Without this,
106 * the kernel can hide possible errors.
108 val_copy = strdup(val);
111 val_copy[size] = '\n';
113 if (write(fd, val_copy, size + 1) == size + 1)
116 pr_debug("write '%s' to tracing/%s failed: %s\n",
117 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
123 put_tracing_file(file);
127 static int write_tracing_file(const char *name, const char *val)
129 return __write_tracing_file(name, val, false);
132 static int append_tracing_file(const char *name, const char *val)
134 return __write_tracing_file(name, val, true);
137 static int read_tracing_file_to_stdout(const char *name)
144 file = get_tracing_file(name);
146 pr_debug("cannot get tracing file: %s\n", name);
150 fd = open(file, O_RDONLY);
152 pr_debug("cannot open tracing file: %s: %s\n",
153 name, str_error_r(errno, buf, sizeof(buf)));
157 /* read contents to stdout */
159 int n = read(fd, buf, sizeof(buf));
165 if (fwrite(buf, n, 1, stdout) != 1)
173 put_tracing_file(file);
177 static int write_tracing_file_int(const char *name, int value)
181 snprintf(buf, sizeof(buf), "%d", value);
182 if (write_tracing_file(name, buf) < 0)
188 static int write_tracing_option_file(const char *name, const char *val)
193 if (asprintf(&file, "options/%s", name) < 0)
196 ret = __write_tracing_file(file, val, false);
201 static int reset_tracing_cpu(void);
202 static void reset_tracing_filters(void);
204 static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
206 write_tracing_option_file("function-fork", "0");
207 write_tracing_option_file("func_stack_trace", "0");
210 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
212 if (write_tracing_file("tracing_on", "0") < 0)
215 if (write_tracing_file("current_tracer", "nop") < 0)
218 if (write_tracing_file("set_ftrace_pid", " ") < 0)
221 if (reset_tracing_cpu() < 0)
224 if (write_tracing_file("max_graph_depth", "0") < 0)
227 reset_tracing_filters();
228 reset_tracing_options(ftrace);
232 static int set_tracing_pid(struct perf_ftrace *ftrace)
237 if (target__has_cpu(&ftrace->target))
240 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
241 scnprintf(buf, sizeof(buf), "%d",
242 ftrace->evlist->core.threads->map[i]);
243 if (append_tracing_file("set_ftrace_pid", buf) < 0)
249 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
256 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
257 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
258 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
260 cpumask = malloc(mask_size);
261 if (cpumask == NULL) {
262 pr_debug("failed to allocate cpu mask\n");
266 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
268 ret = write_tracing_file("tracing_cpumask", cpumask);
274 static int set_tracing_cpu(struct perf_ftrace *ftrace)
276 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
278 if (!target__has_cpu(&ftrace->target))
281 return set_tracing_cpumask(cpumap);
284 static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
286 if (!ftrace->func_stack_trace)
289 if (write_tracing_option_file("func_stack_trace", "1") < 0)
295 static int reset_tracing_cpu(void)
297 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
300 ret = set_tracing_cpumask(cpumap);
301 perf_cpu_map__put(cpumap);
305 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
307 struct filter_entry *pos;
309 list_for_each_entry(pos, funcs, list) {
310 if (append_tracing_file(filter_file, pos->name) < 0)
317 static int set_tracing_filters(struct perf_ftrace *ftrace)
321 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
325 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
329 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
333 /* old kernels do not have this filter */
334 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
339 static void reset_tracing_filters(void)
341 write_tracing_file("set_ftrace_filter", " ");
342 write_tracing_file("set_ftrace_notrace", " ");
343 write_tracing_file("set_graph_function", " ");
344 write_tracing_file("set_graph_notrace", " ");
347 static int set_tracing_depth(struct perf_ftrace *ftrace)
349 if (ftrace->graph_depth == 0)
352 if (ftrace->graph_depth < 0) {
353 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
357 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
363 static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
367 if (ftrace->percpu_buffer_size == 0)
370 ret = write_tracing_file_int("buffer_size_kb",
371 ftrace->percpu_buffer_size / 1024);
378 static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
380 if (!ftrace->inherit)
383 if (write_tracing_option_file("function-fork", "1") < 0)
389 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
394 struct pollfd pollfd = {
398 if (!(perf_cap__capable(CAP_PERFMON) ||
399 perf_cap__capable(CAP_SYS_ADMIN))) {
400 pr_err("ftrace only works for %s!\n",
401 #ifdef HAVE_LIBCAP_SUPPORT
402 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
410 signal(SIGINT, sig_handler);
411 signal(SIGUSR1, sig_handler);
412 signal(SIGCHLD, sig_handler);
413 signal(SIGPIPE, sig_handler);
415 if (ftrace->list_avail_functions)
416 return read_tracing_file_to_stdout("available_filter_functions");
418 if (reset_tracing_files(ftrace) < 0) {
419 pr_err("failed to reset ftrace\n");
423 /* reset ftrace buffer */
424 if (write_tracing_file("trace", "0") < 0)
427 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
428 &ftrace->target, argv, false,
429 ftrace__workload_exec_failed_signal) < 0) {
433 if (set_tracing_pid(ftrace) < 0) {
434 pr_err("failed to set ftrace pid\n");
438 if (set_tracing_cpu(ftrace) < 0) {
439 pr_err("failed to set tracing cpumask\n");
443 if (set_tracing_func_stack_trace(ftrace) < 0) {
444 pr_err("failed to set tracing option func_stack_trace\n");
448 if (set_tracing_filters(ftrace) < 0) {
449 pr_err("failed to set tracing filters\n");
453 if (set_tracing_depth(ftrace) < 0) {
454 pr_err("failed to set graph depth\n");
458 if (set_tracing_percpu_buffer_size(ftrace) < 0) {
459 pr_err("failed to set tracing per-cpu buffer size\n");
463 if (set_tracing_trace_inherit(ftrace) < 0) {
464 pr_err("failed to set tracing option function-fork\n");
468 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
469 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
475 trace_file = get_tracing_file("trace_pipe");
477 pr_err("failed to open trace_pipe\n");
481 trace_fd = open(trace_file, O_RDONLY);
483 put_tracing_file(trace_file);
486 pr_err("failed to open trace_pipe\n");
490 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
491 pollfd.fd = trace_fd;
493 /* display column headers */
494 read_tracing_file_to_stdout("trace");
496 if (write_tracing_file("tracing_on", "1") < 0) {
497 pr_err("can't enable tracing\n");
501 perf_evlist__start_workload(ftrace->evlist);
504 if (poll(&pollfd, 1, -1) < 0)
507 if (pollfd.revents & POLLIN) {
508 int n = read(trace_fd, buf, sizeof(buf));
511 if (fwrite(buf, n, 1, stdout) != 1)
516 write_tracing_file("tracing_on", "0");
518 if (workload_exec_errno) {
519 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
520 /* flush stdout first so below error msg appears at the end. */
522 pr_err("workload failed: %s\n", emsg);
526 /* read remaining buffer contents */
528 int n = read(trace_fd, buf, sizeof(buf));
531 if (fwrite(buf, n, 1, stdout) != 1)
538 reset_tracing_files(ftrace);
540 return (done && !workload_exec_errno) ? 0 : -1;
543 static int perf_ftrace_config(const char *var, const char *value, void *cb)
545 struct perf_ftrace *ftrace = cb;
547 if (!strstarts(var, "ftrace."))
550 if (strcmp(var, "ftrace.tracer"))
553 if (!strcmp(value, "function_graph") ||
554 !strcmp(value, "function")) {
555 ftrace->tracer = value;
559 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
563 static int parse_filter_func(const struct option *opt, const char *str,
564 int unset __maybe_unused)
566 struct list_head *head = opt->value;
567 struct filter_entry *entry;
569 entry = malloc(sizeof(*entry) + strlen(str) + 1);
573 strcpy(entry->name, str);
574 list_add_tail(&entry->list, head);
579 static void delete_filter_func(struct list_head *head)
581 struct filter_entry *pos, *tmp;
583 list_for_each_entry_safe(pos, tmp, head, list) {
584 list_del_init(&pos->list);
589 static int parse_buffer_size(const struct option *opt,
590 const char *str, int unset)
592 unsigned long *s = (unsigned long *)opt->value;
593 static struct parse_tag tags_size[] = {
594 { .tag = 'B', .mult = 1 },
595 { .tag = 'K', .mult = 1 << 10 },
596 { .tag = 'M', .mult = 1 << 20 },
597 { .tag = 'G', .mult = 1 << 30 },
607 val = parse_tag_value(str, tags_size);
608 if (val != (unsigned long) -1) {
610 pr_err("buffer size too small, must larger than 1KB.");
620 static int parse_func_tracer_opts(const struct option *opt,
621 const char *str, int unset)
624 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
625 struct sublevel_option func_tracer_opts[] = {
626 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
633 ret = perf_parse_sublevel_options(str, func_tracer_opts);
640 static void select_tracer(struct perf_ftrace *ftrace)
642 bool graph = !list_empty(&ftrace->graph_funcs) ||
643 !list_empty(&ftrace->nograph_funcs);
644 bool func = !list_empty(&ftrace->filters) ||
645 !list_empty(&ftrace->notrace);
647 /* The function_graph has priority over function tracer. */
649 ftrace->tracer = "function_graph";
651 ftrace->tracer = "function";
652 /* Otherwise, the default tracer is used. */
654 pr_debug("%s tracer is used\n", ftrace->tracer);
657 int cmd_ftrace(int argc, const char **argv)
660 struct perf_ftrace ftrace = {
661 .tracer = DEFAULT_TRACER,
662 .target = { .uid = UINT_MAX, },
664 const char * const ftrace_usage[] = {
665 "perf ftrace [<options>] [<command>]",
666 "perf ftrace [<options>] -- <command> [<options>]",
669 const struct option ftrace_options[] = {
670 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
671 "tracer to use: function_graph(default) or function"),
672 OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
673 "Show available functions to filter"),
674 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
675 "trace on existing process id"),
676 OPT_INCR('v', "verbose", &verbose,
678 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
679 "system-wide collection from all CPUs"),
680 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
681 "list of cpus to monitor"),
682 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
683 "trace given functions using function tracer",
685 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
686 "do not trace given functions", parse_filter_func),
687 OPT_CALLBACK(0, "func-opts", &ftrace, "options",
688 "function tracer options, available options: call-graph",
689 parse_func_tracer_opts),
690 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
691 "trace given functions using function_graph tracer",
693 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
694 "Set nograph filter on given functions", parse_filter_func),
695 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
696 "Max depth for function graph tracer"),
697 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
698 "size of per cpu buffer", parse_buffer_size),
699 OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
700 "trace children processes"),
704 INIT_LIST_HEAD(&ftrace.filters);
705 INIT_LIST_HEAD(&ftrace.notrace);
706 INIT_LIST_HEAD(&ftrace.graph_funcs);
707 INIT_LIST_HEAD(&ftrace.nograph_funcs);
709 ret = perf_config(perf_ftrace_config, &ftrace);
713 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
714 PARSE_OPT_STOP_AT_NON_OPTION);
715 if (!argc && target__none(&ftrace.target))
716 ftrace.target.system_wide = true;
718 select_tracer(&ftrace);
720 ret = target__validate(&ftrace.target);
724 target__strerror(&ftrace.target, ret, errbuf, 512);
725 pr_err("%s\n", errbuf);
726 goto out_delete_filters;
729 ftrace.evlist = evlist__new();
730 if (ftrace.evlist == NULL) {
732 goto out_delete_filters;
735 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
737 goto out_delete_evlist;
739 ret = __cmd_ftrace(&ftrace, argc, argv);
742 evlist__delete(ftrace.evlist);
745 delete_filter_func(&ftrace.filters);
746 delete_filter_func(&ftrace.notrace);
747 delete_filter_func(&ftrace.graph_funcs);
748 delete_filter_func(&ftrace.nograph_funcs);