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",
31 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
34 const char *perf_event__name(unsigned int id)
36 if (id >= ARRAY_SIZE(perf_event__names))
38 if (!perf_event__names[id])
40 return perf_event__names[id];
43 static struct perf_sample synth_sample = {
52 static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
54 char filename[PATH_MAX];
60 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
62 fp = fopen(filename, "r");
64 pr_debug("couldn't open %s\n", filename);
68 while (!comm[0] || (tgid < 0)) {
69 if (fgets(bf, sizeof(bf), fp) == NULL) {
70 pr_warning("couldn't get COMM and pgid, malformed %s\n",
75 if (memcmp(bf, "Name:", 5) == 0) {
77 while (*name && isspace(*name))
79 size = strlen(name) - 1;
82 memcpy(comm, name, size);
85 } else if (memcmp(bf, "Tgid:", 5) == 0) {
87 while (*tgids && isspace(*tgids))
98 static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
99 union perf_event *event, pid_t pid,
100 perf_event__handler_t process,
101 struct machine *machine)
106 memset(&event->comm, 0, sizeof(event->comm));
108 if (machine__is_host(machine))
109 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
110 sizeof(event->comm.comm));
117 event->comm.pid = tgid;
118 event->comm.header.type = PERF_RECORD_COMM;
120 size = strlen(event->comm.comm) + 1;
121 size = PERF_ALIGN(size, sizeof(u64));
122 memset(event->comm.comm + size, 0, machine->id_hdr_size);
123 event->comm.header.size = (sizeof(event->comm) -
124 (sizeof(event->comm.comm) - size) +
125 machine->id_hdr_size);
126 event->comm.tid = pid;
128 if (process(tool, event, &synth_sample, machine) != 0)
135 static int perf_event__synthesize_fork(struct perf_tool *tool,
136 union perf_event *event, pid_t pid,
137 pid_t tgid, perf_event__handler_t process,
138 struct machine *machine)
140 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
142 /* this is really a clone event but we use fork to synthesize it */
143 event->fork.ppid = tgid;
144 event->fork.ptid = tgid;
145 event->fork.pid = tgid;
146 event->fork.tid = pid;
147 event->fork.header.type = PERF_RECORD_FORK;
149 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
151 if (process(tool, event, &synth_sample, machine) != 0)
157 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
158 union perf_event *event,
159 pid_t pid, pid_t tgid,
160 perf_event__handler_t process,
161 struct machine *machine,
164 char filename[PATH_MAX];
168 if (machine__is_default_guest(machine))
171 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
172 machine->root_dir, pid);
174 fp = fopen(filename, "r");
177 * We raced with a task exiting - just return:
179 pr_debug("couldn't open %s\n", filename);
183 event->header.type = PERF_RECORD_MMAP2;
188 char execname[PATH_MAX];
189 char anonstr[] = "//anon";
194 if (fgets(bf, sizeof(bf), fp) == NULL)
197 /* ensure null termination since stack will be reused. */
198 strcpy(execname, "");
200 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
201 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
202 &event->mmap2.start, &event->mmap2.len, prot,
203 &event->mmap2.pgoff, &event->mmap2.maj,
208 * Anon maps don't have the execname.
213 event->mmap2.ino = (u64)ino;
216 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
218 if (machine__is_host(machine))
219 event->header.misc = PERF_RECORD_MISC_USER;
221 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
223 /* map protection and flags bits */
224 event->mmap2.prot = 0;
225 event->mmap2.flags = 0;
227 event->mmap2.prot |= PROT_READ;
229 event->mmap2.prot |= PROT_WRITE;
231 event->mmap2.prot |= PROT_EXEC;
234 event->mmap2.flags |= MAP_SHARED;
236 event->mmap2.flags |= MAP_PRIVATE;
238 if (prot[2] != 'x') {
239 if (!mmap_data || prot[0] != 'r')
242 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
245 if (!strcmp(execname, ""))
246 strcpy(execname, anonstr);
248 size = strlen(execname) + 1;
249 memcpy(event->mmap2.filename, execname, size);
250 size = PERF_ALIGN(size, sizeof(u64));
251 event->mmap2.len -= event->mmap.start;
252 event->mmap2.header.size = (sizeof(event->mmap2) -
253 (sizeof(event->mmap2.filename) - size));
254 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
255 event->mmap2.header.size += machine->id_hdr_size;
256 event->mmap2.pid = tgid;
257 event->mmap2.tid = pid;
259 if (process(tool, event, &synth_sample, machine) != 0) {
269 int perf_event__synthesize_modules(struct perf_tool *tool,
270 perf_event__handler_t process,
271 struct machine *machine)
275 struct map_groups *kmaps = &machine->kmaps;
276 union perf_event *event = zalloc((sizeof(event->mmap) +
277 machine->id_hdr_size));
279 pr_debug("Not enough memory synthesizing mmap event "
280 "for kernel modules\n");
284 event->header.type = PERF_RECORD_MMAP;
287 * kernel uses 0 for user space maps, see kernel/perf_event.c
290 if (machine__is_host(machine))
291 event->header.misc = PERF_RECORD_MISC_KERNEL;
293 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
295 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
296 nd; nd = rb_next(nd)) {
298 struct map *pos = rb_entry(nd, struct map, rb_node);
300 if (pos->dso->kernel)
303 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
304 event->mmap.header.type = PERF_RECORD_MMAP;
305 event->mmap.header.size = (sizeof(event->mmap) -
306 (sizeof(event->mmap.filename) - size));
307 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
308 event->mmap.header.size += machine->id_hdr_size;
309 event->mmap.start = pos->start;
310 event->mmap.len = pos->end - pos->start;
311 event->mmap.pid = machine->pid;
313 memcpy(event->mmap.filename, pos->dso->long_name,
314 pos->dso->long_name_len + 1);
315 if (process(tool, event, &synth_sample, machine) != 0) {
325 static int __event__synthesize_thread(union perf_event *comm_event,
326 union perf_event *mmap_event,
327 union perf_event *fork_event,
329 perf_event__handler_t process,
330 struct perf_tool *tool,
331 struct machine *machine, bool mmap_data)
333 char filename[PATH_MAX];
335 struct dirent dirent, *next;
338 /* special case: only send one comm event using passed in pid */
340 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
346 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
347 process, machine, mmap_data);
350 if (machine__is_default_guest(machine))
353 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
354 machine->root_dir, pid);
356 tasks = opendir(filename);
358 pr_debug("couldn't open %s\n", filename);
362 while (!readdir_r(tasks, &dirent, &next) && next) {
367 _pid = strtol(dirent.d_name, &end, 10);
371 tgid = perf_event__synthesize_comm(tool, comm_event, _pid,
377 /* process the parent's maps too */
378 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
379 process, machine, mmap_data);
381 /* only fork the tid's map, to save time */
382 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
394 int perf_event__synthesize_thread_map(struct perf_tool *tool,
395 struct thread_map *threads,
396 perf_event__handler_t process,
397 struct machine *machine,
400 union perf_event *comm_event, *mmap_event, *fork_event;
401 int err = -1, thread, j;
403 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
404 if (comm_event == NULL)
407 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
408 if (mmap_event == NULL)
411 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
412 if (fork_event == NULL)
416 for (thread = 0; thread < threads->nr; ++thread) {
417 if (__event__synthesize_thread(comm_event, mmap_event,
419 threads->map[thread], 0,
420 process, tool, machine,
427 * comm.pid is set to thread group id by
428 * perf_event__synthesize_comm
430 if ((int) comm_event->comm.pid != threads->map[thread]) {
431 bool need_leader = true;
433 /* is thread group leader in thread_map? */
434 for (j = 0; j < threads->nr; ++j) {
435 if ((int) comm_event->comm.pid == threads->map[j]) {
441 /* if not, generate events for it */
443 __event__synthesize_thread(comm_event, mmap_event,
445 comm_event->comm.pid, 0,
446 process, tool, machine,
462 int perf_event__synthesize_threads(struct perf_tool *tool,
463 perf_event__handler_t process,
464 struct machine *machine, bool mmap_data)
467 char proc_path[PATH_MAX];
468 struct dirent dirent, *next;
469 union perf_event *comm_event, *mmap_event, *fork_event;
472 if (machine__is_default_guest(machine))
475 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
476 if (comm_event == NULL)
479 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
480 if (mmap_event == NULL)
483 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
484 if (fork_event == NULL)
487 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
488 proc = opendir(proc_path);
493 while (!readdir_r(proc, &dirent, &next) && next) {
495 pid_t pid = strtol(dirent.d_name, &end, 10);
497 if (*end) /* only interested in proper numerical dirents */
500 * We may race with exiting thread, so don't stop just because
501 * one thread couldn't be synthesized.
503 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
504 1, process, tool, machine, mmap_data);
519 struct process_symbol_args {
524 static int find_symbol_cb(void *arg, const char *name, char type,
527 struct process_symbol_args *args = arg;
530 * Must be a function or at least an alias, as in PARISC64, where "_text" is
531 * an 'A' to the same address as "_stext".
533 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
534 type == 'A') || strcmp(name, args->name))
541 u64 kallsyms__get_function_start(const char *kallsyms_filename,
542 const char *symbol_name)
544 struct process_symbol_args args = { .name = symbol_name, };
546 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
552 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
553 perf_event__handler_t process,
554 struct machine *machine)
557 const char *mmap_name;
558 char name_buff[PATH_MAX];
562 union perf_event *event;
564 if (machine->vmlinux_maps[0] == NULL)
568 * We should get this from /sys/kernel/sections/.text, but till that is
569 * available use this, and after it is use this as a fallback for older
572 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
574 pr_debug("Not enough memory synthesizing mmap event "
575 "for kernel modules\n");
579 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
580 if (machine__is_host(machine)) {
582 * kernel uses PERF_RECORD_MISC_USER for user space maps,
583 * see kernel/perf_event.c __perf_event_mmap
585 event->header.misc = PERF_RECORD_MISC_KERNEL;
587 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
590 map = machine->vmlinux_maps[MAP__FUNCTION];
591 kmap = map__kmap(map);
592 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
593 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
594 size = PERF_ALIGN(size, sizeof(u64));
595 event->mmap.header.type = PERF_RECORD_MMAP;
596 event->mmap.header.size = (sizeof(event->mmap) -
597 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
598 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
599 event->mmap.start = map->start;
600 event->mmap.len = map->end - event->mmap.start;
601 event->mmap.pid = machine->pid;
603 err = process(tool, event, &synth_sample, machine);
609 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
613 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
618 return fprintf(fp, "%s: %s:%d\n", s, event->comm.comm, event->comm.tid);
621 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
622 union perf_event *event,
623 struct perf_sample *sample,
624 struct machine *machine)
626 return machine__process_comm_event(machine, event, sample);
629 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
630 union perf_event *event,
631 struct perf_sample *sample,
632 struct machine *machine)
634 return machine__process_lost_event(machine, event, sample);
637 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
639 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
640 event->mmap.pid, event->mmap.tid, event->mmap.start,
641 event->mmap.len, event->mmap.pgoff,
642 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
643 event->mmap.filename);
646 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
648 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
649 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
650 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
651 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
652 event->mmap2.min, event->mmap2.ino,
653 event->mmap2.ino_generation,
654 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
655 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
656 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
657 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
658 event->mmap2.filename);
661 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
662 union perf_event *event,
663 struct perf_sample *sample,
664 struct machine *machine)
666 return machine__process_mmap_event(machine, event, sample);
669 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
670 union perf_event *event,
671 struct perf_sample *sample,
672 struct machine *machine)
674 return machine__process_mmap2_event(machine, event, sample);
677 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
679 return fprintf(fp, "(%d:%d):(%d:%d)\n",
680 event->fork.pid, event->fork.tid,
681 event->fork.ppid, event->fork.ptid);
684 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
685 union perf_event *event,
686 struct perf_sample *sample,
687 struct machine *machine)
689 return machine__process_fork_event(machine, event, sample);
692 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
693 union perf_event *event,
694 struct perf_sample *sample,
695 struct machine *machine)
697 return machine__process_exit_event(machine, event, sample);
700 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
702 size_t ret = fprintf(fp, "PERF_RECORD_%s",
703 perf_event__name(event->header.type));
705 switch (event->header.type) {
706 case PERF_RECORD_COMM:
707 ret += perf_event__fprintf_comm(event, fp);
709 case PERF_RECORD_FORK:
710 case PERF_RECORD_EXIT:
711 ret += perf_event__fprintf_task(event, fp);
713 case PERF_RECORD_MMAP:
714 ret += perf_event__fprintf_mmap(event, fp);
716 case PERF_RECORD_MMAP2:
717 ret += perf_event__fprintf_mmap2(event, fp);
720 ret += fprintf(fp, "\n");
726 int perf_event__process(struct perf_tool *tool __maybe_unused,
727 union perf_event *event,
728 struct perf_sample *sample,
729 struct machine *machine)
731 return machine__process_event(machine, event, sample);
734 void thread__find_addr_map(struct thread *thread, u8 cpumode,
735 enum map_type type, u64 addr,
736 struct addr_location *al)
738 struct map_groups *mg = thread->mg;
739 struct machine *machine = mg->machine;
740 bool load_map = false;
742 al->machine = machine;
745 al->cpumode = cpumode;
748 if (machine == NULL) {
753 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
755 mg = &machine->kmaps;
757 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
759 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
761 mg = &machine->kmaps;
763 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
769 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
770 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
772 al->filtered |= (1 << HIST_FILTER__GUEST);
773 if ((cpumode == PERF_RECORD_MISC_USER ||
774 cpumode == PERF_RECORD_MISC_KERNEL) &&
776 al->filtered |= (1 << HIST_FILTER__HOST);
781 al->map = map_groups__find(mg, type, al->addr);
782 if (al->map == NULL) {
784 * If this is outside of all known maps, and is a negative
785 * address, try to look it up in the kernel dso, as it might be
786 * a vsyscall or vdso (which executes in user-mode).
788 * XXX This is nasty, we should have a symbol list in the
789 * "[vdso]" dso, but for now lets use the old trick of looking
790 * in the whole kernel symbol list.
792 if (cpumode == PERF_RECORD_MISC_USER && machine &&
793 mg != &machine->kmaps &&
794 machine__kernel_ip(machine, al->addr)) {
795 mg = &machine->kmaps;
801 * Kernel maps might be changed when loading symbols so loading
802 * must be done prior to using kernel maps.
805 map__load(al->map, machine->symbol_filter);
806 al->addr = al->map->map_ip(al->map, al->addr);
810 void thread__find_addr_location(struct thread *thread,
811 u8 cpumode, enum map_type type, u64 addr,
812 struct addr_location *al)
814 thread__find_addr_map(thread, cpumode, type, addr, al);
816 al->sym = map__find_symbol(al->map, al->addr,
817 thread->mg->machine->symbol_filter);
822 int perf_event__preprocess_sample(const union perf_event *event,
823 struct machine *machine,
824 struct addr_location *al,
825 struct perf_sample *sample)
827 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
828 struct thread *thread = machine__findnew_thread(machine, sample->pid,
834 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
836 * Have we already created the kernel maps for this machine?
838 * This should have happened earlier, when we processed the kernel MMAP
839 * events, but for older perf.data files there was no such thing, so do
842 if (cpumode == PERF_RECORD_MISC_KERNEL &&
843 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
844 machine__create_kernel_maps(machine);
846 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
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 thread *thread,
906 struct addr_location *al)
908 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
910 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
912 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
915 al->cpu = sample->cpu;
919 al->sym = map__find_symbol(al->map, al->addr, NULL);