Thomas Gleixner | 1c6bec5 | 2019-06-01 10:08:47 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 2 | /* |
| 3 | * builtin-ftrace.c |
| 4 | * |
| 5 | * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "builtin.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 10 | #include <errno.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | #include <signal.h> |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 13 | #include <stdlib.h> |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 14 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 15 | #include <poll.h> |
Igor Lubashev | c766f3d | 2019-08-07 10:44:17 -0400 | [diff] [blame] | 16 | #include <linux/capability.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 17 | #include <linux/string.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 18 | |
| 19 | #include "debug.h" |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 20 | #include <subcmd/pager.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 21 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 20a9ed2 | 2017-04-18 11:44:58 -0300 | [diff] [blame] | 22 | #include <api/fs/tracing_path.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 23 | #include "evlist.h" |
| 24 | #include "target.h" |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 25 | #include "cpumap.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 26 | #include "thread_map.h" |
Igor Lubashev | c766f3d | 2019-08-07 10:44:17 -0400 | [diff] [blame] | 27 | #include "util/cap.h" |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 28 | #include "util/config.h" |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 29 | #include "util/units.h" |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 30 | #include "util/parse-sublevel-options.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 31 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 32 | #define DEFAULT_TRACER "function_graph" |
| 33 | |
| 34 | struct perf_ftrace { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 35 | struct evlist *evlist; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 36 | struct target target; |
| 37 | const char *tracer; |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 38 | bool list_avail_functions; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 39 | struct list_head filters; |
| 40 | struct list_head notrace; |
| 41 | struct list_head graph_funcs; |
| 42 | struct list_head nograph_funcs; |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 43 | int graph_depth; |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 44 | unsigned long percpu_buffer_size; |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 45 | bool inherit; |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 46 | int func_stack_trace; |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 47 | int func_irq_info; |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 48 | int graph_nosleep_time; |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 49 | int graph_noirqs; |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 50 | int graph_verbose; |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 51 | int graph_thresh; |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 52 | unsigned int initial_delay; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct filter_entry { |
| 56 | struct list_head list; |
| 57 | char name[]; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 58 | }; |
| 59 | |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 60 | static volatile int workload_exec_errno; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 61 | static bool done; |
| 62 | |
| 63 | static 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 | */ |
| 75 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, |
| 76 | siginfo_t *info __maybe_unused, |
| 77 | void *ucontext __maybe_unused) |
| 78 | { |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 79 | workload_exec_errno = info->si_value.sival_int; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 80 | done = true; |
| 81 | } |
| 82 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 83 | static int __write_tracing_file(const char *name, const char *val, bool append) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 84 | { |
| 85 | char *file; |
| 86 | int fd, ret = -1; |
| 87 | ssize_t size = strlen(val); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 88 | int flags = O_WRONLY; |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 89 | char errbuf[512]; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 90 | char *val_copy; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 91 | |
| 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 Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 98 | if (append) |
| 99 | flags |= O_APPEND; |
| 100 | else |
| 101 | flags |= O_TRUNC; |
| 102 | |
| 103 | fd = open(file, flags); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 104 | if (fd < 0) { |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 105 | pr_debug("cannot open tracing file: %s: %s\n", |
| 106 | name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 107 | goto out; |
| 108 | } |
| 109 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 110 | /* |
| 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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 120 | ret = 0; |
| 121 | else |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 122 | pr_debug("write '%s' to tracing/%s failed: %s\n", |
| 123 | val, name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 124 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 125 | free(val_copy); |
| 126 | out_close: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 127 | close(fd); |
| 128 | out: |
| 129 | put_tracing_file(file); |
| 130 | return ret; |
| 131 | } |
| 132 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 133 | static int write_tracing_file(const char *name, const char *val) |
| 134 | { |
| 135 | return __write_tracing_file(name, val, false); |
| 136 | } |
| 137 | |
| 138 | static int append_tracing_file(const char *name, const char *val) |
| 139 | { |
| 140 | return __write_tracing_file(name, val, true); |
| 141 | } |
| 142 | |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 143 | static 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 | |
| 176 | out_close: |
| 177 | close(fd); |
| 178 | out: |
| 179 | put_tracing_file(file); |
| 180 | return ret; |
| 181 | } |
| 182 | |
Changbin Du | 68faab0 | 2020-08-08 10:31:26 +0800 | [diff] [blame] | 183 | static 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 Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 194 | static 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 Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 207 | static int reset_tracing_cpu(void); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 208 | static void reset_tracing_filters(void); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 209 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 210 | static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused) |
| 211 | { |
| 212 | write_tracing_option_file("function-fork", "0"); |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 213 | write_tracing_option_file("func_stack_trace", "0"); |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 214 | write_tracing_option_file("sleep-time", "1"); |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 215 | write_tracing_option_file("funcgraph-irqs", "1"); |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 216 | write_tracing_option_file("funcgraph-proc", "0"); |
| 217 | write_tracing_option_file("funcgraph-abstime", "0"); |
| 218 | write_tracing_option_file("latency-format", "0"); |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 219 | write_tracing_option_file("irq-info", "0"); |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 220 | } |
| 221 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 222 | static 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 Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 233 | if (reset_tracing_cpu() < 0) |
| 234 | return -1; |
| 235 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 236 | if (write_tracing_file("max_graph_depth", "0") < 0) |
| 237 | return -1; |
| 238 | |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 239 | if (write_tracing_file("tracing_thresh", "0") < 0) |
| 240 | return -1; |
| 241 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 242 | reset_tracing_filters(); |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 243 | reset_tracing_options(ftrace); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 247 | static 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 Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 255 | for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) { |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 256 | scnprintf(buf, sizeof(buf), "%d", |
Jiri Olsa | 03617c2 | 2019-07-21 13:24:42 +0200 | [diff] [blame] | 257 | ftrace->evlist->core.threads->map[i]); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 258 | if (append_tracing_file("set_ftrace_pid", buf) < 0) |
| 259 | return -1; |
| 260 | } |
| 261 | return 0; |
| 262 | } |
| 263 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 264 | static int set_tracing_cpumask(struct perf_cpu_map *cpumap) |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 265 | { |
| 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 Zhe | cf30ae7 | 2019-08-02 16:29:51 +0800 | [diff] [blame] | 272 | mask_size = last_cpu / 4 + 2; /* one more byte for EOS */ |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 273 | 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 | |
| 289 | static int set_tracing_cpu(struct perf_ftrace *ftrace) |
| 290 | { |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 291 | struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus; |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 292 | |
| 293 | if (!target__has_cpu(&ftrace->target)) |
| 294 | return 0; |
| 295 | |
| 296 | return set_tracing_cpumask(cpumap); |
| 297 | } |
| 298 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 299 | static 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 Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 310 | static 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 Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 321 | static int reset_tracing_cpu(void) |
| 322 | { |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 323 | struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 324 | int ret; |
| 325 | |
| 326 | ret = set_tracing_cpumask(cpumap); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 327 | perf_cpu_map__put(cpumap); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 331 | static 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 | |
| 343 | static 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 | |
| 365 | static 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 Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 373 | static int set_tracing_depth(struct perf_ftrace *ftrace) |
| 374 | { |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 375 | 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 Du | 68faab0 | 2020-08-08 10:31:26 +0800 | [diff] [blame] | 383 | if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0) |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 384 | return -1; |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 389 | static 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 Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 404 | static 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 Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 415 | static 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 Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 426 | static 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 Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 437 | static 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 Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 454 | static 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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 468 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) |
| 469 | { |
| 470 | char *trace_file; |
| 471 | int trace_fd; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 472 | char buf[4096]; |
| 473 | struct pollfd pollfd = { |
| 474 | .events = POLLIN, |
| 475 | }; |
| 476 | |
Alexey Budankov | 6b3e0e2 | 2020-04-02 11:47:35 +0300 | [diff] [blame] | 477 | if (!(perf_cap__capable(CAP_PERFMON) || |
| 478 | perf_cap__capable(CAP_SYS_ADMIN))) { |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 479 | pr_err("ftrace only works for %s!\n", |
| 480 | #ifdef HAVE_LIBCAP_SUPPORT |
Alexey Budankov | 6b3e0e2 | 2020-04-02 11:47:35 +0300 | [diff] [blame] | 481 | "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 482 | #else |
| 483 | "root" |
| 484 | #endif |
| 485 | ); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 486 | return -1; |
| 487 | } |
| 488 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 489 | signal(SIGINT, sig_handler); |
| 490 | signal(SIGUSR1, sig_handler); |
| 491 | signal(SIGCHLD, sig_handler); |
Namhyung Kim | 5833596 | 2017-02-24 10:12:51 +0900 | [diff] [blame] | 492 | signal(SIGPIPE, sig_handler); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 493 | |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 494 | if (ftrace->list_avail_functions) |
| 495 | return read_tracing_file_to_stdout("available_filter_functions"); |
| 496 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 497 | if (reset_tracing_files(ftrace) < 0) { |
| 498 | pr_err("failed to reset ftrace\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 499 | goto out; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 500 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 501 | |
| 502 | /* reset ftrace buffer */ |
| 503 | if (write_tracing_file("trace", "0") < 0) |
| 504 | goto out; |
| 505 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 506 | if (argc && perf_evlist__prepare_workload(ftrace->evlist, |
| 507 | &ftrace->target, argv, false, |
| 508 | ftrace__workload_exec_failed_signal) < 0) { |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 509 | goto out; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (set_tracing_pid(ftrace) < 0) { |
| 513 | pr_err("failed to set ftrace pid\n"); |
| 514 | goto out_reset; |
| 515 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 516 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 517 | if (set_tracing_cpu(ftrace) < 0) { |
| 518 | pr_err("failed to set tracing cpumask\n"); |
| 519 | goto out_reset; |
| 520 | } |
| 521 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 522 | 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 Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 527 | 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 Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 532 | if (set_tracing_filters(ftrace) < 0) { |
| 533 | pr_err("failed to set tracing filters\n"); |
| 534 | goto out_reset; |
| 535 | } |
| 536 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 537 | if (set_tracing_depth(ftrace) < 0) { |
| 538 | pr_err("failed to set graph depth\n"); |
| 539 | goto out_reset; |
| 540 | } |
| 541 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 542 | 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 Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 547 | 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 Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 552 | 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 Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 557 | 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 Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 562 | 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 Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 567 | if (set_tracing_thresh(ftrace) < 0) { |
| 568 | pr_err("failed to set tracing thresh\n"); |
| 569 | goto out_reset; |
| 570 | } |
| 571 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 572 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
| 573 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 574 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 575 | } |
| 576 | |
Namhyung Kim | 29681bc | 2017-06-18 23:23:00 +0900 | [diff] [blame] | 577 | setup_pager(); |
| 578 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 579 | trace_file = get_tracing_file("trace_pipe"); |
| 580 | if (!trace_file) { |
| 581 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 582 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 583 | } |
| 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 Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 591 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); |
| 595 | pollfd.fd = trace_fd; |
| 596 | |
Changbin Du | 81523c1 | 2020-08-08 10:31:28 +0800 | [diff] [blame] | 597 | /* display column headers */ |
| 598 | read_tracing_file_to_stdout("trace"); |
| 599 | |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 600 | 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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | perf_evlist__start_workload(ftrace->evlist); |
| 608 | |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 609 | 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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 617 | 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 Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 632 | 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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 640 | /* 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 | |
| 649 | out_close_fd: |
| 650 | close(trace_fd); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 651 | out_reset: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 652 | reset_tracing_files(ftrace); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 653 | out: |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 654 | return (done && !workload_exec_errno) ? 0 : -1; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 655 | } |
| 656 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 657 | static int perf_ftrace_config(const char *var, const char *value, void *cb) |
| 658 | { |
| 659 | struct perf_ftrace *ftrace = cb; |
| 660 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 661 | if (!strstarts(var, "ftrace.")) |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 662 | 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 Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 677 | static 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 | |
| 693 | static 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 Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 698 | list_del_init(&pos->list); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 699 | free(pos); |
| 700 | } |
| 701 | } |
| 702 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 703 | static 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 Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 734 | static 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 Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 741 | { .name = "irq-info", .value_ptr = &ftrace->func_irq_info }, |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 742 | { .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 Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 755 | static 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 Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 762 | { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs }, |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 763 | { .name = "verbose", .value_ptr = &ftrace->graph_verbose }, |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 764 | { .name = "thresh", .value_ptr = &ftrace->graph_thresh }, |
Changbin Du | a8f87a5 | 2020-08-08 10:31:37 +0800 | [diff] [blame] | 765 | { .name = "depth", .value_ptr = &ftrace->graph_depth }, |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 766 | { .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 Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 779 | static 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 Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 796 | int cmd_ftrace(int argc, const char **argv) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 797 | { |
| 798 | int ret; |
| 799 | struct perf_ftrace ftrace = { |
Taeung Song | bf062bd | 2017-01-26 18:35:37 +0900 | [diff] [blame] | 800 | .tracer = DEFAULT_TRACER, |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 801 | .target = { .uid = UINT_MAX, }, |
| 802 | }; |
| 803 | const char * const ftrace_usage[] = { |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 804 | "perf ftrace [<options>] [<command>]", |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 805 | "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 Melo | ec34787 | 2017-01-18 21:49:14 -0300 | [diff] [blame] | 810 | "tracer to use: function_graph(default) or function"), |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 811 | OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions, |
| 812 | "Show available functions to filter"), |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 813 | OPT_STRING('p', "pid", &ftrace.target.pid, "pid", |
| 814 | "trace on existing process id"), |
Changbin Du | 42145d7 | 2020-08-08 10:31:39 +0800 | [diff] [blame^] | 815 | /* 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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 818 | OPT_INCR('v', "verbose", &verbose, |
| 819 | "be more verbose"), |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 820 | 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 Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 824 | OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 825 | "trace given functions using function tracer", |
| 826 | parse_filter_func), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 827 | OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func", |
| 828 | "do not trace given functions", parse_filter_func), |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 829 | OPT_CALLBACK(0, "func-opts", &ftrace, "options", |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 830 | "function tracer options, available options: call-graph,irq-info", |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 831 | parse_func_tracer_opts), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 832 | OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func", |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 833 | "trace given functions using function_graph tracer", |
| 834 | parse_filter_func), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 835 | OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func", |
| 836 | "Set nograph filter on given functions", parse_filter_func), |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 837 | OPT_CALLBACK(0, "graph-opts", &ftrace, "options", |
Changbin Du | a8f87a5 | 2020-08-08 10:31:37 +0800 | [diff] [blame] | 838 | "graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>", |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 839 | parse_graph_tracer_opts), |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 840 | OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size", |
| 841 | "size of per cpu buffer", parse_buffer_size), |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 842 | OPT_BOOLEAN(0, "inherit", &ftrace.inherit, |
| 843 | "trace children processes"), |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 844 | OPT_UINTEGER('D', "delay", &ftrace.initial_delay, |
| 845 | "ms to wait before starting tracing after program start"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 846 | OPT_END() |
| 847 | }; |
| 848 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 849 | 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 Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 854 | ret = perf_config(perf_ftrace_config, &ftrace); |
| 855 | if (ret < 0) |
| 856 | return -1; |
| 857 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 858 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
| 859 | PARSE_OPT_STOP_AT_NON_OPTION); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 860 | if (!argc && target__none(&ftrace.target)) |
Changbin Du | 452b0d1 | 2020-05-10 23:06:10 +0800 | [diff] [blame] | 861 | ftrace.target.system_wide = true; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 862 | |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 863 | select_tracer(&ftrace); |
| 864 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 865 | 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 Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 871 | goto out_delete_filters; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 872 | } |
| 873 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 874 | ftrace.evlist = evlist__new(); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 875 | if (ftrace.evlist == NULL) { |
| 876 | ret = -ENOMEM; |
| 877 | goto out_delete_filters; |
| 878 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 879 | |
| 880 | ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target); |
| 881 | if (ret < 0) |
| 882 | goto out_delete_evlist; |
| 883 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 884 | ret = __cmd_ftrace(&ftrace, argc, argv); |
| 885 | |
| 886 | out_delete_evlist: |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 887 | evlist__delete(ftrace.evlist); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 888 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 889 | out_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 Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 895 | return ret; |
| 896 | } |