1 #include <linux/types.h>
10 const char *event__name[] = {
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",
27 static struct sample_data synth_sample = {
36 static pid_t event__synthesize_comm(event_t *event, pid_t pid, int full,
37 event__handler_t process,
38 struct perf_session *session)
40 char filename[PATH_MAX];
45 struct dirent dirent, *next;
48 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
50 fp = fopen(filename, "r");
54 * We raced with a task exiting - just return:
56 pr_debug("couldn't open %s\n", filename);
60 memset(&event->comm, 0, sizeof(event->comm));
62 while (!event->comm.comm[0] || !event->comm.pid) {
63 if (fgets(bf, sizeof(bf), fp) == NULL) {
64 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
68 if (memcmp(bf, "Name:", 5) == 0) {
70 while (*name && isspace(*name))
72 size = strlen(name) - 1;
73 memcpy(event->comm.comm, name, size++);
74 } else if (memcmp(bf, "Tgid:", 5) == 0) {
76 while (*tgids && isspace(*tgids))
78 tgid = event->comm.pid = atoi(tgids);
82 event->comm.header.type = PERF_RECORD_COMM;
83 size = ALIGN(size, sizeof(u64));
84 memset(event->comm.comm + size, 0, session->id_hdr_size);
85 event->comm.header.size = (sizeof(event->comm) -
86 (sizeof(event->comm.comm) - size) +
87 session->id_hdr_size);
89 event->comm.tid = pid;
91 process(event, &synth_sample, session);
95 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
97 tasks = opendir(filename);
101 while (!readdir_r(tasks, &dirent, &next) && next) {
103 pid = strtol(dirent.d_name, &end, 10);
107 event->comm.tid = pid;
109 process(event, &synth_sample, session);
119 static int event__synthesize_mmap_events(event_t *event, pid_t pid, pid_t tgid,
120 event__handler_t process,
121 struct perf_session *session)
123 char filename[PATH_MAX];
126 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
128 fp = fopen(filename, "r");
131 * We raced with a task exiting - just return:
133 pr_debug("couldn't open %s\n", filename);
137 event->header.type = PERF_RECORD_MMAP;
139 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
141 event->header.misc = PERF_RECORD_MISC_USER;
144 char bf[BUFSIZ], *pbf = bf;
147 if (fgets(bf, sizeof(bf), fp) == NULL)
150 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
151 n = hex2u64(pbf, &event->mmap.start);
155 n = hex2u64(pbf, &event->mmap.len);
159 if (*pbf == 'x') { /* vm_exec */
160 char *execname = strchr(bf, '/');
163 if (execname == NULL)
164 execname = strstr(bf, "[vdso]");
166 if (execname == NULL)
170 n = hex2u64(pbf, &event->mmap.pgoff);
172 size = strlen(execname);
173 execname[size - 1] = '\0'; /* Remove \n */
174 memcpy(event->mmap.filename, execname, size);
175 size = ALIGN(size, sizeof(u64));
176 event->mmap.len -= event->mmap.start;
177 event->mmap.header.size = (sizeof(event->mmap) -
178 (sizeof(event->mmap.filename) - size));
179 memset(event->mmap.filename + size, 0, session->id_hdr_size);
180 event->mmap.header.size += session->id_hdr_size;
181 event->mmap.pid = tgid;
182 event->mmap.tid = pid;
184 process(event, &synth_sample, session);
192 int event__synthesize_modules(event__handler_t process,
193 struct perf_session *session,
194 struct machine *machine)
197 struct map_groups *kmaps = &machine->kmaps;
198 event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size);
201 pr_debug("Not enough memory synthesizing mmap event "
202 "for kernel modules\n");
206 event->header.type = PERF_RECORD_MMAP;
209 * kernel uses 0 for user space maps, see kernel/perf_event.c
212 if (machine__is_host(machine))
213 event->header.misc = PERF_RECORD_MISC_KERNEL;
215 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
217 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
218 nd; nd = rb_next(nd)) {
220 struct map *pos = rb_entry(nd, struct map, rb_node);
222 if (pos->dso->kernel)
225 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
226 event->mmap.header.type = PERF_RECORD_MMAP;
227 event->mmap.header.size = (sizeof(event->mmap) -
228 (sizeof(event->mmap.filename) - size));
229 memset(event->mmap.filename + size, 0, session->id_hdr_size);
230 event->mmap.header.size += session->id_hdr_size;
231 event->mmap.start = pos->start;
232 event->mmap.len = pos->end - pos->start;
233 event->mmap.pid = machine->pid;
235 memcpy(event->mmap.filename, pos->dso->long_name,
236 pos->dso->long_name_len + 1);
237 process(event, &synth_sample, session);
244 static int __event__synthesize_thread(event_t *comm_event, event_t *mmap_event,
245 pid_t pid, event__handler_t process,
246 struct perf_session *session)
248 pid_t tgid = event__synthesize_comm(comm_event, pid, 1, process,
252 return event__synthesize_mmap_events(mmap_event, pid, tgid,
256 int event__synthesize_thread(pid_t pid, event__handler_t process,
257 struct perf_session *session)
259 event_t *comm_event, *mmap_event;
262 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
263 if (comm_event == NULL)
266 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
267 if (mmap_event == NULL)
270 err = __event__synthesize_thread(comm_event, mmap_event, pid,
279 int event__synthesize_threads(event__handler_t process,
280 struct perf_session *session)
283 struct dirent dirent, *next;
284 event_t *comm_event, *mmap_event;
287 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
288 if (comm_event == NULL)
291 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
292 if (mmap_event == NULL)
295 proc = opendir("/proc");
299 while (!readdir_r(proc, &dirent, &next) && next) {
301 pid_t pid = strtol(dirent.d_name, &end, 10);
303 if (*end) /* only interested in proper numerical dirents */
306 __event__synthesize_thread(comm_event, mmap_event, pid,
320 struct process_symbol_args {
325 static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
327 struct process_symbol_args *args = arg;
330 * Must be a function or at least an alias, as in PARISC64, where "_text" is
331 * an 'A' to the same address as "_stext".
333 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
334 type == 'A') || strcmp(name, args->name))
341 int event__synthesize_kernel_mmap(event__handler_t process,
342 struct perf_session *session,
343 struct machine *machine,
344 const char *symbol_name)
347 const char *filename, *mmap_name;
349 char name_buff[PATH_MAX];
353 * We should get this from /sys/kernel/sections/.text, but till that is
354 * available use this, and after it is use this as a fallback for older
357 struct process_symbol_args args = { .name = symbol_name, };
358 event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size);
361 pr_debug("Not enough memory synthesizing mmap event "
362 "for kernel modules\n");
366 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
367 if (machine__is_host(machine)) {
369 * kernel uses PERF_RECORD_MISC_USER for user space maps,
370 * see kernel/perf_event.c __perf_event_mmap
372 event->header.misc = PERF_RECORD_MISC_KERNEL;
373 filename = "/proc/kallsyms";
375 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
376 if (machine__is_default_guest(machine))
377 filename = (char *) symbol_conf.default_guest_kallsyms;
379 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
384 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
387 map = machine->vmlinux_maps[MAP__FUNCTION];
388 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
389 "%s%s", mmap_name, symbol_name) + 1;
390 size = ALIGN(size, sizeof(u64));
391 event->mmap.header.type = PERF_RECORD_MMAP;
392 event->mmap.header.size = (sizeof(event->mmap) -
393 (sizeof(event->mmap.filename) - size) + session->id_hdr_size);
394 event->mmap.pgoff = args.start;
395 event->mmap.start = map->start;
396 event->mmap.len = map->end - event->mmap.start;
397 event->mmap.pid = machine->pid;
399 err = process(event, &synth_sample, session);
405 static void thread__comm_adjust(struct thread *self, struct hists *hists)
407 char *comm = self->comm;
409 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
410 (!symbol_conf.comm_list ||
411 strlist__has_entry(symbol_conf.comm_list, comm))) {
412 u16 slen = strlen(comm);
414 if (hists__new_col_len(hists, HISTC_COMM, slen))
415 hists__set_col_len(hists, HISTC_THREAD, slen + 6);
419 static int thread__set_comm_adjust(struct thread *self, const char *comm,
422 int ret = thread__set_comm(self, comm);
427 thread__comm_adjust(self, hists);
432 int event__process_comm(event_t *self, struct sample_data *sample __used,
433 struct perf_session *session)
435 struct thread *thread = perf_session__findnew(session, self->comm.tid);
437 dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid);
439 if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm,
441 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
448 int event__process_lost(event_t *self, struct sample_data *sample __used,
449 struct perf_session *session)
451 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
452 session->hists.stats.total_lost += self->lost.lost;
456 static void event_set_kernel_mmap_len(struct map **maps, event_t *self)
458 maps[MAP__FUNCTION]->start = self->mmap.start;
459 maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
461 * Be a bit paranoid here, some perf.data file came with
462 * a zero sized synthesized MMAP event for the kernel.
464 if (maps[MAP__FUNCTION]->end == 0)
465 maps[MAP__FUNCTION]->end = ~0ULL;
468 static int event__process_kernel_mmap(event_t *self,
469 struct perf_session *session)
472 char kmmap_prefix[PATH_MAX];
473 struct machine *machine;
474 enum dso_kernel_type kernel_type;
477 machine = perf_session__findnew_machine(session, self->mmap.pid);
479 pr_err("Can't find id %d's machine\n", self->mmap.pid);
483 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
484 if (machine__is_host(machine))
485 kernel_type = DSO_TYPE_KERNEL;
487 kernel_type = DSO_TYPE_GUEST_KERNEL;
489 is_kernel_mmap = memcmp(self->mmap.filename,
491 strlen(kmmap_prefix)) == 0;
492 if (self->mmap.filename[0] == '/' ||
493 (!is_kernel_mmap && self->mmap.filename[0] == '[')) {
495 char short_module_name[1024];
498 if (self->mmap.filename[0] == '/') {
499 name = strrchr(self->mmap.filename, '/');
504 dot = strrchr(name, '.');
507 snprintf(short_module_name, sizeof(short_module_name),
508 "[%.*s]", (int)(dot - name), name);
509 strxfrchar(short_module_name, '-', '_');
511 strcpy(short_module_name, self->mmap.filename);
513 map = machine__new_module(machine, self->mmap.start,
514 self->mmap.filename);
518 name = strdup(short_module_name);
522 map->dso->short_name = name;
523 map->dso->sname_alloc = 1;
524 map->end = map->start + self->mmap.len;
525 } else if (is_kernel_mmap) {
526 const char *symbol_name = (self->mmap.filename +
527 strlen(kmmap_prefix));
529 * Should be there already, from the build-id table in
532 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
537 kernel->kernel = kernel_type;
538 if (__machine__create_kernel_maps(machine, kernel) < 0)
541 event_set_kernel_mmap_len(machine->vmlinux_maps, self);
542 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
545 if (machine__is_default_guest(machine)) {
547 * preload dso of guest kernel and modules
549 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
558 int event__process_mmap(event_t *self, struct sample_data *sample __used,
559 struct perf_session *session)
561 struct machine *machine;
562 struct thread *thread;
564 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
567 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
568 self->mmap.pid, self->mmap.tid, self->mmap.start,
569 self->mmap.len, self->mmap.pgoff, self->mmap.filename);
571 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
572 cpumode == PERF_RECORD_MISC_KERNEL) {
573 ret = event__process_kernel_mmap(self, session);
579 machine = perf_session__find_host_machine(session);
582 thread = perf_session__findnew(session, self->mmap.pid);
585 map = map__new(&machine->user_dsos, self->mmap.start,
586 self->mmap.len, self->mmap.pgoff,
587 self->mmap.pid, self->mmap.filename,
592 thread__insert_map(thread, map);
596 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
600 int event__process_task(event_t *self, struct sample_data *sample __used,
601 struct perf_session *session)
603 struct thread *thread = perf_session__findnew(session, self->fork.tid);
604 struct thread *parent = perf_session__findnew(session, self->fork.ptid);
606 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
607 self->fork.ppid, self->fork.ptid);
609 if (self->header.type == PERF_RECORD_EXIT) {
610 perf_session__remove_thread(session, thread);
614 if (thread == NULL || parent == NULL ||
615 thread__fork(thread, parent) < 0) {
616 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
623 int event__process(event_t *event, struct sample_data *sample,
624 struct perf_session *session)
626 switch (event->header.type) {
627 case PERF_RECORD_COMM:
628 event__process_comm(event, sample, session);
630 case PERF_RECORD_MMAP:
631 event__process_mmap(event, sample, session);
633 case PERF_RECORD_FORK:
634 case PERF_RECORD_EXIT:
635 event__process_task(event, sample, session);
644 void thread__find_addr_map(struct thread *self,
645 struct perf_session *session, u8 cpumode,
646 enum map_type type, pid_t pid, u64 addr,
647 struct addr_location *al)
649 struct map_groups *mg = &self->mg;
650 struct machine *machine = NULL;
654 al->cpumode = cpumode;
655 al->filtered = false;
657 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
659 machine = perf_session__find_host_machine(session);
660 if (machine == NULL) {
664 mg = &machine->kmaps;
665 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
667 machine = perf_session__find_host_machine(session);
668 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
670 machine = perf_session__find_machine(session, pid);
671 if (machine == NULL) {
675 mg = &machine->kmaps;
678 * 'u' means guest os user space.
679 * TODO: We don't support guest user space. Might support late.
681 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
687 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
688 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
691 if ((cpumode == PERF_RECORD_MISC_USER ||
692 cpumode == PERF_RECORD_MISC_KERNEL) &&
699 al->map = map_groups__find(mg, type, al->addr);
700 if (al->map == NULL) {
702 * If this is outside of all known maps, and is a negative
703 * address, try to look it up in the kernel dso, as it might be
704 * a vsyscall or vdso (which executes in user-mode).
706 * XXX This is nasty, we should have a symbol list in the
707 * "[vdso]" dso, but for now lets use the old trick of looking
708 * in the whole kernel symbol list.
710 if ((long long)al->addr < 0 &&
711 cpumode == PERF_RECORD_MISC_KERNEL &&
712 machine && mg != &machine->kmaps) {
713 mg = &machine->kmaps;
717 al->addr = al->map->map_ip(al->map, al->addr);
720 void thread__find_addr_location(struct thread *self,
721 struct perf_session *session, u8 cpumode,
722 enum map_type type, pid_t pid, u64 addr,
723 struct addr_location *al,
724 symbol_filter_t filter)
726 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
728 al->sym = map__find_symbol(al->map, al->addr, filter);
733 static void dso__calc_col_width(struct dso *self, struct hists *hists)
735 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
736 (!symbol_conf.dso_list ||
737 strlist__has_entry(symbol_conf.dso_list, self->name))) {
738 u16 slen = dso__name_len(self);
739 hists__new_col_len(hists, HISTC_DSO, slen);
742 self->slen_calculated = 1;
745 int event__preprocess_sample(const event_t *self, struct perf_session *session,
746 struct addr_location *al, struct sample_data *data,
747 symbol_filter_t filter)
749 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
750 struct thread *thread = perf_session__findnew(session, self->ip.pid);
755 if (symbol_conf.comm_list &&
756 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
759 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
761 * Have we already created the kernel maps for the host machine?
763 * This should have happened earlier, when we processed the kernel MMAP
764 * events, but for older perf.data files there was no such thing, so do
767 if (cpumode == PERF_RECORD_MISC_KERNEL &&
768 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
769 machine__create_kernel_maps(&session->host_machine);
771 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
772 self->ip.pid, self->ip.ip, al);
773 dump_printf(" ...... dso: %s\n",
774 al->map ? al->map->dso->long_name :
775 al->level == 'H' ? "[hypervisor]" : "<not found>");
780 if (symbol_conf.dso_list &&
781 (!al->map || !al->map->dso ||
782 !(strlist__has_entry(symbol_conf.dso_list,
783 al->map->dso->short_name) ||
784 (al->map->dso->short_name != al->map->dso->long_name &&
785 strlist__has_entry(symbol_conf.dso_list,
786 al->map->dso->long_name)))))
789 * We have to do this here as we may have a dso with no symbol
790 * hit that has a name longer than the ones with symbols
793 if (!sort_dso.elide && !al->map->dso->slen_calculated)
794 dso__calc_col_width(al->map->dso, &session->hists);
796 al->sym = map__find_symbol(al->map, al->addr, filter);
798 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
800 if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width &&
801 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
802 !symbol_conf.dso_list)
803 hists__set_col_len(&session->hists, HISTC_DSO,
804 unresolved_col_width);
807 if (symbol_conf.sym_list && al->sym &&
808 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
818 static int event__parse_id_sample(const event_t *event,
819 struct perf_session *session,
820 struct sample_data *sample)
825 sample->cpu = sample->pid = sample->tid = -1;
826 sample->stream_id = sample->id = sample->time = -1ULL;
828 if (!session->sample_id_all)
831 array = event->sample.array;
832 array += ((event->header.size -
833 sizeof(event->header)) / sizeof(u64)) - 1;
834 type = session->sample_type;
836 if (type & PERF_SAMPLE_CPU) {
837 u32 *p = (u32 *)array;
842 if (type & PERF_SAMPLE_STREAM_ID) {
843 sample->stream_id = *array;
847 if (type & PERF_SAMPLE_ID) {
852 if (type & PERF_SAMPLE_TIME) {
853 sample->time = *array;
857 if (type & PERF_SAMPLE_TID) {
858 u32 *p = (u32 *)array;
866 int event__parse_sample(const event_t *event, struct perf_session *session,
867 struct sample_data *data)
872 if (event->header.type != PERF_RECORD_SAMPLE)
873 return event__parse_id_sample(event, session, data);
875 array = event->sample.array;
876 type = session->sample_type;
878 if (type & PERF_SAMPLE_IP) {
879 data->ip = event->ip.ip;
883 if (type & PERF_SAMPLE_TID) {
884 u32 *p = (u32 *)array;
890 if (type & PERF_SAMPLE_TIME) {
895 if (type & PERF_SAMPLE_ADDR) {
901 if (type & PERF_SAMPLE_ID) {
906 if (type & PERF_SAMPLE_STREAM_ID) {
907 data->stream_id = *array;
911 if (type & PERF_SAMPLE_CPU) {
912 u32 *p = (u32 *)array;
918 if (type & PERF_SAMPLE_PERIOD) {
919 data->period = *array;
923 if (type & PERF_SAMPLE_READ) {
924 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
928 if (type & PERF_SAMPLE_CALLCHAIN) {
929 data->callchain = (struct ip_callchain *)array;
930 array += 1 + data->callchain->nr;
933 if (type & PERF_SAMPLE_RAW) {
934 u32 *p = (u32 *)array;