Commit | Line | Data |
---|---|---|
454c407e TZ |
1 | /* |
2 | * builtin-inject.c | |
3 | * | |
4 | * Builtin inject command: Examine the live mode (stdin) event stream | |
5 | * and repipe it to stdout while optionally injecting additional | |
6 | * events into it. | |
7 | */ | |
8 | #include "builtin.h" | |
9 | ||
10 | #include "perf.h" | |
26a031e1 AV |
11 | #include "util/color.h" |
12 | #include "util/evlist.h" | |
13 | #include "util/evsel.h" | |
454c407e | 14 | #include "util/session.h" |
45694aa7 | 15 | #include "util/tool.h" |
454c407e TZ |
16 | #include "util/debug.h" |
17 | ||
18 | #include "util/parse-options.h" | |
19 | ||
26a031e1 AV |
20 | #include <linux/list.h> |
21 | ||
5ded57ac ACM |
22 | struct perf_inject { |
23 | struct perf_tool tool; | |
24 | bool build_ids; | |
26a031e1 | 25 | bool sched_stat; |
e558a5bd AV |
26 | const char *input_name; |
27 | int pipe_output, | |
28 | output; | |
29 | u64 bytes_written; | |
26a031e1 AV |
30 | struct list_head samples; |
31 | }; | |
32 | ||
33 | struct event_entry { | |
34 | struct list_head node; | |
35 | u32 tid; | |
36 | union perf_event event[0]; | |
5ded57ac | 37 | }; |
454c407e | 38 | |
e558a5bd | 39 | static int perf_event__repipe_synth(struct perf_tool *tool, |
d20deb64 | 40 | union perf_event *event, |
1d037ca1 | 41 | struct machine *machine __maybe_unused) |
454c407e | 42 | { |
e558a5bd | 43 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); |
454c407e TZ |
44 | uint32_t size; |
45 | void *buf = event; | |
46 | ||
47 | size = event->header.size; | |
48 | ||
49 | while (size) { | |
e558a5bd | 50 | int ret = write(inject->output, buf, size); |
454c407e TZ |
51 | if (ret < 0) |
52 | return -errno; | |
53 | ||
54 | size -= ret; | |
55 | buf += ret; | |
e558a5bd | 56 | inject->bytes_written += ret; |
454c407e TZ |
57 | } |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
45694aa7 | 62 | static int perf_event__repipe_op2_synth(struct perf_tool *tool, |
743eb868 | 63 | union perf_event *event, |
1d037ca1 IT |
64 | struct perf_session *session |
65 | __maybe_unused) | |
743eb868 | 66 | { |
45694aa7 | 67 | return perf_event__repipe_synth(tool, event, NULL); |
743eb868 ACM |
68 | } |
69 | ||
45694aa7 | 70 | static int perf_event__repipe_event_type_synth(struct perf_tool *tool, |
743eb868 ACM |
71 | union perf_event *event) |
72 | { | |
45694aa7 | 73 | return perf_event__repipe_synth(tool, event, NULL); |
743eb868 ACM |
74 | } |
75 | ||
d20deb64 | 76 | static int perf_event__repipe_tracing_data_synth(union perf_event *event, |
1d037ca1 IT |
77 | struct perf_session *session |
78 | __maybe_unused) | |
d20deb64 | 79 | { |
743eb868 | 80 | return perf_event__repipe_synth(NULL, event, NULL); |
d20deb64 ACM |
81 | } |
82 | ||
10d0f086 | 83 | static int perf_event__repipe_attr(union perf_event *event, |
1d037ca1 | 84 | struct perf_evlist **pevlist __maybe_unused) |
10d0f086 | 85 | { |
1a1ed1ba SE |
86 | int ret; |
87 | ret = perf_event__process_attr(event, pevlist); | |
88 | if (ret) | |
89 | return ret; | |
90 | ||
d20deb64 | 91 | return perf_event__repipe_synth(NULL, event, NULL); |
10d0f086 ACM |
92 | } |
93 | ||
45694aa7 | 94 | static int perf_event__repipe(struct perf_tool *tool, |
d20deb64 | 95 | union perf_event *event, |
1d037ca1 | 96 | struct perf_sample *sample __maybe_unused, |
743eb868 | 97 | struct machine *machine) |
640c03ce | 98 | { |
45694aa7 | 99 | return perf_event__repipe_synth(tool, event, machine); |
640c03ce ACM |
100 | } |
101 | ||
26a031e1 AV |
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); | |
107 | ||
45694aa7 | 108 | static int perf_event__repipe_sample(struct perf_tool *tool, |
d20deb64 | 109 | union perf_event *event, |
26a031e1 AV |
110 | struct perf_sample *sample, |
111 | struct perf_evsel *evsel, | |
112 | struct machine *machine) | |
9e69c210 | 113 | { |
26a031e1 AV |
114 | if (evsel->handler.func) { |
115 | inject_handler f = evsel->handler.func; | |
116 | return f(tool, event, sample, evsel, machine); | |
117 | } | |
118 | ||
45694aa7 | 119 | return perf_event__repipe_synth(tool, event, machine); |
9e69c210 ACM |
120 | } |
121 | ||
45694aa7 | 122 | static int perf_event__repipe_mmap(struct perf_tool *tool, |
d20deb64 | 123 | union perf_event *event, |
8115d60c | 124 | struct perf_sample *sample, |
743eb868 | 125 | struct machine *machine) |
454c407e TZ |
126 | { |
127 | int err; | |
128 | ||
45694aa7 ACM |
129 | err = perf_event__process_mmap(tool, event, sample, machine); |
130 | perf_event__repipe(tool, event, sample, machine); | |
454c407e TZ |
131 | |
132 | return err; | |
133 | } | |
134 | ||
f62d3f0f | 135 | static int perf_event__repipe_fork(struct perf_tool *tool, |
d20deb64 | 136 | union perf_event *event, |
8115d60c | 137 | struct perf_sample *sample, |
743eb868 | 138 | struct machine *machine) |
454c407e TZ |
139 | { |
140 | int err; | |
141 | ||
f62d3f0f | 142 | err = perf_event__process_fork(tool, event, sample, machine); |
45694aa7 | 143 | perf_event__repipe(tool, event, sample, machine); |
454c407e TZ |
144 | |
145 | return err; | |
146 | } | |
147 | ||
8115d60c ACM |
148 | static int perf_event__repipe_tracing_data(union perf_event *event, |
149 | struct perf_session *session) | |
454c407e TZ |
150 | { |
151 | int err; | |
152 | ||
743eb868 | 153 | perf_event__repipe_synth(NULL, event, NULL); |
8115d60c | 154 | err = perf_event__process_tracing_data(event, session); |
454c407e TZ |
155 | |
156 | return err; | |
157 | } | |
158 | ||
090f7204 | 159 | static int dso__read_build_id(struct dso *self) |
454c407e | 160 | { |
090f7204 ACM |
161 | if (self->has_build_id) |
162 | return 0; | |
454c407e | 163 | |
090f7204 ACM |
164 | if (filename__read_build_id(self->long_name, self->build_id, |
165 | sizeof(self->build_id)) > 0) { | |
166 | self->has_build_id = true; | |
167 | return 0; | |
168 | } | |
454c407e | 169 | |
090f7204 ACM |
170 | return -1; |
171 | } | |
454c407e | 172 | |
45694aa7 | 173 | static int dso__inject_build_id(struct dso *self, struct perf_tool *tool, |
743eb868 | 174 | struct machine *machine) |
090f7204 ACM |
175 | { |
176 | u16 misc = PERF_RECORD_MISC_USER; | |
090f7204 | 177 | int err; |
454c407e | 178 | |
090f7204 ACM |
179 | if (dso__read_build_id(self) < 0) { |
180 | pr_debug("no build_id found for %s\n", self->long_name); | |
181 | return -1; | |
182 | } | |
454c407e | 183 | |
090f7204 ACM |
184 | if (self->kernel) |
185 | misc = PERF_RECORD_MISC_KERNEL; | |
454c407e | 186 | |
45694aa7 | 187 | err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe, |
743eb868 | 188 | machine); |
090f7204 ACM |
189 | if (err) { |
190 | pr_err("Can't synthesize build_id event for %s\n", self->long_name); | |
454c407e TZ |
191 | return -1; |
192 | } | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
45694aa7 | 197 | static int perf_event__inject_buildid(struct perf_tool *tool, |
d20deb64 | 198 | union perf_event *event, |
8115d60c | 199 | struct perf_sample *sample, |
1d037ca1 | 200 | struct perf_evsel *evsel __maybe_unused, |
743eb868 | 201 | struct machine *machine) |
454c407e TZ |
202 | { |
203 | struct addr_location al; | |
204 | struct thread *thread; | |
205 | u8 cpumode; | |
454c407e TZ |
206 | |
207 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
208 | ||
743eb868 | 209 | thread = machine__findnew_thread(machine, event->ip.pid); |
454c407e TZ |
210 | if (thread == NULL) { |
211 | pr_err("problem processing %d event, skipping it.\n", | |
212 | event->header.type); | |
454c407e TZ |
213 | goto repipe; |
214 | } | |
215 | ||
743eb868 ACM |
216 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
217 | event->ip.ip, &al); | |
454c407e TZ |
218 | |
219 | if (al.map != NULL) { | |
220 | if (!al.map->dso->hit) { | |
221 | al.map->dso->hit = 1; | |
090f7204 | 222 | if (map__load(al.map, NULL) >= 0) { |
45694aa7 | 223 | dso__inject_build_id(al.map->dso, tool, machine); |
090f7204 ACM |
224 | /* |
225 | * If this fails, too bad, let the other side | |
226 | * account this as unresolved. | |
227 | */ | |
393be2e3 | 228 | } else { |
29a0fc9b | 229 | #ifdef LIBELF_SUPPORT |
454c407e TZ |
230 | pr_warning("no symbols found in %s, maybe " |
231 | "install a debug package?\n", | |
232 | al.map->dso->long_name); | |
393be2e3 NK |
233 | #endif |
234 | } | |
454c407e TZ |
235 | } |
236 | } | |
237 | ||
238 | repipe: | |
45694aa7 | 239 | perf_event__repipe(tool, event, sample, machine); |
090f7204 | 240 | return 0; |
454c407e TZ |
241 | } |
242 | ||
26a031e1 AV |
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) | |
248 | { | |
249 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); | |
250 | struct event_entry *ent; | |
251 | ||
252 | list_for_each_entry(ent, &inject->samples, node) { | |
253 | if (sample->tid == ent->tid) { | |
254 | list_del_init(&ent->node); | |
255 | free(ent); | |
256 | break; | |
257 | } | |
258 | } | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
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) | |
268 | { | |
269 | struct perf_inject *inject = container_of(tool, struct perf_inject, tool); | |
270 | struct event_entry *ent; | |
271 | ||
272 | perf_inject__sched_process_exit(tool, event, sample, evsel, machine); | |
273 | ||
274 | ent = malloc(event->header.size + sizeof(struct event_entry)); | |
275 | if (ent == NULL) { | |
276 | color_fprintf(stderr, PERF_COLOR_RED, | |
277 | "Not enough memory to process sched switch event!"); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | ent->tid = sample->tid; | |
282 | memcpy(&ent->event, event, event->header.size); | |
283 | list_add(&ent->node, &inject->samples); | |
284 | return 0; | |
285 | } | |
286 | ||
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) | |
292 | { | |
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"); | |
298 | ||
299 | list_for_each_entry(ent, &inject->samples, node) { | |
300 | if (pid == ent->tid) | |
301 | goto found; | |
302 | } | |
303 | ||
304 | return 0; | |
305 | found: | |
306 | event_sw = &ent->event[0]; | |
307 | perf_evsel__parse_sample(evsel, event_sw, &sample_sw); | |
308 | ||
309 | sample_sw.period = sample->period; | |
310 | sample_sw.time = sample->time; | |
311 | perf_event__synthesize_sample(event_sw, evsel->attr.sample_type, | |
312 | &sample_sw, false); | |
313 | return perf_event__repipe(tool, event_sw, &sample_sw, machine); | |
314 | } | |
315 | ||
454c407e TZ |
316 | extern volatile int session_done; |
317 | ||
1d037ca1 | 318 | static void sig_handler(int sig __maybe_unused) |
454c407e TZ |
319 | { |
320 | session_done = 1; | |
321 | } | |
322 | ||
26a031e1 AV |
323 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
324 | u64 sample_type, const char *sample_msg) | |
325 | { | |
326 | struct perf_event_attr *attr = &evsel->attr; | |
327 | const char *name = perf_evsel__name(evsel); | |
328 | ||
329 | if (!(attr->sample_type & sample_type)) { | |
330 | pr_err("Samples for %s event do not have %s attribute set.", | |
331 | name, sample_msg); | |
332 | return -EINVAL; | |
333 | } | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
5ded57ac | 338 | static int __cmd_inject(struct perf_inject *inject) |
454c407e TZ |
339 | { |
340 | struct perf_session *session; | |
341 | int ret = -EINVAL; | |
342 | ||
343 | signal(SIGINT, sig_handler); | |
344 | ||
5ded57ac ACM |
345 | if (inject->build_ids) { |
346 | inject->tool.sample = perf_event__inject_buildid; | |
347 | inject->tool.mmap = perf_event__repipe_mmap; | |
f62d3f0f | 348 | inject->tool.fork = perf_event__repipe_fork; |
5ded57ac | 349 | inject->tool.tracing_data = perf_event__repipe_tracing_data; |
454c407e TZ |
350 | } |
351 | ||
e558a5bd | 352 | session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool); |
454c407e TZ |
353 | if (session == NULL) |
354 | return -ENOMEM; | |
355 | ||
26a031e1 AV |
356 | if (inject->sched_stat) { |
357 | struct perf_evsel *evsel; | |
358 | ||
359 | inject->tool.ordered_samples = true; | |
360 | ||
361 | list_for_each_entry(evsel, &session->evlist->entries, node) { | |
362 | const char *name = perf_evsel__name(evsel); | |
363 | ||
364 | if (!strcmp(name, "sched:sched_switch")) { | |
365 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID")) | |
366 | return -EINVAL; | |
367 | ||
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; | |
373 | } | |
374 | } | |
375 | ||
e558a5bd AV |
376 | if (!inject->pipe_output) |
377 | lseek(inject->output, session->header.data_offset, SEEK_SET); | |
378 | ||
5ded57ac | 379 | ret = perf_session__process_events(session, &inject->tool); |
454c407e | 380 | |
e558a5bd AV |
381 | if (!inject->pipe_output) { |
382 | session->header.data_size = inject->bytes_written; | |
383 | perf_session__write_header(session, session->evlist, inject->output, true); | |
384 | } | |
385 | ||
454c407e TZ |
386 | perf_session__delete(session); |
387 | ||
388 | return ret; | |
389 | } | |
390 | ||
1d037ca1 | 391 | int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused) |
454c407e | 392 | { |
5ded57ac ACM |
393 | struct perf_inject inject = { |
394 | .tool = { | |
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, | |
408 | }, | |
e558a5bd | 409 | .input_name = "-", |
26a031e1 | 410 | .samples = LIST_HEAD_INIT(inject.samples), |
5ded57ac | 411 | }; |
e558a5bd | 412 | const char *output_name = "-"; |
5ded57ac ACM |
413 | const struct option options[] = { |
414 | OPT_BOOLEAN('b', "build-ids", &inject.build_ids, | |
415 | "Inject build-ids into the output stream"), | |
e558a5bd AV |
416 | OPT_STRING('i', "input", &inject.input_name, "file", |
417 | "input file name"), | |
418 | OPT_STRING('o', "output", &output_name, "file", | |
419 | "output file name"), | |
26a031e1 AV |
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"), | |
5ded57ac ACM |
423 | OPT_INCR('v', "verbose", &verbose, |
424 | "be more verbose (show build ids, etc)"), | |
425 | OPT_END() | |
426 | }; | |
002439e8 ACM |
427 | const char * const inject_usage[] = { |
428 | "perf inject [<options>]", | |
429 | NULL | |
430 | }; | |
5ded57ac | 431 | |
002439e8 | 432 | argc = parse_options(argc, argv, options, inject_usage, 0); |
454c407e TZ |
433 | |
434 | /* | |
435 | * Any (unrecognized) arguments left? | |
436 | */ | |
437 | if (argc) | |
002439e8 | 438 | usage_with_options(inject_usage, options); |
454c407e | 439 | |
e558a5bd AV |
440 | if (!strcmp(output_name, "-")) { |
441 | inject.pipe_output = 1; | |
442 | inject.output = STDOUT_FILENO; | |
443 | } else { | |
444 | inject.output = open(output_name, O_CREAT | O_WRONLY | O_TRUNC, | |
445 | S_IRUSR | S_IWUSR); | |
446 | if (inject.output < 0) { | |
447 | perror("failed to create output file"); | |
448 | return -1; | |
449 | } | |
450 | } | |
451 | ||
454c407e TZ |
452 | if (symbol__init() < 0) |
453 | return -1; | |
454 | ||
5ded57ac | 455 | return __cmd_inject(&inject); |
454c407e | 456 | } |