blob: 13abb737fd12d6da4ab017d3cac1a0f0c1fdf077 [file] [log] [blame]
Thomas Gleixner1c6bec52019-06-01 10:08:47 +02001// SPDX-License-Identifier: GPL-2.0-only
Namhyung Kimd01f4e82013-03-07 21:45:20 +09002/*
3 * builtin-ftrace.c
4 *
5 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
Namhyung Kimd01f4e82013-03-07 21:45:20 +09006 */
7
8#include "builtin.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +09009
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030010#include <errno.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090011#include <unistd.h>
12#include <signal.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030013#include <stdlib.h>
Namhyung Kima9af6be2017-02-24 10:12:48 +090014#include <fcntl.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030015#include <poll.h>
Igor Lubashevc766f3d2019-08-07 10:44:17 -040016#include <linux/capability.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030017#include <linux/string.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090018
19#include "debug.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030020#include <subcmd/pager.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090021#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo20a9ed22017-04-18 11:44:58 -030022#include <api/fs/tracing_path.h>
Namhyung Kimd01f4e82013-03-07 21:45:20 +090023#include "evlist.h"
24#include "target.h"
Namhyung Kimdc231032017-02-24 10:12:50 +090025#include "cpumap.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090026#include "thread_map.h"
Igor Lubashevc766f3d2019-08-07 10:44:17 -040027#include "util/cap.h"
Taeung Songb05d1092017-01-31 20:38:29 +090028#include "util/config.h"
Changbin Du846e1932020-08-08 10:31:27 +080029#include "util/units.h"
Changbin Dub1d84af2020-08-08 10:31:31 +080030#include "util/parse-sublevel-options.h"
Namhyung Kimd01f4e82013-03-07 21:45:20 +090031
Namhyung Kimd01f4e82013-03-07 21:45:20 +090032#define DEFAULT_TRACER "function_graph"
33
34struct perf_ftrace {
Jiri Olsa63503db2019-07-21 13:23:52 +020035 struct evlist *evlist;
Namhyung Kim78b83e82017-06-18 23:23:01 +090036 struct target target;
37 const char *tracer;
Changbin Dud6d81bf2020-08-08 10:31:25 +080038 bool list_avail_functions;
Namhyung Kim78b83e82017-06-18 23:23:01 +090039 struct list_head filters;
40 struct list_head notrace;
41 struct list_head graph_funcs;
42 struct list_head nograph_funcs;
Namhyung Kim1096c352017-06-18 23:23:02 +090043 int graph_depth;
Changbin Du846e1932020-08-08 10:31:27 +080044 unsigned long percpu_buffer_size;
Changbin Du5b3474722020-08-08 10:31:29 +080045 bool inherit;
Changbin Dub1d84af2020-08-08 10:31:31 +080046 int func_stack_trace;
Changbin Duc81fc342020-08-08 10:31:34 +080047 int func_irq_info;
Changbin Du38988f22020-08-08 10:31:32 +080048 int graph_nosleep_time;
Changbin Dud1bcf172020-08-08 10:31:33 +080049 int graph_noirqs;
Changbin Du59486fb2020-08-08 10:31:35 +080050 int graph_verbose;
Changbin Du00c85d52020-08-08 10:31:36 +080051 int graph_thresh;
Changbin Du6555c2f2020-08-08 10:31:38 +080052 unsigned int initial_delay;
Namhyung Kim78b83e82017-06-18 23:23:01 +090053};
54
55struct filter_entry {
56 struct list_head list;
57 char name[];
Namhyung Kimd01f4e82013-03-07 21:45:20 +090058};
59
Changbin Du51a09d82020-05-10 23:06:11 +080060static volatile int workload_exec_errno;
Namhyung Kimd01f4e82013-03-07 21:45:20 +090061static bool done;
62
63static void sig_handler(int sig __maybe_unused)
64{
65 done = true;
66}
67
68/*
69 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
70 * we asked by setting its exec_error to the function below,
71 * ftrace__workload_exec_failed_signal.
72 *
73 * XXX We need to handle this more appropriately, emitting an error, etc.
74 */
75static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
76 siginfo_t *info __maybe_unused,
77 void *ucontext __maybe_unused)
78{
Changbin Du51a09d82020-05-10 23:06:11 +080079 workload_exec_errno = info->si_value.sival_int;
Namhyung Kimd01f4e82013-03-07 21:45:20 +090080 done = true;
81}
82
Namhyung Kima9af6be2017-02-24 10:12:48 +090083static int __write_tracing_file(const char *name, const char *val, bool append)
Namhyung Kimd01f4e82013-03-07 21:45:20 +090084{
85 char *file;
86 int fd, ret = -1;
87 ssize_t size = strlen(val);
Namhyung Kima9af6be2017-02-24 10:12:48 +090088 int flags = O_WRONLY;
Namhyung Kime7bd9ba2017-06-18 23:22:59 +090089 char errbuf[512];
Changbin Du63cd02d2018-02-19 10:33:29 +080090 char *val_copy;
Namhyung Kimd01f4e82013-03-07 21:45:20 +090091
92 file = get_tracing_file(name);
93 if (!file) {
94 pr_debug("cannot get tracing file: %s\n", name);
95 return -1;
96 }
97
Namhyung Kima9af6be2017-02-24 10:12:48 +090098 if (append)
99 flags |= O_APPEND;
100 else
101 flags |= O_TRUNC;
102
103 fd = open(file, flags);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900104 if (fd < 0) {
Namhyung Kime7bd9ba2017-06-18 23:22:59 +0900105 pr_debug("cannot open tracing file: %s: %s\n",
106 name, str_error_r(errno, errbuf, sizeof(errbuf)));
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900107 goto out;
108 }
109
Changbin Du63cd02d2018-02-19 10:33:29 +0800110 /*
111 * Copy the original value and append a '\n'. Without this,
112 * the kernel can hide possible errors.
113 */
114 val_copy = strdup(val);
115 if (!val_copy)
116 goto out_close;
117 val_copy[size] = '\n';
118
119 if (write(fd, val_copy, size + 1) == size + 1)
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900120 ret = 0;
121 else
Namhyung Kime7bd9ba2017-06-18 23:22:59 +0900122 pr_debug("write '%s' to tracing/%s failed: %s\n",
123 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900124
Changbin Du63cd02d2018-02-19 10:33:29 +0800125 free(val_copy);
126out_close:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900127 close(fd);
128out:
129 put_tracing_file(file);
130 return ret;
131}
132
Namhyung Kima9af6be2017-02-24 10:12:48 +0900133static int write_tracing_file(const char *name, const char *val)
134{
135 return __write_tracing_file(name, val, false);
136}
137
138static int append_tracing_file(const char *name, const char *val)
139{
140 return __write_tracing_file(name, val, true);
141}
142
Changbin Dud6d81bf2020-08-08 10:31:25 +0800143static int read_tracing_file_to_stdout(const char *name)
144{
145 char buf[4096];
146 char *file;
147 int fd;
148 int ret = -1;
149
150 file = get_tracing_file(name);
151 if (!file) {
152 pr_debug("cannot get tracing file: %s\n", name);
153 return -1;
154 }
155
156 fd = open(file, O_RDONLY);
157 if (fd < 0) {
158 pr_debug("cannot open tracing file: %s: %s\n",
159 name, str_error_r(errno, buf, sizeof(buf)));
160 goto out;
161 }
162
163 /* read contents to stdout */
164 while (true) {
165 int n = read(fd, buf, sizeof(buf));
166 if (n == 0)
167 break;
168 else if (n < 0)
169 goto out_close;
170
171 if (fwrite(buf, n, 1, stdout) != 1)
172 goto out_close;
173 }
174 ret = 0;
175
176out_close:
177 close(fd);
178out:
179 put_tracing_file(file);
180 return ret;
181}
182
Changbin Du68faab02020-08-08 10:31:26 +0800183static int write_tracing_file_int(const char *name, int value)
184{
185 char buf[16];
186
187 snprintf(buf, sizeof(buf), "%d", value);
188 if (write_tracing_file(name, buf) < 0)
189 return -1;
190
191 return 0;
192}
193
Changbin Du5b3474722020-08-08 10:31:29 +0800194static int write_tracing_option_file(const char *name, const char *val)
195{
196 char *file;
197 int ret;
198
199 if (asprintf(&file, "options/%s", name) < 0)
200 return -1;
201
202 ret = __write_tracing_file(file, val, false);
203 free(file);
204 return ret;
205}
206
Namhyung Kimdc231032017-02-24 10:12:50 +0900207static int reset_tracing_cpu(void);
Namhyung Kim78b83e82017-06-18 23:23:01 +0900208static void reset_tracing_filters(void);
Namhyung Kimdc231032017-02-24 10:12:50 +0900209
Changbin Du5b3474722020-08-08 10:31:29 +0800210static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
211{
212 write_tracing_option_file("function-fork", "0");
Changbin Dub1d84af2020-08-08 10:31:31 +0800213 write_tracing_option_file("func_stack_trace", "0");
Changbin Du38988f22020-08-08 10:31:32 +0800214 write_tracing_option_file("sleep-time", "1");
Changbin Dud1bcf172020-08-08 10:31:33 +0800215 write_tracing_option_file("funcgraph-irqs", "1");
Changbin Du59486fb2020-08-08 10:31:35 +0800216 write_tracing_option_file("funcgraph-proc", "0");
217 write_tracing_option_file("funcgraph-abstime", "0");
218 write_tracing_option_file("latency-format", "0");
Changbin Duc81fc342020-08-08 10:31:34 +0800219 write_tracing_option_file("irq-info", "0");
Changbin Du5b3474722020-08-08 10:31:29 +0800220}
221
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900222static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
223{
224 if (write_tracing_file("tracing_on", "0") < 0)
225 return -1;
226
227 if (write_tracing_file("current_tracer", "nop") < 0)
228 return -1;
229
230 if (write_tracing_file("set_ftrace_pid", " ") < 0)
231 return -1;
232
Namhyung Kimdc231032017-02-24 10:12:50 +0900233 if (reset_tracing_cpu() < 0)
234 return -1;
235
Namhyung Kim1096c352017-06-18 23:23:02 +0900236 if (write_tracing_file("max_graph_depth", "0") < 0)
237 return -1;
238
Changbin Du00c85d52020-08-08 10:31:36 +0800239 if (write_tracing_file("tracing_thresh", "0") < 0)
240 return -1;
241
Namhyung Kim78b83e82017-06-18 23:23:01 +0900242 reset_tracing_filters();
Changbin Du5b3474722020-08-08 10:31:29 +0800243 reset_tracing_options(ftrace);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900244 return 0;
245}
246
Namhyung Kima9af6be2017-02-24 10:12:48 +0900247static int set_tracing_pid(struct perf_ftrace *ftrace)
248{
249 int i;
250 char buf[16];
251
252 if (target__has_cpu(&ftrace->target))
253 return 0;
254
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200255 for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
Namhyung Kima9af6be2017-02-24 10:12:48 +0900256 scnprintf(buf, sizeof(buf), "%d",
Jiri Olsa03617c22019-07-21 13:24:42 +0200257 ftrace->evlist->core.threads->map[i]);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900258 if (append_tracing_file("set_ftrace_pid", buf) < 0)
259 return -1;
260 }
261 return 0;
262}
263
Jiri Olsaf8548392019-07-21 13:23:49 +0200264static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
Namhyung Kimdc231032017-02-24 10:12:50 +0900265{
266 char *cpumask;
267 size_t mask_size;
268 int ret;
269 int last_cpu;
270
271 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
He Zhecf30ae72019-08-02 16:29:51 +0800272 mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
Namhyung Kimdc231032017-02-24 10:12:50 +0900273 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
274
275 cpumask = malloc(mask_size);
276 if (cpumask == NULL) {
277 pr_debug("failed to allocate cpu mask\n");
278 return -1;
279 }
280
281 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
282
283 ret = write_tracing_file("tracing_cpumask", cpumask);
284
285 free(cpumask);
286 return ret;
287}
288
289static int set_tracing_cpu(struct perf_ftrace *ftrace)
290{
Jiri Olsaf72f9012019-07-21 13:24:41 +0200291 struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus;
Namhyung Kimdc231032017-02-24 10:12:50 +0900292
293 if (!target__has_cpu(&ftrace->target))
294 return 0;
295
296 return set_tracing_cpumask(cpumap);
297}
298
Changbin Dub1d84af2020-08-08 10:31:31 +0800299static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
300{
301 if (!ftrace->func_stack_trace)
302 return 0;
303
304 if (write_tracing_option_file("func_stack_trace", "1") < 0)
305 return -1;
306
307 return 0;
308}
309
Changbin Duc81fc342020-08-08 10:31:34 +0800310static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
311{
312 if (!ftrace->func_irq_info)
313 return 0;
314
315 if (write_tracing_option_file("irq-info", "1") < 0)
316 return -1;
317
318 return 0;
319}
320
Namhyung Kimdc231032017-02-24 10:12:50 +0900321static int reset_tracing_cpu(void)
322{
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200323 struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
Namhyung Kimdc231032017-02-24 10:12:50 +0900324 int ret;
325
326 ret = set_tracing_cpumask(cpumap);
Jiri Olsa38f01d82019-07-21 13:24:17 +0200327 perf_cpu_map__put(cpumap);
Namhyung Kimdc231032017-02-24 10:12:50 +0900328 return ret;
329}
330
Namhyung Kim78b83e82017-06-18 23:23:01 +0900331static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
332{
333 struct filter_entry *pos;
334
335 list_for_each_entry(pos, funcs, list) {
336 if (append_tracing_file(filter_file, pos->name) < 0)
337 return -1;
338 }
339
340 return 0;
341}
342
343static int set_tracing_filters(struct perf_ftrace *ftrace)
344{
345 int ret;
346
347 ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
348 if (ret < 0)
349 return ret;
350
351 ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
352 if (ret < 0)
353 return ret;
354
355 ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
356 if (ret < 0)
357 return ret;
358
359 /* old kernels do not have this filter */
360 __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
361
362 return ret;
363}
364
365static void reset_tracing_filters(void)
366{
367 write_tracing_file("set_ftrace_filter", " ");
368 write_tracing_file("set_ftrace_notrace", " ");
369 write_tracing_file("set_graph_function", " ");
370 write_tracing_file("set_graph_notrace", " ");
371}
372
Namhyung Kim1096c352017-06-18 23:23:02 +0900373static int set_tracing_depth(struct perf_ftrace *ftrace)
374{
Namhyung Kim1096c352017-06-18 23:23:02 +0900375 if (ftrace->graph_depth == 0)
376 return 0;
377
378 if (ftrace->graph_depth < 0) {
379 pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
380 return -1;
381 }
382
Changbin Du68faab02020-08-08 10:31:26 +0800383 if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
Namhyung Kim1096c352017-06-18 23:23:02 +0900384 return -1;
385
386 return 0;
387}
388
Changbin Du846e1932020-08-08 10:31:27 +0800389static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
390{
391 int ret;
392
393 if (ftrace->percpu_buffer_size == 0)
394 return 0;
395
396 ret = write_tracing_file_int("buffer_size_kb",
397 ftrace->percpu_buffer_size / 1024);
398 if (ret < 0)
399 return ret;
400
401 return 0;
402}
403
Changbin Du5b3474722020-08-08 10:31:29 +0800404static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
405{
406 if (!ftrace->inherit)
407 return 0;
408
409 if (write_tracing_option_file("function-fork", "1") < 0)
410 return -1;
411
412 return 0;
413}
414
Changbin Du38988f22020-08-08 10:31:32 +0800415static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
416{
417 if (!ftrace->graph_nosleep_time)
418 return 0;
419
420 if (write_tracing_option_file("sleep-time", "0") < 0)
421 return -1;
422
423 return 0;
424}
425
Changbin Dud1bcf172020-08-08 10:31:33 +0800426static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
427{
428 if (!ftrace->graph_noirqs)
429 return 0;
430
431 if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
432 return -1;
433
434 return 0;
435}
436
Changbin Du59486fb2020-08-08 10:31:35 +0800437static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
438{
439 if (!ftrace->graph_verbose)
440 return 0;
441
442 if (write_tracing_option_file("funcgraph-proc", "1") < 0)
443 return -1;
444
445 if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
446 return -1;
447
448 if (write_tracing_option_file("latency-format", "1") < 0)
449 return -1;
450
451 return 0;
452}
453
Changbin Du00c85d52020-08-08 10:31:36 +0800454static int set_tracing_thresh(struct perf_ftrace *ftrace)
455{
456 int ret;
457
458 if (ftrace->graph_thresh == 0)
459 return 0;
460
461 ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
462 if (ret < 0)
463 return ret;
464
465 return 0;
466}
467
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900468static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
469{
470 char *trace_file;
471 int trace_fd;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900472 char buf[4096];
473 struct pollfd pollfd = {
474 .events = POLLIN,
475 };
476
Alexey Budankov6b3e0e22020-04-02 11:47:35 +0300477 if (!(perf_cap__capable(CAP_PERFMON) ||
478 perf_cap__capable(CAP_SYS_ADMIN))) {
Arnaldo Carvalho de Melo73e5de72019-08-12 17:27:11 -0300479 pr_err("ftrace only works for %s!\n",
480#ifdef HAVE_LIBCAP_SUPPORT
Alexey Budankov6b3e0e22020-04-02 11:47:35 +0300481 "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
Arnaldo Carvalho de Melo73e5de72019-08-12 17:27:11 -0300482#else
483 "root"
484#endif
485 );
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900486 return -1;
487 }
488
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900489 signal(SIGINT, sig_handler);
490 signal(SIGUSR1, sig_handler);
491 signal(SIGCHLD, sig_handler);
Namhyung Kim58335962017-02-24 10:12:51 +0900492 signal(SIGPIPE, sig_handler);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900493
Changbin Dud6d81bf2020-08-08 10:31:25 +0800494 if (ftrace->list_avail_functions)
495 return read_tracing_file_to_stdout("available_filter_functions");
496
Changbin Du63cd02d2018-02-19 10:33:29 +0800497 if (reset_tracing_files(ftrace) < 0) {
498 pr_err("failed to reset ftrace\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900499 goto out;
Changbin Du63cd02d2018-02-19 10:33:29 +0800500 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900501
502 /* reset ftrace buffer */
503 if (write_tracing_file("trace", "0") < 0)
504 goto out;
505
Namhyung Kima9af6be2017-02-24 10:12:48 +0900506 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
507 &ftrace->target, argv, false,
508 ftrace__workload_exec_failed_signal) < 0) {
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900509 goto out;
Namhyung Kima9af6be2017-02-24 10:12:48 +0900510 }
511
512 if (set_tracing_pid(ftrace) < 0) {
513 pr_err("failed to set ftrace pid\n");
514 goto out_reset;
515 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900516
Namhyung Kimdc231032017-02-24 10:12:50 +0900517 if (set_tracing_cpu(ftrace) < 0) {
518 pr_err("failed to set tracing cpumask\n");
519 goto out_reset;
520 }
521
Changbin Dub1d84af2020-08-08 10:31:31 +0800522 if (set_tracing_func_stack_trace(ftrace) < 0) {
523 pr_err("failed to set tracing option func_stack_trace\n");
524 goto out_reset;
525 }
526
Changbin Duc81fc342020-08-08 10:31:34 +0800527 if (set_tracing_func_irqinfo(ftrace) < 0) {
528 pr_err("failed to set tracing option irq-info\n");
529 goto out_reset;
530 }
531
Namhyung Kim78b83e82017-06-18 23:23:01 +0900532 if (set_tracing_filters(ftrace) < 0) {
533 pr_err("failed to set tracing filters\n");
534 goto out_reset;
535 }
536
Namhyung Kim1096c352017-06-18 23:23:02 +0900537 if (set_tracing_depth(ftrace) < 0) {
538 pr_err("failed to set graph depth\n");
539 goto out_reset;
540 }
541
Changbin Du846e1932020-08-08 10:31:27 +0800542 if (set_tracing_percpu_buffer_size(ftrace) < 0) {
543 pr_err("failed to set tracing per-cpu buffer size\n");
544 goto out_reset;
545 }
546
Changbin Du5b3474722020-08-08 10:31:29 +0800547 if (set_tracing_trace_inherit(ftrace) < 0) {
548 pr_err("failed to set tracing option function-fork\n");
549 goto out_reset;
550 }
551
Changbin Du38988f22020-08-08 10:31:32 +0800552 if (set_tracing_sleep_time(ftrace) < 0) {
553 pr_err("failed to set tracing option sleep-time\n");
554 goto out_reset;
555 }
556
Changbin Dud1bcf172020-08-08 10:31:33 +0800557 if (set_tracing_funcgraph_irqs(ftrace) < 0) {
558 pr_err("failed to set tracing option funcgraph-irqs\n");
559 goto out_reset;
560 }
561
Changbin Du59486fb2020-08-08 10:31:35 +0800562 if (set_tracing_funcgraph_verbose(ftrace) < 0) {
563 pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
564 goto out_reset;
565 }
566
Changbin Du00c85d52020-08-08 10:31:36 +0800567 if (set_tracing_thresh(ftrace) < 0) {
568 pr_err("failed to set tracing thresh\n");
569 goto out_reset;
570 }
571
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900572 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
573 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900574 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900575 }
576
Namhyung Kim29681bc2017-06-18 23:23:00 +0900577 setup_pager();
578
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900579 trace_file = get_tracing_file("trace_pipe");
580 if (!trace_file) {
581 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900582 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900583 }
584
585 trace_fd = open(trace_file, O_RDONLY);
586
587 put_tracing_file(trace_file);
588
589 if (trace_fd < 0) {
590 pr_err("failed to open trace_pipe\n");
Namhyung Kima9af6be2017-02-24 10:12:48 +0900591 goto out_reset;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900592 }
593
594 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
595 pollfd.fd = trace_fd;
596
Changbin Du81523c12020-08-08 10:31:28 +0800597 /* display column headers */
598 read_tracing_file_to_stdout("trace");
599
Changbin Du6555c2f2020-08-08 10:31:38 +0800600 if (!ftrace->initial_delay) {
601 if (write_tracing_file("tracing_on", "1") < 0) {
602 pr_err("can't enable tracing\n");
603 goto out_close_fd;
604 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900605 }
606
607 perf_evlist__start_workload(ftrace->evlist);
608
Changbin Du6555c2f2020-08-08 10:31:38 +0800609 if (ftrace->initial_delay) {
610 usleep(ftrace->initial_delay * 1000);
611 if (write_tracing_file("tracing_on", "1") < 0) {
612 pr_err("can't enable tracing\n");
613 goto out_close_fd;
614 }
615 }
616
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900617 while (!done) {
618 if (poll(&pollfd, 1, -1) < 0)
619 break;
620
621 if (pollfd.revents & POLLIN) {
622 int n = read(trace_fd, buf, sizeof(buf));
623 if (n < 0)
624 break;
625 if (fwrite(buf, n, 1, stdout) != 1)
626 break;
627 }
628 }
629
630 write_tracing_file("tracing_on", "0");
631
Changbin Du51a09d82020-05-10 23:06:11 +0800632 if (workload_exec_errno) {
633 const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
634 /* flush stdout first so below error msg appears at the end. */
635 fflush(stdout);
636 pr_err("workload failed: %s\n", emsg);
637 goto out_close_fd;
638 }
639
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900640 /* read remaining buffer contents */
641 while (true) {
642 int n = read(trace_fd, buf, sizeof(buf));
643 if (n <= 0)
644 break;
645 if (fwrite(buf, n, 1, stdout) != 1)
646 break;
647 }
648
649out_close_fd:
650 close(trace_fd);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900651out_reset:
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900652 reset_tracing_files(ftrace);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900653out:
Changbin Du51a09d82020-05-10 23:06:11 +0800654 return (done && !workload_exec_errno) ? 0 : -1;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900655}
656
Taeung Songb05d1092017-01-31 20:38:29 +0900657static int perf_ftrace_config(const char *var, const char *value, void *cb)
658{
659 struct perf_ftrace *ftrace = cb;
660
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300661 if (!strstarts(var, "ftrace."))
Taeung Songb05d1092017-01-31 20:38:29 +0900662 return 0;
663
664 if (strcmp(var, "ftrace.tracer"))
665 return -1;
666
667 if (!strcmp(value, "function_graph") ||
668 !strcmp(value, "function")) {
669 ftrace->tracer = value;
670 return 0;
671 }
672
673 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
674 return -1;
675}
676
Namhyung Kim78b83e82017-06-18 23:23:01 +0900677static int parse_filter_func(const struct option *opt, const char *str,
678 int unset __maybe_unused)
679{
680 struct list_head *head = opt->value;
681 struct filter_entry *entry;
682
683 entry = malloc(sizeof(*entry) + strlen(str) + 1);
684 if (entry == NULL)
685 return -ENOMEM;
686
687 strcpy(entry->name, str);
688 list_add_tail(&entry->list, head);
689
690 return 0;
691}
692
693static void delete_filter_func(struct list_head *head)
694{
695 struct filter_entry *pos, *tmp;
696
697 list_for_each_entry_safe(pos, tmp, head, list) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300698 list_del_init(&pos->list);
Namhyung Kim78b83e82017-06-18 23:23:01 +0900699 free(pos);
700 }
701}
702
Changbin Du846e1932020-08-08 10:31:27 +0800703static int parse_buffer_size(const struct option *opt,
704 const char *str, int unset)
705{
706 unsigned long *s = (unsigned long *)opt->value;
707 static struct parse_tag tags_size[] = {
708 { .tag = 'B', .mult = 1 },
709 { .tag = 'K', .mult = 1 << 10 },
710 { .tag = 'M', .mult = 1 << 20 },
711 { .tag = 'G', .mult = 1 << 30 },
712 { .tag = 0 },
713 };
714 unsigned long val;
715
716 if (unset) {
717 *s = 0;
718 return 0;
719 }
720
721 val = parse_tag_value(str, tags_size);
722 if (val != (unsigned long) -1) {
723 if (val < 1024) {
724 pr_err("buffer size too small, must larger than 1KB.");
725 return -1;
726 }
727 *s = val;
728 return 0;
729 }
730
731 return -1;
732}
733
Changbin Dub1d84af2020-08-08 10:31:31 +0800734static int parse_func_tracer_opts(const struct option *opt,
735 const char *str, int unset)
736{
737 int ret;
738 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
739 struct sublevel_option func_tracer_opts[] = {
740 { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
Changbin Duc81fc342020-08-08 10:31:34 +0800741 { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
Changbin Dub1d84af2020-08-08 10:31:31 +0800742 { .name = NULL, }
743 };
744
745 if (unset)
746 return 0;
747
748 ret = perf_parse_sublevel_options(str, func_tracer_opts);
749 if (ret)
750 return ret;
751
752 return 0;
753}
754
Changbin Du38988f22020-08-08 10:31:32 +0800755static int parse_graph_tracer_opts(const struct option *opt,
756 const char *str, int unset)
757{
758 int ret;
759 struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
760 struct sublevel_option graph_tracer_opts[] = {
761 { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
Changbin Dud1bcf172020-08-08 10:31:33 +0800762 { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
Changbin Du59486fb2020-08-08 10:31:35 +0800763 { .name = "verbose", .value_ptr = &ftrace->graph_verbose },
Changbin Du00c85d52020-08-08 10:31:36 +0800764 { .name = "thresh", .value_ptr = &ftrace->graph_thresh },
Changbin Dua8f87a52020-08-08 10:31:37 +0800765 { .name = "depth", .value_ptr = &ftrace->graph_depth },
Changbin Du38988f22020-08-08 10:31:32 +0800766 { .name = NULL, }
767 };
768
769 if (unset)
770 return 0;
771
772 ret = perf_parse_sublevel_options(str, graph_tracer_opts);
773 if (ret)
774 return ret;
775
776 return 0;
777}
778
Changbin Dueb6d31a2020-08-08 10:31:24 +0800779static void select_tracer(struct perf_ftrace *ftrace)
780{
781 bool graph = !list_empty(&ftrace->graph_funcs) ||
782 !list_empty(&ftrace->nograph_funcs);
783 bool func = !list_empty(&ftrace->filters) ||
784 !list_empty(&ftrace->notrace);
785
786 /* The function_graph has priority over function tracer. */
787 if (graph)
788 ftrace->tracer = "function_graph";
789 else if (func)
790 ftrace->tracer = "function";
791 /* Otherwise, the default tracer is used. */
792
793 pr_debug("%s tracer is used\n", ftrace->tracer);
794}
795
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300796int cmd_ftrace(int argc, const char **argv)
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900797{
798 int ret;
799 struct perf_ftrace ftrace = {
Taeung Songbf062bd2017-01-26 18:35:37 +0900800 .tracer = DEFAULT_TRACER,
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900801 .target = { .uid = UINT_MAX, },
802 };
803 const char * const ftrace_usage[] = {
Namhyung Kima9af6be2017-02-24 10:12:48 +0900804 "perf ftrace [<options>] [<command>]",
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900805 "perf ftrace [<options>] -- <command> [<options>]",
806 NULL
807 };
808 const struct option ftrace_options[] = {
809 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
Arnaldo Carvalho de Meloec347872017-01-18 21:49:14 -0300810 "tracer to use: function_graph(default) or function"),
Changbin Dud6d81bf2020-08-08 10:31:25 +0800811 OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions,
812 "Show available functions to filter"),
Namhyung Kima9af6be2017-02-24 10:12:48 +0900813 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
814 "trace on existing process id"),
Changbin Du42145d72020-08-08 10:31:39 +0800815 /* TODO: Add short option -t after -t/--tracer can be removed. */
816 OPT_STRING(0, "tid", &ftrace.target.tid, "tid",
817 "trace on existing thread id (exclusive to --pid)"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900818 OPT_INCR('v', "verbose", &verbose,
819 "be more verbose"),
Namhyung Kimdc231032017-02-24 10:12:50 +0900820 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
821 "system-wide collection from all CPUs"),
822 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
823 "list of cpus to monitor"),
Namhyung Kim78b83e82017-06-18 23:23:01 +0900824 OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
Changbin Dueb6d31a2020-08-08 10:31:24 +0800825 "trace given functions using function tracer",
826 parse_filter_func),
Namhyung Kim78b83e82017-06-18 23:23:01 +0900827 OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
828 "do not trace given functions", parse_filter_func),
Changbin Dub1d84af2020-08-08 10:31:31 +0800829 OPT_CALLBACK(0, "func-opts", &ftrace, "options",
Changbin Duc81fc342020-08-08 10:31:34 +0800830 "function tracer options, available options: call-graph,irq-info",
Changbin Dub1d84af2020-08-08 10:31:31 +0800831 parse_func_tracer_opts),
Namhyung Kim78b83e82017-06-18 23:23:01 +0900832 OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
Changbin Dueb6d31a2020-08-08 10:31:24 +0800833 "trace given functions using function_graph tracer",
834 parse_filter_func),
Namhyung Kim78b83e82017-06-18 23:23:01 +0900835 OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
836 "Set nograph filter on given functions", parse_filter_func),
Changbin Du38988f22020-08-08 10:31:32 +0800837 OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
Changbin Dua8f87a52020-08-08 10:31:37 +0800838 "graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>",
Changbin Du38988f22020-08-08 10:31:32 +0800839 parse_graph_tracer_opts),
Changbin Du846e1932020-08-08 10:31:27 +0800840 OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
841 "size of per cpu buffer", parse_buffer_size),
Changbin Du5b3474722020-08-08 10:31:29 +0800842 OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
843 "trace children processes"),
Changbin Du6555c2f2020-08-08 10:31:38 +0800844 OPT_UINTEGER('D', "delay", &ftrace.initial_delay,
845 "ms to wait before starting tracing after program start"),
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900846 OPT_END()
847 };
848
Namhyung Kim78b83e82017-06-18 23:23:01 +0900849 INIT_LIST_HEAD(&ftrace.filters);
850 INIT_LIST_HEAD(&ftrace.notrace);
851 INIT_LIST_HEAD(&ftrace.graph_funcs);
852 INIT_LIST_HEAD(&ftrace.nograph_funcs);
853
Taeung Songb05d1092017-01-31 20:38:29 +0900854 ret = perf_config(perf_ftrace_config, &ftrace);
855 if (ret < 0)
856 return -1;
857
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900858 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
859 PARSE_OPT_STOP_AT_NON_OPTION);
Namhyung Kima9af6be2017-02-24 10:12:48 +0900860 if (!argc && target__none(&ftrace.target))
Changbin Du452b0d12020-05-10 23:06:10 +0800861 ftrace.target.system_wide = true;
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900862
Changbin Dueb6d31a2020-08-08 10:31:24 +0800863 select_tracer(&ftrace);
864
Namhyung Kima9af6be2017-02-24 10:12:48 +0900865 ret = target__validate(&ftrace.target);
866 if (ret) {
867 char errbuf[512];
868
869 target__strerror(&ftrace.target, ret, errbuf, 512);
870 pr_err("%s\n", errbuf);
Namhyung Kim78b83e82017-06-18 23:23:01 +0900871 goto out_delete_filters;
Namhyung Kima9af6be2017-02-24 10:12:48 +0900872 }
873
Jiri Olsa0f98b112019-07-21 13:23:55 +0200874 ftrace.evlist = evlist__new();
Namhyung Kim78b83e82017-06-18 23:23:01 +0900875 if (ftrace.evlist == NULL) {
876 ret = -ENOMEM;
877 goto out_delete_filters;
878 }
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900879
880 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
881 if (ret < 0)
882 goto out_delete_evlist;
883
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900884 ret = __cmd_ftrace(&ftrace, argc, argv);
885
886out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +0200887 evlist__delete(ftrace.evlist);
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900888
Namhyung Kim78b83e82017-06-18 23:23:01 +0900889out_delete_filters:
890 delete_filter_func(&ftrace.filters);
891 delete_filter_func(&ftrace.notrace);
892 delete_filter_func(&ftrace.graph_funcs);
893 delete_filter_func(&ftrace.nograph_funcs);
894
Namhyung Kimd01f4e82013-03-07 21:45:20 +0900895 return ret;
896}