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 | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 47 | int graph_nosleep_time; |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame^] | 48 | int graph_noirqs; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | struct filter_entry { |
| 52 | struct list_head list; |
| 53 | char name[]; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 56 | static volatile int workload_exec_errno; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 57 | static bool done; |
| 58 | |
| 59 | static void sig_handler(int sig __maybe_unused) |
| 60 | { |
| 61 | done = true; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since |
| 66 | * we asked by setting its exec_error to the function below, |
| 67 | * ftrace__workload_exec_failed_signal. |
| 68 | * |
| 69 | * XXX We need to handle this more appropriately, emitting an error, etc. |
| 70 | */ |
| 71 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, |
| 72 | siginfo_t *info __maybe_unused, |
| 73 | void *ucontext __maybe_unused) |
| 74 | { |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 75 | workload_exec_errno = info->si_value.sival_int; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 76 | done = true; |
| 77 | } |
| 78 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | char *file; |
| 82 | int fd, ret = -1; |
| 83 | ssize_t size = strlen(val); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 84 | int flags = O_WRONLY; |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 85 | char errbuf[512]; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 86 | char *val_copy; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 87 | |
| 88 | file = get_tracing_file(name); |
| 89 | if (!file) { |
| 90 | pr_debug("cannot get tracing file: %s\n", name); |
| 91 | return -1; |
| 92 | } |
| 93 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 94 | if (append) |
| 95 | flags |= O_APPEND; |
| 96 | else |
| 97 | flags |= O_TRUNC; |
| 98 | |
| 99 | fd = open(file, flags); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 100 | if (fd < 0) { |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 101 | pr_debug("cannot open tracing file: %s: %s\n", |
| 102 | name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 103 | goto out; |
| 104 | } |
| 105 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 106 | /* |
| 107 | * Copy the original value and append a '\n'. Without this, |
| 108 | * the kernel can hide possible errors. |
| 109 | */ |
| 110 | val_copy = strdup(val); |
| 111 | if (!val_copy) |
| 112 | goto out_close; |
| 113 | val_copy[size] = '\n'; |
| 114 | |
| 115 | if (write(fd, val_copy, size + 1) == size + 1) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 116 | ret = 0; |
| 117 | else |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 118 | pr_debug("write '%s' to tracing/%s failed: %s\n", |
| 119 | val, name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 120 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 121 | free(val_copy); |
| 122 | out_close: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 123 | close(fd); |
| 124 | out: |
| 125 | put_tracing_file(file); |
| 126 | return ret; |
| 127 | } |
| 128 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 129 | static int write_tracing_file(const char *name, const char *val) |
| 130 | { |
| 131 | return __write_tracing_file(name, val, false); |
| 132 | } |
| 133 | |
| 134 | static int append_tracing_file(const char *name, const char *val) |
| 135 | { |
| 136 | return __write_tracing_file(name, val, true); |
| 137 | } |
| 138 | |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 139 | static int read_tracing_file_to_stdout(const char *name) |
| 140 | { |
| 141 | char buf[4096]; |
| 142 | char *file; |
| 143 | int fd; |
| 144 | int ret = -1; |
| 145 | |
| 146 | file = get_tracing_file(name); |
| 147 | if (!file) { |
| 148 | pr_debug("cannot get tracing file: %s\n", name); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | fd = open(file, O_RDONLY); |
| 153 | if (fd < 0) { |
| 154 | pr_debug("cannot open tracing file: %s: %s\n", |
| 155 | name, str_error_r(errno, buf, sizeof(buf))); |
| 156 | goto out; |
| 157 | } |
| 158 | |
| 159 | /* read contents to stdout */ |
| 160 | while (true) { |
| 161 | int n = read(fd, buf, sizeof(buf)); |
| 162 | if (n == 0) |
| 163 | break; |
| 164 | else if (n < 0) |
| 165 | goto out_close; |
| 166 | |
| 167 | if (fwrite(buf, n, 1, stdout) != 1) |
| 168 | goto out_close; |
| 169 | } |
| 170 | ret = 0; |
| 171 | |
| 172 | out_close: |
| 173 | close(fd); |
| 174 | out: |
| 175 | put_tracing_file(file); |
| 176 | return ret; |
| 177 | } |
| 178 | |
Changbin Du | 68faab0 | 2020-08-08 10:31:26 +0800 | [diff] [blame] | 179 | static int write_tracing_file_int(const char *name, int value) |
| 180 | { |
| 181 | char buf[16]; |
| 182 | |
| 183 | snprintf(buf, sizeof(buf), "%d", value); |
| 184 | if (write_tracing_file(name, buf) < 0) |
| 185 | return -1; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 190 | static int write_tracing_option_file(const char *name, const char *val) |
| 191 | { |
| 192 | char *file; |
| 193 | int ret; |
| 194 | |
| 195 | if (asprintf(&file, "options/%s", name) < 0) |
| 196 | return -1; |
| 197 | |
| 198 | ret = __write_tracing_file(file, val, false); |
| 199 | free(file); |
| 200 | return ret; |
| 201 | } |
| 202 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 203 | static int reset_tracing_cpu(void); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 204 | static void reset_tracing_filters(void); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 205 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 206 | static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused) |
| 207 | { |
| 208 | write_tracing_option_file("function-fork", "0"); |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 209 | write_tracing_option_file("func_stack_trace", "0"); |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 210 | write_tracing_option_file("sleep-time", "1"); |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame^] | 211 | write_tracing_option_file("funcgraph-irqs", "1"); |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 214 | static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) |
| 215 | { |
| 216 | if (write_tracing_file("tracing_on", "0") < 0) |
| 217 | return -1; |
| 218 | |
| 219 | if (write_tracing_file("current_tracer", "nop") < 0) |
| 220 | return -1; |
| 221 | |
| 222 | if (write_tracing_file("set_ftrace_pid", " ") < 0) |
| 223 | return -1; |
| 224 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 225 | if (reset_tracing_cpu() < 0) |
| 226 | return -1; |
| 227 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 228 | if (write_tracing_file("max_graph_depth", "0") < 0) |
| 229 | return -1; |
| 230 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 231 | reset_tracing_filters(); |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 232 | reset_tracing_options(ftrace); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 236 | static int set_tracing_pid(struct perf_ftrace *ftrace) |
| 237 | { |
| 238 | int i; |
| 239 | char buf[16]; |
| 240 | |
| 241 | if (target__has_cpu(&ftrace->target)) |
| 242 | return 0; |
| 243 | |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 244 | 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] | 245 | scnprintf(buf, sizeof(buf), "%d", |
Jiri Olsa | 03617c2 | 2019-07-21 13:24:42 +0200 | [diff] [blame] | 246 | ftrace->evlist->core.threads->map[i]); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 247 | if (append_tracing_file("set_ftrace_pid", buf) < 0) |
| 248 | return -1; |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 253 | static int set_tracing_cpumask(struct perf_cpu_map *cpumap) |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 254 | { |
| 255 | char *cpumask; |
| 256 | size_t mask_size; |
| 257 | int ret; |
| 258 | int last_cpu; |
| 259 | |
| 260 | last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1); |
He Zhe | cf30ae7 | 2019-08-02 16:29:51 +0800 | [diff] [blame] | 261 | mask_size = last_cpu / 4 + 2; /* one more byte for EOS */ |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 262 | mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */ |
| 263 | |
| 264 | cpumask = malloc(mask_size); |
| 265 | if (cpumask == NULL) { |
| 266 | pr_debug("failed to allocate cpu mask\n"); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | cpu_map__snprint_mask(cpumap, cpumask, mask_size); |
| 271 | |
| 272 | ret = write_tracing_file("tracing_cpumask", cpumask); |
| 273 | |
| 274 | free(cpumask); |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int set_tracing_cpu(struct perf_ftrace *ftrace) |
| 279 | { |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 280 | struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus; |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 281 | |
| 282 | if (!target__has_cpu(&ftrace->target)) |
| 283 | return 0; |
| 284 | |
| 285 | return set_tracing_cpumask(cpumap); |
| 286 | } |
| 287 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 288 | static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace) |
| 289 | { |
| 290 | if (!ftrace->func_stack_trace) |
| 291 | return 0; |
| 292 | |
| 293 | if (write_tracing_option_file("func_stack_trace", "1") < 0) |
| 294 | return -1; |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 299 | static int reset_tracing_cpu(void) |
| 300 | { |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 301 | struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 302 | int ret; |
| 303 | |
| 304 | ret = set_tracing_cpumask(cpumap); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 305 | perf_cpu_map__put(cpumap); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 306 | return ret; |
| 307 | } |
| 308 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 309 | static int __set_tracing_filter(const char *filter_file, struct list_head *funcs) |
| 310 | { |
| 311 | struct filter_entry *pos; |
| 312 | |
| 313 | list_for_each_entry(pos, funcs, list) { |
| 314 | if (append_tracing_file(filter_file, pos->name) < 0) |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int set_tracing_filters(struct perf_ftrace *ftrace) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters); |
| 326 | if (ret < 0) |
| 327 | return ret; |
| 328 | |
| 329 | ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace); |
| 330 | if (ret < 0) |
| 331 | return ret; |
| 332 | |
| 333 | ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs); |
| 334 | if (ret < 0) |
| 335 | return ret; |
| 336 | |
| 337 | /* old kernels do not have this filter */ |
| 338 | __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs); |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | static void reset_tracing_filters(void) |
| 344 | { |
| 345 | write_tracing_file("set_ftrace_filter", " "); |
| 346 | write_tracing_file("set_ftrace_notrace", " "); |
| 347 | write_tracing_file("set_graph_function", " "); |
| 348 | write_tracing_file("set_graph_notrace", " "); |
| 349 | } |
| 350 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 351 | static int set_tracing_depth(struct perf_ftrace *ftrace) |
| 352 | { |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 353 | if (ftrace->graph_depth == 0) |
| 354 | return 0; |
| 355 | |
| 356 | if (ftrace->graph_depth < 0) { |
| 357 | pr_err("invalid graph depth: %d\n", ftrace->graph_depth); |
| 358 | return -1; |
| 359 | } |
| 360 | |
Changbin Du | 68faab0 | 2020-08-08 10:31:26 +0800 | [diff] [blame] | 361 | if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0) |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 362 | return -1; |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 367 | static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace) |
| 368 | { |
| 369 | int ret; |
| 370 | |
| 371 | if (ftrace->percpu_buffer_size == 0) |
| 372 | return 0; |
| 373 | |
| 374 | ret = write_tracing_file_int("buffer_size_kb", |
| 375 | ftrace->percpu_buffer_size / 1024); |
| 376 | if (ret < 0) |
| 377 | return ret; |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 382 | static int set_tracing_trace_inherit(struct perf_ftrace *ftrace) |
| 383 | { |
| 384 | if (!ftrace->inherit) |
| 385 | return 0; |
| 386 | |
| 387 | if (write_tracing_option_file("function-fork", "1") < 0) |
| 388 | return -1; |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 393 | static int set_tracing_sleep_time(struct perf_ftrace *ftrace) |
| 394 | { |
| 395 | if (!ftrace->graph_nosleep_time) |
| 396 | return 0; |
| 397 | |
| 398 | if (write_tracing_option_file("sleep-time", "0") < 0) |
| 399 | return -1; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame^] | 404 | static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace) |
| 405 | { |
| 406 | if (!ftrace->graph_noirqs) |
| 407 | return 0; |
| 408 | |
| 409 | if (write_tracing_option_file("funcgraph-irqs", "0") < 0) |
| 410 | return -1; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 415 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) |
| 416 | { |
| 417 | char *trace_file; |
| 418 | int trace_fd; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 419 | char buf[4096]; |
| 420 | struct pollfd pollfd = { |
| 421 | .events = POLLIN, |
| 422 | }; |
| 423 | |
Alexey Budankov | 6b3e0e2 | 2020-04-02 11:47:35 +0300 | [diff] [blame] | 424 | if (!(perf_cap__capable(CAP_PERFMON) || |
| 425 | perf_cap__capable(CAP_SYS_ADMIN))) { |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 426 | pr_err("ftrace only works for %s!\n", |
| 427 | #ifdef HAVE_LIBCAP_SUPPORT |
Alexey Budankov | 6b3e0e2 | 2020-04-02 11:47:35 +0300 | [diff] [blame] | 428 | "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 429 | #else |
| 430 | "root" |
| 431 | #endif |
| 432 | ); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 433 | return -1; |
| 434 | } |
| 435 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 436 | signal(SIGINT, sig_handler); |
| 437 | signal(SIGUSR1, sig_handler); |
| 438 | signal(SIGCHLD, sig_handler); |
Namhyung Kim | 5833596 | 2017-02-24 10:12:51 +0900 | [diff] [blame] | 439 | signal(SIGPIPE, sig_handler); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 440 | |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 441 | if (ftrace->list_avail_functions) |
| 442 | return read_tracing_file_to_stdout("available_filter_functions"); |
| 443 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 444 | if (reset_tracing_files(ftrace) < 0) { |
| 445 | pr_err("failed to reset ftrace\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 446 | goto out; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 447 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 448 | |
| 449 | /* reset ftrace buffer */ |
| 450 | if (write_tracing_file("trace", "0") < 0) |
| 451 | goto out; |
| 452 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 453 | if (argc && perf_evlist__prepare_workload(ftrace->evlist, |
| 454 | &ftrace->target, argv, false, |
| 455 | ftrace__workload_exec_failed_signal) < 0) { |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 456 | goto out; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if (set_tracing_pid(ftrace) < 0) { |
| 460 | pr_err("failed to set ftrace pid\n"); |
| 461 | goto out_reset; |
| 462 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 463 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 464 | if (set_tracing_cpu(ftrace) < 0) { |
| 465 | pr_err("failed to set tracing cpumask\n"); |
| 466 | goto out_reset; |
| 467 | } |
| 468 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 469 | if (set_tracing_func_stack_trace(ftrace) < 0) { |
| 470 | pr_err("failed to set tracing option func_stack_trace\n"); |
| 471 | goto out_reset; |
| 472 | } |
| 473 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 474 | if (set_tracing_filters(ftrace) < 0) { |
| 475 | pr_err("failed to set tracing filters\n"); |
| 476 | goto out_reset; |
| 477 | } |
| 478 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 479 | if (set_tracing_depth(ftrace) < 0) { |
| 480 | pr_err("failed to set graph depth\n"); |
| 481 | goto out_reset; |
| 482 | } |
| 483 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 484 | if (set_tracing_percpu_buffer_size(ftrace) < 0) { |
| 485 | pr_err("failed to set tracing per-cpu buffer size\n"); |
| 486 | goto out_reset; |
| 487 | } |
| 488 | |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 489 | if (set_tracing_trace_inherit(ftrace) < 0) { |
| 490 | pr_err("failed to set tracing option function-fork\n"); |
| 491 | goto out_reset; |
| 492 | } |
| 493 | |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 494 | if (set_tracing_sleep_time(ftrace) < 0) { |
| 495 | pr_err("failed to set tracing option sleep-time\n"); |
| 496 | goto out_reset; |
| 497 | } |
| 498 | |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame^] | 499 | if (set_tracing_funcgraph_irqs(ftrace) < 0) { |
| 500 | pr_err("failed to set tracing option funcgraph-irqs\n"); |
| 501 | goto out_reset; |
| 502 | } |
| 503 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 504 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
| 505 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 506 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 507 | } |
| 508 | |
Namhyung Kim | 29681bc | 2017-06-18 23:23:00 +0900 | [diff] [blame] | 509 | setup_pager(); |
| 510 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 511 | trace_file = get_tracing_file("trace_pipe"); |
| 512 | if (!trace_file) { |
| 513 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 514 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | trace_fd = open(trace_file, O_RDONLY); |
| 518 | |
| 519 | put_tracing_file(trace_file); |
| 520 | |
| 521 | if (trace_fd < 0) { |
| 522 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 523 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); |
| 527 | pollfd.fd = trace_fd; |
| 528 | |
Changbin Du | 81523c1 | 2020-08-08 10:31:28 +0800 | [diff] [blame] | 529 | /* display column headers */ |
| 530 | read_tracing_file_to_stdout("trace"); |
| 531 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 532 | if (write_tracing_file("tracing_on", "1") < 0) { |
| 533 | pr_err("can't enable tracing\n"); |
| 534 | goto out_close_fd; |
| 535 | } |
| 536 | |
| 537 | perf_evlist__start_workload(ftrace->evlist); |
| 538 | |
| 539 | while (!done) { |
| 540 | if (poll(&pollfd, 1, -1) < 0) |
| 541 | break; |
| 542 | |
| 543 | if (pollfd.revents & POLLIN) { |
| 544 | int n = read(trace_fd, buf, sizeof(buf)); |
| 545 | if (n < 0) |
| 546 | break; |
| 547 | if (fwrite(buf, n, 1, stdout) != 1) |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | write_tracing_file("tracing_on", "0"); |
| 553 | |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 554 | if (workload_exec_errno) { |
| 555 | const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf)); |
| 556 | /* flush stdout first so below error msg appears at the end. */ |
| 557 | fflush(stdout); |
| 558 | pr_err("workload failed: %s\n", emsg); |
| 559 | goto out_close_fd; |
| 560 | } |
| 561 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 562 | /* read remaining buffer contents */ |
| 563 | while (true) { |
| 564 | int n = read(trace_fd, buf, sizeof(buf)); |
| 565 | if (n <= 0) |
| 566 | break; |
| 567 | if (fwrite(buf, n, 1, stdout) != 1) |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | out_close_fd: |
| 572 | close(trace_fd); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 573 | out_reset: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 574 | reset_tracing_files(ftrace); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 575 | out: |
Changbin Du | 51a09d8 | 2020-05-10 23:06:11 +0800 | [diff] [blame] | 576 | return (done && !workload_exec_errno) ? 0 : -1; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 577 | } |
| 578 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 579 | static int perf_ftrace_config(const char *var, const char *value, void *cb) |
| 580 | { |
| 581 | struct perf_ftrace *ftrace = cb; |
| 582 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 583 | if (!strstarts(var, "ftrace.")) |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 584 | return 0; |
| 585 | |
| 586 | if (strcmp(var, "ftrace.tracer")) |
| 587 | return -1; |
| 588 | |
| 589 | if (!strcmp(value, "function_graph") || |
| 590 | !strcmp(value, "function")) { |
| 591 | ftrace->tracer = value; |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | pr_err("Please select \"function_graph\" (default) or \"function\"\n"); |
| 596 | return -1; |
| 597 | } |
| 598 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 599 | static int parse_filter_func(const struct option *opt, const char *str, |
| 600 | int unset __maybe_unused) |
| 601 | { |
| 602 | struct list_head *head = opt->value; |
| 603 | struct filter_entry *entry; |
| 604 | |
| 605 | entry = malloc(sizeof(*entry) + strlen(str) + 1); |
| 606 | if (entry == NULL) |
| 607 | return -ENOMEM; |
| 608 | |
| 609 | strcpy(entry->name, str); |
| 610 | list_add_tail(&entry->list, head); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static void delete_filter_func(struct list_head *head) |
| 616 | { |
| 617 | struct filter_entry *pos, *tmp; |
| 618 | |
| 619 | list_for_each_entry_safe(pos, tmp, head, list) { |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 620 | list_del_init(&pos->list); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 621 | free(pos); |
| 622 | } |
| 623 | } |
| 624 | |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 625 | static int parse_buffer_size(const struct option *opt, |
| 626 | const char *str, int unset) |
| 627 | { |
| 628 | unsigned long *s = (unsigned long *)opt->value; |
| 629 | static struct parse_tag tags_size[] = { |
| 630 | { .tag = 'B', .mult = 1 }, |
| 631 | { .tag = 'K', .mult = 1 << 10 }, |
| 632 | { .tag = 'M', .mult = 1 << 20 }, |
| 633 | { .tag = 'G', .mult = 1 << 30 }, |
| 634 | { .tag = 0 }, |
| 635 | }; |
| 636 | unsigned long val; |
| 637 | |
| 638 | if (unset) { |
| 639 | *s = 0; |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | val = parse_tag_value(str, tags_size); |
| 644 | if (val != (unsigned long) -1) { |
| 645 | if (val < 1024) { |
| 646 | pr_err("buffer size too small, must larger than 1KB."); |
| 647 | return -1; |
| 648 | } |
| 649 | *s = val; |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | return -1; |
| 654 | } |
| 655 | |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 656 | static int parse_func_tracer_opts(const struct option *opt, |
| 657 | const char *str, int unset) |
| 658 | { |
| 659 | int ret; |
| 660 | struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; |
| 661 | struct sublevel_option func_tracer_opts[] = { |
| 662 | { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace }, |
| 663 | { .name = NULL, } |
| 664 | }; |
| 665 | |
| 666 | if (unset) |
| 667 | return 0; |
| 668 | |
| 669 | ret = perf_parse_sublevel_options(str, func_tracer_opts); |
| 670 | if (ret) |
| 671 | return ret; |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 676 | static int parse_graph_tracer_opts(const struct option *opt, |
| 677 | const char *str, int unset) |
| 678 | { |
| 679 | int ret; |
| 680 | struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value; |
| 681 | struct sublevel_option graph_tracer_opts[] = { |
| 682 | { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time }, |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame^] | 683 | { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs }, |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 684 | { .name = NULL, } |
| 685 | }; |
| 686 | |
| 687 | if (unset) |
| 688 | return 0; |
| 689 | |
| 690 | ret = perf_parse_sublevel_options(str, graph_tracer_opts); |
| 691 | if (ret) |
| 692 | return ret; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 697 | static void select_tracer(struct perf_ftrace *ftrace) |
| 698 | { |
| 699 | bool graph = !list_empty(&ftrace->graph_funcs) || |
| 700 | !list_empty(&ftrace->nograph_funcs); |
| 701 | bool func = !list_empty(&ftrace->filters) || |
| 702 | !list_empty(&ftrace->notrace); |
| 703 | |
| 704 | /* The function_graph has priority over function tracer. */ |
| 705 | if (graph) |
| 706 | ftrace->tracer = "function_graph"; |
| 707 | else if (func) |
| 708 | ftrace->tracer = "function"; |
| 709 | /* Otherwise, the default tracer is used. */ |
| 710 | |
| 711 | pr_debug("%s tracer is used\n", ftrace->tracer); |
| 712 | } |
| 713 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 714 | int cmd_ftrace(int argc, const char **argv) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 715 | { |
| 716 | int ret; |
| 717 | struct perf_ftrace ftrace = { |
Taeung Song | bf062bd | 2017-01-26 18:35:37 +0900 | [diff] [blame] | 718 | .tracer = DEFAULT_TRACER, |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 719 | .target = { .uid = UINT_MAX, }, |
| 720 | }; |
| 721 | const char * const ftrace_usage[] = { |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 722 | "perf ftrace [<options>] [<command>]", |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 723 | "perf ftrace [<options>] -- <command> [<options>]", |
| 724 | NULL |
| 725 | }; |
| 726 | const struct option ftrace_options[] = { |
| 727 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", |
Arnaldo Carvalho de Melo | ec34787 | 2017-01-18 21:49:14 -0300 | [diff] [blame] | 728 | "tracer to use: function_graph(default) or function"), |
Changbin Du | d6d81bf | 2020-08-08 10:31:25 +0800 | [diff] [blame] | 729 | OPT_BOOLEAN('F', "funcs", &ftrace.list_avail_functions, |
| 730 | "Show available functions to filter"), |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 731 | OPT_STRING('p', "pid", &ftrace.target.pid, "pid", |
| 732 | "trace on existing process id"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 733 | OPT_INCR('v', "verbose", &verbose, |
| 734 | "be more verbose"), |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 735 | OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide, |
| 736 | "system-wide collection from all CPUs"), |
| 737 | OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu", |
| 738 | "list of cpus to monitor"), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 739 | OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 740 | "trace given functions using function tracer", |
| 741 | parse_filter_func), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 742 | OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func", |
| 743 | "do not trace given functions", parse_filter_func), |
Changbin Du | b1d84af | 2020-08-08 10:31:31 +0800 | [diff] [blame] | 744 | OPT_CALLBACK(0, "func-opts", &ftrace, "options", |
| 745 | "function tracer options, available options: call-graph", |
| 746 | parse_func_tracer_opts), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 747 | OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func", |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 748 | "trace given functions using function_graph tracer", |
| 749 | parse_filter_func), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 750 | OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func", |
| 751 | "Set nograph filter on given functions", parse_filter_func), |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 752 | OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth, |
| 753 | "Max depth for function graph tracer"), |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 754 | OPT_CALLBACK(0, "graph-opts", &ftrace, "options", |
Changbin Du | d1bcf17 | 2020-08-08 10:31:33 +0800 | [diff] [blame^] | 755 | "graph tracer options, available options: nosleep-time,noirqs", |
Changbin Du | 38988f2 | 2020-08-08 10:31:32 +0800 | [diff] [blame] | 756 | parse_graph_tracer_opts), |
Changbin Du | 846e193 | 2020-08-08 10:31:27 +0800 | [diff] [blame] | 757 | OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size", |
| 758 | "size of per cpu buffer", parse_buffer_size), |
Changbin Du | 5b347472 | 2020-08-08 10:31:29 +0800 | [diff] [blame] | 759 | OPT_BOOLEAN(0, "inherit", &ftrace.inherit, |
| 760 | "trace children processes"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 761 | OPT_END() |
| 762 | }; |
| 763 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 764 | INIT_LIST_HEAD(&ftrace.filters); |
| 765 | INIT_LIST_HEAD(&ftrace.notrace); |
| 766 | INIT_LIST_HEAD(&ftrace.graph_funcs); |
| 767 | INIT_LIST_HEAD(&ftrace.nograph_funcs); |
| 768 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 769 | ret = perf_config(perf_ftrace_config, &ftrace); |
| 770 | if (ret < 0) |
| 771 | return -1; |
| 772 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 773 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
| 774 | PARSE_OPT_STOP_AT_NON_OPTION); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 775 | if (!argc && target__none(&ftrace.target)) |
Changbin Du | 452b0d1 | 2020-05-10 23:06:10 +0800 | [diff] [blame] | 776 | ftrace.target.system_wide = true; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 777 | |
Changbin Du | eb6d31a | 2020-08-08 10:31:24 +0800 | [diff] [blame] | 778 | select_tracer(&ftrace); |
| 779 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 780 | ret = target__validate(&ftrace.target); |
| 781 | if (ret) { |
| 782 | char errbuf[512]; |
| 783 | |
| 784 | target__strerror(&ftrace.target, ret, errbuf, 512); |
| 785 | pr_err("%s\n", errbuf); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 786 | goto out_delete_filters; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 787 | } |
| 788 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 789 | ftrace.evlist = evlist__new(); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 790 | if (ftrace.evlist == NULL) { |
| 791 | ret = -ENOMEM; |
| 792 | goto out_delete_filters; |
| 793 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 794 | |
| 795 | ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target); |
| 796 | if (ret < 0) |
| 797 | goto out_delete_evlist; |
| 798 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 799 | ret = __cmd_ftrace(&ftrace, argc, argv); |
| 800 | |
| 801 | out_delete_evlist: |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 802 | evlist__delete(ftrace.evlist); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 803 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 804 | out_delete_filters: |
| 805 | delete_filter_func(&ftrace.filters); |
| 806 | delete_filter_func(&ftrace.notrace); |
| 807 | delete_filter_func(&ftrace.graph_funcs); |
| 808 | delete_filter_func(&ftrace.nograph_funcs); |
| 809 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 810 | return ret; |
| 811 | } |