blob: 23a76cf3846f4605834af2d405296a898e0869f5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tom Zanussi454c4072010-05-01 01:41:20 -05002/*
3 * builtin-inject.c
4 *
5 * Builtin inject command: Examine the live mode (stdin) event stream
6 * and repipe it to stdout while optionally injecting additional
7 * events into it.
8 */
9#include "builtin.h"
10
Andrew Vagin26a031e2012-08-07 16:56:04 +040011#include "util/color.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030012#include "util/dso.h"
Andrew Vagin26a031e2012-08-07 16:56:04 +040013#include "util/evlist.h"
14#include "util/evsel.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010015#include "util/map.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050016#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020017#include "util/tool.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050018#include "util/debug.h"
Andrew Vagin54a3cf52012-08-07 16:56:05 +040019#include "util/build-id.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030021#include "util/auxtrace.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010022#include "util/jit.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010023#include "util/symbol.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030024#include "util/synthetic-events.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030025#include "util/thread.h"
Tom Zanussi454c4072010-05-01 01:41:20 -050026
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060027#include <subcmd/parse-options.h>
Tom Zanussi454c4072010-05-01 01:41:20 -050028
Andrew Vagin26a031e2012-08-07 16:56:04 +040029#include <linux/list.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030030#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030031#include <signal.h>
Andrew Vagin26a031e2012-08-07 16:56:04 +040032
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030033struct perf_inject {
Jiri Olsa34069122013-10-29 19:04:57 +010034 struct perf_tool tool;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +090035 struct perf_session *session;
Jiri Olsa34069122013-10-29 19:04:57 +010036 bool build_ids;
37 bool sched_stat;
Adrian Huntercd10b282015-04-30 17:37:26 +030038 bool have_auxtrace;
Adrian Hunterf56fb982015-09-25 16:15:55 +030039 bool strip;
Stephane Eranian9b07e272015-11-30 10:02:21 +010040 bool jit_mode;
Jiri Olsa34069122013-10-29 19:04:57 +010041 const char *input_name;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010042 struct perf_data output;
Jiri Olsa34069122013-10-29 19:04:57 +010043 u64 bytes_written;
Adrian Hunter73117302015-09-25 16:15:54 +030044 u64 aux_id;
Jiri Olsa34069122013-10-29 19:04:57 +010045 struct list_head samples;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +030046 struct itrace_synth_opts itrace_synth_opts;
Andrew Vagin26a031e2012-08-07 16:56:04 +040047};
48
49struct event_entry {
50 struct list_head node;
51 u32 tid;
52 union perf_event event[0];
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -030053};
Tom Zanussi454c4072010-05-01 01:41:20 -050054
Adrian Huntercd17a9b2015-04-21 12:21:54 +030055static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
Tom Zanussi454c4072010-05-01 01:41:20 -050056{
Jiri Olsa34069122013-10-29 19:04:57 +010057 ssize_t size;
Tom Zanussi454c4072010-05-01 01:41:20 -050058
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010059 size = perf_data__write(&inject->output, buf, sz);
Jiri Olsa34069122013-10-29 19:04:57 +010060 if (size < 0)
61 return -errno;
Tom Zanussi454c4072010-05-01 01:41:20 -050062
Jiri Olsa34069122013-10-29 19:04:57 +010063 inject->bytes_written += size;
Tom Zanussi454c4072010-05-01 01:41:20 -050064 return 0;
65}
66
Adrian Huntercd17a9b2015-04-21 12:21:54 +030067static int perf_event__repipe_synth(struct perf_tool *tool,
68 union perf_event *event)
69{
70 struct perf_inject *inject = container_of(tool, struct perf_inject,
71 tool);
72
73 return output_bytes(inject, event, event->header.size);
74}
75
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -030076static int perf_event__repipe_oe_synth(struct perf_tool *tool,
77 union perf_event *event,
78 struct ordered_events *oe __maybe_unused)
79{
80 return perf_event__repipe_synth(tool, event);
81}
82
Jiri Olsae12b2022016-03-10 17:41:13 +010083#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +010084static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
85 union perf_event *event __maybe_unused,
86 struct ordered_events *oe __maybe_unused)
87{
88 return 0;
89}
90#endif
91
Jiri Olsa89f16882018-09-13 14:54:03 +020092static int perf_event__repipe_op2_synth(struct perf_session *session,
93 union perf_event *event)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020094{
Jiri Olsa89f16882018-09-13 14:54:03 +020095 return perf_event__repipe_synth(session->tool, event);
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020096}
97
Adrian Hunter47c3d102013-07-04 16:20:21 +030098static int perf_event__repipe_attr(struct perf_tool *tool,
99 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +0200100 struct evlist **pevlist)
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200101{
Adrian Hunter89c97d92013-10-22 10:34:09 +0300102 struct perf_inject *inject = container_of(tool, struct perf_inject,
103 tool);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +0200104 int ret;
Adrian Hunter47c3d102013-07-04 16:20:21 +0300105
106 ret = perf_event__process_attr(tool, event, pevlist);
Stephane Eranian1a1ed1b2012-05-15 13:28:11 +0200107 if (ret)
108 return ret;
109
Jiri Olsaa261e4a2014-06-05 18:51:44 +0200110 if (!inject->output.is_pipe)
Adrian Hunter89c97d92013-10-22 10:34:09 +0300111 return 0;
112
Adrian Hunter47c3d102013-07-04 16:20:21 +0300113 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -0200114}
115
Adrian Huntere31f0d02015-04-30 17:37:27 +0300116#ifdef HAVE_AUXTRACE_SUPPORT
117
118static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
119{
120 char buf[4096];
121 ssize_t ssz;
122 int ret;
123
124 while (size > 0) {
125 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
126 if (ssz < 0)
127 return -errno;
128 ret = output_bytes(inject, buf, ssz);
129 if (ret)
130 return ret;
131 size -= ssz;
132 }
133
134 return 0;
135}
136
Jiri Olsa73365552018-09-13 14:54:04 +0200137static s64 perf_event__repipe_auxtrace(struct perf_session *session,
138 union perf_event *event)
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300139{
Jiri Olsa73365552018-09-13 14:54:04 +0200140 struct perf_tool *tool = session->tool;
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300141 struct perf_inject *inject = container_of(tool, struct perf_inject,
142 tool);
143 int ret;
144
Adrian Huntercd10b282015-04-30 17:37:26 +0300145 inject->have_auxtrace = true;
146
Adrian Hunter99fa2982015-04-30 17:37:25 +0300147 if (!inject->output.is_pipe) {
148 off_t offset;
149
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100150 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300151 if (offset == -1)
152 return -errno;
153 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
154 event, offset);
155 if (ret < 0)
156 return ret;
157 }
158
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100159 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300160 ret = output_bytes(inject, event, event->header.size);
161 if (ret < 0)
162 return ret;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100163 ret = copy_bytes(inject, perf_data__fd(session->data),
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300164 event->auxtrace.size);
165 } else {
166 ret = output_bytes(inject, event,
167 event->header.size + event->auxtrace.size);
168 }
169 if (ret < 0)
170 return ret;
171
172 return event->auxtrace.size;
173}
174
Adrian Huntere31f0d02015-04-30 17:37:27 +0300175#else
176
177static s64
Jiri Olsa73365552018-09-13 14:54:04 +0200178perf_event__repipe_auxtrace(struct perf_session *session __maybe_unused,
179 union perf_event *event __maybe_unused)
Adrian Huntere31f0d02015-04-30 17:37:27 +0300180{
181 pr_err("AUX area tracing not supported\n");
182 return -EINVAL;
183}
184
185#endif
186
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200187static int perf_event__repipe(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200188 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300189 struct perf_sample *sample __maybe_unused,
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300190 struct machine *machine __maybe_unused)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200191{
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300192 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200193}
194
Adrian Hunterf56fb982015-09-25 16:15:55 +0300195static int perf_event__drop(struct perf_tool *tool __maybe_unused,
196 union perf_event *event __maybe_unused,
197 struct perf_sample *sample __maybe_unused,
198 struct machine *machine __maybe_unused)
199{
200 return 0;
201}
202
Adrian Hunter73117302015-09-25 16:15:54 +0300203static int perf_event__drop_aux(struct perf_tool *tool,
204 union perf_event *event __maybe_unused,
205 struct perf_sample *sample,
206 struct machine *machine __maybe_unused)
207{
208 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
209
210 if (!inject->aux_id)
211 inject->aux_id = sample->id;
212
213 return 0;
214}
215
Andrew Vagin26a031e2012-08-07 16:56:04 +0400216typedef int (*inject_handler)(struct perf_tool *tool,
217 union perf_event *event,
218 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200219 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400220 struct machine *machine);
221
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200222static int perf_event__repipe_sample(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200223 union perf_event *event,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400224 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200225 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400226 struct machine *machine)
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300227{
Arnaldo Carvalho de Melo40978e92019-07-03 16:02:09 -0300228 if (evsel && evsel->handler) {
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300229 inject_handler f = evsel->handler;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400230 return f(tool, event, sample, evsel, machine);
231 }
232
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400233 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
234
Adrian Hunter63c2c9f2013-07-04 16:20:20 +0300235 return perf_event__repipe_synth(tool, event);
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300236}
237
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200238static int perf_event__repipe_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200239 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200240 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200241 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500242{
243 int err;
244
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200245 err = perf_event__process_mmap(tool, event, sample, machine);
246 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500247
248 return err;
249}
250
Jiri Olsae12b2022016-03-10 17:41:13 +0100251#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100252static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
253 union perf_event *event,
254 struct perf_sample *sample,
255 struct machine *machine)
256{
257 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
258 u64 n = 0;
Adrian Hunter570735b2016-03-07 16:44:40 -0300259 int ret;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100260
261 /*
262 * if jit marker, then inject jit mmaps and generate ELF images
263 */
Adrian Hunter570735b2016-03-07 16:44:40 -0300264 ret = jit_process(inject->session, &inject->output, machine,
265 event->mmap.filename, sample->pid, &n);
266 if (ret < 0)
267 return ret;
268 if (ret) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100269 inject->bytes_written += n;
270 return 0;
271 }
272 return perf_event__repipe_mmap(tool, event, sample, machine);
273}
274#endif
275
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200276static int perf_event__repipe_mmap2(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_mmap2(tool, event, sample, machine);
284 perf_event__repipe(tool, event, sample, machine);
285
286 return err;
287}
288
Jiri Olsae12b2022016-03-10 17:41:13 +0100289#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100290static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
291 union perf_event *event,
292 struct perf_sample *sample,
293 struct machine *machine)
294{
295 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
296 u64 n = 0;
Adrian Hunter570735b2016-03-07 16:44:40 -0300297 int ret;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100298
299 /*
300 * if jit marker, then inject jit mmaps and generate ELF images
301 */
Adrian Hunter570735b2016-03-07 16:44:40 -0300302 ret = jit_process(inject->session, &inject->output, machine,
303 event->mmap2.filename, sample->pid, &n);
304 if (ret < 0)
305 return ret;
306 if (ret) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100307 inject->bytes_written += n;
308 return 0;
309 }
310 return perf_event__repipe_mmap2(tool, event, sample, machine);
311}
312#endif
313
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300314static int perf_event__repipe_fork(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200315 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200316 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200317 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500318{
319 int err;
320
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300321 err = perf_event__process_fork(tool, event, sample, machine);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200322 perf_event__repipe(tool, event, sample, machine);
Tom Zanussi454c4072010-05-01 01:41:20 -0500323
324 return err;
325}
326
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300327static int perf_event__repipe_comm(struct perf_tool *tool,
328 union perf_event *event,
329 struct perf_sample *sample,
330 struct machine *machine)
331{
332 int err;
333
334 err = perf_event__process_comm(tool, event, sample, machine);
335 perf_event__repipe(tool, event, sample, machine);
336
337 return err;
338}
339
Hari Bathinif3b36142017-03-08 02:11:43 +0530340static int perf_event__repipe_namespaces(struct perf_tool *tool,
341 union perf_event *event,
342 struct perf_sample *sample,
343 struct machine *machine)
344{
345 int err = perf_event__process_namespaces(tool, event, sample, machine);
346
347 perf_event__repipe(tool, event, sample, machine);
348
349 return err;
350}
351
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300352static int perf_event__repipe_exit(struct perf_tool *tool,
353 union perf_event *event,
354 struct perf_sample *sample,
355 struct machine *machine)
356{
357 int err;
358
359 err = perf_event__process_exit(tool, event, sample, machine);
360 perf_event__repipe(tool, event, sample, machine);
361
362 return err;
363}
364
Jiri Olsa89f16882018-09-13 14:54:03 +0200365static int perf_event__repipe_tracing_data(struct perf_session *session,
366 union perf_event *event)
Tom Zanussi454c4072010-05-01 01:41:20 -0500367{
368 int err;
369
Jiri Olsa89f16882018-09-13 14:54:03 +0200370 perf_event__repipe_synth(session->tool, event);
371 err = perf_event__process_tracing_data(session, event);
Tom Zanussi454c4072010-05-01 01:41:20 -0500372
373 return err;
374}
375
Jiri Olsa89f16882018-09-13 14:54:03 +0200376static int perf_event__repipe_id_index(struct perf_session *session,
377 union perf_event *event)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300378{
379 int err;
380
Jiri Olsa89f16882018-09-13 14:54:03 +0200381 perf_event__repipe_synth(session->tool, event);
382 err = perf_event__process_id_index(session, event);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300383
384 return err;
385}
386
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300387static int dso__read_build_id(struct dso *dso)
Tom Zanussi454c4072010-05-01 01:41:20 -0500388{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300389 if (dso->has_build_id)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300390 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500391
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300392 if (filename__read_build_id(dso->long_name, dso->build_id,
393 sizeof(dso->build_id)) > 0) {
394 dso->has_build_id = true;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300395 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500396 }
397
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300398 return -1;
399}
Tom Zanussi454c4072010-05-01 01:41:20 -0500400
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300401static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200402 struct machine *machine)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300403{
404 u16 misc = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300405 int err;
Tom Zanussi454c4072010-05-01 01:41:20 -0500406
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300407 if (dso__read_build_id(dso) < 0) {
408 pr_debug("no build_id found for %s\n", dso->long_name);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300409 return -1;
410 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500411
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300412 if (dso->kernel)
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300413 misc = PERF_RECORD_MISC_KERNEL;
414
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300415 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200416 machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300417 if (err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300418 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
Tom Zanussi454c4072010-05-01 01:41:20 -0500419 return -1;
420 }
421
422 return 0;
423}
424
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200425static int perf_event__inject_buildid(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200426 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200427 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200428 struct evsel *evsel __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200429 struct machine *machine)
Tom Zanussi454c4072010-05-01 01:41:20 -0500430{
431 struct addr_location al;
432 struct thread *thread;
Tom Zanussi454c4072010-05-01 01:41:20 -0500433
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900434 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
Tom Zanussi454c4072010-05-01 01:41:20 -0500435 if (thread == NULL) {
436 pr_err("problem processing %d event, skipping it.\n",
437 event->header.type);
Tom Zanussi454c4072010-05-01 01:41:20 -0500438 goto repipe;
439 }
440
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300441 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
Tom Zanussi454c4072010-05-01 01:41:20 -0500442 if (!al.map->dso->hit) {
443 al.map->dso->hit = 1;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300444 if (map__load(al.map) >= 0) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200445 dso__inject_build_id(al.map->dso, tool, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300446 /*
447 * If this fails, too bad, let the other side
448 * account this as unresolved.
449 */
Namhyung Kim393be2e2012-08-06 13:41:21 +0900450 } else {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200451#ifdef HAVE_LIBELF_SUPPORT
Tom Zanussi454c4072010-05-01 01:41:20 -0500452 pr_warning("no symbols found in %s, maybe "
453 "install a debug package?\n",
454 al.map->dso->long_name);
Namhyung Kim393be2e2012-08-06 13:41:21 +0900455#endif
456 }
Tom Zanussi454c4072010-05-01 01:41:20 -0500457 }
458 }
459
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300460 thread__put(thread);
Tom Zanussi454c4072010-05-01 01:41:20 -0500461repipe:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200462 perf_event__repipe(tool, event, sample, machine);
Arnaldo Carvalho de Melo090f7202010-05-02 19:46:36 -0300463 return 0;
Tom Zanussi454c4072010-05-01 01:41:20 -0500464}
465
Andrew Vagin26a031e2012-08-07 16:56:04 +0400466static int perf_inject__sched_process_exit(struct perf_tool *tool,
467 union perf_event *event __maybe_unused,
468 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200469 struct evsel *evsel __maybe_unused,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400470 struct machine *machine __maybe_unused)
471{
472 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
473 struct event_entry *ent;
474
475 list_for_each_entry(ent, &inject->samples, node) {
476 if (sample->tid == ent->tid) {
477 list_del_init(&ent->node);
478 free(ent);
479 break;
480 }
481 }
482
483 return 0;
484}
485
486static int perf_inject__sched_switch(struct perf_tool *tool,
487 union perf_event *event,
488 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200489 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400490 struct machine *machine)
491{
492 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
493 struct event_entry *ent;
494
495 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
496
497 ent = malloc(event->header.size + sizeof(struct event_entry));
498 if (ent == NULL) {
499 color_fprintf(stderr, PERF_COLOR_RED,
500 "Not enough memory to process sched switch event!");
501 return -1;
502 }
503
504 ent->tid = sample->tid;
505 memcpy(&ent->event, event, event->header.size);
506 list_add(&ent->node, &inject->samples);
507 return 0;
508}
509
510static int perf_inject__sched_stat(struct perf_tool *tool,
511 union perf_event *event __maybe_unused,
512 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200513 struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400514 struct machine *machine)
515{
516 struct event_entry *ent;
517 union perf_event *event_sw;
518 struct perf_sample sample_sw;
519 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
520 u32 pid = perf_evsel__intval(evsel, sample, "pid");
521
522 list_for_each_entry(ent, &inject->samples, node) {
523 if (pid == ent->tid)
524 goto found;
525 }
526
527 return 0;
528found:
529 event_sw = &ent->event[0];
530 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
531
532 sample_sw.period = sample->period;
533 sample_sw.time = sample->time;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200534 perf_event__synthesize_sample(event_sw, evsel->core.attr.sample_type,
535 evsel->core.attr.read_format, &sample_sw);
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400536 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400537 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
538}
539
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300540static void sig_handler(int sig __maybe_unused)
Tom Zanussi454c4072010-05-01 01:41:20 -0500541{
542 session_done = 1;
543}
544
Jiri Olsa32dcd022019-07-21 13:23:51 +0200545static int perf_evsel__check_stype(struct evsel *evsel,
Andrew Vagin26a031e2012-08-07 16:56:04 +0400546 u64 sample_type, const char *sample_msg)
547{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200548 struct perf_event_attr *attr = &evsel->core.attr;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400549 const char *name = perf_evsel__name(evsel);
550
551 if (!(attr->sample_type & sample_type)) {
552 pr_err("Samples for %s event do not have %s attribute set.",
553 name, sample_msg);
554 return -EINVAL;
555 }
556
557 return 0;
558}
559
Adrian Hunterf56fb982015-09-25 16:15:55 +0300560static int drop_sample(struct perf_tool *tool __maybe_unused,
561 union perf_event *event __maybe_unused,
562 struct perf_sample *sample __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200563 struct evsel *evsel __maybe_unused,
Adrian Hunterf56fb982015-09-25 16:15:55 +0300564 struct machine *machine __maybe_unused)
565{
566 return 0;
567}
568
569static void strip_init(struct perf_inject *inject)
570{
Jiri Olsa63503db2019-07-21 13:23:52 +0200571 struct evlist *evlist = inject->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200572 struct evsel *evsel;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300573
574 inject->tool.context_switch = perf_event__drop;
575
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300576 evlist__for_each_entry(evlist, evsel)
Adrian Hunterf56fb982015-09-25 16:15:55 +0300577 evsel->handler = drop_sample;
578}
579
Jiri Olsa32dcd022019-07-21 13:23:51 +0200580static bool has_tracking(struct evsel *evsel)
Adrian Hunterf56fb982015-09-25 16:15:55 +0300581{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200582 return evsel->core.attr.mmap || evsel->core.attr.mmap2 || evsel->core.attr.comm ||
583 evsel->core.attr.task;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300584}
585
586#define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
587 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
588
589/*
590 * In order that the perf.data file is parsable, tracking events like MMAP need
591 * their selected event to exist, except if there is only 1 selected event left
592 * and it has a compatible sample type.
593 */
Jiri Olsa63503db2019-07-21 13:23:52 +0200594static bool ok_to_remove(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200595 struct evsel *evsel_to_remove)
Adrian Hunterf56fb982015-09-25 16:15:55 +0300596{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200597 struct evsel *evsel;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300598 int cnt = 0;
599 bool ok = false;
600
601 if (!has_tracking(evsel_to_remove))
602 return true;
603
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300604 evlist__for_each_entry(evlist, evsel) {
Adrian Hunterf56fb982015-09-25 16:15:55 +0300605 if (evsel->handler != drop_sample) {
606 cnt += 1;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200607 if ((evsel->core.attr.sample_type & COMPAT_MASK) ==
608 (evsel_to_remove->core.attr.sample_type & COMPAT_MASK))
Adrian Hunterf56fb982015-09-25 16:15:55 +0300609 ok = true;
610 }
611 }
612
613 return ok && cnt == 1;
614}
615
616static void strip_fini(struct perf_inject *inject)
617{
Jiri Olsa63503db2019-07-21 13:23:52 +0200618 struct evlist *evlist = inject->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200619 struct evsel *evsel, *tmp;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300620
621 /* Remove non-synthesized evsels if possible */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300622 evlist__for_each_entry_safe(evlist, tmp, evsel) {
Adrian Hunterf56fb982015-09-25 16:15:55 +0300623 if (evsel->handler == drop_sample &&
624 ok_to_remove(evlist, evsel)) {
625 pr_debug("Deleting %s\n", perf_evsel__name(evsel));
Jiri Olsa16251022019-07-21 13:24:00 +0200626 evlist__remove(evlist, evsel);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200627 evsel__delete(evsel);
Adrian Hunterf56fb982015-09-25 16:15:55 +0300628 }
629 }
630}
631
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300632static int __cmd_inject(struct perf_inject *inject)
Tom Zanussi454c4072010-05-01 01:41:20 -0500633{
Tom Zanussi454c4072010-05-01 01:41:20 -0500634 int ret = -EINVAL;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900635 struct perf_session *session = inject->session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100636 struct perf_data *data_out = &inject->output;
637 int fd = perf_data__fd(data_out);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300638 u64 output_data_offset;
Tom Zanussi454c4072010-05-01 01:41:20 -0500639
640 signal(SIGINT, sig_handler);
641
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300642 if (inject->build_ids || inject->sched_stat ||
643 inject->itrace_synth_opts.set) {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300644 inject->tool.mmap = perf_event__repipe_mmap;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200645 inject->tool.mmap2 = perf_event__repipe_mmap2;
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300646 inject->tool.fork = perf_event__repipe_fork;
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300647 inject->tool.tracing_data = perf_event__repipe_tracing_data;
Tom Zanussi454c4072010-05-01 01:41:20 -0500648 }
649
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300650 output_data_offset = session->header.data_offset;
651
Andrew Vagin54a3cf52012-08-07 16:56:05 +0400652 if (inject->build_ids) {
653 inject->tool.sample = perf_event__inject_buildid;
654 } else if (inject->sched_stat) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200655 struct evsel *evsel;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400656
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300657 evlist__for_each_entry(session->evlist, evsel) {
Andrew Vagin26a031e2012-08-07 16:56:04 +0400658 const char *name = perf_evsel__name(evsel);
659
660 if (!strcmp(name, "sched:sched_switch")) {
661 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
662 return -EINVAL;
663
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300664 evsel->handler = perf_inject__sched_switch;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400665 } else if (!strcmp(name, "sched:sched_process_exit"))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300666 evsel->handler = perf_inject__sched_process_exit;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400667 else if (!strncmp(name, "sched:sched_stat_", 17))
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300668 evsel->handler = perf_inject__sched_stat;
Andrew Vagin26a031e2012-08-07 16:56:04 +0400669 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300670 } else if (inject->itrace_synth_opts.set) {
671 session->itrace_synth_opts = &inject->itrace_synth_opts;
672 inject->itrace_synth_opts.inject = true;
673 inject->tool.comm = perf_event__repipe_comm;
Hari Bathinif3b36142017-03-08 02:11:43 +0530674 inject->tool.namespaces = perf_event__repipe_namespaces;
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300675 inject->tool.exit = perf_event__repipe_exit;
676 inject->tool.id_index = perf_event__repipe_id_index;
677 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
678 inject->tool.auxtrace = perf_event__process_auxtrace;
Adrian Hunter73117302015-09-25 16:15:54 +0300679 inject->tool.aux = perf_event__drop_aux;
680 inject->tool.itrace_start = perf_event__drop_aux,
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300681 inject->tool.ordered_events = true;
682 inject->tool.ordering_requires_timestamps = true;
683 /* Allow space in the header for new attributes */
684 output_data_offset = 4096;
Adrian Hunterf56fb982015-09-25 16:15:55 +0300685 if (inject->strip)
686 strip_init(inject);
Andrew Vagin26a031e2012-08-07 16:56:04 +0400687 }
688
Adrian Hunter99fa2982015-04-30 17:37:25 +0300689 if (!inject->itrace_synth_opts.set)
690 auxtrace_index__free(&session->auxtrace_index);
691
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100692 if (!data_out->is_pipe)
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300693 lseek(fd, output_data_offset, SEEK_SET);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400694
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300695 ret = perf_session__process_events(session);
David Carrillo-Cisnerosbb8d5212017-04-10 13:14:26 -0700696 if (ret)
697 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500698
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100699 if (!data_out->is_pipe) {
Adrian Hunter640dad42016-03-07 16:44:38 -0300700 if (inject->build_ids)
Adrian Huntere38b43c2014-07-14 13:02:34 +0300701 perf_header__set_feat(&session->header,
702 HEADER_BUILD_ID);
Adrian Hunter640dad42016-03-07 16:44:38 -0300703 /*
704 * Keep all buildids when there is unprocessed AUX data because
705 * it is not known which ones the AUX trace hits.
706 */
707 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
708 inject->have_auxtrace && !inject->itrace_synth_opts.set)
709 dsos__hit_all(session);
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300710 /*
711 * The AUX areas have been removed and replaced with
Adrian Hunter73117302015-09-25 16:15:54 +0300712 * synthesized hardware events, so clear the feature flag and
713 * remove the evsel.
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300714 */
Adrian Hunter051a01b2015-09-25 16:15:43 +0300715 if (inject->itrace_synth_opts.set) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200716 struct evsel *evsel;
Adrian Hunter73117302015-09-25 16:15:54 +0300717
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300718 perf_header__clear_feat(&session->header,
719 HEADER_AUXTRACE);
Adrian Hunter051a01b2015-09-25 16:15:43 +0300720 if (inject->itrace_synth_opts.last_branch)
721 perf_header__set_feat(&session->header,
722 HEADER_BRANCH_STACK);
Adrian Hunter73117302015-09-25 16:15:54 +0300723 evsel = perf_evlist__id2evsel_strict(session->evlist,
724 inject->aux_id);
725 if (evsel) {
726 pr_debug("Deleting %s\n",
727 perf_evsel__name(evsel));
Jiri Olsa16251022019-07-21 13:24:00 +0200728 evlist__remove(session->evlist, evsel);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200729 evsel__delete(evsel);
Adrian Hunter73117302015-09-25 16:15:54 +0300730 }
Adrian Hunterf56fb982015-09-25 16:15:55 +0300731 if (inject->strip)
732 strip_fini(inject);
Adrian Hunter051a01b2015-09-25 16:15:43 +0300733 }
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300734 session->header.data_offset = output_data_offset;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400735 session->header.data_size = inject->bytes_written;
Namhyung Kim42aa2762015-01-29 17:06:48 +0900736 perf_session__write_header(session, session->evlist, fd, true);
Andrew Vagine558a5b2012-08-07 16:56:02 +0400737 }
738
Tom Zanussi454c4072010-05-01 01:41:20 -0500739 return ret;
740}
741
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300742int cmd_inject(int argc, const char **argv)
Tom Zanussi454c4072010-05-01 01:41:20 -0500743{
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300744 struct perf_inject inject = {
745 .tool = {
746 .sample = perf_event__repipe_sample,
747 .mmap = perf_event__repipe,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200748 .mmap2 = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300749 .comm = perf_event__repipe,
750 .fork = perf_event__repipe,
751 .exit = perf_event__repipe,
752 .lost = perf_event__repipe,
Adrian Hunterd8145b32015-11-13 11:48:32 +0200753 .lost_samples = perf_event__repipe,
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300754 .aux = perf_event__repipe,
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300755 .itrace_start = perf_event__repipe,
Adrian Hunter02860392015-07-21 12:44:03 +0300756 .context_switch = perf_event__repipe,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300757 .read = perf_event__repipe_sample,
758 .throttle = perf_event__repipe,
759 .unthrottle = perf_event__repipe,
760 .attr = perf_event__repipe_attr,
Adrian Hunter47c3d102013-07-04 16:20:21 +0300761 .tracing_data = perf_event__repipe_op2_synth,
Adrian Huntercd17a9b2015-04-21 12:21:54 +0300762 .auxtrace_info = perf_event__repipe_op2_synth,
763 .auxtrace = perf_event__repipe_auxtrace,
764 .auxtrace_error = perf_event__repipe_op2_synth,
Adrian Hunter46bc29b2016-03-08 10:38:44 +0200765 .time_conv = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melod704ebd2015-03-03 12:37:54 -0300766 .finished_round = perf_event__repipe_oe_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300767 .build_id = perf_event__repipe_op2_synth,
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200768 .id_index = perf_event__repipe_op2_synth,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -0700769 .feature = perf_event__repipe_op2_synth,
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300770 },
Andrew Vagine558a5b2012-08-07 16:56:02 +0400771 .input_name = "-",
Andrew Vagin26a031e2012-08-07 16:56:04 +0400772 .samples = LIST_HEAD_INIT(inject.samples),
Jiri Olsa34069122013-10-29 19:04:57 +0100773 .output = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100774 .path = "-",
775 .mode = PERF_DATA_MODE_WRITE,
Jiri Olsa34069122013-10-29 19:04:57 +0100776 },
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300777 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100778 struct perf_data data = {
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900779 .mode = PERF_DATA_MODE_READ,
780 };
781 int ret;
782
Stephane Eranian9b07e272015-11-30 10:02:21 +0100783 struct option options[] = {
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300784 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
785 "Inject build-ids into the output stream"),
Andrew Vagine558a5b2012-08-07 16:56:02 +0400786 OPT_STRING('i', "input", &inject.input_name, "file",
787 "input file name"),
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100788 OPT_STRING('o', "output", &inject.output.path, "file",
Andrew Vagine558a5b2012-08-07 16:56:02 +0400789 "output file name"),
Andrew Vagin26a031e2012-08-07 16:56:04 +0400790 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
791 "Merge sched-stat and sched-switch for getting events "
792 "where and how long tasks slept"),
Jiri Olsae12b2022016-03-10 17:41:13 +0100793#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100794 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
Jiri Olsae12b2022016-03-10 17:41:13 +0100795#endif
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300796 OPT_INCR('v', "verbose", &verbose,
797 "be more verbose (show build ids, etc)"),
Adrian Huntera7a2b8b2014-07-22 16:17:38 +0300798 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
799 "kallsyms pathname"),
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100800 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300801 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
Andi Kleenc12e0392018-09-13 20:10:31 -0700802 NULL, "opts", "Instruction Tracing options\n"
803 ITRACE_HELP,
Adrian Hunter0f0aa5e2015-04-09 18:54:00 +0300804 itrace_parse_synth_opts),
Adrian Hunterf56fb982015-09-25 16:15:55 +0300805 OPT_BOOLEAN(0, "strip", &inject.strip,
806 "strip non-synthesized events (use with --itrace)"),
Arnaldo Carvalho de Melo5ded57a2012-09-30 19:54:10 -0300807 OPT_END()
808 };
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300809 const char * const inject_usage[] = {
810 "perf inject [<options>]",
811 NULL
812 };
Jiri Olsae12b2022016-03-10 17:41:13 +0100813#ifndef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100814 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
815#endif
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300816 argc = parse_options(argc, argv, options, inject_usage, 0);
Tom Zanussi454c4072010-05-01 01:41:20 -0500817
818 /*
819 * Any (unrecognized) arguments left?
820 */
821 if (argc)
Arnaldo Carvalho de Melo002439e2012-10-01 15:20:58 -0300822 usage_with_options(inject_usage, options);
Tom Zanussi454c4072010-05-01 01:41:20 -0500823
Adrian Hunterf56fb982015-09-25 16:15:55 +0300824 if (inject.strip && !inject.itrace_synth_opts.set) {
825 pr_err("--strip option requires --itrace option\n");
826 return -1;
827 }
828
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100829 if (perf_data__open(&inject.output)) {
Jiri Olsa34069122013-10-29 19:04:57 +0100830 perror("failed to create output file");
831 return -1;
Andrew Vagine558a5b2012-08-07 16:56:02 +0400832 }
833
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300834 inject.tool.ordered_events = inject.sched_stat;
835
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100836 data.path = inject.input_name;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100837 inject.session = perf_session__new(&data, true, &inject.tool);
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900838 if (inject.session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900839 return -1;
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900840
Alexey Budankov371a3372019-03-18 20:45:44 +0300841 if (zstd_init(&(inject.session->zstd_data), 0) < 0)
842 pr_warning("Decompression initialization failed.\n");
843
Arnaldo Carvalho de Melo921f3fa2016-01-22 18:41:00 -0300844 if (inject.build_ids) {
845 /*
846 * to make sure the mmap records are ordered correctly
847 * and so that the correct especially due to jitted code
848 * mmaps. We cannot generate the buildid hit list and
849 * inject the jit mmaps at the same time for now.
850 */
851 inject.tool.ordered_events = true;
852 inject.tool.ordering_requires_timestamps = true;
853 }
Jiri Olsae12b2022016-03-10 17:41:13 +0100854#ifdef HAVE_JITDUMP
Stephane Eranian9b07e272015-11-30 10:02:21 +0100855 if (inject.jit_mode) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100856 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
857 inject.tool.mmap = perf_event__jit_repipe_mmap;
858 inject.tool.ordered_events = true;
859 inject.tool.ordering_requires_timestamps = true;
860 /*
861 * JIT MMAP injection injects all MMAP events in one go, so it
862 * does not obey finished_round semantics.
863 */
864 inject.tool.finished_round = perf_event__drop_oe;
865 }
866#endif
Taeung Song9fedfb02015-06-30 17:15:20 +0900867 ret = symbol__init(&inject.session->header.env);
868 if (ret < 0)
869 goto out_delete;
Tom Zanussi454c4072010-05-01 01:41:20 -0500870
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900871 ret = __cmd_inject(&inject);
872
Taeung Song9fedfb02015-06-30 17:15:20 +0900873out_delete:
Alexey Budankov371a3372019-03-18 20:45:44 +0300874 zstd_fini(&(inject.session->zstd_data));
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900875 perf_session__delete(inject.session);
Namhyung Kim1cb8bdc2014-08-12 15:40:37 +0900876 return ret;
Tom Zanussi454c4072010-05-01 01:41:20 -0500877}