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 static struct perf_sample synth_sample = {
47 static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
49 char filename[PATH_MAX];
55 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
57 fp = fopen(filename, "r");
59 pr_debug("couldn't open %s\n", filename);
63 while (!comm[0] || (tgid < 0)) {
64 if (fgets(bf, sizeof(bf), fp) == NULL) {
65 pr_warning("couldn't get COMM and pgid, malformed %s\n",
70 if (memcmp(bf, "Name:", 5) == 0) {
72 while (*name && isspace(*name))
74 size = strlen(name) - 1;
77 memcpy(comm, name, size);
80 } else if (memcmp(bf, "Tgid:", 5) == 0) {
82 while (*tgids && isspace(*tgids))
93 static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
94 union perf_event *event, pid_t pid,
96 perf_event__handler_t process,
97 struct machine *machine)
99 char filename[PATH_MAX];
102 struct dirent dirent, *next;
105 memset(&event->comm, 0, sizeof(event->comm));
107 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
108 sizeof(event->comm.comm));
112 event->comm.pid = tgid;
113 event->comm.header.type = PERF_RECORD_COMM;
115 size = strlen(event->comm.comm) + 1;
116 size = PERF_ALIGN(size, sizeof(u64));
117 memset(event->comm.comm + size, 0, machine->id_hdr_size);
118 event->comm.header.size = (sizeof(event->comm) -
119 (sizeof(event->comm.comm) - size) +
120 machine->id_hdr_size);
122 event->comm.tid = pid;
124 if (process(tool, event, &synth_sample, machine) != 0)
130 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
132 tasks = opendir(filename);
134 pr_debug("couldn't open %s\n", filename);
138 while (!readdir_r(tasks, &dirent, &next) && next) {
140 pid = strtol(dirent.d_name, &end, 10);
144 /* already have tgid; jut want to update the comm */
145 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
146 sizeof(event->comm.comm));
148 size = strlen(event->comm.comm) + 1;
149 size = PERF_ALIGN(size, sizeof(u64));
150 memset(event->comm.comm + size, 0, machine->id_hdr_size);
151 event->comm.header.size = (sizeof(event->comm) -
152 (sizeof(event->comm.comm) - size) +
153 machine->id_hdr_size);
155 event->comm.tid = pid;
157 if (process(tool, event, &synth_sample, machine) != 0) {
168 static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
169 union perf_event *event,
170 pid_t pid, pid_t tgid,
171 perf_event__handler_t process,
172 struct machine *machine)
174 char filename[PATH_MAX];
178 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
180 fp = fopen(filename, "r");
183 * We raced with a task exiting - just return:
185 pr_debug("couldn't open %s\n", filename);
189 event->header.type = PERF_RECORD_MMAP;
191 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
193 event->header.misc = PERF_RECORD_MISC_USER;
198 char execname[PATH_MAX];
199 char anonstr[] = "//anon";
202 if (fgets(bf, sizeof(bf), fp) == NULL)
205 /* ensure null termination since stack will be reused. */
206 strcpy(execname, "");
208 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
209 sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
210 &event->mmap.start, &event->mmap.len, prot,
211 &event->mmap.pgoff, execname);
216 if (!strcmp(execname, ""))
217 strcpy(execname, anonstr);
219 size = strlen(execname) + 1;
220 memcpy(event->mmap.filename, execname, size);
221 size = PERF_ALIGN(size, sizeof(u64));
222 event->mmap.len -= event->mmap.start;
223 event->mmap.header.size = (sizeof(event->mmap) -
224 (sizeof(event->mmap.filename) - size));
225 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
226 event->mmap.header.size += machine->id_hdr_size;
227 event->mmap.pid = tgid;
228 event->mmap.tid = pid;
230 if (process(tool, event, &synth_sample, machine) != 0) {
240 int perf_event__synthesize_modules(struct perf_tool *tool,
241 perf_event__handler_t process,
242 struct machine *machine)
246 struct map_groups *kmaps = &machine->kmaps;
247 union perf_event *event = zalloc((sizeof(event->mmap) +
248 machine->id_hdr_size));
250 pr_debug("Not enough memory synthesizing mmap event "
251 "for kernel modules\n");
255 event->header.type = PERF_RECORD_MMAP;
258 * kernel uses 0 for user space maps, see kernel/perf_event.c
261 if (machine__is_host(machine))
262 event->header.misc = PERF_RECORD_MISC_KERNEL;
264 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
266 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
267 nd; nd = rb_next(nd)) {
269 struct map *pos = rb_entry(nd, struct map, rb_node);
271 if (pos->dso->kernel)
274 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
275 event->mmap.header.type = PERF_RECORD_MMAP;
276 event->mmap.header.size = (sizeof(event->mmap) -
277 (sizeof(event->mmap.filename) - size));
278 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
279 event->mmap.header.size += machine->id_hdr_size;
280 event->mmap.start = pos->start;
281 event->mmap.len = pos->end - pos->start;
282 event->mmap.pid = machine->pid;
284 memcpy(event->mmap.filename, pos->dso->long_name,
285 pos->dso->long_name_len + 1);
286 if (process(tool, event, &synth_sample, machine) != 0) {
296 static int __event__synthesize_thread(union perf_event *comm_event,
297 union perf_event *mmap_event,
299 perf_event__handler_t process,
300 struct perf_tool *tool,
301 struct machine *machine)
303 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
307 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
311 int perf_event__synthesize_thread_map(struct perf_tool *tool,
312 struct thread_map *threads,
313 perf_event__handler_t process,
314 struct machine *machine)
316 union perf_event *comm_event, *mmap_event;
317 int err = -1, thread, j;
319 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
320 if (comm_event == NULL)
323 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
324 if (mmap_event == NULL)
328 for (thread = 0; thread < threads->nr; ++thread) {
329 if (__event__synthesize_thread(comm_event, mmap_event,
330 threads->map[thread], 0,
331 process, tool, machine)) {
337 * comm.pid is set to thread group id by
338 * perf_event__synthesize_comm
340 if ((int) comm_event->comm.pid != threads->map[thread]) {
341 bool need_leader = true;
343 /* is thread group leader in thread_map? */
344 for (j = 0; j < threads->nr; ++j) {
345 if ((int) comm_event->comm.pid == threads->map[j]) {
351 /* if not, generate events for it */
353 __event__synthesize_thread(comm_event,
355 comm_event->comm.pid, 0,
356 process, tool, machine)) {
369 int perf_event__synthesize_threads(struct perf_tool *tool,
370 perf_event__handler_t process,
371 struct machine *machine)
374 struct dirent dirent, *next;
375 union perf_event *comm_event, *mmap_event;
378 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
379 if (comm_event == NULL)
382 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
383 if (mmap_event == NULL)
386 proc = opendir("/proc");
390 while (!readdir_r(proc, &dirent, &next) && next) {
392 pid_t pid = strtol(dirent.d_name, &end, 10);
394 if (*end) /* only interested in proper numerical dirents */
397 * We may race with exiting thread, so don't stop just because
398 * one thread couldn't be synthesized.
400 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
401 process, tool, machine);
414 struct process_symbol_args {
419 static int find_symbol_cb(void *arg, const char *name, char type,
422 struct process_symbol_args *args = arg;
425 * Must be a function or at least an alias, as in PARISC64, where "_text" is
426 * an 'A' to the same address as "_stext".
428 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
429 type == 'A') || strcmp(name, args->name))
436 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
437 perf_event__handler_t process,
438 struct machine *machine,
439 const char *symbol_name)
442 const char *filename, *mmap_name;
444 char name_buff[PATH_MAX];
448 * We should get this from /sys/kernel/sections/.text, but till that is
449 * available use this, and after it is use this as a fallback for older
452 struct process_symbol_args args = { .name = symbol_name, };
453 union perf_event *event = zalloc((sizeof(event->mmap) +
454 machine->id_hdr_size));
456 pr_debug("Not enough memory synthesizing mmap event "
457 "for kernel modules\n");
461 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
462 if (machine__is_host(machine)) {
464 * kernel uses PERF_RECORD_MISC_USER for user space maps,
465 * see kernel/perf_event.c __perf_event_mmap
467 event->header.misc = PERF_RECORD_MISC_KERNEL;
468 filename = "/proc/kallsyms";
470 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
471 if (machine__is_default_guest(machine))
472 filename = (char *) symbol_conf.default_guest_kallsyms;
474 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
479 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
484 map = machine->vmlinux_maps[MAP__FUNCTION];
485 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
486 "%s%s", mmap_name, symbol_name) + 1;
487 size = PERF_ALIGN(size, sizeof(u64));
488 event->mmap.header.type = PERF_RECORD_MMAP;
489 event->mmap.header.size = (sizeof(event->mmap) -
490 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
491 event->mmap.pgoff = args.start;
492 event->mmap.start = map->start;
493 event->mmap.len = map->end - event->mmap.start;
494 event->mmap.pid = machine->pid;
496 err = process(tool, event, &synth_sample, machine);
502 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
504 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
507 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
508 union perf_event *event,
509 struct perf_sample *sample __maybe_unused,
510 struct machine *machine)
512 return machine__process_comm_event(machine, event);
515 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
516 union perf_event *event,
517 struct perf_sample *sample __maybe_unused,
518 struct machine *machine)
520 return machine__process_lost_event(machine, event);
523 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
525 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
526 event->mmap.pid, event->mmap.tid, event->mmap.start,
527 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
530 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
531 union perf_event *event,
532 struct perf_sample *sample __maybe_unused,
533 struct machine *machine)
535 return machine__process_mmap_event(machine, event);
538 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
540 return fprintf(fp, "(%d:%d):(%d:%d)\n",
541 event->fork.pid, event->fork.tid,
542 event->fork.ppid, event->fork.ptid);
545 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
546 union perf_event *event,
547 struct perf_sample *sample __maybe_unused,
548 struct machine *machine)
550 return machine__process_fork_event(machine, event);
553 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
554 union perf_event *event,
555 struct perf_sample *sample __maybe_unused,
556 struct machine *machine)
558 return machine__process_exit_event(machine, event);
561 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
563 size_t ret = fprintf(fp, "PERF_RECORD_%s",
564 perf_event__name(event->header.type));
566 switch (event->header.type) {
567 case PERF_RECORD_COMM:
568 ret += perf_event__fprintf_comm(event, fp);
570 case PERF_RECORD_FORK:
571 case PERF_RECORD_EXIT:
572 ret += perf_event__fprintf_task(event, fp);
574 case PERF_RECORD_MMAP:
575 ret += perf_event__fprintf_mmap(event, fp);
578 ret += fprintf(fp, "\n");
584 int perf_event__process(struct perf_tool *tool __maybe_unused,
585 union perf_event *event,
586 struct perf_sample *sample __maybe_unused,
587 struct machine *machine)
589 return machine__process_event(machine, event);
592 void thread__find_addr_map(struct thread *self,
593 struct machine *machine, u8 cpumode,
594 enum map_type type, u64 addr,
595 struct addr_location *al)
597 struct map_groups *mg = &self->mg;
598 bool load_map = false;
602 al->cpumode = cpumode;
603 al->filtered = false;
605 if (machine == NULL) {
610 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
612 mg = &machine->kmaps;
614 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
616 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
618 mg = &machine->kmaps;
622 * 'u' means guest os user space.
623 * TODO: We don't support guest user space. Might support late.
625 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
631 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
632 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
635 if ((cpumode == PERF_RECORD_MISC_USER ||
636 cpumode == PERF_RECORD_MISC_KERNEL) &&
643 al->map = map_groups__find(mg, type, al->addr);
644 if (al->map == NULL) {
646 * If this is outside of all known maps, and is a negative
647 * address, try to look it up in the kernel dso, as it might be
648 * a vsyscall or vdso (which executes in user-mode).
650 * XXX This is nasty, we should have a symbol list in the
651 * "[vdso]" dso, but for now lets use the old trick of looking
652 * in the whole kernel symbol list.
654 if ((long long)al->addr < 0 &&
655 cpumode == PERF_RECORD_MISC_USER &&
656 machine && mg != &machine->kmaps) {
657 mg = &machine->kmaps;
662 * Kernel maps might be changed when loading symbols so loading
663 * must be done prior to using kernel maps.
666 map__load(al->map, machine->symbol_filter);
667 al->addr = al->map->map_ip(al->map, al->addr);
671 void thread__find_addr_location(struct thread *thread, struct machine *machine,
672 u8 cpumode, enum map_type type, u64 addr,
673 struct addr_location *al)
675 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
677 al->sym = map__find_symbol(al->map, al->addr,
678 machine->symbol_filter);
683 int perf_event__preprocess_sample(const union perf_event *event,
684 struct machine *machine,
685 struct addr_location *al,
686 struct perf_sample *sample)
688 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
689 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
694 if (symbol_conf.comm_list &&
695 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
698 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->tid);
700 * Have we already created the kernel maps for this machine?
702 * This should have happened earlier, when we processed the kernel MMAP
703 * events, but for older perf.data files there was no such thing, so do
706 if (cpumode == PERF_RECORD_MISC_KERNEL &&
707 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
708 machine__create_kernel_maps(machine);
710 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
712 dump_printf(" ...... dso: %s\n",
713 al->map ? al->map->dso->long_name :
714 al->level == 'H' ? "[hypervisor]" : "<not found>");
716 al->cpu = sample->cpu;
719 struct dso *dso = al->map->dso;
721 if (symbol_conf.dso_list &&
722 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
724 (dso->short_name != dso->long_name &&
725 strlist__has_entry(symbol_conf.dso_list,
729 al->sym = map__find_symbol(al->map, al->addr,
730 machine->symbol_filter);
733 if (symbol_conf.sym_list &&
734 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,