xref: /openbmc/linux/tools/perf/builtin-ftrace.c (revision 8520a98dbab61e9e340cdfb72dd17ccc8a98961e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * builtin-ftrace.c
4  *
5  * Copyright (c) 2013  LG Electronics,  Namhyung Kim <namhyung@kernel.org>
6  */
7 
8 #include "builtin.h"
9 
10 #include <errno.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <fcntl.h>
14 #include <poll.h>
15 #include <linux/capability.h>
16 #include <linux/string.h>
17 
18 #include "debug.h"
19 #include <subcmd/pager.h>
20 #include <subcmd/parse-options.h>
21 #include <api/fs/tracing_path.h>
22 #include "evlist.h"
23 #include "target.h"
24 #include "cpumap.h"
25 #include "thread_map.h"
26 #include "util/cap.h"
27 #include "util/config.h"
28 
29 #define DEFAULT_TRACER  "function_graph"
30 
31 struct perf_ftrace {
32 	struct evlist		*evlist;
33 	struct target		target;
34 	const char		*tracer;
35 	struct list_head	filters;
36 	struct list_head	notrace;
37 	struct list_head	graph_funcs;
38 	struct list_head	nograph_funcs;
39 	int			graph_depth;
40 };
41 
42 struct filter_entry {
43 	struct list_head	list;
44 	char			name[];
45 };
46 
47 static bool done;
48 
49 static void sig_handler(int sig __maybe_unused)
50 {
51 	done = true;
52 }
53 
54 /*
55  * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
56  * we asked by setting its exec_error to the function below,
57  * ftrace__workload_exec_failed_signal.
58  *
59  * XXX We need to handle this more appropriately, emitting an error, etc.
60  */
61 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
62 						siginfo_t *info __maybe_unused,
63 						void *ucontext __maybe_unused)
64 {
65 	/* workload_exec_errno = info->si_value.sival_int; */
66 	done = true;
67 }
68 
69 static int __write_tracing_file(const char *name, const char *val, bool append)
70 {
71 	char *file;
72 	int fd, ret = -1;
73 	ssize_t size = strlen(val);
74 	int flags = O_WRONLY;
75 	char errbuf[512];
76 	char *val_copy;
77 
78 	file = get_tracing_file(name);
79 	if (!file) {
80 		pr_debug("cannot get tracing file: %s\n", name);
81 		return -1;
82 	}
83 
84 	if (append)
85 		flags |= O_APPEND;
86 	else
87 		flags |= O_TRUNC;
88 
89 	fd = open(file, flags);
90 	if (fd < 0) {
91 		pr_debug("cannot open tracing file: %s: %s\n",
92 			 name, str_error_r(errno, errbuf, sizeof(errbuf)));
93 		goto out;
94 	}
95 
96 	/*
97 	 * Copy the original value and append a '\n'. Without this,
98 	 * the kernel can hide possible errors.
99 	 */
100 	val_copy = strdup(val);
101 	if (!val_copy)
102 		goto out_close;
103 	val_copy[size] = '\n';
104 
105 	if (write(fd, val_copy, size + 1) == size + 1)
106 		ret = 0;
107 	else
108 		pr_debug("write '%s' to tracing/%s failed: %s\n",
109 			 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
110 
111 	free(val_copy);
112 out_close:
113 	close(fd);
114 out:
115 	put_tracing_file(file);
116 	return ret;
117 }
118 
119 static int write_tracing_file(const char *name, const char *val)
120 {
121 	return __write_tracing_file(name, val, false);
122 }
123 
124 static int append_tracing_file(const char *name, const char *val)
125 {
126 	return __write_tracing_file(name, val, true);
127 }
128 
129 static int reset_tracing_cpu(void);
130 static void reset_tracing_filters(void);
131 
132 static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
133 {
134 	if (write_tracing_file("tracing_on", "0") < 0)
135 		return -1;
136 
137 	if (write_tracing_file("current_tracer", "nop") < 0)
138 		return -1;
139 
140 	if (write_tracing_file("set_ftrace_pid", " ") < 0)
141 		return -1;
142 
143 	if (reset_tracing_cpu() < 0)
144 		return -1;
145 
146 	if (write_tracing_file("max_graph_depth", "0") < 0)
147 		return -1;
148 
149 	reset_tracing_filters();
150 	return 0;
151 }
152 
153 static int set_tracing_pid(struct perf_ftrace *ftrace)
154 {
155 	int i;
156 	char buf[16];
157 
158 	if (target__has_cpu(&ftrace->target))
159 		return 0;
160 
161 	for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
162 		scnprintf(buf, sizeof(buf), "%d",
163 			  ftrace->evlist->core.threads->map[i]);
164 		if (append_tracing_file("set_ftrace_pid", buf) < 0)
165 			return -1;
166 	}
167 	return 0;
168 }
169 
170 static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
171 {
172 	char *cpumask;
173 	size_t mask_size;
174 	int ret;
175 	int last_cpu;
176 
177 	last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
178 	mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
179 	mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
180 
181 	cpumask = malloc(mask_size);
182 	if (cpumask == NULL) {
183 		pr_debug("failed to allocate cpu mask\n");
184 		return -1;
185 	}
186 
187 	cpu_map__snprint_mask(cpumap, cpumask, mask_size);
188 
189 	ret = write_tracing_file("tracing_cpumask", cpumask);
190 
191 	free(cpumask);
192 	return ret;
193 }
194 
195 static int set_tracing_cpu(struct perf_ftrace *ftrace)
196 {
197 	struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
198 
199 	if (!target__has_cpu(&ftrace->target))
200 		return 0;
201 
202 	return set_tracing_cpumask(cpumap);
203 }
204 
205 static int reset_tracing_cpu(void)
206 {
207 	struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
208 	int ret;
209 
210 	ret = set_tracing_cpumask(cpumap);
211 	perf_cpu_map__put(cpumap);
212 	return ret;
213 }
214 
215 static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
216 {
217 	struct filter_entry *pos;
218 
219 	list_for_each_entry(pos, funcs, list) {
220 		if (append_tracing_file(filter_file, pos->name) < 0)
221 			return -1;
222 	}
223 
224 	return 0;
225 }
226 
227 static int set_tracing_filters(struct perf_ftrace *ftrace)
228 {
229 	int ret;
230 
231 	ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
232 	if (ret < 0)
233 		return ret;
234 
235 	ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
236 	if (ret < 0)
237 		return ret;
238 
239 	ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
240 	if (ret < 0)
241 		return ret;
242 
243 	/* old kernels do not have this filter */
244 	__set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
245 
246 	return ret;
247 }
248 
249 static void reset_tracing_filters(void)
250 {
251 	write_tracing_file("set_ftrace_filter", " ");
252 	write_tracing_file("set_ftrace_notrace", " ");
253 	write_tracing_file("set_graph_function", " ");
254 	write_tracing_file("set_graph_notrace", " ");
255 }
256 
257 static int set_tracing_depth(struct perf_ftrace *ftrace)
258 {
259 	char buf[16];
260 
261 	if (ftrace->graph_depth == 0)
262 		return 0;
263 
264 	if (ftrace->graph_depth < 0) {
265 		pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
266 		return -1;
267 	}
268 
269 	snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
270 
271 	if (write_tracing_file("max_graph_depth", buf) < 0)
272 		return -1;
273 
274 	return 0;
275 }
276 
277 static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
278 {
279 	char *trace_file;
280 	int trace_fd;
281 	char buf[4096];
282 	struct pollfd pollfd = {
283 		.events = POLLIN,
284 	};
285 
286 	if (!perf_cap__capable(CAP_SYS_ADMIN)) {
287 		pr_err("ftrace only works for %s!\n",
288 #ifdef HAVE_LIBCAP_SUPPORT
289 		"users with the SYS_ADMIN capability"
290 #else
291 		"root"
292 #endif
293 		);
294 		return -1;
295 	}
296 
297 	signal(SIGINT, sig_handler);
298 	signal(SIGUSR1, sig_handler);
299 	signal(SIGCHLD, sig_handler);
300 	signal(SIGPIPE, sig_handler);
301 
302 	if (reset_tracing_files(ftrace) < 0) {
303 		pr_err("failed to reset ftrace\n");
304 		goto out;
305 	}
306 
307 	/* reset ftrace buffer */
308 	if (write_tracing_file("trace", "0") < 0)
309 		goto out;
310 
311 	if (argc && perf_evlist__prepare_workload(ftrace->evlist,
312 				&ftrace->target, argv, false,
313 				ftrace__workload_exec_failed_signal) < 0) {
314 		goto out;
315 	}
316 
317 	if (set_tracing_pid(ftrace) < 0) {
318 		pr_err("failed to set ftrace pid\n");
319 		goto out_reset;
320 	}
321 
322 	if (set_tracing_cpu(ftrace) < 0) {
323 		pr_err("failed to set tracing cpumask\n");
324 		goto out_reset;
325 	}
326 
327 	if (set_tracing_filters(ftrace) < 0) {
328 		pr_err("failed to set tracing filters\n");
329 		goto out_reset;
330 	}
331 
332 	if (set_tracing_depth(ftrace) < 0) {
333 		pr_err("failed to set graph depth\n");
334 		goto out_reset;
335 	}
336 
337 	if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
338 		pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
339 		goto out_reset;
340 	}
341 
342 	setup_pager();
343 
344 	trace_file = get_tracing_file("trace_pipe");
345 	if (!trace_file) {
346 		pr_err("failed to open trace_pipe\n");
347 		goto out_reset;
348 	}
349 
350 	trace_fd = open(trace_file, O_RDONLY);
351 
352 	put_tracing_file(trace_file);
353 
354 	if (trace_fd < 0) {
355 		pr_err("failed to open trace_pipe\n");
356 		goto out_reset;
357 	}
358 
359 	fcntl(trace_fd, F_SETFL, O_NONBLOCK);
360 	pollfd.fd = trace_fd;
361 
362 	if (write_tracing_file("tracing_on", "1") < 0) {
363 		pr_err("can't enable tracing\n");
364 		goto out_close_fd;
365 	}
366 
367 	perf_evlist__start_workload(ftrace->evlist);
368 
369 	while (!done) {
370 		if (poll(&pollfd, 1, -1) < 0)
371 			break;
372 
373 		if (pollfd.revents & POLLIN) {
374 			int n = read(trace_fd, buf, sizeof(buf));
375 			if (n < 0)
376 				break;
377 			if (fwrite(buf, n, 1, stdout) != 1)
378 				break;
379 		}
380 	}
381 
382 	write_tracing_file("tracing_on", "0");
383 
384 	/* read remaining buffer contents */
385 	while (true) {
386 		int n = read(trace_fd, buf, sizeof(buf));
387 		if (n <= 0)
388 			break;
389 		if (fwrite(buf, n, 1, stdout) != 1)
390 			break;
391 	}
392 
393 out_close_fd:
394 	close(trace_fd);
395 out_reset:
396 	reset_tracing_files(ftrace);
397 out:
398 	return done ? 0 : -1;
399 }
400 
401 static int perf_ftrace_config(const char *var, const char *value, void *cb)
402 {
403 	struct perf_ftrace *ftrace = cb;
404 
405 	if (!strstarts(var, "ftrace."))
406 		return 0;
407 
408 	if (strcmp(var, "ftrace.tracer"))
409 		return -1;
410 
411 	if (!strcmp(value, "function_graph") ||
412 	    !strcmp(value, "function")) {
413 		ftrace->tracer = value;
414 		return 0;
415 	}
416 
417 	pr_err("Please select \"function_graph\" (default) or \"function\"\n");
418 	return -1;
419 }
420 
421 static int parse_filter_func(const struct option *opt, const char *str,
422 			     int unset __maybe_unused)
423 {
424 	struct list_head *head = opt->value;
425 	struct filter_entry *entry;
426 
427 	entry = malloc(sizeof(*entry) + strlen(str) + 1);
428 	if (entry == NULL)
429 		return -ENOMEM;
430 
431 	strcpy(entry->name, str);
432 	list_add_tail(&entry->list, head);
433 
434 	return 0;
435 }
436 
437 static void delete_filter_func(struct list_head *head)
438 {
439 	struct filter_entry *pos, *tmp;
440 
441 	list_for_each_entry_safe(pos, tmp, head, list) {
442 		list_del_init(&pos->list);
443 		free(pos);
444 	}
445 }
446 
447 int cmd_ftrace(int argc, const char **argv)
448 {
449 	int ret;
450 	struct perf_ftrace ftrace = {
451 		.tracer = DEFAULT_TRACER,
452 		.target = { .uid = UINT_MAX, },
453 	};
454 	const char * const ftrace_usage[] = {
455 		"perf ftrace [<options>] [<command>]",
456 		"perf ftrace [<options>] -- <command> [<options>]",
457 		NULL
458 	};
459 	const struct option ftrace_options[] = {
460 	OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
461 		   "tracer to use: function_graph(default) or function"),
462 	OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
463 		   "trace on existing process id"),
464 	OPT_INCR('v', "verbose", &verbose,
465 		 "be more verbose"),
466 	OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
467 		    "system-wide collection from all CPUs"),
468 	OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
469 		    "list of cpus to monitor"),
470 	OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
471 		     "trace given functions only", parse_filter_func),
472 	OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
473 		     "do not trace given functions", parse_filter_func),
474 	OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
475 		     "Set graph filter on given functions", parse_filter_func),
476 	OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
477 		     "Set nograph filter on given functions", parse_filter_func),
478 	OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
479 		    "Max depth for function graph tracer"),
480 	OPT_END()
481 	};
482 
483 	INIT_LIST_HEAD(&ftrace.filters);
484 	INIT_LIST_HEAD(&ftrace.notrace);
485 	INIT_LIST_HEAD(&ftrace.graph_funcs);
486 	INIT_LIST_HEAD(&ftrace.nograph_funcs);
487 
488 	ret = perf_config(perf_ftrace_config, &ftrace);
489 	if (ret < 0)
490 		return -1;
491 
492 	argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
493 			    PARSE_OPT_STOP_AT_NON_OPTION);
494 	if (!argc && target__none(&ftrace.target))
495 		usage_with_options(ftrace_usage, ftrace_options);
496 
497 	ret = target__validate(&ftrace.target);
498 	if (ret) {
499 		char errbuf[512];
500 
501 		target__strerror(&ftrace.target, ret, errbuf, 512);
502 		pr_err("%s\n", errbuf);
503 		goto out_delete_filters;
504 	}
505 
506 	ftrace.evlist = evlist__new();
507 	if (ftrace.evlist == NULL) {
508 		ret = -ENOMEM;
509 		goto out_delete_filters;
510 	}
511 
512 	ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
513 	if (ret < 0)
514 		goto out_delete_evlist;
515 
516 	ret = __cmd_ftrace(&ftrace, argc, argv);
517 
518 out_delete_evlist:
519 	evlist__delete(ftrace.evlist);
520 
521 out_delete_filters:
522 	delete_filter_func(&ftrace.filters);
523 	delete_filter_func(&ftrace.notrace);
524 	delete_filter_func(&ftrace.graph_funcs);
525 	delete_filter_func(&ftrace.nograph_funcs);
526 
527 	return ret;
528 }
529