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 <internal/lib.h>
32 #include <linux/err.h>
33 #include <subcmd/parse-options.h>
34 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
36 #include <linux/list.h>
37 #include <linux/string.h>
42 struct perf_tool tool;
43 struct perf_session *session;
51 bool in_place_update_dry_run;
54 const char *input_name;
55 struct perf_data output;
58 struct list_head samples;
59 struct itrace_synth_opts itrace_synth_opts;
60 char event_copy[PERF_SAMPLE_MAX_SIZE];
61 struct perf_file_section secs[HEADER_FEAT_BITS];
65 struct list_head node;
67 union perf_event event[];
70 static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
71 struct machine *machine, u8 cpumode, u32 flags);
73 static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
77 size = perf_data__write(&inject->output, buf, sz);
81 inject->bytes_written += size;
85 static int perf_event__repipe_synth(struct perf_tool *tool,
86 union perf_event *event)
88 struct perf_inject *inject = container_of(tool, struct perf_inject,
91 return output_bytes(inject, event, event->header.size);
94 static int perf_event__repipe_oe_synth(struct perf_tool *tool,
95 union perf_event *event,
96 struct ordered_events *oe __maybe_unused)
98 return perf_event__repipe_synth(tool, event);
102 static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
103 union perf_event *event __maybe_unused,
104 struct ordered_events *oe __maybe_unused)
110 static int perf_event__repipe_op2_synth(struct perf_session *session,
111 union perf_event *event)
113 return perf_event__repipe_synth(session->tool, event);
116 static int perf_event__repipe_op4_synth(struct perf_session *session,
117 union perf_event *event,
118 u64 data __maybe_unused,
119 const char *str __maybe_unused)
121 return perf_event__repipe_synth(session->tool, event);
124 static int perf_event__repipe_attr(struct perf_tool *tool,
125 union perf_event *event,
126 struct evlist **pevlist)
128 struct perf_inject *inject = container_of(tool, struct perf_inject,
132 ret = perf_event__process_attr(tool, event, pevlist);
136 if (!inject->is_pipe)
139 return perf_event__repipe_synth(tool, event);
142 static int perf_event__repipe_event_update(struct perf_tool *tool,
143 union perf_event *event,
144 struct evlist **pevlist __maybe_unused)
146 return perf_event__repipe_synth(tool, event);
149 #ifdef HAVE_AUXTRACE_SUPPORT
151 static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
158 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
161 ret = output_bytes(inject, buf, ssz);
170 static s64 perf_event__repipe_auxtrace(struct perf_session *session,
171 union perf_event *event)
173 struct perf_tool *tool = session->tool;
174 struct perf_inject *inject = container_of(tool, struct perf_inject,
178 inject->have_auxtrace = true;
180 if (!inject->output.is_pipe) {
183 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
186 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
192 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
193 ret = output_bytes(inject, event, event->header.size);
196 ret = copy_bytes(inject, perf_data__fd(session->data),
197 event->auxtrace.size);
199 ret = output_bytes(inject, event,
200 event->header.size + event->auxtrace.size);
205 return event->auxtrace.size;
211 perf_event__repipe_auxtrace(struct perf_session *session __maybe_unused,
212 union perf_event *event __maybe_unused)
214 pr_err("AUX area tracing not supported\n");
220 static int perf_event__repipe(struct perf_tool *tool,
221 union perf_event *event,
222 struct perf_sample *sample __maybe_unused,
223 struct machine *machine __maybe_unused)
225 return perf_event__repipe_synth(tool, event);
228 static int perf_event__drop(struct perf_tool *tool __maybe_unused,
229 union perf_event *event __maybe_unused,
230 struct perf_sample *sample __maybe_unused,
231 struct machine *machine __maybe_unused)
236 static int perf_event__drop_aux(struct perf_tool *tool,
237 union perf_event *event __maybe_unused,
238 struct perf_sample *sample,
239 struct machine *machine __maybe_unused)
241 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
244 inject->aux_id = sample->id;
249 static union perf_event *
250 perf_inject__cut_auxtrace_sample(struct perf_inject *inject,
251 union perf_event *event,
252 struct perf_sample *sample)
254 size_t sz1 = sample->aux_sample.data - (void *)event;
255 size_t sz2 = event->header.size - sample->aux_sample.size - sz1;
256 union perf_event *ev = (union perf_event *)inject->event_copy;
258 if (sz1 > event->header.size || sz2 > event->header.size ||
259 sz1 + sz2 > event->header.size ||
260 sz1 < sizeof(struct perf_event_header) + sizeof(u64))
263 memcpy(ev, event, sz1);
264 memcpy((void *)ev + sz1, (void *)event + event->header.size - sz2, sz2);
265 ev->header.size = sz1 + sz2;
266 ((u64 *)((void *)ev + sz1))[-1] = 0;
271 typedef int (*inject_handler)(struct perf_tool *tool,
272 union perf_event *event,
273 struct perf_sample *sample,
275 struct machine *machine);
277 static int perf_event__repipe_sample(struct perf_tool *tool,
278 union perf_event *event,
279 struct perf_sample *sample,
281 struct machine *machine)
283 struct perf_inject *inject = container_of(tool, struct perf_inject,
286 if (evsel && evsel->handler) {
287 inject_handler f = evsel->handler;
288 return f(tool, event, sample, evsel, machine);
291 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
293 if (inject->itrace_synth_opts.set && sample->aux_sample.size)
294 event = perf_inject__cut_auxtrace_sample(inject, event, sample);
296 return perf_event__repipe_synth(tool, event);
299 static int perf_event__repipe_mmap(struct perf_tool *tool,
300 union perf_event *event,
301 struct perf_sample *sample,
302 struct machine *machine)
306 err = perf_event__process_mmap(tool, event, sample, machine);
307 perf_event__repipe(tool, event, sample, machine);
313 static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
314 union perf_event *event,
315 struct perf_sample *sample,
316 struct machine *machine)
318 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
323 * if jit marker, then inject jit mmaps and generate ELF images
325 ret = jit_process(inject->session, &inject->output, machine,
326 event->mmap.filename, event->mmap.pid, event->mmap.tid, &n);
330 inject->bytes_written += n;
333 return perf_event__repipe_mmap(tool, event, sample, machine);
337 static struct dso *findnew_dso(int pid, int tid, const char *filename,
338 struct dso_id *id, struct machine *machine)
340 struct thread *thread;
341 struct nsinfo *nsi = NULL;
346 thread = machine__findnew_thread(machine, pid, tid);
347 if (thread == NULL) {
348 pr_err("cannot find or create a task %d/%d.\n", tid, pid);
352 vdso = is_vdso_map(filename);
353 nsi = nsinfo__get(thread->nsinfo);
356 /* The vdso maps are always on the host and not the
357 * container. Ensure that we don't use setns to look
360 nnsi = nsinfo__copy(nsi);
363 nsinfo__clear_need_setns(nnsi);
366 dso = machine__findnew_vdso(machine, thread);
368 dso = machine__findnew_dso_id(machine, filename, id);
372 nsinfo__put(dso->nsinfo);
381 static int perf_event__repipe_buildid_mmap(struct perf_tool *tool,
382 union perf_event *event,
383 struct perf_sample *sample,
384 struct machine *machine)
388 dso = findnew_dso(event->mmap.pid, event->mmap.tid,
389 event->mmap.filename, NULL, machine);
391 if (dso && !dso->hit) {
393 dso__inject_build_id(dso, tool, machine, sample->cpumode, 0);
397 return perf_event__repipe(tool, event, sample, machine);
400 static int perf_event__repipe_mmap2(struct perf_tool *tool,
401 union perf_event *event,
402 struct perf_sample *sample,
403 struct machine *machine)
407 err = perf_event__process_mmap2(tool, event, sample, machine);
408 perf_event__repipe(tool, event, sample, machine);
410 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
413 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
414 event->mmap2.filename, NULL, machine);
416 /* mark it not to inject build-id */
426 static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
427 union perf_event *event,
428 struct perf_sample *sample,
429 struct machine *machine)
431 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
436 * if jit marker, then inject jit mmaps and generate ELF images
438 ret = jit_process(inject->session, &inject->output, machine,
439 event->mmap2.filename, event->mmap2.pid, event->mmap2.tid, &n);
443 inject->bytes_written += n;
446 return perf_event__repipe_mmap2(tool, event, sample, machine);
450 static int perf_event__repipe_buildid_mmap2(struct perf_tool *tool,
451 union perf_event *event,
452 struct perf_sample *sample,
453 struct machine *machine)
455 struct dso_id dso_id = {
456 .maj = event->mmap2.maj,
457 .min = event->mmap2.min,
458 .ino = event->mmap2.ino,
459 .ino_generation = event->mmap2.ino_generation,
463 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
464 /* cannot use dso_id since it'd have invalid info */
465 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
466 event->mmap2.filename, NULL, machine);
468 /* mark it not to inject build-id */
475 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid,
476 event->mmap2.filename, &dso_id, machine);
478 if (dso && !dso->hit) {
480 dso__inject_build_id(dso, tool, machine, sample->cpumode,
485 perf_event__repipe(tool, event, sample, machine);
490 static int perf_event__repipe_fork(struct perf_tool *tool,
491 union perf_event *event,
492 struct perf_sample *sample,
493 struct machine *machine)
497 err = perf_event__process_fork(tool, event, sample, machine);
498 perf_event__repipe(tool, event, sample, machine);
503 static int perf_event__repipe_comm(struct perf_tool *tool,
504 union perf_event *event,
505 struct perf_sample *sample,
506 struct machine *machine)
510 err = perf_event__process_comm(tool, event, sample, machine);
511 perf_event__repipe(tool, event, sample, machine);
516 static int perf_event__repipe_namespaces(struct perf_tool *tool,
517 union perf_event *event,
518 struct perf_sample *sample,
519 struct machine *machine)
521 int err = perf_event__process_namespaces(tool, event, sample, machine);
523 perf_event__repipe(tool, event, sample, machine);
528 static int perf_event__repipe_exit(struct perf_tool *tool,
529 union perf_event *event,
530 struct perf_sample *sample,
531 struct machine *machine)
535 err = perf_event__process_exit(tool, event, sample, machine);
536 perf_event__repipe(tool, event, sample, machine);
541 static int perf_event__repipe_tracing_data(struct perf_session *session,
542 union perf_event *event)
544 perf_event__repipe_synth(session->tool, event);
546 return perf_event__process_tracing_data(session, event);
549 static int dso__read_build_id(struct dso *dso)
553 if (dso->has_build_id)
556 nsinfo__mountns_enter(dso->nsinfo, &nsc);
557 if (filename__read_build_id(dso->long_name, &dso->bid) > 0)
558 dso->has_build_id = true;
559 else if (dso->nsinfo) {
562 new_name = filename_with_chroot(dso->nsinfo->pid,
564 if (new_name && filename__read_build_id(new_name, &dso->bid) > 0)
565 dso->has_build_id = true;
568 nsinfo__mountns_exit(&nsc);
570 return dso->has_build_id ? 0 : -1;
573 static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
574 struct machine *machine, u8 cpumode, u32 flags)
578 if (is_anon_memory(dso->long_name) || flags & MAP_HUGETLB)
580 if (is_no_dso_memory(dso->long_name))
583 if (dso__read_build_id(dso) < 0) {
584 pr_debug("no build_id found for %s\n", dso->long_name);
588 err = perf_event__synthesize_build_id(tool, dso, cpumode,
589 perf_event__repipe, machine);
591 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
598 int perf_event__inject_buildid(struct perf_tool *tool, union perf_event *event,
599 struct perf_sample *sample,
600 struct evsel *evsel __maybe_unused,
601 struct machine *machine)
603 struct addr_location al;
604 struct thread *thread;
606 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
607 if (thread == NULL) {
608 pr_err("problem processing %d event, skipping it.\n",
613 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
614 if (!al.map->dso->hit) {
615 al.map->dso->hit = 1;
616 dso__inject_build_id(al.map->dso, tool, machine,
617 sample->cpumode, al.map->flags);
623 perf_event__repipe(tool, event, sample, machine);
627 static int perf_inject__sched_process_exit(struct perf_tool *tool,
628 union perf_event *event __maybe_unused,
629 struct perf_sample *sample,
630 struct evsel *evsel __maybe_unused,
631 struct machine *machine __maybe_unused)
633 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
634 struct event_entry *ent;
636 list_for_each_entry(ent, &inject->samples, node) {
637 if (sample->tid == ent->tid) {
638 list_del_init(&ent->node);
647 static int perf_inject__sched_switch(struct perf_tool *tool,
648 union perf_event *event,
649 struct perf_sample *sample,
651 struct machine *machine)
653 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
654 struct event_entry *ent;
656 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
658 ent = malloc(event->header.size + sizeof(struct event_entry));
660 color_fprintf(stderr, PERF_COLOR_RED,
661 "Not enough memory to process sched switch event!");
665 ent->tid = sample->tid;
666 memcpy(&ent->event, event, event->header.size);
667 list_add(&ent->node, &inject->samples);
671 static int perf_inject__sched_stat(struct perf_tool *tool,
672 union perf_event *event __maybe_unused,
673 struct perf_sample *sample,
675 struct machine *machine)
677 struct event_entry *ent;
678 union perf_event *event_sw;
679 struct perf_sample sample_sw;
680 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
681 u32 pid = evsel__intval(evsel, sample, "pid");
683 list_for_each_entry(ent, &inject->samples, node) {
690 event_sw = &ent->event[0];
691 evsel__parse_sample(evsel, event_sw, &sample_sw);
693 sample_sw.period = sample->period;
694 sample_sw.time = sample->time;
695 perf_event__synthesize_sample(event_sw, evsel->core.attr.sample_type,
696 evsel->core.attr.read_format, &sample_sw);
697 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
698 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
701 static void sig_handler(int sig __maybe_unused)
706 static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg)
708 struct perf_event_attr *attr = &evsel->core.attr;
709 const char *name = evsel__name(evsel);
711 if (!(attr->sample_type & sample_type)) {
712 pr_err("Samples for %s event do not have %s attribute set.",
720 static int drop_sample(struct perf_tool *tool __maybe_unused,
721 union perf_event *event __maybe_unused,
722 struct perf_sample *sample __maybe_unused,
723 struct evsel *evsel __maybe_unused,
724 struct machine *machine __maybe_unused)
729 static void strip_init(struct perf_inject *inject)
731 struct evlist *evlist = inject->session->evlist;
734 inject->tool.context_switch = perf_event__drop;
736 evlist__for_each_entry(evlist, evsel)
737 evsel->handler = drop_sample;
740 static int parse_vm_time_correlation(const struct option *opt, const char *str, int unset)
742 struct perf_inject *inject = opt->value;
749 inject->itrace_synth_opts.set = true;
750 inject->itrace_synth_opts.vm_time_correlation = true;
751 inject->in_place_update = true;
756 dry_run = skip_spaces(str);
757 if (!strncmp(dry_run, "dry-run", strlen("dry-run"))) {
758 inject->itrace_synth_opts.vm_tm_corr_dry_run = true;
759 inject->in_place_update_dry_run = true;
760 args = dry_run + strlen("dry-run");
765 inject->itrace_synth_opts.vm_tm_corr_args = strdup(args);
767 return inject->itrace_synth_opts.vm_tm_corr_args ? 0 : -ENOMEM;
770 static int save_section_info_cb(struct perf_file_section *section,
771 struct perf_header *ph __maybe_unused,
772 int feat, int fd __maybe_unused, void *data)
774 struct perf_inject *inject = data;
776 inject->secs[feat] = *section;
780 static int save_section_info(struct perf_inject *inject)
782 struct perf_header *header = &inject->session->header;
783 int fd = perf_data__fd(inject->session->data);
785 return perf_header__process_sections(header, fd, inject, save_section_info_cb);
788 static bool keep_feat(int feat)
791 /* Keep original information that describes the machine or software */
792 case HEADER_TRACING_DATA:
793 case HEADER_HOSTNAME:
794 case HEADER_OSRELEASE:
800 case HEADER_TOTAL_MEM:
801 case HEADER_CPU_TOPOLOGY:
802 case HEADER_NUMA_TOPOLOGY:
803 case HEADER_PMU_MAPPINGS:
805 case HEADER_MEM_TOPOLOGY:
807 case HEADER_BPF_PROG_INFO:
809 case HEADER_CPU_PMU_CAPS:
810 case HEADER_CLOCK_DATA:
811 case HEADER_HYBRID_TOPOLOGY:
812 case HEADER_HYBRID_CPU_PMU_CAPS:
814 /* Information that can be updated */
815 case HEADER_BUILD_ID:
817 case HEADER_EVENT_DESC:
818 case HEADER_BRANCH_STACK:
819 case HEADER_GROUP_DESC:
820 case HEADER_AUXTRACE:
822 case HEADER_SAMPLE_TIME:
823 case HEADER_DIR_FORMAT:
824 case HEADER_COMPRESSED:
830 static int read_file(int fd, u64 offs, void *buf, size_t sz)
832 ssize_t ret = preadn(fd, buf, sz, offs);
836 if ((size_t)ret != sz)
841 static int feat_copy(struct perf_inject *inject, int feat, struct feat_writer *fw)
843 int fd = perf_data__fd(inject->session->data);
844 u64 offs = inject->secs[feat].offset;
845 size_t sz = inject->secs[feat].size;
846 void *buf = malloc(sz);
852 ret = read_file(fd, offs, buf, sz);
856 ret = fw->write(fw, buf, sz);
863 struct feat_copier fc;
864 struct perf_inject *inject;
867 static int feat_copy_cb(struct feat_copier *fc, int feat, struct feat_writer *fw)
869 struct inject_fc *inj_fc = container_of(fc, struct inject_fc, fc);
870 struct perf_inject *inject = inj_fc->inject;
873 if (!inject->secs[feat].offset ||
877 ret = feat_copy(inject, feat, fw);
881 return 1; /* Feature section copied */
884 static int copy_kcore_dir(struct perf_inject *inject)
889 ret = asprintf(&cmd, "cp -r -n %s/kcore_dir* %s >/dev/null 2>&1",
890 inject->input_name, inject->output.path);
893 pr_debug("%s\n", cmd);
899 static int output_fd(struct perf_inject *inject)
901 return inject->in_place_update ? -1 : perf_data__fd(&inject->output);
904 static int __cmd_inject(struct perf_inject *inject)
907 struct perf_session *session = inject->session;
908 int fd = output_fd(inject);
909 u64 output_data_offset;
911 signal(SIGINT, sig_handler);
913 if (inject->build_ids || inject->sched_stat ||
914 inject->itrace_synth_opts.set || inject->build_id_all) {
915 inject->tool.mmap = perf_event__repipe_mmap;
916 inject->tool.mmap2 = perf_event__repipe_mmap2;
917 inject->tool.fork = perf_event__repipe_fork;
918 inject->tool.tracing_data = perf_event__repipe_tracing_data;
921 output_data_offset = perf_session__data_offset(session->evlist);
923 if (inject->build_id_all) {
924 inject->tool.mmap = perf_event__repipe_buildid_mmap;
925 inject->tool.mmap2 = perf_event__repipe_buildid_mmap2;
926 } else if (inject->build_ids) {
927 inject->tool.sample = perf_event__inject_buildid;
928 } else if (inject->sched_stat) {
931 evlist__for_each_entry(session->evlist, evsel) {
932 const char *name = evsel__name(evsel);
934 if (!strcmp(name, "sched:sched_switch")) {
935 if (evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
938 evsel->handler = perf_inject__sched_switch;
939 } else if (!strcmp(name, "sched:sched_process_exit"))
940 evsel->handler = perf_inject__sched_process_exit;
941 else if (!strncmp(name, "sched:sched_stat_", 17))
942 evsel->handler = perf_inject__sched_stat;
944 } else if (inject->itrace_synth_opts.vm_time_correlation) {
945 session->itrace_synth_opts = &inject->itrace_synth_opts;
946 memset(&inject->tool, 0, sizeof(inject->tool));
947 inject->tool.id_index = perf_event__process_id_index;
948 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
949 inject->tool.auxtrace = perf_event__process_auxtrace;
950 inject->tool.auxtrace_error = perf_event__process_auxtrace_error;
951 inject->tool.ordered_events = true;
952 inject->tool.ordering_requires_timestamps = true;
953 } else if (inject->itrace_synth_opts.set) {
954 session->itrace_synth_opts = &inject->itrace_synth_opts;
955 inject->itrace_synth_opts.inject = true;
956 inject->tool.comm = perf_event__repipe_comm;
957 inject->tool.namespaces = perf_event__repipe_namespaces;
958 inject->tool.exit = perf_event__repipe_exit;
959 inject->tool.id_index = perf_event__process_id_index;
960 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
961 inject->tool.auxtrace = perf_event__process_auxtrace;
962 inject->tool.aux = perf_event__drop_aux;
963 inject->tool.itrace_start = perf_event__drop_aux;
964 inject->tool.aux_output_hw_id = perf_event__drop_aux;
965 inject->tool.ordered_events = true;
966 inject->tool.ordering_requires_timestamps = true;
967 /* Allow space in the header for new attributes */
968 output_data_offset = roundup(8192 + session->header.data_offset, 4096);
973 if (!inject->itrace_synth_opts.set)
974 auxtrace_index__free(&session->auxtrace_index);
976 if (!inject->is_pipe && !inject->in_place_update)
977 lseek(fd, output_data_offset, SEEK_SET);
979 ret = perf_session__process_events(session);
983 if (!inject->is_pipe && !inject->in_place_update) {
984 struct inject_fc inj_fc = {
985 .fc.copy = feat_copy_cb,
989 if (inject->build_ids)
990 perf_header__set_feat(&session->header,
993 * Keep all buildids when there is unprocessed AUX data because
994 * it is not known which ones the AUX trace hits.
996 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
997 inject->have_auxtrace && !inject->itrace_synth_opts.set)
998 dsos__hit_all(session);
1000 * The AUX areas have been removed and replaced with
1001 * synthesized hardware events, so clear the feature flag.
1003 if (inject->itrace_synth_opts.set) {
1004 perf_header__clear_feat(&session->header,
1006 if (inject->itrace_synth_opts.last_branch ||
1007 inject->itrace_synth_opts.add_last_branch)
1008 perf_header__set_feat(&session->header,
1009 HEADER_BRANCH_STACK);
1011 session->header.data_offset = output_data_offset;
1012 session->header.data_size = inject->bytes_written;
1013 perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc);
1015 if (inject->copy_kcore_dir) {
1016 ret = copy_kcore_dir(inject);
1025 int cmd_inject(int argc, const char **argv)
1027 struct perf_inject inject = {
1029 .sample = perf_event__repipe_sample,
1030 .read = perf_event__repipe_sample,
1031 .mmap = perf_event__repipe,
1032 .mmap2 = perf_event__repipe,
1033 .comm = perf_event__repipe,
1034 .namespaces = perf_event__repipe,
1035 .cgroup = perf_event__repipe,
1036 .fork = perf_event__repipe,
1037 .exit = perf_event__repipe,
1038 .lost = perf_event__repipe,
1039 .lost_samples = perf_event__repipe,
1040 .aux = perf_event__repipe,
1041 .itrace_start = perf_event__repipe,
1042 .aux_output_hw_id = perf_event__repipe,
1043 .context_switch = perf_event__repipe,
1044 .throttle = perf_event__repipe,
1045 .unthrottle = perf_event__repipe,
1046 .ksymbol = perf_event__repipe,
1047 .bpf = perf_event__repipe,
1048 .text_poke = perf_event__repipe,
1049 .attr = perf_event__repipe_attr,
1050 .event_update = perf_event__repipe_event_update,
1051 .tracing_data = perf_event__repipe_op2_synth,
1052 .finished_round = perf_event__repipe_oe_synth,
1053 .build_id = perf_event__repipe_op2_synth,
1054 .id_index = perf_event__repipe_op2_synth,
1055 .auxtrace_info = perf_event__repipe_op2_synth,
1056 .auxtrace_error = perf_event__repipe_op2_synth,
1057 .time_conv = perf_event__repipe_op2_synth,
1058 .thread_map = perf_event__repipe_op2_synth,
1059 .cpu_map = perf_event__repipe_op2_synth,
1060 .stat_config = perf_event__repipe_op2_synth,
1061 .stat = perf_event__repipe_op2_synth,
1062 .stat_round = perf_event__repipe_op2_synth,
1063 .feature = perf_event__repipe_op2_synth,
1064 .compressed = perf_event__repipe_op4_synth,
1065 .auxtrace = perf_event__repipe_auxtrace,
1068 .samples = LIST_HEAD_INIT(inject.samples),
1071 .mode = PERF_DATA_MODE_WRITE,
1075 struct perf_data data = {
1076 .mode = PERF_DATA_MODE_READ,
1082 struct option options[] = {
1083 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
1084 "Inject build-ids into the output stream"),
1085 OPT_BOOLEAN(0, "buildid-all", &inject.build_id_all,
1086 "Inject build-ids of all DSOs into the output stream"),
1087 OPT_STRING('i', "input", &inject.input_name, "file",
1089 OPT_STRING('o', "output", &inject.output.path, "file",
1090 "output file name"),
1091 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
1092 "Merge sched-stat and sched-switch for getting events "
1093 "where and how long tasks slept"),
1095 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
1097 OPT_INCR('v', "verbose", &verbose,
1098 "be more verbose (show build ids, etc)"),
1099 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1100 "file", "vmlinux pathname"),
1101 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1102 "don't load vmlinux even if found"),
1103 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
1104 "kallsyms pathname"),
1105 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
1106 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
1107 NULL, "opts", "Instruction Tracing options\n"
1109 itrace_parse_synth_opts),
1110 OPT_BOOLEAN(0, "strip", &inject.strip,
1111 "strip non-synthesized events (use with --itrace)"),
1112 OPT_CALLBACK_OPTARG(0, "vm-time-correlation", &inject, NULL, "opts",
1113 "correlate time between VM guests and the host",
1114 parse_vm_time_correlation),
1117 const char * const inject_usage[] = {
1118 "perf inject [<options>]",
1121 #ifndef HAVE_JITDUMP
1122 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
1124 argc = parse_options(argc, argv, options, inject_usage, 0);
1127 * Any (unrecognized) arguments left?
1130 usage_with_options(inject_usage, options);
1132 if (inject.strip && !inject.itrace_synth_opts.set) {
1133 pr_err("--strip option requires --itrace option\n");
1137 if (symbol__validate_sym_arguments())
1140 if (inject.in_place_update) {
1141 if (!strcmp(inject.input_name, "-")) {
1142 pr_err("Input file name required for in-place updating\n");
1145 if (strcmp(inject.output.path, "-")) {
1146 pr_err("Output file name must not be specified for in-place updating\n");
1149 if (!data.force && !inject.in_place_update_dry_run) {
1150 pr_err("The input file would be updated in place, "
1151 "the --force option is required.\n");
1154 if (!inject.in_place_update_dry_run)
1155 data.in_place_update = true;
1157 if (strcmp(inject.output.path, "-") && !inject.strip &&
1158 has_kcore_dir(inject.input_name)) {
1159 inject.output.is_dir = true;
1160 inject.copy_kcore_dir = true;
1162 if (perf_data__open(&inject.output)) {
1163 perror("failed to create output file");
1168 data.path = inject.input_name;
1169 if (!strcmp(inject.input_name, "-") || inject.output.is_pipe) {
1170 inject.is_pipe = true;
1172 * Do not repipe header when input is a regular file
1173 * since either it can rewrite the header at the end
1174 * or write a new pipe header.
1176 if (strcmp(inject.input_name, "-"))
1180 inject.session = __perf_session__new(&data, repipe,
1183 if (IS_ERR(inject.session)) {
1184 ret = PTR_ERR(inject.session);
1185 goto out_close_output;
1188 if (zstd_init(&(inject.session->zstd_data), 0) < 0)
1189 pr_warning("Decompression initialization failed.\n");
1191 /* Save original section info before feature bits change */
1192 ret = save_section_info(&inject);
1196 if (!data.is_pipe && inject.output.is_pipe) {
1197 ret = perf_header__write_pipe(perf_data__fd(&inject.output));
1199 pr_err("Couldn't write a new pipe header.\n");
1203 ret = perf_event__synthesize_for_pipe(&inject.tool,
1206 perf_event__repipe);
1211 if (inject.build_ids && !inject.build_id_all) {
1213 * to make sure the mmap records are ordered correctly
1214 * and so that the correct especially due to jitted code
1215 * mmaps. We cannot generate the buildid hit list and
1216 * inject the jit mmaps at the same time for now.
1218 inject.tool.ordered_events = true;
1219 inject.tool.ordering_requires_timestamps = true;
1222 if (inject.sched_stat) {
1223 inject.tool.ordered_events = true;
1227 if (inject.jit_mode) {
1228 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
1229 inject.tool.mmap = perf_event__jit_repipe_mmap;
1230 inject.tool.ordered_events = true;
1231 inject.tool.ordering_requires_timestamps = true;
1233 * JIT MMAP injection injects all MMAP events in one go, so it
1234 * does not obey finished_round semantics.
1236 inject.tool.finished_round = perf_event__drop_oe;
1239 ret = symbol__init(&inject.session->header.env);
1243 ret = __cmd_inject(&inject);
1246 zstd_fini(&(inject.session->zstd_data));
1247 perf_session__delete(inject.session);
1249 if (!inject.in_place_update)
1250 perf_data__close(&inject.output);
1251 free(inject.itrace_synth_opts.vm_tm_corr_args);