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" |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 29 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 30 | #define DEFAULT_TRACER "function_graph" |
| 31 | |
| 32 | struct perf_ftrace { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 33 | struct evlist *evlist; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 34 | struct target target; |
| 35 | const char *tracer; |
| 36 | struct list_head filters; |
| 37 | struct list_head notrace; |
| 38 | struct list_head graph_funcs; |
| 39 | struct list_head nograph_funcs; |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 40 | int graph_depth; |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct filter_entry { |
| 44 | struct list_head list; |
| 45 | char name[]; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static bool done; |
| 49 | |
| 50 | static void sig_handler(int sig __maybe_unused) |
| 51 | { |
| 52 | done = true; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since |
| 57 | * we asked by setting its exec_error to the function below, |
| 58 | * ftrace__workload_exec_failed_signal. |
| 59 | * |
| 60 | * XXX We need to handle this more appropriately, emitting an error, etc. |
| 61 | */ |
| 62 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, |
| 63 | siginfo_t *info __maybe_unused, |
| 64 | void *ucontext __maybe_unused) |
| 65 | { |
| 66 | /* workload_exec_errno = info->si_value.sival_int; */ |
| 67 | done = true; |
| 68 | } |
| 69 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | char *file; |
| 73 | int fd, ret = -1; |
| 74 | ssize_t size = strlen(val); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 75 | int flags = O_WRONLY; |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 76 | char errbuf[512]; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 77 | char *val_copy; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 78 | |
| 79 | file = get_tracing_file(name); |
| 80 | if (!file) { |
| 81 | pr_debug("cannot get tracing file: %s\n", name); |
| 82 | return -1; |
| 83 | } |
| 84 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 85 | if (append) |
| 86 | flags |= O_APPEND; |
| 87 | else |
| 88 | flags |= O_TRUNC; |
| 89 | |
| 90 | fd = open(file, flags); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 91 | if (fd < 0) { |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 92 | pr_debug("cannot open tracing file: %s: %s\n", |
| 93 | name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 94 | goto out; |
| 95 | } |
| 96 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 97 | /* |
| 98 | * Copy the original value and append a '\n'. Without this, |
| 99 | * the kernel can hide possible errors. |
| 100 | */ |
| 101 | val_copy = strdup(val); |
| 102 | if (!val_copy) |
| 103 | goto out_close; |
| 104 | val_copy[size] = '\n'; |
| 105 | |
| 106 | if (write(fd, val_copy, size + 1) == size + 1) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 107 | ret = 0; |
| 108 | else |
Namhyung Kim | e7bd9ba | 2017-06-18 23:22:59 +0900 | [diff] [blame] | 109 | pr_debug("write '%s' to tracing/%s failed: %s\n", |
| 110 | val, name, str_error_r(errno, errbuf, sizeof(errbuf))); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 111 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 112 | free(val_copy); |
| 113 | out_close: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 114 | close(fd); |
| 115 | out: |
| 116 | put_tracing_file(file); |
| 117 | return ret; |
| 118 | } |
| 119 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 120 | static int write_tracing_file(const char *name, const char *val) |
| 121 | { |
| 122 | return __write_tracing_file(name, val, false); |
| 123 | } |
| 124 | |
| 125 | static int append_tracing_file(const char *name, const char *val) |
| 126 | { |
| 127 | return __write_tracing_file(name, val, true); |
| 128 | } |
| 129 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 130 | static int reset_tracing_cpu(void); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 131 | static void reset_tracing_filters(void); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 132 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 133 | static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) |
| 134 | { |
| 135 | if (write_tracing_file("tracing_on", "0") < 0) |
| 136 | return -1; |
| 137 | |
| 138 | if (write_tracing_file("current_tracer", "nop") < 0) |
| 139 | return -1; |
| 140 | |
| 141 | if (write_tracing_file("set_ftrace_pid", " ") < 0) |
| 142 | return -1; |
| 143 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 144 | if (reset_tracing_cpu() < 0) |
| 145 | return -1; |
| 146 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 147 | if (write_tracing_file("max_graph_depth", "0") < 0) |
| 148 | return -1; |
| 149 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 150 | reset_tracing_filters(); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 154 | static int set_tracing_pid(struct perf_ftrace *ftrace) |
| 155 | { |
| 156 | int i; |
| 157 | char buf[16]; |
| 158 | |
| 159 | if (target__has_cpu(&ftrace->target)) |
| 160 | return 0; |
| 161 | |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 162 | 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] | 163 | scnprintf(buf, sizeof(buf), "%d", |
Jiri Olsa | 03617c2 | 2019-07-21 13:24:42 +0200 | [diff] [blame] | 164 | ftrace->evlist->core.threads->map[i]); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 165 | if (append_tracing_file("set_ftrace_pid", buf) < 0) |
| 166 | return -1; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 171 | static int set_tracing_cpumask(struct perf_cpu_map *cpumap) |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 172 | { |
| 173 | char *cpumask; |
| 174 | size_t mask_size; |
| 175 | int ret; |
| 176 | int last_cpu; |
| 177 | |
| 178 | last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1); |
He Zhe | cf30ae7 | 2019-08-02 16:29:51 +0800 | [diff] [blame] | 179 | mask_size = last_cpu / 4 + 2; /* one more byte for EOS */ |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 180 | mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */ |
| 181 | |
| 182 | cpumask = malloc(mask_size); |
| 183 | if (cpumask == NULL) { |
| 184 | pr_debug("failed to allocate cpu mask\n"); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | cpu_map__snprint_mask(cpumap, cpumask, mask_size); |
| 189 | |
| 190 | ret = write_tracing_file("tracing_cpumask", cpumask); |
| 191 | |
| 192 | free(cpumask); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static int set_tracing_cpu(struct perf_ftrace *ftrace) |
| 197 | { |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 198 | struct perf_cpu_map *cpumap = ftrace->evlist->core.cpus; |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 199 | |
| 200 | if (!target__has_cpu(&ftrace->target)) |
| 201 | return 0; |
| 202 | |
| 203 | return set_tracing_cpumask(cpumap); |
| 204 | } |
| 205 | |
| 206 | static int reset_tracing_cpu(void) |
| 207 | { |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 208 | struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 209 | int ret; |
| 210 | |
| 211 | ret = set_tracing_cpumask(cpumap); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 212 | perf_cpu_map__put(cpumap); |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 216 | static int __set_tracing_filter(const char *filter_file, struct list_head *funcs) |
| 217 | { |
| 218 | struct filter_entry *pos; |
| 219 | |
| 220 | list_for_each_entry(pos, funcs, list) { |
| 221 | if (append_tracing_file(filter_file, pos->name) < 0) |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int set_tracing_filters(struct perf_ftrace *ftrace) |
| 229 | { |
| 230 | int ret; |
| 231 | |
| 232 | ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters); |
| 233 | if (ret < 0) |
| 234 | return ret; |
| 235 | |
| 236 | ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace); |
| 237 | if (ret < 0) |
| 238 | return ret; |
| 239 | |
| 240 | ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs); |
| 241 | if (ret < 0) |
| 242 | return ret; |
| 243 | |
| 244 | /* old kernels do not have this filter */ |
| 245 | __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static void reset_tracing_filters(void) |
| 251 | { |
| 252 | write_tracing_file("set_ftrace_filter", " "); |
| 253 | write_tracing_file("set_ftrace_notrace", " "); |
| 254 | write_tracing_file("set_graph_function", " "); |
| 255 | write_tracing_file("set_graph_notrace", " "); |
| 256 | } |
| 257 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 258 | static int set_tracing_depth(struct perf_ftrace *ftrace) |
| 259 | { |
| 260 | char buf[16]; |
| 261 | |
| 262 | if (ftrace->graph_depth == 0) |
| 263 | return 0; |
| 264 | |
| 265 | if (ftrace->graph_depth < 0) { |
| 266 | pr_err("invalid graph depth: %d\n", ftrace->graph_depth); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth); |
| 271 | |
| 272 | if (write_tracing_file("max_graph_depth", buf) < 0) |
| 273 | return -1; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 278 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) |
| 279 | { |
| 280 | char *trace_file; |
| 281 | int trace_fd; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 282 | char buf[4096]; |
| 283 | struct pollfd pollfd = { |
| 284 | .events = POLLIN, |
| 285 | }; |
| 286 | |
Igor Lubashev | c766f3d | 2019-08-07 10:44:17 -0400 | [diff] [blame] | 287 | if (!perf_cap__capable(CAP_SYS_ADMIN)) { |
Arnaldo Carvalho de Melo | 73e5de7 | 2019-08-12 17:27:11 -0300 | [diff] [blame] | 288 | pr_err("ftrace only works for %s!\n", |
| 289 | #ifdef HAVE_LIBCAP_SUPPORT |
| 290 | "users with the SYS_ADMIN capability" |
| 291 | #else |
| 292 | "root" |
| 293 | #endif |
| 294 | ); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 295 | return -1; |
| 296 | } |
| 297 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 298 | signal(SIGINT, sig_handler); |
| 299 | signal(SIGUSR1, sig_handler); |
| 300 | signal(SIGCHLD, sig_handler); |
Namhyung Kim | 5833596 | 2017-02-24 10:12:51 +0900 | [diff] [blame] | 301 | signal(SIGPIPE, sig_handler); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 302 | |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 303 | if (reset_tracing_files(ftrace) < 0) { |
| 304 | pr_err("failed to reset ftrace\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 305 | goto out; |
Changbin Du | 63cd02d | 2018-02-19 10:33:29 +0800 | [diff] [blame] | 306 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 307 | |
| 308 | /* reset ftrace buffer */ |
| 309 | if (write_tracing_file("trace", "0") < 0) |
| 310 | goto out; |
| 311 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 312 | if (argc && perf_evlist__prepare_workload(ftrace->evlist, |
| 313 | &ftrace->target, argv, false, |
| 314 | ftrace__workload_exec_failed_signal) < 0) { |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 315 | goto out; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (set_tracing_pid(ftrace) < 0) { |
| 319 | pr_err("failed to set ftrace pid\n"); |
| 320 | goto out_reset; |
| 321 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 322 | |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 323 | if (set_tracing_cpu(ftrace) < 0) { |
| 324 | pr_err("failed to set tracing cpumask\n"); |
| 325 | goto out_reset; |
| 326 | } |
| 327 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 328 | if (set_tracing_filters(ftrace) < 0) { |
| 329 | pr_err("failed to set tracing filters\n"); |
| 330 | goto out_reset; |
| 331 | } |
| 332 | |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 333 | if (set_tracing_depth(ftrace) < 0) { |
| 334 | pr_err("failed to set graph depth\n"); |
| 335 | goto out_reset; |
| 336 | } |
| 337 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 338 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
| 339 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 340 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 341 | } |
| 342 | |
Namhyung Kim | 29681bc | 2017-06-18 23:23:00 +0900 | [diff] [blame] | 343 | setup_pager(); |
| 344 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 345 | trace_file = get_tracing_file("trace_pipe"); |
| 346 | if (!trace_file) { |
| 347 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 348 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | trace_fd = open(trace_file, O_RDONLY); |
| 352 | |
| 353 | put_tracing_file(trace_file); |
| 354 | |
| 355 | if (trace_fd < 0) { |
| 356 | pr_err("failed to open trace_pipe\n"); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 357 | goto out_reset; |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); |
| 361 | pollfd.fd = trace_fd; |
| 362 | |
| 363 | if (write_tracing_file("tracing_on", "1") < 0) { |
| 364 | pr_err("can't enable tracing\n"); |
| 365 | goto out_close_fd; |
| 366 | } |
| 367 | |
| 368 | perf_evlist__start_workload(ftrace->evlist); |
| 369 | |
| 370 | while (!done) { |
| 371 | if (poll(&pollfd, 1, -1) < 0) |
| 372 | break; |
| 373 | |
| 374 | if (pollfd.revents & POLLIN) { |
| 375 | int n = read(trace_fd, buf, sizeof(buf)); |
| 376 | if (n < 0) |
| 377 | break; |
| 378 | if (fwrite(buf, n, 1, stdout) != 1) |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | write_tracing_file("tracing_on", "0"); |
| 384 | |
| 385 | /* read remaining buffer contents */ |
| 386 | while (true) { |
| 387 | int n = read(trace_fd, buf, sizeof(buf)); |
| 388 | if (n <= 0) |
| 389 | break; |
| 390 | if (fwrite(buf, n, 1, stdout) != 1) |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | out_close_fd: |
| 395 | close(trace_fd); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 396 | out_reset: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 397 | reset_tracing_files(ftrace); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 398 | out: |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 399 | return done ? 0 : -1; |
| 400 | } |
| 401 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 402 | static int perf_ftrace_config(const char *var, const char *value, void *cb) |
| 403 | { |
| 404 | struct perf_ftrace *ftrace = cb; |
| 405 | |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 406 | if (!strstarts(var, "ftrace.")) |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 407 | return 0; |
| 408 | |
| 409 | if (strcmp(var, "ftrace.tracer")) |
| 410 | return -1; |
| 411 | |
| 412 | if (!strcmp(value, "function_graph") || |
| 413 | !strcmp(value, "function")) { |
| 414 | ftrace->tracer = value; |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | pr_err("Please select \"function_graph\" (default) or \"function\"\n"); |
| 419 | return -1; |
| 420 | } |
| 421 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 422 | static int parse_filter_func(const struct option *opt, const char *str, |
| 423 | int unset __maybe_unused) |
| 424 | { |
| 425 | struct list_head *head = opt->value; |
| 426 | struct filter_entry *entry; |
| 427 | |
| 428 | entry = malloc(sizeof(*entry) + strlen(str) + 1); |
| 429 | if (entry == NULL) |
| 430 | return -ENOMEM; |
| 431 | |
| 432 | strcpy(entry->name, str); |
| 433 | list_add_tail(&entry->list, head); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static void delete_filter_func(struct list_head *head) |
| 439 | { |
| 440 | struct filter_entry *pos, *tmp; |
| 441 | |
| 442 | list_for_each_entry_safe(pos, tmp, head, list) { |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 443 | list_del_init(&pos->list); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 444 | free(pos); |
| 445 | } |
| 446 | } |
| 447 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 448 | int cmd_ftrace(int argc, const char **argv) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 449 | { |
| 450 | int ret; |
| 451 | struct perf_ftrace ftrace = { |
Taeung Song | bf062bd | 2017-01-26 18:35:37 +0900 | [diff] [blame] | 452 | .tracer = DEFAULT_TRACER, |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 453 | .target = { .uid = UINT_MAX, }, |
| 454 | }; |
| 455 | const char * const ftrace_usage[] = { |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 456 | "perf ftrace [<options>] [<command>]", |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 457 | "perf ftrace [<options>] -- <command> [<options>]", |
| 458 | NULL |
| 459 | }; |
| 460 | const struct option ftrace_options[] = { |
| 461 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", |
Arnaldo Carvalho de Melo | ec34787 | 2017-01-18 21:49:14 -0300 | [diff] [blame] | 462 | "tracer to use: function_graph(default) or function"), |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 463 | OPT_STRING('p', "pid", &ftrace.target.pid, "pid", |
| 464 | "trace on existing process id"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 465 | OPT_INCR('v', "verbose", &verbose, |
| 466 | "be more verbose"), |
Namhyung Kim | dc23103 | 2017-02-24 10:12:50 +0900 | [diff] [blame] | 467 | OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide, |
| 468 | "system-wide collection from all CPUs"), |
| 469 | OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu", |
| 470 | "list of cpus to monitor"), |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 471 | OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func", |
| 472 | "trace given functions only", parse_filter_func), |
| 473 | OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func", |
| 474 | "do not trace given functions", parse_filter_func), |
| 475 | OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func", |
| 476 | "Set graph filter on given functions", parse_filter_func), |
| 477 | OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func", |
| 478 | "Set nograph filter on given functions", parse_filter_func), |
Namhyung Kim | 1096c35 | 2017-06-18 23:23:02 +0900 | [diff] [blame] | 479 | OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth, |
| 480 | "Max depth for function graph tracer"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 481 | OPT_END() |
| 482 | }; |
| 483 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 484 | INIT_LIST_HEAD(&ftrace.filters); |
| 485 | INIT_LIST_HEAD(&ftrace.notrace); |
| 486 | INIT_LIST_HEAD(&ftrace.graph_funcs); |
| 487 | INIT_LIST_HEAD(&ftrace.nograph_funcs); |
| 488 | |
Taeung Song | b05d109 | 2017-01-31 20:38:29 +0900 | [diff] [blame] | 489 | ret = perf_config(perf_ftrace_config, &ftrace); |
| 490 | if (ret < 0) |
| 491 | return -1; |
| 492 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 493 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
| 494 | PARSE_OPT_STOP_AT_NON_OPTION); |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 495 | if (!argc && target__none(&ftrace.target)) |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 496 | usage_with_options(ftrace_usage, ftrace_options); |
| 497 | |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 498 | ret = target__validate(&ftrace.target); |
| 499 | if (ret) { |
| 500 | char errbuf[512]; |
| 501 | |
| 502 | target__strerror(&ftrace.target, ret, errbuf, 512); |
| 503 | pr_err("%s\n", errbuf); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 504 | goto out_delete_filters; |
Namhyung Kim | a9af6be | 2017-02-24 10:12:48 +0900 | [diff] [blame] | 505 | } |
| 506 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 507 | ftrace.evlist = evlist__new(); |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 508 | if (ftrace.evlist == NULL) { |
| 509 | ret = -ENOMEM; |
| 510 | goto out_delete_filters; |
| 511 | } |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 512 | |
| 513 | ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target); |
| 514 | if (ret < 0) |
| 515 | goto out_delete_evlist; |
| 516 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 517 | ret = __cmd_ftrace(&ftrace, argc, argv); |
| 518 | |
| 519 | out_delete_evlist: |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 520 | evlist__delete(ftrace.evlist); |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 521 | |
Namhyung Kim | 78b83e8 | 2017-06-18 23:23:01 +0900 | [diff] [blame] | 522 | out_delete_filters: |
| 523 | delete_filter_func(&ftrace.filters); |
| 524 | delete_filter_func(&ftrace.notrace); |
| 525 | delete_filter_func(&ftrace.graph_funcs); |
| 526 | delete_filter_func(&ftrace.nograph_funcs); |
| 527 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 528 | return ret; |
| 529 | } |