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 pid_t event__synthesize_comm(pid_t pid, int full,
28 event__handler_t process,
29 struct perf_session *session)
32 char filename[PATH_MAX];
37 struct dirent dirent, *next;
40 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
42 fp = fopen(filename, "r");
46 * We raced with a task exiting - just return:
48 pr_debug("couldn't open %s\n", filename);
52 memset(&ev.comm, 0, sizeof(ev.comm));
53 while (!ev.comm.comm[0] || !ev.comm.pid) {
54 if (fgets(bf, sizeof(bf), fp) == NULL)
57 if (memcmp(bf, "Name:", 5) == 0) {
59 while (*name && isspace(*name))
61 size = strlen(name) - 1;
62 memcpy(ev.comm.comm, name, size++);
63 } else if (memcmp(bf, "Tgid:", 5) == 0) {
65 while (*tgids && isspace(*tgids))
67 tgid = ev.comm.pid = atoi(tgids);
71 ev.comm.header.type = PERF_RECORD_COMM;
72 size = ALIGN(size, sizeof(u64));
73 ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
78 process(&ev, session);
82 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
84 tasks = opendir(filename);
88 while (!readdir_r(tasks, &dirent, &next) && next) {
90 pid = strtol(dirent.d_name, &end, 10);
96 process(&ev, session);
105 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
109 static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
110 event__handler_t process,
111 struct perf_session *session)
113 char filename[PATH_MAX];
116 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
118 fp = fopen(filename, "r");
121 * We raced with a task exiting - just return:
123 pr_debug("couldn't open %s\n", filename);
128 char bf[BUFSIZ], *pbf = bf;
131 .type = PERF_RECORD_MMAP,
133 * Just like the kernel, see __perf_event_mmap
134 * in kernel/perf_event.c
136 .misc = PERF_RECORD_MISC_USER,
141 if (fgets(bf, sizeof(bf), fp) == NULL)
144 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
145 n = hex2u64(pbf, &ev.mmap.start);
149 n = hex2u64(pbf, &ev.mmap.len);
153 if (*pbf == 'x') { /* vm_exec */
155 char *execname = strchr(bf, '/');
158 if (execname == NULL)
159 execname = strstr(bf, "[vdso]");
161 if (execname == NULL)
165 n = hex2u64(pbf, &vm_pgoff);
166 /* pgoff is in bytes, not pages */
168 ev.mmap.pgoff = vm_pgoff << getpagesize();
172 size = strlen(execname);
173 execname[size - 1] = '\0'; /* Remove \n */
174 memcpy(ev.mmap.filename, execname, size);
175 size = ALIGN(size, sizeof(u64));
176 ev.mmap.len -= ev.mmap.start;
177 ev.mmap.header.size = (sizeof(ev.mmap) -
178 (sizeof(ev.mmap.filename) - size));
182 process(&ev, session);
190 int event__synthesize_modules(event__handler_t process,
191 struct perf_session *session,
192 struct machine *machine)
195 struct map_groups *kmaps = &machine->kmaps;
199 * kernel uses 0 for user space maps, see kernel/perf_event.c
202 if (machine__is_host(machine))
203 misc = PERF_RECORD_MISC_KERNEL;
205 misc = PERF_RECORD_MISC_GUEST_KERNEL;
207 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
208 nd; nd = rb_next(nd)) {
211 struct map *pos = rb_entry(nd, struct map, rb_node);
213 if (pos->dso->kernel)
216 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
217 memset(&ev, 0, sizeof(ev));
218 ev.mmap.header.misc = misc;
219 ev.mmap.header.type = PERF_RECORD_MMAP;
220 ev.mmap.header.size = (sizeof(ev.mmap) -
221 (sizeof(ev.mmap.filename) - size));
222 ev.mmap.start = pos->start;
223 ev.mmap.len = pos->end - pos->start;
224 ev.mmap.pid = machine->pid;
226 memcpy(ev.mmap.filename, pos->dso->long_name,
227 pos->dso->long_name_len + 1);
228 process(&ev, session);
234 int event__synthesize_thread(pid_t pid, event__handler_t process,
235 struct perf_session *session)
237 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
240 return event__synthesize_mmap_events(pid, tgid, process, session);
243 void event__synthesize_threads(event__handler_t process,
244 struct perf_session *session)
247 struct dirent dirent, *next;
249 proc = opendir("/proc");
251 while (!readdir_r(proc, &dirent, &next) && next) {
253 pid_t pid = strtol(dirent.d_name, &end, 10);
255 if (*end) /* only interested in proper numerical dirents */
258 event__synthesize_thread(pid, process, session);
264 struct process_symbol_args {
269 static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
271 struct process_symbol_args *args = arg;
274 * Must be a function or at least an alias, as in PARISC64, where "_text" is
275 * an 'A' to the same address as "_stext".
277 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
278 type == 'A') || strcmp(name, args->name))
285 int event__synthesize_kernel_mmap(event__handler_t process,
286 struct perf_session *session,
287 struct machine *machine,
288 const char *symbol_name)
291 const char *filename, *mmap_name;
293 char name_buff[PATH_MAX];
298 .type = PERF_RECORD_MMAP,
302 * We should get this from /sys/kernel/sections/.text, but till that is
303 * available use this, and after it is use this as a fallback for older
306 struct process_symbol_args args = { .name = symbol_name, };
308 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
309 if (machine__is_host(machine)) {
311 * kernel uses PERF_RECORD_MISC_USER for user space maps,
312 * see kernel/perf_event.c __perf_event_mmap
314 ev.header.misc = PERF_RECORD_MISC_KERNEL;
315 filename = "/proc/kallsyms";
317 ev.header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
318 if (machine__is_default_guest(machine))
319 filename = (char *) symbol_conf.default_guest_kallsyms;
321 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
326 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
329 map = machine->vmlinux_maps[MAP__FUNCTION];
330 size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
331 "%s%s", mmap_name, symbol_name) + 1;
332 size = ALIGN(size, sizeof(u64));
333 ev.mmap.header.size = (sizeof(ev.mmap) -
334 (sizeof(ev.mmap.filename) - size));
335 ev.mmap.pgoff = args.start;
336 ev.mmap.start = map->start;
337 ev.mmap.len = map->end - ev.mmap.start;
338 ev.mmap.pid = machine->pid;
340 return process(&ev, session);
343 static void thread__comm_adjust(struct thread *self, struct hists *hists)
345 char *comm = self->comm;
347 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
348 (!symbol_conf.comm_list ||
349 strlist__has_entry(symbol_conf.comm_list, comm))) {
350 u16 slen = strlen(comm);
352 if (hists__new_col_len(hists, HISTC_COMM, slen))
353 hists__set_col_len(hists, HISTC_THREAD, slen + 6);
357 static int thread__set_comm_adjust(struct thread *self, const char *comm,
360 int ret = thread__set_comm(self, comm);
365 thread__comm_adjust(self, hists);
370 int event__process_comm(event_t *self, struct perf_session *session)
372 struct thread *thread = perf_session__findnew(session, self->comm.tid);
374 dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid);
376 if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm,
378 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
385 int event__process_lost(event_t *self, struct perf_session *session)
387 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
388 session->hists.stats.total_lost += self->lost.lost;
392 static void event_set_kernel_mmap_len(struct map **maps, event_t *self)
394 maps[MAP__FUNCTION]->start = self->mmap.start;
395 maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
397 * Be a bit paranoid here, some perf.data file came with
398 * a zero sized synthesized MMAP event for the kernel.
400 if (maps[MAP__FUNCTION]->end == 0)
401 maps[MAP__FUNCTION]->end = ~0UL;
404 static int event__process_kernel_mmap(event_t *self,
405 struct perf_session *session)
408 char kmmap_prefix[PATH_MAX];
409 struct machine *machine;
410 enum dso_kernel_type kernel_type;
413 machine = perf_session__findnew_machine(session, self->mmap.pid);
415 pr_err("Can't find id %d's machine\n", self->mmap.pid);
419 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
420 if (machine__is_host(machine))
421 kernel_type = DSO_TYPE_KERNEL;
423 kernel_type = DSO_TYPE_GUEST_KERNEL;
425 is_kernel_mmap = memcmp(self->mmap.filename,
427 strlen(kmmap_prefix)) == 0;
428 if (self->mmap.filename[0] == '/' ||
429 (!is_kernel_mmap && self->mmap.filename[0] == '[')) {
431 char short_module_name[1024];
434 if (self->mmap.filename[0] == '/') {
435 name = strrchr(self->mmap.filename, '/');
440 dot = strrchr(name, '.');
443 snprintf(short_module_name, sizeof(short_module_name),
444 "[%.*s]", (int)(dot - name), name);
445 strxfrchar(short_module_name, '-', '_');
447 strcpy(short_module_name, self->mmap.filename);
449 map = machine__new_module(machine, self->mmap.start,
450 self->mmap.filename);
454 name = strdup(short_module_name);
458 map->dso->short_name = name;
459 map->dso->sname_alloc = 1;
460 map->end = map->start + self->mmap.len;
461 } else if (is_kernel_mmap) {
462 const char *symbol_name = (self->mmap.filename +
463 strlen(kmmap_prefix));
465 * Should be there already, from the build-id table in
468 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
473 kernel->kernel = kernel_type;
474 if (__machine__create_kernel_maps(machine, kernel) < 0)
477 event_set_kernel_mmap_len(machine->vmlinux_maps, self);
478 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
481 if (machine__is_default_guest(machine)) {
483 * preload dso of guest kernel and modules
485 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
494 int event__process_mmap(event_t *self, struct perf_session *session)
496 struct machine *machine;
497 struct thread *thread;
499 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
502 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
503 self->mmap.pid, self->mmap.tid, self->mmap.start,
504 self->mmap.len, self->mmap.pgoff, self->mmap.filename);
506 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
507 cpumode == PERF_RECORD_MISC_KERNEL) {
508 ret = event__process_kernel_mmap(self, session);
514 machine = perf_session__find_host_machine(session);
517 thread = perf_session__findnew(session, self->mmap.pid);
520 map = map__new(&machine->user_dsos, self->mmap.start,
521 self->mmap.len, self->mmap.pgoff,
522 self->mmap.pid, self->mmap.filename,
527 thread__insert_map(thread, map);
531 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
535 int event__process_task(event_t *self, struct perf_session *session)
537 struct thread *thread = perf_session__findnew(session, self->fork.tid);
538 struct thread *parent = perf_session__findnew(session, self->fork.ptid);
540 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
541 self->fork.ppid, self->fork.ptid);
543 if (self->header.type == PERF_RECORD_EXIT) {
544 perf_session__remove_thread(session, thread);
548 if (thread == NULL || parent == NULL ||
549 thread__fork(thread, parent) < 0) {
550 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
557 void thread__find_addr_map(struct thread *self,
558 struct perf_session *session, u8 cpumode,
559 enum map_type type, pid_t pid, u64 addr,
560 struct addr_location *al)
562 struct map_groups *mg = &self->mg;
563 struct machine *machine = NULL;
567 al->cpumode = cpumode;
568 al->filtered = false;
570 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
572 machine = perf_session__find_host_machine(session);
573 if (machine == NULL) {
577 mg = &machine->kmaps;
578 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
580 machine = perf_session__find_host_machine(session);
581 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
583 machine = perf_session__find_machine(session, pid);
584 if (machine == NULL) {
588 mg = &machine->kmaps;
591 * 'u' means guest os user space.
592 * TODO: We don't support guest user space. Might support late.
594 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
600 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
601 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
604 if ((cpumode == PERF_RECORD_MISC_USER ||
605 cpumode == PERF_RECORD_MISC_KERNEL) &&
612 al->map = map_groups__find(mg, type, al->addr);
613 if (al->map == NULL) {
615 * If this is outside of all known maps, and is a negative
616 * address, try to look it up in the kernel dso, as it might be
617 * a vsyscall or vdso (which executes in user-mode).
619 * XXX This is nasty, we should have a symbol list in the
620 * "[vdso]" dso, but for now lets use the old trick of looking
621 * in the whole kernel symbol list.
623 if ((long long)al->addr < 0 &&
624 cpumode == PERF_RECORD_MISC_KERNEL &&
625 machine && mg != &machine->kmaps) {
626 mg = &machine->kmaps;
630 al->addr = al->map->map_ip(al->map, al->addr);
633 void thread__find_addr_location(struct thread *self,
634 struct perf_session *session, u8 cpumode,
635 enum map_type type, pid_t pid, u64 addr,
636 struct addr_location *al,
637 symbol_filter_t filter)
639 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
641 al->sym = map__find_symbol(al->map, al->addr, filter);
646 static void dso__calc_col_width(struct dso *self, struct hists *hists)
648 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
649 (!symbol_conf.dso_list ||
650 strlist__has_entry(symbol_conf.dso_list, self->name))) {
651 u16 slen = dso__name_len(self);
652 hists__new_col_len(hists, HISTC_DSO, slen);
655 self->slen_calculated = 1;
658 int event__preprocess_sample(const event_t *self, struct perf_session *session,
659 struct addr_location *al, struct sample_data *data,
660 symbol_filter_t filter)
662 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
663 struct thread *thread;
665 event__parse_sample(self, session->sample_type, data);
667 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld cpu:%d\n",
668 self->header.misc, data->pid, data->tid, data->ip,
669 data->period, data->cpu);
671 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
674 dump_printf("... chain: nr:%Lu\n", data->callchain->nr);
676 if (!ip_callchain__valid(data->callchain, self)) {
677 pr_debug("call-chain problem with event, "
683 for (i = 0; i < data->callchain->nr; i++)
684 dump_printf("..... %2d: %016Lx\n",
685 i, data->callchain->ips[i]);
688 thread = perf_session__findnew(session, self->ip.pid);
692 if (symbol_conf.comm_list &&
693 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
696 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
698 * Have we already created the kernel maps for the host machine?
700 * This should have happened earlier, when we processed the kernel MMAP
701 * events, but for older perf.data files there was no such thing, so do
704 if (cpumode == PERF_RECORD_MISC_KERNEL &&
705 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
706 machine__create_kernel_maps(&session->host_machine);
708 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
709 self->ip.pid, self->ip.ip, al);
710 dump_printf(" ...... dso: %s\n",
711 al->map ? al->map->dso->long_name :
712 al->level == 'H' ? "[hypervisor]" : "<not found>");
717 if (symbol_conf.dso_list &&
718 (!al->map || !al->map->dso ||
719 !(strlist__has_entry(symbol_conf.dso_list,
720 al->map->dso->short_name) ||
721 (al->map->dso->short_name != al->map->dso->long_name &&
722 strlist__has_entry(symbol_conf.dso_list,
723 al->map->dso->long_name)))))
726 * We have to do this here as we may have a dso with no symbol
727 * hit that has a name longer than the ones with symbols
730 if (!sort_dso.elide && !al->map->dso->slen_calculated)
731 dso__calc_col_width(al->map->dso, &session->hists);
733 al->sym = map__find_symbol(al->map, al->addr, filter);
735 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
737 if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width &&
738 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
739 !symbol_conf.dso_list)
740 hists__set_col_len(&session->hists, HISTC_DSO,
741 unresolved_col_width);
744 if (symbol_conf.sym_list && al->sym &&
745 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
755 int event__parse_sample(const event_t *event, u64 type, struct sample_data *data)
757 const u64 *array = event->sample.array;
759 if (type & PERF_SAMPLE_IP) {
760 data->ip = event->ip.ip;
764 if (type & PERF_SAMPLE_TID) {
765 u32 *p = (u32 *)array;
771 if (type & PERF_SAMPLE_TIME) {
776 if (type & PERF_SAMPLE_ADDR) {
782 if (type & PERF_SAMPLE_ID) {
787 if (type & PERF_SAMPLE_STREAM_ID) {
788 data->stream_id = *array;
792 if (type & PERF_SAMPLE_CPU) {
793 u32 *p = (u32 *)array;
799 if (type & PERF_SAMPLE_PERIOD) {
800 data->period = *array;
804 if (type & PERF_SAMPLE_READ) {
805 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
809 if (type & PERF_SAMPLE_CALLCHAIN) {
810 data->callchain = (struct ip_callchain *)array;
811 array += 1 + data->callchain->nr;
814 if (type & PERF_SAMPLE_RAW) {
815 u32 *p = (u32 *)array;