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_MMAP2] = "MMAP2",
15 [PERF_RECORD_LOST] = "LOST",
16 [PERF_RECORD_COMM] = "COMM",
17 [PERF_RECORD_EXIT] = "EXIT",
18 [PERF_RECORD_THROTTLE] = "THROTTLE",
19 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
20 [PERF_RECORD_FORK] = "FORK",
21 [PERF_RECORD_READ] = "READ",
22 [PERF_RECORD_SAMPLE] = "SAMPLE",
23 [PERF_RECORD_HEADER_ATTR] = "ATTR",
24 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
25 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
26 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
27 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
30 const char *perf_event__name(unsigned int id)
32 if (id >= ARRAY_SIZE(perf_event__names))
34 if (!perf_event__names[id])
36 return perf_event__names[id];
39 static struct perf_sample synth_sample = {
48 static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
50 char filename[PATH_MAX];
56 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
58 fp = fopen(filename, "r");
60 pr_debug("couldn't open %s\n", filename);
64 while (!comm[0] || (tgid < 0)) {
65 if (fgets(bf, sizeof(bf), fp) == NULL) {
66 pr_warning("couldn't get COMM and pgid, malformed %s\n",
71 if (memcmp(bf, "Name:", 5) == 0) {
73 while (*name && isspace(*name))
75 size = strlen(name) - 1;
78 memcpy(comm, name, size);
81 } else if (memcmp(bf, "Tgid:", 5) == 0) {
83 while (*tgids && isspace(*tgids))
94 static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
95 union perf_event *event, pid_t pid,
97 perf_event__handler_t process,
98 struct machine *machine)
100 char filename[PATH_MAX];
103 struct dirent dirent, *next;
106 memset(&event->comm, 0, sizeof(event->comm));
108 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
109 sizeof(event->comm.comm));
113 event->comm.pid = tgid;
114 event->comm.header.type = PERF_RECORD_COMM;
116 size = strlen(event->comm.comm) + 1;
117 size = PERF_ALIGN(size, sizeof(u64));
118 memset(event->comm.comm + size, 0, machine->id_hdr_size);
119 event->comm.header.size = (sizeof(event->comm) -
120 (sizeof(event->comm.comm) - size) +
121 machine->id_hdr_size);
123 event->comm.tid = pid;
125 if (process(tool, event, &synth_sample, machine) != 0)
131 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
133 tasks = opendir(filename);
135 pr_debug("couldn't open %s\n", filename);
139 while (!readdir_r(tasks, &dirent, &next) && next) {
141 pid = strtol(dirent.d_name, &end, 10);
145 /* already have tgid; jut want to update the comm */
146 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
147 sizeof(event->comm.comm));
149 size = strlen(event->comm.comm) + 1;
150 size = PERF_ALIGN(size, sizeof(u64));
151 memset(event->comm.comm + size, 0, machine->id_hdr_size);
152 event->comm.header.size = (sizeof(event->comm) -
153 (sizeof(event->comm.comm) - size) +
154 machine->id_hdr_size);
156 event->comm.tid = pid;
158 if (process(tool, event, &synth_sample, machine) != 0) {
169 static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
170 union perf_event *event,
171 pid_t pid, pid_t tgid,
172 perf_event__handler_t process,
173 struct machine *machine,
176 char filename[PATH_MAX];
180 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
182 fp = fopen(filename, "r");
185 * We raced with a task exiting - just return:
187 pr_debug("couldn't open %s\n", filename);
191 event->header.type = PERF_RECORD_MMAP;
196 char execname[PATH_MAX];
197 char anonstr[] = "//anon";
201 if (fgets(bf, sizeof(bf), fp) == NULL)
204 /* ensure null termination since stack will be reused. */
205 strcpy(execname, "");
207 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
208 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
209 &event->mmap.start, &event->mmap.len, prot,
216 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
218 event->header.misc = PERF_RECORD_MISC_USER;
220 if (prot[2] != 'x') {
221 if (!mmap_data || prot[0] != 'r')
224 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
227 if (!strcmp(execname, ""))
228 strcpy(execname, anonstr);
230 size = strlen(execname) + 1;
231 memcpy(event->mmap.filename, execname, size);
232 size = PERF_ALIGN(size, sizeof(u64));
233 event->mmap.len -= event->mmap.start;
234 event->mmap.header.size = (sizeof(event->mmap) -
235 (sizeof(event->mmap.filename) - size));
236 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
237 event->mmap.header.size += machine->id_hdr_size;
238 event->mmap.pid = tgid;
239 event->mmap.tid = pid;
241 if (process(tool, event, &synth_sample, machine) != 0) {
251 int perf_event__synthesize_modules(struct perf_tool *tool,
252 perf_event__handler_t process,
253 struct machine *machine)
257 struct map_groups *kmaps = &machine->kmaps;
258 union perf_event *event = zalloc((sizeof(event->mmap) +
259 machine->id_hdr_size));
261 pr_debug("Not enough memory synthesizing mmap event "
262 "for kernel modules\n");
266 event->header.type = PERF_RECORD_MMAP;
269 * kernel uses 0 for user space maps, see kernel/perf_event.c
272 if (machine__is_host(machine))
273 event->header.misc = PERF_RECORD_MISC_KERNEL;
275 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
277 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
278 nd; nd = rb_next(nd)) {
280 struct map *pos = rb_entry(nd, struct map, rb_node);
282 if (pos->dso->kernel)
285 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
286 event->mmap.header.type = PERF_RECORD_MMAP;
287 event->mmap.header.size = (sizeof(event->mmap) -
288 (sizeof(event->mmap.filename) - size));
289 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
290 event->mmap.header.size += machine->id_hdr_size;
291 event->mmap.start = pos->start;
292 event->mmap.len = pos->end - pos->start;
293 event->mmap.pid = machine->pid;
295 memcpy(event->mmap.filename, pos->dso->long_name,
296 pos->dso->long_name_len + 1);
297 if (process(tool, event, &synth_sample, machine) != 0) {
307 static int __event__synthesize_thread(union perf_event *comm_event,
308 union perf_event *mmap_event,
310 perf_event__handler_t process,
311 struct perf_tool *tool,
312 struct machine *machine, bool mmap_data)
314 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
318 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
319 process, machine, mmap_data);
322 int perf_event__synthesize_thread_map(struct perf_tool *tool,
323 struct thread_map *threads,
324 perf_event__handler_t process,
325 struct machine *machine,
328 union perf_event *comm_event, *mmap_event;
329 int err = -1, thread, j;
331 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
332 if (comm_event == NULL)
335 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
336 if (mmap_event == NULL)
340 for (thread = 0; thread < threads->nr; ++thread) {
341 if (__event__synthesize_thread(comm_event, mmap_event,
342 threads->map[thread], 0,
343 process, tool, machine,
350 * comm.pid is set to thread group id by
351 * perf_event__synthesize_comm
353 if ((int) comm_event->comm.pid != threads->map[thread]) {
354 bool need_leader = true;
356 /* is thread group leader in thread_map? */
357 for (j = 0; j < threads->nr; ++j) {
358 if ((int) comm_event->comm.pid == threads->map[j]) {
364 /* if not, generate events for it */
366 __event__synthesize_thread(comm_event, mmap_event,
367 comm_event->comm.pid, 0,
368 process, tool, machine,
382 int perf_event__synthesize_threads(struct perf_tool *tool,
383 perf_event__handler_t process,
384 struct machine *machine, bool mmap_data)
387 struct dirent dirent, *next;
388 union perf_event *comm_event, *mmap_event;
391 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
392 if (comm_event == NULL)
395 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
396 if (mmap_event == NULL)
399 proc = opendir("/proc");
403 while (!readdir_r(proc, &dirent, &next) && next) {
405 pid_t pid = strtol(dirent.d_name, &end, 10);
407 if (*end) /* only interested in proper numerical dirents */
410 * We may race with exiting thread, so don't stop just because
411 * one thread couldn't be synthesized.
413 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
414 process, tool, machine, mmap_data);
427 struct process_symbol_args {
432 static int find_symbol_cb(void *arg, const char *name, char type,
435 struct process_symbol_args *args = arg;
438 * Must be a function or at least an alias, as in PARISC64, where "_text" is
439 * an 'A' to the same address as "_stext".
441 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
442 type == 'A') || strcmp(name, args->name))
449 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
450 perf_event__handler_t process,
451 struct machine *machine,
452 const char *symbol_name)
455 const char *filename, *mmap_name;
457 char name_buff[PATH_MAX];
461 * We should get this from /sys/kernel/sections/.text, but till that is
462 * available use this, and after it is use this as a fallback for older
465 struct process_symbol_args args = { .name = symbol_name, };
466 union perf_event *event = zalloc((sizeof(event->mmap) +
467 machine->id_hdr_size));
469 pr_debug("Not enough memory synthesizing mmap event "
470 "for kernel modules\n");
474 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
475 if (machine__is_host(machine)) {
477 * kernel uses PERF_RECORD_MISC_USER for user space maps,
478 * see kernel/perf_event.c __perf_event_mmap
480 event->header.misc = PERF_RECORD_MISC_KERNEL;
481 filename = "/proc/kallsyms";
483 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
484 if (machine__is_default_guest(machine))
485 filename = (char *) symbol_conf.default_guest_kallsyms;
487 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
492 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
497 map = machine->vmlinux_maps[MAP__FUNCTION];
498 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
499 "%s%s", mmap_name, symbol_name) + 1;
500 size = PERF_ALIGN(size, sizeof(u64));
501 event->mmap.header.type = PERF_RECORD_MMAP;
502 event->mmap.header.size = (sizeof(event->mmap) -
503 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
504 event->mmap.pgoff = args.start;
505 event->mmap.start = map->start;
506 event->mmap.len = map->end - event->mmap.start;
507 event->mmap.pid = machine->pid;
509 err = process(tool, event, &synth_sample, machine);
515 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
517 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
520 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
521 union perf_event *event,
522 struct perf_sample *sample,
523 struct machine *machine)
525 return machine__process_comm_event(machine, event, sample);
528 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
529 union perf_event *event,
530 struct perf_sample *sample,
531 struct machine *machine)
533 return machine__process_lost_event(machine, event, sample);
536 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
538 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
539 event->mmap.pid, event->mmap.tid, event->mmap.start,
540 event->mmap.len, event->mmap.pgoff,
541 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
542 event->mmap.filename);
545 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
547 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
548 " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
549 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
550 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
551 event->mmap2.min, event->mmap2.ino,
552 event->mmap2.ino_generation,
553 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
554 event->mmap2.filename);
557 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
558 union perf_event *event,
559 struct perf_sample *sample,
560 struct machine *machine)
562 return machine__process_mmap_event(machine, event, sample);
565 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
566 union perf_event *event,
567 struct perf_sample *sample,
568 struct machine *machine)
570 return machine__process_mmap2_event(machine, event, sample);
573 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
575 return fprintf(fp, "(%d:%d):(%d:%d)\n",
576 event->fork.pid, event->fork.tid,
577 event->fork.ppid, event->fork.ptid);
580 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
581 union perf_event *event,
582 struct perf_sample *sample,
583 struct machine *machine)
585 return machine__process_fork_event(machine, event, sample);
588 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
589 union perf_event *event,
590 struct perf_sample *sample,
591 struct machine *machine)
593 return machine__process_exit_event(machine, event, sample);
596 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
598 size_t ret = fprintf(fp, "PERF_RECORD_%s",
599 perf_event__name(event->header.type));
601 switch (event->header.type) {
602 case PERF_RECORD_COMM:
603 ret += perf_event__fprintf_comm(event, fp);
605 case PERF_RECORD_FORK:
606 case PERF_RECORD_EXIT:
607 ret += perf_event__fprintf_task(event, fp);
609 case PERF_RECORD_MMAP:
610 ret += perf_event__fprintf_mmap(event, fp);
612 case PERF_RECORD_MMAP2:
613 ret += perf_event__fprintf_mmap2(event, fp);
616 ret += fprintf(fp, "\n");
622 int perf_event__process(struct perf_tool *tool __maybe_unused,
623 union perf_event *event,
624 struct perf_sample *sample,
625 struct machine *machine)
627 return machine__process_event(machine, event, sample);
630 void thread__find_addr_map(struct thread *thread,
631 struct machine *machine, u8 cpumode,
632 enum map_type type, u64 addr,
633 struct addr_location *al)
635 struct map_groups *mg = &thread->mg;
636 bool load_map = false;
640 al->cpumode = cpumode;
641 al->filtered = false;
643 if (machine == NULL) {
648 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
650 mg = &machine->kmaps;
652 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
654 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
656 mg = &machine->kmaps;
660 * 'u' means guest os user space.
661 * TODO: We don't support guest user space. Might support late.
663 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
669 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
670 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
673 if ((cpumode == PERF_RECORD_MISC_USER ||
674 cpumode == PERF_RECORD_MISC_KERNEL) &&
681 al->map = map_groups__find(mg, type, al->addr);
682 if (al->map == NULL) {
684 * If this is outside of all known maps, and is a negative
685 * address, try to look it up in the kernel dso, as it might be
686 * a vsyscall or vdso (which executes in user-mode).
688 * XXX This is nasty, we should have a symbol list in the
689 * "[vdso]" dso, but for now lets use the old trick of looking
690 * in the whole kernel symbol list.
692 if ((long long)al->addr < 0 &&
693 cpumode == PERF_RECORD_MISC_USER &&
694 machine && mg != &machine->kmaps) {
695 mg = &machine->kmaps;
700 * Kernel maps might be changed when loading symbols so loading
701 * must be done prior to using kernel maps.
704 map__load(al->map, machine->symbol_filter);
705 al->addr = al->map->map_ip(al->map, al->addr);
709 void thread__find_addr_location(struct thread *thread, struct machine *machine,
710 u8 cpumode, enum map_type type, u64 addr,
711 struct addr_location *al)
713 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
715 al->sym = map__find_symbol(al->map, al->addr,
716 machine->symbol_filter);
721 int perf_event__preprocess_sample(const union perf_event *event,
722 struct machine *machine,
723 struct addr_location *al,
724 struct perf_sample *sample)
726 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
727 struct thread *thread = machine__findnew_thread(machine, sample->pid,
733 if (symbol_conf.comm_list &&
734 !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread)))
737 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
739 * Have we already created the kernel maps for this machine?
741 * This should have happened earlier, when we processed the kernel MMAP
742 * events, but for older perf.data files there was no such thing, so do
745 if (cpumode == PERF_RECORD_MISC_KERNEL &&
746 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
747 machine__create_kernel_maps(machine);
749 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
751 dump_printf(" ...... dso: %s\n",
752 al->map ? al->map->dso->long_name :
753 al->level == 'H' ? "[hypervisor]" : "<not found>");
755 al->cpu = sample->cpu;
758 struct dso *dso = al->map->dso;
760 if (symbol_conf.dso_list &&
761 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
763 (dso->short_name != dso->long_name &&
764 strlist__has_entry(symbol_conf.dso_list,
768 al->sym = map__find_symbol(al->map, al->addr,
769 machine->symbol_filter);
772 if (symbol_conf.sym_list &&
773 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,