perf session: Simplify tool stubs
[deliverable/linux.git] / tools / perf / builtin-inject.c
CommitLineData
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 16#include "util/debug.h"
54a3cf59 17#include "util/build-id.h"
f5fc1412 18#include "util/data.h"
0f0aa5e0 19#include "util/auxtrace.h"
9b07e27f 20#include "util/jit.h"
454c407e 21
4b6ab94e 22#include <subcmd/parse-options.h>
454c407e 23
26a031e1
AV
24#include <linux/list.h>
25
5ded57ac 26struct perf_inject {
3406912c 27 struct perf_tool tool;
1cb8bdcc 28 struct perf_session *session;
3406912c
JO
29 bool build_ids;
30 bool sched_stat;
cd10b289 31 bool have_auxtrace;
f56fb986 32 bool strip;
9b07e27f 33 bool jit_mode;
3406912c
JO
34 const char *input_name;
35 struct perf_data_file output;
36 u64 bytes_written;
73117308 37 u64 aux_id;
3406912c 38 struct list_head samples;
0f0aa5e0 39 struct itrace_synth_opts itrace_synth_opts;
26a031e1
AV
40};
41
42struct event_entry {
43 struct list_head node;
44 u32 tid;
45 union perf_event event[0];
5ded57ac 46};
454c407e 47
cd17a9b5 48static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
454c407e 49{
3406912c 50 ssize_t size;
454c407e 51
cd17a9b5 52 size = perf_data_file__write(&inject->output, buf, sz);
3406912c
JO
53 if (size < 0)
54 return -errno;
454c407e 55
3406912c 56 inject->bytes_written += size;
454c407e
TZ
57 return 0;
58}
59
cd17a9b5
AH
60static int perf_event__repipe_synth(struct perf_tool *tool,
61 union perf_event *event)
62{
63 struct perf_inject *inject = container_of(tool, struct perf_inject,
64 tool);
65
66 return output_bytes(inject, event, event->header.size);
67}
68
d704ebda
ACM
69static int perf_event__repipe_oe_synth(struct perf_tool *tool,
70 union perf_event *event,
71 struct ordered_events *oe __maybe_unused)
72{
73 return perf_event__repipe_synth(tool, event);
74}
75
9b07e27f
SE
76#ifdef HAVE_LIBELF_SUPPORT
77static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
78 union perf_event *event __maybe_unused,
79 struct ordered_events *oe __maybe_unused)
80{
81 return 0;
82}
83#endif
84
45694aa7 85static int perf_event__repipe_op2_synth(struct perf_tool *tool,
743eb868 86 union perf_event *event,
1d037ca1
IT
87 struct perf_session *session
88 __maybe_unused)
743eb868 89{
63c2c9f8 90 return perf_event__repipe_synth(tool, event);
743eb868
ACM
91}
92
47c3d109
AH
93static int perf_event__repipe_attr(struct perf_tool *tool,
94 union perf_event *event,
95 struct perf_evlist **pevlist)
10d0f086 96{
89c97d93
AH
97 struct perf_inject *inject = container_of(tool, struct perf_inject,
98 tool);
1a1ed1ba 99 int ret;
47c3d109
AH
100
101 ret = perf_event__process_attr(tool, event, pevlist);
1a1ed1ba
SE
102 if (ret)
103 return ret;
104
a261e4a0 105 if (!inject->output.is_pipe)
89c97d93
AH
106 return 0;
107
47c3d109 108 return perf_event__repipe_synth(tool, event);
10d0f086
ACM
109}
110
e31f0d01
AH
111#ifdef HAVE_AUXTRACE_SUPPORT
112
113static int copy_bytes(struct perf_inject *inject, int fd, off_t size)
114{
115 char buf[4096];
116 ssize_t ssz;
117 int ret;
118
119 while (size > 0) {
120 ssz = read(fd, buf, min(size, (off_t)sizeof(buf)));
121 if (ssz < 0)
122 return -errno;
123 ret = output_bytes(inject, buf, ssz);
124 if (ret)
125 return ret;
126 size -= ssz;
127 }
128
129 return 0;
130}
131
cd17a9b5
AH
132static s64 perf_event__repipe_auxtrace(struct perf_tool *tool,
133 union perf_event *event,
134 struct perf_session *session
135 __maybe_unused)
136{
137 struct perf_inject *inject = container_of(tool, struct perf_inject,
138 tool);
139 int ret;
140
cd10b289
AH
141 inject->have_auxtrace = true;
142
99fa2984
AH
143 if (!inject->output.is_pipe) {
144 off_t offset;
145
146 offset = lseek(inject->output.fd, 0, SEEK_CUR);
147 if (offset == -1)
148 return -errno;
149 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index,
150 event, offset);
151 if (ret < 0)
152 return ret;
153 }
154
cd17a9b5
AH
155 if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
156 ret = output_bytes(inject, event, event->header.size);
157 if (ret < 0)
158 return ret;
159 ret = copy_bytes(inject, perf_data_file__fd(session->file),
160 event->auxtrace.size);
161 } else {
162 ret = output_bytes(inject, event,
163 event->header.size + event->auxtrace.size);
164 }
165 if (ret < 0)
166 return ret;
167
168 return event->auxtrace.size;
169}
170
e31f0d01
AH
171#else
172
173static s64
174perf_event__repipe_auxtrace(struct perf_tool *tool __maybe_unused,
175 union perf_event *event __maybe_unused,
176 struct perf_session *session __maybe_unused)
177{
178 pr_err("AUX area tracing not supported\n");
179 return -EINVAL;
180}
181
182#endif
183
45694aa7 184static int perf_event__repipe(struct perf_tool *tool,
d20deb64 185 union perf_event *event,
1d037ca1 186 struct perf_sample *sample __maybe_unused,
63c2c9f8 187 struct machine *machine __maybe_unused)
640c03ce 188{
63c2c9f8 189 return perf_event__repipe_synth(tool, event);
640c03ce
ACM
190}
191
f56fb986
AH
192static 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)
196{
197 return 0;
198}
199
73117308
AH
200static 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)
204{
205 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
206
207 if (!inject->aux_id)
208 inject->aux_id = sample->id;
209
210 return 0;
211}
212
26a031e1
AV
213typedef 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);
218
45694aa7 219static int perf_event__repipe_sample(struct perf_tool *tool,
d20deb64 220 union perf_event *event,
26a031e1
AV
221 struct perf_sample *sample,
222 struct perf_evsel *evsel,
223 struct machine *machine)
9e69c210 224{
744a9719
ACM
225 if (evsel->handler) {
226 inject_handler f = evsel->handler;
26a031e1
AV
227 return f(tool, event, sample, evsel, machine);
228 }
229
54a3cf59
AV
230 build_id__mark_dso_hit(tool, event, sample, evsel, machine);
231
63c2c9f8 232 return perf_event__repipe_synth(tool, event);
9e69c210
ACM
233}
234
45694aa7 235static int perf_event__repipe_mmap(struct perf_tool *tool,
d20deb64 236 union perf_event *event,
8115d60c 237 struct perf_sample *sample,
743eb868 238 struct machine *machine)
454c407e
TZ
239{
240 int err;
241
45694aa7
ACM
242 err = perf_event__process_mmap(tool, event, sample, machine);
243 perf_event__repipe(tool, event, sample, machine);
454c407e
TZ
244
245 return err;
246}
247
9b07e27f
SE
248#ifdef HAVE_LIBELF_SUPPORT
249static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
250 union perf_event *event,
251 struct perf_sample *sample,
252 struct machine *machine)
253{
254 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
255 u64 n = 0;
256
257 /*
258 * if jit marker, then inject jit mmaps and generate ELF images
259 */
260 if (!jit_process(inject->session, &inject->output, machine,
261 event->mmap.filename, sample->pid, &n)) {
262 inject->bytes_written += n;
263 return 0;
264 }
265 return perf_event__repipe_mmap(tool, event, sample, machine);
266}
267#endif
268
5c5e854b
SE
269static int perf_event__repipe_mmap2(struct perf_tool *tool,
270 union perf_event *event,
271 struct perf_sample *sample,
272 struct machine *machine)
273{
274 int err;
275
276 err = perf_event__process_mmap2(tool, event, sample, machine);
277 perf_event__repipe(tool, event, sample, machine);
278
279 return err;
280}
281
9b07e27f
SE
282#ifdef HAVE_LIBELF_SUPPORT
283static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
284 union perf_event *event,
285 struct perf_sample *sample,
286 struct machine *machine)
287{
288 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
289 u64 n = 0;
290
291 /*
292 * if jit marker, then inject jit mmaps and generate ELF images
293 */
294 if (!jit_process(inject->session, &inject->output, machine,
295 event->mmap2.filename, sample->pid, &n)) {
296 inject->bytes_written += n;
297 return 0;
298 }
299 return perf_event__repipe_mmap2(tool, event, sample, machine);
300}
301#endif
302
f62d3f0f 303static int perf_event__repipe_fork(struct perf_tool *tool,
d20deb64 304 union perf_event *event,
8115d60c 305 struct perf_sample *sample,
743eb868 306 struct machine *machine)
454c407e
TZ
307{
308 int err;
309
f62d3f0f 310 err = perf_event__process_fork(tool, event, sample, machine);
45694aa7 311 perf_event__repipe(tool, event, sample, machine);
454c407e
TZ
312
313 return err;
314}
315
0f0aa5e0
AH
316static int perf_event__repipe_comm(struct perf_tool *tool,
317 union perf_event *event,
318 struct perf_sample *sample,
319 struct machine *machine)
320{
321 int err;
322
323 err = perf_event__process_comm(tool, event, sample, machine);
324 perf_event__repipe(tool, event, sample, machine);
325
326 return err;
327}
328
329static int perf_event__repipe_exit(struct perf_tool *tool,
330 union perf_event *event,
331 struct perf_sample *sample,
332 struct machine *machine)
333{
334 int err;
335
336 err = perf_event__process_exit(tool, event, sample, machine);
337 perf_event__repipe(tool, event, sample, machine);
338
339 return err;
340}
341
47c3d109
AH
342static int perf_event__repipe_tracing_data(struct perf_tool *tool,
343 union perf_event *event,
8115d60c 344 struct perf_session *session)
454c407e
TZ
345{
346 int err;
347
47c3d109
AH
348 perf_event__repipe_synth(tool, event);
349 err = perf_event__process_tracing_data(tool, event, session);
454c407e
TZ
350
351 return err;
352}
353
0f0aa5e0
AH
354static int perf_event__repipe_id_index(struct perf_tool *tool,
355 union perf_event *event,
356 struct perf_session *session)
357{
358 int err;
359
360 perf_event__repipe_synth(tool, event);
361 err = perf_event__process_id_index(tool, event, session);
362
363 return err;
364}
365
c824c433 366static int dso__read_build_id(struct dso *dso)
454c407e 367{
c824c433 368 if (dso->has_build_id)
090f7204 369 return 0;
454c407e 370
c824c433
ACM
371 if (filename__read_build_id(dso->long_name, dso->build_id,
372 sizeof(dso->build_id)) > 0) {
373 dso->has_build_id = true;
090f7204
ACM
374 return 0;
375 }
454c407e 376
090f7204
ACM
377 return -1;
378}
454c407e 379
c824c433 380static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
743eb868 381 struct machine *machine)
090f7204
ACM
382{
383 u16 misc = PERF_RECORD_MISC_USER;
090f7204 384 int err;
454c407e 385
c824c433
ACM
386 if (dso__read_build_id(dso) < 0) {
387 pr_debug("no build_id found for %s\n", dso->long_name);
090f7204
ACM
388 return -1;
389 }
454c407e 390
c824c433 391 if (dso->kernel)
090f7204 392 misc = PERF_RECORD_MISC_KERNEL;
454c407e 393
c824c433 394 err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
743eb868 395 machine);
090f7204 396 if (err) {
c824c433 397 pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
454c407e
TZ
398 return -1;
399 }
400
401 return 0;
402}
403
45694aa7 404static int perf_event__inject_buildid(struct perf_tool *tool,
d20deb64 405 union perf_event *event,
8115d60c 406 struct perf_sample *sample,
1d037ca1 407 struct perf_evsel *evsel __maybe_unused,
743eb868 408 struct machine *machine)
454c407e
TZ
409{
410 struct addr_location al;
411 struct thread *thread;
412 u8 cpumode;
454c407e
TZ
413
414 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
415
13ce34df 416 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
454c407e
TZ
417 if (thread == NULL) {
418 pr_err("problem processing %d event, skipping it.\n",
419 event->header.type);
454c407e
TZ
420 goto repipe;
421 }
422
bb871a9c 423 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al);
454c407e
TZ
424
425 if (al.map != NULL) {
426 if (!al.map->dso->hit) {
427 al.map->dso->hit = 1;
090f7204 428 if (map__load(al.map, NULL) >= 0) {
45694aa7 429 dso__inject_build_id(al.map->dso, tool, machine);
090f7204
ACM
430 /*
431 * If this fails, too bad, let the other side
432 * account this as unresolved.
433 */
393be2e3 434 } else {
89fe808a 435#ifdef HAVE_LIBELF_SUPPORT
454c407e
TZ
436 pr_warning("no symbols found in %s, maybe "
437 "install a debug package?\n",
438 al.map->dso->long_name);
393be2e3
NK
439#endif
440 }
454c407e
TZ
441 }
442 }
443
b91fc39f 444 thread__put(thread);
454c407e 445repipe:
45694aa7 446 perf_event__repipe(tool, event, sample, machine);
090f7204 447 return 0;
454c407e
TZ
448}
449
26a031e1
AV
450static int perf_inject__sched_process_exit(struct perf_tool *tool,
451 union perf_event *event __maybe_unused,
452 struct perf_sample *sample,
453 struct perf_evsel *evsel __maybe_unused,
454 struct machine *machine __maybe_unused)
455{
456 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
457 struct event_entry *ent;
458
459 list_for_each_entry(ent, &inject->samples, node) {
460 if (sample->tid == ent->tid) {
461 list_del_init(&ent->node);
462 free(ent);
463 break;
464 }
465 }
466
467 return 0;
468}
469
470static int perf_inject__sched_switch(struct perf_tool *tool,
471 union perf_event *event,
472 struct perf_sample *sample,
473 struct perf_evsel *evsel,
474 struct machine *machine)
475{
476 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
477 struct event_entry *ent;
478
479 perf_inject__sched_process_exit(tool, event, sample, evsel, machine);
480
481 ent = malloc(event->header.size + sizeof(struct event_entry));
482 if (ent == NULL) {
483 color_fprintf(stderr, PERF_COLOR_RED,
484 "Not enough memory to process sched switch event!");
485 return -1;
486 }
487
488 ent->tid = sample->tid;
489 memcpy(&ent->event, event, event->header.size);
490 list_add(&ent->node, &inject->samples);
491 return 0;
492}
493
494static int perf_inject__sched_stat(struct perf_tool *tool,
495 union perf_event *event __maybe_unused,
496 struct perf_sample *sample,
497 struct perf_evsel *evsel,
498 struct machine *machine)
499{
500 struct event_entry *ent;
501 union perf_event *event_sw;
502 struct perf_sample sample_sw;
503 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
504 u32 pid = perf_evsel__intval(evsel, sample, "pid");
505
506 list_for_each_entry(ent, &inject->samples, node) {
507 if (pid == ent->tid)
508 goto found;
509 }
510
511 return 0;
512found:
513 event_sw = &ent->event[0];
514 perf_evsel__parse_sample(evsel, event_sw, &sample_sw);
515
516 sample_sw.period = sample->period;
517 sample_sw.time = sample->time;
518 perf_event__synthesize_sample(event_sw, evsel->attr.sample_type,
d03f2170
AH
519 evsel->attr.read_format, &sample_sw,
520 false);
54a3cf59 521 build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);
26a031e1
AV
522 return perf_event__repipe(tool, event_sw, &sample_sw, machine);
523}
524
1d037ca1 525static void sig_handler(int sig __maybe_unused)
454c407e
TZ
526{
527 session_done = 1;
528}
529
26a031e1
AV
530static int perf_evsel__check_stype(struct perf_evsel *evsel,
531 u64 sample_type, const char *sample_msg)
532{
533 struct perf_event_attr *attr = &evsel->attr;
534 const char *name = perf_evsel__name(evsel);
535
536 if (!(attr->sample_type & sample_type)) {
537 pr_err("Samples for %s event do not have %s attribute set.",
538 name, sample_msg);
539 return -EINVAL;
540 }
541
542 return 0;
543}
544
f56fb986
AH
545static int drop_sample(struct perf_tool *tool __maybe_unused,
546 union perf_event *event __maybe_unused,
547 struct perf_sample *sample __maybe_unused,
548 struct perf_evsel *evsel __maybe_unused,
549 struct machine *machine __maybe_unused)
550{
551 return 0;
552}
553
554static void strip_init(struct perf_inject *inject)
555{
556 struct perf_evlist *evlist = inject->session->evlist;
557 struct perf_evsel *evsel;
558
559 inject->tool.context_switch = perf_event__drop;
560
561 evlist__for_each(evlist, evsel)
562 evsel->handler = drop_sample;
563}
564
565static bool has_tracking(struct perf_evsel *evsel)
566{
567 return evsel->attr.mmap || evsel->attr.mmap2 || evsel->attr.comm ||
568 evsel->attr.task;
569}
570
571#define COMPAT_MASK (PERF_SAMPLE_ID | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \
572 PERF_SAMPLE_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER)
573
574/*
575 * In order that the perf.data file is parsable, tracking events like MMAP need
576 * their selected event to exist, except if there is only 1 selected event left
577 * and it has a compatible sample type.
578 */
579static bool ok_to_remove(struct perf_evlist *evlist,
580 struct perf_evsel *evsel_to_remove)
581{
582 struct perf_evsel *evsel;
583 int cnt = 0;
584 bool ok = false;
585
586 if (!has_tracking(evsel_to_remove))
587 return true;
588
589 evlist__for_each(evlist, evsel) {
590 if (evsel->handler != drop_sample) {
591 cnt += 1;
592 if ((evsel->attr.sample_type & COMPAT_MASK) ==
593 (evsel_to_remove->attr.sample_type & COMPAT_MASK))
594 ok = true;
595 }
596 }
597
598 return ok && cnt == 1;
599}
600
601static void strip_fini(struct perf_inject *inject)
602{
603 struct perf_evlist *evlist = inject->session->evlist;
604 struct perf_evsel *evsel, *tmp;
605
606 /* Remove non-synthesized evsels if possible */
607 evlist__for_each_safe(evlist, tmp, evsel) {
608 if (evsel->handler == drop_sample &&
609 ok_to_remove(evlist, evsel)) {
610 pr_debug("Deleting %s\n", perf_evsel__name(evsel));
611 perf_evlist__remove(evlist, evsel);
612 perf_evsel__delete(evsel);
613 }
614 }
615}
616
5ded57ac 617static int __cmd_inject(struct perf_inject *inject)
454c407e 618{
454c407e 619 int ret = -EINVAL;
1cb8bdcc 620 struct perf_session *session = inject->session;
3406912c 621 struct perf_data_file *file_out = &inject->output;
42aa276f 622 int fd = perf_data_file__fd(file_out);
0f0aa5e0 623 u64 output_data_offset;
454c407e
TZ
624
625 signal(SIGINT, sig_handler);
626
0f0aa5e0
AH
627 if (inject->build_ids || inject->sched_stat ||
628 inject->itrace_synth_opts.set) {
5ded57ac 629 inject->tool.mmap = perf_event__repipe_mmap;
5c5e854b 630 inject->tool.mmap2 = perf_event__repipe_mmap2;
f62d3f0f 631 inject->tool.fork = perf_event__repipe_fork;
5ded57ac 632 inject->tool.tracing_data = perf_event__repipe_tracing_data;
454c407e
TZ
633 }
634
0f0aa5e0
AH
635 output_data_offset = session->header.data_offset;
636
54a3cf59
AV
637 if (inject->build_ids) {
638 inject->tool.sample = perf_event__inject_buildid;
639 } else if (inject->sched_stat) {
26a031e1
AV
640 struct perf_evsel *evsel;
641
0050f7aa 642 evlist__for_each(session->evlist, evsel) {
26a031e1
AV
643 const char *name = perf_evsel__name(evsel);
644
645 if (!strcmp(name, "sched:sched_switch")) {
646 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))
647 return -EINVAL;
648
744a9719 649 evsel->handler = perf_inject__sched_switch;
26a031e1 650 } else if (!strcmp(name, "sched:sched_process_exit"))
744a9719 651 evsel->handler = perf_inject__sched_process_exit;
26a031e1 652 else if (!strncmp(name, "sched:sched_stat_", 17))
744a9719 653 evsel->handler = perf_inject__sched_stat;
26a031e1 654 }
0f0aa5e0
AH
655 } else if (inject->itrace_synth_opts.set) {
656 session->itrace_synth_opts = &inject->itrace_synth_opts;
657 inject->itrace_synth_opts.inject = true;
658 inject->tool.comm = perf_event__repipe_comm;
659 inject->tool.exit = perf_event__repipe_exit;
660 inject->tool.id_index = perf_event__repipe_id_index;
661 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
662 inject->tool.auxtrace = perf_event__process_auxtrace;
73117308
AH
663 inject->tool.aux = perf_event__drop_aux;
664 inject->tool.itrace_start = perf_event__drop_aux,
0f0aa5e0
AH
665 inject->tool.ordered_events = true;
666 inject->tool.ordering_requires_timestamps = true;
667 /* Allow space in the header for new attributes */
668 output_data_offset = 4096;
f56fb986
AH
669 if (inject->strip)
670 strip_init(inject);
26a031e1
AV
671 }
672
99fa2984
AH
673 if (!inject->itrace_synth_opts.set)
674 auxtrace_index__free(&session->auxtrace_index);
675
3406912c 676 if (!file_out->is_pipe)
0f0aa5e0 677 lseek(fd, output_data_offset, SEEK_SET);
e558a5bd 678
b7b61cbe 679 ret = perf_session__process_events(session);
454c407e 680
3406912c 681 if (!file_out->is_pipe) {
640dad47 682 if (inject->build_ids)
e38b43c3
AH
683 perf_header__set_feat(&session->header,
684 HEADER_BUILD_ID);
640dad47
AH
685 /*
686 * Keep all buildids when there is unprocessed AUX data because
687 * it is not known which ones the AUX trace hits.
688 */
689 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) &&
690 inject->have_auxtrace && !inject->itrace_synth_opts.set)
691 dsos__hit_all(session);
0f0aa5e0
AH
692 /*
693 * The AUX areas have been removed and replaced with
73117308
AH
694 * synthesized hardware events, so clear the feature flag and
695 * remove the evsel.
0f0aa5e0 696 */
051a01b9 697 if (inject->itrace_synth_opts.set) {
73117308
AH
698 struct perf_evsel *evsel;
699
0f0aa5e0
AH
700 perf_header__clear_feat(&session->header,
701 HEADER_AUXTRACE);
051a01b9
AH
702 if (inject->itrace_synth_opts.last_branch)
703 perf_header__set_feat(&session->header,
704 HEADER_BRANCH_STACK);
73117308
AH
705 evsel = perf_evlist__id2evsel_strict(session->evlist,
706 inject->aux_id);
707 if (evsel) {
708 pr_debug("Deleting %s\n",
709 perf_evsel__name(evsel));
710 perf_evlist__remove(session->evlist, evsel);
711 perf_evsel__delete(evsel);
712 }
f56fb986
AH
713 if (inject->strip)
714 strip_fini(inject);
051a01b9 715 }
0f0aa5e0 716 session->header.data_offset = output_data_offset;
e558a5bd 717 session->header.data_size = inject->bytes_written;
42aa276f 718 perf_session__write_header(session, session->evlist, fd, true);
e558a5bd
AV
719 }
720
454c407e
TZ
721 return ret;
722}
723
9b07e27f
SE
724#ifdef HAVE_LIBELF_SUPPORT
725static int
726jit_validate_events(struct perf_session *session)
727{
728 struct perf_evsel *evsel;
729
730 /*
731 * check that all events use CLOCK_MONOTONIC
732 */
733 evlist__for_each(session->evlist, evsel) {
734 if (evsel->attr.use_clockid == 0 || evsel->attr.clockid != CLOCK_MONOTONIC)
735 return -1;
736 }
737 return 0;
738}
739#endif
740
1d037ca1 741int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
454c407e 742{
5ded57ac
ACM
743 struct perf_inject inject = {
744 .tool = {
745 .sample = perf_event__repipe_sample,
746 .mmap = perf_event__repipe,
5c5e854b 747 .mmap2 = perf_event__repipe,
5ded57ac
ACM
748 .comm = perf_event__repipe,
749 .fork = perf_event__repipe,
750 .exit = perf_event__repipe,
751 .lost = perf_event__repipe,
d8145b3e 752 .lost_samples = perf_event__repipe,
4a96f7a0 753 .aux = perf_event__repipe,
0ad21f68 754 .itrace_start = perf_event__repipe,
0286039f 755 .context_switch = perf_event__repipe,
5ded57ac
ACM
756 .read = perf_event__repipe_sample,
757 .throttle = perf_event__repipe,
758 .unthrottle = perf_event__repipe,
759 .attr = perf_event__repipe_attr,
47c3d109 760 .tracing_data = perf_event__repipe_op2_synth,
cd17a9b5
AH
761 .auxtrace_info = perf_event__repipe_op2_synth,
762 .auxtrace = perf_event__repipe_auxtrace,
763 .auxtrace_error = perf_event__repipe_op2_synth,
d704ebda 764 .finished_round = perf_event__repipe_oe_synth,
5ded57ac 765 .build_id = perf_event__repipe_op2_synth,
3c659eed 766 .id_index = perf_event__repipe_op2_synth,
5ded57ac 767 },
e558a5bd 768 .input_name = "-",
26a031e1 769 .samples = LIST_HEAD_INIT(inject.samples),
3406912c
JO
770 .output = {
771 .path = "-",
772 .mode = PERF_DATA_MODE_WRITE,
773 },
5ded57ac 774 };
1cb8bdcc
NK
775 struct perf_data_file file = {
776 .mode = PERF_DATA_MODE_READ,
777 };
778 int ret;
779
9b07e27f 780 struct option options[] = {
5ded57ac
ACM
781 OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
782 "Inject build-ids into the output stream"),
e558a5bd
AV
783 OPT_STRING('i', "input", &inject.input_name, "file",
784 "input file name"),
3406912c 785 OPT_STRING('o', "output", &inject.output.path, "file",
e558a5bd 786 "output file name"),
26a031e1
AV
787 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
788 "Merge sched-stat and sched-switch for getting events "
789 "where and how long tasks slept"),
9b07e27f 790 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
5ded57ac
ACM
791 OPT_INCR('v', "verbose", &verbose,
792 "be more verbose (show build ids, etc)"),
a7a2b8b4
AH
793 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
794 "kallsyms pathname"),
ccaa474c 795 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
0f0aa5e0
AH
796 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
797 NULL, "opts", "Instruction Tracing options",
798 itrace_parse_synth_opts),
f56fb986
AH
799 OPT_BOOLEAN(0, "strip", &inject.strip,
800 "strip non-synthesized events (use with --itrace)"),
5ded57ac
ACM
801 OPT_END()
802 };
002439e8
ACM
803 const char * const inject_usage[] = {
804 "perf inject [<options>]",
805 NULL
806 };
9b07e27f
SE
807#ifndef HAVE_LIBELF_SUPPORT
808 set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
809#endif
002439e8 810 argc = parse_options(argc, argv, options, inject_usage, 0);
454c407e
TZ
811
812 /*
813 * Any (unrecognized) arguments left?
814 */
815 if (argc)
002439e8 816 usage_with_options(inject_usage, options);
454c407e 817
f56fb986
AH
818 if (inject.strip && !inject.itrace_synth_opts.set) {
819 pr_err("--strip option requires --itrace option\n");
820 return -1;
821 }
822
3406912c
JO
823 if (perf_data_file__open(&inject.output)) {
824 perror("failed to create output file");
825 return -1;
e558a5bd
AV
826 }
827
b7b61cbe
ACM
828 inject.tool.ordered_events = inject.sched_stat;
829
1cb8bdcc
NK
830 file.path = inject.input_name;
831 inject.session = perf_session__new(&file, true, &inject.tool);
832 if (inject.session == NULL)
52e02834 833 return -1;
1cb8bdcc 834
921f3fad
ACM
835 if (inject.build_ids) {
836 /*
837 * to make sure the mmap records are ordered correctly
838 * and so that the correct especially due to jitted code
839 * mmaps. We cannot generate the buildid hit list and
840 * inject the jit mmaps at the same time for now.
841 */
842 inject.tool.ordered_events = true;
843 inject.tool.ordering_requires_timestamps = true;
844 }
9b07e27f
SE
845#ifdef HAVE_LIBELF_SUPPORT
846 if (inject.jit_mode) {
847 /*
848 * validate event is using the correct clockid
849 */
850 if (jit_validate_events(inject.session)) {
851 fprintf(stderr, "error, jitted code must be sampled with perf record -k 1\n");
852 return -1;
853 }
854 inject.tool.mmap2 = perf_event__jit_repipe_mmap2;
855 inject.tool.mmap = perf_event__jit_repipe_mmap;
856 inject.tool.ordered_events = true;
857 inject.tool.ordering_requires_timestamps = true;
858 /*
859 * JIT MMAP injection injects all MMAP events in one go, so it
860 * does not obey finished_round semantics.
861 */
862 inject.tool.finished_round = perf_event__drop_oe;
863 }
864#endif
9fedfb0c
TS
865 ret = symbol__init(&inject.session->header.env);
866 if (ret < 0)
867 goto out_delete;
454c407e 868
1cb8bdcc
NK
869 ret = __cmd_inject(&inject);
870
9fedfb0c 871out_delete:
1cb8bdcc 872 perf_session__delete(inject.session);
1cb8bdcc 873 return ret;
454c407e 874}
This page took 0.259749 seconds and 5 git commands to generate.