xref: /OK3568_Linux_fs/kernel/tools/perf/builtin-script.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include "builtin.h"
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include "util/counts.h"
5*4882a593Smuzhiyun #include "util/debug.h"
6*4882a593Smuzhiyun #include "util/dso.h"
7*4882a593Smuzhiyun #include <subcmd/exec-cmd.h>
8*4882a593Smuzhiyun #include "util/header.h"
9*4882a593Smuzhiyun #include <subcmd/parse-options.h>
10*4882a593Smuzhiyun #include "util/perf_regs.h"
11*4882a593Smuzhiyun #include "util/session.h"
12*4882a593Smuzhiyun #include "util/tool.h"
13*4882a593Smuzhiyun #include "util/map.h"
14*4882a593Smuzhiyun #include "util/srcline.h"
15*4882a593Smuzhiyun #include "util/symbol.h"
16*4882a593Smuzhiyun #include "util/thread.h"
17*4882a593Smuzhiyun #include "util/trace-event.h"
18*4882a593Smuzhiyun #include "util/evlist.h"
19*4882a593Smuzhiyun #include "util/evsel.h"
20*4882a593Smuzhiyun #include "util/evsel_fprintf.h"
21*4882a593Smuzhiyun #include "util/evswitch.h"
22*4882a593Smuzhiyun #include "util/sort.h"
23*4882a593Smuzhiyun #include "util/data.h"
24*4882a593Smuzhiyun #include "util/auxtrace.h"
25*4882a593Smuzhiyun #include "util/cpumap.h"
26*4882a593Smuzhiyun #include "util/thread_map.h"
27*4882a593Smuzhiyun #include "util/stat.h"
28*4882a593Smuzhiyun #include "util/color.h"
29*4882a593Smuzhiyun #include "util/string2.h"
30*4882a593Smuzhiyun #include "util/thread-stack.h"
31*4882a593Smuzhiyun #include "util/time-utils.h"
32*4882a593Smuzhiyun #include "util/path.h"
33*4882a593Smuzhiyun #include "ui/ui.h"
34*4882a593Smuzhiyun #include "print_binary.h"
35*4882a593Smuzhiyun #include "archinsn.h"
36*4882a593Smuzhiyun #include <linux/bitmap.h>
37*4882a593Smuzhiyun #include <linux/kernel.h>
38*4882a593Smuzhiyun #include <linux/stringify.h>
39*4882a593Smuzhiyun #include <linux/time64.h>
40*4882a593Smuzhiyun #include <linux/zalloc.h>
41*4882a593Smuzhiyun #include <sys/utsname.h>
42*4882a593Smuzhiyun #include "asm/bug.h"
43*4882a593Smuzhiyun #include "util/mem-events.h"
44*4882a593Smuzhiyun #include "util/dump-insn.h"
45*4882a593Smuzhiyun #include <dirent.h>
46*4882a593Smuzhiyun #include <errno.h>
47*4882a593Smuzhiyun #include <inttypes.h>
48*4882a593Smuzhiyun #include <signal.h>
49*4882a593Smuzhiyun #include <sys/param.h>
50*4882a593Smuzhiyun #include <sys/types.h>
51*4882a593Smuzhiyun #include <sys/stat.h>
52*4882a593Smuzhiyun #include <fcntl.h>
53*4882a593Smuzhiyun #include <unistd.h>
54*4882a593Smuzhiyun #include <subcmd/pager.h>
55*4882a593Smuzhiyun #include <perf/evlist.h>
56*4882a593Smuzhiyun #include <linux/err.h>
57*4882a593Smuzhiyun #include "util/record.h"
58*4882a593Smuzhiyun #include "util/util.h"
59*4882a593Smuzhiyun #include "perf.h"
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #include <linux/ctype.h>
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static char const		*script_name;
64*4882a593Smuzhiyun static char const		*generate_script_lang;
65*4882a593Smuzhiyun static bool			reltime;
66*4882a593Smuzhiyun static bool			deltatime;
67*4882a593Smuzhiyun static u64			initial_time;
68*4882a593Smuzhiyun static u64			previous_time;
69*4882a593Smuzhiyun static bool			debug_mode;
70*4882a593Smuzhiyun static u64			last_timestamp;
71*4882a593Smuzhiyun static u64			nr_unordered;
72*4882a593Smuzhiyun static bool			no_callchain;
73*4882a593Smuzhiyun static bool			latency_format;
74*4882a593Smuzhiyun static bool			system_wide;
75*4882a593Smuzhiyun static bool			print_flags;
76*4882a593Smuzhiyun static const char		*cpu_list;
77*4882a593Smuzhiyun static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
78*4882a593Smuzhiyun static struct perf_stat_config	stat_config;
79*4882a593Smuzhiyun static int			max_blocks;
80*4882a593Smuzhiyun static bool			native_arch;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun enum perf_output_field {
85*4882a593Smuzhiyun 	PERF_OUTPUT_COMM            = 1ULL << 0,
86*4882a593Smuzhiyun 	PERF_OUTPUT_TID             = 1ULL << 1,
87*4882a593Smuzhiyun 	PERF_OUTPUT_PID             = 1ULL << 2,
88*4882a593Smuzhiyun 	PERF_OUTPUT_TIME            = 1ULL << 3,
89*4882a593Smuzhiyun 	PERF_OUTPUT_CPU             = 1ULL << 4,
90*4882a593Smuzhiyun 	PERF_OUTPUT_EVNAME          = 1ULL << 5,
91*4882a593Smuzhiyun 	PERF_OUTPUT_TRACE           = 1ULL << 6,
92*4882a593Smuzhiyun 	PERF_OUTPUT_IP              = 1ULL << 7,
93*4882a593Smuzhiyun 	PERF_OUTPUT_SYM             = 1ULL << 8,
94*4882a593Smuzhiyun 	PERF_OUTPUT_DSO             = 1ULL << 9,
95*4882a593Smuzhiyun 	PERF_OUTPUT_ADDR            = 1ULL << 10,
96*4882a593Smuzhiyun 	PERF_OUTPUT_SYMOFFSET       = 1ULL << 11,
97*4882a593Smuzhiyun 	PERF_OUTPUT_SRCLINE         = 1ULL << 12,
98*4882a593Smuzhiyun 	PERF_OUTPUT_PERIOD          = 1ULL << 13,
99*4882a593Smuzhiyun 	PERF_OUTPUT_IREGS	    = 1ULL << 14,
100*4882a593Smuzhiyun 	PERF_OUTPUT_BRSTACK	    = 1ULL << 15,
101*4882a593Smuzhiyun 	PERF_OUTPUT_BRSTACKSYM	    = 1ULL << 16,
102*4882a593Smuzhiyun 	PERF_OUTPUT_DATA_SRC	    = 1ULL << 17,
103*4882a593Smuzhiyun 	PERF_OUTPUT_WEIGHT	    = 1ULL << 18,
104*4882a593Smuzhiyun 	PERF_OUTPUT_BPF_OUTPUT	    = 1ULL << 19,
105*4882a593Smuzhiyun 	PERF_OUTPUT_CALLINDENT	    = 1ULL << 20,
106*4882a593Smuzhiyun 	PERF_OUTPUT_INSN	    = 1ULL << 21,
107*4882a593Smuzhiyun 	PERF_OUTPUT_INSNLEN	    = 1ULL << 22,
108*4882a593Smuzhiyun 	PERF_OUTPUT_BRSTACKINSN	    = 1ULL << 23,
109*4882a593Smuzhiyun 	PERF_OUTPUT_BRSTACKOFF	    = 1ULL << 24,
110*4882a593Smuzhiyun 	PERF_OUTPUT_SYNTH           = 1ULL << 25,
111*4882a593Smuzhiyun 	PERF_OUTPUT_PHYS_ADDR       = 1ULL << 26,
112*4882a593Smuzhiyun 	PERF_OUTPUT_UREGS	    = 1ULL << 27,
113*4882a593Smuzhiyun 	PERF_OUTPUT_METRIC	    = 1ULL << 28,
114*4882a593Smuzhiyun 	PERF_OUTPUT_MISC            = 1ULL << 29,
115*4882a593Smuzhiyun 	PERF_OUTPUT_SRCCODE	    = 1ULL << 30,
116*4882a593Smuzhiyun 	PERF_OUTPUT_IPC             = 1ULL << 31,
117*4882a593Smuzhiyun 	PERF_OUTPUT_TOD             = 1ULL << 32,
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun struct perf_script {
121*4882a593Smuzhiyun 	struct perf_tool	tool;
122*4882a593Smuzhiyun 	struct perf_session	*session;
123*4882a593Smuzhiyun 	bool			show_task_events;
124*4882a593Smuzhiyun 	bool			show_mmap_events;
125*4882a593Smuzhiyun 	bool			show_switch_events;
126*4882a593Smuzhiyun 	bool			show_namespace_events;
127*4882a593Smuzhiyun 	bool			show_lost_events;
128*4882a593Smuzhiyun 	bool			show_round_events;
129*4882a593Smuzhiyun 	bool			show_bpf_events;
130*4882a593Smuzhiyun 	bool			show_cgroup_events;
131*4882a593Smuzhiyun 	bool			show_text_poke_events;
132*4882a593Smuzhiyun 	bool			allocated;
133*4882a593Smuzhiyun 	bool			per_event_dump;
134*4882a593Smuzhiyun 	bool			stitch_lbr;
135*4882a593Smuzhiyun 	struct evswitch		evswitch;
136*4882a593Smuzhiyun 	struct perf_cpu_map	*cpus;
137*4882a593Smuzhiyun 	struct perf_thread_map *threads;
138*4882a593Smuzhiyun 	int			name_width;
139*4882a593Smuzhiyun 	const char              *time_str;
140*4882a593Smuzhiyun 	struct perf_time_interval *ptime_range;
141*4882a593Smuzhiyun 	int			range_size;
142*4882a593Smuzhiyun 	int			range_num;
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun struct output_option {
146*4882a593Smuzhiyun 	const char *str;
147*4882a593Smuzhiyun 	enum perf_output_field field;
148*4882a593Smuzhiyun } all_output_options[] = {
149*4882a593Smuzhiyun 	{.str = "comm",  .field = PERF_OUTPUT_COMM},
150*4882a593Smuzhiyun 	{.str = "tid",   .field = PERF_OUTPUT_TID},
151*4882a593Smuzhiyun 	{.str = "pid",   .field = PERF_OUTPUT_PID},
152*4882a593Smuzhiyun 	{.str = "time",  .field = PERF_OUTPUT_TIME},
153*4882a593Smuzhiyun 	{.str = "cpu",   .field = PERF_OUTPUT_CPU},
154*4882a593Smuzhiyun 	{.str = "event", .field = PERF_OUTPUT_EVNAME},
155*4882a593Smuzhiyun 	{.str = "trace", .field = PERF_OUTPUT_TRACE},
156*4882a593Smuzhiyun 	{.str = "ip",    .field = PERF_OUTPUT_IP},
157*4882a593Smuzhiyun 	{.str = "sym",   .field = PERF_OUTPUT_SYM},
158*4882a593Smuzhiyun 	{.str = "dso",   .field = PERF_OUTPUT_DSO},
159*4882a593Smuzhiyun 	{.str = "addr",  .field = PERF_OUTPUT_ADDR},
160*4882a593Smuzhiyun 	{.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
161*4882a593Smuzhiyun 	{.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
162*4882a593Smuzhiyun 	{.str = "period", .field = PERF_OUTPUT_PERIOD},
163*4882a593Smuzhiyun 	{.str = "iregs", .field = PERF_OUTPUT_IREGS},
164*4882a593Smuzhiyun 	{.str = "uregs", .field = PERF_OUTPUT_UREGS},
165*4882a593Smuzhiyun 	{.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
166*4882a593Smuzhiyun 	{.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
167*4882a593Smuzhiyun 	{.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
168*4882a593Smuzhiyun 	{.str = "weight",   .field = PERF_OUTPUT_WEIGHT},
169*4882a593Smuzhiyun 	{.str = "bpf-output",   .field = PERF_OUTPUT_BPF_OUTPUT},
170*4882a593Smuzhiyun 	{.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
171*4882a593Smuzhiyun 	{.str = "insn", .field = PERF_OUTPUT_INSN},
172*4882a593Smuzhiyun 	{.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
173*4882a593Smuzhiyun 	{.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
174*4882a593Smuzhiyun 	{.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
175*4882a593Smuzhiyun 	{.str = "synth", .field = PERF_OUTPUT_SYNTH},
176*4882a593Smuzhiyun 	{.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
177*4882a593Smuzhiyun 	{.str = "metric", .field = PERF_OUTPUT_METRIC},
178*4882a593Smuzhiyun 	{.str = "misc", .field = PERF_OUTPUT_MISC},
179*4882a593Smuzhiyun 	{.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
180*4882a593Smuzhiyun 	{.str = "ipc", .field = PERF_OUTPUT_IPC},
181*4882a593Smuzhiyun 	{.str = "tod", .field = PERF_OUTPUT_TOD},
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun enum {
185*4882a593Smuzhiyun 	OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
186*4882a593Smuzhiyun 	OUTPUT_TYPE_MAX
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /* default set to maintain compatibility with current format */
190*4882a593Smuzhiyun static struct {
191*4882a593Smuzhiyun 	bool user_set;
192*4882a593Smuzhiyun 	bool wildcard_set;
193*4882a593Smuzhiyun 	unsigned int print_ip_opts;
194*4882a593Smuzhiyun 	u64 fields;
195*4882a593Smuzhiyun 	u64 invalid_fields;
196*4882a593Smuzhiyun 	u64 user_set_fields;
197*4882a593Smuzhiyun 	u64 user_unset_fields;
198*4882a593Smuzhiyun } output[OUTPUT_TYPE_MAX] = {
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	[PERF_TYPE_HARDWARE] = {
201*4882a593Smuzhiyun 		.user_set = false,
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
204*4882a593Smuzhiyun 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
205*4882a593Smuzhiyun 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
206*4882a593Smuzhiyun 			      PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
207*4882a593Smuzhiyun 			      PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
210*4882a593Smuzhiyun 	},
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	[PERF_TYPE_SOFTWARE] = {
213*4882a593Smuzhiyun 		.user_set = false,
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
216*4882a593Smuzhiyun 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
217*4882a593Smuzhiyun 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
218*4882a593Smuzhiyun 			      PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
219*4882a593Smuzhiyun 			      PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
220*4882a593Smuzhiyun 			      PERF_OUTPUT_BPF_OUTPUT,
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 		.invalid_fields = PERF_OUTPUT_TRACE,
223*4882a593Smuzhiyun 	},
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	[PERF_TYPE_TRACEPOINT] = {
226*4882a593Smuzhiyun 		.user_set = false,
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
229*4882a593Smuzhiyun 				  PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
230*4882a593Smuzhiyun 				  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
231*4882a593Smuzhiyun 	},
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	[PERF_TYPE_HW_CACHE] = {
234*4882a593Smuzhiyun 		.user_set = false,
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
237*4882a593Smuzhiyun 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
238*4882a593Smuzhiyun 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
239*4882a593Smuzhiyun 			      PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
240*4882a593Smuzhiyun 			      PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
243*4882a593Smuzhiyun 	},
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	[PERF_TYPE_RAW] = {
246*4882a593Smuzhiyun 		.user_set = false,
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
249*4882a593Smuzhiyun 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
250*4882a593Smuzhiyun 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
251*4882a593Smuzhiyun 			      PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
252*4882a593Smuzhiyun 			      PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
253*4882a593Smuzhiyun 			      PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
254*4882a593Smuzhiyun 			      PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
257*4882a593Smuzhiyun 	},
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	[PERF_TYPE_BREAKPOINT] = {
260*4882a593Smuzhiyun 		.user_set = false,
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
263*4882a593Smuzhiyun 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
264*4882a593Smuzhiyun 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
265*4882a593Smuzhiyun 			      PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
266*4882a593Smuzhiyun 			      PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
269*4882a593Smuzhiyun 	},
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	[OUTPUT_TYPE_SYNTH] = {
272*4882a593Smuzhiyun 		.user_set = false,
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
275*4882a593Smuzhiyun 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
276*4882a593Smuzhiyun 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
277*4882a593Smuzhiyun 			      PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
278*4882a593Smuzhiyun 			      PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
281*4882a593Smuzhiyun 	},
282*4882a593Smuzhiyun };
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun struct evsel_script {
285*4882a593Smuzhiyun        char *filename;
286*4882a593Smuzhiyun        FILE *fp;
287*4882a593Smuzhiyun        u64  samples;
288*4882a593Smuzhiyun        /* For metric output */
289*4882a593Smuzhiyun        u64  val;
290*4882a593Smuzhiyun        int  gnum;
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun 
evsel_script(struct evsel * evsel)293*4882a593Smuzhiyun static inline struct evsel_script *evsel_script(struct evsel *evsel)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	return (struct evsel_script *)evsel->priv;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
perf_evsel_script__new(struct evsel * evsel,struct perf_data * data)298*4882a593Smuzhiyun static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
299*4882a593Smuzhiyun 							struct perf_data *data)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	struct evsel_script *es = zalloc(sizeof(*es));
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (es != NULL) {
304*4882a593Smuzhiyun 		if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
305*4882a593Smuzhiyun 			goto out_free;
306*4882a593Smuzhiyun 		es->fp = fopen(es->filename, "w");
307*4882a593Smuzhiyun 		if (es->fp == NULL)
308*4882a593Smuzhiyun 			goto out_free_filename;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	return es;
312*4882a593Smuzhiyun out_free_filename:
313*4882a593Smuzhiyun 	zfree(&es->filename);
314*4882a593Smuzhiyun out_free:
315*4882a593Smuzhiyun 	free(es);
316*4882a593Smuzhiyun 	return NULL;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
perf_evsel_script__delete(struct evsel_script * es)319*4882a593Smuzhiyun static void perf_evsel_script__delete(struct evsel_script *es)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	zfree(&es->filename);
322*4882a593Smuzhiyun 	fclose(es->fp);
323*4882a593Smuzhiyun 	es->fp = NULL;
324*4882a593Smuzhiyun 	free(es);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
perf_evsel_script__fprintf(struct evsel_script * es,FILE * fp)327*4882a593Smuzhiyun static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct stat st;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	fstat(fileno(es->fp), &st);
332*4882a593Smuzhiyun 	return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
333*4882a593Smuzhiyun 		       st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
output_type(unsigned int type)336*4882a593Smuzhiyun static inline int output_type(unsigned int type)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	switch (type) {
339*4882a593Smuzhiyun 	case PERF_TYPE_SYNTH:
340*4882a593Smuzhiyun 		return OUTPUT_TYPE_SYNTH;
341*4882a593Smuzhiyun 	default:
342*4882a593Smuzhiyun 		return type;
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
attr_type(unsigned int type)346*4882a593Smuzhiyun static inline unsigned int attr_type(unsigned int type)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	switch (type) {
349*4882a593Smuzhiyun 	case OUTPUT_TYPE_SYNTH:
350*4882a593Smuzhiyun 		return PERF_TYPE_SYNTH;
351*4882a593Smuzhiyun 	default:
352*4882a593Smuzhiyun 		return type;
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
output_set_by_user(void)356*4882a593Smuzhiyun static bool output_set_by_user(void)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	int j;
359*4882a593Smuzhiyun 	for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
360*4882a593Smuzhiyun 		if (output[j].user_set)
361*4882a593Smuzhiyun 			return true;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 	return false;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
output_field2str(enum perf_output_field field)366*4882a593Smuzhiyun static const char *output_field2str(enum perf_output_field field)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	int i, imax = ARRAY_SIZE(all_output_options);
369*4882a593Smuzhiyun 	const char *str = "";
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	for (i = 0; i < imax; ++i) {
372*4882a593Smuzhiyun 		if (all_output_options[i].field == field) {
373*4882a593Smuzhiyun 			str = all_output_options[i].str;
374*4882a593Smuzhiyun 			break;
375*4882a593Smuzhiyun 		}
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 	return str;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun #define PRINT_FIELD(x)  (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
381*4882a593Smuzhiyun 
evsel__do_check_stype(struct evsel * evsel,u64 sample_type,const char * sample_msg,enum perf_output_field field,bool allow_user_set)382*4882a593Smuzhiyun static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
383*4882a593Smuzhiyun 				 enum perf_output_field field, bool allow_user_set)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
386*4882a593Smuzhiyun 	int type = output_type(attr->type);
387*4882a593Smuzhiyun 	const char *evname;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	if (attr->sample_type & sample_type)
390*4882a593Smuzhiyun 		return 0;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (output[type].user_set_fields & field) {
393*4882a593Smuzhiyun 		if (allow_user_set)
394*4882a593Smuzhiyun 			return 0;
395*4882a593Smuzhiyun 		evname = evsel__name(evsel);
396*4882a593Smuzhiyun 		pr_err("Samples for '%s' event do not have %s attribute set. "
397*4882a593Smuzhiyun 		       "Cannot print '%s' field.\n",
398*4882a593Smuzhiyun 		       evname, sample_msg, output_field2str(field));
399*4882a593Smuzhiyun 		return -1;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	/* user did not ask for it explicitly so remove from the default list */
403*4882a593Smuzhiyun 	output[type].fields &= ~field;
404*4882a593Smuzhiyun 	evname = evsel__name(evsel);
405*4882a593Smuzhiyun 	pr_debug("Samples for '%s' event do not have %s attribute set. "
406*4882a593Smuzhiyun 		 "Skipping '%s' field.\n",
407*4882a593Smuzhiyun 		 evname, sample_msg, output_field2str(field));
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return 0;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
evsel__check_stype(struct evsel * evsel,u64 sample_type,const char * sample_msg,enum perf_output_field field)412*4882a593Smuzhiyun static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
413*4882a593Smuzhiyun 			      enum perf_output_field field)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun 
evsel__check_attr(struct evsel * evsel,struct perf_session * session)418*4882a593Smuzhiyun static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
421*4882a593Smuzhiyun 	bool allow_user_set;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (perf_header__has_feat(&session->header, HEADER_STAT))
424*4882a593Smuzhiyun 		return 0;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	allow_user_set = perf_header__has_feat(&session->header,
427*4882a593Smuzhiyun 					       HEADER_AUXTRACE);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	if (PRINT_FIELD(TRACE) &&
430*4882a593Smuzhiyun 	    !perf_session__has_traces(session, "record -R"))
431*4882a593Smuzhiyun 		return -EINVAL;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	if (PRINT_FIELD(IP)) {
434*4882a593Smuzhiyun 		if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
435*4882a593Smuzhiyun 			return -EINVAL;
436*4882a593Smuzhiyun 	}
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	if (PRINT_FIELD(ADDR) &&
439*4882a593Smuzhiyun 	    evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
440*4882a593Smuzhiyun 		return -EINVAL;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	if (PRINT_FIELD(DATA_SRC) &&
443*4882a593Smuzhiyun 	    evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC))
444*4882a593Smuzhiyun 		return -EINVAL;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (PRINT_FIELD(WEIGHT) &&
447*4882a593Smuzhiyun 	    evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT", PERF_OUTPUT_WEIGHT))
448*4882a593Smuzhiyun 		return -EINVAL;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	if (PRINT_FIELD(SYM) &&
451*4882a593Smuzhiyun 	    !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
452*4882a593Smuzhiyun 		pr_err("Display of symbols requested but neither sample IP nor "
453*4882a593Smuzhiyun 			   "sample address\navailable. Hence, no addresses to convert "
454*4882a593Smuzhiyun 		       "to symbols.\n");
455*4882a593Smuzhiyun 		return -EINVAL;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 	if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
458*4882a593Smuzhiyun 		pr_err("Display of offsets requested but symbol is not"
459*4882a593Smuzhiyun 		       "selected.\n");
460*4882a593Smuzhiyun 		return -EINVAL;
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun 	if (PRINT_FIELD(DSO) &&
463*4882a593Smuzhiyun 	    !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
464*4882a593Smuzhiyun 		pr_err("Display of DSO requested but no address to convert.\n");
465*4882a593Smuzhiyun 		return -EINVAL;
466*4882a593Smuzhiyun 	}
467*4882a593Smuzhiyun 	if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
468*4882a593Smuzhiyun 		pr_err("Display of source line number requested but sample IP is not\n"
469*4882a593Smuzhiyun 		       "selected. Hence, no address to lookup the source line number.\n");
470*4882a593Smuzhiyun 		return -EINVAL;
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 	if (PRINT_FIELD(BRSTACKINSN) && !allow_user_set &&
473*4882a593Smuzhiyun 	    !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
474*4882a593Smuzhiyun 		pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
475*4882a593Smuzhiyun 		       "Hint: run 'perf record -b ...'\n");
476*4882a593Smuzhiyun 		return -EINVAL;
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 	if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
479*4882a593Smuzhiyun 	    evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
480*4882a593Smuzhiyun 		return -EINVAL;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (PRINT_FIELD(TIME) &&
483*4882a593Smuzhiyun 	    evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
484*4882a593Smuzhiyun 		return -EINVAL;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (PRINT_FIELD(CPU) &&
487*4882a593Smuzhiyun 	    evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
488*4882a593Smuzhiyun 		return -EINVAL;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	if (PRINT_FIELD(IREGS) &&
491*4882a593Smuzhiyun 	    evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
492*4882a593Smuzhiyun 		return -EINVAL;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	if (PRINT_FIELD(UREGS) &&
495*4882a593Smuzhiyun 	    evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
496*4882a593Smuzhiyun 		return -EINVAL;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	if (PRINT_FIELD(PHYS_ADDR) &&
499*4882a593Smuzhiyun 	    evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR))
500*4882a593Smuzhiyun 		return -EINVAL;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	return 0;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
set_print_ip_opts(struct perf_event_attr * attr)505*4882a593Smuzhiyun static void set_print_ip_opts(struct perf_event_attr *attr)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	unsigned int type = output_type(attr->type);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	output[type].print_ip_opts = 0;
510*4882a593Smuzhiyun 	if (PRINT_FIELD(IP))
511*4882a593Smuzhiyun 		output[type].print_ip_opts |= EVSEL__PRINT_IP;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (PRINT_FIELD(SYM))
514*4882a593Smuzhiyun 		output[type].print_ip_opts |= EVSEL__PRINT_SYM;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if (PRINT_FIELD(DSO))
517*4882a593Smuzhiyun 		output[type].print_ip_opts |= EVSEL__PRINT_DSO;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	if (PRINT_FIELD(SYMOFFSET))
520*4882a593Smuzhiyun 		output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	if (PRINT_FIELD(SRCLINE))
523*4882a593Smuzhiyun 		output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun  * verify all user requested events exist and the samples
528*4882a593Smuzhiyun  * have the expected data
529*4882a593Smuzhiyun  */
perf_session__check_output_opt(struct perf_session * session)530*4882a593Smuzhiyun static int perf_session__check_output_opt(struct perf_session *session)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	bool tod = false;
533*4882a593Smuzhiyun 	unsigned int j;
534*4882a593Smuzhiyun 	struct evsel *evsel;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
537*4882a593Smuzhiyun 		evsel = perf_session__find_first_evtype(session, attr_type(j));
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 		/*
540*4882a593Smuzhiyun 		 * even if fields is set to 0 (ie., show nothing) event must
541*4882a593Smuzhiyun 		 * exist if user explicitly includes it on the command line
542*4882a593Smuzhiyun 		 */
543*4882a593Smuzhiyun 		if (!evsel && output[j].user_set && !output[j].wildcard_set &&
544*4882a593Smuzhiyun 		    j != OUTPUT_TYPE_SYNTH) {
545*4882a593Smuzhiyun 			pr_err("%s events do not exist. "
546*4882a593Smuzhiyun 			       "Remove corresponding -F option to proceed.\n",
547*4882a593Smuzhiyun 			       event_type(j));
548*4882a593Smuzhiyun 			return -1;
549*4882a593Smuzhiyun 		}
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 		if (evsel && output[j].fields &&
552*4882a593Smuzhiyun 			evsel__check_attr(evsel, session))
553*4882a593Smuzhiyun 			return -1;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 		if (evsel == NULL)
556*4882a593Smuzhiyun 			continue;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 		set_print_ip_opts(&evsel->core.attr);
559*4882a593Smuzhiyun 		tod |= output[j].fields & PERF_OUTPUT_TOD;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (!no_callchain) {
563*4882a593Smuzhiyun 		bool use_callchain = false;
564*4882a593Smuzhiyun 		bool not_pipe = false;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 		evlist__for_each_entry(session->evlist, evsel) {
567*4882a593Smuzhiyun 			not_pipe = true;
568*4882a593Smuzhiyun 			if (evsel__has_callchain(evsel)) {
569*4882a593Smuzhiyun 				use_callchain = true;
570*4882a593Smuzhiyun 				break;
571*4882a593Smuzhiyun 			}
572*4882a593Smuzhiyun 		}
573*4882a593Smuzhiyun 		if (not_pipe && !use_callchain)
574*4882a593Smuzhiyun 			symbol_conf.use_callchain = false;
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	/*
578*4882a593Smuzhiyun 	 * set default for tracepoints to print symbols only
579*4882a593Smuzhiyun 	 * if callchains are present
580*4882a593Smuzhiyun 	 */
581*4882a593Smuzhiyun 	if (symbol_conf.use_callchain &&
582*4882a593Smuzhiyun 	    !output[PERF_TYPE_TRACEPOINT].user_set) {
583*4882a593Smuzhiyun 		j = PERF_TYPE_TRACEPOINT;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 		evlist__for_each_entry(session->evlist, evsel) {
586*4882a593Smuzhiyun 			if (evsel->core.attr.type != j)
587*4882a593Smuzhiyun 				continue;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 			if (evsel__has_callchain(evsel)) {
590*4882a593Smuzhiyun 				output[j].fields |= PERF_OUTPUT_IP;
591*4882a593Smuzhiyun 				output[j].fields |= PERF_OUTPUT_SYM;
592*4882a593Smuzhiyun 				output[j].fields |= PERF_OUTPUT_SYMOFFSET;
593*4882a593Smuzhiyun 				output[j].fields |= PERF_OUTPUT_DSO;
594*4882a593Smuzhiyun 				set_print_ip_opts(&evsel->core.attr);
595*4882a593Smuzhiyun 				goto out;
596*4882a593Smuzhiyun 			}
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (tod && !session->header.env.clock.enabled) {
601*4882a593Smuzhiyun 		pr_err("Can't provide 'tod' time, missing clock data. "
602*4882a593Smuzhiyun 		       "Please record with -k/--clockid option.\n");
603*4882a593Smuzhiyun 		return -1;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun out:
606*4882a593Smuzhiyun 	return 0;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
perf_sample__fprintf_regs(struct regs_dump * regs,uint64_t mask,FILE * fp)609*4882a593Smuzhiyun static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
610*4882a593Smuzhiyun 				     FILE *fp)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	unsigned i = 0, r;
613*4882a593Smuzhiyun 	int printed = 0;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (!regs || !regs->regs)
616*4882a593Smuzhiyun 		return 0;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
621*4882a593Smuzhiyun 		u64 val = regs->regs[i++];
622*4882a593Smuzhiyun 		printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	return printed;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun #define DEFAULT_TOD_FMT "%F %H:%M:%S"
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun static char*
tod_scnprintf(struct perf_script * script,char * buf,int buflen,u64 timestamp)631*4882a593Smuzhiyun tod_scnprintf(struct perf_script *script, char *buf, int buflen,
632*4882a593Smuzhiyun 	     u64 timestamp)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	u64 tod_ns, clockid_ns;
635*4882a593Smuzhiyun 	struct perf_env *env;
636*4882a593Smuzhiyun 	unsigned long nsec;
637*4882a593Smuzhiyun 	struct tm ltime;
638*4882a593Smuzhiyun 	char date[64];
639*4882a593Smuzhiyun 	time_t sec;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	buf[0] = '\0';
642*4882a593Smuzhiyun 	if (buflen < 64 || !script)
643*4882a593Smuzhiyun 		return buf;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	env = &script->session->header.env;
646*4882a593Smuzhiyun 	if (!env->clock.enabled) {
647*4882a593Smuzhiyun 		scnprintf(buf, buflen, "disabled");
648*4882a593Smuzhiyun 		return buf;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	clockid_ns = env->clock.clockid_ns;
652*4882a593Smuzhiyun 	tod_ns     = env->clock.tod_ns;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	if (timestamp > clockid_ns)
655*4882a593Smuzhiyun 		tod_ns += timestamp - clockid_ns;
656*4882a593Smuzhiyun 	else
657*4882a593Smuzhiyun 		tod_ns -= clockid_ns - timestamp;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	sec  = (time_t) (tod_ns / NSEC_PER_SEC);
660*4882a593Smuzhiyun 	nsec = tod_ns - sec * NSEC_PER_SEC;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	if (localtime_r(&sec, &ltime) == NULL) {
663*4882a593Smuzhiyun 		scnprintf(buf, buflen, "failed");
664*4882a593Smuzhiyun 	} else {
665*4882a593Smuzhiyun 		strftime(date, sizeof(date), DEFAULT_TOD_FMT, &ltime);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		if (symbol_conf.nanosecs) {
668*4882a593Smuzhiyun 			snprintf(buf, buflen, "%s.%09lu", date, nsec);
669*4882a593Smuzhiyun 		} else {
670*4882a593Smuzhiyun 			snprintf(buf, buflen, "%s.%06lu",
671*4882a593Smuzhiyun 				 date, nsec / NSEC_PER_USEC);
672*4882a593Smuzhiyun 		}
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	return buf;
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun 
perf_sample__fprintf_iregs(struct perf_sample * sample,struct perf_event_attr * attr,FILE * fp)678*4882a593Smuzhiyun static int perf_sample__fprintf_iregs(struct perf_sample *sample,
679*4882a593Smuzhiyun 				      struct perf_event_attr *attr, FILE *fp)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun 	return perf_sample__fprintf_regs(&sample->intr_regs,
682*4882a593Smuzhiyun 					 attr->sample_regs_intr, fp);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
perf_sample__fprintf_uregs(struct perf_sample * sample,struct perf_event_attr * attr,FILE * fp)685*4882a593Smuzhiyun static int perf_sample__fprintf_uregs(struct perf_sample *sample,
686*4882a593Smuzhiyun 				      struct perf_event_attr *attr, FILE *fp)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	return perf_sample__fprintf_regs(&sample->user_regs,
689*4882a593Smuzhiyun 					 attr->sample_regs_user, fp);
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun 
perf_sample__fprintf_start(struct perf_script * script,struct perf_sample * sample,struct thread * thread,struct evsel * evsel,u32 type,FILE * fp)692*4882a593Smuzhiyun static int perf_sample__fprintf_start(struct perf_script *script,
693*4882a593Smuzhiyun 				      struct perf_sample *sample,
694*4882a593Smuzhiyun 				      struct thread *thread,
695*4882a593Smuzhiyun 				      struct evsel *evsel,
696*4882a593Smuzhiyun 				      u32 type, FILE *fp)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
699*4882a593Smuzhiyun 	unsigned long secs;
700*4882a593Smuzhiyun 	unsigned long long nsecs;
701*4882a593Smuzhiyun 	int printed = 0;
702*4882a593Smuzhiyun 	char tstr[128];
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	if (PRINT_FIELD(COMM)) {
705*4882a593Smuzhiyun 		const char *comm = thread ? thread__comm_str(thread) : ":-1";
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 		if (latency_format)
708*4882a593Smuzhiyun 			printed += fprintf(fp, "%8.8s ", comm);
709*4882a593Smuzhiyun 		else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
710*4882a593Smuzhiyun 			printed += fprintf(fp, "%s ", comm);
711*4882a593Smuzhiyun 		else
712*4882a593Smuzhiyun 			printed += fprintf(fp, "%16s ", comm);
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
716*4882a593Smuzhiyun 		printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
717*4882a593Smuzhiyun 	else if (PRINT_FIELD(PID))
718*4882a593Smuzhiyun 		printed += fprintf(fp, "%5d ", sample->pid);
719*4882a593Smuzhiyun 	else if (PRINT_FIELD(TID))
720*4882a593Smuzhiyun 		printed += fprintf(fp, "%5d ", sample->tid);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	if (PRINT_FIELD(CPU)) {
723*4882a593Smuzhiyun 		if (latency_format)
724*4882a593Smuzhiyun 			printed += fprintf(fp, "%3d ", sample->cpu);
725*4882a593Smuzhiyun 		else
726*4882a593Smuzhiyun 			printed += fprintf(fp, "[%03d] ", sample->cpu);
727*4882a593Smuzhiyun 	}
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	if (PRINT_FIELD(MISC)) {
730*4882a593Smuzhiyun 		int ret = 0;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 		#define has(m) \
733*4882a593Smuzhiyun 			(sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 		if (has(KERNEL))
736*4882a593Smuzhiyun 			ret += fprintf(fp, "K");
737*4882a593Smuzhiyun 		if (has(USER))
738*4882a593Smuzhiyun 			ret += fprintf(fp, "U");
739*4882a593Smuzhiyun 		if (has(HYPERVISOR))
740*4882a593Smuzhiyun 			ret += fprintf(fp, "H");
741*4882a593Smuzhiyun 		if (has(GUEST_KERNEL))
742*4882a593Smuzhiyun 			ret += fprintf(fp, "G");
743*4882a593Smuzhiyun 		if (has(GUEST_USER))
744*4882a593Smuzhiyun 			ret += fprintf(fp, "g");
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 		switch (type) {
747*4882a593Smuzhiyun 		case PERF_RECORD_MMAP:
748*4882a593Smuzhiyun 		case PERF_RECORD_MMAP2:
749*4882a593Smuzhiyun 			if (has(MMAP_DATA))
750*4882a593Smuzhiyun 				ret += fprintf(fp, "M");
751*4882a593Smuzhiyun 			break;
752*4882a593Smuzhiyun 		case PERF_RECORD_COMM:
753*4882a593Smuzhiyun 			if (has(COMM_EXEC))
754*4882a593Smuzhiyun 				ret += fprintf(fp, "E");
755*4882a593Smuzhiyun 			break;
756*4882a593Smuzhiyun 		case PERF_RECORD_SWITCH:
757*4882a593Smuzhiyun 		case PERF_RECORD_SWITCH_CPU_WIDE:
758*4882a593Smuzhiyun 			if (has(SWITCH_OUT)) {
759*4882a593Smuzhiyun 				ret += fprintf(fp, "S");
760*4882a593Smuzhiyun 				if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
761*4882a593Smuzhiyun 					ret += fprintf(fp, "p");
762*4882a593Smuzhiyun 			}
763*4882a593Smuzhiyun 		default:
764*4882a593Smuzhiyun 			break;
765*4882a593Smuzhiyun 		}
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 		#undef has
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 		ret += fprintf(fp, "%*s", 6 - ret, " ");
770*4882a593Smuzhiyun 		printed += ret;
771*4882a593Smuzhiyun 	}
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	if (PRINT_FIELD(TOD)) {
774*4882a593Smuzhiyun 		tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
775*4882a593Smuzhiyun 		printed += fprintf(fp, "%s ", tstr);
776*4882a593Smuzhiyun 	}
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	if (PRINT_FIELD(TIME)) {
779*4882a593Smuzhiyun 		u64 t = sample->time;
780*4882a593Smuzhiyun 		if (reltime) {
781*4882a593Smuzhiyun 			if (!initial_time)
782*4882a593Smuzhiyun 				initial_time = sample->time;
783*4882a593Smuzhiyun 			t = sample->time - initial_time;
784*4882a593Smuzhiyun 		} else if (deltatime) {
785*4882a593Smuzhiyun 			if (previous_time)
786*4882a593Smuzhiyun 				t = sample->time - previous_time;
787*4882a593Smuzhiyun 			else {
788*4882a593Smuzhiyun 				t = 0;
789*4882a593Smuzhiyun 			}
790*4882a593Smuzhiyun 			previous_time = sample->time;
791*4882a593Smuzhiyun 		}
792*4882a593Smuzhiyun 		nsecs = t;
793*4882a593Smuzhiyun 		secs = nsecs / NSEC_PER_SEC;
794*4882a593Smuzhiyun 		nsecs -= secs * NSEC_PER_SEC;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 		if (symbol_conf.nanosecs)
797*4882a593Smuzhiyun 			printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
798*4882a593Smuzhiyun 		else {
799*4882a593Smuzhiyun 			char sample_time[32];
800*4882a593Smuzhiyun 			timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
801*4882a593Smuzhiyun 			printed += fprintf(fp, "%12s: ", sample_time);
802*4882a593Smuzhiyun 		}
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	return printed;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun static inline char
mispred_str(struct branch_entry * br)809*4882a593Smuzhiyun mispred_str(struct branch_entry *br)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun 	if (!(br->flags.mispred  || br->flags.predicted))
812*4882a593Smuzhiyun 		return '-';
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	return br->flags.predicted ? 'P' : 'M';
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
perf_sample__fprintf_brstack(struct perf_sample * sample,struct thread * thread,struct perf_event_attr * attr,FILE * fp)817*4882a593Smuzhiyun static int perf_sample__fprintf_brstack(struct perf_sample *sample,
818*4882a593Smuzhiyun 					struct thread *thread,
819*4882a593Smuzhiyun 					struct perf_event_attr *attr, FILE *fp)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun 	struct branch_stack *br = sample->branch_stack;
822*4882a593Smuzhiyun 	struct branch_entry *entries = perf_sample__branch_entries(sample);
823*4882a593Smuzhiyun 	struct addr_location alf, alt;
824*4882a593Smuzhiyun 	u64 i, from, to;
825*4882a593Smuzhiyun 	int printed = 0;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	if (!(br && br->nr))
828*4882a593Smuzhiyun 		return 0;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	for (i = 0; i < br->nr; i++) {
831*4882a593Smuzhiyun 		from = entries[i].from;
832*4882a593Smuzhiyun 		to   = entries[i].to;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
835*4882a593Smuzhiyun 			memset(&alf, 0, sizeof(alf));
836*4882a593Smuzhiyun 			memset(&alt, 0, sizeof(alt));
837*4882a593Smuzhiyun 			thread__find_map_fb(thread, sample->cpumode, from, &alf);
838*4882a593Smuzhiyun 			thread__find_map_fb(thread, sample->cpumode, to, &alt);
839*4882a593Smuzhiyun 		}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 		printed += fprintf(fp, " 0x%"PRIx64, from);
842*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
843*4882a593Smuzhiyun 			printed += fprintf(fp, "(");
844*4882a593Smuzhiyun 			printed += map__fprintf_dsoname(alf.map, fp);
845*4882a593Smuzhiyun 			printed += fprintf(fp, ")");
846*4882a593Smuzhiyun 		}
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 		printed += fprintf(fp, "/0x%"PRIx64, to);
849*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
850*4882a593Smuzhiyun 			printed += fprintf(fp, "(");
851*4882a593Smuzhiyun 			printed += map__fprintf_dsoname(alt.map, fp);
852*4882a593Smuzhiyun 			printed += fprintf(fp, ")");
853*4882a593Smuzhiyun 		}
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 		printed += fprintf(fp, "/%c/%c/%c/%d ",
856*4882a593Smuzhiyun 			mispred_str(entries + i),
857*4882a593Smuzhiyun 			entries[i].flags.in_tx ? 'X' : '-',
858*4882a593Smuzhiyun 			entries[i].flags.abort ? 'A' : '-',
859*4882a593Smuzhiyun 			entries[i].flags.cycles);
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	return printed;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun 
perf_sample__fprintf_brstacksym(struct perf_sample * sample,struct thread * thread,struct perf_event_attr * attr,FILE * fp)865*4882a593Smuzhiyun static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
866*4882a593Smuzhiyun 					   struct thread *thread,
867*4882a593Smuzhiyun 					   struct perf_event_attr *attr, FILE *fp)
868*4882a593Smuzhiyun {
869*4882a593Smuzhiyun 	struct branch_stack *br = sample->branch_stack;
870*4882a593Smuzhiyun 	struct branch_entry *entries = perf_sample__branch_entries(sample);
871*4882a593Smuzhiyun 	struct addr_location alf, alt;
872*4882a593Smuzhiyun 	u64 i, from, to;
873*4882a593Smuzhiyun 	int printed = 0;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	if (!(br && br->nr))
876*4882a593Smuzhiyun 		return 0;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	for (i = 0; i < br->nr; i++) {
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 		memset(&alf, 0, sizeof(alf));
881*4882a593Smuzhiyun 		memset(&alt, 0, sizeof(alt));
882*4882a593Smuzhiyun 		from = entries[i].from;
883*4882a593Smuzhiyun 		to   = entries[i].to;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 		thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
886*4882a593Smuzhiyun 		thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 		printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
889*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
890*4882a593Smuzhiyun 			printed += fprintf(fp, "(");
891*4882a593Smuzhiyun 			printed += map__fprintf_dsoname(alf.map, fp);
892*4882a593Smuzhiyun 			printed += fprintf(fp, ")");
893*4882a593Smuzhiyun 		}
894*4882a593Smuzhiyun 		printed += fprintf(fp, "%c", '/');
895*4882a593Smuzhiyun 		printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
896*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
897*4882a593Smuzhiyun 			printed += fprintf(fp, "(");
898*4882a593Smuzhiyun 			printed += map__fprintf_dsoname(alt.map, fp);
899*4882a593Smuzhiyun 			printed += fprintf(fp, ")");
900*4882a593Smuzhiyun 		}
901*4882a593Smuzhiyun 		printed += fprintf(fp, "/%c/%c/%c/%d ",
902*4882a593Smuzhiyun 			mispred_str(entries + i),
903*4882a593Smuzhiyun 			entries[i].flags.in_tx ? 'X' : '-',
904*4882a593Smuzhiyun 			entries[i].flags.abort ? 'A' : '-',
905*4882a593Smuzhiyun 			entries[i].flags.cycles);
906*4882a593Smuzhiyun 	}
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	return printed;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun 
perf_sample__fprintf_brstackoff(struct perf_sample * sample,struct thread * thread,struct perf_event_attr * attr,FILE * fp)911*4882a593Smuzhiyun static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
912*4882a593Smuzhiyun 					   struct thread *thread,
913*4882a593Smuzhiyun 					   struct perf_event_attr *attr, FILE *fp)
914*4882a593Smuzhiyun {
915*4882a593Smuzhiyun 	struct branch_stack *br = sample->branch_stack;
916*4882a593Smuzhiyun 	struct branch_entry *entries = perf_sample__branch_entries(sample);
917*4882a593Smuzhiyun 	struct addr_location alf, alt;
918*4882a593Smuzhiyun 	u64 i, from, to;
919*4882a593Smuzhiyun 	int printed = 0;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	if (!(br && br->nr))
922*4882a593Smuzhiyun 		return 0;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	for (i = 0; i < br->nr; i++) {
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 		memset(&alf, 0, sizeof(alf));
927*4882a593Smuzhiyun 		memset(&alt, 0, sizeof(alt));
928*4882a593Smuzhiyun 		from = entries[i].from;
929*4882a593Smuzhiyun 		to   = entries[i].to;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 		if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
932*4882a593Smuzhiyun 		    !alf.map->dso->adjust_symbols)
933*4882a593Smuzhiyun 			from = map__map_ip(alf.map, from);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 		if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
936*4882a593Smuzhiyun 		    !alt.map->dso->adjust_symbols)
937*4882a593Smuzhiyun 			to = map__map_ip(alt.map, to);
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 		printed += fprintf(fp, " 0x%"PRIx64, from);
940*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
941*4882a593Smuzhiyun 			printed += fprintf(fp, "(");
942*4882a593Smuzhiyun 			printed += map__fprintf_dsoname(alf.map, fp);
943*4882a593Smuzhiyun 			printed += fprintf(fp, ")");
944*4882a593Smuzhiyun 		}
945*4882a593Smuzhiyun 		printed += fprintf(fp, "/0x%"PRIx64, to);
946*4882a593Smuzhiyun 		if (PRINT_FIELD(DSO)) {
947*4882a593Smuzhiyun 			printed += fprintf(fp, "(");
948*4882a593Smuzhiyun 			printed += map__fprintf_dsoname(alt.map, fp);
949*4882a593Smuzhiyun 			printed += fprintf(fp, ")");
950*4882a593Smuzhiyun 		}
951*4882a593Smuzhiyun 		printed += fprintf(fp, "/%c/%c/%c/%d ",
952*4882a593Smuzhiyun 			mispred_str(entries + i),
953*4882a593Smuzhiyun 			entries[i].flags.in_tx ? 'X' : '-',
954*4882a593Smuzhiyun 			entries[i].flags.abort ? 'A' : '-',
955*4882a593Smuzhiyun 			entries[i].flags.cycles);
956*4882a593Smuzhiyun 	}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	return printed;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun #define MAXBB 16384UL
961*4882a593Smuzhiyun 
grab_bb(u8 * buffer,u64 start,u64 end,struct machine * machine,struct thread * thread,bool * is64bit,u8 * cpumode,bool last)962*4882a593Smuzhiyun static int grab_bb(u8 *buffer, u64 start, u64 end,
963*4882a593Smuzhiyun 		    struct machine *machine, struct thread *thread,
964*4882a593Smuzhiyun 		    bool *is64bit, u8 *cpumode, bool last)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	long offset, len;
967*4882a593Smuzhiyun 	struct addr_location al;
968*4882a593Smuzhiyun 	bool kernel;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	if (!start || !end)
971*4882a593Smuzhiyun 		return 0;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	kernel = machine__kernel_ip(machine, start);
974*4882a593Smuzhiyun 	if (kernel)
975*4882a593Smuzhiyun 		*cpumode = PERF_RECORD_MISC_KERNEL;
976*4882a593Smuzhiyun 	else
977*4882a593Smuzhiyun 		*cpumode = PERF_RECORD_MISC_USER;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	/*
980*4882a593Smuzhiyun 	 * Block overlaps between kernel and user.
981*4882a593Smuzhiyun 	 * This can happen due to ring filtering
982*4882a593Smuzhiyun 	 * On Intel CPUs the entry into the kernel is filtered,
983*4882a593Smuzhiyun 	 * but the exit is not. Let the caller patch it up.
984*4882a593Smuzhiyun 	 */
985*4882a593Smuzhiyun 	if (kernel != machine__kernel_ip(machine, end)) {
986*4882a593Smuzhiyun 		pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
987*4882a593Smuzhiyun 		return -ENXIO;
988*4882a593Smuzhiyun 	}
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	memset(&al, 0, sizeof(al));
991*4882a593Smuzhiyun 	if (end - start > MAXBB - MAXINSN) {
992*4882a593Smuzhiyun 		if (last)
993*4882a593Smuzhiyun 			pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
994*4882a593Smuzhiyun 		else
995*4882a593Smuzhiyun 			pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
996*4882a593Smuzhiyun 		return 0;
997*4882a593Smuzhiyun 	}
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
1000*4882a593Smuzhiyun 		pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1001*4882a593Smuzhiyun 		return 0;
1002*4882a593Smuzhiyun 	}
1003*4882a593Smuzhiyun 	if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
1004*4882a593Smuzhiyun 		pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1005*4882a593Smuzhiyun 		return 0;
1006*4882a593Smuzhiyun 	}
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	/* Load maps to ensure dso->is_64_bit has been updated */
1009*4882a593Smuzhiyun 	map__load(al.map);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	offset = al.map->map_ip(al.map, start);
1012*4882a593Smuzhiyun 	len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
1013*4882a593Smuzhiyun 				    end - start + MAXINSN);
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	*is64bit = al.map->dso->is_64_bit;
1016*4882a593Smuzhiyun 	if (len <= 0)
1017*4882a593Smuzhiyun 		pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
1018*4882a593Smuzhiyun 			start, end);
1019*4882a593Smuzhiyun 	return len;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
map__fprintf_srccode(struct map * map,u64 addr,FILE * fp,struct srccode_state * state)1022*4882a593Smuzhiyun static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun 	char *srcfile;
1025*4882a593Smuzhiyun 	int ret = 0;
1026*4882a593Smuzhiyun 	unsigned line;
1027*4882a593Smuzhiyun 	int len;
1028*4882a593Smuzhiyun 	char *srccode;
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	if (!map || !map->dso)
1031*4882a593Smuzhiyun 		return 0;
1032*4882a593Smuzhiyun 	srcfile = get_srcline_split(map->dso,
1033*4882a593Smuzhiyun 				    map__rip_2objdump(map, addr),
1034*4882a593Smuzhiyun 				    &line);
1035*4882a593Smuzhiyun 	if (!srcfile)
1036*4882a593Smuzhiyun 		return 0;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	/* Avoid redundant printing */
1039*4882a593Smuzhiyun 	if (state &&
1040*4882a593Smuzhiyun 	    state->srcfile &&
1041*4882a593Smuzhiyun 	    !strcmp(state->srcfile, srcfile) &&
1042*4882a593Smuzhiyun 	    state->line == line) {
1043*4882a593Smuzhiyun 		free(srcfile);
1044*4882a593Smuzhiyun 		return 0;
1045*4882a593Smuzhiyun 	}
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	srccode = find_sourceline(srcfile, line, &len);
1048*4882a593Smuzhiyun 	if (!srccode)
1049*4882a593Smuzhiyun 		goto out_free_line;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	if (state) {
1054*4882a593Smuzhiyun 		state->srcfile = srcfile;
1055*4882a593Smuzhiyun 		state->line = line;
1056*4882a593Smuzhiyun 	}
1057*4882a593Smuzhiyun 	return ret;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun out_free_line:
1060*4882a593Smuzhiyun 	free(srcfile);
1061*4882a593Smuzhiyun 	return ret;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun 
print_srccode(struct thread * thread,u8 cpumode,uint64_t addr)1064*4882a593Smuzhiyun static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun 	struct addr_location al;
1067*4882a593Smuzhiyun 	int ret = 0;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	memset(&al, 0, sizeof(al));
1070*4882a593Smuzhiyun 	thread__find_map(thread, cpumode, addr, &al);
1071*4882a593Smuzhiyun 	if (!al.map)
1072*4882a593Smuzhiyun 		return 0;
1073*4882a593Smuzhiyun 	ret = map__fprintf_srccode(al.map, al.addr, stdout,
1074*4882a593Smuzhiyun 		    &thread->srccode_state);
1075*4882a593Smuzhiyun 	if (ret)
1076*4882a593Smuzhiyun 		ret += printf("\n");
1077*4882a593Smuzhiyun 	return ret;
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun 
ip__fprintf_jump(uint64_t ip,struct branch_entry * en,struct perf_insn * x,u8 * inbuf,int len,int insn,FILE * fp,int * total_cycles)1080*4882a593Smuzhiyun static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
1081*4882a593Smuzhiyun 			    struct perf_insn *x, u8 *inbuf, int len,
1082*4882a593Smuzhiyun 			    int insn, FILE *fp, int *total_cycles)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
1085*4882a593Smuzhiyun 			      dump_insn(x, ip, inbuf, len, NULL),
1086*4882a593Smuzhiyun 			      en->flags.predicted ? " PRED" : "",
1087*4882a593Smuzhiyun 			      en->flags.mispred ? " MISPRED" : "",
1088*4882a593Smuzhiyun 			      en->flags.in_tx ? " INTX" : "",
1089*4882a593Smuzhiyun 			      en->flags.abort ? " ABORT" : "");
1090*4882a593Smuzhiyun 	if (en->flags.cycles) {
1091*4882a593Smuzhiyun 		*total_cycles += en->flags.cycles;
1092*4882a593Smuzhiyun 		printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
1093*4882a593Smuzhiyun 		if (insn)
1094*4882a593Smuzhiyun 			printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
1095*4882a593Smuzhiyun 	}
1096*4882a593Smuzhiyun 	return printed + fprintf(fp, "\n");
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun 
ip__fprintf_sym(uint64_t addr,struct thread * thread,u8 cpumode,int cpu,struct symbol ** lastsym,struct perf_event_attr * attr,FILE * fp)1099*4882a593Smuzhiyun static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1100*4882a593Smuzhiyun 			   u8 cpumode, int cpu, struct symbol **lastsym,
1101*4882a593Smuzhiyun 			   struct perf_event_attr *attr, FILE *fp)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun 	struct addr_location al;
1104*4882a593Smuzhiyun 	int off, printed = 0;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	memset(&al, 0, sizeof(al));
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	thread__find_map(thread, cpumode, addr, &al);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
1111*4882a593Smuzhiyun 		return 0;
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	al.cpu = cpu;
1114*4882a593Smuzhiyun 	al.sym = NULL;
1115*4882a593Smuzhiyun 	if (al.map)
1116*4882a593Smuzhiyun 		al.sym = map__find_symbol(al.map, al.addr);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	if (!al.sym)
1119*4882a593Smuzhiyun 		return 0;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	if (al.addr < al.sym->end)
1122*4882a593Smuzhiyun 		off = al.addr - al.sym->start;
1123*4882a593Smuzhiyun 	else
1124*4882a593Smuzhiyun 		off = al.addr - al.map->start - al.sym->start;
1125*4882a593Smuzhiyun 	printed += fprintf(fp, "\t%s", al.sym->name);
1126*4882a593Smuzhiyun 	if (off)
1127*4882a593Smuzhiyun 		printed += fprintf(fp, "%+d", off);
1128*4882a593Smuzhiyun 	printed += fprintf(fp, ":");
1129*4882a593Smuzhiyun 	if (PRINT_FIELD(SRCLINE))
1130*4882a593Smuzhiyun 		printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1131*4882a593Smuzhiyun 	printed += fprintf(fp, "\n");
1132*4882a593Smuzhiyun 	*lastsym = al.sym;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	return printed;
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun 
perf_sample__fprintf_brstackinsn(struct perf_sample * sample,struct thread * thread,struct perf_event_attr * attr,struct machine * machine,FILE * fp)1137*4882a593Smuzhiyun static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1138*4882a593Smuzhiyun 					    struct thread *thread,
1139*4882a593Smuzhiyun 					    struct perf_event_attr *attr,
1140*4882a593Smuzhiyun 					    struct machine *machine, FILE *fp)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun 	struct branch_stack *br = sample->branch_stack;
1143*4882a593Smuzhiyun 	struct branch_entry *entries = perf_sample__branch_entries(sample);
1144*4882a593Smuzhiyun 	u64 start, end;
1145*4882a593Smuzhiyun 	int i, insn, len, nr, ilen, printed = 0;
1146*4882a593Smuzhiyun 	struct perf_insn x;
1147*4882a593Smuzhiyun 	u8 buffer[MAXBB];
1148*4882a593Smuzhiyun 	unsigned off;
1149*4882a593Smuzhiyun 	struct symbol *lastsym = NULL;
1150*4882a593Smuzhiyun 	int total_cycles = 0;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	if (!(br && br->nr))
1153*4882a593Smuzhiyun 		return 0;
1154*4882a593Smuzhiyun 	nr = br->nr;
1155*4882a593Smuzhiyun 	if (max_blocks && nr > max_blocks + 1)
1156*4882a593Smuzhiyun 		nr = max_blocks + 1;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	x.thread = thread;
1159*4882a593Smuzhiyun 	x.cpu = sample->cpu;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	printed += fprintf(fp, "%c", '\n');
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	/* Handle first from jump, of which we don't know the entry. */
1164*4882a593Smuzhiyun 	len = grab_bb(buffer, entries[nr-1].from,
1165*4882a593Smuzhiyun 			entries[nr-1].from,
1166*4882a593Smuzhiyun 			machine, thread, &x.is64bit, &x.cpumode, false);
1167*4882a593Smuzhiyun 	if (len > 0) {
1168*4882a593Smuzhiyun 		printed += ip__fprintf_sym(entries[nr - 1].from, thread,
1169*4882a593Smuzhiyun 					   x.cpumode, x.cpu, &lastsym, attr, fp);
1170*4882a593Smuzhiyun 		printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
1171*4882a593Smuzhiyun 					    &x, buffer, len, 0, fp, &total_cycles);
1172*4882a593Smuzhiyun 		if (PRINT_FIELD(SRCCODE))
1173*4882a593Smuzhiyun 			printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
1174*4882a593Smuzhiyun 	}
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	/* Print all blocks */
1177*4882a593Smuzhiyun 	for (i = nr - 2; i >= 0; i--) {
1178*4882a593Smuzhiyun 		if (entries[i].from || entries[i].to)
1179*4882a593Smuzhiyun 			pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1180*4882a593Smuzhiyun 				 entries[i].from,
1181*4882a593Smuzhiyun 				 entries[i].to);
1182*4882a593Smuzhiyun 		start = entries[i + 1].to;
1183*4882a593Smuzhiyun 		end   = entries[i].from;
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 		len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1186*4882a593Smuzhiyun 		/* Patch up missing kernel transfers due to ring filters */
1187*4882a593Smuzhiyun 		if (len == -ENXIO && i > 0) {
1188*4882a593Smuzhiyun 			end = entries[--i].from;
1189*4882a593Smuzhiyun 			pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1190*4882a593Smuzhiyun 			len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1191*4882a593Smuzhiyun 		}
1192*4882a593Smuzhiyun 		if (len <= 0)
1193*4882a593Smuzhiyun 			continue;
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 		insn = 0;
1196*4882a593Smuzhiyun 		for (off = 0; off < (unsigned)len; off += ilen) {
1197*4882a593Smuzhiyun 			uint64_t ip = start + off;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 			printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1200*4882a593Smuzhiyun 			if (ip == end) {
1201*4882a593Smuzhiyun 				printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
1202*4882a593Smuzhiyun 							    &total_cycles);
1203*4882a593Smuzhiyun 				if (PRINT_FIELD(SRCCODE))
1204*4882a593Smuzhiyun 					printed += print_srccode(thread, x.cpumode, ip);
1205*4882a593Smuzhiyun 				break;
1206*4882a593Smuzhiyun 			} else {
1207*4882a593Smuzhiyun 				ilen = 0;
1208*4882a593Smuzhiyun 				printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1209*4882a593Smuzhiyun 						   dump_insn(&x, ip, buffer + off, len - off, &ilen));
1210*4882a593Smuzhiyun 				if (ilen == 0)
1211*4882a593Smuzhiyun 					break;
1212*4882a593Smuzhiyun 				if (PRINT_FIELD(SRCCODE))
1213*4882a593Smuzhiyun 					print_srccode(thread, x.cpumode, ip);
1214*4882a593Smuzhiyun 				insn++;
1215*4882a593Smuzhiyun 			}
1216*4882a593Smuzhiyun 		}
1217*4882a593Smuzhiyun 		if (off != end - start)
1218*4882a593Smuzhiyun 			printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
1219*4882a593Smuzhiyun 	}
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	/*
1222*4882a593Smuzhiyun 	 * Hit the branch? In this case we are already done, and the target
1223*4882a593Smuzhiyun 	 * has not been executed yet.
1224*4882a593Smuzhiyun 	 */
1225*4882a593Smuzhiyun 	if (entries[0].from == sample->ip)
1226*4882a593Smuzhiyun 		goto out;
1227*4882a593Smuzhiyun 	if (entries[0].flags.abort)
1228*4882a593Smuzhiyun 		goto out;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/*
1231*4882a593Smuzhiyun 	 * Print final block upto sample
1232*4882a593Smuzhiyun 	 *
1233*4882a593Smuzhiyun 	 * Due to pipeline delays the LBRs might be missing a branch
1234*4882a593Smuzhiyun 	 * or two, which can result in very large or negative blocks
1235*4882a593Smuzhiyun 	 * between final branch and sample. When this happens just
1236*4882a593Smuzhiyun 	 * continue walking after the last TO until we hit a branch.
1237*4882a593Smuzhiyun 	 */
1238*4882a593Smuzhiyun 	start = entries[0].to;
1239*4882a593Smuzhiyun 	end = sample->ip;
1240*4882a593Smuzhiyun 	if (end < start) {
1241*4882a593Smuzhiyun 		/* Missing jump. Scan 128 bytes for the next branch */
1242*4882a593Smuzhiyun 		end = start + 128;
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 	len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1245*4882a593Smuzhiyun 	printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1246*4882a593Smuzhiyun 	if (len <= 0) {
1247*4882a593Smuzhiyun 		/* Print at least last IP if basic block did not work */
1248*4882a593Smuzhiyun 		len = grab_bb(buffer, sample->ip, sample->ip,
1249*4882a593Smuzhiyun 			      machine, thread, &x.is64bit, &x.cpumode, false);
1250*4882a593Smuzhiyun 		if (len <= 0)
1251*4882a593Smuzhiyun 			goto out;
1252*4882a593Smuzhiyun 		printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
1253*4882a593Smuzhiyun 			dump_insn(&x, sample->ip, buffer, len, NULL));
1254*4882a593Smuzhiyun 		if (PRINT_FIELD(SRCCODE))
1255*4882a593Smuzhiyun 			print_srccode(thread, x.cpumode, sample->ip);
1256*4882a593Smuzhiyun 		goto out;
1257*4882a593Smuzhiyun 	}
1258*4882a593Smuzhiyun 	for (off = 0; off <= end - start; off += ilen) {
1259*4882a593Smuzhiyun 		ilen = 0;
1260*4882a593Smuzhiyun 		printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1261*4882a593Smuzhiyun 				   dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1262*4882a593Smuzhiyun 		if (ilen == 0)
1263*4882a593Smuzhiyun 			break;
1264*4882a593Smuzhiyun 		if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1265*4882a593Smuzhiyun 			/*
1266*4882a593Smuzhiyun 			 * Hit a missing branch. Just stop.
1267*4882a593Smuzhiyun 			 */
1268*4882a593Smuzhiyun 			printed += fprintf(fp, "\t... not reaching sample ...\n");
1269*4882a593Smuzhiyun 			break;
1270*4882a593Smuzhiyun 		}
1271*4882a593Smuzhiyun 		if (PRINT_FIELD(SRCCODE))
1272*4882a593Smuzhiyun 			print_srccode(thread, x.cpumode, start + off);
1273*4882a593Smuzhiyun 	}
1274*4882a593Smuzhiyun out:
1275*4882a593Smuzhiyun 	return printed;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
perf_sample__fprintf_addr(struct perf_sample * sample,struct thread * thread,struct perf_event_attr * attr,FILE * fp)1278*4882a593Smuzhiyun static int perf_sample__fprintf_addr(struct perf_sample *sample,
1279*4882a593Smuzhiyun 				     struct thread *thread,
1280*4882a593Smuzhiyun 				     struct perf_event_attr *attr, FILE *fp)
1281*4882a593Smuzhiyun {
1282*4882a593Smuzhiyun 	struct addr_location al;
1283*4882a593Smuzhiyun 	int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	if (!sample_addr_correlates_sym(attr))
1286*4882a593Smuzhiyun 		goto out;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	thread__resolve(thread, &al, sample);
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	if (PRINT_FIELD(SYM)) {
1291*4882a593Smuzhiyun 		printed += fprintf(fp, " ");
1292*4882a593Smuzhiyun 		if (PRINT_FIELD(SYMOFFSET))
1293*4882a593Smuzhiyun 			printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1294*4882a593Smuzhiyun 		else
1295*4882a593Smuzhiyun 			printed += symbol__fprintf_symname(al.sym, fp);
1296*4882a593Smuzhiyun 	}
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	if (PRINT_FIELD(DSO)) {
1299*4882a593Smuzhiyun 		printed += fprintf(fp, " (");
1300*4882a593Smuzhiyun 		printed += map__fprintf_dsoname(al.map, fp);
1301*4882a593Smuzhiyun 		printed += fprintf(fp, ")");
1302*4882a593Smuzhiyun 	}
1303*4882a593Smuzhiyun out:
1304*4882a593Smuzhiyun 	return printed;
1305*4882a593Smuzhiyun }
1306*4882a593Smuzhiyun 
resolve_branch_sym(struct perf_sample * sample,struct evsel * evsel,struct thread * thread,struct addr_location * al,u64 * ip)1307*4882a593Smuzhiyun static const char *resolve_branch_sym(struct perf_sample *sample,
1308*4882a593Smuzhiyun 				      struct evsel *evsel,
1309*4882a593Smuzhiyun 				      struct thread *thread,
1310*4882a593Smuzhiyun 				      struct addr_location *al,
1311*4882a593Smuzhiyun 				      u64 *ip)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	struct addr_location addr_al;
1314*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
1315*4882a593Smuzhiyun 	const char *name = NULL;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1318*4882a593Smuzhiyun 		if (sample_addr_correlates_sym(attr)) {
1319*4882a593Smuzhiyun 			thread__resolve(thread, &addr_al, sample);
1320*4882a593Smuzhiyun 			if (addr_al.sym)
1321*4882a593Smuzhiyun 				name = addr_al.sym->name;
1322*4882a593Smuzhiyun 			else
1323*4882a593Smuzhiyun 				*ip = sample->addr;
1324*4882a593Smuzhiyun 		} else {
1325*4882a593Smuzhiyun 			*ip = sample->addr;
1326*4882a593Smuzhiyun 		}
1327*4882a593Smuzhiyun 	} else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1328*4882a593Smuzhiyun 		if (al->sym)
1329*4882a593Smuzhiyun 			name = al->sym->name;
1330*4882a593Smuzhiyun 		else
1331*4882a593Smuzhiyun 			*ip = sample->ip;
1332*4882a593Smuzhiyun 	}
1333*4882a593Smuzhiyun 	return name;
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun 
perf_sample__fprintf_callindent(struct perf_sample * sample,struct evsel * evsel,struct thread * thread,struct addr_location * al,FILE * fp)1336*4882a593Smuzhiyun static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1337*4882a593Smuzhiyun 					   struct evsel *evsel,
1338*4882a593Smuzhiyun 					   struct thread *thread,
1339*4882a593Smuzhiyun 					   struct addr_location *al, FILE *fp)
1340*4882a593Smuzhiyun {
1341*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
1342*4882a593Smuzhiyun 	size_t depth = thread_stack__depth(thread, sample->cpu);
1343*4882a593Smuzhiyun 	const char *name = NULL;
1344*4882a593Smuzhiyun 	static int spacing;
1345*4882a593Smuzhiyun 	int len = 0;
1346*4882a593Smuzhiyun 	int dlen = 0;
1347*4882a593Smuzhiyun 	u64 ip = 0;
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	/*
1350*4882a593Smuzhiyun 	 * The 'return' has already been popped off the stack so the depth has
1351*4882a593Smuzhiyun 	 * to be adjusted to match the 'call'.
1352*4882a593Smuzhiyun 	 */
1353*4882a593Smuzhiyun 	if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1354*4882a593Smuzhiyun 		depth += 1;
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	name = resolve_branch_sym(sample, evsel, thread, al, &ip);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1359*4882a593Smuzhiyun 		dlen += fprintf(fp, "(");
1360*4882a593Smuzhiyun 		dlen += map__fprintf_dsoname(al->map, fp);
1361*4882a593Smuzhiyun 		dlen += fprintf(fp, ")\t");
1362*4882a593Smuzhiyun 	}
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	if (name)
1365*4882a593Smuzhiyun 		len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1366*4882a593Smuzhiyun 	else if (ip)
1367*4882a593Smuzhiyun 		len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	if (len < 0)
1370*4882a593Smuzhiyun 		return len;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	/*
1373*4882a593Smuzhiyun 	 * Try to keep the output length from changing frequently so that the
1374*4882a593Smuzhiyun 	 * output lines up more nicely.
1375*4882a593Smuzhiyun 	 */
1376*4882a593Smuzhiyun 	if (len > spacing || (len && len < spacing - 52))
1377*4882a593Smuzhiyun 		spacing = round_up(len + 4, 32);
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	if (len < spacing)
1380*4882a593Smuzhiyun 		len += fprintf(fp, "%*s", spacing - len, "");
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	return len + dlen;
1383*4882a593Smuzhiyun }
1384*4882a593Smuzhiyun 
arch_fetch_insn(struct perf_sample * sample __maybe_unused,struct thread * thread __maybe_unused,struct machine * machine __maybe_unused)1385*4882a593Smuzhiyun __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1386*4882a593Smuzhiyun 			    struct thread *thread __maybe_unused,
1387*4882a593Smuzhiyun 			    struct machine *machine __maybe_unused)
1388*4882a593Smuzhiyun {
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun 
perf_sample__fprintf_insn(struct perf_sample * sample,struct perf_event_attr * attr,struct thread * thread,struct machine * machine,FILE * fp)1391*4882a593Smuzhiyun static int perf_sample__fprintf_insn(struct perf_sample *sample,
1392*4882a593Smuzhiyun 				     struct perf_event_attr *attr,
1393*4882a593Smuzhiyun 				     struct thread *thread,
1394*4882a593Smuzhiyun 				     struct machine *machine, FILE *fp)
1395*4882a593Smuzhiyun {
1396*4882a593Smuzhiyun 	int printed = 0;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	if (sample->insn_len == 0 && native_arch)
1399*4882a593Smuzhiyun 		arch_fetch_insn(sample, thread, machine);
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 	if (PRINT_FIELD(INSNLEN))
1402*4882a593Smuzhiyun 		printed += fprintf(fp, " ilen: %d", sample->insn_len);
1403*4882a593Smuzhiyun 	if (PRINT_FIELD(INSN) && sample->insn_len) {
1404*4882a593Smuzhiyun 		int i;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 		printed += fprintf(fp, " insn:");
1407*4882a593Smuzhiyun 		for (i = 0; i < sample->insn_len; i++)
1408*4882a593Smuzhiyun 			printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1409*4882a593Smuzhiyun 	}
1410*4882a593Smuzhiyun 	if (PRINT_FIELD(BRSTACKINSN))
1411*4882a593Smuzhiyun 		printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	return printed;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
perf_sample__fprintf_ipc(struct perf_sample * sample,struct perf_event_attr * attr,FILE * fp)1416*4882a593Smuzhiyun static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1417*4882a593Smuzhiyun 				    struct perf_event_attr *attr, FILE *fp)
1418*4882a593Smuzhiyun {
1419*4882a593Smuzhiyun 	unsigned int ipc;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1422*4882a593Smuzhiyun 		return 0;
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 	ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 	return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1427*4882a593Smuzhiyun 		       ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun 
perf_sample__fprintf_bts(struct perf_sample * sample,struct evsel * evsel,struct thread * thread,struct addr_location * al,struct machine * machine,FILE * fp)1430*4882a593Smuzhiyun static int perf_sample__fprintf_bts(struct perf_sample *sample,
1431*4882a593Smuzhiyun 				    struct evsel *evsel,
1432*4882a593Smuzhiyun 				    struct thread *thread,
1433*4882a593Smuzhiyun 				    struct addr_location *al,
1434*4882a593Smuzhiyun 				    struct machine *machine, FILE *fp)
1435*4882a593Smuzhiyun {
1436*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
1437*4882a593Smuzhiyun 	unsigned int type = output_type(attr->type);
1438*4882a593Smuzhiyun 	bool print_srcline_last = false;
1439*4882a593Smuzhiyun 	int printed = 0;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	if (PRINT_FIELD(CALLINDENT))
1442*4882a593Smuzhiyun 		printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	/* print branch_from information */
1445*4882a593Smuzhiyun 	if (PRINT_FIELD(IP)) {
1446*4882a593Smuzhiyun 		unsigned int print_opts = output[type].print_ip_opts;
1447*4882a593Smuzhiyun 		struct callchain_cursor *cursor = NULL;
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 		if (symbol_conf.use_callchain && sample->callchain &&
1450*4882a593Smuzhiyun 		    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1451*4882a593Smuzhiyun 					      sample, NULL, NULL, scripting_max_stack) == 0)
1452*4882a593Smuzhiyun 			cursor = &callchain_cursor;
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 		if (cursor == NULL) {
1455*4882a593Smuzhiyun 			printed += fprintf(fp, " ");
1456*4882a593Smuzhiyun 			if (print_opts & EVSEL__PRINT_SRCLINE) {
1457*4882a593Smuzhiyun 				print_srcline_last = true;
1458*4882a593Smuzhiyun 				print_opts &= ~EVSEL__PRINT_SRCLINE;
1459*4882a593Smuzhiyun 			}
1460*4882a593Smuzhiyun 		} else
1461*4882a593Smuzhiyun 			printed += fprintf(fp, "\n");
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 		printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1464*4882a593Smuzhiyun 					       symbol_conf.bt_stop_list, fp);
1465*4882a593Smuzhiyun 	}
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	/* print branch_to information */
1468*4882a593Smuzhiyun 	if (PRINT_FIELD(ADDR) ||
1469*4882a593Smuzhiyun 	    ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
1470*4882a593Smuzhiyun 	     !output[type].user_set)) {
1471*4882a593Smuzhiyun 		printed += fprintf(fp, " => ");
1472*4882a593Smuzhiyun 		printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1473*4882a593Smuzhiyun 	}
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	printed += perf_sample__fprintf_ipc(sample, attr, fp);
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	if (print_srcline_last)
1478*4882a593Smuzhiyun 		printed += map__fprintf_srcline(al->map, al->addr, "\n  ", fp);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1481*4882a593Smuzhiyun 	printed += fprintf(fp, "\n");
1482*4882a593Smuzhiyun 	if (PRINT_FIELD(SRCCODE)) {
1483*4882a593Smuzhiyun 		int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1484*4882a593Smuzhiyun 					 &thread->srccode_state);
1485*4882a593Smuzhiyun 		if (ret) {
1486*4882a593Smuzhiyun 			printed += ret;
1487*4882a593Smuzhiyun 			printed += printf("\n");
1488*4882a593Smuzhiyun 		}
1489*4882a593Smuzhiyun 	}
1490*4882a593Smuzhiyun 	return printed;
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun static struct {
1494*4882a593Smuzhiyun 	u32 flags;
1495*4882a593Smuzhiyun 	const char *name;
1496*4882a593Smuzhiyun } sample_flags[] = {
1497*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1498*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1499*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1500*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH, "jmp"},
1501*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1502*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1503*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1504*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1505*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1506*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |	PERF_IP_FLAG_INTERRUPT, "hw int"},
1507*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1508*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1509*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1510*4882a593Smuzhiyun 	{0, NULL}
1511*4882a593Smuzhiyun };
1512*4882a593Smuzhiyun 
sample_flags_to_name(u32 flags)1513*4882a593Smuzhiyun static const char *sample_flags_to_name(u32 flags)
1514*4882a593Smuzhiyun {
1515*4882a593Smuzhiyun 	int i;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	for (i = 0; sample_flags[i].name ; i++) {
1518*4882a593Smuzhiyun 		if (sample_flags[i].flags == flags)
1519*4882a593Smuzhiyun 			return sample_flags[i].name;
1520*4882a593Smuzhiyun 	}
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	return NULL;
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun 
perf_sample__fprintf_flags(u32 flags,FILE * fp)1525*4882a593Smuzhiyun static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun 	const char *chars = PERF_IP_FLAG_CHARS;
1528*4882a593Smuzhiyun 	const int n = strlen(PERF_IP_FLAG_CHARS);
1529*4882a593Smuzhiyun 	bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1530*4882a593Smuzhiyun 	const char *name = NULL;
1531*4882a593Smuzhiyun 	char str[33];
1532*4882a593Smuzhiyun 	int i, pos = 0;
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1535*4882a593Smuzhiyun 	if (name)
1536*4882a593Smuzhiyun 		return fprintf(fp, "  %-15s%4s ", name, in_tx ? "(x)" : "");
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1539*4882a593Smuzhiyun 		name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1540*4882a593Smuzhiyun 		if (name)
1541*4882a593Smuzhiyun 			return fprintf(fp, "  tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1542*4882a593Smuzhiyun 	}
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	if (flags & PERF_IP_FLAG_TRACE_END) {
1545*4882a593Smuzhiyun 		name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1546*4882a593Smuzhiyun 		if (name)
1547*4882a593Smuzhiyun 			return fprintf(fp, "  tr end  %-7s%4s ", name, in_tx ? "(x)" : "");
1548*4882a593Smuzhiyun 	}
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 	for (i = 0; i < n; i++, flags >>= 1) {
1551*4882a593Smuzhiyun 		if (flags & 1)
1552*4882a593Smuzhiyun 			str[pos++] = chars[i];
1553*4882a593Smuzhiyun 	}
1554*4882a593Smuzhiyun 	for (; i < 32; i++, flags >>= 1) {
1555*4882a593Smuzhiyun 		if (flags & 1)
1556*4882a593Smuzhiyun 			str[pos++] = '?';
1557*4882a593Smuzhiyun 	}
1558*4882a593Smuzhiyun 	str[pos] = 0;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	return fprintf(fp, "  %-19s ", str);
1561*4882a593Smuzhiyun }
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun struct printer_data {
1564*4882a593Smuzhiyun 	int line_no;
1565*4882a593Smuzhiyun 	bool hit_nul;
1566*4882a593Smuzhiyun 	bool is_printable;
1567*4882a593Smuzhiyun };
1568*4882a593Smuzhiyun 
sample__fprintf_bpf_output(enum binary_printer_ops op,unsigned int val,void * extra,FILE * fp)1569*4882a593Smuzhiyun static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1570*4882a593Smuzhiyun 				      unsigned int val,
1571*4882a593Smuzhiyun 				      void *extra, FILE *fp)
1572*4882a593Smuzhiyun {
1573*4882a593Smuzhiyun 	unsigned char ch = (unsigned char)val;
1574*4882a593Smuzhiyun 	struct printer_data *printer_data = extra;
1575*4882a593Smuzhiyun 	int printed = 0;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	switch (op) {
1578*4882a593Smuzhiyun 	case BINARY_PRINT_DATA_BEGIN:
1579*4882a593Smuzhiyun 		printed += fprintf(fp, "\n");
1580*4882a593Smuzhiyun 		break;
1581*4882a593Smuzhiyun 	case BINARY_PRINT_LINE_BEGIN:
1582*4882a593Smuzhiyun 		printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1583*4882a593Smuzhiyun 						        "           ");
1584*4882a593Smuzhiyun 		break;
1585*4882a593Smuzhiyun 	case BINARY_PRINT_ADDR:
1586*4882a593Smuzhiyun 		printed += fprintf(fp, " %04x:", val);
1587*4882a593Smuzhiyun 		break;
1588*4882a593Smuzhiyun 	case BINARY_PRINT_NUM_DATA:
1589*4882a593Smuzhiyun 		printed += fprintf(fp, " %02x", val);
1590*4882a593Smuzhiyun 		break;
1591*4882a593Smuzhiyun 	case BINARY_PRINT_NUM_PAD:
1592*4882a593Smuzhiyun 		printed += fprintf(fp, "   ");
1593*4882a593Smuzhiyun 		break;
1594*4882a593Smuzhiyun 	case BINARY_PRINT_SEP:
1595*4882a593Smuzhiyun 		printed += fprintf(fp, "  ");
1596*4882a593Smuzhiyun 		break;
1597*4882a593Smuzhiyun 	case BINARY_PRINT_CHAR_DATA:
1598*4882a593Smuzhiyun 		if (printer_data->hit_nul && ch)
1599*4882a593Smuzhiyun 			printer_data->is_printable = false;
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 		if (!isprint(ch)) {
1602*4882a593Smuzhiyun 			printed += fprintf(fp, "%c", '.');
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 			if (!printer_data->is_printable)
1605*4882a593Smuzhiyun 				break;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 			if (ch == '\0')
1608*4882a593Smuzhiyun 				printer_data->hit_nul = true;
1609*4882a593Smuzhiyun 			else
1610*4882a593Smuzhiyun 				printer_data->is_printable = false;
1611*4882a593Smuzhiyun 		} else {
1612*4882a593Smuzhiyun 			printed += fprintf(fp, "%c", ch);
1613*4882a593Smuzhiyun 		}
1614*4882a593Smuzhiyun 		break;
1615*4882a593Smuzhiyun 	case BINARY_PRINT_CHAR_PAD:
1616*4882a593Smuzhiyun 		printed += fprintf(fp, " ");
1617*4882a593Smuzhiyun 		break;
1618*4882a593Smuzhiyun 	case BINARY_PRINT_LINE_END:
1619*4882a593Smuzhiyun 		printed += fprintf(fp, "\n");
1620*4882a593Smuzhiyun 		printer_data->line_no++;
1621*4882a593Smuzhiyun 		break;
1622*4882a593Smuzhiyun 	case BINARY_PRINT_DATA_END:
1623*4882a593Smuzhiyun 	default:
1624*4882a593Smuzhiyun 		break;
1625*4882a593Smuzhiyun 	}
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	return printed;
1628*4882a593Smuzhiyun }
1629*4882a593Smuzhiyun 
perf_sample__fprintf_bpf_output(struct perf_sample * sample,FILE * fp)1630*4882a593Smuzhiyun static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1631*4882a593Smuzhiyun {
1632*4882a593Smuzhiyun 	unsigned int nr_bytes = sample->raw_size;
1633*4882a593Smuzhiyun 	struct printer_data printer_data = {0, false, true};
1634*4882a593Smuzhiyun 	int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1635*4882a593Smuzhiyun 				      sample__fprintf_bpf_output, &printer_data, fp);
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun 	if (printer_data.is_printable && printer_data.hit_nul)
1638*4882a593Smuzhiyun 		printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	return printed;
1641*4882a593Smuzhiyun }
1642*4882a593Smuzhiyun 
perf_sample__fprintf_spacing(int len,int spacing,FILE * fp)1643*4882a593Smuzhiyun static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1644*4882a593Smuzhiyun {
1645*4882a593Smuzhiyun 	if (len > 0 && len < spacing)
1646*4882a593Smuzhiyun 		return fprintf(fp, "%*s", spacing - len, "");
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	return 0;
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun 
perf_sample__fprintf_pt_spacing(int len,FILE * fp)1651*4882a593Smuzhiyun static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun 	return perf_sample__fprintf_spacing(len, 34, fp);
1654*4882a593Smuzhiyun }
1655*4882a593Smuzhiyun 
perf_sample__fprintf_synth_ptwrite(struct perf_sample * sample,FILE * fp)1656*4882a593Smuzhiyun static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun 	struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1659*4882a593Smuzhiyun 	int len;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	if (perf_sample__bad_synth_size(sample, *data))
1662*4882a593Smuzhiyun 		return 0;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1665*4882a593Smuzhiyun 		     data->ip, le64_to_cpu(data->payload));
1666*4882a593Smuzhiyun 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1667*4882a593Smuzhiyun }
1668*4882a593Smuzhiyun 
perf_sample__fprintf_synth_mwait(struct perf_sample * sample,FILE * fp)1669*4882a593Smuzhiyun static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1670*4882a593Smuzhiyun {
1671*4882a593Smuzhiyun 	struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1672*4882a593Smuzhiyun 	int len;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	if (perf_sample__bad_synth_size(sample, *data))
1675*4882a593Smuzhiyun 		return 0;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	len = fprintf(fp, " hints: %#x extensions: %#x ",
1678*4882a593Smuzhiyun 		      data->hints, data->extensions);
1679*4882a593Smuzhiyun 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun 
perf_sample__fprintf_synth_pwre(struct perf_sample * sample,FILE * fp)1682*4882a593Smuzhiyun static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1683*4882a593Smuzhiyun {
1684*4882a593Smuzhiyun 	struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1685*4882a593Smuzhiyun 	int len;
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	if (perf_sample__bad_synth_size(sample, *data))
1688*4882a593Smuzhiyun 		return 0;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1691*4882a593Smuzhiyun 		      data->hw, data->cstate, data->subcstate);
1692*4882a593Smuzhiyun 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1693*4882a593Smuzhiyun }
1694*4882a593Smuzhiyun 
perf_sample__fprintf_synth_exstop(struct perf_sample * sample,FILE * fp)1695*4882a593Smuzhiyun static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1696*4882a593Smuzhiyun {
1697*4882a593Smuzhiyun 	struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1698*4882a593Smuzhiyun 	int len;
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	if (perf_sample__bad_synth_size(sample, *data))
1701*4882a593Smuzhiyun 		return 0;
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	len = fprintf(fp, " IP: %u ", data->ip);
1704*4882a593Smuzhiyun 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1705*4882a593Smuzhiyun }
1706*4882a593Smuzhiyun 
perf_sample__fprintf_synth_pwrx(struct perf_sample * sample,FILE * fp)1707*4882a593Smuzhiyun static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1708*4882a593Smuzhiyun {
1709*4882a593Smuzhiyun 	struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1710*4882a593Smuzhiyun 	int len;
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	if (perf_sample__bad_synth_size(sample, *data))
1713*4882a593Smuzhiyun 		return 0;
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 	len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1716*4882a593Smuzhiyun 		     data->deepest_cstate, data->last_cstate,
1717*4882a593Smuzhiyun 		     data->wake_reason);
1718*4882a593Smuzhiyun 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1719*4882a593Smuzhiyun }
1720*4882a593Smuzhiyun 
perf_sample__fprintf_synth_cbr(struct perf_sample * sample,FILE * fp)1721*4882a593Smuzhiyun static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1722*4882a593Smuzhiyun {
1723*4882a593Smuzhiyun 	struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1724*4882a593Smuzhiyun 	unsigned int percent, freq;
1725*4882a593Smuzhiyun 	int len;
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun 	if (perf_sample__bad_synth_size(sample, *data))
1728*4882a593Smuzhiyun 		return 0;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	freq = (le32_to_cpu(data->freq) + 500) / 1000;
1731*4882a593Smuzhiyun 	len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1732*4882a593Smuzhiyun 	if (data->max_nonturbo) {
1733*4882a593Smuzhiyun 		percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1734*4882a593Smuzhiyun 		len += fprintf(fp, "(%3u%%) ", percent);
1735*4882a593Smuzhiyun 	}
1736*4882a593Smuzhiyun 	return len + perf_sample__fprintf_pt_spacing(len, fp);
1737*4882a593Smuzhiyun }
1738*4882a593Smuzhiyun 
perf_sample__fprintf_synth(struct perf_sample * sample,struct evsel * evsel,FILE * fp)1739*4882a593Smuzhiyun static int perf_sample__fprintf_synth(struct perf_sample *sample,
1740*4882a593Smuzhiyun 				      struct evsel *evsel, FILE *fp)
1741*4882a593Smuzhiyun {
1742*4882a593Smuzhiyun 	switch (evsel->core.attr.config) {
1743*4882a593Smuzhiyun 	case PERF_SYNTH_INTEL_PTWRITE:
1744*4882a593Smuzhiyun 		return perf_sample__fprintf_synth_ptwrite(sample, fp);
1745*4882a593Smuzhiyun 	case PERF_SYNTH_INTEL_MWAIT:
1746*4882a593Smuzhiyun 		return perf_sample__fprintf_synth_mwait(sample, fp);
1747*4882a593Smuzhiyun 	case PERF_SYNTH_INTEL_PWRE:
1748*4882a593Smuzhiyun 		return perf_sample__fprintf_synth_pwre(sample, fp);
1749*4882a593Smuzhiyun 	case PERF_SYNTH_INTEL_EXSTOP:
1750*4882a593Smuzhiyun 		return perf_sample__fprintf_synth_exstop(sample, fp);
1751*4882a593Smuzhiyun 	case PERF_SYNTH_INTEL_PWRX:
1752*4882a593Smuzhiyun 		return perf_sample__fprintf_synth_pwrx(sample, fp);
1753*4882a593Smuzhiyun 	case PERF_SYNTH_INTEL_CBR:
1754*4882a593Smuzhiyun 		return perf_sample__fprintf_synth_cbr(sample, fp);
1755*4882a593Smuzhiyun 	default:
1756*4882a593Smuzhiyun 		break;
1757*4882a593Smuzhiyun 	}
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	return 0;
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun 
evlist__max_name_len(struct evlist * evlist)1762*4882a593Smuzhiyun static int evlist__max_name_len(struct evlist *evlist)
1763*4882a593Smuzhiyun {
1764*4882a593Smuzhiyun 	struct evsel *evsel;
1765*4882a593Smuzhiyun 	int max = 0;
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, evsel) {
1768*4882a593Smuzhiyun 		int len = strlen(evsel__name(evsel));
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 		max = MAX(len, max);
1771*4882a593Smuzhiyun 	}
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun 	return max;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun 
data_src__fprintf(u64 data_src,FILE * fp)1776*4882a593Smuzhiyun static int data_src__fprintf(u64 data_src, FILE *fp)
1777*4882a593Smuzhiyun {
1778*4882a593Smuzhiyun 	struct mem_info mi = { .data_src.val = data_src };
1779*4882a593Smuzhiyun 	char decode[100];
1780*4882a593Smuzhiyun 	char out[100];
1781*4882a593Smuzhiyun 	static int maxlen;
1782*4882a593Smuzhiyun 	int len;
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	perf_script__meminfo_scnprintf(decode, 100, &mi);
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1787*4882a593Smuzhiyun 	if (maxlen < len)
1788*4882a593Smuzhiyun 		maxlen = len;
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	return fprintf(fp, "%-*s", maxlen, out);
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun struct metric_ctx {
1794*4882a593Smuzhiyun 	struct perf_sample	*sample;
1795*4882a593Smuzhiyun 	struct thread		*thread;
1796*4882a593Smuzhiyun 	struct evsel	*evsel;
1797*4882a593Smuzhiyun 	FILE 			*fp;
1798*4882a593Smuzhiyun };
1799*4882a593Smuzhiyun 
script_print_metric(struct perf_stat_config * config __maybe_unused,void * ctx,const char * color,const char * fmt,const char * unit,double val)1800*4882a593Smuzhiyun static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1801*4882a593Smuzhiyun 				void *ctx, const char *color,
1802*4882a593Smuzhiyun 			        const char *fmt,
1803*4882a593Smuzhiyun 			        const char *unit, double val)
1804*4882a593Smuzhiyun {
1805*4882a593Smuzhiyun 	struct metric_ctx *mctx = ctx;
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun 	if (!fmt)
1808*4882a593Smuzhiyun 		return;
1809*4882a593Smuzhiyun 	perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
1810*4882a593Smuzhiyun 				   PERF_RECORD_SAMPLE, mctx->fp);
1811*4882a593Smuzhiyun 	fputs("\tmetric: ", mctx->fp);
1812*4882a593Smuzhiyun 	if (color)
1813*4882a593Smuzhiyun 		color_fprintf(mctx->fp, color, fmt, val);
1814*4882a593Smuzhiyun 	else
1815*4882a593Smuzhiyun 		printf(fmt, val);
1816*4882a593Smuzhiyun 	fprintf(mctx->fp, " %s\n", unit);
1817*4882a593Smuzhiyun }
1818*4882a593Smuzhiyun 
script_new_line(struct perf_stat_config * config __maybe_unused,void * ctx)1819*4882a593Smuzhiyun static void script_new_line(struct perf_stat_config *config __maybe_unused,
1820*4882a593Smuzhiyun 			    void *ctx)
1821*4882a593Smuzhiyun {
1822*4882a593Smuzhiyun 	struct metric_ctx *mctx = ctx;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
1825*4882a593Smuzhiyun 				   PERF_RECORD_SAMPLE, mctx->fp);
1826*4882a593Smuzhiyun 	fputs("\tmetric: ", mctx->fp);
1827*4882a593Smuzhiyun }
1828*4882a593Smuzhiyun 
perf_sample__fprint_metric(struct perf_script * script,struct thread * thread,struct evsel * evsel,struct perf_sample * sample,FILE * fp)1829*4882a593Smuzhiyun static void perf_sample__fprint_metric(struct perf_script *script,
1830*4882a593Smuzhiyun 				       struct thread *thread,
1831*4882a593Smuzhiyun 				       struct evsel *evsel,
1832*4882a593Smuzhiyun 				       struct perf_sample *sample,
1833*4882a593Smuzhiyun 				       FILE *fp)
1834*4882a593Smuzhiyun {
1835*4882a593Smuzhiyun 	struct perf_stat_output_ctx ctx = {
1836*4882a593Smuzhiyun 		.print_metric = script_print_metric,
1837*4882a593Smuzhiyun 		.new_line = script_new_line,
1838*4882a593Smuzhiyun 		.ctx = &(struct metric_ctx) {
1839*4882a593Smuzhiyun 				.sample = sample,
1840*4882a593Smuzhiyun 				.thread = thread,
1841*4882a593Smuzhiyun 				.evsel  = evsel,
1842*4882a593Smuzhiyun 				.fp     = fp,
1843*4882a593Smuzhiyun 			 },
1844*4882a593Smuzhiyun 		.force_header = false,
1845*4882a593Smuzhiyun 	};
1846*4882a593Smuzhiyun 	struct evsel *ev2;
1847*4882a593Smuzhiyun 	u64 val;
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	if (!evsel->stats)
1850*4882a593Smuzhiyun 		perf_evlist__alloc_stats(script->session->evlist, false);
1851*4882a593Smuzhiyun 	if (evsel_script(evsel->leader)->gnum++ == 0)
1852*4882a593Smuzhiyun 		perf_stat__reset_shadow_stats();
1853*4882a593Smuzhiyun 	val = sample->period * evsel->scale;
1854*4882a593Smuzhiyun 	perf_stat__update_shadow_stats(evsel,
1855*4882a593Smuzhiyun 				       val,
1856*4882a593Smuzhiyun 				       sample->cpu,
1857*4882a593Smuzhiyun 				       &rt_stat);
1858*4882a593Smuzhiyun 	evsel_script(evsel)->val = val;
1859*4882a593Smuzhiyun 	if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
1860*4882a593Smuzhiyun 		for_each_group_member (ev2, evsel->leader) {
1861*4882a593Smuzhiyun 			perf_stat__print_shadow_stats(&stat_config, ev2,
1862*4882a593Smuzhiyun 						      evsel_script(ev2)->val,
1863*4882a593Smuzhiyun 						      sample->cpu,
1864*4882a593Smuzhiyun 						      &ctx,
1865*4882a593Smuzhiyun 						      NULL,
1866*4882a593Smuzhiyun 						      &rt_stat);
1867*4882a593Smuzhiyun 		}
1868*4882a593Smuzhiyun 		evsel_script(evsel->leader)->gnum = 0;
1869*4882a593Smuzhiyun 	}
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun 
show_event(struct perf_sample * sample,struct evsel * evsel,struct thread * thread,struct addr_location * al)1872*4882a593Smuzhiyun static bool show_event(struct perf_sample *sample,
1873*4882a593Smuzhiyun 		       struct evsel *evsel,
1874*4882a593Smuzhiyun 		       struct thread *thread,
1875*4882a593Smuzhiyun 		       struct addr_location *al)
1876*4882a593Smuzhiyun {
1877*4882a593Smuzhiyun 	int depth = thread_stack__depth(thread, sample->cpu);
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun 	if (!symbol_conf.graph_function)
1880*4882a593Smuzhiyun 		return true;
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	if (thread->filter) {
1883*4882a593Smuzhiyun 		if (depth <= thread->filter_entry_depth) {
1884*4882a593Smuzhiyun 			thread->filter = false;
1885*4882a593Smuzhiyun 			return false;
1886*4882a593Smuzhiyun 		}
1887*4882a593Smuzhiyun 		return true;
1888*4882a593Smuzhiyun 	} else {
1889*4882a593Smuzhiyun 		const char *s = symbol_conf.graph_function;
1890*4882a593Smuzhiyun 		u64 ip;
1891*4882a593Smuzhiyun 		const char *name = resolve_branch_sym(sample, evsel, thread, al,
1892*4882a593Smuzhiyun 				&ip);
1893*4882a593Smuzhiyun 		unsigned nlen;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 		if (!name)
1896*4882a593Smuzhiyun 			return false;
1897*4882a593Smuzhiyun 		nlen = strlen(name);
1898*4882a593Smuzhiyun 		while (*s) {
1899*4882a593Smuzhiyun 			unsigned len = strcspn(s, ",");
1900*4882a593Smuzhiyun 			if (nlen == len && !strncmp(name, s, len)) {
1901*4882a593Smuzhiyun 				thread->filter = true;
1902*4882a593Smuzhiyun 				thread->filter_entry_depth = depth;
1903*4882a593Smuzhiyun 				return true;
1904*4882a593Smuzhiyun 			}
1905*4882a593Smuzhiyun 			s += len;
1906*4882a593Smuzhiyun 			if (*s == ',')
1907*4882a593Smuzhiyun 				s++;
1908*4882a593Smuzhiyun 		}
1909*4882a593Smuzhiyun 		return false;
1910*4882a593Smuzhiyun 	}
1911*4882a593Smuzhiyun }
1912*4882a593Smuzhiyun 
process_event(struct perf_script * script,struct perf_sample * sample,struct evsel * evsel,struct addr_location * al,struct machine * machine)1913*4882a593Smuzhiyun static void process_event(struct perf_script *script,
1914*4882a593Smuzhiyun 			  struct perf_sample *sample, struct evsel *evsel,
1915*4882a593Smuzhiyun 			  struct addr_location *al,
1916*4882a593Smuzhiyun 			  struct machine *machine)
1917*4882a593Smuzhiyun {
1918*4882a593Smuzhiyun 	struct thread *thread = al->thread;
1919*4882a593Smuzhiyun 	struct perf_event_attr *attr = &evsel->core.attr;
1920*4882a593Smuzhiyun 	unsigned int type = output_type(attr->type);
1921*4882a593Smuzhiyun 	struct evsel_script *es = evsel->priv;
1922*4882a593Smuzhiyun 	FILE *fp = es->fp;
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun 	if (output[type].fields == 0)
1925*4882a593Smuzhiyun 		return;
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	if (!show_event(sample, evsel, thread, al))
1928*4882a593Smuzhiyun 		return;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	if (evswitch__discard(&script->evswitch, evsel))
1931*4882a593Smuzhiyun 		return;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	++es->samples;
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 	perf_sample__fprintf_start(script, sample, thread, evsel,
1936*4882a593Smuzhiyun 				   PERF_RECORD_SAMPLE, fp);
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 	if (PRINT_FIELD(PERIOD))
1939*4882a593Smuzhiyun 		fprintf(fp, "%10" PRIu64 " ", sample->period);
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 	if (PRINT_FIELD(EVNAME)) {
1942*4882a593Smuzhiyun 		const char *evname = evsel__name(evsel);
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 		if (!script->name_width)
1945*4882a593Smuzhiyun 			script->name_width = evlist__max_name_len(script->session->evlist);
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun 		fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
1948*4882a593Smuzhiyun 	}
1949*4882a593Smuzhiyun 
1950*4882a593Smuzhiyun 	if (print_flags)
1951*4882a593Smuzhiyun 		perf_sample__fprintf_flags(sample->flags, fp);
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	if (is_bts_event(attr)) {
1954*4882a593Smuzhiyun 		perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
1955*4882a593Smuzhiyun 		return;
1956*4882a593Smuzhiyun 	}
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun 	if (PRINT_FIELD(TRACE) && sample->raw_data) {
1959*4882a593Smuzhiyun 		event_format__fprintf(evsel->tp_format, sample->cpu,
1960*4882a593Smuzhiyun 				      sample->raw_data, sample->raw_size, fp);
1961*4882a593Smuzhiyun 	}
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1964*4882a593Smuzhiyun 		perf_sample__fprintf_synth(sample, evsel, fp);
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 	if (PRINT_FIELD(ADDR))
1967*4882a593Smuzhiyun 		perf_sample__fprintf_addr(sample, thread, attr, fp);
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 	if (PRINT_FIELD(DATA_SRC))
1970*4882a593Smuzhiyun 		data_src__fprintf(sample->data_src, fp);
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun 	if (PRINT_FIELD(WEIGHT))
1973*4882a593Smuzhiyun 		fprintf(fp, "%16" PRIu64, sample->weight);
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 	if (PRINT_FIELD(IP)) {
1976*4882a593Smuzhiyun 		struct callchain_cursor *cursor = NULL;
1977*4882a593Smuzhiyun 
1978*4882a593Smuzhiyun 		if (script->stitch_lbr)
1979*4882a593Smuzhiyun 			al->thread->lbr_stitch_enable = true;
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 		if (symbol_conf.use_callchain && sample->callchain &&
1982*4882a593Smuzhiyun 		    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1983*4882a593Smuzhiyun 					      sample, NULL, NULL, scripting_max_stack) == 0)
1984*4882a593Smuzhiyun 			cursor = &callchain_cursor;
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 		fputc(cursor ? '\n' : ' ', fp);
1987*4882a593Smuzhiyun 		sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
1988*4882a593Smuzhiyun 				    symbol_conf.bt_stop_list, fp);
1989*4882a593Smuzhiyun 	}
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun 	if (PRINT_FIELD(IREGS))
1992*4882a593Smuzhiyun 		perf_sample__fprintf_iregs(sample, attr, fp);
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	if (PRINT_FIELD(UREGS))
1995*4882a593Smuzhiyun 		perf_sample__fprintf_uregs(sample, attr, fp);
1996*4882a593Smuzhiyun 
1997*4882a593Smuzhiyun 	if (PRINT_FIELD(BRSTACK))
1998*4882a593Smuzhiyun 		perf_sample__fprintf_brstack(sample, thread, attr, fp);
1999*4882a593Smuzhiyun 	else if (PRINT_FIELD(BRSTACKSYM))
2000*4882a593Smuzhiyun 		perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
2001*4882a593Smuzhiyun 	else if (PRINT_FIELD(BRSTACKOFF))
2002*4882a593Smuzhiyun 		perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
2003*4882a593Smuzhiyun 
2004*4882a593Smuzhiyun 	if (evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
2005*4882a593Smuzhiyun 		perf_sample__fprintf_bpf_output(sample, fp);
2006*4882a593Smuzhiyun 	perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	if (PRINT_FIELD(PHYS_ADDR))
2009*4882a593Smuzhiyun 		fprintf(fp, "%16" PRIx64, sample->phys_addr);
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun 	perf_sample__fprintf_ipc(sample, attr, fp);
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 	fprintf(fp, "\n");
2014*4882a593Smuzhiyun 
2015*4882a593Smuzhiyun 	if (PRINT_FIELD(SRCCODE)) {
2016*4882a593Smuzhiyun 		if (map__fprintf_srccode(al->map, al->addr, stdout,
2017*4882a593Smuzhiyun 					 &thread->srccode_state))
2018*4882a593Smuzhiyun 			printf("\n");
2019*4882a593Smuzhiyun 	}
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	if (PRINT_FIELD(METRIC))
2022*4882a593Smuzhiyun 		perf_sample__fprint_metric(script, thread, evsel, sample, fp);
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 	if (verbose)
2025*4882a593Smuzhiyun 		fflush(fp);
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun static struct scripting_ops	*scripting_ops;
2029*4882a593Smuzhiyun 
__process_stat(struct evsel * counter,u64 tstamp)2030*4882a593Smuzhiyun static void __process_stat(struct evsel *counter, u64 tstamp)
2031*4882a593Smuzhiyun {
2032*4882a593Smuzhiyun 	int nthreads = perf_thread_map__nr(counter->core.threads);
2033*4882a593Smuzhiyun 	int ncpus = evsel__nr_cpus(counter);
2034*4882a593Smuzhiyun 	int cpu, thread;
2035*4882a593Smuzhiyun 	static int header_printed;
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun 	if (counter->core.system_wide)
2038*4882a593Smuzhiyun 		nthreads = 1;
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	if (!header_printed) {
2041*4882a593Smuzhiyun 		printf("%3s %8s %15s %15s %15s %15s %s\n",
2042*4882a593Smuzhiyun 		       "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
2043*4882a593Smuzhiyun 		header_printed = 1;
2044*4882a593Smuzhiyun 	}
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 	for (thread = 0; thread < nthreads; thread++) {
2047*4882a593Smuzhiyun 		for (cpu = 0; cpu < ncpus; cpu++) {
2048*4882a593Smuzhiyun 			struct perf_counts_values *counts;
2049*4882a593Smuzhiyun 
2050*4882a593Smuzhiyun 			counts = perf_counts(counter->counts, cpu, thread);
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 			printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
2053*4882a593Smuzhiyun 				counter->core.cpus->map[cpu],
2054*4882a593Smuzhiyun 				perf_thread_map__pid(counter->core.threads, thread),
2055*4882a593Smuzhiyun 				counts->val,
2056*4882a593Smuzhiyun 				counts->ena,
2057*4882a593Smuzhiyun 				counts->run,
2058*4882a593Smuzhiyun 				tstamp,
2059*4882a593Smuzhiyun 				evsel__name(counter));
2060*4882a593Smuzhiyun 		}
2061*4882a593Smuzhiyun 	}
2062*4882a593Smuzhiyun }
2063*4882a593Smuzhiyun 
process_stat(struct evsel * counter,u64 tstamp)2064*4882a593Smuzhiyun static void process_stat(struct evsel *counter, u64 tstamp)
2065*4882a593Smuzhiyun {
2066*4882a593Smuzhiyun 	if (scripting_ops && scripting_ops->process_stat)
2067*4882a593Smuzhiyun 		scripting_ops->process_stat(&stat_config, counter, tstamp);
2068*4882a593Smuzhiyun 	else
2069*4882a593Smuzhiyun 		__process_stat(counter, tstamp);
2070*4882a593Smuzhiyun }
2071*4882a593Smuzhiyun 
process_stat_interval(u64 tstamp)2072*4882a593Smuzhiyun static void process_stat_interval(u64 tstamp)
2073*4882a593Smuzhiyun {
2074*4882a593Smuzhiyun 	if (scripting_ops && scripting_ops->process_stat_interval)
2075*4882a593Smuzhiyun 		scripting_ops->process_stat_interval(tstamp);
2076*4882a593Smuzhiyun }
2077*4882a593Smuzhiyun 
setup_scripting(void)2078*4882a593Smuzhiyun static void setup_scripting(void)
2079*4882a593Smuzhiyun {
2080*4882a593Smuzhiyun 	setup_perl_scripting();
2081*4882a593Smuzhiyun 	setup_python_scripting();
2082*4882a593Smuzhiyun }
2083*4882a593Smuzhiyun 
flush_scripting(void)2084*4882a593Smuzhiyun static int flush_scripting(void)
2085*4882a593Smuzhiyun {
2086*4882a593Smuzhiyun 	return scripting_ops ? scripting_ops->flush_script() : 0;
2087*4882a593Smuzhiyun }
2088*4882a593Smuzhiyun 
cleanup_scripting(void)2089*4882a593Smuzhiyun static int cleanup_scripting(void)
2090*4882a593Smuzhiyun {
2091*4882a593Smuzhiyun 	pr_debug("\nperf script stopped\n");
2092*4882a593Smuzhiyun 
2093*4882a593Smuzhiyun 	return scripting_ops ? scripting_ops->stop_script() : 0;
2094*4882a593Smuzhiyun }
2095*4882a593Smuzhiyun 
filter_cpu(struct perf_sample * sample)2096*4882a593Smuzhiyun static bool filter_cpu(struct perf_sample *sample)
2097*4882a593Smuzhiyun {
2098*4882a593Smuzhiyun 	if (cpu_list && sample->cpu != (u32)-1)
2099*4882a593Smuzhiyun 		return !test_bit(sample->cpu, cpu_bitmap);
2100*4882a593Smuzhiyun 	return false;
2101*4882a593Smuzhiyun }
2102*4882a593Smuzhiyun 
process_sample_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)2103*4882a593Smuzhiyun static int process_sample_event(struct perf_tool *tool,
2104*4882a593Smuzhiyun 				union perf_event *event,
2105*4882a593Smuzhiyun 				struct perf_sample *sample,
2106*4882a593Smuzhiyun 				struct evsel *evsel,
2107*4882a593Smuzhiyun 				struct machine *machine)
2108*4882a593Smuzhiyun {
2109*4882a593Smuzhiyun 	struct perf_script *scr = container_of(tool, struct perf_script, tool);
2110*4882a593Smuzhiyun 	struct addr_location al;
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 	if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2113*4882a593Smuzhiyun 					  sample->time)) {
2114*4882a593Smuzhiyun 		return 0;
2115*4882a593Smuzhiyun 	}
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	if (debug_mode) {
2118*4882a593Smuzhiyun 		if (sample->time < last_timestamp) {
2119*4882a593Smuzhiyun 			pr_err("Samples misordered, previous: %" PRIu64
2120*4882a593Smuzhiyun 				" this: %" PRIu64 "\n", last_timestamp,
2121*4882a593Smuzhiyun 				sample->time);
2122*4882a593Smuzhiyun 			nr_unordered++;
2123*4882a593Smuzhiyun 		}
2124*4882a593Smuzhiyun 		last_timestamp = sample->time;
2125*4882a593Smuzhiyun 		return 0;
2126*4882a593Smuzhiyun 	}
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 	if (machine__resolve(machine, &al, sample) < 0) {
2129*4882a593Smuzhiyun 		pr_err("problem processing %d event, skipping it.\n",
2130*4882a593Smuzhiyun 		       event->header.type);
2131*4882a593Smuzhiyun 		return -1;
2132*4882a593Smuzhiyun 	}
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun 	if (al.filtered)
2135*4882a593Smuzhiyun 		goto out_put;
2136*4882a593Smuzhiyun 
2137*4882a593Smuzhiyun 	if (filter_cpu(sample))
2138*4882a593Smuzhiyun 		goto out_put;
2139*4882a593Smuzhiyun 
2140*4882a593Smuzhiyun 	if (scripting_ops)
2141*4882a593Smuzhiyun 		scripting_ops->process_event(event, sample, evsel, &al);
2142*4882a593Smuzhiyun 	else
2143*4882a593Smuzhiyun 		process_event(scr, sample, evsel, &al, machine);
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun out_put:
2146*4882a593Smuzhiyun 	addr_location__put(&al);
2147*4882a593Smuzhiyun 	return 0;
2148*4882a593Smuzhiyun }
2149*4882a593Smuzhiyun 
process_attr(struct perf_tool * tool,union perf_event * event,struct evlist ** pevlist)2150*4882a593Smuzhiyun static int process_attr(struct perf_tool *tool, union perf_event *event,
2151*4882a593Smuzhiyun 			struct evlist **pevlist)
2152*4882a593Smuzhiyun {
2153*4882a593Smuzhiyun 	struct perf_script *scr = container_of(tool, struct perf_script, tool);
2154*4882a593Smuzhiyun 	struct evlist *evlist;
2155*4882a593Smuzhiyun 	struct evsel *evsel, *pos;
2156*4882a593Smuzhiyun 	u64 sample_type;
2157*4882a593Smuzhiyun 	int err;
2158*4882a593Smuzhiyun 	static struct evsel_script *es;
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 	err = perf_event__process_attr(tool, event, pevlist);
2161*4882a593Smuzhiyun 	if (err)
2162*4882a593Smuzhiyun 		return err;
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	evlist = *pevlist;
2165*4882a593Smuzhiyun 	evsel = evlist__last(*pevlist);
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	if (!evsel->priv) {
2168*4882a593Smuzhiyun 		if (scr->per_event_dump) {
2169*4882a593Smuzhiyun 			evsel->priv = perf_evsel_script__new(evsel,
2170*4882a593Smuzhiyun 						scr->session->data);
2171*4882a593Smuzhiyun 		} else {
2172*4882a593Smuzhiyun 			es = zalloc(sizeof(*es));
2173*4882a593Smuzhiyun 			if (!es)
2174*4882a593Smuzhiyun 				return -ENOMEM;
2175*4882a593Smuzhiyun 			es->fp = stdout;
2176*4882a593Smuzhiyun 			evsel->priv = es;
2177*4882a593Smuzhiyun 		}
2178*4882a593Smuzhiyun 	}
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 	if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2181*4882a593Smuzhiyun 	    evsel->core.attr.type != PERF_TYPE_SYNTH)
2182*4882a593Smuzhiyun 		return 0;
2183*4882a593Smuzhiyun 
2184*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, pos) {
2185*4882a593Smuzhiyun 		if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
2186*4882a593Smuzhiyun 			return 0;
2187*4882a593Smuzhiyun 	}
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	if (evsel->core.attr.sample_type) {
2190*4882a593Smuzhiyun 		err = evsel__check_attr(evsel, scr->session);
2191*4882a593Smuzhiyun 		if (err)
2192*4882a593Smuzhiyun 			return err;
2193*4882a593Smuzhiyun 	}
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 	/*
2196*4882a593Smuzhiyun 	 * Check if we need to enable callchains based
2197*4882a593Smuzhiyun 	 * on events sample_type.
2198*4882a593Smuzhiyun 	 */
2199*4882a593Smuzhiyun 	sample_type = evlist__combined_sample_type(evlist);
2200*4882a593Smuzhiyun 	callchain_param_setup(sample_type);
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun 	/* Enable fields for callchain entries */
2203*4882a593Smuzhiyun 	if (symbol_conf.use_callchain &&
2204*4882a593Smuzhiyun 	    (sample_type & PERF_SAMPLE_CALLCHAIN ||
2205*4882a593Smuzhiyun 	     sample_type & PERF_SAMPLE_BRANCH_STACK ||
2206*4882a593Smuzhiyun 	     (sample_type & PERF_SAMPLE_REGS_USER &&
2207*4882a593Smuzhiyun 	      sample_type & PERF_SAMPLE_STACK_USER))) {
2208*4882a593Smuzhiyun 		int type = output_type(evsel->core.attr.type);
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun 		if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
2211*4882a593Smuzhiyun 			output[type].fields |= PERF_OUTPUT_IP;
2212*4882a593Smuzhiyun 		if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
2213*4882a593Smuzhiyun 			output[type].fields |= PERF_OUTPUT_SYM;
2214*4882a593Smuzhiyun 	}
2215*4882a593Smuzhiyun 	set_print_ip_opts(&evsel->core.attr);
2216*4882a593Smuzhiyun 	return 0;
2217*4882a593Smuzhiyun }
2218*4882a593Smuzhiyun 
print_event_with_time(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine,pid_t pid,pid_t tid,u64 timestamp)2219*4882a593Smuzhiyun static int print_event_with_time(struct perf_tool *tool,
2220*4882a593Smuzhiyun 				 union perf_event *event,
2221*4882a593Smuzhiyun 				 struct perf_sample *sample,
2222*4882a593Smuzhiyun 				 struct machine *machine,
2223*4882a593Smuzhiyun 				 pid_t pid, pid_t tid, u64 timestamp)
2224*4882a593Smuzhiyun {
2225*4882a593Smuzhiyun 	struct perf_script *script = container_of(tool, struct perf_script, tool);
2226*4882a593Smuzhiyun 	struct perf_session *session = script->session;
2227*4882a593Smuzhiyun 	struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2228*4882a593Smuzhiyun 	struct thread *thread = NULL;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	if (evsel && !evsel->core.attr.sample_id_all) {
2231*4882a593Smuzhiyun 		sample->cpu = 0;
2232*4882a593Smuzhiyun 		sample->time = timestamp;
2233*4882a593Smuzhiyun 		sample->pid = pid;
2234*4882a593Smuzhiyun 		sample->tid = tid;
2235*4882a593Smuzhiyun 	}
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun 	if (filter_cpu(sample))
2238*4882a593Smuzhiyun 		return 0;
2239*4882a593Smuzhiyun 
2240*4882a593Smuzhiyun 	if (tid != -1)
2241*4882a593Smuzhiyun 		thread = machine__findnew_thread(machine, pid, tid);
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	if (evsel) {
2244*4882a593Smuzhiyun 		perf_sample__fprintf_start(script, sample, thread, evsel,
2245*4882a593Smuzhiyun 					   event->header.type, stdout);
2246*4882a593Smuzhiyun 	}
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 	perf_event__fprintf(event, machine, stdout);
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 	thread__put(thread);
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 	return 0;
2253*4882a593Smuzhiyun }
2254*4882a593Smuzhiyun 
print_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine,pid_t pid,pid_t tid)2255*4882a593Smuzhiyun static int print_event(struct perf_tool *tool, union perf_event *event,
2256*4882a593Smuzhiyun 		       struct perf_sample *sample, struct machine *machine,
2257*4882a593Smuzhiyun 		       pid_t pid, pid_t tid)
2258*4882a593Smuzhiyun {
2259*4882a593Smuzhiyun 	return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
2260*4882a593Smuzhiyun }
2261*4882a593Smuzhiyun 
process_comm_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2262*4882a593Smuzhiyun static int process_comm_event(struct perf_tool *tool,
2263*4882a593Smuzhiyun 			      union perf_event *event,
2264*4882a593Smuzhiyun 			      struct perf_sample *sample,
2265*4882a593Smuzhiyun 			      struct machine *machine)
2266*4882a593Smuzhiyun {
2267*4882a593Smuzhiyun 	if (perf_event__process_comm(tool, event, sample, machine) < 0)
2268*4882a593Smuzhiyun 		return -1;
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, event->comm.pid,
2271*4882a593Smuzhiyun 			   event->comm.tid);
2272*4882a593Smuzhiyun }
2273*4882a593Smuzhiyun 
process_namespaces_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2274*4882a593Smuzhiyun static int process_namespaces_event(struct perf_tool *tool,
2275*4882a593Smuzhiyun 				    union perf_event *event,
2276*4882a593Smuzhiyun 				    struct perf_sample *sample,
2277*4882a593Smuzhiyun 				    struct machine *machine)
2278*4882a593Smuzhiyun {
2279*4882a593Smuzhiyun 	if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2280*4882a593Smuzhiyun 		return -1;
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, event->namespaces.pid,
2283*4882a593Smuzhiyun 			   event->namespaces.tid);
2284*4882a593Smuzhiyun }
2285*4882a593Smuzhiyun 
process_cgroup_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2286*4882a593Smuzhiyun static int process_cgroup_event(struct perf_tool *tool,
2287*4882a593Smuzhiyun 				union perf_event *event,
2288*4882a593Smuzhiyun 				struct perf_sample *sample,
2289*4882a593Smuzhiyun 				struct machine *machine)
2290*4882a593Smuzhiyun {
2291*4882a593Smuzhiyun 	if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
2292*4882a593Smuzhiyun 		return -1;
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, sample->pid,
2295*4882a593Smuzhiyun 			    sample->tid);
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun 
process_fork_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2298*4882a593Smuzhiyun static int process_fork_event(struct perf_tool *tool,
2299*4882a593Smuzhiyun 			      union perf_event *event,
2300*4882a593Smuzhiyun 			      struct perf_sample *sample,
2301*4882a593Smuzhiyun 			      struct machine *machine)
2302*4882a593Smuzhiyun {
2303*4882a593Smuzhiyun 	if (perf_event__process_fork(tool, event, sample, machine) < 0)
2304*4882a593Smuzhiyun 		return -1;
2305*4882a593Smuzhiyun 
2306*4882a593Smuzhiyun 	return print_event_with_time(tool, event, sample, machine,
2307*4882a593Smuzhiyun 				     event->fork.pid, event->fork.tid,
2308*4882a593Smuzhiyun 				     event->fork.time);
2309*4882a593Smuzhiyun }
process_exit_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2310*4882a593Smuzhiyun static int process_exit_event(struct perf_tool *tool,
2311*4882a593Smuzhiyun 			      union perf_event *event,
2312*4882a593Smuzhiyun 			      struct perf_sample *sample,
2313*4882a593Smuzhiyun 			      struct machine *machine)
2314*4882a593Smuzhiyun {
2315*4882a593Smuzhiyun 	/* Print before 'exit' deletes anything */
2316*4882a593Smuzhiyun 	if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
2317*4882a593Smuzhiyun 				  event->fork.tid, event->fork.time))
2318*4882a593Smuzhiyun 		return -1;
2319*4882a593Smuzhiyun 
2320*4882a593Smuzhiyun 	return perf_event__process_exit(tool, event, sample, machine);
2321*4882a593Smuzhiyun }
2322*4882a593Smuzhiyun 
process_mmap_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2323*4882a593Smuzhiyun static int process_mmap_event(struct perf_tool *tool,
2324*4882a593Smuzhiyun 			      union perf_event *event,
2325*4882a593Smuzhiyun 			      struct perf_sample *sample,
2326*4882a593Smuzhiyun 			      struct machine *machine)
2327*4882a593Smuzhiyun {
2328*4882a593Smuzhiyun 	if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2329*4882a593Smuzhiyun 		return -1;
2330*4882a593Smuzhiyun 
2331*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, event->mmap.pid,
2332*4882a593Smuzhiyun 			   event->mmap.tid);
2333*4882a593Smuzhiyun }
2334*4882a593Smuzhiyun 
process_mmap2_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2335*4882a593Smuzhiyun static int process_mmap2_event(struct perf_tool *tool,
2336*4882a593Smuzhiyun 			      union perf_event *event,
2337*4882a593Smuzhiyun 			      struct perf_sample *sample,
2338*4882a593Smuzhiyun 			      struct machine *machine)
2339*4882a593Smuzhiyun {
2340*4882a593Smuzhiyun 	if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2341*4882a593Smuzhiyun 		return -1;
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, event->mmap2.pid,
2344*4882a593Smuzhiyun 			   event->mmap2.tid);
2345*4882a593Smuzhiyun }
2346*4882a593Smuzhiyun 
process_switch_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2347*4882a593Smuzhiyun static int process_switch_event(struct perf_tool *tool,
2348*4882a593Smuzhiyun 				union perf_event *event,
2349*4882a593Smuzhiyun 				struct perf_sample *sample,
2350*4882a593Smuzhiyun 				struct machine *machine)
2351*4882a593Smuzhiyun {
2352*4882a593Smuzhiyun 	struct perf_script *script = container_of(tool, struct perf_script, tool);
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun 	if (perf_event__process_switch(tool, event, sample, machine) < 0)
2355*4882a593Smuzhiyun 		return -1;
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun 	if (scripting_ops && scripting_ops->process_switch && !filter_cpu(sample))
2358*4882a593Smuzhiyun 		scripting_ops->process_switch(event, sample, machine);
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun 	if (!script->show_switch_events)
2361*4882a593Smuzhiyun 		return 0;
2362*4882a593Smuzhiyun 
2363*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, sample->pid,
2364*4882a593Smuzhiyun 			   sample->tid);
2365*4882a593Smuzhiyun }
2366*4882a593Smuzhiyun 
2367*4882a593Smuzhiyun static int
process_lost_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2368*4882a593Smuzhiyun process_lost_event(struct perf_tool *tool,
2369*4882a593Smuzhiyun 		   union perf_event *event,
2370*4882a593Smuzhiyun 		   struct perf_sample *sample,
2371*4882a593Smuzhiyun 		   struct machine *machine)
2372*4882a593Smuzhiyun {
2373*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, sample->pid,
2374*4882a593Smuzhiyun 			   sample->tid);
2375*4882a593Smuzhiyun }
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun static int
process_finished_round_event(struct perf_tool * tool __maybe_unused,union perf_event * event,struct ordered_events * oe __maybe_unused)2378*4882a593Smuzhiyun process_finished_round_event(struct perf_tool *tool __maybe_unused,
2379*4882a593Smuzhiyun 			     union perf_event *event,
2380*4882a593Smuzhiyun 			     struct ordered_events *oe __maybe_unused)
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun {
2383*4882a593Smuzhiyun 	perf_event__fprintf(event, NULL, stdout);
2384*4882a593Smuzhiyun 	return 0;
2385*4882a593Smuzhiyun }
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun static int
process_bpf_events(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)2388*4882a593Smuzhiyun process_bpf_events(struct perf_tool *tool __maybe_unused,
2389*4882a593Smuzhiyun 		   union perf_event *event,
2390*4882a593Smuzhiyun 		   struct perf_sample *sample,
2391*4882a593Smuzhiyun 		   struct machine *machine)
2392*4882a593Smuzhiyun {
2393*4882a593Smuzhiyun 	if (machine__process_ksymbol(machine, event, sample) < 0)
2394*4882a593Smuzhiyun 		return -1;
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, sample->pid,
2397*4882a593Smuzhiyun 			   sample->tid);
2398*4882a593Smuzhiyun }
2399*4882a593Smuzhiyun 
process_text_poke_events(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)2400*4882a593Smuzhiyun static int process_text_poke_events(struct perf_tool *tool,
2401*4882a593Smuzhiyun 				    union perf_event *event,
2402*4882a593Smuzhiyun 				    struct perf_sample *sample,
2403*4882a593Smuzhiyun 				    struct machine *machine)
2404*4882a593Smuzhiyun {
2405*4882a593Smuzhiyun 	if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
2406*4882a593Smuzhiyun 		return -1;
2407*4882a593Smuzhiyun 
2408*4882a593Smuzhiyun 	return print_event(tool, event, sample, machine, sample->pid,
2409*4882a593Smuzhiyun 			   sample->tid);
2410*4882a593Smuzhiyun }
2411*4882a593Smuzhiyun 
sig_handler(int sig __maybe_unused)2412*4882a593Smuzhiyun static void sig_handler(int sig __maybe_unused)
2413*4882a593Smuzhiyun {
2414*4882a593Smuzhiyun 	session_done = 1;
2415*4882a593Smuzhiyun }
2416*4882a593Smuzhiyun 
perf_script__fclose_per_event_dump(struct perf_script * script)2417*4882a593Smuzhiyun static void perf_script__fclose_per_event_dump(struct perf_script *script)
2418*4882a593Smuzhiyun {
2419*4882a593Smuzhiyun 	struct evlist *evlist = script->session->evlist;
2420*4882a593Smuzhiyun 	struct evsel *evsel;
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun 	evlist__for_each_entry(evlist, evsel) {
2423*4882a593Smuzhiyun 		if (!evsel->priv)
2424*4882a593Smuzhiyun 			break;
2425*4882a593Smuzhiyun 		perf_evsel_script__delete(evsel->priv);
2426*4882a593Smuzhiyun 		evsel->priv = NULL;
2427*4882a593Smuzhiyun 	}
2428*4882a593Smuzhiyun }
2429*4882a593Smuzhiyun 
perf_script__fopen_per_event_dump(struct perf_script * script)2430*4882a593Smuzhiyun static int perf_script__fopen_per_event_dump(struct perf_script *script)
2431*4882a593Smuzhiyun {
2432*4882a593Smuzhiyun 	struct evsel *evsel;
2433*4882a593Smuzhiyun 
2434*4882a593Smuzhiyun 	evlist__for_each_entry(script->session->evlist, evsel) {
2435*4882a593Smuzhiyun 		/*
2436*4882a593Smuzhiyun 		 * Already setup? I.e. we may be called twice in cases like
2437*4882a593Smuzhiyun 		 * Intel PT, one for the intel_pt// and dummy events, then
2438*4882a593Smuzhiyun 		 * for the evsels syntheized from the auxtrace info.
2439*4882a593Smuzhiyun 		 *
2440*4882a593Smuzhiyun 		 * Ses perf_script__process_auxtrace_info.
2441*4882a593Smuzhiyun 		 */
2442*4882a593Smuzhiyun 		if (evsel->priv != NULL)
2443*4882a593Smuzhiyun 			continue;
2444*4882a593Smuzhiyun 
2445*4882a593Smuzhiyun 		evsel->priv = perf_evsel_script__new(evsel, script->session->data);
2446*4882a593Smuzhiyun 		if (evsel->priv == NULL)
2447*4882a593Smuzhiyun 			goto out_err_fclose;
2448*4882a593Smuzhiyun 	}
2449*4882a593Smuzhiyun 
2450*4882a593Smuzhiyun 	return 0;
2451*4882a593Smuzhiyun 
2452*4882a593Smuzhiyun out_err_fclose:
2453*4882a593Smuzhiyun 	perf_script__fclose_per_event_dump(script);
2454*4882a593Smuzhiyun 	return -1;
2455*4882a593Smuzhiyun }
2456*4882a593Smuzhiyun 
perf_script__setup_per_event_dump(struct perf_script * script)2457*4882a593Smuzhiyun static int perf_script__setup_per_event_dump(struct perf_script *script)
2458*4882a593Smuzhiyun {
2459*4882a593Smuzhiyun 	struct evsel *evsel;
2460*4882a593Smuzhiyun 	static struct evsel_script es_stdout;
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun 	if (script->per_event_dump)
2463*4882a593Smuzhiyun 		return perf_script__fopen_per_event_dump(script);
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun 	es_stdout.fp = stdout;
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	evlist__for_each_entry(script->session->evlist, evsel)
2468*4882a593Smuzhiyun 		evsel->priv = &es_stdout;
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 	return 0;
2471*4882a593Smuzhiyun }
2472*4882a593Smuzhiyun 
perf_script__exit_per_event_dump_stats(struct perf_script * script)2473*4882a593Smuzhiyun static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2474*4882a593Smuzhiyun {
2475*4882a593Smuzhiyun 	struct evsel *evsel;
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun 	evlist__for_each_entry(script->session->evlist, evsel) {
2478*4882a593Smuzhiyun 		struct evsel_script *es = evsel->priv;
2479*4882a593Smuzhiyun 
2480*4882a593Smuzhiyun 		perf_evsel_script__fprintf(es, stdout);
2481*4882a593Smuzhiyun 		perf_evsel_script__delete(es);
2482*4882a593Smuzhiyun 		evsel->priv = NULL;
2483*4882a593Smuzhiyun 	}
2484*4882a593Smuzhiyun }
2485*4882a593Smuzhiyun 
perf_script__exit(struct perf_script * script)2486*4882a593Smuzhiyun static void perf_script__exit(struct perf_script *script)
2487*4882a593Smuzhiyun {
2488*4882a593Smuzhiyun 	perf_thread_map__put(script->threads);
2489*4882a593Smuzhiyun 	perf_cpu_map__put(script->cpus);
2490*4882a593Smuzhiyun }
2491*4882a593Smuzhiyun 
__cmd_script(struct perf_script * script)2492*4882a593Smuzhiyun static int __cmd_script(struct perf_script *script)
2493*4882a593Smuzhiyun {
2494*4882a593Smuzhiyun 	int ret;
2495*4882a593Smuzhiyun 
2496*4882a593Smuzhiyun 	signal(SIGINT, sig_handler);
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 	perf_stat__init_shadow_stats();
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun 	/* override event processing functions */
2501*4882a593Smuzhiyun 	if (script->show_task_events) {
2502*4882a593Smuzhiyun 		script->tool.comm = process_comm_event;
2503*4882a593Smuzhiyun 		script->tool.fork = process_fork_event;
2504*4882a593Smuzhiyun 		script->tool.exit = process_exit_event;
2505*4882a593Smuzhiyun 	}
2506*4882a593Smuzhiyun 	if (script->show_mmap_events) {
2507*4882a593Smuzhiyun 		script->tool.mmap = process_mmap_event;
2508*4882a593Smuzhiyun 		script->tool.mmap2 = process_mmap2_event;
2509*4882a593Smuzhiyun 	}
2510*4882a593Smuzhiyun 	if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
2511*4882a593Smuzhiyun 		script->tool.context_switch = process_switch_event;
2512*4882a593Smuzhiyun 	if (script->show_namespace_events)
2513*4882a593Smuzhiyun 		script->tool.namespaces = process_namespaces_event;
2514*4882a593Smuzhiyun 	if (script->show_cgroup_events)
2515*4882a593Smuzhiyun 		script->tool.cgroup = process_cgroup_event;
2516*4882a593Smuzhiyun 	if (script->show_lost_events)
2517*4882a593Smuzhiyun 		script->tool.lost = process_lost_event;
2518*4882a593Smuzhiyun 	if (script->show_round_events) {
2519*4882a593Smuzhiyun 		script->tool.ordered_events = false;
2520*4882a593Smuzhiyun 		script->tool.finished_round = process_finished_round_event;
2521*4882a593Smuzhiyun 	}
2522*4882a593Smuzhiyun 	if (script->show_bpf_events) {
2523*4882a593Smuzhiyun 		script->tool.ksymbol = process_bpf_events;
2524*4882a593Smuzhiyun 		script->tool.bpf     = process_bpf_events;
2525*4882a593Smuzhiyun 	}
2526*4882a593Smuzhiyun 	if (script->show_text_poke_events) {
2527*4882a593Smuzhiyun 		script->tool.ksymbol   = process_bpf_events;
2528*4882a593Smuzhiyun 		script->tool.text_poke = process_text_poke_events;
2529*4882a593Smuzhiyun 	}
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 	if (perf_script__setup_per_event_dump(script)) {
2532*4882a593Smuzhiyun 		pr_err("Couldn't create the per event dump files\n");
2533*4882a593Smuzhiyun 		return -1;
2534*4882a593Smuzhiyun 	}
2535*4882a593Smuzhiyun 
2536*4882a593Smuzhiyun 	ret = perf_session__process_events(script->session);
2537*4882a593Smuzhiyun 
2538*4882a593Smuzhiyun 	if (script->per_event_dump)
2539*4882a593Smuzhiyun 		perf_script__exit_per_event_dump_stats(script);
2540*4882a593Smuzhiyun 
2541*4882a593Smuzhiyun 	if (debug_mode)
2542*4882a593Smuzhiyun 		pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun 	return ret;
2545*4882a593Smuzhiyun }
2546*4882a593Smuzhiyun 
2547*4882a593Smuzhiyun struct script_spec {
2548*4882a593Smuzhiyun 	struct list_head	node;
2549*4882a593Smuzhiyun 	struct scripting_ops	*ops;
2550*4882a593Smuzhiyun 	char			spec[];
2551*4882a593Smuzhiyun };
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun static LIST_HEAD(script_specs);
2554*4882a593Smuzhiyun 
script_spec__new(const char * spec,struct scripting_ops * ops)2555*4882a593Smuzhiyun static struct script_spec *script_spec__new(const char *spec,
2556*4882a593Smuzhiyun 					    struct scripting_ops *ops)
2557*4882a593Smuzhiyun {
2558*4882a593Smuzhiyun 	struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2559*4882a593Smuzhiyun 
2560*4882a593Smuzhiyun 	if (s != NULL) {
2561*4882a593Smuzhiyun 		strcpy(s->spec, spec);
2562*4882a593Smuzhiyun 		s->ops = ops;
2563*4882a593Smuzhiyun 	}
2564*4882a593Smuzhiyun 
2565*4882a593Smuzhiyun 	return s;
2566*4882a593Smuzhiyun }
2567*4882a593Smuzhiyun 
script_spec__add(struct script_spec * s)2568*4882a593Smuzhiyun static void script_spec__add(struct script_spec *s)
2569*4882a593Smuzhiyun {
2570*4882a593Smuzhiyun 	list_add_tail(&s->node, &script_specs);
2571*4882a593Smuzhiyun }
2572*4882a593Smuzhiyun 
script_spec__find(const char * spec)2573*4882a593Smuzhiyun static struct script_spec *script_spec__find(const char *spec)
2574*4882a593Smuzhiyun {
2575*4882a593Smuzhiyun 	struct script_spec *s;
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun 	list_for_each_entry(s, &script_specs, node)
2578*4882a593Smuzhiyun 		if (strcasecmp(s->spec, spec) == 0)
2579*4882a593Smuzhiyun 			return s;
2580*4882a593Smuzhiyun 	return NULL;
2581*4882a593Smuzhiyun }
2582*4882a593Smuzhiyun 
script_spec_register(const char * spec,struct scripting_ops * ops)2583*4882a593Smuzhiyun int script_spec_register(const char *spec, struct scripting_ops *ops)
2584*4882a593Smuzhiyun {
2585*4882a593Smuzhiyun 	struct script_spec *s;
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun 	s = script_spec__find(spec);
2588*4882a593Smuzhiyun 	if (s)
2589*4882a593Smuzhiyun 		return -1;
2590*4882a593Smuzhiyun 
2591*4882a593Smuzhiyun 	s = script_spec__new(spec, ops);
2592*4882a593Smuzhiyun 	if (!s)
2593*4882a593Smuzhiyun 		return -1;
2594*4882a593Smuzhiyun 	else
2595*4882a593Smuzhiyun 		script_spec__add(s);
2596*4882a593Smuzhiyun 
2597*4882a593Smuzhiyun 	return 0;
2598*4882a593Smuzhiyun }
2599*4882a593Smuzhiyun 
script_spec__lookup(const char * spec)2600*4882a593Smuzhiyun static struct scripting_ops *script_spec__lookup(const char *spec)
2601*4882a593Smuzhiyun {
2602*4882a593Smuzhiyun 	struct script_spec *s = script_spec__find(spec);
2603*4882a593Smuzhiyun 	if (!s)
2604*4882a593Smuzhiyun 		return NULL;
2605*4882a593Smuzhiyun 
2606*4882a593Smuzhiyun 	return s->ops;
2607*4882a593Smuzhiyun }
2608*4882a593Smuzhiyun 
list_available_languages(void)2609*4882a593Smuzhiyun static void list_available_languages(void)
2610*4882a593Smuzhiyun {
2611*4882a593Smuzhiyun 	struct script_spec *s;
2612*4882a593Smuzhiyun 
2613*4882a593Smuzhiyun 	fprintf(stderr, "\n");
2614*4882a593Smuzhiyun 	fprintf(stderr, "Scripting language extensions (used in "
2615*4882a593Smuzhiyun 		"perf script -s [spec:]script.[spec]):\n\n");
2616*4882a593Smuzhiyun 
2617*4882a593Smuzhiyun 	list_for_each_entry(s, &script_specs, node)
2618*4882a593Smuzhiyun 		fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 	fprintf(stderr, "\n");
2621*4882a593Smuzhiyun }
2622*4882a593Smuzhiyun 
parse_scriptname(const struct option * opt __maybe_unused,const char * str,int unset __maybe_unused)2623*4882a593Smuzhiyun static int parse_scriptname(const struct option *opt __maybe_unused,
2624*4882a593Smuzhiyun 			    const char *str, int unset __maybe_unused)
2625*4882a593Smuzhiyun {
2626*4882a593Smuzhiyun 	char spec[PATH_MAX];
2627*4882a593Smuzhiyun 	const char *script, *ext;
2628*4882a593Smuzhiyun 	int len;
2629*4882a593Smuzhiyun 
2630*4882a593Smuzhiyun 	if (strcmp(str, "lang") == 0) {
2631*4882a593Smuzhiyun 		list_available_languages();
2632*4882a593Smuzhiyun 		exit(0);
2633*4882a593Smuzhiyun 	}
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 	script = strchr(str, ':');
2636*4882a593Smuzhiyun 	if (script) {
2637*4882a593Smuzhiyun 		len = script - str;
2638*4882a593Smuzhiyun 		if (len >= PATH_MAX) {
2639*4882a593Smuzhiyun 			fprintf(stderr, "invalid language specifier");
2640*4882a593Smuzhiyun 			return -1;
2641*4882a593Smuzhiyun 		}
2642*4882a593Smuzhiyun 		strncpy(spec, str, len);
2643*4882a593Smuzhiyun 		spec[len] = '\0';
2644*4882a593Smuzhiyun 		scripting_ops = script_spec__lookup(spec);
2645*4882a593Smuzhiyun 		if (!scripting_ops) {
2646*4882a593Smuzhiyun 			fprintf(stderr, "invalid language specifier");
2647*4882a593Smuzhiyun 			return -1;
2648*4882a593Smuzhiyun 		}
2649*4882a593Smuzhiyun 		script++;
2650*4882a593Smuzhiyun 	} else {
2651*4882a593Smuzhiyun 		script = str;
2652*4882a593Smuzhiyun 		ext = strrchr(script, '.');
2653*4882a593Smuzhiyun 		if (!ext) {
2654*4882a593Smuzhiyun 			fprintf(stderr, "invalid script extension");
2655*4882a593Smuzhiyun 			return -1;
2656*4882a593Smuzhiyun 		}
2657*4882a593Smuzhiyun 		scripting_ops = script_spec__lookup(++ext);
2658*4882a593Smuzhiyun 		if (!scripting_ops) {
2659*4882a593Smuzhiyun 			fprintf(stderr, "invalid script extension");
2660*4882a593Smuzhiyun 			return -1;
2661*4882a593Smuzhiyun 		}
2662*4882a593Smuzhiyun 	}
2663*4882a593Smuzhiyun 
2664*4882a593Smuzhiyun 	script_name = strdup(script);
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun 	return 0;
2667*4882a593Smuzhiyun }
2668*4882a593Smuzhiyun 
parse_output_fields(const struct option * opt __maybe_unused,const char * arg,int unset __maybe_unused)2669*4882a593Smuzhiyun static int parse_output_fields(const struct option *opt __maybe_unused,
2670*4882a593Smuzhiyun 			    const char *arg, int unset __maybe_unused)
2671*4882a593Smuzhiyun {
2672*4882a593Smuzhiyun 	char *tok, *strtok_saveptr = NULL;
2673*4882a593Smuzhiyun 	int i, imax = ARRAY_SIZE(all_output_options);
2674*4882a593Smuzhiyun 	int j;
2675*4882a593Smuzhiyun 	int rc = 0;
2676*4882a593Smuzhiyun 	char *str = strdup(arg);
2677*4882a593Smuzhiyun 	int type = -1;
2678*4882a593Smuzhiyun 	enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2679*4882a593Smuzhiyun 
2680*4882a593Smuzhiyun 	if (!str)
2681*4882a593Smuzhiyun 		return -ENOMEM;
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun 	/* first word can state for which event type the user is specifying
2684*4882a593Smuzhiyun 	 * the fields. If no type exists, the specified fields apply to all
2685*4882a593Smuzhiyun 	 * event types found in the file minus the invalid fields for a type.
2686*4882a593Smuzhiyun 	 */
2687*4882a593Smuzhiyun 	tok = strchr(str, ':');
2688*4882a593Smuzhiyun 	if (tok) {
2689*4882a593Smuzhiyun 		*tok = '\0';
2690*4882a593Smuzhiyun 		tok++;
2691*4882a593Smuzhiyun 		if (!strcmp(str, "hw"))
2692*4882a593Smuzhiyun 			type = PERF_TYPE_HARDWARE;
2693*4882a593Smuzhiyun 		else if (!strcmp(str, "sw"))
2694*4882a593Smuzhiyun 			type = PERF_TYPE_SOFTWARE;
2695*4882a593Smuzhiyun 		else if (!strcmp(str, "trace"))
2696*4882a593Smuzhiyun 			type = PERF_TYPE_TRACEPOINT;
2697*4882a593Smuzhiyun 		else if (!strcmp(str, "raw"))
2698*4882a593Smuzhiyun 			type = PERF_TYPE_RAW;
2699*4882a593Smuzhiyun 		else if (!strcmp(str, "break"))
2700*4882a593Smuzhiyun 			type = PERF_TYPE_BREAKPOINT;
2701*4882a593Smuzhiyun 		else if (!strcmp(str, "synth"))
2702*4882a593Smuzhiyun 			type = OUTPUT_TYPE_SYNTH;
2703*4882a593Smuzhiyun 		else {
2704*4882a593Smuzhiyun 			fprintf(stderr, "Invalid event type in field string.\n");
2705*4882a593Smuzhiyun 			rc = -EINVAL;
2706*4882a593Smuzhiyun 			goto out;
2707*4882a593Smuzhiyun 		}
2708*4882a593Smuzhiyun 
2709*4882a593Smuzhiyun 		if (output[type].user_set)
2710*4882a593Smuzhiyun 			pr_warning("Overriding previous field request for %s events.\n",
2711*4882a593Smuzhiyun 				   event_type(type));
2712*4882a593Smuzhiyun 
2713*4882a593Smuzhiyun 		/* Don't override defaults for +- */
2714*4882a593Smuzhiyun 		if (strchr(tok, '+') || strchr(tok, '-'))
2715*4882a593Smuzhiyun 			goto parse;
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun 		output[type].fields = 0;
2718*4882a593Smuzhiyun 		output[type].user_set = true;
2719*4882a593Smuzhiyun 		output[type].wildcard_set = false;
2720*4882a593Smuzhiyun 
2721*4882a593Smuzhiyun 	} else {
2722*4882a593Smuzhiyun 		tok = str;
2723*4882a593Smuzhiyun 		if (strlen(str) == 0) {
2724*4882a593Smuzhiyun 			fprintf(stderr,
2725*4882a593Smuzhiyun 				"Cannot set fields to 'none' for all event types.\n");
2726*4882a593Smuzhiyun 			rc = -EINVAL;
2727*4882a593Smuzhiyun 			goto out;
2728*4882a593Smuzhiyun 		}
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun 		/* Don't override defaults for +- */
2731*4882a593Smuzhiyun 		if (strchr(str, '+') || strchr(str, '-'))
2732*4882a593Smuzhiyun 			goto parse;
2733*4882a593Smuzhiyun 
2734*4882a593Smuzhiyun 		if (output_set_by_user())
2735*4882a593Smuzhiyun 			pr_warning("Overriding previous field request for all events.\n");
2736*4882a593Smuzhiyun 
2737*4882a593Smuzhiyun 		for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2738*4882a593Smuzhiyun 			output[j].fields = 0;
2739*4882a593Smuzhiyun 			output[j].user_set = true;
2740*4882a593Smuzhiyun 			output[j].wildcard_set = true;
2741*4882a593Smuzhiyun 		}
2742*4882a593Smuzhiyun 	}
2743*4882a593Smuzhiyun 
2744*4882a593Smuzhiyun parse:
2745*4882a593Smuzhiyun 	for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2746*4882a593Smuzhiyun 		if (*tok == '+') {
2747*4882a593Smuzhiyun 			if (change == SET)
2748*4882a593Smuzhiyun 				goto out_badmix;
2749*4882a593Smuzhiyun 			change = ADD;
2750*4882a593Smuzhiyun 			tok++;
2751*4882a593Smuzhiyun 		} else if (*tok == '-') {
2752*4882a593Smuzhiyun 			if (change == SET)
2753*4882a593Smuzhiyun 				goto out_badmix;
2754*4882a593Smuzhiyun 			change = REMOVE;
2755*4882a593Smuzhiyun 			tok++;
2756*4882a593Smuzhiyun 		} else {
2757*4882a593Smuzhiyun 			if (change != SET && change != DEFAULT)
2758*4882a593Smuzhiyun 				goto out_badmix;
2759*4882a593Smuzhiyun 			change = SET;
2760*4882a593Smuzhiyun 		}
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 		for (i = 0; i < imax; ++i) {
2763*4882a593Smuzhiyun 			if (strcmp(tok, all_output_options[i].str) == 0)
2764*4882a593Smuzhiyun 				break;
2765*4882a593Smuzhiyun 		}
2766*4882a593Smuzhiyun 		if (i == imax && strcmp(tok, "flags") == 0) {
2767*4882a593Smuzhiyun 			print_flags = change == REMOVE ? false : true;
2768*4882a593Smuzhiyun 			continue;
2769*4882a593Smuzhiyun 		}
2770*4882a593Smuzhiyun 		if (i == imax) {
2771*4882a593Smuzhiyun 			fprintf(stderr, "Invalid field requested.\n");
2772*4882a593Smuzhiyun 			rc = -EINVAL;
2773*4882a593Smuzhiyun 			goto out;
2774*4882a593Smuzhiyun 		}
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun 		if (type == -1) {
2777*4882a593Smuzhiyun 			/* add user option to all events types for
2778*4882a593Smuzhiyun 			 * which it is valid
2779*4882a593Smuzhiyun 			 */
2780*4882a593Smuzhiyun 			for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2781*4882a593Smuzhiyun 				if (output[j].invalid_fields & all_output_options[i].field) {
2782*4882a593Smuzhiyun 					pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2783*4882a593Smuzhiyun 						   all_output_options[i].str, event_type(j));
2784*4882a593Smuzhiyun 				} else {
2785*4882a593Smuzhiyun 					if (change == REMOVE) {
2786*4882a593Smuzhiyun 						output[j].fields &= ~all_output_options[i].field;
2787*4882a593Smuzhiyun 						output[j].user_set_fields &= ~all_output_options[i].field;
2788*4882a593Smuzhiyun 						output[j].user_unset_fields |= all_output_options[i].field;
2789*4882a593Smuzhiyun 					} else {
2790*4882a593Smuzhiyun 						output[j].fields |= all_output_options[i].field;
2791*4882a593Smuzhiyun 						output[j].user_set_fields |= all_output_options[i].field;
2792*4882a593Smuzhiyun 						output[j].user_unset_fields &= ~all_output_options[i].field;
2793*4882a593Smuzhiyun 					}
2794*4882a593Smuzhiyun 					output[j].user_set = true;
2795*4882a593Smuzhiyun 					output[j].wildcard_set = true;
2796*4882a593Smuzhiyun 				}
2797*4882a593Smuzhiyun 			}
2798*4882a593Smuzhiyun 		} else {
2799*4882a593Smuzhiyun 			if (output[type].invalid_fields & all_output_options[i].field) {
2800*4882a593Smuzhiyun 				fprintf(stderr, "\'%s\' not valid for %s events.\n",
2801*4882a593Smuzhiyun 					 all_output_options[i].str, event_type(type));
2802*4882a593Smuzhiyun 
2803*4882a593Smuzhiyun 				rc = -EINVAL;
2804*4882a593Smuzhiyun 				goto out;
2805*4882a593Smuzhiyun 			}
2806*4882a593Smuzhiyun 			if (change == REMOVE)
2807*4882a593Smuzhiyun 				output[type].fields &= ~all_output_options[i].field;
2808*4882a593Smuzhiyun 			else
2809*4882a593Smuzhiyun 				output[type].fields |= all_output_options[i].field;
2810*4882a593Smuzhiyun 			output[type].user_set = true;
2811*4882a593Smuzhiyun 			output[type].wildcard_set = true;
2812*4882a593Smuzhiyun 		}
2813*4882a593Smuzhiyun 	}
2814*4882a593Smuzhiyun 
2815*4882a593Smuzhiyun 	if (type >= 0) {
2816*4882a593Smuzhiyun 		if (output[type].fields == 0) {
2817*4882a593Smuzhiyun 			pr_debug("No fields requested for %s type. "
2818*4882a593Smuzhiyun 				 "Events will not be displayed.\n", event_type(type));
2819*4882a593Smuzhiyun 		}
2820*4882a593Smuzhiyun 	}
2821*4882a593Smuzhiyun 	goto out;
2822*4882a593Smuzhiyun 
2823*4882a593Smuzhiyun out_badmix:
2824*4882a593Smuzhiyun 	fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2825*4882a593Smuzhiyun 	rc = -EINVAL;
2826*4882a593Smuzhiyun out:
2827*4882a593Smuzhiyun 	free(str);
2828*4882a593Smuzhiyun 	return rc;
2829*4882a593Smuzhiyun }
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun #define for_each_lang(scripts_path, scripts_dir, lang_dirent)		\
2832*4882a593Smuzhiyun 	while ((lang_dirent = readdir(scripts_dir)) != NULL)		\
2833*4882a593Smuzhiyun 		if ((lang_dirent->d_type == DT_DIR ||			\
2834*4882a593Smuzhiyun 		     (lang_dirent->d_type == DT_UNKNOWN &&		\
2835*4882a593Smuzhiyun 		      is_directory(scripts_path, lang_dirent))) &&	\
2836*4882a593Smuzhiyun 		    (strcmp(lang_dirent->d_name, ".")) &&		\
2837*4882a593Smuzhiyun 		    (strcmp(lang_dirent->d_name, "..")))
2838*4882a593Smuzhiyun 
2839*4882a593Smuzhiyun #define for_each_script(lang_path, lang_dir, script_dirent)		\
2840*4882a593Smuzhiyun 	while ((script_dirent = readdir(lang_dir)) != NULL)		\
2841*4882a593Smuzhiyun 		if (script_dirent->d_type != DT_DIR &&			\
2842*4882a593Smuzhiyun 		    (script_dirent->d_type != DT_UNKNOWN ||		\
2843*4882a593Smuzhiyun 		     !is_directory(lang_path, script_dirent)))
2844*4882a593Smuzhiyun 
2845*4882a593Smuzhiyun 
2846*4882a593Smuzhiyun #define RECORD_SUFFIX			"-record"
2847*4882a593Smuzhiyun #define REPORT_SUFFIX			"-report"
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun struct script_desc {
2850*4882a593Smuzhiyun 	struct list_head	node;
2851*4882a593Smuzhiyun 	char			*name;
2852*4882a593Smuzhiyun 	char			*half_liner;
2853*4882a593Smuzhiyun 	char			*args;
2854*4882a593Smuzhiyun };
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun static LIST_HEAD(script_descs);
2857*4882a593Smuzhiyun 
script_desc__new(const char * name)2858*4882a593Smuzhiyun static struct script_desc *script_desc__new(const char *name)
2859*4882a593Smuzhiyun {
2860*4882a593Smuzhiyun 	struct script_desc *s = zalloc(sizeof(*s));
2861*4882a593Smuzhiyun 
2862*4882a593Smuzhiyun 	if (s != NULL && name)
2863*4882a593Smuzhiyun 		s->name = strdup(name);
2864*4882a593Smuzhiyun 
2865*4882a593Smuzhiyun 	return s;
2866*4882a593Smuzhiyun }
2867*4882a593Smuzhiyun 
script_desc__delete(struct script_desc * s)2868*4882a593Smuzhiyun static void script_desc__delete(struct script_desc *s)
2869*4882a593Smuzhiyun {
2870*4882a593Smuzhiyun 	zfree(&s->name);
2871*4882a593Smuzhiyun 	zfree(&s->half_liner);
2872*4882a593Smuzhiyun 	zfree(&s->args);
2873*4882a593Smuzhiyun 	free(s);
2874*4882a593Smuzhiyun }
2875*4882a593Smuzhiyun 
script_desc__add(struct script_desc * s)2876*4882a593Smuzhiyun static void script_desc__add(struct script_desc *s)
2877*4882a593Smuzhiyun {
2878*4882a593Smuzhiyun 	list_add_tail(&s->node, &script_descs);
2879*4882a593Smuzhiyun }
2880*4882a593Smuzhiyun 
script_desc__find(const char * name)2881*4882a593Smuzhiyun static struct script_desc *script_desc__find(const char *name)
2882*4882a593Smuzhiyun {
2883*4882a593Smuzhiyun 	struct script_desc *s;
2884*4882a593Smuzhiyun 
2885*4882a593Smuzhiyun 	list_for_each_entry(s, &script_descs, node)
2886*4882a593Smuzhiyun 		if (strcasecmp(s->name, name) == 0)
2887*4882a593Smuzhiyun 			return s;
2888*4882a593Smuzhiyun 	return NULL;
2889*4882a593Smuzhiyun }
2890*4882a593Smuzhiyun 
script_desc__findnew(const char * name)2891*4882a593Smuzhiyun static struct script_desc *script_desc__findnew(const char *name)
2892*4882a593Smuzhiyun {
2893*4882a593Smuzhiyun 	struct script_desc *s = script_desc__find(name);
2894*4882a593Smuzhiyun 
2895*4882a593Smuzhiyun 	if (s)
2896*4882a593Smuzhiyun 		return s;
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun 	s = script_desc__new(name);
2899*4882a593Smuzhiyun 	if (!s)
2900*4882a593Smuzhiyun 		return NULL;
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun 	script_desc__add(s);
2903*4882a593Smuzhiyun 
2904*4882a593Smuzhiyun 	return s;
2905*4882a593Smuzhiyun }
2906*4882a593Smuzhiyun 
ends_with(const char * str,const char * suffix)2907*4882a593Smuzhiyun static const char *ends_with(const char *str, const char *suffix)
2908*4882a593Smuzhiyun {
2909*4882a593Smuzhiyun 	size_t suffix_len = strlen(suffix);
2910*4882a593Smuzhiyun 	const char *p = str;
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	if (strlen(str) > suffix_len) {
2913*4882a593Smuzhiyun 		p = str + strlen(str) - suffix_len;
2914*4882a593Smuzhiyun 		if (!strncmp(p, suffix, suffix_len))
2915*4882a593Smuzhiyun 			return p;
2916*4882a593Smuzhiyun 	}
2917*4882a593Smuzhiyun 
2918*4882a593Smuzhiyun 	return NULL;
2919*4882a593Smuzhiyun }
2920*4882a593Smuzhiyun 
read_script_info(struct script_desc * desc,const char * filename)2921*4882a593Smuzhiyun static int read_script_info(struct script_desc *desc, const char *filename)
2922*4882a593Smuzhiyun {
2923*4882a593Smuzhiyun 	char line[BUFSIZ], *p;
2924*4882a593Smuzhiyun 	FILE *fp;
2925*4882a593Smuzhiyun 
2926*4882a593Smuzhiyun 	fp = fopen(filename, "r");
2927*4882a593Smuzhiyun 	if (!fp)
2928*4882a593Smuzhiyun 		return -1;
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	while (fgets(line, sizeof(line), fp)) {
2931*4882a593Smuzhiyun 		p = skip_spaces(line);
2932*4882a593Smuzhiyun 		if (strlen(p) == 0)
2933*4882a593Smuzhiyun 			continue;
2934*4882a593Smuzhiyun 		if (*p != '#')
2935*4882a593Smuzhiyun 			continue;
2936*4882a593Smuzhiyun 		p++;
2937*4882a593Smuzhiyun 		if (strlen(p) && *p == '!')
2938*4882a593Smuzhiyun 			continue;
2939*4882a593Smuzhiyun 
2940*4882a593Smuzhiyun 		p = skip_spaces(p);
2941*4882a593Smuzhiyun 		if (strlen(p) && p[strlen(p) - 1] == '\n')
2942*4882a593Smuzhiyun 			p[strlen(p) - 1] = '\0';
2943*4882a593Smuzhiyun 
2944*4882a593Smuzhiyun 		if (!strncmp(p, "description:", strlen("description:"))) {
2945*4882a593Smuzhiyun 			p += strlen("description:");
2946*4882a593Smuzhiyun 			desc->half_liner = strdup(skip_spaces(p));
2947*4882a593Smuzhiyun 			continue;
2948*4882a593Smuzhiyun 		}
2949*4882a593Smuzhiyun 
2950*4882a593Smuzhiyun 		if (!strncmp(p, "args:", strlen("args:"))) {
2951*4882a593Smuzhiyun 			p += strlen("args:");
2952*4882a593Smuzhiyun 			desc->args = strdup(skip_spaces(p));
2953*4882a593Smuzhiyun 			continue;
2954*4882a593Smuzhiyun 		}
2955*4882a593Smuzhiyun 	}
2956*4882a593Smuzhiyun 
2957*4882a593Smuzhiyun 	fclose(fp);
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun 	return 0;
2960*4882a593Smuzhiyun }
2961*4882a593Smuzhiyun 
get_script_root(struct dirent * script_dirent,const char * suffix)2962*4882a593Smuzhiyun static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2963*4882a593Smuzhiyun {
2964*4882a593Smuzhiyun 	char *script_root, *str;
2965*4882a593Smuzhiyun 
2966*4882a593Smuzhiyun 	script_root = strdup(script_dirent->d_name);
2967*4882a593Smuzhiyun 	if (!script_root)
2968*4882a593Smuzhiyun 		return NULL;
2969*4882a593Smuzhiyun 
2970*4882a593Smuzhiyun 	str = (char *)ends_with(script_root, suffix);
2971*4882a593Smuzhiyun 	if (!str) {
2972*4882a593Smuzhiyun 		free(script_root);
2973*4882a593Smuzhiyun 		return NULL;
2974*4882a593Smuzhiyun 	}
2975*4882a593Smuzhiyun 
2976*4882a593Smuzhiyun 	*str = '\0';
2977*4882a593Smuzhiyun 	return script_root;
2978*4882a593Smuzhiyun }
2979*4882a593Smuzhiyun 
list_available_scripts(const struct option * opt __maybe_unused,const char * s __maybe_unused,int unset __maybe_unused)2980*4882a593Smuzhiyun static int list_available_scripts(const struct option *opt __maybe_unused,
2981*4882a593Smuzhiyun 				  const char *s __maybe_unused,
2982*4882a593Smuzhiyun 				  int unset __maybe_unused)
2983*4882a593Smuzhiyun {
2984*4882a593Smuzhiyun 	struct dirent *script_dirent, *lang_dirent;
2985*4882a593Smuzhiyun 	char scripts_path[MAXPATHLEN];
2986*4882a593Smuzhiyun 	DIR *scripts_dir, *lang_dir;
2987*4882a593Smuzhiyun 	char script_path[MAXPATHLEN];
2988*4882a593Smuzhiyun 	char lang_path[MAXPATHLEN];
2989*4882a593Smuzhiyun 	struct script_desc *desc;
2990*4882a593Smuzhiyun 	char first_half[BUFSIZ];
2991*4882a593Smuzhiyun 	char *script_root;
2992*4882a593Smuzhiyun 
2993*4882a593Smuzhiyun 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2994*4882a593Smuzhiyun 
2995*4882a593Smuzhiyun 	scripts_dir = opendir(scripts_path);
2996*4882a593Smuzhiyun 	if (!scripts_dir) {
2997*4882a593Smuzhiyun 		fprintf(stdout,
2998*4882a593Smuzhiyun 			"open(%s) failed.\n"
2999*4882a593Smuzhiyun 			"Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3000*4882a593Smuzhiyun 			scripts_path);
3001*4882a593Smuzhiyun 		exit(-1);
3002*4882a593Smuzhiyun 	}
3003*4882a593Smuzhiyun 
3004*4882a593Smuzhiyun 	for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3005*4882a593Smuzhiyun 		scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3006*4882a593Smuzhiyun 			  lang_dirent->d_name);
3007*4882a593Smuzhiyun 		lang_dir = opendir(lang_path);
3008*4882a593Smuzhiyun 		if (!lang_dir)
3009*4882a593Smuzhiyun 			continue;
3010*4882a593Smuzhiyun 
3011*4882a593Smuzhiyun 		for_each_script(lang_path, lang_dir, script_dirent) {
3012*4882a593Smuzhiyun 			script_root = get_script_root(script_dirent, REPORT_SUFFIX);
3013*4882a593Smuzhiyun 			if (script_root) {
3014*4882a593Smuzhiyun 				desc = script_desc__findnew(script_root);
3015*4882a593Smuzhiyun 				scnprintf(script_path, MAXPATHLEN, "%s/%s",
3016*4882a593Smuzhiyun 					  lang_path, script_dirent->d_name);
3017*4882a593Smuzhiyun 				read_script_info(desc, script_path);
3018*4882a593Smuzhiyun 				free(script_root);
3019*4882a593Smuzhiyun 			}
3020*4882a593Smuzhiyun 		}
3021*4882a593Smuzhiyun 	}
3022*4882a593Smuzhiyun 
3023*4882a593Smuzhiyun 	fprintf(stdout, "List of available trace scripts:\n");
3024*4882a593Smuzhiyun 	list_for_each_entry(desc, &script_descs, node) {
3025*4882a593Smuzhiyun 		sprintf(first_half, "%s %s", desc->name,
3026*4882a593Smuzhiyun 			desc->args ? desc->args : "");
3027*4882a593Smuzhiyun 		fprintf(stdout, "  %-36s %s\n", first_half,
3028*4882a593Smuzhiyun 			desc->half_liner ? desc->half_liner : "");
3029*4882a593Smuzhiyun 	}
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun 	exit(0);
3032*4882a593Smuzhiyun }
3033*4882a593Smuzhiyun 
3034*4882a593Smuzhiyun /*
3035*4882a593Smuzhiyun  * Some scripts specify the required events in their "xxx-record" file,
3036*4882a593Smuzhiyun  * this function will check if the events in perf.data match those
3037*4882a593Smuzhiyun  * mentioned in the "xxx-record".
3038*4882a593Smuzhiyun  *
3039*4882a593Smuzhiyun  * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3040*4882a593Smuzhiyun  * which is covered well now. And new parsing code should be added to
3041*4882a593Smuzhiyun  * cover the future complexing formats like event groups etc.
3042*4882a593Smuzhiyun  */
check_ev_match(char * dir_name,char * scriptname,struct perf_session * session)3043*4882a593Smuzhiyun static int check_ev_match(char *dir_name, char *scriptname,
3044*4882a593Smuzhiyun 			struct perf_session *session)
3045*4882a593Smuzhiyun {
3046*4882a593Smuzhiyun 	char filename[MAXPATHLEN], evname[128];
3047*4882a593Smuzhiyun 	char line[BUFSIZ], *p;
3048*4882a593Smuzhiyun 	struct evsel *pos;
3049*4882a593Smuzhiyun 	int match, len;
3050*4882a593Smuzhiyun 	FILE *fp;
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun 	scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
3053*4882a593Smuzhiyun 
3054*4882a593Smuzhiyun 	fp = fopen(filename, "r");
3055*4882a593Smuzhiyun 	if (!fp)
3056*4882a593Smuzhiyun 		return -1;
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun 	while (fgets(line, sizeof(line), fp)) {
3059*4882a593Smuzhiyun 		p = skip_spaces(line);
3060*4882a593Smuzhiyun 		if (*p == '#')
3061*4882a593Smuzhiyun 			continue;
3062*4882a593Smuzhiyun 
3063*4882a593Smuzhiyun 		while (strlen(p)) {
3064*4882a593Smuzhiyun 			p = strstr(p, "-e");
3065*4882a593Smuzhiyun 			if (!p)
3066*4882a593Smuzhiyun 				break;
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun 			p += 2;
3069*4882a593Smuzhiyun 			p = skip_spaces(p);
3070*4882a593Smuzhiyun 			len = strcspn(p, " \t");
3071*4882a593Smuzhiyun 			if (!len)
3072*4882a593Smuzhiyun 				break;
3073*4882a593Smuzhiyun 
3074*4882a593Smuzhiyun 			snprintf(evname, len + 1, "%s", p);
3075*4882a593Smuzhiyun 
3076*4882a593Smuzhiyun 			match = 0;
3077*4882a593Smuzhiyun 			evlist__for_each_entry(session->evlist, pos) {
3078*4882a593Smuzhiyun 				if (!strcmp(evsel__name(pos), evname)) {
3079*4882a593Smuzhiyun 					match = 1;
3080*4882a593Smuzhiyun 					break;
3081*4882a593Smuzhiyun 				}
3082*4882a593Smuzhiyun 			}
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 			if (!match) {
3085*4882a593Smuzhiyun 				fclose(fp);
3086*4882a593Smuzhiyun 				return -1;
3087*4882a593Smuzhiyun 			}
3088*4882a593Smuzhiyun 		}
3089*4882a593Smuzhiyun 	}
3090*4882a593Smuzhiyun 
3091*4882a593Smuzhiyun 	fclose(fp);
3092*4882a593Smuzhiyun 	return 0;
3093*4882a593Smuzhiyun }
3094*4882a593Smuzhiyun 
3095*4882a593Smuzhiyun /*
3096*4882a593Smuzhiyun  * Return -1 if none is found, otherwise the actual scripts number.
3097*4882a593Smuzhiyun  *
3098*4882a593Smuzhiyun  * Currently the only user of this function is the script browser, which
3099*4882a593Smuzhiyun  * will list all statically runnable scripts, select one, execute it and
3100*4882a593Smuzhiyun  * show the output in a perf browser.
3101*4882a593Smuzhiyun  */
find_scripts(char ** scripts_array,char ** scripts_path_array,int num,int pathlen)3102*4882a593Smuzhiyun int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3103*4882a593Smuzhiyun 		 int pathlen)
3104*4882a593Smuzhiyun {
3105*4882a593Smuzhiyun 	struct dirent *script_dirent, *lang_dirent;
3106*4882a593Smuzhiyun 	char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
3107*4882a593Smuzhiyun 	DIR *scripts_dir, *lang_dir;
3108*4882a593Smuzhiyun 	struct perf_session *session;
3109*4882a593Smuzhiyun 	struct perf_data data = {
3110*4882a593Smuzhiyun 		.path = input_name,
3111*4882a593Smuzhiyun 		.mode = PERF_DATA_MODE_READ,
3112*4882a593Smuzhiyun 	};
3113*4882a593Smuzhiyun 	char *temp;
3114*4882a593Smuzhiyun 	int i = 0;
3115*4882a593Smuzhiyun 
3116*4882a593Smuzhiyun 	session = perf_session__new(&data, false, NULL);
3117*4882a593Smuzhiyun 	if (IS_ERR(session))
3118*4882a593Smuzhiyun 		return PTR_ERR(session);
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3121*4882a593Smuzhiyun 
3122*4882a593Smuzhiyun 	scripts_dir = opendir(scripts_path);
3123*4882a593Smuzhiyun 	if (!scripts_dir) {
3124*4882a593Smuzhiyun 		perf_session__delete(session);
3125*4882a593Smuzhiyun 		return -1;
3126*4882a593Smuzhiyun 	}
3127*4882a593Smuzhiyun 
3128*4882a593Smuzhiyun 	for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3129*4882a593Smuzhiyun 		scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3130*4882a593Smuzhiyun 			  lang_dirent->d_name);
3131*4882a593Smuzhiyun #ifndef HAVE_LIBPERL_SUPPORT
3132*4882a593Smuzhiyun 		if (strstr(lang_path, "perl"))
3133*4882a593Smuzhiyun 			continue;
3134*4882a593Smuzhiyun #endif
3135*4882a593Smuzhiyun #ifndef HAVE_LIBPYTHON_SUPPORT
3136*4882a593Smuzhiyun 		if (strstr(lang_path, "python"))
3137*4882a593Smuzhiyun 			continue;
3138*4882a593Smuzhiyun #endif
3139*4882a593Smuzhiyun 
3140*4882a593Smuzhiyun 		lang_dir = opendir(lang_path);
3141*4882a593Smuzhiyun 		if (!lang_dir)
3142*4882a593Smuzhiyun 			continue;
3143*4882a593Smuzhiyun 
3144*4882a593Smuzhiyun 		for_each_script(lang_path, lang_dir, script_dirent) {
3145*4882a593Smuzhiyun 			/* Skip those real time scripts: xxxtop.p[yl] */
3146*4882a593Smuzhiyun 			if (strstr(script_dirent->d_name, "top."))
3147*4882a593Smuzhiyun 				continue;
3148*4882a593Smuzhiyun 			if (i >= num)
3149*4882a593Smuzhiyun 				break;
3150*4882a593Smuzhiyun 			snprintf(scripts_path_array[i], pathlen, "%s/%s",
3151*4882a593Smuzhiyun 				lang_path,
3152*4882a593Smuzhiyun 				script_dirent->d_name);
3153*4882a593Smuzhiyun 			temp = strchr(script_dirent->d_name, '.');
3154*4882a593Smuzhiyun 			snprintf(scripts_array[i],
3155*4882a593Smuzhiyun 				(temp - script_dirent->d_name) + 1,
3156*4882a593Smuzhiyun 				"%s", script_dirent->d_name);
3157*4882a593Smuzhiyun 
3158*4882a593Smuzhiyun 			if (check_ev_match(lang_path,
3159*4882a593Smuzhiyun 					scripts_array[i], session))
3160*4882a593Smuzhiyun 				continue;
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 			i++;
3163*4882a593Smuzhiyun 		}
3164*4882a593Smuzhiyun 		closedir(lang_dir);
3165*4882a593Smuzhiyun 	}
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 	closedir(scripts_dir);
3168*4882a593Smuzhiyun 	perf_session__delete(session);
3169*4882a593Smuzhiyun 	return i;
3170*4882a593Smuzhiyun }
3171*4882a593Smuzhiyun 
get_script_path(const char * script_root,const char * suffix)3172*4882a593Smuzhiyun static char *get_script_path(const char *script_root, const char *suffix)
3173*4882a593Smuzhiyun {
3174*4882a593Smuzhiyun 	struct dirent *script_dirent, *lang_dirent;
3175*4882a593Smuzhiyun 	char scripts_path[MAXPATHLEN];
3176*4882a593Smuzhiyun 	char script_path[MAXPATHLEN];
3177*4882a593Smuzhiyun 	DIR *scripts_dir, *lang_dir;
3178*4882a593Smuzhiyun 	char lang_path[MAXPATHLEN];
3179*4882a593Smuzhiyun 	char *__script_root;
3180*4882a593Smuzhiyun 
3181*4882a593Smuzhiyun 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3182*4882a593Smuzhiyun 
3183*4882a593Smuzhiyun 	scripts_dir = opendir(scripts_path);
3184*4882a593Smuzhiyun 	if (!scripts_dir)
3185*4882a593Smuzhiyun 		return NULL;
3186*4882a593Smuzhiyun 
3187*4882a593Smuzhiyun 	for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3188*4882a593Smuzhiyun 		scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3189*4882a593Smuzhiyun 			  lang_dirent->d_name);
3190*4882a593Smuzhiyun 		lang_dir = opendir(lang_path);
3191*4882a593Smuzhiyun 		if (!lang_dir)
3192*4882a593Smuzhiyun 			continue;
3193*4882a593Smuzhiyun 
3194*4882a593Smuzhiyun 		for_each_script(lang_path, lang_dir, script_dirent) {
3195*4882a593Smuzhiyun 			__script_root = get_script_root(script_dirent, suffix);
3196*4882a593Smuzhiyun 			if (__script_root && !strcmp(script_root, __script_root)) {
3197*4882a593Smuzhiyun 				free(__script_root);
3198*4882a593Smuzhiyun 				closedir(scripts_dir);
3199*4882a593Smuzhiyun 				scnprintf(script_path, MAXPATHLEN, "%s/%s",
3200*4882a593Smuzhiyun 					  lang_path, script_dirent->d_name);
3201*4882a593Smuzhiyun 				closedir(lang_dir);
3202*4882a593Smuzhiyun 				return strdup(script_path);
3203*4882a593Smuzhiyun 			}
3204*4882a593Smuzhiyun 			free(__script_root);
3205*4882a593Smuzhiyun 		}
3206*4882a593Smuzhiyun 		closedir(lang_dir);
3207*4882a593Smuzhiyun 	}
3208*4882a593Smuzhiyun 	closedir(scripts_dir);
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun 	return NULL;
3211*4882a593Smuzhiyun }
3212*4882a593Smuzhiyun 
is_top_script(const char * script_path)3213*4882a593Smuzhiyun static bool is_top_script(const char *script_path)
3214*4882a593Smuzhiyun {
3215*4882a593Smuzhiyun 	return ends_with(script_path, "top") == NULL ? false : true;
3216*4882a593Smuzhiyun }
3217*4882a593Smuzhiyun 
has_required_arg(char * script_path)3218*4882a593Smuzhiyun static int has_required_arg(char *script_path)
3219*4882a593Smuzhiyun {
3220*4882a593Smuzhiyun 	struct script_desc *desc;
3221*4882a593Smuzhiyun 	int n_args = 0;
3222*4882a593Smuzhiyun 	char *p;
3223*4882a593Smuzhiyun 
3224*4882a593Smuzhiyun 	desc = script_desc__new(NULL);
3225*4882a593Smuzhiyun 
3226*4882a593Smuzhiyun 	if (read_script_info(desc, script_path))
3227*4882a593Smuzhiyun 		goto out;
3228*4882a593Smuzhiyun 
3229*4882a593Smuzhiyun 	if (!desc->args)
3230*4882a593Smuzhiyun 		goto out;
3231*4882a593Smuzhiyun 
3232*4882a593Smuzhiyun 	for (p = desc->args; *p; p++)
3233*4882a593Smuzhiyun 		if (*p == '<')
3234*4882a593Smuzhiyun 			n_args++;
3235*4882a593Smuzhiyun out:
3236*4882a593Smuzhiyun 	script_desc__delete(desc);
3237*4882a593Smuzhiyun 
3238*4882a593Smuzhiyun 	return n_args;
3239*4882a593Smuzhiyun }
3240*4882a593Smuzhiyun 
have_cmd(int argc,const char ** argv)3241*4882a593Smuzhiyun static int have_cmd(int argc, const char **argv)
3242*4882a593Smuzhiyun {
3243*4882a593Smuzhiyun 	char **__argv = malloc(sizeof(const char *) * argc);
3244*4882a593Smuzhiyun 
3245*4882a593Smuzhiyun 	if (!__argv) {
3246*4882a593Smuzhiyun 		pr_err("malloc failed\n");
3247*4882a593Smuzhiyun 		return -1;
3248*4882a593Smuzhiyun 	}
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	memcpy(__argv, argv, sizeof(const char *) * argc);
3251*4882a593Smuzhiyun 	argc = parse_options(argc, (const char **)__argv, record_options,
3252*4882a593Smuzhiyun 			     NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3253*4882a593Smuzhiyun 	free(__argv);
3254*4882a593Smuzhiyun 
3255*4882a593Smuzhiyun 	system_wide = (argc == 0);
3256*4882a593Smuzhiyun 
3257*4882a593Smuzhiyun 	return 0;
3258*4882a593Smuzhiyun }
3259*4882a593Smuzhiyun 
script__setup_sample_type(struct perf_script * script)3260*4882a593Smuzhiyun static void script__setup_sample_type(struct perf_script *script)
3261*4882a593Smuzhiyun {
3262*4882a593Smuzhiyun 	struct perf_session *session = script->session;
3263*4882a593Smuzhiyun 	u64 sample_type = evlist__combined_sample_type(session->evlist);
3264*4882a593Smuzhiyun 
3265*4882a593Smuzhiyun 	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3266*4882a593Smuzhiyun 		if ((sample_type & PERF_SAMPLE_REGS_USER) &&
3267*4882a593Smuzhiyun 		    (sample_type & PERF_SAMPLE_STACK_USER)) {
3268*4882a593Smuzhiyun 			callchain_param.record_mode = CALLCHAIN_DWARF;
3269*4882a593Smuzhiyun 			dwarf_callchain_users = true;
3270*4882a593Smuzhiyun 		} else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
3271*4882a593Smuzhiyun 			callchain_param.record_mode = CALLCHAIN_LBR;
3272*4882a593Smuzhiyun 		else
3273*4882a593Smuzhiyun 			callchain_param.record_mode = CALLCHAIN_FP;
3274*4882a593Smuzhiyun 	}
3275*4882a593Smuzhiyun 
3276*4882a593Smuzhiyun 	if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
3277*4882a593Smuzhiyun 		pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
3278*4882a593Smuzhiyun 			   "Please apply --call-graph lbr when recording.\n");
3279*4882a593Smuzhiyun 		script->stitch_lbr = false;
3280*4882a593Smuzhiyun 	}
3281*4882a593Smuzhiyun }
3282*4882a593Smuzhiyun 
process_stat_round_event(struct perf_session * session,union perf_event * event)3283*4882a593Smuzhiyun static int process_stat_round_event(struct perf_session *session,
3284*4882a593Smuzhiyun 				    union perf_event *event)
3285*4882a593Smuzhiyun {
3286*4882a593Smuzhiyun 	struct perf_record_stat_round *round = &event->stat_round;
3287*4882a593Smuzhiyun 	struct evsel *counter;
3288*4882a593Smuzhiyun 
3289*4882a593Smuzhiyun 	evlist__for_each_entry(session->evlist, counter) {
3290*4882a593Smuzhiyun 		perf_stat_process_counter(&stat_config, counter);
3291*4882a593Smuzhiyun 		process_stat(counter, round->time);
3292*4882a593Smuzhiyun 	}
3293*4882a593Smuzhiyun 
3294*4882a593Smuzhiyun 	process_stat_interval(round->time);
3295*4882a593Smuzhiyun 	return 0;
3296*4882a593Smuzhiyun }
3297*4882a593Smuzhiyun 
process_stat_config_event(struct perf_session * session __maybe_unused,union perf_event * event)3298*4882a593Smuzhiyun static int process_stat_config_event(struct perf_session *session __maybe_unused,
3299*4882a593Smuzhiyun 				     union perf_event *event)
3300*4882a593Smuzhiyun {
3301*4882a593Smuzhiyun 	perf_event__read_stat_config(&stat_config, &event->stat_config);
3302*4882a593Smuzhiyun 	return 0;
3303*4882a593Smuzhiyun }
3304*4882a593Smuzhiyun 
set_maps(struct perf_script * script)3305*4882a593Smuzhiyun static int set_maps(struct perf_script *script)
3306*4882a593Smuzhiyun {
3307*4882a593Smuzhiyun 	struct evlist *evlist = script->session->evlist;
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 	if (!script->cpus || !script->threads)
3310*4882a593Smuzhiyun 		return 0;
3311*4882a593Smuzhiyun 
3312*4882a593Smuzhiyun 	if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3313*4882a593Smuzhiyun 		return -EINVAL;
3314*4882a593Smuzhiyun 
3315*4882a593Smuzhiyun 	perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
3316*4882a593Smuzhiyun 
3317*4882a593Smuzhiyun 	if (perf_evlist__alloc_stats(evlist, true))
3318*4882a593Smuzhiyun 		return -ENOMEM;
3319*4882a593Smuzhiyun 
3320*4882a593Smuzhiyun 	script->allocated = true;
3321*4882a593Smuzhiyun 	return 0;
3322*4882a593Smuzhiyun }
3323*4882a593Smuzhiyun 
3324*4882a593Smuzhiyun static
process_thread_map_event(struct perf_session * session,union perf_event * event)3325*4882a593Smuzhiyun int process_thread_map_event(struct perf_session *session,
3326*4882a593Smuzhiyun 			     union perf_event *event)
3327*4882a593Smuzhiyun {
3328*4882a593Smuzhiyun 	struct perf_tool *tool = session->tool;
3329*4882a593Smuzhiyun 	struct perf_script *script = container_of(tool, struct perf_script, tool);
3330*4882a593Smuzhiyun 
3331*4882a593Smuzhiyun 	if (script->threads) {
3332*4882a593Smuzhiyun 		pr_warning("Extra thread map event, ignoring.\n");
3333*4882a593Smuzhiyun 		return 0;
3334*4882a593Smuzhiyun 	}
3335*4882a593Smuzhiyun 
3336*4882a593Smuzhiyun 	script->threads = thread_map__new_event(&event->thread_map);
3337*4882a593Smuzhiyun 	if (!script->threads)
3338*4882a593Smuzhiyun 		return -ENOMEM;
3339*4882a593Smuzhiyun 
3340*4882a593Smuzhiyun 	return set_maps(script);
3341*4882a593Smuzhiyun }
3342*4882a593Smuzhiyun 
3343*4882a593Smuzhiyun static
process_cpu_map_event(struct perf_session * session,union perf_event * event)3344*4882a593Smuzhiyun int process_cpu_map_event(struct perf_session *session,
3345*4882a593Smuzhiyun 			  union perf_event *event)
3346*4882a593Smuzhiyun {
3347*4882a593Smuzhiyun 	struct perf_tool *tool = session->tool;
3348*4882a593Smuzhiyun 	struct perf_script *script = container_of(tool, struct perf_script, tool);
3349*4882a593Smuzhiyun 
3350*4882a593Smuzhiyun 	if (script->cpus) {
3351*4882a593Smuzhiyun 		pr_warning("Extra cpu map event, ignoring.\n");
3352*4882a593Smuzhiyun 		return 0;
3353*4882a593Smuzhiyun 	}
3354*4882a593Smuzhiyun 
3355*4882a593Smuzhiyun 	script->cpus = cpu_map__new_data(&event->cpu_map.data);
3356*4882a593Smuzhiyun 	if (!script->cpus)
3357*4882a593Smuzhiyun 		return -ENOMEM;
3358*4882a593Smuzhiyun 
3359*4882a593Smuzhiyun 	return set_maps(script);
3360*4882a593Smuzhiyun }
3361*4882a593Smuzhiyun 
process_feature_event(struct perf_session * session,union perf_event * event)3362*4882a593Smuzhiyun static int process_feature_event(struct perf_session *session,
3363*4882a593Smuzhiyun 				 union perf_event *event)
3364*4882a593Smuzhiyun {
3365*4882a593Smuzhiyun 	if (event->feat.feat_id < HEADER_LAST_FEATURE)
3366*4882a593Smuzhiyun 		return perf_event__process_feature(session, event);
3367*4882a593Smuzhiyun 	return 0;
3368*4882a593Smuzhiyun }
3369*4882a593Smuzhiyun 
3370*4882a593Smuzhiyun #ifdef HAVE_AUXTRACE_SUPPORT
perf_script__process_auxtrace_info(struct perf_session * session,union perf_event * event)3371*4882a593Smuzhiyun static int perf_script__process_auxtrace_info(struct perf_session *session,
3372*4882a593Smuzhiyun 					      union perf_event *event)
3373*4882a593Smuzhiyun {
3374*4882a593Smuzhiyun 	struct perf_tool *tool = session->tool;
3375*4882a593Smuzhiyun 
3376*4882a593Smuzhiyun 	int ret = perf_event__process_auxtrace_info(session, event);
3377*4882a593Smuzhiyun 
3378*4882a593Smuzhiyun 	if (ret == 0) {
3379*4882a593Smuzhiyun 		struct perf_script *script = container_of(tool, struct perf_script, tool);
3380*4882a593Smuzhiyun 
3381*4882a593Smuzhiyun 		ret = perf_script__setup_per_event_dump(script);
3382*4882a593Smuzhiyun 	}
3383*4882a593Smuzhiyun 
3384*4882a593Smuzhiyun 	return ret;
3385*4882a593Smuzhiyun }
3386*4882a593Smuzhiyun #else
3387*4882a593Smuzhiyun #define perf_script__process_auxtrace_info 0
3388*4882a593Smuzhiyun #endif
3389*4882a593Smuzhiyun 
parse_insn_trace(const struct option * opt __maybe_unused,const char * str __maybe_unused,int unset __maybe_unused)3390*4882a593Smuzhiyun static int parse_insn_trace(const struct option *opt __maybe_unused,
3391*4882a593Smuzhiyun 			    const char *str __maybe_unused,
3392*4882a593Smuzhiyun 			    int unset __maybe_unused)
3393*4882a593Smuzhiyun {
3394*4882a593Smuzhiyun 	parse_output_fields(NULL, "+insn,-event,-period", 0);
3395*4882a593Smuzhiyun 	itrace_parse_synth_opts(opt, "i0ns", 0);
3396*4882a593Smuzhiyun 	symbol_conf.nanosecs = true;
3397*4882a593Smuzhiyun 	return 0;
3398*4882a593Smuzhiyun }
3399*4882a593Smuzhiyun 
parse_xed(const struct option * opt __maybe_unused,const char * str __maybe_unused,int unset __maybe_unused)3400*4882a593Smuzhiyun static int parse_xed(const struct option *opt __maybe_unused,
3401*4882a593Smuzhiyun 		     const char *str __maybe_unused,
3402*4882a593Smuzhiyun 		     int unset __maybe_unused)
3403*4882a593Smuzhiyun {
3404*4882a593Smuzhiyun 	if (isatty(1))
3405*4882a593Smuzhiyun 		force_pager("xed -F insn: -A -64 | less");
3406*4882a593Smuzhiyun 	else
3407*4882a593Smuzhiyun 		force_pager("xed -F insn: -A -64");
3408*4882a593Smuzhiyun 	return 0;
3409*4882a593Smuzhiyun }
3410*4882a593Smuzhiyun 
parse_call_trace(const struct option * opt __maybe_unused,const char * str __maybe_unused,int unset __maybe_unused)3411*4882a593Smuzhiyun static int parse_call_trace(const struct option *opt __maybe_unused,
3412*4882a593Smuzhiyun 			    const char *str __maybe_unused,
3413*4882a593Smuzhiyun 			    int unset __maybe_unused)
3414*4882a593Smuzhiyun {
3415*4882a593Smuzhiyun 	parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3416*4882a593Smuzhiyun 	itrace_parse_synth_opts(opt, "cewp", 0);
3417*4882a593Smuzhiyun 	symbol_conf.nanosecs = true;
3418*4882a593Smuzhiyun 	symbol_conf.pad_output_len_dso = 50;
3419*4882a593Smuzhiyun 	return 0;
3420*4882a593Smuzhiyun }
3421*4882a593Smuzhiyun 
parse_callret_trace(const struct option * opt __maybe_unused,const char * str __maybe_unused,int unset __maybe_unused)3422*4882a593Smuzhiyun static int parse_callret_trace(const struct option *opt __maybe_unused,
3423*4882a593Smuzhiyun 			    const char *str __maybe_unused,
3424*4882a593Smuzhiyun 			    int unset __maybe_unused)
3425*4882a593Smuzhiyun {
3426*4882a593Smuzhiyun 	parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3427*4882a593Smuzhiyun 	itrace_parse_synth_opts(opt, "crewp", 0);
3428*4882a593Smuzhiyun 	symbol_conf.nanosecs = true;
3429*4882a593Smuzhiyun 	return 0;
3430*4882a593Smuzhiyun }
3431*4882a593Smuzhiyun 
cmd_script(int argc,const char ** argv)3432*4882a593Smuzhiyun int cmd_script(int argc, const char **argv)
3433*4882a593Smuzhiyun {
3434*4882a593Smuzhiyun 	bool show_full_info = false;
3435*4882a593Smuzhiyun 	bool header = false;
3436*4882a593Smuzhiyun 	bool header_only = false;
3437*4882a593Smuzhiyun 	bool script_started = false;
3438*4882a593Smuzhiyun 	char *rec_script_path = NULL;
3439*4882a593Smuzhiyun 	char *rep_script_path = NULL;
3440*4882a593Smuzhiyun 	struct perf_session *session;
3441*4882a593Smuzhiyun 	struct itrace_synth_opts itrace_synth_opts = {
3442*4882a593Smuzhiyun 		.set = false,
3443*4882a593Smuzhiyun 		.default_no_sample = true,
3444*4882a593Smuzhiyun 	};
3445*4882a593Smuzhiyun 	struct utsname uts;
3446*4882a593Smuzhiyun 	char *script_path = NULL;
3447*4882a593Smuzhiyun 	const char **__argv;
3448*4882a593Smuzhiyun 	int i, j, err = 0;
3449*4882a593Smuzhiyun 	struct perf_script script = {
3450*4882a593Smuzhiyun 		.tool = {
3451*4882a593Smuzhiyun 			.sample		 = process_sample_event,
3452*4882a593Smuzhiyun 			.mmap		 = perf_event__process_mmap,
3453*4882a593Smuzhiyun 			.mmap2		 = perf_event__process_mmap2,
3454*4882a593Smuzhiyun 			.comm		 = perf_event__process_comm,
3455*4882a593Smuzhiyun 			.namespaces	 = perf_event__process_namespaces,
3456*4882a593Smuzhiyun 			.cgroup		 = perf_event__process_cgroup,
3457*4882a593Smuzhiyun 			.exit		 = perf_event__process_exit,
3458*4882a593Smuzhiyun 			.fork		 = perf_event__process_fork,
3459*4882a593Smuzhiyun 			.attr		 = process_attr,
3460*4882a593Smuzhiyun 			.event_update   = perf_event__process_event_update,
3461*4882a593Smuzhiyun 			.tracing_data	 = perf_event__process_tracing_data,
3462*4882a593Smuzhiyun 			.feature	 = process_feature_event,
3463*4882a593Smuzhiyun 			.build_id	 = perf_event__process_build_id,
3464*4882a593Smuzhiyun 			.id_index	 = perf_event__process_id_index,
3465*4882a593Smuzhiyun 			.auxtrace_info	 = perf_script__process_auxtrace_info,
3466*4882a593Smuzhiyun 			.auxtrace	 = perf_event__process_auxtrace,
3467*4882a593Smuzhiyun 			.auxtrace_error	 = perf_event__process_auxtrace_error,
3468*4882a593Smuzhiyun 			.stat		 = perf_event__process_stat_event,
3469*4882a593Smuzhiyun 			.stat_round	 = process_stat_round_event,
3470*4882a593Smuzhiyun 			.stat_config	 = process_stat_config_event,
3471*4882a593Smuzhiyun 			.thread_map	 = process_thread_map_event,
3472*4882a593Smuzhiyun 			.cpu_map	 = process_cpu_map_event,
3473*4882a593Smuzhiyun 			.ordered_events	 = true,
3474*4882a593Smuzhiyun 			.ordering_requires_timestamps = true,
3475*4882a593Smuzhiyun 		},
3476*4882a593Smuzhiyun 	};
3477*4882a593Smuzhiyun 	struct perf_data data = {
3478*4882a593Smuzhiyun 		.mode = PERF_DATA_MODE_READ,
3479*4882a593Smuzhiyun 	};
3480*4882a593Smuzhiyun 	const struct option options[] = {
3481*4882a593Smuzhiyun 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3482*4882a593Smuzhiyun 		    "dump raw trace in ASCII"),
3483*4882a593Smuzhiyun 	OPT_INCR('v', "verbose", &verbose,
3484*4882a593Smuzhiyun 		 "be more verbose (show symbol address, etc)"),
3485*4882a593Smuzhiyun 	OPT_BOOLEAN('L', "Latency", &latency_format,
3486*4882a593Smuzhiyun 		    "show latency attributes (irqs/preemption disabled, etc)"),
3487*4882a593Smuzhiyun 	OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3488*4882a593Smuzhiyun 			   list_available_scripts),
3489*4882a593Smuzhiyun 	OPT_CALLBACK('s', "script", NULL, "name",
3490*4882a593Smuzhiyun 		     "script file name (lang:script name, script name, or *)",
3491*4882a593Smuzhiyun 		     parse_scriptname),
3492*4882a593Smuzhiyun 	OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3493*4882a593Smuzhiyun 		   "generate perf-script.xx script in specified language"),
3494*4882a593Smuzhiyun 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
3495*4882a593Smuzhiyun 	OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3496*4882a593Smuzhiyun 		   "do various checks like samples ordering and lost events"),
3497*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "header", &header, "Show data header."),
3498*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
3499*4882a593Smuzhiyun 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3500*4882a593Smuzhiyun 		   "file", "vmlinux pathname"),
3501*4882a593Smuzhiyun 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3502*4882a593Smuzhiyun 		   "file", "kallsyms pathname"),
3503*4882a593Smuzhiyun 	OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3504*4882a593Smuzhiyun 		    "When printing symbols do not display call chain"),
3505*4882a593Smuzhiyun 	OPT_CALLBACK(0, "symfs", NULL, "directory",
3506*4882a593Smuzhiyun 		     "Look for files with symbols relative to this directory",
3507*4882a593Smuzhiyun 		     symbol__config_symfs),
3508*4882a593Smuzhiyun 	OPT_CALLBACK('F', "fields", NULL, "str",
3509*4882a593Smuzhiyun 		     "comma separated output fields prepend with 'type:'. "
3510*4882a593Smuzhiyun 		     "+field to add and -field to remove."
3511*4882a593Smuzhiyun 		     "Valid types: hw,sw,trace,raw,synth. "
3512*4882a593Smuzhiyun 		     "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3513*4882a593Smuzhiyun 		     "addr,symoff,srcline,period,iregs,uregs,brstack,"
3514*4882a593Smuzhiyun 		     "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3515*4882a593Smuzhiyun 		     "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc,tod",
3516*4882a593Smuzhiyun 		     parse_output_fields),
3517*4882a593Smuzhiyun 	OPT_BOOLEAN('a', "all-cpus", &system_wide,
3518*4882a593Smuzhiyun 		    "system-wide collection from all CPUs"),
3519*4882a593Smuzhiyun 	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3520*4882a593Smuzhiyun 		   "only consider these symbols"),
3521*4882a593Smuzhiyun 	OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3522*4882a593Smuzhiyun 			"Decode instructions from itrace", parse_insn_trace),
3523*4882a593Smuzhiyun 	OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3524*4882a593Smuzhiyun 			"Run xed disassembler on output", parse_xed),
3525*4882a593Smuzhiyun 	OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3526*4882a593Smuzhiyun 			"Decode calls from from itrace", parse_call_trace),
3527*4882a593Smuzhiyun 	OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3528*4882a593Smuzhiyun 			"Decode calls and returns from itrace", parse_callret_trace),
3529*4882a593Smuzhiyun 	OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3530*4882a593Smuzhiyun 			"Only print symbols and callees with --call-trace/--call-ret-trace"),
3531*4882a593Smuzhiyun 	OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3532*4882a593Smuzhiyun 		   "Stop display of callgraph at these symbols"),
3533*4882a593Smuzhiyun 	OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3534*4882a593Smuzhiyun 	OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3535*4882a593Smuzhiyun 		   "only display events for these comms"),
3536*4882a593Smuzhiyun 	OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3537*4882a593Smuzhiyun 		   "only consider symbols in these pids"),
3538*4882a593Smuzhiyun 	OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3539*4882a593Smuzhiyun 		   "only consider symbols in these tids"),
3540*4882a593Smuzhiyun 	OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3541*4882a593Smuzhiyun 		     "Set the maximum stack depth when parsing the callchain, "
3542*4882a593Smuzhiyun 		     "anything beyond the specified depth will be ignored. "
3543*4882a593Smuzhiyun 		     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3544*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
3545*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
3546*4882a593Smuzhiyun 	OPT_BOOLEAN('I', "show-info", &show_full_info,
3547*4882a593Smuzhiyun 		    "display extended information from perf.data file"),
3548*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3549*4882a593Smuzhiyun 		    "Show the path of [kernel.kallsyms]"),
3550*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3551*4882a593Smuzhiyun 		    "Show the fork/comm/exit events"),
3552*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3553*4882a593Smuzhiyun 		    "Show the mmap events"),
3554*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3555*4882a593Smuzhiyun 		    "Show context switch events (if recorded)"),
3556*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3557*4882a593Smuzhiyun 		    "Show namespace events (if recorded)"),
3558*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
3559*4882a593Smuzhiyun 		    "Show cgroup events (if recorded)"),
3560*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3561*4882a593Smuzhiyun 		    "Show lost events (if recorded)"),
3562*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3563*4882a593Smuzhiyun 		    "Show round events (if recorded)"),
3564*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3565*4882a593Smuzhiyun 		    "Show bpf related events (if recorded)"),
3566*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
3567*4882a593Smuzhiyun 		    "Show text poke related events (if recorded)"),
3568*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3569*4882a593Smuzhiyun 		    "Dump trace output to files named by the monitored events"),
3570*4882a593Smuzhiyun 	OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3571*4882a593Smuzhiyun 	OPT_INTEGER(0, "max-blocks", &max_blocks,
3572*4882a593Smuzhiyun 		    "Maximum number of code blocks to dump with brstackinsn"),
3573*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
3574*4882a593Smuzhiyun 		    "Use 9 decimal places when displaying time"),
3575*4882a593Smuzhiyun 	OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3576*4882a593Smuzhiyun 			    "Instruction Tracing options\n" ITRACE_HELP,
3577*4882a593Smuzhiyun 			    itrace_parse_synth_opts),
3578*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3579*4882a593Smuzhiyun 			"Show full source file name path for source lines"),
3580*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3581*4882a593Smuzhiyun 			"Enable symbol demangling"),
3582*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3583*4882a593Smuzhiyun 			"Enable kernel symbol demangling"),
3584*4882a593Smuzhiyun 	OPT_STRING(0, "time", &script.time_str, "str",
3585*4882a593Smuzhiyun 		   "Time span of interest (start,stop)"),
3586*4882a593Smuzhiyun 	OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3587*4882a593Smuzhiyun 		    "Show inline function"),
3588*4882a593Smuzhiyun 	OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3589*4882a593Smuzhiyun 		   "guest mount directory under which every guest os"
3590*4882a593Smuzhiyun 		   " instance has a subdir"),
3591*4882a593Smuzhiyun 	OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3592*4882a593Smuzhiyun 		   "file", "file saving guest os vmlinux"),
3593*4882a593Smuzhiyun 	OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3594*4882a593Smuzhiyun 		   "file", "file saving guest os /proc/kallsyms"),
3595*4882a593Smuzhiyun 	OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3596*4882a593Smuzhiyun 		   "file", "file saving guest os /proc/modules"),
3597*4882a593Smuzhiyun 	OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
3598*4882a593Smuzhiyun 		    "Enable LBR callgraph stitching approach"),
3599*4882a593Smuzhiyun 	OPTS_EVSWITCH(&script.evswitch),
3600*4882a593Smuzhiyun 	OPT_END()
3601*4882a593Smuzhiyun 	};
3602*4882a593Smuzhiyun 	const char * const script_subcommands[] = { "record", "report", NULL };
3603*4882a593Smuzhiyun 	const char *script_usage[] = {
3604*4882a593Smuzhiyun 		"perf script [<options>]",
3605*4882a593Smuzhiyun 		"perf script [<options>] record <script> [<record-options>] <command>",
3606*4882a593Smuzhiyun 		"perf script [<options>] report <script> [script-args]",
3607*4882a593Smuzhiyun 		"perf script [<options>] <script> [<record-options>] <command>",
3608*4882a593Smuzhiyun 		"perf script [<options>] <top-script> [script-args]",
3609*4882a593Smuzhiyun 		NULL
3610*4882a593Smuzhiyun 	};
3611*4882a593Smuzhiyun 
3612*4882a593Smuzhiyun 	perf_set_singlethreaded();
3613*4882a593Smuzhiyun 
3614*4882a593Smuzhiyun 	setup_scripting();
3615*4882a593Smuzhiyun 
3616*4882a593Smuzhiyun 	argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3617*4882a593Smuzhiyun 			     PARSE_OPT_STOP_AT_NON_OPTION);
3618*4882a593Smuzhiyun 
3619*4882a593Smuzhiyun 	if (symbol_conf.guestmount ||
3620*4882a593Smuzhiyun 	    symbol_conf.default_guest_vmlinux_name ||
3621*4882a593Smuzhiyun 	    symbol_conf.default_guest_kallsyms ||
3622*4882a593Smuzhiyun 	    symbol_conf.default_guest_modules) {
3623*4882a593Smuzhiyun 		/*
3624*4882a593Smuzhiyun 		 * Enable guest sample processing.
3625*4882a593Smuzhiyun 		 */
3626*4882a593Smuzhiyun 		perf_guest = true;
3627*4882a593Smuzhiyun 	}
3628*4882a593Smuzhiyun 
3629*4882a593Smuzhiyun 	data.path  = input_name;
3630*4882a593Smuzhiyun 	data.force = symbol_conf.force;
3631*4882a593Smuzhiyun 
3632*4882a593Smuzhiyun 	if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3633*4882a593Smuzhiyun 		rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3634*4882a593Smuzhiyun 		if (!rec_script_path)
3635*4882a593Smuzhiyun 			return cmd_record(argc, argv);
3636*4882a593Smuzhiyun 	}
3637*4882a593Smuzhiyun 
3638*4882a593Smuzhiyun 	if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3639*4882a593Smuzhiyun 		rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3640*4882a593Smuzhiyun 		if (!rep_script_path) {
3641*4882a593Smuzhiyun 			fprintf(stderr,
3642*4882a593Smuzhiyun 				"Please specify a valid report script"
3643*4882a593Smuzhiyun 				"(see 'perf script -l' for listing)\n");
3644*4882a593Smuzhiyun 			return -1;
3645*4882a593Smuzhiyun 		}
3646*4882a593Smuzhiyun 	}
3647*4882a593Smuzhiyun 
3648*4882a593Smuzhiyun 	if (reltime && deltatime) {
3649*4882a593Smuzhiyun 		fprintf(stderr,
3650*4882a593Smuzhiyun 			"reltime and deltatime - the two don't get along well. "
3651*4882a593Smuzhiyun 			"Please limit to --reltime or --deltatime.\n");
3652*4882a593Smuzhiyun 		return -1;
3653*4882a593Smuzhiyun 	}
3654*4882a593Smuzhiyun 
3655*4882a593Smuzhiyun 	if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
3656*4882a593Smuzhiyun 	    itrace_synth_opts.callchain_sz > scripting_max_stack)
3657*4882a593Smuzhiyun 		scripting_max_stack = itrace_synth_opts.callchain_sz;
3658*4882a593Smuzhiyun 
3659*4882a593Smuzhiyun 	/* make sure PERF_EXEC_PATH is set for scripts */
3660*4882a593Smuzhiyun 	set_argv_exec_path(get_argv_exec_path());
3661*4882a593Smuzhiyun 
3662*4882a593Smuzhiyun 	if (argc && !script_name && !rec_script_path && !rep_script_path) {
3663*4882a593Smuzhiyun 		int live_pipe[2];
3664*4882a593Smuzhiyun 		int rep_args;
3665*4882a593Smuzhiyun 		pid_t pid;
3666*4882a593Smuzhiyun 
3667*4882a593Smuzhiyun 		rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3668*4882a593Smuzhiyun 		rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3669*4882a593Smuzhiyun 
3670*4882a593Smuzhiyun 		if (!rec_script_path && !rep_script_path) {
3671*4882a593Smuzhiyun 			usage_with_options_msg(script_usage, options,
3672*4882a593Smuzhiyun 				"Couldn't find script `%s'\n\n See perf"
3673*4882a593Smuzhiyun 				" script -l for available scripts.\n", argv[0]);
3674*4882a593Smuzhiyun 		}
3675*4882a593Smuzhiyun 
3676*4882a593Smuzhiyun 		if (is_top_script(argv[0])) {
3677*4882a593Smuzhiyun 			rep_args = argc - 1;
3678*4882a593Smuzhiyun 		} else {
3679*4882a593Smuzhiyun 			int rec_args;
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 			rep_args = has_required_arg(rep_script_path);
3682*4882a593Smuzhiyun 			rec_args = (argc - 1) - rep_args;
3683*4882a593Smuzhiyun 			if (rec_args < 0) {
3684*4882a593Smuzhiyun 				usage_with_options_msg(script_usage, options,
3685*4882a593Smuzhiyun 					"`%s' script requires options."
3686*4882a593Smuzhiyun 					"\n\n See perf script -l for available "
3687*4882a593Smuzhiyun 					"scripts and options.\n", argv[0]);
3688*4882a593Smuzhiyun 			}
3689*4882a593Smuzhiyun 		}
3690*4882a593Smuzhiyun 
3691*4882a593Smuzhiyun 		if (pipe(live_pipe) < 0) {
3692*4882a593Smuzhiyun 			perror("failed to create pipe");
3693*4882a593Smuzhiyun 			return -1;
3694*4882a593Smuzhiyun 		}
3695*4882a593Smuzhiyun 
3696*4882a593Smuzhiyun 		pid = fork();
3697*4882a593Smuzhiyun 		if (pid < 0) {
3698*4882a593Smuzhiyun 			perror("failed to fork");
3699*4882a593Smuzhiyun 			return -1;
3700*4882a593Smuzhiyun 		}
3701*4882a593Smuzhiyun 
3702*4882a593Smuzhiyun 		if (!pid) {
3703*4882a593Smuzhiyun 			j = 0;
3704*4882a593Smuzhiyun 
3705*4882a593Smuzhiyun 			dup2(live_pipe[1], 1);
3706*4882a593Smuzhiyun 			close(live_pipe[0]);
3707*4882a593Smuzhiyun 
3708*4882a593Smuzhiyun 			if (is_top_script(argv[0])) {
3709*4882a593Smuzhiyun 				system_wide = true;
3710*4882a593Smuzhiyun 			} else if (!system_wide) {
3711*4882a593Smuzhiyun 				if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3712*4882a593Smuzhiyun 					err = -1;
3713*4882a593Smuzhiyun 					goto out;
3714*4882a593Smuzhiyun 				}
3715*4882a593Smuzhiyun 			}
3716*4882a593Smuzhiyun 
3717*4882a593Smuzhiyun 			__argv = malloc((argc + 6) * sizeof(const char *));
3718*4882a593Smuzhiyun 			if (!__argv) {
3719*4882a593Smuzhiyun 				pr_err("malloc failed\n");
3720*4882a593Smuzhiyun 				err = -ENOMEM;
3721*4882a593Smuzhiyun 				goto out;
3722*4882a593Smuzhiyun 			}
3723*4882a593Smuzhiyun 
3724*4882a593Smuzhiyun 			__argv[j++] = "/bin/sh";
3725*4882a593Smuzhiyun 			__argv[j++] = rec_script_path;
3726*4882a593Smuzhiyun 			if (system_wide)
3727*4882a593Smuzhiyun 				__argv[j++] = "-a";
3728*4882a593Smuzhiyun 			__argv[j++] = "-q";
3729*4882a593Smuzhiyun 			__argv[j++] = "-o";
3730*4882a593Smuzhiyun 			__argv[j++] = "-";
3731*4882a593Smuzhiyun 			for (i = rep_args + 1; i < argc; i++)
3732*4882a593Smuzhiyun 				__argv[j++] = argv[i];
3733*4882a593Smuzhiyun 			__argv[j++] = NULL;
3734*4882a593Smuzhiyun 
3735*4882a593Smuzhiyun 			execvp("/bin/sh", (char **)__argv);
3736*4882a593Smuzhiyun 			free(__argv);
3737*4882a593Smuzhiyun 			exit(-1);
3738*4882a593Smuzhiyun 		}
3739*4882a593Smuzhiyun 
3740*4882a593Smuzhiyun 		dup2(live_pipe[0], 0);
3741*4882a593Smuzhiyun 		close(live_pipe[1]);
3742*4882a593Smuzhiyun 
3743*4882a593Smuzhiyun 		__argv = malloc((argc + 4) * sizeof(const char *));
3744*4882a593Smuzhiyun 		if (!__argv) {
3745*4882a593Smuzhiyun 			pr_err("malloc failed\n");
3746*4882a593Smuzhiyun 			err = -ENOMEM;
3747*4882a593Smuzhiyun 			goto out;
3748*4882a593Smuzhiyun 		}
3749*4882a593Smuzhiyun 
3750*4882a593Smuzhiyun 		j = 0;
3751*4882a593Smuzhiyun 		__argv[j++] = "/bin/sh";
3752*4882a593Smuzhiyun 		__argv[j++] = rep_script_path;
3753*4882a593Smuzhiyun 		for (i = 1; i < rep_args + 1; i++)
3754*4882a593Smuzhiyun 			__argv[j++] = argv[i];
3755*4882a593Smuzhiyun 		__argv[j++] = "-i";
3756*4882a593Smuzhiyun 		__argv[j++] = "-";
3757*4882a593Smuzhiyun 		__argv[j++] = NULL;
3758*4882a593Smuzhiyun 
3759*4882a593Smuzhiyun 		execvp("/bin/sh", (char **)__argv);
3760*4882a593Smuzhiyun 		free(__argv);
3761*4882a593Smuzhiyun 		exit(-1);
3762*4882a593Smuzhiyun 	}
3763*4882a593Smuzhiyun 
3764*4882a593Smuzhiyun 	if (rec_script_path)
3765*4882a593Smuzhiyun 		script_path = rec_script_path;
3766*4882a593Smuzhiyun 	if (rep_script_path)
3767*4882a593Smuzhiyun 		script_path = rep_script_path;
3768*4882a593Smuzhiyun 
3769*4882a593Smuzhiyun 	if (script_path) {
3770*4882a593Smuzhiyun 		j = 0;
3771*4882a593Smuzhiyun 
3772*4882a593Smuzhiyun 		if (!rec_script_path)
3773*4882a593Smuzhiyun 			system_wide = false;
3774*4882a593Smuzhiyun 		else if (!system_wide) {
3775*4882a593Smuzhiyun 			if (have_cmd(argc - 1, &argv[1]) != 0) {
3776*4882a593Smuzhiyun 				err = -1;
3777*4882a593Smuzhiyun 				goto out;
3778*4882a593Smuzhiyun 			}
3779*4882a593Smuzhiyun 		}
3780*4882a593Smuzhiyun 
3781*4882a593Smuzhiyun 		__argv = malloc((argc + 2) * sizeof(const char *));
3782*4882a593Smuzhiyun 		if (!__argv) {
3783*4882a593Smuzhiyun 			pr_err("malloc failed\n");
3784*4882a593Smuzhiyun 			err = -ENOMEM;
3785*4882a593Smuzhiyun 			goto out;
3786*4882a593Smuzhiyun 		}
3787*4882a593Smuzhiyun 
3788*4882a593Smuzhiyun 		__argv[j++] = "/bin/sh";
3789*4882a593Smuzhiyun 		__argv[j++] = script_path;
3790*4882a593Smuzhiyun 		if (system_wide)
3791*4882a593Smuzhiyun 			__argv[j++] = "-a";
3792*4882a593Smuzhiyun 		for (i = 2; i < argc; i++)
3793*4882a593Smuzhiyun 			__argv[j++] = argv[i];
3794*4882a593Smuzhiyun 		__argv[j++] = NULL;
3795*4882a593Smuzhiyun 
3796*4882a593Smuzhiyun 		execvp("/bin/sh", (char **)__argv);
3797*4882a593Smuzhiyun 		free(__argv);
3798*4882a593Smuzhiyun 		exit(-1);
3799*4882a593Smuzhiyun 	}
3800*4882a593Smuzhiyun 
3801*4882a593Smuzhiyun 	if (!script_name) {
3802*4882a593Smuzhiyun 		setup_pager();
3803*4882a593Smuzhiyun 		use_browser = 0;
3804*4882a593Smuzhiyun 	}
3805*4882a593Smuzhiyun 
3806*4882a593Smuzhiyun 	session = perf_session__new(&data, false, &script.tool);
3807*4882a593Smuzhiyun 	if (IS_ERR(session))
3808*4882a593Smuzhiyun 		return PTR_ERR(session);
3809*4882a593Smuzhiyun 
3810*4882a593Smuzhiyun 	if (header || header_only) {
3811*4882a593Smuzhiyun 		script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3812*4882a593Smuzhiyun 		perf_session__fprintf_info(session, stdout, show_full_info);
3813*4882a593Smuzhiyun 		if (header_only)
3814*4882a593Smuzhiyun 			goto out_delete;
3815*4882a593Smuzhiyun 	}
3816*4882a593Smuzhiyun 	if (show_full_info)
3817*4882a593Smuzhiyun 		script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3818*4882a593Smuzhiyun 
3819*4882a593Smuzhiyun 	if (symbol__init(&session->header.env) < 0)
3820*4882a593Smuzhiyun 		goto out_delete;
3821*4882a593Smuzhiyun 
3822*4882a593Smuzhiyun 	uname(&uts);
3823*4882a593Smuzhiyun 	if (data.is_pipe) { /* Assume pipe_mode indicates native_arch */
3824*4882a593Smuzhiyun 		native_arch = true;
3825*4882a593Smuzhiyun 	} else if (session->header.env.arch) {
3826*4882a593Smuzhiyun 		if (!strcmp(uts.machine, session->header.env.arch))
3827*4882a593Smuzhiyun 			native_arch = true;
3828*4882a593Smuzhiyun 		else if (!strcmp(uts.machine, "x86_64") &&
3829*4882a593Smuzhiyun 			 !strcmp(session->header.env.arch, "i386"))
3830*4882a593Smuzhiyun 			native_arch = true;
3831*4882a593Smuzhiyun 	}
3832*4882a593Smuzhiyun 
3833*4882a593Smuzhiyun 	script.session = session;
3834*4882a593Smuzhiyun 	script__setup_sample_type(&script);
3835*4882a593Smuzhiyun 
3836*4882a593Smuzhiyun 	if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3837*4882a593Smuzhiyun 	    symbol_conf.graph_function)
3838*4882a593Smuzhiyun 		itrace_synth_opts.thread_stack = true;
3839*4882a593Smuzhiyun 
3840*4882a593Smuzhiyun 	session->itrace_synth_opts = &itrace_synth_opts;
3841*4882a593Smuzhiyun 
3842*4882a593Smuzhiyun 	if (cpu_list) {
3843*4882a593Smuzhiyun 		err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3844*4882a593Smuzhiyun 		if (err < 0)
3845*4882a593Smuzhiyun 			goto out_delete;
3846*4882a593Smuzhiyun 		itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3847*4882a593Smuzhiyun 	}
3848*4882a593Smuzhiyun 
3849*4882a593Smuzhiyun 	if (!no_callchain)
3850*4882a593Smuzhiyun 		symbol_conf.use_callchain = true;
3851*4882a593Smuzhiyun 	else
3852*4882a593Smuzhiyun 		symbol_conf.use_callchain = false;
3853*4882a593Smuzhiyun 
3854*4882a593Smuzhiyun 	if (session->tevent.pevent &&
3855*4882a593Smuzhiyun 	    tep_set_function_resolver(session->tevent.pevent,
3856*4882a593Smuzhiyun 				      machine__resolve_kernel_addr,
3857*4882a593Smuzhiyun 				      &session->machines.host) < 0) {
3858*4882a593Smuzhiyun 		pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3859*4882a593Smuzhiyun 		err = -1;
3860*4882a593Smuzhiyun 		goto out_delete;
3861*4882a593Smuzhiyun 	}
3862*4882a593Smuzhiyun 
3863*4882a593Smuzhiyun 	if (generate_script_lang) {
3864*4882a593Smuzhiyun 		struct stat perf_stat;
3865*4882a593Smuzhiyun 		int input;
3866*4882a593Smuzhiyun 
3867*4882a593Smuzhiyun 		if (output_set_by_user()) {
3868*4882a593Smuzhiyun 			fprintf(stderr,
3869*4882a593Smuzhiyun 				"custom fields not supported for generated scripts");
3870*4882a593Smuzhiyun 			err = -EINVAL;
3871*4882a593Smuzhiyun 			goto out_delete;
3872*4882a593Smuzhiyun 		}
3873*4882a593Smuzhiyun 
3874*4882a593Smuzhiyun 		input = open(data.path, O_RDONLY);	/* input_name */
3875*4882a593Smuzhiyun 		if (input < 0) {
3876*4882a593Smuzhiyun 			err = -errno;
3877*4882a593Smuzhiyun 			perror("failed to open file");
3878*4882a593Smuzhiyun 			goto out_delete;
3879*4882a593Smuzhiyun 		}
3880*4882a593Smuzhiyun 
3881*4882a593Smuzhiyun 		err = fstat(input, &perf_stat);
3882*4882a593Smuzhiyun 		if (err < 0) {
3883*4882a593Smuzhiyun 			perror("failed to stat file");
3884*4882a593Smuzhiyun 			goto out_delete;
3885*4882a593Smuzhiyun 		}
3886*4882a593Smuzhiyun 
3887*4882a593Smuzhiyun 		if (!perf_stat.st_size) {
3888*4882a593Smuzhiyun 			fprintf(stderr, "zero-sized file, nothing to do!\n");
3889*4882a593Smuzhiyun 			goto out_delete;
3890*4882a593Smuzhiyun 		}
3891*4882a593Smuzhiyun 
3892*4882a593Smuzhiyun 		scripting_ops = script_spec__lookup(generate_script_lang);
3893*4882a593Smuzhiyun 		if (!scripting_ops) {
3894*4882a593Smuzhiyun 			fprintf(stderr, "invalid language specifier");
3895*4882a593Smuzhiyun 			err = -ENOENT;
3896*4882a593Smuzhiyun 			goto out_delete;
3897*4882a593Smuzhiyun 		}
3898*4882a593Smuzhiyun 
3899*4882a593Smuzhiyun 		err = scripting_ops->generate_script(session->tevent.pevent,
3900*4882a593Smuzhiyun 						     "perf-script");
3901*4882a593Smuzhiyun 		goto out_delete;
3902*4882a593Smuzhiyun 	}
3903*4882a593Smuzhiyun 
3904*4882a593Smuzhiyun 	if (script_name) {
3905*4882a593Smuzhiyun 		err = scripting_ops->start_script(script_name, argc, argv);
3906*4882a593Smuzhiyun 		if (err)
3907*4882a593Smuzhiyun 			goto out_delete;
3908*4882a593Smuzhiyun 		pr_debug("perf script started with script %s\n\n", script_name);
3909*4882a593Smuzhiyun 		script_started = true;
3910*4882a593Smuzhiyun 	}
3911*4882a593Smuzhiyun 
3912*4882a593Smuzhiyun 
3913*4882a593Smuzhiyun 	err = perf_session__check_output_opt(session);
3914*4882a593Smuzhiyun 	if (err < 0)
3915*4882a593Smuzhiyun 		goto out_delete;
3916*4882a593Smuzhiyun 
3917*4882a593Smuzhiyun 	if (script.time_str) {
3918*4882a593Smuzhiyun 		err = perf_time__parse_for_ranges_reltime(script.time_str, session,
3919*4882a593Smuzhiyun 						  &script.ptime_range,
3920*4882a593Smuzhiyun 						  &script.range_size,
3921*4882a593Smuzhiyun 						  &script.range_num,
3922*4882a593Smuzhiyun 						  reltime);
3923*4882a593Smuzhiyun 		if (err < 0)
3924*4882a593Smuzhiyun 			goto out_delete;
3925*4882a593Smuzhiyun 
3926*4882a593Smuzhiyun 		itrace_synth_opts__set_time_range(&itrace_synth_opts,
3927*4882a593Smuzhiyun 						  script.ptime_range,
3928*4882a593Smuzhiyun 						  script.range_num);
3929*4882a593Smuzhiyun 	}
3930*4882a593Smuzhiyun 
3931*4882a593Smuzhiyun 	err = evswitch__init(&script.evswitch, session->evlist, stderr);
3932*4882a593Smuzhiyun 	if (err)
3933*4882a593Smuzhiyun 		goto out_delete;
3934*4882a593Smuzhiyun 
3935*4882a593Smuzhiyun 	if (zstd_init(&(session->zstd_data), 0) < 0)
3936*4882a593Smuzhiyun 		pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
3937*4882a593Smuzhiyun 
3938*4882a593Smuzhiyun 	err = __cmd_script(&script);
3939*4882a593Smuzhiyun 
3940*4882a593Smuzhiyun 	flush_scripting();
3941*4882a593Smuzhiyun 
3942*4882a593Smuzhiyun out_delete:
3943*4882a593Smuzhiyun 	if (script.ptime_range) {
3944*4882a593Smuzhiyun 		itrace_synth_opts__clear_time_range(&itrace_synth_opts);
3945*4882a593Smuzhiyun 		zfree(&script.ptime_range);
3946*4882a593Smuzhiyun 	}
3947*4882a593Smuzhiyun 
3948*4882a593Smuzhiyun 	perf_evlist__free_stats(session->evlist);
3949*4882a593Smuzhiyun 	perf_session__delete(session);
3950*4882a593Smuzhiyun 	perf_script__exit(&script);
3951*4882a593Smuzhiyun 
3952*4882a593Smuzhiyun 	if (script_started)
3953*4882a593Smuzhiyun 		cleanup_scripting();
3954*4882a593Smuzhiyun out:
3955*4882a593Smuzhiyun 	return err;
3956*4882a593Smuzhiyun }
3957