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 */
154 char *execname
= strchr(bf
, '/');
157 if (execname
== NULL
)
158 execname
= strstr(bf
, "[vdso]");
160 if (execname
== NULL
)
164 n
= hex2u64(pbf
, &ev
.mmap
.pgoff
);
166 size
= strlen(execname
);
167 execname
[size
- 1] = '\0'; /* Remove \n */
168 memcpy(ev
.mmap
.filename
, execname
, size
);
169 size
= ALIGN(size
, sizeof(u64
));
170 ev
.mmap
.len
-= ev
.mmap
.start
;
171 ev
.mmap
.header
.size
= (sizeof(ev
.mmap
) -
172 (sizeof(ev
.mmap
.filename
) - size
));
176 process(&ev
, session
);
184 int event__synthesize_modules(event__handler_t process
,
185 struct perf_session
*session
,
186 struct machine
*machine
)
189 struct map_groups
*kmaps
= &machine
->kmaps
;
193 * kernel uses 0 for user space maps, see kernel/perf_event.c
196 if (machine__is_host(machine
))
197 misc
= PERF_RECORD_MISC_KERNEL
;
199 misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
201 for (nd
= rb_first(&kmaps
->maps
[MAP__FUNCTION
]);
202 nd
; nd
= rb_next(nd
)) {
205 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
207 if (pos
->dso
->kernel
)
210 size
= ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
211 memset(&ev
, 0, sizeof(ev
));
212 ev
.mmap
.header
.misc
= misc
;
213 ev
.mmap
.header
.type
= PERF_RECORD_MMAP
;
214 ev
.mmap
.header
.size
= (sizeof(ev
.mmap
) -
215 (sizeof(ev
.mmap
.filename
) - size
));
216 ev
.mmap
.start
= pos
->start
;
217 ev
.mmap
.len
= pos
->end
- pos
->start
;
218 ev
.mmap
.pid
= machine
->pid
;
220 memcpy(ev
.mmap
.filename
, pos
->dso
->long_name
,
221 pos
->dso
->long_name_len
+ 1);
222 process(&ev
, session
);
228 int event__synthesize_thread(pid_t pid
, event__handler_t process
,
229 struct perf_session
*session
)
231 pid_t tgid
= event__synthesize_comm(pid
, 1, process
, session
);
234 return event__synthesize_mmap_events(pid
, tgid
, process
, session
);
237 void event__synthesize_threads(event__handler_t process
,
238 struct perf_session
*session
)
241 struct dirent dirent
, *next
;
243 proc
= opendir("/proc");
245 while (!readdir_r(proc
, &dirent
, &next
) && next
) {
247 pid_t pid
= strtol(dirent
.d_name
, &end
, 10);
249 if (*end
) /* only interested in proper numerical dirents */
252 event__synthesize_thread(pid
, process
, session
);
258 struct process_symbol_args
{
263 static int find_symbol_cb(void *arg
, const char *name
, char type
, u64 start
)
265 struct process_symbol_args
*args
= arg
;
268 * Must be a function or at least an alias, as in PARISC64, where "_text" is
269 * an 'A' to the same address as "_stext".
271 if (!(symbol_type__is_a(type
, MAP__FUNCTION
) ||
272 type
== 'A') || strcmp(name
, args
->name
))
279 int event__synthesize_kernel_mmap(event__handler_t process
,
280 struct perf_session
*session
,
281 struct machine
*machine
,
282 const char *symbol_name
)
285 const char *filename
, *mmap_name
;
287 char name_buff
[PATH_MAX
];
292 .type
= PERF_RECORD_MMAP
,
296 * We should get this from /sys/kernel/sections/.text, but till that is
297 * available use this, and after it is use this as a fallback for older
300 struct process_symbol_args args
= { .name
= symbol_name
, };
302 mmap_name
= machine__mmap_name(machine
, name_buff
, sizeof(name_buff
));
303 if (machine__is_host(machine
)) {
305 * kernel uses PERF_RECORD_MISC_USER for user space maps,
306 * see kernel/perf_event.c __perf_event_mmap
308 ev
.header
.misc
= PERF_RECORD_MISC_KERNEL
;
309 filename
= "/proc/kallsyms";
311 ev
.header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
312 if (machine__is_default_guest(machine
))
313 filename
= (char *) symbol_conf
.default_guest_kallsyms
;
315 sprintf(path
, "%s/proc/kallsyms", machine
->root_dir
);
320 if (kallsyms__parse(filename
, &args
, find_symbol_cb
) <= 0)
323 map
= machine
->vmlinux_maps
[MAP__FUNCTION
];
324 size
= snprintf(ev
.mmap
.filename
, sizeof(ev
.mmap
.filename
),
325 "%s%s", mmap_name
, symbol_name
) + 1;
326 size
= ALIGN(size
, sizeof(u64
));
327 ev
.mmap
.header
.size
= (sizeof(ev
.mmap
) -
328 (sizeof(ev
.mmap
.filename
) - size
));
329 ev
.mmap
.pgoff
= args
.start
;
330 ev
.mmap
.start
= map
->start
;
331 ev
.mmap
.len
= map
->end
- ev
.mmap
.start
;
332 ev
.mmap
.pid
= machine
->pid
;
334 return process(&ev
, session
);
337 static void thread__comm_adjust(struct thread
*self
, struct hists
*hists
)
339 char *comm
= self
->comm
;
341 if (!symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
342 (!symbol_conf
.comm_list
||
343 strlist__has_entry(symbol_conf
.comm_list
, comm
))) {
344 u16 slen
= strlen(comm
);
346 if (hists__new_col_len(hists
, HISTC_COMM
, slen
))
347 hists__set_col_len(hists
, HISTC_THREAD
, slen
+ 6);
351 static int thread__set_comm_adjust(struct thread
*self
, const char *comm
,
354 int ret
= thread__set_comm(self
, comm
);
359 thread__comm_adjust(self
, hists
);
364 int event__process_comm(event_t
*self
, struct perf_session
*session
)
366 struct thread
*thread
= perf_session__findnew(session
, self
->comm
.tid
);
368 dump_printf(": %s:%d\n", self
->comm
.comm
, self
->comm
.tid
);
370 if (thread
== NULL
|| thread__set_comm_adjust(thread
, self
->comm
.comm
,
372 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
379 int event__process_lost(event_t
*self
, struct perf_session
*session
)
381 dump_printf(": id:%Ld: lost:%Ld\n", self
->lost
.id
, self
->lost
.lost
);
382 session
->hists
.stats
.total_lost
+= self
->lost
.lost
;
386 static void event_set_kernel_mmap_len(struct map
**maps
, event_t
*self
)
388 maps
[MAP__FUNCTION
]->start
= self
->mmap
.start
;
389 maps
[MAP__FUNCTION
]->end
= self
->mmap
.start
+ self
->mmap
.len
;
391 * Be a bit paranoid here, some perf.data file came with
392 * a zero sized synthesized MMAP event for the kernel.
394 if (maps
[MAP__FUNCTION
]->end
== 0)
395 maps
[MAP__FUNCTION
]->end
= ~0UL;
398 static int event__process_kernel_mmap(event_t
*self
,
399 struct perf_session
*session
)
402 char kmmap_prefix
[PATH_MAX
];
403 struct machine
*machine
;
404 enum dso_kernel_type kernel_type
;
407 machine
= perf_session__findnew_machine(session
, self
->mmap
.pid
);
409 pr_err("Can't find id %d's machine\n", self
->mmap
.pid
);
413 machine__mmap_name(machine
, kmmap_prefix
, sizeof(kmmap_prefix
));
414 if (machine__is_host(machine
))
415 kernel_type
= DSO_TYPE_KERNEL
;
417 kernel_type
= DSO_TYPE_GUEST_KERNEL
;
419 is_kernel_mmap
= memcmp(self
->mmap
.filename
,
421 strlen(kmmap_prefix
)) == 0;
422 if (self
->mmap
.filename
[0] == '/' ||
423 (!is_kernel_mmap
&& self
->mmap
.filename
[0] == '[')) {
425 char short_module_name
[1024];
428 if (self
->mmap
.filename
[0] == '/') {
429 name
= strrchr(self
->mmap
.filename
, '/');
434 dot
= strrchr(name
, '.');
437 snprintf(short_module_name
, sizeof(short_module_name
),
438 "[%.*s]", (int)(dot
- name
), name
);
439 strxfrchar(short_module_name
, '-', '_');
441 strcpy(short_module_name
, self
->mmap
.filename
);
443 map
= machine__new_module(machine
, self
->mmap
.start
,
444 self
->mmap
.filename
);
448 name
= strdup(short_module_name
);
452 map
->dso
->short_name
= name
;
453 map
->dso
->sname_alloc
= 1;
454 map
->end
= map
->start
+ self
->mmap
.len
;
455 } else if (is_kernel_mmap
) {
456 const char *symbol_name
= (self
->mmap
.filename
+
457 strlen(kmmap_prefix
));
459 * Should be there already, from the build-id table in
462 struct dso
*kernel
= __dsos__findnew(&machine
->kernel_dsos
,
467 kernel
->kernel
= kernel_type
;
468 if (__machine__create_kernel_maps(machine
, kernel
) < 0)
471 event_set_kernel_mmap_len(machine
->vmlinux_maps
, self
);
472 perf_session__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
,
475 if (machine__is_default_guest(machine
)) {
477 * preload dso of guest kernel and modules
479 dso__load(kernel
, machine
->vmlinux_maps
[MAP__FUNCTION
],
488 int event__process_mmap(event_t
*self
, struct perf_session
*session
)
490 struct machine
*machine
;
491 struct thread
*thread
;
493 u8 cpumode
= self
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
496 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
497 self
->mmap
.pid
, self
->mmap
.tid
, self
->mmap
.start
,
498 self
->mmap
.len
, self
->mmap
.pgoff
, self
->mmap
.filename
);
500 if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
501 cpumode
== PERF_RECORD_MISC_KERNEL
) {
502 ret
= event__process_kernel_mmap(self
, session
);
508 machine
= perf_session__find_host_machine(session
);
511 thread
= perf_session__findnew(session
, self
->mmap
.pid
);
514 map
= map__new(&machine
->user_dsos
, self
->mmap
.start
,
515 self
->mmap
.len
, self
->mmap
.pgoff
,
516 self
->mmap
.pid
, self
->mmap
.filename
,
521 thread__insert_map(thread
, map
);
525 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
529 int event__process_task(event_t
*self
, struct perf_session
*session
)
531 struct thread
*thread
= perf_session__findnew(session
, self
->fork
.tid
);
532 struct thread
*parent
= perf_session__findnew(session
, self
->fork
.ptid
);
534 dump_printf("(%d:%d):(%d:%d)\n", self
->fork
.pid
, self
->fork
.tid
,
535 self
->fork
.ppid
, self
->fork
.ptid
);
537 if (self
->header
.type
== PERF_RECORD_EXIT
) {
538 perf_session__remove_thread(session
, thread
);
542 if (thread
== NULL
|| parent
== NULL
||
543 thread__fork(thread
, parent
) < 0) {
544 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
551 int event__process(event_t
*event
, struct perf_session
*session
)
553 switch (event
->header
.type
) {
554 case PERF_RECORD_COMM
:
555 event__process_comm(event
, session
);
557 case PERF_RECORD_MMAP
:
558 event__process_mmap(event
, session
);
560 case PERF_RECORD_FORK
:
561 case PERF_RECORD_EXIT
:
562 event__process_task(event
, session
);
571 void thread__find_addr_map(struct thread
*self
,
572 struct perf_session
*session
, u8 cpumode
,
573 enum map_type type
, pid_t pid
, u64 addr
,
574 struct addr_location
*al
)
576 struct map_groups
*mg
= &self
->mg
;
577 struct machine
*machine
= NULL
;
581 al
->cpumode
= cpumode
;
582 al
->filtered
= false;
584 if (cpumode
== PERF_RECORD_MISC_KERNEL
&& perf_host
) {
586 machine
= perf_session__find_host_machine(session
);
587 if (machine
== NULL
) {
591 mg
= &machine
->kmaps
;
592 } else if (cpumode
== PERF_RECORD_MISC_USER
&& perf_host
) {
594 machine
= perf_session__find_host_machine(session
);
595 } else if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
597 machine
= perf_session__find_machine(session
, pid
);
598 if (machine
== NULL
) {
602 mg
= &machine
->kmaps
;
605 * 'u' means guest os user space.
606 * TODO: We don't support guest user space. Might support late.
608 if (cpumode
== PERF_RECORD_MISC_GUEST_USER
&& perf_guest
)
614 if ((cpumode
== PERF_RECORD_MISC_GUEST_USER
||
615 cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) &&
618 if ((cpumode
== PERF_RECORD_MISC_USER
||
619 cpumode
== PERF_RECORD_MISC_KERNEL
) &&
626 al
->map
= map_groups__find(mg
, type
, al
->addr
);
627 if (al
->map
== NULL
) {
628 if ((long long)al
->addr
< 0 &&
629 cpumode
== PERF_RECORD_MISC_KERNEL
&&
630 machine
&& mg
!= &machine
->kmaps
) {
631 mg
= &machine
->kmaps
;
635 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
638 void thread__find_addr_location(struct thread
*self
,
639 struct perf_session
*session
, u8 cpumode
,
640 enum map_type type
, pid_t pid
, u64 addr
,
641 struct addr_location
*al
,
642 symbol_filter_t filter
)
644 thread__find_addr_map(self
, session
, cpumode
, type
, pid
, addr
, al
);
646 al
->sym
= map__find_symbol(al
->map
, al
->addr
, filter
);
651 static void dso__calc_col_width(struct dso
*self
, struct hists
*hists
)
653 if (!symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
654 (!symbol_conf
.dso_list
||
655 strlist__has_entry(symbol_conf
.dso_list
, self
->name
))) {
656 u16 slen
= dso__name_len(self
);
657 hists__new_col_len(hists
, HISTC_DSO
, slen
);
660 self
->slen_calculated
= 1;
663 int event__preprocess_sample(const event_t
*self
, struct perf_session
*session
,
664 struct addr_location
*al
, struct sample_data
*data
,
665 symbol_filter_t filter
)
667 u8 cpumode
= self
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
668 struct thread
*thread
;
670 event__parse_sample(self
, session
->sample_type
, data
);
672 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld cpu:%d\n",
673 self
->header
.misc
, data
->pid
, data
->tid
, data
->ip
,
674 data
->period
, data
->cpu
);
676 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
679 dump_printf("... chain: nr:%Lu\n", data
->callchain
->nr
);
681 if (!ip_callchain__valid(data
->callchain
, self
)) {
682 pr_debug("call-chain problem with event, "
688 for (i
= 0; i
< data
->callchain
->nr
; i
++)
689 dump_printf("..... %2d: %016Lx\n",
690 i
, data
->callchain
->ips
[i
]);
693 thread
= perf_session__findnew(session
, self
->ip
.pid
);
697 if (symbol_conf
.comm_list
&&
698 !strlist__has_entry(symbol_conf
.comm_list
, thread
->comm
))
701 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
703 * Have we already created the kernel maps for the host machine?
705 * This should have happened earlier, when we processed the kernel MMAP
706 * events, but for older perf.data files there was no such thing, so do
709 if (cpumode
== PERF_RECORD_MISC_KERNEL
&&
710 session
->host_machine
.vmlinux_maps
[MAP__FUNCTION
] == NULL
)
711 machine__create_kernel_maps(&session
->host_machine
);
713 thread__find_addr_map(thread
, session
, cpumode
, MAP__FUNCTION
,
714 self
->ip
.pid
, self
->ip
.ip
, al
);
715 dump_printf(" ...... dso: %s\n",
716 al
->map
? al
->map
->dso
->long_name
:
717 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
722 if (symbol_conf
.dso_list
&&
723 (!al
->map
|| !al
->map
->dso
||
724 !(strlist__has_entry(symbol_conf
.dso_list
,
725 al
->map
->dso
->short_name
) ||
726 (al
->map
->dso
->short_name
!= al
->map
->dso
->long_name
&&
727 strlist__has_entry(symbol_conf
.dso_list
,
728 al
->map
->dso
->long_name
)))))
731 * We have to do this here as we may have a dso with no symbol
732 * hit that has a name longer than the ones with symbols
735 if (!sort_dso
.elide
&& !al
->map
->dso
->slen_calculated
)
736 dso__calc_col_width(al
->map
->dso
, &session
->hists
);
738 al
->sym
= map__find_symbol(al
->map
, al
->addr
, filter
);
740 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
742 if (hists__col_len(&session
->hists
, HISTC_DSO
) < unresolved_col_width
&&
743 !symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
744 !symbol_conf
.dso_list
)
745 hists__set_col_len(&session
->hists
, HISTC_DSO
,
746 unresolved_col_width
);
749 if (symbol_conf
.sym_list
&& al
->sym
&&
750 !strlist__has_entry(symbol_conf
.sym_list
, al
->sym
->name
))
760 int event__parse_sample(const event_t
*event
, u64 type
, struct sample_data
*data
)
762 const u64
*array
= event
->sample
.array
;
764 if (type
& PERF_SAMPLE_IP
) {
765 data
->ip
= event
->ip
.ip
;
769 if (type
& PERF_SAMPLE_TID
) {
770 u32
*p
= (u32
*)array
;
776 if (type
& PERF_SAMPLE_TIME
) {
781 if (type
& PERF_SAMPLE_ADDR
) {
787 if (type
& PERF_SAMPLE_ID
) {
792 if (type
& PERF_SAMPLE_STREAM_ID
) {
793 data
->stream_id
= *array
;
797 if (type
& PERF_SAMPLE_CPU
) {
798 u32
*p
= (u32
*)array
;
804 if (type
& PERF_SAMPLE_PERIOD
) {
805 data
->period
= *array
;
809 if (type
& PERF_SAMPLE_READ
) {
810 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
814 if (type
& PERF_SAMPLE_CALLCHAIN
) {
815 data
->callchain
= (struct ip_callchain
*)array
;
816 array
+= 1 + data
->callchain
->nr
;
819 if (type
& PERF_SAMPLE_RAW
) {
820 u32
*p
= (u32
*)array
;