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 "util/auxtrace.h"
20 #include <linux/bitmap.h>
22 static char const *script_name
;
23 static char const *generate_script_lang
;
24 static bool debug_mode
;
25 static u64 last_timestamp
;
26 static u64 nr_unordered
;
27 static bool no_callchain
;
28 static bool latency_format
;
29 static bool system_wide
;
30 static bool print_flags
;
31 static const char *cpu_list
;
32 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
34 enum perf_output_field
{
35 PERF_OUTPUT_COMM
= 1U << 0,
36 PERF_OUTPUT_TID
= 1U << 1,
37 PERF_OUTPUT_PID
= 1U << 2,
38 PERF_OUTPUT_TIME
= 1U << 3,
39 PERF_OUTPUT_CPU
= 1U << 4,
40 PERF_OUTPUT_EVNAME
= 1U << 5,
41 PERF_OUTPUT_TRACE
= 1U << 6,
42 PERF_OUTPUT_IP
= 1U << 7,
43 PERF_OUTPUT_SYM
= 1U << 8,
44 PERF_OUTPUT_DSO
= 1U << 9,
45 PERF_OUTPUT_ADDR
= 1U << 10,
46 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
47 PERF_OUTPUT_SRCLINE
= 1U << 12,
48 PERF_OUTPUT_PERIOD
= 1U << 13,
51 struct output_option
{
53 enum perf_output_field field
;
54 } all_output_options
[] = {
55 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
56 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
57 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
58 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
59 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
60 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
61 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
62 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
63 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
64 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
65 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
66 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
67 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
68 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
71 /* default set to maintain compatibility with current format */
75 unsigned int print_ip_opts
;
78 } output
[PERF_TYPE_MAX
] = {
80 [PERF_TYPE_HARDWARE
] = {
83 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
84 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
85 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
86 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
89 .invalid_fields
= PERF_OUTPUT_TRACE
,
92 [PERF_TYPE_SOFTWARE
] = {
95 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
96 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
97 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
98 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
101 .invalid_fields
= PERF_OUTPUT_TRACE
,
104 [PERF_TYPE_TRACEPOINT
] = {
107 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
108 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
109 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
,
115 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
116 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
117 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
118 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
121 .invalid_fields
= PERF_OUTPUT_TRACE
,
125 static bool output_set_by_user(void)
128 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
129 if (output
[j
].user_set
)
135 static const char *output_field2str(enum perf_output_field field
)
137 int i
, imax
= ARRAY_SIZE(all_output_options
);
138 const char *str
= "";
140 for (i
= 0; i
< imax
; ++i
) {
141 if (all_output_options
[i
].field
== field
) {
142 str
= all_output_options
[i
].str
;
149 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
151 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
152 u64 sample_type
, const char *sample_msg
,
153 enum perf_output_field field
,
156 struct perf_event_attr
*attr
= &evsel
->attr
;
157 int type
= attr
->type
;
160 if (attr
->sample_type
& sample_type
)
163 if (output
[type
].user_set
) {
166 evname
= perf_evsel__name(evsel
);
167 pr_err("Samples for '%s' event do not have %s attribute set. "
168 "Cannot print '%s' field.\n",
169 evname
, sample_msg
, output_field2str(field
));
173 /* user did not ask for it explicitly so remove from the default list */
174 output
[type
].fields
&= ~field
;
175 evname
= perf_evsel__name(evsel
);
176 pr_debug("Samples for '%s' event do not have %s attribute set. "
177 "Skipping '%s' field.\n",
178 evname
, sample_msg
, output_field2str(field
));
183 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
184 u64 sample_type
, const char *sample_msg
,
185 enum perf_output_field field
)
187 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
191 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
192 struct perf_session
*session
)
194 struct perf_event_attr
*attr
= &evsel
->attr
;
197 allow_user_set
= perf_header__has_feat(&session
->header
,
200 if (PRINT_FIELD(TRACE
) &&
201 !perf_session__has_traces(session
, "record -R"))
204 if (PRINT_FIELD(IP
)) {
205 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
210 if (PRINT_FIELD(ADDR
) &&
211 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
212 PERF_OUTPUT_ADDR
, allow_user_set
))
215 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
216 pr_err("Display of symbols requested but neither sample IP nor "
217 "sample address\nis selected. Hence, no addresses to convert "
221 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
222 pr_err("Display of offsets requested but symbol is not"
226 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
227 pr_err("Display of DSO requested but neither sample IP nor "
228 "sample address\nis selected. Hence, no addresses to convert "
232 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
233 pr_err("Display of source line number requested but sample IP is not\n"
234 "selected. Hence, no address to lookup the source line number.\n");
238 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
239 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
240 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
243 if (PRINT_FIELD(TIME
) &&
244 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
248 if (PRINT_FIELD(CPU
) &&
249 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
250 PERF_OUTPUT_CPU
, allow_user_set
))
253 if (PRINT_FIELD(PERIOD
) &&
254 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PERIOD
, "PERIOD",
261 static void set_print_ip_opts(struct perf_event_attr
*attr
)
263 unsigned int type
= attr
->type
;
265 output
[type
].print_ip_opts
= 0;
267 output
[type
].print_ip_opts
|= PRINT_IP_OPT_IP
;
269 if (PRINT_FIELD(SYM
))
270 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SYM
;
272 if (PRINT_FIELD(DSO
))
273 output
[type
].print_ip_opts
|= PRINT_IP_OPT_DSO
;
275 if (PRINT_FIELD(SYMOFFSET
))
276 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SYMOFFSET
;
278 if (PRINT_FIELD(SRCLINE
))
279 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SRCLINE
;
283 * verify all user requested events exist and the samples
284 * have the expected data
286 static int perf_session__check_output_opt(struct perf_session
*session
)
289 struct perf_evsel
*evsel
;
291 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
292 evsel
= perf_session__find_first_evtype(session
, j
);
295 * even if fields is set to 0 (ie., show nothing) event must
296 * exist if user explicitly includes it on the command line
298 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
299 pr_err("%s events do not exist. "
300 "Remove corresponding -f option to proceed.\n",
305 if (evsel
&& output
[j
].fields
&&
306 perf_evsel__check_attr(evsel
, session
))
312 set_print_ip_opts(&evsel
->attr
);
316 bool use_callchain
= false;
318 evlist__for_each(session
->evlist
, evsel
) {
319 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
320 use_callchain
= true;
325 symbol_conf
.use_callchain
= false;
329 * set default for tracepoints to print symbols only
330 * if callchains are present
332 if (symbol_conf
.use_callchain
&&
333 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
334 struct perf_event_attr
*attr
;
336 j
= PERF_TYPE_TRACEPOINT
;
337 evsel
= perf_session__find_first_evtype(session
, j
);
343 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
344 output
[j
].fields
|= PERF_OUTPUT_IP
;
345 output
[j
].fields
|= PERF_OUTPUT_SYM
;
346 output
[j
].fields
|= PERF_OUTPUT_DSO
;
347 set_print_ip_opts(attr
);
355 static void print_sample_start(struct perf_sample
*sample
,
356 struct thread
*thread
,
357 struct perf_evsel
*evsel
)
359 struct perf_event_attr
*attr
= &evsel
->attr
;
362 unsigned long long nsecs
;
364 if (PRINT_FIELD(COMM
)) {
366 printf("%8.8s ", thread__comm_str(thread
));
367 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
368 printf("%s ", thread__comm_str(thread
));
370 printf("%16s ", thread__comm_str(thread
));
373 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
374 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
375 else if (PRINT_FIELD(PID
))
376 printf("%5d ", sample
->pid
);
377 else if (PRINT_FIELD(TID
))
378 printf("%5d ", sample
->tid
);
380 if (PRINT_FIELD(CPU
)) {
382 printf("%3d ", sample
->cpu
);
384 printf("[%03d] ", sample
->cpu
);
387 if (PRINT_FIELD(TIME
)) {
388 nsecs
= sample
->time
;
389 secs
= nsecs
/ NSECS_PER_SEC
;
390 nsecs
-= secs
* NSECS_PER_SEC
;
391 usecs
= nsecs
/ NSECS_PER_USEC
;
392 printf("%5lu.%06lu: ", secs
, usecs
);
396 static void print_sample_addr(union perf_event
*event
,
397 struct perf_sample
*sample
,
398 struct thread
*thread
,
399 struct perf_event_attr
*attr
)
401 struct addr_location al
;
403 printf("%16" PRIx64
, sample
->addr
);
405 if (!sample_addr_correlates_sym(attr
))
408 perf_event__preprocess_sample_addr(event
, sample
, thread
, &al
);
410 if (PRINT_FIELD(SYM
)) {
412 if (PRINT_FIELD(SYMOFFSET
))
413 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
415 symbol__fprintf_symname(al
.sym
, stdout
);
418 if (PRINT_FIELD(DSO
)) {
420 map__fprintf_dsoname(al
.map
, stdout
);
425 static void print_sample_bts(union perf_event
*event
,
426 struct perf_sample
*sample
,
427 struct perf_evsel
*evsel
,
428 struct thread
*thread
,
429 struct addr_location
*al
)
431 struct perf_event_attr
*attr
= &evsel
->attr
;
432 bool print_srcline_last
= false;
434 /* print branch_from information */
435 if (PRINT_FIELD(IP
)) {
436 unsigned int print_opts
= output
[attr
->type
].print_ip_opts
;
438 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
442 if (print_opts
& PRINT_IP_OPT_SRCLINE
) {
443 print_srcline_last
= true;
444 print_opts
&= ~PRINT_IP_OPT_SRCLINE
;
447 perf_evsel__print_ip(evsel
, sample
, al
, print_opts
,
448 PERF_MAX_STACK_DEPTH
);
451 /* print branch_to information */
452 if (PRINT_FIELD(ADDR
) ||
453 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
454 !output
[attr
->type
].user_set
)) {
456 print_sample_addr(event
, sample
, thread
, attr
);
459 if (print_srcline_last
)
460 map__fprintf_srcline(al
->map
, al
->addr
, "\n ", stdout
);
465 static void print_sample_flags(u32 flags
)
467 const char *chars
= PERF_IP_FLAG_CHARS
;
468 const int n
= strlen(PERF_IP_FLAG_CHARS
);
472 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
474 str
[pos
++] = chars
[i
];
476 for (; i
< 32; i
++, flags
>>= 1) {
481 printf(" %-4s ", str
);
484 static void process_event(union perf_event
*event
, struct perf_sample
*sample
,
485 struct perf_evsel
*evsel
, struct addr_location
*al
)
487 struct thread
*thread
= al
->thread
;
488 struct perf_event_attr
*attr
= &evsel
->attr
;
490 if (output
[attr
->type
].fields
== 0)
493 print_sample_start(sample
, thread
, evsel
);
495 if (PRINT_FIELD(PERIOD
))
496 printf("%10" PRIu64
" ", sample
->period
);
498 if (PRINT_FIELD(EVNAME
)) {
499 const char *evname
= perf_evsel__name(evsel
);
500 printf("%s: ", evname
? evname
: "[unknown]");
504 print_sample_flags(sample
->flags
);
506 if (is_bts_event(attr
)) {
507 print_sample_bts(event
, sample
, evsel
, thread
, al
);
511 if (PRINT_FIELD(TRACE
))
512 event_format__print(evsel
->tp_format
, sample
->cpu
,
513 sample
->raw_data
, sample
->raw_size
);
514 if (PRINT_FIELD(ADDR
))
515 print_sample_addr(event
, sample
, thread
, attr
);
517 if (PRINT_FIELD(IP
)) {
518 if (!symbol_conf
.use_callchain
)
523 perf_evsel__print_ip(evsel
, sample
, al
,
524 output
[attr
->type
].print_ip_opts
,
525 PERF_MAX_STACK_DEPTH
);
531 static int default_start_script(const char *script __maybe_unused
,
532 int argc __maybe_unused
,
533 const char **argv __maybe_unused
)
538 static int default_flush_script(void)
543 static int default_stop_script(void)
548 static int default_generate_script(struct pevent
*pevent __maybe_unused
,
549 const char *outfile __maybe_unused
)
554 static struct scripting_ops default_scripting_ops
= {
555 .start_script
= default_start_script
,
556 .flush_script
= default_flush_script
,
557 .stop_script
= default_stop_script
,
558 .process_event
= process_event
,
559 .generate_script
= default_generate_script
,
562 static struct scripting_ops
*scripting_ops
;
564 static void setup_scripting(void)
566 setup_perl_scripting();
567 setup_python_scripting();
569 scripting_ops
= &default_scripting_ops
;
572 static int flush_scripting(void)
574 return scripting_ops
->flush_script();
577 static int cleanup_scripting(void)
579 pr_debug("\nperf script stopped\n");
581 return scripting_ops
->stop_script();
584 static int process_sample_event(struct perf_tool
*tool __maybe_unused
,
585 union perf_event
*event
,
586 struct perf_sample
*sample
,
587 struct perf_evsel
*evsel
,
588 struct machine
*machine
)
590 struct addr_location al
;
593 if (sample
->time
< last_timestamp
) {
594 pr_err("Samples misordered, previous: %" PRIu64
595 " this: %" PRIu64
"\n", last_timestamp
,
599 last_timestamp
= sample
->time
;
603 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
604 pr_err("problem processing %d event, skipping it.\n",
612 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
615 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
617 addr_location__put(&al
);
622 struct perf_tool tool
;
623 struct perf_session
*session
;
624 bool show_task_events
;
625 bool show_mmap_events
;
626 bool show_switch_events
;
629 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
630 struct perf_evlist
**pevlist
)
632 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
633 struct perf_evlist
*evlist
;
634 struct perf_evsel
*evsel
, *pos
;
637 err
= perf_event__process_attr(tool
, event
, pevlist
);
642 evsel
= perf_evlist__last(*pevlist
);
644 if (evsel
->attr
.type
>= PERF_TYPE_MAX
)
647 evlist__for_each(evlist
, pos
) {
648 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
652 set_print_ip_opts(&evsel
->attr
);
654 return perf_evsel__check_attr(evsel
, scr
->session
);
657 static int process_comm_event(struct perf_tool
*tool
,
658 union perf_event
*event
,
659 struct perf_sample
*sample
,
660 struct machine
*machine
)
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__id2evsel(session
->evlist
, sample
->id
);
668 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
669 if (thread
== NULL
) {
670 pr_debug("problem processing COMM event, skipping it.\n");
674 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
677 if (!evsel
->attr
.sample_id_all
) {
680 sample
->tid
= event
->comm
.tid
;
681 sample
->pid
= event
->comm
.pid
;
683 print_sample_start(sample
, thread
, evsel
);
684 perf_event__fprintf(event
, stdout
);
691 static int process_fork_event(struct perf_tool
*tool
,
692 union perf_event
*event
,
693 struct perf_sample
*sample
,
694 struct machine
*machine
)
696 struct thread
*thread
;
697 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
698 struct perf_session
*session
= script
->session
;
699 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
701 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
704 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
705 if (thread
== NULL
) {
706 pr_debug("problem processing FORK event, skipping it.\n");
710 if (!evsel
->attr
.sample_id_all
) {
712 sample
->time
= event
->fork
.time
;
713 sample
->tid
= event
->fork
.tid
;
714 sample
->pid
= event
->fork
.pid
;
716 print_sample_start(sample
, thread
, evsel
);
717 perf_event__fprintf(event
, stdout
);
722 static int process_exit_event(struct perf_tool
*tool
,
723 union perf_event
*event
,
724 struct perf_sample
*sample
,
725 struct machine
*machine
)
728 struct thread
*thread
;
729 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
730 struct perf_session
*session
= script
->session
;
731 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
733 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
734 if (thread
== NULL
) {
735 pr_debug("problem processing EXIT event, skipping it.\n");
739 if (!evsel
->attr
.sample_id_all
) {
742 sample
->tid
= event
->comm
.tid
;
743 sample
->pid
= event
->comm
.pid
;
745 print_sample_start(sample
, thread
, evsel
);
746 perf_event__fprintf(event
, stdout
);
748 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
755 static int process_mmap_event(struct perf_tool
*tool
,
756 union perf_event
*event
,
757 struct perf_sample
*sample
,
758 struct machine
*machine
)
760 struct thread
*thread
;
761 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
762 struct perf_session
*session
= script
->session
;
763 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
765 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
768 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
769 if (thread
== NULL
) {
770 pr_debug("problem processing MMAP event, skipping it.\n");
774 if (!evsel
->attr
.sample_id_all
) {
777 sample
->tid
= event
->mmap
.tid
;
778 sample
->pid
= event
->mmap
.pid
;
780 print_sample_start(sample
, thread
, evsel
);
781 perf_event__fprintf(event
, stdout
);
786 static int process_mmap2_event(struct perf_tool
*tool
,
787 union perf_event
*event
,
788 struct perf_sample
*sample
,
789 struct machine
*machine
)
791 struct thread
*thread
;
792 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
793 struct perf_session
*session
= script
->session
;
794 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
796 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
799 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
800 if (thread
== NULL
) {
801 pr_debug("problem processing MMAP2 event, skipping it.\n");
805 if (!evsel
->attr
.sample_id_all
) {
808 sample
->tid
= event
->mmap2
.tid
;
809 sample
->pid
= event
->mmap2
.pid
;
811 print_sample_start(sample
, thread
, evsel
);
812 perf_event__fprintf(event
, stdout
);
817 static int process_switch_event(struct perf_tool
*tool
,
818 union perf_event
*event
,
819 struct perf_sample
*sample
,
820 struct machine
*machine
)
822 struct thread
*thread
;
823 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
824 struct perf_session
*session
= script
->session
;
825 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
827 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
830 thread
= machine__findnew_thread(machine
, sample
->pid
,
832 if (thread
== NULL
) {
833 pr_debug("problem processing SWITCH event, skipping it.\n");
837 print_sample_start(sample
, thread
, evsel
);
838 perf_event__fprintf(event
, stdout
);
843 static void sig_handler(int sig __maybe_unused
)
848 static int __cmd_script(struct perf_script
*script
)
852 signal(SIGINT
, sig_handler
);
854 /* override event processing functions */
855 if (script
->show_task_events
) {
856 script
->tool
.comm
= process_comm_event
;
857 script
->tool
.fork
= process_fork_event
;
858 script
->tool
.exit
= process_exit_event
;
860 if (script
->show_mmap_events
) {
861 script
->tool
.mmap
= process_mmap_event
;
862 script
->tool
.mmap2
= process_mmap2_event
;
864 if (script
->show_switch_events
)
865 script
->tool
.context_switch
= process_switch_event
;
867 ret
= perf_session__process_events(script
->session
);
870 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
876 struct list_head node
;
877 struct scripting_ops
*ops
;
881 static LIST_HEAD(script_specs
);
883 static struct script_spec
*script_spec__new(const char *spec
,
884 struct scripting_ops
*ops
)
886 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
889 strcpy(s
->spec
, spec
);
896 static void script_spec__add(struct script_spec
*s
)
898 list_add_tail(&s
->node
, &script_specs
);
901 static struct script_spec
*script_spec__find(const char *spec
)
903 struct script_spec
*s
;
905 list_for_each_entry(s
, &script_specs
, node
)
906 if (strcasecmp(s
->spec
, spec
) == 0)
911 static struct script_spec
*script_spec__findnew(const char *spec
,
912 struct scripting_ops
*ops
)
914 struct script_spec
*s
= script_spec__find(spec
);
919 s
= script_spec__new(spec
, ops
);
928 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
930 struct script_spec
*s
;
932 s
= script_spec__find(spec
);
936 s
= script_spec__findnew(spec
, ops
);
943 static struct scripting_ops
*script_spec__lookup(const char *spec
)
945 struct script_spec
*s
= script_spec__find(spec
);
952 static void list_available_languages(void)
954 struct script_spec
*s
;
956 fprintf(stderr
, "\n");
957 fprintf(stderr
, "Scripting language extensions (used in "
958 "perf script -s [spec:]script.[spec]):\n\n");
960 list_for_each_entry(s
, &script_specs
, node
)
961 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
963 fprintf(stderr
, "\n");
966 static int parse_scriptname(const struct option
*opt __maybe_unused
,
967 const char *str
, int unset __maybe_unused
)
970 const char *script
, *ext
;
973 if (strcmp(str
, "lang") == 0) {
974 list_available_languages();
978 script
= strchr(str
, ':');
981 if (len
>= PATH_MAX
) {
982 fprintf(stderr
, "invalid language specifier");
985 strncpy(spec
, str
, len
);
987 scripting_ops
= script_spec__lookup(spec
);
988 if (!scripting_ops
) {
989 fprintf(stderr
, "invalid language specifier");
995 ext
= strrchr(script
, '.');
997 fprintf(stderr
, "invalid script extension");
1000 scripting_ops
= script_spec__lookup(++ext
);
1001 if (!scripting_ops
) {
1002 fprintf(stderr
, "invalid script extension");
1007 script_name
= strdup(script
);
1012 static int parse_output_fields(const struct option
*opt __maybe_unused
,
1013 const char *arg
, int unset __maybe_unused
)
1016 int i
, imax
= ARRAY_SIZE(all_output_options
);
1019 char *str
= strdup(arg
);
1025 /* first word can state for which event type the user is specifying
1026 * the fields. If no type exists, the specified fields apply to all
1027 * event types found in the file minus the invalid fields for a type.
1029 tok
= strchr(str
, ':');
1033 if (!strcmp(str
, "hw"))
1034 type
= PERF_TYPE_HARDWARE
;
1035 else if (!strcmp(str
, "sw"))
1036 type
= PERF_TYPE_SOFTWARE
;
1037 else if (!strcmp(str
, "trace"))
1038 type
= PERF_TYPE_TRACEPOINT
;
1039 else if (!strcmp(str
, "raw"))
1040 type
= PERF_TYPE_RAW
;
1042 fprintf(stderr
, "Invalid event type in field string.\n");
1047 if (output
[type
].user_set
)
1048 pr_warning("Overriding previous field request for %s events.\n",
1051 output
[type
].fields
= 0;
1052 output
[type
].user_set
= true;
1053 output
[type
].wildcard_set
= false;
1057 if (strlen(str
) == 0) {
1059 "Cannot set fields to 'none' for all event types.\n");
1064 if (output_set_by_user())
1065 pr_warning("Overriding previous field request for all events.\n");
1067 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1068 output
[j
].fields
= 0;
1069 output
[j
].user_set
= true;
1070 output
[j
].wildcard_set
= true;
1074 for (tok
= strtok(tok
, ","); tok
; tok
= strtok(NULL
, ",")) {
1075 for (i
= 0; i
< imax
; ++i
) {
1076 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
1079 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
1084 fprintf(stderr
, "Invalid field requested.\n");
1090 /* add user option to all events types for
1093 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1094 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
1095 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1096 all_output_options
[i
].str
, event_type(j
));
1098 output
[j
].fields
|= all_output_options
[i
].field
;
1101 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
1102 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
1103 all_output_options
[i
].str
, event_type(type
));
1108 output
[type
].fields
|= all_output_options
[i
].field
;
1113 if (output
[type
].fields
== 0) {
1114 pr_debug("No fields requested for %s type. "
1115 "Events will not be displayed.\n", event_type(type
));
1124 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1125 static int is_directory(const char *base_path
, const struct dirent
*dent
)
1127 char path
[PATH_MAX
];
1130 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
1131 if (stat(path
, &st
))
1134 return S_ISDIR(st
.st_mode
);
1137 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1138 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1140 if ((lang_dirent.d_type == DT_DIR || \
1141 (lang_dirent.d_type == DT_UNKNOWN && \
1142 is_directory(scripts_path, &lang_dirent))) && \
1143 (strcmp(lang_dirent.d_name, ".")) && \
1144 (strcmp(lang_dirent.d_name, "..")))
1146 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1147 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1149 if (script_dirent.d_type != DT_DIR && \
1150 (script_dirent.d_type != DT_UNKNOWN || \
1151 !is_directory(lang_path, &script_dirent)))
1154 #define RECORD_SUFFIX "-record"
1155 #define REPORT_SUFFIX "-report"
1157 struct script_desc
{
1158 struct list_head node
;
1164 static LIST_HEAD(script_descs
);
1166 static struct script_desc
*script_desc__new(const char *name
)
1168 struct script_desc
*s
= zalloc(sizeof(*s
));
1170 if (s
!= NULL
&& name
)
1171 s
->name
= strdup(name
);
1176 static void script_desc__delete(struct script_desc
*s
)
1179 zfree(&s
->half_liner
);
1184 static void script_desc__add(struct script_desc
*s
)
1186 list_add_tail(&s
->node
, &script_descs
);
1189 static struct script_desc
*script_desc__find(const char *name
)
1191 struct script_desc
*s
;
1193 list_for_each_entry(s
, &script_descs
, node
)
1194 if (strcasecmp(s
->name
, name
) == 0)
1199 static struct script_desc
*script_desc__findnew(const char *name
)
1201 struct script_desc
*s
= script_desc__find(name
);
1206 s
= script_desc__new(name
);
1208 goto out_delete_desc
;
1210 script_desc__add(s
);
1215 script_desc__delete(s
);
1220 static const char *ends_with(const char *str
, const char *suffix
)
1222 size_t suffix_len
= strlen(suffix
);
1223 const char *p
= str
;
1225 if (strlen(str
) > suffix_len
) {
1226 p
= str
+ strlen(str
) - suffix_len
;
1227 if (!strncmp(p
, suffix
, suffix_len
))
1234 static int read_script_info(struct script_desc
*desc
, const char *filename
)
1236 char line
[BUFSIZ
], *p
;
1239 fp
= fopen(filename
, "r");
1243 while (fgets(line
, sizeof(line
), fp
)) {
1250 if (strlen(p
) && *p
== '!')
1254 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
1255 p
[strlen(p
) - 1] = '\0';
1257 if (!strncmp(p
, "description:", strlen("description:"))) {
1258 p
+= strlen("description:");
1259 desc
->half_liner
= strdup(ltrim(p
));
1263 if (!strncmp(p
, "args:", strlen("args:"))) {
1264 p
+= strlen("args:");
1265 desc
->args
= strdup(ltrim(p
));
1275 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
1277 char *script_root
, *str
;
1279 script_root
= strdup(script_dirent
->d_name
);
1283 str
= (char *)ends_with(script_root
, suffix
);
1293 static int list_available_scripts(const struct option
*opt __maybe_unused
,
1294 const char *s __maybe_unused
,
1295 int unset __maybe_unused
)
1297 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1298 char scripts_path
[MAXPATHLEN
];
1299 DIR *scripts_dir
, *lang_dir
;
1300 char script_path
[MAXPATHLEN
];
1301 char lang_path
[MAXPATHLEN
];
1302 struct script_desc
*desc
;
1303 char first_half
[BUFSIZ
];
1306 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1308 scripts_dir
= opendir(scripts_path
);
1312 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1313 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1314 lang_dirent
.d_name
);
1315 lang_dir
= opendir(lang_path
);
1319 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1320 script_root
= get_script_root(&script_dirent
, REPORT_SUFFIX
);
1322 desc
= script_desc__findnew(script_root
);
1323 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1324 lang_path
, script_dirent
.d_name
);
1325 read_script_info(desc
, script_path
);
1331 fprintf(stdout
, "List of available trace scripts:\n");
1332 list_for_each_entry(desc
, &script_descs
, node
) {
1333 sprintf(first_half
, "%s %s", desc
->name
,
1334 desc
->args
? desc
->args
: "");
1335 fprintf(stdout
, " %-36s %s\n", first_half
,
1336 desc
->half_liner
? desc
->half_liner
: "");
1343 * Some scripts specify the required events in their "xxx-record" file,
1344 * this function will check if the events in perf.data match those
1345 * mentioned in the "xxx-record".
1347 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1348 * which is covered well now. And new parsing code should be added to
1349 * cover the future complexing formats like event groups etc.
1351 static int check_ev_match(char *dir_name
, char *scriptname
,
1352 struct perf_session
*session
)
1354 char filename
[MAXPATHLEN
], evname
[128];
1355 char line
[BUFSIZ
], *p
;
1356 struct perf_evsel
*pos
;
1360 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
1362 fp
= fopen(filename
, "r");
1366 while (fgets(line
, sizeof(line
), fp
)) {
1372 p
= strstr(p
, "-e");
1378 len
= strcspn(p
, " \t");
1382 snprintf(evname
, len
+ 1, "%s", p
);
1385 evlist__for_each(session
->evlist
, pos
) {
1386 if (!strcmp(perf_evsel__name(pos
), evname
)) {
1404 * Return -1 if none is found, otherwise the actual scripts number.
1406 * Currently the only user of this function is the script browser, which
1407 * will list all statically runnable scripts, select one, execute it and
1408 * show the output in a perf browser.
1410 int find_scripts(char **scripts_array
, char **scripts_path_array
)
1412 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1413 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
1414 DIR *scripts_dir
, *lang_dir
;
1415 struct perf_session
*session
;
1416 struct perf_data_file file
= {
1418 .mode
= PERF_DATA_MODE_READ
,
1423 session
= perf_session__new(&file
, false, NULL
);
1427 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1429 scripts_dir
= opendir(scripts_path
);
1431 perf_session__delete(session
);
1435 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1436 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
1437 lang_dirent
.d_name
);
1439 if (strstr(lang_path
, "perl"))
1443 if (strstr(lang_path
, "python"))
1447 lang_dir
= opendir(lang_path
);
1451 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1452 /* Skip those real time scripts: xxxtop.p[yl] */
1453 if (strstr(script_dirent
.d_name
, "top."))
1455 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
1456 script_dirent
.d_name
);
1457 temp
= strchr(script_dirent
.d_name
, '.');
1458 snprintf(scripts_array
[i
],
1459 (temp
- script_dirent
.d_name
) + 1,
1460 "%s", script_dirent
.d_name
);
1462 if (check_ev_match(lang_path
,
1463 scripts_array
[i
], session
))
1471 closedir(scripts_dir
);
1472 perf_session__delete(session
);
1476 static char *get_script_path(const char *script_root
, const char *suffix
)
1478 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1479 char scripts_path
[MAXPATHLEN
];
1480 char script_path
[MAXPATHLEN
];
1481 DIR *scripts_dir
, *lang_dir
;
1482 char lang_path
[MAXPATHLEN
];
1483 char *__script_root
;
1485 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1487 scripts_dir
= opendir(scripts_path
);
1491 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1492 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1493 lang_dirent
.d_name
);
1494 lang_dir
= opendir(lang_path
);
1498 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1499 __script_root
= get_script_root(&script_dirent
, suffix
);
1500 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
1501 free(__script_root
);
1503 closedir(scripts_dir
);
1504 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1505 lang_path
, script_dirent
.d_name
);
1506 return strdup(script_path
);
1508 free(__script_root
);
1512 closedir(scripts_dir
);
1517 static bool is_top_script(const char *script_path
)
1519 return ends_with(script_path
, "top") == NULL
? false : true;
1522 static int has_required_arg(char *script_path
)
1524 struct script_desc
*desc
;
1528 desc
= script_desc__new(NULL
);
1530 if (read_script_info(desc
, script_path
))
1536 for (p
= desc
->args
; *p
; p
++)
1540 script_desc__delete(desc
);
1545 static int have_cmd(int argc
, const char **argv
)
1547 char **__argv
= malloc(sizeof(const char *) * argc
);
1550 pr_err("malloc failed\n");
1554 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1555 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1556 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1559 system_wide
= (argc
== 0);
1564 static void script__setup_sample_type(struct perf_script
*script
)
1566 struct perf_session
*session
= script
->session
;
1567 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
1569 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
1570 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
1571 (sample_type
& PERF_SAMPLE_STACK_USER
))
1572 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
1573 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
1574 callchain_param
.record_mode
= CALLCHAIN_LBR
;
1576 callchain_param
.record_mode
= CALLCHAIN_FP
;
1580 int cmd_script(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1582 bool show_full_info
= false;
1583 bool header
= false;
1584 bool header_only
= false;
1585 bool script_started
= false;
1586 char *rec_script_path
= NULL
;
1587 char *rep_script_path
= NULL
;
1588 struct perf_session
*session
;
1589 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
1590 char *script_path
= NULL
;
1591 const char **__argv
;
1593 struct perf_script script
= {
1595 .sample
= process_sample_event
,
1596 .mmap
= perf_event__process_mmap
,
1597 .mmap2
= perf_event__process_mmap2
,
1598 .comm
= perf_event__process_comm
,
1599 .exit
= perf_event__process_exit
,
1600 .fork
= perf_event__process_fork
,
1601 .attr
= process_attr
,
1602 .tracing_data
= perf_event__process_tracing_data
,
1603 .build_id
= perf_event__process_build_id
,
1604 .id_index
= perf_event__process_id_index
,
1605 .auxtrace_info
= perf_event__process_auxtrace_info
,
1606 .auxtrace
= perf_event__process_auxtrace
,
1607 .auxtrace_error
= perf_event__process_auxtrace_error
,
1608 .ordered_events
= true,
1609 .ordering_requires_timestamps
= true,
1612 struct perf_data_file file
= {
1613 .mode
= PERF_DATA_MODE_READ
,
1615 const struct option options
[] = {
1616 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1617 "dump raw trace in ASCII"),
1618 OPT_INCR('v', "verbose", &verbose
,
1619 "be more verbose (show symbol address, etc)"),
1620 OPT_BOOLEAN('L', "Latency", &latency_format
,
1621 "show latency attributes (irqs/preemption disabled, etc)"),
1622 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
1623 list_available_scripts
),
1624 OPT_CALLBACK('s', "script", NULL
, "name",
1625 "script file name (lang:script name, script name, or *)",
1627 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
1628 "generate perf-script.xx script in specified language"),
1629 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
1630 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
1631 "do various checks like samples ordering and lost events"),
1632 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
1633 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
1634 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
1635 "file", "vmlinux pathname"),
1636 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
1637 "file", "kallsyms pathname"),
1638 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
1639 "When printing symbols do not display call chain"),
1640 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1641 "Look for files with symbols relative to this directory"),
1642 OPT_CALLBACK('F', "fields", NULL
, "str",
1643 "comma separated output fields prepend with 'type:'. "
1644 "Valid types: hw,sw,trace,raw. "
1645 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1646 "addr,symoff,period,flags", parse_output_fields
),
1647 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
1648 "system-wide collection from all CPUs"),
1649 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
1650 "only consider these symbols"),
1651 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
1652 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
1653 "only display events for these comms"),
1654 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
1655 "only consider symbols in these pids"),
1656 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
1657 "only consider symbols in these tids"),
1658 OPT_BOOLEAN('I', "show-info", &show_full_info
,
1659 "display extended information from perf.data file"),
1660 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
1661 "Show the path of [kernel.kallsyms]"),
1662 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
1663 "Show the fork/comm/exit events"),
1664 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
1665 "Show the mmap events"),
1666 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
1667 "Show context switch events (if recorded)"),
1668 OPT_BOOLEAN('f', "force", &file
.force
, "don't complain, do it"),
1669 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
1670 "Instruction Tracing options",
1671 itrace_parse_synth_opts
),
1672 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
1673 "Show full source file name path for source lines"),
1674 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
1675 "Enable symbol demangling"),
1676 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
1677 "Enable kernel symbol demangling"),
1681 const char * const script_subcommands
[] = { "record", "report", NULL
};
1682 const char *script_usage
[] = {
1683 "perf script [<options>]",
1684 "perf script [<options>] record <script> [<record-options>] <command>",
1685 "perf script [<options>] report <script> [script-args]",
1686 "perf script [<options>] <script> [<record-options>] <command>",
1687 "perf script [<options>] <top-script> [script-args]",
1693 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
1694 PARSE_OPT_STOP_AT_NON_OPTION
);
1696 file
.path
= input_name
;
1698 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
1699 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
1700 if (!rec_script_path
)
1701 return cmd_record(argc
, argv
, NULL
);
1704 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
1705 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
1706 if (!rep_script_path
) {
1708 "Please specify a valid report script"
1709 "(see 'perf script -l' for listing)\n");
1714 /* make sure PERF_EXEC_PATH is set for scripts */
1715 perf_set_argv_exec_path(perf_exec_path());
1717 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
1722 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
1723 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
1725 if (!rec_script_path
&& !rep_script_path
) {
1726 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
1727 " script -l for available scripts.\n", argv
[0]);
1728 usage_with_options(script_usage
, options
);
1731 if (is_top_script(argv
[0])) {
1732 rep_args
= argc
- 1;
1736 rep_args
= has_required_arg(rep_script_path
);
1737 rec_args
= (argc
- 1) - rep_args
;
1739 fprintf(stderr
, " %s script requires options."
1740 "\n\n See perf script -l for available "
1741 "scripts and options.\n", argv
[0]);
1742 usage_with_options(script_usage
, options
);
1746 if (pipe(live_pipe
) < 0) {
1747 perror("failed to create pipe");
1753 perror("failed to fork");
1760 dup2(live_pipe
[1], 1);
1761 close(live_pipe
[0]);
1763 if (is_top_script(argv
[0])) {
1765 } else if (!system_wide
) {
1766 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
1772 __argv
= malloc((argc
+ 6) * sizeof(const char *));
1774 pr_err("malloc failed\n");
1779 __argv
[j
++] = "/bin/sh";
1780 __argv
[j
++] = rec_script_path
;
1786 for (i
= rep_args
+ 1; i
< argc
; i
++)
1787 __argv
[j
++] = argv
[i
];
1790 execvp("/bin/sh", (char **)__argv
);
1795 dup2(live_pipe
[0], 0);
1796 close(live_pipe
[1]);
1798 __argv
= malloc((argc
+ 4) * sizeof(const char *));
1800 pr_err("malloc failed\n");
1806 __argv
[j
++] = "/bin/sh";
1807 __argv
[j
++] = rep_script_path
;
1808 for (i
= 1; i
< rep_args
+ 1; i
++)
1809 __argv
[j
++] = argv
[i
];
1814 execvp("/bin/sh", (char **)__argv
);
1819 if (rec_script_path
)
1820 script_path
= rec_script_path
;
1821 if (rep_script_path
)
1822 script_path
= rep_script_path
;
1827 if (!rec_script_path
)
1828 system_wide
= false;
1829 else if (!system_wide
) {
1830 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
1836 __argv
= malloc((argc
+ 2) * sizeof(const char *));
1838 pr_err("malloc failed\n");
1843 __argv
[j
++] = "/bin/sh";
1844 __argv
[j
++] = script_path
;
1847 for (i
= 2; i
< argc
; i
++)
1848 __argv
[j
++] = argv
[i
];
1851 execvp("/bin/sh", (char **)__argv
);
1859 session
= perf_session__new(&file
, false, &script
.tool
);
1860 if (session
== NULL
)
1863 if (header
|| header_only
) {
1864 perf_session__fprintf_info(session
, stdout
, show_full_info
);
1869 if (symbol__init(&session
->header
.env
) < 0)
1872 script
.session
= session
;
1873 script__setup_sample_type(&script
);
1875 session
->itrace_synth_opts
= &itrace_synth_opts
;
1878 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
1884 symbol_conf
.use_callchain
= true;
1886 symbol_conf
.use_callchain
= false;
1888 if (session
->tevent
.pevent
&&
1889 pevent_set_function_resolver(session
->tevent
.pevent
,
1890 machine__resolve_kernel_addr
,
1891 &session
->machines
.host
) < 0) {
1892 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
1896 if (generate_script_lang
) {
1897 struct stat perf_stat
;
1900 if (output_set_by_user()) {
1902 "custom fields not supported for generated scripts");
1907 input
= open(file
.path
, O_RDONLY
); /* input_name */
1910 perror("failed to open file");
1914 err
= fstat(input
, &perf_stat
);
1916 perror("failed to stat file");
1920 if (!perf_stat
.st_size
) {
1921 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1925 scripting_ops
= script_spec__lookup(generate_script_lang
);
1926 if (!scripting_ops
) {
1927 fprintf(stderr
, "invalid language specifier");
1932 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
1938 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
1941 pr_debug("perf script started with script %s\n\n", script_name
);
1942 script_started
= true;
1946 err
= perf_session__check_output_opt(session
);
1950 err
= __cmd_script(&script
);
1955 perf_session__delete(session
);
1958 cleanup_scripting();