Commit | Line | Data |
---|---|---|
1c6bec5b | 1 | // SPDX-License-Identifier: GPL-2.0-only |
d01f4e8d NK |
2 | /* |
3 | * builtin-ftrace.c | |
4 | * | |
5 | * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org> | |
0094024a | 6 | * Copyright (c) 2020 Changbin Du <changbin.du@gmail.com>, significant enhancement. |
d01f4e8d NK |
7 | */ |
8 | ||
9 | #include "builtin.h" | |
d01f4e8d | 10 | |
a43783ae | 11 | #include <errno.h> |
d01f4e8d NK |
12 | #include <unistd.h> |
13 | #include <signal.h> | |
f2a39fe8 | 14 | #include <stdlib.h> |
a9af6be5 | 15 | #include <fcntl.h> |
4208735d | 16 | #include <poll.h> |
c766f3df | 17 | #include <linux/capability.h> |
8520a98d | 18 | #include <linux/string.h> |
d01f4e8d NK |
19 | |
20 | #include "debug.h" | |
8520a98d | 21 | #include <subcmd/pager.h> |
d01f4e8d | 22 | #include <subcmd/parse-options.h> |
20a9ed28 | 23 | #include <api/fs/tracing_path.h> |
d01f4e8d NK |
24 | #include "evlist.h" |
25 | #include "target.h" | |
dc231032 | 26 | #include "cpumap.h" |
d01f4e8d | 27 | #include "thread_map.h" |
2ae05fe0 | 28 | #include "strfilter.h" |
c766f3df | 29 | #include "util/cap.h" |
b05d1093 | 30 | #include "util/config.h" |
846e1939 | 31 | #include "util/units.h" |
b1d84af6 | 32 | #include "util/parse-sublevel-options.h" |
d01f4e8d | 33 | |
d01f4e8d NK |
34 | #define DEFAULT_TRACER "function_graph" |
35 | ||
36 | struct perf_ftrace { | |
63503dba | 37 | struct evlist *evlist; |
78b83e8b NK |
38 | struct target target; |
39 | const char *tracer; | |
40 | struct list_head filters; | |
41 | struct list_head notrace; | |
42 | struct list_head graph_funcs; | |
43 | struct list_head nograph_funcs; | |
1096c35a | 44 | int graph_depth; |
846e1939 | 45 | unsigned long percpu_buffer_size; |
5b347472 | 46 | bool inherit; |
b1d84af6 | 47 | int func_stack_trace; |
c81fc34e | 48 | int func_irq_info; |
38988f2e | 49 | int graph_nosleep_time; |
d1bcf17c | 50 | int graph_noirqs; |
59486fb0 | 51 | int graph_verbose; |
00c85d5f | 52 | int graph_thresh; |
6555c2f6 | 53 | unsigned int initial_delay; |
78b83e8b NK |
54 | }; |
55 | ||
56 | struct filter_entry { | |
57 | struct list_head list; | |
58 | char name[]; | |
d01f4e8d NK |
59 | }; |
60 | ||
51a09d8f | 61 | static volatile int workload_exec_errno; |
d01f4e8d NK |
62 | static bool done; |
63 | ||
64 | static void sig_handler(int sig __maybe_unused) | |
65 | { | |
66 | done = true; | |
67 | } | |
68 | ||
69 | /* | |
7b392ef0 | 70 | * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since |
d01f4e8d NK |
71 | * we asked by setting its exec_error to the function below, |
72 | * ftrace__workload_exec_failed_signal. | |
73 | * | |
74 | * XXX We need to handle this more appropriately, emitting an error, etc. | |
75 | */ | |
76 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, | |
77 | siginfo_t *info __maybe_unused, | |
78 | void *ucontext __maybe_unused) | |
79 | { | |
51a09d8f | 80 | workload_exec_errno = info->si_value.sival_int; |
d01f4e8d NK |
81 | done = true; |
82 | } | |
83 | ||
a9af6be5 | 84 | static int __write_tracing_file(const char *name, const char *val, bool append) |
d01f4e8d NK |
85 | { |
86 | char *file; | |
87 | int fd, ret = -1; | |
88 | ssize_t size = strlen(val); | |
a9af6be5 | 89 | int flags = O_WRONLY; |
e7bd9ba2 | 90 | char errbuf[512]; |
63cd02d8 | 91 | char *val_copy; |
d01f4e8d NK |
92 | |
93 | file = get_tracing_file(name); | |
94 | if (!file) { | |
95 | pr_debug("cannot get tracing file: %s\n", name); | |
96 | return -1; | |
97 | } | |
98 | ||
a9af6be5 NK |
99 | if (append) |
100 | flags |= O_APPEND; | |
101 | else | |
102 | flags |= O_TRUNC; | |
103 | ||
104 | fd = open(file, flags); | |
d01f4e8d | 105 | if (fd < 0) { |
e7bd9ba2 NK |
106 | pr_debug("cannot open tracing file: %s: %s\n", |
107 | name, str_error_r(errno, errbuf, sizeof(errbuf))); | |
d01f4e8d NK |
108 | goto out; |
109 | } | |
110 | ||
63cd02d8 CD |
111 | /* |
112 | * Copy the original value and append a '\n'. Without this, | |
113 | * the kernel can hide possible errors. | |
114 | */ | |
115 | val_copy = strdup(val); | |
116 | if (!val_copy) | |
117 | goto out_close; | |
118 | val_copy[size] = '\n'; | |
119 | ||
120 | if (write(fd, val_copy, size + 1) == size + 1) | |
d01f4e8d NK |
121 | ret = 0; |
122 | else | |
e7bd9ba2 NK |
123 | pr_debug("write '%s' to tracing/%s failed: %s\n", |
124 | val, name, str_error_r(errno, errbuf, sizeof(errbuf))); | |
d01f4e8d | 125 | |
63cd02d8 CD |
126 | free(val_copy); |
127 | out_close: | |
d01f4e8d NK |
128 | close(fd); |
129 | out: | |
130 | put_tracing_file(file); | |
131 | return ret; | |
132 | } | |
133 | ||
a9af6be5 NK |
134 | static int write_tracing_file(const char *name, const char *val) |
135 | { | |
136 | return __write_tracing_file(name, val, false); | |
137 | } | |
138 | ||
139 | static int append_tracing_file(const char *name, const char *val) | |
140 | { | |
141 | return __write_tracing_file(name, val, true); | |
142 | } | |
143 | ||
d6d81bfe CD |
144 | static int read_tracing_file_to_stdout(const char *name) |
145 | { | |
146 | char buf[4096]; | |
147 | char *file; | |
148 | int fd; | |
149 | int ret = -1; | |
150 | ||
151 | file = get_tracing_file(name); | |
152 | if (!file) { | |
153 | pr_debug("cannot get tracing file: %s\n", name); | |
154 | return -1; | |
155 | } | |
156 | ||
157 | fd = open(file, O_RDONLY); | |
158 | if (fd < 0) { | |
159 | pr_debug("cannot open tracing file: %s: %s\n", | |
160 | name, str_error_r(errno, buf, sizeof(buf))); | |
161 | goto out; | |
162 | } | |
163 | ||
164 | /* read contents to stdout */ | |
165 | while (true) { | |
166 | int n = read(fd, buf, sizeof(buf)); | |
167 | if (n == 0) | |
168 | break; | |
169 | else if (n < 0) | |
170 | goto out_close; | |
171 | ||
172 | if (fwrite(buf, n, 1, stdout) != 1) | |
173 | goto out_close; | |
174 | } | |
175 | ret = 0; | |
176 | ||
177 | out_close: | |
178 | close(fd); | |
179 | out: | |
180 | put_tracing_file(file); | |
181 | return ret; | |
182 | } | |
183 | ||
2ae05fe0 CD |
184 | static int read_tracing_file_by_line(const char *name, |
185 | void (*cb)(char *str, void *arg), | |
186 | void *cb_arg) | |
187 | { | |
188 | char *line = NULL; | |
189 | size_t len = 0; | |
190 | char *file; | |
191 | FILE *fp; | |
192 | ||
193 | file = get_tracing_file(name); | |
194 | if (!file) { | |
195 | pr_debug("cannot get tracing file: %s\n", name); | |
196 | return -1; | |
197 | } | |
198 | ||
199 | fp = fopen(file, "r"); | |
200 | if (fp == NULL) { | |
201 | pr_debug("cannot open tracing file: %s\n", name); | |
202 | put_tracing_file(file); | |
203 | return -1; | |
204 | } | |
205 | ||
206 | while (getline(&line, &len, fp) != -1) { | |
207 | cb(line, cb_arg); | |
208 | } | |
209 | ||
210 | if (line) | |
211 | free(line); | |
212 | ||
213 | fclose(fp); | |
214 | put_tracing_file(file); | |
215 | return 0; | |
216 | } | |
217 | ||
68faab0f CD |
218 | static int write_tracing_file_int(const char *name, int value) |
219 | { | |
220 | char buf[16]; | |
221 | ||
222 | snprintf(buf, sizeof(buf), "%d", value); | |
223 | if (write_tracing_file(name, buf) < 0) | |
224 | return -1; | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
5b347472 CD |
229 | static int write_tracing_option_file(const char *name, const char *val) |
230 | { | |
231 | char *file; | |
232 | int ret; | |
233 | ||
234 | if (asprintf(&file, "options/%s", name) < 0) | |
235 | return -1; | |
236 | ||
237 | ret = __write_tracing_file(file, val, false); | |
238 | free(file); | |
239 | return ret; | |
240 | } | |
241 | ||
dc231032 | 242 | static int reset_tracing_cpu(void); |
78b83e8b | 243 | static void reset_tracing_filters(void); |
dc231032 | 244 | |
5b347472 CD |
245 | static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused) |
246 | { | |
247 | write_tracing_option_file("function-fork", "0"); | |
b1d84af6 | 248 | write_tracing_option_file("func_stack_trace", "0"); |
38988f2e | 249 | write_tracing_option_file("sleep-time", "1"); |
d1bcf17c | 250 | write_tracing_option_file("funcgraph-irqs", "1"); |
59486fb0 CD |
251 | write_tracing_option_file("funcgraph-proc", "0"); |
252 | write_tracing_option_file("funcgraph-abstime", "0"); | |
253 | write_tracing_option_file("latency-format", "0"); | |
c81fc34e | 254 | write_tracing_option_file("irq-info", "0"); |
5b347472 CD |
255 | } |
256 | ||
d01f4e8d NK |
257 | static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) |
258 | { | |
259 | if (write_tracing_file("tracing_on", "0") < 0) | |
260 | return -1; | |
261 | ||
262 | if (write_tracing_file("current_tracer", "nop") < 0) | |
263 | return -1; | |
264 | ||
265 | if (write_tracing_file("set_ftrace_pid", " ") < 0) | |
266 | return -1; | |
267 | ||
dc231032 NK |
268 | if (reset_tracing_cpu() < 0) |
269 | return -1; | |
270 | ||
1096c35a NK |
271 | if (write_tracing_file("max_graph_depth", "0") < 0) |
272 | return -1; | |
273 | ||
00c85d5f CD |
274 | if (write_tracing_file("tracing_thresh", "0") < 0) |
275 | return -1; | |
276 | ||
78b83e8b | 277 | reset_tracing_filters(); |
5b347472 | 278 | reset_tracing_options(ftrace); |
d01f4e8d NK |
279 | return 0; |
280 | } | |
281 | ||
a9af6be5 NK |
282 | static int set_tracing_pid(struct perf_ftrace *ftrace) |
283 | { | |
284 | int i; | |
285 | char buf[16]; | |
286 | ||
287 | if (target__has_cpu(&ftrace->target)) | |
288 | return 0; | |
289 | ||
a2f354e3 | 290 | for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) { |
a9af6be5 | 291 | scnprintf(buf, sizeof(buf), "%d", |
671b60cb | 292 | perf_thread_map__pid(ftrace->evlist->core.threads, i)); |
a9af6be5 NK |
293 | if (append_tracing_file("set_ftrace_pid", buf) < 0) |
294 | return -1; | |
295 | } | |
296 | return 0; | |
297 | } | |
298 | ||
f854839b | 299 | static int set_tracing_cpumask(struct perf_cpu_map *cpumap) |
dc231032 NK |
300 | { |
301 | char *cpumask; | |
302 | size_t mask_size; | |
303 | int ret; | |
304 | int last_cpu; | |
305 | ||
306 | last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1); | |
cf30ae72 | 307 | mask_size = last_cpu / 4 + 2; /* one more byte for EOS */ |
dc231032 NK |
308 | mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */ |
309 | ||
310 | cpumask = malloc(mask_size); | |
311 | if (cpumask == NULL) { | |
312 | pr_debug("failed to allocate cpu mask\n"); | |
313 | return -1; | |
314 | } | |
315 | ||
316 | cpu_map__snprint_mask(cpumap, cpumask, mask_size); | |
317 | ||
318 | ret = write_tracing_file("tracing_cpumask", cpumask); | |
319 | ||
320 | free(cpumask); | |
321 | return ret; | |
322 | } | |
323 | ||
324 | static int set_tracing_cpu(struct perf_ftrace *ftrace) | |
325 | { | |
f72f901d | 326 | struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus; |
dc231032 NK |
327 | |
328 | if (!target__has_cpu(&ftrace->target)) | |
329 | return 0; | |
330 | ||
331 | return set_tracing_cpumask(cpumap); | |
332 | } | |
333 | ||
b1d84af6 CD |
334 | static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace) |
335 | { | |
336 | if (!ftrace->func_stack_trace) | |
337 | return 0; | |
338 | ||
339 | if (write_tracing_option_file("func_stack_trace", "1") < 0) | |
340 | return -1; | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
c81fc34e CD |
345 | static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace) |
346 | { | |
347 | if (!ftrace->func_irq_info) | |
348 | return 0; | |
349 | ||
350 | if (write_tracing_option_file("irq-info", "1") < 0) | |
351 | return -1; | |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
dc231032 NK |
356 | static int reset_tracing_cpu(void) |
357 | { | |
9c3516d1 | 358 | struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL); |
dc231032 NK |
359 | int ret; |
360 | ||
361 | ret = set_tracing_cpumask(cpumap); | |
38f01d8d | 362 | perf_cpu_map__put(cpumap); |
dc231032 NK |
363 | return ret; |
364 | } | |
365 | ||
78b83e8b NK |
366 | static int __set_tracing_filter(const char *filter_file, struct list_head *funcs) |
367 | { | |
368 | struct filter_entry *pos; | |
369 | ||
370 | list_for_each_entry(pos, funcs, list) { | |
371 | if (append_tracing_file(filter_file, pos->name) < 0) | |
372 | return -1; | |
373 | } | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | static int set_tracing_filters(struct perf_ftrace *ftrace) | |
379 | { | |
380 | int ret; | |
381 | ||
382 | ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters); | |
383 | if (ret < 0) | |
384 | return ret; | |
385 | ||
386 | ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace); | |
387 | if (ret < 0) | |
388 | return ret; | |
389 | ||
390 | ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs); | |
391 | if (ret < 0) | |
392 | return ret; | |
393 | ||
394 | /* old kernels do not have this filter */ | |
395 | __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs); | |
396 | ||
397 | return ret; | |
398 | } | |
399 | ||
400 | static void reset_tracing_filters(void) | |
401 | { | |
402 | write_tracing_file("set_ftrace_filter", " "); | |
403 | write_tracing_file("set_ftrace_notrace", " "); | |
404 | write_tracing_file("set_graph_function", " "); | |
405 | write_tracing_file("set_graph_notrace", " "); | |
406 | } | |
407 | ||
1096c35a NK |
408 | static int set_tracing_depth(struct perf_ftrace *ftrace) |
409 | { | |
1096c35a NK |
410 | if (ftrace->graph_depth == 0) |
411 | return 0; | |
412 | ||
413 | if (ftrace->graph_depth < 0) { | |
414 | pr_err("invalid graph depth: %d\n", ftrace->graph_depth); | |
415 | return -1; | |
416 | } | |
417 | ||
68faab0f | 418 | if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0) |
1096c35a NK |
419 | return -1; |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
846e1939 CD |
424 | static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace) |
425 | { | |
426 | int ret; | |
427 | ||
428 | if (ftrace->percpu_buffer_size == 0) | |
429 | return 0; | |
430 | ||
431 | ret = write_tracing_file_int("buffer_size_kb", | |
432 | ftrace->percpu_buffer_size / 1024); | |
433 | if (ret < 0) | |
434 | return ret; | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
5b347472 CD |
439 | static int set_tracing_trace_inherit(struct perf_ftrace *ftrace) |
440 | { | |
441 | if (!ftrace->inherit) | |
442 | return 0; | |
443 | ||
444 | if (write_tracing_option_file("function-fork", "1") < 0) | |
445 | return -1; | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
38988f2e CD |
450 | static int set_tracing_sleep_time(struct perf_ftrace *ftrace) |
451 | { | |
452 | if (!ftrace->graph_nosleep_time) | |
453 | return 0; | |
454 | ||
455 | if (write_tracing_option_file("sleep-time", "0") < 0) | |
456 | return -1; | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
d1bcf17c CD |
461 | static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace) |
462 | { | |
463 | if (!ftrace->graph_noirqs) | |
464 | return 0; | |
465 | ||
466 | if (write_tracing_option_file("funcgraph-irqs", "0") < 0) | |
467 | return -1; | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
59486fb0 CD |
472 | static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace) |
473 | { | |
474 | if (!ftrace->graph_verbose) | |
475 | return 0; | |
476 | ||
477 | if (write_tracing_option_file("funcgraph-proc", "1") < 0) | |
478 | return -1; | |
479 | ||
480 | if (write_tracing_option_file("funcgraph-abstime", "1") < 0) | |
481 | return -1; | |
482 | ||
483 | if (write_tracing_option_file("latency-format", "1") < 0) | |
484 | return -1; | |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
00c85d5f CD |
489 | static int set_tracing_thresh(struct perf_ftrace *ftrace) |
490 | { | |
491 | int ret; | |
492 | ||
493 | if (ftrace->graph_thresh == 0) | |
494 | return 0; | |
495 | ||
496 | ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh); | |
497 | if (ret < 0) | |
498 | return ret; | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
3c4dc21b | 503 | static int set_tracing_options(struct perf_ftrace *ftrace) |
d01f4e8d | 504 | { |
a9af6be5 NK |
505 | if (set_tracing_pid(ftrace) < 0) { |
506 | pr_err("failed to set ftrace pid\n"); | |
3c4dc21b | 507 | return -1; |
d01f4e8d NK |
508 | } |
509 | ||
dc231032 NK |
510 | if (set_tracing_cpu(ftrace) < 0) { |
511 | pr_err("failed to set tracing cpumask\n"); | |
3c4dc21b | 512 | return -1; |
dc231032 NK |
513 | } |
514 | ||
b1d84af6 CD |
515 | if (set_tracing_func_stack_trace(ftrace) < 0) { |
516 | pr_err("failed to set tracing option func_stack_trace\n"); | |
3c4dc21b | 517 | return -1; |
b1d84af6 CD |
518 | } |
519 | ||
c81fc34e CD |
520 | if (set_tracing_func_irqinfo(ftrace) < 0) { |
521 | pr_err("failed to set tracing option irq-info\n"); | |
3c4dc21b | 522 | return -1; |
c81fc34e CD |
523 | } |
524 | ||
78b83e8b NK |
525 | if (set_tracing_filters(ftrace) < 0) { |
526 | pr_err("failed to set tracing filters\n"); | |
3c4dc21b | 527 | return -1; |
78b83e8b NK |
528 | } |
529 | ||
1096c35a NK |
530 | if (set_tracing_depth(ftrace) < 0) { |
531 | pr_err("failed to set graph depth\n"); | |
3c4dc21b | 532 | return -1; |
1096c35a NK |
533 | } |
534 | ||
846e1939 CD |
535 | if (set_tracing_percpu_buffer_size(ftrace) < 0) { |
536 | pr_err("failed to set tracing per-cpu buffer size\n"); | |
3c4dc21b | 537 | return -1; |
846e1939 CD |
538 | } |
539 | ||
5b347472 CD |
540 | if (set_tracing_trace_inherit(ftrace) < 0) { |
541 | pr_err("failed to set tracing option function-fork\n"); | |
3c4dc21b | 542 | return -1; |
5b347472 CD |
543 | } |
544 | ||
38988f2e CD |
545 | if (set_tracing_sleep_time(ftrace) < 0) { |
546 | pr_err("failed to set tracing option sleep-time\n"); | |
3c4dc21b | 547 | return -1; |
38988f2e CD |
548 | } |
549 | ||
d1bcf17c CD |
550 | if (set_tracing_funcgraph_irqs(ftrace) < 0) { |
551 | pr_err("failed to set tracing option funcgraph-irqs\n"); | |
3c4dc21b | 552 | return -1; |
d1bcf17c CD |
553 | } |
554 | ||
59486fb0 CD |
555 | if (set_tracing_funcgraph_verbose(ftrace) < 0) { |
556 | pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n"); | |
3c4dc21b | 557 | return -1; |
59486fb0 CD |
558 | } |
559 | ||
00c85d5f CD |
560 | if (set_tracing_thresh(ftrace) < 0) { |
561 | pr_err("failed to set tracing thresh\n"); | |
3c4dc21b CD |
562 | return -1; |
563 | } | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
a9b8ae8a NK |
568 | static void select_tracer(struct perf_ftrace *ftrace) |
569 | { | |
570 | bool graph = !list_empty(&ftrace->graph_funcs) || | |
571 | !list_empty(&ftrace->nograph_funcs); | |
572 | bool func = !list_empty(&ftrace->filters) || | |
573 | !list_empty(&ftrace->notrace); | |
574 | ||
575 | /* The function_graph has priority over function tracer. */ | |
576 | if (graph) | |
577 | ftrace->tracer = "function_graph"; | |
578 | else if (func) | |
579 | ftrace->tracer = "function"; | |
580 | /* Otherwise, the default tracer is used. */ | |
581 | ||
582 | pr_debug("%s tracer is used\n", ftrace->tracer); | |
583 | } | |
584 | ||
585 | static int __cmd_ftrace(struct perf_ftrace *ftrace) | |
3c4dc21b CD |
586 | { |
587 | char *trace_file; | |
588 | int trace_fd; | |
589 | char buf[4096]; | |
590 | struct pollfd pollfd = { | |
591 | .events = POLLIN, | |
592 | }; | |
593 | ||
594 | if (!(perf_cap__capable(CAP_PERFMON) || | |
595 | perf_cap__capable(CAP_SYS_ADMIN))) { | |
596 | pr_err("ftrace only works for %s!\n", | |
597 | #ifdef HAVE_LIBCAP_SUPPORT | |
598 | "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" | |
599 | #else | |
600 | "root" | |
601 | #endif | |
602 | ); | |
603 | return -1; | |
00c85d5f CD |
604 | } |
605 | ||
a9b8ae8a | 606 | select_tracer(ftrace); |
3c4dc21b | 607 | |
3c4dc21b CD |
608 | if (reset_tracing_files(ftrace) < 0) { |
609 | pr_err("failed to reset ftrace\n"); | |
610 | goto out; | |
611 | } | |
612 | ||
613 | /* reset ftrace buffer */ | |
614 | if (write_tracing_file("trace", "0") < 0) | |
615 | goto out; | |
616 | ||
3c4dc21b CD |
617 | if (set_tracing_options(ftrace) < 0) |
618 | goto out_reset; | |
619 | ||
a9af6be5 NK |
620 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
621 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); | |
622 | goto out_reset; | |
d01f4e8d NK |
623 | } |
624 | ||
29681bc5 NK |
625 | setup_pager(); |
626 | ||
d01f4e8d NK |
627 | trace_file = get_tracing_file("trace_pipe"); |
628 | if (!trace_file) { | |
629 | pr_err("failed to open trace_pipe\n"); | |
a9af6be5 | 630 | goto out_reset; |
d01f4e8d NK |
631 | } |
632 | ||
633 | trace_fd = open(trace_file, O_RDONLY); | |
634 | ||
635 | put_tracing_file(trace_file); | |
636 | ||
637 | if (trace_fd < 0) { | |
638 | pr_err("failed to open trace_pipe\n"); | |
a9af6be5 | 639 | goto out_reset; |
d01f4e8d NK |
640 | } |
641 | ||
642 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); | |
643 | pollfd.fd = trace_fd; | |
644 | ||
81523c1e CD |
645 | /* display column headers */ |
646 | read_tracing_file_to_stdout("trace"); | |
647 | ||
6555c2f6 CD |
648 | if (!ftrace->initial_delay) { |
649 | if (write_tracing_file("tracing_on", "1") < 0) { | |
650 | pr_err("can't enable tracing\n"); | |
651 | goto out_close_fd; | |
652 | } | |
d01f4e8d NK |
653 | } |
654 | ||
7b392ef0 | 655 | evlist__start_workload(ftrace->evlist); |
d01f4e8d | 656 | |
6555c2f6 CD |
657 | if (ftrace->initial_delay) { |
658 | usleep(ftrace->initial_delay * 1000); | |
659 | if (write_tracing_file("tracing_on", "1") < 0) { | |
660 | pr_err("can't enable tracing\n"); | |
661 | goto out_close_fd; | |
662 | } | |
663 | } | |
664 | ||
d01f4e8d NK |
665 | while (!done) { |
666 | if (poll(&pollfd, 1, -1) < 0) | |
667 | break; | |
668 | ||
669 | if (pollfd.revents & POLLIN) { | |
670 | int n = read(trace_fd, buf, sizeof(buf)); | |
671 | if (n < 0) | |
672 | break; | |
673 | if (fwrite(buf, n, 1, stdout) != 1) | |
674 | break; | |
675 | } | |
676 | } | |
677 | ||
678 | write_tracing_file("tracing_on", "0"); | |
679 | ||
51a09d8f CD |
680 | if (workload_exec_errno) { |
681 | const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf)); | |
682 | /* flush stdout first so below error msg appears at the end. */ | |
683 | fflush(stdout); | |
684 | pr_err("workload failed: %s\n", emsg); | |
685 | goto out_close_fd; | |
686 | } | |
687 | ||
d01f4e8d NK |
688 | /* read remaining buffer contents */ |
689 | while (true) { | |
690 | int n = read(trace_fd, buf, sizeof(buf)); | |
691 | if (n <= 0) | |
692 | break; | |
693 | if (fwrite(buf, n, 1, stdout) != 1) | |
694 | break; | |
695 | } | |
696 | ||
697 | out_close_fd: | |
698 | close(trace_fd); | |
a9af6be5 | 699 | out_reset: |
d01f4e8d | 700 | reset_tracing_files(ftrace); |
a9af6be5 | 701 | out: |
51a09d8f | 702 | return (done && !workload_exec_errno) ? 0 : -1; |
d01f4e8d NK |
703 | } |
704 | ||
b05d1093 TS |
705 | static int perf_ftrace_config(const char *var, const char *value, void *cb) |
706 | { | |
707 | struct perf_ftrace *ftrace = cb; | |
708 | ||
8e99b6d4 | 709 | if (!strstarts(var, "ftrace.")) |
b05d1093 TS |
710 | return 0; |
711 | ||
712 | if (strcmp(var, "ftrace.tracer")) | |
713 | return -1; | |
714 | ||
715 | if (!strcmp(value, "function_graph") || | |
716 | !strcmp(value, "function")) { | |
717 | ftrace->tracer = value; | |
718 | return 0; | |
719 | } | |
720 | ||
721 | pr_err("Please select \"function_graph\" (default) or \"function\"\n"); | |
722 | return -1; | |
723 | } | |
724 | ||
2ae05fe0 CD |
725 | static void list_function_cb(char *str, void *arg) |
726 | { | |
727 | struct strfilter *filter = (struct strfilter *)arg; | |
728 | ||
729 | if (strfilter__compare(filter, str)) | |
730 | printf("%s", str); | |
731 | } | |
732 | ||
733 | static int opt_list_avail_functions(const struct option *opt __maybe_unused, | |
734 | const char *str, int unset) | |
735 | { | |
736 | struct strfilter *filter; | |
737 | const char *err = NULL; | |
738 | int ret; | |
739 | ||
740 | if (unset || !str) | |
741 | return -1; | |
742 | ||
743 | filter = strfilter__new(str, &err); | |
744 | if (!filter) | |
745 | return err ? -EINVAL : -ENOMEM; | |
746 | ||
747 | ret = strfilter__or(filter, str, &err); | |
748 | if (ret == -EINVAL) { | |
749 | pr_err("Filter parse error at %td.\n", err - str + 1); | |
750 | pr_err("Source: \"%s\"\n", str); | |
751 | pr_err(" %*c\n", (int)(err - str + 1), '^'); | |
752 | strfilter__delete(filter); | |
753 | return ret; | |
754 | } | |
755 | ||
756 | ret = read_tracing_file_by_line("available_filter_functions", | |
757 | list_function_cb, filter); | |
758 | strfilter__delete(filter); | |
759 | if (ret < 0) | |
760 | return ret; | |
761 | ||
762 | exit(0); | |
763 | } | |
764 | ||
78b83e8b NK |
765 | static int parse_filter_func(const struct option *opt, const char *str, |
766 | int unset __maybe_unused) | |
767 | { | |
768 | struct list_head *head = opt->value; | |
769 | struct filter_entry *entry; | |
770 | ||
771 | entry = malloc(sizeof(*entry) + strlen(str) + 1); | |
772 | if (entry == NULL) | |
773 | return -ENOMEM; | |
774 | ||
775 | strcpy(entry->name, str); | |
776 | list_add_tail(&entry->list, head); | |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
781 | static void delete_filter_func(struct list_head *head) | |
782 | { | |
783 | struct filter_entry *pos, *tmp; | |
784 | ||
785 | list_for_each_entry_safe(pos, tmp, head, list) { | |
e56fbc9d | 786 | list_del_init(&pos->list); |
78b83e8b NK |
787 | free(pos); |
788 | } | |
789 | } | |
790 | ||
846e1939 CD |
791 | static int parse_buffer_size(const struct option *opt, |
792 | const char *str, int unset) | |
793 | { | |
794 | unsigned long *s = (unsigned long *)opt->value; | |
795 | static struct parse_tag tags_size[] = { | |
796 | { .tag = 'B', .mult = 1 }, | |
797 | { .tag = 'K', .mult = 1 << 10 }, | |
798 | { .tag = 'M', .mult = 1 << 20 }, | |
799 | { .tag = 'G', .mult = 1 << 30 }, | |
800 | { .tag = 0 }, | |
801 | }; | |
802 | unsigned long val; | |
803 | ||
804 | if (unset) { | |
805 | *s = 0; | |
806 | return 0; | |
807 | } | |
808 | ||
809 | val = parse_tag_value(str, tags_size); | |
810 | if (val != (unsigned long) -1) { | |
811 | if (val < 1024) { | |
812 | pr_err("buffer size too small, must larger than 1KB."); | |
813 | return -1; | |
814 | } | |
815 | *s = val; | |
816 | return 0; | |
817 | } | |
818 | ||
819 | return -1; | |
820 | } | |
821 | ||
b1d84af6 CD |
822 | static int parse_func_tracer_opts(const struct option *opt, |
823 | const char *str, int unset) | |
824 | { | |
825 | int ret; | |
826 | struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; | |
827 | struct sublevel_option func_tracer_opts[] = { | |
828 | { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace }, | |
c81fc34e | 829 | { .name = "irq-info", .value_ptr = &ftrace->func_irq_info }, |
b1d84af6 CD |
830 | { .name = NULL, } |
831 | }; | |
832 | ||
833 | if (unset) | |
834 | return 0; | |
835 | ||
836 | ret = perf_parse_sublevel_options(str, func_tracer_opts); | |
837 | if (ret) | |
838 | return ret; | |
839 | ||
840 | return 0; | |
841 | } | |
842 | ||
38988f2e CD |
843 | static int parse_graph_tracer_opts(const struct option *opt, |
844 | const char *str, int unset) | |
845 | { | |
846 | int ret; | |
847 | struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; | |
848 | struct sublevel_option graph_tracer_opts[] = { | |
849 | { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time }, | |
d1bcf17c | 850 | { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs }, |
59486fb0 | 851 | { .name = "verbose", .value_ptr = &ftrace->graph_verbose }, |
00c85d5f | 852 | { .name = "thresh", .value_ptr = &ftrace->graph_thresh }, |
a8f87a5c | 853 | { .name = "depth", .value_ptr = &ftrace->graph_depth }, |
38988f2e CD |
854 | { .name = NULL, } |
855 | }; | |
856 | ||
857 | if (unset) | |
858 | return 0; | |
859 | ||
860 | ret = perf_parse_sublevel_options(str, graph_tracer_opts); | |
861 | if (ret) | |
862 | return ret; | |
863 | ||
864 | return 0; | |
865 | } | |
866 | ||
b0ad8ea6 | 867 | int cmd_ftrace(int argc, const char **argv) |
d01f4e8d NK |
868 | { |
869 | int ret; | |
870 | struct perf_ftrace ftrace = { | |
bf062bd2 | 871 | .tracer = DEFAULT_TRACER, |
d01f4e8d NK |
872 | .target = { .uid = UINT_MAX, }, |
873 | }; | |
416e15ad | 874 | const struct option common_options[] = { |
a9af6be5 | 875 | OPT_STRING('p', "pid", &ftrace.target.pid, "pid", |
492e4edb | 876 | "Trace on existing process id"), |
42145d71 CD |
877 | /* TODO: Add short option -t after -t/--tracer can be removed. */ |
878 | OPT_STRING(0, "tid", &ftrace.target.tid, "tid", | |
492e4edb | 879 | "Trace on existing thread id (exclusive to --pid)"), |
d01f4e8d | 880 | OPT_INCR('v', "verbose", &verbose, |
492e4edb | 881 | "Be more verbose"), |
dc231032 | 882 | OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide, |
492e4edb | 883 | "System-wide collection from all CPUs"), |
dc231032 | 884 | OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu", |
492e4edb | 885 | "List of cpus to monitor"), |
416e15ad NK |
886 | OPT_END() |
887 | }; | |
888 | const struct option ftrace_options[] = { | |
889 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", | |
890 | "Tracer to use: function_graph(default) or function"), | |
891 | OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]", | |
892 | "Show available functions to filter", | |
893 | opt_list_avail_functions, "*"), | |
78b83e8b | 894 | OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", |
492e4edb | 895 | "Trace given functions using function tracer", |
eb6d31ae | 896 | parse_filter_func), |
78b83e8b | 897 | OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func", |
492e4edb | 898 | "Do not trace given functions", parse_filter_func), |
b1d84af6 | 899 | OPT_CALLBACK(0, "func-opts", &ftrace, "options", |
492e4edb | 900 | "Function tracer options, available options: call-graph,irq-info", |
b1d84af6 | 901 | parse_func_tracer_opts), |
78b83e8b | 902 | OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func", |
492e4edb | 903 | "Trace given functions using function_graph tracer", |
eb6d31ae | 904 | parse_filter_func), |
78b83e8b NK |
905 | OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func", |
906 | "Set nograph filter on given functions", parse_filter_func), | |
38988f2e | 907 | OPT_CALLBACK(0, "graph-opts", &ftrace, "options", |
492e4edb | 908 | "Graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>", |
38988f2e | 909 | parse_graph_tracer_opts), |
846e1939 | 910 | OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size", |
492e4edb | 911 | "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size), |
5b347472 | 912 | OPT_BOOLEAN(0, "inherit", &ftrace.inherit, |
492e4edb | 913 | "Trace children processes"), |
6555c2f6 | 914 | OPT_UINTEGER('D', "delay", &ftrace.initial_delay, |
492e4edb | 915 | "Number of milliseconds to wait before starting tracing after program start"), |
416e15ad NK |
916 | OPT_PARENT(common_options), |
917 | }; | |
918 | ||
919 | const char * const ftrace_usage[] = { | |
920 | "perf ftrace [<options>] [<command>]", | |
921 | "perf ftrace [<options>] -- [<command>] [<options>]", | |
922 | "perf ftrace trace [<options>] [<command>]", | |
923 | "perf ftrace trace [<options>] -- [<command>] [<options>]", | |
924 | NULL | |
d01f4e8d NK |
925 | }; |
926 | ||
78b83e8b NK |
927 | INIT_LIST_HEAD(&ftrace.filters); |
928 | INIT_LIST_HEAD(&ftrace.notrace); | |
929 | INIT_LIST_HEAD(&ftrace.graph_funcs); | |
930 | INIT_LIST_HEAD(&ftrace.nograph_funcs); | |
931 | ||
a9b8ae8a NK |
932 | signal(SIGINT, sig_handler); |
933 | signal(SIGUSR1, sig_handler); | |
934 | signal(SIGCHLD, sig_handler); | |
935 | signal(SIGPIPE, sig_handler); | |
936 | ||
b05d1093 TS |
937 | ret = perf_config(perf_ftrace_config, &ftrace); |
938 | if (ret < 0) | |
939 | return -1; | |
940 | ||
416e15ad NK |
941 | if (argc > 1 && !strcmp(argv[1], "trace")) { |
942 | argc--; | |
943 | argv++; | |
944 | } | |
945 | ||
d01f4e8d NK |
946 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
947 | PARSE_OPT_STOP_AT_NON_OPTION); | |
a9af6be5 | 948 | if (!argc && target__none(&ftrace.target)) |
452b0d16 | 949 | ftrace.target.system_wide = true; |
d01f4e8d | 950 | |
a9af6be5 NK |
951 | ret = target__validate(&ftrace.target); |
952 | if (ret) { | |
953 | char errbuf[512]; | |
954 | ||
955 | target__strerror(&ftrace.target, ret, errbuf, 512); | |
956 | pr_err("%s\n", errbuf); | |
78b83e8b | 957 | goto out_delete_filters; |
a9af6be5 NK |
958 | } |
959 | ||
0f98b11c | 960 | ftrace.evlist = evlist__new(); |
78b83e8b NK |
961 | if (ftrace.evlist == NULL) { |
962 | ret = -ENOMEM; | |
963 | goto out_delete_filters; | |
964 | } | |
d01f4e8d | 965 | |
7748bb71 | 966 | ret = evlist__create_maps(ftrace.evlist, &ftrace.target); |
d01f4e8d NK |
967 | if (ret < 0) |
968 | goto out_delete_evlist; | |
969 | ||
a9b8ae8a NK |
970 | if (argc) { |
971 | ret = evlist__prepare_workload(ftrace.evlist, &ftrace.target, | |
972 | argv, false, | |
973 | ftrace__workload_exec_failed_signal); | |
974 | if (ret < 0) | |
975 | goto out_delete_evlist; | |
976 | } | |
977 | ||
978 | ret = __cmd_ftrace(&ftrace); | |
d01f4e8d NK |
979 | |
980 | out_delete_evlist: | |
c12995a5 | 981 | evlist__delete(ftrace.evlist); |
d01f4e8d | 982 | |
78b83e8b NK |
983 | out_delete_filters: |
984 | delete_filter_func(&ftrace.filters); | |
985 | delete_filter_func(&ftrace.notrace); | |
986 | delete_filter_func(&ftrace.graph_funcs); | |
987 | delete_filter_func(&ftrace.nograph_funcs); | |
988 | ||
d01f4e8d NK |
989 | return ret; |
990 | } |