perf script: Add period data column
[deliverable/linux.git] / tools / perf / builtin-script.c
1 #include "builtin.h"
2
3 #include "perf.h"
4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include "util/exec_cmd.h"
7 #include "util/header.h"
8 #include "util/parse-options.h"
9 #include "util/session.h"
10 #include "util/tool.h"
11 #include "util/symbol.h"
12 #include "util/thread.h"
13 #include "util/trace-event.h"
14 #include "util/util.h"
15 #include "util/evlist.h"
16 #include "util/evsel.h"
17 #include "util/sort.h"
18 #include "util/data.h"
19 #include <linux/bitmap.h>
20
21 static char const *script_name;
22 static char const *generate_script_lang;
23 static bool debug_mode;
24 static u64 last_timestamp;
25 static u64 nr_unordered;
26 extern const struct option record_options[];
27 static bool no_callchain;
28 static bool latency_format;
29 static bool system_wide;
30 static const char *cpu_list;
31 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
32
33 enum perf_output_field {
34 PERF_OUTPUT_COMM = 1U << 0,
35 PERF_OUTPUT_TID = 1U << 1,
36 PERF_OUTPUT_PID = 1U << 2,
37 PERF_OUTPUT_TIME = 1U << 3,
38 PERF_OUTPUT_CPU = 1U << 4,
39 PERF_OUTPUT_EVNAME = 1U << 5,
40 PERF_OUTPUT_TRACE = 1U << 6,
41 PERF_OUTPUT_IP = 1U << 7,
42 PERF_OUTPUT_SYM = 1U << 8,
43 PERF_OUTPUT_DSO = 1U << 9,
44 PERF_OUTPUT_ADDR = 1U << 10,
45 PERF_OUTPUT_SYMOFFSET = 1U << 11,
46 PERF_OUTPUT_SRCLINE = 1U << 12,
47 PERF_OUTPUT_PERIOD = 1U << 13,
48 };
49
50 struct output_option {
51 const char *str;
52 enum perf_output_field field;
53 } all_output_options[] = {
54 {.str = "comm", .field = PERF_OUTPUT_COMM},
55 {.str = "tid", .field = PERF_OUTPUT_TID},
56 {.str = "pid", .field = PERF_OUTPUT_PID},
57 {.str = "time", .field = PERF_OUTPUT_TIME},
58 {.str = "cpu", .field = PERF_OUTPUT_CPU},
59 {.str = "event", .field = PERF_OUTPUT_EVNAME},
60 {.str = "trace", .field = PERF_OUTPUT_TRACE},
61 {.str = "ip", .field = PERF_OUTPUT_IP},
62 {.str = "sym", .field = PERF_OUTPUT_SYM},
63 {.str = "dso", .field = PERF_OUTPUT_DSO},
64 {.str = "addr", .field = PERF_OUTPUT_ADDR},
65 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
66 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
67 {.str = "period", .field = PERF_OUTPUT_PERIOD},
68 };
69
70 /* default set to maintain compatibility with current format */
71 static struct {
72 bool user_set;
73 bool wildcard_set;
74 unsigned int print_ip_opts;
75 u64 fields;
76 u64 invalid_fields;
77 } output[PERF_TYPE_MAX] = {
78
79 [PERF_TYPE_HARDWARE] = {
80 .user_set = false,
81
82 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
83 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
84 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
85 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
86
87 .invalid_fields = PERF_OUTPUT_TRACE,
88 },
89
90 [PERF_TYPE_SOFTWARE] = {
91 .user_set = false,
92
93 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
94 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
95 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
96 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
97
98 .invalid_fields = PERF_OUTPUT_TRACE,
99 },
100
101 [PERF_TYPE_TRACEPOINT] = {
102 .user_set = false,
103
104 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
105 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
106 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
107 },
108
109 [PERF_TYPE_RAW] = {
110 .user_set = false,
111
112 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
113 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
114 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
115 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
116
117 .invalid_fields = PERF_OUTPUT_TRACE,
118 },
119 };
120
121 static bool output_set_by_user(void)
122 {
123 int j;
124 for (j = 0; j < PERF_TYPE_MAX; ++j) {
125 if (output[j].user_set)
126 return true;
127 }
128 return false;
129 }
130
131 static const char *output_field2str(enum perf_output_field field)
132 {
133 int i, imax = ARRAY_SIZE(all_output_options);
134 const char *str = "";
135
136 for (i = 0; i < imax; ++i) {
137 if (all_output_options[i].field == field) {
138 str = all_output_options[i].str;
139 break;
140 }
141 }
142 return str;
143 }
144
145 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
146
147 static int perf_evsel__check_stype(struct perf_evsel *evsel,
148 u64 sample_type, const char *sample_msg,
149 enum perf_output_field field)
150 {
151 struct perf_event_attr *attr = &evsel->attr;
152 int type = attr->type;
153 const char *evname;
154
155 if (attr->sample_type & sample_type)
156 return 0;
157
158 if (output[type].user_set) {
159 evname = perf_evsel__name(evsel);
160 pr_err("Samples for '%s' event do not have %s attribute set. "
161 "Cannot print '%s' field.\n",
162 evname, sample_msg, output_field2str(field));
163 return -1;
164 }
165
166 /* user did not ask for it explicitly so remove from the default list */
167 output[type].fields &= ~field;
168 evname = perf_evsel__name(evsel);
169 pr_debug("Samples for '%s' event do not have %s attribute set. "
170 "Skipping '%s' field.\n",
171 evname, sample_msg, output_field2str(field));
172
173 return 0;
174 }
175
176 static int perf_evsel__check_attr(struct perf_evsel *evsel,
177 struct perf_session *session)
178 {
179 struct perf_event_attr *attr = &evsel->attr;
180
181 if (PRINT_FIELD(TRACE) &&
182 !perf_session__has_traces(session, "record -R"))
183 return -EINVAL;
184
185 if (PRINT_FIELD(IP)) {
186 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
187 PERF_OUTPUT_IP))
188 return -EINVAL;
189 }
190
191 if (PRINT_FIELD(ADDR) &&
192 perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
193 PERF_OUTPUT_ADDR))
194 return -EINVAL;
195
196 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
197 pr_err("Display of symbols requested but neither sample IP nor "
198 "sample address\nis selected. Hence, no addresses to convert "
199 "to symbols.\n");
200 return -EINVAL;
201 }
202 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
203 pr_err("Display of offsets requested but symbol is not"
204 "selected.\n");
205 return -EINVAL;
206 }
207 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
208 pr_err("Display of DSO requested but neither sample IP nor "
209 "sample address\nis selected. Hence, no addresses to convert "
210 "to DSO.\n");
211 return -EINVAL;
212 }
213 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
214 pr_err("Display of source line number requested but sample IP is not\n"
215 "selected. Hence, no address to lookup the source line number.\n");
216 return -EINVAL;
217 }
218
219 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
220 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
221 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
222 return -EINVAL;
223
224 if (PRINT_FIELD(TIME) &&
225 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
226 PERF_OUTPUT_TIME))
227 return -EINVAL;
228
229 if (PRINT_FIELD(CPU) &&
230 perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
231 PERF_OUTPUT_CPU))
232 return -EINVAL;
233
234 if (PRINT_FIELD(PERIOD) &&
235 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
236 PERF_OUTPUT_PERIOD))
237 return -EINVAL;
238
239 return 0;
240 }
241
242 static void set_print_ip_opts(struct perf_event_attr *attr)
243 {
244 unsigned int type = attr->type;
245
246 output[type].print_ip_opts = 0;
247 if (PRINT_FIELD(IP))
248 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
249
250 if (PRINT_FIELD(SYM))
251 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
252
253 if (PRINT_FIELD(DSO))
254 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
255
256 if (PRINT_FIELD(SYMOFFSET))
257 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
258
259 if (PRINT_FIELD(SRCLINE))
260 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
261 }
262
263 /*
264 * verify all user requested events exist and the samples
265 * have the expected data
266 */
267 static int perf_session__check_output_opt(struct perf_session *session)
268 {
269 int j;
270 struct perf_evsel *evsel;
271
272 for (j = 0; j < PERF_TYPE_MAX; ++j) {
273 evsel = perf_session__find_first_evtype(session, j);
274
275 /*
276 * even if fields is set to 0 (ie., show nothing) event must
277 * exist if user explicitly includes it on the command line
278 */
279 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
280 pr_err("%s events do not exist. "
281 "Remove corresponding -f option to proceed.\n",
282 event_type(j));
283 return -1;
284 }
285
286 if (evsel && output[j].fields &&
287 perf_evsel__check_attr(evsel, session))
288 return -1;
289
290 if (evsel == NULL)
291 continue;
292
293 set_print_ip_opts(&evsel->attr);
294 }
295
296 if (!no_callchain) {
297 bool use_callchain = false;
298
299 evlist__for_each(session->evlist, evsel) {
300 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
301 use_callchain = true;
302 break;
303 }
304 }
305 if (!use_callchain)
306 symbol_conf.use_callchain = false;
307 }
308
309 /*
310 * set default for tracepoints to print symbols only
311 * if callchains are present
312 */
313 if (symbol_conf.use_callchain &&
314 !output[PERF_TYPE_TRACEPOINT].user_set) {
315 struct perf_event_attr *attr;
316
317 j = PERF_TYPE_TRACEPOINT;
318 evsel = perf_session__find_first_evtype(session, j);
319 if (evsel == NULL)
320 goto out;
321
322 attr = &evsel->attr;
323
324 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
325 output[j].fields |= PERF_OUTPUT_IP;
326 output[j].fields |= PERF_OUTPUT_SYM;
327 output[j].fields |= PERF_OUTPUT_DSO;
328 set_print_ip_opts(attr);
329 }
330 }
331
332 out:
333 return 0;
334 }
335
336 static void print_sample_start(struct perf_sample *sample,
337 struct thread *thread,
338 struct perf_evsel *evsel)
339 {
340 struct perf_event_attr *attr = &evsel->attr;
341 unsigned long secs;
342 unsigned long usecs;
343 unsigned long long nsecs;
344
345 if (PRINT_FIELD(COMM)) {
346 if (latency_format)
347 printf("%8.8s ", thread__comm_str(thread));
348 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
349 printf("%s ", thread__comm_str(thread));
350 else
351 printf("%16s ", thread__comm_str(thread));
352 }
353
354 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
355 printf("%5d/%-5d ", sample->pid, sample->tid);
356 else if (PRINT_FIELD(PID))
357 printf("%5d ", sample->pid);
358 else if (PRINT_FIELD(TID))
359 printf("%5d ", sample->tid);
360
361 if (PRINT_FIELD(CPU)) {
362 if (latency_format)
363 printf("%3d ", sample->cpu);
364 else
365 printf("[%03d] ", sample->cpu);
366 }
367
368 if (PRINT_FIELD(TIME)) {
369 nsecs = sample->time;
370 secs = nsecs / NSECS_PER_SEC;
371 nsecs -= secs * NSECS_PER_SEC;
372 usecs = nsecs / NSECS_PER_USEC;
373 printf("%5lu.%06lu: ", secs, usecs);
374 }
375 }
376
377 static void print_sample_addr(union perf_event *event,
378 struct perf_sample *sample,
379 struct machine *machine,
380 struct thread *thread,
381 struct perf_event_attr *attr)
382 {
383 struct addr_location al;
384
385 printf("%16" PRIx64, sample->addr);
386
387 if (!sample_addr_correlates_sym(attr))
388 return;
389
390 perf_event__preprocess_sample_addr(event, sample, machine, thread, &al);
391
392 if (PRINT_FIELD(SYM)) {
393 printf(" ");
394 if (PRINT_FIELD(SYMOFFSET))
395 symbol__fprintf_symname_offs(al.sym, &al, stdout);
396 else
397 symbol__fprintf_symname(al.sym, stdout);
398 }
399
400 if (PRINT_FIELD(DSO)) {
401 printf(" (");
402 map__fprintf_dsoname(al.map, stdout);
403 printf(")");
404 }
405 }
406
407 static void print_sample_bts(union perf_event *event,
408 struct perf_sample *sample,
409 struct perf_evsel *evsel,
410 struct thread *thread,
411 struct addr_location *al)
412 {
413 struct perf_event_attr *attr = &evsel->attr;
414 bool print_srcline_last = false;
415
416 /* print branch_from information */
417 if (PRINT_FIELD(IP)) {
418 unsigned int print_opts = output[attr->type].print_ip_opts;
419
420 if (symbol_conf.use_callchain && sample->callchain) {
421 printf("\n");
422 } else {
423 printf(" ");
424 if (print_opts & PRINT_IP_OPT_SRCLINE) {
425 print_srcline_last = true;
426 print_opts &= ~PRINT_IP_OPT_SRCLINE;
427 }
428 }
429 perf_evsel__print_ip(evsel, sample, al, print_opts,
430 PERF_MAX_STACK_DEPTH);
431 }
432
433 /* print branch_to information */
434 if (PRINT_FIELD(ADDR) ||
435 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
436 !output[attr->type].user_set)) {
437 printf(" => ");
438 print_sample_addr(event, sample, al->machine, thread, attr);
439 }
440
441 if (print_srcline_last)
442 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
443
444 printf("\n");
445 }
446
447 static void process_event(union perf_event *event, struct perf_sample *sample,
448 struct perf_evsel *evsel, struct thread *thread,
449 struct addr_location *al)
450 {
451 struct perf_event_attr *attr = &evsel->attr;
452
453 if (output[attr->type].fields == 0)
454 return;
455
456 print_sample_start(sample, thread, evsel);
457
458 if (PRINT_FIELD(PERIOD))
459 printf("%10" PRIu64 " ", sample->period);
460
461 if (PRINT_FIELD(EVNAME)) {
462 const char *evname = perf_evsel__name(evsel);
463 printf("%s: ", evname ? evname : "[unknown]");
464 }
465
466 if (is_bts_event(attr)) {
467 print_sample_bts(event, sample, evsel, thread, al);
468 return;
469 }
470
471 if (PRINT_FIELD(TRACE))
472 event_format__print(evsel->tp_format, sample->cpu,
473 sample->raw_data, sample->raw_size);
474 if (PRINT_FIELD(ADDR))
475 print_sample_addr(event, sample, al->machine, thread, attr);
476
477 if (PRINT_FIELD(IP)) {
478 if (!symbol_conf.use_callchain)
479 printf(" ");
480 else
481 printf("\n");
482
483 perf_evsel__print_ip(evsel, sample, al,
484 output[attr->type].print_ip_opts,
485 PERF_MAX_STACK_DEPTH);
486 }
487
488 printf("\n");
489 }
490
491 static int default_start_script(const char *script __maybe_unused,
492 int argc __maybe_unused,
493 const char **argv __maybe_unused)
494 {
495 return 0;
496 }
497
498 static int default_flush_script(void)
499 {
500 return 0;
501 }
502
503 static int default_stop_script(void)
504 {
505 return 0;
506 }
507
508 static int default_generate_script(struct pevent *pevent __maybe_unused,
509 const char *outfile __maybe_unused)
510 {
511 return 0;
512 }
513
514 static struct scripting_ops default_scripting_ops = {
515 .start_script = default_start_script,
516 .flush_script = default_flush_script,
517 .stop_script = default_stop_script,
518 .process_event = process_event,
519 .generate_script = default_generate_script,
520 };
521
522 static struct scripting_ops *scripting_ops;
523
524 static void setup_scripting(void)
525 {
526 setup_perl_scripting();
527 setup_python_scripting();
528
529 scripting_ops = &default_scripting_ops;
530 }
531
532 static int flush_scripting(void)
533 {
534 return scripting_ops->flush_script();
535 }
536
537 static int cleanup_scripting(void)
538 {
539 pr_debug("\nperf script stopped\n");
540
541 return scripting_ops->stop_script();
542 }
543
544 static int process_sample_event(struct perf_tool *tool __maybe_unused,
545 union perf_event *event,
546 struct perf_sample *sample,
547 struct perf_evsel *evsel,
548 struct machine *machine)
549 {
550 struct addr_location al;
551 struct thread *thread = machine__findnew_thread(machine, sample->pid,
552 sample->tid);
553
554 if (thread == NULL) {
555 pr_debug("problem processing %d event, skipping it.\n",
556 event->header.type);
557 return -1;
558 }
559
560 if (debug_mode) {
561 if (sample->time < last_timestamp) {
562 pr_err("Samples misordered, previous: %" PRIu64
563 " this: %" PRIu64 "\n", last_timestamp,
564 sample->time);
565 nr_unordered++;
566 }
567 last_timestamp = sample->time;
568 return 0;
569 }
570
571 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
572 pr_err("problem processing %d event, skipping it.\n",
573 event->header.type);
574 return -1;
575 }
576
577 if (al.filtered)
578 return 0;
579
580 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
581 return 0;
582
583 scripting_ops->process_event(event, sample, evsel, thread, &al);
584
585 return 0;
586 }
587
588 struct perf_script {
589 struct perf_tool tool;
590 struct perf_session *session;
591 bool show_task_events;
592 bool show_mmap_events;
593 };
594
595 static int process_attr(struct perf_tool *tool, union perf_event *event,
596 struct perf_evlist **pevlist)
597 {
598 struct perf_script *scr = container_of(tool, struct perf_script, tool);
599 struct perf_evlist *evlist;
600 struct perf_evsel *evsel, *pos;
601 int err;
602
603 err = perf_event__process_attr(tool, event, pevlist);
604 if (err)
605 return err;
606
607 evlist = *pevlist;
608 evsel = perf_evlist__last(*pevlist);
609
610 if (evsel->attr.type >= PERF_TYPE_MAX)
611 return 0;
612
613 evlist__for_each(evlist, pos) {
614 if (pos->attr.type == evsel->attr.type && pos != evsel)
615 return 0;
616 }
617
618 set_print_ip_opts(&evsel->attr);
619
620 return perf_evsel__check_attr(evsel, scr->session);
621 }
622
623 static int process_comm_event(struct perf_tool *tool,
624 union perf_event *event,
625 struct perf_sample *sample,
626 struct machine *machine)
627 {
628 struct thread *thread;
629 struct perf_script *script = container_of(tool, struct perf_script, tool);
630 struct perf_session *session = script->session;
631 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
632 int ret = -1;
633
634 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
635 if (thread == NULL) {
636 pr_debug("problem processing COMM event, skipping it.\n");
637 return -1;
638 }
639
640 if (perf_event__process_comm(tool, event, sample, machine) < 0)
641 goto out;
642
643 if (!evsel->attr.sample_id_all) {
644 sample->cpu = 0;
645 sample->time = 0;
646 sample->tid = event->comm.tid;
647 sample->pid = event->comm.pid;
648 }
649 print_sample_start(sample, thread, evsel);
650 perf_event__fprintf(event, stdout);
651 ret = 0;
652
653 out:
654 return ret;
655 }
656
657 static int process_fork_event(struct perf_tool *tool,
658 union perf_event *event,
659 struct perf_sample *sample,
660 struct machine *machine)
661 {
662 struct thread *thread;
663 struct perf_script *script = container_of(tool, struct perf_script, tool);
664 struct perf_session *session = script->session;
665 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
666
667 if (perf_event__process_fork(tool, event, sample, machine) < 0)
668 return -1;
669
670 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
671 if (thread == NULL) {
672 pr_debug("problem processing FORK event, skipping it.\n");
673 return -1;
674 }
675
676 if (!evsel->attr.sample_id_all) {
677 sample->cpu = 0;
678 sample->time = event->fork.time;
679 sample->tid = event->fork.tid;
680 sample->pid = event->fork.pid;
681 }
682 print_sample_start(sample, thread, evsel);
683 perf_event__fprintf(event, stdout);
684
685 return 0;
686 }
687 static int process_exit_event(struct perf_tool *tool,
688 union perf_event *event,
689 struct perf_sample *sample,
690 struct machine *machine)
691 {
692 struct thread *thread;
693 struct perf_script *script = container_of(tool, struct perf_script, tool);
694 struct perf_session *session = script->session;
695 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
696
697 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
698 if (thread == NULL) {
699 pr_debug("problem processing EXIT event, skipping it.\n");
700 return -1;
701 }
702
703 if (!evsel->attr.sample_id_all) {
704 sample->cpu = 0;
705 sample->time = 0;
706 sample->tid = event->comm.tid;
707 sample->pid = event->comm.pid;
708 }
709 print_sample_start(sample, thread, evsel);
710 perf_event__fprintf(event, stdout);
711
712 if (perf_event__process_exit(tool, event, sample, machine) < 0)
713 return -1;
714
715 return 0;
716 }
717
718 static int process_mmap_event(struct perf_tool *tool,
719 union perf_event *event,
720 struct perf_sample *sample,
721 struct machine *machine)
722 {
723 struct thread *thread;
724 struct perf_script *script = container_of(tool, struct perf_script, tool);
725 struct perf_session *session = script->session;
726 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
727
728 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
729 return -1;
730
731 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
732 if (thread == NULL) {
733 pr_debug("problem processing MMAP event, skipping it.\n");
734 return -1;
735 }
736
737 if (!evsel->attr.sample_id_all) {
738 sample->cpu = 0;
739 sample->time = 0;
740 sample->tid = event->mmap.tid;
741 sample->pid = event->mmap.pid;
742 }
743 print_sample_start(sample, thread, evsel);
744 perf_event__fprintf(event, stdout);
745
746 return 0;
747 }
748
749 static int process_mmap2_event(struct perf_tool *tool,
750 union perf_event *event,
751 struct perf_sample *sample,
752 struct machine *machine)
753 {
754 struct thread *thread;
755 struct perf_script *script = container_of(tool, struct perf_script, tool);
756 struct perf_session *session = script->session;
757 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
758
759 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
760 return -1;
761
762 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
763 if (thread == NULL) {
764 pr_debug("problem processing MMAP2 event, skipping it.\n");
765 return -1;
766 }
767
768 if (!evsel->attr.sample_id_all) {
769 sample->cpu = 0;
770 sample->time = 0;
771 sample->tid = event->mmap2.tid;
772 sample->pid = event->mmap2.pid;
773 }
774 print_sample_start(sample, thread, evsel);
775 perf_event__fprintf(event, stdout);
776
777 return 0;
778 }
779
780 static void sig_handler(int sig __maybe_unused)
781 {
782 session_done = 1;
783 }
784
785 static int __cmd_script(struct perf_script *script)
786 {
787 int ret;
788
789 signal(SIGINT, sig_handler);
790
791 /* override event processing functions */
792 if (script->show_task_events) {
793 script->tool.comm = process_comm_event;
794 script->tool.fork = process_fork_event;
795 script->tool.exit = process_exit_event;
796 }
797 if (script->show_mmap_events) {
798 script->tool.mmap = process_mmap_event;
799 script->tool.mmap2 = process_mmap2_event;
800 }
801
802 ret = perf_session__process_events(script->session, &script->tool);
803
804 if (debug_mode)
805 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
806
807 return ret;
808 }
809
810 struct script_spec {
811 struct list_head node;
812 struct scripting_ops *ops;
813 char spec[0];
814 };
815
816 static LIST_HEAD(script_specs);
817
818 static struct script_spec *script_spec__new(const char *spec,
819 struct scripting_ops *ops)
820 {
821 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
822
823 if (s != NULL) {
824 strcpy(s->spec, spec);
825 s->ops = ops;
826 }
827
828 return s;
829 }
830
831 static void script_spec__add(struct script_spec *s)
832 {
833 list_add_tail(&s->node, &script_specs);
834 }
835
836 static struct script_spec *script_spec__find(const char *spec)
837 {
838 struct script_spec *s;
839
840 list_for_each_entry(s, &script_specs, node)
841 if (strcasecmp(s->spec, spec) == 0)
842 return s;
843 return NULL;
844 }
845
846 static struct script_spec *script_spec__findnew(const char *spec,
847 struct scripting_ops *ops)
848 {
849 struct script_spec *s = script_spec__find(spec);
850
851 if (s)
852 return s;
853
854 s = script_spec__new(spec, ops);
855 if (!s)
856 return NULL;
857
858 script_spec__add(s);
859
860 return s;
861 }
862
863 int script_spec_register(const char *spec, struct scripting_ops *ops)
864 {
865 struct script_spec *s;
866
867 s = script_spec__find(spec);
868 if (s)
869 return -1;
870
871 s = script_spec__findnew(spec, ops);
872 if (!s)
873 return -1;
874
875 return 0;
876 }
877
878 static struct scripting_ops *script_spec__lookup(const char *spec)
879 {
880 struct script_spec *s = script_spec__find(spec);
881 if (!s)
882 return NULL;
883
884 return s->ops;
885 }
886
887 static void list_available_languages(void)
888 {
889 struct script_spec *s;
890
891 fprintf(stderr, "\n");
892 fprintf(stderr, "Scripting language extensions (used in "
893 "perf script -s [spec:]script.[spec]):\n\n");
894
895 list_for_each_entry(s, &script_specs, node)
896 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
897
898 fprintf(stderr, "\n");
899 }
900
901 static int parse_scriptname(const struct option *opt __maybe_unused,
902 const char *str, int unset __maybe_unused)
903 {
904 char spec[PATH_MAX];
905 const char *script, *ext;
906 int len;
907
908 if (strcmp(str, "lang") == 0) {
909 list_available_languages();
910 exit(0);
911 }
912
913 script = strchr(str, ':');
914 if (script) {
915 len = script - str;
916 if (len >= PATH_MAX) {
917 fprintf(stderr, "invalid language specifier");
918 return -1;
919 }
920 strncpy(spec, str, len);
921 spec[len] = '\0';
922 scripting_ops = script_spec__lookup(spec);
923 if (!scripting_ops) {
924 fprintf(stderr, "invalid language specifier");
925 return -1;
926 }
927 script++;
928 } else {
929 script = str;
930 ext = strrchr(script, '.');
931 if (!ext) {
932 fprintf(stderr, "invalid script extension");
933 return -1;
934 }
935 scripting_ops = script_spec__lookup(++ext);
936 if (!scripting_ops) {
937 fprintf(stderr, "invalid script extension");
938 return -1;
939 }
940 }
941
942 script_name = strdup(script);
943
944 return 0;
945 }
946
947 static int parse_output_fields(const struct option *opt __maybe_unused,
948 const char *arg, int unset __maybe_unused)
949 {
950 char *tok;
951 int i, imax = ARRAY_SIZE(all_output_options);
952 int j;
953 int rc = 0;
954 char *str = strdup(arg);
955 int type = -1;
956
957 if (!str)
958 return -ENOMEM;
959
960 /* first word can state for which event type the user is specifying
961 * the fields. If no type exists, the specified fields apply to all
962 * event types found in the file minus the invalid fields for a type.
963 */
964 tok = strchr(str, ':');
965 if (tok) {
966 *tok = '\0';
967 tok++;
968 if (!strcmp(str, "hw"))
969 type = PERF_TYPE_HARDWARE;
970 else if (!strcmp(str, "sw"))
971 type = PERF_TYPE_SOFTWARE;
972 else if (!strcmp(str, "trace"))
973 type = PERF_TYPE_TRACEPOINT;
974 else if (!strcmp(str, "raw"))
975 type = PERF_TYPE_RAW;
976 else {
977 fprintf(stderr, "Invalid event type in field string.\n");
978 rc = -EINVAL;
979 goto out;
980 }
981
982 if (output[type].user_set)
983 pr_warning("Overriding previous field request for %s events.\n",
984 event_type(type));
985
986 output[type].fields = 0;
987 output[type].user_set = true;
988 output[type].wildcard_set = false;
989
990 } else {
991 tok = str;
992 if (strlen(str) == 0) {
993 fprintf(stderr,
994 "Cannot set fields to 'none' for all event types.\n");
995 rc = -EINVAL;
996 goto out;
997 }
998
999 if (output_set_by_user())
1000 pr_warning("Overriding previous field request for all events.\n");
1001
1002 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1003 output[j].fields = 0;
1004 output[j].user_set = true;
1005 output[j].wildcard_set = true;
1006 }
1007 }
1008
1009 tok = strtok(tok, ",");
1010 while (tok) {
1011 for (i = 0; i < imax; ++i) {
1012 if (strcmp(tok, all_output_options[i].str) == 0)
1013 break;
1014 }
1015 if (i == imax) {
1016 fprintf(stderr, "Invalid field requested.\n");
1017 rc = -EINVAL;
1018 goto out;
1019 }
1020
1021 if (type == -1) {
1022 /* add user option to all events types for
1023 * which it is valid
1024 */
1025 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1026 if (output[j].invalid_fields & all_output_options[i].field) {
1027 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1028 all_output_options[i].str, event_type(j));
1029 } else
1030 output[j].fields |= all_output_options[i].field;
1031 }
1032 } else {
1033 if (output[type].invalid_fields & all_output_options[i].field) {
1034 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1035 all_output_options[i].str, event_type(type));
1036
1037 rc = -EINVAL;
1038 goto out;
1039 }
1040 output[type].fields |= all_output_options[i].field;
1041 }
1042
1043 tok = strtok(NULL, ",");
1044 }
1045
1046 if (type >= 0) {
1047 if (output[type].fields == 0) {
1048 pr_debug("No fields requested for %s type. "
1049 "Events will not be displayed.\n", event_type(type));
1050 }
1051 }
1052
1053 out:
1054 free(str);
1055 return rc;
1056 }
1057
1058 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1059 static int is_directory(const char *base_path, const struct dirent *dent)
1060 {
1061 char path[PATH_MAX];
1062 struct stat st;
1063
1064 sprintf(path, "%s/%s", base_path, dent->d_name);
1065 if (stat(path, &st))
1066 return 0;
1067
1068 return S_ISDIR(st.st_mode);
1069 }
1070
1071 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1072 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1073 lang_next) \
1074 if ((lang_dirent.d_type == DT_DIR || \
1075 (lang_dirent.d_type == DT_UNKNOWN && \
1076 is_directory(scripts_path, &lang_dirent))) && \
1077 (strcmp(lang_dirent.d_name, ".")) && \
1078 (strcmp(lang_dirent.d_name, "..")))
1079
1080 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1081 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1082 script_next) \
1083 if (script_dirent.d_type != DT_DIR && \
1084 (script_dirent.d_type != DT_UNKNOWN || \
1085 !is_directory(lang_path, &script_dirent)))
1086
1087
1088 #define RECORD_SUFFIX "-record"
1089 #define REPORT_SUFFIX "-report"
1090
1091 struct script_desc {
1092 struct list_head node;
1093 char *name;
1094 char *half_liner;
1095 char *args;
1096 };
1097
1098 static LIST_HEAD(script_descs);
1099
1100 static struct script_desc *script_desc__new(const char *name)
1101 {
1102 struct script_desc *s = zalloc(sizeof(*s));
1103
1104 if (s != NULL && name)
1105 s->name = strdup(name);
1106
1107 return s;
1108 }
1109
1110 static void script_desc__delete(struct script_desc *s)
1111 {
1112 zfree(&s->name);
1113 zfree(&s->half_liner);
1114 zfree(&s->args);
1115 free(s);
1116 }
1117
1118 static void script_desc__add(struct script_desc *s)
1119 {
1120 list_add_tail(&s->node, &script_descs);
1121 }
1122
1123 static struct script_desc *script_desc__find(const char *name)
1124 {
1125 struct script_desc *s;
1126
1127 list_for_each_entry(s, &script_descs, node)
1128 if (strcasecmp(s->name, name) == 0)
1129 return s;
1130 return NULL;
1131 }
1132
1133 static struct script_desc *script_desc__findnew(const char *name)
1134 {
1135 struct script_desc *s = script_desc__find(name);
1136
1137 if (s)
1138 return s;
1139
1140 s = script_desc__new(name);
1141 if (!s)
1142 goto out_delete_desc;
1143
1144 script_desc__add(s);
1145
1146 return s;
1147
1148 out_delete_desc:
1149 script_desc__delete(s);
1150
1151 return NULL;
1152 }
1153
1154 static const char *ends_with(const char *str, const char *suffix)
1155 {
1156 size_t suffix_len = strlen(suffix);
1157 const char *p = str;
1158
1159 if (strlen(str) > suffix_len) {
1160 p = str + strlen(str) - suffix_len;
1161 if (!strncmp(p, suffix, suffix_len))
1162 return p;
1163 }
1164
1165 return NULL;
1166 }
1167
1168 static int read_script_info(struct script_desc *desc, const char *filename)
1169 {
1170 char line[BUFSIZ], *p;
1171 FILE *fp;
1172
1173 fp = fopen(filename, "r");
1174 if (!fp)
1175 return -1;
1176
1177 while (fgets(line, sizeof(line), fp)) {
1178 p = ltrim(line);
1179 if (strlen(p) == 0)
1180 continue;
1181 if (*p != '#')
1182 continue;
1183 p++;
1184 if (strlen(p) && *p == '!')
1185 continue;
1186
1187 p = ltrim(p);
1188 if (strlen(p) && p[strlen(p) - 1] == '\n')
1189 p[strlen(p) - 1] = '\0';
1190
1191 if (!strncmp(p, "description:", strlen("description:"))) {
1192 p += strlen("description:");
1193 desc->half_liner = strdup(ltrim(p));
1194 continue;
1195 }
1196
1197 if (!strncmp(p, "args:", strlen("args:"))) {
1198 p += strlen("args:");
1199 desc->args = strdup(ltrim(p));
1200 continue;
1201 }
1202 }
1203
1204 fclose(fp);
1205
1206 return 0;
1207 }
1208
1209 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1210 {
1211 char *script_root, *str;
1212
1213 script_root = strdup(script_dirent->d_name);
1214 if (!script_root)
1215 return NULL;
1216
1217 str = (char *)ends_with(script_root, suffix);
1218 if (!str) {
1219 free(script_root);
1220 return NULL;
1221 }
1222
1223 *str = '\0';
1224 return script_root;
1225 }
1226
1227 static int list_available_scripts(const struct option *opt __maybe_unused,
1228 const char *s __maybe_unused,
1229 int unset __maybe_unused)
1230 {
1231 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1232 char scripts_path[MAXPATHLEN];
1233 DIR *scripts_dir, *lang_dir;
1234 char script_path[MAXPATHLEN];
1235 char lang_path[MAXPATHLEN];
1236 struct script_desc *desc;
1237 char first_half[BUFSIZ];
1238 char *script_root;
1239
1240 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1241
1242 scripts_dir = opendir(scripts_path);
1243 if (!scripts_dir)
1244 return -1;
1245
1246 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1247 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1248 lang_dirent.d_name);
1249 lang_dir = opendir(lang_path);
1250 if (!lang_dir)
1251 continue;
1252
1253 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1254 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1255 if (script_root) {
1256 desc = script_desc__findnew(script_root);
1257 snprintf(script_path, MAXPATHLEN, "%s/%s",
1258 lang_path, script_dirent.d_name);
1259 read_script_info(desc, script_path);
1260 free(script_root);
1261 }
1262 }
1263 }
1264
1265 fprintf(stdout, "List of available trace scripts:\n");
1266 list_for_each_entry(desc, &script_descs, node) {
1267 sprintf(first_half, "%s %s", desc->name,
1268 desc->args ? desc->args : "");
1269 fprintf(stdout, " %-36s %s\n", first_half,
1270 desc->half_liner ? desc->half_liner : "");
1271 }
1272
1273 exit(0);
1274 }
1275
1276 /*
1277 * Some scripts specify the required events in their "xxx-record" file,
1278 * this function will check if the events in perf.data match those
1279 * mentioned in the "xxx-record".
1280 *
1281 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1282 * which is covered well now. And new parsing code should be added to
1283 * cover the future complexing formats like event groups etc.
1284 */
1285 static int check_ev_match(char *dir_name, char *scriptname,
1286 struct perf_session *session)
1287 {
1288 char filename[MAXPATHLEN], evname[128];
1289 char line[BUFSIZ], *p;
1290 struct perf_evsel *pos;
1291 int match, len;
1292 FILE *fp;
1293
1294 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1295
1296 fp = fopen(filename, "r");
1297 if (!fp)
1298 return -1;
1299
1300 while (fgets(line, sizeof(line), fp)) {
1301 p = ltrim(line);
1302 if (*p == '#')
1303 continue;
1304
1305 while (strlen(p)) {
1306 p = strstr(p, "-e");
1307 if (!p)
1308 break;
1309
1310 p += 2;
1311 p = ltrim(p);
1312 len = strcspn(p, " \t");
1313 if (!len)
1314 break;
1315
1316 snprintf(evname, len + 1, "%s", p);
1317
1318 match = 0;
1319 evlist__for_each(session->evlist, pos) {
1320 if (!strcmp(perf_evsel__name(pos), evname)) {
1321 match = 1;
1322 break;
1323 }
1324 }
1325
1326 if (!match) {
1327 fclose(fp);
1328 return -1;
1329 }
1330 }
1331 }
1332
1333 fclose(fp);
1334 return 0;
1335 }
1336
1337 /*
1338 * Return -1 if none is found, otherwise the actual scripts number.
1339 *
1340 * Currently the only user of this function is the script browser, which
1341 * will list all statically runnable scripts, select one, execute it and
1342 * show the output in a perf browser.
1343 */
1344 int find_scripts(char **scripts_array, char **scripts_path_array)
1345 {
1346 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1347 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1348 DIR *scripts_dir, *lang_dir;
1349 struct perf_session *session;
1350 struct perf_data_file file = {
1351 .path = input_name,
1352 .mode = PERF_DATA_MODE_READ,
1353 };
1354 char *temp;
1355 int i = 0;
1356
1357 session = perf_session__new(&file, false, NULL);
1358 if (!session)
1359 return -1;
1360
1361 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1362
1363 scripts_dir = opendir(scripts_path);
1364 if (!scripts_dir) {
1365 perf_session__delete(session);
1366 return -1;
1367 }
1368
1369 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1370 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1371 lang_dirent.d_name);
1372 #ifdef NO_LIBPERL
1373 if (strstr(lang_path, "perl"))
1374 continue;
1375 #endif
1376 #ifdef NO_LIBPYTHON
1377 if (strstr(lang_path, "python"))
1378 continue;
1379 #endif
1380
1381 lang_dir = opendir(lang_path);
1382 if (!lang_dir)
1383 continue;
1384
1385 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1386 /* Skip those real time scripts: xxxtop.p[yl] */
1387 if (strstr(script_dirent.d_name, "top."))
1388 continue;
1389 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1390 script_dirent.d_name);
1391 temp = strchr(script_dirent.d_name, '.');
1392 snprintf(scripts_array[i],
1393 (temp - script_dirent.d_name) + 1,
1394 "%s", script_dirent.d_name);
1395
1396 if (check_ev_match(lang_path,
1397 scripts_array[i], session))
1398 continue;
1399
1400 i++;
1401 }
1402 closedir(lang_dir);
1403 }
1404
1405 closedir(scripts_dir);
1406 perf_session__delete(session);
1407 return i;
1408 }
1409
1410 static char *get_script_path(const char *script_root, const char *suffix)
1411 {
1412 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1413 char scripts_path[MAXPATHLEN];
1414 char script_path[MAXPATHLEN];
1415 DIR *scripts_dir, *lang_dir;
1416 char lang_path[MAXPATHLEN];
1417 char *__script_root;
1418
1419 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1420
1421 scripts_dir = opendir(scripts_path);
1422 if (!scripts_dir)
1423 return NULL;
1424
1425 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1426 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1427 lang_dirent.d_name);
1428 lang_dir = opendir(lang_path);
1429 if (!lang_dir)
1430 continue;
1431
1432 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1433 __script_root = get_script_root(&script_dirent, suffix);
1434 if (__script_root && !strcmp(script_root, __script_root)) {
1435 free(__script_root);
1436 closedir(lang_dir);
1437 closedir(scripts_dir);
1438 snprintf(script_path, MAXPATHLEN, "%s/%s",
1439 lang_path, script_dirent.d_name);
1440 return strdup(script_path);
1441 }
1442 free(__script_root);
1443 }
1444 closedir(lang_dir);
1445 }
1446 closedir(scripts_dir);
1447
1448 return NULL;
1449 }
1450
1451 static bool is_top_script(const char *script_path)
1452 {
1453 return ends_with(script_path, "top") == NULL ? false : true;
1454 }
1455
1456 static int has_required_arg(char *script_path)
1457 {
1458 struct script_desc *desc;
1459 int n_args = 0;
1460 char *p;
1461
1462 desc = script_desc__new(NULL);
1463
1464 if (read_script_info(desc, script_path))
1465 goto out;
1466
1467 if (!desc->args)
1468 goto out;
1469
1470 for (p = desc->args; *p; p++)
1471 if (*p == '<')
1472 n_args++;
1473 out:
1474 script_desc__delete(desc);
1475
1476 return n_args;
1477 }
1478
1479 static int have_cmd(int argc, const char **argv)
1480 {
1481 char **__argv = malloc(sizeof(const char *) * argc);
1482
1483 if (!__argv) {
1484 pr_err("malloc failed\n");
1485 return -1;
1486 }
1487
1488 memcpy(__argv, argv, sizeof(const char *) * argc);
1489 argc = parse_options(argc, (const char **)__argv, record_options,
1490 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1491 free(__argv);
1492
1493 system_wide = (argc == 0);
1494
1495 return 0;
1496 }
1497
1498 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1499 {
1500 bool show_full_info = false;
1501 bool header = false;
1502 bool header_only = false;
1503 bool script_started = false;
1504 char *rec_script_path = NULL;
1505 char *rep_script_path = NULL;
1506 struct perf_session *session;
1507 char *script_path = NULL;
1508 const char **__argv;
1509 int i, j, err = 0;
1510 struct perf_script script = {
1511 .tool = {
1512 .sample = process_sample_event,
1513 .mmap = perf_event__process_mmap,
1514 .mmap2 = perf_event__process_mmap2,
1515 .comm = perf_event__process_comm,
1516 .exit = perf_event__process_exit,
1517 .fork = perf_event__process_fork,
1518 .attr = process_attr,
1519 .tracing_data = perf_event__process_tracing_data,
1520 .build_id = perf_event__process_build_id,
1521 .ordered_events = true,
1522 .ordering_requires_timestamps = true,
1523 },
1524 };
1525 const struct option options[] = {
1526 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1527 "dump raw trace in ASCII"),
1528 OPT_INCR('v', "verbose", &verbose,
1529 "be more verbose (show symbol address, etc)"),
1530 OPT_BOOLEAN('L', "Latency", &latency_format,
1531 "show latency attributes (irqs/preemption disabled, etc)"),
1532 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1533 list_available_scripts),
1534 OPT_CALLBACK('s', "script", NULL, "name",
1535 "script file name (lang:script name, script name, or *)",
1536 parse_scriptname),
1537 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1538 "generate perf-script.xx script in specified language"),
1539 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1540 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1541 "do various checks like samples ordering and lost events"),
1542 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1543 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
1544 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1545 "file", "vmlinux pathname"),
1546 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1547 "file", "kallsyms pathname"),
1548 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1549 "When printing symbols do not display call chain"),
1550 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1551 "Look for files with symbols relative to this directory"),
1552 OPT_CALLBACK('f', "fields", NULL, "str",
1553 "comma separated output fields prepend with 'type:'. "
1554 "Valid types: hw,sw,trace,raw. "
1555 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1556 "addr,symoff,period", parse_output_fields),
1557 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1558 "system-wide collection from all CPUs"),
1559 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1560 "only consider these symbols"),
1561 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1562 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1563 "only display events for these comms"),
1564 OPT_BOOLEAN('I', "show-info", &show_full_info,
1565 "display extended information from perf.data file"),
1566 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1567 "Show the path of [kernel.kallsyms]"),
1568 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1569 "Show the fork/comm/exit events"),
1570 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1571 "Show the mmap events"),
1572 OPT_END()
1573 };
1574 const char * const script_usage[] = {
1575 "perf script [<options>]",
1576 "perf script [<options>] record <script> [<record-options>] <command>",
1577 "perf script [<options>] report <script> [script-args]",
1578 "perf script [<options>] <script> [<record-options>] <command>",
1579 "perf script [<options>] <top-script> [script-args]",
1580 NULL
1581 };
1582 struct perf_data_file file = {
1583 .mode = PERF_DATA_MODE_READ,
1584 };
1585
1586 setup_scripting();
1587
1588 argc = parse_options(argc, argv, options, script_usage,
1589 PARSE_OPT_STOP_AT_NON_OPTION);
1590
1591 file.path = input_name;
1592
1593 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1594 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1595 if (!rec_script_path)
1596 return cmd_record(argc, argv, NULL);
1597 }
1598
1599 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1600 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1601 if (!rep_script_path) {
1602 fprintf(stderr,
1603 "Please specify a valid report script"
1604 "(see 'perf script -l' for listing)\n");
1605 return -1;
1606 }
1607 }
1608
1609 /* make sure PERF_EXEC_PATH is set for scripts */
1610 perf_set_argv_exec_path(perf_exec_path());
1611
1612 if (argc && !script_name && !rec_script_path && !rep_script_path) {
1613 int live_pipe[2];
1614 int rep_args;
1615 pid_t pid;
1616
1617 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1618 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1619
1620 if (!rec_script_path && !rep_script_path) {
1621 fprintf(stderr, " Couldn't find script %s\n\n See perf"
1622 " script -l for available scripts.\n", argv[0]);
1623 usage_with_options(script_usage, options);
1624 }
1625
1626 if (is_top_script(argv[0])) {
1627 rep_args = argc - 1;
1628 } else {
1629 int rec_args;
1630
1631 rep_args = has_required_arg(rep_script_path);
1632 rec_args = (argc - 1) - rep_args;
1633 if (rec_args < 0) {
1634 fprintf(stderr, " %s script requires options."
1635 "\n\n See perf script -l for available "
1636 "scripts and options.\n", argv[0]);
1637 usage_with_options(script_usage, options);
1638 }
1639 }
1640
1641 if (pipe(live_pipe) < 0) {
1642 perror("failed to create pipe");
1643 return -1;
1644 }
1645
1646 pid = fork();
1647 if (pid < 0) {
1648 perror("failed to fork");
1649 return -1;
1650 }
1651
1652 if (!pid) {
1653 j = 0;
1654
1655 dup2(live_pipe[1], 1);
1656 close(live_pipe[0]);
1657
1658 if (is_top_script(argv[0])) {
1659 system_wide = true;
1660 } else if (!system_wide) {
1661 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1662 err = -1;
1663 goto out;
1664 }
1665 }
1666
1667 __argv = malloc((argc + 6) * sizeof(const char *));
1668 if (!__argv) {
1669 pr_err("malloc failed\n");
1670 err = -ENOMEM;
1671 goto out;
1672 }
1673
1674 __argv[j++] = "/bin/sh";
1675 __argv[j++] = rec_script_path;
1676 if (system_wide)
1677 __argv[j++] = "-a";
1678 __argv[j++] = "-q";
1679 __argv[j++] = "-o";
1680 __argv[j++] = "-";
1681 for (i = rep_args + 1; i < argc; i++)
1682 __argv[j++] = argv[i];
1683 __argv[j++] = NULL;
1684
1685 execvp("/bin/sh", (char **)__argv);
1686 free(__argv);
1687 exit(-1);
1688 }
1689
1690 dup2(live_pipe[0], 0);
1691 close(live_pipe[1]);
1692
1693 __argv = malloc((argc + 4) * sizeof(const char *));
1694 if (!__argv) {
1695 pr_err("malloc failed\n");
1696 err = -ENOMEM;
1697 goto out;
1698 }
1699
1700 j = 0;
1701 __argv[j++] = "/bin/sh";
1702 __argv[j++] = rep_script_path;
1703 for (i = 1; i < rep_args + 1; i++)
1704 __argv[j++] = argv[i];
1705 __argv[j++] = "-i";
1706 __argv[j++] = "-";
1707 __argv[j++] = NULL;
1708
1709 execvp("/bin/sh", (char **)__argv);
1710 free(__argv);
1711 exit(-1);
1712 }
1713
1714 if (rec_script_path)
1715 script_path = rec_script_path;
1716 if (rep_script_path)
1717 script_path = rep_script_path;
1718
1719 if (script_path) {
1720 j = 0;
1721
1722 if (!rec_script_path)
1723 system_wide = false;
1724 else if (!system_wide) {
1725 if (have_cmd(argc - 1, &argv[1]) != 0) {
1726 err = -1;
1727 goto out;
1728 }
1729 }
1730
1731 __argv = malloc((argc + 2) * sizeof(const char *));
1732 if (!__argv) {
1733 pr_err("malloc failed\n");
1734 err = -ENOMEM;
1735 goto out;
1736 }
1737
1738 __argv[j++] = "/bin/sh";
1739 __argv[j++] = script_path;
1740 if (system_wide)
1741 __argv[j++] = "-a";
1742 for (i = 2; i < argc; i++)
1743 __argv[j++] = argv[i];
1744 __argv[j++] = NULL;
1745
1746 execvp("/bin/sh", (char **)__argv);
1747 free(__argv);
1748 exit(-1);
1749 }
1750
1751 if (!script_name)
1752 setup_pager();
1753
1754 session = perf_session__new(&file, false, &script.tool);
1755 if (session == NULL)
1756 return -1;
1757
1758 if (header || header_only) {
1759 perf_session__fprintf_info(session, stdout, show_full_info);
1760 if (header_only)
1761 goto out_delete;
1762 }
1763
1764 if (symbol__init(&session->header.env) < 0)
1765 goto out_delete;
1766
1767 script.session = session;
1768
1769 if (cpu_list) {
1770 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1771 if (err < 0)
1772 goto out_delete;
1773 }
1774
1775 if (!no_callchain)
1776 symbol_conf.use_callchain = true;
1777 else
1778 symbol_conf.use_callchain = false;
1779
1780 if (generate_script_lang) {
1781 struct stat perf_stat;
1782 int input;
1783
1784 if (output_set_by_user()) {
1785 fprintf(stderr,
1786 "custom fields not supported for generated scripts");
1787 err = -EINVAL;
1788 goto out_delete;
1789 }
1790
1791 input = open(file.path, O_RDONLY); /* input_name */
1792 if (input < 0) {
1793 err = -errno;
1794 perror("failed to open file");
1795 goto out_delete;
1796 }
1797
1798 err = fstat(input, &perf_stat);
1799 if (err < 0) {
1800 perror("failed to stat file");
1801 goto out_delete;
1802 }
1803
1804 if (!perf_stat.st_size) {
1805 fprintf(stderr, "zero-sized file, nothing to do!\n");
1806 goto out_delete;
1807 }
1808
1809 scripting_ops = script_spec__lookup(generate_script_lang);
1810 if (!scripting_ops) {
1811 fprintf(stderr, "invalid language specifier");
1812 err = -ENOENT;
1813 goto out_delete;
1814 }
1815
1816 err = scripting_ops->generate_script(session->tevent.pevent,
1817 "perf-script");
1818 goto out_delete;
1819 }
1820
1821 if (script_name) {
1822 err = scripting_ops->start_script(script_name, argc, argv);
1823 if (err)
1824 goto out_delete;
1825 pr_debug("perf script started with script %s\n\n", script_name);
1826 script_started = true;
1827 }
1828
1829
1830 err = perf_session__check_output_opt(session);
1831 if (err < 0)
1832 goto out_delete;
1833
1834 err = __cmd_script(&script);
1835
1836 flush_scripting();
1837
1838 out_delete:
1839 perf_session__delete(session);
1840
1841 if (script_started)
1842 cleanup_scripting();
1843 out:
1844 return err;
1845 }
This page took 0.074769 seconds and 5 git commands to generate.