1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/kernel.h>
7 #include <linux/types.h>
11 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
12 #include <api/fs/fs.h>
13 #include <linux/perf_event.h>
22 #include "thread_map.h"
23 #include "sane_ctype.h"
26 #include "symbol/kallsyms.h"
30 #include "bpf-event.h"
32 #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
34 static const char *perf_event__names
[] = {
36 [PERF_RECORD_MMAP
] = "MMAP",
37 [PERF_RECORD_MMAP2
] = "MMAP2",
38 [PERF_RECORD_LOST
] = "LOST",
39 [PERF_RECORD_COMM
] = "COMM",
40 [PERF_RECORD_EXIT
] = "EXIT",
41 [PERF_RECORD_THROTTLE
] = "THROTTLE",
42 [PERF_RECORD_UNTHROTTLE
] = "UNTHROTTLE",
43 [PERF_RECORD_FORK
] = "FORK",
44 [PERF_RECORD_READ
] = "READ",
45 [PERF_RECORD_SAMPLE
] = "SAMPLE",
46 [PERF_RECORD_AUX
] = "AUX",
47 [PERF_RECORD_ITRACE_START
] = "ITRACE_START",
48 [PERF_RECORD_LOST_SAMPLES
] = "LOST_SAMPLES",
49 [PERF_RECORD_SWITCH
] = "SWITCH",
50 [PERF_RECORD_SWITCH_CPU_WIDE
] = "SWITCH_CPU_WIDE",
51 [PERF_RECORD_NAMESPACES
] = "NAMESPACES",
52 [PERF_RECORD_KSYMBOL
] = "KSYMBOL",
53 [PERF_RECORD_BPF_EVENT
] = "BPF_EVENT",
54 [PERF_RECORD_HEADER_ATTR
] = "ATTR",
55 [PERF_RECORD_HEADER_EVENT_TYPE
] = "EVENT_TYPE",
56 [PERF_RECORD_HEADER_TRACING_DATA
] = "TRACING_DATA",
57 [PERF_RECORD_HEADER_BUILD_ID
] = "BUILD_ID",
58 [PERF_RECORD_FINISHED_ROUND
] = "FINISHED_ROUND",
59 [PERF_RECORD_ID_INDEX
] = "ID_INDEX",
60 [PERF_RECORD_AUXTRACE_INFO
] = "AUXTRACE_INFO",
61 [PERF_RECORD_AUXTRACE
] = "AUXTRACE",
62 [PERF_RECORD_AUXTRACE_ERROR
] = "AUXTRACE_ERROR",
63 [PERF_RECORD_THREAD_MAP
] = "THREAD_MAP",
64 [PERF_RECORD_CPU_MAP
] = "CPU_MAP",
65 [PERF_RECORD_STAT_CONFIG
] = "STAT_CONFIG",
66 [PERF_RECORD_STAT
] = "STAT",
67 [PERF_RECORD_STAT_ROUND
] = "STAT_ROUND",
68 [PERF_RECORD_EVENT_UPDATE
] = "EVENT_UPDATE",
69 [PERF_RECORD_TIME_CONV
] = "TIME_CONV",
70 [PERF_RECORD_HEADER_FEATURE
] = "FEATURE",
71 [PERF_RECORD_COMPRESSED
] = "COMPRESSED",
74 static const char *perf_ns__names
[] = {
75 [NET_NS_INDEX
] = "net",
76 [UTS_NS_INDEX
] = "uts",
77 [IPC_NS_INDEX
] = "ipc",
78 [PID_NS_INDEX
] = "pid",
79 [USER_NS_INDEX
] = "user",
80 [MNT_NS_INDEX
] = "mnt",
81 [CGROUP_NS_INDEX
] = "cgroup",
84 unsigned int proc_map_timeout
= DEFAULT_PROC_MAP_PARSE_TIMEOUT
;
86 const char *perf_event__name(unsigned int id
)
88 if (id
>= ARRAY_SIZE(perf_event__names
))
90 if (!perf_event__names
[id
])
92 return perf_event__names
[id
];
95 static const char *perf_ns__name(unsigned int id
)
97 if (id
>= ARRAY_SIZE(perf_ns__names
))
99 return perf_ns__names
[id
];
102 int perf_tool__process_synth_event(struct perf_tool
*tool
,
103 union perf_event
*event
,
104 struct machine
*machine
,
105 perf_event__handler_t process
)
107 struct perf_sample synth_sample
= {
114 .cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
,
117 return process(tool
, event
, &synth_sample
, machine
);
121 * Assumes that the first 4095 bytes of /proc/pid/stat contains
122 * the comm, tgid and ppid.
124 static int perf_event__get_comm_ids(pid_t pid
, char *comm
, size_t len
,
125 pid_t
*tgid
, pid_t
*ppid
)
127 char filename
[PATH_MAX
];
132 char *name
, *tgids
, *ppids
;
137 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
139 fd
= open(filename
, O_RDONLY
);
141 pr_debug("couldn't open %s\n", filename
);
145 n
= read(fd
, bf
, sizeof(bf
) - 1);
148 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
154 name
= strstr(bf
, "Name:");
155 tgids
= strstr(bf
, "Tgid:");
156 ppids
= strstr(bf
, "PPid:");
161 name
+= 5; /* strlen("Name:") */
164 nl
= strchr(name
, '\n');
171 memcpy(comm
, name
, size
);
174 pr_debug("Name: string not found for pid %d\n", pid
);
178 tgids
+= 5; /* strlen("Tgid:") */
181 pr_debug("Tgid: string not found for pid %d\n", pid
);
185 ppids
+= 5; /* strlen("PPid:") */
188 pr_debug("PPid: string not found for pid %d\n", pid
);
194 static int perf_event__prepare_comm(union perf_event
*event
, pid_t pid
,
195 struct machine
*machine
,
196 pid_t
*tgid
, pid_t
*ppid
)
202 memset(&event
->comm
, 0, sizeof(event
->comm
));
204 if (machine__is_host(machine
)) {
205 if (perf_event__get_comm_ids(pid
, event
->comm
.comm
,
206 sizeof(event
->comm
.comm
),
211 *tgid
= machine
->pid
;
217 event
->comm
.pid
= *tgid
;
218 event
->comm
.header
.type
= PERF_RECORD_COMM
;
220 size
= strlen(event
->comm
.comm
) + 1;
221 size
= PERF_ALIGN(size
, sizeof(u64
));
222 memset(event
->comm
.comm
+ size
, 0, machine
->id_hdr_size
);
223 event
->comm
.header
.size
= (sizeof(event
->comm
) -
224 (sizeof(event
->comm
.comm
) - size
) +
225 machine
->id_hdr_size
);
226 event
->comm
.tid
= pid
;
231 pid_t
perf_event__synthesize_comm(struct perf_tool
*tool
,
232 union perf_event
*event
, pid_t pid
,
233 perf_event__handler_t process
,
234 struct machine
*machine
)
238 if (perf_event__prepare_comm(event
, pid
, machine
, &tgid
, &ppid
) != 0)
241 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
247 static void perf_event__get_ns_link_info(pid_t pid
, const char *ns
,
248 struct perf_ns_link_info
*ns_link_info
)
253 sprintf(proc_ns
, "/proc/%u/ns/%s", pid
, ns
);
254 if (stat64(proc_ns
, &st
) == 0) {
255 ns_link_info
->dev
= st
.st_dev
;
256 ns_link_info
->ino
= st
.st_ino
;
260 int perf_event__synthesize_namespaces(struct perf_tool
*tool
,
261 union perf_event
*event
,
262 pid_t pid
, pid_t tgid
,
263 perf_event__handler_t process
,
264 struct machine
*machine
)
267 struct perf_ns_link_info
*ns_link_info
;
269 if (!tool
|| !tool
->namespace_events
)
272 memset(&event
->namespaces
, 0, (sizeof(event
->namespaces
) +
273 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
274 machine
->id_hdr_size
));
276 event
->namespaces
.pid
= tgid
;
277 event
->namespaces
.tid
= pid
;
279 event
->namespaces
.nr_namespaces
= NR_NAMESPACES
;
281 ns_link_info
= event
->namespaces
.link_info
;
283 for (idx
= 0; idx
< event
->namespaces
.nr_namespaces
; idx
++)
284 perf_event__get_ns_link_info(pid
, perf_ns__name(idx
),
287 event
->namespaces
.header
.type
= PERF_RECORD_NAMESPACES
;
289 event
->namespaces
.header
.size
= (sizeof(event
->namespaces
) +
290 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
291 machine
->id_hdr_size
);
293 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
299 static int perf_event__synthesize_fork(struct perf_tool
*tool
,
300 union perf_event
*event
,
301 pid_t pid
, pid_t tgid
, pid_t ppid
,
302 perf_event__handler_t process
,
303 struct machine
*machine
)
305 memset(&event
->fork
, 0, sizeof(event
->fork
) + machine
->id_hdr_size
);
308 * for main thread set parent to ppid from status file. For other
309 * threads set parent pid to main thread. ie., assume main thread
310 * spawns all threads in a process
313 event
->fork
.ppid
= ppid
;
314 event
->fork
.ptid
= ppid
;
316 event
->fork
.ppid
= tgid
;
317 event
->fork
.ptid
= tgid
;
319 event
->fork
.pid
= tgid
;
320 event
->fork
.tid
= pid
;
321 event
->fork
.header
.type
= PERF_RECORD_FORK
;
322 event
->fork
.header
.misc
= PERF_RECORD_MISC_FORK_EXEC
;
324 event
->fork
.header
.size
= (sizeof(event
->fork
) + machine
->id_hdr_size
);
326 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
332 int perf_event__synthesize_mmap_events(struct perf_tool
*tool
,
333 union perf_event
*event
,
334 pid_t pid
, pid_t tgid
,
335 perf_event__handler_t process
,
336 struct machine
*machine
,
339 char filename
[PATH_MAX
];
341 unsigned long long t
;
342 bool truncation
= false;
343 unsigned long long timeout
= proc_map_timeout
* 1000000ULL;
345 const char *hugetlbfs_mnt
= hugetlbfs__mountpoint();
346 int hugetlbfs_mnt_len
= hugetlbfs_mnt
? strlen(hugetlbfs_mnt
) : 0;
348 if (machine__is_default_guest(machine
))
351 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task/%d/maps",
352 machine
->root_dir
, pid
, pid
);
354 fp
= fopen(filename
, "r");
357 * We raced with a task exiting - just return:
359 pr_debug("couldn't open %s\n", filename
);
363 event
->header
.type
= PERF_RECORD_MMAP2
;
369 char execname
[PATH_MAX
];
370 char anonstr
[] = "//anon";
375 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
378 if ((rdclock() - t
) > timeout
) {
379 pr_warning("Reading %s time out. "
380 "You may want to increase "
381 "the time limit by --proc-map-timeout\n",
387 /* ensure null termination since stack will be reused. */
388 strcpy(execname
, "");
390 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
391 n
= sscanf(bf
, "%"PRIx64
"-%"PRIx64
" %s %"PRIx64
" %x:%x %u %[^\n]\n",
392 &event
->mmap2
.start
, &event
->mmap2
.len
, prot
,
393 &event
->mmap2
.pgoff
, &event
->mmap2
.maj
,
398 * Anon maps don't have the execname.
403 event
->mmap2
.ino
= (u64
)ino
;
406 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
408 if (machine__is_host(machine
))
409 event
->header
.misc
= PERF_RECORD_MISC_USER
;
411 event
->header
.misc
= PERF_RECORD_MISC_GUEST_USER
;
413 /* map protection and flags bits */
414 event
->mmap2
.prot
= 0;
415 event
->mmap2
.flags
= 0;
417 event
->mmap2
.prot
|= PROT_READ
;
419 event
->mmap2
.prot
|= PROT_WRITE
;
421 event
->mmap2
.prot
|= PROT_EXEC
;
424 event
->mmap2
.flags
|= MAP_SHARED
;
426 event
->mmap2
.flags
|= MAP_PRIVATE
;
428 if (prot
[2] != 'x') {
429 if (!mmap_data
|| prot
[0] != 'r')
432 event
->header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
437 event
->header
.misc
|= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
;
439 if (!strcmp(execname
, ""))
440 strcpy(execname
, anonstr
);
442 if (hugetlbfs_mnt_len
&&
443 !strncmp(execname
, hugetlbfs_mnt
, hugetlbfs_mnt_len
)) {
444 strcpy(execname
, anonstr
);
445 event
->mmap2
.flags
|= MAP_HUGETLB
;
448 size
= strlen(execname
) + 1;
449 memcpy(event
->mmap2
.filename
, execname
, size
);
450 size
= PERF_ALIGN(size
, sizeof(u64
));
451 event
->mmap2
.len
-= event
->mmap
.start
;
452 event
->mmap2
.header
.size
= (sizeof(event
->mmap2
) -
453 (sizeof(event
->mmap2
.filename
) - size
));
454 memset(event
->mmap2
.filename
+ size
, 0, machine
->id_hdr_size
);
455 event
->mmap2
.header
.size
+= machine
->id_hdr_size
;
456 event
->mmap2
.pid
= tgid
;
457 event
->mmap2
.tid
= pid
;
459 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
472 int perf_event__synthesize_modules(struct perf_tool
*tool
,
473 perf_event__handler_t process
,
474 struct machine
*machine
)
478 struct maps
*maps
= machine__kernel_maps(machine
);
479 union perf_event
*event
= zalloc((sizeof(event
->mmap
) +
480 machine
->id_hdr_size
));
482 pr_debug("Not enough memory synthesizing mmap event "
483 "for kernel modules\n");
487 event
->header
.type
= PERF_RECORD_MMAP
;
490 * kernel uses 0 for user space maps, see kernel/perf_event.c
493 if (machine__is_host(machine
))
494 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
496 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
498 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
501 if (!__map__is_kmodule(pos
))
504 size
= PERF_ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
505 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
506 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
507 (sizeof(event
->mmap
.filename
) - size
));
508 memset(event
->mmap
.filename
+ size
, 0, machine
->id_hdr_size
);
509 event
->mmap
.header
.size
+= machine
->id_hdr_size
;
510 event
->mmap
.start
= pos
->start
;
511 event
->mmap
.len
= pos
->end
- pos
->start
;
512 event
->mmap
.pid
= machine
->pid
;
514 memcpy(event
->mmap
.filename
, pos
->dso
->long_name
,
515 pos
->dso
->long_name_len
+ 1);
516 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
526 static int __event__synthesize_thread(union perf_event
*comm_event
,
527 union perf_event
*mmap_event
,
528 union perf_event
*fork_event
,
529 union perf_event
*namespaces_event
,
531 perf_event__handler_t process
,
532 struct perf_tool
*tool
,
533 struct machine
*machine
,
536 char filename
[PATH_MAX
];
538 struct dirent
*dirent
;
542 /* special case: only send one comm event using passed in pid */
544 tgid
= perf_event__synthesize_comm(tool
, comm_event
, pid
,
550 if (perf_event__synthesize_namespaces(tool
, namespaces_event
, pid
,
551 tgid
, process
, machine
) < 0)
555 * send mmap only for thread group leader
556 * see thread__init_map_groups
559 perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
560 process
, machine
, mmap_data
))
566 if (machine__is_default_guest(machine
))
569 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task",
570 machine
->root_dir
, pid
);
572 tasks
= opendir(filename
);
574 pr_debug("couldn't open %s\n", filename
);
578 while ((dirent
= readdir(tasks
)) != NULL
) {
582 _pid
= strtol(dirent
->d_name
, &end
, 10);
587 if (perf_event__prepare_comm(comm_event
, _pid
, machine
,
591 if (perf_event__synthesize_fork(tool
, fork_event
, _pid
, tgid
,
592 ppid
, process
, machine
) < 0)
595 if (perf_event__synthesize_namespaces(tool
, namespaces_event
, _pid
,
596 tgid
, process
, machine
) < 0)
600 * Send the prepared comm event
602 if (perf_tool__process_synth_event(tool
, comm_event
, machine
, process
) != 0)
607 /* process the parent's maps too */
608 rc
= perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
609 process
, machine
, mmap_data
);
619 int perf_event__synthesize_thread_map(struct perf_tool
*tool
,
620 struct thread_map
*threads
,
621 perf_event__handler_t process
,
622 struct machine
*machine
,
625 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
626 union perf_event
*namespaces_event
;
627 int err
= -1, thread
, j
;
629 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
630 if (comm_event
== NULL
)
633 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
634 if (mmap_event
== NULL
)
637 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
638 if (fork_event
== NULL
)
641 namespaces_event
= malloc(sizeof(namespaces_event
->namespaces
) +
642 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
643 machine
->id_hdr_size
);
644 if (namespaces_event
== NULL
)
648 for (thread
= 0; thread
< threads
->nr
; ++thread
) {
649 if (__event__synthesize_thread(comm_event
, mmap_event
,
650 fork_event
, namespaces_event
,
651 thread_map__pid(threads
, thread
), 0,
652 process
, tool
, machine
,
659 * comm.pid is set to thread group id by
660 * perf_event__synthesize_comm
662 if ((int) comm_event
->comm
.pid
!= thread_map__pid(threads
, thread
)) {
663 bool need_leader
= true;
665 /* is thread group leader in thread_map? */
666 for (j
= 0; j
< threads
->nr
; ++j
) {
667 if ((int) comm_event
->comm
.pid
== thread_map__pid(threads
, j
)) {
673 /* if not, generate events for it */
675 __event__synthesize_thread(comm_event
, mmap_event
,
676 fork_event
, namespaces_event
,
677 comm_event
->comm
.pid
, 0,
678 process
, tool
, machine
,
685 free(namespaces_event
);
696 static int __perf_event__synthesize_threads(struct perf_tool
*tool
,
697 perf_event__handler_t process
,
698 struct machine
*machine
,
700 struct dirent
**dirent
,
704 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
705 union perf_event
*namespaces_event
;
711 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
712 if (comm_event
== NULL
)
715 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
716 if (mmap_event
== NULL
)
719 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
720 if (fork_event
== NULL
)
723 namespaces_event
= malloc(sizeof(namespaces_event
->namespaces
) +
724 (NR_NAMESPACES
* sizeof(struct perf_ns_link_info
)) +
725 machine
->id_hdr_size
);
726 if (namespaces_event
== NULL
)
729 for (i
= start
; i
< start
+ num
; i
++) {
730 if (!isdigit(dirent
[i
]->d_name
[0]))
733 pid
= (pid_t
)strtol(dirent
[i
]->d_name
, &end
, 10);
734 /* only interested in proper numerical dirents */
738 * We may race with exiting thread, so don't stop just because
739 * one thread couldn't be synthesized.
741 __event__synthesize_thread(comm_event
, mmap_event
, fork_event
,
742 namespaces_event
, pid
, 1, process
,
743 tool
, machine
, mmap_data
);
747 free(namespaces_event
);
758 struct synthesize_threads_arg
{
759 struct perf_tool
*tool
;
760 perf_event__handler_t process
;
761 struct machine
*machine
;
763 struct dirent
**dirent
;
768 static void *synthesize_threads_worker(void *arg
)
770 struct synthesize_threads_arg
*args
= arg
;
772 __perf_event__synthesize_threads(args
->tool
, args
->process
,
773 args
->machine
, args
->mmap_data
,
775 args
->start
, args
->num
);
779 int perf_event__synthesize_threads(struct perf_tool
*tool
,
780 perf_event__handler_t process
,
781 struct machine
*machine
,
783 unsigned int nr_threads_synthesize
)
785 struct synthesize_threads_arg
*args
= NULL
;
786 pthread_t
*synthesize_threads
= NULL
;
787 char proc_path
[PATH_MAX
];
788 struct dirent
**dirent
;
796 if (machine__is_default_guest(machine
))
799 snprintf(proc_path
, sizeof(proc_path
), "%s/proc", machine
->root_dir
);
800 n
= scandir(proc_path
, &dirent
, 0, alphasort
);
804 if (nr_threads_synthesize
== UINT_MAX
)
805 thread_nr
= sysconf(_SC_NPROCESSORS_ONLN
);
807 thread_nr
= nr_threads_synthesize
;
809 if (thread_nr
<= 1) {
810 err
= __perf_event__synthesize_threads(tool
, process
,
818 synthesize_threads
= calloc(sizeof(pthread_t
), thread_nr
);
819 if (synthesize_threads
== NULL
)
822 args
= calloc(sizeof(*args
), thread_nr
);
826 num_per_thread
= n
/ thread_nr
;
828 for (i
= 0; i
< thread_nr
; i
++) {
830 args
[i
].process
= process
;
831 args
[i
].machine
= machine
;
832 args
[i
].mmap_data
= mmap_data
;
833 args
[i
].dirent
= dirent
;
835 for (i
= 0; i
< m
; i
++) {
836 args
[i
].num
= num_per_thread
+ 1;
837 args
[i
].start
= i
* args
[i
].num
;
840 base
= args
[i
-1].start
+ args
[i
-1].num
;
841 for (j
= i
; j
< thread_nr
; j
++) {
842 args
[j
].num
= num_per_thread
;
843 args
[j
].start
= base
+ (j
- i
) * args
[i
].num
;
846 for (i
= 0; i
< thread_nr
; i
++) {
847 if (pthread_create(&synthesize_threads
[i
], NULL
,
848 synthesize_threads_worker
, &args
[i
]))
853 for (i
= 0; i
< thread_nr
; i
++)
854 pthread_join(synthesize_threads
[i
], NULL
);
857 free(synthesize_threads
);
859 for (i
= 0; i
< n
; i
++)
866 struct process_symbol_args
{
871 static int find_symbol_cb(void *arg
, const char *name
, char type
,
874 struct process_symbol_args
*args
= arg
;
877 * Must be a function or at least an alias, as in PARISC64, where "_text" is
878 * an 'A' to the same address as "_stext".
880 if (!(kallsyms__is_function(type
) ||
881 type
== 'A') || strcmp(name
, args
->name
))
888 int kallsyms__get_function_start(const char *kallsyms_filename
,
889 const char *symbol_name
, u64
*addr
)
891 struct process_symbol_args args
= { .name
= symbol_name
, };
893 if (kallsyms__parse(kallsyms_filename
, &args
, find_symbol_cb
) <= 0)
900 int __weak
perf_event__synthesize_extra_kmaps(struct perf_tool
*tool __maybe_unused
,
901 perf_event__handler_t process __maybe_unused
,
902 struct machine
*machine __maybe_unused
)
907 static int __perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
908 perf_event__handler_t process
,
909 struct machine
*machine
)
912 struct map
*map
= machine__kernel_map(machine
);
915 union perf_event
*event
;
917 if (symbol_conf
.kptr_restrict
)
923 * We should get this from /sys/kernel/sections/.text, but till that is
924 * available use this, and after it is use this as a fallback for older
927 event
= zalloc((sizeof(event
->mmap
) + machine
->id_hdr_size
));
929 pr_debug("Not enough memory synthesizing mmap event "
930 "for kernel modules\n");
934 if (machine__is_host(machine
)) {
936 * kernel uses PERF_RECORD_MISC_USER for user space maps,
937 * see kernel/perf_event.c __perf_event_mmap
939 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
941 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
944 kmap
= map__kmap(map
);
945 size
= snprintf(event
->mmap
.filename
, sizeof(event
->mmap
.filename
),
946 "%s%s", machine
->mmap_name
, kmap
->ref_reloc_sym
->name
) + 1;
947 size
= PERF_ALIGN(size
, sizeof(u64
));
948 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
949 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
950 (sizeof(event
->mmap
.filename
) - size
) + machine
->id_hdr_size
);
951 event
->mmap
.pgoff
= kmap
->ref_reloc_sym
->addr
;
952 event
->mmap
.start
= map
->start
;
953 event
->mmap
.len
= map
->end
- event
->mmap
.start
;
954 event
->mmap
.pid
= machine
->pid
;
956 err
= perf_tool__process_synth_event(tool
, event
, machine
, process
);
962 int perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
963 perf_event__handler_t process
,
964 struct machine
*machine
)
968 err
= __perf_event__synthesize_kernel_mmap(tool
, process
, machine
);
972 return perf_event__synthesize_extra_kmaps(tool
, process
, machine
);
975 int perf_event__synthesize_thread_map2(struct perf_tool
*tool
,
976 struct thread_map
*threads
,
977 perf_event__handler_t process
,
978 struct machine
*machine
)
980 union perf_event
*event
;
983 size
= sizeof(event
->thread_map
);
984 size
+= threads
->nr
* sizeof(event
->thread_map
.entries
[0]);
986 event
= zalloc(size
);
990 event
->header
.type
= PERF_RECORD_THREAD_MAP
;
991 event
->header
.size
= size
;
992 event
->thread_map
.nr
= threads
->nr
;
994 for (i
= 0; i
< threads
->nr
; i
++) {
995 struct thread_map_event_entry
*entry
= &event
->thread_map
.entries
[i
];
996 char *comm
= thread_map__comm(threads
, i
);
1001 entry
->pid
= thread_map__pid(threads
, i
);
1002 strncpy((char *) &entry
->comm
, comm
, sizeof(entry
->comm
));
1005 err
= process(tool
, event
, NULL
, machine
);
1011 static void synthesize_cpus(struct cpu_map_entries
*cpus
,
1012 struct cpu_map
*map
)
1018 for (i
= 0; i
< map
->nr
; i
++)
1019 cpus
->cpu
[i
] = map
->map
[i
];
1022 static void synthesize_mask(struct cpu_map_mask
*mask
,
1023 struct cpu_map
*map
, int max
)
1027 mask
->nr
= BITS_TO_LONGS(max
);
1028 mask
->long_size
= sizeof(long);
1030 for (i
= 0; i
< map
->nr
; i
++)
1031 set_bit(map
->map
[i
], mask
->mask
);
1034 static size_t cpus_size(struct cpu_map
*map
)
1036 return sizeof(struct cpu_map_entries
) + map
->nr
* sizeof(u16
);
1039 static size_t mask_size(struct cpu_map
*map
, int *max
)
1045 for (i
= 0; i
< map
->nr
; i
++) {
1046 /* bit possition of the cpu is + 1 */
1047 int bit
= map
->map
[i
] + 1;
1053 return sizeof(struct cpu_map_mask
) + BITS_TO_LONGS(*max
) * sizeof(long);
1056 void *cpu_map_data__alloc(struct cpu_map
*map
, size_t *size
, u16
*type
, int *max
)
1058 size_t size_cpus
, size_mask
;
1059 bool is_dummy
= cpu_map__empty(map
);
1062 * Both array and mask data have variable size based
1063 * on the number of cpus and their actual values.
1064 * The size of the 'struct cpu_map_data' is:
1066 * array = size of 'struct cpu_map_entries' +
1067 * number of cpus * sizeof(u64)
1069 * mask = size of 'struct cpu_map_mask' +
1070 * maximum cpu bit converted to size of longs
1072 * and finaly + the size of 'struct cpu_map_data'.
1074 size_cpus
= cpus_size(map
);
1075 size_mask
= mask_size(map
, max
);
1077 if (is_dummy
|| (size_cpus
< size_mask
)) {
1079 *type
= PERF_CPU_MAP__CPUS
;
1082 *type
= PERF_CPU_MAP__MASK
;
1085 *size
+= sizeof(struct cpu_map_data
);
1086 *size
= PERF_ALIGN(*size
, sizeof(u64
));
1087 return zalloc(*size
);
1090 void cpu_map_data__synthesize(struct cpu_map_data
*data
, struct cpu_map
*map
,
1096 case PERF_CPU_MAP__CPUS
:
1097 synthesize_cpus((struct cpu_map_entries
*) data
->data
, map
);
1099 case PERF_CPU_MAP__MASK
:
1100 synthesize_mask((struct cpu_map_mask
*) data
->data
, map
, max
);
1106 static struct cpu_map_event
* cpu_map_event__new(struct cpu_map
*map
)
1108 size_t size
= sizeof(struct cpu_map_event
);
1109 struct cpu_map_event
*event
;
1113 event
= cpu_map_data__alloc(map
, &size
, &type
, &max
);
1117 event
->header
.type
= PERF_RECORD_CPU_MAP
;
1118 event
->header
.size
= size
;
1119 event
->data
.type
= type
;
1121 cpu_map_data__synthesize(&event
->data
, map
, type
, max
);
1125 int perf_event__synthesize_cpu_map(struct perf_tool
*tool
,
1126 struct cpu_map
*map
,
1127 perf_event__handler_t process
,
1128 struct machine
*machine
)
1130 struct cpu_map_event
*event
;
1133 event
= cpu_map_event__new(map
);
1137 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
1143 int perf_event__synthesize_stat_config(struct perf_tool
*tool
,
1144 struct perf_stat_config
*config
,
1145 perf_event__handler_t process
,
1146 struct machine
*machine
)
1148 struct stat_config_event
*event
;
1149 int size
, i
= 0, err
;
1151 size
= sizeof(*event
);
1152 size
+= (PERF_STAT_CONFIG_TERM__MAX
* sizeof(event
->data
[0]));
1154 event
= zalloc(size
);
1158 event
->header
.type
= PERF_RECORD_STAT_CONFIG
;
1159 event
->header
.size
= size
;
1160 event
->nr
= PERF_STAT_CONFIG_TERM__MAX
;
1162 #define ADD(__term, __val) \
1163 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1164 event->data[i].val = __val; \
1167 ADD(AGGR_MODE
, config
->aggr_mode
)
1168 ADD(INTERVAL
, config
->interval
)
1169 ADD(SCALE
, config
->scale
)
1171 WARN_ONCE(i
!= PERF_STAT_CONFIG_TERM__MAX
,
1172 "stat config terms unbalanced\n");
1175 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
1181 int perf_event__synthesize_stat(struct perf_tool
*tool
,
1182 u32 cpu
, u32 thread
, u64 id
,
1183 struct perf_counts_values
*count
,
1184 perf_event__handler_t process
,
1185 struct machine
*machine
)
1187 struct stat_event event
;
1189 event
.header
.type
= PERF_RECORD_STAT
;
1190 event
.header
.size
= sizeof(event
);
1191 event
.header
.misc
= 0;
1195 event
.thread
= thread
;
1196 event
.val
= count
->val
;
1197 event
.ena
= count
->ena
;
1198 event
.run
= count
->run
;
1200 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
1203 int perf_event__synthesize_stat_round(struct perf_tool
*tool
,
1204 u64 evtime
, u64 type
,
1205 perf_event__handler_t process
,
1206 struct machine
*machine
)
1208 struct stat_round_event event
;
1210 event
.header
.type
= PERF_RECORD_STAT_ROUND
;
1211 event
.header
.size
= sizeof(event
);
1212 event
.header
.misc
= 0;
1214 event
.time
= evtime
;
1217 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
1220 void perf_event__read_stat_config(struct perf_stat_config
*config
,
1221 struct stat_config_event
*event
)
1225 for (i
= 0; i
< event
->nr
; i
++) {
1227 switch (event
->data
[i
].tag
) {
1228 #define CASE(__term, __val) \
1229 case PERF_STAT_CONFIG_TERM__##__term: \
1230 config->__val = event->data[i].val; \
1233 CASE(AGGR_MODE
, aggr_mode
)
1235 CASE(INTERVAL
, interval
)
1238 pr_warning("unknown stat config term %" PRIu64
"\n",
1239 event
->data
[i
].tag
);
1244 size_t perf_event__fprintf_comm(union perf_event
*event
, FILE *fp
)
1248 if (event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
)
1253 return fprintf(fp
, "%s: %s:%d/%d\n", s
, event
->comm
.comm
, event
->comm
.pid
, event
->comm
.tid
);
1256 size_t perf_event__fprintf_namespaces(union perf_event
*event
, FILE *fp
)
1259 struct perf_ns_link_info
*ns_link_info
;
1260 u32 nr_namespaces
, idx
;
1262 ns_link_info
= event
->namespaces
.link_info
;
1263 nr_namespaces
= event
->namespaces
.nr_namespaces
;
1265 ret
+= fprintf(fp
, " %d/%d - nr_namespaces: %u\n\t\t[",
1266 event
->namespaces
.pid
,
1267 event
->namespaces
.tid
,
1270 for (idx
= 0; idx
< nr_namespaces
; idx
++) {
1271 if (idx
&& (idx
% 4 == 0))
1272 ret
+= fprintf(fp
, "\n\t\t ");
1274 ret
+= fprintf(fp
, "%u/%s: %" PRIu64
"/%#" PRIx64
"%s", idx
,
1275 perf_ns__name(idx
), (u64
)ns_link_info
[idx
].dev
,
1276 (u64
)ns_link_info
[idx
].ino
,
1277 ((idx
+ 1) != nr_namespaces
) ? ", " : "]\n");
1283 int perf_event__process_comm(struct perf_tool
*tool __maybe_unused
,
1284 union perf_event
*event
,
1285 struct perf_sample
*sample
,
1286 struct machine
*machine
)
1288 return machine__process_comm_event(machine
, event
, sample
);
1291 int perf_event__process_namespaces(struct perf_tool
*tool __maybe_unused
,
1292 union perf_event
*event
,
1293 struct perf_sample
*sample
,
1294 struct machine
*machine
)
1296 return machine__process_namespaces_event(machine
, event
, sample
);
1299 int perf_event__process_lost(struct perf_tool
*tool __maybe_unused
,
1300 union perf_event
*event
,
1301 struct perf_sample
*sample
,
1302 struct machine
*machine
)
1304 return machine__process_lost_event(machine
, event
, sample
);
1307 int perf_event__process_aux(struct perf_tool
*tool __maybe_unused
,
1308 union perf_event
*event
,
1309 struct perf_sample
*sample __maybe_unused
,
1310 struct machine
*machine
)
1312 return machine__process_aux_event(machine
, event
);
1315 int perf_event__process_itrace_start(struct perf_tool
*tool __maybe_unused
,
1316 union perf_event
*event
,
1317 struct perf_sample
*sample __maybe_unused
,
1318 struct machine
*machine
)
1320 return machine__process_itrace_start_event(machine
, event
);
1323 int perf_event__process_lost_samples(struct perf_tool
*tool __maybe_unused
,
1324 union perf_event
*event
,
1325 struct perf_sample
*sample
,
1326 struct machine
*machine
)
1328 return machine__process_lost_samples_event(machine
, event
, sample
);
1331 int perf_event__process_switch(struct perf_tool
*tool __maybe_unused
,
1332 union perf_event
*event
,
1333 struct perf_sample
*sample __maybe_unused
,
1334 struct machine
*machine
)
1336 return machine__process_switch_event(machine
, event
);
1339 int perf_event__process_ksymbol(struct perf_tool
*tool __maybe_unused
,
1340 union perf_event
*event
,
1341 struct perf_sample
*sample __maybe_unused
,
1342 struct machine
*machine
)
1344 return machine__process_ksymbol(machine
, event
, sample
);
1347 int perf_event__process_bpf_event(struct perf_tool
*tool __maybe_unused
,
1348 union perf_event
*event
,
1349 struct perf_sample
*sample __maybe_unused
,
1350 struct machine
*machine
)
1352 return machine__process_bpf_event(machine
, event
, sample
);
1355 size_t perf_event__fprintf_mmap(union perf_event
*event
, FILE *fp
)
1357 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
"]: %c %s\n",
1358 event
->mmap
.pid
, event
->mmap
.tid
, event
->mmap
.start
,
1359 event
->mmap
.len
, event
->mmap
.pgoff
,
1360 (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
) ? 'r' : 'x',
1361 event
->mmap
.filename
);
1364 size_t perf_event__fprintf_mmap2(union perf_event
*event
, FILE *fp
)
1366 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
1367 " %02x:%02x %"PRIu64
" %"PRIu64
"]: %c%c%c%c %s\n",
1368 event
->mmap2
.pid
, event
->mmap2
.tid
, event
->mmap2
.start
,
1369 event
->mmap2
.len
, event
->mmap2
.pgoff
, event
->mmap2
.maj
,
1370 event
->mmap2
.min
, event
->mmap2
.ino
,
1371 event
->mmap2
.ino_generation
,
1372 (event
->mmap2
.prot
& PROT_READ
) ? 'r' : '-',
1373 (event
->mmap2
.prot
& PROT_WRITE
) ? 'w' : '-',
1374 (event
->mmap2
.prot
& PROT_EXEC
) ? 'x' : '-',
1375 (event
->mmap2
.flags
& MAP_SHARED
) ? 's' : 'p',
1376 event
->mmap2
.filename
);
1379 size_t perf_event__fprintf_thread_map(union perf_event
*event
, FILE *fp
)
1381 struct thread_map
*threads
= thread_map__new_event(&event
->thread_map
);
1384 ret
= fprintf(fp
, " nr: ");
1387 ret
+= thread_map__fprintf(threads
, fp
);
1389 ret
+= fprintf(fp
, "failed to get threads from event\n");
1391 thread_map__put(threads
);
1395 size_t perf_event__fprintf_cpu_map(union perf_event
*event
, FILE *fp
)
1397 struct cpu_map
*cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
1400 ret
= fprintf(fp
, ": ");
1403 ret
+= cpu_map__fprintf(cpus
, fp
);
1405 ret
+= fprintf(fp
, "failed to get cpumap from event\n");
1411 int perf_event__process_mmap(struct perf_tool
*tool __maybe_unused
,
1412 union perf_event
*event
,
1413 struct perf_sample
*sample
,
1414 struct machine
*machine
)
1416 return machine__process_mmap_event(machine
, event
, sample
);
1419 int perf_event__process_mmap2(struct perf_tool
*tool __maybe_unused
,
1420 union perf_event
*event
,
1421 struct perf_sample
*sample
,
1422 struct machine
*machine
)
1424 return machine__process_mmap2_event(machine
, event
, sample
);
1427 size_t perf_event__fprintf_task(union perf_event
*event
, FILE *fp
)
1429 return fprintf(fp
, "(%d:%d):(%d:%d)\n",
1430 event
->fork
.pid
, event
->fork
.tid
,
1431 event
->fork
.ppid
, event
->fork
.ptid
);
1434 int perf_event__process_fork(struct perf_tool
*tool __maybe_unused
,
1435 union perf_event
*event
,
1436 struct perf_sample
*sample
,
1437 struct machine
*machine
)
1439 return machine__process_fork_event(machine
, event
, sample
);
1442 int perf_event__process_exit(struct perf_tool
*tool __maybe_unused
,
1443 union perf_event
*event
,
1444 struct perf_sample
*sample
,
1445 struct machine
*machine
)
1447 return machine__process_exit_event(machine
, event
, sample
);
1450 size_t perf_event__fprintf_aux(union perf_event
*event
, FILE *fp
)
1452 return fprintf(fp
, " offset: %#"PRIx64
" size: %#"PRIx64
" flags: %#"PRIx64
" [%s%s%s]\n",
1453 event
->aux
.aux_offset
, event
->aux
.aux_size
,
1455 event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
? "T" : "",
1456 event
->aux
.flags
& PERF_AUX_FLAG_OVERWRITE
? "O" : "",
1457 event
->aux
.flags
& PERF_AUX_FLAG_PARTIAL
? "P" : "");
1460 size_t perf_event__fprintf_itrace_start(union perf_event
*event
, FILE *fp
)
1462 return fprintf(fp
, " pid: %u tid: %u\n",
1463 event
->itrace_start
.pid
, event
->itrace_start
.tid
);
1466 size_t perf_event__fprintf_switch(union perf_event
*event
, FILE *fp
)
1468 bool out
= event
->header
.misc
& PERF_RECORD_MISC_SWITCH_OUT
;
1469 const char *in_out
= !out
? "IN " :
1470 !(event
->header
.misc
& PERF_RECORD_MISC_SWITCH_OUT_PREEMPT
) ?
1471 "OUT " : "OUT preempt";
1473 if (event
->header
.type
== PERF_RECORD_SWITCH
)
1474 return fprintf(fp
, " %s\n", in_out
);
1476 return fprintf(fp
, " %s %s pid/tid: %5u/%-5u\n",
1477 in_out
, out
? "next" : "prev",
1478 event
->context_switch
.next_prev_pid
,
1479 event
->context_switch
.next_prev_tid
);
1482 static size_t perf_event__fprintf_lost(union perf_event
*event
, FILE *fp
)
1484 return fprintf(fp
, " lost %" PRIu64
"\n", event
->lost
.lost
);
1487 size_t perf_event__fprintf_ksymbol(union perf_event
*event
, FILE *fp
)
1489 return fprintf(fp
, " addr %" PRIx64
" len %u type %u flags 0x%x name %s\n",
1490 event
->ksymbol_event
.addr
, event
->ksymbol_event
.len
,
1491 event
->ksymbol_event
.ksym_type
,
1492 event
->ksymbol_event
.flags
, event
->ksymbol_event
.name
);
1495 size_t perf_event__fprintf_bpf_event(union perf_event
*event
, FILE *fp
)
1497 return fprintf(fp
, " type %u, flags %u, id %u\n",
1498 event
->bpf_event
.type
, event
->bpf_event
.flags
,
1499 event
->bpf_event
.id
);
1502 size_t perf_event__fprintf(union perf_event
*event
, FILE *fp
)
1504 size_t ret
= fprintf(fp
, "PERF_RECORD_%s",
1505 perf_event__name(event
->header
.type
));
1507 switch (event
->header
.type
) {
1508 case PERF_RECORD_COMM
:
1509 ret
+= perf_event__fprintf_comm(event
, fp
);
1511 case PERF_RECORD_FORK
:
1512 case PERF_RECORD_EXIT
:
1513 ret
+= perf_event__fprintf_task(event
, fp
);
1515 case PERF_RECORD_MMAP
:
1516 ret
+= perf_event__fprintf_mmap(event
, fp
);
1518 case PERF_RECORD_NAMESPACES
:
1519 ret
+= perf_event__fprintf_namespaces(event
, fp
);
1521 case PERF_RECORD_MMAP2
:
1522 ret
+= perf_event__fprintf_mmap2(event
, fp
);
1524 case PERF_RECORD_AUX
:
1525 ret
+= perf_event__fprintf_aux(event
, fp
);
1527 case PERF_RECORD_ITRACE_START
:
1528 ret
+= perf_event__fprintf_itrace_start(event
, fp
);
1530 case PERF_RECORD_SWITCH
:
1531 case PERF_RECORD_SWITCH_CPU_WIDE
:
1532 ret
+= perf_event__fprintf_switch(event
, fp
);
1534 case PERF_RECORD_LOST
:
1535 ret
+= perf_event__fprintf_lost(event
, fp
);
1537 case PERF_RECORD_KSYMBOL
:
1538 ret
+= perf_event__fprintf_ksymbol(event
, fp
);
1540 case PERF_RECORD_BPF_EVENT
:
1541 ret
+= perf_event__fprintf_bpf_event(event
, fp
);
1544 ret
+= fprintf(fp
, "\n");
1550 int perf_event__process(struct perf_tool
*tool __maybe_unused
,
1551 union perf_event
*event
,
1552 struct perf_sample
*sample
,
1553 struct machine
*machine
)
1555 return machine__process_event(machine
, event
, sample
);
1558 struct map
*thread__find_map(struct thread
*thread
, u8 cpumode
, u64 addr
,
1559 struct addr_location
*al
)
1561 struct map_groups
*mg
= thread
->mg
;
1562 struct machine
*machine
= mg
->machine
;
1563 bool load_map
= false;
1565 al
->machine
= machine
;
1566 al
->thread
= thread
;
1568 al
->cpumode
= cpumode
;
1571 if (machine
== NULL
) {
1576 if (cpumode
== PERF_RECORD_MISC_KERNEL
&& perf_host
) {
1578 mg
= &machine
->kmaps
;
1580 } else if (cpumode
== PERF_RECORD_MISC_USER
&& perf_host
) {
1582 } else if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
1584 mg
= &machine
->kmaps
;
1586 } else if (cpumode
== PERF_RECORD_MISC_GUEST_USER
&& perf_guest
) {
1592 if ((cpumode
== PERF_RECORD_MISC_GUEST_USER
||
1593 cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) &&
1595 al
->filtered
|= (1 << HIST_FILTER__GUEST
);
1596 if ((cpumode
== PERF_RECORD_MISC_USER
||
1597 cpumode
== PERF_RECORD_MISC_KERNEL
) &&
1599 al
->filtered
|= (1 << HIST_FILTER__HOST
);
1604 al
->map
= map_groups__find(mg
, al
->addr
);
1605 if (al
->map
!= NULL
) {
1607 * Kernel maps might be changed when loading symbols so loading
1608 * must be done prior to using kernel maps.
1612 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
1619 * For branch stacks or branch samples, the sample cpumode might not be correct
1620 * because it applies only to the sample 'ip' and not necessary to 'addr' or
1621 * branch stack addresses. If possible, use a fallback to deal with those cases.
1623 struct map
*thread__find_map_fb(struct thread
*thread
, u8 cpumode
, u64 addr
,
1624 struct addr_location
*al
)
1626 struct map
*map
= thread__find_map(thread
, cpumode
, addr
, al
);
1627 struct machine
*machine
= thread
->mg
->machine
;
1628 u8 addr_cpumode
= machine__addr_cpumode(machine
, cpumode
, addr
);
1630 if (map
|| addr_cpumode
== cpumode
)
1633 return thread__find_map(thread
, addr_cpumode
, addr
, al
);
1636 struct symbol
*thread__find_symbol(struct thread
*thread
, u8 cpumode
,
1637 u64 addr
, struct addr_location
*al
)
1640 if (thread__find_map(thread
, cpumode
, addr
, al
))
1641 al
->sym
= map__find_symbol(al
->map
, al
->addr
);
1645 struct symbol
*thread__find_symbol_fb(struct thread
*thread
, u8 cpumode
,
1646 u64 addr
, struct addr_location
*al
)
1649 if (thread__find_map_fb(thread
, cpumode
, addr
, al
))
1650 al
->sym
= map__find_symbol(al
->map
, al
->addr
);
1655 * Callers need to drop the reference to al->thread, obtained in
1656 * machine__findnew_thread()
1658 int machine__resolve(struct machine
*machine
, struct addr_location
*al
,
1659 struct perf_sample
*sample
)
1661 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
1667 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread
), thread
->tid
);
1668 thread__find_map(thread
, sample
->cpumode
, sample
->ip
, al
);
1669 dump_printf(" ...... dso: %s\n",
1670 al
->map
? al
->map
->dso
->long_name
:
1671 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
1673 if (thread__is_filtered(thread
))
1674 al
->filtered
|= (1 << HIST_FILTER__THREAD
);
1677 al
->cpu
= sample
->cpu
;
1682 struct perf_env
*env
= machine
->env
;
1684 if (env
&& env
->cpu
)
1685 al
->socket
= env
->cpu
[al
->cpu
].socket_id
;
1689 struct dso
*dso
= al
->map
->dso
;
1691 if (symbol_conf
.dso_list
&&
1692 (!dso
|| !(strlist__has_entry(symbol_conf
.dso_list
,
1694 (dso
->short_name
!= dso
->long_name
&&
1695 strlist__has_entry(symbol_conf
.dso_list
,
1696 dso
->long_name
))))) {
1697 al
->filtered
|= (1 << HIST_FILTER__DSO
);
1700 al
->sym
= map__find_symbol(al
->map
, al
->addr
);
1703 if (symbol_conf
.sym_list
&&
1704 (!al
->sym
|| !strlist__has_entry(symbol_conf
.sym_list
,
1706 al
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1713 * The preprocess_sample method will return with reference counts for the
1714 * in it, when done using (and perhaps getting ref counts if needing to
1715 * keep a pointer to one of those entries) it must be paired with
1716 * addr_location__put(), so that the refcounts can be decremented.
1718 void addr_location__put(struct addr_location
*al
)
1720 thread__zput(al
->thread
);
1723 bool is_bts_event(struct perf_event_attr
*attr
)
1725 return attr
->type
== PERF_TYPE_HARDWARE
&&
1726 (attr
->config
& PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
1727 attr
->sample_period
== 1;
1730 bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
1732 if (attr
->type
== PERF_TYPE_SOFTWARE
&&
1733 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
||
1734 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
||
1735 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
))
1738 if (is_bts_event(attr
))
1744 void thread__resolve(struct thread
*thread
, struct addr_location
*al
,
1745 struct perf_sample
*sample
)
1747 thread__find_map_fb(thread
, sample
->cpumode
, sample
->addr
, al
);
1749 al
->cpu
= sample
->cpu
;
1753 al
->sym
= map__find_symbol(al
->map
, al
->addr
);