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"
18 #include "util/parse-options.h"
20 #include <linux/list.h>
23 struct perf_tool tool
;
26 const char *input_name
;
30 struct list_head samples
;
34 struct list_head node
;
36 union perf_event event
[0];
39 static int perf_event__repipe_synth(struct perf_tool
*tool
,
40 union perf_event
*event
,
41 struct machine
*machine __maybe_unused
)
43 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
47 size
= event
->header
.size
;
50 int ret
= write(inject
->output
, buf
, size
);
56 inject
->bytes_written
+= ret
;
62 static int perf_event__repipe_op2_synth(struct perf_tool
*tool
,
63 union perf_event
*event
,
64 struct perf_session
*session
67 return perf_event__repipe_synth(tool
, event
, NULL
);
70 static int perf_event__repipe_event_type_synth(struct perf_tool
*tool
,
71 union perf_event
*event
)
73 return perf_event__repipe_synth(tool
, event
, NULL
);
76 static int perf_event__repipe_tracing_data_synth(union perf_event
*event
,
77 struct perf_session
*session
80 return perf_event__repipe_synth(NULL
, event
, NULL
);
83 static int perf_event__repipe_attr(union perf_event
*event
,
84 struct perf_evlist
**pevlist __maybe_unused
)
87 ret
= perf_event__process_attr(event
, pevlist
);
91 return perf_event__repipe_synth(NULL
, event
, NULL
);
94 static int perf_event__repipe(struct perf_tool
*tool
,
95 union perf_event
*event
,
96 struct perf_sample
*sample __maybe_unused
,
97 struct machine
*machine
)
99 return perf_event__repipe_synth(tool
, event
, machine
);
102 typedef int (*inject_handler
)(struct perf_tool
*tool
,
103 union perf_event
*event
,
104 struct perf_sample
*sample
,
105 struct perf_evsel
*evsel
,
106 struct machine
*machine
);
108 static int perf_event__repipe_sample(struct perf_tool
*tool
,
109 union perf_event
*event
,
110 struct perf_sample
*sample
,
111 struct perf_evsel
*evsel
,
112 struct machine
*machine
)
114 if (evsel
->handler
.func
) {
115 inject_handler f
= evsel
->handler
.func
;
116 return f(tool
, event
, sample
, evsel
, machine
);
119 return perf_event__repipe_synth(tool
, event
, machine
);
122 static int perf_event__repipe_mmap(struct perf_tool
*tool
,
123 union perf_event
*event
,
124 struct perf_sample
*sample
,
125 struct machine
*machine
)
129 err
= perf_event__process_mmap(tool
, event
, sample
, machine
);
130 perf_event__repipe(tool
, event
, sample
, machine
);
135 static int perf_event__repipe_fork(struct perf_tool
*tool
,
136 union perf_event
*event
,
137 struct perf_sample
*sample
,
138 struct machine
*machine
)
142 err
= perf_event__process_fork(tool
, event
, sample
, machine
);
143 perf_event__repipe(tool
, event
, sample
, machine
);
148 static int perf_event__repipe_tracing_data(union perf_event
*event
,
149 struct perf_session
*session
)
153 perf_event__repipe_synth(NULL
, event
, NULL
);
154 err
= perf_event__process_tracing_data(event
, session
);
159 static int dso__read_build_id(struct dso
*self
)
161 if (self
->has_build_id
)
164 if (filename__read_build_id(self
->long_name
, self
->build_id
,
165 sizeof(self
->build_id
)) > 0) {
166 self
->has_build_id
= true;
173 static int dso__inject_build_id(struct dso
*self
, struct perf_tool
*tool
,
174 struct machine
*machine
)
176 u16 misc
= PERF_RECORD_MISC_USER
;
179 if (dso__read_build_id(self
) < 0) {
180 pr_debug("no build_id found for %s\n", self
->long_name
);
185 misc
= PERF_RECORD_MISC_KERNEL
;
187 err
= perf_event__synthesize_build_id(tool
, self
, misc
, perf_event__repipe
,
190 pr_err("Can't synthesize build_id event for %s\n", self
->long_name
);
197 static int perf_event__inject_buildid(struct perf_tool
*tool
,
198 union perf_event
*event
,
199 struct perf_sample
*sample
,
200 struct perf_evsel
*evsel __maybe_unused
,
201 struct machine
*machine
)
203 struct addr_location al
;
204 struct thread
*thread
;
207 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
209 thread
= machine__findnew_thread(machine
, event
->ip
.pid
);
210 if (thread
== NULL
) {
211 pr_err("problem processing %d event, skipping it.\n",
216 thread__find_addr_map(thread
, machine
, cpumode
, MAP__FUNCTION
,
219 if (al
.map
!= NULL
) {
220 if (!al
.map
->dso
->hit
) {
221 al
.map
->dso
->hit
= 1;
222 if (map__load(al
.map
, NULL
) >= 0) {
223 dso__inject_build_id(al
.map
->dso
, tool
, machine
);
225 * If this fails, too bad, let the other side
226 * account this as unresolved.
229 #ifdef LIBELF_SUPPORT
230 pr_warning("no symbols found in %s, maybe "
231 "install a debug package?\n",
232 al
.map
->dso
->long_name
);
239 perf_event__repipe(tool
, event
, sample
, machine
);
243 static int perf_inject__sched_process_exit(struct perf_tool
*tool
,
244 union perf_event
*event __maybe_unused
,
245 struct perf_sample
*sample
,
246 struct perf_evsel
*evsel __maybe_unused
,
247 struct machine
*machine __maybe_unused
)
249 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
250 struct event_entry
*ent
;
252 list_for_each_entry(ent
, &inject
->samples
, node
) {
253 if (sample
->tid
== ent
->tid
) {
254 list_del_init(&ent
->node
);
263 static int perf_inject__sched_switch(struct perf_tool
*tool
,
264 union perf_event
*event
,
265 struct perf_sample
*sample
,
266 struct perf_evsel
*evsel
,
267 struct machine
*machine
)
269 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
270 struct event_entry
*ent
;
272 perf_inject__sched_process_exit(tool
, event
, sample
, evsel
, machine
);
274 ent
= malloc(event
->header
.size
+ sizeof(struct event_entry
));
276 color_fprintf(stderr
, PERF_COLOR_RED
,
277 "Not enough memory to process sched switch event!");
281 ent
->tid
= sample
->tid
;
282 memcpy(&ent
->event
, event
, event
->header
.size
);
283 list_add(&ent
->node
, &inject
->samples
);
287 static int perf_inject__sched_stat(struct perf_tool
*tool
,
288 union perf_event
*event __maybe_unused
,
289 struct perf_sample
*sample
,
290 struct perf_evsel
*evsel
,
291 struct machine
*machine
)
293 struct event_entry
*ent
;
294 union perf_event
*event_sw
;
295 struct perf_sample sample_sw
;
296 struct perf_inject
*inject
= container_of(tool
, struct perf_inject
, tool
);
297 u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
299 list_for_each_entry(ent
, &inject
->samples
, node
) {
306 event_sw
= &ent
->event
[0];
307 perf_evsel__parse_sample(evsel
, event_sw
, &sample_sw
);
309 sample_sw
.period
= sample
->period
;
310 sample_sw
.time
= sample
->time
;
311 perf_event__synthesize_sample(event_sw
, evsel
->attr
.sample_type
,
313 return perf_event__repipe(tool
, event_sw
, &sample_sw
, machine
);
316 extern volatile int session_done
;
318 static void sig_handler(int sig __maybe_unused
)
323 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
324 u64 sample_type
, const char *sample_msg
)
326 struct perf_event_attr
*attr
= &evsel
->attr
;
327 const char *name
= perf_evsel__name(evsel
);
329 if (!(attr
->sample_type
& sample_type
)) {
330 pr_err("Samples for %s event do not have %s attribute set.",
338 static int __cmd_inject(struct perf_inject
*inject
)
340 struct perf_session
*session
;
343 signal(SIGINT
, sig_handler
);
345 if (inject
->build_ids
) {
346 inject
->tool
.sample
= perf_event__inject_buildid
;
347 inject
->tool
.mmap
= perf_event__repipe_mmap
;
348 inject
->tool
.fork
= perf_event__repipe_fork
;
349 inject
->tool
.tracing_data
= perf_event__repipe_tracing_data
;
352 session
= perf_session__new(inject
->input_name
, O_RDONLY
, false, true, &inject
->tool
);
356 if (inject
->sched_stat
) {
357 struct perf_evsel
*evsel
;
359 inject
->tool
.ordered_samples
= true;
361 list_for_each_entry(evsel
, &session
->evlist
->entries
, node
) {
362 const char *name
= perf_evsel__name(evsel
);
364 if (!strcmp(name
, "sched:sched_switch")) {
365 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID"))
368 evsel
->handler
.func
= perf_inject__sched_switch
;
369 } else if (!strcmp(name
, "sched:sched_process_exit"))
370 evsel
->handler
.func
= perf_inject__sched_process_exit
;
371 else if (!strncmp(name
, "sched:sched_stat_", 17))
372 evsel
->handler
.func
= perf_inject__sched_stat
;
376 if (!inject
->pipe_output
)
377 lseek(inject
->output
, session
->header
.data_offset
, SEEK_SET
);
379 ret
= perf_session__process_events(session
, &inject
->tool
);
381 if (!inject
->pipe_output
) {
382 session
->header
.data_size
= inject
->bytes_written
;
383 perf_session__write_header(session
, session
->evlist
, inject
->output
, true);
386 perf_session__delete(session
);
391 int cmd_inject(int argc
, const char **argv
, const char *prefix __maybe_unused
)
393 struct perf_inject inject
= {
395 .sample
= perf_event__repipe_sample
,
396 .mmap
= perf_event__repipe
,
397 .comm
= perf_event__repipe
,
398 .fork
= perf_event__repipe
,
399 .exit
= perf_event__repipe
,
400 .lost
= perf_event__repipe
,
401 .read
= perf_event__repipe_sample
,
402 .throttle
= perf_event__repipe
,
403 .unthrottle
= perf_event__repipe
,
404 .attr
= perf_event__repipe_attr
,
405 .event_type
= perf_event__repipe_event_type_synth
,
406 .tracing_data
= perf_event__repipe_tracing_data_synth
,
407 .build_id
= perf_event__repipe_op2_synth
,
410 .samples
= LIST_HEAD_INIT(inject
.samples
),
412 const char *output_name
= "-";
413 const struct option options
[] = {
414 OPT_BOOLEAN('b', "build-ids", &inject
.build_ids
,
415 "Inject build-ids into the output stream"),
416 OPT_STRING('i', "input", &inject
.input_name
, "file",
418 OPT_STRING('o', "output", &output_name
, "file",
420 OPT_BOOLEAN('s', "sched-stat", &inject
.sched_stat
,
421 "Merge sched-stat and sched-switch for getting events "
422 "where and how long tasks slept"),
423 OPT_INCR('v', "verbose", &verbose
,
424 "be more verbose (show build ids, etc)"),
427 const char * const inject_usage
[] = {
428 "perf inject [<options>]",
432 argc
= parse_options(argc
, argv
, options
, inject_usage
, 0);
435 * Any (unrecognized) arguments left?
438 usage_with_options(inject_usage
, options
);
440 if (!strcmp(output_name
, "-")) {
441 inject
.pipe_output
= 1;
442 inject
.output
= STDOUT_FILENO
;
444 inject
.output
= open(output_name
, O_CREAT
| O_WRONLY
| O_TRUNC
,
446 if (inject
.output
< 0) {
447 perror("failed to create output file");
452 if (symbol__init() < 0)
455 return __cmd_inject(&inject
);