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;
48 static volatile int workload_exec_errno;
51 static void sig_handler(int sig __maybe_unused)
57 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
58 * we asked by setting its exec_error to the function below,
59 * ftrace__workload_exec_failed_signal.
61 * XXX We need to handle this more appropriately, emitting an error, etc.
63 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
64 siginfo_t *info __maybe_unused,
65 void *ucontext __maybe_unused)
67 workload_exec_errno = info->si_value.sival_int;
71 static int __write_tracing_file(const char *name, const char *val, bool append)
75 ssize_t size = strlen(val);
80 file = get_tracing_file(name);
82 pr_debug("cannot get tracing file: %s\n", name);
91 fd = open(file, flags);
93 pr_debug("cannot open tracing file: %s: %s\n",
94 name, str_error_r(errno, errbuf, sizeof(errbuf)));
99 * Copy the original value and append a '\n'. Without this,
100 * the kernel can hide possible errors.
102 val_copy = strdup(val);
105 val_copy[size] = '\n';
107 if (write(fd, val_copy, size + 1) == size + 1)
110 pr_debug("write '%s' to tracing/%s failed: %s\n",
111 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
117 put_tracing_file(file);
121 static int write_tracing_file(const char *name, const char *val)
123 return __write_tracing_file(name, val, false);
126 static int append_tracing_file(const char *name, const char *val)
128 return __write_tracing_file(name, val, true);
131 static int reset_tracing_cpu(void);
132 static void reset_tracing_filters(void);
134 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
136 if (write_tracing_file("tracing_on", "0") < 0)
139 if (write_tracing_file("current_tracer", "nop") < 0)
142 if (write_tracing_file("set_ftrace_pid", " ") < 0)
145 if (reset_tracing_cpu() < 0)
148 if (write_tracing_file("max_graph_depth", "0") < 0)
151 reset_tracing_filters();
155 static int set_tracing_pid(struct perf_ftrace *ftrace)
160 if (target__has_cpu(&ftrace->target))
163 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
164 scnprintf(buf, sizeof(buf), "%d",
165 ftrace->evlist->core.threads->map[i]);
166 if (append_tracing_file("set_ftrace_pid", buf) < 0)
172 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
179 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
180 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
181 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
183 cpumask = malloc(mask_size);
184 if (cpumask == NULL) {
185 pr_debug("failed to allocate cpu mask\n");
189 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
191 ret = write_tracing_file("tracing_cpumask", cpumask);
197 static int set_tracing_cpu(struct perf_ftrace *ftrace)
199 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
201 if (!target__has_cpu(&ftrace->target))
204 return set_tracing_cpumask(cpumap);
207 static int reset_tracing_cpu(void)
209 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
212 ret = set_tracing_cpumask(cpumap);
213 perf_cpu_map__put(cpumap);
217 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
219 struct filter_entry *pos;
221 list_for_each_entry(pos, funcs, list) {
222 if (append_tracing_file(filter_file, pos->name) < 0)
229 static int set_tracing_filters(struct perf_ftrace *ftrace)
233 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
237 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
241 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
245 /* old kernels do not have this filter */
246 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
251 static void reset_tracing_filters(void)
253 write_tracing_file("set_ftrace_filter", " ");
254 write_tracing_file("set_ftrace_notrace", " ");
255 write_tracing_file("set_graph_function", " ");
256 write_tracing_file("set_graph_notrace", " ");
259 static int set_tracing_depth(struct perf_ftrace *ftrace)
263 if (ftrace->graph_depth == 0)
266 if (ftrace->graph_depth < 0) {
267 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
271 snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
273 if (write_tracing_file("max_graph_depth", buf) < 0)
279 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
284 struct pollfd pollfd = {
288 if (!(perf_cap__capable(CAP_PERFMON) ||
289 perf_cap__capable(CAP_SYS_ADMIN))) {
290 pr_err("ftrace only works for %s!\n",
291 #ifdef HAVE_LIBCAP_SUPPORT
292 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
300 signal(SIGINT, sig_handler);
301 signal(SIGUSR1, sig_handler);
302 signal(SIGCHLD, sig_handler);
303 signal(SIGPIPE, sig_handler);
305 if (reset_tracing_files(ftrace) < 0) {
306 pr_err("failed to reset ftrace\n");
310 /* reset ftrace buffer */
311 if (write_tracing_file("trace", "0") < 0)
314 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
315 &ftrace->target, argv, false,
316 ftrace__workload_exec_failed_signal) < 0) {
320 if (set_tracing_pid(ftrace) < 0) {
321 pr_err("failed to set ftrace pid\n");
325 if (set_tracing_cpu(ftrace) < 0) {
326 pr_err("failed to set tracing cpumask\n");
330 if (set_tracing_filters(ftrace) < 0) {
331 pr_err("failed to set tracing filters\n");
335 if (set_tracing_depth(ftrace) < 0) {
336 pr_err("failed to set graph depth\n");
340 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
341 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
347 trace_file = get_tracing_file("trace_pipe");
349 pr_err("failed to open trace_pipe\n");
353 trace_fd = open(trace_file, O_RDONLY);
355 put_tracing_file(trace_file);
358 pr_err("failed to open trace_pipe\n");
362 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
363 pollfd.fd = trace_fd;
365 if (write_tracing_file("tracing_on", "1") < 0) {
366 pr_err("can't enable tracing\n");
370 perf_evlist__start_workload(ftrace->evlist);
373 if (poll(&pollfd, 1, -1) < 0)
376 if (pollfd.revents & POLLIN) {
377 int n = read(trace_fd, buf, sizeof(buf));
380 if (fwrite(buf, n, 1, stdout) != 1)
385 write_tracing_file("tracing_on", "0");
387 if (workload_exec_errno) {
388 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
389 /* flush stdout first so below error msg appears at the end. */
391 pr_err("workload failed: %s\n", emsg);
395 /* read remaining buffer contents */
397 int n = read(trace_fd, buf, sizeof(buf));
400 if (fwrite(buf, n, 1, stdout) != 1)
407 reset_tracing_files(ftrace);
409 return (done && !workload_exec_errno) ? 0 : -1;
412 static int perf_ftrace_config(const char *var, const char *value, void *cb)
414 struct perf_ftrace *ftrace = cb;
416 if (!strstarts(var, "ftrace."))
419 if (strcmp(var, "ftrace.tracer"))
422 if (!strcmp(value, "function_graph") ||
423 !strcmp(value, "function")) {
424 ftrace->tracer = value;
428 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
432 static int parse_filter_func(const struct option *opt, const char *str,
433 int unset __maybe_unused)
435 struct list_head *head = opt->value;
436 struct filter_entry *entry;
438 entry = malloc(sizeof(*entry) + strlen(str) + 1);
442 strcpy(entry->name, str);
443 list_add_tail(&entry->list, head);
448 static void delete_filter_func(struct list_head *head)
450 struct filter_entry *pos, *tmp;
452 list_for_each_entry_safe(pos, tmp, head, list) {
453 list_del_init(&pos->list);
458 int cmd_ftrace(int argc, const char **argv)
461 struct perf_ftrace ftrace = {
462 .tracer = DEFAULT_TRACER,
463 .target = { .uid = UINT_MAX, },
465 const char * const ftrace_usage[] = {
466 "perf ftrace [<options>] [<command>]",
467 "perf ftrace [<options>] -- <command> [<options>]",
470 const struct option ftrace_options[] = {
471 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
472 "tracer to use: function_graph(default) or function"),
473 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
474 "trace on existing process id"),
475 OPT_INCR('v', "verbose", &verbose,
477 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
478 "system-wide collection from all CPUs"),
479 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
480 "list of cpus to monitor"),
481 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
482 "trace given functions only", parse_filter_func),
483 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
484 "do not trace given functions", parse_filter_func),
485 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
486 "Set graph filter on given functions", parse_filter_func),
487 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
488 "Set nograph filter on given functions", parse_filter_func),
489 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
490 "Max depth for function graph tracer"),
494 INIT_LIST_HEAD(&ftrace.filters);
495 INIT_LIST_HEAD(&ftrace.notrace);
496 INIT_LIST_HEAD(&ftrace.graph_funcs);
497 INIT_LIST_HEAD(&ftrace.nograph_funcs);
499 ret = perf_config(perf_ftrace_config, &ftrace);
503 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
504 PARSE_OPT_STOP_AT_NON_OPTION);
505 if (!argc && target__none(&ftrace.target))
506 ftrace.target.system_wide = true;
508 ret = target__validate(&ftrace.target);
512 target__strerror(&ftrace.target, ret, errbuf, 512);
513 pr_err("%s\n", errbuf);
514 goto out_delete_filters;
517 ftrace.evlist = evlist__new();
518 if (ftrace.evlist == NULL) {
520 goto out_delete_filters;
523 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
525 goto out_delete_evlist;
527 ret = __cmd_ftrace(&ftrace, argc, argv);
530 evlist__delete(ftrace.evlist);
533 delete_filter_func(&ftrace.filters);
534 delete_filter_func(&ftrace.notrace);
535 delete_filter_func(&ftrace.graph_funcs);
536 delete_filter_func(&ftrace.nograph_funcs);