blob: c5f6515f07235eb61d2510dd02a8cf9626f1fcc1 [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"
Andrew Vagin26a031e2012-08-07 16:56:04 +040011#include "util/color.h"
12#include "util/evlist.h"
13#include "util/evsel.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050014#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020015#include "util/tool.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050016#include "util/debug.h"
Andrew Vagin54a3cf52012-08-07 16:56:05 +040017#include "util/build-id.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020018#include "util/data.h"
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030019#include "util/auxtrace.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050020
21#include "util/parse-options.h"
22
Andrew Vagin26a031e2012-08-07 16:56:04 +040023#include <linux/list.h>
24
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030025struct perf_inject {
Jiri Olsa34069122013-10-29 19:04:57 +010026 struct perf_tool tool;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +090027 struct perf_session *session;
Jiri Olsa34069122013-10-29 19:04:57 +010028 bool build_ids;
29 bool sched_stat;
30 const char *input_name;
31 struct perf_data_file output;
32 u64 bytes_written;
33 struct list_head samples;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030034 struct itrace_synth_opts itrace_synth_opts;
Andrew Vagin26a031e2012-08-07 16:56:04 +040035};
36
37struct event_entry {
38 struct list_head node;
39 u32 tid;
40 union perf_event event[0];
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030041};
Tom Zanussi454c4072010-05-01 01:41:20 -050042
Adrian Huntercd17a9b2015-04-21 12:21:54 +030043static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
Tom Zanussi454c4072010-05-01 01:41:20 -050044{
Jiri Olsa34069122013-10-29 19:04:57 +010045 ssize_t size;
Tom Zanussi454c4072010-05-01 01:41:20 -050046
Adrian Huntercd17a9b2015-04-21 12:21:54 +030047 size = perf_data_file__write(&inject->output, buf, sz);
Jiri Olsa34069122013-10-29 19:04:57 +010048 if (size < 0)
49 return -errno;
Tom Zanussi454c4072010-05-01 01:41:20 -050050
Jiri Olsa34069122013-10-29 19:04:57 +010051 inject->bytes_written += size;
Tom Zanussi454c4072010-05-01 01:41:20 -050052 return 0;
53}
54
Adrian Huntercd17a9b2015-04-21 12:21:54 +030055static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
56{
57 char buf[4096];
58 ssize_t ssz;
59 int ret;
60
61 while (size > 0) {
62 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
63 if (ssz < 0)
64 return -errno;
65 ret = output_bytes(inject, buf, ssz);
66 if (ret)
67 return ret;
68 size -= ssz;
69 }
70
71 return 0;
72}
73
74static int perf_event__repipe_synth(struct perf_tool *tool,
75 union perf_event *event)
76{
77 struct perf_inject *inject = container_of(tool, struct perf_inject,
78 tool);
79
80 return output_bytes(inject, event, event->header.size);
81}
82
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030083static int perf_event__repipe_oe_synth(struct perf_tool *tool,
84 union perf_event *event,
85 struct ordered_events *oe __maybe_unused)
86{
87 return perf_event__repipe_synth(tool, event);
88}
89
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020090static int perf_event__repipe_op2_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020091 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030092 struct perf_session *session
93 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020094{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +030095 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020096}
97
Adrian Hunter47c3d102013-07-04 16:20:21 +030098static int perf_event__repipe_attr(struct perf_tool *tool,
99 union perf_event *event,
100 struct perf_evlist **pevlist)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200101{
Adrian Hunter89c97d92013-10-22 10:34:09 +0300102 struct perf_inject *inject = container_of(tool, struct perf_inject,
103 tool);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +0200104 int ret;
Adrian Hunter47c3d102013-07-04 16:20:21 +0300105
106 ret = perf_event__process_attr(tool, event, pevlist);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +0200107 if (ret)
108 return ret;
109
Jiri Olsaa261e4a2014-06-05 18:51:44 +0200110 if (!inject->output.is_pipe)
Adrian Hunter89c97d92013-10-22 10:34:09 +0300111 return 0;
112
Adrian Hunter47c3d102013-07-04 16:20:21 +0300113 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200114}
115
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300116static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
117 union perf_event *event,
118 struct perf_session *session
119 __maybe_unused)
120{
121 struct perf_inject *inject = container_of(tool, struct perf_inject,
122 tool);
123 int ret;
124
125 if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
126 ret = output_bytes(inject, event, event->header.size);
127 if (ret < 0)
128 return ret;
129 ret = copy_bytes(inject, perf_data_file__fd(session->file),
130 event->auxtrace.size);
131 } else {
132 ret = output_bytes(inject, event,
133 event->header.size + event->auxtrace.size);
134 }
135 if (ret < 0)
136 return ret;
137
138 return event->auxtrace.size;
139}
140
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200141static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200142 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300143 struct perf_sample *sample __maybe_unused,
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300144 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200145{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300146 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200147}
148
Andrew Vagin26a031e2012-08-07 16:56:04 +0400149typedef int (*inject_handler)(struct perf_tool *tool,
150 union perf_event *event,
151 struct perf_sample *sample,
152 struct perf_evsel *evsel,
153 struct machine *machine);
154
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200155static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200156 union perf_event *event,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400157 struct perf_sample *sample,
158 struct perf_evsel *evsel,
159 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300160{
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300161 if (evsel->handler) {
162 inject_handler f = evsel->handler;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400163 return f(tool, event, sample, evsel, machine);
164 }
165
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400166 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
167
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300168 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300169}
170
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200171static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200172 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200173 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200174 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500175{
176 int err;
177
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200178 err = perf_event__process_mmap(tool, event, sample, machine);
179 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500180
181 return err;
182}
183
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200184static int perf_event__repipe_mmap2(struct perf_tool *tool,
185 union perf_event *event,
186 struct perf_sample *sample,
187 struct machine *machine)
188{
189 int err;
190
191 err = perf_event__process_mmap2(tool, event, sample, machine);
192 perf_event__repipe(tool, event, sample, machine);
193
194 return err;
195}
196
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300197static int perf_event__repipe_fork(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200198 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200199 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200200 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500201{
202 int err;
203
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300204 err = perf_event__process_fork(tool, event, sample, machine);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200205 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500206
207 return err;
208}
209
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300210static int perf_event__repipe_comm(struct perf_tool *tool,
211 union perf_event *event,
212 struct perf_sample *sample,
213 struct machine *machine)
214{
215 int err;
216
217 err = perf_event__process_comm(tool, event, sample, machine);
218 perf_event__repipe(tool, event, sample, machine);
219
220 return err;
221}
222
223static int perf_event__repipe_exit(struct perf_tool *tool,
224 union perf_event *event,
225 struct perf_sample *sample,
226 struct machine *machine)
227{
228 int err;
229
230 err = perf_event__process_exit(tool, event, sample, machine);
231 perf_event__repipe(tool, event, sample, machine);
232
233 return err;
234}
235
Adrian Hunter47c3d102013-07-04 16:20:21 +0300236static int perf_event__repipe_tracing_data(struct perf_tool *tool,
237 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200238 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500239{
240 int err;
241
Adrian Hunter47c3d102013-07-04 16:20:21 +0300242 perf_event__repipe_synth(tool, event);
243 err = perf_event__process_tracing_data(tool, event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500244
245 return err;
246}
247
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300248static int perf_event__repipe_id_index(struct perf_tool *tool,
249 union perf_event *event,
250 struct perf_session *session)
251{
252 int err;
253
254 perf_event__repipe_synth(tool, event);
255 err = perf_event__process_id_index(tool, event, session);
256
257 return err;
258}
259
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300260static int dso__read_build_id(struct dso *dso)
Tom Zanussi454c4072010-05-01 01:41:20 -0500261{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300262 if (dso->has_build_id)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300263 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500264
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300265 if (filename__read_build_id(dso->long_name, dso->build_id,
266 sizeof(dso->build_id)) > 0) {
267 dso->has_build_id = true;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300268 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500269 }
270
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300271 return -1;
272}
Tom Zanussi454c4072010-05-01 01:41:20 -0500273
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300274static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200275 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300276{
277 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300278 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500279
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300280 if (dso__read_build_id(dso) < 0) {
281 pr_debug("no build_id found for %s\n", dso->long_name);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300282 return -1;
283 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500284
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300285 if (dso->kernel)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300286 misc = PERF_RECORD_MISC_KERNEL;
287
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300288 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200289 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300290 if (err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300291 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500292 return -1;
293 }
294
295 return 0;
296}
297
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200298static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200299 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200300 struct perf_sample *sample,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300301 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200302 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500303{
304 struct addr_location al;
305 struct thread *thread;
306 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500307
308 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
309
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900310 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500311 if (thread == NULL) {
312 pr_err("problem processing %d event, skipping it.\n",
313 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500314 goto repipe;
315 }
316
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300317 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500318
319 if (al.map != NULL) {
320 if (!al.map->dso->hit) {
321 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300322 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200323 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300324 /*
325 * If this fails, too bad, let the other side
326 * account this as unresolved.
327 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900328 } else {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200329#ifdef HAVE_LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500330 pr_warning("no symbols found in %s, maybe "
331 "install a debug package?\n",
332 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900333#endif
334 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500335 }
336 }
337
338repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200339 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300340 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500341}
342
Andrew Vagin26a031e2012-08-07 16:56:04 +0400343static int perf_inject__sched_process_exit(struct perf_tool *tool,
344 union perf_event *event __maybe_unused,
345 struct perf_sample *sample,
346 struct perf_evsel *evsel __maybe_unused,
347 struct machine *machine __maybe_unused)
348{
349 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
350 struct event_entry *ent;
351
352 list_for_each_entry(ent, &inject->samples, node) {
353 if (sample->tid == ent->tid) {
354 list_del_init(&ent->node);
355 free(ent);
356 break;
357 }
358 }
359
360 return 0;
361}
362
363static int perf_inject__sched_switch(struct perf_tool *tool,
364 union perf_event *event,
365 struct perf_sample *sample,
366 struct perf_evsel *evsel,
367 struct machine *machine)
368{
369 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
370 struct event_entry *ent;
371
372 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
373
374 ent = malloc(event->header.size + sizeof(struct event_entry));
375 if (ent == NULL) {
376 color_fprintf(stderr, PERF_COLOR_RED,
377 "Not enough memory to process sched switch event!");
378 return -1;
379 }
380
381 ent->tid = sample->tid;
382 memcpy(&ent->event, event, event->header.size);
383 list_add(&ent->node, &inject->samples);
384 return 0;
385}
386
387static int perf_inject__sched_stat(struct perf_tool *tool,
388 union perf_event *event __maybe_unused,
389 struct perf_sample *sample,
390 struct perf_evsel *evsel,
391 struct machine *machine)
392{
393 struct event_entry *ent;
394 union perf_event *event_sw;
395 struct perf_sample sample_sw;
396 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
397 u32 pid = perf_evsel__intval(evsel, sample, "pid");
398
399 list_for_each_entry(ent, &inject->samples, node) {
400 if (pid == ent->tid)
401 goto found;
402 }
403
404 return 0;
405found:
406 event_sw = &ent->event[0];
407 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
408
409 sample_sw.period = sample->period;
410 sample_sw.time = sample->time;
411 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
Adrian Hunterd03f2172013-08-27 11:23:11 +0300412 evsel->attr.read_format, &sample_sw,
413 false);
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400414 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400415 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
416}
417
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300418static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500419{
420 session_done = 1;
421}
422
Andrew Vagin26a031e2012-08-07 16:56:04 +0400423static int perf_evsel__check_stype(struct perf_evsel *evsel,
424 u64 sample_type, const char *sample_msg)
425{
426 struct perf_event_attr *attr = &evsel->attr;
427 const char *name = perf_evsel__name(evsel);
428
429 if (!(attr->sample_type & sample_type)) {
430 pr_err("Samples for %s event do not have %s attribute set.",
431 name, sample_msg);
432 return -EINVAL;
433 }
434
435 return 0;
436}
437
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300438static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500439{
Tom Zanussi454c4072010-05-01 01:41:20 -0500440 int ret = -EINVAL;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900441 struct perf_session *session = inject->session;
Jiri Olsa34069122013-10-29 19:04:57 +0100442 struct perf_data_file *file_out = &inject->output;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900443 int fd = perf_data_file__fd(file_out);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300444 u64 output_data_offset;
Tom Zanussi454c4072010-05-01 01:41:20 -0500445
446 signal(SIGINT, sig_handler);
447
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300448 if (inject->build_ids || inject->sched_stat ||
449 inject->itrace_synth_opts.set) {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300450 inject->tool.mmap = perf_event__repipe_mmap;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200451 inject->tool.mmap2 = perf_event__repipe_mmap2;
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300452 inject->tool.fork = perf_event__repipe_fork;
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300453 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500454 }
455
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300456 output_data_offset = session->header.data_offset;
457
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400458 if (inject->build_ids) {
459 inject->tool.sample = perf_event__inject_buildid;
460 } else if (inject->sched_stat) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400461 struct perf_evsel *evsel;
462
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300463 evlist__for_each(session->evlist, evsel) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400464 const char *name = perf_evsel__name(evsel);
465
466 if (!strcmp(name, "sched:sched_switch")) {
467 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
468 return -EINVAL;
469
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300470 evsel->handler = perf_inject__sched_switch;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400471 } else if (!strcmp(name, "sched:sched_process_exit"))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300472 evsel->handler = perf_inject__sched_process_exit;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400473 else if (!strncmp(name, "sched:sched_stat_", 17))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300474 evsel->handler = perf_inject__sched_stat;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400475 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300476 } else if (inject->itrace_synth_opts.set) {
477 session->itrace_synth_opts = &inject->itrace_synth_opts;
478 inject->itrace_synth_opts.inject = true;
479 inject->tool.comm = perf_event__repipe_comm;
480 inject->tool.exit = perf_event__repipe_exit;
481 inject->tool.id_index = perf_event__repipe_id_index;
482 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
483 inject->tool.auxtrace = perf_event__process_auxtrace;
484 inject->tool.ordered_events = true;
485 inject->tool.ordering_requires_timestamps = true;
486 /* Allow space in the header for new attributes */
487 output_data_offset = 4096;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400488 }
489
Jiri Olsa34069122013-10-29 19:04:57 +0100490 if (!file_out->is_pipe)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300491 lseek(fd, output_data_offset, SEEK_SET);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400492
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300493 ret = perf_session__process_events(session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500494
Jiri Olsa34069122013-10-29 19:04:57 +0100495 if (!file_out->is_pipe) {
Adrian Huntere38b43c2014-07-14 13:02:34 +0300496 if (inject->build_ids)
497 perf_header__set_feat(&session->header,
498 HEADER_BUILD_ID);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300499 /*
500 * The AUX areas have been removed and replaced with
501 * synthesized hardware events, so clear the feature flag.
502 */
503 if (inject->itrace_synth_opts.set)
504 perf_header__clear_feat(&session->header,
505 HEADER_AUXTRACE);
506 session->header.data_offset = output_data_offset;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400507 session->header.data_size = inject->bytes_written;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900508 perf_session__write_header(session, session->evlist, fd, true);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400509 }
510
Tom Zanussi454c4072010-05-01 01:41:20 -0500511 return ret;
512}
513
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300514int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500515{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300516 struct perf_inject inject = {
517 .tool = {
518 .sample = perf_event__repipe_sample,
519 .mmap = perf_event__repipe,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200520 .mmap2 = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300521 .comm = perf_event__repipe,
522 .fork = perf_event__repipe,
523 .exit = perf_event__repipe,
524 .lost = perf_event__repipe,
525 .read = perf_event__repipe_sample,
526 .throttle = perf_event__repipe,
527 .unthrottle = perf_event__repipe,
528 .attr = perf_event__repipe_attr,
Adrian Hunter47c3d102013-07-04 16:20:21 +0300529 .tracing_data = perf_event__repipe_op2_synth,
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300530 .auxtrace_info = perf_event__repipe_op2_synth,
531 .auxtrace = perf_event__repipe_auxtrace,
532 .auxtrace_error = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300533 .finished_round = perf_event__repipe_oe_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300534 .build_id = perf_event__repipe_op2_synth,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200535 .id_index = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300536 },
Andrew Vagine558a5b2012-08-07 16:56:02 +0400537 .input_name = "-",
Andrew Vagin26a031e2012-08-07 16:56:04 +0400538 .samples = LIST_HEAD_INIT(inject.samples),
Jiri Olsa34069122013-10-29 19:04:57 +0100539 .output = {
540 .path = "-",
541 .mode = PERF_DATA_MODE_WRITE,
542 },
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300543 };
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900544 struct perf_data_file file = {
545 .mode = PERF_DATA_MODE_READ,
546 };
547 int ret;
548
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300549 const struct option options[] = {
550 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
551 "Inject build-ids into the output stream"),
Andrew Vagine558a5b2012-08-07 16:56:02 +0400552 OPT_STRING('i', "input", &inject.input_name, "file",
553 "input file name"),
Jiri Olsa34069122013-10-29 19:04:57 +0100554 OPT_STRING('o', "output", &inject.output.path, "file",
Andrew Vagine558a5b2012-08-07 16:56:02 +0400555 "output file name"),
Andrew Vagin26a031e2012-08-07 16:56:04 +0400556 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
557 "Merge sched-stat and sched-switch for getting events "
558 "where and how long tasks slept"),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300559 OPT_INCR('v', "verbose", &verbose,
560 "be more verbose (show build ids, etc)"),
Adrian Huntera7a2b8b2014-07-22 16:17:38 +0300561 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
562 "kallsyms pathname"),
Yunlong Songccaa4742015-04-02 21:47:11 +0800563 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300564 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
565 NULL, "opts", "Instruction Tracing options",
566 itrace_parse_synth_opts),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300567 OPT_END()
568 };
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300569 const char * const inject_usage[] = {
570 "perf inject [<options>]",
571 NULL
572 };
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300573
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300574 argc = parse_options(argc, argv, options, inject_usage, 0);
Tom Zanussi454c4072010-05-01 01:41:20 -0500575
576 /*
577 * Any (unrecognized) arguments left?
578 */
579 if (argc)
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300580 usage_with_options(inject_usage, options);
Tom Zanussi454c4072010-05-01 01:41:20 -0500581
Jiri Olsa34069122013-10-29 19:04:57 +0100582 if (perf_data_file__open(&inject.output)) {
583 perror("failed to create output file");
584 return -1;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400585 }
586
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300587 inject.tool.ordered_events = inject.sched_stat;
588
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900589 file.path = inject.input_name;
590 inject.session = perf_session__new(&file, true, &inject.tool);
591 if (inject.session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900592 return -1;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900593
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900594 if (symbol__init(&inject.session->header.env) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -0500595 return -1;
596
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900597 ret = __cmd_inject(&inject);
598
599 perf_session__delete(inject.session);
600
601 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500602}