1 #include <linux/types.h>
8 #include "thread_map.h"
10 static const char *perf_event__names[] = {
12 [PERF_RECORD_MMAP] = "MMAP",
13 [PERF_RECORD_LOST] = "LOST",
14 [PERF_RECORD_COMM] = "COMM",
15 [PERF_RECORD_EXIT] = "EXIT",
16 [PERF_RECORD_THROTTLE] = "THROTTLE",
17 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
18 [PERF_RECORD_FORK] = "FORK",
19 [PERF_RECORD_READ] = "READ",
20 [PERF_RECORD_SAMPLE] = "SAMPLE",
21 [PERF_RECORD_HEADER_ATTR] = "ATTR",
22 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
23 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
24 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
25 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
28 const char *perf_event__name(unsigned int id)
30 if (id >= ARRAY_SIZE(perf_event__names))
32 if (!perf_event__names[id])
34 return perf_event__names[id];
37 static struct perf_sample synth_sample = {
46 static pid_t perf_event__synthesize_comm(struct perf_event_ops *ops,
47 union perf_event *event, pid_t pid,
48 int full, perf_event__handler_t process,
49 struct machine *machine)
51 char filename[PATH_MAX];
56 struct dirent dirent, *next;
59 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
61 fp = fopen(filename, "r");
65 * We raced with a task exiting - just return:
67 pr_debug("couldn't open %s\n", filename);
71 memset(&event->comm, 0, sizeof(event->comm));
73 while (!event->comm.comm[0] || !event->comm.pid) {
74 if (fgets(bf, sizeof(bf), fp) == NULL) {
75 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
79 if (memcmp(bf, "Name:", 5) == 0) {
81 while (*name && isspace(*name))
83 size = strlen(name) - 1;
84 memcpy(event->comm.comm, name, size++);
85 } else if (memcmp(bf, "Tgid:", 5) == 0) {
87 while (*tgids && isspace(*tgids))
89 tgid = event->comm.pid = atoi(tgids);
93 event->comm.header.type = PERF_RECORD_COMM;
94 size = ALIGN(size, sizeof(u64));
95 memset(event->comm.comm + size, 0, machine->id_hdr_size);
96 event->comm.header.size = (sizeof(event->comm) -
97 (sizeof(event->comm.comm) - size) +
98 machine->id_hdr_size);
100 event->comm.tid = pid;
102 process(ops, event, &synth_sample, machine);
106 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
108 tasks = opendir(filename);
112 while (!readdir_r(tasks, &dirent, &next) && next) {
114 pid = strtol(dirent.d_name, &end, 10);
118 event->comm.tid = pid;
120 process(ops, event, &synth_sample, machine);
130 static int perf_event__synthesize_mmap_events(struct perf_event_ops *ops,
131 union perf_event *event,
132 pid_t pid, pid_t tgid,
133 perf_event__handler_t process,
134 struct machine *machine)
136 char filename[PATH_MAX];
139 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
141 fp = fopen(filename, "r");
144 * We raced with a task exiting - just return:
146 pr_debug("couldn't open %s\n", filename);
150 event->header.type = PERF_RECORD_MMAP;
152 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
154 event->header.misc = PERF_RECORD_MISC_USER;
157 char bf[BUFSIZ], *pbf = bf;
160 if (fgets(bf, sizeof(bf), fp) == NULL)
163 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
164 n = hex2u64(pbf, &event->mmap.start);
168 n = hex2u64(pbf, &event->mmap.len);
172 if (*pbf == 'x') { /* vm_exec */
173 char anonstr[] = "//anon\n";
174 char *execname = strchr(bf, '/');
177 if (execname == NULL)
178 execname = strstr(bf, "[vdso]");
180 /* Catch anonymous mmaps */
181 if ((execname == NULL) && !strstr(bf, "["))
184 if (execname == NULL)
188 n = hex2u64(pbf, &event->mmap.pgoff);
190 size = strlen(execname);
191 execname[size - 1] = '\0'; /* Remove \n */
192 memcpy(event->mmap.filename, execname, size);
193 size = ALIGN(size, sizeof(u64));
194 event->mmap.len -= event->mmap.start;
195 event->mmap.header.size = (sizeof(event->mmap) -
196 (sizeof(event->mmap.filename) - size));
197 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
198 event->mmap.header.size += machine->id_hdr_size;
199 event->mmap.pid = tgid;
200 event->mmap.tid = pid;
202 process(ops, event, &synth_sample, machine);
210 int perf_event__synthesize_modules(struct perf_event_ops *ops,
211 perf_event__handler_t process,
212 struct machine *machine)
215 struct map_groups *kmaps = &machine->kmaps;
216 union perf_event *event = zalloc((sizeof(event->mmap) +
217 machine->id_hdr_size));
219 pr_debug("Not enough memory synthesizing mmap event "
220 "for kernel modules\n");
224 event->header.type = PERF_RECORD_MMAP;
227 * kernel uses 0 for user space maps, see kernel/perf_event.c
230 if (machine__is_host(machine))
231 event->header.misc = PERF_RECORD_MISC_KERNEL;
233 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
235 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
236 nd; nd = rb_next(nd)) {
238 struct map *pos = rb_entry(nd, struct map, rb_node);
240 if (pos->dso->kernel)
243 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
244 event->mmap.header.type = PERF_RECORD_MMAP;
245 event->mmap.header.size = (sizeof(event->mmap) -
246 (sizeof(event->mmap.filename) - size));
247 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
248 event->mmap.header.size += machine->id_hdr_size;
249 event->mmap.start = pos->start;
250 event->mmap.len = pos->end - pos->start;
251 event->mmap.pid = machine->pid;
253 memcpy(event->mmap.filename, pos->dso->long_name,
254 pos->dso->long_name_len + 1);
255 process(ops, event, &synth_sample, machine);
262 static int __event__synthesize_thread(union perf_event *comm_event,
263 union perf_event *mmap_event,
264 pid_t pid, perf_event__handler_t process,
265 struct perf_event_ops *ops,
266 struct machine *machine)
268 pid_t tgid = perf_event__synthesize_comm(ops, comm_event, pid, 1,
272 return perf_event__synthesize_mmap_events(ops, mmap_event, pid, tgid,
276 int perf_event__synthesize_thread_map(struct perf_event_ops *ops,
277 struct thread_map *threads,
278 perf_event__handler_t process,
279 struct machine *machine)
281 union perf_event *comm_event, *mmap_event;
282 int err = -1, thread;
284 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
285 if (comm_event == NULL)
288 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
289 if (mmap_event == NULL)
293 for (thread = 0; thread < threads->nr; ++thread) {
294 if (__event__synthesize_thread(comm_event, mmap_event,
295 threads->map[thread],
296 process, ops, machine)) {
308 int perf_event__synthesize_threads(struct perf_event_ops *ops,
309 perf_event__handler_t process,
310 struct machine *machine)
313 struct dirent dirent, *next;
314 union perf_event *comm_event, *mmap_event;
317 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
318 if (comm_event == NULL)
321 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
322 if (mmap_event == NULL)
325 proc = opendir("/proc");
329 while (!readdir_r(proc, &dirent, &next) && next) {
331 pid_t pid = strtol(dirent.d_name, &end, 10);
333 if (*end) /* only interested in proper numerical dirents */
336 __event__synthesize_thread(comm_event, mmap_event, pid,
337 process, ops, machine);
350 struct process_symbol_args {
355 static int find_symbol_cb(void *arg, const char *name, char type,
356 u64 start, u64 end __used)
358 struct process_symbol_args *args = arg;
361 * Must be a function or at least an alias, as in PARISC64, where "_text" is
362 * an 'A' to the same address as "_stext".
364 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
365 type == 'A') || strcmp(name, args->name))
372 int perf_event__synthesize_kernel_mmap(struct perf_event_ops *ops,
373 perf_event__handler_t process,
374 struct machine *machine,
375 const char *symbol_name)
378 const char *filename, *mmap_name;
380 char name_buff[PATH_MAX];
384 * We should get this from /sys/kernel/sections/.text, but till that is
385 * available use this, and after it is use this as a fallback for older
388 struct process_symbol_args args = { .name = symbol_name, };
389 union perf_event *event = zalloc((sizeof(event->mmap) +
390 machine->id_hdr_size));
392 pr_debug("Not enough memory synthesizing mmap event "
393 "for kernel modules\n");
397 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
398 if (machine__is_host(machine)) {
400 * kernel uses PERF_RECORD_MISC_USER for user space maps,
401 * see kernel/perf_event.c __perf_event_mmap
403 event->header.misc = PERF_RECORD_MISC_KERNEL;
404 filename = "/proc/kallsyms";
406 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
407 if (machine__is_default_guest(machine))
408 filename = (char *) symbol_conf.default_guest_kallsyms;
410 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
415 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
418 map = machine->vmlinux_maps[MAP__FUNCTION];
419 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
420 "%s%s", mmap_name, symbol_name) + 1;
421 size = ALIGN(size, sizeof(u64));
422 event->mmap.header.type = PERF_RECORD_MMAP;
423 event->mmap.header.size = (sizeof(event->mmap) -
424 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
425 event->mmap.pgoff = args.start;
426 event->mmap.start = map->start;
427 event->mmap.len = map->end - event->mmap.start;
428 event->mmap.pid = machine->pid;
430 err = process(ops, event, &synth_sample, machine);
436 int perf_event__process_comm(struct perf_event_ops *ops __used,
437 union perf_event *event,
438 struct perf_sample *sample __used,
439 struct machine *machine)
441 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
443 dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
445 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
446 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
453 int perf_event__process_lost(struct perf_event_ops *ops __used,
454 union perf_event *event,
455 struct perf_sample *sample __used,
456 struct machine *machine __used)
458 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
459 event->lost.id, event->lost.lost);
463 static void perf_event__set_kernel_mmap_len(union perf_event *event,
466 maps[MAP__FUNCTION]->start = event->mmap.start;
467 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
469 * Be a bit paranoid here, some perf.data file came with
470 * a zero sized synthesized MMAP event for the kernel.
472 if (maps[MAP__FUNCTION]->end == 0)
473 maps[MAP__FUNCTION]->end = ~0ULL;
476 static int perf_event__process_kernel_mmap(struct perf_event_ops *ops __used,
477 union perf_event *event,
478 struct machine *machine)
481 char kmmap_prefix[PATH_MAX];
482 enum dso_kernel_type kernel_type;
485 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
486 if (machine__is_host(machine))
487 kernel_type = DSO_TYPE_KERNEL;
489 kernel_type = DSO_TYPE_GUEST_KERNEL;
491 is_kernel_mmap = memcmp(event->mmap.filename,
493 strlen(kmmap_prefix)) == 0;
494 if (event->mmap.filename[0] == '/' ||
495 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
497 char short_module_name[1024];
500 if (event->mmap.filename[0] == '/') {
501 name = strrchr(event->mmap.filename, '/');
506 dot = strrchr(name, '.');
509 snprintf(short_module_name, sizeof(short_module_name),
510 "[%.*s]", (int)(dot - name), name);
511 strxfrchar(short_module_name, '-', '_');
513 strcpy(short_module_name, event->mmap.filename);
515 map = machine__new_module(machine, event->mmap.start,
516 event->mmap.filename);
520 name = strdup(short_module_name);
524 map->dso->short_name = name;
525 map->dso->sname_alloc = 1;
526 map->end = map->start + event->mmap.len;
527 } else if (is_kernel_mmap) {
528 const char *symbol_name = (event->mmap.filename +
529 strlen(kmmap_prefix));
531 * Should be there already, from the build-id table in
534 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
539 kernel->kernel = kernel_type;
540 if (__machine__create_kernel_maps(machine, kernel) < 0)
543 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
546 * Avoid using a zero address (kptr_restrict) for the ref reloc
547 * symbol. Effectively having zero here means that at record
548 * time /proc/sys/kernel/kptr_restrict was non zero.
550 if (event->mmap.pgoff != 0) {
551 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
556 if (machine__is_default_guest(machine)) {
558 * preload dso of guest kernel and modules
560 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
569 int perf_event__process_mmap(struct perf_event_ops *ops,
570 union perf_event *event,
571 struct perf_sample *sample __used,
572 struct machine *machine)
574 struct thread *thread;
576 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
579 dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
580 event->mmap.pid, event->mmap.tid, event->mmap.start,
581 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
583 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
584 cpumode == PERF_RECORD_MISC_KERNEL) {
585 ret = perf_event__process_kernel_mmap(ops, event, machine);
591 thread = machine__findnew_thread(machine, event->mmap.pid);
594 map = map__new(&machine->user_dsos, event->mmap.start,
595 event->mmap.len, event->mmap.pgoff,
596 event->mmap.pid, event->mmap.filename,
601 thread__insert_map(thread, map);
605 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
609 int perf_event__process_task(struct perf_event_ops *ops __used,
610 union perf_event *event,
611 struct perf_sample *sample __used,
612 struct machine *machine)
614 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
615 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
617 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
618 event->fork.ppid, event->fork.ptid);
620 if (event->header.type == PERF_RECORD_EXIT) {
621 machine__remove_thread(machine, thread);
625 if (thread == NULL || parent == NULL ||
626 thread__fork(thread, parent) < 0) {
627 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
634 int perf_event__process(struct perf_event_ops *ops, union perf_event *event,
635 struct perf_sample *sample, struct machine *machine)
637 switch (event->header.type) {
638 case PERF_RECORD_COMM:
639 perf_event__process_comm(ops, event, sample, machine);
641 case PERF_RECORD_MMAP:
642 perf_event__process_mmap(ops, event, sample, machine);
644 case PERF_RECORD_FORK:
645 case PERF_RECORD_EXIT:
646 perf_event__process_task(ops, event, sample, machine);
648 case PERF_RECORD_LOST:
649 perf_event__process_lost(ops, event, sample, machine);
657 void thread__find_addr_map(struct thread *self,
658 struct machine *machine, u8 cpumode,
659 enum map_type type, u64 addr,
660 struct addr_location *al)
662 struct map_groups *mg = &self->mg;
666 al->cpumode = cpumode;
667 al->filtered = false;
669 if (machine == NULL) {
674 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
676 mg = &machine->kmaps;
677 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
679 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
681 mg = &machine->kmaps;
684 * 'u' means guest os user space.
685 * TODO: We don't support guest user space. Might support late.
687 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
693 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
694 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
697 if ((cpumode == PERF_RECORD_MISC_USER ||
698 cpumode == PERF_RECORD_MISC_KERNEL) &&
705 al->map = map_groups__find(mg, type, al->addr);
706 if (al->map == NULL) {
708 * If this is outside of all known maps, and is a negative
709 * address, try to look it up in the kernel dso, as it might be
710 * a vsyscall or vdso (which executes in user-mode).
712 * XXX This is nasty, we should have a symbol list in the
713 * "[vdso]" dso, but for now lets use the old trick of looking
714 * in the whole kernel symbol list.
716 if ((long long)al->addr < 0 &&
717 cpumode == PERF_RECORD_MISC_USER &&
718 machine && mg != &machine->kmaps) {
719 mg = &machine->kmaps;
723 al->addr = al->map->map_ip(al->map, al->addr);
726 void thread__find_addr_location(struct thread *thread, struct machine *machine,
727 u8 cpumode, enum map_type type, u64 addr,
728 struct addr_location *al,
729 symbol_filter_t filter)
731 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
733 al->sym = map__find_symbol(al->map, al->addr, filter);
738 int perf_event__preprocess_sample(const union perf_event *event,
739 struct machine *machine,
740 struct addr_location *al,
741 struct perf_sample *sample,
742 symbol_filter_t filter)
744 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
745 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
750 if (symbol_conf.comm_list &&
751 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
754 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
756 * Have we already created the kernel maps for this machine?
758 * This should have happened earlier, when we processed the kernel MMAP
759 * events, but for older perf.data files there was no such thing, so do
762 if (cpumode == PERF_RECORD_MISC_KERNEL &&
763 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
764 machine__create_kernel_maps(machine);
766 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
768 dump_printf(" ...... dso: %s\n",
769 al->map ? al->map->dso->long_name :
770 al->level == 'H' ? "[hypervisor]" : "<not found>");
772 al->cpu = sample->cpu;
775 if (symbol_conf.dso_list &&
776 (!al->map || !al->map->dso ||
777 !(strlist__has_entry(symbol_conf.dso_list,
778 al->map->dso->short_name) ||
779 (al->map->dso->short_name != al->map->dso->long_name &&
780 strlist__has_entry(symbol_conf.dso_list,
781 al->map->dso->long_name)))))
784 al->sym = map__find_symbol(al->map, al->addr, filter);
787 if (symbol_conf.sym_list && al->sym &&
788 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))