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/perf_regs.h"
10 #include "util/session.h"
11 #include "util/tool.h"
12 #include "util/symbol.h"
13 #include "util/thread.h"
14 #include "util/trace-event.h"
15 #include "util/util.h"
16 #include "util/evlist.h"
17 #include "util/evsel.h"
18 #include "util/sort.h"
19 #include "util/data.h"
20 #include "util/auxtrace.h"
21 #include <linux/bitmap.h>
23 static char const *script_name
;
24 static char const *generate_script_lang
;
25 static bool debug_mode
;
26 static u64 last_timestamp
;
27 static u64 nr_unordered
;
28 static bool no_callchain
;
29 static bool latency_format
;
30 static bool system_wide
;
31 static bool print_flags
;
33 static const char *cpu_list
;
34 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
36 unsigned int scripting_max_stack
= PERF_MAX_STACK_DEPTH
;
38 enum perf_output_field
{
39 PERF_OUTPUT_COMM
= 1U << 0,
40 PERF_OUTPUT_TID
= 1U << 1,
41 PERF_OUTPUT_PID
= 1U << 2,
42 PERF_OUTPUT_TIME
= 1U << 3,
43 PERF_OUTPUT_CPU
= 1U << 4,
44 PERF_OUTPUT_EVNAME
= 1U << 5,
45 PERF_OUTPUT_TRACE
= 1U << 6,
46 PERF_OUTPUT_IP
= 1U << 7,
47 PERF_OUTPUT_SYM
= 1U << 8,
48 PERF_OUTPUT_DSO
= 1U << 9,
49 PERF_OUTPUT_ADDR
= 1U << 10,
50 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
51 PERF_OUTPUT_SRCLINE
= 1U << 12,
52 PERF_OUTPUT_PERIOD
= 1U << 13,
53 PERF_OUTPUT_IREGS
= 1U << 14,
54 PERF_OUTPUT_BRSTACK
= 1U << 15,
55 PERF_OUTPUT_BRSTACKSYM
= 1U << 16,
58 struct output_option
{
60 enum perf_output_field field
;
61 } all_output_options
[] = {
62 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
63 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
64 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
65 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
66 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
67 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
68 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
69 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
70 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
71 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
72 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
73 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
74 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
75 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
76 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
77 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
78 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
81 /* default set to maintain compatibility with current format */
85 unsigned int print_ip_opts
;
88 } output
[PERF_TYPE_MAX
] = {
90 [PERF_TYPE_HARDWARE
] = {
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
|
99 .invalid_fields
= PERF_OUTPUT_TRACE
,
102 [PERF_TYPE_SOFTWARE
] = {
105 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
106 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
107 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
108 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
111 .invalid_fields
= PERF_OUTPUT_TRACE
,
114 [PERF_TYPE_TRACEPOINT
] = {
117 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
118 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
119 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
,
125 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
126 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
127 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
128 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
131 .invalid_fields
= PERF_OUTPUT_TRACE
,
135 static bool output_set_by_user(void)
138 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
139 if (output
[j
].user_set
)
145 static const char *output_field2str(enum perf_output_field field
)
147 int i
, imax
= ARRAY_SIZE(all_output_options
);
148 const char *str
= "";
150 for (i
= 0; i
< imax
; ++i
) {
151 if (all_output_options
[i
].field
== field
) {
152 str
= all_output_options
[i
].str
;
159 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
161 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
162 u64 sample_type
, const char *sample_msg
,
163 enum perf_output_field field
,
166 struct perf_event_attr
*attr
= &evsel
->attr
;
167 int type
= attr
->type
;
170 if (attr
->sample_type
& sample_type
)
173 if (output
[type
].user_set
) {
176 evname
= perf_evsel__name(evsel
);
177 pr_err("Samples for '%s' event do not have %s attribute set. "
178 "Cannot print '%s' field.\n",
179 evname
, sample_msg
, output_field2str(field
));
183 /* user did not ask for it explicitly so remove from the default list */
184 output
[type
].fields
&= ~field
;
185 evname
= perf_evsel__name(evsel
);
186 pr_debug("Samples for '%s' event do not have %s attribute set. "
187 "Skipping '%s' field.\n",
188 evname
, sample_msg
, output_field2str(field
));
193 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
194 u64 sample_type
, const char *sample_msg
,
195 enum perf_output_field field
)
197 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
201 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
202 struct perf_session
*session
)
204 struct perf_event_attr
*attr
= &evsel
->attr
;
207 allow_user_set
= perf_header__has_feat(&session
->header
,
210 if (PRINT_FIELD(TRACE
) &&
211 !perf_session__has_traces(session
, "record -R"))
214 if (PRINT_FIELD(IP
)) {
215 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
220 if (PRINT_FIELD(ADDR
) &&
221 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
222 PERF_OUTPUT_ADDR
, allow_user_set
))
225 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
226 pr_err("Display of symbols requested but neither sample IP nor "
227 "sample address\nis selected. Hence, no addresses to convert "
231 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
232 pr_err("Display of offsets requested but symbol is not"
236 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
237 pr_err("Display of DSO requested but neither sample IP nor "
238 "sample address\nis selected. Hence, no addresses to convert "
242 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
243 pr_err("Display of source line number requested but sample IP is not\n"
244 "selected. Hence, no address to lookup the source line number.\n");
248 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
249 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
250 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
253 if (PRINT_FIELD(TIME
) &&
254 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
258 if (PRINT_FIELD(CPU
) &&
259 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
260 PERF_OUTPUT_CPU
, allow_user_set
))
263 if (PRINT_FIELD(PERIOD
) &&
264 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PERIOD
, "PERIOD",
268 if (PRINT_FIELD(IREGS
) &&
269 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS",
276 static void set_print_ip_opts(struct perf_event_attr
*attr
)
278 unsigned int type
= attr
->type
;
280 output
[type
].print_ip_opts
= 0;
282 output
[type
].print_ip_opts
|= PRINT_IP_OPT_IP
;
284 if (PRINT_FIELD(SYM
))
285 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SYM
;
287 if (PRINT_FIELD(DSO
))
288 output
[type
].print_ip_opts
|= PRINT_IP_OPT_DSO
;
290 if (PRINT_FIELD(SYMOFFSET
))
291 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SYMOFFSET
;
293 if (PRINT_FIELD(SRCLINE
))
294 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SRCLINE
;
298 * verify all user requested events exist and the samples
299 * have the expected data
301 static int perf_session__check_output_opt(struct perf_session
*session
)
304 struct perf_evsel
*evsel
;
306 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
307 evsel
= perf_session__find_first_evtype(session
, j
);
310 * even if fields is set to 0 (ie., show nothing) event must
311 * exist if user explicitly includes it on the command line
313 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
314 pr_err("%s events do not exist. "
315 "Remove corresponding -f option to proceed.\n",
320 if (evsel
&& output
[j
].fields
&&
321 perf_evsel__check_attr(evsel
, session
))
327 set_print_ip_opts(&evsel
->attr
);
331 bool use_callchain
= false;
333 evlist__for_each(session
->evlist
, evsel
) {
334 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
335 use_callchain
= true;
340 symbol_conf
.use_callchain
= false;
344 * set default for tracepoints to print symbols only
345 * if callchains are present
347 if (symbol_conf
.use_callchain
&&
348 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
349 struct perf_event_attr
*attr
;
351 j
= PERF_TYPE_TRACEPOINT
;
352 evsel
= perf_session__find_first_evtype(session
, j
);
358 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
359 output
[j
].fields
|= PERF_OUTPUT_IP
;
360 output
[j
].fields
|= PERF_OUTPUT_SYM
;
361 output
[j
].fields
|= PERF_OUTPUT_DSO
;
362 set_print_ip_opts(attr
);
370 static void print_sample_iregs(union perf_event
*event __maybe_unused
,
371 struct perf_sample
*sample
,
372 struct thread
*thread __maybe_unused
,
373 struct perf_event_attr
*attr
)
375 struct regs_dump
*regs
= &sample
->intr_regs
;
376 uint64_t mask
= attr
->sample_regs_intr
;
382 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
383 u64 val
= regs
->regs
[i
++];
384 printf("%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
388 static void print_sample_start(struct perf_sample
*sample
,
389 struct thread
*thread
,
390 struct perf_evsel
*evsel
)
392 struct perf_event_attr
*attr
= &evsel
->attr
;
395 unsigned long long nsecs
;
397 if (PRINT_FIELD(COMM
)) {
399 printf("%8.8s ", thread__comm_str(thread
));
400 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
401 printf("%s ", thread__comm_str(thread
));
403 printf("%16s ", thread__comm_str(thread
));
406 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
407 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
408 else if (PRINT_FIELD(PID
))
409 printf("%5d ", sample
->pid
);
410 else if (PRINT_FIELD(TID
))
411 printf("%5d ", sample
->tid
);
413 if (PRINT_FIELD(CPU
)) {
415 printf("%3d ", sample
->cpu
);
417 printf("[%03d] ", sample
->cpu
);
420 if (PRINT_FIELD(TIME
)) {
421 nsecs
= sample
->time
;
422 secs
= nsecs
/ NSECS_PER_SEC
;
423 nsecs
-= secs
* NSECS_PER_SEC
;
424 usecs
= nsecs
/ NSECS_PER_USEC
;
426 printf("%5lu.%09llu: ", secs
, nsecs
);
428 printf("%5lu.%06lu: ", secs
, usecs
);
433 mispred_str(struct branch_entry
*br
)
435 if (!(br
->flags
.mispred
|| br
->flags
.predicted
))
438 return br
->flags
.predicted
? 'P' : 'M';
441 static void print_sample_brstack(union perf_event
*event __maybe_unused
,
442 struct perf_sample
*sample
,
443 struct thread
*thread __maybe_unused
,
444 struct perf_event_attr
*attr __maybe_unused
)
446 struct branch_stack
*br
= sample
->branch_stack
;
452 for (i
= 0; i
< br
->nr
; i
++) {
453 printf(" 0x%"PRIx64
"/0x%"PRIx64
"/%c/%c/%c/%d ",
456 mispred_str( br
->entries
+ i
),
457 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
458 br
->entries
[i
].flags
.abort
? 'A' : '-',
459 br
->entries
[i
].flags
.cycles
);
463 static void print_sample_brstacksym(union perf_event
*event __maybe_unused
,
464 struct perf_sample
*sample
,
465 struct thread
*thread __maybe_unused
,
466 struct perf_event_attr
*attr __maybe_unused
)
468 struct branch_stack
*br
= sample
->branch_stack
;
469 struct addr_location alf
, alt
;
470 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
476 for (i
= 0; i
< br
->nr
; i
++) {
478 memset(&alf
, 0, sizeof(alf
));
479 memset(&alt
, 0, sizeof(alt
));
480 from
= br
->entries
[i
].from
;
481 to
= br
->entries
[i
].to
;
483 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, from
, &alf
);
485 alf
.sym
= map__find_symbol(alf
.map
, alf
.addr
, NULL
);
487 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, to
, &alt
);
489 alt
.sym
= map__find_symbol(alt
.map
, alt
.addr
, NULL
);
491 symbol__fprintf_symname_offs(alf
.sym
, &alf
, stdout
);
493 symbol__fprintf_symname_offs(alt
.sym
, &alt
, stdout
);
494 printf("/%c/%c/%c/%d ",
495 mispred_str( br
->entries
+ i
),
496 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
497 br
->entries
[i
].flags
.abort
? 'A' : '-',
498 br
->entries
[i
].flags
.cycles
);
503 static void print_sample_addr(union perf_event
*event
,
504 struct perf_sample
*sample
,
505 struct thread
*thread
,
506 struct perf_event_attr
*attr
)
508 struct addr_location al
;
510 printf("%16" PRIx64
, sample
->addr
);
512 if (!sample_addr_correlates_sym(attr
))
515 perf_event__preprocess_sample_addr(event
, sample
, thread
, &al
);
517 if (PRINT_FIELD(SYM
)) {
519 if (PRINT_FIELD(SYMOFFSET
))
520 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
522 symbol__fprintf_symname(al
.sym
, stdout
);
525 if (PRINT_FIELD(DSO
)) {
527 map__fprintf_dsoname(al
.map
, stdout
);
532 static void print_sample_bts(union perf_event
*event
,
533 struct perf_sample
*sample
,
534 struct perf_evsel
*evsel
,
535 struct thread
*thread
,
536 struct addr_location
*al
)
538 struct perf_event_attr
*attr
= &evsel
->attr
;
539 bool print_srcline_last
= false;
541 /* print branch_from information */
542 if (PRINT_FIELD(IP
)) {
543 unsigned int print_opts
= output
[attr
->type
].print_ip_opts
;
545 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
549 if (print_opts
& PRINT_IP_OPT_SRCLINE
) {
550 print_srcline_last
= true;
551 print_opts
&= ~PRINT_IP_OPT_SRCLINE
;
554 perf_evsel__print_ip(evsel
, sample
, al
, print_opts
,
555 scripting_max_stack
);
558 /* print branch_to information */
559 if (PRINT_FIELD(ADDR
) ||
560 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
561 !output
[attr
->type
].user_set
)) {
563 print_sample_addr(event
, sample
, thread
, attr
);
566 if (print_srcline_last
)
567 map__fprintf_srcline(al
->map
, al
->addr
, "\n ", stdout
);
572 static void print_sample_flags(u32 flags
)
574 const char *chars
= PERF_IP_FLAG_CHARS
;
575 const int n
= strlen(PERF_IP_FLAG_CHARS
);
579 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
581 str
[pos
++] = chars
[i
];
583 for (; i
< 32; i
++, flags
>>= 1) {
588 printf(" %-4s ", str
);
591 static void process_event(union perf_event
*event
, struct perf_sample
*sample
,
592 struct perf_evsel
*evsel
, struct addr_location
*al
)
594 struct thread
*thread
= al
->thread
;
595 struct perf_event_attr
*attr
= &evsel
->attr
;
597 if (output
[attr
->type
].fields
== 0)
600 print_sample_start(sample
, thread
, evsel
);
602 if (PRINT_FIELD(PERIOD
))
603 printf("%10" PRIu64
" ", sample
->period
);
605 if (PRINT_FIELD(EVNAME
)) {
606 const char *evname
= perf_evsel__name(evsel
);
607 printf("%s: ", evname
? evname
: "[unknown]");
611 print_sample_flags(sample
->flags
);
613 if (is_bts_event(attr
)) {
614 print_sample_bts(event
, sample
, evsel
, thread
, al
);
618 if (PRINT_FIELD(TRACE
))
619 event_format__print(evsel
->tp_format
, sample
->cpu
,
620 sample
->raw_data
, sample
->raw_size
);
621 if (PRINT_FIELD(ADDR
))
622 print_sample_addr(event
, sample
, thread
, attr
);
624 if (PRINT_FIELD(IP
)) {
625 if (!symbol_conf
.use_callchain
)
630 perf_evsel__print_ip(evsel
, sample
, al
,
631 output
[attr
->type
].print_ip_opts
,
632 scripting_max_stack
);
635 if (PRINT_FIELD(IREGS
))
636 print_sample_iregs(event
, sample
, thread
, attr
);
638 if (PRINT_FIELD(BRSTACK
))
639 print_sample_brstack(event
, sample
, thread
, attr
);
640 else if (PRINT_FIELD(BRSTACKSYM
))
641 print_sample_brstacksym(event
, sample
, thread
, attr
);
646 static int default_start_script(const char *script __maybe_unused
,
647 int argc __maybe_unused
,
648 const char **argv __maybe_unused
)
653 static int default_flush_script(void)
658 static int default_stop_script(void)
663 static int default_generate_script(struct pevent
*pevent __maybe_unused
,
664 const char *outfile __maybe_unused
)
669 static struct scripting_ops default_scripting_ops
= {
670 .start_script
= default_start_script
,
671 .flush_script
= default_flush_script
,
672 .stop_script
= default_stop_script
,
673 .process_event
= process_event
,
674 .generate_script
= default_generate_script
,
677 static struct scripting_ops
*scripting_ops
;
679 static void setup_scripting(void)
681 setup_perl_scripting();
682 setup_python_scripting();
684 scripting_ops
= &default_scripting_ops
;
687 static int flush_scripting(void)
689 return scripting_ops
->flush_script();
692 static int cleanup_scripting(void)
694 pr_debug("\nperf script stopped\n");
696 return scripting_ops
->stop_script();
699 static int process_sample_event(struct perf_tool
*tool __maybe_unused
,
700 union perf_event
*event
,
701 struct perf_sample
*sample
,
702 struct perf_evsel
*evsel
,
703 struct machine
*machine
)
705 struct addr_location al
;
708 if (sample
->time
< last_timestamp
) {
709 pr_err("Samples misordered, previous: %" PRIu64
710 " this: %" PRIu64
"\n", last_timestamp
,
714 last_timestamp
= sample
->time
;
718 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
719 pr_err("problem processing %d event, skipping it.\n",
727 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
730 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
732 addr_location__put(&al
);
737 struct perf_tool tool
;
738 struct perf_session
*session
;
739 bool show_task_events
;
740 bool show_mmap_events
;
741 bool show_switch_events
;
744 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
745 struct perf_evlist
**pevlist
)
747 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
748 struct perf_evlist
*evlist
;
749 struct perf_evsel
*evsel
, *pos
;
752 err
= perf_event__process_attr(tool
, event
, pevlist
);
757 evsel
= perf_evlist__last(*pevlist
);
759 if (evsel
->attr
.type
>= PERF_TYPE_MAX
)
762 evlist__for_each(evlist
, pos
) {
763 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
767 set_print_ip_opts(&evsel
->attr
);
769 if (evsel
->attr
.sample_type
)
770 err
= perf_evsel__check_attr(evsel
, scr
->session
);
775 static int process_comm_event(struct perf_tool
*tool
,
776 union perf_event
*event
,
777 struct perf_sample
*sample
,
778 struct machine
*machine
)
780 struct thread
*thread
;
781 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
782 struct perf_session
*session
= script
->session
;
783 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
786 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
787 if (thread
== NULL
) {
788 pr_debug("problem processing COMM event, skipping it.\n");
792 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
795 if (!evsel
->attr
.sample_id_all
) {
798 sample
->tid
= event
->comm
.tid
;
799 sample
->pid
= event
->comm
.pid
;
801 print_sample_start(sample
, thread
, evsel
);
802 perf_event__fprintf(event
, stdout
);
809 static int process_fork_event(struct perf_tool
*tool
,
810 union perf_event
*event
,
811 struct perf_sample
*sample
,
812 struct machine
*machine
)
814 struct thread
*thread
;
815 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
816 struct perf_session
*session
= script
->session
;
817 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
819 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
822 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
823 if (thread
== NULL
) {
824 pr_debug("problem processing FORK event, skipping it.\n");
828 if (!evsel
->attr
.sample_id_all
) {
830 sample
->time
= event
->fork
.time
;
831 sample
->tid
= event
->fork
.tid
;
832 sample
->pid
= event
->fork
.pid
;
834 print_sample_start(sample
, thread
, evsel
);
835 perf_event__fprintf(event
, stdout
);
840 static int process_exit_event(struct perf_tool
*tool
,
841 union perf_event
*event
,
842 struct perf_sample
*sample
,
843 struct machine
*machine
)
846 struct thread
*thread
;
847 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
848 struct perf_session
*session
= script
->session
;
849 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
851 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
852 if (thread
== NULL
) {
853 pr_debug("problem processing EXIT event, skipping it.\n");
857 if (!evsel
->attr
.sample_id_all
) {
860 sample
->tid
= event
->fork
.tid
;
861 sample
->pid
= event
->fork
.pid
;
863 print_sample_start(sample
, thread
, evsel
);
864 perf_event__fprintf(event
, stdout
);
866 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
873 static int process_mmap_event(struct perf_tool
*tool
,
874 union perf_event
*event
,
875 struct perf_sample
*sample
,
876 struct machine
*machine
)
878 struct thread
*thread
;
879 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
880 struct perf_session
*session
= script
->session
;
881 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
883 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
886 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
887 if (thread
== NULL
) {
888 pr_debug("problem processing MMAP event, skipping it.\n");
892 if (!evsel
->attr
.sample_id_all
) {
895 sample
->tid
= event
->mmap
.tid
;
896 sample
->pid
= event
->mmap
.pid
;
898 print_sample_start(sample
, thread
, evsel
);
899 perf_event__fprintf(event
, stdout
);
904 static int process_mmap2_event(struct perf_tool
*tool
,
905 union perf_event
*event
,
906 struct perf_sample
*sample
,
907 struct machine
*machine
)
909 struct thread
*thread
;
910 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
911 struct perf_session
*session
= script
->session
;
912 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
914 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
917 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
918 if (thread
== NULL
) {
919 pr_debug("problem processing MMAP2 event, skipping it.\n");
923 if (!evsel
->attr
.sample_id_all
) {
926 sample
->tid
= event
->mmap2
.tid
;
927 sample
->pid
= event
->mmap2
.pid
;
929 print_sample_start(sample
, thread
, evsel
);
930 perf_event__fprintf(event
, stdout
);
935 static int process_switch_event(struct perf_tool
*tool
,
936 union perf_event
*event
,
937 struct perf_sample
*sample
,
938 struct machine
*machine
)
940 struct thread
*thread
;
941 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
942 struct perf_session
*session
= script
->session
;
943 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
945 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
948 thread
= machine__findnew_thread(machine
, sample
->pid
,
950 if (thread
== NULL
) {
951 pr_debug("problem processing SWITCH event, skipping it.\n");
955 print_sample_start(sample
, thread
, evsel
);
956 perf_event__fprintf(event
, stdout
);
961 static void sig_handler(int sig __maybe_unused
)
966 static int __cmd_script(struct perf_script
*script
)
970 signal(SIGINT
, sig_handler
);
972 /* override event processing functions */
973 if (script
->show_task_events
) {
974 script
->tool
.comm
= process_comm_event
;
975 script
->tool
.fork
= process_fork_event
;
976 script
->tool
.exit
= process_exit_event
;
978 if (script
->show_mmap_events
) {
979 script
->tool
.mmap
= process_mmap_event
;
980 script
->tool
.mmap2
= process_mmap2_event
;
982 if (script
->show_switch_events
)
983 script
->tool
.context_switch
= process_switch_event
;
985 ret
= perf_session__process_events(script
->session
);
988 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
994 struct list_head node
;
995 struct scripting_ops
*ops
;
999 static LIST_HEAD(script_specs
);
1001 static struct script_spec
*script_spec__new(const char *spec
,
1002 struct scripting_ops
*ops
)
1004 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
1007 strcpy(s
->spec
, spec
);
1014 static void script_spec__add(struct script_spec
*s
)
1016 list_add_tail(&s
->node
, &script_specs
);
1019 static struct script_spec
*script_spec__find(const char *spec
)
1021 struct script_spec
*s
;
1023 list_for_each_entry(s
, &script_specs
, node
)
1024 if (strcasecmp(s
->spec
, spec
) == 0)
1029 static struct script_spec
*script_spec__findnew(const char *spec
,
1030 struct scripting_ops
*ops
)
1032 struct script_spec
*s
= script_spec__find(spec
);
1037 s
= script_spec__new(spec
, ops
);
1041 script_spec__add(s
);
1046 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
1048 struct script_spec
*s
;
1050 s
= script_spec__find(spec
);
1054 s
= script_spec__findnew(spec
, ops
);
1061 static struct scripting_ops
*script_spec__lookup(const char *spec
)
1063 struct script_spec
*s
= script_spec__find(spec
);
1070 static void list_available_languages(void)
1072 struct script_spec
*s
;
1074 fprintf(stderr
, "\n");
1075 fprintf(stderr
, "Scripting language extensions (used in "
1076 "perf script -s [spec:]script.[spec]):\n\n");
1078 list_for_each_entry(s
, &script_specs
, node
)
1079 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
1081 fprintf(stderr
, "\n");
1084 static int parse_scriptname(const struct option
*opt __maybe_unused
,
1085 const char *str
, int unset __maybe_unused
)
1087 char spec
[PATH_MAX
];
1088 const char *script
, *ext
;
1091 if (strcmp(str
, "lang") == 0) {
1092 list_available_languages();
1096 script
= strchr(str
, ':');
1099 if (len
>= PATH_MAX
) {
1100 fprintf(stderr
, "invalid language specifier");
1103 strncpy(spec
, str
, len
);
1105 scripting_ops
= script_spec__lookup(spec
);
1106 if (!scripting_ops
) {
1107 fprintf(stderr
, "invalid language specifier");
1113 ext
= strrchr(script
, '.');
1115 fprintf(stderr
, "invalid script extension");
1118 scripting_ops
= script_spec__lookup(++ext
);
1119 if (!scripting_ops
) {
1120 fprintf(stderr
, "invalid script extension");
1125 script_name
= strdup(script
);
1130 static int parse_output_fields(const struct option
*opt __maybe_unused
,
1131 const char *arg
, int unset __maybe_unused
)
1134 int i
, imax
= ARRAY_SIZE(all_output_options
);
1137 char *str
= strdup(arg
);
1143 /* first word can state for which event type the user is specifying
1144 * the fields. If no type exists, the specified fields apply to all
1145 * event types found in the file minus the invalid fields for a type.
1147 tok
= strchr(str
, ':');
1151 if (!strcmp(str
, "hw"))
1152 type
= PERF_TYPE_HARDWARE
;
1153 else if (!strcmp(str
, "sw"))
1154 type
= PERF_TYPE_SOFTWARE
;
1155 else if (!strcmp(str
, "trace"))
1156 type
= PERF_TYPE_TRACEPOINT
;
1157 else if (!strcmp(str
, "raw"))
1158 type
= PERF_TYPE_RAW
;
1160 fprintf(stderr
, "Invalid event type in field string.\n");
1165 if (output
[type
].user_set
)
1166 pr_warning("Overriding previous field request for %s events.\n",
1169 output
[type
].fields
= 0;
1170 output
[type
].user_set
= true;
1171 output
[type
].wildcard_set
= false;
1175 if (strlen(str
) == 0) {
1177 "Cannot set fields to 'none' for all event types.\n");
1182 if (output_set_by_user())
1183 pr_warning("Overriding previous field request for all events.\n");
1185 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1186 output
[j
].fields
= 0;
1187 output
[j
].user_set
= true;
1188 output
[j
].wildcard_set
= true;
1192 for (tok
= strtok(tok
, ","); tok
; tok
= strtok(NULL
, ",")) {
1193 for (i
= 0; i
< imax
; ++i
) {
1194 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
1197 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
1202 fprintf(stderr
, "Invalid field requested.\n");
1208 /* add user option to all events types for
1211 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1212 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
1213 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1214 all_output_options
[i
].str
, event_type(j
));
1216 output
[j
].fields
|= all_output_options
[i
].field
;
1219 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
1220 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
1221 all_output_options
[i
].str
, event_type(type
));
1226 output
[type
].fields
|= all_output_options
[i
].field
;
1231 if (output
[type
].fields
== 0) {
1232 pr_debug("No fields requested for %s type. "
1233 "Events will not be displayed.\n", event_type(type
));
1242 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1243 static int is_directory(const char *base_path
, const struct dirent
*dent
)
1245 char path
[PATH_MAX
];
1248 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
1249 if (stat(path
, &st
))
1252 return S_ISDIR(st
.st_mode
);
1255 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1256 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1257 if ((lang_dirent->d_type == DT_DIR || \
1258 (lang_dirent->d_type == DT_UNKNOWN && \
1259 is_directory(scripts_path, lang_dirent))) && \
1260 (strcmp(lang_dirent->d_name, ".")) && \
1261 (strcmp(lang_dirent->d_name, "..")))
1263 #define for_each_script(lang_path, lang_dir, script_dirent) \
1264 while ((script_dirent = readdir(lang_dir)) != NULL) \
1265 if (script_dirent->d_type != DT_DIR && \
1266 (script_dirent->d_type != DT_UNKNOWN || \
1267 !is_directory(lang_path, script_dirent)))
1270 #define RECORD_SUFFIX "-record"
1271 #define REPORT_SUFFIX "-report"
1273 struct script_desc
{
1274 struct list_head node
;
1280 static LIST_HEAD(script_descs
);
1282 static struct script_desc
*script_desc__new(const char *name
)
1284 struct script_desc
*s
= zalloc(sizeof(*s
));
1286 if (s
!= NULL
&& name
)
1287 s
->name
= strdup(name
);
1292 static void script_desc__delete(struct script_desc
*s
)
1295 zfree(&s
->half_liner
);
1300 static void script_desc__add(struct script_desc
*s
)
1302 list_add_tail(&s
->node
, &script_descs
);
1305 static struct script_desc
*script_desc__find(const char *name
)
1307 struct script_desc
*s
;
1309 list_for_each_entry(s
, &script_descs
, node
)
1310 if (strcasecmp(s
->name
, name
) == 0)
1315 static struct script_desc
*script_desc__findnew(const char *name
)
1317 struct script_desc
*s
= script_desc__find(name
);
1322 s
= script_desc__new(name
);
1324 goto out_delete_desc
;
1326 script_desc__add(s
);
1331 script_desc__delete(s
);
1336 static const char *ends_with(const char *str
, const char *suffix
)
1338 size_t suffix_len
= strlen(suffix
);
1339 const char *p
= str
;
1341 if (strlen(str
) > suffix_len
) {
1342 p
= str
+ strlen(str
) - suffix_len
;
1343 if (!strncmp(p
, suffix
, suffix_len
))
1350 static int read_script_info(struct script_desc
*desc
, const char *filename
)
1352 char line
[BUFSIZ
], *p
;
1355 fp
= fopen(filename
, "r");
1359 while (fgets(line
, sizeof(line
), fp
)) {
1366 if (strlen(p
) && *p
== '!')
1370 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
1371 p
[strlen(p
) - 1] = '\0';
1373 if (!strncmp(p
, "description:", strlen("description:"))) {
1374 p
+= strlen("description:");
1375 desc
->half_liner
= strdup(ltrim(p
));
1379 if (!strncmp(p
, "args:", strlen("args:"))) {
1380 p
+= strlen("args:");
1381 desc
->args
= strdup(ltrim(p
));
1391 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
1393 char *script_root
, *str
;
1395 script_root
= strdup(script_dirent
->d_name
);
1399 str
= (char *)ends_with(script_root
, suffix
);
1409 static int list_available_scripts(const struct option
*opt __maybe_unused
,
1410 const char *s __maybe_unused
,
1411 int unset __maybe_unused
)
1413 struct dirent
*script_dirent
, *lang_dirent
;
1414 char scripts_path
[MAXPATHLEN
];
1415 DIR *scripts_dir
, *lang_dir
;
1416 char script_path
[MAXPATHLEN
];
1417 char lang_path
[MAXPATHLEN
];
1418 struct script_desc
*desc
;
1419 char first_half
[BUFSIZ
];
1422 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1424 scripts_dir
= opendir(scripts_path
);
1428 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1429 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1430 lang_dirent
->d_name
);
1431 lang_dir
= opendir(lang_path
);
1435 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1436 script_root
= get_script_root(script_dirent
, REPORT_SUFFIX
);
1438 desc
= script_desc__findnew(script_root
);
1439 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1440 lang_path
, script_dirent
->d_name
);
1441 read_script_info(desc
, script_path
);
1447 fprintf(stdout
, "List of available trace scripts:\n");
1448 list_for_each_entry(desc
, &script_descs
, node
) {
1449 sprintf(first_half
, "%s %s", desc
->name
,
1450 desc
->args
? desc
->args
: "");
1451 fprintf(stdout
, " %-36s %s\n", first_half
,
1452 desc
->half_liner
? desc
->half_liner
: "");
1459 * Some scripts specify the required events in their "xxx-record" file,
1460 * this function will check if the events in perf.data match those
1461 * mentioned in the "xxx-record".
1463 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1464 * which is covered well now. And new parsing code should be added to
1465 * cover the future complexing formats like event groups etc.
1467 static int check_ev_match(char *dir_name
, char *scriptname
,
1468 struct perf_session
*session
)
1470 char filename
[MAXPATHLEN
], evname
[128];
1471 char line
[BUFSIZ
], *p
;
1472 struct perf_evsel
*pos
;
1476 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
1478 fp
= fopen(filename
, "r");
1482 while (fgets(line
, sizeof(line
), fp
)) {
1488 p
= strstr(p
, "-e");
1494 len
= strcspn(p
, " \t");
1498 snprintf(evname
, len
+ 1, "%s", p
);
1501 evlist__for_each(session
->evlist
, pos
) {
1502 if (!strcmp(perf_evsel__name(pos
), evname
)) {
1520 * Return -1 if none is found, otherwise the actual scripts number.
1522 * Currently the only user of this function is the script browser, which
1523 * will list all statically runnable scripts, select one, execute it and
1524 * show the output in a perf browser.
1526 int find_scripts(char **scripts_array
, char **scripts_path_array
)
1528 struct dirent
*script_dirent
, *lang_dirent
;
1529 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
1530 DIR *scripts_dir
, *lang_dir
;
1531 struct perf_session
*session
;
1532 struct perf_data_file file
= {
1534 .mode
= PERF_DATA_MODE_READ
,
1539 session
= perf_session__new(&file
, false, NULL
);
1543 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1545 scripts_dir
= opendir(scripts_path
);
1547 perf_session__delete(session
);
1551 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1552 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
1553 lang_dirent
->d_name
);
1555 if (strstr(lang_path
, "perl"))
1559 if (strstr(lang_path
, "python"))
1563 lang_dir
= opendir(lang_path
);
1567 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1568 /* Skip those real time scripts: xxxtop.p[yl] */
1569 if (strstr(script_dirent
->d_name
, "top."))
1571 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
1572 script_dirent
->d_name
);
1573 temp
= strchr(script_dirent
->d_name
, '.');
1574 snprintf(scripts_array
[i
],
1575 (temp
- script_dirent
->d_name
) + 1,
1576 "%s", script_dirent
->d_name
);
1578 if (check_ev_match(lang_path
,
1579 scripts_array
[i
], session
))
1587 closedir(scripts_dir
);
1588 perf_session__delete(session
);
1592 static char *get_script_path(const char *script_root
, const char *suffix
)
1594 struct dirent
*script_dirent
, *lang_dirent
;
1595 char scripts_path
[MAXPATHLEN
];
1596 char script_path
[MAXPATHLEN
];
1597 DIR *scripts_dir
, *lang_dir
;
1598 char lang_path
[MAXPATHLEN
];
1599 char *__script_root
;
1601 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1603 scripts_dir
= opendir(scripts_path
);
1607 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1608 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1609 lang_dirent
->d_name
);
1610 lang_dir
= opendir(lang_path
);
1614 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1615 __script_root
= get_script_root(script_dirent
, suffix
);
1616 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
1617 free(__script_root
);
1619 closedir(scripts_dir
);
1620 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1621 lang_path
, script_dirent
->d_name
);
1622 return strdup(script_path
);
1624 free(__script_root
);
1628 closedir(scripts_dir
);
1633 static bool is_top_script(const char *script_path
)
1635 return ends_with(script_path
, "top") == NULL
? false : true;
1638 static int has_required_arg(char *script_path
)
1640 struct script_desc
*desc
;
1644 desc
= script_desc__new(NULL
);
1646 if (read_script_info(desc
, script_path
))
1652 for (p
= desc
->args
; *p
; p
++)
1656 script_desc__delete(desc
);
1661 static int have_cmd(int argc
, const char **argv
)
1663 char **__argv
= malloc(sizeof(const char *) * argc
);
1666 pr_err("malloc failed\n");
1670 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1671 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1672 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1675 system_wide
= (argc
== 0);
1680 static void script__setup_sample_type(struct perf_script
*script
)
1682 struct perf_session
*session
= script
->session
;
1683 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
1685 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
1686 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
1687 (sample_type
& PERF_SAMPLE_STACK_USER
))
1688 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
1689 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
1690 callchain_param
.record_mode
= CALLCHAIN_LBR
;
1692 callchain_param
.record_mode
= CALLCHAIN_FP
;
1696 int cmd_script(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1698 bool show_full_info
= false;
1699 bool header
= false;
1700 bool header_only
= false;
1701 bool script_started
= false;
1702 char *rec_script_path
= NULL
;
1703 char *rep_script_path
= NULL
;
1704 struct perf_session
*session
;
1705 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
1706 char *script_path
= NULL
;
1707 const char **__argv
;
1709 struct perf_script script
= {
1711 .sample
= process_sample_event
,
1712 .mmap
= perf_event__process_mmap
,
1713 .mmap2
= perf_event__process_mmap2
,
1714 .comm
= perf_event__process_comm
,
1715 .exit
= perf_event__process_exit
,
1716 .fork
= perf_event__process_fork
,
1717 .attr
= process_attr
,
1718 .tracing_data
= perf_event__process_tracing_data
,
1719 .build_id
= perf_event__process_build_id
,
1720 .id_index
= perf_event__process_id_index
,
1721 .auxtrace_info
= perf_event__process_auxtrace_info
,
1722 .auxtrace
= perf_event__process_auxtrace
,
1723 .auxtrace_error
= perf_event__process_auxtrace_error
,
1724 .ordered_events
= true,
1725 .ordering_requires_timestamps
= true,
1728 struct perf_data_file file
= {
1729 .mode
= PERF_DATA_MODE_READ
,
1731 const struct option options
[] = {
1732 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1733 "dump raw trace in ASCII"),
1734 OPT_INCR('v', "verbose", &verbose
,
1735 "be more verbose (show symbol address, etc)"),
1736 OPT_BOOLEAN('L', "Latency", &latency_format
,
1737 "show latency attributes (irqs/preemption disabled, etc)"),
1738 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
1739 list_available_scripts
),
1740 OPT_CALLBACK('s', "script", NULL
, "name",
1741 "script file name (lang:script name, script name, or *)",
1743 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
1744 "generate perf-script.xx script in specified language"),
1745 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
1746 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
1747 "do various checks like samples ordering and lost events"),
1748 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
1749 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
1750 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
1751 "file", "vmlinux pathname"),
1752 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
1753 "file", "kallsyms pathname"),
1754 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
1755 "When printing symbols do not display call chain"),
1756 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1757 "Look for files with symbols relative to this directory"),
1758 OPT_CALLBACK('F', "fields", NULL
, "str",
1759 "comma separated output fields prepend with 'type:'. "
1760 "Valid types: hw,sw,trace,raw. "
1761 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1762 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields
),
1763 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
1764 "system-wide collection from all CPUs"),
1765 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
1766 "only consider these symbols"),
1767 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
1768 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
1769 "only display events for these comms"),
1770 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
1771 "only consider symbols in these pids"),
1772 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
1773 "only consider symbols in these tids"),
1774 OPT_BOOLEAN('I', "show-info", &show_full_info
,
1775 "display extended information from perf.data file"),
1776 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
1777 "Show the path of [kernel.kallsyms]"),
1778 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
1779 "Show the fork/comm/exit events"),
1780 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
1781 "Show the mmap events"),
1782 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
1783 "Show context switch events (if recorded)"),
1784 OPT_BOOLEAN('f', "force", &file
.force
, "don't complain, do it"),
1785 OPT_BOOLEAN(0, "ns", &nanosecs
,
1786 "Use 9 decimal places when displaying time"),
1787 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
1788 "Instruction Tracing options",
1789 itrace_parse_synth_opts
),
1790 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
1791 "Show full source file name path for source lines"),
1792 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
1793 "Enable symbol demangling"),
1794 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
1795 "Enable kernel symbol demangling"),
1799 const char * const script_subcommands
[] = { "record", "report", NULL
};
1800 const char *script_usage
[] = {
1801 "perf script [<options>]",
1802 "perf script [<options>] record <script> [<record-options>] <command>",
1803 "perf script [<options>] report <script> [script-args]",
1804 "perf script [<options>] <script> [<record-options>] <command>",
1805 "perf script [<options>] <top-script> [script-args]",
1811 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
1812 PARSE_OPT_STOP_AT_NON_OPTION
);
1814 file
.path
= input_name
;
1816 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
1817 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
1818 if (!rec_script_path
)
1819 return cmd_record(argc
, argv
, NULL
);
1822 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
1823 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
1824 if (!rep_script_path
) {
1826 "Please specify a valid report script"
1827 "(see 'perf script -l' for listing)\n");
1832 if (itrace_synth_opts
.callchain
&&
1833 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
1834 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
1836 /* make sure PERF_EXEC_PATH is set for scripts */
1837 perf_set_argv_exec_path(perf_exec_path());
1839 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
1844 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
1845 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
1847 if (!rec_script_path
&& !rep_script_path
) {
1848 usage_with_options_msg(script_usage
, options
,
1849 "Couldn't find script `%s'\n\n See perf"
1850 " script -l for available scripts.\n", argv
[0]);
1853 if (is_top_script(argv
[0])) {
1854 rep_args
= argc
- 1;
1858 rep_args
= has_required_arg(rep_script_path
);
1859 rec_args
= (argc
- 1) - rep_args
;
1861 usage_with_options_msg(script_usage
, options
,
1862 "`%s' script requires options."
1863 "\n\n See perf script -l for available "
1864 "scripts and options.\n", argv
[0]);
1868 if (pipe(live_pipe
) < 0) {
1869 perror("failed to create pipe");
1875 perror("failed to fork");
1882 dup2(live_pipe
[1], 1);
1883 close(live_pipe
[0]);
1885 if (is_top_script(argv
[0])) {
1887 } else if (!system_wide
) {
1888 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
1894 __argv
= malloc((argc
+ 6) * sizeof(const char *));
1896 pr_err("malloc failed\n");
1901 __argv
[j
++] = "/bin/sh";
1902 __argv
[j
++] = rec_script_path
;
1908 for (i
= rep_args
+ 1; i
< argc
; i
++)
1909 __argv
[j
++] = argv
[i
];
1912 execvp("/bin/sh", (char **)__argv
);
1917 dup2(live_pipe
[0], 0);
1918 close(live_pipe
[1]);
1920 __argv
= malloc((argc
+ 4) * sizeof(const char *));
1922 pr_err("malloc failed\n");
1928 __argv
[j
++] = "/bin/sh";
1929 __argv
[j
++] = rep_script_path
;
1930 for (i
= 1; i
< rep_args
+ 1; i
++)
1931 __argv
[j
++] = argv
[i
];
1936 execvp("/bin/sh", (char **)__argv
);
1941 if (rec_script_path
)
1942 script_path
= rec_script_path
;
1943 if (rep_script_path
)
1944 script_path
= rep_script_path
;
1949 if (!rec_script_path
)
1950 system_wide
= false;
1951 else if (!system_wide
) {
1952 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
1958 __argv
= malloc((argc
+ 2) * sizeof(const char *));
1960 pr_err("malloc failed\n");
1965 __argv
[j
++] = "/bin/sh";
1966 __argv
[j
++] = script_path
;
1969 for (i
= 2; i
< argc
; i
++)
1970 __argv
[j
++] = argv
[i
];
1973 execvp("/bin/sh", (char **)__argv
);
1981 session
= perf_session__new(&file
, false, &script
.tool
);
1982 if (session
== NULL
)
1985 if (header
|| header_only
) {
1986 perf_session__fprintf_info(session
, stdout
, show_full_info
);
1991 if (symbol__init(&session
->header
.env
) < 0)
1994 script
.session
= session
;
1995 script__setup_sample_type(&script
);
1997 session
->itrace_synth_opts
= &itrace_synth_opts
;
2000 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
2006 symbol_conf
.use_callchain
= true;
2008 symbol_conf
.use_callchain
= false;
2010 if (session
->tevent
.pevent
&&
2011 pevent_set_function_resolver(session
->tevent
.pevent
,
2012 machine__resolve_kernel_addr
,
2013 &session
->machines
.host
) < 0) {
2014 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
2018 if (generate_script_lang
) {
2019 struct stat perf_stat
;
2022 if (output_set_by_user()) {
2024 "custom fields not supported for generated scripts");
2029 input
= open(file
.path
, O_RDONLY
); /* input_name */
2032 perror("failed to open file");
2036 err
= fstat(input
, &perf_stat
);
2038 perror("failed to stat file");
2042 if (!perf_stat
.st_size
) {
2043 fprintf(stderr
, "zero-sized file, nothing to do!\n");
2047 scripting_ops
= script_spec__lookup(generate_script_lang
);
2048 if (!scripting_ops
) {
2049 fprintf(stderr
, "invalid language specifier");
2054 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
2060 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
2063 pr_debug("perf script started with script %s\n\n", script_name
);
2064 script_started
= true;
2068 err
= perf_session__check_output_opt(session
);
2072 err
= __cmd_script(&script
);
2077 perf_session__delete(session
);
2080 cleanup_scripting();