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> |
Changbin Du | 0094024 | 2020-08-08 10:31:41 +0800 | [diff] [blame] | 6 | * Copyright (c) 2020 Changbin Du <changbin.du@gmail.com>, significant enhancement. |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "builtin.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 11 | #include <errno.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 12 | #include <unistd.h> |
| 13 | #include <signal.h> |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 14 | #include <stdlib.h> |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 15 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 16 | #include <poll.h> |
Igor Lubashev | c766f3d | 2019-08-07 10:44:17 -0400 | [diff] [blame] | 17 | #include <linux/capability.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 18 | #include <linux/string.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 19 | |
| 20 | #include "debug.h" |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 21 | #include <subcmd/pager.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 22 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 20a9ed2 | 2017-04-18 11:44:58 -0300 | [diff] [blame] | 23 | #include <api/fs/tracing_path.h> |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 24 | #include "evlist.h" |
| 25 | #include "target.h" |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 26 | #include "cpumap.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 27 | #include "thread_map.h" |
Changbin Du | 2ae05fe | 2020-09-04 23:23:57 +0800 | [diff] [blame] | 28 | #include "strfilter.h" |
Igor Lubashev | c766f3d | 2019-08-07 10:44:17 -0400 | [diff] [blame] | 29 | #include "util/cap.h" |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 30 | #include "util/config.h" |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 31 | #include "util/units.h" |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 32 | #include "util/parse-sublevel-options.h" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 33 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 34 | #define DEFAULT_TRACER "function_graph" |
| 35 | |
| 36 | struct perf_ftrace { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 37 | struct evlist *evlist; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 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; |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 44 | int graph_depth; |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 45 | unsigned long percpu_buffer_size; |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 46 | bool inherit; |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 47 | int func_stack_trace; |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 48 | int func_irq_info; |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 49 | int graph_nosleep_time; |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 50 | int graph_noirqs; |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 51 | int graph_verbose; |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 52 | int graph_thresh; |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 53 | unsigned int initial_delay; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct filter_entry { |
| 57 | struct list_head list; |
| 58 | char name[]; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 61 | static volatile int workload_exec_errno; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 62 | static bool done; |
| 63 | |
| 64 | static void sig_handler(int sig __maybe_unused) |
| 65 | { |
| 66 | done = true; |
| 67 | } |
| 68 | |
| 69 | /* |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 70 | * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 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 | { |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 80 | workload_exec_errno = info->si_value.sival_int; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 81 | done = true; |
| 82 | } |
| 83 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | char *file; |
| 87 | int fd, ret = -1; |
| 88 | ssize_t size = strlen(val); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 89 | int flags = O_WRONLY; |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 90 | char errbuf[512]; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 91 | char *val_copy; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 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 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 99 | if (append) |
| 100 | flags |= O_APPEND; |
| 101 | else |
| 102 | flags |= O_TRUNC; |
| 103 | |
| 104 | fd = open(file, flags); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 105 | if (fd < 0) { |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 106 | pr_debug("cannot open tracing file: %s: %s\n", |
| 107 | name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 108 | goto out; |
| 109 | } |
| 110 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 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) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 121 | ret = 0; |
| 122 | else |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 123 | pr_debug("write '%s' to tracing/%s failed: %s\n", |
| 124 | val, name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 125 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 126 | free(val_copy); |
| 127 | out_close: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 128 | close(fd); |
| 129 | out: |
| 130 | put_tracing_file(file); |
| 131 | return ret; |
| 132 | } |
| 133 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 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 | |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 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 | |
Changbin Du | 2ae05fe | 2020-09-04 23:23:57 +0800 | [diff] [blame] | 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 | |
Changbin Du | 68faab0 | 2020-08-08 10:31:26 +0800 | [diff] [blame] | 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 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 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 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 242 | static int reset_tracing_cpu(void); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 243 | static void reset_tracing_filters(void); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 244 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 245 | static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused) |
| 246 | { |
| 247 | write_tracing_option_file("function-fork", "0"); |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 248 | write_tracing_option_file("func_stack_trace", "0"); |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 249 | write_tracing_option_file("sleep-time", "1"); |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 250 | write_tracing_option_file("funcgraph-irqs", "1"); |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 251 | write_tracing_option_file("funcgraph-proc", "0"); |
| 252 | write_tracing_option_file("funcgraph-abstime", "0"); |
| 253 | write_tracing_option_file("latency-format", "0"); |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 254 | write_tracing_option_file("irq-info", "0"); |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 255 | } |
| 256 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 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 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 268 | if (reset_tracing_cpu() < 0) |
| 269 | return -1; |
| 270 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 271 | if (write_tracing_file("max_graph_depth", "0") < 0) |
| 272 | return -1; |
| 273 | |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 274 | if (write_tracing_file("tracing_thresh", "0") < 0) |
| 275 | return -1; |
| 276 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 277 | reset_tracing_filters(); |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 278 | reset_tracing_options(ftrace); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 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 | |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 290 | 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] | 291 | scnprintf(buf, sizeof(buf), "%d", |
Thomas Richter | 671b60c | 2021-04-21 14:04:00 +0200 | [diff] [blame^] | 292 | perf_thread_map__pid(ftrace->evlist->core.threads, i)); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 293 | if (append_tracing_file("set_ftrace_pid", buf) < 0) |
| 294 | return -1; |
| 295 | } |
| 296 | return 0; |
| 297 | } |
| 298 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 299 | static int set_tracing_cpumask(struct perf_cpu_map *cpumap) |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 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); |
He Zhe | cf30ae7 | 2019-08-02 16:29:51 +0800 | [diff] [blame] | 307 | mask_size = last_cpu / 4 + 2; /* one more byte for EOS */ |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 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 | { |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 326 | struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus; |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 327 | |
| 328 | if (!target__has_cpu(&ftrace->target)) |
| 329 | return 0; |
| 330 | |
| 331 | return set_tracing_cpumask(cpumap); |
| 332 | } |
| 333 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 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 | |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 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 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 356 | static int reset_tracing_cpu(void) |
| 357 | { |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 358 | struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 359 | int ret; |
| 360 | |
| 361 | ret = set_tracing_cpumask(cpumap); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 362 | perf_cpu_map__put(cpumap); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 363 | return ret; |
| 364 | } |
| 365 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 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 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 408 | static int set_tracing_depth(struct perf_ftrace *ftrace) |
| 409 | { |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 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 | |
Changbin Du | 68faab0 | 2020-08-08 10:31:26 +0800 | [diff] [blame] | 418 | if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0) |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 419 | return -1; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 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 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 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 | |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 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 | |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 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 | |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 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 | |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 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 | |
Changbin Du | 3c4dc21 | 2020-08-08 10:31:40 +0800 | [diff] [blame] | 503 | static int set_tracing_options(struct perf_ftrace *ftrace) |
| 504 | { |
| 505 | if (set_tracing_pid(ftrace) < 0) { |
| 506 | pr_err("failed to set ftrace pid\n"); |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | if (set_tracing_cpu(ftrace) < 0) { |
| 511 | pr_err("failed to set tracing cpumask\n"); |
| 512 | return -1; |
| 513 | } |
| 514 | |
| 515 | if (set_tracing_func_stack_trace(ftrace) < 0) { |
| 516 | pr_err("failed to set tracing option func_stack_trace\n"); |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | if (set_tracing_func_irqinfo(ftrace) < 0) { |
| 521 | pr_err("failed to set tracing option irq-info\n"); |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | if (set_tracing_filters(ftrace) < 0) { |
| 526 | pr_err("failed to set tracing filters\n"); |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | if (set_tracing_depth(ftrace) < 0) { |
| 531 | pr_err("failed to set graph depth\n"); |
| 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | if (set_tracing_percpu_buffer_size(ftrace) < 0) { |
| 536 | pr_err("failed to set tracing per-cpu buffer size\n"); |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | if (set_tracing_trace_inherit(ftrace) < 0) { |
| 541 | pr_err("failed to set tracing option function-fork\n"); |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | if (set_tracing_sleep_time(ftrace) < 0) { |
| 546 | pr_err("failed to set tracing option sleep-time\n"); |
| 547 | return -1; |
| 548 | } |
| 549 | |
| 550 | if (set_tracing_funcgraph_irqs(ftrace) < 0) { |
| 551 | pr_err("failed to set tracing option funcgraph-irqs\n"); |
| 552 | return -1; |
| 553 | } |
| 554 | |
| 555 | if (set_tracing_funcgraph_verbose(ftrace) < 0) { |
| 556 | pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n"); |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | if (set_tracing_thresh(ftrace) < 0) { |
| 561 | pr_err("failed to set tracing thresh\n"); |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 568 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) |
| 569 | { |
| 570 | char *trace_file; |
| 571 | int trace_fd; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 572 | char buf[4096]; |
| 573 | struct pollfd pollfd = { |
| 574 | .events = POLLIN, |
| 575 | }; |
| 576 | |
Alexey Budankov | 6b3e0e2 | 2020-04-02 11:47:35 +0300 | [diff] [blame] | 577 | if (!(perf_cap__capable(CAP_PERFMON) || |
| 578 | perf_cap__capable(CAP_SYS_ADMIN))) { |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 579 | pr_err("ftrace only works for %s!\n", |
| 580 | #ifdef HAVE_LIBCAP_SUPPORT |
Alexey Budankov | 6b3e0e2 | 2020-04-02 11:47:35 +0300 | [diff] [blame] | 581 | "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 582 | #else |
| 583 | "root" |
| 584 | #endif |
| 585 | ); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 586 | return -1; |
| 587 | } |
| 588 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 589 | signal(SIGINT, sig_handler); |
| 590 | signal(SIGUSR1, sig_handler); |
| 591 | signal(SIGCHLD, sig_handler); |
Namhyung Kim | 5833596 | 2017-02-24 10:12:51 +0900 | [diff] [blame] | 592 | signal(SIGPIPE, sig_handler); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 593 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 594 | if (reset_tracing_files(ftrace) < 0) { |
| 595 | pr_err("failed to reset ftrace\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 596 | goto out; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 597 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 598 | |
| 599 | /* reset ftrace buffer */ |
| 600 | if (write_tracing_file("trace", "0") < 0) |
| 601 | goto out; |
| 602 | |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 603 | if (argc && evlist__prepare_workload(ftrace->evlist, &ftrace->target, argv, false, |
| 604 | ftrace__workload_exec_failed_signal) < 0) { |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 605 | goto out; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 606 | } |
| 607 | |
Changbin Du | 3c4dc21 | 2020-08-08 10:31:40 +0800 | [diff] [blame] | 608 | if (set_tracing_options(ftrace) < 0) |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 609 | goto out_reset; |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 610 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 611 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
| 612 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 613 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 614 | } |
| 615 | |
Namhyung Kim | 29681bc | 2017-06-18 23:23:00 +0900 | [diff] [blame] | 616 | setup_pager(); |
| 617 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 618 | trace_file = get_tracing_file("trace_pipe"); |
| 619 | if (!trace_file) { |
| 620 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 621 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | trace_fd = open(trace_file, O_RDONLY); |
| 625 | |
| 626 | put_tracing_file(trace_file); |
| 627 | |
| 628 | if (trace_fd < 0) { |
| 629 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 630 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); |
| 634 | pollfd.fd = trace_fd; |
| 635 | |
Changbin Du | 81523c1 | 2020-08-08 10:31:28 +0800 | [diff] [blame] | 636 | /* display column headers */ |
| 637 | read_tracing_file_to_stdout("trace"); |
| 638 | |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 639 | if (!ftrace->initial_delay) { |
| 640 | if (write_tracing_file("tracing_on", "1") < 0) { |
| 641 | pr_err("can't enable tracing\n"); |
| 642 | goto out_close_fd; |
| 643 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 644 | } |
| 645 | |
Arnaldo Carvalho de Melo | 7b392ef | 2020-11-30 09:26:54 -0300 | [diff] [blame] | 646 | evlist__start_workload(ftrace->evlist); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 647 | |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 648 | if (ftrace->initial_delay) { |
| 649 | usleep(ftrace->initial_delay * 1000); |
| 650 | if (write_tracing_file("tracing_on", "1") < 0) { |
| 651 | pr_err("can't enable tracing\n"); |
| 652 | goto out_close_fd; |
| 653 | } |
| 654 | } |
| 655 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 656 | while (!done) { |
| 657 | if (poll(&pollfd, 1, -1) < 0) |
| 658 | break; |
| 659 | |
| 660 | if (pollfd.revents & POLLIN) { |
| 661 | int n = read(trace_fd, buf, sizeof(buf)); |
| 662 | if (n < 0) |
| 663 | break; |
| 664 | if (fwrite(buf, n, 1, stdout) != 1) |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | write_tracing_file("tracing_on", "0"); |
| 670 | |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 671 | if (workload_exec_errno) { |
| 672 | const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf)); |
| 673 | /* flush stdout first so below error msg appears at the end. */ |
| 674 | fflush(stdout); |
| 675 | pr_err("workload failed: %s\n", emsg); |
| 676 | goto out_close_fd; |
| 677 | } |
| 678 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 679 | /* read remaining buffer contents */ |
| 680 | while (true) { |
| 681 | int n = read(trace_fd, buf, sizeof(buf)); |
| 682 | if (n <= 0) |
| 683 | break; |
| 684 | if (fwrite(buf, n, 1, stdout) != 1) |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | out_close_fd: |
| 689 | close(trace_fd); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 690 | out_reset: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 691 | reset_tracing_files(ftrace); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 692 | out: |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 693 | return (done && !workload_exec_errno) ? 0 : -1; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 694 | } |
| 695 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 696 | static int perf_ftrace_config(const char *var, const char *value, void *cb) |
| 697 | { |
| 698 | struct perf_ftrace *ftrace = cb; |
| 699 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 700 | if (!strstarts(var, "ftrace.")) |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 701 | return 0; |
| 702 | |
| 703 | if (strcmp(var, "ftrace.tracer")) |
| 704 | return -1; |
| 705 | |
| 706 | if (!strcmp(value, "function_graph") || |
| 707 | !strcmp(value, "function")) { |
| 708 | ftrace->tracer = value; |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | pr_err("Please select \"function_graph\" (default) or \"function\"\n"); |
| 713 | return -1; |
| 714 | } |
| 715 | |
Changbin Du | 2ae05fe | 2020-09-04 23:23:57 +0800 | [diff] [blame] | 716 | static void list_function_cb(char *str, void *arg) |
| 717 | { |
| 718 | struct strfilter *filter = (struct strfilter *)arg; |
| 719 | |
| 720 | if (strfilter__compare(filter, str)) |
| 721 | printf("%s", str); |
| 722 | } |
| 723 | |
| 724 | static int opt_list_avail_functions(const struct option *opt __maybe_unused, |
| 725 | const char *str, int unset) |
| 726 | { |
| 727 | struct strfilter *filter; |
| 728 | const char *err = NULL; |
| 729 | int ret; |
| 730 | |
| 731 | if (unset || !str) |
| 732 | return -1; |
| 733 | |
| 734 | filter = strfilter__new(str, &err); |
| 735 | if (!filter) |
| 736 | return err ? -EINVAL : -ENOMEM; |
| 737 | |
| 738 | ret = strfilter__or(filter, str, &err); |
| 739 | if (ret == -EINVAL) { |
| 740 | pr_err("Filter parse error at %td.\n", err - str + 1); |
| 741 | pr_err("Source: \"%s\"\n", str); |
| 742 | pr_err(" %*c\n", (int)(err - str + 1), '^'); |
| 743 | strfilter__delete(filter); |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | ret = read_tracing_file_by_line("available_filter_functions", |
| 748 | list_function_cb, filter); |
| 749 | strfilter__delete(filter); |
| 750 | if (ret < 0) |
| 751 | return ret; |
| 752 | |
| 753 | exit(0); |
| 754 | } |
| 755 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 756 | static int parse_filter_func(const struct option *opt, const char *str, |
| 757 | int unset __maybe_unused) |
| 758 | { |
| 759 | struct list_head *head = opt->value; |
| 760 | struct filter_entry *entry; |
| 761 | |
| 762 | entry = malloc(sizeof(*entry) + strlen(str) + 1); |
| 763 | if (entry == NULL) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | strcpy(entry->name, str); |
| 767 | list_add_tail(&entry->list, head); |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static void delete_filter_func(struct list_head *head) |
| 773 | { |
| 774 | struct filter_entry *pos, *tmp; |
| 775 | |
| 776 | list_for_each_entry_safe(pos, tmp, head, list) { |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 777 | list_del_init(&pos->list); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 778 | free(pos); |
| 779 | } |
| 780 | } |
| 781 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 782 | static int parse_buffer_size(const struct option *opt, |
| 783 | const char *str, int unset) |
| 784 | { |
| 785 | unsigned long *s = (unsigned long *)opt->value; |
| 786 | static struct parse_tag tags_size[] = { |
| 787 | { .tag = 'B', .mult = 1 }, |
| 788 | { .tag = 'K', .mult = 1 << 10 }, |
| 789 | { .tag = 'M', .mult = 1 << 20 }, |
| 790 | { .tag = 'G', .mult = 1 << 30 }, |
| 791 | { .tag = 0 }, |
| 792 | }; |
| 793 | unsigned long val; |
| 794 | |
| 795 | if (unset) { |
| 796 | *s = 0; |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | val = parse_tag_value(str, tags_size); |
| 801 | if (val != (unsigned long) -1) { |
| 802 | if (val < 1024) { |
| 803 | pr_err("buffer size too small, must larger than 1KB."); |
| 804 | return -1; |
| 805 | } |
| 806 | *s = val; |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | return -1; |
| 811 | } |
| 812 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 813 | static int parse_func_tracer_opts(const struct option *opt, |
| 814 | const char *str, int unset) |
| 815 | { |
| 816 | int ret; |
| 817 | struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; |
| 818 | struct sublevel_option func_tracer_opts[] = { |
| 819 | { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace }, |
Changbin Du | c81fc34 | 2020-08-08 10:31:34 +0800 | [diff] [blame] | 820 | { .name = "irq-info", .value_ptr = &ftrace->func_irq_info }, |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 821 | { .name = NULL, } |
| 822 | }; |
| 823 | |
| 824 | if (unset) |
| 825 | return 0; |
| 826 | |
| 827 | ret = perf_parse_sublevel_options(str, func_tracer_opts); |
| 828 | if (ret) |
| 829 | return ret; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 834 | static int parse_graph_tracer_opts(const struct option *opt, |
| 835 | const char *str, int unset) |
| 836 | { |
| 837 | int ret; |
| 838 | struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; |
| 839 | struct sublevel_option graph_tracer_opts[] = { |
| 840 | { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time }, |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame] | 841 | { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs }, |
Changbin Du | 59486fb | 2020-08-08 10:31:35 +0800 | [diff] [blame] | 842 | { .name = "verbose", .value_ptr = &ftrace->graph_verbose }, |
Changbin Du | 00c85d5 | 2020-08-08 10:31:36 +0800 | [diff] [blame] | 843 | { .name = "thresh", .value_ptr = &ftrace->graph_thresh }, |
Changbin Du | a8f87a5 | 2020-08-08 10:31:37 +0800 | [diff] [blame] | 844 | { .name = "depth", .value_ptr = &ftrace->graph_depth }, |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 845 | { .name = NULL, } |
| 846 | }; |
| 847 | |
| 848 | if (unset) |
| 849 | return 0; |
| 850 | |
| 851 | ret = perf_parse_sublevel_options(str, graph_tracer_opts); |
| 852 | if (ret) |
| 853 | return ret; |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 858 | static void select_tracer(struct perf_ftrace *ftrace) |
| 859 | { |
| 860 | bool graph = !list_empty(&ftrace->graph_funcs) || |
| 861 | !list_empty(&ftrace->nograph_funcs); |
| 862 | bool func = !list_empty(&ftrace->filters) || |
| 863 | !list_empty(&ftrace->notrace); |
| 864 | |
| 865 | /* The function_graph has priority over function tracer. */ |
| 866 | if (graph) |
| 867 | ftrace->tracer = "function_graph"; |
| 868 | else if (func) |
| 869 | ftrace->tracer = "function"; |
| 870 | /* Otherwise, the default tracer is used. */ |
| 871 | |
| 872 | pr_debug("%s tracer is used\n", ftrace->tracer); |
| 873 | } |
| 874 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 875 | int cmd_ftrace(int argc, const char **argv) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 876 | { |
| 877 | int ret; |
| 878 | struct perf_ftrace ftrace = { |
Taeung Song | bf062bd | 2017-01-26 18:35:37 +0900 | [diff] [blame] | 879 | .tracer = DEFAULT_TRACER, |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 880 | .target = { .uid = UINT_MAX, }, |
| 881 | }; |
| 882 | const char * const ftrace_usage[] = { |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 883 | "perf ftrace [<options>] [<command>]", |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 884 | "perf ftrace [<options>] -- <command> [<options>]", |
| 885 | NULL |
| 886 | }; |
| 887 | const struct option ftrace_options[] = { |
| 888 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 889 | "Tracer to use: function_graph(default) or function"), |
Changbin Du | 2ae05fe | 2020-09-04 23:23:57 +0800 | [diff] [blame] | 890 | OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]", |
| 891 | "Show available functions to filter", |
| 892 | opt_list_avail_functions, "*"), |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 893 | OPT_STRING('p', "pid", &ftrace.target.pid, "pid", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 894 | "Trace on existing process id"), |
Changbin Du | 42145d7 | 2020-08-08 10:31:39 +0800 | [diff] [blame] | 895 | /* TODO: Add short option -t after -t/--tracer can be removed. */ |
| 896 | OPT_STRING(0, "tid", &ftrace.target.tid, "tid", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 897 | "Trace on existing thread id (exclusive to --pid)"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 898 | OPT_INCR('v', "verbose", &verbose, |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 899 | "Be more verbose"), |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 900 | OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide, |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 901 | "System-wide collection from all CPUs"), |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 902 | OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 903 | "List of cpus to monitor"), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 904 | OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 905 | "Trace given functions using function tracer", |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 906 | parse_filter_func), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 907 | OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 908 | "Do not trace given functions", parse_filter_func), |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 909 | OPT_CALLBACK(0, "func-opts", &ftrace, "options", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 910 | "Function tracer options, available options: call-graph,irq-info", |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 911 | parse_func_tracer_opts), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 912 | OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 913 | "Trace given functions using function_graph tracer", |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 914 | parse_filter_func), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 915 | OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func", |
| 916 | "Set nograph filter on given functions", parse_filter_func), |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 917 | OPT_CALLBACK(0, "graph-opts", &ftrace, "options", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 918 | "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] | 919 | parse_graph_tracer_opts), |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 920 | OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size", |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 921 | "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size), |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 922 | OPT_BOOLEAN(0, "inherit", &ftrace.inherit, |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 923 | "Trace children processes"), |
Changbin Du | 6555c2f | 2020-08-08 10:31:38 +0800 | [diff] [blame] | 924 | OPT_UINTEGER('D', "delay", &ftrace.initial_delay, |
Arnaldo Carvalho de Melo | 492e4ed | 2020-08-14 09:55:33 -0300 | [diff] [blame] | 925 | "Number of milliseconds to wait before starting tracing after program start"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 926 | OPT_END() |
| 927 | }; |
| 928 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 929 | INIT_LIST_HEAD(&ftrace.filters); |
| 930 | INIT_LIST_HEAD(&ftrace.notrace); |
| 931 | INIT_LIST_HEAD(&ftrace.graph_funcs); |
| 932 | INIT_LIST_HEAD(&ftrace.nograph_funcs); |
| 933 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 934 | ret = perf_config(perf_ftrace_config, &ftrace); |
| 935 | if (ret < 0) |
| 936 | return -1; |
| 937 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 938 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
| 939 | PARSE_OPT_STOP_AT_NON_OPTION); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 940 | if (!argc && target__none(&ftrace.target)) |
Changbin Du | 452b0d1 | 2020-05-10 23:06:10 +0800 | [diff] [blame] | 941 | ftrace.target.system_wide = true; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 942 | |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 943 | select_tracer(&ftrace); |
| 944 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 945 | ret = target__validate(&ftrace.target); |
| 946 | if (ret) { |
| 947 | char errbuf[512]; |
| 948 | |
| 949 | target__strerror(&ftrace.target, ret, errbuf, 512); |
| 950 | pr_err("%s\n", errbuf); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 951 | goto out_delete_filters; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 952 | } |
| 953 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 954 | ftrace.evlist = evlist__new(); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 955 | if (ftrace.evlist == NULL) { |
| 956 | ret = -ENOMEM; |
| 957 | goto out_delete_filters; |
| 958 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 959 | |
Arnaldo Carvalho de Melo | 7748bb7 | 2020-11-30 14:56:52 -0300 | [diff] [blame] | 960 | ret = evlist__create_maps(ftrace.evlist, &ftrace.target); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 961 | if (ret < 0) |
| 962 | goto out_delete_evlist; |
| 963 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 964 | ret = __cmd_ftrace(&ftrace, argc, argv); |
| 965 | |
| 966 | out_delete_evlist: |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 967 | evlist__delete(ftrace.evlist); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 968 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 969 | out_delete_filters: |
| 970 | delete_filter_func(&ftrace.filters); |
| 971 | delete_filter_func(&ftrace.notrace); |
| 972 | delete_filter_func(&ftrace.graph_funcs); |
| 973 | delete_filter_func(&ftrace.nograph_funcs); |
| 974 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 975 | return ret; |
| 976 | } |