1 #include <linux/types.h>
11 #include "thread_map.h"
12 #include "symbol/kallsyms.h"
14 static const char *perf_event__names[] = {
16 [PERF_RECORD_MMAP] = "MMAP",
17 [PERF_RECORD_MMAP2] = "MMAP2",
18 [PERF_RECORD_LOST] = "LOST",
19 [PERF_RECORD_COMM] = "COMM",
20 [PERF_RECORD_EXIT] = "EXIT",
21 [PERF_RECORD_THROTTLE] = "THROTTLE",
22 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
23 [PERF_RECORD_FORK] = "FORK",
24 [PERF_RECORD_READ] = "READ",
25 [PERF_RECORD_SAMPLE] = "SAMPLE",
26 [PERF_RECORD_HEADER_ATTR] = "ATTR",
27 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
28 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
29 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
30 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
33 const char *perf_event__name(unsigned int id)
35 if (id >= ARRAY_SIZE(perf_event__names))
37 if (!perf_event__names[id])
39 return perf_event__names[id];
42 static struct perf_sample synth_sample = {
51 static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
53 char filename[PATH_MAX];
59 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
61 fp = fopen(filename, "r");
63 pr_debug("couldn't open %s\n", filename);
67 while (!comm[0] || (tgid < 0)) {
68 if (fgets(bf, sizeof(bf), fp) == NULL) {
69 pr_warning("couldn't get COMM and pgid, malformed %s\n",
74 if (memcmp(bf, "Name:", 5) == 0) {
76 while (*name && isspace(*name))
78 size = strlen(name) - 1;
81 memcpy(comm, name, size);
84 } else if (memcmp(bf, "Tgid:", 5) == 0) {
86 while (*tgids && isspace(*tgids))
97 static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
98 union perf_event *event, pid_t pid,
99 perf_event__handler_t process,
100 struct machine *machine)
105 memset(&event->comm, 0, sizeof(event->comm));
107 if (machine__is_host(machine))
108 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
109 sizeof(event->comm.comm));
116 event->comm.pid = tgid;
117 event->comm.header.type = PERF_RECORD_COMM;
119 size = strlen(event->comm.comm) + 1;
120 size = PERF_ALIGN(size, sizeof(u64));
121 memset(event->comm.comm + size, 0, machine->id_hdr_size);
122 event->comm.header.size = (sizeof(event->comm) -
123 (sizeof(event->comm.comm) - size) +
124 machine->id_hdr_size);
125 event->comm.tid = pid;
127 if (process(tool, event, &synth_sample, machine) != 0)
134 static int perf_event__synthesize_fork(struct perf_tool *tool,
135 union perf_event *event, pid_t pid,
136 pid_t tgid, perf_event__handler_t process,
137 struct machine *machine)
139 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
141 /* this is really a clone event but we use fork to synthesize it */
142 event->fork.ppid = tgid;
143 event->fork.ptid = tgid;
144 event->fork.pid = tgid;
145 event->fork.tid = pid;
146 event->fork.header.type = PERF_RECORD_FORK;
148 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
150 if (process(tool, event, &synth_sample, machine) != 0)
156 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
157 union perf_event *event,
158 pid_t pid, pid_t tgid,
159 perf_event__handler_t process,
160 struct machine *machine,
163 char filename[PATH_MAX];
167 if (machine__is_default_guest(machine))
170 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
171 machine->root_dir, pid);
173 fp = fopen(filename, "r");
176 * We raced with a task exiting - just return:
178 pr_debug("couldn't open %s\n", filename);
182 event->header.type = PERF_RECORD_MMAP2;
187 char execname[PATH_MAX];
188 char anonstr[] = "//anon";
193 if (fgets(bf, sizeof(bf), fp) == NULL)
196 /* ensure null termination since stack will be reused. */
197 strcpy(execname, "");
199 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
200 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
201 &event->mmap2.start, &event->mmap2.len, prot,
202 &event->mmap2.pgoff, &event->mmap2.maj,
207 * Anon maps don't have the execname.
212 event->mmap2.ino = (u64)ino;
215 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
217 if (machine__is_host(machine))
218 event->header.misc = PERF_RECORD_MISC_USER;
220 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
222 /* map protection and flags bits */
223 event->mmap2.prot = 0;
224 event->mmap2.flags = 0;
226 event->mmap2.prot |= PROT_READ;
228 event->mmap2.prot |= PROT_WRITE;
230 event->mmap2.prot |= PROT_EXEC;
233 event->mmap2.flags |= MAP_SHARED;
235 event->mmap2.flags |= MAP_PRIVATE;
237 if (prot[2] != 'x') {
238 if (!mmap_data || prot[0] != 'r')
241 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
244 if (!strcmp(execname, ""))
245 strcpy(execname, anonstr);
247 size = strlen(execname) + 1;
248 memcpy(event->mmap2.filename, execname, size);
249 size = PERF_ALIGN(size, sizeof(u64));
250 event->mmap2.len -= event->mmap.start;
251 event->mmap2.header.size = (sizeof(event->mmap2) -
252 (sizeof(event->mmap2.filename) - size));
253 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
254 event->mmap2.header.size += machine->id_hdr_size;
255 event->mmap2.pid = tgid;
256 event->mmap2.tid = pid;
258 if (process(tool, event, &synth_sample, machine) != 0) {
268 int perf_event__synthesize_modules(struct perf_tool *tool,
269 perf_event__handler_t process,
270 struct machine *machine)
274 struct map_groups *kmaps = &machine->kmaps;
275 union perf_event *event = zalloc((sizeof(event->mmap) +
276 machine->id_hdr_size));
278 pr_debug("Not enough memory synthesizing mmap event "
279 "for kernel modules\n");
283 event->header.type = PERF_RECORD_MMAP;
286 * kernel uses 0 for user space maps, see kernel/perf_event.c
289 if (machine__is_host(machine))
290 event->header.misc = PERF_RECORD_MISC_KERNEL;
292 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
294 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
295 nd; nd = rb_next(nd)) {
297 struct map *pos = rb_entry(nd, struct map, rb_node);
299 if (pos->dso->kernel)
302 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
303 event->mmap.header.type = PERF_RECORD_MMAP;
304 event->mmap.header.size = (sizeof(event->mmap) -
305 (sizeof(event->mmap.filename) - size));
306 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
307 event->mmap.header.size += machine->id_hdr_size;
308 event->mmap.start = pos->start;
309 event->mmap.len = pos->end - pos->start;
310 event->mmap.pid = machine->pid;
312 memcpy(event->mmap.filename, pos->dso->long_name,
313 pos->dso->long_name_len + 1);
314 if (process(tool, event, &synth_sample, machine) != 0) {
324 static int __event__synthesize_thread(union perf_event *comm_event,
325 union perf_event *mmap_event,
326 union perf_event *fork_event,
328 perf_event__handler_t process,
329 struct perf_tool *tool,
330 struct machine *machine, bool mmap_data)
332 char filename[PATH_MAX];
334 struct dirent dirent, *next;
337 /* special case: only send one comm event using passed in pid */
339 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
345 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
346 process, machine, mmap_data);
349 if (machine__is_default_guest(machine))
352 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
353 machine->root_dir, pid);
355 tasks = opendir(filename);
357 pr_debug("couldn't open %s\n", filename);
361 while (!readdir_r(tasks, &dirent, &next) && next) {
366 _pid = strtol(dirent.d_name, &end, 10);
370 tgid = perf_event__synthesize_comm(tool, comm_event, _pid,
376 /* process the parent's maps too */
377 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
378 process, machine, mmap_data);
380 /* only fork the tid's map, to save time */
381 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
393 int perf_event__synthesize_thread_map(struct perf_tool *tool,
394 struct thread_map *threads,
395 perf_event__handler_t process,
396 struct machine *machine,
399 union perf_event *comm_event, *mmap_event, *fork_event;
400 int err = -1, thread, j;
402 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
403 if (comm_event == NULL)
406 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
407 if (mmap_event == NULL)
410 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
411 if (fork_event == NULL)
415 for (thread = 0; thread < threads->nr; ++thread) {
416 if (__event__synthesize_thread(comm_event, mmap_event,
418 threads->map[thread], 0,
419 process, tool, machine,
426 * comm.pid is set to thread group id by
427 * perf_event__synthesize_comm
429 if ((int) comm_event->comm.pid != threads->map[thread]) {
430 bool need_leader = true;
432 /* is thread group leader in thread_map? */
433 for (j = 0; j < threads->nr; ++j) {
434 if ((int) comm_event->comm.pid == threads->map[j]) {
440 /* if not, generate events for it */
442 __event__synthesize_thread(comm_event, mmap_event,
444 comm_event->comm.pid, 0,
445 process, tool, machine,
461 int perf_event__synthesize_threads(struct perf_tool *tool,
462 perf_event__handler_t process,
463 struct machine *machine, bool mmap_data)
466 char proc_path[PATH_MAX];
467 struct dirent dirent, *next;
468 union perf_event *comm_event, *mmap_event, *fork_event;
471 if (machine__is_default_guest(machine))
474 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
475 if (comm_event == NULL)
478 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
479 if (mmap_event == NULL)
482 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
483 if (fork_event == NULL)
486 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
487 proc = opendir(proc_path);
492 while (!readdir_r(proc, &dirent, &next) && next) {
494 pid_t pid = strtol(dirent.d_name, &end, 10);
496 if (*end) /* only interested in proper numerical dirents */
499 * We may race with exiting thread, so don't stop just because
500 * one thread couldn't be synthesized.
502 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
503 1, process, tool, machine, mmap_data);
518 struct process_symbol_args {
523 static int find_symbol_cb(void *arg, const char *name, char type,
526 struct process_symbol_args *args = arg;
529 * Must be a function or at least an alias, as in PARISC64, where "_text" is
530 * an 'A' to the same address as "_stext".
532 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
533 type == 'A') || strcmp(name, args->name))
540 u64 kallsyms__get_function_start(const char *kallsyms_filename,
541 const char *symbol_name)
543 struct process_symbol_args args = { .name = symbol_name, };
545 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
551 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
552 perf_event__handler_t process,
553 struct machine *machine)
556 const char *mmap_name;
557 char name_buff[PATH_MAX];
561 union perf_event *event;
563 if (machine->vmlinux_maps[0] == NULL)
567 * We should get this from /sys/kernel/sections/.text, but till that is
568 * available use this, and after it is use this as a fallback for older
571 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
573 pr_debug("Not enough memory synthesizing mmap event "
574 "for kernel modules\n");
578 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
579 if (machine__is_host(machine)) {
581 * kernel uses PERF_RECORD_MISC_USER for user space maps,
582 * see kernel/perf_event.c __perf_event_mmap
584 event->header.misc = PERF_RECORD_MISC_KERNEL;
586 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
589 map = machine->vmlinux_maps[MAP__FUNCTION];
590 kmap = map__kmap(map);
591 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
592 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
593 size = PERF_ALIGN(size, sizeof(u64));
594 event->mmap.header.type = PERF_RECORD_MMAP;
595 event->mmap.header.size = (sizeof(event->mmap) -
596 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
597 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
598 event->mmap.start = map->start;
599 event->mmap.len = map->end - event->mmap.start;
600 event->mmap.pid = machine->pid;
602 err = process(tool, event, &synth_sample, machine);
608 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
612 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
617 return fprintf(fp, "%s: %s:%d\n", s, event->comm.comm, event->comm.tid);
620 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
621 union perf_event *event,
622 struct perf_sample *sample,
623 struct machine *machine)
625 return machine__process_comm_event(machine, event, sample);
628 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
629 union perf_event *event,
630 struct perf_sample *sample,
631 struct machine *machine)
633 return machine__process_lost_event(machine, event, sample);
636 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
638 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
639 event->mmap.pid, event->mmap.tid, event->mmap.start,
640 event->mmap.len, event->mmap.pgoff,
641 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
642 event->mmap.filename);
645 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
647 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
648 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
649 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
650 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
651 event->mmap2.min, event->mmap2.ino,
652 event->mmap2.ino_generation,
653 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
654 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
655 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
656 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
657 event->mmap2.filename);
660 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
661 union perf_event *event,
662 struct perf_sample *sample,
663 struct machine *machine)
665 return machine__process_mmap_event(machine, event, sample);
668 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
669 union perf_event *event,
670 struct perf_sample *sample,
671 struct machine *machine)
673 return machine__process_mmap2_event(machine, event, sample);
676 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
678 return fprintf(fp, "(%d:%d):(%d:%d)\n",
679 event->fork.pid, event->fork.tid,
680 event->fork.ppid, event->fork.ptid);
683 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
684 union perf_event *event,
685 struct perf_sample *sample,
686 struct machine *machine)
688 return machine__process_fork_event(machine, event, sample);
691 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
692 union perf_event *event,
693 struct perf_sample *sample,
694 struct machine *machine)
696 return machine__process_exit_event(machine, event, sample);
699 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
701 size_t ret = fprintf(fp, "PERF_RECORD_%s",
702 perf_event__name(event->header.type));
704 switch (event->header.type) {
705 case PERF_RECORD_COMM:
706 ret += perf_event__fprintf_comm(event, fp);
708 case PERF_RECORD_FORK:
709 case PERF_RECORD_EXIT:
710 ret += perf_event__fprintf_task(event, fp);
712 case PERF_RECORD_MMAP:
713 ret += perf_event__fprintf_mmap(event, fp);
715 case PERF_RECORD_MMAP2:
716 ret += perf_event__fprintf_mmap2(event, fp);
719 ret += fprintf(fp, "\n");
725 int perf_event__process(struct perf_tool *tool __maybe_unused,
726 union perf_event *event,
727 struct perf_sample *sample,
728 struct machine *machine)
730 return machine__process_event(machine, event, sample);
733 void thread__find_addr_map(struct thread *thread,
734 struct machine *machine, u8 cpumode,
735 enum map_type type, u64 addr,
736 struct addr_location *al)
738 struct map_groups *mg = thread->mg;
739 bool load_map = false;
741 al->machine = machine;
744 al->cpumode = cpumode;
747 if (machine == NULL) {
752 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
754 mg = &machine->kmaps;
756 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
758 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
760 mg = &machine->kmaps;
762 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
768 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
769 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
771 al->filtered |= (1 << HIST_FILTER__GUEST);
772 if ((cpumode == PERF_RECORD_MISC_USER ||
773 cpumode == PERF_RECORD_MISC_KERNEL) &&
775 al->filtered |= (1 << HIST_FILTER__HOST);
780 al->map = map_groups__find(mg, type, al->addr);
781 if (al->map == NULL) {
783 * If this is outside of all known maps, and is a negative
784 * address, try to look it up in the kernel dso, as it might be
785 * a vsyscall or vdso (which executes in user-mode).
787 * XXX This is nasty, we should have a symbol list in the
788 * "[vdso]" dso, but for now lets use the old trick of looking
789 * in the whole kernel symbol list.
791 if (cpumode == PERF_RECORD_MISC_USER && machine &&
792 mg != &machine->kmaps &&
793 machine__kernel_ip(machine, al->addr)) {
794 mg = &machine->kmaps;
800 * Kernel maps might be changed when loading symbols so loading
801 * must be done prior to using kernel maps.
804 map__load(al->map, machine->symbol_filter);
805 al->addr = al->map->map_ip(al->map, al->addr);
809 void thread__find_addr_location(struct thread *thread, struct machine *machine,
810 u8 cpumode, enum map_type type, u64 addr,
811 struct addr_location *al)
813 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
815 al->sym = map__find_symbol(al->map, al->addr,
816 machine->symbol_filter);
821 int perf_event__preprocess_sample(const union perf_event *event,
822 struct machine *machine,
823 struct addr_location *al,
824 struct perf_sample *sample)
826 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
827 struct thread *thread = machine__findnew_thread(machine, sample->pid,
833 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
835 * Have we already created the kernel maps for this machine?
837 * This should have happened earlier, when we processed the kernel MMAP
838 * events, but for older perf.data files there was no such thing, so do
841 if (cpumode == PERF_RECORD_MISC_KERNEL &&
842 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
843 machine__create_kernel_maps(machine);
845 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
847 dump_printf(" ...... dso: %s\n",
848 al->map ? al->map->dso->long_name :
849 al->level == 'H' ? "[hypervisor]" : "<not found>");
851 if (thread__is_filtered(thread))
852 al->filtered |= (1 << HIST_FILTER__THREAD);
855 al->cpu = sample->cpu;
858 struct dso *dso = al->map->dso;
860 if (symbol_conf.dso_list &&
861 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
863 (dso->short_name != dso->long_name &&
864 strlist__has_entry(symbol_conf.dso_list,
865 dso->long_name))))) {
866 al->filtered |= (1 << HIST_FILTER__DSO);
869 al->sym = map__find_symbol(al->map, al->addr,
870 machine->symbol_filter);
873 if (symbol_conf.sym_list &&
874 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
876 al->filtered |= (1 << HIST_FILTER__SYMBOL);
882 bool is_bts_event(struct perf_event_attr *attr)
884 return attr->type == PERF_TYPE_HARDWARE &&
885 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
886 attr->sample_period == 1;
889 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
891 if (attr->type == PERF_TYPE_SOFTWARE &&
892 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
893 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
894 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
897 if (is_bts_event(attr))
903 void perf_event__preprocess_sample_addr(union perf_event *event,
904 struct perf_sample *sample,
905 struct machine *machine,
906 struct thread *thread,
907 struct addr_location *al)
909 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
911 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
914 thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
917 al->cpu = sample->cpu;
921 al->sym = map__find_symbol(al->map, al->addr, NULL);