4 * Builtin inject command: Examine the live mode (stdin) event stream
5 * and repipe it to stdout while optionally injecting additional
11 #include "util/color.h"
12 #include "util/evlist.h"
13 #include "util/evsel.h"
14 #include "util/session.h"
15 #include "util/tool.h"
16 #include "util/debug.h"
17 #include "util/build-id.h"
18 #include "util/data.h"
19 #include "util/auxtrace.h"
21 #include <subcmd/parse-options.h>
23 #include <linux/list.h>
26 struct perf_tool tool
;
27 struct perf_session
*session
;
32 const char *input_name
;
33 struct perf_data_file output
;
36 struct list_head samples
;
37 struct itrace_synth_opts itrace_synth_opts
;
41 struct list_head node
;
43 union perf_event event
[0];
46 static int output_bytes(struct perf_inject
*inject
, void *buf
, size_t sz
)
50 size
= perf_data_file__write(&inject
->output
, buf
, sz
);
54 inject
->bytes_written
+= size
;
58 static int perf_event__repipe_synth(struct perf_tool
*tool
,
59 union perf_event
*event
)
61 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
,
64 return output_bytes(inject
, event
, event
->header
.size
);
67 static int perf_event__repipe_oe_synth(struct perf_tool
*tool
,
68 union perf_event
*event
,
69 struct ordered_events
*oe __maybe_unused
)
71 return perf_event__repipe_synth(tool
, event
);
74 static int perf_event__repipe_op2_synth(struct perf_tool
*tool
,
75 union perf_event
*event
,
76 struct perf_session
*session
79 return perf_event__repipe_synth(tool
, event
);
82 static int perf_event__repipe_attr(struct perf_tool
*tool
,
83 union perf_event
*event
,
84 struct perf_evlist
**pevlist
)
86 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
,
90 ret
= perf_event__process_attr(tool
, event
, pevlist
);
94 if (!inject
->output
.is_pipe
)
97 return perf_event__repipe_synth(tool
, event
);
100 #ifdef HAVE_AUXTRACE_SUPPORT
102 static int copy_bytes(struct perf_inject
*inject
, int fd
, off_t size
)
109 ssz
= read(fd
, buf
, min(size
, (off_t
)sizeof(buf
)));
112 ret
= output_bytes(inject
, buf
, ssz
);
121 static s64
perf_event__repipe_auxtrace(struct perf_tool
*tool
,
122 union perf_event
*event
,
123 struct perf_session
*session
126 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
,
130 inject
->have_auxtrace
= true;
132 if (!inject
->output
.is_pipe
) {
135 offset
= lseek(inject
->output
.fd
, 0, SEEK_CUR
);
138 ret
= auxtrace_index__auxtrace_event(&session
->auxtrace_index
,
144 if (perf_data_file__is_pipe(session
->file
) || !session
->one_mmap
) {
145 ret
= output_bytes(inject
, event
, event
->header
.size
);
148 ret
= copy_bytes(inject
, perf_data_file__fd(session
->file
),
149 event
->auxtrace
.size
);
151 ret
= output_bytes(inject
, event
,
152 event
->header
.size
+ event
->auxtrace
.size
);
157 return event
->auxtrace
.size
;
163 perf_event__repipe_auxtrace(struct perf_tool
*tool __maybe_unused
,
164 union perf_event
*event __maybe_unused
,
165 struct perf_session
*session __maybe_unused
)
167 pr_err("AUX area tracing not supported\n");
173 static int perf_event__repipe(struct perf_tool
*tool
,
174 union perf_event
*event
,
175 struct perf_sample
*sample __maybe_unused
,
176 struct machine
*machine __maybe_unused
)
178 return perf_event__repipe_synth(tool
, event
);
181 static int perf_event__drop(struct perf_tool
*tool __maybe_unused
,
182 union perf_event
*event __maybe_unused
,
183 struct perf_sample
*sample __maybe_unused
,
184 struct machine
*machine __maybe_unused
)
189 static int perf_event__drop_aux(struct perf_tool
*tool
,
190 union perf_event
*event __maybe_unused
,
191 struct perf_sample
*sample
,
192 struct machine
*machine __maybe_unused
)
194 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
197 inject
->aux_id
= sample
->id
;
202 typedef int (*inject_handler
)(struct perf_tool
*tool
,
203 union perf_event
*event
,
204 struct perf_sample
*sample
,
205 struct perf_evsel
*evsel
,
206 struct machine
*machine
);
208 static int perf_event__repipe_sample(struct perf_tool
*tool
,
209 union perf_event
*event
,
210 struct perf_sample
*sample
,
211 struct perf_evsel
*evsel
,
212 struct machine
*machine
)
214 if (evsel
->handler
) {
215 inject_handler f
= evsel
->handler
;
216 return f(tool
, event
, sample
, evsel
, machine
);
219 build_id__mark_dso_hit(tool
, event
, sample
, evsel
, machine
);
221 return perf_event__repipe_synth(tool
, event
);
224 static int perf_event__repipe_mmap(struct perf_tool
*tool
,
225 union perf_event
*event
,
226 struct perf_sample
*sample
,
227 struct machine
*machine
)
231 err
= perf_event__process_mmap(tool
, event
, sample
, machine
);
232 perf_event__repipe(tool
, event
, sample
, machine
);
237 static int perf_event__repipe_mmap2(struct perf_tool
*tool
,
238 union perf_event
*event
,
239 struct perf_sample
*sample
,
240 struct machine
*machine
)
244 err
= perf_event__process_mmap2(tool
, event
, sample
, machine
);
245 perf_event__repipe(tool
, event
, sample
, machine
);
250 static int perf_event__repipe_fork(struct perf_tool
*tool
,
251 union perf_event
*event
,
252 struct perf_sample
*sample
,
253 struct machine
*machine
)
257 err
= perf_event__process_fork(tool
, event
, sample
, machine
);
258 perf_event__repipe(tool
, event
, sample
, machine
);
263 static int perf_event__repipe_comm(struct perf_tool
*tool
,
264 union perf_event
*event
,
265 struct perf_sample
*sample
,
266 struct machine
*machine
)
270 err
= perf_event__process_comm(tool
, event
, sample
, machine
);
271 perf_event__repipe(tool
, event
, sample
, machine
);
276 static int perf_event__repipe_exit(struct perf_tool
*tool
,
277 union perf_event
*event
,
278 struct perf_sample
*sample
,
279 struct machine
*machine
)
283 err
= perf_event__process_exit(tool
, event
, sample
, machine
);
284 perf_event__repipe(tool
, event
, sample
, machine
);
289 static int perf_event__repipe_tracing_data(struct perf_tool
*tool
,
290 union perf_event
*event
,
291 struct perf_session
*session
)
295 perf_event__repipe_synth(tool
, event
);
296 err
= perf_event__process_tracing_data(tool
, event
, session
);
301 static int perf_event__repipe_id_index(struct perf_tool
*tool
,
302 union perf_event
*event
,
303 struct perf_session
*session
)
307 perf_event__repipe_synth(tool
, event
);
308 err
= perf_event__process_id_index(tool
, event
, session
);
313 static int dso__read_build_id(struct dso
*dso
)
315 if (dso
->has_build_id
)
318 if (filename__read_build_id(dso
->long_name
, dso
->build_id
,
319 sizeof(dso
->build_id
)) > 0) {
320 dso
->has_build_id
= true;
327 static int dso__inject_build_id(struct dso
*dso
, struct perf_tool
*tool
,
328 struct machine
*machine
)
330 u16 misc
= PERF_RECORD_MISC_USER
;
333 if (dso__read_build_id(dso
) < 0) {
334 pr_debug("no build_id found for %s\n", dso
->long_name
);
339 misc
= PERF_RECORD_MISC_KERNEL
;
341 err
= perf_event__synthesize_build_id(tool
, dso
, misc
, perf_event__repipe
,
344 pr_err("Can't synthesize build_id event for %s\n", dso
->long_name
);
351 static int perf_event__inject_buildid(struct perf_tool
*tool
,
352 union perf_event
*event
,
353 struct perf_sample
*sample
,
354 struct perf_evsel
*evsel __maybe_unused
,
355 struct machine
*machine
)
357 struct addr_location al
;
358 struct thread
*thread
;
361 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
363 thread
= machine__findnew_thread(machine
, sample
->pid
, sample
->tid
);
364 if (thread
== NULL
) {
365 pr_err("problem processing %d event, skipping it.\n",
370 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, sample
->ip
, &al
);
372 if (al
.map
!= NULL
) {
373 if (!al
.map
->dso
->hit
) {
374 al
.map
->dso
->hit
= 1;
375 if (map__load(al
.map
, NULL
) >= 0) {
376 dso__inject_build_id(al
.map
->dso
, tool
, machine
);
378 * If this fails, too bad, let the other side
379 * account this as unresolved.
382 #ifdef HAVE_LIBELF_SUPPORT
383 pr_warning("no symbols found in %s, maybe "
384 "install a debug package?\n",
385 al
.map
->dso
->long_name
);
393 perf_event__repipe(tool
, event
, sample
, machine
);
397 static int perf_inject__sched_process_exit(struct perf_tool
*tool
,
398 union perf_event
*event __maybe_unused
,
399 struct perf_sample
*sample
,
400 struct perf_evsel
*evsel __maybe_unused
,
401 struct machine
*machine __maybe_unused
)
403 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
404 struct event_entry
*ent
;
406 list_for_each_entry(ent
, &inject
->samples
, node
) {
407 if (sample
->tid
== ent
->tid
) {
408 list_del_init(&ent
->node
);
417 static int perf_inject__sched_switch(struct perf_tool
*tool
,
418 union perf_event
*event
,
419 struct perf_sample
*sample
,
420 struct perf_evsel
*evsel
,
421 struct machine
*machine
)
423 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
424 struct event_entry
*ent
;
426 perf_inject__sched_process_exit(tool
, event
, sample
, evsel
, machine
);
428 ent
= malloc(event
->header
.size
+ sizeof(struct event_entry
));
430 color_fprintf(stderr
, PERF_COLOR_RED
,
431 "Not enough memory to process sched switch event!");
435 ent
->tid
= sample
->tid
;
436 memcpy(&ent
->event
, event
, event
->header
.size
);
437 list_add(&ent
->node
, &inject
->samples
);
441 static int perf_inject__sched_stat(struct perf_tool
*tool
,
442 union perf_event
*event __maybe_unused
,
443 struct perf_sample
*sample
,
444 struct perf_evsel
*evsel
,
445 struct machine
*machine
)
447 struct event_entry
*ent
;
448 union perf_event
*event_sw
;
449 struct perf_sample sample_sw
;
450 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
451 u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
453 list_for_each_entry(ent
, &inject
->samples
, node
) {
460 event_sw
= &ent
->event
[0];
461 perf_evsel__parse_sample(evsel
, event_sw
, &sample_sw
);
463 sample_sw
.period
= sample
->period
;
464 sample_sw
.time
= sample
->time
;
465 perf_event__synthesize_sample(event_sw
, evsel
->attr
.sample_type
,
466 evsel
->attr
.read_format
, &sample_sw
,
468 build_id__mark_dso_hit(tool
, event_sw
, &sample_sw
, evsel
, machine
);
469 return perf_event__repipe(tool
, event_sw
, &sample_sw
, machine
);
472 static void sig_handler(int sig __maybe_unused
)
477 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
478 u64 sample_type
, const char *sample_msg
)
480 struct perf_event_attr
*attr
= &evsel
->attr
;
481 const char *name
= perf_evsel__name(evsel
);
483 if (!(attr
->sample_type
& sample_type
)) {
484 pr_err("Samples for %s event do not have %s attribute set.",
492 static int drop_sample(struct perf_tool
*tool __maybe_unused
,
493 union perf_event
*event __maybe_unused
,
494 struct perf_sample
*sample __maybe_unused
,
495 struct perf_evsel
*evsel __maybe_unused
,
496 struct machine
*machine __maybe_unused
)
501 static void strip_init(struct perf_inject
*inject
)
503 struct perf_evlist
*evlist
= inject
->session
->evlist
;
504 struct perf_evsel
*evsel
;
506 inject
->tool
.context_switch
= perf_event__drop
;
508 evlist__for_each(evlist
, evsel
)
509 evsel
->handler
= drop_sample
;
512 static bool has_tracking(struct perf_evsel
*evsel
)
514 return evsel
->attr
.mmap
|| evsel
->attr
.mmap2
|| evsel
->attr
.comm
||
518 #define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
519 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
522 * In order that the perf.data file is parsable, tracking events like MMAP need
523 * their selected event to exist, except if there is only 1 selected event left
524 * and it has a compatible sample type.
526 static bool ok_to_remove(struct perf_evlist
*evlist
,
527 struct perf_evsel
*evsel_to_remove
)
529 struct perf_evsel
*evsel
;
533 if (!has_tracking(evsel_to_remove
))
536 evlist__for_each(evlist
, evsel
) {
537 if (evsel
->handler
!= drop_sample
) {
539 if ((evsel
->attr
.sample_type
& COMPAT_MASK
) ==
540 (evsel_to_remove
->attr
.sample_type
& COMPAT_MASK
))
545 return ok
&& cnt
== 1;
548 static void strip_fini(struct perf_inject
*inject
)
550 struct perf_evlist
*evlist
= inject
->session
->evlist
;
551 struct perf_evsel
*evsel
, *tmp
;
553 /* Remove non-synthesized evsels if possible */
554 evlist__for_each_safe(evlist
, tmp
, evsel
) {
555 if (evsel
->handler
== drop_sample
&&
556 ok_to_remove(evlist
, evsel
)) {
557 pr_debug("Deleting %s\n", perf_evsel__name(evsel
));
558 perf_evlist__remove(evlist
, evsel
);
559 perf_evsel__delete(evsel
);
564 static int __cmd_inject(struct perf_inject
*inject
)
567 struct perf_session
*session
= inject
->session
;
568 struct perf_data_file
*file_out
= &inject
->output
;
569 int fd
= perf_data_file__fd(file_out
);
570 u64 output_data_offset
;
572 signal(SIGINT
, sig_handler
);
574 if (inject
->build_ids
|| inject
->sched_stat
||
575 inject
->itrace_synth_opts
.set
) {
576 inject
->tool
.mmap
= perf_event__repipe_mmap
;
577 inject
->tool
.mmap2
= perf_event__repipe_mmap2
;
578 inject
->tool
.fork
= perf_event__repipe_fork
;
579 inject
->tool
.tracing_data
= perf_event__repipe_tracing_data
;
582 output_data_offset
= session
->header
.data_offset
;
584 if (inject
->build_ids
) {
585 inject
->tool
.sample
= perf_event__inject_buildid
;
586 } else if (inject
->sched_stat
) {
587 struct perf_evsel
*evsel
;
589 evlist__for_each(session
->evlist
, evsel
) {
590 const char *name
= perf_evsel__name(evsel
);
592 if (!strcmp(name
, "sched:sched_switch")) {
593 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID"))
596 evsel
->handler
= perf_inject__sched_switch
;
597 } else if (!strcmp(name
, "sched:sched_process_exit"))
598 evsel
->handler
= perf_inject__sched_process_exit
;
599 else if (!strncmp(name
, "sched:sched_stat_", 17))
600 evsel
->handler
= perf_inject__sched_stat
;
602 } else if (inject
->itrace_synth_opts
.set
) {
603 session
->itrace_synth_opts
= &inject
->itrace_synth_opts
;
604 inject
->itrace_synth_opts
.inject
= true;
605 inject
->tool
.comm
= perf_event__repipe_comm
;
606 inject
->tool
.exit
= perf_event__repipe_exit
;
607 inject
->tool
.id_index
= perf_event__repipe_id_index
;
608 inject
->tool
.auxtrace_info
= perf_event__process_auxtrace_info
;
609 inject
->tool
.auxtrace
= perf_event__process_auxtrace
;
610 inject
->tool
.aux
= perf_event__drop_aux
;
611 inject
->tool
.itrace_start
= perf_event__drop_aux
,
612 inject
->tool
.ordered_events
= true;
613 inject
->tool
.ordering_requires_timestamps
= true;
614 /* Allow space in the header for new attributes */
615 output_data_offset
= 4096;
620 if (!inject
->itrace_synth_opts
.set
)
621 auxtrace_index__free(&session
->auxtrace_index
);
623 if (!file_out
->is_pipe
)
624 lseek(fd
, output_data_offset
, SEEK_SET
);
626 ret
= perf_session__process_events(session
);
628 if (!file_out
->is_pipe
) {
629 if (inject
->build_ids
) {
630 perf_header__set_feat(&session
->header
,
632 if (inject
->have_auxtrace
)
633 dsos__hit_all(session
);
636 * The AUX areas have been removed and replaced with
637 * synthesized hardware events, so clear the feature flag and
640 if (inject
->itrace_synth_opts
.set
) {
641 struct perf_evsel
*evsel
;
643 perf_header__clear_feat(&session
->header
,
645 if (inject
->itrace_synth_opts
.last_branch
)
646 perf_header__set_feat(&session
->header
,
647 HEADER_BRANCH_STACK
);
648 evsel
= perf_evlist__id2evsel_strict(session
->evlist
,
651 pr_debug("Deleting %s\n",
652 perf_evsel__name(evsel
));
653 perf_evlist__remove(session
->evlist
, evsel
);
654 perf_evsel__delete(evsel
);
659 session
->header
.data_offset
= output_data_offset
;
660 session
->header
.data_size
= inject
->bytes_written
;
661 perf_session__write_header(session
, session
->evlist
, fd
, true);
667 int cmd_inject(int argc
, const char **argv
, const char *prefix __maybe_unused
)
669 struct perf_inject inject
= {
671 .sample
= perf_event__repipe_sample
,
672 .mmap
= perf_event__repipe
,
673 .mmap2
= perf_event__repipe
,
674 .comm
= perf_event__repipe
,
675 .fork
= perf_event__repipe
,
676 .exit
= perf_event__repipe
,
677 .lost
= perf_event__repipe
,
678 .lost_samples
= perf_event__repipe
,
679 .aux
= perf_event__repipe
,
680 .itrace_start
= perf_event__repipe
,
681 .context_switch
= perf_event__repipe
,
682 .read
= perf_event__repipe_sample
,
683 .throttle
= perf_event__repipe
,
684 .unthrottle
= perf_event__repipe
,
685 .attr
= perf_event__repipe_attr
,
686 .tracing_data
= perf_event__repipe_op2_synth
,
687 .auxtrace_info
= perf_event__repipe_op2_synth
,
688 .auxtrace
= perf_event__repipe_auxtrace
,
689 .auxtrace_error
= perf_event__repipe_op2_synth
,
690 .finished_round
= perf_event__repipe_oe_synth
,
691 .build_id
= perf_event__repipe_op2_synth
,
692 .id_index
= perf_event__repipe_op2_synth
,
695 .samples
= LIST_HEAD_INIT(inject
.samples
),
698 .mode
= PERF_DATA_MODE_WRITE
,
701 struct perf_data_file file
= {
702 .mode
= PERF_DATA_MODE_READ
,
706 const struct option options
[] = {
707 OPT_BOOLEAN('b', "build-ids", &inject
.build_ids
,
708 "Inject build-ids into the output stream"),
709 OPT_STRING('i', "input", &inject
.input_name
, "file",
711 OPT_STRING('o', "output", &inject
.output
.path
, "file",
713 OPT_BOOLEAN('s', "sched-stat", &inject
.sched_stat
,
714 "Merge sched-stat and sched-switch for getting events "
715 "where and how long tasks slept"),
716 OPT_INCR('v', "verbose", &verbose
,
717 "be more verbose (show build ids, etc)"),
718 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
, "file",
719 "kallsyms pathname"),
720 OPT_BOOLEAN('f', "force", &file
.force
, "don't complain, do it"),
721 OPT_CALLBACK_OPTARG(0, "itrace", &inject
.itrace_synth_opts
,
722 NULL
, "opts", "Instruction Tracing options",
723 itrace_parse_synth_opts
),
724 OPT_BOOLEAN(0, "strip", &inject
.strip
,
725 "strip non-synthesized events (use with --itrace)"),
728 const char * const inject_usage
[] = {
729 "perf inject [<options>]",
733 argc
= parse_options(argc
, argv
, options
, inject_usage
, 0);
736 * Any (unrecognized) arguments left?
739 usage_with_options(inject_usage
, options
);
741 if (inject
.strip
&& !inject
.itrace_synth_opts
.set
) {
742 pr_err("--strip option requires --itrace option\n");
746 if (perf_data_file__open(&inject
.output
)) {
747 perror("failed to create output file");
751 inject
.tool
.ordered_events
= inject
.sched_stat
;
753 file
.path
= inject
.input_name
;
754 inject
.session
= perf_session__new(&file
, true, &inject
.tool
);
755 if (inject
.session
== NULL
)
758 ret
= symbol__init(&inject
.session
->header
.env
);
762 ret
= __cmd_inject(&inject
);
765 perf_session__delete(inject
.session
);