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/evlist.h"
14 #include "util/evsel.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"
23 #include "util/symbol.h"
24 #include "util/synthetic-events.h"
25 #include "util/thread.h"
26 #include <linux/err.h>
28 #include <subcmd/parse-options.h>
30 #include <linux/list.h>
35 struct perf_tool tool;
36 struct perf_session *session;
42 const char *input_name;
43 struct perf_data output;
46 struct list_head samples;
47 struct itrace_synth_opts itrace_synth_opts;
48 char event_copy[PERF_SAMPLE_MAX_SIZE];
52 struct list_head node;
54 union perf_event event[];
57 static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
61 size = perf_data__write(&inject->output, buf, sz);
65 inject->bytes_written += size;
69 static int perf_event__repipe_synth(struct perf_tool *tool,
70 union perf_event *event)
72 struct perf_inject *inject = container_of(tool, struct perf_inject,
75 return output_bytes(inject, event, event->header.size);
78 static int perf_event__repipe_oe_synth(struct perf_tool *tool,
79 union perf_event *event,
80 struct ordered_events *oe __maybe_unused)
82 return perf_event__repipe_synth(tool, event);
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)
94 static int perf_event__repipe_op2_synth(struct perf_session *session,
95 union perf_event *event)
97 return perf_event__repipe_synth(session->tool, event);
100 static int perf_event__repipe_op4_synth(struct perf_session *session,
101 union perf_event *event,
102 u64 data __maybe_unused)
104 return perf_event__repipe_synth(session->tool, event);
107 static int perf_event__repipe_attr(struct perf_tool *tool,
108 union perf_event *event,
109 struct evlist **pevlist)
111 struct perf_inject *inject = container_of(tool, struct perf_inject,
115 ret = perf_event__process_attr(tool, event, pevlist);
119 if (!inject->output.is_pipe)
122 return perf_event__repipe_synth(tool, event);
125 static int perf_event__repipe_event_update(struct perf_tool *tool,
126 union perf_event *event,
127 struct evlist **pevlist __maybe_unused)
129 return perf_event__repipe_synth(tool, event);
132 #ifdef HAVE_AUXTRACE_SUPPORT
134 static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
141 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
144 ret = output_bytes(inject, buf, ssz);
153 static s64 perf_event__repipe_auxtrace(struct perf_session *session,
154 union perf_event *event)
156 struct perf_tool *tool = session->tool;
157 struct perf_inject *inject = container_of(tool, struct perf_inject,
161 inject->have_auxtrace = true;
163 if (!inject->output.is_pipe) {
166 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
169 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
175 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
176 ret = output_bytes(inject, event, event->header.size);
179 ret = copy_bytes(inject, perf_data__fd(session->data),
180 event->auxtrace.size);
182 ret = output_bytes(inject, event,
183 event->header.size + event->auxtrace.size);
188 return event->auxtrace.size;
194 perf_event__repipe_auxtrace(struct perf_session *session __maybe_unused,
195 union perf_event *event __maybe_unused)
197 pr_err("AUX area tracing not supported\n");
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)
208 return perf_event__repipe_synth(tool, event);
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)
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)
224 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
227 inject->aux_id = sample->id;
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)
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;
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))
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;
254 typedef int (*inject_handler)(struct perf_tool *tool,
255 union perf_event *event,
256 struct perf_sample *sample,
258 struct machine *machine);
260 static int perf_event__repipe_sample(struct perf_tool *tool,
261 union perf_event *event,
262 struct perf_sample *sample,
264 struct machine *machine)
266 struct perf_inject *inject = container_of(tool, struct perf_inject,
269 if (evsel && evsel->handler) {
270 inject_handler f = evsel->handler;
271 return f(tool, event, sample, evsel, machine);
274 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
276 if (inject->itrace_synth_opts.set && sample->aux_sample.size)
277 event = perf_inject__cut_auxtrace_sample(inject, event, sample);
279 return perf_event__repipe_synth(tool, event);
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)
289 err = perf_event__process_mmap(tool, event, sample, machine);
290 perf_event__repipe(tool, event, sample, machine);
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)
301 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
306 * if jit marker, then inject jit mmaps and generate ELF images
308 ret = jit_process(inject->session, &inject->output, machine,
309 event->mmap.filename, event->mmap.pid, &n);
313 inject->bytes_written += n;
316 return perf_event__repipe_mmap(tool, event, sample, machine);
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)
327 err = perf_event__process_mmap2(tool, event, sample, machine);
328 perf_event__repipe(tool, event, sample, machine);
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)
339 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
344 * if jit marker, then inject jit mmaps and generate ELF images
346 ret = jit_process(inject->session, &inject->output, machine,
347 event->mmap2.filename, event->mmap2.pid, &n);
351 inject->bytes_written += n;
354 return perf_event__repipe_mmap2(tool, event, sample, machine);
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)
365 err = perf_event__process_fork(tool, event, sample, machine);
366 perf_event__repipe(tool, event, sample, machine);
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)
378 err = perf_event__process_comm(tool, event, sample, machine);
379 perf_event__repipe(tool, event, sample, machine);
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)
389 int err = perf_event__process_namespaces(tool, event, sample, machine);
391 perf_event__repipe(tool, event, sample, machine);
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)
403 err = perf_event__process_exit(tool, event, sample, machine);
404 perf_event__repipe(tool, event, sample, machine);
409 static int perf_event__repipe_tracing_data(struct perf_session *session,
410 union perf_event *event)
414 perf_event__repipe_synth(session->tool, event);
415 err = perf_event__process_tracing_data(session, event);
420 static int dso__read_build_id(struct dso *dso)
422 if (dso->has_build_id)
425 if (filename__read_build_id(dso->long_name, dso->build_id,
426 sizeof(dso->build_id)) > 0) {
427 dso->has_build_id = true;
434 static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
435 struct machine *machine)
437 u16 misc = PERF_RECORD_MISC_USER;
440 if (dso__read_build_id(dso) < 0) {
441 pr_debug("no build_id found for %s\n", dso->long_name);
446 misc = PERF_RECORD_MISC_KERNEL;
448 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
451 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
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)
463 struct addr_location al;
464 struct thread *thread;
466 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
467 if (thread == NULL) {
468 pr_err("problem processing %d event, skipping it.\n",
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);
479 * If this fails, too bad, let the other side
480 * account this as unresolved.
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);
494 perf_event__repipe(tool, event, sample, machine);
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)
504 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
505 struct event_entry *ent;
507 list_for_each_entry(ent, &inject->samples, node) {
508 if (sample->tid == ent->tid) {
509 list_del_init(&ent->node);
518 static int perf_inject__sched_switch(struct perf_tool *tool,
519 union perf_event *event,
520 struct perf_sample *sample,
522 struct machine *machine)
524 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
525 struct event_entry *ent;
527 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
529 ent = malloc(event->header.size + sizeof(struct event_entry));
531 color_fprintf(stderr, PERF_COLOR_RED,
532 "Not enough memory to process sched switch event!");
536 ent->tid = sample->tid;
537 memcpy(&ent->event, event, event->header.size);
538 list_add(&ent->node, &inject->samples);
542 static int perf_inject__sched_stat(struct perf_tool *tool,
543 union perf_event *event __maybe_unused,
544 struct perf_sample *sample,
546 struct machine *machine)
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");
554 list_for_each_entry(ent, &inject->samples, node) {
561 event_sw = &ent->event[0];
562 evsel__parse_sample(evsel, event_sw, &sample_sw);
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);
572 static void sig_handler(int sig __maybe_unused)
577 static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg)
579 struct perf_event_attr *attr = &evsel->core.attr;
580 const char *name = evsel__name(evsel);
582 if (!(attr->sample_type & sample_type)) {
583 pr_err("Samples for %s event do not have %s attribute set.",
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)
600 static void strip_init(struct perf_inject *inject)
602 struct evlist *evlist = inject->session->evlist;
605 inject->tool.context_switch = perf_event__drop;
607 evlist__for_each_entry(evlist, evsel)
608 evsel->handler = drop_sample;
611 static int __cmd_inject(struct perf_inject *inject)
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;
619 signal(SIGINT, sig_handler);
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;
629 output_data_offset = session->header.data_offset;
631 if (inject->build_ids) {
632 inject->tool.sample = perf_event__inject_buildid;
633 } else if (inject->sched_stat) {
636 evlist__for_each_entry(session->evlist, evsel) {
637 const char *name = evsel__name(evsel);
639 if (!strcmp(name, "sched:sched_switch")) {
640 if (evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
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;
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;
668 if (!inject->itrace_synth_opts.set)
669 auxtrace_index__free(&session->auxtrace_index);
671 if (!data_out->is_pipe)
672 lseek(fd, output_data_offset, SEEK_SET);
674 ret = perf_session__process_events(session);
678 if (!data_out->is_pipe) {
679 if (inject->build_ids)
680 perf_header__set_feat(&session->header,
683 * Keep all buildids when there is unprocessed AUX data because
684 * it is not known which ones the AUX trace hits.
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);
690 * The AUX areas have been removed and replaced with
691 * synthesized hardware events, so clear the feature flag and
694 if (inject->itrace_synth_opts.set) {
697 perf_header__clear_feat(&session->header,
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,
706 pr_debug("Deleting %s\n", evsel__name(evsel));
707 evlist__remove(session->evlist, evsel);
708 evsel__delete(evsel);
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);
719 int cmd_inject(int argc, const char **argv)
721 struct perf_inject inject = {
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,
761 .samples = LIST_HEAD_INIT(inject.samples),
764 .mode = PERF_DATA_MODE_WRITE,
767 struct perf_data data = {
768 .mode = PERF_DATA_MODE_READ,
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",
777 OPT_STRING('o', "output", &inject.output.path, "file",
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"),
783 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
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"
793 itrace_parse_synth_opts),
794 OPT_BOOLEAN(0, "strip", &inject.strip,
795 "strip non-synthesized events (use with --itrace)"),
798 const char * const inject_usage[] = {
799 "perf inject [<options>]",
803 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
805 argc = parse_options(argc, argv, options, inject_usage, 0);
808 * Any (unrecognized) arguments left?
811 usage_with_options(inject_usage, options);
813 if (inject.strip && !inject.itrace_synth_opts.set) {
814 pr_err("--strip option requires --itrace option\n");
818 if (perf_data__open(&inject.output)) {
819 perror("failed to create output file");
823 inject.tool.ordered_events = inject.sched_stat;
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);
830 if (zstd_init(&(inject.session->zstd_data), 0) < 0)
831 pr_warning("Decompression initialization failed.\n");
833 if (inject.build_ids) {
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.
840 inject.tool.ordered_events = true;
841 inject.tool.ordering_requires_timestamps = true;
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;
850 * JIT MMAP injection injects all MMAP events in one go, so it
851 * does not obey finished_round semantics.
853 inject.tool.finished_round = perf_event__drop_oe;
856 ret = symbol__init(&inject.session->header.env);
860 ret = __cmd_inject(&inject);
863 zstd_fini(&(inject.session->zstd_data));
864 perf_session__delete(inject.session);