4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <perf/cpumap.h>
10 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
11 #include <linux/perf_event.h>
12 #include <linux/zalloc.h>
23 #include "thread_map.h"
24 #include "time-utils.h"
25 #include <linux/ctype.h>
27 #include "util/namespaces.h"
29 #include "symbol/kallsyms.h"
33 #include "bpf-event.h"
37 static const char *perf_event__names[] = {
39 [PERF_RECORD_MMAP] = "MMAP",
40 [PERF_RECORD_MMAP2] = "MMAP2",
41 [PERF_RECORD_LOST] = "LOST",
42 [PERF_RECORD_COMM] = "COMM",
43 [PERF_RECORD_EXIT] = "EXIT",
44 [PERF_RECORD_THROTTLE] = "THROTTLE",
45 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
46 [PERF_RECORD_FORK] = "FORK",
47 [PERF_RECORD_READ] = "READ",
48 [PERF_RECORD_SAMPLE] = "SAMPLE",
49 [PERF_RECORD_AUX] = "AUX",
50 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
51 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
52 [PERF_RECORD_SWITCH] = "SWITCH",
53 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
54 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
55 [PERF_RECORD_KSYMBOL] = "KSYMBOL",
56 [PERF_RECORD_BPF_EVENT] = "BPF_EVENT",
57 [PERF_RECORD_CGROUP] = "CGROUP",
58 [PERF_RECORD_HEADER_ATTR] = "ATTR",
59 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
60 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
61 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
62 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
63 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
64 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
65 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
66 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
67 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
68 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
69 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
70 [PERF_RECORD_STAT] = "STAT",
71 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
72 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
73 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
74 [PERF_RECORD_HEADER_FEATURE] = "FEATURE",
75 [PERF_RECORD_COMPRESSED] = "COMPRESSED",
78 const char *perf_event__name(unsigned int id)
80 if (id >= ARRAY_SIZE(perf_event__names))
82 if (!perf_event__names[id])
84 return perf_event__names[id];
87 struct process_symbol_args {
92 static int find_symbol_cb(void *arg, const char *name, char type,
95 struct process_symbol_args *args = arg;
98 * Must be a function or at least an alias, as in PARISC64, where "_text" is
99 * an 'A' to the same address as "_stext".
101 if (!(kallsyms__is_function(type) ||
102 type == 'A') || strcmp(name, args->name))
109 int kallsyms__get_function_start(const char *kallsyms_filename,
110 const char *symbol_name, u64 *addr)
112 struct process_symbol_args args = { .name = symbol_name, };
114 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
121 void perf_event__read_stat_config(struct perf_stat_config *config,
122 struct perf_record_stat_config *event)
126 for (i = 0; i < event->nr; i++) {
128 switch (event->data[i].tag) {
129 #define CASE(__term, __val) \
130 case PERF_STAT_CONFIG_TERM__##__term: \
131 config->__val = event->data[i].val; \
134 CASE(AGGR_MODE, aggr_mode)
136 CASE(INTERVAL, interval)
139 pr_warning("unknown stat config term %" PRI_lu64 "\n",
145 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
149 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
154 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
157 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
160 struct perf_ns_link_info *ns_link_info;
161 u32 nr_namespaces, idx;
163 ns_link_info = event->namespaces.link_info;
164 nr_namespaces = event->namespaces.nr_namespaces;
166 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
167 event->namespaces.pid,
168 event->namespaces.tid,
171 for (idx = 0; idx < nr_namespaces; idx++) {
172 if (idx && (idx % 4 == 0))
173 ret += fprintf(fp, "\n\t\t ");
175 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
176 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
177 (u64)ns_link_info[idx].ino,
178 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
184 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp)
186 return fprintf(fp, " cgroup: %" PRI_lu64 " %s\n",
187 event->cgroup.id, event->cgroup.path);
190 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
191 union perf_event *event,
192 struct perf_sample *sample,
193 struct machine *machine)
195 return machine__process_comm_event(machine, event, sample);
198 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
199 union perf_event *event,
200 struct perf_sample *sample,
201 struct machine *machine)
203 return machine__process_namespaces_event(machine, event, sample);
206 int perf_event__process_cgroup(struct perf_tool *tool __maybe_unused,
207 union perf_event *event,
208 struct perf_sample *sample,
209 struct machine *machine)
211 return machine__process_cgroup_event(machine, event, sample);
214 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
215 union perf_event *event,
216 struct perf_sample *sample,
217 struct machine *machine)
219 return machine__process_lost_event(machine, event, sample);
222 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
223 union perf_event *event,
224 struct perf_sample *sample __maybe_unused,
225 struct machine *machine)
227 return machine__process_aux_event(machine, event);
230 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
231 union perf_event *event,
232 struct perf_sample *sample __maybe_unused,
233 struct machine *machine)
235 return machine__process_itrace_start_event(machine, event);
238 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
239 union perf_event *event,
240 struct perf_sample *sample,
241 struct machine *machine)
243 return machine__process_lost_samples_event(machine, event, sample);
246 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
247 union perf_event *event,
248 struct perf_sample *sample __maybe_unused,
249 struct machine *machine)
251 return machine__process_switch_event(machine, event);
254 int perf_event__process_ksymbol(struct perf_tool *tool __maybe_unused,
255 union perf_event *event,
256 struct perf_sample *sample __maybe_unused,
257 struct machine *machine)
259 return machine__process_ksymbol(machine, event, sample);
262 int perf_event__process_bpf(struct perf_tool *tool __maybe_unused,
263 union perf_event *event,
264 struct perf_sample *sample,
265 struct machine *machine)
267 return machine__process_bpf(machine, event, sample);
270 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
272 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n",
273 event->mmap.pid, event->mmap.tid, event->mmap.start,
274 event->mmap.len, event->mmap.pgoff,
275 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
276 event->mmap.filename);
279 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
281 return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64
282 " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n",
283 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
284 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
285 event->mmap2.min, event->mmap2.ino,
286 event->mmap2.ino_generation,
287 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
288 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
289 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
290 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
291 event->mmap2.filename);
294 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
296 struct perf_thread_map *threads = thread_map__new_event(&event->thread_map);
299 ret = fprintf(fp, " nr: ");
302 ret += thread_map__fprintf(threads, fp);
304 ret += fprintf(fp, "failed to get threads from event\n");
306 perf_thread_map__put(threads);
310 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
312 struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
315 ret = fprintf(fp, ": ");
318 ret += cpu_map__fprintf(cpus, fp);
320 ret += fprintf(fp, "failed to get cpumap from event\n");
322 perf_cpu_map__put(cpus);
326 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
327 union perf_event *event,
328 struct perf_sample *sample,
329 struct machine *machine)
331 return machine__process_mmap_event(machine, event, sample);
334 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
335 union perf_event *event,
336 struct perf_sample *sample,
337 struct machine *machine)
339 return machine__process_mmap2_event(machine, event, sample);
342 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
344 return fprintf(fp, "(%d:%d):(%d:%d)\n",
345 event->fork.pid, event->fork.tid,
346 event->fork.ppid, event->fork.ptid);
349 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
350 union perf_event *event,
351 struct perf_sample *sample,
352 struct machine *machine)
354 return machine__process_fork_event(machine, event, sample);
357 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
358 union perf_event *event,
359 struct perf_sample *sample,
360 struct machine *machine)
362 return machine__process_exit_event(machine, event, sample);
365 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
367 return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s]\n",
368 event->aux.aux_offset, event->aux.aux_size,
370 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
371 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
372 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
375 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
377 return fprintf(fp, " pid: %u tid: %u\n",
378 event->itrace_start.pid, event->itrace_start.tid);
381 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
383 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
384 const char *in_out = !out ? "IN " :
385 !(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ?
386 "OUT " : "OUT preempt";
388 if (event->header.type == PERF_RECORD_SWITCH)
389 return fprintf(fp, " %s\n", in_out);
391 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
392 in_out, out ? "next" : "prev",
393 event->context_switch.next_prev_pid,
394 event->context_switch.next_prev_tid);
397 static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp)
399 return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost);
402 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
404 return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n",
405 event->ksymbol.addr, event->ksymbol.len,
406 event->ksymbol.ksym_type,
407 event->ksymbol.flags, event->ksymbol.name);
410 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
412 return fprintf(fp, " type %u, flags %u, id %u\n",
413 event->bpf.type, event->bpf.flags, event->bpf.id);
416 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
418 size_t ret = fprintf(fp, "PERF_RECORD_%s",
419 perf_event__name(event->header.type));
421 switch (event->header.type) {
422 case PERF_RECORD_COMM:
423 ret += perf_event__fprintf_comm(event, fp);
425 case PERF_RECORD_FORK:
426 case PERF_RECORD_EXIT:
427 ret += perf_event__fprintf_task(event, fp);
429 case PERF_RECORD_MMAP:
430 ret += perf_event__fprintf_mmap(event, fp);
432 case PERF_RECORD_NAMESPACES:
433 ret += perf_event__fprintf_namespaces(event, fp);
435 case PERF_RECORD_CGROUP:
436 ret += perf_event__fprintf_cgroup(event, fp);
438 case PERF_RECORD_MMAP2:
439 ret += perf_event__fprintf_mmap2(event, fp);
441 case PERF_RECORD_AUX:
442 ret += perf_event__fprintf_aux(event, fp);
444 case PERF_RECORD_ITRACE_START:
445 ret += perf_event__fprintf_itrace_start(event, fp);
447 case PERF_RECORD_SWITCH:
448 case PERF_RECORD_SWITCH_CPU_WIDE:
449 ret += perf_event__fprintf_switch(event, fp);
451 case PERF_RECORD_LOST:
452 ret += perf_event__fprintf_lost(event, fp);
454 case PERF_RECORD_KSYMBOL:
455 ret += perf_event__fprintf_ksymbol(event, fp);
457 case PERF_RECORD_BPF_EVENT:
458 ret += perf_event__fprintf_bpf(event, fp);
461 ret += fprintf(fp, "\n");
467 int perf_event__process(struct perf_tool *tool __maybe_unused,
468 union perf_event *event,
469 struct perf_sample *sample,
470 struct machine *machine)
472 return machine__process_event(machine, event, sample);
475 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
476 struct addr_location *al)
478 struct maps *maps = thread->maps;
479 struct machine *machine = maps->machine;
480 bool load_map = false;
485 al->cpumode = cpumode;
488 if (machine == NULL) {
493 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
495 al->maps = maps = &machine->kmaps;
497 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
499 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
501 al->maps = maps = &machine->kmaps;
503 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
509 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
510 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
512 al->filtered |= (1 << HIST_FILTER__GUEST);
513 if ((cpumode == PERF_RECORD_MISC_USER ||
514 cpumode == PERF_RECORD_MISC_KERNEL) &&
516 al->filtered |= (1 << HIST_FILTER__HOST);
521 al->map = maps__find(maps, al->addr);
522 if (al->map != NULL) {
524 * Kernel maps might be changed when loading symbols so loading
525 * must be done prior to using kernel maps.
529 al->addr = al->map->map_ip(al->map, al->addr);
536 * For branch stacks or branch samples, the sample cpumode might not be correct
537 * because it applies only to the sample 'ip' and not necessary to 'addr' or
538 * branch stack addresses. If possible, use a fallback to deal with those cases.
540 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
541 struct addr_location *al)
543 struct map *map = thread__find_map(thread, cpumode, addr, al);
544 struct machine *machine = thread->maps->machine;
545 u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr);
547 if (map || addr_cpumode == cpumode)
550 return thread__find_map(thread, addr_cpumode, addr, al);
553 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
554 u64 addr, struct addr_location *al)
557 if (thread__find_map(thread, cpumode, addr, al))
558 al->sym = map__find_symbol(al->map, al->addr);
562 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
563 u64 addr, struct addr_location *al)
566 if (thread__find_map_fb(thread, cpumode, addr, al))
567 al->sym = map__find_symbol(al->map, al->addr);
572 * Callers need to drop the reference to al->thread, obtained in
573 * machine__findnew_thread()
575 int machine__resolve(struct machine *machine, struct addr_location *al,
576 struct perf_sample *sample)
578 struct thread *thread = machine__findnew_thread(machine, sample->pid,
584 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
585 thread__find_map(thread, sample->cpumode, sample->ip, al);
586 dump_printf(" ...... dso: %s\n",
587 al->map ? al->map->dso->long_name :
588 al->level == 'H' ? "[hypervisor]" : "<not found>");
590 if (thread__is_filtered(thread))
591 al->filtered |= (1 << HIST_FILTER__THREAD);
594 al->cpu = sample->cpu;
599 struct perf_env *env = machine->env;
602 al->socket = env->cpu[al->cpu].socket_id;
606 struct dso *dso = al->map->dso;
608 if (symbol_conf.dso_list &&
609 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
611 (dso->short_name != dso->long_name &&
612 strlist__has_entry(symbol_conf.dso_list,
613 dso->long_name))))) {
614 al->filtered |= (1 << HIST_FILTER__DSO);
617 al->sym = map__find_symbol(al->map, al->addr);
620 if (symbol_conf.sym_list) {
622 char al_addr_str[32];
623 size_t sz = sizeof(al_addr_str);
626 ret = strlist__has_entry(symbol_conf.sym_list,
629 if (!ret && al->sym) {
630 snprintf(al_addr_str, sz, "0x%"PRIx64,
631 al->map->unmap_ip(al->map, al->sym->start));
632 ret = strlist__has_entry(symbol_conf.sym_list,
636 al->filtered |= (1 << HIST_FILTER__SYMBOL);
643 * The preprocess_sample method will return with reference counts for the
644 * in it, when done using (and perhaps getting ref counts if needing to
645 * keep a pointer to one of those entries) it must be paired with
646 * addr_location__put(), so that the refcounts can be decremented.
648 void addr_location__put(struct addr_location *al)
650 thread__zput(al->thread);
653 bool is_bts_event(struct perf_event_attr *attr)
655 return attr->type == PERF_TYPE_HARDWARE &&
656 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
657 attr->sample_period == 1;
660 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
662 if (attr->type == PERF_TYPE_SOFTWARE &&
663 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
664 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
665 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
668 if (is_bts_event(attr))
674 void thread__resolve(struct thread *thread, struct addr_location *al,
675 struct perf_sample *sample)
677 thread__find_map_fb(thread, sample->cpumode, sample->addr, al);
679 al->cpu = sample->cpu;
683 al->sym = map__find_symbol(al->map, al->addr);