xref: /OK3568_Linux_fs/kernel/tools/perf/util/event.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #include <errno.h>
2*4882a593Smuzhiyun #include <fcntl.h>
3*4882a593Smuzhiyun #include <inttypes.h>
4*4882a593Smuzhiyun #include <linux/kernel.h>
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun #include <perf/cpumap.h>
7*4882a593Smuzhiyun #include <sys/types.h>
8*4882a593Smuzhiyun #include <sys/stat.h>
9*4882a593Smuzhiyun #include <unistd.h>
10*4882a593Smuzhiyun #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
11*4882a593Smuzhiyun #include <linux/perf_event.h>
12*4882a593Smuzhiyun #include <linux/zalloc.h>
13*4882a593Smuzhiyun #include "cpumap.h"
14*4882a593Smuzhiyun #include "dso.h"
15*4882a593Smuzhiyun #include "event.h"
16*4882a593Smuzhiyun #include "debug.h"
17*4882a593Smuzhiyun #include "hist.h"
18*4882a593Smuzhiyun #include "machine.h"
19*4882a593Smuzhiyun #include "sort.h"
20*4882a593Smuzhiyun #include "string2.h"
21*4882a593Smuzhiyun #include "strlist.h"
22*4882a593Smuzhiyun #include "thread.h"
23*4882a593Smuzhiyun #include "thread_map.h"
24*4882a593Smuzhiyun #include "time-utils.h"
25*4882a593Smuzhiyun #include <linux/ctype.h>
26*4882a593Smuzhiyun #include "map.h"
27*4882a593Smuzhiyun #include "util/namespaces.h"
28*4882a593Smuzhiyun #include "symbol.h"
29*4882a593Smuzhiyun #include "symbol/kallsyms.h"
30*4882a593Smuzhiyun #include "asm/bug.h"
31*4882a593Smuzhiyun #include "stat.h"
32*4882a593Smuzhiyun #include "session.h"
33*4882a593Smuzhiyun #include "bpf-event.h"
34*4882a593Smuzhiyun #include "print_binary.h"
35*4882a593Smuzhiyun #include "tool.h"
36*4882a593Smuzhiyun #include "../perf.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun static const char *perf_event__names[] = {
39*4882a593Smuzhiyun 	[0]					= "TOTAL",
40*4882a593Smuzhiyun 	[PERF_RECORD_MMAP]			= "MMAP",
41*4882a593Smuzhiyun 	[PERF_RECORD_MMAP2]			= "MMAP2",
42*4882a593Smuzhiyun 	[PERF_RECORD_LOST]			= "LOST",
43*4882a593Smuzhiyun 	[PERF_RECORD_COMM]			= "COMM",
44*4882a593Smuzhiyun 	[PERF_RECORD_EXIT]			= "EXIT",
45*4882a593Smuzhiyun 	[PERF_RECORD_THROTTLE]			= "THROTTLE",
46*4882a593Smuzhiyun 	[PERF_RECORD_UNTHROTTLE]		= "UNTHROTTLE",
47*4882a593Smuzhiyun 	[PERF_RECORD_FORK]			= "FORK",
48*4882a593Smuzhiyun 	[PERF_RECORD_READ]			= "READ",
49*4882a593Smuzhiyun 	[PERF_RECORD_SAMPLE]			= "SAMPLE",
50*4882a593Smuzhiyun 	[PERF_RECORD_AUX]			= "AUX",
51*4882a593Smuzhiyun 	[PERF_RECORD_ITRACE_START]		= "ITRACE_START",
52*4882a593Smuzhiyun 	[PERF_RECORD_LOST_SAMPLES]		= "LOST_SAMPLES",
53*4882a593Smuzhiyun 	[PERF_RECORD_SWITCH]			= "SWITCH",
54*4882a593Smuzhiyun 	[PERF_RECORD_SWITCH_CPU_WIDE]		= "SWITCH_CPU_WIDE",
55*4882a593Smuzhiyun 	[PERF_RECORD_NAMESPACES]		= "NAMESPACES",
56*4882a593Smuzhiyun 	[PERF_RECORD_KSYMBOL]			= "KSYMBOL",
57*4882a593Smuzhiyun 	[PERF_RECORD_BPF_EVENT]			= "BPF_EVENT",
58*4882a593Smuzhiyun 	[PERF_RECORD_CGROUP]			= "CGROUP",
59*4882a593Smuzhiyun 	[PERF_RECORD_TEXT_POKE]			= "TEXT_POKE",
60*4882a593Smuzhiyun 	[PERF_RECORD_HEADER_ATTR]		= "ATTR",
61*4882a593Smuzhiyun 	[PERF_RECORD_HEADER_EVENT_TYPE]		= "EVENT_TYPE",
62*4882a593Smuzhiyun 	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
63*4882a593Smuzhiyun 	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
64*4882a593Smuzhiyun 	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
65*4882a593Smuzhiyun 	[PERF_RECORD_ID_INDEX]			= "ID_INDEX",
66*4882a593Smuzhiyun 	[PERF_RECORD_AUXTRACE_INFO]		= "AUXTRACE_INFO",
67*4882a593Smuzhiyun 	[PERF_RECORD_AUXTRACE]			= "AUXTRACE",
68*4882a593Smuzhiyun 	[PERF_RECORD_AUXTRACE_ERROR]		= "AUXTRACE_ERROR",
69*4882a593Smuzhiyun 	[PERF_RECORD_THREAD_MAP]		= "THREAD_MAP",
70*4882a593Smuzhiyun 	[PERF_RECORD_CPU_MAP]			= "CPU_MAP",
71*4882a593Smuzhiyun 	[PERF_RECORD_STAT_CONFIG]		= "STAT_CONFIG",
72*4882a593Smuzhiyun 	[PERF_RECORD_STAT]			= "STAT",
73*4882a593Smuzhiyun 	[PERF_RECORD_STAT_ROUND]		= "STAT_ROUND",
74*4882a593Smuzhiyun 	[PERF_RECORD_EVENT_UPDATE]		= "EVENT_UPDATE",
75*4882a593Smuzhiyun 	[PERF_RECORD_TIME_CONV]			= "TIME_CONV",
76*4882a593Smuzhiyun 	[PERF_RECORD_HEADER_FEATURE]		= "FEATURE",
77*4882a593Smuzhiyun 	[PERF_RECORD_COMPRESSED]		= "COMPRESSED",
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
perf_event__name(unsigned int id)80*4882a593Smuzhiyun const char *perf_event__name(unsigned int id)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	if (id >= ARRAY_SIZE(perf_event__names))
83*4882a593Smuzhiyun 		return "INVALID";
84*4882a593Smuzhiyun 	if (!perf_event__names[id])
85*4882a593Smuzhiyun 		return "UNKNOWN";
86*4882a593Smuzhiyun 	return perf_event__names[id];
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun struct process_symbol_args {
90*4882a593Smuzhiyun 	const char *name;
91*4882a593Smuzhiyun 	u64	   start;
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
find_symbol_cb(void * arg,const char * name,char type,u64 start)94*4882a593Smuzhiyun static int find_symbol_cb(void *arg, const char *name, char type,
95*4882a593Smuzhiyun 			  u64 start)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct process_symbol_args *args = arg;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/*
100*4882a593Smuzhiyun 	 * Must be a function or at least an alias, as in PARISC64, where "_text" is
101*4882a593Smuzhiyun 	 * an 'A' to the same address as "_stext".
102*4882a593Smuzhiyun 	 */
103*4882a593Smuzhiyun 	if (!(kallsyms__is_function(type) ||
104*4882a593Smuzhiyun 	      type == 'A') || strcmp(name, args->name))
105*4882a593Smuzhiyun 		return 0;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	args->start = start;
108*4882a593Smuzhiyun 	return 1;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
kallsyms__get_function_start(const char * kallsyms_filename,const char * symbol_name,u64 * addr)111*4882a593Smuzhiyun int kallsyms__get_function_start(const char *kallsyms_filename,
112*4882a593Smuzhiyun 				 const char *symbol_name, u64 *addr)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	struct process_symbol_args args = { .name = symbol_name, };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
117*4882a593Smuzhiyun 		return -1;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	*addr = args.start;
120*4882a593Smuzhiyun 	return 0;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
perf_event__read_stat_config(struct perf_stat_config * config,struct perf_record_stat_config * event)123*4882a593Smuzhiyun void perf_event__read_stat_config(struct perf_stat_config *config,
124*4882a593Smuzhiyun 				  struct perf_record_stat_config *event)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	unsigned i;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	for (i = 0; i < event->nr; i++) {
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 		switch (event->data[i].tag) {
131*4882a593Smuzhiyun #define CASE(__term, __val)					\
132*4882a593Smuzhiyun 		case PERF_STAT_CONFIG_TERM__##__term:		\
133*4882a593Smuzhiyun 			config->__val = event->data[i].val;	\
134*4882a593Smuzhiyun 			break;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		CASE(AGGR_MODE, aggr_mode)
137*4882a593Smuzhiyun 		CASE(SCALE,     scale)
138*4882a593Smuzhiyun 		CASE(INTERVAL,  interval)
139*4882a593Smuzhiyun #undef CASE
140*4882a593Smuzhiyun 		default:
141*4882a593Smuzhiyun 			pr_warning("unknown stat config term %" PRI_lu64 "\n",
142*4882a593Smuzhiyun 				   event->data[i].tag);
143*4882a593Smuzhiyun 		}
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
perf_event__fprintf_comm(union perf_event * event,FILE * fp)147*4882a593Smuzhiyun size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	const char *s;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
152*4882a593Smuzhiyun 		s = " exec";
153*4882a593Smuzhiyun 	else
154*4882a593Smuzhiyun 		s = "";
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
perf_event__fprintf_namespaces(union perf_event * event,FILE * fp)159*4882a593Smuzhiyun size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	size_t ret = 0;
162*4882a593Smuzhiyun 	struct perf_ns_link_info *ns_link_info;
163*4882a593Smuzhiyun 	u32 nr_namespaces, idx;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ns_link_info = event->namespaces.link_info;
166*4882a593Smuzhiyun 	nr_namespaces = event->namespaces.nr_namespaces;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
169*4882a593Smuzhiyun 		       event->namespaces.pid,
170*4882a593Smuzhiyun 		       event->namespaces.tid,
171*4882a593Smuzhiyun 		       nr_namespaces);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	for (idx = 0; idx < nr_namespaces; idx++) {
174*4882a593Smuzhiyun 		if (idx && (idx % 4 == 0))
175*4882a593Smuzhiyun 			ret += fprintf(fp, "\n\t\t ");
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		ret  += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
178*4882a593Smuzhiyun 				perf_ns__name(idx), (u64)ns_link_info[idx].dev,
179*4882a593Smuzhiyun 				(u64)ns_link_info[idx].ino,
180*4882a593Smuzhiyun 				((idx + 1) != nr_namespaces) ? ", " : "]\n");
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	return ret;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
perf_event__fprintf_cgroup(union perf_event * event,FILE * fp)186*4882a593Smuzhiyun size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	return fprintf(fp, " cgroup: %" PRI_lu64 " %s\n",
189*4882a593Smuzhiyun 		       event->cgroup.id, event->cgroup.path);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
perf_event__process_comm(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)192*4882a593Smuzhiyun int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
193*4882a593Smuzhiyun 			     union perf_event *event,
194*4882a593Smuzhiyun 			     struct perf_sample *sample,
195*4882a593Smuzhiyun 			     struct machine *machine)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	return machine__process_comm_event(machine, event, sample);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
perf_event__process_namespaces(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)200*4882a593Smuzhiyun int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
201*4882a593Smuzhiyun 				   union perf_event *event,
202*4882a593Smuzhiyun 				   struct perf_sample *sample,
203*4882a593Smuzhiyun 				   struct machine *machine)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	return machine__process_namespaces_event(machine, event, sample);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun 
perf_event__process_cgroup(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)208*4882a593Smuzhiyun int perf_event__process_cgroup(struct perf_tool *tool __maybe_unused,
209*4882a593Smuzhiyun 			       union perf_event *event,
210*4882a593Smuzhiyun 			       struct perf_sample *sample,
211*4882a593Smuzhiyun 			       struct machine *machine)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	return machine__process_cgroup_event(machine, event, sample);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
perf_event__process_lost(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)216*4882a593Smuzhiyun int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
217*4882a593Smuzhiyun 			     union perf_event *event,
218*4882a593Smuzhiyun 			     struct perf_sample *sample,
219*4882a593Smuzhiyun 			     struct machine *machine)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	return machine__process_lost_event(machine, event, sample);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
perf_event__process_aux(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)224*4882a593Smuzhiyun int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
225*4882a593Smuzhiyun 			    union perf_event *event,
226*4882a593Smuzhiyun 			    struct perf_sample *sample __maybe_unused,
227*4882a593Smuzhiyun 			    struct machine *machine)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	return machine__process_aux_event(machine, event);
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
perf_event__process_itrace_start(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)232*4882a593Smuzhiyun int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
233*4882a593Smuzhiyun 				     union perf_event *event,
234*4882a593Smuzhiyun 				     struct perf_sample *sample __maybe_unused,
235*4882a593Smuzhiyun 				     struct machine *machine)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun 	return machine__process_itrace_start_event(machine, event);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
perf_event__process_lost_samples(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)240*4882a593Smuzhiyun int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
241*4882a593Smuzhiyun 				     union perf_event *event,
242*4882a593Smuzhiyun 				     struct perf_sample *sample,
243*4882a593Smuzhiyun 				     struct machine *machine)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	return machine__process_lost_samples_event(machine, event, sample);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
perf_event__process_switch(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)248*4882a593Smuzhiyun int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
249*4882a593Smuzhiyun 			       union perf_event *event,
250*4882a593Smuzhiyun 			       struct perf_sample *sample __maybe_unused,
251*4882a593Smuzhiyun 			       struct machine *machine)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	return machine__process_switch_event(machine, event);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
perf_event__process_ksymbol(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)256*4882a593Smuzhiyun int perf_event__process_ksymbol(struct perf_tool *tool __maybe_unused,
257*4882a593Smuzhiyun 				union perf_event *event,
258*4882a593Smuzhiyun 				struct perf_sample *sample __maybe_unused,
259*4882a593Smuzhiyun 				struct machine *machine)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	return machine__process_ksymbol(machine, event, sample);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
perf_event__process_bpf(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)264*4882a593Smuzhiyun int perf_event__process_bpf(struct perf_tool *tool __maybe_unused,
265*4882a593Smuzhiyun 			    union perf_event *event,
266*4882a593Smuzhiyun 			    struct perf_sample *sample,
267*4882a593Smuzhiyun 			    struct machine *machine)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	return machine__process_bpf(machine, event, sample);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
perf_event__process_text_poke(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)272*4882a593Smuzhiyun int perf_event__process_text_poke(struct perf_tool *tool __maybe_unused,
273*4882a593Smuzhiyun 				  union perf_event *event,
274*4882a593Smuzhiyun 				  struct perf_sample *sample,
275*4882a593Smuzhiyun 				  struct machine *machine)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	return machine__process_text_poke(machine, event, sample);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
perf_event__fprintf_mmap(union perf_event * event,FILE * fp)280*4882a593Smuzhiyun size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n",
283*4882a593Smuzhiyun 		       event->mmap.pid, event->mmap.tid, event->mmap.start,
284*4882a593Smuzhiyun 		       event->mmap.len, event->mmap.pgoff,
285*4882a593Smuzhiyun 		       (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
286*4882a593Smuzhiyun 		       event->mmap.filename);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
perf_event__fprintf_mmap2(union perf_event * event,FILE * fp)289*4882a593Smuzhiyun size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
292*4882a593Smuzhiyun 			   " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n",
293*4882a593Smuzhiyun 		       event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
294*4882a593Smuzhiyun 		       event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
295*4882a593Smuzhiyun 		       event->mmap2.min, event->mmap2.ino,
296*4882a593Smuzhiyun 		       event->mmap2.ino_generation,
297*4882a593Smuzhiyun 		       (event->mmap2.prot & PROT_READ) ? 'r' : '-',
298*4882a593Smuzhiyun 		       (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
299*4882a593Smuzhiyun 		       (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
300*4882a593Smuzhiyun 		       (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
301*4882a593Smuzhiyun 		       event->mmap2.filename);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
perf_event__fprintf_thread_map(union perf_event * event,FILE * fp)304*4882a593Smuzhiyun size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	struct perf_thread_map *threads = thread_map__new_event(&event->thread_map);
307*4882a593Smuzhiyun 	size_t ret;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	ret = fprintf(fp, " nr: ");
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (threads)
312*4882a593Smuzhiyun 		ret += thread_map__fprintf(threads, fp);
313*4882a593Smuzhiyun 	else
314*4882a593Smuzhiyun 		ret += fprintf(fp, "failed to get threads from event\n");
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	perf_thread_map__put(threads);
317*4882a593Smuzhiyun 	return ret;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
perf_event__fprintf_cpu_map(union perf_event * event,FILE * fp)320*4882a593Smuzhiyun size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
323*4882a593Smuzhiyun 	size_t ret;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	ret = fprintf(fp, ": ");
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	if (cpus)
328*4882a593Smuzhiyun 		ret += cpu_map__fprintf(cpus, fp);
329*4882a593Smuzhiyun 	else
330*4882a593Smuzhiyun 		ret += fprintf(fp, "failed to get cpumap from event\n");
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	perf_cpu_map__put(cpus);
333*4882a593Smuzhiyun 	return ret;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun 
perf_event__process_mmap(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)336*4882a593Smuzhiyun int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
337*4882a593Smuzhiyun 			     union perf_event *event,
338*4882a593Smuzhiyun 			     struct perf_sample *sample,
339*4882a593Smuzhiyun 			     struct machine *machine)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	return machine__process_mmap_event(machine, event, sample);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
perf_event__process_mmap2(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)344*4882a593Smuzhiyun int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
345*4882a593Smuzhiyun 			     union perf_event *event,
346*4882a593Smuzhiyun 			     struct perf_sample *sample,
347*4882a593Smuzhiyun 			     struct machine *machine)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	return machine__process_mmap2_event(machine, event, sample);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
perf_event__fprintf_task(union perf_event * event,FILE * fp)352*4882a593Smuzhiyun size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	return fprintf(fp, "(%d:%d):(%d:%d)\n",
355*4882a593Smuzhiyun 		       event->fork.pid, event->fork.tid,
356*4882a593Smuzhiyun 		       event->fork.ppid, event->fork.ptid);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
perf_event__process_fork(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)359*4882a593Smuzhiyun int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
360*4882a593Smuzhiyun 			     union perf_event *event,
361*4882a593Smuzhiyun 			     struct perf_sample *sample,
362*4882a593Smuzhiyun 			     struct machine *machine)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	return machine__process_fork_event(machine, event, sample);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
perf_event__process_exit(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)367*4882a593Smuzhiyun int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
368*4882a593Smuzhiyun 			     union perf_event *event,
369*4882a593Smuzhiyun 			     struct perf_sample *sample,
370*4882a593Smuzhiyun 			     struct machine *machine)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	return machine__process_exit_event(machine, event, sample);
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
perf_event__fprintf_aux(union perf_event * event,FILE * fp)375*4882a593Smuzhiyun size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s]\n",
378*4882a593Smuzhiyun 		       event->aux.aux_offset, event->aux.aux_size,
379*4882a593Smuzhiyun 		       event->aux.flags,
380*4882a593Smuzhiyun 		       event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
381*4882a593Smuzhiyun 		       event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
382*4882a593Smuzhiyun 		       event->aux.flags & PERF_AUX_FLAG_PARTIAL   ? "P" : "");
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun 
perf_event__fprintf_itrace_start(union perf_event * event,FILE * fp)385*4882a593Smuzhiyun size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun 	return fprintf(fp, " pid: %u tid: %u\n",
388*4882a593Smuzhiyun 		       event->itrace_start.pid, event->itrace_start.tid);
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
perf_event__fprintf_switch(union perf_event * event,FILE * fp)391*4882a593Smuzhiyun size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
394*4882a593Smuzhiyun 	const char *in_out = !out ? "IN         " :
395*4882a593Smuzhiyun 		!(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ?
396*4882a593Smuzhiyun 				    "OUT        " : "OUT preempt";
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_SWITCH)
399*4882a593Smuzhiyun 		return fprintf(fp, " %s\n", in_out);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return fprintf(fp, " %s  %s pid/tid: %5d/%-5d\n",
402*4882a593Smuzhiyun 		       in_out, out ? "next" : "prev",
403*4882a593Smuzhiyun 		       event->context_switch.next_prev_pid,
404*4882a593Smuzhiyun 		       event->context_switch.next_prev_tid);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
perf_event__fprintf_lost(union perf_event * event,FILE * fp)407*4882a593Smuzhiyun static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
perf_event__fprintf_ksymbol(union perf_event * event,FILE * fp)412*4882a593Smuzhiyun size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n",
415*4882a593Smuzhiyun 		       event->ksymbol.addr, event->ksymbol.len,
416*4882a593Smuzhiyun 		       event->ksymbol.ksym_type,
417*4882a593Smuzhiyun 		       event->ksymbol.flags, event->ksymbol.name);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
perf_event__fprintf_bpf(union perf_event * event,FILE * fp)420*4882a593Smuzhiyun size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	return fprintf(fp, " type %u, flags %u, id %u\n",
423*4882a593Smuzhiyun 		       event->bpf.type, event->bpf.flags, event->bpf.id);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun 
text_poke_printer(enum binary_printer_ops op,unsigned int val,void * extra,FILE * fp)426*4882a593Smuzhiyun static int text_poke_printer(enum binary_printer_ops op, unsigned int val,
427*4882a593Smuzhiyun 			     void *extra, FILE *fp)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	bool old = *(bool *)extra;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	switch ((int)op) {
432*4882a593Smuzhiyun 	case BINARY_PRINT_LINE_BEGIN:
433*4882a593Smuzhiyun 		return fprintf(fp, "            %s bytes:", old ? "Old" : "New");
434*4882a593Smuzhiyun 	case BINARY_PRINT_NUM_DATA:
435*4882a593Smuzhiyun 		return fprintf(fp, " %02x", val);
436*4882a593Smuzhiyun 	case BINARY_PRINT_LINE_END:
437*4882a593Smuzhiyun 		return fprintf(fp, "\n");
438*4882a593Smuzhiyun 	default:
439*4882a593Smuzhiyun 		return 0;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
perf_event__fprintf_text_poke(union perf_event * event,struct machine * machine,FILE * fp)443*4882a593Smuzhiyun size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine, FILE *fp)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct perf_record_text_poke_event *tp = &event->text_poke;
446*4882a593Smuzhiyun 	size_t ret;
447*4882a593Smuzhiyun 	bool old;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	ret = fprintf(fp, " %" PRI_lx64 " ", tp->addr);
450*4882a593Smuzhiyun 	if (machine) {
451*4882a593Smuzhiyun 		struct addr_location al;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 		al.map = maps__find(&machine->kmaps, tp->addr);
454*4882a593Smuzhiyun 		if (al.map && map__load(al.map) >= 0) {
455*4882a593Smuzhiyun 			al.addr = al.map->map_ip(al.map, tp->addr);
456*4882a593Smuzhiyun 			al.sym = map__find_symbol(al.map, al.addr);
457*4882a593Smuzhiyun 			if (al.sym)
458*4882a593Smuzhiyun 				ret += symbol__fprintf_symname_offs(al.sym, &al, fp);
459*4882a593Smuzhiyun 		}
460*4882a593Smuzhiyun 	}
461*4882a593Smuzhiyun 	ret += fprintf(fp, " old len %u new len %u\n", tp->old_len, tp->new_len);
462*4882a593Smuzhiyun 	old = true;
463*4882a593Smuzhiyun 	ret += binary__fprintf(tp->bytes, tp->old_len, 16, text_poke_printer,
464*4882a593Smuzhiyun 			       &old, fp);
465*4882a593Smuzhiyun 	old = false;
466*4882a593Smuzhiyun 	ret += binary__fprintf(tp->bytes + tp->old_len, tp->new_len, 16,
467*4882a593Smuzhiyun 			       text_poke_printer, &old, fp);
468*4882a593Smuzhiyun 	return ret;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
perf_event__fprintf(union perf_event * event,struct machine * machine,FILE * fp)471*4882a593Smuzhiyun size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	size_t ret = fprintf(fp, "PERF_RECORD_%s",
474*4882a593Smuzhiyun 			     perf_event__name(event->header.type));
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	switch (event->header.type) {
477*4882a593Smuzhiyun 	case PERF_RECORD_COMM:
478*4882a593Smuzhiyun 		ret += perf_event__fprintf_comm(event, fp);
479*4882a593Smuzhiyun 		break;
480*4882a593Smuzhiyun 	case PERF_RECORD_FORK:
481*4882a593Smuzhiyun 	case PERF_RECORD_EXIT:
482*4882a593Smuzhiyun 		ret += perf_event__fprintf_task(event, fp);
483*4882a593Smuzhiyun 		break;
484*4882a593Smuzhiyun 	case PERF_RECORD_MMAP:
485*4882a593Smuzhiyun 		ret += perf_event__fprintf_mmap(event, fp);
486*4882a593Smuzhiyun 		break;
487*4882a593Smuzhiyun 	case PERF_RECORD_NAMESPACES:
488*4882a593Smuzhiyun 		ret += perf_event__fprintf_namespaces(event, fp);
489*4882a593Smuzhiyun 		break;
490*4882a593Smuzhiyun 	case PERF_RECORD_CGROUP:
491*4882a593Smuzhiyun 		ret += perf_event__fprintf_cgroup(event, fp);
492*4882a593Smuzhiyun 		break;
493*4882a593Smuzhiyun 	case PERF_RECORD_MMAP2:
494*4882a593Smuzhiyun 		ret += perf_event__fprintf_mmap2(event, fp);
495*4882a593Smuzhiyun 		break;
496*4882a593Smuzhiyun 	case PERF_RECORD_AUX:
497*4882a593Smuzhiyun 		ret += perf_event__fprintf_aux(event, fp);
498*4882a593Smuzhiyun 		break;
499*4882a593Smuzhiyun 	case PERF_RECORD_ITRACE_START:
500*4882a593Smuzhiyun 		ret += perf_event__fprintf_itrace_start(event, fp);
501*4882a593Smuzhiyun 		break;
502*4882a593Smuzhiyun 	case PERF_RECORD_SWITCH:
503*4882a593Smuzhiyun 	case PERF_RECORD_SWITCH_CPU_WIDE:
504*4882a593Smuzhiyun 		ret += perf_event__fprintf_switch(event, fp);
505*4882a593Smuzhiyun 		break;
506*4882a593Smuzhiyun 	case PERF_RECORD_LOST:
507*4882a593Smuzhiyun 		ret += perf_event__fprintf_lost(event, fp);
508*4882a593Smuzhiyun 		break;
509*4882a593Smuzhiyun 	case PERF_RECORD_KSYMBOL:
510*4882a593Smuzhiyun 		ret += perf_event__fprintf_ksymbol(event, fp);
511*4882a593Smuzhiyun 		break;
512*4882a593Smuzhiyun 	case PERF_RECORD_BPF_EVENT:
513*4882a593Smuzhiyun 		ret += perf_event__fprintf_bpf(event, fp);
514*4882a593Smuzhiyun 		break;
515*4882a593Smuzhiyun 	case PERF_RECORD_TEXT_POKE:
516*4882a593Smuzhiyun 		ret += perf_event__fprintf_text_poke(event, machine, fp);
517*4882a593Smuzhiyun 		break;
518*4882a593Smuzhiyun 	default:
519*4882a593Smuzhiyun 		ret += fprintf(fp, "\n");
520*4882a593Smuzhiyun 	}
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	return ret;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
perf_event__process(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)525*4882a593Smuzhiyun int perf_event__process(struct perf_tool *tool __maybe_unused,
526*4882a593Smuzhiyun 			union perf_event *event,
527*4882a593Smuzhiyun 			struct perf_sample *sample,
528*4882a593Smuzhiyun 			struct machine *machine)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	return machine__process_event(machine, event, sample);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
thread__find_map(struct thread * thread,u8 cpumode,u64 addr,struct addr_location * al)533*4882a593Smuzhiyun struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
534*4882a593Smuzhiyun 			     struct addr_location *al)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	struct maps *maps = thread->maps;
537*4882a593Smuzhiyun 	struct machine *machine = maps->machine;
538*4882a593Smuzhiyun 	bool load_map = false;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	al->maps = maps;
541*4882a593Smuzhiyun 	al->thread = thread;
542*4882a593Smuzhiyun 	al->addr = addr;
543*4882a593Smuzhiyun 	al->cpumode = cpumode;
544*4882a593Smuzhiyun 	al->filtered = 0;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	if (machine == NULL) {
547*4882a593Smuzhiyun 		al->map = NULL;
548*4882a593Smuzhiyun 		return NULL;
549*4882a593Smuzhiyun 	}
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
552*4882a593Smuzhiyun 		al->level = 'k';
553*4882a593Smuzhiyun 		al->maps = maps = &machine->kmaps;
554*4882a593Smuzhiyun 		load_map = true;
555*4882a593Smuzhiyun 	} else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
556*4882a593Smuzhiyun 		al->level = '.';
557*4882a593Smuzhiyun 	} else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
558*4882a593Smuzhiyun 		al->level = 'g';
559*4882a593Smuzhiyun 		al->maps = maps = &machine->kmaps;
560*4882a593Smuzhiyun 		load_map = true;
561*4882a593Smuzhiyun 	} else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
562*4882a593Smuzhiyun 		al->level = 'u';
563*4882a593Smuzhiyun 	} else {
564*4882a593Smuzhiyun 		al->level = 'H';
565*4882a593Smuzhiyun 		al->map = NULL;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 		if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
568*4882a593Smuzhiyun 			cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
569*4882a593Smuzhiyun 			!perf_guest)
570*4882a593Smuzhiyun 			al->filtered |= (1 << HIST_FILTER__GUEST);
571*4882a593Smuzhiyun 		if ((cpumode == PERF_RECORD_MISC_USER ||
572*4882a593Smuzhiyun 			cpumode == PERF_RECORD_MISC_KERNEL) &&
573*4882a593Smuzhiyun 			!perf_host)
574*4882a593Smuzhiyun 			al->filtered |= (1 << HIST_FILTER__HOST);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 		return NULL;
577*4882a593Smuzhiyun 	}
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	al->map = maps__find(maps, al->addr);
580*4882a593Smuzhiyun 	if (al->map != NULL) {
581*4882a593Smuzhiyun 		/*
582*4882a593Smuzhiyun 		 * Kernel maps might be changed when loading symbols so loading
583*4882a593Smuzhiyun 		 * must be done prior to using kernel maps.
584*4882a593Smuzhiyun 		 */
585*4882a593Smuzhiyun 		if (load_map)
586*4882a593Smuzhiyun 			map__load(al->map);
587*4882a593Smuzhiyun 		al->addr = al->map->map_ip(al->map, al->addr);
588*4882a593Smuzhiyun 	}
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	return al->map;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun /*
594*4882a593Smuzhiyun  * For branch stacks or branch samples, the sample cpumode might not be correct
595*4882a593Smuzhiyun  * because it applies only to the sample 'ip' and not necessary to 'addr' or
596*4882a593Smuzhiyun  * branch stack addresses. If possible, use a fallback to deal with those cases.
597*4882a593Smuzhiyun  */
thread__find_map_fb(struct thread * thread,u8 cpumode,u64 addr,struct addr_location * al)598*4882a593Smuzhiyun struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
599*4882a593Smuzhiyun 				struct addr_location *al)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun 	struct map *map = thread__find_map(thread, cpumode, addr, al);
602*4882a593Smuzhiyun 	struct machine *machine = thread->maps->machine;
603*4882a593Smuzhiyun 	u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	if (map || addr_cpumode == cpumode)
606*4882a593Smuzhiyun 		return map;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return thread__find_map(thread, addr_cpumode, addr, al);
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
thread__find_symbol(struct thread * thread,u8 cpumode,u64 addr,struct addr_location * al)611*4882a593Smuzhiyun struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
612*4882a593Smuzhiyun 				   u64 addr, struct addr_location *al)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun 	al->sym = NULL;
615*4882a593Smuzhiyun 	if (thread__find_map(thread, cpumode, addr, al))
616*4882a593Smuzhiyun 		al->sym = map__find_symbol(al->map, al->addr);
617*4882a593Smuzhiyun 	return al->sym;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
thread__find_symbol_fb(struct thread * thread,u8 cpumode,u64 addr,struct addr_location * al)620*4882a593Smuzhiyun struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
621*4882a593Smuzhiyun 				      u64 addr, struct addr_location *al)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun 	al->sym = NULL;
624*4882a593Smuzhiyun 	if (thread__find_map_fb(thread, cpumode, addr, al))
625*4882a593Smuzhiyun 		al->sym = map__find_symbol(al->map, al->addr);
626*4882a593Smuzhiyun 	return al->sym;
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun /*
630*4882a593Smuzhiyun  * Callers need to drop the reference to al->thread, obtained in
631*4882a593Smuzhiyun  * machine__findnew_thread()
632*4882a593Smuzhiyun  */
machine__resolve(struct machine * machine,struct addr_location * al,struct perf_sample * sample)633*4882a593Smuzhiyun int machine__resolve(struct machine *machine, struct addr_location *al,
634*4882a593Smuzhiyun 		     struct perf_sample *sample)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun 	struct thread *thread = machine__findnew_thread(machine, sample->pid,
637*4882a593Smuzhiyun 							sample->tid);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	if (thread == NULL)
640*4882a593Smuzhiyun 		return -1;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
643*4882a593Smuzhiyun 	thread__find_map(thread, sample->cpumode, sample->ip, al);
644*4882a593Smuzhiyun 	dump_printf(" ...... dso: %s\n",
645*4882a593Smuzhiyun 		    al->map ? al->map->dso->long_name :
646*4882a593Smuzhiyun 			al->level == 'H' ? "[hypervisor]" : "<not found>");
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	if (thread__is_filtered(thread))
649*4882a593Smuzhiyun 		al->filtered |= (1 << HIST_FILTER__THREAD);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	al->sym = NULL;
652*4882a593Smuzhiyun 	al->cpu = sample->cpu;
653*4882a593Smuzhiyun 	al->socket = -1;
654*4882a593Smuzhiyun 	al->srcline = NULL;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (al->cpu >= 0) {
657*4882a593Smuzhiyun 		struct perf_env *env = machine->env;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 		if (env && env->cpu)
660*4882a593Smuzhiyun 			al->socket = env->cpu[al->cpu].socket_id;
661*4882a593Smuzhiyun 	}
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (al->map) {
664*4882a593Smuzhiyun 		struct dso *dso = al->map->dso;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 		if (symbol_conf.dso_list &&
667*4882a593Smuzhiyun 		    (!dso || !(strlist__has_entry(symbol_conf.dso_list,
668*4882a593Smuzhiyun 						  dso->short_name) ||
669*4882a593Smuzhiyun 			       (dso->short_name != dso->long_name &&
670*4882a593Smuzhiyun 				strlist__has_entry(symbol_conf.dso_list,
671*4882a593Smuzhiyun 						   dso->long_name))))) {
672*4882a593Smuzhiyun 			al->filtered |= (1 << HIST_FILTER__DSO);
673*4882a593Smuzhiyun 		}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 		al->sym = map__find_symbol(al->map, al->addr);
676*4882a593Smuzhiyun 	} else if (symbol_conf.dso_list) {
677*4882a593Smuzhiyun 		al->filtered |= (1 << HIST_FILTER__DSO);
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (symbol_conf.sym_list) {
681*4882a593Smuzhiyun 		int ret = 0;
682*4882a593Smuzhiyun 		char al_addr_str[32];
683*4882a593Smuzhiyun 		size_t sz = sizeof(al_addr_str);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 		if (al->sym) {
686*4882a593Smuzhiyun 			ret = strlist__has_entry(symbol_conf.sym_list,
687*4882a593Smuzhiyun 						al->sym->name);
688*4882a593Smuzhiyun 		}
689*4882a593Smuzhiyun 		if (!ret && al->sym) {
690*4882a593Smuzhiyun 			snprintf(al_addr_str, sz, "0x%"PRIx64,
691*4882a593Smuzhiyun 				al->map->unmap_ip(al->map, al->sym->start));
692*4882a593Smuzhiyun 			ret = strlist__has_entry(symbol_conf.sym_list,
693*4882a593Smuzhiyun 						al_addr_str);
694*4882a593Smuzhiyun 		}
695*4882a593Smuzhiyun 		if (!ret)
696*4882a593Smuzhiyun 			al->filtered |= (1 << HIST_FILTER__SYMBOL);
697*4882a593Smuzhiyun 	}
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	return 0;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun /*
703*4882a593Smuzhiyun  * The preprocess_sample method will return with reference counts for the
704*4882a593Smuzhiyun  * in it, when done using (and perhaps getting ref counts if needing to
705*4882a593Smuzhiyun  * keep a pointer to one of those entries) it must be paired with
706*4882a593Smuzhiyun  * addr_location__put(), so that the refcounts can be decremented.
707*4882a593Smuzhiyun  */
addr_location__put(struct addr_location * al)708*4882a593Smuzhiyun void addr_location__put(struct addr_location *al)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun 	thread__zput(al->thread);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
is_bts_event(struct perf_event_attr * attr)713*4882a593Smuzhiyun bool is_bts_event(struct perf_event_attr *attr)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun 	return attr->type == PERF_TYPE_HARDWARE &&
716*4882a593Smuzhiyun 	       (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
717*4882a593Smuzhiyun 	       attr->sample_period == 1;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
sample_addr_correlates_sym(struct perf_event_attr * attr)720*4882a593Smuzhiyun bool sample_addr_correlates_sym(struct perf_event_attr *attr)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	if (attr->type == PERF_TYPE_SOFTWARE &&
723*4882a593Smuzhiyun 	    (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
724*4882a593Smuzhiyun 	     attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
725*4882a593Smuzhiyun 	     attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
726*4882a593Smuzhiyun 		return true;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (is_bts_event(attr))
729*4882a593Smuzhiyun 		return true;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	return false;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun 
thread__resolve(struct thread * thread,struct addr_location * al,struct perf_sample * sample)734*4882a593Smuzhiyun void thread__resolve(struct thread *thread, struct addr_location *al,
735*4882a593Smuzhiyun 		     struct perf_sample *sample)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun 	thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	al->cpu = sample->cpu;
740*4882a593Smuzhiyun 	al->sym = NULL;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	if (al->map)
743*4882a593Smuzhiyun 		al->sym = map__find_symbol(al->map, al->addr);
744*4882a593Smuzhiyun }
745