blob: 9b6119f134b4f365aba31bc6ab66b7f4fb3f7528 [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;
Adrian Hunter73117302015-09-25 16:15:54 +030034 u64 aux_id;
Jiri Olsa34069122013-10-29 19:04:57 +010035 struct list_head samples;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030036 struct itrace_synth_opts itrace_synth_opts;
Andrew Vagin26a031e2012-08-07 16:56:04 +040037};
38
39struct event_entry {
40 struct list_head node;
41 u32 tid;
42 union perf_event event[0];
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030043};
Tom Zanussi454c4072010-05-01 01:41:20 -050044
Adrian Huntercd17a9b2015-04-21 12:21:54 +030045static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
Tom Zanussi454c4072010-05-01 01:41:20 -050046{
Jiri Olsa34069122013-10-29 19:04:57 +010047 ssize_t size;
Tom Zanussi454c4072010-05-01 01:41:20 -050048
Adrian Huntercd17a9b2015-04-21 12:21:54 +030049 size = perf_data_file__write(&inject->output, buf, sz);
Jiri Olsa34069122013-10-29 19:04:57 +010050 if (size < 0)
51 return -errno;
Tom Zanussi454c4072010-05-01 01:41:20 -050052
Jiri Olsa34069122013-10-29 19:04:57 +010053 inject->bytes_written += size;
Tom Zanussi454c4072010-05-01 01:41:20 -050054 return 0;
55}
56
Adrian Huntercd17a9b2015-04-21 12:21:54 +030057static int perf_event__repipe_synth(struct perf_tool *tool,
58 union perf_event *event)
59{
60 struct perf_inject *inject = container_of(tool, struct perf_inject,
61 tool);
62
63 return output_bytes(inject, event, event->header.size);
64}
65
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030066static int perf_event__repipe_oe_synth(struct perf_tool *tool,
67 union perf_event *event,
68 struct ordered_events *oe __maybe_unused)
69{
70 return perf_event__repipe_synth(tool, event);
71}
72
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020073static int perf_event__repipe_op2_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020074 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030075 struct perf_session *session
76 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020077{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +030078 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020079}
80
Adrian Hunter47c3d102013-07-04 16:20:21 +030081static int perf_event__repipe_attr(struct perf_tool *tool,
82 union perf_event *event,
83 struct perf_evlist **pevlist)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020084{
Adrian Hunter89c97d92013-10-22 10:34:09 +030085 struct perf_inject *inject = container_of(tool, struct perf_inject,
86 tool);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020087 int ret;
Adrian Hunter47c3d102013-07-04 16:20:21 +030088
89 ret = perf_event__process_attr(tool, event, pevlist);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020090 if (ret)
91 return ret;
92
Jiri Olsaa261e4a2014-06-05 18:51:44 +020093 if (!inject->output.is_pipe)
Adrian Hunter89c97d92013-10-22 10:34:09 +030094 return 0;
95
Adrian Hunter47c3d102013-07-04 16:20:21 +030096 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020097}
98
Adrian Huntere31f0d02015-04-30 17:37:27 +030099#ifdef HAVE_AUXTRACE_SUPPORT
100
101static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
102{
103 char buf[4096];
104 ssize_t ssz;
105 int ret;
106
107 while (size > 0) {
108 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
109 if (ssz < 0)
110 return -errno;
111 ret = output_bytes(inject, buf, ssz);
112 if (ret)
113 return ret;
114 size -= ssz;
115 }
116
117 return 0;
118}
119
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300120static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
121 union perf_event *event,
122 struct perf_session *session
123 __maybe_unused)
124{
125 struct perf_inject *inject = container_of(tool, struct perf_inject,
126 tool);
127 int ret;
128
Adrian Huntercd10b282015-04-30 17:37:26 +0300129 inject->have_auxtrace = true;
130
Adrian Hunter99fa2982015-04-30 17:37:25 +0300131 if (!inject->output.is_pipe) {
132 off_t offset;
133
134 offset = lseek(inject->output.fd, 0, SEEK_CUR);
135 if (offset == -1)
136 return -errno;
137 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
138 event, offset);
139 if (ret < 0)
140 return ret;
141 }
142
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300143 if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
144 ret = output_bytes(inject, event, event->header.size);
145 if (ret < 0)
146 return ret;
147 ret = copy_bytes(inject, perf_data_file__fd(session->file),
148 event->auxtrace.size);
149 } else {
150 ret = output_bytes(inject, event,
151 event->header.size + event->auxtrace.size);
152 }
153 if (ret < 0)
154 return ret;
155
156 return event->auxtrace.size;
157}
158
Adrian Huntere31f0d02015-04-30 17:37:27 +0300159#else
160
161static s64
162perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
163 union perf_event *event __maybe_unused,
164 struct perf_session *session __maybe_unused)
165{
166 pr_err("AUX area tracing not supported\n");
167 return -EINVAL;
168}
169
170#endif
171
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200172static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200173 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300174 struct perf_sample *sample __maybe_unused,
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300175 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200176{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300177 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200178}
179
Adrian Hunter73117302015-09-25 16:15:54 +0300180static int perf_event__drop_aux(struct perf_tool *tool,
181 union perf_event *event __maybe_unused,
182 struct perf_sample *sample,
183 struct machine *machine __maybe_unused)
184{
185 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
186
187 if (!inject->aux_id)
188 inject->aux_id = sample->id;
189
190 return 0;
191}
192
Andrew Vagin26a031e2012-08-07 16:56:04 +0400193typedef int (*inject_handler)(struct perf_tool *tool,
194 union perf_event *event,
195 struct perf_sample *sample,
196 struct perf_evsel *evsel,
197 struct machine *machine);
198
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200199static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200200 union perf_event *event,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400201 struct perf_sample *sample,
202 struct perf_evsel *evsel,
203 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300204{
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300205 if (evsel->handler) {
206 inject_handler f = evsel->handler;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400207 return f(tool, event, sample, evsel, machine);
208 }
209
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400210 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
211
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300212 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300213}
214
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200215static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200216 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200217 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200218 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500219{
220 int err;
221
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200222 err = perf_event__process_mmap(tool, event, sample, machine);
223 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500224
225 return err;
226}
227
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200228static int perf_event__repipe_mmap2(struct perf_tool *tool,
229 union perf_event *event,
230 struct perf_sample *sample,
231 struct machine *machine)
232{
233 int err;
234
235 err = perf_event__process_mmap2(tool, event, sample, machine);
236 perf_event__repipe(tool, event, sample, machine);
237
238 return err;
239}
240
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300241static int perf_event__repipe_fork(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200242 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200243 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200244 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500245{
246 int err;
247
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300248 err = perf_event__process_fork(tool, event, sample, machine);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200249 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500250
251 return err;
252}
253
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300254static int perf_event__repipe_comm(struct perf_tool *tool,
255 union perf_event *event,
256 struct perf_sample *sample,
257 struct machine *machine)
258{
259 int err;
260
261 err = perf_event__process_comm(tool, event, sample, machine);
262 perf_event__repipe(tool, event, sample, machine);
263
264 return err;
265}
266
267static int perf_event__repipe_exit(struct perf_tool *tool,
268 union perf_event *event,
269 struct perf_sample *sample,
270 struct machine *machine)
271{
272 int err;
273
274 err = perf_event__process_exit(tool, event, sample, machine);
275 perf_event__repipe(tool, event, sample, machine);
276
277 return err;
278}
279
Adrian Hunter47c3d102013-07-04 16:20:21 +0300280static int perf_event__repipe_tracing_data(struct perf_tool *tool,
281 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200282 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500283{
284 int err;
285
Adrian Hunter47c3d102013-07-04 16:20:21 +0300286 perf_event__repipe_synth(tool, event);
287 err = perf_event__process_tracing_data(tool, event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500288
289 return err;
290}
291
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300292static int perf_event__repipe_id_index(struct perf_tool *tool,
293 union perf_event *event,
294 struct perf_session *session)
295{
296 int err;
297
298 perf_event__repipe_synth(tool, event);
299 err = perf_event__process_id_index(tool, event, session);
300
301 return err;
302}
303
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300304static int dso__read_build_id(struct dso *dso)
Tom Zanussi454c4072010-05-01 01:41:20 -0500305{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300306 if (dso->has_build_id)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300307 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500308
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300309 if (filename__read_build_id(dso->long_name, dso->build_id,
310 sizeof(dso->build_id)) > 0) {
311 dso->has_build_id = true;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300312 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500313 }
314
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300315 return -1;
316}
Tom Zanussi454c4072010-05-01 01:41:20 -0500317
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300318static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200319 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300320{
321 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300322 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500323
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300324 if (dso__read_build_id(dso) < 0) {
325 pr_debug("no build_id found for %s\n", dso->long_name);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300326 return -1;
327 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500328
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300329 if (dso->kernel)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300330 misc = PERF_RECORD_MISC_KERNEL;
331
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300332 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200333 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300334 if (err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300335 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500336 return -1;
337 }
338
339 return 0;
340}
341
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200342static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200343 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200344 struct perf_sample *sample,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300345 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200346 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500347{
348 struct addr_location al;
349 struct thread *thread;
350 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500351
352 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
353
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900354 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500355 if (thread == NULL) {
356 pr_err("problem processing %d event, skipping it.\n",
357 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500358 goto repipe;
359 }
360
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300361 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500362
363 if (al.map != NULL) {
364 if (!al.map->dso->hit) {
365 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300366 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200367 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300368 /*
369 * If this fails, too bad, let the other side
370 * account this as unresolved.
371 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900372 } else {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200373#ifdef HAVE_LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500374 pr_warning("no symbols found in %s, maybe "
375 "install a debug package?\n",
376 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900377#endif
378 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500379 }
380 }
381
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300382 thread__put(thread);
Tom Zanussi454c4072010-05-01 01:41:20 -0500383repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200384 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300385 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500386}
387
Andrew Vagin26a031e2012-08-07 16:56:04 +0400388static int perf_inject__sched_process_exit(struct perf_tool *tool,
389 union perf_event *event __maybe_unused,
390 struct perf_sample *sample,
391 struct perf_evsel *evsel __maybe_unused,
392 struct machine *machine __maybe_unused)
393{
394 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
395 struct event_entry *ent;
396
397 list_for_each_entry(ent, &inject->samples, node) {
398 if (sample->tid == ent->tid) {
399 list_del_init(&ent->node);
400 free(ent);
401 break;
402 }
403 }
404
405 return 0;
406}
407
408static int perf_inject__sched_switch(struct perf_tool *tool,
409 union perf_event *event,
410 struct perf_sample *sample,
411 struct perf_evsel *evsel,
412 struct machine *machine)
413{
414 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
415 struct event_entry *ent;
416
417 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
418
419 ent = malloc(event->header.size + sizeof(struct event_entry));
420 if (ent == NULL) {
421 color_fprintf(stderr, PERF_COLOR_RED,
422 "Not enough memory to process sched switch event!");
423 return -1;
424 }
425
426 ent->tid = sample->tid;
427 memcpy(&ent->event, event, event->header.size);
428 list_add(&ent->node, &inject->samples);
429 return 0;
430}
431
432static int perf_inject__sched_stat(struct perf_tool *tool,
433 union perf_event *event __maybe_unused,
434 struct perf_sample *sample,
435 struct perf_evsel *evsel,
436 struct machine *machine)
437{
438 struct event_entry *ent;
439 union perf_event *event_sw;
440 struct perf_sample sample_sw;
441 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
442 u32 pid = perf_evsel__intval(evsel, sample, "pid");
443
444 list_for_each_entry(ent, &inject->samples, node) {
445 if (pid == ent->tid)
446 goto found;
447 }
448
449 return 0;
450found:
451 event_sw = &ent->event[0];
452 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
453
454 sample_sw.period = sample->period;
455 sample_sw.time = sample->time;
456 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
Adrian Hunterd03f2172013-08-27 11:23:11 +0300457 evsel->attr.read_format, &sample_sw,
458 false);
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400459 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400460 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
461}
462
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300463static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500464{
465 session_done = 1;
466}
467
Andrew Vagin26a031e2012-08-07 16:56:04 +0400468static int perf_evsel__check_stype(struct perf_evsel *evsel,
469 u64 sample_type, const char *sample_msg)
470{
471 struct perf_event_attr *attr = &evsel->attr;
472 const char *name = perf_evsel__name(evsel);
473
474 if (!(attr->sample_type & sample_type)) {
475 pr_err("Samples for %s event do not have %s attribute set.",
476 name, sample_msg);
477 return -EINVAL;
478 }
479
480 return 0;
481}
482
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300483static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500484{
Tom Zanussi454c4072010-05-01 01:41:20 -0500485 int ret = -EINVAL;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900486 struct perf_session *session = inject->session;
Jiri Olsa34069122013-10-29 19:04:57 +0100487 struct perf_data_file *file_out = &inject->output;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900488 int fd = perf_data_file__fd(file_out);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300489 u64 output_data_offset;
Tom Zanussi454c4072010-05-01 01:41:20 -0500490
491 signal(SIGINT, sig_handler);
492
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300493 if (inject->build_ids || inject->sched_stat ||
494 inject->itrace_synth_opts.set) {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300495 inject->tool.mmap = perf_event__repipe_mmap;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200496 inject->tool.mmap2 = perf_event__repipe_mmap2;
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300497 inject->tool.fork = perf_event__repipe_fork;
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300498 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500499 }
500
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300501 output_data_offset = session->header.data_offset;
502
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400503 if (inject->build_ids) {
504 inject->tool.sample = perf_event__inject_buildid;
505 } else if (inject->sched_stat) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400506 struct perf_evsel *evsel;
507
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300508 evlist__for_each(session->evlist, evsel) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400509 const char *name = perf_evsel__name(evsel);
510
511 if (!strcmp(name, "sched:sched_switch")) {
512 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
513 return -EINVAL;
514
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300515 evsel->handler = perf_inject__sched_switch;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400516 } else if (!strcmp(name, "sched:sched_process_exit"))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300517 evsel->handler = perf_inject__sched_process_exit;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400518 else if (!strncmp(name, "sched:sched_stat_", 17))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300519 evsel->handler = perf_inject__sched_stat;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400520 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300521 } else if (inject->itrace_synth_opts.set) {
522 session->itrace_synth_opts = &inject->itrace_synth_opts;
523 inject->itrace_synth_opts.inject = true;
524 inject->tool.comm = perf_event__repipe_comm;
525 inject->tool.exit = perf_event__repipe_exit;
526 inject->tool.id_index = perf_event__repipe_id_index;
527 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
528 inject->tool.auxtrace = perf_event__process_auxtrace;
Adrian Hunter73117302015-09-25 16:15:54 +0300529 inject->tool.aux = perf_event__drop_aux;
530 inject->tool.itrace_start = perf_event__drop_aux,
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300531 inject->tool.ordered_events = true;
532 inject->tool.ordering_requires_timestamps = true;
533 /* Allow space in the header for new attributes */
534 output_data_offset = 4096;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400535 }
536
Adrian Hunter99fa2982015-04-30 17:37:25 +0300537 if (!inject->itrace_synth_opts.set)
538 auxtrace_index__free(&session->auxtrace_index);
539
Jiri Olsa34069122013-10-29 19:04:57 +0100540 if (!file_out->is_pipe)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300541 lseek(fd, output_data_offset, SEEK_SET);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400542
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300543 ret = perf_session__process_events(session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500544
Jiri Olsa34069122013-10-29 19:04:57 +0100545 if (!file_out->is_pipe) {
Adrian Huntercd10b282015-04-30 17:37:26 +0300546 if (inject->build_ids) {
Adrian Huntere38b43c2014-07-14 13:02:34 +0300547 perf_header__set_feat(&session->header,
548 HEADER_BUILD_ID);
Adrian Huntercd10b282015-04-30 17:37:26 +0300549 if (inject->have_auxtrace)
550 dsos__hit_all(session);
551 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300552 /*
553 * The AUX areas have been removed and replaced with
Adrian Hunter73117302015-09-25 16:15:54 +0300554 * synthesized hardware events, so clear the feature flag and
555 * remove the evsel.
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300556 */
Adrian Hunter051a01b2015-09-25 16:15:43 +0300557 if (inject->itrace_synth_opts.set) {
Adrian Hunter73117302015-09-25 16:15:54 +0300558 struct perf_evsel *evsel;
559
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300560 perf_header__clear_feat(&session->header,
561 HEADER_AUXTRACE);
Adrian Hunter051a01b2015-09-25 16:15:43 +0300562 if (inject->itrace_synth_opts.last_branch)
563 perf_header__set_feat(&session->header,
564 HEADER_BRANCH_STACK);
Adrian Hunter73117302015-09-25 16:15:54 +0300565 evsel = perf_evlist__id2evsel_strict(session->evlist,
566 inject->aux_id);
567 if (evsel) {
568 pr_debug("Deleting %s\n",
569 perf_evsel__name(evsel));
570 perf_evlist__remove(session->evlist, evsel);
571 perf_evsel__delete(evsel);
572 }
Adrian Hunter051a01b2015-09-25 16:15:43 +0300573 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300574 session->header.data_offset = output_data_offset;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400575 session->header.data_size = inject->bytes_written;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900576 perf_session__write_header(session, session->evlist, fd, true);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400577 }
578
Tom Zanussi454c4072010-05-01 01:41:20 -0500579 return ret;
580}
581
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300582int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500583{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300584 struct perf_inject inject = {
585 .tool = {
586 .sample = perf_event__repipe_sample,
587 .mmap = perf_event__repipe,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200588 .mmap2 = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300589 .comm = perf_event__repipe,
590 .fork = perf_event__repipe,
591 .exit = perf_event__repipe,
592 .lost = perf_event__repipe,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300593 .aux = perf_event__repipe,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300594 .itrace_start = perf_event__repipe,
Adrian Hunter02860392015-07-21 12:44:03 +0300595 .context_switch = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300596 .read = perf_event__repipe_sample,
597 .throttle = perf_event__repipe,
598 .unthrottle = perf_event__repipe,
599 .attr = perf_event__repipe_attr,
Adrian Hunter47c3d102013-07-04 16:20:21 +0300600 .tracing_data = perf_event__repipe_op2_synth,
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300601 .auxtrace_info = perf_event__repipe_op2_synth,
602 .auxtrace = perf_event__repipe_auxtrace,
603 .auxtrace_error = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300604 .finished_round = perf_event__repipe_oe_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300605 .build_id = perf_event__repipe_op2_synth,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200606 .id_index = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300607 },
Andrew Vagine558a5b2012-08-07 16:56:02 +0400608 .input_name = "-",
Andrew Vagin26a031e2012-08-07 16:56:04 +0400609 .samples = LIST_HEAD_INIT(inject.samples),
Jiri Olsa34069122013-10-29 19:04:57 +0100610 .output = {
611 .path = "-",
612 .mode = PERF_DATA_MODE_WRITE,
613 },
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300614 };
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900615 struct perf_data_file file = {
616 .mode = PERF_DATA_MODE_READ,
617 };
618 int ret;
619
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300620 const struct option options[] = {
621 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
622 "Inject build-ids into the output stream"),
Andrew Vagine558a5b2012-08-07 16:56:02 +0400623 OPT_STRING('i', "input", &inject.input_name, "file",
624 "input file name"),
Jiri Olsa34069122013-10-29 19:04:57 +0100625 OPT_STRING('o', "output", &inject.output.path, "file",
Andrew Vagine558a5b2012-08-07 16:56:02 +0400626 "output file name"),
Andrew Vagin26a031e2012-08-07 16:56:04 +0400627 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
628 "Merge sched-stat and sched-switch for getting events "
629 "where and how long tasks slept"),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300630 OPT_INCR('v', "verbose", &verbose,
631 "be more verbose (show build ids, etc)"),
Adrian Huntera7a2b8b2014-07-22 16:17:38 +0300632 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
633 "kallsyms pathname"),
Yunlong Songccaa4742015-04-02 21:47:11 +0800634 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300635 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
636 NULL, "opts", "Instruction Tracing options",
637 itrace_parse_synth_opts),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300638 OPT_END()
639 };
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300640 const char * const inject_usage[] = {
641 "perf inject [<options>]",
642 NULL
643 };
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300644
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300645 argc = parse_options(argc, argv, options, inject_usage, 0);
Tom Zanussi454c4072010-05-01 01:41:20 -0500646
647 /*
648 * Any (unrecognized) arguments left?
649 */
650 if (argc)
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300651 usage_with_options(inject_usage, options);
Tom Zanussi454c4072010-05-01 01:41:20 -0500652
Jiri Olsa34069122013-10-29 19:04:57 +0100653 if (perf_data_file__open(&inject.output)) {
654 perror("failed to create output file");
655 return -1;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400656 }
657
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300658 inject.tool.ordered_events = inject.sched_stat;
659
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900660 file.path = inject.input_name;
661 inject.session = perf_session__new(&file, true, &inject.tool);
662 if (inject.session == NULL)
Taeung Song52e02832014-09-24 10:33:37 +0900663 return -1;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900664
Taeung Song9fedfb02015-06-30 17:15:20 +0900665 ret = symbol__init(&inject.session->header.env);
666 if (ret < 0)
667 goto out_delete;
Tom Zanussi454c4072010-05-01 01:41:20 -0500668
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900669 ret = __cmd_inject(&inject);
670
Taeung Song9fedfb02015-06-30 17:15:20 +0900671out_delete:
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900672 perf_session__delete(inject.session);
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900673 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500674}