]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-ftrace.c
perf tools: Remove string.h, unistd.h and sys/stat.h from util.h
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-ftrace.c
CommitLineData
d01f4e8d
NK
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
a43783ae 12#include <errno.h>
d01f4e8d
NK
13#include <unistd.h>
14#include <signal.h>
a9af6be5 15#include <fcntl.h>
d01f4e8d
NK
16
17#include "debug.h"
18#include <subcmd/parse-options.h>
20a9ed28 19#include <api/fs/tracing_path.h>
d01f4e8d
NK
20#include "evlist.h"
21#include "target.h"
dc231032 22#include "cpumap.h"
d01f4e8d 23#include "thread_map.h"
b05d1093 24#include "util/config.h"
d01f4e8d
NK
25
26
27#define DEFAULT_TRACER "function_graph"
28
29struct perf_ftrace {
30 struct perf_evlist *evlist;
31 struct target target;
32 const char *tracer;
33};
34
35static bool done;
36
37static void sig_handler(int sig __maybe_unused)
38{
39 done = true;
40}
41
42/*
43 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
44 * we asked by setting its exec_error to the function below,
45 * ftrace__workload_exec_failed_signal.
46 *
47 * XXX We need to handle this more appropriately, emitting an error, etc.
48 */
49static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
50 siginfo_t *info __maybe_unused,
51 void *ucontext __maybe_unused)
52{
53 /* workload_exec_errno = info->si_value.sival_int; */
54 done = true;
55}
56
a9af6be5 57static int __write_tracing_file(const char *name, const char *val, bool append)
d01f4e8d
NK
58{
59 char *file;
60 int fd, ret = -1;
61 ssize_t size = strlen(val);
a9af6be5 62 int flags = O_WRONLY;
d01f4e8d
NK
63
64 file = get_tracing_file(name);
65 if (!file) {
66 pr_debug("cannot get tracing file: %s\n", name);
67 return -1;
68 }
69
a9af6be5
NK
70 if (append)
71 flags |= O_APPEND;
72 else
73 flags |= O_TRUNC;
74
75 fd = open(file, flags);
d01f4e8d
NK
76 if (fd < 0) {
77 pr_debug("cannot open tracing file: %s\n", name);
78 goto out;
79 }
80
81 if (write(fd, val, size) == size)
82 ret = 0;
83 else
84 pr_debug("write '%s' to tracing/%s failed\n", val, name);
85
86 close(fd);
87out:
88 put_tracing_file(file);
89 return ret;
90}
91
a9af6be5
NK
92static int write_tracing_file(const char *name, const char *val)
93{
94 return __write_tracing_file(name, val, false);
95}
96
97static int append_tracing_file(const char *name, const char *val)
98{
99 return __write_tracing_file(name, val, true);
100}
101
dc231032
NK
102static int reset_tracing_cpu(void);
103
d01f4e8d
NK
104static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
105{
106 if (write_tracing_file("tracing_on", "0") < 0)
107 return -1;
108
109 if (write_tracing_file("current_tracer", "nop") < 0)
110 return -1;
111
112 if (write_tracing_file("set_ftrace_pid", " ") < 0)
113 return -1;
114
dc231032
NK
115 if (reset_tracing_cpu() < 0)
116 return -1;
117
d01f4e8d
NK
118 return 0;
119}
120
a9af6be5
NK
121static int set_tracing_pid(struct perf_ftrace *ftrace)
122{
123 int i;
124 char buf[16];
125
126 if (target__has_cpu(&ftrace->target))
127 return 0;
128
129 for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
130 scnprintf(buf, sizeof(buf), "%d",
131 ftrace->evlist->threads->map[i]);
132 if (append_tracing_file("set_ftrace_pid", buf) < 0)
133 return -1;
134 }
135 return 0;
136}
137
dc231032
NK
138static int set_tracing_cpumask(struct cpu_map *cpumap)
139{
140 char *cpumask;
141 size_t mask_size;
142 int ret;
143 int last_cpu;
144
145 last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
146 mask_size = (last_cpu + 3) / 4 + 1;
147 mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
148
149 cpumask = malloc(mask_size);
150 if (cpumask == NULL) {
151 pr_debug("failed to allocate cpu mask\n");
152 return -1;
153 }
154
155 cpu_map__snprint_mask(cpumap, cpumask, mask_size);
156
157 ret = write_tracing_file("tracing_cpumask", cpumask);
158
159 free(cpumask);
160 return ret;
161}
162
163static int set_tracing_cpu(struct perf_ftrace *ftrace)
164{
165 struct cpu_map *cpumap = ftrace->evlist->cpus;
166
167 if (!target__has_cpu(&ftrace->target))
168 return 0;
169
170 return set_tracing_cpumask(cpumap);
171}
172
173static int reset_tracing_cpu(void)
174{
175 struct cpu_map *cpumap = cpu_map__new(NULL);
176 int ret;
177
178 ret = set_tracing_cpumask(cpumap);
179 cpu_map__put(cpumap);
180 return ret;
181}
182
d01f4e8d
NK
183static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
184{
185 char *trace_file;
186 int trace_fd;
d01f4e8d
NK
187 char buf[4096];
188 struct pollfd pollfd = {
189 .events = POLLIN,
190 };
191
192 if (geteuid() != 0) {
193 pr_err("ftrace only works for root!\n");
194 return -1;
195 }
196
d01f4e8d
NK
197 signal(SIGINT, sig_handler);
198 signal(SIGUSR1, sig_handler);
199 signal(SIGCHLD, sig_handler);
58335964 200 signal(SIGPIPE, sig_handler);
d01f4e8d 201
a9af6be5
NK
202 if (reset_tracing_files(ftrace) < 0)
203 goto out;
d01f4e8d
NK
204
205 /* reset ftrace buffer */
206 if (write_tracing_file("trace", "0") < 0)
207 goto out;
208
a9af6be5
NK
209 if (argc && perf_evlist__prepare_workload(ftrace->evlist,
210 &ftrace->target, argv, false,
211 ftrace__workload_exec_failed_signal) < 0) {
d01f4e8d
NK
212 goto out;
213 }
214
a9af6be5
NK
215 if (set_tracing_pid(ftrace) < 0) {
216 pr_err("failed to set ftrace pid\n");
217 goto out_reset;
d01f4e8d
NK
218 }
219
dc231032
NK
220 if (set_tracing_cpu(ftrace) < 0) {
221 pr_err("failed to set tracing cpumask\n");
222 goto out_reset;
223 }
224
a9af6be5
NK
225 if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
226 pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
227 goto out_reset;
d01f4e8d
NK
228 }
229
230 trace_file = get_tracing_file("trace_pipe");
231 if (!trace_file) {
232 pr_err("failed to open trace_pipe\n");
a9af6be5 233 goto out_reset;
d01f4e8d
NK
234 }
235
236 trace_fd = open(trace_file, O_RDONLY);
237
238 put_tracing_file(trace_file);
239
240 if (trace_fd < 0) {
241 pr_err("failed to open trace_pipe\n");
a9af6be5 242 goto out_reset;
d01f4e8d
NK
243 }
244
245 fcntl(trace_fd, F_SETFL, O_NONBLOCK);
246 pollfd.fd = trace_fd;
247
248 if (write_tracing_file("tracing_on", "1") < 0) {
249 pr_err("can't enable tracing\n");
250 goto out_close_fd;
251 }
252
58335964
NK
253 setup_pager();
254
d01f4e8d
NK
255 perf_evlist__start_workload(ftrace->evlist);
256
257 while (!done) {
258 if (poll(&pollfd, 1, -1) < 0)
259 break;
260
261 if (pollfd.revents & POLLIN) {
262 int n = read(trace_fd, buf, sizeof(buf));
263 if (n < 0)
264 break;
265 if (fwrite(buf, n, 1, stdout) != 1)
266 break;
267 }
268 }
269
270 write_tracing_file("tracing_on", "0");
271
272 /* read remaining buffer contents */
273 while (true) {
274 int n = read(trace_fd, buf, sizeof(buf));
275 if (n <= 0)
276 break;
277 if (fwrite(buf, n, 1, stdout) != 1)
278 break;
279 }
280
281out_close_fd:
282 close(trace_fd);
a9af6be5 283out_reset:
d01f4e8d 284 reset_tracing_files(ftrace);
a9af6be5 285out:
d01f4e8d
NK
286 return done ? 0 : -1;
287}
288
b05d1093
TS
289static int perf_ftrace_config(const char *var, const char *value, void *cb)
290{
291 struct perf_ftrace *ftrace = cb;
292
293 if (prefixcmp(var, "ftrace."))
294 return 0;
295
296 if (strcmp(var, "ftrace.tracer"))
297 return -1;
298
299 if (!strcmp(value, "function_graph") ||
300 !strcmp(value, "function")) {
301 ftrace->tracer = value;
302 return 0;
303 }
304
305 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
306 return -1;
307}
308
b0ad8ea6 309int cmd_ftrace(int argc, const char **argv)
d01f4e8d
NK
310{
311 int ret;
312 struct perf_ftrace ftrace = {
bf062bd2 313 .tracer = DEFAULT_TRACER,
d01f4e8d
NK
314 .target = { .uid = UINT_MAX, },
315 };
316 const char * const ftrace_usage[] = {
a9af6be5 317 "perf ftrace [<options>] [<command>]",
d01f4e8d
NK
318 "perf ftrace [<options>] -- <command> [<options>]",
319 NULL
320 };
321 const struct option ftrace_options[] = {
322 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
ec347870 323 "tracer to use: function_graph(default) or function"),
a9af6be5
NK
324 OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
325 "trace on existing process id"),
d01f4e8d
NK
326 OPT_INCR('v', "verbose", &verbose,
327 "be more verbose"),
dc231032
NK
328 OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
329 "system-wide collection from all CPUs"),
330 OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
331 "list of cpus to monitor"),
d01f4e8d
NK
332 OPT_END()
333 };
334
b05d1093
TS
335 ret = perf_config(perf_ftrace_config, &ftrace);
336 if (ret < 0)
337 return -1;
338
d01f4e8d
NK
339 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
340 PARSE_OPT_STOP_AT_NON_OPTION);
a9af6be5 341 if (!argc && target__none(&ftrace.target))
d01f4e8d
NK
342 usage_with_options(ftrace_usage, ftrace_options);
343
a9af6be5
NK
344 ret = target__validate(&ftrace.target);
345 if (ret) {
346 char errbuf[512];
347
348 target__strerror(&ftrace.target, ret, errbuf, 512);
349 pr_err("%s\n", errbuf);
350 return -EINVAL;
351 }
352
d01f4e8d
NK
353 ftrace.evlist = perf_evlist__new();
354 if (ftrace.evlist == NULL)
355 return -ENOMEM;
356
357 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
358 if (ret < 0)
359 goto out_delete_evlist;
360
d01f4e8d
NK
361 ret = __cmd_ftrace(&ftrace, argc, argv);
362
363out_delete_evlist:
364 perf_evlist__delete(ftrace.evlist);
365
366 return ret;
367}