4 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 * Released under the GPL v2.
17 #include <subcmd/parse-options.h>
20 #include "thread_map.h"
21 #include "util/config.h"
24 #define DEFAULT_TRACER "function_graph"
27 struct perf_evlist
*evlist
;
34 static void sig_handler(int sig __maybe_unused
)
40 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
41 * we asked by setting its exec_error to the function below,
42 * ftrace__workload_exec_failed_signal.
44 * XXX We need to handle this more appropriately, emitting an error, etc.
46 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused
,
47 siginfo_t
*info __maybe_unused
,
48 void *ucontext __maybe_unused
)
50 /* workload_exec_errno = info->si_value.sival_int; */
54 static int __write_tracing_file(const char *name
, const char *val
, bool append
)
58 ssize_t size
= strlen(val
);
61 file
= get_tracing_file(name
);
63 pr_debug("cannot get tracing file: %s\n", name
);
72 fd
= open(file
, flags
);
74 pr_debug("cannot open tracing file: %s\n", name
);
78 if (write(fd
, val
, size
) == size
)
81 pr_debug("write '%s' to tracing/%s failed\n", val
, name
);
85 put_tracing_file(file
);
89 static int write_tracing_file(const char *name
, const char *val
)
91 return __write_tracing_file(name
, val
, false);
94 static int append_tracing_file(const char *name
, const char *val
)
96 return __write_tracing_file(name
, val
, true);
99 static int reset_tracing_files(struct perf_ftrace
*ftrace __maybe_unused
)
101 if (write_tracing_file("tracing_on", "0") < 0)
104 if (write_tracing_file("current_tracer", "nop") < 0)
107 if (write_tracing_file("set_ftrace_pid", " ") < 0)
113 static int set_tracing_pid(struct perf_ftrace
*ftrace
)
118 if (target__has_cpu(&ftrace
->target
))
121 for (i
= 0; i
< thread_map__nr(ftrace
->evlist
->threads
); i
++) {
122 scnprintf(buf
, sizeof(buf
), "%d",
123 ftrace
->evlist
->threads
->map
[i
]);
124 if (append_tracing_file("set_ftrace_pid", buf
) < 0)
130 static int __cmd_ftrace(struct perf_ftrace
*ftrace
, int argc
, const char **argv
)
135 struct pollfd pollfd
= {
139 if (geteuid() != 0) {
140 pr_err("ftrace only works for root!\n");
144 signal(SIGINT
, sig_handler
);
145 signal(SIGUSR1
, sig_handler
);
146 signal(SIGCHLD
, sig_handler
);
148 if (reset_tracing_files(ftrace
) < 0)
151 /* reset ftrace buffer */
152 if (write_tracing_file("trace", "0") < 0)
155 if (argc
&& perf_evlist__prepare_workload(ftrace
->evlist
,
156 &ftrace
->target
, argv
, false,
157 ftrace__workload_exec_failed_signal
) < 0) {
161 if (set_tracing_pid(ftrace
) < 0) {
162 pr_err("failed to set ftrace pid\n");
166 if (write_tracing_file("current_tracer", ftrace
->tracer
) < 0) {
167 pr_err("failed to set current_tracer to %s\n", ftrace
->tracer
);
171 trace_file
= get_tracing_file("trace_pipe");
173 pr_err("failed to open trace_pipe\n");
177 trace_fd
= open(trace_file
, O_RDONLY
);
179 put_tracing_file(trace_file
);
182 pr_err("failed to open trace_pipe\n");
186 fcntl(trace_fd
, F_SETFL
, O_NONBLOCK
);
187 pollfd
.fd
= trace_fd
;
189 if (write_tracing_file("tracing_on", "1") < 0) {
190 pr_err("can't enable tracing\n");
194 perf_evlist__start_workload(ftrace
->evlist
);
197 if (poll(&pollfd
, 1, -1) < 0)
200 if (pollfd
.revents
& POLLIN
) {
201 int n
= read(trace_fd
, buf
, sizeof(buf
));
204 if (fwrite(buf
, n
, 1, stdout
) != 1)
209 write_tracing_file("tracing_on", "0");
211 /* read remaining buffer contents */
213 int n
= read(trace_fd
, buf
, sizeof(buf
));
216 if (fwrite(buf
, n
, 1, stdout
) != 1)
223 reset_tracing_files(ftrace
);
225 return done
? 0 : -1;
228 static int perf_ftrace_config(const char *var
, const char *value
, void *cb
)
230 struct perf_ftrace
*ftrace
= cb
;
232 if (prefixcmp(var
, "ftrace."))
235 if (strcmp(var
, "ftrace.tracer"))
238 if (!strcmp(value
, "function_graph") ||
239 !strcmp(value
, "function")) {
240 ftrace
->tracer
= value
;
244 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
248 int cmd_ftrace(int argc
, const char **argv
, const char *prefix __maybe_unused
)
251 struct perf_ftrace ftrace
= {
252 .tracer
= DEFAULT_TRACER
,
253 .target
= { .uid
= UINT_MAX
, },
255 const char * const ftrace_usage
[] = {
256 "perf ftrace [<options>] [<command>]",
257 "perf ftrace [<options>] -- <command> [<options>]",
260 const struct option ftrace_options
[] = {
261 OPT_STRING('t', "tracer", &ftrace
.tracer
, "tracer",
262 "tracer to use: function_graph(default) or function"),
263 OPT_STRING('p', "pid", &ftrace
.target
.pid
, "pid",
264 "trace on existing process id"),
265 OPT_INCR('v', "verbose", &verbose
,
270 ret
= perf_config(perf_ftrace_config
, &ftrace
);
274 argc
= parse_options(argc
, argv
, ftrace_options
, ftrace_usage
,
275 PARSE_OPT_STOP_AT_NON_OPTION
);
276 if (!argc
&& target__none(&ftrace
.target
))
277 usage_with_options(ftrace_usage
, ftrace_options
);
279 ret
= target__validate(&ftrace
.target
);
283 target__strerror(&ftrace
.target
, ret
, errbuf
, 512);
284 pr_err("%s\n", errbuf
);
288 ftrace
.evlist
= perf_evlist__new();
289 if (ftrace
.evlist
== NULL
)
292 ret
= perf_evlist__create_maps(ftrace
.evlist
, &ftrace
.target
);
294 goto out_delete_evlist
;
296 ret
= __cmd_ftrace(&ftrace
, argc
, argv
);
299 perf_evlist__delete(ftrace
.evlist
);