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 struct list_head filters;
37 struct list_head notrace;
38 struct list_head graph_funcs;
39 struct list_head nograph_funcs;
44 struct list_head list;
50 static void sig_handler(int sig __maybe_unused)
56 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
57 * we asked by setting its exec_error to the function below,
58 * ftrace__workload_exec_failed_signal.
60 * XXX We need to handle this more appropriately, emitting an error, etc.
62 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
63 siginfo_t *info __maybe_unused,
64 void *ucontext __maybe_unused)
66 /* workload_exec_errno = info->si_value.sival_int; */
70 static int __write_tracing_file(const char *name, const char *val, bool append)
74 ssize_t size = strlen(val);
79 file = get_tracing_file(name);
81 pr_debug("cannot get tracing file: %s\n", name);
90 fd = open(file, flags);
92 pr_debug("cannot open tracing file: %s: %s\n",
93 name, str_error_r(errno, errbuf, sizeof(errbuf)));
98 * Copy the original value and append a '\n'. Without this,
99 * the kernel can hide possible errors.
101 val_copy = strdup(val);
104 val_copy[size] = '\n';
106 if (write(fd, val_copy, size + 1) == size + 1)
109 pr_debug("write '%s' to tracing/%s failed: %s\n",
110 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
116 put_tracing_file(file);
120 static int write_tracing_file(const char *name, const char *val)
122 return __write_tracing_file(name, val, false);
125 static int append_tracing_file(const char *name, const char *val)
127 return __write_tracing_file(name, val, true);
130 static int reset_tracing_cpu(void);
131 static void reset_tracing_filters(void);
133 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
135 if (write_tracing_file("tracing_on", "0") < 0)
138 if (write_tracing_file("current_tracer", "nop") < 0)
141 if (write_tracing_file("set_ftrace_pid", " ") < 0)
144 if (reset_tracing_cpu() < 0)
147 if (write_tracing_file("max_graph_depth", "0") < 0)
150 reset_tracing_filters();
154 static int set_tracing_pid(struct perf_ftrace *ftrace)
159 if (target__has_cpu(&ftrace->target))
162 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
163 scnprintf(buf, sizeof(buf), "%d",
164 ftrace->evlist->core.threads->map[i]);
165 if (append_tracing_file("set_ftrace_pid", buf) < 0)
171 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
178 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
179 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
180 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
182 cpumask = malloc(mask_size);
183 if (cpumask == NULL) {
184 pr_debug("failed to allocate cpu mask\n");
188 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
190 ret = write_tracing_file("tracing_cpumask", cpumask);
196 static int set_tracing_cpu(struct perf_ftrace *ftrace)
198 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
200 if (!target__has_cpu(&ftrace->target))
203 return set_tracing_cpumask(cpumap);
206 static int reset_tracing_cpu(void)
208 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
211 ret = set_tracing_cpumask(cpumap);
212 perf_cpu_map__put(cpumap);
216 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
218 struct filter_entry *pos;
220 list_for_each_entry(pos, funcs, list) {
221 if (append_tracing_file(filter_file, pos->name) < 0)
228 static int set_tracing_filters(struct perf_ftrace *ftrace)
232 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
236 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
240 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
244 /* old kernels do not have this filter */
245 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
250 static void reset_tracing_filters(void)
252 write_tracing_file("set_ftrace_filter", " ");
253 write_tracing_file("set_ftrace_notrace", " ");
254 write_tracing_file("set_graph_function", " ");
255 write_tracing_file("set_graph_notrace", " ");
258 static int set_tracing_depth(struct perf_ftrace *ftrace)
262 if (ftrace->graph_depth == 0)
265 if (ftrace->graph_depth < 0) {
266 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
270 snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
272 if (write_tracing_file("max_graph_depth", buf) < 0)
278 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
283 struct pollfd pollfd = {
287 if (!(perf_cap__capable(CAP_PERFMON) ||
288 perf_cap__capable(CAP_SYS_ADMIN))) {
289 pr_err("ftrace only works for %s!\n",
290 #ifdef HAVE_LIBCAP_SUPPORT
291 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
299 signal(SIGINT, sig_handler);
300 signal(SIGUSR1, sig_handler);
301 signal(SIGCHLD, sig_handler);
302 signal(SIGPIPE, sig_handler);
304 if (reset_tracing_files(ftrace) < 0) {
305 pr_err("failed to reset ftrace\n");
309 /* reset ftrace buffer */
310 if (write_tracing_file("trace", "0") < 0)
313 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
314 &ftrace->target, argv, false,
315 ftrace__workload_exec_failed_signal) < 0) {
319 if (set_tracing_pid(ftrace) < 0) {
320 pr_err("failed to set ftrace pid\n");
324 if (set_tracing_cpu(ftrace) < 0) {
325 pr_err("failed to set tracing cpumask\n");
329 if (set_tracing_filters(ftrace) < 0) {
330 pr_err("failed to set tracing filters\n");
334 if (set_tracing_depth(ftrace) < 0) {
335 pr_err("failed to set graph depth\n");
339 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
340 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
346 trace_file = get_tracing_file("trace_pipe");
348 pr_err("failed to open trace_pipe\n");
352 trace_fd = open(trace_file, O_RDONLY);
354 put_tracing_file(trace_file);
357 pr_err("failed to open trace_pipe\n");
361 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
362 pollfd.fd = trace_fd;
364 if (write_tracing_file("tracing_on", "1") < 0) {
365 pr_err("can't enable tracing\n");
369 perf_evlist__start_workload(ftrace->evlist);
372 if (poll(&pollfd, 1, -1) < 0)
375 if (pollfd.revents & POLLIN) {
376 int n = read(trace_fd, buf, sizeof(buf));
379 if (fwrite(buf, n, 1, stdout) != 1)
384 write_tracing_file("tracing_on", "0");
386 /* read remaining buffer contents */
388 int n = read(trace_fd, buf, sizeof(buf));
391 if (fwrite(buf, n, 1, stdout) != 1)
398 reset_tracing_files(ftrace);
400 return done ? 0 : -1;
403 static int perf_ftrace_config(const char *var, const char *value, void *cb)
405 struct perf_ftrace *ftrace = cb;
407 if (!strstarts(var, "ftrace."))
410 if (strcmp(var, "ftrace.tracer"))
413 if (!strcmp(value, "function_graph") ||
414 !strcmp(value, "function")) {
415 ftrace->tracer = value;
419 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
423 static int parse_filter_func(const struct option *opt, const char *str,
424 int unset __maybe_unused)
426 struct list_head *head = opt->value;
427 struct filter_entry *entry;
429 entry = malloc(sizeof(*entry) + strlen(str) + 1);
433 strcpy(entry->name, str);
434 list_add_tail(&entry->list, head);
439 static void delete_filter_func(struct list_head *head)
441 struct filter_entry *pos, *tmp;
443 list_for_each_entry_safe(pos, tmp, head, list) {
444 list_del_init(&pos->list);
449 int cmd_ftrace(int argc, const char **argv)
452 struct perf_ftrace ftrace = {
453 .tracer = DEFAULT_TRACER,
454 .target = { .uid = UINT_MAX, },
456 const char * const ftrace_usage[] = {
457 "perf ftrace [<options>] [<command>]",
458 "perf ftrace [<options>] -- <command> [<options>]",
461 const struct option ftrace_options[] = {
462 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
463 "tracer to use: function_graph(default) or function"),
464 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
465 "trace on existing process id"),
466 OPT_INCR('v', "verbose", &verbose,
468 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
469 "system-wide collection from all CPUs"),
470 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
471 "list of cpus to monitor"),
472 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
473 "trace given functions only", parse_filter_func),
474 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
475 "do not trace given functions", parse_filter_func),
476 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
477 "Set graph filter on given functions", parse_filter_func),
478 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
479 "Set nograph filter on given functions", parse_filter_func),
480 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
481 "Max depth for function graph tracer"),
485 INIT_LIST_HEAD(&ftrace.filters);
486 INIT_LIST_HEAD(&ftrace.notrace);
487 INIT_LIST_HEAD(&ftrace.graph_funcs);
488 INIT_LIST_HEAD(&ftrace.nograph_funcs);
490 ret = perf_config(perf_ftrace_config, &ftrace);
494 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
495 PARSE_OPT_STOP_AT_NON_OPTION);
496 if (!argc && target__none(&ftrace.target))
497 usage_with_options(ftrace_usage, ftrace_options);
499 ret = target__validate(&ftrace.target);
503 target__strerror(&ftrace.target, ret, errbuf, 512);
504 pr_err("%s\n", errbuf);
505 goto out_delete_filters;
508 ftrace.evlist = evlist__new();
509 if (ftrace.evlist == NULL) {
511 goto out_delete_filters;
514 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
516 goto out_delete_evlist;
518 ret = __cmd_ftrace(&ftrace, argc, argv);
521 evlist__delete(ftrace.evlist);
524 delete_filter_func(&ftrace.filters);
525 delete_filter_func(&ftrace.notrace);
526 delete_filter_func(&ftrace.graph_funcs);
527 delete_filter_func(&ftrace.nograph_funcs);