1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/kernel.h>
6 #include <linux/types.h>
10 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
11 #include <api/fs/fs.h>
12 #include <linux/perf_event.h>
21 #include "thread_map.h"
22 #include "sane_ctype.h"
23 #include "symbol/kallsyms.h"
27 static const char *perf_event__names
[] = {
29 [PERF_RECORD_MMAP
] = "MMAP",
30 [PERF_RECORD_MMAP2
] = "MMAP2",
31 [PERF_RECORD_LOST
] = "LOST",
32 [PERF_RECORD_COMM
] = "COMM",
33 [PERF_RECORD_EXIT
] = "EXIT",
34 [PERF_RECORD_THROTTLE
] = "THROTTLE",
35 [PERF_RECORD_UNTHROTTLE
] = "UNTHROTTLE",
36 [PERF_RECORD_FORK
] = "FORK",
37 [PERF_RECORD_READ
] = "READ",
38 [PERF_RECORD_SAMPLE
] = "SAMPLE",
39 [PERF_RECORD_AUX
] = "AUX",
40 [PERF_RECORD_ITRACE_START
] = "ITRACE_START",
41 [PERF_RECORD_LOST_SAMPLES
] = "LOST_SAMPLES",
42 [PERF_RECORD_SWITCH
] = "SWITCH",
43 [PERF_RECORD_SWITCH_CPU_WIDE
] = "SWITCH_CPU_WIDE",
44 [PERF_RECORD_NAMESPACES
] = "NAMESPACES",
45 [PERF_RECORD_HEADER_ATTR
] = "ATTR",
46 [PERF_RECORD_HEADER_EVENT_TYPE
] = "EVENT_TYPE",
47 [PERF_RECORD_HEADER_TRACING_DATA
] = "TRACING_DATA",
48 [PERF_RECORD_HEADER_BUILD_ID
] = "BUILD_ID",
49 [PERF_RECORD_FINISHED_ROUND
] = "FINISHED_ROUND",
50 [PERF_RECORD_ID_INDEX
] = "ID_INDEX",
51 [PERF_RECORD_AUXTRACE_INFO
] = "AUXTRACE_INFO",
52 [PERF_RECORD_AUXTRACE
] = "AUXTRACE",
53 [PERF_RECORD_AUXTRACE_ERROR
] = "AUXTRACE_ERROR",
54 [PERF_RECORD_THREAD_MAP
] = "THREAD_MAP",
55 [PERF_RECORD_CPU_MAP
] = "CPU_MAP",
56 [PERF_RECORD_STAT_CONFIG
] = "STAT_CONFIG",
57 [PERF_RECORD_STAT
] = "STAT",
58 [PERF_RECORD_STAT_ROUND
] = "STAT_ROUND",
59 [PERF_RECORD_EVENT_UPDATE
] = "EVENT_UPDATE",
60 [PERF_RECORD_TIME_CONV
] = "TIME_CONV",
61 [PERF_RECORD_HEADER_FEATURE
] = "FEATURE",
64 static const char *perf_ns__names
[] = {
65 [NET_NS_INDEX
] = "net",
66 [UTS_NS_INDEX
] = "uts",
67 [IPC_NS_INDEX
] = "ipc",
68 [PID_NS_INDEX
] = "pid",
69 [USER_NS_INDEX
] = "user",
70 [MNT_NS_INDEX
] = "mnt",
71 [CGROUP_NS_INDEX
] = "cgroup",
74 const char *perf_event__name(unsigned int id
)
76 if (id
>= ARRAY_SIZE(perf_event__names
))
78 if (!perf_event__names
[id
])
80 return perf_event__names
[id
];
83 static const char *perf_ns__name(unsigned int id
)
85 if (id
>= ARRAY_SIZE(perf_ns__names
))
87 return perf_ns__names
[id
];
90 static int perf_tool__process_synth_event(struct perf_tool
*tool
,
91 union perf_event
*event
,
92 struct machine
*machine
,
93 perf_event__handler_t process
)
95 struct perf_sample synth_sample
= {
102 .cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
,
105 return process(tool
, event
, &synth_sample
, machine
);
109 * Assumes that the first 4095 bytes of /proc/pid/stat contains
110 * the comm, tgid and ppid.
112 static int perf_event__get_comm_ids(pid_t pid
, char *comm
, size_t len
,
113 pid_t
*tgid
, pid_t
*ppid
)
115 char filename
[PATH_MAX
];
120 char *name
, *tgids
, *ppids
;
125 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
127 fd
= open(filename
, O_RDONLY
);
129 pr_debug("couldn't open %s\n", filename
);
133 n
= read(fd
, bf
, sizeof(bf
) - 1);
136 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
142 name
= strstr(bf
, "Name:");
143 tgids
= strstr(bf
, "Tgid:");
144 ppids
= strstr(bf
, "PPid:");
149 name
+= 5; /* strlen("Name:") */
152 nl
= strchr(name
, '\n');
159 memcpy(comm
, name
, size
);
162 pr_debug("Name: string not found for pid %d\n", pid
);
166 tgids
+= 5; /* strlen("Tgid:") */
169 pr_debug("Tgid: string not found for pid %d\n", pid
);
173 ppids
+= 5; /* strlen("PPid:") */
176 pr_debug("PPid: string not found for pid %d\n", pid
);
182 static int perf_event__prepare_comm(union perf_event
*event
, pid_t pid
,
183 struct machine
*machine
,
184 pid_t
*tgid
, pid_t
*ppid
)
190 memset(&event
->comm
, 0, sizeof(event
->comm
));
192 if (machine__is_host(machine
)) {
193 if (perf_event__get_comm_ids(pid
, event
->comm
.comm
,
194 sizeof(event
->comm
.comm
),
199 *tgid
= machine
->pid
;
205 event
->comm
.pid
= *tgid
;
206 event
->comm
.header
.type
= PERF_RECORD_COMM
;
208 size
= strlen(event
->comm
.comm
) + 1;
209 size
= PERF_ALIGN(size
, sizeof(u64
));
210 memset(event
->comm
.comm
+ size
, 0, machine
->id_hdr_size
);
211 event
->comm
.header
.size
= (sizeof(event
->comm
) -
212 (sizeof(event
->comm
.comm
) - size
) +
213 machine
->id_hdr_size
);
214 event
->comm
.tid
= pid
;
219 pid_t
perf_event__synthesize_comm(struct perf_tool
*tool
,
220 union perf_event
*event
, pid_t pid
,
221 perf_event__handler_t process
,
222 struct machine
*machine
)
226 if (perf_event__prepare_comm(event
, pid
, machine
, &tgid
, &ppid
) != 0)
229 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
235 static void perf_event__get_ns_link_info(pid_t pid
, const char *ns
,
236 struct perf_ns_link_info
*ns_link_info
)
241 sprintf(proc_ns
, "/proc/%u/ns/%s", pid
, ns
);
242 if (stat64(proc_ns
, &st
) == 0) {
243 ns_link_info
->dev
= st
.st_dev
;
244 ns_link_info
->ino
= st
.st_ino
;
248 int perf_event__synthesize_namespaces(struct perf_tool
*tool
,
249 union perf_event
*event
,
250 pid_t pid
, pid_t tgid
,
251 perf_event__handler_t process
,
252 struct machine
*machine
)
255 struct perf_ns_link_info
*ns_link_info
;
257 if (!tool
|| !tool
->namespace_events
)
260 memset(&event
->namespaces
, 0, (sizeof(event
->namespaces
) +
261 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
262 machine
->id_hdr_size
));
264 event
->namespaces
.pid
= tgid
;
265 event
->namespaces
.tid
= pid
;
267 event
->namespaces
.nr_namespaces
= NR_NAMESPACES
;
269 ns_link_info
= event
->namespaces
.link_info
;
271 for (idx
= 0; idx
< event
->namespaces
.nr_namespaces
; idx
++)
272 perf_event__get_ns_link_info(pid
, perf_ns__name(idx
),
275 event
->namespaces
.header
.type
= PERF_RECORD_NAMESPACES
;
277 event
->namespaces
.header
.size
= (sizeof(event
->namespaces
) +
278 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
279 machine
->id_hdr_size
);
281 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
287 static int perf_event__synthesize_fork(struct perf_tool
*tool
,
288 union perf_event
*event
,
289 pid_t pid
, pid_t tgid
, pid_t ppid
,
290 perf_event__handler_t process
,
291 struct machine
*machine
)
293 memset(&event
->fork
, 0, sizeof(event
->fork
) + machine
->id_hdr_size
);
296 * for main thread set parent to ppid from status file. For other
297 * threads set parent pid to main thread. ie., assume main thread
298 * spawns all threads in a process
301 event
->fork
.ppid
= ppid
;
302 event
->fork
.ptid
= ppid
;
304 event
->fork
.ppid
= tgid
;
305 event
->fork
.ptid
= tgid
;
307 event
->fork
.pid
= tgid
;
308 event
->fork
.tid
= pid
;
309 event
->fork
.header
.type
= PERF_RECORD_FORK
;
311 event
->fork
.header
.size
= (sizeof(event
->fork
) + machine
->id_hdr_size
);
313 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
319 int perf_event__synthesize_mmap_events(struct perf_tool
*tool
,
320 union perf_event
*event
,
321 pid_t pid
, pid_t tgid
,
322 perf_event__handler_t process
,
323 struct machine
*machine
,
325 unsigned int proc_map_timeout
)
327 char filename
[PATH_MAX
];
329 unsigned long long t
;
330 bool truncation
= false;
331 unsigned long long timeout
= proc_map_timeout
* 1000000ULL;
333 const char *hugetlbfs_mnt
= hugetlbfs__mountpoint();
334 int hugetlbfs_mnt_len
= hugetlbfs_mnt
? strlen(hugetlbfs_mnt
) : 0;
336 if (machine__is_default_guest(machine
))
339 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task/%d/maps",
340 machine
->root_dir
, pid
, pid
);
342 fp
= fopen(filename
, "r");
345 * We raced with a task exiting - just return:
347 pr_debug("couldn't open %s\n", filename
);
351 event
->header
.type
= PERF_RECORD_MMAP2
;
357 char execname
[PATH_MAX
];
358 char anonstr
[] = "//anon";
363 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
366 if ((rdclock() - t
) > timeout
) {
367 pr_warning("Reading %s time out. "
368 "You may want to increase "
369 "the time limit by --proc-map-timeout\n",
375 /* ensure null termination since stack will be reused. */
376 strcpy(execname
, "");
378 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
379 n
= sscanf(bf
, "%"PRIx64
"-%"PRIx64
" %s %"PRIx64
" %x:%x %u %[^\n]\n",
380 &event
->mmap2
.start
, &event
->mmap2
.len
, prot
,
381 &event
->mmap2
.pgoff
, &event
->mmap2
.maj
,
386 * Anon maps don't have the execname.
391 event
->mmap2
.ino
= (u64
)ino
;
394 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
396 if (machine__is_host(machine
))
397 event
->header
.misc
= PERF_RECORD_MISC_USER
;
399 event
->header
.misc
= PERF_RECORD_MISC_GUEST_USER
;
401 /* map protection and flags bits */
402 event
->mmap2
.prot
= 0;
403 event
->mmap2
.flags
= 0;
405 event
->mmap2
.prot
|= PROT_READ
;
407 event
->mmap2
.prot
|= PROT_WRITE
;
409 event
->mmap2
.prot
|= PROT_EXEC
;
412 event
->mmap2
.flags
|= MAP_SHARED
;
414 event
->mmap2
.flags
|= MAP_PRIVATE
;
416 if (prot
[2] != 'x') {
417 if (!mmap_data
|| prot
[0] != 'r')
420 event
->header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
425 event
->header
.misc
|= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
;
427 if (!strcmp(execname
, ""))
428 strcpy(execname
, anonstr
);
430 if (hugetlbfs_mnt_len
&&
431 !strncmp(execname
, hugetlbfs_mnt
, hugetlbfs_mnt_len
)) {
432 strcpy(execname
, anonstr
);
433 event
->mmap2
.flags
|= MAP_HUGETLB
;
436 size
= strlen(execname
) + 1;
437 memcpy(event
->mmap2
.filename
, execname
, size
);
438 size
= PERF_ALIGN(size
, sizeof(u64
));
439 event
->mmap2
.len
-= event
->mmap
.start
;
440 event
->mmap2
.header
.size
= (sizeof(event
->mmap2
) -
441 (sizeof(event
->mmap2
.filename
) - size
));
442 memset(event
->mmap2
.filename
+ size
, 0, machine
->id_hdr_size
);
443 event
->mmap2
.header
.size
+= machine
->id_hdr_size
;
444 event
->mmap2
.pid
= tgid
;
445 event
->mmap2
.tid
= pid
;
447 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
460 int perf_event__synthesize_modules(struct perf_tool
*tool
,
461 perf_event__handler_t process
,
462 struct machine
*machine
)
466 struct map_groups
*kmaps
= &machine
->kmaps
;
467 struct maps
*maps
= &kmaps
->maps
[MAP__FUNCTION
];
468 union perf_event
*event
= zalloc((sizeof(event
->mmap
) +
469 machine
->id_hdr_size
));
471 pr_debug("Not enough memory synthesizing mmap event "
472 "for kernel modules\n");
476 event
->header
.type
= PERF_RECORD_MMAP
;
479 * kernel uses 0 for user space maps, see kernel/perf_event.c
482 if (machine__is_host(machine
))
483 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
485 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
487 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
490 if (__map__is_kernel(pos
))
493 size
= PERF_ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
494 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
495 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
496 (sizeof(event
->mmap
.filename
) - size
));
497 memset(event
->mmap
.filename
+ size
, 0, machine
->id_hdr_size
);
498 event
->mmap
.header
.size
+= machine
->id_hdr_size
;
499 event
->mmap
.start
= pos
->start
;
500 event
->mmap
.len
= pos
->end
- pos
->start
;
501 event
->mmap
.pid
= machine
->pid
;
503 memcpy(event
->mmap
.filename
, pos
->dso
->long_name
,
504 pos
->dso
->long_name_len
+ 1);
505 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
515 static int __event__synthesize_thread(union perf_event
*comm_event
,
516 union perf_event
*mmap_event
,
517 union perf_event
*fork_event
,
518 union perf_event
*namespaces_event
,
520 perf_event__handler_t process
,
521 struct perf_tool
*tool
,
522 struct machine
*machine
,
524 unsigned int proc_map_timeout
)
526 char filename
[PATH_MAX
];
528 struct dirent
*dirent
;
532 /* special case: only send one comm event using passed in pid */
534 tgid
= perf_event__synthesize_comm(tool
, comm_event
, pid
,
540 if (perf_event__synthesize_namespaces(tool
, namespaces_event
, pid
,
541 tgid
, process
, machine
) < 0)
545 return perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
546 process
, machine
, mmap_data
,
550 if (machine__is_default_guest(machine
))
553 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task",
554 machine
->root_dir
, pid
);
556 tasks
= opendir(filename
);
558 pr_debug("couldn't open %s\n", filename
);
562 while ((dirent
= readdir(tasks
)) != NULL
) {
566 _pid
= strtol(dirent
->d_name
, &end
, 10);
571 if (perf_event__prepare_comm(comm_event
, _pid
, machine
,
575 if (perf_event__synthesize_fork(tool
, fork_event
, _pid
, tgid
,
576 ppid
, process
, machine
) < 0)
579 if (perf_event__synthesize_namespaces(tool
, namespaces_event
, _pid
,
580 tgid
, process
, machine
) < 0)
584 * Send the prepared comm event
586 if (perf_tool__process_synth_event(tool
, comm_event
, machine
, process
) != 0)
591 /* process the parent's maps too */
592 rc
= perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
593 process
, machine
, mmap_data
, proc_map_timeout
);
603 int perf_event__synthesize_thread_map(struct perf_tool
*tool
,
604 struct thread_map
*threads
,
605 perf_event__handler_t process
,
606 struct machine
*machine
,
608 unsigned int proc_map_timeout
)
610 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
611 union perf_event
*namespaces_event
;
612 int err
= -1, thread
, j
;
614 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
615 if (comm_event
== NULL
)
618 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
619 if (mmap_event
== NULL
)
622 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
623 if (fork_event
== NULL
)
626 namespaces_event
= malloc(sizeof(namespaces_event
->namespaces
) +
627 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
628 machine
->id_hdr_size
);
629 if (namespaces_event
== NULL
)
633 for (thread
= 0; thread
< threads
->nr
; ++thread
) {
634 if (__event__synthesize_thread(comm_event
, mmap_event
,
635 fork_event
, namespaces_event
,
636 thread_map__pid(threads
, thread
), 0,
637 process
, tool
, machine
,
638 mmap_data
, proc_map_timeout
)) {
644 * comm.pid is set to thread group id by
645 * perf_event__synthesize_comm
647 if ((int) comm_event
->comm
.pid
!= thread_map__pid(threads
, thread
)) {
648 bool need_leader
= true;
650 /* is thread group leader in thread_map? */
651 for (j
= 0; j
< threads
->nr
; ++j
) {
652 if ((int) comm_event
->comm
.pid
== thread_map__pid(threads
, j
)) {
658 /* if not, generate events for it */
660 __event__synthesize_thread(comm_event
, mmap_event
,
661 fork_event
, namespaces_event
,
662 comm_event
->comm
.pid
, 0,
663 process
, tool
, machine
,
664 mmap_data
, proc_map_timeout
)) {
670 free(namespaces_event
);
681 int perf_event__synthesize_threads(struct perf_tool
*tool
,
682 perf_event__handler_t process
,
683 struct machine
*machine
,
685 unsigned int proc_map_timeout
)
688 char proc_path
[PATH_MAX
];
689 struct dirent
*dirent
;
690 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
691 union perf_event
*namespaces_event
;
694 if (machine__is_default_guest(machine
))
697 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
698 if (comm_event
== NULL
)
701 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
702 if (mmap_event
== NULL
)
705 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
706 if (fork_event
== NULL
)
709 namespaces_event
= malloc(sizeof(namespaces_event
->namespaces
) +
710 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
711 machine
->id_hdr_size
);
712 if (namespaces_event
== NULL
)
715 snprintf(proc_path
, sizeof(proc_path
), "%s/proc", machine
->root_dir
);
716 proc
= opendir(proc_path
);
719 goto out_free_namespaces
;
721 while ((dirent
= readdir(proc
)) != NULL
) {
723 pid_t pid
= strtol(dirent
->d_name
, &end
, 10);
725 if (*end
) /* only interested in proper numerical dirents */
728 * We may race with exiting thread, so don't stop just because
729 * one thread couldn't be synthesized.
731 __event__synthesize_thread(comm_event
, mmap_event
, fork_event
,
732 namespaces_event
, pid
, 1, process
,
733 tool
, machine
, mmap_data
,
740 free(namespaces_event
);
751 struct process_symbol_args
{
756 static int find_symbol_cb(void *arg
, const char *name
, char type
,
759 struct process_symbol_args
*args
= arg
;
762 * Must be a function or at least an alias, as in PARISC64, where "_text" is
763 * an 'A' to the same address as "_stext".
765 if (!(symbol_type__is_a(type
, MAP__FUNCTION
) ||
766 type
== 'A') || strcmp(name
, args
->name
))
773 int kallsyms__get_function_start(const char *kallsyms_filename
,
774 const char *symbol_name
, u64
*addr
)
776 struct process_symbol_args args
= { .name
= symbol_name
, };
778 if (kallsyms__parse(kallsyms_filename
, &args
, find_symbol_cb
) <= 0)
785 int perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
786 perf_event__handler_t process
,
787 struct machine
*machine
)
790 const char *mmap_name
;
791 char name_buff
[PATH_MAX
];
792 struct map
*map
= machine__kernel_map(machine
);
795 union perf_event
*event
;
797 if (symbol_conf
.kptr_restrict
)
803 * We should get this from /sys/kernel/sections/.text, but till that is
804 * available use this, and after it is use this as a fallback for older
807 event
= zalloc((sizeof(event
->mmap
) + machine
->id_hdr_size
));
809 pr_debug("Not enough memory synthesizing mmap event "
810 "for kernel modules\n");
814 mmap_name
= machine__mmap_name(machine
, name_buff
, sizeof(name_buff
));
815 if (machine__is_host(machine
)) {
817 * kernel uses PERF_RECORD_MISC_USER for user space maps,
818 * see kernel/perf_event.c __perf_event_mmap
820 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
822 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
825 kmap
= map__kmap(map
);
826 size
= snprintf(event
->mmap
.filename
, sizeof(event
->mmap
.filename
),
827 "%s%s", mmap_name
, kmap
->ref_reloc_sym
->name
) + 1;
828 size
= PERF_ALIGN(size
, sizeof(u64
));
829 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
830 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
831 (sizeof(event
->mmap
.filename
) - size
) + machine
->id_hdr_size
);
832 event
->mmap
.pgoff
= kmap
->ref_reloc_sym
->addr
;
833 event
->mmap
.start
= map
->start
;
834 event
->mmap
.len
= map
->end
- event
->mmap
.start
;
835 event
->mmap
.pid
= machine
->pid
;
837 err
= perf_tool__process_synth_event(tool
, event
, machine
, process
);
843 int perf_event__synthesize_thread_map2(struct perf_tool
*tool
,
844 struct thread_map
*threads
,
845 perf_event__handler_t process
,
846 struct machine
*machine
)
848 union perf_event
*event
;
851 size
= sizeof(event
->thread_map
);
852 size
+= threads
->nr
* sizeof(event
->thread_map
.entries
[0]);
854 event
= zalloc(size
);
858 event
->header
.type
= PERF_RECORD_THREAD_MAP
;
859 event
->header
.size
= size
;
860 event
->thread_map
.nr
= threads
->nr
;
862 for (i
= 0; i
< threads
->nr
; i
++) {
863 struct thread_map_event_entry
*entry
= &event
->thread_map
.entries
[i
];
864 char *comm
= thread_map__comm(threads
, i
);
869 entry
->pid
= thread_map__pid(threads
, i
);
870 strncpy((char *) &entry
->comm
, comm
, sizeof(entry
->comm
));
873 err
= process(tool
, event
, NULL
, machine
);
879 static void synthesize_cpus(struct cpu_map_entries
*cpus
,
886 for (i
= 0; i
< map
->nr
; i
++)
887 cpus
->cpu
[i
] = map
->map
[i
];
890 static void synthesize_mask(struct cpu_map_mask
*mask
,
891 struct cpu_map
*map
, int max
)
895 mask
->nr
= BITS_TO_LONGS(max
);
896 mask
->long_size
= sizeof(long);
898 for (i
= 0; i
< map
->nr
; i
++)
899 set_bit(map
->map
[i
], mask
->mask
);
902 static size_t cpus_size(struct cpu_map
*map
)
904 return sizeof(struct cpu_map_entries
) + map
->nr
* sizeof(u16
);
907 static size_t mask_size(struct cpu_map
*map
, int *max
)
913 for (i
= 0; i
< map
->nr
; i
++) {
914 /* bit possition of the cpu is + 1 */
915 int bit
= map
->map
[i
] + 1;
921 return sizeof(struct cpu_map_mask
) + BITS_TO_LONGS(*max
) * sizeof(long);
924 void *cpu_map_data__alloc(struct cpu_map
*map
, size_t *size
, u16
*type
, int *max
)
926 size_t size_cpus
, size_mask
;
927 bool is_dummy
= cpu_map__empty(map
);
930 * Both array and mask data have variable size based
931 * on the number of cpus and their actual values.
932 * The size of the 'struct cpu_map_data' is:
934 * array = size of 'struct cpu_map_entries' +
935 * number of cpus * sizeof(u64)
937 * mask = size of 'struct cpu_map_mask' +
938 * maximum cpu bit converted to size of longs
940 * and finaly + the size of 'struct cpu_map_data'.
942 size_cpus
= cpus_size(map
);
943 size_mask
= mask_size(map
, max
);
945 if (is_dummy
|| (size_cpus
< size_mask
)) {
947 *type
= PERF_CPU_MAP__CPUS
;
950 *type
= PERF_CPU_MAP__MASK
;
953 *size
+= sizeof(struct cpu_map_data
);
954 return zalloc(*size
);
957 void cpu_map_data__synthesize(struct cpu_map_data
*data
, struct cpu_map
*map
,
963 case PERF_CPU_MAP__CPUS
:
964 synthesize_cpus((struct cpu_map_entries
*) data
->data
, map
);
966 case PERF_CPU_MAP__MASK
:
967 synthesize_mask((struct cpu_map_mask
*) data
->data
, map
, max
);
973 static struct cpu_map_event
* cpu_map_event__new(struct cpu_map
*map
)
975 size_t size
= sizeof(struct cpu_map_event
);
976 struct cpu_map_event
*event
;
980 event
= cpu_map_data__alloc(map
, &size
, &type
, &max
);
984 event
->header
.type
= PERF_RECORD_CPU_MAP
;
985 event
->header
.size
= size
;
986 event
->data
.type
= type
;
988 cpu_map_data__synthesize(&event
->data
, map
, type
, max
);
992 int perf_event__synthesize_cpu_map(struct perf_tool
*tool
,
994 perf_event__handler_t process
,
995 struct machine
*machine
)
997 struct cpu_map_event
*event
;
1000 event
= cpu_map_event__new(map
);
1004 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
1010 int perf_event__synthesize_stat_config(struct perf_tool
*tool
,
1011 struct perf_stat_config
*config
,
1012 perf_event__handler_t process
,
1013 struct machine
*machine
)
1015 struct stat_config_event
*event
;
1016 int size
, i
= 0, err
;
1018 size
= sizeof(*event
);
1019 size
+= (PERF_STAT_CONFIG_TERM__MAX
* sizeof(event
->data
[0]));
1021 event
= zalloc(size
);
1025 event
->header
.type
= PERF_RECORD_STAT_CONFIG
;
1026 event
->header
.size
= size
;
1027 event
->nr
= PERF_STAT_CONFIG_TERM__MAX
;
1029 #define ADD(__term, __val) \
1030 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1031 event->data[i].val = __val; \
1034 ADD(AGGR_MODE
, config
->aggr_mode
)
1035 ADD(INTERVAL
, config
->interval
)
1036 ADD(SCALE
, config
->scale
)
1038 WARN_ONCE(i
!= PERF_STAT_CONFIG_TERM__MAX
,
1039 "stat config terms unbalanced\n");
1042 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
1048 int perf_event__synthesize_stat(struct perf_tool
*tool
,
1049 u32 cpu
, u32 thread
, u64 id
,
1050 struct perf_counts_values
*count
,
1051 perf_event__handler_t process
,
1052 struct machine
*machine
)
1054 struct stat_event event
;
1056 event
.header
.type
= PERF_RECORD_STAT
;
1057 event
.header
.size
= sizeof(event
);
1058 event
.header
.misc
= 0;
1062 event
.thread
= thread
;
1063 event
.val
= count
->val
;
1064 event
.ena
= count
->ena
;
1065 event
.run
= count
->run
;
1067 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
1070 int perf_event__synthesize_stat_round(struct perf_tool
*tool
,
1071 u64 evtime
, u64 type
,
1072 perf_event__handler_t process
,
1073 struct machine
*machine
)
1075 struct stat_round_event event
;
1077 event
.header
.type
= PERF_RECORD_STAT_ROUND
;
1078 event
.header
.size
= sizeof(event
);
1079 event
.header
.misc
= 0;
1081 event
.time
= evtime
;
1084 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
1087 void perf_event__read_stat_config(struct perf_stat_config
*config
,
1088 struct stat_config_event
*event
)
1092 for (i
= 0; i
< event
->nr
; i
++) {
1094 switch (event
->data
[i
].tag
) {
1095 #define CASE(__term, __val) \
1096 case PERF_STAT_CONFIG_TERM__##__term: \
1097 config->__val = event->data[i].val; \
1100 CASE(AGGR_MODE
, aggr_mode
)
1102 CASE(INTERVAL
, interval
)
1105 pr_warning("unknown stat config term %" PRIu64
"\n",
1106 event
->data
[i
].tag
);
1111 size_t perf_event__fprintf_comm(union perf_event
*event
, FILE *fp
)
1115 if (event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
)
1120 return fprintf(fp
, "%s: %s:%d/%d\n", s
, event
->comm
.comm
, event
->comm
.pid
, event
->comm
.tid
);
1123 size_t perf_event__fprintf_namespaces(union perf_event
*event
, FILE *fp
)
1126 struct perf_ns_link_info
*ns_link_info
;
1127 u32 nr_namespaces
, idx
;
1129 ns_link_info
= event
->namespaces
.link_info
;
1130 nr_namespaces
= event
->namespaces
.nr_namespaces
;
1132 ret
+= fprintf(fp
, " %d/%d - nr_namespaces: %u\n\t\t[",
1133 event
->namespaces
.pid
,
1134 event
->namespaces
.tid
,
1137 for (idx
= 0; idx
< nr_namespaces
; idx
++) {
1138 if (idx
&& (idx
% 4 == 0))
1139 ret
+= fprintf(fp
, "\n\t\t ");
1141 ret
+= fprintf(fp
, "%u/%s: %" PRIu64
"/%#" PRIx64
"%s", idx
,
1142 perf_ns__name(idx
), (u64
)ns_link_info
[idx
].dev
,
1143 (u64
)ns_link_info
[idx
].ino
,
1144 ((idx
+ 1) != nr_namespaces
) ? ", " : "]\n");
1150 int perf_event__process_comm(struct perf_tool
*tool __maybe_unused
,
1151 union perf_event
*event
,
1152 struct perf_sample
*sample
,
1153 struct machine
*machine
)
1155 return machine__process_comm_event(machine
, event
, sample
);
1158 int perf_event__process_namespaces(struct perf_tool
*tool __maybe_unused
,
1159 union perf_event
*event
,
1160 struct perf_sample
*sample
,
1161 struct machine
*machine
)
1163 return machine__process_namespaces_event(machine
, event
, sample
);
1166 int perf_event__process_lost(struct perf_tool
*tool __maybe_unused
,
1167 union perf_event
*event
,
1168 struct perf_sample
*sample
,
1169 struct machine
*machine
)
1171 return machine__process_lost_event(machine
, event
, sample
);
1174 int perf_event__process_aux(struct perf_tool
*tool __maybe_unused
,
1175 union perf_event
*event
,
1176 struct perf_sample
*sample __maybe_unused
,
1177 struct machine
*machine
)
1179 return machine__process_aux_event(machine
, event
);
1182 int perf_event__process_itrace_start(struct perf_tool
*tool __maybe_unused
,
1183 union perf_event
*event
,
1184 struct perf_sample
*sample __maybe_unused
,
1185 struct machine
*machine
)
1187 return machine__process_itrace_start_event(machine
, event
);
1190 int perf_event__process_lost_samples(struct perf_tool
*tool __maybe_unused
,
1191 union perf_event
*event
,
1192 struct perf_sample
*sample
,
1193 struct machine
*machine
)
1195 return machine__process_lost_samples_event(machine
, event
, sample
);
1198 int perf_event__process_switch(struct perf_tool
*tool __maybe_unused
,
1199 union perf_event
*event
,
1200 struct perf_sample
*sample __maybe_unused
,
1201 struct machine
*machine
)
1203 return machine__process_switch_event(machine
, event
);
1206 size_t perf_event__fprintf_mmap(union perf_event
*event
, FILE *fp
)
1208 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
"]: %c %s\n",
1209 event
->mmap
.pid
, event
->mmap
.tid
, event
->mmap
.start
,
1210 event
->mmap
.len
, event
->mmap
.pgoff
,
1211 (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
) ? 'r' : 'x',
1212 event
->mmap
.filename
);
1215 size_t perf_event__fprintf_mmap2(union perf_event
*event
, FILE *fp
)
1217 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
1218 " %02x:%02x %"PRIu64
" %"PRIu64
"]: %c%c%c%c %s\n",
1219 event
->mmap2
.pid
, event
->mmap2
.tid
, event
->mmap2
.start
,
1220 event
->mmap2
.len
, event
->mmap2
.pgoff
, event
->mmap2
.maj
,
1221 event
->mmap2
.min
, event
->mmap2
.ino
,
1222 event
->mmap2
.ino_generation
,
1223 (event
->mmap2
.prot
& PROT_READ
) ? 'r' : '-',
1224 (event
->mmap2
.prot
& PROT_WRITE
) ? 'w' : '-',
1225 (event
->mmap2
.prot
& PROT_EXEC
) ? 'x' : '-',
1226 (event
->mmap2
.flags
& MAP_SHARED
) ? 's' : 'p',
1227 event
->mmap2
.filename
);
1230 size_t perf_event__fprintf_thread_map(union perf_event
*event
, FILE *fp
)
1232 struct thread_map
*threads
= thread_map__new_event(&event
->thread_map
);
1235 ret
= fprintf(fp
, " nr: ");
1238 ret
+= thread_map__fprintf(threads
, fp
);
1240 ret
+= fprintf(fp
, "failed to get threads from event\n");
1242 thread_map__put(threads
);
1246 size_t perf_event__fprintf_cpu_map(union perf_event
*event
, FILE *fp
)
1248 struct cpu_map
*cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
1251 ret
= fprintf(fp
, ": ");
1254 ret
+= cpu_map__fprintf(cpus
, fp
);
1256 ret
+= fprintf(fp
, "failed to get cpumap from event\n");
1262 int perf_event__process_mmap(struct perf_tool
*tool __maybe_unused
,
1263 union perf_event
*event
,
1264 struct perf_sample
*sample
,
1265 struct machine
*machine
)
1267 return machine__process_mmap_event(machine
, event
, sample
);
1270 int perf_event__process_mmap2(struct perf_tool
*tool __maybe_unused
,
1271 union perf_event
*event
,
1272 struct perf_sample
*sample
,
1273 struct machine
*machine
)
1275 return machine__process_mmap2_event(machine
, event
, sample
);
1278 size_t perf_event__fprintf_task(union perf_event
*event
, FILE *fp
)
1280 return fprintf(fp
, "(%d:%d):(%d:%d)\n",
1281 event
->fork
.pid
, event
->fork
.tid
,
1282 event
->fork
.ppid
, event
->fork
.ptid
);
1285 int perf_event__process_fork(struct perf_tool
*tool __maybe_unused
,
1286 union perf_event
*event
,
1287 struct perf_sample
*sample
,
1288 struct machine
*machine
)
1290 return machine__process_fork_event(machine
, event
, sample
);
1293 int perf_event__process_exit(struct perf_tool
*tool __maybe_unused
,
1294 union perf_event
*event
,
1295 struct perf_sample
*sample
,
1296 struct machine
*machine
)
1298 return machine__process_exit_event(machine
, event
, sample
);
1301 size_t perf_event__fprintf_aux(union perf_event
*event
, FILE *fp
)
1303 return fprintf(fp
, " offset: %#"PRIx64
" size: %#"PRIx64
" flags: %#"PRIx64
" [%s%s%s]\n",
1304 event
->aux
.aux_offset
, event
->aux
.aux_size
,
1306 event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
? "T" : "",
1307 event
->aux
.flags
& PERF_AUX_FLAG_OVERWRITE
? "O" : "",
1308 event
->aux
.flags
& PERF_AUX_FLAG_PARTIAL
? "P" : "");
1311 size_t perf_event__fprintf_itrace_start(union perf_event
*event
, FILE *fp
)
1313 return fprintf(fp
, " pid: %u tid: %u\n",
1314 event
->itrace_start
.pid
, event
->itrace_start
.tid
);
1317 size_t perf_event__fprintf_switch(union perf_event
*event
, FILE *fp
)
1319 bool out
= event
->header
.misc
& PERF_RECORD_MISC_SWITCH_OUT
;
1320 const char *in_out
= out
? "OUT" : "IN ";
1322 if (event
->header
.type
== PERF_RECORD_SWITCH
)
1323 return fprintf(fp
, " %s\n", in_out
);
1325 return fprintf(fp
, " %s %s pid/tid: %5u/%-5u\n",
1326 in_out
, out
? "next" : "prev",
1327 event
->context_switch
.next_prev_pid
,
1328 event
->context_switch
.next_prev_tid
);
1331 size_t perf_event__fprintf(union perf_event
*event
, FILE *fp
)
1333 size_t ret
= fprintf(fp
, "PERF_RECORD_%s",
1334 perf_event__name(event
->header
.type
));
1336 switch (event
->header
.type
) {
1337 case PERF_RECORD_COMM
:
1338 ret
+= perf_event__fprintf_comm(event
, fp
);
1340 case PERF_RECORD_FORK
:
1341 case PERF_RECORD_EXIT
:
1342 ret
+= perf_event__fprintf_task(event
, fp
);
1344 case PERF_RECORD_MMAP
:
1345 ret
+= perf_event__fprintf_mmap(event
, fp
);
1347 case PERF_RECORD_NAMESPACES
:
1348 ret
+= perf_event__fprintf_namespaces(event
, fp
);
1350 case PERF_RECORD_MMAP2
:
1351 ret
+= perf_event__fprintf_mmap2(event
, fp
);
1353 case PERF_RECORD_AUX
:
1354 ret
+= perf_event__fprintf_aux(event
, fp
);
1356 case PERF_RECORD_ITRACE_START
:
1357 ret
+= perf_event__fprintf_itrace_start(event
, fp
);
1359 case PERF_RECORD_SWITCH
:
1360 case PERF_RECORD_SWITCH_CPU_WIDE
:
1361 ret
+= perf_event__fprintf_switch(event
, fp
);
1364 ret
+= fprintf(fp
, "\n");
1370 int perf_event__process(struct perf_tool
*tool __maybe_unused
,
1371 union perf_event
*event
,
1372 struct perf_sample
*sample
,
1373 struct machine
*machine
)
1375 return machine__process_event(machine
, event
, sample
);
1378 void thread__find_addr_map(struct thread
*thread
, u8 cpumode
,
1379 enum map_type type
, u64 addr
,
1380 struct addr_location
*al
)
1382 struct map_groups
*mg
= thread
->mg
;
1383 struct machine
*machine
= mg
->machine
;
1384 bool load_map
= false;
1386 al
->machine
= machine
;
1387 al
->thread
= thread
;
1389 al
->cpumode
= cpumode
;
1392 if (machine
== NULL
) {
1397 if (cpumode
== PERF_RECORD_MISC_KERNEL
&& perf_host
) {
1399 mg
= &machine
->kmaps
;
1401 } else if (cpumode
== PERF_RECORD_MISC_USER
&& perf_host
) {
1403 } else if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
1405 mg
= &machine
->kmaps
;
1407 } else if (cpumode
== PERF_RECORD_MISC_GUEST_USER
&& perf_guest
) {
1413 if ((cpumode
== PERF_RECORD_MISC_GUEST_USER
||
1414 cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) &&
1416 al
->filtered
|= (1 << HIST_FILTER__GUEST
);
1417 if ((cpumode
== PERF_RECORD_MISC_USER
||
1418 cpumode
== PERF_RECORD_MISC_KERNEL
) &&
1420 al
->filtered
|= (1 << HIST_FILTER__HOST
);
1425 al
->map
= map_groups__find(mg
, type
, al
->addr
);
1426 if (al
->map
== NULL
) {
1428 * If this is outside of all known maps, and is a negative
1429 * address, try to look it up in the kernel dso, as it might be
1430 * a vsyscall or vdso (which executes in user-mode).
1432 * XXX This is nasty, we should have a symbol list in the
1433 * "[vdso]" dso, but for now lets use the old trick of looking
1434 * in the whole kernel symbol list.
1436 if (cpumode
== PERF_RECORD_MISC_USER
&& machine
&&
1437 mg
!= &machine
->kmaps
&&
1438 machine__kernel_ip(machine
, al
->addr
)) {
1439 mg
= &machine
->kmaps
;
1445 * Kernel maps might be changed when loading symbols so loading
1446 * must be done prior to using kernel maps.
1450 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
1454 void thread__find_addr_location(struct thread
*thread
,
1455 u8 cpumode
, enum map_type type
, u64 addr
,
1456 struct addr_location
*al
)
1458 thread__find_addr_map(thread
, cpumode
, type
, addr
, al
);
1459 if (al
->map
!= NULL
)
1460 al
->sym
= map__find_symbol(al
->map
, al
->addr
);
1466 * Callers need to drop the reference to al->thread, obtained in
1467 * machine__findnew_thread()
1469 int machine__resolve(struct machine
*machine
, struct addr_location
*al
,
1470 struct perf_sample
*sample
)
1472 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
1478 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread
), thread
->tid
);
1480 * Have we already created the kernel maps for this machine?
1482 * This should have happened earlier, when we processed the kernel MMAP
1483 * events, but for older perf.data files there was no such thing, so do
1486 if (sample
->cpumode
== PERF_RECORD_MISC_KERNEL
&&
1487 machine__kernel_map(machine
) == NULL
)
1488 machine__create_kernel_maps(machine
);
1490 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, sample
->ip
, al
);
1491 dump_printf(" ...... dso: %s\n",
1492 al
->map
? al
->map
->dso
->long_name
:
1493 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
1495 if (thread__is_filtered(thread
))
1496 al
->filtered
|= (1 << HIST_FILTER__THREAD
);
1499 al
->cpu
= sample
->cpu
;
1503 struct perf_env
*env
= machine
->env
;
1505 if (env
&& env
->cpu
)
1506 al
->socket
= env
->cpu
[al
->cpu
].socket_id
;
1510 struct dso
*dso
= al
->map
->dso
;
1512 if (symbol_conf
.dso_list
&&
1513 (!dso
|| !(strlist__has_entry(symbol_conf
.dso_list
,
1515 (dso
->short_name
!= dso
->long_name
&&
1516 strlist__has_entry(symbol_conf
.dso_list
,
1517 dso
->long_name
))))) {
1518 al
->filtered
|= (1 << HIST_FILTER__DSO
);
1521 al
->sym
= map__find_symbol(al
->map
, al
->addr
);
1524 if (symbol_conf
.sym_list
&&
1525 (!al
->sym
|| !strlist__has_entry(symbol_conf
.sym_list
,
1527 al
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1534 * The preprocess_sample method will return with reference counts for the
1535 * in it, when done using (and perhaps getting ref counts if needing to
1536 * keep a pointer to one of those entries) it must be paired with
1537 * addr_location__put(), so that the refcounts can be decremented.
1539 void addr_location__put(struct addr_location
*al
)
1541 thread__zput(al
->thread
);
1544 bool is_bts_event(struct perf_event_attr
*attr
)
1546 return attr
->type
== PERF_TYPE_HARDWARE
&&
1547 (attr
->config
& PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
1548 attr
->sample_period
== 1;
1551 bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
1553 if (attr
->type
== PERF_TYPE_SOFTWARE
&&
1554 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
||
1555 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
||
1556 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
))
1559 if (is_bts_event(attr
))
1565 void thread__resolve(struct thread
*thread
, struct addr_location
*al
,
1566 struct perf_sample
*sample
)
1568 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, sample
->addr
, al
);
1570 thread__find_addr_map(thread
, sample
->cpumode
, MAP__VARIABLE
,
1573 al
->cpu
= sample
->cpu
;
1577 al
->sym
= map__find_symbol(al
->map
, al
->addr
);