Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 1 | /* |
| 2 | * builtin-ftrace.c |
| 3 | * |
| 4 | * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org> |
| 5 | * |
| 6 | * Released under the GPL v2. |
| 7 | */ |
| 8 | |
| 9 | #include "builtin.h" |
| 10 | #include "perf.h" |
| 11 | |
| 12 | #include <unistd.h> |
| 13 | #include <signal.h> |
| 14 | |
| 15 | #include "debug.h" |
| 16 | #include <subcmd/parse-options.h> |
| 17 | #include "evlist.h" |
| 18 | #include "target.h" |
| 19 | #include "thread_map.h" |
| 20 | |
| 21 | |
| 22 | #define DEFAULT_TRACER "function_graph" |
| 23 | |
| 24 | struct perf_ftrace { |
| 25 | struct perf_evlist *evlist; |
| 26 | struct target target; |
| 27 | const char *tracer; |
| 28 | }; |
| 29 | |
| 30 | static bool done; |
| 31 | |
| 32 | static void sig_handler(int sig __maybe_unused) |
| 33 | { |
| 34 | done = true; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since |
| 39 | * we asked by setting its exec_error to the function below, |
| 40 | * ftrace__workload_exec_failed_signal. |
| 41 | * |
| 42 | * XXX We need to handle this more appropriately, emitting an error, etc. |
| 43 | */ |
| 44 | static void ftrace__workload_exec_failed_signal(int signo __maybe_unused, |
| 45 | siginfo_t *info __maybe_unused, |
| 46 | void *ucontext __maybe_unused) |
| 47 | { |
| 48 | /* workload_exec_errno = info->si_value.sival_int; */ |
| 49 | done = true; |
| 50 | } |
| 51 | |
| 52 | static int write_tracing_file(const char *name, const char *val) |
| 53 | { |
| 54 | char *file; |
| 55 | int fd, ret = -1; |
| 56 | ssize_t size = strlen(val); |
| 57 | |
| 58 | file = get_tracing_file(name); |
| 59 | if (!file) { |
| 60 | pr_debug("cannot get tracing file: %s\n", name); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | fd = open(file, O_WRONLY); |
| 65 | if (fd < 0) { |
| 66 | pr_debug("cannot open tracing file: %s\n", name); |
| 67 | goto out; |
| 68 | } |
| 69 | |
| 70 | if (write(fd, val, size) == size) |
| 71 | ret = 0; |
| 72 | else |
| 73 | pr_debug("write '%s' to tracing/%s failed\n", val, name); |
| 74 | |
| 75 | close(fd); |
| 76 | out: |
| 77 | put_tracing_file(file); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused) |
| 82 | { |
| 83 | if (write_tracing_file("tracing_on", "0") < 0) |
| 84 | return -1; |
| 85 | |
| 86 | if (write_tracing_file("current_tracer", "nop") < 0) |
| 87 | return -1; |
| 88 | |
| 89 | if (write_tracing_file("set_ftrace_pid", " ") < 0) |
| 90 | return -1; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv) |
| 96 | { |
| 97 | char *trace_file; |
| 98 | int trace_fd; |
| 99 | char *trace_pid; |
| 100 | char buf[4096]; |
| 101 | struct pollfd pollfd = { |
| 102 | .events = POLLIN, |
| 103 | }; |
| 104 | |
| 105 | if (geteuid() != 0) { |
| 106 | pr_err("ftrace only works for root!\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | if (argc < 1) |
| 111 | return -1; |
| 112 | |
| 113 | signal(SIGINT, sig_handler); |
| 114 | signal(SIGUSR1, sig_handler); |
| 115 | signal(SIGCHLD, sig_handler); |
| 116 | |
| 117 | reset_tracing_files(ftrace); |
| 118 | |
| 119 | /* reset ftrace buffer */ |
| 120 | if (write_tracing_file("trace", "0") < 0) |
| 121 | goto out; |
| 122 | |
| 123 | if (perf_evlist__prepare_workload(ftrace->evlist, &ftrace->target, |
| 124 | argv, false, ftrace__workload_exec_failed_signal) < 0) |
| 125 | goto out; |
| 126 | |
| 127 | if (write_tracing_file("current_tracer", ftrace->tracer) < 0) { |
| 128 | pr_err("failed to set current_tracer to %s\n", ftrace->tracer); |
| 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | if (asprintf(&trace_pid, "%d", thread_map__pid(ftrace->evlist->threads, 0)) < 0) { |
| 133 | pr_err("failed to allocate pid string\n"); |
| 134 | goto out; |
| 135 | } |
| 136 | |
| 137 | if (write_tracing_file("set_ftrace_pid", trace_pid) < 0) { |
| 138 | pr_err("failed to set pid: %s\n", trace_pid); |
| 139 | goto out_free_pid; |
| 140 | } |
| 141 | |
| 142 | trace_file = get_tracing_file("trace_pipe"); |
| 143 | if (!trace_file) { |
| 144 | pr_err("failed to open trace_pipe\n"); |
| 145 | goto out_free_pid; |
| 146 | } |
| 147 | |
| 148 | trace_fd = open(trace_file, O_RDONLY); |
| 149 | |
| 150 | put_tracing_file(trace_file); |
| 151 | |
| 152 | if (trace_fd < 0) { |
| 153 | pr_err("failed to open trace_pipe\n"); |
| 154 | goto out_free_pid; |
| 155 | } |
| 156 | |
| 157 | fcntl(trace_fd, F_SETFL, O_NONBLOCK); |
| 158 | pollfd.fd = trace_fd; |
| 159 | |
| 160 | if (write_tracing_file("tracing_on", "1") < 0) { |
| 161 | pr_err("can't enable tracing\n"); |
| 162 | goto out_close_fd; |
| 163 | } |
| 164 | |
| 165 | perf_evlist__start_workload(ftrace->evlist); |
| 166 | |
| 167 | while (!done) { |
| 168 | if (poll(&pollfd, 1, -1) < 0) |
| 169 | break; |
| 170 | |
| 171 | if (pollfd.revents & POLLIN) { |
| 172 | int n = read(trace_fd, buf, sizeof(buf)); |
| 173 | if (n < 0) |
| 174 | break; |
| 175 | if (fwrite(buf, n, 1, stdout) != 1) |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | write_tracing_file("tracing_on", "0"); |
| 181 | |
| 182 | /* read remaining buffer contents */ |
| 183 | while (true) { |
| 184 | int n = read(trace_fd, buf, sizeof(buf)); |
| 185 | if (n <= 0) |
| 186 | break; |
| 187 | if (fwrite(buf, n, 1, stdout) != 1) |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | out_close_fd: |
| 192 | close(trace_fd); |
| 193 | out_free_pid: |
| 194 | free(trace_pid); |
| 195 | out: |
| 196 | reset_tracing_files(ftrace); |
| 197 | |
| 198 | return done ? 0 : -1; |
| 199 | } |
| 200 | |
| 201 | int cmd_ftrace(int argc, const char **argv, const char *prefix __maybe_unused) |
| 202 | { |
| 203 | int ret; |
| 204 | struct perf_ftrace ftrace = { |
Taeung Song | bf062bd | 2017-01-26 18:35:37 +0900 | [diff] [blame^] | 205 | .tracer = DEFAULT_TRACER, |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 206 | .target = { .uid = UINT_MAX, }, |
| 207 | }; |
| 208 | const char * const ftrace_usage[] = { |
| 209 | "perf ftrace [<options>] <command>", |
| 210 | "perf ftrace [<options>] -- <command> [<options>]", |
| 211 | NULL |
| 212 | }; |
| 213 | const struct option ftrace_options[] = { |
| 214 | OPT_STRING('t', "tracer", &ftrace.tracer, "tracer", |
Arnaldo Carvalho de Melo | ec34787 | 2017-01-18 21:49:14 -0300 | [diff] [blame] | 215 | "tracer to use: function_graph(default) or function"), |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 216 | OPT_INCR('v', "verbose", &verbose, |
| 217 | "be more verbose"), |
| 218 | OPT_END() |
| 219 | }; |
| 220 | |
| 221 | argc = parse_options(argc, argv, ftrace_options, ftrace_usage, |
| 222 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 223 | if (!argc) |
| 224 | usage_with_options(ftrace_usage, ftrace_options); |
| 225 | |
| 226 | ftrace.evlist = perf_evlist__new(); |
| 227 | if (ftrace.evlist == NULL) |
| 228 | return -ENOMEM; |
| 229 | |
| 230 | ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target); |
| 231 | if (ret < 0) |
| 232 | goto out_delete_evlist; |
| 233 | |
Namhyung Kim | d01f4e8 | 2013-03-07 21:45:20 +0900 | [diff] [blame] | 234 | ret = __cmd_ftrace(&ftrace, argc, argv); |
| 235 | |
| 236 | out_delete_evlist: |
| 237 | perf_evlist__delete(ftrace.evlist); |
| 238 | |
| 239 | return ret; |
| 240 | } |