perf script: Display addr/data_src/weight columns for raw events
[deliverable/linux.git] / tools / perf / builtin-script.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
b7eead86 3#include "perf.h"
5f9c39dc 4#include "util/cache.h"
b7eead86 5#include "util/debug.h"
4b6ab94e 6#include <subcmd/exec-cmd.h>
b7eead86 7#include "util/header.h"
4b6ab94e 8#include <subcmd/parse-options.h>
fc36f948 9#include "util/perf_regs.h"
b7eead86 10#include "util/session.h"
45694aa7 11#include "util/tool.h"
5f9c39dc
FW
12#include "util/symbol.h"
13#include "util/thread.h"
cf72344d 14#include "util/trace-event.h"
b7eead86 15#include "util/util.h"
1424dc96
DA
16#include "util/evlist.h"
17#include "util/evsel.h"
36385be5 18#include "util/sort.h"
f5fc1412 19#include "util/data.h"
7a680eb9 20#include "util/auxtrace.h"
cfc8874a
JO
21#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
5d67be97 24#include <linux/bitmap.h>
cfc8874a 25#include "asm/bug.h"
5f9c39dc 26
956ffd02
TZ
27static char const *script_name;
28static char const *generate_script_lang;
ffabd99e 29static bool debug_mode;
e1889d75 30static u64 last_timestamp;
6fcf7ddb 31static u64 nr_unordered;
c0230b2b 32static bool no_callchain;
47390ae2 33static bool latency_format;
317df650 34static bool system_wide;
400ea6d3 35static bool print_flags;
83e19860 36static bool nanosecs;
5d67be97
AB
37static const char *cpu_list;
38static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
91a2c3d5 39static struct perf_stat_config stat_config;
956ffd02 40
44cbe729 41unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
03cd1fed 42
745f43e3
DA
43enum 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,
787bef17
DA
51 PERF_OUTPUT_IP = 1U << 7,
52 PERF_OUTPUT_SYM = 1U << 8,
610723f2 53 PERF_OUTPUT_DSO = 1U << 9,
7cec0922 54 PERF_OUTPUT_ADDR = 1U << 10,
a978f2ab 55 PERF_OUTPUT_SYMOFFSET = 1U << 11,
cc8fae1d 56 PERF_OUTPUT_SRCLINE = 1U << 12,
535aeaae 57 PERF_OUTPUT_PERIOD = 1U << 13,
fc36f948 58 PERF_OUTPUT_IREGS = 1U << 14,
dc323ce8
SE
59 PERF_OUTPUT_BRSTACK = 1U << 15,
60 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
94ddddfa
JO
61 PERF_OUTPUT_DATA_SRC = 1U << 17,
62 PERF_OUTPUT_WEIGHT = 1U << 18,
745f43e3
DA
63};
64
65struct output_option {
66 const char *str;
67 enum perf_output_field field;
68} all_output_options[] = {
69 {.str = "comm", .field = PERF_OUTPUT_COMM},
70 {.str = "tid", .field = PERF_OUTPUT_TID},
71 {.str = "pid", .field = PERF_OUTPUT_PID},
72 {.str = "time", .field = PERF_OUTPUT_TIME},
73 {.str = "cpu", .field = PERF_OUTPUT_CPU},
74 {.str = "event", .field = PERF_OUTPUT_EVNAME},
75 {.str = "trace", .field = PERF_OUTPUT_TRACE},
787bef17 76 {.str = "ip", .field = PERF_OUTPUT_IP},
c0230b2b 77 {.str = "sym", .field = PERF_OUTPUT_SYM},
610723f2 78 {.str = "dso", .field = PERF_OUTPUT_DSO},
7cec0922 79 {.str = "addr", .field = PERF_OUTPUT_ADDR},
a978f2ab 80 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
cc8fae1d 81 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
535aeaae 82 {.str = "period", .field = PERF_OUTPUT_PERIOD},
fc36f948 83 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
dc323ce8
SE
84 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
85 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
94ddddfa
JO
86 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
87 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
745f43e3
DA
88};
89
90/* default set to maintain compatibility with current format */
2c9e45f7
DA
91static struct {
92 bool user_set;
9cbdb702 93 bool wildcard_set;
a6ffaf91 94 unsigned int print_ip_opts;
2c9e45f7
DA
95 u64 fields;
96 u64 invalid_fields;
97} output[PERF_TYPE_MAX] = {
98
99 [PERF_TYPE_HARDWARE] = {
100 .user_set = false,
101
102 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
103 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 104 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
105 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
106 PERF_OUTPUT_PERIOD,
2c9e45f7
DA
107
108 .invalid_fields = PERF_OUTPUT_TRACE,
109 },
110
111 [PERF_TYPE_SOFTWARE] = {
112 .user_set = false,
113
114 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
115 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 116 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
117 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
118 PERF_OUTPUT_PERIOD,
2c9e45f7
DA
119
120 .invalid_fields = PERF_OUTPUT_TRACE,
121 },
122
123 [PERF_TYPE_TRACEPOINT] = {
124 .user_set = false,
125
126 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
127 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
128 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
129 },
0817a6a3
AS
130
131 [PERF_TYPE_RAW] = {
132 .user_set = false,
133
134 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
135 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 136 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71 137 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
ff7b1915
JO
138 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
139 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
0817a6a3
AS
140
141 .invalid_fields = PERF_OUTPUT_TRACE,
142 },
27cfef00
WN
143
144 [PERF_TYPE_BREAKPOINT] = {
145 .user_set = false,
146
147 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
148 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
149 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
150 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
151 PERF_OUTPUT_PERIOD,
152
153 .invalid_fields = PERF_OUTPUT_TRACE,
154 },
1424dc96 155};
745f43e3 156
2c9e45f7
DA
157static bool output_set_by_user(void)
158{
159 int j;
160 for (j = 0; j < PERF_TYPE_MAX; ++j) {
161 if (output[j].user_set)
162 return true;
163 }
164 return false;
165}
745f43e3 166
9cbdb702
DA
167static const char *output_field2str(enum perf_output_field field)
168{
169 int i, imax = ARRAY_SIZE(all_output_options);
170 const char *str = "";
171
172 for (i = 0; i < imax; ++i) {
173 if (all_output_options[i].field == field) {
174 str = all_output_options[i].str;
175 break;
176 }
177 }
178 return str;
179}
180
2c9e45f7 181#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
1424dc96 182
6d5cdd64
AH
183static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
184 u64 sample_type, const char *sample_msg,
185 enum perf_output_field field,
186 bool allow_user_set)
1424dc96 187{
5bff01f6 188 struct perf_event_attr *attr = &evsel->attr;
9cbdb702
DA
189 int type = attr->type;
190 const char *evname;
191
192 if (attr->sample_type & sample_type)
193 return 0;
194
195 if (output[type].user_set) {
6d5cdd64
AH
196 if (allow_user_set)
197 return 0;
5bff01f6 198 evname = perf_evsel__name(evsel);
9cbdb702
DA
199 pr_err("Samples for '%s' event do not have %s attribute set. "
200 "Cannot print '%s' field.\n",
201 evname, sample_msg, output_field2str(field));
202 return -1;
203 }
204
205 /* user did not ask for it explicitly so remove from the default list */
206 output[type].fields &= ~field;
5bff01f6 207 evname = perf_evsel__name(evsel);
9cbdb702
DA
208 pr_debug("Samples for '%s' event do not have %s attribute set. "
209 "Skipping '%s' field.\n",
210 evname, sample_msg, output_field2str(field));
211
212 return 0;
213}
214
6d5cdd64
AH
215static int perf_evsel__check_stype(struct perf_evsel *evsel,
216 u64 sample_type, const char *sample_msg,
217 enum perf_output_field field)
218{
219 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
220 false);
221}
222
9cbdb702
DA
223static int perf_evsel__check_attr(struct perf_evsel *evsel,
224 struct perf_session *session)
225{
226 struct perf_event_attr *attr = &evsel->attr;
6d5cdd64
AH
227 bool allow_user_set;
228
e099eba8
JO
229 if (perf_header__has_feat(&session->header, HEADER_STAT))
230 return 0;
231
6d5cdd64
AH
232 allow_user_set = perf_header__has_feat(&session->header,
233 HEADER_AUXTRACE);
9cbdb702 234
1424dc96
DA
235 if (PRINT_FIELD(TRACE) &&
236 !perf_session__has_traces(session, "record -R"))
237 return -EINVAL;
238
787bef17 239 if (PRINT_FIELD(IP)) {
5bff01f6
ACM
240 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
241 PERF_OUTPUT_IP))
1424dc96 242 return -EINVAL;
1424dc96 243 }
7cec0922
DA
244
245 if (PRINT_FIELD(ADDR) &&
6d5cdd64
AH
246 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
247 PERF_OUTPUT_ADDR, allow_user_set))
7cec0922
DA
248 return -EINVAL;
249
94ddddfa
JO
250 if (PRINT_FIELD(DATA_SRC) &&
251 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
252 PERF_OUTPUT_DATA_SRC))
253 return -EINVAL;
254
255 if (PRINT_FIELD(WEIGHT) &&
256 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
257 PERF_OUTPUT_WEIGHT))
258 return -EINVAL;
259
7cec0922
DA
260 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
261 pr_err("Display of symbols requested but neither sample IP nor "
262 "sample address\nis selected. Hence, no addresses to convert "
263 "to symbols.\n");
787bef17
DA
264 return -EINVAL;
265 }
a978f2ab
AN
266 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
267 pr_err("Display of offsets requested but symbol is not"
268 "selected.\n");
269 return -EINVAL;
270 }
7cec0922
DA
271 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
272 pr_err("Display of DSO requested but neither sample IP nor "
273 "sample address\nis selected. Hence, no addresses to convert "
274 "to DSO.\n");
610723f2
DA
275 return -EINVAL;
276 }
cc8fae1d
AH
277 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
278 pr_err("Display of source line number requested but sample IP is not\n"
279 "selected. Hence, no address to lookup the source line number.\n");
280 return -EINVAL;
281 }
1424dc96
DA
282
283 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
5bff01f6
ACM
284 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
285 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
1424dc96 286 return -EINVAL;
1424dc96
DA
287
288 if (PRINT_FIELD(TIME) &&
5bff01f6
ACM
289 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
290 PERF_OUTPUT_TIME))
1424dc96 291 return -EINVAL;
1424dc96
DA
292
293 if (PRINT_FIELD(CPU) &&
6d5cdd64
AH
294 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
295 PERF_OUTPUT_CPU, allow_user_set))
1424dc96 296 return -EINVAL;
9cbdb702 297
535aeaae
JO
298 if (PRINT_FIELD(PERIOD) &&
299 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
300 PERF_OUTPUT_PERIOD))
301 return -EINVAL;
302
fc36f948
SE
303 if (PRINT_FIELD(IREGS) &&
304 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
305 PERF_OUTPUT_IREGS))
306 return -EINVAL;
307
9cbdb702
DA
308 return 0;
309}
310
7ea95727
AH
311static void set_print_ip_opts(struct perf_event_attr *attr)
312{
313 unsigned int type = attr->type;
314
315 output[type].print_ip_opts = 0;
316 if (PRINT_FIELD(IP))
317 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
318
319 if (PRINT_FIELD(SYM))
320 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
321
322 if (PRINT_FIELD(DSO))
323 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
324
325 if (PRINT_FIELD(SYMOFFSET))
326 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
cc8fae1d
AH
327
328 if (PRINT_FIELD(SRCLINE))
329 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
7ea95727
AH
330}
331
9cbdb702
DA
332/*
333 * verify all user requested events exist and the samples
334 * have the expected data
335 */
336static int perf_session__check_output_opt(struct perf_session *session)
337{
338 int j;
339 struct perf_evsel *evsel;
340
341 for (j = 0; j < PERF_TYPE_MAX; ++j) {
342 evsel = perf_session__find_first_evtype(session, j);
343
344 /*
345 * even if fields is set to 0 (ie., show nothing) event must
346 * exist if user explicitly includes it on the command line
347 */
348 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
349 pr_err("%s events do not exist. "
350 "Remove corresponding -f option to proceed.\n",
351 event_type(j));
352 return -1;
353 }
354
355 if (evsel && output[j].fields &&
356 perf_evsel__check_attr(evsel, session))
357 return -1;
a6ffaf91
DA
358
359 if (evsel == NULL)
360 continue;
361
7ea95727 362 set_print_ip_opts(&evsel->attr);
1424dc96
DA
363 }
364
98526ee7
AH
365 if (!no_callchain) {
366 bool use_callchain = false;
367
368 evlist__for_each(session->evlist, evsel) {
369 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
370 use_callchain = true;
371 break;
372 }
373 }
374 if (!use_callchain)
375 symbol_conf.use_callchain = false;
376 }
377
80b8b496
DA
378 /*
379 * set default for tracepoints to print symbols only
380 * if callchains are present
381 */
382 if (symbol_conf.use_callchain &&
383 !output[PERF_TYPE_TRACEPOINT].user_set) {
384 struct perf_event_attr *attr;
385
386 j = PERF_TYPE_TRACEPOINT;
387 evsel = perf_session__find_first_evtype(session, j);
388 if (evsel == NULL)
389 goto out;
390
391 attr = &evsel->attr;
392
393 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
394 output[j].fields |= PERF_OUTPUT_IP;
395 output[j].fields |= PERF_OUTPUT_SYM;
396 output[j].fields |= PERF_OUTPUT_DSO;
397 set_print_ip_opts(attr);
398 }
399 }
400
401out:
1424dc96
DA
402 return 0;
403}
745f43e3 404
fc36f948
SE
405static void print_sample_iregs(union perf_event *event __maybe_unused,
406 struct perf_sample *sample,
407 struct thread *thread __maybe_unused,
408 struct perf_event_attr *attr)
409{
410 struct regs_dump *regs = &sample->intr_regs;
411 uint64_t mask = attr->sample_regs_intr;
412 unsigned i = 0, r;
413
414 if (!regs)
415 return;
416
417 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
418 u64 val = regs->regs[i++];
419 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
420 }
421}
422
fcf65bf1 423static void print_sample_start(struct perf_sample *sample,
1424dc96 424 struct thread *thread,
5bff01f6 425 struct perf_evsel *evsel)
c70c94b4 426{
5bff01f6 427 struct perf_event_attr *attr = &evsel->attr;
c70c94b4
DA
428 unsigned long secs;
429 unsigned long usecs;
745f43e3
DA
430 unsigned long long nsecs;
431
432 if (PRINT_FIELD(COMM)) {
433 if (latency_format)
b9c5143a 434 printf("%8.8s ", thread__comm_str(thread));
787bef17 435 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
b9c5143a 436 printf("%s ", thread__comm_str(thread));
745f43e3 437 else
b9c5143a 438 printf("%16s ", thread__comm_str(thread));
745f43e3 439 }
c70c94b4 440
745f43e3
DA
441 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
442 printf("%5d/%-5d ", sample->pid, sample->tid);
443 else if (PRINT_FIELD(PID))
444 printf("%5d ", sample->pid);
445 else if (PRINT_FIELD(TID))
446 printf("%5d ", sample->tid);
447
448 if (PRINT_FIELD(CPU)) {
449 if (latency_format)
450 printf("%3d ", sample->cpu);
451 else
452 printf("[%03d] ", sample->cpu);
453 }
c70c94b4 454
745f43e3
DA
455 if (PRINT_FIELD(TIME)) {
456 nsecs = sample->time;
457 secs = nsecs / NSECS_PER_SEC;
458 nsecs -= secs * NSECS_PER_SEC;
459 usecs = nsecs / NSECS_PER_USEC;
83e19860
AH
460 if (nanosecs)
461 printf("%5lu.%09llu: ", secs, nsecs);
462 else
463 printf("%5lu.%06lu: ", secs, usecs);
745f43e3 464 }
c70c94b4
DA
465}
466
dc323ce8
SE
467static inline char
468mispred_str(struct branch_entry *br)
469{
470 if (!(br->flags.mispred || br->flags.predicted))
471 return '-';
472
473 return br->flags.predicted ? 'P' : 'M';
474}
475
476static void print_sample_brstack(union perf_event *event __maybe_unused,
477 struct perf_sample *sample,
478 struct thread *thread __maybe_unused,
479 struct perf_event_attr *attr __maybe_unused)
480{
481 struct branch_stack *br = sample->branch_stack;
482 u64 i;
483
484 if (!(br && br->nr))
485 return;
486
487 for (i = 0; i < br->nr; i++) {
488 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
489 br->entries[i].from,
490 br->entries[i].to,
491 mispred_str( br->entries + i),
492 br->entries[i].flags.in_tx? 'X' : '-',
493 br->entries[i].flags.abort? 'A' : '-',
494 br->entries[i].flags.cycles);
495 }
496}
497
498static void print_sample_brstacksym(union perf_event *event __maybe_unused,
499 struct perf_sample *sample,
500 struct thread *thread __maybe_unused,
501 struct perf_event_attr *attr __maybe_unused)
502{
503 struct branch_stack *br = sample->branch_stack;
504 struct addr_location alf, alt;
505 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
506 u64 i, from, to;
507
508 if (!(br && br->nr))
509 return;
510
511 for (i = 0; i < br->nr; i++) {
512
513 memset(&alf, 0, sizeof(alf));
514 memset(&alt, 0, sizeof(alt));
515 from = br->entries[i].from;
516 to = br->entries[i].to;
517
518 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
519 if (alf.map)
520 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
521
522 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
523 if (alt.map)
524 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
525
526 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
527 putchar('/');
528 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
529 printf("/%c/%c/%c/%d ",
530 mispred_str( br->entries + i),
531 br->entries[i].flags.in_tx? 'X' : '-',
532 br->entries[i].flags.abort? 'A' : '-',
533 br->entries[i].flags.cycles);
534 }
535}
536
537
7cec0922
DA
538static void print_sample_addr(union perf_event *event,
539 struct perf_sample *sample,
7cec0922
DA
540 struct thread *thread,
541 struct perf_event_attr *attr)
542{
543 struct addr_location al;
7cec0922
DA
544
545 printf("%16" PRIx64, sample->addr);
546
547 if (!sample_addr_correlates_sym(attr))
548 return;
549
bb871a9c 550 perf_event__preprocess_sample_addr(event, sample, thread, &al);
7cec0922
DA
551
552 if (PRINT_FIELD(SYM)) {
547a92e0 553 printf(" ");
a978f2ab
AN
554 if (PRINT_FIELD(SYMOFFSET))
555 symbol__fprintf_symname_offs(al.sym, &al, stdout);
556 else
557 symbol__fprintf_symname(al.sym, stdout);
7cec0922
DA
558 }
559
560 if (PRINT_FIELD(DSO)) {
547a92e0
AN
561 printf(" (");
562 map__fprintf_dsoname(al.map, stdout);
563 printf(")");
7cec0922
DA
564 }
565}
566
95582596
AN
567static void print_sample_bts(union perf_event *event,
568 struct perf_sample *sample,
569 struct perf_evsel *evsel,
a2cb3cf2
AH
570 struct thread *thread,
571 struct addr_location *al)
95582596
AN
572{
573 struct perf_event_attr *attr = &evsel->attr;
8066be5f 574 bool print_srcline_last = false;
95582596
AN
575
576 /* print branch_from information */
577 if (PRINT_FIELD(IP)) {
8066be5f
AH
578 unsigned int print_opts = output[attr->type].print_ip_opts;
579
580 if (symbol_conf.use_callchain && sample->callchain) {
95582596 581 printf("\n");
8066be5f
AH
582 } else {
583 printf(" ");
584 if (print_opts & PRINT_IP_OPT_SRCLINE) {
585 print_srcline_last = true;
586 print_opts &= ~PRINT_IP_OPT_SRCLINE;
587 }
588 }
589 perf_evsel__print_ip(evsel, sample, al, print_opts,
03cd1fed 590 scripting_max_stack);
95582596
AN
591 }
592
95582596 593 /* print branch_to information */
243be3dd
AH
594 if (PRINT_FIELD(ADDR) ||
595 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
578bea40
AH
596 !output[attr->type].user_set)) {
597 printf(" => ");
bb871a9c 598 print_sample_addr(event, sample, thread, attr);
578bea40 599 }
95582596 600
8066be5f
AH
601 if (print_srcline_last)
602 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
603
95582596
AN
604 printf("\n");
605}
606
400ea6d3
AH
607static void print_sample_flags(u32 flags)
608{
609 const char *chars = PERF_IP_FLAG_CHARS;
610 const int n = strlen(PERF_IP_FLAG_CHARS);
611 char str[33];
612 int i, pos = 0;
613
614 for (i = 0; i < n; i++, flags >>= 1) {
615 if (flags & 1)
616 str[pos++] = chars[i];
617 }
618 for (; i < 32; i++, flags >>= 1) {
619 if (flags & 1)
620 str[pos++] = '?';
621 }
622 str[pos] = 0;
623 printf(" %-4s ", str);
624}
625
809e9423
JO
626struct perf_script {
627 struct perf_tool tool;
628 struct perf_session *session;
629 bool show_task_events;
630 bool show_mmap_events;
631 bool show_switch_events;
cfc8874a
JO
632 bool allocated;
633 struct cpu_map *cpus;
634 struct thread_map *threads;
9cdbc409 635 int name_width;
809e9423
JO
636};
637
9cdbc409
JO
638static int perf_evlist__max_name_len(struct perf_evlist *evlist)
639{
640 struct perf_evsel *evsel;
641 int max = 0;
642
643 evlist__for_each(evlist, evsel) {
644 int len = strlen(perf_evsel__name(evsel));
645
646 max = MAX(len, max);
647 }
648
649 return max;
650}
651
652static void process_event(struct perf_script *script, union perf_event *event,
809e9423
JO
653 struct perf_sample *sample, struct perf_evsel *evsel,
654 struct addr_location *al)
be6d842a 655{
f9d5d549 656 struct thread *thread = al->thread;
9e69c210 657 struct perf_event_attr *attr = &evsel->attr;
1424dc96 658
2c9e45f7 659 if (output[attr->type].fields == 0)
1424dc96
DA
660 return;
661
fcf65bf1 662 print_sample_start(sample, thread, evsel);
745f43e3 663
535aeaae
JO
664 if (PRINT_FIELD(PERIOD))
665 printf("%10" PRIu64 " ", sample->period);
666
e944d3d7
NK
667 if (PRINT_FIELD(EVNAME)) {
668 const char *evname = perf_evsel__name(evsel);
9cdbc409
JO
669
670 if (!script->name_width)
671 script->name_width = perf_evlist__max_name_len(script->session->evlist);
672
673 printf("%*s: ", script->name_width,
674 evname ? evname : "[unknown]");
e944d3d7
NK
675 }
676
400ea6d3
AH
677 if (print_flags)
678 print_sample_flags(sample->flags);
679
95582596 680 if (is_bts_event(attr)) {
cc22e575 681 print_sample_bts(event, sample, evsel, thread, al);
95582596
AN
682 return;
683 }
684
745f43e3 685 if (PRINT_FIELD(TRACE))
fcf65bf1
ACM
686 event_format__print(evsel->tp_format, sample->cpu,
687 sample->raw_data, sample->raw_size);
7cec0922 688 if (PRINT_FIELD(ADDR))
bb871a9c 689 print_sample_addr(event, sample, thread, attr);
7cec0922 690
94ddddfa
JO
691 if (PRINT_FIELD(DATA_SRC))
692 printf("%16" PRIx64, sample->data_src);
693
694 if (PRINT_FIELD(WEIGHT))
695 printf("%16" PRIu64, sample->weight);
696
787bef17 697 if (PRINT_FIELD(IP)) {
c0230b2b
DA
698 if (!symbol_conf.use_callchain)
699 printf(" ");
700 else
701 printf("\n");
a6ffaf91 702
cc22e575 703 perf_evsel__print_ip(evsel, sample, al,
307cbb92 704 output[attr->type].print_ip_opts,
03cd1fed 705 scripting_max_stack);
c0230b2b
DA
706 }
707
fc36f948
SE
708 if (PRINT_FIELD(IREGS))
709 print_sample_iregs(event, sample, thread, attr);
710
dc323ce8
SE
711 if (PRINT_FIELD(BRSTACK))
712 print_sample_brstack(event, sample, thread, attr);
713 else if (PRINT_FIELD(BRSTACKSYM))
714 print_sample_brstacksym(event, sample, thread, attr);
715
c70c94b4 716 printf("\n");
be6d842a
DA
717}
718
956ffd02
TZ
719static struct scripting_ops *scripting_ops;
720
36e33c53
JO
721static void __process_stat(struct perf_evsel *counter, u64 tstamp)
722{
723 int nthreads = thread_map__nr(counter->threads);
724 int ncpus = perf_evsel__nr_cpus(counter);
725 int cpu, thread;
726 static int header_printed;
727
728 if (counter->system_wide)
729 nthreads = 1;
730
731 if (!header_printed) {
732 printf("%3s %8s %15s %15s %15s %15s %s\n",
733 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
734 header_printed = 1;
735 }
736
737 for (thread = 0; thread < nthreads; thread++) {
738 for (cpu = 0; cpu < ncpus; cpu++) {
739 struct perf_counts_values *counts;
740
741 counts = perf_counts(counter->counts, cpu, thread);
742
743 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
744 counter->cpus->map[cpu],
745 thread_map__pid(counter->threads, thread),
746 counts->val,
747 counts->ena,
748 counts->run,
749 tstamp,
750 perf_evsel__name(counter));
751 }
752 }
753}
754
e099eba8
JO
755static void process_stat(struct perf_evsel *counter, u64 tstamp)
756{
757 if (scripting_ops && scripting_ops->process_stat)
758 scripting_ops->process_stat(&stat_config, counter, tstamp);
36e33c53
JO
759 else
760 __process_stat(counter, tstamp);
e099eba8
JO
761}
762
763static void process_stat_interval(u64 tstamp)
764{
765 if (scripting_ops && scripting_ops->process_stat_interval)
766 scripting_ops->process_stat_interval(tstamp);
767}
768
956ffd02
TZ
769static void setup_scripting(void)
770{
16c632de 771 setup_perl_scripting();
7e4b21b8 772 setup_python_scripting();
956ffd02
TZ
773}
774
d445dd2a
AH
775static int flush_scripting(void)
776{
2aaecfc5 777 return scripting_ops ? scripting_ops->flush_script() : 0;
d445dd2a
AH
778}
779
956ffd02
TZ
780static int cleanup_scripting(void)
781{
133dc4c3 782 pr_debug("\nperf script stopped\n");
3824a4e8 783
2aaecfc5 784 return scripting_ops ? scripting_ops->stop_script() : 0;
956ffd02
TZ
785}
786
809e9423 787static int process_sample_event(struct perf_tool *tool,
d20deb64 788 union perf_event *event,
8115d60c 789 struct perf_sample *sample,
9e69c210 790 struct perf_evsel *evsel,
743eb868 791 struct machine *machine)
5f9c39dc 792{
809e9423 793 struct perf_script *scr = container_of(tool, struct perf_script, tool);
e7984b7b 794 struct addr_location al;
5f9c39dc 795
1424dc96
DA
796 if (debug_mode) {
797 if (sample->time < last_timestamp) {
798 pr_err("Samples misordered, previous: %" PRIu64
799 " this: %" PRIu64 "\n", last_timestamp,
800 sample->time);
801 nr_unordered++;
e1889d75 802 }
1424dc96
DA
803 last_timestamp = sample->time;
804 return 0;
5f9c39dc 805 }
5d67be97 806
e44baa3e 807 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
e7984b7b
DA
808 pr_err("problem processing %d event, skipping it.\n",
809 event->header.type);
810 return -1;
811 }
812
813 if (al.filtered)
b91fc39f 814 goto out_put;
e7984b7b 815
5d67be97 816 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
b91fc39f 817 goto out_put;
5d67be97 818
2aaecfc5
JO
819 if (scripting_ops)
820 scripting_ops->process_event(event, sample, evsel, &al);
821 else
809e9423 822 process_event(scr, event, sample, evsel, &al);
2aaecfc5 823
b91fc39f
ACM
824out_put:
825 addr_location__put(&al);
5f9c39dc
FW
826 return 0;
827}
828
7ea95727
AH
829static int process_attr(struct perf_tool *tool, union perf_event *event,
830 struct perf_evlist **pevlist)
831{
832 struct perf_script *scr = container_of(tool, struct perf_script, tool);
833 struct perf_evlist *evlist;
834 struct perf_evsel *evsel, *pos;
835 int err;
836
837 err = perf_event__process_attr(tool, event, pevlist);
838 if (err)
839 return err;
840
841 evlist = *pevlist;
842 evsel = perf_evlist__last(*pevlist);
843
844 if (evsel->attr.type >= PERF_TYPE_MAX)
845 return 0;
846
0050f7aa 847 evlist__for_each(evlist, pos) {
7ea95727
AH
848 if (pos->attr.type == evsel->attr.type && pos != evsel)
849 return 0;
850 }
851
852 set_print_ip_opts(&evsel->attr);
853
d2b5a315
JO
854 if (evsel->attr.sample_type)
855 err = perf_evsel__check_attr(evsel, scr->session);
856
857 return err;
7ea95727
AH
858}
859
ad7ebb9a
NK
860static int process_comm_event(struct perf_tool *tool,
861 union perf_event *event,
862 struct perf_sample *sample,
863 struct machine *machine)
864{
865 struct thread *thread;
866 struct perf_script *script = container_of(tool, struct perf_script, tool);
867 struct perf_session *session = script->session;
06b234ec 868 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
869 int ret = -1;
870
871 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
872 if (thread == NULL) {
873 pr_debug("problem processing COMM event, skipping it.\n");
874 return -1;
875 }
876
877 if (perf_event__process_comm(tool, event, sample, machine) < 0)
878 goto out;
879
880 if (!evsel->attr.sample_id_all) {
881 sample->cpu = 0;
882 sample->time = 0;
883 sample->tid = event->comm.tid;
884 sample->pid = event->comm.pid;
885 }
886 print_sample_start(sample, thread, evsel);
887 perf_event__fprintf(event, stdout);
888 ret = 0;
ad7ebb9a 889out:
b91fc39f 890 thread__put(thread);
ad7ebb9a
NK
891 return ret;
892}
893
894static int process_fork_event(struct perf_tool *tool,
895 union perf_event *event,
896 struct perf_sample *sample,
897 struct machine *machine)
898{
899 struct thread *thread;
900 struct perf_script *script = container_of(tool, struct perf_script, tool);
901 struct perf_session *session = script->session;
06b234ec 902 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
903
904 if (perf_event__process_fork(tool, event, sample, machine) < 0)
905 return -1;
906
907 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
908 if (thread == NULL) {
909 pr_debug("problem processing FORK event, skipping it.\n");
910 return -1;
911 }
912
913 if (!evsel->attr.sample_id_all) {
914 sample->cpu = 0;
915 sample->time = event->fork.time;
916 sample->tid = event->fork.tid;
917 sample->pid = event->fork.pid;
918 }
919 print_sample_start(sample, thread, evsel);
920 perf_event__fprintf(event, stdout);
b91fc39f 921 thread__put(thread);
ad7ebb9a
NK
922
923 return 0;
924}
925static int process_exit_event(struct perf_tool *tool,
926 union perf_event *event,
927 struct perf_sample *sample,
928 struct machine *machine)
929{
b91fc39f 930 int err = 0;
ad7ebb9a
NK
931 struct thread *thread;
932 struct perf_script *script = container_of(tool, struct perf_script, tool);
933 struct perf_session *session = script->session;
06b234ec 934 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
935
936 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
937 if (thread == NULL) {
938 pr_debug("problem processing EXIT event, skipping it.\n");
939 return -1;
940 }
941
942 if (!evsel->attr.sample_id_all) {
943 sample->cpu = 0;
944 sample->time = 0;
53ff6bc3
AH
945 sample->tid = event->fork.tid;
946 sample->pid = event->fork.pid;
ad7ebb9a
NK
947 }
948 print_sample_start(sample, thread, evsel);
949 perf_event__fprintf(event, stdout);
950
951 if (perf_event__process_exit(tool, event, sample, machine) < 0)
b91fc39f 952 err = -1;
ad7ebb9a 953
b91fc39f
ACM
954 thread__put(thread);
955 return err;
ad7ebb9a
NK
956}
957
ba1ddf42
NK
958static int process_mmap_event(struct perf_tool *tool,
959 union perf_event *event,
960 struct perf_sample *sample,
961 struct machine *machine)
962{
963 struct thread *thread;
964 struct perf_script *script = container_of(tool, struct perf_script, tool);
965 struct perf_session *session = script->session;
06b234ec 966 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ba1ddf42
NK
967
968 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
969 return -1;
970
971 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
972 if (thread == NULL) {
973 pr_debug("problem processing MMAP event, skipping it.\n");
974 return -1;
975 }
976
977 if (!evsel->attr.sample_id_all) {
978 sample->cpu = 0;
979 sample->time = 0;
980 sample->tid = event->mmap.tid;
981 sample->pid = event->mmap.pid;
982 }
983 print_sample_start(sample, thread, evsel);
984 perf_event__fprintf(event, stdout);
b91fc39f 985 thread__put(thread);
ba1ddf42
NK
986 return 0;
987}
988
989static int process_mmap2_event(struct perf_tool *tool,
990 union perf_event *event,
991 struct perf_sample *sample,
992 struct machine *machine)
993{
994 struct thread *thread;
995 struct perf_script *script = container_of(tool, struct perf_script, tool);
996 struct perf_session *session = script->session;
06b234ec 997 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ba1ddf42
NK
998
999 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1000 return -1;
1001
1002 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1003 if (thread == NULL) {
1004 pr_debug("problem processing MMAP2 event, skipping it.\n");
1005 return -1;
1006 }
1007
1008 if (!evsel->attr.sample_id_all) {
1009 sample->cpu = 0;
1010 sample->time = 0;
1011 sample->tid = event->mmap2.tid;
1012 sample->pid = event->mmap2.pid;
1013 }
1014 print_sample_start(sample, thread, evsel);
1015 perf_event__fprintf(event, stdout);
b91fc39f 1016 thread__put(thread);
ba1ddf42
NK
1017 return 0;
1018}
1019
7c14898b
AH
1020static int process_switch_event(struct perf_tool *tool,
1021 union perf_event *event,
1022 struct perf_sample *sample,
1023 struct machine *machine)
1024{
1025 struct thread *thread;
1026 struct perf_script *script = container_of(tool, struct perf_script, tool);
1027 struct perf_session *session = script->session;
1028 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1029
1030 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1031 return -1;
1032
1033 thread = machine__findnew_thread(machine, sample->pid,
1034 sample->tid);
1035 if (thread == NULL) {
1036 pr_debug("problem processing SWITCH event, skipping it.\n");
1037 return -1;
1038 }
1039
1040 print_sample_start(sample, thread, evsel);
1041 perf_event__fprintf(event, stdout);
1042 thread__put(thread);
1043 return 0;
1044}
1045
1d037ca1 1046static void sig_handler(int sig __maybe_unused)
c239da3b
TZ
1047{
1048 session_done = 1;
1049}
1050
6f3e5eda 1051static int __cmd_script(struct perf_script *script)
5f9c39dc 1052{
6fcf7ddb
FW
1053 int ret;
1054
c239da3b
TZ
1055 signal(SIGINT, sig_handler);
1056
ad7ebb9a
NK
1057 /* override event processing functions */
1058 if (script->show_task_events) {
1059 script->tool.comm = process_comm_event;
1060 script->tool.fork = process_fork_event;
1061 script->tool.exit = process_exit_event;
1062 }
ba1ddf42
NK
1063 if (script->show_mmap_events) {
1064 script->tool.mmap = process_mmap_event;
1065 script->tool.mmap2 = process_mmap2_event;
1066 }
7c14898b
AH
1067 if (script->show_switch_events)
1068 script->tool.context_switch = process_switch_event;
ad7ebb9a 1069
b7b61cbe 1070 ret = perf_session__process_events(script->session);
6fcf7ddb 1071
6d8afb56 1072 if (debug_mode)
9486aa38 1073 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
6fcf7ddb
FW
1074
1075 return ret;
5f9c39dc
FW
1076}
1077
956ffd02
TZ
1078struct script_spec {
1079 struct list_head node;
1080 struct scripting_ops *ops;
1081 char spec[0];
1082};
1083
eccdfe2d 1084static LIST_HEAD(script_specs);
956ffd02
TZ
1085
1086static struct script_spec *script_spec__new(const char *spec,
1087 struct scripting_ops *ops)
1088{
1089 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1090
1091 if (s != NULL) {
1092 strcpy(s->spec, spec);
1093 s->ops = ops;
1094 }
1095
1096 return s;
1097}
1098
956ffd02
TZ
1099static void script_spec__add(struct script_spec *s)
1100{
1101 list_add_tail(&s->node, &script_specs);
1102}
1103
1104static struct script_spec *script_spec__find(const char *spec)
1105{
1106 struct script_spec *s;
1107
1108 list_for_each_entry(s, &script_specs, node)
1109 if (strcasecmp(s->spec, spec) == 0)
1110 return s;
1111 return NULL;
1112}
1113
1114static struct script_spec *script_spec__findnew(const char *spec,
1115 struct scripting_ops *ops)
1116{
1117 struct script_spec *s = script_spec__find(spec);
1118
1119 if (s)
1120 return s;
1121
1122 s = script_spec__new(spec, ops);
1123 if (!s)
466e2876 1124 return NULL;
956ffd02
TZ
1125
1126 script_spec__add(s);
1127
1128 return s;
956ffd02
TZ
1129}
1130
1131int script_spec_register(const char *spec, struct scripting_ops *ops)
1132{
1133 struct script_spec *s;
1134
1135 s = script_spec__find(spec);
1136 if (s)
1137 return -1;
1138
1139 s = script_spec__findnew(spec, ops);
1140 if (!s)
1141 return -1;
1142
1143 return 0;
1144}
1145
1146static struct scripting_ops *script_spec__lookup(const char *spec)
1147{
1148 struct script_spec *s = script_spec__find(spec);
1149 if (!s)
1150 return NULL;
1151
1152 return s->ops;
1153}
1154
1155static void list_available_languages(void)
1156{
1157 struct script_spec *s;
1158
1159 fprintf(stderr, "\n");
1160 fprintf(stderr, "Scripting language extensions (used in "
133dc4c3 1161 "perf script -s [spec:]script.[spec]):\n\n");
956ffd02
TZ
1162
1163 list_for_each_entry(s, &script_specs, node)
1164 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1165
1166 fprintf(stderr, "\n");
1167}
1168
1d037ca1
IT
1169static int parse_scriptname(const struct option *opt __maybe_unused,
1170 const char *str, int unset __maybe_unused)
956ffd02
TZ
1171{
1172 char spec[PATH_MAX];
1173 const char *script, *ext;
1174 int len;
1175
f526d68b 1176 if (strcmp(str, "lang") == 0) {
956ffd02 1177 list_available_languages();
f526d68b 1178 exit(0);
956ffd02
TZ
1179 }
1180
1181 script = strchr(str, ':');
1182 if (script) {
1183 len = script - str;
1184 if (len >= PATH_MAX) {
1185 fprintf(stderr, "invalid language specifier");
1186 return -1;
1187 }
1188 strncpy(spec, str, len);
1189 spec[len] = '\0';
1190 scripting_ops = script_spec__lookup(spec);
1191 if (!scripting_ops) {
1192 fprintf(stderr, "invalid language specifier");
1193 return -1;
1194 }
1195 script++;
1196 } else {
1197 script = str;
d1e95bb5 1198 ext = strrchr(script, '.');
956ffd02
TZ
1199 if (!ext) {
1200 fprintf(stderr, "invalid script extension");
1201 return -1;
1202 }
1203 scripting_ops = script_spec__lookup(++ext);
1204 if (!scripting_ops) {
1205 fprintf(stderr, "invalid script extension");
1206 return -1;
1207 }
1208 }
1209
1210 script_name = strdup(script);
1211
1212 return 0;
1213}
1214
1d037ca1
IT
1215static int parse_output_fields(const struct option *opt __maybe_unused,
1216 const char *arg, int unset __maybe_unused)
745f43e3
DA
1217{
1218 char *tok;
50ca19ae 1219 int i, imax = ARRAY_SIZE(all_output_options);
2c9e45f7 1220 int j;
745f43e3
DA
1221 int rc = 0;
1222 char *str = strdup(arg);
1424dc96 1223 int type = -1;
745f43e3
DA
1224
1225 if (!str)
1226 return -ENOMEM;
1227
2c9e45f7
DA
1228 /* first word can state for which event type the user is specifying
1229 * the fields. If no type exists, the specified fields apply to all
1230 * event types found in the file minus the invalid fields for a type.
1424dc96 1231 */
2c9e45f7
DA
1232 tok = strchr(str, ':');
1233 if (tok) {
1234 *tok = '\0';
1235 tok++;
1236 if (!strcmp(str, "hw"))
1237 type = PERF_TYPE_HARDWARE;
1238 else if (!strcmp(str, "sw"))
1239 type = PERF_TYPE_SOFTWARE;
1240 else if (!strcmp(str, "trace"))
1241 type = PERF_TYPE_TRACEPOINT;
0817a6a3
AS
1242 else if (!strcmp(str, "raw"))
1243 type = PERF_TYPE_RAW;
27cfef00
WN
1244 else if (!strcmp(str, "break"))
1245 type = PERF_TYPE_BREAKPOINT;
2c9e45f7
DA
1246 else {
1247 fprintf(stderr, "Invalid event type in field string.\n");
38efb539
RR
1248 rc = -EINVAL;
1249 goto out;
2c9e45f7
DA
1250 }
1251
1252 if (output[type].user_set)
1253 pr_warning("Overriding previous field request for %s events.\n",
1254 event_type(type));
1255
1256 output[type].fields = 0;
1257 output[type].user_set = true;
9cbdb702 1258 output[type].wildcard_set = false;
2c9e45f7
DA
1259
1260 } else {
1261 tok = str;
1262 if (strlen(str) == 0) {
1263 fprintf(stderr,
1264 "Cannot set fields to 'none' for all event types.\n");
1265 rc = -EINVAL;
1266 goto out;
1267 }
1268
1269 if (output_set_by_user())
1270 pr_warning("Overriding previous field request for all events.\n");
1271
1272 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1273 output[j].fields = 0;
1274 output[j].user_set = true;
9cbdb702 1275 output[j].wildcard_set = true;
2c9e45f7 1276 }
745f43e3
DA
1277 }
1278
400ea6d3 1279 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
745f43e3 1280 for (i = 0; i < imax; ++i) {
2c9e45f7 1281 if (strcmp(tok, all_output_options[i].str) == 0)
745f43e3 1282 break;
745f43e3 1283 }
400ea6d3
AH
1284 if (i == imax && strcmp(tok, "flags") == 0) {
1285 print_flags = true;
1286 continue;
1287 }
745f43e3 1288 if (i == imax) {
2c9e45f7 1289 fprintf(stderr, "Invalid field requested.\n");
745f43e3 1290 rc = -EINVAL;
2c9e45f7 1291 goto out;
745f43e3
DA
1292 }
1293
2c9e45f7
DA
1294 if (type == -1) {
1295 /* add user option to all events types for
1296 * which it is valid
1297 */
1298 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1299 if (output[j].invalid_fields & all_output_options[i].field) {
1300 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1301 all_output_options[i].str, event_type(j));
1302 } else
1303 output[j].fields |= all_output_options[i].field;
1304 }
1305 } else {
1306 if (output[type].invalid_fields & all_output_options[i].field) {
1307 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1308 all_output_options[i].str, event_type(type));
1309
1310 rc = -EINVAL;
1311 goto out;
1312 }
1313 output[type].fields |= all_output_options[i].field;
1314 }
745f43e3
DA
1315 }
1316
2c9e45f7
DA
1317 if (type >= 0) {
1318 if (output[type].fields == 0) {
1319 pr_debug("No fields requested for %s type. "
1320 "Events will not be displayed.\n", event_type(type));
1321 }
1322 }
745f43e3 1323
2c9e45f7 1324out:
745f43e3
DA
1325 free(str);
1326 return rc;
1327}
1328
008f29d3
SB
1329/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1330static int is_directory(const char *base_path, const struct dirent *dent)
1331{
1332 char path[PATH_MAX];
1333 struct stat st;
1334
1335 sprintf(path, "%s/%s", base_path, dent->d_name);
1336 if (stat(path, &st))
1337 return 0;
1338
1339 return S_ISDIR(st.st_mode);
1340}
1341
1342#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
4b9c0c59
TZ
1343 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1344 lang_next) \
008f29d3
SB
1345 if ((lang_dirent.d_type == DT_DIR || \
1346 (lang_dirent.d_type == DT_UNKNOWN && \
1347 is_directory(scripts_path, &lang_dirent))) && \
4b9c0c59
TZ
1348 (strcmp(lang_dirent.d_name, ".")) && \
1349 (strcmp(lang_dirent.d_name, "..")))
1350
008f29d3 1351#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
4b9c0c59
TZ
1352 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1353 script_next) \
008f29d3
SB
1354 if (script_dirent.d_type != DT_DIR && \
1355 (script_dirent.d_type != DT_UNKNOWN || \
1356 !is_directory(lang_path, &script_dirent)))
4b9c0c59
TZ
1357
1358
1359#define RECORD_SUFFIX "-record"
1360#define REPORT_SUFFIX "-report"
1361
1362struct script_desc {
1363 struct list_head node;
1364 char *name;
1365 char *half_liner;
1366 char *args;
1367};
1368
eccdfe2d 1369static LIST_HEAD(script_descs);
4b9c0c59
TZ
1370
1371static struct script_desc *script_desc__new(const char *name)
1372{
1373 struct script_desc *s = zalloc(sizeof(*s));
1374
b5b87312 1375 if (s != NULL && name)
4b9c0c59
TZ
1376 s->name = strdup(name);
1377
1378 return s;
1379}
1380
1381static void script_desc__delete(struct script_desc *s)
1382{
74cf249d
ACM
1383 zfree(&s->name);
1384 zfree(&s->half_liner);
1385 zfree(&s->args);
4b9c0c59
TZ
1386 free(s);
1387}
1388
1389static void script_desc__add(struct script_desc *s)
1390{
1391 list_add_tail(&s->node, &script_descs);
1392}
1393
1394static struct script_desc *script_desc__find(const char *name)
1395{
1396 struct script_desc *s;
1397
1398 list_for_each_entry(s, &script_descs, node)
1399 if (strcasecmp(s->name, name) == 0)
1400 return s;
1401 return NULL;
1402}
1403
1404static struct script_desc *script_desc__findnew(const char *name)
1405{
1406 struct script_desc *s = script_desc__find(name);
1407
1408 if (s)
1409 return s;
1410
1411 s = script_desc__new(name);
1412 if (!s)
1413 goto out_delete_desc;
1414
1415 script_desc__add(s);
1416
1417 return s;
1418
1419out_delete_desc:
1420 script_desc__delete(s);
1421
1422 return NULL;
1423}
1424
965bb6be 1425static const char *ends_with(const char *str, const char *suffix)
4b9c0c59
TZ
1426{
1427 size_t suffix_len = strlen(suffix);
965bb6be 1428 const char *p = str;
4b9c0c59
TZ
1429
1430 if (strlen(str) > suffix_len) {
1431 p = str + strlen(str) - suffix_len;
1432 if (!strncmp(p, suffix, suffix_len))
1433 return p;
1434 }
1435
1436 return NULL;
1437}
1438
4b9c0c59
TZ
1439static int read_script_info(struct script_desc *desc, const char *filename)
1440{
1441 char line[BUFSIZ], *p;
1442 FILE *fp;
1443
1444 fp = fopen(filename, "r");
1445 if (!fp)
1446 return -1;
1447
1448 while (fgets(line, sizeof(line), fp)) {
1449 p = ltrim(line);
1450 if (strlen(p) == 0)
1451 continue;
1452 if (*p != '#')
1453 continue;
1454 p++;
1455 if (strlen(p) && *p == '!')
1456 continue;
1457
1458 p = ltrim(p);
1459 if (strlen(p) && p[strlen(p) - 1] == '\n')
1460 p[strlen(p) - 1] = '\0';
1461
1462 if (!strncmp(p, "description:", strlen("description:"))) {
1463 p += strlen("description:");
1464 desc->half_liner = strdup(ltrim(p));
1465 continue;
1466 }
1467
1468 if (!strncmp(p, "args:", strlen("args:"))) {
1469 p += strlen("args:");
1470 desc->args = strdup(ltrim(p));
1471 continue;
1472 }
1473 }
1474
1475 fclose(fp);
1476
1477 return 0;
1478}
1479
38efb539
RR
1480static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1481{
1482 char *script_root, *str;
1483
1484 script_root = strdup(script_dirent->d_name);
1485 if (!script_root)
1486 return NULL;
1487
1488 str = (char *)ends_with(script_root, suffix);
1489 if (!str) {
1490 free(script_root);
1491 return NULL;
1492 }
1493
1494 *str = '\0';
1495 return script_root;
1496}
1497
1d037ca1
IT
1498static int list_available_scripts(const struct option *opt __maybe_unused,
1499 const char *s __maybe_unused,
1500 int unset __maybe_unused)
4b9c0c59
TZ
1501{
1502 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1503 char scripts_path[MAXPATHLEN];
1504 DIR *scripts_dir, *lang_dir;
1505 char script_path[MAXPATHLEN];
1506 char lang_path[MAXPATHLEN];
1507 struct script_desc *desc;
1508 char first_half[BUFSIZ];
1509 char *script_root;
4b9c0c59 1510
46113a54 1511 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
4b9c0c59
TZ
1512
1513 scripts_dir = opendir(scripts_path);
1514 if (!scripts_dir)
1515 return -1;
1516
008f29d3 1517 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
4b9c0c59
TZ
1518 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1519 lang_dirent.d_name);
1520 lang_dir = opendir(lang_path);
1521 if (!lang_dir)
1522 continue;
1523
008f29d3 1524 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
38efb539
RR
1525 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1526 if (script_root) {
4b9c0c59
TZ
1527 desc = script_desc__findnew(script_root);
1528 snprintf(script_path, MAXPATHLEN, "%s/%s",
1529 lang_path, script_dirent.d_name);
1530 read_script_info(desc, script_path);
38efb539 1531 free(script_root);
4b9c0c59 1532 }
4b9c0c59
TZ
1533 }
1534 }
1535
1536 fprintf(stdout, "List of available trace scripts:\n");
1537 list_for_each_entry(desc, &script_descs, node) {
1538 sprintf(first_half, "%s %s", desc->name,
1539 desc->args ? desc->args : "");
1540 fprintf(stdout, " %-36s %s\n", first_half,
1541 desc->half_liner ? desc->half_liner : "");
1542 }
1543
1544 exit(0);
1545}
1546
49e639e2
FT
1547/*
1548 * Some scripts specify the required events in their "xxx-record" file,
1549 * this function will check if the events in perf.data match those
1550 * mentioned in the "xxx-record".
1551 *
1552 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1553 * which is covered well now. And new parsing code should be added to
1554 * cover the future complexing formats like event groups etc.
1555 */
1556static int check_ev_match(char *dir_name, char *scriptname,
1557 struct perf_session *session)
1558{
1559 char filename[MAXPATHLEN], evname[128];
1560 char line[BUFSIZ], *p;
1561 struct perf_evsel *pos;
1562 int match, len;
1563 FILE *fp;
1564
1565 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1566
1567 fp = fopen(filename, "r");
1568 if (!fp)
1569 return -1;
1570
1571 while (fgets(line, sizeof(line), fp)) {
1572 p = ltrim(line);
1573 if (*p == '#')
1574 continue;
1575
1576 while (strlen(p)) {
1577 p = strstr(p, "-e");
1578 if (!p)
1579 break;
1580
1581 p += 2;
1582 p = ltrim(p);
1583 len = strcspn(p, " \t");
1584 if (!len)
1585 break;
1586
1587 snprintf(evname, len + 1, "%s", p);
1588
1589 match = 0;
0050f7aa 1590 evlist__for_each(session->evlist, pos) {
49e639e2
FT
1591 if (!strcmp(perf_evsel__name(pos), evname)) {
1592 match = 1;
1593 break;
1594 }
1595 }
1596
1597 if (!match) {
1598 fclose(fp);
1599 return -1;
1600 }
1601 }
1602 }
1603
1604 fclose(fp);
1605 return 0;
1606}
1607
e5f3705e
FT
1608/*
1609 * Return -1 if none is found, otherwise the actual scripts number.
1610 *
1611 * Currently the only user of this function is the script browser, which
1612 * will list all statically runnable scripts, select one, execute it and
1613 * show the output in a perf browser.
1614 */
1615int find_scripts(char **scripts_array, char **scripts_path_array)
1616{
1617 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
49e639e2 1618 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
e5f3705e 1619 DIR *scripts_dir, *lang_dir;
49e639e2 1620 struct perf_session *session;
f5fc1412
JO
1621 struct perf_data_file file = {
1622 .path = input_name,
1623 .mode = PERF_DATA_MODE_READ,
1624 };
e5f3705e
FT
1625 char *temp;
1626 int i = 0;
1627
f5fc1412 1628 session = perf_session__new(&file, false, NULL);
49e639e2
FT
1629 if (!session)
1630 return -1;
1631
46113a54 1632 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
e5f3705e
FT
1633
1634 scripts_dir = opendir(scripts_path);
49e639e2
FT
1635 if (!scripts_dir) {
1636 perf_session__delete(session);
e5f3705e 1637 return -1;
49e639e2 1638 }
e5f3705e
FT
1639
1640 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1641 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1642 lang_dirent.d_name);
1643#ifdef NO_LIBPERL
1644 if (strstr(lang_path, "perl"))
1645 continue;
1646#endif
1647#ifdef NO_LIBPYTHON
1648 if (strstr(lang_path, "python"))
1649 continue;
1650#endif
1651
1652 lang_dir = opendir(lang_path);
1653 if (!lang_dir)
1654 continue;
1655
1656 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1657 /* Skip those real time scripts: xxxtop.p[yl] */
1658 if (strstr(script_dirent.d_name, "top."))
1659 continue;
1660 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1661 script_dirent.d_name);
1662 temp = strchr(script_dirent.d_name, '.');
1663 snprintf(scripts_array[i],
1664 (temp - script_dirent.d_name) + 1,
1665 "%s", script_dirent.d_name);
49e639e2
FT
1666
1667 if (check_ev_match(lang_path,
1668 scripts_array[i], session))
1669 continue;
1670
e5f3705e
FT
1671 i++;
1672 }
49e639e2 1673 closedir(lang_dir);
e5f3705e
FT
1674 }
1675
49e639e2
FT
1676 closedir(scripts_dir);
1677 perf_session__delete(session);
e5f3705e
FT
1678 return i;
1679}
1680
3875294f
TZ
1681static char *get_script_path(const char *script_root, const char *suffix)
1682{
1683 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1684 char scripts_path[MAXPATHLEN];
1685 char script_path[MAXPATHLEN];
1686 DIR *scripts_dir, *lang_dir;
1687 char lang_path[MAXPATHLEN];
38efb539 1688 char *__script_root;
3875294f 1689
46113a54 1690 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3875294f
TZ
1691
1692 scripts_dir = opendir(scripts_path);
1693 if (!scripts_dir)
1694 return NULL;
1695
008f29d3 1696 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
3875294f
TZ
1697 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1698 lang_dirent.d_name);
1699 lang_dir = opendir(lang_path);
1700 if (!lang_dir)
1701 continue;
1702
008f29d3 1703 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
38efb539
RR
1704 __script_root = get_script_root(&script_dirent, suffix);
1705 if (__script_root && !strcmp(script_root, __script_root)) {
1706 free(__script_root);
946ef2a2
NK
1707 closedir(lang_dir);
1708 closedir(scripts_dir);
3875294f
TZ
1709 snprintf(script_path, MAXPATHLEN, "%s/%s",
1710 lang_path, script_dirent.d_name);
38efb539 1711 return strdup(script_path);
3875294f
TZ
1712 }
1713 free(__script_root);
1714 }
946ef2a2 1715 closedir(lang_dir);
3875294f 1716 }
946ef2a2 1717 closedir(scripts_dir);
3875294f 1718
38efb539 1719 return NULL;
3875294f
TZ
1720}
1721
b5b87312
TZ
1722static bool is_top_script(const char *script_path)
1723{
965bb6be 1724 return ends_with(script_path, "top") == NULL ? false : true;
b5b87312
TZ
1725}
1726
1727static int has_required_arg(char *script_path)
1728{
1729 struct script_desc *desc;
1730 int n_args = 0;
1731 char *p;
1732
1733 desc = script_desc__new(NULL);
1734
1735 if (read_script_info(desc, script_path))
1736 goto out;
1737
1738 if (!desc->args)
1739 goto out;
1740
1741 for (p = desc->args; *p; p++)
1742 if (*p == '<')
1743 n_args++;
1744out:
1745 script_desc__delete(desc);
1746
1747 return n_args;
1748}
1749
69b6470e
ACM
1750static int have_cmd(int argc, const char **argv)
1751{
1752 char **__argv = malloc(sizeof(const char *) * argc);
1753
1754 if (!__argv) {
1755 pr_err("malloc failed\n");
1756 return -1;
1757 }
1758
1759 memcpy(__argv, argv, sizeof(const char *) * argc);
1760 argc = parse_options(argc, (const char **)__argv, record_options,
1761 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1762 free(__argv);
5f9c39dc 1763
69b6470e
ACM
1764 system_wide = (argc == 0);
1765
1766 return 0;
1767}
1768
7322d6c9
JO
1769static void script__setup_sample_type(struct perf_script *script)
1770{
1771 struct perf_session *session = script->session;
1772 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1773
1774 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1775 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1776 (sample_type & PERF_SAMPLE_STACK_USER))
1777 callchain_param.record_mode = CALLCHAIN_DWARF;
1778 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1779 callchain_param.record_mode = CALLCHAIN_LBR;
1780 else
1781 callchain_param.record_mode = CALLCHAIN_FP;
1782 }
1783}
1784
e099eba8
JO
1785static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1786 union perf_event *event,
1787 struct perf_session *session)
1788{
1789 struct stat_round_event *round = &event->stat_round;
1790 struct perf_evsel *counter;
1791
1792 evlist__for_each(session->evlist, counter) {
1793 perf_stat_process_counter(&stat_config, counter);
1794 process_stat(counter, round->time);
1795 }
1796
1797 process_stat_interval(round->time);
1798 return 0;
1799}
1800
91a2c3d5
JO
1801static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1802 union perf_event *event,
1803 struct perf_session *session __maybe_unused)
1804{
1805 perf_event__read_stat_config(&stat_config, &event->stat_config);
1806 return 0;
1807}
1808
cfc8874a
JO
1809static int set_maps(struct perf_script *script)
1810{
1811 struct perf_evlist *evlist = script->session->evlist;
1812
1813 if (!script->cpus || !script->threads)
1814 return 0;
1815
1816 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1817 return -EINVAL;
1818
1819 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1820
1821 if (perf_evlist__alloc_stats(evlist, true))
1822 return -ENOMEM;
1823
1824 script->allocated = true;
1825 return 0;
1826}
1827
1828static
1829int process_thread_map_event(struct perf_tool *tool,
1830 union perf_event *event,
1831 struct perf_session *session __maybe_unused)
1832{
1833 struct perf_script *script = container_of(tool, struct perf_script, tool);
1834
1835 if (script->threads) {
1836 pr_warning("Extra thread map event, ignoring.\n");
1837 return 0;
1838 }
1839
1840 script->threads = thread_map__new_event(&event->thread_map);
1841 if (!script->threads)
1842 return -ENOMEM;
1843
1844 return set_maps(script);
1845}
1846
1847static
1848int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1849 union perf_event *event,
1850 struct perf_session *session __maybe_unused)
1851{
1852 struct perf_script *script = container_of(tool, struct perf_script, tool);
1853
1854 if (script->cpus) {
1855 pr_warning("Extra cpu map event, ignoring.\n");
1856 return 0;
1857 }
1858
1859 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1860 if (!script->cpus)
1861 return -ENOMEM;
1862
1863 return set_maps(script);
1864}
1865
69b6470e
ACM
1866int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1867{
1868 bool show_full_info = false;
e90debdd
JO
1869 bool header = false;
1870 bool header_only = false;
6cc870f0 1871 bool script_started = false;
69b6470e
ACM
1872 char *rec_script_path = NULL;
1873 char *rep_script_path = NULL;
1874 struct perf_session *session;
7a680eb9 1875 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
69b6470e
ACM
1876 char *script_path = NULL;
1877 const char **__argv;
6cc870f0 1878 int i, j, err = 0;
6f3e5eda
AH
1879 struct perf_script script = {
1880 .tool = {
1881 .sample = process_sample_event,
1882 .mmap = perf_event__process_mmap,
1883 .mmap2 = perf_event__process_mmap2,
1884 .comm = perf_event__process_comm,
1885 .exit = perf_event__process_exit,
1886 .fork = perf_event__process_fork,
7ea95727 1887 .attr = process_attr,
6f3e5eda
AH
1888 .tracing_data = perf_event__process_tracing_data,
1889 .build_id = perf_event__process_build_id,
7a680eb9
AH
1890 .id_index = perf_event__process_id_index,
1891 .auxtrace_info = perf_event__process_auxtrace_info,
1892 .auxtrace = perf_event__process_auxtrace,
1893 .auxtrace_error = perf_event__process_auxtrace_error,
e099eba8
JO
1894 .stat = perf_event__process_stat_event,
1895 .stat_round = process_stat_round_event,
91a2c3d5 1896 .stat_config = process_stat_config_event,
cfc8874a
JO
1897 .thread_map = process_thread_map_event,
1898 .cpu_map = process_cpu_map_event,
0a8cb85c 1899 .ordered_events = true,
6f3e5eda
AH
1900 .ordering_requires_timestamps = true,
1901 },
1902 };
06af0f2c
YS
1903 struct perf_data_file file = {
1904 .mode = PERF_DATA_MODE_READ,
1905 };
69b6470e 1906 const struct option options[] = {
5f9c39dc
FW
1907 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1908 "dump raw trace in ASCII"),
c0555642 1909 OPT_INCR('v', "verbose", &verbose,
69b6470e 1910 "be more verbose (show symbol address, etc)"),
4b9c0c59 1911 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 1912 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
1913 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1914 list_available_scripts),
956ffd02
TZ
1915 OPT_CALLBACK('s', "script", NULL, "name",
1916 "script file name (lang:script name, script name, or *)",
1917 parse_scriptname),
1918 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
133dc4c3 1919 "generate perf-script.xx script in specified language"),
69b6470e 1920 OPT_STRING('i', "input", &input_name, "file", "input file name"),
ffabd99e
FW
1921 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1922 "do various checks like samples ordering and lost events"),
e90debdd
JO
1923 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1924 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
c0230b2b
DA
1925 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1926 "file", "vmlinux pathname"),
1927 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1928 "file", "kallsyms pathname"),
1929 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1930 "When printing symbols do not display call chain"),
1931 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1932 "Look for files with symbols relative to this directory"),
06af0f2c 1933 OPT_CALLBACK('F', "fields", NULL, "str",
a978f2ab
AN
1934 "comma separated output fields prepend with 'type:'. "
1935 "Valid types: hw,sw,trace,raw. "
1936 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
dc323ce8 1937 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
317df650 1938 OPT_BOOLEAN('a', "all-cpus", &system_wide,
69b6470e 1939 "system-wide collection from all CPUs"),
36385be5
FT
1940 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1941 "only consider these symbols"),
c8e66720 1942 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
e7984b7b
DA
1943 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1944 "only display events for these comms"),
e03eaa40
DA
1945 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1946 "only consider symbols in these pids"),
1947 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1948 "only consider symbols in these tids"),
fbe96f29
SE
1949 OPT_BOOLEAN('I', "show-info", &show_full_info,
1950 "display extended information from perf.data file"),
0bc8d205
AN
1951 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1952 "Show the path of [kernel.kallsyms]"),
ad7ebb9a
NK
1953 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1954 "Show the fork/comm/exit events"),
ba1ddf42
NK
1955 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1956 "Show the mmap events"),
7c14898b
AH
1957 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1958 "Show context switch events (if recorded)"),
06af0f2c 1959 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
83e19860
AH
1960 OPT_BOOLEAN(0, "ns", &nanosecs,
1961 "Use 9 decimal places when displaying time"),
7a680eb9
AH
1962 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1963 "Instruction Tracing options",
1964 itrace_parse_synth_opts),
a9710ba0
AK
1965 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1966 "Show full source file name path for source lines"),
77e0070d
MD
1967 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1968 "Enable symbol demangling"),
1969 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1970 "Enable kernel symbol demangling"),
1971
1909629f 1972 OPT_END()
69b6470e 1973 };
40cae2b7
YS
1974 const char * const script_subcommands[] = { "record", "report", NULL };
1975 const char *script_usage[] = {
69b6470e
ACM
1976 "perf script [<options>]",
1977 "perf script [<options>] record <script> [<record-options>] <command>",
1978 "perf script [<options>] report <script> [script-args]",
1979 "perf script [<options>] <script> [<record-options>] <command>",
1980 "perf script [<options>] <top-script> [script-args]",
1981 NULL
1982 };
3875294f 1983
b5b87312
TZ
1984 setup_scripting();
1985
40cae2b7 1986 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
b5b87312
TZ
1987 PARSE_OPT_STOP_AT_NON_OPTION);
1988
f5fc1412
JO
1989 file.path = input_name;
1990
b5b87312
TZ
1991 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1992 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1993 if (!rec_script_path)
1994 return cmd_record(argc, argv, NULL);
3875294f
TZ
1995 }
1996
b5b87312
TZ
1997 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1998 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1999 if (!rep_script_path) {
3875294f 2000 fprintf(stderr,
b5b87312 2001 "Please specify a valid report script"
133dc4c3 2002 "(see 'perf script -l' for listing)\n");
3875294f
TZ
2003 return -1;
2004 }
3875294f
TZ
2005 }
2006
3c5b645f
AH
2007 if (itrace_synth_opts.callchain &&
2008 itrace_synth_opts.callchain_sz > scripting_max_stack)
2009 scripting_max_stack = itrace_synth_opts.callchain_sz;
2010
44e668c6 2011 /* make sure PERF_EXEC_PATH is set for scripts */
46113a54 2012 set_argv_exec_path(get_argv_exec_path());
44e668c6 2013
b5b87312 2014 if (argc && !script_name && !rec_script_path && !rep_script_path) {
a0cccc2e 2015 int live_pipe[2];
b5b87312 2016 int rep_args;
a0cccc2e
TZ
2017 pid_t pid;
2018
b5b87312
TZ
2019 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2020 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2021
2022 if (!rec_script_path && !rep_script_path) {
c7118369
NK
2023 usage_with_options_msg(script_usage, options,
2024 "Couldn't find script `%s'\n\n See perf"
133dc4c3 2025 " script -l for available scripts.\n", argv[0]);
a0cccc2e
TZ
2026 }
2027
b5b87312
TZ
2028 if (is_top_script(argv[0])) {
2029 rep_args = argc - 1;
2030 } else {
2031 int rec_args;
2032
2033 rep_args = has_required_arg(rep_script_path);
2034 rec_args = (argc - 1) - rep_args;
2035 if (rec_args < 0) {
c7118369
NK
2036 usage_with_options_msg(script_usage, options,
2037 "`%s' script requires options."
133dc4c3 2038 "\n\n See perf script -l for available "
b5b87312 2039 "scripts and options.\n", argv[0]);
b5b87312 2040 }
a0cccc2e
TZ
2041 }
2042
2043 if (pipe(live_pipe) < 0) {
2044 perror("failed to create pipe");
d54b1a9e 2045 return -1;
a0cccc2e
TZ
2046 }
2047
2048 pid = fork();
2049 if (pid < 0) {
2050 perror("failed to fork");
d54b1a9e 2051 return -1;
a0cccc2e
TZ
2052 }
2053
2054 if (!pid) {
b5b87312
TZ
2055 j = 0;
2056
a0cccc2e
TZ
2057 dup2(live_pipe[1], 1);
2058 close(live_pipe[0]);
2059
317df650
RR
2060 if (is_top_script(argv[0])) {
2061 system_wide = true;
2062 } else if (!system_wide) {
d54b1a9e
DA
2063 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2064 err = -1;
2065 goto out;
2066 }
317df650 2067 }
b5b87312
TZ
2068
2069 __argv = malloc((argc + 6) * sizeof(const char *));
d54b1a9e
DA
2070 if (!__argv) {
2071 pr_err("malloc failed\n");
2072 err = -ENOMEM;
2073 goto out;
2074 }
e8719adf 2075
b5b87312
TZ
2076 __argv[j++] = "/bin/sh";
2077 __argv[j++] = rec_script_path;
2078 if (system_wide)
2079 __argv[j++] = "-a";
2080 __argv[j++] = "-q";
2081 __argv[j++] = "-o";
2082 __argv[j++] = "-";
2083 for (i = rep_args + 1; i < argc; i++)
2084 __argv[j++] = argv[i];
2085 __argv[j++] = NULL;
a0cccc2e
TZ
2086
2087 execvp("/bin/sh", (char **)__argv);
e8719adf 2088 free(__argv);
a0cccc2e
TZ
2089 exit(-1);
2090 }
2091
2092 dup2(live_pipe[0], 0);
2093 close(live_pipe[1]);
2094
b5b87312 2095 __argv = malloc((argc + 4) * sizeof(const char *));
d54b1a9e
DA
2096 if (!__argv) {
2097 pr_err("malloc failed\n");
2098 err = -ENOMEM;
2099 goto out;
2100 }
2101
b5b87312
TZ
2102 j = 0;
2103 __argv[j++] = "/bin/sh";
2104 __argv[j++] = rep_script_path;
2105 for (i = 1; i < rep_args + 1; i++)
2106 __argv[j++] = argv[i];
2107 __argv[j++] = "-i";
2108 __argv[j++] = "-";
2109 __argv[j++] = NULL;
a0cccc2e
TZ
2110
2111 execvp("/bin/sh", (char **)__argv);
e8719adf 2112 free(__argv);
a0cccc2e
TZ
2113 exit(-1);
2114 }
2115
b5b87312
TZ
2116 if (rec_script_path)
2117 script_path = rec_script_path;
2118 if (rep_script_path)
2119 script_path = rep_script_path;
34c86ea9 2120
b5b87312 2121 if (script_path) {
b5b87312 2122 j = 0;
3875294f 2123
317df650
RR
2124 if (!rec_script_path)
2125 system_wide = false;
d54b1a9e
DA
2126 else if (!system_wide) {
2127 if (have_cmd(argc - 1, &argv[1]) != 0) {
2128 err = -1;
2129 goto out;
2130 }
2131 }
34c86ea9 2132
b5b87312 2133 __argv = malloc((argc + 2) * sizeof(const char *));
d54b1a9e
DA
2134 if (!__argv) {
2135 pr_err("malloc failed\n");
2136 err = -ENOMEM;
2137 goto out;
2138 }
2139
34c86ea9
TZ
2140 __argv[j++] = "/bin/sh";
2141 __argv[j++] = script_path;
2142 if (system_wide)
2143 __argv[j++] = "-a";
b5b87312 2144 for (i = 2; i < argc; i++)
34c86ea9
TZ
2145 __argv[j++] = argv[i];
2146 __argv[j++] = NULL;
3875294f
TZ
2147
2148 execvp("/bin/sh", (char **)__argv);
e8719adf 2149 free(__argv);
3875294f
TZ
2150 exit(-1);
2151 }
956ffd02 2152
cf4fee50
TZ
2153 if (!script_name)
2154 setup_pager();
5f9c39dc 2155
6f3e5eda 2156 session = perf_session__new(&file, false, &script.tool);
d8f66248 2157 if (session == NULL)
52e02834 2158 return -1;
d8f66248 2159
e90debdd
JO
2160 if (header || header_only) {
2161 perf_session__fprintf_info(session, stdout, show_full_info);
2162 if (header_only)
6cc870f0 2163 goto out_delete;
e90debdd
JO
2164 }
2165
0a7e6d1b 2166 if (symbol__init(&session->header.env) < 0)
38520dc3
NK
2167 goto out_delete;
2168
6f3e5eda 2169 script.session = session;
7322d6c9 2170 script__setup_sample_type(&script);
6f3e5eda 2171
7a680eb9
AH
2172 session->itrace_synth_opts = &itrace_synth_opts;
2173
5d67be97 2174 if (cpu_list) {
6cc870f0
NK
2175 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2176 if (err < 0)
2177 goto out_delete;
5d67be97
AB
2178 }
2179
1424dc96 2180 if (!no_callchain)
c0230b2b
DA
2181 symbol_conf.use_callchain = true;
2182 else
2183 symbol_conf.use_callchain = false;
2184
9ee67421
ACM
2185 if (session->tevent.pevent &&
2186 pevent_set_function_resolver(session->tevent.pevent,
ccb3a829
ACM
2187 machine__resolve_kernel_addr,
2188 &session->machines.host) < 0) {
2189 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2190 return -1;
2191 }
2192
956ffd02
TZ
2193 if (generate_script_lang) {
2194 struct stat perf_stat;
745f43e3
DA
2195 int input;
2196
2c9e45f7 2197 if (output_set_by_user()) {
745f43e3
DA
2198 fprintf(stderr,
2199 "custom fields not supported for generated scripts");
6cc870f0
NK
2200 err = -EINVAL;
2201 goto out_delete;
745f43e3 2202 }
956ffd02 2203
cc9784bd 2204 input = open(file.path, O_RDONLY); /* input_name */
956ffd02 2205 if (input < 0) {
6cc870f0 2206 err = -errno;
956ffd02 2207 perror("failed to open file");
6cc870f0 2208 goto out_delete;
956ffd02
TZ
2209 }
2210
2211 err = fstat(input, &perf_stat);
2212 if (err < 0) {
2213 perror("failed to stat file");
6cc870f0 2214 goto out_delete;
956ffd02
TZ
2215 }
2216
2217 if (!perf_stat.st_size) {
2218 fprintf(stderr, "zero-sized file, nothing to do!\n");
6cc870f0 2219 goto out_delete;
956ffd02
TZ
2220 }
2221
2222 scripting_ops = script_spec__lookup(generate_script_lang);
2223 if (!scripting_ops) {
2224 fprintf(stderr, "invalid language specifier");
6cc870f0
NK
2225 err = -ENOENT;
2226 goto out_delete;
956ffd02
TZ
2227 }
2228
29f5ffd3 2229 err = scripting_ops->generate_script(session->tevent.pevent,
da378962 2230 "perf-script");
6cc870f0 2231 goto out_delete;
956ffd02
TZ
2232 }
2233
2234 if (script_name) {
586bc5cc 2235 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02 2236 if (err)
6cc870f0 2237 goto out_delete;
133dc4c3 2238 pr_debug("perf script started with script %s\n\n", script_name);
6cc870f0 2239 script_started = true;
956ffd02
TZ
2240 }
2241
9cbdb702
DA
2242
2243 err = perf_session__check_output_opt(session);
2244 if (err < 0)
6cc870f0 2245 goto out_delete;
9cbdb702 2246
6f3e5eda 2247 err = __cmd_script(&script);
956ffd02 2248
d445dd2a
AH
2249 flush_scripting();
2250
6cc870f0 2251out_delete:
cfc8874a 2252 perf_evlist__free_stats(session->evlist);
d8f66248 2253 perf_session__delete(session);
6cc870f0
NK
2254
2255 if (script_started)
2256 cleanup_scripting();
956ffd02
TZ
2257out:
2258 return err;
5f9c39dc 2259}
This page took 0.337453 seconds and 5 git commands to generate.