1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
18 #include <subcmd/parse-options.h>
19 #include <api/fs/tracing_path.h>
23 #include "thread_map.h"
24 #include "util/config.h"
27 #define DEFAULT_TRACER "function_graph"
30 struct perf_evlist *evlist;
33 struct list_head filters;
34 struct list_head notrace;
35 struct list_head graph_funcs;
36 struct list_head nograph_funcs;
41 struct list_head list;
47 static void sig_handler(int sig __maybe_unused)
53 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
54 * we asked by setting its exec_error to the function below,
55 * ftrace__workload_exec_failed_signal.
57 * XXX We need to handle this more appropriately, emitting an error, etc.
59 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
60 siginfo_t *info __maybe_unused,
61 void *ucontext __maybe_unused)
63 /* workload_exec_errno = info->si_value.sival_int; */
67 static int __write_tracing_file(const char *name, const char *val, bool append)
71 ssize_t size = strlen(val);
76 file = get_tracing_file(name);
78 pr_debug("cannot get tracing file: %s\n", name);
87 fd = open(file, flags);
89 pr_debug("cannot open tracing file: %s: %s\n",
90 name, str_error_r(errno, errbuf, sizeof(errbuf)));
95 * Copy the original value and append a '\n'. Without this,
96 * the kernel can hide possible errors.
98 val_copy = strdup(val);
101 val_copy[size] = '\n';
103 if (write(fd, val_copy, size + 1) == size + 1)
106 pr_debug("write '%s' to tracing/%s failed: %s\n",
107 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
113 put_tracing_file(file);
117 static int write_tracing_file(const char *name, const char *val)
119 return __write_tracing_file(name, val, false);
122 static int append_tracing_file(const char *name, const char *val)
124 return __write_tracing_file(name, val, true);
127 static int reset_tracing_cpu(void);
128 static void reset_tracing_filters(void);
130 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
132 if (write_tracing_file("tracing_on", "0") < 0)
135 if (write_tracing_file("current_tracer", "nop") < 0)
138 if (write_tracing_file("set_ftrace_pid", " ") < 0)
141 if (reset_tracing_cpu() < 0)
144 if (write_tracing_file("max_graph_depth", "0") < 0)
147 reset_tracing_filters();
151 static int set_tracing_pid(struct perf_ftrace *ftrace)
156 if (target__has_cpu(&ftrace->target))
159 for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
160 scnprintf(buf, sizeof(buf), "%d",
161 ftrace->evlist->threads->map[i]);
162 if (append_tracing_file("set_ftrace_pid", buf) < 0)
168 static int set_tracing_cpumask(struct cpu_map *cpumap)
175 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
176 mask_size = (last_cpu + 3) / 4 + 1;
177 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
179 cpumask = malloc(mask_size);
180 if (cpumask == NULL) {
181 pr_debug("failed to allocate cpu mask\n");
185 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
187 ret = write_tracing_file("tracing_cpumask", cpumask);
193 static int set_tracing_cpu(struct perf_ftrace *ftrace)
195 struct cpu_map *cpumap = ftrace->evlist->cpus;
197 if (!target__has_cpu(&ftrace->target))
200 return set_tracing_cpumask(cpumap);
203 static int reset_tracing_cpu(void)
205 struct cpu_map *cpumap = cpu_map__new(NULL);
208 ret = set_tracing_cpumask(cpumap);
209 cpu_map__put(cpumap);
213 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
215 struct filter_entry *pos;
217 list_for_each_entry(pos, funcs, list) {
218 if (append_tracing_file(filter_file, pos->name) < 0)
225 static int set_tracing_filters(struct perf_ftrace *ftrace)
229 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
233 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
237 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
241 /* old kernels do not have this filter */
242 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
247 static void reset_tracing_filters(void)
249 write_tracing_file("set_ftrace_filter", " ");
250 write_tracing_file("set_ftrace_notrace", " ");
251 write_tracing_file("set_graph_function", " ");
252 write_tracing_file("set_graph_notrace", " ");
255 static int set_tracing_depth(struct perf_ftrace *ftrace)
259 if (ftrace->graph_depth == 0)
262 if (ftrace->graph_depth < 0) {
263 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
267 snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
269 if (write_tracing_file("max_graph_depth", buf) < 0)
275 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
280 struct pollfd pollfd = {
284 if (geteuid() != 0) {
285 pr_err("ftrace only works for root!\n");
289 signal(SIGINT, sig_handler);
290 signal(SIGUSR1, sig_handler);
291 signal(SIGCHLD, sig_handler);
292 signal(SIGPIPE, sig_handler);
294 if (reset_tracing_files(ftrace) < 0) {
295 pr_err("failed to reset ftrace\n");
299 /* reset ftrace buffer */
300 if (write_tracing_file("trace", "0") < 0)
303 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
304 &ftrace->target, argv, false,
305 ftrace__workload_exec_failed_signal) < 0) {
309 if (set_tracing_pid(ftrace) < 0) {
310 pr_err("failed to set ftrace pid\n");
314 if (set_tracing_cpu(ftrace) < 0) {
315 pr_err("failed to set tracing cpumask\n");
319 if (set_tracing_filters(ftrace) < 0) {
320 pr_err("failed to set tracing filters\n");
324 if (set_tracing_depth(ftrace) < 0) {
325 pr_err("failed to set graph depth\n");
329 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
330 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
336 trace_file = get_tracing_file("trace_pipe");
338 pr_err("failed to open trace_pipe\n");
342 trace_fd = open(trace_file, O_RDONLY);
344 put_tracing_file(trace_file);
347 pr_err("failed to open trace_pipe\n");
351 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
352 pollfd.fd = trace_fd;
354 if (write_tracing_file("tracing_on", "1") < 0) {
355 pr_err("can't enable tracing\n");
359 perf_evlist__start_workload(ftrace->evlist);
362 if (poll(&pollfd, 1, -1) < 0)
365 if (pollfd.revents & POLLIN) {
366 int n = read(trace_fd, buf, sizeof(buf));
369 if (fwrite(buf, n, 1, stdout) != 1)
374 write_tracing_file("tracing_on", "0");
376 /* read remaining buffer contents */
378 int n = read(trace_fd, buf, sizeof(buf));
381 if (fwrite(buf, n, 1, stdout) != 1)
388 reset_tracing_files(ftrace);
390 return done ? 0 : -1;
393 static int perf_ftrace_config(const char *var, const char *value, void *cb)
395 struct perf_ftrace *ftrace = cb;
397 if (!strstarts(var, "ftrace."))
400 if (strcmp(var, "ftrace.tracer"))
403 if (!strcmp(value, "function_graph") ||
404 !strcmp(value, "function")) {
405 ftrace->tracer = value;
409 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
413 static int parse_filter_func(const struct option *opt, const char *str,
414 int unset __maybe_unused)
416 struct list_head *head = opt->value;
417 struct filter_entry *entry;
419 entry = malloc(sizeof(*entry) + strlen(str) + 1);
423 strcpy(entry->name, str);
424 list_add_tail(&entry->list, head);
429 static void delete_filter_func(struct list_head *head)
431 struct filter_entry *pos, *tmp;
433 list_for_each_entry_safe(pos, tmp, head, list) {
434 list_del(&pos->list);
439 int cmd_ftrace(int argc, const char **argv)
442 struct perf_ftrace ftrace = {
443 .tracer = DEFAULT_TRACER,
444 .target = { .uid = UINT_MAX, },
446 const char * const ftrace_usage[] = {
447 "perf ftrace [<options>] [<command>]",
448 "perf ftrace [<options>] -- <command> [<options>]",
451 const struct option ftrace_options[] = {
452 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
453 "tracer to use: function_graph(default) or function"),
454 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
455 "trace on existing process id"),
456 OPT_INCR('v', "verbose", &verbose,
458 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
459 "system-wide collection from all CPUs"),
460 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
461 "list of cpus to monitor"),
462 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
463 "trace given functions only", parse_filter_func),
464 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
465 "do not trace given functions", parse_filter_func),
466 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
467 "Set graph filter on given functions", parse_filter_func),
468 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
469 "Set nograph filter on given functions", parse_filter_func),
470 OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
471 "Max depth for function graph tracer"),
475 INIT_LIST_HEAD(&ftrace.filters);
476 INIT_LIST_HEAD(&ftrace.notrace);
477 INIT_LIST_HEAD(&ftrace.graph_funcs);
478 INIT_LIST_HEAD(&ftrace.nograph_funcs);
480 ret = perf_config(perf_ftrace_config, &ftrace);
484 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
485 PARSE_OPT_STOP_AT_NON_OPTION);
486 if (!argc && target__none(&ftrace.target))
487 usage_with_options(ftrace_usage, ftrace_options);
489 ret = target__validate(&ftrace.target);
493 target__strerror(&ftrace.target, ret, errbuf, 512);
494 pr_err("%s\n", errbuf);
495 goto out_delete_filters;
498 ftrace.evlist = perf_evlist__new();
499 if (ftrace.evlist == NULL) {
501 goto out_delete_filters;
504 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
506 goto out_delete_evlist;
508 ret = __cmd_ftrace(&ftrace, argc, argv);
511 perf_evlist__delete(ftrace.evlist);
514 delete_filter_func(&ftrace.filters);
515 delete_filter_func(&ftrace.notrace);
516 delete_filter_func(&ftrace.graph_funcs);
517 delete_filter_func(&ftrace.nograph_funcs);