4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include <subcmd/exec-cmd.h>
7 #include "util/header.h"
8 #include <subcmd/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 "util/cpumap.h"
22 #include "util/thread_map.h"
23 #include "util/stat.h"
24 #include <linux/bitmap.h>
27 static char const *script_name
;
28 static char const *generate_script_lang
;
29 static bool debug_mode
;
30 static u64 last_timestamp
;
31 static u64 nr_unordered
;
32 static bool no_callchain
;
33 static bool latency_format
;
34 static bool system_wide
;
35 static bool print_flags
;
37 static const char *cpu_list
;
38 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
39 static struct perf_stat_config stat_config
;
41 unsigned int scripting_max_stack
= PERF_MAX_STACK_DEPTH
;
43 enum perf_output_field
{
44 PERF_OUTPUT_COMM
= 1U << 0,
45 PERF_OUTPUT_TID
= 1U << 1,
46 PERF_OUTPUT_PID
= 1U << 2,
47 PERF_OUTPUT_TIME
= 1U << 3,
48 PERF_OUTPUT_CPU
= 1U << 4,
49 PERF_OUTPUT_EVNAME
= 1U << 5,
50 PERF_OUTPUT_TRACE
= 1U << 6,
51 PERF_OUTPUT_IP
= 1U << 7,
52 PERF_OUTPUT_SYM
= 1U << 8,
53 PERF_OUTPUT_DSO
= 1U << 9,
54 PERF_OUTPUT_ADDR
= 1U << 10,
55 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
56 PERF_OUTPUT_SRCLINE
= 1U << 12,
57 PERF_OUTPUT_PERIOD
= 1U << 13,
58 PERF_OUTPUT_IREGS
= 1U << 14,
59 PERF_OUTPUT_BRSTACK
= 1U << 15,
60 PERF_OUTPUT_BRSTACKSYM
= 1U << 16,
63 struct output_option
{
65 enum perf_output_field field
;
66 } all_output_options
[] = {
67 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
68 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
69 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
70 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
71 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
72 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
73 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
74 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
75 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
76 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
77 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
78 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
79 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
80 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
81 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
82 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
83 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
86 /* default set to maintain compatibility with current format */
90 unsigned int print_ip_opts
;
93 } output
[PERF_TYPE_MAX
] = {
95 [PERF_TYPE_HARDWARE
] = {
98 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
99 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
100 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
101 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
104 .invalid_fields
= PERF_OUTPUT_TRACE
,
107 [PERF_TYPE_SOFTWARE
] = {
110 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
111 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
112 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
113 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
116 .invalid_fields
= PERF_OUTPUT_TRACE
,
119 [PERF_TYPE_TRACEPOINT
] = {
122 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
123 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
124 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
,
130 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
131 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
132 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
133 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
136 .invalid_fields
= PERF_OUTPUT_TRACE
,
139 [PERF_TYPE_BREAKPOINT
] = {
142 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
143 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
144 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
145 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
148 .invalid_fields
= PERF_OUTPUT_TRACE
,
152 static bool output_set_by_user(void)
155 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
156 if (output
[j
].user_set
)
162 static const char *output_field2str(enum perf_output_field field
)
164 int i
, imax
= ARRAY_SIZE(all_output_options
);
165 const char *str
= "";
167 for (i
= 0; i
< imax
; ++i
) {
168 if (all_output_options
[i
].field
== field
) {
169 str
= all_output_options
[i
].str
;
176 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
178 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
179 u64 sample_type
, const char *sample_msg
,
180 enum perf_output_field field
,
183 struct perf_event_attr
*attr
= &evsel
->attr
;
184 int type
= attr
->type
;
187 if (attr
->sample_type
& sample_type
)
190 if (output
[type
].user_set
) {
193 evname
= perf_evsel__name(evsel
);
194 pr_err("Samples for '%s' event do not have %s attribute set. "
195 "Cannot print '%s' field.\n",
196 evname
, sample_msg
, output_field2str(field
));
200 /* user did not ask for it explicitly so remove from the default list */
201 output
[type
].fields
&= ~field
;
202 evname
= perf_evsel__name(evsel
);
203 pr_debug("Samples for '%s' event do not have %s attribute set. "
204 "Skipping '%s' field.\n",
205 evname
, sample_msg
, output_field2str(field
));
210 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
211 u64 sample_type
, const char *sample_msg
,
212 enum perf_output_field field
)
214 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
218 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
219 struct perf_session
*session
)
221 struct perf_event_attr
*attr
= &evsel
->attr
;
224 allow_user_set
= perf_header__has_feat(&session
->header
,
227 if (PRINT_FIELD(TRACE
) &&
228 !perf_session__has_traces(session
, "record -R"))
231 if (PRINT_FIELD(IP
)) {
232 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
237 if (PRINT_FIELD(ADDR
) &&
238 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
239 PERF_OUTPUT_ADDR
, allow_user_set
))
242 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
243 pr_err("Display of symbols requested but neither sample IP nor "
244 "sample address\nis selected. Hence, no addresses to convert "
248 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
249 pr_err("Display of offsets requested but symbol is not"
253 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
254 pr_err("Display of DSO requested but neither sample IP nor "
255 "sample address\nis selected. Hence, no addresses to convert "
259 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
260 pr_err("Display of source line number requested but sample IP is not\n"
261 "selected. Hence, no address to lookup the source line number.\n");
265 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
266 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
267 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
270 if (PRINT_FIELD(TIME
) &&
271 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
275 if (PRINT_FIELD(CPU
) &&
276 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
277 PERF_OUTPUT_CPU
, allow_user_set
))
280 if (PRINT_FIELD(PERIOD
) &&
281 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PERIOD
, "PERIOD",
285 if (PRINT_FIELD(IREGS
) &&
286 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS",
293 static void set_print_ip_opts(struct perf_event_attr
*attr
)
295 unsigned int type
= attr
->type
;
297 output
[type
].print_ip_opts
= 0;
299 output
[type
].print_ip_opts
|= PRINT_IP_OPT_IP
;
301 if (PRINT_FIELD(SYM
))
302 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SYM
;
304 if (PRINT_FIELD(DSO
))
305 output
[type
].print_ip_opts
|= PRINT_IP_OPT_DSO
;
307 if (PRINT_FIELD(SYMOFFSET
))
308 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SYMOFFSET
;
310 if (PRINT_FIELD(SRCLINE
))
311 output
[type
].print_ip_opts
|= PRINT_IP_OPT_SRCLINE
;
315 * verify all user requested events exist and the samples
316 * have the expected data
318 static int perf_session__check_output_opt(struct perf_session
*session
)
321 struct perf_evsel
*evsel
;
323 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
324 evsel
= perf_session__find_first_evtype(session
, j
);
327 * even if fields is set to 0 (ie., show nothing) event must
328 * exist if user explicitly includes it on the command line
330 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
331 pr_err("%s events do not exist. "
332 "Remove corresponding -f option to proceed.\n",
337 if (evsel
&& output
[j
].fields
&&
338 perf_evsel__check_attr(evsel
, session
))
344 set_print_ip_opts(&evsel
->attr
);
348 bool use_callchain
= false;
350 evlist__for_each(session
->evlist
, evsel
) {
351 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
352 use_callchain
= true;
357 symbol_conf
.use_callchain
= false;
361 * set default for tracepoints to print symbols only
362 * if callchains are present
364 if (symbol_conf
.use_callchain
&&
365 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
366 struct perf_event_attr
*attr
;
368 j
= PERF_TYPE_TRACEPOINT
;
369 evsel
= perf_session__find_first_evtype(session
, j
);
375 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
376 output
[j
].fields
|= PERF_OUTPUT_IP
;
377 output
[j
].fields
|= PERF_OUTPUT_SYM
;
378 output
[j
].fields
|= PERF_OUTPUT_DSO
;
379 set_print_ip_opts(attr
);
387 static void print_sample_iregs(union perf_event
*event __maybe_unused
,
388 struct perf_sample
*sample
,
389 struct thread
*thread __maybe_unused
,
390 struct perf_event_attr
*attr
)
392 struct regs_dump
*regs
= &sample
->intr_regs
;
393 uint64_t mask
= attr
->sample_regs_intr
;
399 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
400 u64 val
= regs
->regs
[i
++];
401 printf("%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
405 static void print_sample_start(struct perf_sample
*sample
,
406 struct thread
*thread
,
407 struct perf_evsel
*evsel
)
409 struct perf_event_attr
*attr
= &evsel
->attr
;
412 unsigned long long nsecs
;
414 if (PRINT_FIELD(COMM
)) {
416 printf("%8.8s ", thread__comm_str(thread
));
417 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
418 printf("%s ", thread__comm_str(thread
));
420 printf("%16s ", thread__comm_str(thread
));
423 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
424 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
425 else if (PRINT_FIELD(PID
))
426 printf("%5d ", sample
->pid
);
427 else if (PRINT_FIELD(TID
))
428 printf("%5d ", sample
->tid
);
430 if (PRINT_FIELD(CPU
)) {
432 printf("%3d ", sample
->cpu
);
434 printf("[%03d] ", sample
->cpu
);
437 if (PRINT_FIELD(TIME
)) {
438 nsecs
= sample
->time
;
439 secs
= nsecs
/ NSECS_PER_SEC
;
440 nsecs
-= secs
* NSECS_PER_SEC
;
441 usecs
= nsecs
/ NSECS_PER_USEC
;
443 printf("%5lu.%09llu: ", secs
, nsecs
);
445 printf("%5lu.%06lu: ", secs
, usecs
);
450 mispred_str(struct branch_entry
*br
)
452 if (!(br
->flags
.mispred
|| br
->flags
.predicted
))
455 return br
->flags
.predicted
? 'P' : 'M';
458 static void print_sample_brstack(union perf_event
*event __maybe_unused
,
459 struct perf_sample
*sample
,
460 struct thread
*thread __maybe_unused
,
461 struct perf_event_attr
*attr __maybe_unused
)
463 struct branch_stack
*br
= sample
->branch_stack
;
469 for (i
= 0; i
< br
->nr
; i
++) {
470 printf(" 0x%"PRIx64
"/0x%"PRIx64
"/%c/%c/%c/%d ",
473 mispred_str( br
->entries
+ i
),
474 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
475 br
->entries
[i
].flags
.abort
? 'A' : '-',
476 br
->entries
[i
].flags
.cycles
);
480 static void print_sample_brstacksym(union perf_event
*event __maybe_unused
,
481 struct perf_sample
*sample
,
482 struct thread
*thread __maybe_unused
,
483 struct perf_event_attr
*attr __maybe_unused
)
485 struct branch_stack
*br
= sample
->branch_stack
;
486 struct addr_location alf
, alt
;
487 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
493 for (i
= 0; i
< br
->nr
; i
++) {
495 memset(&alf
, 0, sizeof(alf
));
496 memset(&alt
, 0, sizeof(alt
));
497 from
= br
->entries
[i
].from
;
498 to
= br
->entries
[i
].to
;
500 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, from
, &alf
);
502 alf
.sym
= map__find_symbol(alf
.map
, alf
.addr
, NULL
);
504 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, to
, &alt
);
506 alt
.sym
= map__find_symbol(alt
.map
, alt
.addr
, NULL
);
508 symbol__fprintf_symname_offs(alf
.sym
, &alf
, stdout
);
510 symbol__fprintf_symname_offs(alt
.sym
, &alt
, stdout
);
511 printf("/%c/%c/%c/%d ",
512 mispred_str( br
->entries
+ i
),
513 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
514 br
->entries
[i
].flags
.abort
? 'A' : '-',
515 br
->entries
[i
].flags
.cycles
);
520 static void print_sample_addr(union perf_event
*event
,
521 struct perf_sample
*sample
,
522 struct thread
*thread
,
523 struct perf_event_attr
*attr
)
525 struct addr_location al
;
527 printf("%16" PRIx64
, sample
->addr
);
529 if (!sample_addr_correlates_sym(attr
))
532 perf_event__preprocess_sample_addr(event
, sample
, thread
, &al
);
534 if (PRINT_FIELD(SYM
)) {
536 if (PRINT_FIELD(SYMOFFSET
))
537 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
539 symbol__fprintf_symname(al
.sym
, stdout
);
542 if (PRINT_FIELD(DSO
)) {
544 map__fprintf_dsoname(al
.map
, stdout
);
549 static void print_sample_bts(union perf_event
*event
,
550 struct perf_sample
*sample
,
551 struct perf_evsel
*evsel
,
552 struct thread
*thread
,
553 struct addr_location
*al
)
555 struct perf_event_attr
*attr
= &evsel
->attr
;
556 bool print_srcline_last
= false;
558 /* print branch_from information */
559 if (PRINT_FIELD(IP
)) {
560 unsigned int print_opts
= output
[attr
->type
].print_ip_opts
;
562 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
566 if (print_opts
& PRINT_IP_OPT_SRCLINE
) {
567 print_srcline_last
= true;
568 print_opts
&= ~PRINT_IP_OPT_SRCLINE
;
571 perf_evsel__print_ip(evsel
, sample
, al
, print_opts
,
572 scripting_max_stack
);
575 /* print branch_to information */
576 if (PRINT_FIELD(ADDR
) ||
577 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
578 !output
[attr
->type
].user_set
)) {
580 print_sample_addr(event
, sample
, thread
, attr
);
583 if (print_srcline_last
)
584 map__fprintf_srcline(al
->map
, al
->addr
, "\n ", stdout
);
589 static void print_sample_flags(u32 flags
)
591 const char *chars
= PERF_IP_FLAG_CHARS
;
592 const int n
= strlen(PERF_IP_FLAG_CHARS
);
596 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
598 str
[pos
++] = chars
[i
];
600 for (; i
< 32; i
++, flags
>>= 1) {
605 printf(" %-4s ", str
);
609 struct perf_tool tool
;
610 struct perf_session
*session
;
611 bool show_task_events
;
612 bool show_mmap_events
;
613 bool show_switch_events
;
615 struct cpu_map
*cpus
;
616 struct thread_map
*threads
;
619 static void process_event(struct perf_script
*script __maybe_unused
, union perf_event
*event
,
620 struct perf_sample
*sample
, struct perf_evsel
*evsel
,
621 struct addr_location
*al
)
623 struct thread
*thread
= al
->thread
;
624 struct perf_event_attr
*attr
= &evsel
->attr
;
626 if (output
[attr
->type
].fields
== 0)
629 print_sample_start(sample
, thread
, evsel
);
631 if (PRINT_FIELD(PERIOD
))
632 printf("%10" PRIu64
" ", sample
->period
);
634 if (PRINT_FIELD(EVNAME
)) {
635 const char *evname
= perf_evsel__name(evsel
);
636 printf("%s: ", evname
? evname
: "[unknown]");
640 print_sample_flags(sample
->flags
);
642 if (is_bts_event(attr
)) {
643 print_sample_bts(event
, sample
, evsel
, thread
, al
);
647 if (PRINT_FIELD(TRACE
))
648 event_format__print(evsel
->tp_format
, sample
->cpu
,
649 sample
->raw_data
, sample
->raw_size
);
650 if (PRINT_FIELD(ADDR
))
651 print_sample_addr(event
, sample
, thread
, attr
);
653 if (PRINT_FIELD(IP
)) {
654 if (!symbol_conf
.use_callchain
)
659 perf_evsel__print_ip(evsel
, sample
, al
,
660 output
[attr
->type
].print_ip_opts
,
661 scripting_max_stack
);
664 if (PRINT_FIELD(IREGS
))
665 print_sample_iregs(event
, sample
, thread
, attr
);
667 if (PRINT_FIELD(BRSTACK
))
668 print_sample_brstack(event
, sample
, thread
, attr
);
669 else if (PRINT_FIELD(BRSTACKSYM
))
670 print_sample_brstacksym(event
, sample
, thread
, attr
);
675 static struct scripting_ops
*scripting_ops
;
677 static void setup_scripting(void)
679 setup_perl_scripting();
680 setup_python_scripting();
683 static int flush_scripting(void)
685 return scripting_ops
? scripting_ops
->flush_script() : 0;
688 static int cleanup_scripting(void)
690 pr_debug("\nperf script stopped\n");
692 return scripting_ops
? scripting_ops
->stop_script() : 0;
695 static int process_sample_event(struct perf_tool
*tool
,
696 union perf_event
*event
,
697 struct perf_sample
*sample
,
698 struct perf_evsel
*evsel
,
699 struct machine
*machine
)
701 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
702 struct addr_location al
;
705 if (sample
->time
< last_timestamp
) {
706 pr_err("Samples misordered, previous: %" PRIu64
707 " this: %" PRIu64
"\n", last_timestamp
,
711 last_timestamp
= sample
->time
;
715 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
716 pr_err("problem processing %d event, skipping it.\n",
724 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
728 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
730 process_event(scr
, event
, sample
, evsel
, &al
);
733 addr_location__put(&al
);
737 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
738 struct perf_evlist
**pevlist
)
740 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
741 struct perf_evlist
*evlist
;
742 struct perf_evsel
*evsel
, *pos
;
745 err
= perf_event__process_attr(tool
, event
, pevlist
);
750 evsel
= perf_evlist__last(*pevlist
);
752 if (evsel
->attr
.type
>= PERF_TYPE_MAX
)
755 evlist__for_each(evlist
, pos
) {
756 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
760 set_print_ip_opts(&evsel
->attr
);
762 if (evsel
->attr
.sample_type
)
763 err
= perf_evsel__check_attr(evsel
, scr
->session
);
768 static int process_comm_event(struct perf_tool
*tool
,
769 union perf_event
*event
,
770 struct perf_sample
*sample
,
771 struct machine
*machine
)
773 struct thread
*thread
;
774 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
775 struct perf_session
*session
= script
->session
;
776 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
779 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
780 if (thread
== NULL
) {
781 pr_debug("problem processing COMM event, skipping it.\n");
785 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
788 if (!evsel
->attr
.sample_id_all
) {
791 sample
->tid
= event
->comm
.tid
;
792 sample
->pid
= event
->comm
.pid
;
794 print_sample_start(sample
, thread
, evsel
);
795 perf_event__fprintf(event
, stdout
);
802 static int process_fork_event(struct perf_tool
*tool
,
803 union perf_event
*event
,
804 struct perf_sample
*sample
,
805 struct machine
*machine
)
807 struct thread
*thread
;
808 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
809 struct perf_session
*session
= script
->session
;
810 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
812 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
815 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
816 if (thread
== NULL
) {
817 pr_debug("problem processing FORK event, skipping it.\n");
821 if (!evsel
->attr
.sample_id_all
) {
823 sample
->time
= event
->fork
.time
;
824 sample
->tid
= event
->fork
.tid
;
825 sample
->pid
= event
->fork
.pid
;
827 print_sample_start(sample
, thread
, evsel
);
828 perf_event__fprintf(event
, stdout
);
833 static int process_exit_event(struct perf_tool
*tool
,
834 union perf_event
*event
,
835 struct perf_sample
*sample
,
836 struct machine
*machine
)
839 struct thread
*thread
;
840 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
841 struct perf_session
*session
= script
->session
;
842 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
844 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
845 if (thread
== NULL
) {
846 pr_debug("problem processing EXIT event, skipping it.\n");
850 if (!evsel
->attr
.sample_id_all
) {
853 sample
->tid
= event
->fork
.tid
;
854 sample
->pid
= event
->fork
.pid
;
856 print_sample_start(sample
, thread
, evsel
);
857 perf_event__fprintf(event
, stdout
);
859 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
866 static int process_mmap_event(struct perf_tool
*tool
,
867 union perf_event
*event
,
868 struct perf_sample
*sample
,
869 struct machine
*machine
)
871 struct thread
*thread
;
872 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
873 struct perf_session
*session
= script
->session
;
874 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
876 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
879 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
880 if (thread
== NULL
) {
881 pr_debug("problem processing MMAP event, skipping it.\n");
885 if (!evsel
->attr
.sample_id_all
) {
888 sample
->tid
= event
->mmap
.tid
;
889 sample
->pid
= event
->mmap
.pid
;
891 print_sample_start(sample
, thread
, evsel
);
892 perf_event__fprintf(event
, stdout
);
897 static int process_mmap2_event(struct perf_tool
*tool
,
898 union perf_event
*event
,
899 struct perf_sample
*sample
,
900 struct machine
*machine
)
902 struct thread
*thread
;
903 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
904 struct perf_session
*session
= script
->session
;
905 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
907 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
910 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
911 if (thread
== NULL
) {
912 pr_debug("problem processing MMAP2 event, skipping it.\n");
916 if (!evsel
->attr
.sample_id_all
) {
919 sample
->tid
= event
->mmap2
.tid
;
920 sample
->pid
= event
->mmap2
.pid
;
922 print_sample_start(sample
, thread
, evsel
);
923 perf_event__fprintf(event
, stdout
);
928 static int process_switch_event(struct perf_tool
*tool
,
929 union perf_event
*event
,
930 struct perf_sample
*sample
,
931 struct machine
*machine
)
933 struct thread
*thread
;
934 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
935 struct perf_session
*session
= script
->session
;
936 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
938 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
941 thread
= machine__findnew_thread(machine
, sample
->pid
,
943 if (thread
== NULL
) {
944 pr_debug("problem processing SWITCH event, skipping it.\n");
948 print_sample_start(sample
, thread
, evsel
);
949 perf_event__fprintf(event
, stdout
);
954 static void sig_handler(int sig __maybe_unused
)
959 static int __cmd_script(struct perf_script
*script
)
963 signal(SIGINT
, sig_handler
);
965 /* override event processing functions */
966 if (script
->show_task_events
) {
967 script
->tool
.comm
= process_comm_event
;
968 script
->tool
.fork
= process_fork_event
;
969 script
->tool
.exit
= process_exit_event
;
971 if (script
->show_mmap_events
) {
972 script
->tool
.mmap
= process_mmap_event
;
973 script
->tool
.mmap2
= process_mmap2_event
;
975 if (script
->show_switch_events
)
976 script
->tool
.context_switch
= process_switch_event
;
978 ret
= perf_session__process_events(script
->session
);
981 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
987 struct list_head node
;
988 struct scripting_ops
*ops
;
992 static LIST_HEAD(script_specs
);
994 static struct script_spec
*script_spec__new(const char *spec
,
995 struct scripting_ops
*ops
)
997 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
1000 strcpy(s
->spec
, spec
);
1007 static void script_spec__add(struct script_spec
*s
)
1009 list_add_tail(&s
->node
, &script_specs
);
1012 static struct script_spec
*script_spec__find(const char *spec
)
1014 struct script_spec
*s
;
1016 list_for_each_entry(s
, &script_specs
, node
)
1017 if (strcasecmp(s
->spec
, spec
) == 0)
1022 static struct script_spec
*script_spec__findnew(const char *spec
,
1023 struct scripting_ops
*ops
)
1025 struct script_spec
*s
= script_spec__find(spec
);
1030 s
= script_spec__new(spec
, ops
);
1034 script_spec__add(s
);
1039 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
1041 struct script_spec
*s
;
1043 s
= script_spec__find(spec
);
1047 s
= script_spec__findnew(spec
, ops
);
1054 static struct scripting_ops
*script_spec__lookup(const char *spec
)
1056 struct script_spec
*s
= script_spec__find(spec
);
1063 static void list_available_languages(void)
1065 struct script_spec
*s
;
1067 fprintf(stderr
, "\n");
1068 fprintf(stderr
, "Scripting language extensions (used in "
1069 "perf script -s [spec:]script.[spec]):\n\n");
1071 list_for_each_entry(s
, &script_specs
, node
)
1072 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
1074 fprintf(stderr
, "\n");
1077 static int parse_scriptname(const struct option
*opt __maybe_unused
,
1078 const char *str
, int unset __maybe_unused
)
1080 char spec
[PATH_MAX
];
1081 const char *script
, *ext
;
1084 if (strcmp(str
, "lang") == 0) {
1085 list_available_languages();
1089 script
= strchr(str
, ':');
1092 if (len
>= PATH_MAX
) {
1093 fprintf(stderr
, "invalid language specifier");
1096 strncpy(spec
, str
, len
);
1098 scripting_ops
= script_spec__lookup(spec
);
1099 if (!scripting_ops
) {
1100 fprintf(stderr
, "invalid language specifier");
1106 ext
= strrchr(script
, '.');
1108 fprintf(stderr
, "invalid script extension");
1111 scripting_ops
= script_spec__lookup(++ext
);
1112 if (!scripting_ops
) {
1113 fprintf(stderr
, "invalid script extension");
1118 script_name
= strdup(script
);
1123 static int parse_output_fields(const struct option
*opt __maybe_unused
,
1124 const char *arg
, int unset __maybe_unused
)
1127 int i
, imax
= ARRAY_SIZE(all_output_options
);
1130 char *str
= strdup(arg
);
1136 /* first word can state for which event type the user is specifying
1137 * the fields. If no type exists, the specified fields apply to all
1138 * event types found in the file minus the invalid fields for a type.
1140 tok
= strchr(str
, ':');
1144 if (!strcmp(str
, "hw"))
1145 type
= PERF_TYPE_HARDWARE
;
1146 else if (!strcmp(str
, "sw"))
1147 type
= PERF_TYPE_SOFTWARE
;
1148 else if (!strcmp(str
, "trace"))
1149 type
= PERF_TYPE_TRACEPOINT
;
1150 else if (!strcmp(str
, "raw"))
1151 type
= PERF_TYPE_RAW
;
1152 else if (!strcmp(str
, "break"))
1153 type
= PERF_TYPE_BREAKPOINT
;
1155 fprintf(stderr
, "Invalid event type in field string.\n");
1160 if (output
[type
].user_set
)
1161 pr_warning("Overriding previous field request for %s events.\n",
1164 output
[type
].fields
= 0;
1165 output
[type
].user_set
= true;
1166 output
[type
].wildcard_set
= false;
1170 if (strlen(str
) == 0) {
1172 "Cannot set fields to 'none' for all event types.\n");
1177 if (output_set_by_user())
1178 pr_warning("Overriding previous field request for all events.\n");
1180 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1181 output
[j
].fields
= 0;
1182 output
[j
].user_set
= true;
1183 output
[j
].wildcard_set
= true;
1187 for (tok
= strtok(tok
, ","); tok
; tok
= strtok(NULL
, ",")) {
1188 for (i
= 0; i
< imax
; ++i
) {
1189 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
1192 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
1197 fprintf(stderr
, "Invalid field requested.\n");
1203 /* add user option to all events types for
1206 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1207 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
1208 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1209 all_output_options
[i
].str
, event_type(j
));
1211 output
[j
].fields
|= all_output_options
[i
].field
;
1214 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
1215 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
1216 all_output_options
[i
].str
, event_type(type
));
1221 output
[type
].fields
|= all_output_options
[i
].field
;
1226 if (output
[type
].fields
== 0) {
1227 pr_debug("No fields requested for %s type. "
1228 "Events will not be displayed.\n", event_type(type
));
1237 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1238 static int is_directory(const char *base_path
, const struct dirent
*dent
)
1240 char path
[PATH_MAX
];
1243 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
1244 if (stat(path
, &st
))
1247 return S_ISDIR(st
.st_mode
);
1250 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1251 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1253 if ((lang_dirent.d_type == DT_DIR || \
1254 (lang_dirent.d_type == DT_UNKNOWN && \
1255 is_directory(scripts_path, &lang_dirent))) && \
1256 (strcmp(lang_dirent.d_name, ".")) && \
1257 (strcmp(lang_dirent.d_name, "..")))
1259 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1260 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1262 if (script_dirent.d_type != DT_DIR && \
1263 (script_dirent.d_type != DT_UNKNOWN || \
1264 !is_directory(lang_path, &script_dirent)))
1267 #define RECORD_SUFFIX "-record"
1268 #define REPORT_SUFFIX "-report"
1270 struct script_desc
{
1271 struct list_head node
;
1277 static LIST_HEAD(script_descs
);
1279 static struct script_desc
*script_desc__new(const char *name
)
1281 struct script_desc
*s
= zalloc(sizeof(*s
));
1283 if (s
!= NULL
&& name
)
1284 s
->name
= strdup(name
);
1289 static void script_desc__delete(struct script_desc
*s
)
1292 zfree(&s
->half_liner
);
1297 static void script_desc__add(struct script_desc
*s
)
1299 list_add_tail(&s
->node
, &script_descs
);
1302 static struct script_desc
*script_desc__find(const char *name
)
1304 struct script_desc
*s
;
1306 list_for_each_entry(s
, &script_descs
, node
)
1307 if (strcasecmp(s
->name
, name
) == 0)
1312 static struct script_desc
*script_desc__findnew(const char *name
)
1314 struct script_desc
*s
= script_desc__find(name
);
1319 s
= script_desc__new(name
);
1321 goto out_delete_desc
;
1323 script_desc__add(s
);
1328 script_desc__delete(s
);
1333 static const char *ends_with(const char *str
, const char *suffix
)
1335 size_t suffix_len
= strlen(suffix
);
1336 const char *p
= str
;
1338 if (strlen(str
) > suffix_len
) {
1339 p
= str
+ strlen(str
) - suffix_len
;
1340 if (!strncmp(p
, suffix
, suffix_len
))
1347 static int read_script_info(struct script_desc
*desc
, const char *filename
)
1349 char line
[BUFSIZ
], *p
;
1352 fp
= fopen(filename
, "r");
1356 while (fgets(line
, sizeof(line
), fp
)) {
1363 if (strlen(p
) && *p
== '!')
1367 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
1368 p
[strlen(p
) - 1] = '\0';
1370 if (!strncmp(p
, "description:", strlen("description:"))) {
1371 p
+= strlen("description:");
1372 desc
->half_liner
= strdup(ltrim(p
));
1376 if (!strncmp(p
, "args:", strlen("args:"))) {
1377 p
+= strlen("args:");
1378 desc
->args
= strdup(ltrim(p
));
1388 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
1390 char *script_root
, *str
;
1392 script_root
= strdup(script_dirent
->d_name
);
1396 str
= (char *)ends_with(script_root
, suffix
);
1406 static int list_available_scripts(const struct option
*opt __maybe_unused
,
1407 const char *s __maybe_unused
,
1408 int unset __maybe_unused
)
1410 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1411 char scripts_path
[MAXPATHLEN
];
1412 DIR *scripts_dir
, *lang_dir
;
1413 char script_path
[MAXPATHLEN
];
1414 char lang_path
[MAXPATHLEN
];
1415 struct script_desc
*desc
;
1416 char first_half
[BUFSIZ
];
1419 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1421 scripts_dir
= opendir(scripts_path
);
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
);
1432 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1433 script_root
= get_script_root(&script_dirent
, REPORT_SUFFIX
);
1435 desc
= script_desc__findnew(script_root
);
1436 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1437 lang_path
, script_dirent
.d_name
);
1438 read_script_info(desc
, script_path
);
1444 fprintf(stdout
, "List of available trace scripts:\n");
1445 list_for_each_entry(desc
, &script_descs
, node
) {
1446 sprintf(first_half
, "%s %s", desc
->name
,
1447 desc
->args
? desc
->args
: "");
1448 fprintf(stdout
, " %-36s %s\n", first_half
,
1449 desc
->half_liner
? desc
->half_liner
: "");
1456 * Some scripts specify the required events in their "xxx-record" file,
1457 * this function will check if the events in perf.data match those
1458 * mentioned in the "xxx-record".
1460 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1461 * which is covered well now. And new parsing code should be added to
1462 * cover the future complexing formats like event groups etc.
1464 static int check_ev_match(char *dir_name
, char *scriptname
,
1465 struct perf_session
*session
)
1467 char filename
[MAXPATHLEN
], evname
[128];
1468 char line
[BUFSIZ
], *p
;
1469 struct perf_evsel
*pos
;
1473 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
1475 fp
= fopen(filename
, "r");
1479 while (fgets(line
, sizeof(line
), fp
)) {
1485 p
= strstr(p
, "-e");
1491 len
= strcspn(p
, " \t");
1495 snprintf(evname
, len
+ 1, "%s", p
);
1498 evlist__for_each(session
->evlist
, pos
) {
1499 if (!strcmp(perf_evsel__name(pos
), evname
)) {
1517 * Return -1 if none is found, otherwise the actual scripts number.
1519 * Currently the only user of this function is the script browser, which
1520 * will list all statically runnable scripts, select one, execute it and
1521 * show the output in a perf browser.
1523 int find_scripts(char **scripts_array
, char **scripts_path_array
)
1525 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1526 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
1527 DIR *scripts_dir
, *lang_dir
;
1528 struct perf_session
*session
;
1529 struct perf_data_file file
= {
1531 .mode
= PERF_DATA_MODE_READ
,
1536 session
= perf_session__new(&file
, false, NULL
);
1540 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1542 scripts_dir
= opendir(scripts_path
);
1544 perf_session__delete(session
);
1548 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1549 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
1550 lang_dirent
.d_name
);
1552 if (strstr(lang_path
, "perl"))
1556 if (strstr(lang_path
, "python"))
1560 lang_dir
= opendir(lang_path
);
1564 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1565 /* Skip those real time scripts: xxxtop.p[yl] */
1566 if (strstr(script_dirent
.d_name
, "top."))
1568 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
1569 script_dirent
.d_name
);
1570 temp
= strchr(script_dirent
.d_name
, '.');
1571 snprintf(scripts_array
[i
],
1572 (temp
- script_dirent
.d_name
) + 1,
1573 "%s", script_dirent
.d_name
);
1575 if (check_ev_match(lang_path
,
1576 scripts_array
[i
], session
))
1584 closedir(scripts_dir
);
1585 perf_session__delete(session
);
1589 static char *get_script_path(const char *script_root
, const char *suffix
)
1591 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1592 char scripts_path
[MAXPATHLEN
];
1593 char script_path
[MAXPATHLEN
];
1594 DIR *scripts_dir
, *lang_dir
;
1595 char lang_path
[MAXPATHLEN
];
1596 char *__script_root
;
1598 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1600 scripts_dir
= opendir(scripts_path
);
1604 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1605 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1606 lang_dirent
.d_name
);
1607 lang_dir
= opendir(lang_path
);
1611 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1612 __script_root
= get_script_root(&script_dirent
, suffix
);
1613 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
1614 free(__script_root
);
1616 closedir(scripts_dir
);
1617 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1618 lang_path
, script_dirent
.d_name
);
1619 return strdup(script_path
);
1621 free(__script_root
);
1625 closedir(scripts_dir
);
1630 static bool is_top_script(const char *script_path
)
1632 return ends_with(script_path
, "top") == NULL
? false : true;
1635 static int has_required_arg(char *script_path
)
1637 struct script_desc
*desc
;
1641 desc
= script_desc__new(NULL
);
1643 if (read_script_info(desc
, script_path
))
1649 for (p
= desc
->args
; *p
; p
++)
1653 script_desc__delete(desc
);
1658 static int have_cmd(int argc
, const char **argv
)
1660 char **__argv
= malloc(sizeof(const char *) * argc
);
1663 pr_err("malloc failed\n");
1667 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1668 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1669 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1672 system_wide
= (argc
== 0);
1677 static void script__setup_sample_type(struct perf_script
*script
)
1679 struct perf_session
*session
= script
->session
;
1680 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
1682 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
1683 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
1684 (sample_type
& PERF_SAMPLE_STACK_USER
))
1685 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
1686 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
1687 callchain_param
.record_mode
= CALLCHAIN_LBR
;
1689 callchain_param
.record_mode
= CALLCHAIN_FP
;
1693 static int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
1694 union perf_event
*event
,
1695 struct perf_session
*session __maybe_unused
)
1697 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
1701 static int set_maps(struct perf_script
*script
)
1703 struct perf_evlist
*evlist
= script
->session
->evlist
;
1705 if (!script
->cpus
|| !script
->threads
)
1708 if (WARN_ONCE(script
->allocated
, "stats double allocation\n"))
1711 perf_evlist__set_maps(evlist
, script
->cpus
, script
->threads
);
1713 if (perf_evlist__alloc_stats(evlist
, true))
1716 script
->allocated
= true;
1721 int process_thread_map_event(struct perf_tool
*tool
,
1722 union perf_event
*event
,
1723 struct perf_session
*session __maybe_unused
)
1725 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1727 if (script
->threads
) {
1728 pr_warning("Extra thread map event, ignoring.\n");
1732 script
->threads
= thread_map__new_event(&event
->thread_map
);
1733 if (!script
->threads
)
1736 return set_maps(script
);
1740 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
1741 union perf_event
*event
,
1742 struct perf_session
*session __maybe_unused
)
1744 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1747 pr_warning("Extra cpu map event, ignoring.\n");
1751 script
->cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
1755 return set_maps(script
);
1758 int cmd_script(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1760 bool show_full_info
= false;
1761 bool header
= false;
1762 bool header_only
= false;
1763 bool script_started
= false;
1764 char *rec_script_path
= NULL
;
1765 char *rep_script_path
= NULL
;
1766 struct perf_session
*session
;
1767 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
1768 char *script_path
= NULL
;
1769 const char **__argv
;
1771 struct perf_script script
= {
1773 .sample
= process_sample_event
,
1774 .mmap
= perf_event__process_mmap
,
1775 .mmap2
= perf_event__process_mmap2
,
1776 .comm
= perf_event__process_comm
,
1777 .exit
= perf_event__process_exit
,
1778 .fork
= perf_event__process_fork
,
1779 .attr
= process_attr
,
1780 .tracing_data
= perf_event__process_tracing_data
,
1781 .build_id
= perf_event__process_build_id
,
1782 .id_index
= perf_event__process_id_index
,
1783 .auxtrace_info
= perf_event__process_auxtrace_info
,
1784 .auxtrace
= perf_event__process_auxtrace
,
1785 .auxtrace_error
= perf_event__process_auxtrace_error
,
1786 .stat_config
= process_stat_config_event
,
1787 .thread_map
= process_thread_map_event
,
1788 .cpu_map
= process_cpu_map_event
,
1789 .ordered_events
= true,
1790 .ordering_requires_timestamps
= true,
1793 struct perf_data_file file
= {
1794 .mode
= PERF_DATA_MODE_READ
,
1796 const struct option options
[] = {
1797 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1798 "dump raw trace in ASCII"),
1799 OPT_INCR('v', "verbose", &verbose
,
1800 "be more verbose (show symbol address, etc)"),
1801 OPT_BOOLEAN('L', "Latency", &latency_format
,
1802 "show latency attributes (irqs/preemption disabled, etc)"),
1803 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
1804 list_available_scripts
),
1805 OPT_CALLBACK('s', "script", NULL
, "name",
1806 "script file name (lang:script name, script name, or *)",
1808 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
1809 "generate perf-script.xx script in specified language"),
1810 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
1811 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
1812 "do various checks like samples ordering and lost events"),
1813 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
1814 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
1815 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
1816 "file", "vmlinux pathname"),
1817 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
1818 "file", "kallsyms pathname"),
1819 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
1820 "When printing symbols do not display call chain"),
1821 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1822 "Look for files with symbols relative to this directory"),
1823 OPT_CALLBACK('F', "fields", NULL
, "str",
1824 "comma separated output fields prepend with 'type:'. "
1825 "Valid types: hw,sw,trace,raw. "
1826 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1827 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields
),
1828 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
1829 "system-wide collection from all CPUs"),
1830 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
1831 "only consider these symbols"),
1832 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
1833 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
1834 "only display events for these comms"),
1835 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
1836 "only consider symbols in these pids"),
1837 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
1838 "only consider symbols in these tids"),
1839 OPT_BOOLEAN('I', "show-info", &show_full_info
,
1840 "display extended information from perf.data file"),
1841 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
1842 "Show the path of [kernel.kallsyms]"),
1843 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
1844 "Show the fork/comm/exit events"),
1845 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
1846 "Show the mmap events"),
1847 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
1848 "Show context switch events (if recorded)"),
1849 OPT_BOOLEAN('f', "force", &file
.force
, "don't complain, do it"),
1850 OPT_BOOLEAN(0, "ns", &nanosecs
,
1851 "Use 9 decimal places when displaying time"),
1852 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
1853 "Instruction Tracing options",
1854 itrace_parse_synth_opts
),
1855 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
1856 "Show full source file name path for source lines"),
1857 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
1858 "Enable symbol demangling"),
1859 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
1860 "Enable kernel symbol demangling"),
1864 const char * const script_subcommands
[] = { "record", "report", NULL
};
1865 const char *script_usage
[] = {
1866 "perf script [<options>]",
1867 "perf script [<options>] record <script> [<record-options>] <command>",
1868 "perf script [<options>] report <script> [script-args]",
1869 "perf script [<options>] <script> [<record-options>] <command>",
1870 "perf script [<options>] <top-script> [script-args]",
1876 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
1877 PARSE_OPT_STOP_AT_NON_OPTION
);
1879 file
.path
= input_name
;
1881 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
1882 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
1883 if (!rec_script_path
)
1884 return cmd_record(argc
, argv
, NULL
);
1887 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
1888 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
1889 if (!rep_script_path
) {
1891 "Please specify a valid report script"
1892 "(see 'perf script -l' for listing)\n");
1897 if (itrace_synth_opts
.callchain
&&
1898 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
1899 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
1901 /* make sure PERF_EXEC_PATH is set for scripts */
1902 set_argv_exec_path(get_argv_exec_path());
1904 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
1909 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
1910 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
1912 if (!rec_script_path
&& !rep_script_path
) {
1913 usage_with_options_msg(script_usage
, options
,
1914 "Couldn't find script `%s'\n\n See perf"
1915 " script -l for available scripts.\n", argv
[0]);
1918 if (is_top_script(argv
[0])) {
1919 rep_args
= argc
- 1;
1923 rep_args
= has_required_arg(rep_script_path
);
1924 rec_args
= (argc
- 1) - rep_args
;
1926 usage_with_options_msg(script_usage
, options
,
1927 "`%s' script requires options."
1928 "\n\n See perf script -l for available "
1929 "scripts and options.\n", argv
[0]);
1933 if (pipe(live_pipe
) < 0) {
1934 perror("failed to create pipe");
1940 perror("failed to fork");
1947 dup2(live_pipe
[1], 1);
1948 close(live_pipe
[0]);
1950 if (is_top_script(argv
[0])) {
1952 } else if (!system_wide
) {
1953 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
1959 __argv
= malloc((argc
+ 6) * sizeof(const char *));
1961 pr_err("malloc failed\n");
1966 __argv
[j
++] = "/bin/sh";
1967 __argv
[j
++] = rec_script_path
;
1973 for (i
= rep_args
+ 1; i
< argc
; i
++)
1974 __argv
[j
++] = argv
[i
];
1977 execvp("/bin/sh", (char **)__argv
);
1982 dup2(live_pipe
[0], 0);
1983 close(live_pipe
[1]);
1985 __argv
= malloc((argc
+ 4) * sizeof(const char *));
1987 pr_err("malloc failed\n");
1993 __argv
[j
++] = "/bin/sh";
1994 __argv
[j
++] = rep_script_path
;
1995 for (i
= 1; i
< rep_args
+ 1; i
++)
1996 __argv
[j
++] = argv
[i
];
2001 execvp("/bin/sh", (char **)__argv
);
2006 if (rec_script_path
)
2007 script_path
= rec_script_path
;
2008 if (rep_script_path
)
2009 script_path
= rep_script_path
;
2014 if (!rec_script_path
)
2015 system_wide
= false;
2016 else if (!system_wide
) {
2017 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
2023 __argv
= malloc((argc
+ 2) * sizeof(const char *));
2025 pr_err("malloc failed\n");
2030 __argv
[j
++] = "/bin/sh";
2031 __argv
[j
++] = script_path
;
2034 for (i
= 2; i
< argc
; i
++)
2035 __argv
[j
++] = argv
[i
];
2038 execvp("/bin/sh", (char **)__argv
);
2046 session
= perf_session__new(&file
, false, &script
.tool
);
2047 if (session
== NULL
)
2050 if (header
|| header_only
) {
2051 perf_session__fprintf_info(session
, stdout
, show_full_info
);
2056 if (symbol__init(&session
->header
.env
) < 0)
2059 script
.session
= session
;
2060 script__setup_sample_type(&script
);
2062 session
->itrace_synth_opts
= &itrace_synth_opts
;
2065 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
2071 symbol_conf
.use_callchain
= true;
2073 symbol_conf
.use_callchain
= false;
2075 if (session
->tevent
.pevent
&&
2076 pevent_set_function_resolver(session
->tevent
.pevent
,
2077 machine__resolve_kernel_addr
,
2078 &session
->machines
.host
) < 0) {
2079 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
2083 if (generate_script_lang
) {
2084 struct stat perf_stat
;
2087 if (output_set_by_user()) {
2089 "custom fields not supported for generated scripts");
2094 input
= open(file
.path
, O_RDONLY
); /* input_name */
2097 perror("failed to open file");
2101 err
= fstat(input
, &perf_stat
);
2103 perror("failed to stat file");
2107 if (!perf_stat
.st_size
) {
2108 fprintf(stderr
, "zero-sized file, nothing to do!\n");
2112 scripting_ops
= script_spec__lookup(generate_script_lang
);
2113 if (!scripting_ops
) {
2114 fprintf(stderr
, "invalid language specifier");
2119 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
2125 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
2128 pr_debug("perf script started with script %s\n\n", script_name
);
2129 script_started
= true;
2133 err
= perf_session__check_output_opt(session
);
2137 err
= __cmd_script(&script
);
2142 perf_evlist__free_stats(session
->evlist
);
2143 perf_session__delete(session
);
2146 cleanup_scripting();