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