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