]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-inject.c
perf tools: Remove perf_tool from event_op2
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-inject.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
454c407e
TZ
2/*
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
11#include "perf.h"
26a031e1
AV
12#include "util/color.h"
13#include "util/evlist.h"
14#include "util/evsel.h"
454c407e 15#include "util/session.h"
45694aa7 16#include "util/tool.h"
454c407e 17#include "util/debug.h"
54a3cf59 18#include "util/build-id.h"
f5fc1412 19#include "util/data.h"
0f0aa5e0 20#include "util/auxtrace.h"
9b07e27f 21#include "util/jit.h"
e7ff8920 22#include "util/thread.h"
454c407e 23
4b6ab94e 24#include <subcmd/parse-options.h>
454c407e 25
26a031e1 26#include <linux/list.h>
a43783ae 27#include <errno.h>
9607ad3a 28#include <signal.h>
26a031e1 29
5ded57ac 30struct perf_inject {
3406912c 31 struct perf_tool tool;
1cb8bdcc 32 struct perf_session *session;
3406912c
JO
33 bool build_ids;
34 bool sched_stat;
cd10b289 35 bool have_auxtrace;
f56fb986 36 bool strip;
9b07e27f 37 bool jit_mode;
3406912c 38 const char *input_name;
8ceb41d7 39 struct perf_data output;
3406912c 40 u64 bytes_written;
73117308 41 u64 aux_id;
3406912c 42 struct list_head samples;
0f0aa5e0 43 struct itrace_synth_opts itrace_synth_opts;
26a031e1
AV
44};
45
46struct event_entry {
47 struct list_head node;
48 u32 tid;
49 union perf_event event[0];
5ded57ac 50};
454c407e 51
cd17a9b5 52static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
454c407e 53{
3406912c 54 ssize_t size;
454c407e 55
8ceb41d7 56 size = perf_data__write(&inject->output, buf, sz);
3406912c
JO
57 if (size < 0)
58 return -errno;
454c407e 59
3406912c 60 inject->bytes_written += size;
454c407e
TZ
61 return 0;
62}
63
cd17a9b5
AH
64static int perf_event__repipe_synth(struct perf_tool *tool,
65 union perf_event *event)
66{
67 struct perf_inject *inject = container_of(tool, struct perf_inject,
68 tool);
69
70 return output_bytes(inject, event, event->header.size);
71}
72
d704ebda
ACM
73static int perf_event__repipe_oe_synth(struct perf_tool *tool,
74 union perf_event *event,
75 struct ordered_events *oe __maybe_unused)
76{
77 return perf_event__repipe_synth(tool, event);
78}
79
e12b202f 80#ifdef HAVE_JITDUMP
9b07e27f
SE
81static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
82 union perf_event *event __maybe_unused,
83 struct ordered_events *oe __maybe_unused)
84{
85 return 0;
86}
87#endif
88
89f1688a
JO
89static int perf_event__repipe_op2_synth(struct perf_session *session,
90 union perf_event *event)
743eb868 91{
89f1688a 92 return perf_event__repipe_synth(session->tool, event);
743eb868
ACM
93}
94
47c3d109
AH
95static int perf_event__repipe_attr(struct perf_tool *tool,
96 union perf_event *event,
97 struct perf_evlist **pevlist)
10d0f086 98{
89c97d93
AH
99 struct perf_inject *inject = container_of(tool, struct perf_inject,
100 tool);
1a1ed1ba 101 int ret;
47c3d109
AH
102
103 ret = perf_event__process_attr(tool, event, pevlist);
1a1ed1ba
SE
104 if (ret)
105 return ret;
106
a261e4a0 107 if (!inject->output.is_pipe)
89c97d93
AH
108 return 0;
109
47c3d109 110 return perf_event__repipe_synth(tool, event);
10d0f086
ACM
111}
112
e31f0d01
AH
113#ifdef HAVE_AUXTRACE_SUPPORT
114
115static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
116{
117 char buf[4096];
118 ssize_t ssz;
119 int ret;
120
121 while (size > 0) {
122 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
123 if (ssz < 0)
124 return -errno;
125 ret = output_bytes(inject, buf, ssz);
126 if (ret)
127 return ret;
128 size -= ssz;
129 }
130
131 return 0;
132}
133
cd17a9b5
AH
134static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
135 union perf_event *event,
b8f8eb84 136 struct perf_session *session)
cd17a9b5
AH
137{
138 struct perf_inject *inject = container_of(tool, struct perf_inject,
139 tool);
140 int ret;
141
cd10b289
AH
142 inject->have_auxtrace = true;
143
99fa2984
AH
144 if (!inject->output.is_pipe) {
145 off_t offset;
146
eae8ad80 147 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
99fa2984
AH
148 if (offset == -1)
149 return -errno;
150 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
151 event, offset);
152 if (ret < 0)
153 return ret;
154 }
155
8ceb41d7 156 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
cd17a9b5
AH
157 ret = output_bytes(inject, event, event->header.size);
158 if (ret < 0)
159 return ret;
8ceb41d7 160 ret = copy_bytes(inject, perf_data__fd(session->data),
cd17a9b5
AH
161 event->auxtrace.size);
162 } else {
163 ret = output_bytes(inject, event,
164 event->header.size + event->auxtrace.size);
165 }
166 if (ret < 0)
167 return ret;
168
169 return event->auxtrace.size;
170}
171
e31f0d01
AH
172#else
173
174static s64
175perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
176 union perf_event *event __maybe_unused,
177 struct perf_session *session __maybe_unused)
178{
179 pr_err("AUX area tracing not supported\n");
180 return -EINVAL;
181}
182
183#endif
184
45694aa7 185static int perf_event__repipe(struct perf_tool *tool,
d20deb64 186 union perf_event *event,
1d037ca1 187 struct perf_sample *sample __maybe_unused,
63c2c9f8 188 struct machine *machine __maybe_unused)
640c03ce 189{
63c2c9f8 190 return perf_event__repipe_synth(tool, event);
640c03ce
ACM
191}
192
f56fb986
AH
193static int perf_event__drop(struct perf_tool *tool __maybe_unused,
194 union perf_event *event __maybe_unused,
195 struct perf_sample *sample __maybe_unused,
196 struct machine *machine __maybe_unused)
197{
198 return 0;
199}
200
73117308
AH
201static int perf_event__drop_aux(struct perf_tool *tool,
202 union perf_event *event __maybe_unused,
203 struct perf_sample *sample,
204 struct machine *machine __maybe_unused)
205{
206 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
207
208 if (!inject->aux_id)
209 inject->aux_id = sample->id;
210
211 return 0;
212}
213
26a031e1
AV
214typedef int (*inject_handler)(struct perf_tool *tool,
215 union perf_event *event,
216 struct perf_sample *sample,
217 struct perf_evsel *evsel,
218 struct machine *machine);
219
45694aa7 220static int perf_event__repipe_sample(struct perf_tool *tool,
d20deb64 221 union perf_event *event,
26a031e1
AV
222 struct perf_sample *sample,
223 struct perf_evsel *evsel,
224 struct machine *machine)
9e69c210 225{
744a9719
ACM
226 if (evsel->handler) {
227 inject_handler f = evsel->handler;
26a031e1
AV
228 return f(tool, event, sample, evsel, machine);
229 }
230
54a3cf59
AV
231 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
232
63c2c9f8 233 return perf_event__repipe_synth(tool, event);
9e69c210
ACM
234}
235
45694aa7 236static int perf_event__repipe_mmap(struct perf_tool *tool,
d20deb64 237 union perf_event *event,
8115d60c 238 struct perf_sample *sample,
743eb868 239 struct machine *machine)
454c407e
TZ
240{
241 int err;
242
45694aa7
ACM
243 err = perf_event__process_mmap(tool, event, sample, machine);
244 perf_event__repipe(tool, event, sample, machine);
454c407e
TZ
245
246 return err;
247}
248
e12b202f 249#ifdef HAVE_JITDUMP
9b07e27f
SE
250static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
251 union perf_event *event,
252 struct perf_sample *sample,
253 struct machine *machine)
254{
255 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
256 u64 n = 0;
570735b3 257 int ret;
9b07e27f
SE
258
259 /*
260 * if jit marker, then inject jit mmaps and generate ELF images
261 */
570735b3
AH
262 ret = jit_process(inject->session, &inject->output, machine,
263 event->mmap.filename, sample->pid, &n);
264 if (ret < 0)
265 return ret;
266 if (ret) {
9b07e27f
SE
267 inject->bytes_written += n;
268 return 0;
269 }
270 return perf_event__repipe_mmap(tool, event, sample, machine);
271}
272#endif
273
5c5e854b
SE
274static int perf_event__repipe_mmap2(struct perf_tool *tool,
275 union perf_event *event,
276 struct perf_sample *sample,
277 struct machine *machine)
278{
279 int err;
280
281 err = perf_event__process_mmap2(tool, event, sample, machine);
282 perf_event__repipe(tool, event, sample, machine);
283
284 return err;
285}
286
e12b202f 287#ifdef HAVE_JITDUMP
9b07e27f
SE
288static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
289 union perf_event *event,
290 struct perf_sample *sample,
291 struct machine *machine)
292{
293 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
294 u64 n = 0;
570735b3 295 int ret;
9b07e27f
SE
296
297 /*
298 * if jit marker, then inject jit mmaps and generate ELF images
299 */
570735b3
AH
300 ret = jit_process(inject->session, &inject->output, machine,
301 event->mmap2.filename, sample->pid, &n);
302 if (ret < 0)
303 return ret;
304 if (ret) {
9b07e27f
SE
305 inject->bytes_written += n;
306 return 0;
307 }
308 return perf_event__repipe_mmap2(tool, event, sample, machine);
309}
310#endif
311
f62d3f0f 312static int perf_event__repipe_fork(struct perf_tool *tool,
d20deb64 313 union perf_event *event,
8115d60c 314 struct perf_sample *sample,
743eb868 315 struct machine *machine)
454c407e
TZ
316{
317 int err;
318
f62d3f0f 319 err = perf_event__process_fork(tool, event, sample, machine);
45694aa7 320 perf_event__repipe(tool, event, sample, machine);
454c407e
TZ
321
322 return err;
323}
324
0f0aa5e0
AH
325static int perf_event__repipe_comm(struct perf_tool *tool,
326 union perf_event *event,
327 struct perf_sample *sample,
328 struct machine *machine)
329{
330 int err;
331
332 err = perf_event__process_comm(tool, event, sample, machine);
333 perf_event__repipe(tool, event, sample, machine);
334
335 return err;
336}
337
f3b3614a
HB
338static int perf_event__repipe_namespaces(struct perf_tool *tool,
339 union perf_event *event,
340 struct perf_sample *sample,
341 struct machine *machine)
342{
343 int err = perf_event__process_namespaces(tool, event, sample, machine);
344
345 perf_event__repipe(tool, event, sample, machine);
346
347 return err;
348}
349
0f0aa5e0
AH
350static int perf_event__repipe_exit(struct perf_tool *tool,
351 union perf_event *event,
352 struct perf_sample *sample,
353 struct machine *machine)
354{
355 int err;
356
357 err = perf_event__process_exit(tool, event, sample, machine);
358 perf_event__repipe(tool, event, sample, machine);
359
360 return err;
361}
362
89f1688a
JO
363static int perf_event__repipe_tracing_data(struct perf_session *session,
364 union perf_event *event)
454c407e
TZ
365{
366 int err;
367
89f1688a
JO
368 perf_event__repipe_synth(session->tool, event);
369 err = perf_event__process_tracing_data(session, event);
454c407e
TZ
370
371 return err;
372}
373
89f1688a
JO
374static int perf_event__repipe_id_index(struct perf_session *session,
375 union perf_event *event)
0f0aa5e0
AH
376{
377 int err;
378
89f1688a
JO
379 perf_event__repipe_synth(session->tool, event);
380 err = perf_event__process_id_index(session, event);
0f0aa5e0
AH
381
382 return err;
383}
384
c824c433 385static int dso__read_build_id(struct dso *dso)
454c407e 386{
c824c433 387 if (dso->has_build_id)
090f7204 388 return 0;
454c407e 389
c824c433
ACM
390 if (filename__read_build_id(dso->long_name, dso->build_id,
391 sizeof(dso->build_id)) > 0) {
392 dso->has_build_id = true;
090f7204
ACM
393 return 0;
394 }
454c407e 395
090f7204
ACM
396 return -1;
397}
454c407e 398
c824c433 399static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
743eb868 400 struct machine *machine)
090f7204
ACM
401{
402 u16 misc = PERF_RECORD_MISC_USER;
090f7204 403 int err;
454c407e 404
c824c433
ACM
405 if (dso__read_build_id(dso) < 0) {
406 pr_debug("no build_id found for %s\n", dso->long_name);
090f7204
ACM
407 return -1;
408 }
454c407e 409
c824c433 410 if (dso->kernel)
090f7204 411 misc = PERF_RECORD_MISC_KERNEL;
454c407e 412
c824c433 413 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
743eb868 414 machine);
090f7204 415 if (err) {
c824c433 416 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
454c407e
TZ
417 return -1;
418 }
419
420 return 0;
421}
422
45694aa7 423static int perf_event__inject_buildid(struct perf_tool *tool,
d20deb64 424 union perf_event *event,
8115d60c 425 struct perf_sample *sample,
1d037ca1 426 struct perf_evsel *evsel __maybe_unused,
743eb868 427 struct machine *machine)
454c407e
TZ
428{
429 struct addr_location al;
430 struct thread *thread;
454c407e 431
13ce34df 432 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
454c407e
TZ
433 if (thread == NULL) {
434 pr_err("problem processing %d event, skipping it.\n",
435 event->header.type);
454c407e
TZ
436 goto repipe;
437 }
438
71a84b5a 439 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
454c407e
TZ
440 if (!al.map->dso->hit) {
441 al.map->dso->hit = 1;
be39db9f 442 if (map__load(al.map) >= 0) {
45694aa7 443 dso__inject_build_id(al.map->dso, tool, machine);
090f7204
ACM
444 /*
445 * If this fails, too bad, let the other side
446 * account this as unresolved.
447 */
393be2e3 448 } else {
89fe808a 449#ifdef HAVE_LIBELF_SUPPORT
454c407e
TZ
450 pr_warning("no symbols found in %s, maybe "
451 "install a debug package?\n",
452 al.map->dso->long_name);
393be2e3
NK
453#endif
454 }
454c407e
TZ
455 }
456 }
457
b91fc39f 458 thread__put(thread);
454c407e 459repipe:
45694aa7 460 perf_event__repipe(tool, event, sample, machine);
090f7204 461 return 0;
454c407e
TZ
462}
463
26a031e1
AV
464static int perf_inject__sched_process_exit(struct perf_tool *tool,
465 union perf_event *event __maybe_unused,
466 struct perf_sample *sample,
467 struct perf_evsel *evsel __maybe_unused,
468 struct machine *machine __maybe_unused)
469{
470 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
471 struct event_entry *ent;
472
473 list_for_each_entry(ent, &inject->samples, node) {
474 if (sample->tid == ent->tid) {
475 list_del_init(&ent->node);
476 free(ent);
477 break;
478 }
479 }
480
481 return 0;
482}
483
484static int perf_inject__sched_switch(struct perf_tool *tool,
485 union perf_event *event,
486 struct perf_sample *sample,
487 struct perf_evsel *evsel,
488 struct machine *machine)
489{
490 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
491 struct event_entry *ent;
492
493 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
494
495 ent = malloc(event->header.size + sizeof(struct event_entry));
496 if (ent == NULL) {
497 color_fprintf(stderr, PERF_COLOR_RED,
498 "Not enough memory to process sched switch event!");
499 return -1;
500 }
501
502 ent->tid = sample->tid;
503 memcpy(&ent->event, event, event->header.size);
504 list_add(&ent->node, &inject->samples);
505 return 0;
506}
507
508static int perf_inject__sched_stat(struct perf_tool *tool,
509 union perf_event *event __maybe_unused,
510 struct perf_sample *sample,
511 struct perf_evsel *evsel,
512 struct machine *machine)
513{
514 struct event_entry *ent;
515 union perf_event *event_sw;
516 struct perf_sample sample_sw;
517 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
518 u32 pid = perf_evsel__intval(evsel, sample, "pid");
519
520 list_for_each_entry(ent, &inject->samples, node) {
521 if (pid == ent->tid)
522 goto found;
523 }
524
525 return 0;
526found:
527 event_sw = &ent->event[0];
528 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
529
530 sample_sw.period = sample->period;
531 sample_sw.time = sample->time;
532 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
936f1f30 533 evsel->attr.read_format, &sample_sw);
54a3cf59 534 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
26a031e1
AV
535 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
536}
537
1d037ca1 538static void sig_handler(int sig __maybe_unused)
454c407e
TZ
539{
540 session_done = 1;
541}
542
26a031e1
AV
543static int perf_evsel__check_stype(struct perf_evsel *evsel,
544 u64 sample_type, const char *sample_msg)
545{
546 struct perf_event_attr *attr = &evsel->attr;
547 const char *name = perf_evsel__name(evsel);
548
549 if (!(attr->sample_type & sample_type)) {
550 pr_err("Samples for %s event do not have %s attribute set.",
551 name, sample_msg);
552 return -EINVAL;
553 }
554
555 return 0;
556}
557
f56fb986
AH
558static int drop_sample(struct perf_tool *tool __maybe_unused,
559 union perf_event *event __maybe_unused,
560 struct perf_sample *sample __maybe_unused,
561 struct perf_evsel *evsel __maybe_unused,
562 struct machine *machine __maybe_unused)
563{
564 return 0;
565}
566
567static void strip_init(struct perf_inject *inject)
568{
569 struct perf_evlist *evlist = inject->session->evlist;
570 struct perf_evsel *evsel;
571
572 inject->tool.context_switch = perf_event__drop;
573
e5cadb93 574 evlist__for_each_entry(evlist, evsel)
f56fb986
AH
575 evsel->handler = drop_sample;
576}
577
578static bool has_tracking(struct perf_evsel *evsel)
579{
580 return evsel->attr.mmap || evsel->attr.mmap2 || evsel->attr.comm ||
581 evsel->attr.task;
582}
583
584#define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
585 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
586
587/*
588 * In order that the perf.data file is parsable, tracking events like MMAP need
589 * their selected event to exist, except if there is only 1 selected event left
590 * and it has a compatible sample type.
591 */
592static bool ok_to_remove(struct perf_evlist *evlist,
593 struct perf_evsel *evsel_to_remove)
594{
595 struct perf_evsel *evsel;
596 int cnt = 0;
597 bool ok = false;
598
599 if (!has_tracking(evsel_to_remove))
600 return true;
601
e5cadb93 602 evlist__for_each_entry(evlist, evsel) {
f56fb986
AH
603 if (evsel->handler != drop_sample) {
604 cnt += 1;
605 if ((evsel->attr.sample_type & COMPAT_MASK) ==
606 (evsel_to_remove->attr.sample_type & COMPAT_MASK))
607 ok = true;
608 }
609 }
610
611 return ok && cnt == 1;
612}
613
614static void strip_fini(struct perf_inject *inject)
615{
616 struct perf_evlist *evlist = inject->session->evlist;
617 struct perf_evsel *evsel, *tmp;
618
619 /* Remove non-synthesized evsels if possible */
e5cadb93 620 evlist__for_each_entry_safe(evlist, tmp, evsel) {
f56fb986
AH
621 if (evsel->handler == drop_sample &&
622 ok_to_remove(evlist, evsel)) {
623 pr_debug("Deleting %s\n", perf_evsel__name(evsel));
624 perf_evlist__remove(evlist, evsel);
625 perf_evsel__delete(evsel);
626 }
627 }
628}
629
5ded57ac 630static int __cmd_inject(struct perf_inject *inject)
454c407e 631{
454c407e 632 int ret = -EINVAL;
1cb8bdcc 633 struct perf_session *session = inject->session;
8ceb41d7
JO
634 struct perf_data *data_out = &inject->output;
635 int fd = perf_data__fd(data_out);
0f0aa5e0 636 u64 output_data_offset;
454c407e
TZ
637
638 signal(SIGINT, sig_handler);
639
0f0aa5e0
AH
640 if (inject->build_ids || inject->sched_stat ||
641 inject->itrace_synth_opts.set) {
5ded57ac 642 inject->tool.mmap = perf_event__repipe_mmap;
5c5e854b 643 inject->tool.mmap2 = perf_event__repipe_mmap2;
f62d3f0f 644 inject->tool.fork = perf_event__repipe_fork;
5ded57ac 645 inject->tool.tracing_data = perf_event__repipe_tracing_data;
454c407e
TZ
646 }
647
0f0aa5e0
AH
648 output_data_offset = session->header.data_offset;
649
54a3cf59
AV
650 if (inject->build_ids) {
651 inject->tool.sample = perf_event__inject_buildid;
652 } else if (inject->sched_stat) {
26a031e1
AV
653 struct perf_evsel *evsel;
654
e5cadb93 655 evlist__for_each_entry(session->evlist, evsel) {
26a031e1
AV
656 const char *name = perf_evsel__name(evsel);
657
658 if (!strcmp(name, "sched:sched_switch")) {
659 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
660 return -EINVAL;
661
744a9719 662 evsel->handler = perf_inject__sched_switch;
26a031e1 663 } else if (!strcmp(name, "sched:sched_process_exit"))
744a9719 664 evsel->handler = perf_inject__sched_process_exit;
26a031e1 665 else if (!strncmp(name, "sched:sched_stat_", 17))
744a9719 666 evsel->handler = perf_inject__sched_stat;
26a031e1 667 }
0f0aa5e0
AH
668 } else if (inject->itrace_synth_opts.set) {
669 session->itrace_synth_opts = &inject->itrace_synth_opts;
670 inject->itrace_synth_opts.inject = true;
671 inject->tool.comm = perf_event__repipe_comm;
f3b3614a 672 inject->tool.namespaces = perf_event__repipe_namespaces;
0f0aa5e0
AH
673 inject->tool.exit = perf_event__repipe_exit;
674 inject->tool.id_index = perf_event__repipe_id_index;
675 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
676 inject->tool.auxtrace = perf_event__process_auxtrace;
73117308
AH
677 inject->tool.aux = perf_event__drop_aux;
678 inject->tool.itrace_start = perf_event__drop_aux,
0f0aa5e0
AH
679 inject->tool.ordered_events = true;
680 inject->tool.ordering_requires_timestamps = true;
681 /* Allow space in the header for new attributes */
682 output_data_offset = 4096;
f56fb986
AH
683 if (inject->strip)
684 strip_init(inject);
26a031e1
AV
685 }
686
99fa2984
AH
687 if (!inject->itrace_synth_opts.set)
688 auxtrace_index__free(&session->auxtrace_index);
689
8ceb41d7 690 if (!data_out->is_pipe)
0f0aa5e0 691 lseek(fd, output_data_offset, SEEK_SET);
e558a5bd 692
b7b61cbe 693 ret = perf_session__process_events(session);
bb8d521f
DCC
694 if (ret)
695 return ret;
454c407e 696
8ceb41d7 697 if (!data_out->is_pipe) {
640dad47 698 if (inject->build_ids)
e38b43c3
AH
699 perf_header__set_feat(&session->header,
700 HEADER_BUILD_ID);
640dad47
AH
701 /*
702 * Keep all buildids when there is unprocessed AUX data because
703 * it is not known which ones the AUX trace hits.
704 */
705 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
706 inject->have_auxtrace && !inject->itrace_synth_opts.set)
707 dsos__hit_all(session);
0f0aa5e0
AH
708 /*
709 * The AUX areas have been removed and replaced with
73117308
AH
710 * synthesized hardware events, so clear the feature flag and
711 * remove the evsel.
0f0aa5e0 712 */
051a01b9 713 if (inject->itrace_synth_opts.set) {
73117308
AH
714 struct perf_evsel *evsel;
715
0f0aa5e0
AH
716 perf_header__clear_feat(&session->header,
717 HEADER_AUXTRACE);
051a01b9
AH
718 if (inject->itrace_synth_opts.last_branch)
719 perf_header__set_feat(&session->header,
720 HEADER_BRANCH_STACK);
73117308
AH
721 evsel = perf_evlist__id2evsel_strict(session->evlist,
722 inject->aux_id);
723 if (evsel) {
724 pr_debug("Deleting %s\n",
725 perf_evsel__name(evsel));
726 perf_evlist__remove(session->evlist, evsel);
727 perf_evsel__delete(evsel);
728 }
f56fb986
AH
729 if (inject->strip)
730 strip_fini(inject);
051a01b9 731 }
0f0aa5e0 732 session->header.data_offset = output_data_offset;
e558a5bd 733 session->header.data_size = inject->bytes_written;
42aa276f 734 perf_session__write_header(session, session->evlist, fd, true);
e558a5bd
AV
735 }
736
454c407e
TZ
737 return ret;
738}
739
b0ad8ea6 740int cmd_inject(int argc, const char **argv)
454c407e 741{
5ded57ac
ACM
742 struct perf_inject inject = {
743 .tool = {
744 .sample = perf_event__repipe_sample,
745 .mmap = perf_event__repipe,
5c5e854b 746 .mmap2 = perf_event__repipe,
5ded57ac
ACM
747 .comm = perf_event__repipe,
748 .fork = perf_event__repipe,
749 .exit = perf_event__repipe,
750 .lost = perf_event__repipe,
d8145b3e 751 .lost_samples = perf_event__repipe,
4a96f7a0 752 .aux = perf_event__repipe,
0ad21f68 753 .itrace_start = perf_event__repipe,
0286039f 754 .context_switch = perf_event__repipe,
5ded57ac
ACM
755 .read = perf_event__repipe_sample,
756 .throttle = perf_event__repipe,
757 .unthrottle = perf_event__repipe,
758 .attr = perf_event__repipe_attr,
47c3d109 759 .tracing_data = perf_event__repipe_op2_synth,
cd17a9b5
AH
760 .auxtrace_info = perf_event__repipe_op2_synth,
761 .auxtrace = perf_event__repipe_auxtrace,
762 .auxtrace_error = perf_event__repipe_op2_synth,
46bc29b9 763 .time_conv = perf_event__repipe_op2_synth,
d704ebda 764 .finished_round = perf_event__repipe_oe_synth,
5ded57ac 765 .build_id = perf_event__repipe_op2_synth,
3c659eed 766 .id_index = perf_event__repipe_op2_synth,
e9def1b2 767 .feature = perf_event__repipe_op2_synth,
5ded57ac 768 },
e558a5bd 769 .input_name = "-",
26a031e1 770 .samples = LIST_HEAD_INIT(inject.samples),
3406912c 771 .output = {
eae8ad80
JO
772 .file = {
773 .path = "-",
774 },
775 .mode = PERF_DATA_MODE_WRITE,
3406912c 776 },
5ded57ac 777 };
8ceb41d7 778 struct perf_data data = {
1cb8bdcc
NK
779 .mode = PERF_DATA_MODE_READ,
780 };
781 int ret;
782
9b07e27f 783 struct option options[] = {
5ded57ac
ACM
784 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
785 "Inject build-ids into the output stream"),
e558a5bd
AV
786 OPT_STRING('i', "input", &inject.input_name, "file",
787 "input file name"),
eae8ad80 788 OPT_STRING('o', "output", &inject.output.file.path, "file",
e558a5bd 789 "output file name"),
26a031e1
AV
790 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"),
e12b202f 793#ifdef HAVE_JITDUMP
9b07e27f 794 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
e12b202f 795#endif
5ded57ac
ACM
796 OPT_INCR('v', "verbose", &verbose,
797 "be more verbose (show build ids, etc)"),
a7a2b8b4
AH
798 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
799 "kallsyms pathname"),
8ceb41d7 800 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
0f0aa5e0
AH
801 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
802 NULL, "opts", "Instruction Tracing options",
803 itrace_parse_synth_opts),
f56fb986
AH
804 OPT_BOOLEAN(0, "strip", &inject.strip,
805 "strip non-synthesized events (use with --itrace)"),
5ded57ac
ACM
806 OPT_END()
807 };
002439e8
ACM
808 const char * const inject_usage[] = {
809 "perf inject [<options>]",
810 NULL
811 };
e12b202f 812#ifndef HAVE_JITDUMP
9b07e27f
SE
813 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
814#endif
002439e8 815 argc = parse_options(argc, argv, options, inject_usage, 0);
454c407e
TZ
816
817 /*
818 * Any (unrecognized) arguments left?
819 */
820 if (argc)
002439e8 821 usage_with_options(inject_usage, options);
454c407e 822
f56fb986
AH
823 if (inject.strip && !inject.itrace_synth_opts.set) {
824 pr_err("--strip option requires --itrace option\n");
825 return -1;
826 }
827
8ceb41d7 828 if (perf_data__open(&inject.output)) {
3406912c
JO
829 perror("failed to create output file");
830 return -1;
e558a5bd
AV
831 }
832
b7b61cbe
ACM
833 inject.tool.ordered_events = inject.sched_stat;
834
eae8ad80 835 data.file.path = inject.input_name;
8ceb41d7 836 inject.session = perf_session__new(&data, true, &inject.tool);
1cb8bdcc 837 if (inject.session == NULL)
52e02834 838 return -1;
1cb8bdcc 839
921f3fad
ACM
840 if (inject.build_ids) {
841 /*
842 * to make sure the mmap records are ordered correctly
843 * and so that the correct especially due to jitted code
844 * mmaps. We cannot generate the buildid hit list and
845 * inject the jit mmaps at the same time for now.
846 */
847 inject.tool.ordered_events = true;
848 inject.tool.ordering_requires_timestamps = true;
849 }
e12b202f 850#ifdef HAVE_JITDUMP
9b07e27f 851 if (inject.jit_mode) {
9b07e27f
SE
852 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
853 inject.tool.mmap = perf_event__jit_repipe_mmap;
854 inject.tool.ordered_events = true;
855 inject.tool.ordering_requires_timestamps = true;
856 /*
857 * JIT MMAP injection injects all MMAP events in one go, so it
858 * does not obey finished_round semantics.
859 */
860 inject.tool.finished_round = perf_event__drop_oe;
861 }
862#endif
9fedfb0c
TS
863 ret = symbol__init(&inject.session->header.env);
864 if (ret < 0)
865 goto out_delete;
454c407e 866
1cb8bdcc
NK
867 ret = __cmd_inject(&inject);
868
9fedfb0c 869out_delete:
1cb8bdcc 870 perf_session__delete(inject.session);
1cb8bdcc 871 return ret;
454c407e 872}