1 #include <linux/types.h>
9 #include "thread_map.h"
10 #include "symbol/kallsyms.h"
12 static const char *perf_event__names[] = {
14 [PERF_RECORD_MMAP] = "MMAP",
15 [PERF_RECORD_MMAP2] = "MMAP2",
16 [PERF_RECORD_LOST] = "LOST",
17 [PERF_RECORD_COMM] = "COMM",
18 [PERF_RECORD_EXIT] = "EXIT",
19 [PERF_RECORD_THROTTLE] = "THROTTLE",
20 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
21 [PERF_RECORD_FORK] = "FORK",
22 [PERF_RECORD_READ] = "READ",
23 [PERF_RECORD_SAMPLE] = "SAMPLE",
24 [PERF_RECORD_HEADER_ATTR] = "ATTR",
25 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
26 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
27 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
28 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
31 const char *perf_event__name(unsigned int id)
33 if (id >= ARRAY_SIZE(perf_event__names))
35 if (!perf_event__names[id])
37 return perf_event__names[id];
40 static struct perf_sample synth_sample = {
49 static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
51 char filename[PATH_MAX];
57 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
59 fp = fopen(filename, "r");
61 pr_debug("couldn't open %s\n", filename);
65 while (!comm[0] || (tgid < 0)) {
66 if (fgets(bf, sizeof(bf), fp) == NULL) {
67 pr_warning("couldn't get COMM and pgid, malformed %s\n",
72 if (memcmp(bf, "Name:", 5) == 0) {
74 while (*name && isspace(*name))
76 size = strlen(name) - 1;
79 memcpy(comm, name, size);
82 } else if (memcmp(bf, "Tgid:", 5) == 0) {
84 while (*tgids && isspace(*tgids))
95 static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
96 union perf_event *event, pid_t pid,
98 perf_event__handler_t process,
99 struct machine *machine)
101 char filename[PATH_MAX];
104 struct dirent dirent, *next;
107 memset(&event->comm, 0, sizeof(event->comm));
109 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
110 sizeof(event->comm.comm));
114 event->comm.pid = tgid;
115 event->comm.header.type = PERF_RECORD_COMM;
117 size = strlen(event->comm.comm) + 1;
118 size = PERF_ALIGN(size, sizeof(u64));
119 memset(event->comm.comm + size, 0, machine->id_hdr_size);
120 event->comm.header.size = (sizeof(event->comm) -
121 (sizeof(event->comm.comm) - size) +
122 machine->id_hdr_size);
124 event->comm.tid = pid;
126 if (process(tool, event, &synth_sample, machine) != 0)
132 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
134 tasks = opendir(filename);
136 pr_debug("couldn't open %s\n", filename);
140 while (!readdir_r(tasks, &dirent, &next) && next) {
142 pid = strtol(dirent.d_name, &end, 10);
146 /* already have tgid; jut want to update the comm */
147 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
148 sizeof(event->comm.comm));
150 size = strlen(event->comm.comm) + 1;
151 size = PERF_ALIGN(size, sizeof(u64));
152 memset(event->comm.comm + size, 0, machine->id_hdr_size);
153 event->comm.header.size = (sizeof(event->comm) -
154 (sizeof(event->comm.comm) - size) +
155 machine->id_hdr_size);
157 event->comm.tid = pid;
159 if (process(tool, event, &synth_sample, machine) != 0) {
170 static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
171 union perf_event *event,
172 pid_t pid, pid_t tgid,
173 perf_event__handler_t process,
174 struct machine *machine,
177 char filename[PATH_MAX];
181 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
183 fp = fopen(filename, "r");
186 * We raced with a task exiting - just return:
188 pr_debug("couldn't open %s\n", filename);
192 event->header.type = PERF_RECORD_MMAP;
197 char execname[PATH_MAX];
198 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 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
210 &event->mmap.start, &event->mmap.len, prot,
214 * Anon maps don't have the execname.
219 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
221 event->header.misc = PERF_RECORD_MISC_USER;
223 if (prot[2] != 'x') {
224 if (!mmap_data || prot[0] != 'r')
227 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
230 if (!strcmp(execname, ""))
231 strcpy(execname, anonstr);
233 size = strlen(execname) + 1;
234 memcpy(event->mmap.filename, execname, size);
235 size = PERF_ALIGN(size, sizeof(u64));
236 event->mmap.len -= event->mmap.start;
237 event->mmap.header.size = (sizeof(event->mmap) -
238 (sizeof(event->mmap.filename) - size));
239 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
240 event->mmap.header.size += machine->id_hdr_size;
241 event->mmap.pid = tgid;
242 event->mmap.tid = pid;
244 if (process(tool, event, &synth_sample, machine) != 0) {
254 int perf_event__synthesize_modules(struct perf_tool *tool,
255 perf_event__handler_t process,
256 struct machine *machine)
260 struct map_groups *kmaps = &machine->kmaps;
261 union perf_event *event = zalloc((sizeof(event->mmap) +
262 machine->id_hdr_size));
264 pr_debug("Not enough memory synthesizing mmap event "
265 "for kernel modules\n");
269 event->header.type = PERF_RECORD_MMAP;
272 * kernel uses 0 for user space maps, see kernel/perf_event.c
275 if (machine__is_host(machine))
276 event->header.misc = PERF_RECORD_MISC_KERNEL;
278 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
280 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
281 nd; nd = rb_next(nd)) {
283 struct map *pos = rb_entry(nd, struct map, rb_node);
285 if (pos->dso->kernel)
288 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
289 event->mmap.header.type = PERF_RECORD_MMAP;
290 event->mmap.header.size = (sizeof(event->mmap) -
291 (sizeof(event->mmap.filename) - size));
292 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
293 event->mmap.header.size += machine->id_hdr_size;
294 event->mmap.start = pos->start;
295 event->mmap.len = pos->end - pos->start;
296 event->mmap.pid = machine->pid;
298 memcpy(event->mmap.filename, pos->dso->long_name,
299 pos->dso->long_name_len + 1);
300 if (process(tool, event, &synth_sample, machine) != 0) {
310 static int __event__synthesize_thread(union perf_event *comm_event,
311 union perf_event *mmap_event,
313 perf_event__handler_t process,
314 struct perf_tool *tool,
315 struct machine *machine, bool mmap_data)
317 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
321 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
322 process, machine, mmap_data);
325 int perf_event__synthesize_thread_map(struct perf_tool *tool,
326 struct thread_map *threads,
327 perf_event__handler_t process,
328 struct machine *machine,
331 union perf_event *comm_event, *mmap_event;
332 int err = -1, thread, j;
334 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
335 if (comm_event == NULL)
338 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
339 if (mmap_event == NULL)
343 for (thread = 0; thread < threads->nr; ++thread) {
344 if (__event__synthesize_thread(comm_event, mmap_event,
345 threads->map[thread], 0,
346 process, tool, machine,
353 * comm.pid is set to thread group id by
354 * perf_event__synthesize_comm
356 if ((int) comm_event->comm.pid != threads->map[thread]) {
357 bool need_leader = true;
359 /* is thread group leader in thread_map? */
360 for (j = 0; j < threads->nr; ++j) {
361 if ((int) comm_event->comm.pid == threads->map[j]) {
367 /* if not, generate events for it */
369 __event__synthesize_thread(comm_event, mmap_event,
370 comm_event->comm.pid, 0,
371 process, tool, machine,
385 int perf_event__synthesize_threads(struct perf_tool *tool,
386 perf_event__handler_t process,
387 struct machine *machine, bool mmap_data)
390 struct dirent dirent, *next;
391 union perf_event *comm_event, *mmap_event;
394 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
395 if (comm_event == NULL)
398 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
399 if (mmap_event == NULL)
402 proc = opendir("/proc");
406 while (!readdir_r(proc, &dirent, &next) && next) {
408 pid_t pid = strtol(dirent.d_name, &end, 10);
410 if (*end) /* only interested in proper numerical dirents */
413 * We may race with exiting thread, so don't stop just because
414 * one thread couldn't be synthesized.
416 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
417 process, tool, machine, mmap_data);
430 struct process_symbol_args {
435 static int find_symbol_cb(void *arg, const char *name, char type,
438 struct process_symbol_args *args = arg;
441 * Must be a function or at least an alias, as in PARISC64, where "_text" is
442 * an 'A' to the same address as "_stext".
444 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
445 type == 'A') || strcmp(name, args->name))
452 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
453 perf_event__handler_t process,
454 struct machine *machine,
455 const char *symbol_name)
458 const char *filename, *mmap_name;
460 char name_buff[PATH_MAX];
464 * We should get this from /sys/kernel/sections/.text, but till that is
465 * available use this, and after it is use this as a fallback for older
468 struct process_symbol_args args = { .name = symbol_name, };
469 union perf_event *event = zalloc((sizeof(event->mmap) +
470 machine->id_hdr_size));
472 pr_debug("Not enough memory synthesizing mmap event "
473 "for kernel modules\n");
477 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
478 if (machine__is_host(machine)) {
480 * kernel uses PERF_RECORD_MISC_USER for user space maps,
481 * see kernel/perf_event.c __perf_event_mmap
483 event->header.misc = PERF_RECORD_MISC_KERNEL;
484 filename = "/proc/kallsyms";
486 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
487 if (machine__is_default_guest(machine))
488 filename = (char *) symbol_conf.default_guest_kallsyms;
490 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
495 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
500 map = machine->vmlinux_maps[MAP__FUNCTION];
501 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
502 "%s%s", mmap_name, symbol_name) + 1;
503 size = PERF_ALIGN(size, sizeof(u64));
504 event->mmap.header.type = PERF_RECORD_MMAP;
505 event->mmap.header.size = (sizeof(event->mmap) -
506 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
507 event->mmap.pgoff = args.start;
508 event->mmap.start = map->start;
509 event->mmap.len = map->end - event->mmap.start;
510 event->mmap.pid = machine->pid;
512 err = process(tool, event, &synth_sample, machine);
518 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
520 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
523 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
524 union perf_event *event,
525 struct perf_sample *sample,
526 struct machine *machine)
528 return machine__process_comm_event(machine, event, sample);
531 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
532 union perf_event *event,
533 struct perf_sample *sample,
534 struct machine *machine)
536 return machine__process_lost_event(machine, event, sample);
539 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
541 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
542 event->mmap.pid, event->mmap.tid, event->mmap.start,
543 event->mmap.len, event->mmap.pgoff,
544 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
545 event->mmap.filename);
548 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
550 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
551 " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
552 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
553 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
554 event->mmap2.min, event->mmap2.ino,
555 event->mmap2.ino_generation,
556 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
557 event->mmap2.filename);
560 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
561 union perf_event *event,
562 struct perf_sample *sample,
563 struct machine *machine)
565 return machine__process_mmap_event(machine, event, sample);
568 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
569 union perf_event *event,
570 struct perf_sample *sample,
571 struct machine *machine)
573 return machine__process_mmap2_event(machine, event, sample);
576 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
578 return fprintf(fp, "(%d:%d):(%d:%d)\n",
579 event->fork.pid, event->fork.tid,
580 event->fork.ppid, event->fork.ptid);
583 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
584 union perf_event *event,
585 struct perf_sample *sample,
586 struct machine *machine)
588 return machine__process_fork_event(machine, event, sample);
591 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
592 union perf_event *event,
593 struct perf_sample *sample,
594 struct machine *machine)
596 return machine__process_exit_event(machine, event, sample);
599 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
601 size_t ret = fprintf(fp, "PERF_RECORD_%s",
602 perf_event__name(event->header.type));
604 switch (event->header.type) {
605 case PERF_RECORD_COMM:
606 ret += perf_event__fprintf_comm(event, fp);
608 case PERF_RECORD_FORK:
609 case PERF_RECORD_EXIT:
610 ret += perf_event__fprintf_task(event, fp);
612 case PERF_RECORD_MMAP:
613 ret += perf_event__fprintf_mmap(event, fp);
615 case PERF_RECORD_MMAP2:
616 ret += perf_event__fprintf_mmap2(event, fp);
619 ret += fprintf(fp, "\n");
625 int perf_event__process(struct perf_tool *tool __maybe_unused,
626 union perf_event *event,
627 struct perf_sample *sample,
628 struct machine *machine)
630 return machine__process_event(machine, event, sample);
633 void thread__find_addr_map(struct thread *thread,
634 struct machine *machine, u8 cpumode,
635 enum map_type type, u64 addr,
636 struct addr_location *al)
638 struct map_groups *mg = &thread->mg;
639 bool load_map = false;
641 al->machine = machine;
644 al->cpumode = cpumode;
645 al->filtered = false;
647 if (machine == NULL) {
652 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
654 mg = &machine->kmaps;
656 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
658 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
660 mg = &machine->kmaps;
662 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
668 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
669 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
672 if ((cpumode == PERF_RECORD_MISC_USER ||
673 cpumode == PERF_RECORD_MISC_KERNEL) &&
680 al->map = map_groups__find(mg, type, al->addr);
681 if (al->map == NULL) {
683 * If this is outside of all known maps, and is a negative
684 * address, try to look it up in the kernel dso, as it might be
685 * a vsyscall or vdso (which executes in user-mode).
687 * XXX This is nasty, we should have a symbol list in the
688 * "[vdso]" dso, but for now lets use the old trick of looking
689 * in the whole kernel symbol list.
691 if ((long long)al->addr < 0 &&
692 cpumode == PERF_RECORD_MISC_USER &&
693 machine && mg != &machine->kmaps) {
694 mg = &machine->kmaps;
699 * Kernel maps might be changed when loading symbols so loading
700 * must be done prior to using kernel maps.
703 map__load(al->map, machine->symbol_filter);
704 al->addr = al->map->map_ip(al->map, al->addr);
708 void thread__find_addr_location(struct thread *thread, struct machine *machine,
709 u8 cpumode, enum map_type type, u64 addr,
710 struct addr_location *al)
712 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
714 al->sym = map__find_symbol(al->map, al->addr,
715 machine->symbol_filter);
720 int perf_event__preprocess_sample(const union perf_event *event,
721 struct machine *machine,
722 struct addr_location *al,
723 struct perf_sample *sample)
725 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
726 struct thread *thread = machine__findnew_thread(machine, sample->pid,
732 if (thread__is_filtered(thread))
735 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
737 * Have we already created the kernel maps for this machine?
739 * This should have happened earlier, when we processed the kernel MMAP
740 * events, but for older perf.data files there was no such thing, so do
743 if (cpumode == PERF_RECORD_MISC_KERNEL &&
744 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
745 machine__create_kernel_maps(machine);
747 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
749 dump_printf(" ...... dso: %s\n",
750 al->map ? al->map->dso->long_name :
751 al->level == 'H' ? "[hypervisor]" : "<not found>");
753 al->cpu = sample->cpu;
756 struct dso *dso = al->map->dso;
758 if (symbol_conf.dso_list &&
759 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
761 (dso->short_name != dso->long_name &&
762 strlist__has_entry(symbol_conf.dso_list,
766 al->sym = map__find_symbol(al->map, al->addr,
767 machine->symbol_filter);
770 if (symbol_conf.sym_list &&
771 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,