blob: 3c9ab55f49e644baefb539c3ac6d4def43d0adc6 [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
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030017struct perf_inject {
18 struct perf_tool tool;
19 bool build_ids;
20};
Tom Zanussi454c4072010-05-01 01:41:20 -050021
Irina Tirdea1d037ca2012-09-11 01:15:03 +030022static int perf_event__repipe_synth(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020023 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030024 struct machine *machine __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -050025{
26 uint32_t size;
27 void *buf = event;
28
29 size = event->header.size;
30
31 while (size) {
32 int ret = write(STDOUT_FILENO, buf, size);
33 if (ret < 0)
34 return -errno;
35
36 size -= ret;
37 buf += ret;
38 }
39
40 return 0;
41}
42
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020043static int perf_event__repipe_op2_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020044 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045 struct perf_session *session
46 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020047{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020048 return perf_event__repipe_synth(tool, event, NULL);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020049}
50
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020051static int perf_event__repipe_event_type_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020052 union perf_event *event)
53{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020054 return perf_event__repipe_synth(tool, event, NULL);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020055}
56
57static int perf_event__repipe_tracing_data_synth(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030058 struct perf_session *session
59 __maybe_unused)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020060{
61 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020062}
63
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020064static int perf_event__repipe_attr(union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030065 struct perf_evlist **pevlist __maybe_unused)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020066{
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020067 int ret;
68 ret = perf_event__process_attr(event, pevlist);
69 if (ret)
70 return ret;
71
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020072 return perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020073}
74
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020075static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020076 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030077 struct perf_sample *sample __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020078 struct machine *machine)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020079{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020080 return perf_event__repipe_synth(tool, event, machine);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020081}
82
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020083static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020084 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030085 struct perf_sample *sample __maybe_unused,
86 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020087 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030088{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020089 return perf_event__repipe_synth(tool, event, machine);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -030090}
91
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020092static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020093 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020094 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020095 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -050096{
97 int err;
98
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020099 err = perf_event__process_mmap(tool, event, sample, machine);
100 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500101
102 return err;
103}
104
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200105static int perf_event__repipe_task(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200106 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200107 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200108 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500109{
110 int err;
111
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200112 err = perf_event__process_task(tool, event, sample, machine);
113 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500114
115 return err;
116}
117
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200118static int perf_event__repipe_tracing_data(union perf_event *event,
119 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500120{
121 int err;
122
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200123 perf_event__repipe_synth(NULL, event, NULL);
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200124 err = perf_event__process_tracing_data(event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500125
126 return err;
127}
128
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300129static int dso__read_build_id(struct dso *self)
Tom Zanussi454c4072010-05-01 01:41:20 -0500130{
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300131 if (self->has_build_id)
132 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500133
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300134 if (filename__read_build_id(self->long_name, self->build_id,
135 sizeof(self->build_id)) > 0) {
136 self->has_build_id = true;
137 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500138 }
139
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300140 return -1;
141}
Tom Zanussi454c4072010-05-01 01:41:20 -0500142
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200143static int dso__inject_build_id(struct dso *self, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200144 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300145{
146 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300147 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500148
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300149 if (dso__read_build_id(self) < 0) {
150 pr_debug("no build_id found for %s\n", self->long_name);
151 return -1;
152 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500153
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300154 if (self->kernel)
155 misc = PERF_RECORD_MISC_KERNEL;
156
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200157 err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200158 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300159 if (err) {
160 pr_err("Can't synthesize build_id event for %s\n", self->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500161 return -1;
162 }
163
164 return 0;
165}
166
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200167static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200168 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200169 struct perf_sample *sample,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300170 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200171 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500172{
173 struct addr_location al;
174 struct thread *thread;
175 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500176
177 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
178
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200179 thread = machine__findnew_thread(machine, event->ip.pid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500180 if (thread == NULL) {
181 pr_err("problem processing %d event, skipping it.\n",
182 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500183 goto repipe;
184 }
185
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200186 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
187 event->ip.ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500188
189 if (al.map != NULL) {
190 if (!al.map->dso->hit) {
191 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300192 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200193 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300194 /*
195 * If this fails, too bad, let the other side
196 * account this as unresolved.
197 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900198 } else {
Namhyung Kim29a0fc92012-09-28 18:31:59 +0900199#ifdef LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500200 pr_warning("no symbols found in %s, maybe "
201 "install a debug package?\n",
202 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900203#endif
204 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500205 }
206 }
207
208repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200209 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300210 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500211}
212
Tom Zanussi454c4072010-05-01 01:41:20 -0500213extern volatile int session_done;
214
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300215static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500216{
217 session_done = 1;
218}
219
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300220static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500221{
222 struct perf_session *session;
223 int ret = -EINVAL;
224
225 signal(SIGINT, sig_handler);
226
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300227 if (inject->build_ids) {
228 inject->tool.sample = perf_event__inject_buildid;
229 inject->tool.mmap = perf_event__repipe_mmap;
230 inject->tool.fork = perf_event__repipe_task;
231 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500232 }
233
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300234 session = perf_session__new("-", O_RDONLY, false, true, &inject->tool);
Tom Zanussi454c4072010-05-01 01:41:20 -0500235 if (session == NULL)
236 return -ENOMEM;
237
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300238 ret = perf_session__process_events(session, &inject->tool);
Tom Zanussi454c4072010-05-01 01:41:20 -0500239
240 perf_session__delete(session);
241
242 return ret;
243}
244
245static const char * const report_usage[] = {
246 "perf inject [<options>]",
247 NULL
248};
249
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300250int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500251{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300252 struct perf_inject inject = {
253 .tool = {
254 .sample = perf_event__repipe_sample,
255 .mmap = perf_event__repipe,
256 .comm = perf_event__repipe,
257 .fork = perf_event__repipe,
258 .exit = perf_event__repipe,
259 .lost = perf_event__repipe,
260 .read = perf_event__repipe_sample,
261 .throttle = perf_event__repipe,
262 .unthrottle = perf_event__repipe,
263 .attr = perf_event__repipe_attr,
264 .event_type = perf_event__repipe_event_type_synth,
265 .tracing_data = perf_event__repipe_tracing_data_synth,
266 .build_id = perf_event__repipe_op2_synth,
267 },
268 };
269 const struct option options[] = {
270 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
271 "Inject build-ids into the output stream"),
272 OPT_INCR('v', "verbose", &verbose,
273 "be more verbose (show build ids, etc)"),
274 OPT_END()
275 };
276
Tom Zanussi454c4072010-05-01 01:41:20 -0500277 argc = parse_options(argc, argv, options, report_usage, 0);
278
279 /*
280 * Any (unrecognized) arguments left?
281 */
282 if (argc)
283 usage_with_options(report_usage, options);
284
285 if (symbol__init() < 0)
286 return -1;
287
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300288 return __cmd_inject(&inject);
Tom Zanussi454c4072010-05-01 01:41:20 -0500289}