1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin inject command: Examine the live mode (stdin) event stream
6 * and repipe it to stdout while optionally injecting additional
11 #include "util/color.h"
13 #include "util/vdso.h"
14 #include "util/evlist.h"
15 #include "util/evsel.h"
17 #include "util/session.h"
18 #include "util/tool.h"
19 #include "util/debug.h"
20 #include "util/build-id.h"
21 #include "util/data.h"
22 #include "util/auxtrace.h"
24 #include "util/symbol.h"
25 #include "util/synthetic-events.h"
26 #include "util/thread.h"
27 #include "util/namespaces.h"
28 #include "util/util.h"
30 #include <linux/err.h>
31 #include <subcmd/parse-options.h>
32 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
34 #include <linux/list.h>
35 #include <linux/string.h>
40 struct perf_tool tool;
41 struct perf_session *session;
49 bool in_place_update_dry_run;
51 const char *input_name;
52 struct perf_data output;
55 struct list_head samples;
56 struct itrace_synth_opts itrace_synth_opts;
57 char event_copy[PERF_SAMPLE_MAX_SIZE];
61 struct list_head node;
63 union perf_event event[];
66 static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
67 struct machine *machine, u8 cpumode, u32 flags);
69 static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
73 size = perf_data__write(&inject->output, buf, sz);
77 inject->bytes_written += size;
81 static int perf_event__repipe_synth(struct perf_tool *tool,
82 union perf_event *event)
84 struct perf_inject *inject = container_of(tool, struct perf_inject,
87 return output_bytes(inject, event, event->header.size);
90 static int perf_event__repipe_oe_synth(struct perf_tool *tool,
91 union perf_event *event,
92 struct ordered_events *oe __maybe_unused)
94 return perf_event__repipe_synth(tool, event);
98 static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
99 union perf_event *event __maybe_unused,
100 struct ordered_events *oe __maybe_unused)
106 static int perf_event__repipe_op2_synth(struct perf_session *session,
107 union perf_event *event)
109 return perf_event__repipe_synth(session->tool, event);
112 static int perf_event__repipe_op4_synth(struct perf_session *session,
113 union perf_event *event,
114 u64 data __maybe_unused,
115 const char *str __maybe_unused)
117 return perf_event__repipe_synth(session->tool, event);
120 static int perf_event__repipe_attr(struct perf_tool *tool,
121 union perf_event *event,
122 struct evlist **pevlist)
124 struct perf_inject *inject = container_of(tool, struct perf_inject,
128 ret = perf_event__process_attr(tool, event, pevlist);
132 if (!inject->is_pipe)
135 return perf_event__repipe_synth(tool, event);
138 static int perf_event__repipe_event_update(struct perf_tool *tool,
139 union perf_event *event,
140 struct evlist **pevlist __maybe_unused)
142 return perf_event__repipe_synth(tool, event);
145 #ifdef HAVE_AUXTRACE_SUPPORT
147 static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
154 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
157 ret = output_bytes(inject, buf, ssz);
166 static s64 perf_event__repipe_auxtrace(struct perf_session *session,
167 union perf_event *event)
169 struct perf_tool *tool = session->tool;
170 struct perf_inject *inject = container_of(tool, struct perf_inject,
174 inject->have_auxtrace = true;
176 if (!inject->output.is_pipe) {
179 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
182 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
188 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
189 ret = output_bytes(inject, event, event->header.size);
192 ret = copy_bytes(inject, perf_data__fd(session->data),
193 event->auxtrace.size);
195 ret = output_bytes(inject, event,
196 event->header.size + event->auxtrace.size);
201 return event->auxtrace.size;
207 perf_event__repipe_auxtrace(struct perf_session *session __maybe_unused,
208 union perf_event *event __maybe_unused)
210 pr_err("AUX area tracing not supported\n");
216 static int perf_event__repipe(struct perf_tool *tool,
217 union perf_event *event,
218 struct perf_sample *sample __maybe_unused,
219 struct machine *machine __maybe_unused)
221 return perf_event__repipe_synth(tool, event);
224 static int perf_event__drop(struct perf_tool *tool __maybe_unused,
225 union perf_event *event __maybe_unused,
226 struct perf_sample *sample __maybe_unused,
227 struct machine *machine __maybe_unused)
232 static int perf_event__drop_aux(struct perf_tool *tool,
233 union perf_event *event __maybe_unused,
234 struct perf_sample *sample,
235 struct machine *machine __maybe_unused)
237 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
240 inject->aux_id = sample->id;
245 static union perf_event *
246 perf_inject__cut_auxtrace_sample(struct perf_inject *inject,
247 union perf_event *event,
248 struct perf_sample *sample)
250 size_t sz1 = sample->aux_sample.data - (void *)event;
251 size_t sz2 = event->header.size - sample->aux_sample.size - sz1;
252 union perf_event *ev = (union perf_event *)inject->event_copy;
254 if (sz1 > event->header.size || sz2 > event->header.size ||
255 sz1 + sz2 > event->header.size ||
256 sz1 < sizeof(struct perf_event_header) + sizeof(u64))
259 memcpy(ev, event, sz1);
260 memcpy((void *)ev + sz1, (void *)event + event->header.size - sz2, sz2);
261 ev->header.size = sz1 + sz2;
262 ((u64 *)((void *)ev + sz1))[-1] = 0;
267 typedef int (*inject_handler)(struct perf_tool *tool,
268 union perf_event *event,
269 struct perf_sample *sample,
271 struct machine *machine);
273 static int perf_event__repipe_sample(struct perf_tool *tool,
274 union perf_event *event,
275 struct perf_sample *sample,
277 struct machine *machine)
279 struct perf_inject *inject = container_of(tool, struct perf_inject,
282 if (evsel && evsel->handler) {
283 inject_handler f = evsel->handler;
284 return f(tool, event, sample, evsel, machine);
287 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
289 if (inject->itrace_synth_opts.set && sample->aux_sample.size)
290 event = perf_inject__cut_auxtrace_sample(inject, event, sample);
292 return perf_event__repipe_synth(tool, event);
295 static int perf_event__repipe_mmap(struct perf_tool *tool,
296 union perf_event *event,
297 struct perf_sample *sample,
298 struct machine *machine)
302 err = perf_event__process_mmap(tool, event, sample, machine);
303 perf_event__repipe(tool, event, sample, machine);
309 static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
310 union perf_event *event,
311 struct perf_sample *sample,
312 struct machine *machine)
314 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
319 * if jit marker, then inject jit mmaps and generate ELF images
321 ret = jit_process(inject->session, &inject->output, machine,
322 event->mmap.filename, event->mmap.pid, event->mmap.tid, &n);
326 inject->bytes_written += n;
329 return perf_event__repipe_mmap(tool, event, sample, machine);
333 static struct dso *findnew_dso(int pid, int tid, const char *filename,
334 struct dso_id *id, struct machine *machine)
336 struct thread *thread;
337 struct nsinfo *nsi = NULL;
342 thread = machine__findnew_thread(machine, pid, tid);
343 if (thread == NULL) {
344 pr_err("cannot find or create a task %d/%d.\n", tid, pid);
348 vdso = is_vdso_map(filename);
349 nsi = nsinfo__get(thread->nsinfo);
352 /* The vdso maps are always on the host and not the
353 * container. Ensure that we don't use setns to look
356 nnsi = nsinfo__copy(nsi);
359 nsinfo__clear_need_setns(nnsi);
362 dso = machine__findnew_vdso(machine, thread);
364 dso = machine__findnew_dso_id(machine, filename, id);
368 nsinfo__put(dso->nsinfo);
377 static int perf_event__repipe_buildid_mmap(struct perf_tool *tool,
378 union perf_event *event,
379 struct perf_sample *sample,
380 struct machine *machine)
384 dso = findnew_dso(event->mmap.pid, event->mmap.tid,
385 event->mmap.filename, NULL, machine);
387 if (dso && !dso->hit) {
389 dso__inject_build_id(dso, tool, machine, sample->cpumode, 0);
393 return perf_event__repipe(tool, event, sample, machine);
396 static int perf_event__repipe_mmap2(struct perf_tool *tool,
397 union perf_event *event,
398 struct perf_sample *sample,
399 struct machine *machine)
403 err = perf_event__process_mmap2(tool, event, sample, machine);
404 perf_event__repipe(tool, event, sample, machine);
406 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
409 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
410 event->mmap2.filename, NULL, machine);
412 /* mark it not to inject build-id */
422 static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
423 union perf_event *event,
424 struct perf_sample *sample,
425 struct machine *machine)
427 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
432 * if jit marker, then inject jit mmaps and generate ELF images
434 ret = jit_process(inject->session, &inject->output, machine,
435 event->mmap2.filename, event->mmap2.pid, event->mmap2.tid, &n);
439 inject->bytes_written += n;
442 return perf_event__repipe_mmap2(tool, event, sample, machine);
446 static int perf_event__repipe_buildid_mmap2(struct perf_tool *tool,
447 union perf_event *event,
448 struct perf_sample *sample,
449 struct machine *machine)
451 struct dso_id dso_id = {
452 .maj = event->mmap2.maj,
453 .min = event->mmap2.min,
454 .ino = event->mmap2.ino,
455 .ino_generation = event->mmap2.ino_generation,
459 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
460 /* cannot use dso_id since it'd have invalid info */
461 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
462 event->mmap2.filename, NULL, machine);
464 /* mark it not to inject build-id */
471 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
472 event->mmap2.filename, &dso_id, machine);
474 if (dso && !dso->hit) {
476 dso__inject_build_id(dso, tool, machine, sample->cpumode,
481 perf_event__repipe(tool, event, sample, machine);
486 static int perf_event__repipe_fork(struct perf_tool *tool,
487 union perf_event *event,
488 struct perf_sample *sample,
489 struct machine *machine)
493 err = perf_event__process_fork(tool, event, sample, machine);
494 perf_event__repipe(tool, event, sample, machine);
499 static int perf_event__repipe_comm(struct perf_tool *tool,
500 union perf_event *event,
501 struct perf_sample *sample,
502 struct machine *machine)
506 err = perf_event__process_comm(tool, event, sample, machine);
507 perf_event__repipe(tool, event, sample, machine);
512 static int perf_event__repipe_namespaces(struct perf_tool *tool,
513 union perf_event *event,
514 struct perf_sample *sample,
515 struct machine *machine)
517 int err = perf_event__process_namespaces(tool, event, sample, machine);
519 perf_event__repipe(tool, event, sample, machine);
524 static int perf_event__repipe_exit(struct perf_tool *tool,
525 union perf_event *event,
526 struct perf_sample *sample,
527 struct machine *machine)
531 err = perf_event__process_exit(tool, event, sample, machine);
532 perf_event__repipe(tool, event, sample, machine);
537 static int perf_event__repipe_tracing_data(struct perf_session *session,
538 union perf_event *event)
540 perf_event__repipe_synth(session->tool, event);
542 return perf_event__process_tracing_data(session, event);
545 static int dso__read_build_id(struct dso *dso)
549 if (dso->has_build_id)
552 nsinfo__mountns_enter(dso->nsinfo, &nsc);
553 if (filename__read_build_id(dso->long_name, &dso->bid) > 0)
554 dso->has_build_id = true;
555 else if (dso->nsinfo) {
558 new_name = filename_with_chroot(dso->nsinfo->pid,
560 if (new_name && filename__read_build_id(new_name, &dso->bid) > 0)
561 dso->has_build_id = true;
564 nsinfo__mountns_exit(&nsc);
566 return dso->has_build_id ? 0 : -1;
569 static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
570 struct machine *machine, u8 cpumode, u32 flags)
574 if (is_anon_memory(dso->long_name) || flags & MAP_HUGETLB)
576 if (is_no_dso_memory(dso->long_name))
579 if (dso__read_build_id(dso) < 0) {
580 pr_debug("no build_id found for %s\n", dso->long_name);
584 err = perf_event__synthesize_build_id(tool, dso, cpumode,
585 perf_event__repipe, machine);
587 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
594 int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,
595 struct perf_sample *sample,
596 struct evsel *evsel __maybe_unused,
597 struct machine *machine)
599 struct addr_location al;
600 struct thread *thread;
602 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
603 if (thread == NULL) {
604 pr_err("problem processing %d event, skipping it.\n",
609 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
610 if (!al.map->dso->hit) {
611 al.map->dso->hit = 1;
612 dso__inject_build_id(al.map->dso, tool, machine,
613 sample->cpumode, al.map->flags);
619 perf_event__repipe(tool, event, sample, machine);
623 static int perf_inject__sched_process_exit(struct perf_tool *tool,
624 union perf_event *event __maybe_unused,
625 struct perf_sample *sample,
626 struct evsel *evsel __maybe_unused,
627 struct machine *machine __maybe_unused)
629 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
630 struct event_entry *ent;
632 list_for_each_entry(ent, &inject->samples, node) {
633 if (sample->tid == ent->tid) {
634 list_del_init(&ent->node);
643 static int perf_inject__sched_switch(struct perf_tool *tool,
644 union perf_event *event,
645 struct perf_sample *sample,
647 struct machine *machine)
649 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
650 struct event_entry *ent;
652 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
654 ent = malloc(event->header.size + sizeof(struct event_entry));
656 color_fprintf(stderr, PERF_COLOR_RED,
657 "Not enough memory to process sched switch event!");
661 ent->tid = sample->tid;
662 memcpy(&ent->event, event, event->header.size);
663 list_add(&ent->node, &inject->samples);
667 static int perf_inject__sched_stat(struct perf_tool *tool,
668 union perf_event *event __maybe_unused,
669 struct perf_sample *sample,
671 struct machine *machine)
673 struct event_entry *ent;
674 union perf_event *event_sw;
675 struct perf_sample sample_sw;
676 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
677 u32 pid = evsel__intval(evsel, sample, "pid");
679 list_for_each_entry(ent, &inject->samples, node) {
686 event_sw = &ent->event[0];
687 evsel__parse_sample(evsel, event_sw, &sample_sw);
689 sample_sw.period = sample->period;
690 sample_sw.time = sample->time;
691 perf_event__synthesize_sample(event_sw, evsel->core.attr.sample_type,
692 evsel->core.attr.read_format, &sample_sw);
693 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
694 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
697 static void sig_handler(int sig __maybe_unused)
702 static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg)
704 struct perf_event_attr *attr = &evsel->core.attr;
705 const char *name = evsel__name(evsel);
707 if (!(attr->sample_type & sample_type)) {
708 pr_err("Samples for %s event do not have %s attribute set.",
716 static int drop_sample(struct perf_tool *tool __maybe_unused,
717 union perf_event *event __maybe_unused,
718 struct perf_sample *sample __maybe_unused,
719 struct evsel *evsel __maybe_unused,
720 struct machine *machine __maybe_unused)
725 static void strip_init(struct perf_inject *inject)
727 struct evlist *evlist = inject->session->evlist;
730 inject->tool.context_switch = perf_event__drop;
732 evlist__for_each_entry(evlist, evsel)
733 evsel->handler = drop_sample;
736 static int parse_vm_time_correlation(const struct option *opt, const char *str, int unset)
738 struct perf_inject *inject = opt->value;
745 inject->itrace_synth_opts.set = true;
746 inject->itrace_synth_opts.vm_time_correlation = true;
747 inject->in_place_update = true;
752 dry_run = skip_spaces(str);
753 if (!strncmp(dry_run, "dry-run", strlen("dry-run"))) {
754 inject->itrace_synth_opts.vm_tm_corr_dry_run = true;
755 inject->in_place_update_dry_run = true;
756 args = dry_run + strlen("dry-run");
761 inject->itrace_synth_opts.vm_tm_corr_args = strdup(args);
763 return inject->itrace_synth_opts.vm_tm_corr_args ? 0 : -ENOMEM;
766 static int output_fd(struct perf_inject *inject)
768 return inject->in_place_update ? -1 : perf_data__fd(&inject->output);
771 static int __cmd_inject(struct perf_inject *inject)
774 struct perf_session *session = inject->session;
775 int fd = output_fd(inject);
776 u64 output_data_offset;
778 signal(SIGINT, sig_handler);
780 if (inject->build_ids || inject->sched_stat ||
781 inject->itrace_synth_opts.set || inject->build_id_all) {
782 inject->tool.mmap = perf_event__repipe_mmap;
783 inject->tool.mmap2 = perf_event__repipe_mmap2;
784 inject->tool.fork = perf_event__repipe_fork;
785 inject->tool.tracing_data = perf_event__repipe_tracing_data;
788 output_data_offset = session->header.data_offset;
790 if (inject->build_id_all) {
791 inject->tool.mmap = perf_event__repipe_buildid_mmap;
792 inject->tool.mmap2 = perf_event__repipe_buildid_mmap2;
793 } else if (inject->build_ids) {
794 inject->tool.sample = perf_event__inject_buildid;
795 } else if (inject->sched_stat) {
798 evlist__for_each_entry(session->evlist, evsel) {
799 const char *name = evsel__name(evsel);
801 if (!strcmp(name, "sched:sched_switch")) {
802 if (evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
805 evsel->handler = perf_inject__sched_switch;
806 } else if (!strcmp(name, "sched:sched_process_exit"))
807 evsel->handler = perf_inject__sched_process_exit;
808 else if (!strncmp(name, "sched:sched_stat_", 17))
809 evsel->handler = perf_inject__sched_stat;
811 } else if (inject->itrace_synth_opts.vm_time_correlation) {
812 session->itrace_synth_opts = &inject->itrace_synth_opts;
813 memset(&inject->tool, 0, sizeof(inject->tool));
814 inject->tool.id_index = perf_event__process_id_index;
815 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
816 inject->tool.auxtrace = perf_event__process_auxtrace;
817 inject->tool.auxtrace_error = perf_event__process_auxtrace_error;
818 inject->tool.ordered_events = true;
819 inject->tool.ordering_requires_timestamps = true;
820 } else if (inject->itrace_synth_opts.set) {
821 session->itrace_synth_opts = &inject->itrace_synth_opts;
822 inject->itrace_synth_opts.inject = true;
823 inject->tool.comm = perf_event__repipe_comm;
824 inject->tool.namespaces = perf_event__repipe_namespaces;
825 inject->tool.exit = perf_event__repipe_exit;
826 inject->tool.id_index = perf_event__process_id_index;
827 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
828 inject->tool.auxtrace = perf_event__process_auxtrace;
829 inject->tool.aux = perf_event__drop_aux;
830 inject->tool.itrace_start = perf_event__drop_aux;
831 inject->tool.aux_output_hw_id = perf_event__drop_aux;
832 inject->tool.ordered_events = true;
833 inject->tool.ordering_requires_timestamps = true;
834 /* Allow space in the header for new attributes */
835 output_data_offset = roundup(8192 + session->header.data_offset, 4096);
840 if (!inject->itrace_synth_opts.set)
841 auxtrace_index__free(&session->auxtrace_index);
843 if (!inject->is_pipe && !inject->in_place_update)
844 lseek(fd, output_data_offset, SEEK_SET);
846 ret = perf_session__process_events(session);
850 if (!inject->is_pipe && !inject->in_place_update) {
851 if (inject->build_ids)
852 perf_header__set_feat(&session->header,
855 * Keep all buildids when there is unprocessed AUX data because
856 * it is not known which ones the AUX trace hits.
858 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
859 inject->have_auxtrace && !inject->itrace_synth_opts.set)
860 dsos__hit_all(session);
862 * The AUX areas have been removed and replaced with
863 * synthesized hardware events, so clear the feature flag.
865 if (inject->itrace_synth_opts.set) {
866 perf_header__clear_feat(&session->header,
868 if (inject->itrace_synth_opts.last_branch ||
869 inject->itrace_synth_opts.add_last_branch)
870 perf_header__set_feat(&session->header,
871 HEADER_BRANCH_STACK);
873 session->header.data_offset = output_data_offset;
874 session->header.data_size = inject->bytes_written;
875 perf_session__write_header(session, session->evlist, fd, true);
881 int cmd_inject(int argc, const char **argv)
883 struct perf_inject inject = {
885 .sample = perf_event__repipe_sample,
886 .read = perf_event__repipe_sample,
887 .mmap = perf_event__repipe,
888 .mmap2 = perf_event__repipe,
889 .comm = perf_event__repipe,
890 .namespaces = perf_event__repipe,
891 .cgroup = perf_event__repipe,
892 .fork = perf_event__repipe,
893 .exit = perf_event__repipe,
894 .lost = perf_event__repipe,
895 .lost_samples = perf_event__repipe,
896 .aux = perf_event__repipe,
897 .itrace_start = perf_event__repipe,
898 .aux_output_hw_id = perf_event__repipe,
899 .context_switch = perf_event__repipe,
900 .throttle = perf_event__repipe,
901 .unthrottle = perf_event__repipe,
902 .ksymbol = perf_event__repipe,
903 .bpf = perf_event__repipe,
904 .text_poke = perf_event__repipe,
905 .attr = perf_event__repipe_attr,
906 .event_update = perf_event__repipe_event_update,
907 .tracing_data = perf_event__repipe_op2_synth,
908 .finished_round = perf_event__repipe_oe_synth,
909 .build_id = perf_event__repipe_op2_synth,
910 .id_index = perf_event__repipe_op2_synth,
911 .auxtrace_info = perf_event__repipe_op2_synth,
912 .auxtrace_error = perf_event__repipe_op2_synth,
913 .time_conv = perf_event__repipe_op2_synth,
914 .thread_map = perf_event__repipe_op2_synth,
915 .cpu_map = perf_event__repipe_op2_synth,
916 .stat_config = perf_event__repipe_op2_synth,
917 .stat = perf_event__repipe_op2_synth,
918 .stat_round = perf_event__repipe_op2_synth,
919 .feature = perf_event__repipe_op2_synth,
920 .compressed = perf_event__repipe_op4_synth,
921 .auxtrace = perf_event__repipe_auxtrace,
924 .samples = LIST_HEAD_INIT(inject.samples),
927 .mode = PERF_DATA_MODE_WRITE,
931 struct perf_data data = {
932 .mode = PERF_DATA_MODE_READ,
938 struct option options[] = {
939 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
940 "Inject build-ids into the output stream"),
941 OPT_BOOLEAN(0, "buildid-all", &inject.build_id_all,
942 "Inject build-ids of all DSOs into the output stream"),
943 OPT_STRING('i', "input", &inject.input_name, "file",
945 OPT_STRING('o', "output", &inject.output.path, "file",
947 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
948 "Merge sched-stat and sched-switch for getting events "
949 "where and how long tasks slept"),
951 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
953 OPT_INCR('v', "verbose", &verbose,
954 "be more verbose (show build ids, etc)"),
955 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
956 "file", "vmlinux pathname"),
957 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
958 "don't load vmlinux even if found"),
959 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
960 "kallsyms pathname"),
961 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
962 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
963 NULL, "opts", "Instruction Tracing options\n"
965 itrace_parse_synth_opts),
966 OPT_BOOLEAN(0, "strip", &inject.strip,
967 "strip non-synthesized events (use with --itrace)"),
968 OPT_CALLBACK_OPTARG(0, "vm-time-correlation", &inject, NULL, "opts",
969 "correlate time between VM guests and the host",
970 parse_vm_time_correlation),
973 const char * const inject_usage[] = {
974 "perf inject [<options>]",
978 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
980 argc = parse_options(argc, argv, options, inject_usage, 0);
983 * Any (unrecognized) arguments left?
986 usage_with_options(inject_usage, options);
988 if (inject.strip && !inject.itrace_synth_opts.set) {
989 pr_err("--strip option requires --itrace option\n");
993 if (symbol__validate_sym_arguments())
996 if (inject.in_place_update) {
997 if (!strcmp(inject.input_name, "-")) {
998 pr_err("Input file name required for in-place updating\n");
1001 if (strcmp(inject.output.path, "-")) {
1002 pr_err("Output file name must not be specified for in-place updating\n");
1005 if (!data.force && !inject.in_place_update_dry_run) {
1006 pr_err("The input file would be updated in place, "
1007 "the --force option is required.\n");
1010 if (!inject.in_place_update_dry_run)
1011 data.in_place_update = true;
1012 } else if (perf_data__open(&inject.output)) {
1013 perror("failed to create output file");
1017 data.path = inject.input_name;
1018 if (!strcmp(inject.input_name, "-") || inject.output.is_pipe) {
1019 inject.is_pipe = true;
1021 * Do not repipe header when input is a regular file
1022 * since either it can rewrite the header at the end
1023 * or write a new pipe header.
1025 if (strcmp(inject.input_name, "-"))
1029 inject.session = __perf_session__new(&data, repipe,
1032 if (IS_ERR(inject.session)) {
1033 ret = PTR_ERR(inject.session);
1034 goto out_close_output;
1037 if (zstd_init(&(inject.session->zstd_data), 0) < 0)
1038 pr_warning("Decompression initialization failed.\n");
1040 if (!data.is_pipe && inject.output.is_pipe) {
1041 ret = perf_header__write_pipe(perf_data__fd(&inject.output));
1043 pr_err("Couldn't write a new pipe header.\n");
1047 ret = perf_event__synthesize_for_pipe(&inject.tool,
1050 perf_event__repipe);
1055 if (inject.build_ids && !inject.build_id_all) {
1057 * to make sure the mmap records are ordered correctly
1058 * and so that the correct especially due to jitted code
1059 * mmaps. We cannot generate the buildid hit list and
1060 * inject the jit mmaps at the same time for now.
1062 inject.tool.ordered_events = true;
1063 inject.tool.ordering_requires_timestamps = true;
1066 if (inject.sched_stat) {
1067 inject.tool.ordered_events = true;
1071 if (inject.jit_mode) {
1072 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
1073 inject.tool.mmap = perf_event__jit_repipe_mmap;
1074 inject.tool.ordered_events = true;
1075 inject.tool.ordering_requires_timestamps = true;
1077 * JIT MMAP injection injects all MMAP events in one go, so it
1078 * does not obey finished_round semantics.
1080 inject.tool.finished_round = perf_event__drop_oe;
1083 ret = symbol__init(&inject.session->header.env);
1087 ret = __cmd_inject(&inject);
1090 zstd_fini(&(inject.session->zstd_data));
1091 perf_session__delete(inject.session);
1093 if (!inject.in_place_update)
1094 perf_data__close(&inject.output);
1095 free(inject.itrace_synth_opts.vm_tm_corr_args);