blob: b5ab084233fadbd31a9afe88cd28e64fa6136fd8 [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
Irina Tirdea1d037ca2012-09-11 01:15:03 +030020static int perf_event__repipe_synth(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020021 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030022 struct machine *machine __maybe_unused)
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,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030043 struct perf_session *session
44 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020045{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020046 return perf_event__repipe_synth(tool, event, NULL);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020047}
48
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020049static int perf_event__repipe_event_type_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020050 union perf_event *event)
51{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020052 return perf_event__repipe_synth(tool, event, NULL);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020053}
54
55static int perf_event__repipe_tracing_data_synth(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030056 struct perf_session *session
57 __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020058{
59 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020060}
61
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020062static int perf_event__repipe_attr(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030063 struct perf_evlist **pevlist __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020064{
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020065 int ret;
66 ret = perf_event__process_attr(event, pevlist);
67 if (ret)
68 return ret;
69
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020070 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020071}
72
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020073static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020074 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030075 struct perf_sample *sample __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020076 struct machine *machine)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020077{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020078 return perf_event__repipe_synth(tool, event, machine);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020079}
80
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020081static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020082 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030083 struct perf_sample *sample __maybe_unused,
84 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020085 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030086{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020087 return perf_event__repipe_synth(tool, event, machine);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030088}
89
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020090static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020091 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020092 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020093 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -050094{
95 int err;
96
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020097 err = perf_event__process_mmap(tool, event, sample, machine);
98 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -050099
100 return err;
101}
102
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200103static int perf_event__repipe_task(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200104 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200105 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200106 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500107{
108 int err;
109
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200110 err = perf_event__process_task(tool, event, sample, machine);
111 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500112
113 return err;
114}
115
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200116static int perf_event__repipe_tracing_data(union perf_event *event,
117 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500118{
119 int err;
120
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200121 perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200122 err = perf_event__process_tracing_data(event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500123
124 return err;
125}
126
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300127static int dso__read_build_id(struct dso *self)
Tom Zanussi454c4072010-05-01 01:41:20 -0500128{
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300129 if (self->has_build_id)
130 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500131
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300132 if (filename__read_build_id(self->long_name, self->build_id,
133 sizeof(self->build_id)) > 0) {
134 self->has_build_id = true;
135 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500136 }
137
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300138 return -1;
139}
Tom Zanussi454c4072010-05-01 01:41:20 -0500140
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200141static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200142 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300143{
144 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300145 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500146
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300147 if (dso__read_build_id(self) < 0) {
148 pr_debug("no build_id found for %s\n", self->long_name);
149 return -1;
150 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500151
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300152 if (self->kernel)
153 misc = PERF_RECORD_MISC_KERNEL;
154
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200155 err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200156 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300157 if (err) {
158 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500159 return -1;
160 }
161
162 return 0;
163}
164
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200165static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200166 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200167 struct perf_sample *sample,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300168 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200169 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500170{
171 struct addr_location al;
172 struct thread *thread;
173 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500174
175 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
176
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200177 thread = machine__findnew_thread(machine, event->ip.pid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500178 if (thread == NULL) {
179 pr_err("problem processing %d event, skipping it.\n",
180 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500181 goto repipe;
182 }
183
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200184 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
185 event->ip.ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500186
187 if (al.map != NULL) {
188 if (!al.map->dso->hit) {
189 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300190 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200191 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300192 /*
193 * If this fails, too bad, let the other side
194 * account this as unresolved.
195 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900196 } else {
Namhyung Kim29a0fc92012-09-28 18:31:59 +0900197#ifdef LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500198 pr_warning("no symbols found in %s, maybe "
199 "install a debug package?\n",
200 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900201#endif
202 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500203 }
204 }
205
206repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200207 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300208 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500209}
210
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200211struct perf_tool perf_inject = {
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300212 .sample = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200213 .mmap = perf_event__repipe,
214 .comm = perf_event__repipe,
215 .fork = perf_event__repipe,
216 .exit = perf_event__repipe,
217 .lost = perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200218 .read = perf_event__repipe_sample,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200219 .throttle = perf_event__repipe,
220 .unthrottle = perf_event__repipe,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200221 .attr = perf_event__repipe_attr,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200222 .event_type = perf_event__repipe_event_type_synth,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200223 .tracing_data = perf_event__repipe_tracing_data_synth,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200224 .build_id = perf_event__repipe_op2_synth,
Tom Zanussi454c4072010-05-01 01:41:20 -0500225};
226
227extern volatile int session_done;
228
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300229static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500230{
231 session_done = 1;
232}
233
234static int __cmd_inject(void)
235{
236 struct perf_session *session;
237 int ret = -EINVAL;
238
239 signal(SIGINT, sig_handler);
240
241 if (inject_build_ids) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200242 perf_inject.sample = perf_event__inject_buildid;
243 perf_inject.mmap = perf_event__repipe_mmap;
244 perf_inject.fork = perf_event__repipe_task;
245 perf_inject.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500246 }
247
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200248 session = perf_session__new(input_name, O_RDONLY, false, true, &perf_inject);
Tom Zanussi454c4072010-05-01 01:41:20 -0500249 if (session == NULL)
250 return -ENOMEM;
251
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200252 ret = perf_session__process_events(session, &perf_inject);
Tom Zanussi454c4072010-05-01 01:41:20 -0500253
254 perf_session__delete(session);
255
256 return ret;
257}
258
259static const char * const report_usage[] = {
260 "perf inject [<options>]",
261 NULL
262};
263
264static const struct option options[] = {
Arnaldo Carvalho de Melo11d232e2010-05-04 10:48:22 -0300265 OPT_BOOLEAN('b', "build-ids", &inject_build_ids,
Tom Zanussi454c4072010-05-01 01:41:20 -0500266 "Inject build-ids into the output stream"),
267 OPT_INCR('v', "verbose", &verbose,
268 "be more verbose (show build ids, etc)"),
269 OPT_END()
270};
271
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300272int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500273{
274 argc = parse_options(argc, argv, options, report_usage, 0);
275
276 /*
277 * Any (unrecognized) arguments left?
278 */
279 if (argc)
280 usage_with_options(report_usage, options);
281
282 if (symbol__init() < 0)
283 return -1;
284
285 return __cmd_inject();
286}