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