1 #include <linux/types.h>
9 #include "thread_map.h"
11 static const char *perf_event__names[] = {
13 [PERF_RECORD_MMAP] = "MMAP",
14 [PERF_RECORD_LOST] = "LOST",
15 [PERF_RECORD_COMM] = "COMM",
16 [PERF_RECORD_EXIT] = "EXIT",
17 [PERF_RECORD_THROTTLE] = "THROTTLE",
18 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
19 [PERF_RECORD_FORK] = "FORK",
20 [PERF_RECORD_READ] = "READ",
21 [PERF_RECORD_SAMPLE] = "SAMPLE",
22 [PERF_RECORD_HEADER_ATTR] = "ATTR",
23 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
24 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
25 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
26 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
29 const char *perf_event__name(unsigned int id)
31 if (id >= ARRAY_SIZE(perf_event__names))
33 if (!perf_event__names[id])
35 return perf_event__names[id];
38 int perf_sample_size(u64 sample_type)
40 u64 mask = sample_type & PERF_SAMPLE_MASK;
44 for (i = 0; i < 64; i++) {
45 if (mask & (1ULL << i))
54 static struct perf_sample synth_sample = {
63 static pid_t perf_event__synthesize_comm(union perf_event *event, pid_t pid,
64 int full, perf_event__handler_t process,
65 struct perf_session *session)
67 char filename[PATH_MAX];
72 struct dirent dirent, *next;
75 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
77 fp = fopen(filename, "r");
81 * We raced with a task exiting - just return:
83 pr_debug("couldn't open %s\n", filename);
87 memset(&event->comm, 0, sizeof(event->comm));
89 while (!event->comm.comm[0] || !event->comm.pid) {
90 if (fgets(bf, sizeof(bf), fp) == NULL) {
91 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
95 if (memcmp(bf, "Name:", 5) == 0) {
97 while (*name && isspace(*name))
99 size = strlen(name) - 1;
100 memcpy(event->comm.comm, name, size++);
101 } else if (memcmp(bf, "Tgid:", 5) == 0) {
102 char *tgids = bf + 5;
103 while (*tgids && isspace(*tgids))
105 tgid = event->comm.pid = atoi(tgids);
109 event->comm.header.type = PERF_RECORD_COMM;
110 size = ALIGN(size, sizeof(u64));
111 memset(event->comm.comm + size, 0, session->id_hdr_size);
112 event->comm.header.size = (sizeof(event->comm) -
113 (sizeof(event->comm.comm) - size) +
114 session->id_hdr_size);
116 event->comm.tid = pid;
118 process(event, &synth_sample, session);
122 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
124 tasks = opendir(filename);
128 while (!readdir_r(tasks, &dirent, &next) && next) {
130 pid = strtol(dirent.d_name, &end, 10);
134 event->comm.tid = pid;
136 process(event, &synth_sample, session);
146 static int perf_event__synthesize_mmap_events(union perf_event *event,
147 pid_t pid, pid_t tgid,
148 perf_event__handler_t process,
149 struct perf_session *session)
151 char filename[PATH_MAX];
154 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
156 fp = fopen(filename, "r");
159 * We raced with a task exiting - just return:
161 pr_debug("couldn't open %s\n", filename);
165 event->header.type = PERF_RECORD_MMAP;
167 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
169 event->header.misc = PERF_RECORD_MISC_USER;
172 char bf[BUFSIZ], *pbf = bf;
175 if (fgets(bf, sizeof(bf), fp) == NULL)
178 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
179 n = hex2u64(pbf, &event->mmap.start);
183 n = hex2u64(pbf, &event->mmap.len);
187 if (*pbf == 'x') { /* vm_exec */
188 char *execname = strchr(bf, '/');
191 if (execname == NULL)
192 execname = strstr(bf, "[vdso]");
194 if (execname == NULL)
198 n = hex2u64(pbf, &event->mmap.pgoff);
200 size = strlen(execname);
201 execname[size - 1] = '\0'; /* Remove \n */
202 memcpy(event->mmap.filename, execname, size);
203 size = ALIGN(size, sizeof(u64));
204 event->mmap.len -= event->mmap.start;
205 event->mmap.header.size = (sizeof(event->mmap) -
206 (sizeof(event->mmap.filename) - size));
207 memset(event->mmap.filename + size, 0, session->id_hdr_size);
208 event->mmap.header.size += session->id_hdr_size;
209 event->mmap.pid = tgid;
210 event->mmap.tid = pid;
212 process(event, &synth_sample, session);
220 int perf_event__synthesize_modules(perf_event__handler_t process,
221 struct perf_session *session,
222 struct machine *machine)
225 struct map_groups *kmaps = &machine->kmaps;
226 union perf_event *event = zalloc((sizeof(event->mmap) +
227 session->id_hdr_size));
229 pr_debug("Not enough memory synthesizing mmap event "
230 "for kernel modules\n");
234 event->header.type = PERF_RECORD_MMAP;
237 * kernel uses 0 for user space maps, see kernel/perf_event.c
240 if (machine__is_host(machine))
241 event->header.misc = PERF_RECORD_MISC_KERNEL;
243 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
245 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
246 nd; nd = rb_next(nd)) {
248 struct map *pos = rb_entry(nd, struct map, rb_node);
250 if (pos->dso->kernel)
253 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
254 event->mmap.header.type = PERF_RECORD_MMAP;
255 event->mmap.header.size = (sizeof(event->mmap) -
256 (sizeof(event->mmap.filename) - size));
257 memset(event->mmap.filename + size, 0, session->id_hdr_size);
258 event->mmap.header.size += session->id_hdr_size;
259 event->mmap.start = pos->start;
260 event->mmap.len = pos->end - pos->start;
261 event->mmap.pid = machine->pid;
263 memcpy(event->mmap.filename, pos->dso->long_name,
264 pos->dso->long_name_len + 1);
265 process(event, &synth_sample, session);
272 static int __event__synthesize_thread(union perf_event *comm_event,
273 union perf_event *mmap_event,
274 pid_t pid, perf_event__handler_t process,
275 struct perf_session *session)
277 pid_t tgid = perf_event__synthesize_comm(comm_event, pid, 1, process,
281 return perf_event__synthesize_mmap_events(mmap_event, pid, tgid,
285 int perf_event__synthesize_thread_map(struct thread_map *threads,
286 perf_event__handler_t process,
287 struct perf_session *session)
289 union perf_event *comm_event, *mmap_event;
290 int err = -1, thread;
292 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
293 if (comm_event == NULL)
296 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
297 if (mmap_event == NULL)
301 for (thread = 0; thread < threads->nr; ++thread) {
302 if (__event__synthesize_thread(comm_event, mmap_event,
303 threads->map[thread],
316 int perf_event__synthesize_threads(perf_event__handler_t process,
317 struct perf_session *session)
320 struct dirent dirent, *next;
321 union perf_event *comm_event, *mmap_event;
324 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
325 if (comm_event == NULL)
328 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
329 if (mmap_event == NULL)
332 proc = opendir("/proc");
336 while (!readdir_r(proc, &dirent, &next) && next) {
338 pid_t pid = strtol(dirent.d_name, &end, 10);
340 if (*end) /* only interested in proper numerical dirents */
343 __event__synthesize_thread(comm_event, mmap_event, pid,
357 struct process_symbol_args {
362 static int find_symbol_cb(void *arg, const char *name, char type,
363 u64 start, u64 end __used)
365 struct process_symbol_args *args = arg;
368 * Must be a function or at least an alias, as in PARISC64, where "_text" is
369 * an 'A' to the same address as "_stext".
371 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
372 type == 'A') || strcmp(name, args->name))
379 int perf_event__synthesize_kernel_mmap(perf_event__handler_t process,
380 struct perf_session *session,
381 struct machine *machine,
382 const char *symbol_name)
385 const char *filename, *mmap_name;
387 char name_buff[PATH_MAX];
391 * We should get this from /sys/kernel/sections/.text, but till that is
392 * available use this, and after it is use this as a fallback for older
395 struct process_symbol_args args = { .name = symbol_name, };
396 union perf_event *event = zalloc((sizeof(event->mmap) +
397 session->id_hdr_size));
399 pr_debug("Not enough memory synthesizing mmap event "
400 "for kernel modules\n");
404 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
405 if (machine__is_host(machine)) {
407 * kernel uses PERF_RECORD_MISC_USER for user space maps,
408 * see kernel/perf_event.c __perf_event_mmap
410 event->header.misc = PERF_RECORD_MISC_KERNEL;
411 filename = "/proc/kallsyms";
413 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
414 if (machine__is_default_guest(machine))
415 filename = (char *) symbol_conf.default_guest_kallsyms;
417 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
422 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
425 map = machine->vmlinux_maps[MAP__FUNCTION];
426 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
427 "%s%s", mmap_name, symbol_name) + 1;
428 size = ALIGN(size, sizeof(u64));
429 event->mmap.header.type = PERF_RECORD_MMAP;
430 event->mmap.header.size = (sizeof(event->mmap) -
431 (sizeof(event->mmap.filename) - size) + session->id_hdr_size);
432 event->mmap.pgoff = args.start;
433 event->mmap.start = map->start;
434 event->mmap.len = map->end - event->mmap.start;
435 event->mmap.pid = machine->pid;
437 err = process(event, &synth_sample, session);
443 int perf_event__process_comm(union perf_event *event,
444 struct perf_sample *sample __used,
445 struct perf_session *session)
447 struct thread *thread = perf_session__findnew(session, event->comm.tid);
449 dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
451 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
452 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
459 int perf_event__process_lost(union perf_event *event,
460 struct perf_sample *sample __used,
461 struct perf_session *session)
463 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
464 event->lost.id, event->lost.lost);
465 session->hists.stats.total_lost += event->lost.lost;
469 static void perf_event__set_kernel_mmap_len(union perf_event *event,
472 maps[MAP__FUNCTION]->start = event->mmap.start;
473 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
475 * Be a bit paranoid here, some perf.data file came with
476 * a zero sized synthesized MMAP event for the kernel.
478 if (maps[MAP__FUNCTION]->end == 0)
479 maps[MAP__FUNCTION]->end = ~0ULL;
482 static int perf_event__process_kernel_mmap(union perf_event *event,
483 struct perf_session *session)
486 char kmmap_prefix[PATH_MAX];
487 struct machine *machine;
488 enum dso_kernel_type kernel_type;
491 machine = perf_session__findnew_machine(session, event->mmap.pid);
493 pr_err("Can't find id %d's machine\n", event->mmap.pid);
497 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
498 if (machine__is_host(machine))
499 kernel_type = DSO_TYPE_KERNEL;
501 kernel_type = DSO_TYPE_GUEST_KERNEL;
503 is_kernel_mmap = memcmp(event->mmap.filename,
505 strlen(kmmap_prefix)) == 0;
506 if (event->mmap.filename[0] == '/' ||
507 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
509 char short_module_name[1024];
512 if (event->mmap.filename[0] == '/') {
513 name = strrchr(event->mmap.filename, '/');
518 dot = strrchr(name, '.');
521 snprintf(short_module_name, sizeof(short_module_name),
522 "[%.*s]", (int)(dot - name), name);
523 strxfrchar(short_module_name, '-', '_');
525 strcpy(short_module_name, event->mmap.filename);
527 map = machine__new_module(machine, event->mmap.start,
528 event->mmap.filename);
532 name = strdup(short_module_name);
536 map->dso->short_name = name;
537 map->dso->sname_alloc = 1;
538 map->end = map->start + event->mmap.len;
539 } else if (is_kernel_mmap) {
540 const char *symbol_name = (event->mmap.filename +
541 strlen(kmmap_prefix));
543 * Should be there already, from the build-id table in
546 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
551 kernel->kernel = kernel_type;
552 if (__machine__create_kernel_maps(machine, kernel) < 0)
555 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
556 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
559 if (machine__is_default_guest(machine)) {
561 * preload dso of guest kernel and modules
563 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
572 int perf_event__process_mmap(union perf_event *event,
573 struct perf_sample *sample __used,
574 struct perf_session *session)
576 struct machine *machine;
577 struct thread *thread;
579 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
582 dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
583 event->mmap.pid, event->mmap.tid, event->mmap.start,
584 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
586 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
587 cpumode == PERF_RECORD_MISC_KERNEL) {
588 ret = perf_event__process_kernel_mmap(event, session);
594 machine = perf_session__find_host_machine(session);
597 thread = perf_session__findnew(session, event->mmap.pid);
600 map = map__new(&machine->user_dsos, event->mmap.start,
601 event->mmap.len, event->mmap.pgoff,
602 event->mmap.pid, event->mmap.filename,
607 thread__insert_map(thread, map);
611 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
615 int perf_event__process_task(union perf_event *event,
616 struct perf_sample *sample __used,
617 struct perf_session *session)
619 struct thread *thread = perf_session__findnew(session, event->fork.tid);
620 struct thread *parent = perf_session__findnew(session, event->fork.ptid);
622 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
623 event->fork.ppid, event->fork.ptid);
625 if (event->header.type == PERF_RECORD_EXIT) {
626 perf_session__remove_thread(session, thread);
630 if (thread == NULL || parent == NULL ||
631 thread__fork(thread, parent) < 0) {
632 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
639 int perf_event__process(union perf_event *event, struct perf_sample *sample,
640 struct perf_session *session)
642 switch (event->header.type) {
643 case PERF_RECORD_COMM:
644 perf_event__process_comm(event, sample, session);
646 case PERF_RECORD_MMAP:
647 perf_event__process_mmap(event, sample, session);
649 case PERF_RECORD_FORK:
650 case PERF_RECORD_EXIT:
651 perf_event__process_task(event, sample, session);
653 case PERF_RECORD_LOST:
654 perf_event__process_lost(event, sample, session);
662 void thread__find_addr_map(struct thread *self,
663 struct perf_session *session, u8 cpumode,
664 enum map_type type, pid_t pid, u64 addr,
665 struct addr_location *al)
667 struct map_groups *mg = &self->mg;
668 struct machine *machine = NULL;
672 al->cpumode = cpumode;
673 al->filtered = false;
675 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
677 machine = perf_session__find_host_machine(session);
678 if (machine == NULL) {
682 mg = &machine->kmaps;
683 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
685 machine = perf_session__find_host_machine(session);
686 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
688 machine = perf_session__find_machine(session, pid);
689 if (machine == NULL) {
693 mg = &machine->kmaps;
696 * 'u' means guest os user space.
697 * TODO: We don't support guest user space. Might support late.
699 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
705 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
706 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
709 if ((cpumode == PERF_RECORD_MISC_USER ||
710 cpumode == PERF_RECORD_MISC_KERNEL) &&
717 al->map = map_groups__find(mg, type, al->addr);
718 if (al->map == NULL) {
720 * If this is outside of all known maps, and is a negative
721 * address, try to look it up in the kernel dso, as it might be
722 * a vsyscall or vdso (which executes in user-mode).
724 * XXX This is nasty, we should have a symbol list in the
725 * "[vdso]" dso, but for now lets use the old trick of looking
726 * in the whole kernel symbol list.
728 if ((long long)al->addr < 0 &&
729 cpumode == PERF_RECORD_MISC_USER &&
730 machine && mg != &machine->kmaps) {
731 mg = &machine->kmaps;
735 al->addr = al->map->map_ip(al->map, al->addr);
738 void thread__find_addr_location(struct thread *self,
739 struct perf_session *session, u8 cpumode,
740 enum map_type type, pid_t pid, u64 addr,
741 struct addr_location *al,
742 symbol_filter_t filter)
744 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
746 al->sym = map__find_symbol(al->map, al->addr, filter);
751 int perf_event__preprocess_sample(const union perf_event *event,
752 struct perf_session *session,
753 struct addr_location *al,
754 struct perf_sample *sample,
755 symbol_filter_t filter)
757 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
758 struct thread *thread = perf_session__findnew(session, event->ip.pid);
763 if (symbol_conf.comm_list &&
764 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
767 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
769 * Have we already created the kernel maps for the host machine?
771 * This should have happened earlier, when we processed the kernel MMAP
772 * events, but for older perf.data files there was no such thing, so do
775 if (cpumode == PERF_RECORD_MISC_KERNEL &&
776 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
777 machine__create_kernel_maps(&session->host_machine);
779 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
780 event->ip.pid, event->ip.ip, al);
781 dump_printf(" ...... dso: %s\n",
782 al->map ? al->map->dso->long_name :
783 al->level == 'H' ? "[hypervisor]" : "<not found>");
785 al->cpu = sample->cpu;
788 if (symbol_conf.dso_list &&
789 (!al->map || !al->map->dso ||
790 !(strlist__has_entry(symbol_conf.dso_list,
791 al->map->dso->short_name) ||
792 (al->map->dso->short_name != al->map->dso->long_name &&
793 strlist__has_entry(symbol_conf.dso_list,
794 al->map->dso->long_name)))))
797 al->sym = map__find_symbol(al->map, al->addr, filter);
800 if (symbol_conf.sym_list && al->sym &&
801 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))