blob: 0022e02ed31a7034b9286884ab87c4c131e41fa3 [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
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060021#include <subcmd/parse-options.h>
Tom Zanussi454c4072010-05-01 01:41:20 -050022
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;
Adrian Hunterf56fb982015-09-25 16:15:55 +030031 bool strip;
Jiri Olsa34069122013-10-29 19:04:57 +010032 const char *input_name;
33 struct perf_data_file output;
34 u64 bytes_written;
Adrian Hunter73117302015-09-25 16:15:54 +030035 u64 aux_id;
Jiri Olsa34069122013-10-29 19:04:57 +010036 struct list_head samples;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030037 struct itrace_synth_opts itrace_synth_opts;
Andrew Vagin26a031e2012-08-07 16:56:04 +040038};
39
40struct event_entry {
41 struct list_head node;
42 u32 tid;
43 union perf_event event[0];
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030044};
Tom Zanussi454c4072010-05-01 01:41:20 -050045
Adrian Huntercd17a9b2015-04-21 12:21:54 +030046static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
Tom Zanussi454c4072010-05-01 01:41:20 -050047{
Jiri Olsa34069122013-10-29 19:04:57 +010048 ssize_t size;
Tom Zanussi454c4072010-05-01 01:41:20 -050049
Adrian Huntercd17a9b2015-04-21 12:21:54 +030050 size = perf_data_file__write(&inject->output, buf, sz);
Jiri Olsa34069122013-10-29 19:04:57 +010051 if (size < 0)
52 return -errno;
Tom Zanussi454c4072010-05-01 01:41:20 -050053
Jiri Olsa34069122013-10-29 19:04:57 +010054 inject->bytes_written += size;
Tom Zanussi454c4072010-05-01 01:41:20 -050055 return 0;
56}
57
Adrian Huntercd17a9b2015-04-21 12:21:54 +030058static int perf_event__repipe_synth(struct perf_tool *tool,
59 union perf_event *event)
60{
61 struct perf_inject *inject = container_of(tool, struct perf_inject,
62 tool);
63
64 return output_bytes(inject, event, event->header.size);
65}
66
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030067static int perf_event__repipe_oe_synth(struct perf_tool *tool,
68 union perf_event *event,
69 struct ordered_events *oe __maybe_unused)
70{
71 return perf_event__repipe_synth(tool, event);
72}
73
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020074static int perf_event__repipe_op2_synth(struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020075 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030076 struct perf_session *session
77 __maybe_unused)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020078{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +030079 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020080}
81
Adrian Hunter47c3d102013-07-04 16:20:21 +030082static int perf_event__repipe_attr(struct perf_tool *tool,
83 union perf_event *event,
84 struct perf_evlist **pevlist)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020085{
Adrian Hunter89c97d92013-10-22 10:34:09 +030086 struct perf_inject *inject = container_of(tool, struct perf_inject,
87 tool);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020088 int ret;
Adrian Hunter47c3d102013-07-04 16:20:21 +030089
90 ret = perf_event__process_attr(tool, event, pevlist);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +020091 if (ret)
92 return ret;
93
Jiri Olsaa261e4a2014-06-05 18:51:44 +020094 if (!inject->output.is_pipe)
Adrian Hunter89c97d92013-10-22 10:34:09 +030095 return 0;
96
Adrian Hunter47c3d102013-07-04 16:20:21 +030097 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -020098}
99
Adrian Huntere31f0d02015-04-30 17:37:27 +0300100#ifdef HAVE_AUXTRACE_SUPPORT
101
102static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
103{
104 char buf[4096];
105 ssize_t ssz;
106 int ret;
107
108 while (size > 0) {
109 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
110 if (ssz < 0)
111 return -errno;
112 ret = output_bytes(inject, buf, ssz);
113 if (ret)
114 return ret;
115 size -= ssz;
116 }
117
118 return 0;
119}
120
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300121static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
122 union perf_event *event,
123 struct perf_session *session
124 __maybe_unused)
125{
126 struct perf_inject *inject = container_of(tool, struct perf_inject,
127 tool);
128 int ret;
129
Adrian Huntercd10b282015-04-30 17:37:26 +0300130 inject->have_auxtrace = true;
131
Adrian Hunter99fa2982015-04-30 17:37:25 +0300132 if (!inject->output.is_pipe) {
133 off_t offset;
134
135 offset = lseek(inject->output.fd, 0, SEEK_CUR);
136 if (offset == -1)
137 return -errno;
138 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
139 event, offset);
140 if (ret < 0)
141 return ret;
142 }
143
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300144 if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
145 ret = output_bytes(inject, event, event->header.size);
146 if (ret < 0)
147 return ret;
148 ret = copy_bytes(inject, perf_data_file__fd(session->file),
149 event->auxtrace.size);
150 } else {
151 ret = output_bytes(inject, event,
152 event->header.size + event->auxtrace.size);
153 }
154 if (ret < 0)
155 return ret;
156
157 return event->auxtrace.size;
158}
159
Adrian Huntere31f0d02015-04-30 17:37:27 +0300160#else
161
162static s64
163perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
164 union perf_event *event __maybe_unused,
165 struct perf_session *session __maybe_unused)
166{
167 pr_err("AUX area tracing not supported\n");
168 return -EINVAL;
169}
170
171#endif
172
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200173static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200174 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300175 struct perf_sample *sample __maybe_unused,
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300176 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200177{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300178 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200179}
180
Adrian Hunterf56fb982015-09-25 16:15:55 +0300181static int perf_event__drop(struct perf_tool *tool __maybe_unused,
182 union perf_event *event __maybe_unused,
183 struct perf_sample *sample __maybe_unused,
184 struct machine *machine __maybe_unused)
185{
186 return 0;
187}
188
Adrian Hunter73117302015-09-25 16:15:54 +0300189static int perf_event__drop_aux(struct perf_tool *tool,
190 union perf_event *event __maybe_unused,
191 struct perf_sample *sample,
192 struct machine *machine __maybe_unused)
193{
194 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
195
196 if (!inject->aux_id)
197 inject->aux_id = sample->id;
198
199 return 0;
200}
201
Andrew Vagin26a031e2012-08-07 16:56:04 +0400202typedef int (*inject_handler)(struct perf_tool *tool,
203 union perf_event *event,
204 struct perf_sample *sample,
205 struct perf_evsel *evsel,
206 struct machine *machine);
207
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200208static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200209 union perf_event *event,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400210 struct perf_sample *sample,
211 struct perf_evsel *evsel,
212 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300213{
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300214 if (evsel->handler) {
215 inject_handler f = evsel->handler;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400216 return f(tool, event, sample, evsel, machine);
217 }
218
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400219 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
220
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300221 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300222}
223
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200224static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200225 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200226 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200227 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500228{
229 int err;
230
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200231 err = perf_event__process_mmap(tool, event, sample, machine);
232 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500233
234 return err;
235}
236
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200237static int perf_event__repipe_mmap2(struct perf_tool *tool,
238 union perf_event *event,
239 struct perf_sample *sample,
240 struct machine *machine)
241{
242 int err;
243
244 err = perf_event__process_mmap2(tool, event, sample, machine);
245 perf_event__repipe(tool, event, sample, machine);
246
247 return err;
248}
249
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300250static int perf_event__repipe_fork(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200251 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200252 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200253 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500254{
255 int err;
256
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300257 err = perf_event__process_fork(tool, event, sample, machine);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200258 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500259
260 return err;
261}
262
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300263static int perf_event__repipe_comm(struct perf_tool *tool,
264 union perf_event *event,
265 struct perf_sample *sample,
266 struct machine *machine)
267{
268 int err;
269
270 err = perf_event__process_comm(tool, event, sample, machine);
271 perf_event__repipe(tool, event, sample, machine);
272
273 return err;
274}
275
276static int perf_event__repipe_exit(struct perf_tool *tool,
277 union perf_event *event,
278 struct perf_sample *sample,
279 struct machine *machine)
280{
281 int err;
282
283 err = perf_event__process_exit(tool, event, sample, machine);
284 perf_event__repipe(tool, event, sample, machine);
285
286 return err;
287}
288
Adrian Hunter47c3d102013-07-04 16:20:21 +0300289static int perf_event__repipe_tracing_data(struct perf_tool *tool,
290 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200291 struct perf_session *session)
Tom Zanussi454c4072010-05-01 01:41:20 -0500292{
293 int err;
294
Adrian Hunter47c3d102013-07-04 16:20:21 +0300295 perf_event__repipe_synth(tool, event);
296 err = perf_event__process_tracing_data(tool, event, session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500297
298 return err;
299}
300
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300301static int perf_event__repipe_id_index(struct perf_tool *tool,
302 union perf_event *event,
303 struct perf_session *session)
304{
305 int err;
306
307 perf_event__repipe_synth(tool, event);
308 err = perf_event__process_id_index(tool, event, session);
309
310 return err;
311}
312
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300313static int dso__read_build_id(struct dso *dso)
Tom Zanussi454c4072010-05-01 01:41:20 -0500314{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300315 if (dso->has_build_id)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300316 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500317
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300318 if (filename__read_build_id(dso->long_name, dso->build_id,
319 sizeof(dso->build_id)) > 0) {
320 dso->has_build_id = true;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300321 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500322 }
323
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300324 return -1;
325}
Tom Zanussi454c4072010-05-01 01:41:20 -0500326
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300327static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200328 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300329{
330 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300331 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500332
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300333 if (dso__read_build_id(dso) < 0) {
334 pr_debug("no build_id found for %s\n", dso->long_name);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300335 return -1;
336 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500337
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300338 if (dso->kernel)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300339 misc = PERF_RECORD_MISC_KERNEL;
340
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300341 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200342 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300343 if (err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300344 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500345 return -1;
346 }
347
348 return 0;
349}
350
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200351static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200352 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200353 struct perf_sample *sample,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300354 struct perf_evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200355 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500356{
357 struct addr_location al;
358 struct thread *thread;
359 u8 cpumode;
Tom Zanussi454c4072010-05-01 01:41:20 -0500360
361 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
362
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900363 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500364 if (thread == NULL) {
365 pr_err("problem processing %d event, skipping it.\n",
366 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500367 goto repipe;
368 }
369
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300370 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
Tom Zanussi454c4072010-05-01 01:41:20 -0500371
372 if (al.map != NULL) {
373 if (!al.map->dso->hit) {
374 al.map->dso->hit = 1;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300375 if (map__load(al.map, NULL) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200376 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300377 /*
378 * If this fails, too bad, let the other side
379 * account this as unresolved.
380 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900381 } else {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200382#ifdef HAVE_LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500383 pr_warning("no symbols found in %s, maybe "
384 "install a debug package?\n",
385 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900386#endif
387 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500388 }
389 }
390
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300391 thread__put(thread);
Tom Zanussi454c4072010-05-01 01:41:20 -0500392repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200393 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300394 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500395}
396
Andrew Vagin26a031e2012-08-07 16:56:04 +0400397static int perf_inject__sched_process_exit(struct perf_tool *tool,
398 union perf_event *event __maybe_unused,
399 struct perf_sample *sample,
400 struct perf_evsel *evsel __maybe_unused,
401 struct machine *machine __maybe_unused)
402{
403 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
404 struct event_entry *ent;
405
406 list_for_each_entry(ent, &inject->samples, node) {
407 if (sample->tid == ent->tid) {
408 list_del_init(&ent->node);
409 free(ent);
410 break;
411 }
412 }
413
414 return 0;
415}
416
417static int perf_inject__sched_switch(struct perf_tool *tool,
418 union perf_event *event,
419 struct perf_sample *sample,
420 struct perf_evsel *evsel,
421 struct machine *machine)
422{
423 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
424 struct event_entry *ent;
425
426 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
427
428 ent = malloc(event->header.size + sizeof(struct event_entry));
429 if (ent == NULL) {
430 color_fprintf(stderr, PERF_COLOR_RED,
431 "Not enough memory to process sched switch event!");
432 return -1;
433 }
434
435 ent->tid = sample->tid;
436 memcpy(&ent->event, event, event->header.size);
437 list_add(&ent->node, &inject->samples);
438 return 0;
439}
440
441static int perf_inject__sched_stat(struct perf_tool *tool,
442 union perf_event *event __maybe_unused,
443 struct perf_sample *sample,
444 struct perf_evsel *evsel,
445 struct machine *machine)
446{
447 struct event_entry *ent;
448 union perf_event *event_sw;
449 struct perf_sample sample_sw;
450 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
451 u32 pid = perf_evsel__intval(evsel, sample, "pid");
452
453 list_for_each_entry(ent, &inject->samples, node) {
454 if (pid == ent->tid)
455 goto found;
456 }
457
458 return 0;
459found:
460 event_sw = &ent->event[0];
461 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
462
463 sample_sw.period = sample->period;
464 sample_sw.time = sample->time;
465 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
Adrian Hunterd03f2172013-08-27 11:23:11 +0300466 evsel->attr.read_format, &sample_sw,
467 false);
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400468 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400469 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
470}
471
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300472static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500473{
474 session_done = 1;
475}
476
Andrew Vagin26a031e2012-08-07 16:56:04 +0400477static int perf_evsel__check_stype(struct perf_evsel *evsel,
478 u64 sample_type, const char *sample_msg)
479{
480 struct perf_event_attr *attr = &evsel->attr;
481 const char *name = perf_evsel__name(evsel);
482
483 if (!(attr->sample_type & sample_type)) {
484 pr_err("Samples for %s event do not have %s attribute set.",
485 name, sample_msg);
486 return -EINVAL;
487 }
488
489 return 0;
490}
491
Adrian Hunterf56fb982015-09-25 16:15:55 +0300492static int drop_sample(struct perf_tool *tool __maybe_unused,
493 union perf_event *event __maybe_unused,
494 struct perf_sample *sample __maybe_unused,
495 struct perf_evsel *evsel __maybe_unused,
496 struct machine *machine __maybe_unused)
497{
498 return 0;
499}
500
501static void strip_init(struct perf_inject *inject)
502{
503 struct perf_evlist *evlist = inject->session->evlist;
504 struct perf_evsel *evsel;
505
506 inject->tool.context_switch = perf_event__drop;
507
508 evlist__for_each(evlist, evsel)
509 evsel->handler = drop_sample;
510}
511
512static bool has_tracking(struct perf_evsel *evsel)
513{
514 return evsel->attr.mmap || evsel->attr.mmap2 || evsel->attr.comm ||
515 evsel->attr.task;
516}
517
518#define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
519 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
520
521/*
522 * In order that the perf.data file is parsable, tracking events like MMAP need
523 * their selected event to exist, except if there is only 1 selected event left
524 * and it has a compatible sample type.
525 */
526static bool ok_to_remove(struct perf_evlist *evlist,
527 struct perf_evsel *evsel_to_remove)
528{
529 struct perf_evsel *evsel;
530 int cnt = 0;
531 bool ok = false;
532
533 if (!has_tracking(evsel_to_remove))
534 return true;
535
536 evlist__for_each(evlist, evsel) {
537 if (evsel->handler != drop_sample) {
538 cnt += 1;
539 if ((evsel->attr.sample_type & COMPAT_MASK) ==
540 (evsel_to_remove->attr.sample_type & COMPAT_MASK))
541 ok = true;
542 }
543 }
544
545 return ok && cnt == 1;
546}
547
548static void strip_fini(struct perf_inject *inject)
549{
550 struct perf_evlist *evlist = inject->session->evlist;
551 struct perf_evsel *evsel, *tmp;
552
553 /* Remove non-synthesized evsels if possible */
554 evlist__for_each_safe(evlist, tmp, evsel) {
555 if (evsel->handler == drop_sample &&
556 ok_to_remove(evlist, evsel)) {
557 pr_debug("Deleting %s\n", perf_evsel__name(evsel));
558 perf_evlist__remove(evlist, evsel);
559 perf_evsel__delete(evsel);
560 }
561 }
562}
563
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300564static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500565{
Tom Zanussi454c4072010-05-01 01:41:20 -0500566 int ret = -EINVAL;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900567 struct perf_session *session = inject->session;
Jiri Olsa34069122013-10-29 19:04:57 +0100568 struct perf_data_file *file_out = &inject->output;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900569 int fd = perf_data_file__fd(file_out);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300570 u64 output_data_offset;
Tom Zanussi454c4072010-05-01 01:41:20 -0500571
572 signal(SIGINT, sig_handler);
573
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300574 if (inject->build_ids || inject->sched_stat ||
575 inject->itrace_synth_opts.set) {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300576 inject->tool.mmap = perf_event__repipe_mmap;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200577 inject->tool.mmap2 = perf_event__repipe_mmap2;
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300578 inject->tool.fork = perf_event__repipe_fork;
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300579 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500580 }
581
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300582 output_data_offset = session->header.data_offset;
583
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400584 if (inject->build_ids) {
585 inject->tool.sample = perf_event__inject_buildid;
586 } else if (inject->sched_stat) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400587 struct perf_evsel *evsel;
588
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300589 evlist__for_each(session->evlist, evsel) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400590 const char *name = perf_evsel__name(evsel);
591
592 if (!strcmp(name, "sched:sched_switch")) {
593 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
594 return -EINVAL;
595
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300596 evsel->handler = perf_inject__sched_switch;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400597 } else if (!strcmp(name, "sched:sched_process_exit"))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300598 evsel->handler = perf_inject__sched_process_exit;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400599 else if (!strncmp(name, "sched:sched_stat_", 17))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300600 evsel->handler = perf_inject__sched_stat;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400601 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300602 } else if (inject->itrace_synth_opts.set) {
603 session->itrace_synth_opts = &inject->itrace_synth_opts;
604 inject->itrace_synth_opts.inject = true;
605 inject->tool.comm = perf_event__repipe_comm;
606 inject->tool.exit = perf_event__repipe_exit;
607 inject->tool.id_index = perf_event__repipe_id_index;
608 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
609 inject->tool.auxtrace = perf_event__process_auxtrace;
Adrian Hunter73117302015-09-25 16:15:54 +0300610 inject->tool.aux = perf_event__drop_aux;
611 inject->tool.itrace_start = perf_event__drop_aux,
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300612 inject->tool.ordered_events = true;
613 inject->tool.ordering_requires_timestamps = true;
614 /* Allow space in the header for new attributes */
615 output_data_offset = 4096;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300616 if (inject->strip)
617 strip_init(inject);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400618 }
619
Adrian Hunter99fa2982015-04-30 17:37:25 +0300620 if (!inject->itrace_synth_opts.set)
621 auxtrace_index__free(&session->auxtrace_index);
622
Jiri Olsa34069122013-10-29 19:04:57 +0100623 if (!file_out->is_pipe)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300624 lseek(fd, output_data_offset, SEEK_SET);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400625
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300626 ret = perf_session__process_events(session);
Tom Zanussi454c4072010-05-01 01:41:20 -0500627
Jiri Olsa34069122013-10-29 19:04:57 +0100628 if (!file_out->is_pipe) {
Adrian Huntercd10b282015-04-30 17:37:26 +0300629 if (inject->build_ids) {
Adrian Huntere38b43c2014-07-14 13:02:34 +0300630 perf_header__set_feat(&session->header,
631 HEADER_BUILD_ID);
Adrian Huntercd10b282015-04-30 17:37:26 +0300632 if (inject->have_auxtrace)
633 dsos__hit_all(session);
634 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300635 /*
636 * The AUX areas have been removed and replaced with
Adrian Hunter73117302015-09-25 16:15:54 +0300637 * synthesized hardware events, so clear the feature flag and
638 * remove the evsel.
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300639 */
Adrian Hunter051a01b2015-09-25 16:15:43 +0300640 if (inject->itrace_synth_opts.set) {
Adrian Hunter73117302015-09-25 16:15:54 +0300641 struct perf_evsel *evsel;
642
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300643 perf_header__clear_feat(&session->header,
644 HEADER_AUXTRACE);
Adrian Hunter051a01b2015-09-25 16:15:43 +0300645 if (inject->itrace_synth_opts.last_branch)
646 perf_header__set_feat(&session->header,
647 HEADER_BRANCH_STACK);
Adrian Hunter73117302015-09-25 16:15:54 +0300648 evsel = perf_evlist__id2evsel_strict(session->evlist,
649 inject->aux_id);
650 if (evsel) {
651 pr_debug("Deleting %s\n",
652 perf_evsel__name(evsel));
653 perf_evlist__remove(session->evlist, evsel);
654 perf_evsel__delete(evsel);
655 }
Adrian Hunterf56fb982015-09-25 16:15:55 +0300656 if (inject->strip)
657 strip_fini(inject);
Adrian Hunter051a01b2015-09-25 16:15:43 +0300658 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300659 session->header.data_offset = output_data_offset;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400660 session->header.data_size = inject->bytes_written;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900661 perf_session__write_header(session, session->evlist, fd, true);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400662 }
663
Tom Zanussi454c4072010-05-01 01:41:20 -0500664 return ret;
665}
666
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300667int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500668{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300669 struct perf_inject inject = {
670 .tool = {
671 .sample = perf_event__repipe_sample,
672 .mmap = perf_event__repipe,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200673 .mmap2 = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300674 .comm = perf_event__repipe,
675 .fork = perf_event__repipe,
676 .exit = perf_event__repipe,
677 .lost = perf_event__repipe,
Adrian Hunterd8145b32015-11-13 11:48:32 +0200678 .lost_samples = perf_event__repipe,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300679 .aux = perf_event__repipe,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300680 .itrace_start = perf_event__repipe,
Adrian Hunter02860392015-07-21 12:44:03 +0300681 .context_switch = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300682 .read = perf_event__repipe_sample,
683 .throttle = perf_event__repipe,
684 .unthrottle = perf_event__repipe,
685 .attr = perf_event__repipe_attr,
Adrian Hunter47c3d102013-07-04 16:20:21 +0300686 .tracing_data = perf_event__repipe_op2_synth,
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300687 .auxtrace_info = perf_event__repipe_op2_synth,
688 .auxtrace = perf_event__repipe_auxtrace,
689 .auxtrace_error = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300690 .finished_round = perf_event__repipe_oe_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300691 .build_id = perf_event__repipe_op2_synth,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200692 .id_index = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300693 },
Andrew Vagine558a5b2012-08-07 16:56:02 +0400694 .input_name = "-",
Andrew Vagin26a031e2012-08-07 16:56:04 +0400695 .samples = LIST_HEAD_INIT(inject.samples),
Jiri Olsa34069122013-10-29 19:04:57 +0100696 .output = {
697 .path = "-",
698 .mode = PERF_DATA_MODE_WRITE,
699 },
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300700 };
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900701 struct perf_data_file file = {
702 .mode = PERF_DATA_MODE_READ,
703 };
704 int ret;
705
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300706 const struct option options[] = {
707 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
708 "Inject build-ids into the output stream"),
Andrew Vagine558a5b2012-08-07 16:56:02 +0400709 OPT_STRING('i', "input", &inject.input_name, "file",
710 "input file name"),
Jiri Olsa34069122013-10-29 19:04:57 +0100711 OPT_STRING('o', "output", &inject.output.path, "file",
Andrew Vagine558a5b2012-08-07 16:56:02 +0400712 "output file name"),
Andrew Vagin26a031e2012-08-07 16:56:04 +0400713 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
714 "Merge sched-stat and sched-switch for getting events "
715 "where and how long tasks slept"),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300716 OPT_INCR('v', "verbose", &verbose,
717 "be more verbose (show build ids, etc)"),
Adrian Huntera7a2b8b2014-07-22 16:17:38 +0300718 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
719 "kallsyms pathname"),
Yunlong Songccaa4742015-04-02 21:47:11 +0800720 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300721 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
722 NULL, "opts", "Instruction Tracing options",
723 itrace_parse_synth_opts),
Adrian Hunterf56fb982015-09-25 16:15:55 +0300724 OPT_BOOLEAN(0, "strip", &inject.strip,
725 "strip non-synthesized events (use with --itrace)"),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300726 OPT_END()
727 };
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300728 const char * const inject_usage[] = {
729 "perf inject [<options>]",
730 NULL
731 };
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300732
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300733 argc = parse_options(argc, argv, options, inject_usage, 0);
Tom Zanussi454c4072010-05-01 01:41:20 -0500734
735 /*
736 * Any (unrecognized) arguments left?
737 */
738 if (argc)
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300739 usage_with_options(inject_usage, options);
Tom Zanussi454c4072010-05-01 01:41:20 -0500740
Adrian Hunterf56fb982015-09-25 16:15:55 +0300741 if (inject.strip && !inject.itrace_synth_opts.set) {
742 pr_err("--strip option requires --itrace option\n");
743 return -1;
744 }
745
Jiri Olsa34069122013-10-29 19:04:57 +0100746 if (perf_data_file__open(&inject.output)) {
747 perror("failed to create output file");
748 return -1;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400749 }
750
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300751 inject.tool.ordered_events = inject.sched_stat;
752
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900753 file.path = inject.input_name;
754 inject.session = perf_session__new(&file, true, &inject.tool);
755 if (inject.session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900756 return -1;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900757
Taeung Song9fedfb02015-06-30 17:15:20 +0900758 ret = symbol__init(&inject.session->header.env);
759 if (ret < 0)
760 goto out_delete;
Tom Zanussi454c4072010-05-01 01:41:20 -0500761
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900762 ret = __cmd_inject(&inject);
763
Taeung Song9fedfb02015-06-30 17:15:20 +0900764out_delete:
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900765 perf_session__delete(inject.session);
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900766 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500767}