blob: 64d8ba2fb7bc34c258ace98a4e01011dbdcabc18 [file] [log] [blame]
Tom Zanussi454c4072010-05-01 01:41:20 -05001/*
2 * builtin-inject.c
3 *
4 * Builtin inject command: Examine the live mode (stdin) event stream
5 * and repipe it to stdout while optionally injecting additional
6 * events into it.
7 */
8#include "builtin.h"
9
10#include "perf.h"
11#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050013#include "util/debug.h"
14
15#include "util/parse-options.h"
16
17static char const *input_name = "-";
18static bool inject_build_ids;
19
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020020static int perf_event__repipe_synth(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020021 union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020022 struct machine *machine __used)
Tom Zanussi454c4072010-05-01 01:41:20 -050023{
24 uint32_t size;
25 void *buf = event;
26
27 size = event->header.size;
28
29 while (size) {
30 int ret = write(STDOUT_FILENO, buf, size);
31 if (ret < 0)
32 return -errno;
33
34 size -= ret;
35 buf += ret;
36 }
37
38 return 0;
39}
40
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020041static int perf_event__repipe_op2_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020042 union perf_event *event,
43 struct perf_session *session __used)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020044{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020045 return perf_event__repipe_synth(tool, event, NULL);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020046}
47
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020048static int perf_event__repipe_event_type_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020049 union perf_event *event)
50{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020051 return perf_event__repipe_synth(tool, event, NULL);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020052}
53
54static int perf_event__repipe_tracing_data_synth(union perf_event *event,
55 struct perf_session *session __used)
56{
57 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020058}
59
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020060static int perf_event__repipe_attr(union perf_event *event,
61 struct perf_evlist **pevlist __used)
62{
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020063 int ret;
64 ret = perf_event__process_attr(event, pevlist);
65 if (ret)
66 return ret;
67
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020068 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020069}
70
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020071static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020072 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020073 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020074 struct machine *machine)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020075{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020076 return perf_event__repipe_synth(tool, event, machine);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020077}
78
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020079static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020080 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030081 struct perf_sample *sample __used,
82 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020083 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030084{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020085 return perf_event__repipe_synth(tool, event, machine);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030086}
87
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020088static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020089 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020090 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020091 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -050092{
93 int err;
94
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020095 err = perf_event__process_mmap(tool, event, sample, machine);
96 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -050097
98 return err;
99}
100
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200101static int perf_event__repipe_task(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200102 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200103 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200104 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500105{
106 int err;
107
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200108 err = perf_event__process_task(tool, event, sample, machine);
109 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500110
111 return err;
112}
113
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200114static int perf_event__repipe_tracing_data(union perf_event *event,
115 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500116{
117 int err;
118
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200119 perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200120 err = perf_event__process_tracing_data(event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500121
122 return err;
123}
124
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300125static int dso__read_build_id(struct dso *self)
Tom Zanussi454c4072010-05-01 01:41:20 -0500126{
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300127 if (self->has_build_id)
128 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500129
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300130 if (filename__read_build_id(self->long_name, self->build_id,
131 sizeof(self->build_id)) > 0) {
132 self->has_build_id = true;
133 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500134 }
135
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300136 return -1;
137}
Tom Zanussi454c4072010-05-01 01:41:20 -0500138
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200139static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200140 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300141{
142 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300143 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500144
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300145 if (dso__read_build_id(self) < 0) {
146 pr_debug("no build_id found for %s\n", self->long_name);
147 return -1;
148 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500149
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300150 if (self->kernel)
151 misc = PERF_RECORD_MISC_KERNEL;
152
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200153 err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200154 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300155 if (err) {
156 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500157 return -1;
158 }
159
160 return 0;
161}
162
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200163static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200164 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200165 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300166 struct perf_evsel *evsel __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200167 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500168{
169 struct addr_location al;
170 struct thread *thread;
171 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500172
173 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
174
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200175 thread = machine__findnew_thread(machine, event->ip.pid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500176 if (thread == NULL) {
177 pr_err("problem processing %d event, skipping it.\n",
178 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500179 goto repipe;
180 }
181
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200182 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
183 event->ip.ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500184
185 if (al.map != NULL) {
186 if (!al.map->dso->hit) {
187 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300188 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200189 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300190 /*
191 * If this fails, too bad, let the other side
192 * account this as unresolved.
193 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900194 } else {
195#ifndef NO_LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500196 pr_warning("no symbols found in %s, maybe "
197 "install a debug package?\n",
198 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900199#endif
200 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500201 }
202 }
203
204repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200205 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300206 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500207}
208
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200209struct perf_tool perf_inject = {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300210 .sample = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200211 .mmap = perf_event__repipe,
212 .comm = perf_event__repipe,
213 .fork = perf_event__repipe,
214 .exit = perf_event__repipe,
215 .lost = perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200216 .read = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200217 .throttle = perf_event__repipe,
218 .unthrottle = perf_event__repipe,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200219 .attr = perf_event__repipe_attr,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200220 .event_type = perf_event__repipe_event_type_synth,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200221 .tracing_data = perf_event__repipe_tracing_data_synth,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200222 .build_id = perf_event__repipe_op2_synth,
Tom Zanussi454c4072010-05-01 01:41:20 -0500223};
224
225extern volatile int session_done;
226
227static void sig_handler(int sig __attribute__((__unused__)))
228{
229 session_done = 1;
230}
231
232static int __cmd_inject(void)
233{
234 struct perf_session *session;
235 int ret = -EINVAL;
236
237 signal(SIGINT, sig_handler);
238
239 if (inject_build_ids) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200240 perf_inject.sample = perf_event__inject_buildid;
241 perf_inject.mmap = perf_event__repipe_mmap;
242 perf_inject.fork = perf_event__repipe_task;
243 perf_inject.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500244 }
245
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200246 session = perf_session__new(input_name, O_RDONLY, false, true, &perf_inject);
Tom Zanussi454c4072010-05-01 01:41:20 -0500247 if (session == NULL)
248 return -ENOMEM;
249
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200250 ret = perf_session__process_events(session, &perf_inject);
Tom Zanussi454c4072010-05-01 01:41:20 -0500251
252 perf_session__delete(session);
253
254 return ret;
255}
256
257static const char * const report_usage[] = {
258 "perf inject [<options>]",
259 NULL
260};
261
262static const struct option options[] = {
Arnaldo Carvalho de Melo11d232e2010-05-04 10:48:22 -0300263 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
Tom Zanussi454c4072010-05-01 01:41:20 -0500264 "Inject build-ids into the output stream"),
265 OPT_INCR('v', "verbose", &verbose,
266 "be more verbose (show build ids, etc)"),
267 OPT_END()
268};
269
270int cmd_inject(int argc, const char **argv, const char *prefix __used)
271{
272 argc = parse_options(argc, argv, options, report_usage, 0);
273
274 /*
275 * Any (unrecognized) arguments left?
276 */
277 if (argc)
278 usage_with_options(report_usage, options);
279
280 if (symbol__init() < 0)
281 return -1;
282
283 return __cmd_inject();
284}