]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-ftrace.c
perf ftrace: Introduce new 'ftrace' tool
[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
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
24struct perf_ftrace {
25 struct perf_evlist *evlist;
26 struct target target;
27 const char *tracer;
28};
29
30static bool done;
31
32static 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 */
44static 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
52static 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);
76out:
77 put_tracing_file(file);
78 return ret;
79}
80
81static 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
95static 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
191out_close_fd:
192 close(trace_fd);
193out_free_pid:
194 free(trace_pid);
195out:
196 reset_tracing_files(ftrace);
197
198 return done ? 0 : -1;
199}
200
201int cmd_ftrace(int argc, const char **argv, const char *prefix __maybe_unused)
202{
203 int ret;
204 struct perf_ftrace ftrace = {
205 .target = { .uid = UINT_MAX, },
206 };
207 const char * const ftrace_usage[] = {
208 "perf ftrace [<options>] <command>",
209 "perf ftrace [<options>] -- <command> [<options>]",
210 NULL
211 };
212 const struct option ftrace_options[] = {
213 OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
214 "tracer to use: function_graph or function"),
215 OPT_INCR('v', "verbose", &verbose,
216 "be more verbose"),
217 OPT_END()
218 };
219
220 argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
221 PARSE_OPT_STOP_AT_NON_OPTION);
222 if (!argc)
223 usage_with_options(ftrace_usage, ftrace_options);
224
225 ftrace.evlist = perf_evlist__new();
226 if (ftrace.evlist == NULL)
227 return -ENOMEM;
228
229 ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
230 if (ret < 0)
231 goto out_delete_evlist;
232
233 if (ftrace.tracer == NULL)
234 ftrace.tracer = DEFAULT_TRACER;
235
236 ret = __cmd_ftrace(&ftrace, argc, argv);
237
238out_delete_evlist:
239 perf_evlist__delete(ftrace.evlist);
240
241 return ret;
242}