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
12 #include "util/color.h"
13 #include "util/evlist.h"
14 #include "util/evsel.h"
15 #include "util/session.h"
16 #include "util/tool.h"
17 #include "util/debug.h"
18 #include "util/build-id.h"
19 #include "util/data.h"
20 #include "util/auxtrace.h"
22 #include "util/thread.h"
24 #include <subcmd/parse-options.h>
26 #include <linux/list.h>
31 struct perf_tool tool;
32 struct perf_session *session;
38 const char *input_name;
39 struct perf_data output;
42 struct list_head samples;
43 struct itrace_synth_opts itrace_synth_opts;
47 struct list_head node;
49 union perf_event event[0];
52 static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
56 size = perf_data__write(&inject->output, buf, sz);
60 inject->bytes_written += size;
64 static int perf_event__repipe_synth(struct perf_tool *tool,
65 union perf_event *event)
67 struct perf_inject *inject = container_of(tool, struct perf_inject,
70 return output_bytes(inject, event, event->header.size);
73 static int perf_event__repipe_oe_synth(struct perf_tool *tool,
74 union perf_event *event,
75 struct ordered_events *oe __maybe_unused)
77 return perf_event__repipe_synth(tool, event);
81 static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
82 union perf_event *event __maybe_unused,
83 struct ordered_events *oe __maybe_unused)
89 static int perf_event__repipe_op2_synth(struct perf_session *session,
90 union perf_event *event)
92 return perf_event__repipe_synth(session->tool, event);
95 static int perf_event__repipe_attr(struct perf_tool *tool,
96 union perf_event *event,
97 struct perf_evlist **pevlist)
99 struct perf_inject *inject = container_of(tool, struct perf_inject,
103 ret = perf_event__process_attr(tool, event, pevlist);
107 if (!inject->output.is_pipe)
110 return perf_event__repipe_synth(tool, event);
113 #ifdef HAVE_AUXTRACE_SUPPORT
115 static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
122 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
125 ret = output_bytes(inject, buf, ssz);
134 static s64 perf_event__repipe_auxtrace(struct perf_session *session,
135 union perf_event *event)
137 struct perf_tool *tool = session->tool;
138 struct perf_inject *inject = container_of(tool, struct perf_inject,
142 inject->have_auxtrace = true;
144 if (!inject->output.is_pipe) {
147 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
150 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
156 if (perf_data__is_pipe(session->data) || !session->one_mmap) {
157 ret = output_bytes(inject, event, event->header.size);
160 ret = copy_bytes(inject, perf_data__fd(session->data),
161 event->auxtrace.size);
163 ret = output_bytes(inject, event,
164 event->header.size + event->auxtrace.size);
169 return event->auxtrace.size;
175 perf_event__repipe_auxtrace(struct perf_session *session __maybe_unused,
176 union perf_event *event __maybe_unused)
178 pr_err("AUX area tracing not supported\n");
184 static int perf_event__repipe(struct perf_tool *tool,
185 union perf_event *event,
186 struct perf_sample *sample __maybe_unused,
187 struct machine *machine __maybe_unused)
189 return perf_event__repipe_synth(tool, event);
192 static int perf_event__drop(struct perf_tool *tool __maybe_unused,
193 union perf_event *event __maybe_unused,
194 struct perf_sample *sample __maybe_unused,
195 struct machine *machine __maybe_unused)
200 static int perf_event__drop_aux(struct perf_tool *tool,
201 union perf_event *event __maybe_unused,
202 struct perf_sample *sample,
203 struct machine *machine __maybe_unused)
205 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
208 inject->aux_id = sample->id;
213 typedef int (*inject_handler)(struct perf_tool *tool,
214 union perf_event *event,
215 struct perf_sample *sample,
216 struct perf_evsel *evsel,
217 struct machine *machine);
219 static int perf_event__repipe_sample(struct perf_tool *tool,
220 union perf_event *event,
221 struct perf_sample *sample,
222 struct perf_evsel *evsel,
223 struct machine *machine)
225 if (evsel->handler) {
226 inject_handler f = evsel->handler;
227 return f(tool, event, sample, evsel, machine);
230 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
232 return perf_event__repipe_synth(tool, event);
235 static int perf_event__repipe_mmap(struct perf_tool *tool,
236 union perf_event *event,
237 struct perf_sample *sample,
238 struct machine *machine)
242 err = perf_event__process_mmap(tool, event, sample, machine);
243 perf_event__repipe(tool, event, sample, machine);
249 static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
250 union perf_event *event,
251 struct perf_sample *sample,
252 struct machine *machine)
254 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
259 * if jit marker, then inject jit mmaps and generate ELF images
261 ret = jit_process(inject->session, &inject->output, machine,
262 event->mmap.filename, sample->pid, &n);
266 inject->bytes_written += n;
269 return perf_event__repipe_mmap(tool, event, sample, machine);
273 static int perf_event__repipe_mmap2(struct perf_tool *tool,
274 union perf_event *event,
275 struct perf_sample *sample,
276 struct machine *machine)
280 err = perf_event__process_mmap2(tool, event, sample, machine);
281 perf_event__repipe(tool, event, sample, machine);
287 static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
288 union perf_event *event,
289 struct perf_sample *sample,
290 struct machine *machine)
292 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
297 * if jit marker, then inject jit mmaps and generate ELF images
299 ret = jit_process(inject->session, &inject->output, machine,
300 event->mmap2.filename, sample->pid, &n);
304 inject->bytes_written += n;
307 return perf_event__repipe_mmap2(tool, event, sample, machine);
311 static int perf_event__repipe_fork(struct perf_tool *tool,
312 union perf_event *event,
313 struct perf_sample *sample,
314 struct machine *machine)
318 err = perf_event__process_fork(tool, event, sample, machine);
319 perf_event__repipe(tool, event, sample, machine);
324 static int perf_event__repipe_comm(struct perf_tool *tool,
325 union perf_event *event,
326 struct perf_sample *sample,
327 struct machine *machine)
331 err = perf_event__process_comm(tool, event, sample, machine);
332 perf_event__repipe(tool, event, sample, machine);
337 static int perf_event__repipe_namespaces(struct perf_tool *tool,
338 union perf_event *event,
339 struct perf_sample *sample,
340 struct machine *machine)
342 int err = perf_event__process_namespaces(tool, event, sample, machine);
344 perf_event__repipe(tool, event, sample, machine);
349 static int perf_event__repipe_exit(struct perf_tool *tool,
350 union perf_event *event,
351 struct perf_sample *sample,
352 struct machine *machine)
356 err = perf_event__process_exit(tool, event, sample, machine);
357 perf_event__repipe(tool, event, sample, machine);
362 static int perf_event__repipe_tracing_data(struct perf_session *session,
363 union perf_event *event)
367 perf_event__repipe_synth(session->tool, event);
368 err = perf_event__process_tracing_data(session, event);
373 static int perf_event__repipe_id_index(struct perf_session *session,
374 union perf_event *event)
378 perf_event__repipe_synth(session->tool, event);
379 err = perf_event__process_id_index(session, event);
384 static int dso__read_build_id(struct dso *dso)
386 if (dso->has_build_id)
389 if (filename__read_build_id(dso->long_name, dso->build_id,
390 sizeof(dso->build_id)) > 0) {
391 dso->has_build_id = true;
398 static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
399 struct machine *machine)
401 u16 misc = PERF_RECORD_MISC_USER;
404 if (dso__read_build_id(dso) < 0) {
405 pr_debug("no build_id found for %s\n", dso->long_name);
410 misc = PERF_RECORD_MISC_KERNEL;
412 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
415 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
422 static int perf_event__inject_buildid(struct perf_tool *tool,
423 union perf_event *event,
424 struct perf_sample *sample,
425 struct perf_evsel *evsel __maybe_unused,
426 struct machine *machine)
428 struct addr_location al;
429 struct thread *thread;
431 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
432 if (thread == NULL) {
433 pr_err("problem processing %d event, skipping it.\n",
438 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) {
439 if (!al.map->dso->hit) {
440 al.map->dso->hit = 1;
441 if (map__load(al.map) >= 0) {
442 dso__inject_build_id(al.map->dso, tool, machine);
444 * If this fails, too bad, let the other side
445 * account this as unresolved.
448 #ifdef HAVE_LIBELF_SUPPORT
449 pr_warning("no symbols found in %s, maybe "
450 "install a debug package?\n",
451 al.map->dso->long_name);
459 perf_event__repipe(tool, event, sample, machine);
463 static int perf_inject__sched_process_exit(struct perf_tool *tool,
464 union perf_event *event __maybe_unused,
465 struct perf_sample *sample,
466 struct perf_evsel *evsel __maybe_unused,
467 struct machine *machine __maybe_unused)
469 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
470 struct event_entry *ent;
472 list_for_each_entry(ent, &inject->samples, node) {
473 if (sample->tid == ent->tid) {
474 list_del_init(&ent->node);
483 static int perf_inject__sched_switch(struct perf_tool *tool,
484 union perf_event *event,
485 struct perf_sample *sample,
486 struct perf_evsel *evsel,
487 struct machine *machine)
489 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
490 struct event_entry *ent;
492 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
494 ent = malloc(event->header.size + sizeof(struct event_entry));
496 color_fprintf(stderr, PERF_COLOR_RED,
497 "Not enough memory to process sched switch event!");
501 ent->tid = sample->tid;
502 memcpy(&ent->event, event, event->header.size);
503 list_add(&ent->node, &inject->samples);
507 static int perf_inject__sched_stat(struct perf_tool *tool,
508 union perf_event *event __maybe_unused,
509 struct perf_sample *sample,
510 struct perf_evsel *evsel,
511 struct machine *machine)
513 struct event_entry *ent;
514 union perf_event *event_sw;
515 struct perf_sample sample_sw;
516 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
517 u32 pid = perf_evsel__intval(evsel, sample, "pid");
519 list_for_each_entry(ent, &inject->samples, node) {
526 event_sw = &ent->event[0];
527 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
529 sample_sw.period = sample->period;
530 sample_sw.time = sample->time;
531 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
532 evsel->attr.read_format, &sample_sw);
533 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
534 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
537 static void sig_handler(int sig __maybe_unused)
542 static int perf_evsel__check_stype(struct perf_evsel *evsel,
543 u64 sample_type, const char *sample_msg)
545 struct perf_event_attr *attr = &evsel->attr;
546 const char *name = perf_evsel__name(evsel);
548 if (!(attr->sample_type & sample_type)) {
549 pr_err("Samples for %s event do not have %s attribute set.",
557 static int drop_sample(struct perf_tool *tool __maybe_unused,
558 union perf_event *event __maybe_unused,
559 struct perf_sample *sample __maybe_unused,
560 struct perf_evsel *evsel __maybe_unused,
561 struct machine *machine __maybe_unused)
566 static void strip_init(struct perf_inject *inject)
568 struct perf_evlist *evlist = inject->session->evlist;
569 struct perf_evsel *evsel;
571 inject->tool.context_switch = perf_event__drop;
573 evlist__for_each_entry(evlist, evsel)
574 evsel->handler = drop_sample;
577 static bool has_tracking(struct perf_evsel *evsel)
579 return evsel->attr.mmap || evsel->attr.mmap2 || evsel->attr.comm ||
583 #define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
584 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
587 * In order that the perf.data file is parsable, tracking events like MMAP need
588 * their selected event to exist, except if there is only 1 selected event left
589 * and it has a compatible sample type.
591 static bool ok_to_remove(struct perf_evlist *evlist,
592 struct perf_evsel *evsel_to_remove)
594 struct perf_evsel *evsel;
598 if (!has_tracking(evsel_to_remove))
601 evlist__for_each_entry(evlist, evsel) {
602 if (evsel->handler != drop_sample) {
604 if ((evsel->attr.sample_type & COMPAT_MASK) ==
605 (evsel_to_remove->attr.sample_type & COMPAT_MASK))
610 return ok && cnt == 1;
613 static void strip_fini(struct perf_inject *inject)
615 struct perf_evlist *evlist = inject->session->evlist;
616 struct perf_evsel *evsel, *tmp;
618 /* Remove non-synthesized evsels if possible */
619 evlist__for_each_entry_safe(evlist, tmp, evsel) {
620 if (evsel->handler == drop_sample &&
621 ok_to_remove(evlist, evsel)) {
622 pr_debug("Deleting %s\n", perf_evsel__name(evsel));
623 perf_evlist__remove(evlist, evsel);
624 perf_evsel__delete(evsel);
629 static int __cmd_inject(struct perf_inject *inject)
632 struct perf_session *session = inject->session;
633 struct perf_data *data_out = &inject->output;
634 int fd = perf_data__fd(data_out);
635 u64 output_data_offset;
637 signal(SIGINT, sig_handler);
639 if (inject->build_ids || inject->sched_stat ||
640 inject->itrace_synth_opts.set) {
641 inject->tool.mmap = perf_event__repipe_mmap;
642 inject->tool.mmap2 = perf_event__repipe_mmap2;
643 inject->tool.fork = perf_event__repipe_fork;
644 inject->tool.tracing_data = perf_event__repipe_tracing_data;
647 output_data_offset = session->header.data_offset;
649 if (inject->build_ids) {
650 inject->tool.sample = perf_event__inject_buildid;
651 } else if (inject->sched_stat) {
652 struct perf_evsel *evsel;
654 evlist__for_each_entry(session->evlist, evsel) {
655 const char *name = perf_evsel__name(evsel);
657 if (!strcmp(name, "sched:sched_switch")) {
658 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
661 evsel->handler = perf_inject__sched_switch;
662 } else if (!strcmp(name, "sched:sched_process_exit"))
663 evsel->handler = perf_inject__sched_process_exit;
664 else if (!strncmp(name, "sched:sched_stat_", 17))
665 evsel->handler = perf_inject__sched_stat;
667 } else if (inject->itrace_synth_opts.set) {
668 session->itrace_synth_opts = &inject->itrace_synth_opts;
669 inject->itrace_synth_opts.inject = true;
670 inject->tool.comm = perf_event__repipe_comm;
671 inject->tool.namespaces = perf_event__repipe_namespaces;
672 inject->tool.exit = perf_event__repipe_exit;
673 inject->tool.id_index = perf_event__repipe_id_index;
674 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
675 inject->tool.auxtrace = perf_event__process_auxtrace;
676 inject->tool.aux = perf_event__drop_aux;
677 inject->tool.itrace_start = perf_event__drop_aux,
678 inject->tool.ordered_events = true;
679 inject->tool.ordering_requires_timestamps = true;
680 /* Allow space in the header for new attributes */
681 output_data_offset = 4096;
686 if (!inject->itrace_synth_opts.set)
687 auxtrace_index__free(&session->auxtrace_index);
689 if (!data_out->is_pipe)
690 lseek(fd, output_data_offset, SEEK_SET);
692 ret = perf_session__process_events(session);
696 if (!data_out->is_pipe) {
697 if (inject->build_ids)
698 perf_header__set_feat(&session->header,
701 * Keep all buildids when there is unprocessed AUX data because
702 * it is not known which ones the AUX trace hits.
704 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
705 inject->have_auxtrace && !inject->itrace_synth_opts.set)
706 dsos__hit_all(session);
708 * The AUX areas have been removed and replaced with
709 * synthesized hardware events, so clear the feature flag and
712 if (inject->itrace_synth_opts.set) {
713 struct perf_evsel *evsel;
715 perf_header__clear_feat(&session->header,
717 if (inject->itrace_synth_opts.last_branch)
718 perf_header__set_feat(&session->header,
719 HEADER_BRANCH_STACK);
720 evsel = perf_evlist__id2evsel_strict(session->evlist,
723 pr_debug("Deleting %s\n",
724 perf_evsel__name(evsel));
725 perf_evlist__remove(session->evlist, evsel);
726 perf_evsel__delete(evsel);
731 session->header.data_offset = output_data_offset;
732 session->header.data_size = inject->bytes_written;
733 perf_session__write_header(session, session->evlist, fd, true);
739 int cmd_inject(int argc, const char **argv)
741 struct perf_inject inject = {
743 .sample = perf_event__repipe_sample,
744 .mmap = perf_event__repipe,
745 .mmap2 = perf_event__repipe,
746 .comm = perf_event__repipe,
747 .fork = perf_event__repipe,
748 .exit = perf_event__repipe,
749 .lost = perf_event__repipe,
750 .lost_samples = perf_event__repipe,
751 .aux = perf_event__repipe,
752 .itrace_start = perf_event__repipe,
753 .context_switch = perf_event__repipe,
754 .read = perf_event__repipe_sample,
755 .throttle = perf_event__repipe,
756 .unthrottle = perf_event__repipe,
757 .attr = perf_event__repipe_attr,
758 .tracing_data = perf_event__repipe_op2_synth,
759 .auxtrace_info = perf_event__repipe_op2_synth,
760 .auxtrace = perf_event__repipe_auxtrace,
761 .auxtrace_error = perf_event__repipe_op2_synth,
762 .time_conv = perf_event__repipe_op2_synth,
763 .finished_round = perf_event__repipe_oe_synth,
764 .build_id = perf_event__repipe_op2_synth,
765 .id_index = perf_event__repipe_op2_synth,
766 .feature = perf_event__repipe_op2_synth,
769 .samples = LIST_HEAD_INIT(inject.samples),
774 .mode = PERF_DATA_MODE_WRITE,
777 struct perf_data data = {
778 .mode = PERF_DATA_MODE_READ,
782 struct option options[] = {
783 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
784 "Inject build-ids into the output stream"),
785 OPT_STRING('i', "input", &inject.input_name, "file",
787 OPT_STRING('o', "output", &inject.output.file.path, "file",
789 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
790 "Merge sched-stat and sched-switch for getting events "
791 "where and how long tasks slept"),
793 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
795 OPT_INCR('v', "verbose", &verbose,
796 "be more verbose (show build ids, etc)"),
797 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
798 "kallsyms pathname"),
799 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
800 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
801 NULL, "opts", "Instruction Tracing options\n"
803 itrace_parse_synth_opts),
804 OPT_BOOLEAN(0, "strip", &inject.strip,
805 "strip non-synthesized events (use with --itrace)"),
808 const char * const inject_usage[] = {
809 "perf inject [<options>]",
813 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
815 argc = parse_options(argc, argv, options, inject_usage, 0);
818 * Any (unrecognized) arguments left?
821 usage_with_options(inject_usage, options);
823 if (inject.strip && !inject.itrace_synth_opts.set) {
824 pr_err("--strip option requires --itrace option\n");
828 if (perf_data__open(&inject.output)) {
829 perror("failed to create output file");
833 inject.tool.ordered_events = inject.sched_stat;
835 data.file.path = inject.input_name;
836 inject.session = perf_session__new(&data, true, &inject.tool);
837 if (inject.session == NULL)
840 if (inject.build_ids) {
842 * to make sure the mmap records are ordered correctly
843 * and so that the correct especially due to jitted code
844 * mmaps. We cannot generate the buildid hit list and
845 * inject the jit mmaps at the same time for now.
847 inject.tool.ordered_events = true;
848 inject.tool.ordering_requires_timestamps = true;
851 if (inject.jit_mode) {
852 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
853 inject.tool.mmap = perf_event__jit_repipe_mmap;
854 inject.tool.ordered_events = true;
855 inject.tool.ordering_requires_timestamps = true;
857 * JIT MMAP injection injects all MMAP events in one go, so it
858 * does not obey finished_round semantics.
860 inject.tool.finished_round = perf_event__drop_oe;
863 ret = symbol__init(&inject.session->header.env);
867 ret = __cmd_inject(&inject);
870 perf_session__delete(inject.session);