blob: f62c49b35be04ec698fc51e2b73ded6bb9dfbccf [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 Olsaf5fc1412013-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;
Adrian Huntercd10b282015-04-30 17:37:26 +030030 bool have_auxtrace;
Jiri Olsa34069122013-10-29 19:04:57 +010031 const char *input_name;
32 struct perf_data_file output;
33 u64 bytes_written;
34 struct list_head samples;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030035 struct itrace_synth_opts itrace_synth_opts;
Andrew Vagin26a031e2012-08-07 16:56:04 +040036};
37
38struct event_entry {
39 struct list_head node;
40 u32 tid;
41 union perf_event event[0];
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030042};
Tom Zanussi454c4072010-05-01 01:41:20 -050043
Adrian Huntercd17a9b2015-04-21 12:21:54 +030044static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
Tom Zanussi454c4072010-05-01 01:41:20 -050045{
Jiri Olsa34069122013-10-29 19:04:57 +010046 ssize_t size;
Tom Zanussi454c4072010-05-01 01:41:20 -050047
Adrian Huntercd17a9b2015-04-21 12:21:54 +030048 size = perf_data_file__write(&inject->output, buf, sz);
Jiri Olsa34069122013-10-29 19:04:57 +010049 if (size < 0)
50 return -errno;
Tom Zanussi454c4072010-05-01 01:41:20 -050051
Jiri Olsa34069122013-10-29 19:04:57 +010052 inject->bytes_written += size;
Tom Zanussi454c4072010-05-01 01:41:20 -050053 return 0;
54}
55
Adrian Huntercd17a9b2015-04-21 12:21:54 +030056static int perf_event__repipe_synth(struct perf_tool *tool,
57 union perf_event *event)
58{
59 struct perf_inject *inject = container_of(tool, struct perf_inject,
60 tool);
61
62 return output_bytes(inject, event, event->header.size);
63}
64
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030065static int perf_event__repipe_oe_synth(struct perf_tool *tool,
66 union perf_event *event,
67 struct ordered_events *oe __maybe_unused)
68{
69 return perf_event__repipe_synth(tool, event);
70}
71
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020072static int perf_event__repipe_op2_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020073 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030074 struct perf_session *session
75 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020076{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +030077 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020078}
79
Adrian Hunter47c3d102013-07-04 16:20:21 +030080static int perf_event__repipe_attr(struct perf_tool *tool,
81 union perf_event *event,
82 struct perf_evlist **pevlist)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020083{
Adrian Hunter89c97d92013-10-22 10:34:09 +030084 struct perf_inject *inject = container_of(tool, struct perf_inject,
85 tool);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020086 int ret;
Adrian Hunter47c3d102013-07-04 16:20:21 +030087
88 ret = perf_event__process_attr(tool, event, pevlist);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020089 if (ret)
90 return ret;
91
Jiri Olsaa261e4a2014-06-05 18:51:44 +020092 if (!inject->output.is_pipe)
Adrian Hunter89c97d92013-10-22 10:34:09 +030093 return 0;
94
Adrian Hunter47c3d102013-07-04 16:20:21 +030095 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020096}
97
Adrian Huntere31f0d02015-04-30 17:37:27 +030098#ifdef HAVE_AUXTRACE_SUPPORT
99
100static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
101{
102 char buf[4096];
103 ssize_t ssz;
104 int ret;
105
106 while (size > 0) {
107 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
108 if (ssz < 0)
109 return -errno;
110 ret = output_bytes(inject, buf, ssz);
111 if (ret)
112 return ret;
113 size -= ssz;
114 }
115
116 return 0;
117}
118
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300119static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
120 union perf_event *event,
121 struct perf_session *session
122 __maybe_unused)
123{
124 struct perf_inject *inject = container_of(tool, struct perf_inject,
125 tool);
126 int ret;
127
Adrian Huntercd10b282015-04-30 17:37:26 +0300128 inject->have_auxtrace = true;
129
Adrian Hunter99fa2982015-04-30 17:37:25 +0300130 if (!inject->output.is_pipe) {
131 off_t offset;
132
133 offset = lseek(inject->output.fd, 0, SEEK_CUR);
134 if (offset == -1)
135 return -errno;
136 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
137 event, offset);
138 if (ret < 0)
139 return ret;
140 }
141
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300142 if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
143 ret = output_bytes(inject, event, event->header.size);
144 if (ret < 0)
145 return ret;
146 ret = copy_bytes(inject, perf_data_file__fd(session->file),
147 event->auxtrace.size);
148 } else {
149 ret = output_bytes(inject, event,
150 event->header.size + event->auxtrace.size);
151 }
152 if (ret < 0)
153 return ret;
154
155 return event->auxtrace.size;
156}
157
Adrian Huntere31f0d02015-04-30 17:37:27 +0300158#else
159
160static s64
161perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
162 union perf_event *event __maybe_unused,
163 struct perf_session *session __maybe_unused)
164{
165 pr_err("AUX area tracing not supported\n");
166 return -EINVAL;
167}
168
169#endif
170
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200171static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200172 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300173 struct perf_sample *sample __maybe_unused,
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300174 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200175{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300176 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200177}
178
Andrew Vagin26a031e2012-08-07 16:56:04 +0400179typedef int (*inject_handler)(struct perf_tool *tool,
180 union perf_event *event,
181 struct perf_sample *sample,
182 struct perf_evsel *evsel,
183 struct machine *machine);
184
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200185static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200186 union perf_event *event,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400187 struct perf_sample *sample,
188 struct perf_evsel *evsel,
189 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300190{
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300191 if (evsel->handler) {
192 inject_handler f = evsel->handler;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400193 return f(tool, event, sample, evsel, machine);
194 }
195
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400196 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
197
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300198 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300199}
200
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200201static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200202 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200203 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200204 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500205{
206 int err;
207
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200208 err = perf_event__process_mmap(tool, event, sample, machine);
209 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500210
211 return err;
212}
213
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200214static int perf_event__repipe_mmap2(struct perf_tool *tool,
215 union perf_event *event,
216 struct perf_sample *sample,
217 struct machine *machine)
218{
219 int err;
220
221 err = perf_event__process_mmap2(tool, event, sample, machine);
222 perf_event__repipe(tool, event, sample, machine);
223
224 return err;
225}
226
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300227static int perf_event__repipe_fork(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200228 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200229 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200230 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500231{
232 int err;
233
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300234 err = perf_event__process_fork(tool, event, sample, machine);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200235 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500236
237 return err;
238}
239
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300240static int perf_event__repipe_comm(struct perf_tool *tool,
241 union perf_event *event,
242 struct perf_sample *sample,
243 struct machine *machine)
244{
245 int err;
246
247 err = perf_event__process_comm(tool, event, sample, machine);
248 perf_event__repipe(tool, event, sample, machine);
249
250 return err;
251}
252
253static int perf_event__repipe_exit(struct perf_tool *tool,
254 union perf_event *event,
255 struct perf_sample *sample,
256 struct machine *machine)
257{
258 int err;
259
260 err = perf_event__process_exit(tool, event, sample, machine);
261 perf_event__repipe(tool, event, sample, machine);
262
263 return err;
264}
265
Adrian Hunter47c3d102013-07-04 16:20:21 +0300266static int perf_event__repipe_tracing_data(struct perf_tool *tool,
267 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200268 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500269{
270 int err;
271
Adrian Hunter47c3d102013-07-04 16:20:21 +0300272 perf_event__repipe_synth(tool, event);
273 err = perf_event__process_tracing_data(tool, event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500274
275 return err;
276}
277
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300278static int perf_event__repipe_id_index(struct perf_tool *tool,
279 union perf_event *event,
280 struct perf_session *session)
281{
282 int err;
283
284 perf_event__repipe_synth(tool, event);
285 err = perf_event__process_id_index(tool, event, session);
286
287 return err;
288}
289
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300290static int dso__read_build_id(struct dso *dso)
Tom Zanussi454c4072010-05-01 01:41:20 -0500291{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300292 if (dso->has_build_id)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300293 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500294
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300295 if (filename__read_build_id(dso->long_name, dso->build_id,
296 sizeof(dso->build_id)) > 0) {
297 dso->has_build_id = true;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300298 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500299 }
300
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300301 return -1;
302}
Tom Zanussi454c4072010-05-01 01:41:20 -0500303
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300304static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200305 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300306{
307 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300308 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500309
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300310 if (dso__read_build_id(dso) < 0) {
311 pr_debug("no build_id found for %s\n", dso->long_name);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300312 return -1;
313 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500314
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300315 if (dso->kernel)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300316 misc = PERF_RECORD_MISC_KERNEL;
317
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300318 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200319 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300320 if (err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300321 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500322 return -1;
323 }
324
325 return 0;
326}
327
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200328static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200329 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200330 struct perf_sample *sample,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300331 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200332 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500333{
334 struct addr_location al;
335 struct thread *thread;
336 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500337
338 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
339
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900340 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500341 if (thread == NULL) {
342 pr_err("problem processing %d event, skipping it.\n",
343 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500344 goto repipe;
345 }
346
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300347 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500348
349 if (al.map != NULL) {
350 if (!al.map->dso->hit) {
351 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300352 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200353 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300354 /*
355 * If this fails, too bad, let the other side
356 * account this as unresolved.
357 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900358 } else {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200359#ifdef HAVE_LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500360 pr_warning("no symbols found in %s, maybe "
361 "install a debug package?\n",
362 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900363#endif
364 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500365 }
366 }
367
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300368 thread__put(thread);
Tom Zanussi454c4072010-05-01 01:41:20 -0500369repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200370 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300371 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500372}
373
Andrew Vagin26a031e2012-08-07 16:56:04 +0400374static int perf_inject__sched_process_exit(struct perf_tool *tool,
375 union perf_event *event __maybe_unused,
376 struct perf_sample *sample,
377 struct perf_evsel *evsel __maybe_unused,
378 struct machine *machine __maybe_unused)
379{
380 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
381 struct event_entry *ent;
382
383 list_for_each_entry(ent, &inject->samples, node) {
384 if (sample->tid == ent->tid) {
385 list_del_init(&ent->node);
386 free(ent);
387 break;
388 }
389 }
390
391 return 0;
392}
393
394static int perf_inject__sched_switch(struct perf_tool *tool,
395 union perf_event *event,
396 struct perf_sample *sample,
397 struct perf_evsel *evsel,
398 struct machine *machine)
399{
400 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
401 struct event_entry *ent;
402
403 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
404
405 ent = malloc(event->header.size + sizeof(struct event_entry));
406 if (ent == NULL) {
407 color_fprintf(stderr, PERF_COLOR_RED,
408 "Not enough memory to process sched switch event!");
409 return -1;
410 }
411
412 ent->tid = sample->tid;
413 memcpy(&ent->event, event, event->header.size);
414 list_add(&ent->node, &inject->samples);
415 return 0;
416}
417
418static int perf_inject__sched_stat(struct perf_tool *tool,
419 union perf_event *event __maybe_unused,
420 struct perf_sample *sample,
421 struct perf_evsel *evsel,
422 struct machine *machine)
423{
424 struct event_entry *ent;
425 union perf_event *event_sw;
426 struct perf_sample sample_sw;
427 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
428 u32 pid = perf_evsel__intval(evsel, sample, "pid");
429
430 list_for_each_entry(ent, &inject->samples, node) {
431 if (pid == ent->tid)
432 goto found;
433 }
434
435 return 0;
436found:
437 event_sw = &ent->event[0];
438 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
439
440 sample_sw.period = sample->period;
441 sample_sw.time = sample->time;
442 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
Adrian Hunterd03f2172013-08-27 11:23:11 +0300443 evsel->attr.read_format, &sample_sw,
444 false);
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400445 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400446 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
447}
448
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300449static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500450{
451 session_done = 1;
452}
453
Andrew Vagin26a031e2012-08-07 16:56:04 +0400454static int perf_evsel__check_stype(struct perf_evsel *evsel,
455 u64 sample_type, const char *sample_msg)
456{
457 struct perf_event_attr *attr = &evsel->attr;
458 const char *name = perf_evsel__name(evsel);
459
460 if (!(attr->sample_type & sample_type)) {
461 pr_err("Samples for %s event do not have %s attribute set.",
462 name, sample_msg);
463 return -EINVAL;
464 }
465
466 return 0;
467}
468
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300469static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500470{
Tom Zanussi454c4072010-05-01 01:41:20 -0500471 int ret = -EINVAL;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900472 struct perf_session *session = inject->session;
Jiri Olsa34069122013-10-29 19:04:57 +0100473 struct perf_data_file *file_out = &inject->output;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900474 int fd = perf_data_file__fd(file_out);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300475 u64 output_data_offset;
Tom Zanussi454c4072010-05-01 01:41:20 -0500476
477 signal(SIGINT, sig_handler);
478
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300479 if (inject->build_ids || inject->sched_stat ||
480 inject->itrace_synth_opts.set) {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300481 inject->tool.mmap = perf_event__repipe_mmap;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200482 inject->tool.mmap2 = perf_event__repipe_mmap2;
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300483 inject->tool.fork = perf_event__repipe_fork;
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300484 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500485 }
486
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300487 output_data_offset = session->header.data_offset;
488
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400489 if (inject->build_ids) {
490 inject->tool.sample = perf_event__inject_buildid;
491 } else if (inject->sched_stat) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400492 struct perf_evsel *evsel;
493
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300494 evlist__for_each(session->evlist, evsel) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400495 const char *name = perf_evsel__name(evsel);
496
497 if (!strcmp(name, "sched:sched_switch")) {
498 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
499 return -EINVAL;
500
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300501 evsel->handler = perf_inject__sched_switch;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400502 } else if (!strcmp(name, "sched:sched_process_exit"))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300503 evsel->handler = perf_inject__sched_process_exit;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400504 else if (!strncmp(name, "sched:sched_stat_", 17))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300505 evsel->handler = perf_inject__sched_stat;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400506 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300507 } else if (inject->itrace_synth_opts.set) {
508 session->itrace_synth_opts = &inject->itrace_synth_opts;
509 inject->itrace_synth_opts.inject = true;
510 inject->tool.comm = perf_event__repipe_comm;
511 inject->tool.exit = perf_event__repipe_exit;
512 inject->tool.id_index = perf_event__repipe_id_index;
513 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
514 inject->tool.auxtrace = perf_event__process_auxtrace;
515 inject->tool.ordered_events = true;
516 inject->tool.ordering_requires_timestamps = true;
517 /* Allow space in the header for new attributes */
518 output_data_offset = 4096;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400519 }
520
Adrian Hunter99fa2982015-04-30 17:37:25 +0300521 if (!inject->itrace_synth_opts.set)
522 auxtrace_index__free(&session->auxtrace_index);
523
Jiri Olsa34069122013-10-29 19:04:57 +0100524 if (!file_out->is_pipe)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300525 lseek(fd, output_data_offset, SEEK_SET);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400526
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300527 ret = perf_session__process_events(session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500528
Jiri Olsa34069122013-10-29 19:04:57 +0100529 if (!file_out->is_pipe) {
Adrian Huntercd10b282015-04-30 17:37:26 +0300530 if (inject->build_ids) {
Adrian Huntere38b43c2014-07-14 13:02:34 +0300531 perf_header__set_feat(&session->header,
532 HEADER_BUILD_ID);
Adrian Huntercd10b282015-04-30 17:37:26 +0300533 if (inject->have_auxtrace)
534 dsos__hit_all(session);
535 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300536 /*
537 * The AUX areas have been removed and replaced with
538 * synthesized hardware events, so clear the feature flag.
539 */
540 if (inject->itrace_synth_opts.set)
541 perf_header__clear_feat(&session->header,
542 HEADER_AUXTRACE);
543 session->header.data_offset = output_data_offset;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400544 session->header.data_size = inject->bytes_written;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900545 perf_session__write_header(session, session->evlist, fd, true);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400546 }
547
Tom Zanussi454c4072010-05-01 01:41:20 -0500548 return ret;
549}
550
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300551int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500552{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300553 struct perf_inject inject = {
554 .tool = {
555 .sample = perf_event__repipe_sample,
556 .mmap = perf_event__repipe,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200557 .mmap2 = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300558 .comm = perf_event__repipe,
559 .fork = perf_event__repipe,
560 .exit = perf_event__repipe,
561 .lost = perf_event__repipe,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300562 .aux = perf_event__repipe,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300563 .itrace_start = perf_event__repipe,
Adrian Hunter02860392015-07-21 12:44:03 +0300564 .context_switch = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300565 .read = perf_event__repipe_sample,
566 .throttle = perf_event__repipe,
567 .unthrottle = perf_event__repipe,
568 .attr = perf_event__repipe_attr,
Adrian Hunter47c3d102013-07-04 16:20:21 +0300569 .tracing_data = perf_event__repipe_op2_synth,
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300570 .auxtrace_info = perf_event__repipe_op2_synth,
571 .auxtrace = perf_event__repipe_auxtrace,
572 .auxtrace_error = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300573 .finished_round = perf_event__repipe_oe_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300574 .build_id = perf_event__repipe_op2_synth,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200575 .id_index = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300576 },
Andrew Vagine558a5b2012-08-07 16:56:02 +0400577 .input_name = "-",
Andrew Vagin26a031e2012-08-07 16:56:04 +0400578 .samples = LIST_HEAD_INIT(inject.samples),
Jiri Olsa34069122013-10-29 19:04:57 +0100579 .output = {
580 .path = "-",
581 .mode = PERF_DATA_MODE_WRITE,
582 },
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300583 };
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900584 struct perf_data_file file = {
585 .mode = PERF_DATA_MODE_READ,
586 };
587 int ret;
588
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300589 const struct option options[] = {
590 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
591 "Inject build-ids into the output stream"),
Andrew Vagine558a5b2012-08-07 16:56:02 +0400592 OPT_STRING('i', "input", &inject.input_name, "file",
593 "input file name"),
Jiri Olsa34069122013-10-29 19:04:57 +0100594 OPT_STRING('o', "output", &inject.output.path, "file",
Andrew Vagine558a5b2012-08-07 16:56:02 +0400595 "output file name"),
Andrew Vagin26a031e2012-08-07 16:56:04 +0400596 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
597 "Merge sched-stat and sched-switch for getting events "
598 "where and how long tasks slept"),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300599 OPT_INCR('v', "verbose", &verbose,
600 "be more verbose (show build ids, etc)"),
Adrian Huntera7a2b8b2014-07-22 16:17:38 +0300601 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
602 "kallsyms pathname"),
Yunlong Songccaa4742015-04-02 21:47:11 +0800603 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300604 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
605 NULL, "opts", "Instruction Tracing options",
606 itrace_parse_synth_opts),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300607 OPT_END()
608 };
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300609 const char * const inject_usage[] = {
610 "perf inject [<options>]",
611 NULL
612 };
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300613
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300614 argc = parse_options(argc, argv, options, inject_usage, 0);
Tom Zanussi454c4072010-05-01 01:41:20 -0500615
616 /*
617 * Any (unrecognized) arguments left?
618 */
619 if (argc)
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300620 usage_with_options(inject_usage, options);
Tom Zanussi454c4072010-05-01 01:41:20 -0500621
Jiri Olsa34069122013-10-29 19:04:57 +0100622 if (perf_data_file__open(&inject.output)) {
623 perror("failed to create output file");
624 return -1;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400625 }
626
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300627 inject.tool.ordered_events = inject.sched_stat;
628
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900629 file.path = inject.input_name;
630 inject.session = perf_session__new(&file, true, &inject.tool);
631 if (inject.session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900632 return -1;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900633
Taeung Song9fedfb02015-06-30 17:15:20 +0900634 ret = symbol__init(&inject.session->header.env);
635 if (ret < 0)
636 goto out_delete;
Tom Zanussi454c4072010-05-01 01:41:20 -0500637
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900638 ret = __cmd_inject(&inject);
639
Taeung Song9fedfb02015-06-30 17:15:20 +0900640out_delete:
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900641 perf_session__delete(inject.session);
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900642 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500643}