xref: /openbmc/linux/tools/perf/builtin-script.c (revision 65db92e0965ab56e8031d5c804f26d5be0e47047)
1 #include "builtin.h"
2 
3 #include "perf.h"
4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include <subcmd/exec-cmd.h>
7 #include "util/header.h"
8 #include <subcmd/parse-options.h>
9 #include "util/perf_regs.h"
10 #include "util/session.h"
11 #include "util/tool.h"
12 #include "util/symbol.h"
13 #include "util/thread.h"
14 #include "util/trace-event.h"
15 #include "util/util.h"
16 #include "util/evlist.h"
17 #include "util/evsel.h"
18 #include "util/sort.h"
19 #include "util/data.h"
20 #include "util/auxtrace.h"
21 #include "util/cpumap.h"
22 #include "util/thread_map.h"
23 #include "util/stat.h"
24 #include "util/string2.h"
25 #include "util/thread-stack.h"
26 #include "util/time-utils.h"
27 #include "print_binary.h"
28 #include <linux/bitmap.h>
29 #include <linux/kernel.h>
30 #include <linux/stringify.h>
31 #include <linux/time64.h>
32 #include "asm/bug.h"
33 #include "util/mem-events.h"
34 #include "util/dump-insn.h"
35 #include <dirent.h>
36 #include <errno.h>
37 #include <inttypes.h>
38 #include <signal.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 
44 #include "sane_ctype.h"
45 
46 static char const		*script_name;
47 static char const		*generate_script_lang;
48 static bool			debug_mode;
49 static u64			last_timestamp;
50 static u64			nr_unordered;
51 static bool			no_callchain;
52 static bool			latency_format;
53 static bool			system_wide;
54 static bool			print_flags;
55 static bool			nanosecs;
56 static const char		*cpu_list;
57 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
58 static struct perf_stat_config	stat_config;
59 static int			max_blocks;
60 
61 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
62 
63 enum perf_output_field {
64 	PERF_OUTPUT_COMM            = 1U << 0,
65 	PERF_OUTPUT_TID             = 1U << 1,
66 	PERF_OUTPUT_PID             = 1U << 2,
67 	PERF_OUTPUT_TIME            = 1U << 3,
68 	PERF_OUTPUT_CPU             = 1U << 4,
69 	PERF_OUTPUT_EVNAME          = 1U << 5,
70 	PERF_OUTPUT_TRACE           = 1U << 6,
71 	PERF_OUTPUT_IP              = 1U << 7,
72 	PERF_OUTPUT_SYM             = 1U << 8,
73 	PERF_OUTPUT_DSO             = 1U << 9,
74 	PERF_OUTPUT_ADDR            = 1U << 10,
75 	PERF_OUTPUT_SYMOFFSET       = 1U << 11,
76 	PERF_OUTPUT_SRCLINE         = 1U << 12,
77 	PERF_OUTPUT_PERIOD          = 1U << 13,
78 	PERF_OUTPUT_IREGS	    = 1U << 14,
79 	PERF_OUTPUT_BRSTACK	    = 1U << 15,
80 	PERF_OUTPUT_BRSTACKSYM	    = 1U << 16,
81 	PERF_OUTPUT_DATA_SRC	    = 1U << 17,
82 	PERF_OUTPUT_WEIGHT	    = 1U << 18,
83 	PERF_OUTPUT_BPF_OUTPUT	    = 1U << 19,
84 	PERF_OUTPUT_CALLINDENT	    = 1U << 20,
85 	PERF_OUTPUT_INSN	    = 1U << 21,
86 	PERF_OUTPUT_INSNLEN	    = 1U << 22,
87 	PERF_OUTPUT_BRSTACKINSN	    = 1U << 23,
88 	PERF_OUTPUT_BRSTACKOFF	    = 1U << 24,
89 	PERF_OUTPUT_SYNTH           = 1U << 25,
90 	PERF_OUTPUT_PHYS_ADDR       = 1U << 26,
91 	PERF_OUTPUT_UREGS	    = 1U << 27,
92 };
93 
94 struct output_option {
95 	const char *str;
96 	enum perf_output_field field;
97 } all_output_options[] = {
98 	{.str = "comm",  .field = PERF_OUTPUT_COMM},
99 	{.str = "tid",   .field = PERF_OUTPUT_TID},
100 	{.str = "pid",   .field = PERF_OUTPUT_PID},
101 	{.str = "time",  .field = PERF_OUTPUT_TIME},
102 	{.str = "cpu",   .field = PERF_OUTPUT_CPU},
103 	{.str = "event", .field = PERF_OUTPUT_EVNAME},
104 	{.str = "trace", .field = PERF_OUTPUT_TRACE},
105 	{.str = "ip",    .field = PERF_OUTPUT_IP},
106 	{.str = "sym",   .field = PERF_OUTPUT_SYM},
107 	{.str = "dso",   .field = PERF_OUTPUT_DSO},
108 	{.str = "addr",  .field = PERF_OUTPUT_ADDR},
109 	{.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
110 	{.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
111 	{.str = "period", .field = PERF_OUTPUT_PERIOD},
112 	{.str = "iregs", .field = PERF_OUTPUT_IREGS},
113 	{.str = "uregs", .field = PERF_OUTPUT_UREGS},
114 	{.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
115 	{.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
116 	{.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
117 	{.str = "weight",   .field = PERF_OUTPUT_WEIGHT},
118 	{.str = "bpf-output",   .field = PERF_OUTPUT_BPF_OUTPUT},
119 	{.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
120 	{.str = "insn", .field = PERF_OUTPUT_INSN},
121 	{.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
122 	{.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
123 	{.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
124 	{.str = "synth", .field = PERF_OUTPUT_SYNTH},
125 	{.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
126 };
127 
128 enum {
129 	OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
130 	OUTPUT_TYPE_MAX
131 };
132 
133 /* default set to maintain compatibility with current format */
134 static struct {
135 	bool user_set;
136 	bool wildcard_set;
137 	unsigned int print_ip_opts;
138 	u64 fields;
139 	u64 invalid_fields;
140 } output[OUTPUT_TYPE_MAX] = {
141 
142 	[PERF_TYPE_HARDWARE] = {
143 		.user_set = false,
144 
145 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
146 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
147 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
148 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
149 			      PERF_OUTPUT_PERIOD,
150 
151 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
152 	},
153 
154 	[PERF_TYPE_SOFTWARE] = {
155 		.user_set = false,
156 
157 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
158 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
159 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
160 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
161 			      PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
162 
163 		.invalid_fields = PERF_OUTPUT_TRACE,
164 	},
165 
166 	[PERF_TYPE_TRACEPOINT] = {
167 		.user_set = false,
168 
169 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
170 				  PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
171 				  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
172 	},
173 
174 	[PERF_TYPE_RAW] = {
175 		.user_set = false,
176 
177 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
178 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
179 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
180 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
181 			      PERF_OUTPUT_PERIOD |  PERF_OUTPUT_ADDR |
182 			      PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
183 			      PERF_OUTPUT_PHYS_ADDR,
184 
185 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
186 	},
187 
188 	[PERF_TYPE_BREAKPOINT] = {
189 		.user_set = false,
190 
191 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
192 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
193 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
194 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
195 			      PERF_OUTPUT_PERIOD,
196 
197 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
198 	},
199 
200 	[OUTPUT_TYPE_SYNTH] = {
201 		.user_set = false,
202 
203 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
204 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
205 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
206 			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
207 			      PERF_OUTPUT_SYNTH,
208 
209 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
210 	},
211 };
212 
213 static inline int output_type(unsigned int type)
214 {
215 	switch (type) {
216 	case PERF_TYPE_SYNTH:
217 		return OUTPUT_TYPE_SYNTH;
218 	default:
219 		return type;
220 	}
221 }
222 
223 static inline unsigned int attr_type(unsigned int type)
224 {
225 	switch (type) {
226 	case OUTPUT_TYPE_SYNTH:
227 		return PERF_TYPE_SYNTH;
228 	default:
229 		return type;
230 	}
231 }
232 
233 static bool output_set_by_user(void)
234 {
235 	int j;
236 	for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
237 		if (output[j].user_set)
238 			return true;
239 	}
240 	return false;
241 }
242 
243 static const char *output_field2str(enum perf_output_field field)
244 {
245 	int i, imax = ARRAY_SIZE(all_output_options);
246 	const char *str = "";
247 
248 	for (i = 0; i < imax; ++i) {
249 		if (all_output_options[i].field == field) {
250 			str = all_output_options[i].str;
251 			break;
252 		}
253 	}
254 	return str;
255 }
256 
257 #define PRINT_FIELD(x)  (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
258 
259 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
260 				      u64 sample_type, const char *sample_msg,
261 				      enum perf_output_field field,
262 				      bool allow_user_set)
263 {
264 	struct perf_event_attr *attr = &evsel->attr;
265 	int type = output_type(attr->type);
266 	const char *evname;
267 
268 	if (attr->sample_type & sample_type)
269 		return 0;
270 
271 	if (output[type].user_set) {
272 		if (allow_user_set)
273 			return 0;
274 		evname = perf_evsel__name(evsel);
275 		pr_err("Samples for '%s' event do not have %s attribute set. "
276 		       "Cannot print '%s' field.\n",
277 		       evname, sample_msg, output_field2str(field));
278 		return -1;
279 	}
280 
281 	/* user did not ask for it explicitly so remove from the default list */
282 	output[type].fields &= ~field;
283 	evname = perf_evsel__name(evsel);
284 	pr_debug("Samples for '%s' event do not have %s attribute set. "
285 		 "Skipping '%s' field.\n",
286 		 evname, sample_msg, output_field2str(field));
287 
288 	return 0;
289 }
290 
291 static int perf_evsel__check_stype(struct perf_evsel *evsel,
292 				   u64 sample_type, const char *sample_msg,
293 				   enum perf_output_field field)
294 {
295 	return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
296 					  false);
297 }
298 
299 static int perf_evsel__check_attr(struct perf_evsel *evsel,
300 				  struct perf_session *session)
301 {
302 	struct perf_event_attr *attr = &evsel->attr;
303 	bool allow_user_set;
304 
305 	if (perf_header__has_feat(&session->header, HEADER_STAT))
306 		return 0;
307 
308 	allow_user_set = perf_header__has_feat(&session->header,
309 					       HEADER_AUXTRACE);
310 
311 	if (PRINT_FIELD(TRACE) &&
312 		!perf_session__has_traces(session, "record -R"))
313 		return -EINVAL;
314 
315 	if (PRINT_FIELD(IP)) {
316 		if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
317 					    PERF_OUTPUT_IP))
318 			return -EINVAL;
319 	}
320 
321 	if (PRINT_FIELD(ADDR) &&
322 		perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
323 					   PERF_OUTPUT_ADDR, allow_user_set))
324 		return -EINVAL;
325 
326 	if (PRINT_FIELD(DATA_SRC) &&
327 		perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
328 					PERF_OUTPUT_DATA_SRC))
329 		return -EINVAL;
330 
331 	if (PRINT_FIELD(WEIGHT) &&
332 		perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
333 					PERF_OUTPUT_WEIGHT))
334 		return -EINVAL;
335 
336 	if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
337 		pr_err("Display of symbols requested but neither sample IP nor "
338 			   "sample address\nis selected. Hence, no addresses to convert "
339 		       "to symbols.\n");
340 		return -EINVAL;
341 	}
342 	if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
343 		pr_err("Display of offsets requested but symbol is not"
344 		       "selected.\n");
345 		return -EINVAL;
346 	}
347 	if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
348 	    !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
349 		pr_err("Display of DSO requested but no address to convert.  Select\n"
350 		       "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
351 		return -EINVAL;
352 	}
353 	if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
354 		pr_err("Display of source line number requested but sample IP is not\n"
355 		       "selected. Hence, no address to lookup the source line number.\n");
356 		return -EINVAL;
357 	}
358 	if (PRINT_FIELD(BRSTACKINSN) &&
359 	    !(perf_evlist__combined_branch_type(session->evlist) &
360 	      PERF_SAMPLE_BRANCH_ANY)) {
361 		pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
362 		       "Hint: run 'perf record -b ...'\n");
363 		return -EINVAL;
364 	}
365 	if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
366 		perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
367 					PERF_OUTPUT_TID|PERF_OUTPUT_PID))
368 		return -EINVAL;
369 
370 	if (PRINT_FIELD(TIME) &&
371 		perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
372 					PERF_OUTPUT_TIME))
373 		return -EINVAL;
374 
375 	if (PRINT_FIELD(CPU) &&
376 		perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
377 					   PERF_OUTPUT_CPU, allow_user_set))
378 		return -EINVAL;
379 
380 	if (PRINT_FIELD(PERIOD) &&
381 		perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
382 					PERF_OUTPUT_PERIOD))
383 		return -EINVAL;
384 
385 	if (PRINT_FIELD(IREGS) &&
386 		perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
387 					PERF_OUTPUT_IREGS))
388 		return -EINVAL;
389 
390 	if (PRINT_FIELD(UREGS) &&
391 		perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
392 					PERF_OUTPUT_UREGS))
393 		return -EINVAL;
394 
395 	if (PRINT_FIELD(PHYS_ADDR) &&
396 		perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
397 					PERF_OUTPUT_PHYS_ADDR))
398 		return -EINVAL;
399 
400 	return 0;
401 }
402 
403 static void set_print_ip_opts(struct perf_event_attr *attr)
404 {
405 	unsigned int type = output_type(attr->type);
406 
407 	output[type].print_ip_opts = 0;
408 	if (PRINT_FIELD(IP))
409 		output[type].print_ip_opts |= EVSEL__PRINT_IP;
410 
411 	if (PRINT_FIELD(SYM))
412 		output[type].print_ip_opts |= EVSEL__PRINT_SYM;
413 
414 	if (PRINT_FIELD(DSO))
415 		output[type].print_ip_opts |= EVSEL__PRINT_DSO;
416 
417 	if (PRINT_FIELD(SYMOFFSET))
418 		output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
419 
420 	if (PRINT_FIELD(SRCLINE))
421 		output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
422 }
423 
424 /*
425  * verify all user requested events exist and the samples
426  * have the expected data
427  */
428 static int perf_session__check_output_opt(struct perf_session *session)
429 {
430 	unsigned int j;
431 	struct perf_evsel *evsel;
432 
433 	for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
434 		evsel = perf_session__find_first_evtype(session, attr_type(j));
435 
436 		/*
437 		 * even if fields is set to 0 (ie., show nothing) event must
438 		 * exist if user explicitly includes it on the command line
439 		 */
440 		if (!evsel && output[j].user_set && !output[j].wildcard_set &&
441 		    j != OUTPUT_TYPE_SYNTH) {
442 			pr_err("%s events do not exist. "
443 			       "Remove corresponding -F option to proceed.\n",
444 			       event_type(j));
445 			return -1;
446 		}
447 
448 		if (evsel && output[j].fields &&
449 			perf_evsel__check_attr(evsel, session))
450 			return -1;
451 
452 		if (evsel == NULL)
453 			continue;
454 
455 		set_print_ip_opts(&evsel->attr);
456 	}
457 
458 	if (!no_callchain) {
459 		bool use_callchain = false;
460 		bool not_pipe = false;
461 
462 		evlist__for_each_entry(session->evlist, evsel) {
463 			not_pipe = true;
464 			if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
465 				use_callchain = true;
466 				break;
467 			}
468 		}
469 		if (not_pipe && !use_callchain)
470 			symbol_conf.use_callchain = false;
471 	}
472 
473 	/*
474 	 * set default for tracepoints to print symbols only
475 	 * if callchains are present
476 	 */
477 	if (symbol_conf.use_callchain &&
478 	    !output[PERF_TYPE_TRACEPOINT].user_set) {
479 		struct perf_event_attr *attr;
480 
481 		j = PERF_TYPE_TRACEPOINT;
482 
483 		evlist__for_each_entry(session->evlist, evsel) {
484 			if (evsel->attr.type != j)
485 				continue;
486 
487 			attr = &evsel->attr;
488 
489 			if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
490 				output[j].fields |= PERF_OUTPUT_IP;
491 				output[j].fields |= PERF_OUTPUT_SYM;
492 				output[j].fields |= PERF_OUTPUT_DSO;
493 				set_print_ip_opts(attr);
494 				goto out;
495 			}
496 		}
497 	}
498 
499 out:
500 	return 0;
501 }
502 
503 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
504 				      struct perf_event_attr *attr, FILE *fp)
505 {
506 	struct regs_dump *regs = &sample->intr_regs;
507 	uint64_t mask = attr->sample_regs_intr;
508 	unsigned i = 0, r;
509 	int printed = 0;
510 
511 	if (!regs)
512 		return 0;
513 
514 	for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
515 		u64 val = regs->regs[i++];
516 		printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
517 	}
518 
519 	return printed;
520 }
521 
522 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
523 				      struct perf_event_attr *attr, FILE *fp)
524 {
525 	struct regs_dump *regs = &sample->user_regs;
526 	uint64_t mask = attr->sample_regs_user;
527 	unsigned i = 0, r;
528 	int printed = 0;
529 
530 	if (!regs || !regs->regs)
531 		return 0;
532 
533 	printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
534 
535 	for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
536 		u64 val = regs->regs[i++];
537 		printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
538 	}
539 
540 	return printed;
541 }
542 
543 static int perf_sample__fprintf_start(struct perf_sample *sample,
544 				      struct thread *thread,
545 				      struct perf_evsel *evsel, FILE *fp)
546 {
547 	struct perf_event_attr *attr = &evsel->attr;
548 	unsigned long secs;
549 	unsigned long long nsecs;
550 	int printed = 0;
551 
552 	if (PRINT_FIELD(COMM)) {
553 		if (latency_format)
554 			printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
555 		else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
556 			printed += fprintf(fp, "%s ", thread__comm_str(thread));
557 		else
558 			printed += fprintf(fp, "%16s ", thread__comm_str(thread));
559 	}
560 
561 	if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
562 		printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
563 	else if (PRINT_FIELD(PID))
564 		printed += fprintf(fp, "%5d ", sample->pid);
565 	else if (PRINT_FIELD(TID))
566 		printed += fprintf(fp, "%5d ", sample->tid);
567 
568 	if (PRINT_FIELD(CPU)) {
569 		if (latency_format)
570 			printed += fprintf(fp, "%3d ", sample->cpu);
571 		else
572 			printed += fprintf(fp, "[%03d] ", sample->cpu);
573 	}
574 
575 	if (PRINT_FIELD(TIME)) {
576 		nsecs = sample->time;
577 		secs = nsecs / NSEC_PER_SEC;
578 		nsecs -= secs * NSEC_PER_SEC;
579 
580 		if (nanosecs)
581 			printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
582 		else {
583 			char sample_time[32];
584 			timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
585 			printed += fprintf(fp, "%12s: ", sample_time);
586 		}
587 	}
588 
589 	return printed;
590 }
591 
592 static inline char
593 mispred_str(struct branch_entry *br)
594 {
595 	if (!(br->flags.mispred  || br->flags.predicted))
596 		return '-';
597 
598 	return br->flags.predicted ? 'P' : 'M';
599 }
600 
601 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
602 					struct thread *thread,
603 					struct perf_event_attr *attr, FILE *fp)
604 {
605 	struct branch_stack *br = sample->branch_stack;
606 	struct addr_location alf, alt;
607 	u64 i, from, to;
608 	int printed = 0;
609 
610 	if (!(br && br->nr))
611 		return 0;
612 
613 	for (i = 0; i < br->nr; i++) {
614 		from = br->entries[i].from;
615 		to   = br->entries[i].to;
616 
617 		if (PRINT_FIELD(DSO)) {
618 			memset(&alf, 0, sizeof(alf));
619 			memset(&alt, 0, sizeof(alt));
620 			thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
621 			thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
622 		}
623 
624 		printed += fprintf(fp, " 0x%"PRIx64, from);
625 		if (PRINT_FIELD(DSO)) {
626 			printed += fprintf(fp, "(");
627 			printed += map__fprintf_dsoname(alf.map, fp);
628 			printed += fprintf(fp, ")");
629 		}
630 
631 		printed += fprintf(fp, "/0x%"PRIx64, to);
632 		if (PRINT_FIELD(DSO)) {
633 			printed += fprintf(fp, "(");
634 			printed += map__fprintf_dsoname(alt.map, fp);
635 			printed += fprintf(fp, ")");
636 		}
637 
638 		printed += fprintf(fp, "/%c/%c/%c/%d ",
639 			mispred_str( br->entries + i),
640 			br->entries[i].flags.in_tx? 'X' : '-',
641 			br->entries[i].flags.abort? 'A' : '-',
642 			br->entries[i].flags.cycles);
643 	}
644 
645 	return printed;
646 }
647 
648 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
649 					   struct thread *thread,
650 					   struct perf_event_attr *attr, FILE *fp)
651 {
652 	struct branch_stack *br = sample->branch_stack;
653 	struct addr_location alf, alt;
654 	u64 i, from, to;
655 	int printed = 0;
656 
657 	if (!(br && br->nr))
658 		return 0;
659 
660 	for (i = 0; i < br->nr; i++) {
661 
662 		memset(&alf, 0, sizeof(alf));
663 		memset(&alt, 0, sizeof(alt));
664 		from = br->entries[i].from;
665 		to   = br->entries[i].to;
666 
667 		thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
668 		if (alf.map)
669 			alf.sym = map__find_symbol(alf.map, alf.addr);
670 
671 		thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
672 		if (alt.map)
673 			alt.sym = map__find_symbol(alt.map, alt.addr);
674 
675 		printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
676 		if (PRINT_FIELD(DSO)) {
677 			printed += fprintf(fp, "(");
678 			printed += map__fprintf_dsoname(alf.map, fp);
679 			printed += fprintf(fp, ")");
680 		}
681 		printed += fprintf(fp, "%c", '/');
682 		printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
683 		if (PRINT_FIELD(DSO)) {
684 			printed += fprintf(fp, "(");
685 			printed += map__fprintf_dsoname(alt.map, fp);
686 			printed += fprintf(fp, ")");
687 		}
688 		printed += fprintf(fp, "/%c/%c/%c/%d ",
689 			mispred_str( br->entries + i),
690 			br->entries[i].flags.in_tx? 'X' : '-',
691 			br->entries[i].flags.abort? 'A' : '-',
692 			br->entries[i].flags.cycles);
693 	}
694 
695 	return printed;
696 }
697 
698 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
699 					   struct thread *thread,
700 					   struct perf_event_attr *attr, FILE *fp)
701 {
702 	struct branch_stack *br = sample->branch_stack;
703 	struct addr_location alf, alt;
704 	u64 i, from, to;
705 	int printed = 0;
706 
707 	if (!(br && br->nr))
708 		return 0;
709 
710 	for (i = 0; i < br->nr; i++) {
711 
712 		memset(&alf, 0, sizeof(alf));
713 		memset(&alt, 0, sizeof(alt));
714 		from = br->entries[i].from;
715 		to   = br->entries[i].to;
716 
717 		thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
718 		if (alf.map && !alf.map->dso->adjust_symbols)
719 			from = map__map_ip(alf.map, from);
720 
721 		thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
722 		if (alt.map && !alt.map->dso->adjust_symbols)
723 			to = map__map_ip(alt.map, to);
724 
725 		printed += fprintf(fp, " 0x%"PRIx64, from);
726 		if (PRINT_FIELD(DSO)) {
727 			printed += fprintf(fp, "(");
728 			printed += map__fprintf_dsoname(alf.map, fp);
729 			printed += fprintf(fp, ")");
730 		}
731 		printed += fprintf(fp, "/0x%"PRIx64, to);
732 		if (PRINT_FIELD(DSO)) {
733 			printed += fprintf(fp, "(");
734 			printed += map__fprintf_dsoname(alt.map, fp);
735 			printed += fprintf(fp, ")");
736 		}
737 		printed += fprintf(fp, "/%c/%c/%c/%d ",
738 			mispred_str(br->entries + i),
739 			br->entries[i].flags.in_tx ? 'X' : '-',
740 			br->entries[i].flags.abort ? 'A' : '-',
741 			br->entries[i].flags.cycles);
742 	}
743 
744 	return printed;
745 }
746 #define MAXBB 16384UL
747 
748 static int grab_bb(u8 *buffer, u64 start, u64 end,
749 		    struct machine *machine, struct thread *thread,
750 		    bool *is64bit, u8 *cpumode, bool last)
751 {
752 	long offset, len;
753 	struct addr_location al;
754 	bool kernel;
755 
756 	if (!start || !end)
757 		return 0;
758 
759 	kernel = machine__kernel_ip(machine, start);
760 	if (kernel)
761 		*cpumode = PERF_RECORD_MISC_KERNEL;
762 	else
763 		*cpumode = PERF_RECORD_MISC_USER;
764 
765 	/*
766 	 * Block overlaps between kernel and user.
767 	 * This can happen due to ring filtering
768 	 * On Intel CPUs the entry into the kernel is filtered,
769 	 * but the exit is not. Let the caller patch it up.
770 	 */
771 	if (kernel != machine__kernel_ip(machine, end)) {
772 		printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
773 				start, end);
774 		return -ENXIO;
775 	}
776 
777 	memset(&al, 0, sizeof(al));
778 	if (end - start > MAXBB - MAXINSN) {
779 		if (last)
780 			printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
781 		else
782 			printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
783 		return 0;
784 	}
785 
786 	thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
787 	if (!al.map || !al.map->dso) {
788 		printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
789 		return 0;
790 	}
791 	if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
792 		printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
793 		return 0;
794 	}
795 
796 	/* Load maps to ensure dso->is_64_bit has been updated */
797 	map__load(al.map);
798 
799 	offset = al.map->map_ip(al.map, start);
800 	len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
801 				    end - start + MAXINSN);
802 
803 	*is64bit = al.map->dso->is_64_bit;
804 	if (len <= 0)
805 		printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
806 			start, end);
807 	return len;
808 }
809 
810 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
811 			    struct perf_insn *x, u8 *inbuf, int len,
812 			    int insn, FILE *fp)
813 {
814 	int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
815 			      dump_insn(x, ip, inbuf, len, NULL),
816 			      en->flags.predicted ? " PRED" : "",
817 			      en->flags.mispred ? " MISPRED" : "",
818 			      en->flags.in_tx ? " INTX" : "",
819 			      en->flags.abort ? " ABORT" : "");
820 	if (en->flags.cycles) {
821 		printed += fprintf(fp, " %d cycles", en->flags.cycles);
822 		if (insn)
823 			printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
824 	}
825 	return printed + fprintf(fp, "\n");
826 }
827 
828 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
829 			   u8 cpumode, int cpu, struct symbol **lastsym,
830 			   struct perf_event_attr *attr, FILE *fp)
831 {
832 	struct addr_location al;
833 	int off, printed = 0;
834 
835 	memset(&al, 0, sizeof(al));
836 
837 	thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
838 	if (!al.map)
839 		thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
840 				      addr, &al);
841 	if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
842 		return 0;
843 
844 	al.cpu = cpu;
845 	al.sym = NULL;
846 	if (al.map)
847 		al.sym = map__find_symbol(al.map, al.addr);
848 
849 	if (!al.sym)
850 		return 0;
851 
852 	if (al.addr < al.sym->end)
853 		off = al.addr - al.sym->start;
854 	else
855 		off = al.addr - al.map->start - al.sym->start;
856 	printed += fprintf(fp, "\t%s", al.sym->name);
857 	if (off)
858 		printed += fprintf(fp, "%+d", off);
859 	printed += fprintf(fp, ":");
860 	if (PRINT_FIELD(SRCLINE))
861 		printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
862 	printed += fprintf(fp, "\n");
863 	*lastsym = al.sym;
864 
865 	return printed;
866 }
867 
868 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
869 					    struct thread *thread,
870 					    struct perf_event_attr *attr,
871 					    struct machine *machine, FILE *fp)
872 {
873 	struct branch_stack *br = sample->branch_stack;
874 	u64 start, end;
875 	int i, insn, len, nr, ilen, printed = 0;
876 	struct perf_insn x;
877 	u8 buffer[MAXBB];
878 	unsigned off;
879 	struct symbol *lastsym = NULL;
880 
881 	if (!(br && br->nr))
882 		return 0;
883 	nr = br->nr;
884 	if (max_blocks && nr > max_blocks + 1)
885 		nr = max_blocks + 1;
886 
887 	x.thread = thread;
888 	x.cpu = sample->cpu;
889 
890 	printed += fprintf(fp, "%c", '\n');
891 
892 	/* Handle first from jump, of which we don't know the entry. */
893 	len = grab_bb(buffer, br->entries[nr-1].from,
894 			br->entries[nr-1].from,
895 			machine, thread, &x.is64bit, &x.cpumode, false);
896 	if (len > 0) {
897 		printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
898 					   x.cpumode, x.cpu, &lastsym, attr, fp);
899 		printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
900 					    &x, buffer, len, 0, fp);
901 	}
902 
903 	/* Print all blocks */
904 	for (i = nr - 2; i >= 0; i--) {
905 		if (br->entries[i].from || br->entries[i].to)
906 			pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
907 				 br->entries[i].from,
908 				 br->entries[i].to);
909 		start = br->entries[i + 1].to;
910 		end   = br->entries[i].from;
911 
912 		len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
913 		/* Patch up missing kernel transfers due to ring filters */
914 		if (len == -ENXIO && i > 0) {
915 			end = br->entries[--i].from;
916 			pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
917 			len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
918 		}
919 		if (len <= 0)
920 			continue;
921 
922 		insn = 0;
923 		for (off = 0;; off += ilen) {
924 			uint64_t ip = start + off;
925 
926 			printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
927 			if (ip == end) {
928 				printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
929 				break;
930 			} else {
931 				printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
932 						   dump_insn(&x, ip, buffer + off, len - off, &ilen));
933 				if (ilen == 0)
934 					break;
935 				insn++;
936 			}
937 		}
938 	}
939 
940 	/*
941 	 * Hit the branch? In this case we are already done, and the target
942 	 * has not been executed yet.
943 	 */
944 	if (br->entries[0].from == sample->ip)
945 		goto out;
946 	if (br->entries[0].flags.abort)
947 		goto out;
948 
949 	/*
950 	 * Print final block upto sample
951 	 */
952 	start = br->entries[0].to;
953 	end = sample->ip;
954 	len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
955 	printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
956 	if (len <= 0) {
957 		/* Print at least last IP if basic block did not work */
958 		len = grab_bb(buffer, sample->ip, sample->ip,
959 			      machine, thread, &x.is64bit, &x.cpumode, false);
960 		if (len <= 0)
961 			goto out;
962 
963 		printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
964 			dump_insn(&x, sample->ip, buffer, len, NULL));
965 		goto out;
966 	}
967 	for (off = 0; off <= end - start; off += ilen) {
968 		printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
969 				   dump_insn(&x, start + off, buffer + off, len - off, &ilen));
970 		if (ilen == 0)
971 			break;
972 	}
973 out:
974 	return printed;
975 }
976 
977 static int perf_sample__fprintf_addr(struct perf_sample *sample,
978 				     struct thread *thread,
979 				     struct perf_event_attr *attr, FILE *fp)
980 {
981 	struct addr_location al;
982 	int printed = fprintf(fp, "%16" PRIx64, sample->addr);
983 
984 	if (!sample_addr_correlates_sym(attr))
985 		goto out;
986 
987 	thread__resolve(thread, &al, sample);
988 
989 	if (PRINT_FIELD(SYM)) {
990 		printed += fprintf(fp, " ");
991 		if (PRINT_FIELD(SYMOFFSET))
992 			printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
993 		else
994 			printed += symbol__fprintf_symname(al.sym, fp);
995 	}
996 
997 	if (PRINT_FIELD(DSO)) {
998 		printed += fprintf(fp, " (");
999 		printed += map__fprintf_dsoname(al.map, fp);
1000 		printed += fprintf(fp, ")");
1001 	}
1002 out:
1003 	return printed;
1004 }
1005 
1006 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1007 					   struct perf_evsel *evsel,
1008 					   struct thread *thread,
1009 					   struct addr_location *al, FILE *fp)
1010 {
1011 	struct perf_event_attr *attr = &evsel->attr;
1012 	size_t depth = thread_stack__depth(thread);
1013 	struct addr_location addr_al;
1014 	const char *name = NULL;
1015 	static int spacing;
1016 	int len = 0;
1017 	u64 ip = 0;
1018 
1019 	/*
1020 	 * The 'return' has already been popped off the stack so the depth has
1021 	 * to be adjusted to match the 'call'.
1022 	 */
1023 	if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1024 		depth += 1;
1025 
1026 	if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1027 		if (sample_addr_correlates_sym(attr)) {
1028 			thread__resolve(thread, &addr_al, sample);
1029 			if (addr_al.sym)
1030 				name = addr_al.sym->name;
1031 			else
1032 				ip = sample->addr;
1033 		} else {
1034 			ip = sample->addr;
1035 		}
1036 	} else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1037 		if (al->sym)
1038 			name = al->sym->name;
1039 		else
1040 			ip = sample->ip;
1041 	}
1042 
1043 	if (name)
1044 		len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1045 	else if (ip)
1046 		len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1047 
1048 	if (len < 0)
1049 		return len;
1050 
1051 	/*
1052 	 * Try to keep the output length from changing frequently so that the
1053 	 * output lines up more nicely.
1054 	 */
1055 	if (len > spacing || (len && len < spacing - 52))
1056 		spacing = round_up(len + 4, 32);
1057 
1058 	if (len < spacing)
1059 		len += fprintf(fp, "%*s", spacing - len, "");
1060 
1061 	return len;
1062 }
1063 
1064 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1065 				     struct perf_event_attr *attr,
1066 				     struct thread *thread,
1067 				     struct machine *machine, FILE *fp)
1068 {
1069 	int printed = 0;
1070 
1071 	if (PRINT_FIELD(INSNLEN))
1072 		printed += fprintf(fp, " ilen: %d", sample->insn_len);
1073 	if (PRINT_FIELD(INSN)) {
1074 		int i;
1075 
1076 		printed += fprintf(fp, " insn:");
1077 		for (i = 0; i < sample->insn_len; i++)
1078 			printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1079 	}
1080 	if (PRINT_FIELD(BRSTACKINSN))
1081 		printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1082 
1083 	return printed;
1084 }
1085 
1086 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1087 				    struct perf_evsel *evsel,
1088 				    struct thread *thread,
1089 				    struct addr_location *al,
1090 				    struct machine *machine, FILE *fp)
1091 {
1092 	struct perf_event_attr *attr = &evsel->attr;
1093 	unsigned int type = output_type(attr->type);
1094 	bool print_srcline_last = false;
1095 	int printed = 0;
1096 
1097 	if (PRINT_FIELD(CALLINDENT))
1098 		printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
1099 
1100 	/* print branch_from information */
1101 	if (PRINT_FIELD(IP)) {
1102 		unsigned int print_opts = output[type].print_ip_opts;
1103 		struct callchain_cursor *cursor = NULL;
1104 
1105 		if (symbol_conf.use_callchain && sample->callchain &&
1106 		    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1107 					      sample, NULL, NULL, scripting_max_stack) == 0)
1108 			cursor = &callchain_cursor;
1109 
1110 		if (cursor == NULL) {
1111 			printed += fprintf(fp, " ");
1112 			if (print_opts & EVSEL__PRINT_SRCLINE) {
1113 				print_srcline_last = true;
1114 				print_opts &= ~EVSEL__PRINT_SRCLINE;
1115 			}
1116 		} else
1117 			printed += fprintf(fp, "\n");
1118 
1119 		printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
1120 	}
1121 
1122 	/* print branch_to information */
1123 	if (PRINT_FIELD(ADDR) ||
1124 	    ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
1125 	     !output[type].user_set)) {
1126 		printed += fprintf(fp, " => ");
1127 		printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1128 	}
1129 
1130 	if (print_srcline_last)
1131 		printed += map__fprintf_srcline(al->map, al->addr, "\n  ", fp);
1132 
1133 	printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1134 	return printed + fprintf(fp, "\n");
1135 }
1136 
1137 static struct {
1138 	u32 flags;
1139 	const char *name;
1140 } sample_flags[] = {
1141 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1142 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1143 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1144 	{PERF_IP_FLAG_BRANCH, "jmp"},
1145 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1146 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1147 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1148 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1149 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1150 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |	PERF_IP_FLAG_INTERRUPT, "hw int"},
1151 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1152 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1153 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1154 	{0, NULL}
1155 };
1156 
1157 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1158 {
1159 	const char *chars = PERF_IP_FLAG_CHARS;
1160 	const int n = strlen(PERF_IP_FLAG_CHARS);
1161 	bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1162 	const char *name = NULL;
1163 	char str[33];
1164 	int i, pos = 0;
1165 
1166 	for (i = 0; sample_flags[i].name ; i++) {
1167 		if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1168 			name = sample_flags[i].name;
1169 			break;
1170 		}
1171 	}
1172 
1173 	for (i = 0; i < n; i++, flags >>= 1) {
1174 		if (flags & 1)
1175 			str[pos++] = chars[i];
1176 	}
1177 	for (; i < 32; i++, flags >>= 1) {
1178 		if (flags & 1)
1179 			str[pos++] = '?';
1180 	}
1181 	str[pos] = 0;
1182 
1183 	if (name)
1184 		return fprintf(fp, "  %-7s%4s ", name, in_tx ? "(x)" : "");
1185 
1186 	return fprintf(fp, "  %-11s ", str);
1187 }
1188 
1189 struct printer_data {
1190 	int line_no;
1191 	bool hit_nul;
1192 	bool is_printable;
1193 };
1194 
1195 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1196 				      unsigned int val,
1197 				      void *extra, FILE *fp)
1198 {
1199 	unsigned char ch = (unsigned char)val;
1200 	struct printer_data *printer_data = extra;
1201 	int printed = 0;
1202 
1203 	switch (op) {
1204 	case BINARY_PRINT_DATA_BEGIN:
1205 		printed += fprintf(fp, "\n");
1206 		break;
1207 	case BINARY_PRINT_LINE_BEGIN:
1208 		printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1209 						        "           ");
1210 		break;
1211 	case BINARY_PRINT_ADDR:
1212 		printed += fprintf(fp, " %04x:", val);
1213 		break;
1214 	case BINARY_PRINT_NUM_DATA:
1215 		printed += fprintf(fp, " %02x", val);
1216 		break;
1217 	case BINARY_PRINT_NUM_PAD:
1218 		printed += fprintf(fp, "   ");
1219 		break;
1220 	case BINARY_PRINT_SEP:
1221 		printed += fprintf(fp, "  ");
1222 		break;
1223 	case BINARY_PRINT_CHAR_DATA:
1224 		if (printer_data->hit_nul && ch)
1225 			printer_data->is_printable = false;
1226 
1227 		if (!isprint(ch)) {
1228 			printed += fprintf(fp, "%c", '.');
1229 
1230 			if (!printer_data->is_printable)
1231 				break;
1232 
1233 			if (ch == '\0')
1234 				printer_data->hit_nul = true;
1235 			else
1236 				printer_data->is_printable = false;
1237 		} else {
1238 			printed += fprintf(fp, "%c", ch);
1239 		}
1240 		break;
1241 	case BINARY_PRINT_CHAR_PAD:
1242 		printed += fprintf(fp, " ");
1243 		break;
1244 	case BINARY_PRINT_LINE_END:
1245 		printed += fprintf(fp, "\n");
1246 		printer_data->line_no++;
1247 		break;
1248 	case BINARY_PRINT_DATA_END:
1249 	default:
1250 		break;
1251 	}
1252 
1253 	return printed;
1254 }
1255 
1256 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1257 {
1258 	unsigned int nr_bytes = sample->raw_size;
1259 	struct printer_data printer_data = {0, false, true};
1260 	int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1261 				      sample__fprintf_bpf_output, &printer_data, fp);
1262 
1263 	if (printer_data.is_printable && printer_data.hit_nul)
1264 		printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1265 
1266 	return printed;
1267 }
1268 
1269 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1270 {
1271 	if (len > 0 && len < spacing)
1272 		return fprintf(fp, "%*s", spacing - len, "");
1273 
1274 	return 0;
1275 }
1276 
1277 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1278 {
1279 	return perf_sample__fprintf_spacing(len, 34, fp);
1280 }
1281 
1282 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1283 {
1284 	struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1285 	int len;
1286 
1287 	if (perf_sample__bad_synth_size(sample, *data))
1288 		return 0;
1289 
1290 	len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1291 		     data->ip, le64_to_cpu(data->payload));
1292 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1293 }
1294 
1295 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1296 {
1297 	struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1298 	int len;
1299 
1300 	if (perf_sample__bad_synth_size(sample, *data))
1301 		return 0;
1302 
1303 	len = fprintf(fp, " hints: %#x extensions: %#x ",
1304 		      data->hints, data->extensions);
1305 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1306 }
1307 
1308 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1309 {
1310 	struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1311 	int len;
1312 
1313 	if (perf_sample__bad_synth_size(sample, *data))
1314 		return 0;
1315 
1316 	len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1317 		      data->hw, data->cstate, data->subcstate);
1318 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1319 }
1320 
1321 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1322 {
1323 	struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1324 	int len;
1325 
1326 	if (perf_sample__bad_synth_size(sample, *data))
1327 		return 0;
1328 
1329 	len = fprintf(fp, " IP: %u ", data->ip);
1330 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1331 }
1332 
1333 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1334 {
1335 	struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1336 	int len;
1337 
1338 	if (perf_sample__bad_synth_size(sample, *data))
1339 		return 0;
1340 
1341 	len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1342 		     data->deepest_cstate, data->last_cstate,
1343 		     data->wake_reason);
1344 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1345 }
1346 
1347 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1348 {
1349 	struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1350 	unsigned int percent, freq;
1351 	int len;
1352 
1353 	if (perf_sample__bad_synth_size(sample, *data))
1354 		return 0;
1355 
1356 	freq = (le32_to_cpu(data->freq) + 500) / 1000;
1357 	len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1358 	if (data->max_nonturbo) {
1359 		percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1360 		len += fprintf(fp, "(%3u%%) ", percent);
1361 	}
1362 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1363 }
1364 
1365 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1366 				      struct perf_evsel *evsel, FILE *fp)
1367 {
1368 	switch (evsel->attr.config) {
1369 	case PERF_SYNTH_INTEL_PTWRITE:
1370 		return perf_sample__fprintf_synth_ptwrite(sample, fp);
1371 	case PERF_SYNTH_INTEL_MWAIT:
1372 		return perf_sample__fprintf_synth_mwait(sample, fp);
1373 	case PERF_SYNTH_INTEL_PWRE:
1374 		return perf_sample__fprintf_synth_pwre(sample, fp);
1375 	case PERF_SYNTH_INTEL_EXSTOP:
1376 		return perf_sample__fprintf_synth_exstop(sample, fp);
1377 	case PERF_SYNTH_INTEL_PWRX:
1378 		return perf_sample__fprintf_synth_pwrx(sample, fp);
1379 	case PERF_SYNTH_INTEL_CBR:
1380 		return perf_sample__fprintf_synth_cbr(sample, fp);
1381 	default:
1382 		break;
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 struct perf_script {
1389 	struct perf_tool	tool;
1390 	struct perf_session	*session;
1391 	bool			show_task_events;
1392 	bool			show_mmap_events;
1393 	bool			show_switch_events;
1394 	bool			show_namespace_events;
1395 	bool			allocated;
1396 	struct cpu_map		*cpus;
1397 	struct thread_map	*threads;
1398 	int			name_width;
1399 	const char              *time_str;
1400 	struct perf_time_interval ptime;
1401 };
1402 
1403 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1404 {
1405 	struct perf_evsel *evsel;
1406 	int max = 0;
1407 
1408 	evlist__for_each_entry(evlist, evsel) {
1409 		int len = strlen(perf_evsel__name(evsel));
1410 
1411 		max = MAX(len, max);
1412 	}
1413 
1414 	return max;
1415 }
1416 
1417 static int data_src__fprintf(u64 data_src, FILE *fp)
1418 {
1419 	struct mem_info mi = { .data_src.val = data_src };
1420 	char decode[100];
1421 	char out[100];
1422 	static int maxlen;
1423 	int len;
1424 
1425 	perf_script__meminfo_scnprintf(decode, 100, &mi);
1426 
1427 	len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1428 	if (maxlen < len)
1429 		maxlen = len;
1430 
1431 	return fprintf(fp, "%-*s", maxlen, out);
1432 }
1433 
1434 static void process_event(struct perf_script *script,
1435 			  struct perf_sample *sample, struct perf_evsel *evsel,
1436 			  struct addr_location *al,
1437 			  struct machine *machine)
1438 {
1439 	struct thread *thread = al->thread;
1440 	struct perf_event_attr *attr = &evsel->attr;
1441 	unsigned int type = output_type(attr->type);
1442 	FILE *fp = stdout;
1443 
1444 	if (output[type].fields == 0)
1445 		return;
1446 
1447 	perf_sample__fprintf_start(sample, thread, evsel, fp);
1448 
1449 	if (PRINT_FIELD(PERIOD))
1450 		printf("%10" PRIu64 " ", sample->period);
1451 
1452 	if (PRINT_FIELD(EVNAME)) {
1453 		const char *evname = perf_evsel__name(evsel);
1454 
1455 		if (!script->name_width)
1456 			script->name_width = perf_evlist__max_name_len(script->session->evlist);
1457 
1458 		printf("%*s: ", script->name_width,
1459 		       evname ? evname : "[unknown]");
1460 	}
1461 
1462 	if (print_flags)
1463 		perf_sample__fprintf_flags(sample->flags, fp);
1464 
1465 	if (is_bts_event(attr)) {
1466 		perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
1467 		return;
1468 	}
1469 
1470 	if (PRINT_FIELD(TRACE))
1471 		event_format__print(evsel->tp_format, sample->cpu,
1472 				    sample->raw_data, sample->raw_size);
1473 
1474 	if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1475 		perf_sample__fprintf_synth(sample, evsel, fp);
1476 
1477 	if (PRINT_FIELD(ADDR))
1478 		perf_sample__fprintf_addr(sample, thread, attr, fp);
1479 
1480 	if (PRINT_FIELD(DATA_SRC))
1481 		data_src__fprintf(sample->data_src, fp);
1482 
1483 	if (PRINT_FIELD(WEIGHT))
1484 		fprintf(fp, "%16" PRIu64, sample->weight);
1485 
1486 	if (PRINT_FIELD(IP)) {
1487 		struct callchain_cursor *cursor = NULL;
1488 
1489 		if (symbol_conf.use_callchain && sample->callchain &&
1490 		    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1491 					      sample, NULL, NULL, scripting_max_stack) == 0)
1492 			cursor = &callchain_cursor;
1493 
1494 		fputc(cursor ? '\n' : ' ', fp);
1495 		sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
1496 	}
1497 
1498 	if (PRINT_FIELD(IREGS))
1499 		perf_sample__fprintf_iregs(sample, attr, fp);
1500 
1501 	if (PRINT_FIELD(UREGS))
1502 		perf_sample__fprintf_uregs(sample, attr, fp);
1503 
1504 	if (PRINT_FIELD(BRSTACK))
1505 		perf_sample__fprintf_brstack(sample, thread, attr, fp);
1506 	else if (PRINT_FIELD(BRSTACKSYM))
1507 		perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
1508 	else if (PRINT_FIELD(BRSTACKOFF))
1509 		perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
1510 
1511 	if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1512 		perf_sample__fprintf_bpf_output(sample, fp);
1513 	perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1514 
1515 	if (PRINT_FIELD(PHYS_ADDR))
1516 		printf("%16" PRIx64, sample->phys_addr);
1517 	printf("\n");
1518 }
1519 
1520 static struct scripting_ops	*scripting_ops;
1521 
1522 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1523 {
1524 	int nthreads = thread_map__nr(counter->threads);
1525 	int ncpus = perf_evsel__nr_cpus(counter);
1526 	int cpu, thread;
1527 	static int header_printed;
1528 
1529 	if (counter->system_wide)
1530 		nthreads = 1;
1531 
1532 	if (!header_printed) {
1533 		printf("%3s %8s %15s %15s %15s %15s %s\n",
1534 		       "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1535 		header_printed = 1;
1536 	}
1537 
1538 	for (thread = 0; thread < nthreads; thread++) {
1539 		for (cpu = 0; cpu < ncpus; cpu++) {
1540 			struct perf_counts_values *counts;
1541 
1542 			counts = perf_counts(counter->counts, cpu, thread);
1543 
1544 			printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1545 				counter->cpus->map[cpu],
1546 				thread_map__pid(counter->threads, thread),
1547 				counts->val,
1548 				counts->ena,
1549 				counts->run,
1550 				tstamp,
1551 				perf_evsel__name(counter));
1552 		}
1553 	}
1554 }
1555 
1556 static void process_stat(struct perf_evsel *counter, u64 tstamp)
1557 {
1558 	if (scripting_ops && scripting_ops->process_stat)
1559 		scripting_ops->process_stat(&stat_config, counter, tstamp);
1560 	else
1561 		__process_stat(counter, tstamp);
1562 }
1563 
1564 static void process_stat_interval(u64 tstamp)
1565 {
1566 	if (scripting_ops && scripting_ops->process_stat_interval)
1567 		scripting_ops->process_stat_interval(tstamp);
1568 }
1569 
1570 static void setup_scripting(void)
1571 {
1572 	setup_perl_scripting();
1573 	setup_python_scripting();
1574 }
1575 
1576 static int flush_scripting(void)
1577 {
1578 	return scripting_ops ? scripting_ops->flush_script() : 0;
1579 }
1580 
1581 static int cleanup_scripting(void)
1582 {
1583 	pr_debug("\nperf script stopped\n");
1584 
1585 	return scripting_ops ? scripting_ops->stop_script() : 0;
1586 }
1587 
1588 static int process_sample_event(struct perf_tool *tool,
1589 				union perf_event *event,
1590 				struct perf_sample *sample,
1591 				struct perf_evsel *evsel,
1592 				struct machine *machine)
1593 {
1594 	struct perf_script *scr = container_of(tool, struct perf_script, tool);
1595 	struct addr_location al;
1596 
1597 	if (perf_time__skip_sample(&scr->ptime, sample->time))
1598 		return 0;
1599 
1600 	if (debug_mode) {
1601 		if (sample->time < last_timestamp) {
1602 			pr_err("Samples misordered, previous: %" PRIu64
1603 				" this: %" PRIu64 "\n", last_timestamp,
1604 				sample->time);
1605 			nr_unordered++;
1606 		}
1607 		last_timestamp = sample->time;
1608 		return 0;
1609 	}
1610 
1611 	if (machine__resolve(machine, &al, sample) < 0) {
1612 		pr_err("problem processing %d event, skipping it.\n",
1613 		       event->header.type);
1614 		return -1;
1615 	}
1616 
1617 	if (al.filtered)
1618 		goto out_put;
1619 
1620 	if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1621 		goto out_put;
1622 
1623 	if (scripting_ops)
1624 		scripting_ops->process_event(event, sample, evsel, &al);
1625 	else
1626 		process_event(scr, sample, evsel, &al, machine);
1627 
1628 out_put:
1629 	addr_location__put(&al);
1630 	return 0;
1631 }
1632 
1633 static int process_attr(struct perf_tool *tool, union perf_event *event,
1634 			struct perf_evlist **pevlist)
1635 {
1636 	struct perf_script *scr = container_of(tool, struct perf_script, tool);
1637 	struct perf_evlist *evlist;
1638 	struct perf_evsel *evsel, *pos;
1639 	int err;
1640 
1641 	err = perf_event__process_attr(tool, event, pevlist);
1642 	if (err)
1643 		return err;
1644 
1645 	evlist = *pevlist;
1646 	evsel = perf_evlist__last(*pevlist);
1647 
1648 	if (evsel->attr.type >= PERF_TYPE_MAX &&
1649 	    evsel->attr.type != PERF_TYPE_SYNTH)
1650 		return 0;
1651 
1652 	evlist__for_each_entry(evlist, pos) {
1653 		if (pos->attr.type == evsel->attr.type && pos != evsel)
1654 			return 0;
1655 	}
1656 
1657 	set_print_ip_opts(&evsel->attr);
1658 
1659 	if (evsel->attr.sample_type)
1660 		err = perf_evsel__check_attr(evsel, scr->session);
1661 
1662 	return err;
1663 }
1664 
1665 static int process_comm_event(struct perf_tool *tool,
1666 			      union perf_event *event,
1667 			      struct perf_sample *sample,
1668 			      struct machine *machine)
1669 {
1670 	struct thread *thread;
1671 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1672 	struct perf_session *session = script->session;
1673 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1674 	int ret = -1;
1675 
1676 	thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1677 	if (thread == NULL) {
1678 		pr_debug("problem processing COMM event, skipping it.\n");
1679 		return -1;
1680 	}
1681 
1682 	if (perf_event__process_comm(tool, event, sample, machine) < 0)
1683 		goto out;
1684 
1685 	if (!evsel->attr.sample_id_all) {
1686 		sample->cpu = 0;
1687 		sample->time = 0;
1688 		sample->tid = event->comm.tid;
1689 		sample->pid = event->comm.pid;
1690 	}
1691 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1692 	perf_event__fprintf(event, stdout);
1693 	ret = 0;
1694 out:
1695 	thread__put(thread);
1696 	return ret;
1697 }
1698 
1699 static int process_namespaces_event(struct perf_tool *tool,
1700 				    union perf_event *event,
1701 				    struct perf_sample *sample,
1702 				    struct machine *machine)
1703 {
1704 	struct thread *thread;
1705 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1706 	struct perf_session *session = script->session;
1707 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1708 	int ret = -1;
1709 
1710 	thread = machine__findnew_thread(machine, event->namespaces.pid,
1711 					 event->namespaces.tid);
1712 	if (thread == NULL) {
1713 		pr_debug("problem processing NAMESPACES event, skipping it.\n");
1714 		return -1;
1715 	}
1716 
1717 	if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1718 		goto out;
1719 
1720 	if (!evsel->attr.sample_id_all) {
1721 		sample->cpu = 0;
1722 		sample->time = 0;
1723 		sample->tid = event->namespaces.tid;
1724 		sample->pid = event->namespaces.pid;
1725 	}
1726 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1727 	perf_event__fprintf(event, stdout);
1728 	ret = 0;
1729 out:
1730 	thread__put(thread);
1731 	return ret;
1732 }
1733 
1734 static int process_fork_event(struct perf_tool *tool,
1735 			      union perf_event *event,
1736 			      struct perf_sample *sample,
1737 			      struct machine *machine)
1738 {
1739 	struct thread *thread;
1740 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1741 	struct perf_session *session = script->session;
1742 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1743 
1744 	if (perf_event__process_fork(tool, event, sample, machine) < 0)
1745 		return -1;
1746 
1747 	thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1748 	if (thread == NULL) {
1749 		pr_debug("problem processing FORK event, skipping it.\n");
1750 		return -1;
1751 	}
1752 
1753 	if (!evsel->attr.sample_id_all) {
1754 		sample->cpu = 0;
1755 		sample->time = event->fork.time;
1756 		sample->tid = event->fork.tid;
1757 		sample->pid = event->fork.pid;
1758 	}
1759 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1760 	perf_event__fprintf(event, stdout);
1761 	thread__put(thread);
1762 
1763 	return 0;
1764 }
1765 static int process_exit_event(struct perf_tool *tool,
1766 			      union perf_event *event,
1767 			      struct perf_sample *sample,
1768 			      struct machine *machine)
1769 {
1770 	int err = 0;
1771 	struct thread *thread;
1772 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1773 	struct perf_session *session = script->session;
1774 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1775 
1776 	thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1777 	if (thread == NULL) {
1778 		pr_debug("problem processing EXIT event, skipping it.\n");
1779 		return -1;
1780 	}
1781 
1782 	if (!evsel->attr.sample_id_all) {
1783 		sample->cpu = 0;
1784 		sample->time = 0;
1785 		sample->tid = event->fork.tid;
1786 		sample->pid = event->fork.pid;
1787 	}
1788 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1789 	perf_event__fprintf(event, stdout);
1790 
1791 	if (perf_event__process_exit(tool, event, sample, machine) < 0)
1792 		err = -1;
1793 
1794 	thread__put(thread);
1795 	return err;
1796 }
1797 
1798 static int process_mmap_event(struct perf_tool *tool,
1799 			      union perf_event *event,
1800 			      struct perf_sample *sample,
1801 			      struct machine *machine)
1802 {
1803 	struct thread *thread;
1804 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1805 	struct perf_session *session = script->session;
1806 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1807 
1808 	if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1809 		return -1;
1810 
1811 	thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1812 	if (thread == NULL) {
1813 		pr_debug("problem processing MMAP event, skipping it.\n");
1814 		return -1;
1815 	}
1816 
1817 	if (!evsel->attr.sample_id_all) {
1818 		sample->cpu = 0;
1819 		sample->time = 0;
1820 		sample->tid = event->mmap.tid;
1821 		sample->pid = event->mmap.pid;
1822 	}
1823 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1824 	perf_event__fprintf(event, stdout);
1825 	thread__put(thread);
1826 	return 0;
1827 }
1828 
1829 static int process_mmap2_event(struct perf_tool *tool,
1830 			      union perf_event *event,
1831 			      struct perf_sample *sample,
1832 			      struct machine *machine)
1833 {
1834 	struct thread *thread;
1835 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1836 	struct perf_session *session = script->session;
1837 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1838 
1839 	if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1840 		return -1;
1841 
1842 	thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1843 	if (thread == NULL) {
1844 		pr_debug("problem processing MMAP2 event, skipping it.\n");
1845 		return -1;
1846 	}
1847 
1848 	if (!evsel->attr.sample_id_all) {
1849 		sample->cpu = 0;
1850 		sample->time = 0;
1851 		sample->tid = event->mmap2.tid;
1852 		sample->pid = event->mmap2.pid;
1853 	}
1854 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1855 	perf_event__fprintf(event, stdout);
1856 	thread__put(thread);
1857 	return 0;
1858 }
1859 
1860 static int process_switch_event(struct perf_tool *tool,
1861 				union perf_event *event,
1862 				struct perf_sample *sample,
1863 				struct machine *machine)
1864 {
1865 	struct thread *thread;
1866 	struct perf_script *script = container_of(tool, struct perf_script, tool);
1867 	struct perf_session *session = script->session;
1868 	struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1869 
1870 	if (perf_event__process_switch(tool, event, sample, machine) < 0)
1871 		return -1;
1872 
1873 	thread = machine__findnew_thread(machine, sample->pid,
1874 					 sample->tid);
1875 	if (thread == NULL) {
1876 		pr_debug("problem processing SWITCH event, skipping it.\n");
1877 		return -1;
1878 	}
1879 
1880 	perf_sample__fprintf_start(sample, thread, evsel, stdout);
1881 	perf_event__fprintf(event, stdout);
1882 	thread__put(thread);
1883 	return 0;
1884 }
1885 
1886 static void sig_handler(int sig __maybe_unused)
1887 {
1888 	session_done = 1;
1889 }
1890 
1891 static int __cmd_script(struct perf_script *script)
1892 {
1893 	int ret;
1894 
1895 	signal(SIGINT, sig_handler);
1896 
1897 	/* override event processing functions */
1898 	if (script->show_task_events) {
1899 		script->tool.comm = process_comm_event;
1900 		script->tool.fork = process_fork_event;
1901 		script->tool.exit = process_exit_event;
1902 	}
1903 	if (script->show_mmap_events) {
1904 		script->tool.mmap = process_mmap_event;
1905 		script->tool.mmap2 = process_mmap2_event;
1906 	}
1907 	if (script->show_switch_events)
1908 		script->tool.context_switch = process_switch_event;
1909 	if (script->show_namespace_events)
1910 		script->tool.namespaces = process_namespaces_event;
1911 
1912 	ret = perf_session__process_events(script->session);
1913 
1914 	if (debug_mode)
1915 		pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
1916 
1917 	return ret;
1918 }
1919 
1920 struct script_spec {
1921 	struct list_head	node;
1922 	struct scripting_ops	*ops;
1923 	char			spec[0];
1924 };
1925 
1926 static LIST_HEAD(script_specs);
1927 
1928 static struct script_spec *script_spec__new(const char *spec,
1929 					    struct scripting_ops *ops)
1930 {
1931 	struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1932 
1933 	if (s != NULL) {
1934 		strcpy(s->spec, spec);
1935 		s->ops = ops;
1936 	}
1937 
1938 	return s;
1939 }
1940 
1941 static void script_spec__add(struct script_spec *s)
1942 {
1943 	list_add_tail(&s->node, &script_specs);
1944 }
1945 
1946 static struct script_spec *script_spec__find(const char *spec)
1947 {
1948 	struct script_spec *s;
1949 
1950 	list_for_each_entry(s, &script_specs, node)
1951 		if (strcasecmp(s->spec, spec) == 0)
1952 			return s;
1953 	return NULL;
1954 }
1955 
1956 int script_spec_register(const char *spec, struct scripting_ops *ops)
1957 {
1958 	struct script_spec *s;
1959 
1960 	s = script_spec__find(spec);
1961 	if (s)
1962 		return -1;
1963 
1964 	s = script_spec__new(spec, ops);
1965 	if (!s)
1966 		return -1;
1967 	else
1968 		script_spec__add(s);
1969 
1970 	return 0;
1971 }
1972 
1973 static struct scripting_ops *script_spec__lookup(const char *spec)
1974 {
1975 	struct script_spec *s = script_spec__find(spec);
1976 	if (!s)
1977 		return NULL;
1978 
1979 	return s->ops;
1980 }
1981 
1982 static void list_available_languages(void)
1983 {
1984 	struct script_spec *s;
1985 
1986 	fprintf(stderr, "\n");
1987 	fprintf(stderr, "Scripting language extensions (used in "
1988 		"perf script -s [spec:]script.[spec]):\n\n");
1989 
1990 	list_for_each_entry(s, &script_specs, node)
1991 		fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
1992 
1993 	fprintf(stderr, "\n");
1994 }
1995 
1996 static int parse_scriptname(const struct option *opt __maybe_unused,
1997 			    const char *str, int unset __maybe_unused)
1998 {
1999 	char spec[PATH_MAX];
2000 	const char *script, *ext;
2001 	int len;
2002 
2003 	if (strcmp(str, "lang") == 0) {
2004 		list_available_languages();
2005 		exit(0);
2006 	}
2007 
2008 	script = strchr(str, ':');
2009 	if (script) {
2010 		len = script - str;
2011 		if (len >= PATH_MAX) {
2012 			fprintf(stderr, "invalid language specifier");
2013 			return -1;
2014 		}
2015 		strncpy(spec, str, len);
2016 		spec[len] = '\0';
2017 		scripting_ops = script_spec__lookup(spec);
2018 		if (!scripting_ops) {
2019 			fprintf(stderr, "invalid language specifier");
2020 			return -1;
2021 		}
2022 		script++;
2023 	} else {
2024 		script = str;
2025 		ext = strrchr(script, '.');
2026 		if (!ext) {
2027 			fprintf(stderr, "invalid script extension");
2028 			return -1;
2029 		}
2030 		scripting_ops = script_spec__lookup(++ext);
2031 		if (!scripting_ops) {
2032 			fprintf(stderr, "invalid script extension");
2033 			return -1;
2034 		}
2035 	}
2036 
2037 	script_name = strdup(script);
2038 
2039 	return 0;
2040 }
2041 
2042 static int parse_output_fields(const struct option *opt __maybe_unused,
2043 			    const char *arg, int unset __maybe_unused)
2044 {
2045 	char *tok, *strtok_saveptr = NULL;
2046 	int i, imax = ARRAY_SIZE(all_output_options);
2047 	int j;
2048 	int rc = 0;
2049 	char *str = strdup(arg);
2050 	int type = -1;
2051 	enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2052 
2053 	if (!str)
2054 		return -ENOMEM;
2055 
2056 	/* first word can state for which event type the user is specifying
2057 	 * the fields. If no type exists, the specified fields apply to all
2058 	 * event types found in the file minus the invalid fields for a type.
2059 	 */
2060 	tok = strchr(str, ':');
2061 	if (tok) {
2062 		*tok = '\0';
2063 		tok++;
2064 		if (!strcmp(str, "hw"))
2065 			type = PERF_TYPE_HARDWARE;
2066 		else if (!strcmp(str, "sw"))
2067 			type = PERF_TYPE_SOFTWARE;
2068 		else if (!strcmp(str, "trace"))
2069 			type = PERF_TYPE_TRACEPOINT;
2070 		else if (!strcmp(str, "raw"))
2071 			type = PERF_TYPE_RAW;
2072 		else if (!strcmp(str, "break"))
2073 			type = PERF_TYPE_BREAKPOINT;
2074 		else if (!strcmp(str, "synth"))
2075 			type = OUTPUT_TYPE_SYNTH;
2076 		else {
2077 			fprintf(stderr, "Invalid event type in field string.\n");
2078 			rc = -EINVAL;
2079 			goto out;
2080 		}
2081 
2082 		if (output[type].user_set)
2083 			pr_warning("Overriding previous field request for %s events.\n",
2084 				   event_type(type));
2085 
2086 		output[type].fields = 0;
2087 		output[type].user_set = true;
2088 		output[type].wildcard_set = false;
2089 
2090 	} else {
2091 		tok = str;
2092 		if (strlen(str) == 0) {
2093 			fprintf(stderr,
2094 				"Cannot set fields to 'none' for all event types.\n");
2095 			rc = -EINVAL;
2096 			goto out;
2097 		}
2098 
2099 		/* Don't override defaults for +- */
2100 		if (strchr(str, '+') || strchr(str, '-'))
2101 			goto parse;
2102 
2103 		if (output_set_by_user())
2104 			pr_warning("Overriding previous field request for all events.\n");
2105 
2106 		for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2107 			output[j].fields = 0;
2108 			output[j].user_set = true;
2109 			output[j].wildcard_set = true;
2110 		}
2111 	}
2112 
2113 parse:
2114 	for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2115 		if (*tok == '+') {
2116 			if (change == SET)
2117 				goto out_badmix;
2118 			change = ADD;
2119 			tok++;
2120 		} else if (*tok == '-') {
2121 			if (change == SET)
2122 				goto out_badmix;
2123 			change = REMOVE;
2124 			tok++;
2125 		} else {
2126 			if (change != SET && change != DEFAULT)
2127 				goto out_badmix;
2128 			change = SET;
2129 		}
2130 
2131 		for (i = 0; i < imax; ++i) {
2132 			if (strcmp(tok, all_output_options[i].str) == 0)
2133 				break;
2134 		}
2135 		if (i == imax && strcmp(tok, "flags") == 0) {
2136 			print_flags = change == REMOVE ? false : true;
2137 			continue;
2138 		}
2139 		if (i == imax) {
2140 			fprintf(stderr, "Invalid field requested.\n");
2141 			rc = -EINVAL;
2142 			goto out;
2143 		}
2144 
2145 		if (type == -1) {
2146 			/* add user option to all events types for
2147 			 * which it is valid
2148 			 */
2149 			for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2150 				if (output[j].invalid_fields & all_output_options[i].field) {
2151 					pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2152 						   all_output_options[i].str, event_type(j));
2153 				} else {
2154 					if (change == REMOVE)
2155 						output[j].fields &= ~all_output_options[i].field;
2156 					else
2157 						output[j].fields |= all_output_options[i].field;
2158 				}
2159 			}
2160 		} else {
2161 			if (output[type].invalid_fields & all_output_options[i].field) {
2162 				fprintf(stderr, "\'%s\' not valid for %s events.\n",
2163 					 all_output_options[i].str, event_type(type));
2164 
2165 				rc = -EINVAL;
2166 				goto out;
2167 			}
2168 			output[type].fields |= all_output_options[i].field;
2169 		}
2170 	}
2171 
2172 	if (type >= 0) {
2173 		if (output[type].fields == 0) {
2174 			pr_debug("No fields requested for %s type. "
2175 				 "Events will not be displayed.\n", event_type(type));
2176 		}
2177 	}
2178 	goto out;
2179 
2180 out_badmix:
2181 	fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2182 	rc = -EINVAL;
2183 out:
2184 	free(str);
2185 	return rc;
2186 }
2187 
2188 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2189 static int is_directory(const char *base_path, const struct dirent *dent)
2190 {
2191 	char path[PATH_MAX];
2192 	struct stat st;
2193 
2194 	sprintf(path, "%s/%s", base_path, dent->d_name);
2195 	if (stat(path, &st))
2196 		return 0;
2197 
2198 	return S_ISDIR(st.st_mode);
2199 }
2200 
2201 #define for_each_lang(scripts_path, scripts_dir, lang_dirent)		\
2202 	while ((lang_dirent = readdir(scripts_dir)) != NULL)		\
2203 		if ((lang_dirent->d_type == DT_DIR ||			\
2204 		     (lang_dirent->d_type == DT_UNKNOWN &&		\
2205 		      is_directory(scripts_path, lang_dirent))) &&	\
2206 		    (strcmp(lang_dirent->d_name, ".")) &&		\
2207 		    (strcmp(lang_dirent->d_name, "..")))
2208 
2209 #define for_each_script(lang_path, lang_dir, script_dirent)		\
2210 	while ((script_dirent = readdir(lang_dir)) != NULL)		\
2211 		if (script_dirent->d_type != DT_DIR &&			\
2212 		    (script_dirent->d_type != DT_UNKNOWN ||		\
2213 		     !is_directory(lang_path, script_dirent)))
2214 
2215 
2216 #define RECORD_SUFFIX			"-record"
2217 #define REPORT_SUFFIX			"-report"
2218 
2219 struct script_desc {
2220 	struct list_head	node;
2221 	char			*name;
2222 	char			*half_liner;
2223 	char			*args;
2224 };
2225 
2226 static LIST_HEAD(script_descs);
2227 
2228 static struct script_desc *script_desc__new(const char *name)
2229 {
2230 	struct script_desc *s = zalloc(sizeof(*s));
2231 
2232 	if (s != NULL && name)
2233 		s->name = strdup(name);
2234 
2235 	return s;
2236 }
2237 
2238 static void script_desc__delete(struct script_desc *s)
2239 {
2240 	zfree(&s->name);
2241 	zfree(&s->half_liner);
2242 	zfree(&s->args);
2243 	free(s);
2244 }
2245 
2246 static void script_desc__add(struct script_desc *s)
2247 {
2248 	list_add_tail(&s->node, &script_descs);
2249 }
2250 
2251 static struct script_desc *script_desc__find(const char *name)
2252 {
2253 	struct script_desc *s;
2254 
2255 	list_for_each_entry(s, &script_descs, node)
2256 		if (strcasecmp(s->name, name) == 0)
2257 			return s;
2258 	return NULL;
2259 }
2260 
2261 static struct script_desc *script_desc__findnew(const char *name)
2262 {
2263 	struct script_desc *s = script_desc__find(name);
2264 
2265 	if (s)
2266 		return s;
2267 
2268 	s = script_desc__new(name);
2269 	if (!s)
2270 		return NULL;
2271 
2272 	script_desc__add(s);
2273 
2274 	return s;
2275 }
2276 
2277 static const char *ends_with(const char *str, const char *suffix)
2278 {
2279 	size_t suffix_len = strlen(suffix);
2280 	const char *p = str;
2281 
2282 	if (strlen(str) > suffix_len) {
2283 		p = str + strlen(str) - suffix_len;
2284 		if (!strncmp(p, suffix, suffix_len))
2285 			return p;
2286 	}
2287 
2288 	return NULL;
2289 }
2290 
2291 static int read_script_info(struct script_desc *desc, const char *filename)
2292 {
2293 	char line[BUFSIZ], *p;
2294 	FILE *fp;
2295 
2296 	fp = fopen(filename, "r");
2297 	if (!fp)
2298 		return -1;
2299 
2300 	while (fgets(line, sizeof(line), fp)) {
2301 		p = ltrim(line);
2302 		if (strlen(p) == 0)
2303 			continue;
2304 		if (*p != '#')
2305 			continue;
2306 		p++;
2307 		if (strlen(p) && *p == '!')
2308 			continue;
2309 
2310 		p = ltrim(p);
2311 		if (strlen(p) && p[strlen(p) - 1] == '\n')
2312 			p[strlen(p) - 1] = '\0';
2313 
2314 		if (!strncmp(p, "description:", strlen("description:"))) {
2315 			p += strlen("description:");
2316 			desc->half_liner = strdup(ltrim(p));
2317 			continue;
2318 		}
2319 
2320 		if (!strncmp(p, "args:", strlen("args:"))) {
2321 			p += strlen("args:");
2322 			desc->args = strdup(ltrim(p));
2323 			continue;
2324 		}
2325 	}
2326 
2327 	fclose(fp);
2328 
2329 	return 0;
2330 }
2331 
2332 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2333 {
2334 	char *script_root, *str;
2335 
2336 	script_root = strdup(script_dirent->d_name);
2337 	if (!script_root)
2338 		return NULL;
2339 
2340 	str = (char *)ends_with(script_root, suffix);
2341 	if (!str) {
2342 		free(script_root);
2343 		return NULL;
2344 	}
2345 
2346 	*str = '\0';
2347 	return script_root;
2348 }
2349 
2350 static int list_available_scripts(const struct option *opt __maybe_unused,
2351 				  const char *s __maybe_unused,
2352 				  int unset __maybe_unused)
2353 {
2354 	struct dirent *script_dirent, *lang_dirent;
2355 	char scripts_path[MAXPATHLEN];
2356 	DIR *scripts_dir, *lang_dir;
2357 	char script_path[MAXPATHLEN];
2358 	char lang_path[MAXPATHLEN];
2359 	struct script_desc *desc;
2360 	char first_half[BUFSIZ];
2361 	char *script_root;
2362 
2363 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2364 
2365 	scripts_dir = opendir(scripts_path);
2366 	if (!scripts_dir) {
2367 		fprintf(stdout,
2368 			"open(%s) failed.\n"
2369 			"Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2370 			scripts_path);
2371 		exit(-1);
2372 	}
2373 
2374 	for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2375 		snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2376 			 lang_dirent->d_name);
2377 		lang_dir = opendir(lang_path);
2378 		if (!lang_dir)
2379 			continue;
2380 
2381 		for_each_script(lang_path, lang_dir, script_dirent) {
2382 			script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2383 			if (script_root) {
2384 				desc = script_desc__findnew(script_root);
2385 				snprintf(script_path, MAXPATHLEN, "%s/%s",
2386 					 lang_path, script_dirent->d_name);
2387 				read_script_info(desc, script_path);
2388 				free(script_root);
2389 			}
2390 		}
2391 	}
2392 
2393 	fprintf(stdout, "List of available trace scripts:\n");
2394 	list_for_each_entry(desc, &script_descs, node) {
2395 		sprintf(first_half, "%s %s", desc->name,
2396 			desc->args ? desc->args : "");
2397 		fprintf(stdout, "  %-36s %s\n", first_half,
2398 			desc->half_liner ? desc->half_liner : "");
2399 	}
2400 
2401 	exit(0);
2402 }
2403 
2404 /*
2405  * Some scripts specify the required events in their "xxx-record" file,
2406  * this function will check if the events in perf.data match those
2407  * mentioned in the "xxx-record".
2408  *
2409  * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2410  * which is covered well now. And new parsing code should be added to
2411  * cover the future complexing formats like event groups etc.
2412  */
2413 static int check_ev_match(char *dir_name, char *scriptname,
2414 			struct perf_session *session)
2415 {
2416 	char filename[MAXPATHLEN], evname[128];
2417 	char line[BUFSIZ], *p;
2418 	struct perf_evsel *pos;
2419 	int match, len;
2420 	FILE *fp;
2421 
2422 	sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2423 
2424 	fp = fopen(filename, "r");
2425 	if (!fp)
2426 		return -1;
2427 
2428 	while (fgets(line, sizeof(line), fp)) {
2429 		p = ltrim(line);
2430 		if (*p == '#')
2431 			continue;
2432 
2433 		while (strlen(p)) {
2434 			p = strstr(p, "-e");
2435 			if (!p)
2436 				break;
2437 
2438 			p += 2;
2439 			p = ltrim(p);
2440 			len = strcspn(p, " \t");
2441 			if (!len)
2442 				break;
2443 
2444 			snprintf(evname, len + 1, "%s", p);
2445 
2446 			match = 0;
2447 			evlist__for_each_entry(session->evlist, pos) {
2448 				if (!strcmp(perf_evsel__name(pos), evname)) {
2449 					match = 1;
2450 					break;
2451 				}
2452 			}
2453 
2454 			if (!match) {
2455 				fclose(fp);
2456 				return -1;
2457 			}
2458 		}
2459 	}
2460 
2461 	fclose(fp);
2462 	return 0;
2463 }
2464 
2465 /*
2466  * Return -1 if none is found, otherwise the actual scripts number.
2467  *
2468  * Currently the only user of this function is the script browser, which
2469  * will list all statically runnable scripts, select one, execute it and
2470  * show the output in a perf browser.
2471  */
2472 int find_scripts(char **scripts_array, char **scripts_path_array)
2473 {
2474 	struct dirent *script_dirent, *lang_dirent;
2475 	char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
2476 	DIR *scripts_dir, *lang_dir;
2477 	struct perf_session *session;
2478 	struct perf_data_file file = {
2479 		.path = input_name,
2480 		.mode = PERF_DATA_MODE_READ,
2481 	};
2482 	char *temp;
2483 	int i = 0;
2484 
2485 	session = perf_session__new(&file, false, NULL);
2486 	if (!session)
2487 		return -1;
2488 
2489 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2490 
2491 	scripts_dir = opendir(scripts_path);
2492 	if (!scripts_dir) {
2493 		perf_session__delete(session);
2494 		return -1;
2495 	}
2496 
2497 	for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2498 		snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2499 			 lang_dirent->d_name);
2500 #ifdef NO_LIBPERL
2501 		if (strstr(lang_path, "perl"))
2502 			continue;
2503 #endif
2504 #ifdef NO_LIBPYTHON
2505 		if (strstr(lang_path, "python"))
2506 			continue;
2507 #endif
2508 
2509 		lang_dir = opendir(lang_path);
2510 		if (!lang_dir)
2511 			continue;
2512 
2513 		for_each_script(lang_path, lang_dir, script_dirent) {
2514 			/* Skip those real time scripts: xxxtop.p[yl] */
2515 			if (strstr(script_dirent->d_name, "top."))
2516 				continue;
2517 			sprintf(scripts_path_array[i], "%s/%s", lang_path,
2518 				script_dirent->d_name);
2519 			temp = strchr(script_dirent->d_name, '.');
2520 			snprintf(scripts_array[i],
2521 				(temp - script_dirent->d_name) + 1,
2522 				"%s", script_dirent->d_name);
2523 
2524 			if (check_ev_match(lang_path,
2525 					scripts_array[i], session))
2526 				continue;
2527 
2528 			i++;
2529 		}
2530 		closedir(lang_dir);
2531 	}
2532 
2533 	closedir(scripts_dir);
2534 	perf_session__delete(session);
2535 	return i;
2536 }
2537 
2538 static char *get_script_path(const char *script_root, const char *suffix)
2539 {
2540 	struct dirent *script_dirent, *lang_dirent;
2541 	char scripts_path[MAXPATHLEN];
2542 	char script_path[MAXPATHLEN];
2543 	DIR *scripts_dir, *lang_dir;
2544 	char lang_path[MAXPATHLEN];
2545 	char *__script_root;
2546 
2547 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2548 
2549 	scripts_dir = opendir(scripts_path);
2550 	if (!scripts_dir)
2551 		return NULL;
2552 
2553 	for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2554 		snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2555 			 lang_dirent->d_name);
2556 		lang_dir = opendir(lang_path);
2557 		if (!lang_dir)
2558 			continue;
2559 
2560 		for_each_script(lang_path, lang_dir, script_dirent) {
2561 			__script_root = get_script_root(script_dirent, suffix);
2562 			if (__script_root && !strcmp(script_root, __script_root)) {
2563 				free(__script_root);
2564 				closedir(lang_dir);
2565 				closedir(scripts_dir);
2566 				snprintf(script_path, MAXPATHLEN, "%s/%s",
2567 					 lang_path, script_dirent->d_name);
2568 				return strdup(script_path);
2569 			}
2570 			free(__script_root);
2571 		}
2572 		closedir(lang_dir);
2573 	}
2574 	closedir(scripts_dir);
2575 
2576 	return NULL;
2577 }
2578 
2579 static bool is_top_script(const char *script_path)
2580 {
2581 	return ends_with(script_path, "top") == NULL ? false : true;
2582 }
2583 
2584 static int has_required_arg(char *script_path)
2585 {
2586 	struct script_desc *desc;
2587 	int n_args = 0;
2588 	char *p;
2589 
2590 	desc = script_desc__new(NULL);
2591 
2592 	if (read_script_info(desc, script_path))
2593 		goto out;
2594 
2595 	if (!desc->args)
2596 		goto out;
2597 
2598 	for (p = desc->args; *p; p++)
2599 		if (*p == '<')
2600 			n_args++;
2601 out:
2602 	script_desc__delete(desc);
2603 
2604 	return n_args;
2605 }
2606 
2607 static int have_cmd(int argc, const char **argv)
2608 {
2609 	char **__argv = malloc(sizeof(const char *) * argc);
2610 
2611 	if (!__argv) {
2612 		pr_err("malloc failed\n");
2613 		return -1;
2614 	}
2615 
2616 	memcpy(__argv, argv, sizeof(const char *) * argc);
2617 	argc = parse_options(argc, (const char **)__argv, record_options,
2618 			     NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2619 	free(__argv);
2620 
2621 	system_wide = (argc == 0);
2622 
2623 	return 0;
2624 }
2625 
2626 static void script__setup_sample_type(struct perf_script *script)
2627 {
2628 	struct perf_session *session = script->session;
2629 	u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2630 
2631 	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2632 		if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2633 		    (sample_type & PERF_SAMPLE_STACK_USER))
2634 			callchain_param.record_mode = CALLCHAIN_DWARF;
2635 		else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2636 			callchain_param.record_mode = CALLCHAIN_LBR;
2637 		else
2638 			callchain_param.record_mode = CALLCHAIN_FP;
2639 	}
2640 }
2641 
2642 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2643 				    union perf_event *event,
2644 				    struct perf_session *session)
2645 {
2646 	struct stat_round_event *round = &event->stat_round;
2647 	struct perf_evsel *counter;
2648 
2649 	evlist__for_each_entry(session->evlist, counter) {
2650 		perf_stat_process_counter(&stat_config, counter);
2651 		process_stat(counter, round->time);
2652 	}
2653 
2654 	process_stat_interval(round->time);
2655 	return 0;
2656 }
2657 
2658 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2659 				     union perf_event *event,
2660 				     struct perf_session *session __maybe_unused)
2661 {
2662 	perf_event__read_stat_config(&stat_config, &event->stat_config);
2663 	return 0;
2664 }
2665 
2666 static int set_maps(struct perf_script *script)
2667 {
2668 	struct perf_evlist *evlist = script->session->evlist;
2669 
2670 	if (!script->cpus || !script->threads)
2671 		return 0;
2672 
2673 	if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2674 		return -EINVAL;
2675 
2676 	perf_evlist__set_maps(evlist, script->cpus, script->threads);
2677 
2678 	if (perf_evlist__alloc_stats(evlist, true))
2679 		return -ENOMEM;
2680 
2681 	script->allocated = true;
2682 	return 0;
2683 }
2684 
2685 static
2686 int process_thread_map_event(struct perf_tool *tool,
2687 			     union perf_event *event,
2688 			     struct perf_session *session __maybe_unused)
2689 {
2690 	struct perf_script *script = container_of(tool, struct perf_script, tool);
2691 
2692 	if (script->threads) {
2693 		pr_warning("Extra thread map event, ignoring.\n");
2694 		return 0;
2695 	}
2696 
2697 	script->threads = thread_map__new_event(&event->thread_map);
2698 	if (!script->threads)
2699 		return -ENOMEM;
2700 
2701 	return set_maps(script);
2702 }
2703 
2704 static
2705 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2706 			  union perf_event *event,
2707 			  struct perf_session *session __maybe_unused)
2708 {
2709 	struct perf_script *script = container_of(tool, struct perf_script, tool);
2710 
2711 	if (script->cpus) {
2712 		pr_warning("Extra cpu map event, ignoring.\n");
2713 		return 0;
2714 	}
2715 
2716 	script->cpus = cpu_map__new_data(&event->cpu_map.data);
2717 	if (!script->cpus)
2718 		return -ENOMEM;
2719 
2720 	return set_maps(script);
2721 }
2722 
2723 int cmd_script(int argc, const char **argv)
2724 {
2725 	bool show_full_info = false;
2726 	bool header = false;
2727 	bool header_only = false;
2728 	bool script_started = false;
2729 	char *rec_script_path = NULL;
2730 	char *rep_script_path = NULL;
2731 	struct perf_session *session;
2732 	struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2733 	char *script_path = NULL;
2734 	const char **__argv;
2735 	int i, j, err = 0;
2736 	struct perf_script script = {
2737 		.tool = {
2738 			.sample		 = process_sample_event,
2739 			.mmap		 = perf_event__process_mmap,
2740 			.mmap2		 = perf_event__process_mmap2,
2741 			.comm		 = perf_event__process_comm,
2742 			.namespaces	 = perf_event__process_namespaces,
2743 			.exit		 = perf_event__process_exit,
2744 			.fork		 = perf_event__process_fork,
2745 			.attr		 = process_attr,
2746 			.event_update   = perf_event__process_event_update,
2747 			.tracing_data	 = perf_event__process_tracing_data,
2748 			.feature	 = perf_event__process_feature,
2749 			.build_id	 = perf_event__process_build_id,
2750 			.id_index	 = perf_event__process_id_index,
2751 			.auxtrace_info	 = perf_event__process_auxtrace_info,
2752 			.auxtrace	 = perf_event__process_auxtrace,
2753 			.auxtrace_error	 = perf_event__process_auxtrace_error,
2754 			.stat		 = perf_event__process_stat_event,
2755 			.stat_round	 = process_stat_round_event,
2756 			.stat_config	 = process_stat_config_event,
2757 			.thread_map	 = process_thread_map_event,
2758 			.cpu_map	 = process_cpu_map_event,
2759 			.ordered_events	 = true,
2760 			.ordering_requires_timestamps = true,
2761 		},
2762 	};
2763 	struct perf_data_file file = {
2764 		.mode = PERF_DATA_MODE_READ,
2765 	};
2766 	const struct option options[] = {
2767 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2768 		    "dump raw trace in ASCII"),
2769 	OPT_INCR('v', "verbose", &verbose,
2770 		 "be more verbose (show symbol address, etc)"),
2771 	OPT_BOOLEAN('L', "Latency", &latency_format,
2772 		    "show latency attributes (irqs/preemption disabled, etc)"),
2773 	OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2774 			   list_available_scripts),
2775 	OPT_CALLBACK('s', "script", NULL, "name",
2776 		     "script file name (lang:script name, script name, or *)",
2777 		     parse_scriptname),
2778 	OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2779 		   "generate perf-script.xx script in specified language"),
2780 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
2781 	OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2782 		   "do various checks like samples ordering and lost events"),
2783 	OPT_BOOLEAN(0, "header", &header, "Show data header."),
2784 	OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2785 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2786 		   "file", "vmlinux pathname"),
2787 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2788 		   "file", "kallsyms pathname"),
2789 	OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2790 		    "When printing symbols do not display call chain"),
2791 	OPT_CALLBACK(0, "symfs", NULL, "directory",
2792 		     "Look for files with symbols relative to this directory",
2793 		     symbol__config_symfs),
2794 	OPT_CALLBACK('F', "fields", NULL, "str",
2795 		     "comma separated output fields prepend with 'type:'. "
2796 		     "+field to add and -field to remove."
2797 		     "Valid types: hw,sw,trace,raw,synth. "
2798 		     "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2799 		     "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
2800 		     "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
2801 		     parse_output_fields),
2802 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
2803 		    "system-wide collection from all CPUs"),
2804 	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2805 		   "only consider these symbols"),
2806 	OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2807 		   "Stop display of callgraph at these symbols"),
2808 	OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2809 	OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2810 		   "only display events for these comms"),
2811 	OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2812 		   "only consider symbols in these pids"),
2813 	OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2814 		   "only consider symbols in these tids"),
2815 	OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2816 		     "Set the maximum stack depth when parsing the callchain, "
2817 		     "anything beyond the specified depth will be ignored. "
2818 		     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2819 	OPT_BOOLEAN('I', "show-info", &show_full_info,
2820 		    "display extended information from perf.data file"),
2821 	OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2822 		    "Show the path of [kernel.kallsyms]"),
2823 	OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2824 		    "Show the fork/comm/exit events"),
2825 	OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2826 		    "Show the mmap events"),
2827 	OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2828 		    "Show context switch events (if recorded)"),
2829 	OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2830 		    "Show namespace events (if recorded)"),
2831 	OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2832 	OPT_INTEGER(0, "max-blocks", &max_blocks,
2833 		    "Maximum number of code blocks to dump with brstackinsn"),
2834 	OPT_BOOLEAN(0, "ns", &nanosecs,
2835 		    "Use 9 decimal places when displaying time"),
2836 	OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2837 			    "Instruction Tracing options",
2838 			    itrace_parse_synth_opts),
2839 	OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2840 			"Show full source file name path for source lines"),
2841 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2842 			"Enable symbol demangling"),
2843 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2844 			"Enable kernel symbol demangling"),
2845 	OPT_STRING(0, "time", &script.time_str, "str",
2846 		   "Time span of interest (start,stop)"),
2847 	OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2848 		    "Show inline function"),
2849 	OPT_END()
2850 	};
2851 	const char * const script_subcommands[] = { "record", "report", NULL };
2852 	const char *script_usage[] = {
2853 		"perf script [<options>]",
2854 		"perf script [<options>] record <script> [<record-options>] <command>",
2855 		"perf script [<options>] report <script> [script-args]",
2856 		"perf script [<options>] <script> [<record-options>] <command>",
2857 		"perf script [<options>] <top-script> [script-args]",
2858 		NULL
2859 	};
2860 
2861 	perf_set_singlethreaded();
2862 
2863 	setup_scripting();
2864 
2865 	argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2866 			     PARSE_OPT_STOP_AT_NON_OPTION);
2867 
2868 	file.path = input_name;
2869 	file.force = symbol_conf.force;
2870 
2871 	if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2872 		rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2873 		if (!rec_script_path)
2874 			return cmd_record(argc, argv);
2875 	}
2876 
2877 	if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2878 		rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2879 		if (!rep_script_path) {
2880 			fprintf(stderr,
2881 				"Please specify a valid report script"
2882 				"(see 'perf script -l' for listing)\n");
2883 			return -1;
2884 		}
2885 	}
2886 
2887 	if (itrace_synth_opts.callchain &&
2888 	    itrace_synth_opts.callchain_sz > scripting_max_stack)
2889 		scripting_max_stack = itrace_synth_opts.callchain_sz;
2890 
2891 	/* make sure PERF_EXEC_PATH is set for scripts */
2892 	set_argv_exec_path(get_argv_exec_path());
2893 
2894 	if (argc && !script_name && !rec_script_path && !rep_script_path) {
2895 		int live_pipe[2];
2896 		int rep_args;
2897 		pid_t pid;
2898 
2899 		rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2900 		rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2901 
2902 		if (!rec_script_path && !rep_script_path) {
2903 			usage_with_options_msg(script_usage, options,
2904 				"Couldn't find script `%s'\n\n See perf"
2905 				" script -l for available scripts.\n", argv[0]);
2906 		}
2907 
2908 		if (is_top_script(argv[0])) {
2909 			rep_args = argc - 1;
2910 		} else {
2911 			int rec_args;
2912 
2913 			rep_args = has_required_arg(rep_script_path);
2914 			rec_args = (argc - 1) - rep_args;
2915 			if (rec_args < 0) {
2916 				usage_with_options_msg(script_usage, options,
2917 					"`%s' script requires options."
2918 					"\n\n See perf script -l for available "
2919 					"scripts and options.\n", argv[0]);
2920 			}
2921 		}
2922 
2923 		if (pipe(live_pipe) < 0) {
2924 			perror("failed to create pipe");
2925 			return -1;
2926 		}
2927 
2928 		pid = fork();
2929 		if (pid < 0) {
2930 			perror("failed to fork");
2931 			return -1;
2932 		}
2933 
2934 		if (!pid) {
2935 			j = 0;
2936 
2937 			dup2(live_pipe[1], 1);
2938 			close(live_pipe[0]);
2939 
2940 			if (is_top_script(argv[0])) {
2941 				system_wide = true;
2942 			} else if (!system_wide) {
2943 				if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2944 					err = -1;
2945 					goto out;
2946 				}
2947 			}
2948 
2949 			__argv = malloc((argc + 6) * sizeof(const char *));
2950 			if (!__argv) {
2951 				pr_err("malloc failed\n");
2952 				err = -ENOMEM;
2953 				goto out;
2954 			}
2955 
2956 			__argv[j++] = "/bin/sh";
2957 			__argv[j++] = rec_script_path;
2958 			if (system_wide)
2959 				__argv[j++] = "-a";
2960 			__argv[j++] = "-q";
2961 			__argv[j++] = "-o";
2962 			__argv[j++] = "-";
2963 			for (i = rep_args + 1; i < argc; i++)
2964 				__argv[j++] = argv[i];
2965 			__argv[j++] = NULL;
2966 
2967 			execvp("/bin/sh", (char **)__argv);
2968 			free(__argv);
2969 			exit(-1);
2970 		}
2971 
2972 		dup2(live_pipe[0], 0);
2973 		close(live_pipe[1]);
2974 
2975 		__argv = malloc((argc + 4) * sizeof(const char *));
2976 		if (!__argv) {
2977 			pr_err("malloc failed\n");
2978 			err = -ENOMEM;
2979 			goto out;
2980 		}
2981 
2982 		j = 0;
2983 		__argv[j++] = "/bin/sh";
2984 		__argv[j++] = rep_script_path;
2985 		for (i = 1; i < rep_args + 1; i++)
2986 			__argv[j++] = argv[i];
2987 		__argv[j++] = "-i";
2988 		__argv[j++] = "-";
2989 		__argv[j++] = NULL;
2990 
2991 		execvp("/bin/sh", (char **)__argv);
2992 		free(__argv);
2993 		exit(-1);
2994 	}
2995 
2996 	if (rec_script_path)
2997 		script_path = rec_script_path;
2998 	if (rep_script_path)
2999 		script_path = rep_script_path;
3000 
3001 	if (script_path) {
3002 		j = 0;
3003 
3004 		if (!rec_script_path)
3005 			system_wide = false;
3006 		else if (!system_wide) {
3007 			if (have_cmd(argc - 1, &argv[1]) != 0) {
3008 				err = -1;
3009 				goto out;
3010 			}
3011 		}
3012 
3013 		__argv = malloc((argc + 2) * sizeof(const char *));
3014 		if (!__argv) {
3015 			pr_err("malloc failed\n");
3016 			err = -ENOMEM;
3017 			goto out;
3018 		}
3019 
3020 		__argv[j++] = "/bin/sh";
3021 		__argv[j++] = script_path;
3022 		if (system_wide)
3023 			__argv[j++] = "-a";
3024 		for (i = 2; i < argc; i++)
3025 			__argv[j++] = argv[i];
3026 		__argv[j++] = NULL;
3027 
3028 		execvp("/bin/sh", (char **)__argv);
3029 		free(__argv);
3030 		exit(-1);
3031 	}
3032 
3033 	if (!script_name)
3034 		setup_pager();
3035 
3036 	session = perf_session__new(&file, false, &script.tool);
3037 	if (session == NULL)
3038 		return -1;
3039 
3040 	if (header || header_only) {
3041 		script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3042 		perf_session__fprintf_info(session, stdout, show_full_info);
3043 		if (header_only)
3044 			goto out_delete;
3045 	}
3046 	if (show_full_info)
3047 		script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3048 
3049 	if (symbol__init(&session->header.env) < 0)
3050 		goto out_delete;
3051 
3052 	script.session = session;
3053 	script__setup_sample_type(&script);
3054 
3055 	if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3056 		itrace_synth_opts.thread_stack = true;
3057 
3058 	session->itrace_synth_opts = &itrace_synth_opts;
3059 
3060 	if (cpu_list) {
3061 		err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3062 		if (err < 0)
3063 			goto out_delete;
3064 		itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3065 	}
3066 
3067 	if (!no_callchain)
3068 		symbol_conf.use_callchain = true;
3069 	else
3070 		symbol_conf.use_callchain = false;
3071 
3072 	if (session->tevent.pevent &&
3073 	    pevent_set_function_resolver(session->tevent.pevent,
3074 					 machine__resolve_kernel_addr,
3075 					 &session->machines.host) < 0) {
3076 		pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3077 		err = -1;
3078 		goto out_delete;
3079 	}
3080 
3081 	if (generate_script_lang) {
3082 		struct stat perf_stat;
3083 		int input;
3084 
3085 		if (output_set_by_user()) {
3086 			fprintf(stderr,
3087 				"custom fields not supported for generated scripts");
3088 			err = -EINVAL;
3089 			goto out_delete;
3090 		}
3091 
3092 		input = open(file.path, O_RDONLY);	/* input_name */
3093 		if (input < 0) {
3094 			err = -errno;
3095 			perror("failed to open file");
3096 			goto out_delete;
3097 		}
3098 
3099 		err = fstat(input, &perf_stat);
3100 		if (err < 0) {
3101 			perror("failed to stat file");
3102 			goto out_delete;
3103 		}
3104 
3105 		if (!perf_stat.st_size) {
3106 			fprintf(stderr, "zero-sized file, nothing to do!\n");
3107 			goto out_delete;
3108 		}
3109 
3110 		scripting_ops = script_spec__lookup(generate_script_lang);
3111 		if (!scripting_ops) {
3112 			fprintf(stderr, "invalid language specifier");
3113 			err = -ENOENT;
3114 			goto out_delete;
3115 		}
3116 
3117 		err = scripting_ops->generate_script(session->tevent.pevent,
3118 						     "perf-script");
3119 		goto out_delete;
3120 	}
3121 
3122 	if (script_name) {
3123 		err = scripting_ops->start_script(script_name, argc, argv);
3124 		if (err)
3125 			goto out_delete;
3126 		pr_debug("perf script started with script %s\n\n", script_name);
3127 		script_started = true;
3128 	}
3129 
3130 
3131 	err = perf_session__check_output_opt(session);
3132 	if (err < 0)
3133 		goto out_delete;
3134 
3135 	/* needs to be parsed after looking up reference time */
3136 	if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3137 		pr_err("Invalid time string\n");
3138 		err = -EINVAL;
3139 		goto out_delete;
3140 	}
3141 
3142 	err = __cmd_script(&script);
3143 
3144 	flush_scripting();
3145 
3146 out_delete:
3147 	perf_evlist__free_stats(session->evlist);
3148 	perf_session__delete(session);
3149 
3150 	if (script_started)
3151 		cleanup_scripting();
3152 out:
3153 	return err;
3154 }
3155