4 * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
6 * Released under the GPL v2.
16 #include <subcmd/parse-options.h>
19 #include "thread_map.h"
20 #include "util/config.h"
23 #define DEFAULT_TRACER "function_graph"
26 struct perf_evlist
*evlist
;
33 static void sig_handler(int sig __maybe_unused
)
39 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
40 * we asked by setting its exec_error to the function below,
41 * ftrace__workload_exec_failed_signal.
43 * XXX We need to handle this more appropriately, emitting an error, etc.
45 static void ftrace__workload_exec_failed_signal(int signo __maybe_unused
,
46 siginfo_t
*info __maybe_unused
,
47 void *ucontext __maybe_unused
)
49 /* workload_exec_errno = info->si_value.sival_int; */
53 static int write_tracing_file(const char *name
, const char *val
)
57 ssize_t size
= strlen(val
);
59 file
= get_tracing_file(name
);
61 pr_debug("cannot get tracing file: %s\n", name
);
65 fd
= open(file
, O_WRONLY
);
67 pr_debug("cannot open tracing file: %s\n", name
);
71 if (write(fd
, val
, size
) == size
)
74 pr_debug("write '%s' to tracing/%s failed\n", val
, name
);
78 put_tracing_file(file
);
82 static int reset_tracing_files(struct perf_ftrace
*ftrace __maybe_unused
)
84 if (write_tracing_file("tracing_on", "0") < 0)
87 if (write_tracing_file("current_tracer", "nop") < 0)
90 if (write_tracing_file("set_ftrace_pid", " ") < 0)
96 static int __cmd_ftrace(struct perf_ftrace
*ftrace
, int argc
, const char **argv
)
102 struct pollfd pollfd
= {
106 if (geteuid() != 0) {
107 pr_err("ftrace only works for root!\n");
114 signal(SIGINT
, sig_handler
);
115 signal(SIGUSR1
, sig_handler
);
116 signal(SIGCHLD
, sig_handler
);
118 reset_tracing_files(ftrace
);
120 /* reset ftrace buffer */
121 if (write_tracing_file("trace", "0") < 0)
124 if (perf_evlist__prepare_workload(ftrace
->evlist
, &ftrace
->target
,
125 argv
, false, ftrace__workload_exec_failed_signal
) < 0)
128 if (write_tracing_file("current_tracer", ftrace
->tracer
) < 0) {
129 pr_err("failed to set current_tracer to %s\n", ftrace
->tracer
);
133 if (asprintf(&trace_pid
, "%d", thread_map__pid(ftrace
->evlist
->threads
, 0)) < 0) {
134 pr_err("failed to allocate pid string\n");
138 if (write_tracing_file("set_ftrace_pid", trace_pid
) < 0) {
139 pr_err("failed to set pid: %s\n", trace_pid
);
143 trace_file
= get_tracing_file("trace_pipe");
145 pr_err("failed to open trace_pipe\n");
149 trace_fd
= open(trace_file
, O_RDONLY
);
151 put_tracing_file(trace_file
);
154 pr_err("failed to open trace_pipe\n");
158 fcntl(trace_fd
, F_SETFL
, O_NONBLOCK
);
159 pollfd
.fd
= trace_fd
;
161 if (write_tracing_file("tracing_on", "1") < 0) {
162 pr_err("can't enable tracing\n");
166 perf_evlist__start_workload(ftrace
->evlist
);
169 if (poll(&pollfd
, 1, -1) < 0)
172 if (pollfd
.revents
& POLLIN
) {
173 int n
= read(trace_fd
, buf
, sizeof(buf
));
176 if (fwrite(buf
, n
, 1, stdout
) != 1)
181 write_tracing_file("tracing_on", "0");
183 /* read remaining buffer contents */
185 int n
= read(trace_fd
, buf
, sizeof(buf
));
188 if (fwrite(buf
, n
, 1, stdout
) != 1)
197 reset_tracing_files(ftrace
);
199 return done
? 0 : -1;
202 static int perf_ftrace_config(const char *var
, const char *value
, void *cb
)
204 struct perf_ftrace
*ftrace
= cb
;
206 if (prefixcmp(var
, "ftrace."))
209 if (strcmp(var
, "ftrace.tracer"))
212 if (!strcmp(value
, "function_graph") ||
213 !strcmp(value
, "function")) {
214 ftrace
->tracer
= value
;
218 pr_err("Please select \"function_graph\" (default) or \"function\"\n");
222 int cmd_ftrace(int argc
, const char **argv
, const char *prefix __maybe_unused
)
225 struct perf_ftrace ftrace
= {
226 .tracer
= DEFAULT_TRACER
,
227 .target
= { .uid
= UINT_MAX
, },
229 const char * const ftrace_usage
[] = {
230 "perf ftrace [<options>] <command>",
231 "perf ftrace [<options>] -- <command> [<options>]",
234 const struct option ftrace_options
[] = {
235 OPT_STRING('t', "tracer", &ftrace
.tracer
, "tracer",
236 "tracer to use: function_graph(default) or function"),
237 OPT_INCR('v', "verbose", &verbose
,
242 ret
= perf_config(perf_ftrace_config
, &ftrace
);
246 argc
= parse_options(argc
, argv
, ftrace_options
, ftrace_usage
,
247 PARSE_OPT_STOP_AT_NON_OPTION
);
249 usage_with_options(ftrace_usage
, ftrace_options
);
251 ftrace
.evlist
= perf_evlist__new();
252 if (ftrace
.evlist
== NULL
)
255 ret
= perf_evlist__create_maps(ftrace
.evlist
, &ftrace
.target
);
257 goto out_delete_evlist
;
259 ret
= __cmd_ftrace(&ftrace
, argc
, argv
);
262 perf_evlist__delete(ftrace
.evlist
);