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,
97 perf_event__handler_t process,
98 struct machine *machine)
103 memset(&event->comm, 0, sizeof(event->comm));
105 if (machine__is_host(machine))
106 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
107 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);
123 event->comm.tid = pid;
125 if (process(tool, event, &synth_sample, machine) != 0)
132 static int perf_event__synthesize_fork(struct perf_tool *tool,
133 union perf_event *event, pid_t pid,
134 pid_t tgid, perf_event__handler_t process,
135 struct machine *machine)
137 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
139 /* this is really a clone event but we use fork to synthesize it */
140 event->fork.ppid = tgid;
141 event->fork.ptid = tgid;
142 event->fork.pid = tgid;
143 event->fork.tid = pid;
144 event->fork.header.type = PERF_RECORD_FORK;
146 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
148 if (process(tool, event, &synth_sample, machine) != 0)
154 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
155 union perf_event *event,
156 pid_t pid, pid_t tgid,
157 perf_event__handler_t process,
158 struct machine *machine,
161 char filename[PATH_MAX];
165 if (machine__is_default_guest(machine))
168 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
169 machine->root_dir, pid);
171 fp = fopen(filename, "r");
174 * We raced with a task exiting - just return:
176 pr_debug("couldn't open %s\n", filename);
180 event->header.type = PERF_RECORD_MMAP;
185 char execname[PATH_MAX];
186 char anonstr[] = "//anon";
190 if (fgets(bf, sizeof(bf), fp) == NULL)
193 /* ensure null termination since stack will be reused. */
194 strcpy(execname, "");
196 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
197 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
198 &event->mmap.start, &event->mmap.len, prot,
202 * Anon maps don't have the execname.
207 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
209 if (machine__is_host(machine))
210 event->header.misc = PERF_RECORD_MISC_USER;
212 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
214 if (prot[2] != 'x') {
215 if (!mmap_data || prot[0] != 'r')
218 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
221 if (!strcmp(execname, ""))
222 strcpy(execname, anonstr);
224 size = strlen(execname) + 1;
225 memcpy(event->mmap.filename, execname, size);
226 size = PERF_ALIGN(size, sizeof(u64));
227 event->mmap.len -= event->mmap.start;
228 event->mmap.header.size = (sizeof(event->mmap) -
229 (sizeof(event->mmap.filename) - size));
230 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
231 event->mmap.header.size += machine->id_hdr_size;
232 event->mmap.pid = tgid;
233 event->mmap.tid = pid;
235 if (process(tool, event, &synth_sample, machine) != 0) {
245 int perf_event__synthesize_modules(struct perf_tool *tool,
246 perf_event__handler_t process,
247 struct machine *machine)
251 struct map_groups *kmaps = &machine->kmaps;
252 union perf_event *event = zalloc((sizeof(event->mmap) +
253 machine->id_hdr_size));
255 pr_debug("Not enough memory synthesizing mmap event "
256 "for kernel modules\n");
260 event->header.type = PERF_RECORD_MMAP;
263 * kernel uses 0 for user space maps, see kernel/perf_event.c
266 if (machine__is_host(machine))
267 event->header.misc = PERF_RECORD_MISC_KERNEL;
269 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
271 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
272 nd; nd = rb_next(nd)) {
274 struct map *pos = rb_entry(nd, struct map, rb_node);
276 if (pos->dso->kernel)
279 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
280 event->mmap.header.type = PERF_RECORD_MMAP;
281 event->mmap.header.size = (sizeof(event->mmap) -
282 (sizeof(event->mmap.filename) - size));
283 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
284 event->mmap.header.size += machine->id_hdr_size;
285 event->mmap.start = pos->start;
286 event->mmap.len = pos->end - pos->start;
287 event->mmap.pid = machine->pid;
289 memcpy(event->mmap.filename, pos->dso->long_name,
290 pos->dso->long_name_len + 1);
291 if (process(tool, event, &synth_sample, machine) != 0) {
301 static int __event__synthesize_thread(union perf_event *comm_event,
302 union perf_event *mmap_event,
303 union perf_event *fork_event,
305 perf_event__handler_t process,
306 struct perf_tool *tool,
307 struct machine *machine, bool mmap_data)
309 char filename[PATH_MAX];
311 struct dirent dirent, *next;
314 /* special case: only send one comm event using passed in pid */
316 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
322 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
323 process, machine, mmap_data);
326 if (machine__is_default_guest(machine))
329 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
330 machine->root_dir, pid);
332 tasks = opendir(filename);
334 pr_debug("couldn't open %s\n", filename);
338 while (!readdir_r(tasks, &dirent, &next) && next) {
343 _pid = strtol(dirent.d_name, &end, 10);
347 tgid = perf_event__synthesize_comm(tool, comm_event, _pid,
353 /* process the parent's maps too */
354 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
355 process, machine, mmap_data);
357 /* only fork the tid's map, to save time */
358 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
370 int perf_event__synthesize_thread_map(struct perf_tool *tool,
371 struct thread_map *threads,
372 perf_event__handler_t process,
373 struct machine *machine,
376 union perf_event *comm_event, *mmap_event, *fork_event;
377 int err = -1, thread, j;
379 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
380 if (comm_event == NULL)
383 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
384 if (mmap_event == NULL)
387 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
388 if (fork_event == NULL)
392 for (thread = 0; thread < threads->nr; ++thread) {
393 if (__event__synthesize_thread(comm_event, mmap_event,
395 threads->map[thread], 0,
396 process, tool, machine,
403 * comm.pid is set to thread group id by
404 * perf_event__synthesize_comm
406 if ((int) comm_event->comm.pid != threads->map[thread]) {
407 bool need_leader = true;
409 /* is thread group leader in thread_map? */
410 for (j = 0; j < threads->nr; ++j) {
411 if ((int) comm_event->comm.pid == threads->map[j]) {
417 /* if not, generate events for it */
419 __event__synthesize_thread(comm_event, mmap_event,
421 comm_event->comm.pid, 0,
422 process, tool, machine,
438 int perf_event__synthesize_threads(struct perf_tool *tool,
439 perf_event__handler_t process,
440 struct machine *machine, bool mmap_data)
443 char proc_path[PATH_MAX];
444 struct dirent dirent, *next;
445 union perf_event *comm_event, *mmap_event, *fork_event;
448 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
449 if (comm_event == NULL)
452 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
453 if (mmap_event == NULL)
456 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
457 if (fork_event == NULL)
460 if (machine__is_default_guest(machine))
463 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
464 proc = opendir(proc_path);
469 while (!readdir_r(proc, &dirent, &next) && next) {
471 pid_t pid = strtol(dirent.d_name, &end, 10);
473 if (*end) /* only interested in proper numerical dirents */
476 * We may race with exiting thread, so don't stop just because
477 * one thread couldn't be synthesized.
479 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
480 1, process, tool, machine, mmap_data);
495 struct process_symbol_args {
500 static int find_symbol_cb(void *arg, const char *name, char type,
503 struct process_symbol_args *args = arg;
506 * Must be a function or at least an alias, as in PARISC64, where "_text" is
507 * an 'A' to the same address as "_stext".
509 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
510 type == 'A') || strcmp(name, args->name))
517 u64 kallsyms__get_function_start(const char *kallsyms_filename,
518 const char *symbol_name)
520 struct process_symbol_args args = { .name = symbol_name, };
522 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
528 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
529 perf_event__handler_t process,
530 struct machine *machine)
533 const char *mmap_name;
534 char name_buff[PATH_MAX];
539 * We should get this from /sys/kernel/sections/.text, but till that is
540 * available use this, and after it is use this as a fallback for older
543 union perf_event *event = zalloc((sizeof(event->mmap) +
544 machine->id_hdr_size));
546 pr_debug("Not enough memory synthesizing mmap event "
547 "for kernel modules\n");
551 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
552 if (machine__is_host(machine)) {
554 * kernel uses PERF_RECORD_MISC_USER for user space maps,
555 * see kernel/perf_event.c __perf_event_mmap
557 event->header.misc = PERF_RECORD_MISC_KERNEL;
559 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
562 map = machine->vmlinux_maps[MAP__FUNCTION];
563 kmap = map__kmap(map);
564 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
565 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
566 size = PERF_ALIGN(size, sizeof(u64));
567 event->mmap.header.type = PERF_RECORD_MMAP;
568 event->mmap.header.size = (sizeof(event->mmap) -
569 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
570 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
571 event->mmap.start = map->start;
572 event->mmap.len = map->end - event->mmap.start;
573 event->mmap.pid = machine->pid;
575 err = process(tool, event, &synth_sample, machine);
581 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
583 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
586 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
587 union perf_event *event,
588 struct perf_sample *sample,
589 struct machine *machine)
591 return machine__process_comm_event(machine, event, sample);
594 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
595 union perf_event *event,
596 struct perf_sample *sample,
597 struct machine *machine)
599 return machine__process_lost_event(machine, event, sample);
602 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
604 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
605 event->mmap.pid, event->mmap.tid, event->mmap.start,
606 event->mmap.len, event->mmap.pgoff,
607 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
608 event->mmap.filename);
611 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
613 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
614 " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
615 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
616 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
617 event->mmap2.min, event->mmap2.ino,
618 event->mmap2.ino_generation,
619 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
620 event->mmap2.filename);
623 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
624 union perf_event *event,
625 struct perf_sample *sample,
626 struct machine *machine)
628 return machine__process_mmap_event(machine, event, sample);
631 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
632 union perf_event *event,
633 struct perf_sample *sample,
634 struct machine *machine)
636 return machine__process_mmap2_event(machine, event, sample);
639 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
641 return fprintf(fp, "(%d:%d):(%d:%d)\n",
642 event->fork.pid, event->fork.tid,
643 event->fork.ppid, event->fork.ptid);
646 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
647 union perf_event *event,
648 struct perf_sample *sample,
649 struct machine *machine)
651 return machine__process_fork_event(machine, event, sample);
654 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
655 union perf_event *event,
656 struct perf_sample *sample,
657 struct machine *machine)
659 return machine__process_exit_event(machine, event, sample);
662 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
664 size_t ret = fprintf(fp, "PERF_RECORD_%s",
665 perf_event__name(event->header.type));
667 switch (event->header.type) {
668 case PERF_RECORD_COMM:
669 ret += perf_event__fprintf_comm(event, fp);
671 case PERF_RECORD_FORK:
672 case PERF_RECORD_EXIT:
673 ret += perf_event__fprintf_task(event, fp);
675 case PERF_RECORD_MMAP:
676 ret += perf_event__fprintf_mmap(event, fp);
678 case PERF_RECORD_MMAP2:
679 ret += perf_event__fprintf_mmap2(event, fp);
682 ret += fprintf(fp, "\n");
688 int perf_event__process(struct perf_tool *tool __maybe_unused,
689 union perf_event *event,
690 struct perf_sample *sample,
691 struct machine *machine)
693 return machine__process_event(machine, event, sample);
696 void thread__find_addr_map(struct thread *thread,
697 struct machine *machine, u8 cpumode,
698 enum map_type type, u64 addr,
699 struct addr_location *al)
701 struct map_groups *mg = &thread->mg;
702 bool load_map = false;
704 al->machine = machine;
707 al->cpumode = cpumode;
708 al->filtered = false;
710 if (machine == NULL) {
715 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
717 mg = &machine->kmaps;
719 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
721 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
723 mg = &machine->kmaps;
725 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
731 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
732 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
735 if ((cpumode == PERF_RECORD_MISC_USER ||
736 cpumode == PERF_RECORD_MISC_KERNEL) &&
743 al->map = map_groups__find(mg, type, al->addr);
744 if (al->map == NULL) {
746 * If this is outside of all known maps, and is a negative
747 * address, try to look it up in the kernel dso, as it might be
748 * a vsyscall or vdso (which executes in user-mode).
750 * XXX This is nasty, we should have a symbol list in the
751 * "[vdso]" dso, but for now lets use the old trick of looking
752 * in the whole kernel symbol list.
754 if ((long long)al->addr < 0 &&
755 cpumode == PERF_RECORD_MISC_USER &&
756 machine && mg != &machine->kmaps) {
757 mg = &machine->kmaps;
762 * Kernel maps might be changed when loading symbols so loading
763 * must be done prior to using kernel maps.
766 map__load(al->map, machine->symbol_filter);
767 al->addr = al->map->map_ip(al->map, al->addr);
771 void thread__find_addr_location(struct thread *thread, struct machine *machine,
772 u8 cpumode, enum map_type type, u64 addr,
773 struct addr_location *al)
775 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
777 al->sym = map__find_symbol(al->map, al->addr,
778 machine->symbol_filter);
783 int perf_event__preprocess_sample(const union perf_event *event,
784 struct machine *machine,
785 struct addr_location *al,
786 struct perf_sample *sample)
788 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
789 struct thread *thread = machine__findnew_thread(machine, sample->pid,
795 if (thread__is_filtered(thread))
798 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
800 * Have we already created the kernel maps for this machine?
802 * This should have happened earlier, when we processed the kernel MMAP
803 * events, but for older perf.data files there was no such thing, so do
806 if (cpumode == PERF_RECORD_MISC_KERNEL &&
807 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
808 machine__create_kernel_maps(machine);
810 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
812 dump_printf(" ...... dso: %s\n",
813 al->map ? al->map->dso->long_name :
814 al->level == 'H' ? "[hypervisor]" : "<not found>");
816 al->cpu = sample->cpu;
819 struct dso *dso = al->map->dso;
821 if (symbol_conf.dso_list &&
822 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
824 (dso->short_name != dso->long_name &&
825 strlist__has_entry(symbol_conf.dso_list,
829 al->sym = map__find_symbol(al->map, al->addr,
830 machine->symbol_filter);
833 if (symbol_conf.sym_list &&
834 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,