staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / perf / builtin-ftrace.c
blob01931281040563358793d74151233c8989df1082
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * builtin-ftrace.c
5 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 */
8 #include "builtin.h"
9 #include "perf.h"
11 #include <errno.h>
12 #include <unistd.h>
13 #include <signal.h>
14 #include <fcntl.h>
15 #include <poll.h>
17 #include "debug.h"
18 #include <subcmd/parse-options.h>
19 #include <api/fs/tracing_path.h>
20 #include "evlist.h"
21 #include "target.h"
22 #include "cpumap.h"
23 #include "thread_map.h"
24 #include "util/config.h"
27 #define DEFAULT_TRACER "function_graph"
29 struct perf_ftrace {
30 struct perf_evlist *evlist;
31 struct target target;
32 const char *tracer;
33 struct list_head filters;
34 struct list_head notrace;
35 struct list_head graph_funcs;
36 struct list_head nograph_funcs;
37 int graph_depth;
40 struct filter_entry {
41 struct list_head list;
42 char name[];
45 static bool done;
47 static void sig_handler(int sig __maybe_unused)
49 done = true;
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; */
64 done = true;
67 static int __write_tracing_file(const char *name, const char *val, bool append)
69 char *file;
70 int fd, ret = -1;
71 ssize_t size = strlen(val);
72 int flags = O_WRONLY;
73 char errbuf[512];
74 char *val_copy;
76 file = get_tracing_file(name);
77 if (!file) {
78 pr_debug("cannot get tracing file: %s\n", name);
79 return -1;
82 if (append)
83 flags |= O_APPEND;
84 else
85 flags |= O_TRUNC;
87 fd = open(file, flags);
88 if (fd < 0) {
89 pr_debug("cannot open tracing file: %s: %s\n",
90 name, str_error_r(errno, errbuf, sizeof(errbuf)));
91 goto out;
95 * Copy the original value and append a '\n'. Without this,
96 * the kernel can hide possible errors.
98 val_copy = strdup(val);
99 if (!val_copy)
100 goto out_close;
101 val_copy[size] = '\n';
103 if (write(fd, val_copy, size + 1) == size + 1)
104 ret = 0;
105 else
106 pr_debug("write '%s' to tracing/%s failed: %s\n",
107 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
109 free(val_copy);
110 out_close:
111 close(fd);
112 out:
113 put_tracing_file(file);
114 return ret;
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)
133 return -1;
135 if (write_tracing_file("current_tracer", "nop") < 0)
136 return -1;
138 if (write_tracing_file("set_ftrace_pid", " ") < 0)
139 return -1;
141 if (reset_tracing_cpu() < 0)
142 return -1;
144 if (write_tracing_file("max_graph_depth", "0") < 0)
145 return -1;
147 reset_tracing_filters();
148 return 0;
151 static int set_tracing_pid(struct perf_ftrace *ftrace)
153 int i;
154 char buf[16];
156 if (target__has_cpu(&ftrace->target))
157 return 0;
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)
163 return -1;
165 return 0;
168 static int set_tracing_cpumask(struct cpu_map *cpumap)
170 char *cpumask;
171 size_t mask_size;
172 int ret;
173 int last_cpu;
175 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
176 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
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");
182 return -1;
185 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
187 ret = write_tracing_file("tracing_cpumask", cpumask);
189 free(cpumask);
190 return ret;
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))
198 return 0;
200 return set_tracing_cpumask(cpumap);
203 static int reset_tracing_cpu(void)
205 struct cpu_map *cpumap = cpu_map__new(NULL);
206 int ret;
208 ret = set_tracing_cpumask(cpumap);
209 cpu_map__put(cpumap);
210 return ret;
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)
219 return -1;
222 return 0;
225 static int set_tracing_filters(struct perf_ftrace *ftrace)
227 int ret;
229 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
230 if (ret < 0)
231 return ret;
233 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
234 if (ret < 0)
235 return ret;
237 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
238 if (ret < 0)
239 return ret;
241 /* old kernels do not have this filter */
242 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
244 return ret;
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)
257 char buf[16];
259 if (ftrace->graph_depth == 0)
260 return 0;
262 if (ftrace->graph_depth < 0) {
263 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
264 return -1;
267 snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
269 if (write_tracing_file("max_graph_depth", buf) < 0)
270 return -1;
272 return 0;
275 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
277 char *trace_file;
278 int trace_fd;
279 char buf[4096];
280 struct pollfd pollfd = {
281 .events = POLLIN,
284 if (geteuid() != 0) {
285 pr_err("ftrace only works for root!\n");
286 return -1;
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");
296 goto out;
299 /* reset ftrace buffer */
300 if (write_tracing_file("trace", "0") < 0)
301 goto out;
303 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
304 &ftrace->target, argv, false,
305 ftrace__workload_exec_failed_signal) < 0) {
306 goto out;
309 if (set_tracing_pid(ftrace) < 0) {
310 pr_err("failed to set ftrace pid\n");
311 goto out_reset;
314 if (set_tracing_cpu(ftrace) < 0) {
315 pr_err("failed to set tracing cpumask\n");
316 goto out_reset;
319 if (set_tracing_filters(ftrace) < 0) {
320 pr_err("failed to set tracing filters\n");
321 goto out_reset;
324 if (set_tracing_depth(ftrace) < 0) {
325 pr_err("failed to set graph depth\n");
326 goto out_reset;
329 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
330 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
331 goto out_reset;
334 setup_pager();
336 trace_file = get_tracing_file("trace_pipe");
337 if (!trace_file) {
338 pr_err("failed to open trace_pipe\n");
339 goto out_reset;
342 trace_fd = open(trace_file, O_RDONLY);
344 put_tracing_file(trace_file);
346 if (trace_fd < 0) {
347 pr_err("failed to open trace_pipe\n");
348 goto out_reset;
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");
356 goto out_close_fd;
359 perf_evlist__start_workload(ftrace->evlist);
361 while (!done) {
362 if (poll(&pollfd, 1, -1) < 0)
363 break;
365 if (pollfd.revents & POLLIN) {
366 int n = read(trace_fd, buf, sizeof(buf));
367 if (n < 0)
368 break;
369 if (fwrite(buf, n, 1, stdout) != 1)
370 break;
374 write_tracing_file("tracing_on", "0");
376 /* read remaining buffer contents */
377 while (true) {
378 int n = read(trace_fd, buf, sizeof(buf));
379 if (n <= 0)
380 break;
381 if (fwrite(buf, n, 1, stdout) != 1)
382 break;
385 out_close_fd:
386 close(trace_fd);
387 out_reset:
388 reset_tracing_files(ftrace);
389 out:
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."))
398 return 0;
400 if (strcmp(var, "ftrace.tracer"))
401 return -1;
403 if (!strcmp(value, "function_graph") ||
404 !strcmp(value, "function")) {
405 ftrace->tracer = value;
406 return 0;
409 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
410 return -1;
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);
420 if (entry == NULL)
421 return -ENOMEM;
423 strcpy(entry->name, str);
424 list_add_tail(&entry->list, head);
426 return 0;
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_init(&pos->list);
435 free(pos);
439 int cmd_ftrace(int argc, const char **argv)
441 int ret;
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>]",
449 NULL
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,
457 "be more 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"),
472 OPT_END()
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);
481 if (ret < 0)
482 return -1;
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);
490 if (ret) {
491 char errbuf[512];
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) {
500 ret = -ENOMEM;
501 goto out_delete_filters;
504 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
505 if (ret < 0)
506 goto out_delete_evlist;
508 ret = __cmd_ftrace(&ftrace, argc, argv);
510 out_delete_evlist:
511 perf_evlist__delete(ftrace.evlist);
513 out_delete_filters:
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);
519 return ret;