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