1 #include <linux/types.h>
11 #include "thread_map.h"
12 #include "symbol/kallsyms.h"
14 static const char *perf_event__names
[] = {
16 [PERF_RECORD_MMAP
] = "MMAP",
17 [PERF_RECORD_MMAP2
] = "MMAP2",
18 [PERF_RECORD_LOST
] = "LOST",
19 [PERF_RECORD_COMM
] = "COMM",
20 [PERF_RECORD_EXIT
] = "EXIT",
21 [PERF_RECORD_THROTTLE
] = "THROTTLE",
22 [PERF_RECORD_UNTHROTTLE
] = "UNTHROTTLE",
23 [PERF_RECORD_FORK
] = "FORK",
24 [PERF_RECORD_READ
] = "READ",
25 [PERF_RECORD_SAMPLE
] = "SAMPLE",
26 [PERF_RECORD_AUX
] = "AUX",
27 [PERF_RECORD_ITRACE_START
] = "ITRACE_START",
28 [PERF_RECORD_HEADER_ATTR
] = "ATTR",
29 [PERF_RECORD_HEADER_EVENT_TYPE
] = "EVENT_TYPE",
30 [PERF_RECORD_HEADER_TRACING_DATA
] = "TRACING_DATA",
31 [PERF_RECORD_HEADER_BUILD_ID
] = "BUILD_ID",
32 [PERF_RECORD_FINISHED_ROUND
] = "FINISHED_ROUND",
33 [PERF_RECORD_ID_INDEX
] = "ID_INDEX",
34 [PERF_RECORD_AUXTRACE_INFO
] = "AUXTRACE_INFO",
35 [PERF_RECORD_AUXTRACE
] = "AUXTRACE",
36 [PERF_RECORD_AUXTRACE_ERROR
] = "AUXTRACE_ERROR",
39 const char *perf_event__name(unsigned int id
)
41 if (id
>= ARRAY_SIZE(perf_event__names
))
43 if (!perf_event__names
[id
])
45 return perf_event__names
[id
];
48 static struct perf_sample synth_sample
= {
58 * Assumes that the first 4095 bytes of /proc/pid/stat contains
59 * the comm, tgid and ppid.
61 static int perf_event__get_comm_ids(pid_t pid
, char *comm
, size_t len
,
62 pid_t
*tgid
, pid_t
*ppid
)
64 char filename
[PATH_MAX
];
68 char *nl
, *name
, *tgids
, *ppids
;
73 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
75 fd
= open(filename
, O_RDONLY
);
77 pr_debug("couldn't open %s\n", filename
);
81 n
= read(fd
, bf
, sizeof(bf
) - 1);
84 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
90 name
= strstr(bf
, "Name:");
91 tgids
= strstr(bf
, "Tgid:");
92 ppids
= strstr(bf
, "PPid:");
95 name
+= 5; /* strlen("Name:") */
97 while (*name
&& isspace(*name
))
100 nl
= strchr(name
, '\n');
107 memcpy(comm
, name
, size
);
110 pr_debug("Name: string not found for pid %d\n", pid
);
114 tgids
+= 5; /* strlen("Tgid:") */
117 pr_debug("Tgid: string not found for pid %d\n", pid
);
121 ppids
+= 5; /* strlen("PPid:") */
124 pr_debug("PPid: string not found for pid %d\n", pid
);
130 static int perf_event__prepare_comm(union perf_event
*event
, pid_t pid
,
131 struct machine
*machine
,
132 pid_t
*tgid
, pid_t
*ppid
)
138 memset(&event
->comm
, 0, sizeof(event
->comm
));
140 if (machine__is_host(machine
)) {
141 if (perf_event__get_comm_ids(pid
, event
->comm
.comm
,
142 sizeof(event
->comm
.comm
),
147 *tgid
= machine
->pid
;
153 event
->comm
.pid
= *tgid
;
154 event
->comm
.header
.type
= PERF_RECORD_COMM
;
156 size
= strlen(event
->comm
.comm
) + 1;
157 size
= PERF_ALIGN(size
, sizeof(u64
));
158 memset(event
->comm
.comm
+ size
, 0, machine
->id_hdr_size
);
159 event
->comm
.header
.size
= (sizeof(event
->comm
) -
160 (sizeof(event
->comm
.comm
) - size
) +
161 machine
->id_hdr_size
);
162 event
->comm
.tid
= pid
;
167 static pid_t
perf_event__synthesize_comm(struct perf_tool
*tool
,
168 union perf_event
*event
, pid_t pid
,
169 perf_event__handler_t process
,
170 struct machine
*machine
)
174 if (perf_event__prepare_comm(event
, pid
, machine
, &tgid
, &ppid
) != 0)
177 if (process(tool
, event
, &synth_sample
, machine
) != 0)
183 static int perf_event__synthesize_fork(struct perf_tool
*tool
,
184 union perf_event
*event
,
185 pid_t pid
, pid_t tgid
, pid_t ppid
,
186 perf_event__handler_t process
,
187 struct machine
*machine
)
189 memset(&event
->fork
, 0, sizeof(event
->fork
) + machine
->id_hdr_size
);
192 * for main thread set parent to ppid from status file. For other
193 * threads set parent pid to main thread. ie., assume main thread
194 * spawns all threads in a process
197 event
->fork
.ppid
= ppid
;
198 event
->fork
.ptid
= ppid
;
200 event
->fork
.ppid
= tgid
;
201 event
->fork
.ptid
= tgid
;
203 event
->fork
.pid
= tgid
;
204 event
->fork
.tid
= pid
;
205 event
->fork
.header
.type
= PERF_RECORD_FORK
;
207 event
->fork
.header
.size
= (sizeof(event
->fork
) + machine
->id_hdr_size
);
209 if (process(tool
, event
, &synth_sample
, machine
) != 0)
215 int perf_event__synthesize_mmap_events(struct perf_tool
*tool
,
216 union perf_event
*event
,
217 pid_t pid
, pid_t tgid
,
218 perf_event__handler_t process
,
219 struct machine
*machine
,
222 char filename
[PATH_MAX
];
226 if (machine__is_default_guest(machine
))
229 snprintf(filename
, sizeof(filename
), "%s/proc/%d/maps",
230 machine
->root_dir
, pid
);
232 fp
= fopen(filename
, "r");
235 * We raced with a task exiting - just return:
237 pr_debug("couldn't open %s\n", filename
);
241 event
->header
.type
= PERF_RECORD_MMAP2
;
246 char execname
[PATH_MAX
];
247 char anonstr
[] = "//anon";
252 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
255 /* ensure null termination since stack will be reused. */
256 strcpy(execname
, "");
258 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
259 n
= sscanf(bf
, "%"PRIx64
"-%"PRIx64
" %s %"PRIx64
" %x:%x %u %s\n",
260 &event
->mmap2
.start
, &event
->mmap2
.len
, prot
,
261 &event
->mmap2
.pgoff
, &event
->mmap2
.maj
,
266 * Anon maps don't have the execname.
271 event
->mmap2
.ino
= (u64
)ino
;
274 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
276 if (machine__is_host(machine
))
277 event
->header
.misc
= PERF_RECORD_MISC_USER
;
279 event
->header
.misc
= PERF_RECORD_MISC_GUEST_USER
;
281 /* map protection and flags bits */
282 event
->mmap2
.prot
= 0;
283 event
->mmap2
.flags
= 0;
285 event
->mmap2
.prot
|= PROT_READ
;
287 event
->mmap2
.prot
|= PROT_WRITE
;
289 event
->mmap2
.prot
|= PROT_EXEC
;
292 event
->mmap2
.flags
|= MAP_SHARED
;
294 event
->mmap2
.flags
|= MAP_PRIVATE
;
296 if (prot
[2] != 'x') {
297 if (!mmap_data
|| prot
[0] != 'r')
300 event
->header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
303 if (!strcmp(execname
, ""))
304 strcpy(execname
, anonstr
);
306 size
= strlen(execname
) + 1;
307 memcpy(event
->mmap2
.filename
, execname
, size
);
308 size
= PERF_ALIGN(size
, sizeof(u64
));
309 event
->mmap2
.len
-= event
->mmap
.start
;
310 event
->mmap2
.header
.size
= (sizeof(event
->mmap2
) -
311 (sizeof(event
->mmap2
.filename
) - size
));
312 memset(event
->mmap2
.filename
+ size
, 0, machine
->id_hdr_size
);
313 event
->mmap2
.header
.size
+= machine
->id_hdr_size
;
314 event
->mmap2
.pid
= tgid
;
315 event
->mmap2
.tid
= pid
;
317 if (process(tool
, event
, &synth_sample
, machine
) != 0) {
327 int perf_event__synthesize_modules(struct perf_tool
*tool
,
328 perf_event__handler_t process
,
329 struct machine
*machine
)
333 struct map_groups
*kmaps
= &machine
->kmaps
;
334 struct rb_root
*maps
= &kmaps
->maps
[MAP__FUNCTION
];
335 union perf_event
*event
= zalloc((sizeof(event
->mmap
) +
336 machine
->id_hdr_size
));
338 pr_debug("Not enough memory synthesizing mmap event "
339 "for kernel modules\n");
343 event
->header
.type
= PERF_RECORD_MMAP
;
346 * kernel uses 0 for user space maps, see kernel/perf_event.c
349 if (machine__is_host(machine
))
350 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
352 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
354 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
357 if (pos
->dso
->kernel
)
360 size
= PERF_ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
361 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
362 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
363 (sizeof(event
->mmap
.filename
) - size
));
364 memset(event
->mmap
.filename
+ size
, 0, machine
->id_hdr_size
);
365 event
->mmap
.header
.size
+= machine
->id_hdr_size
;
366 event
->mmap
.start
= pos
->start
;
367 event
->mmap
.len
= pos
->end
- pos
->start
;
368 event
->mmap
.pid
= machine
->pid
;
370 memcpy(event
->mmap
.filename
, pos
->dso
->long_name
,
371 pos
->dso
->long_name_len
+ 1);
372 if (process(tool
, event
, &synth_sample
, machine
) != 0) {
382 static int __event__synthesize_thread(union perf_event
*comm_event
,
383 union perf_event
*mmap_event
,
384 union perf_event
*fork_event
,
386 perf_event__handler_t process
,
387 struct perf_tool
*tool
,
388 struct machine
*machine
, bool mmap_data
)
390 char filename
[PATH_MAX
];
392 struct dirent dirent
, *next
;
396 /* special case: only send one comm event using passed in pid */
398 tgid
= perf_event__synthesize_comm(tool
, comm_event
, pid
,
404 return perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
405 process
, machine
, mmap_data
);
408 if (machine__is_default_guest(machine
))
411 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task",
412 machine
->root_dir
, pid
);
414 tasks
= opendir(filename
);
416 pr_debug("couldn't open %s\n", filename
);
420 while (!readdir_r(tasks
, &dirent
, &next
) && next
) {
424 _pid
= strtol(dirent
.d_name
, &end
, 10);
429 if (perf_event__prepare_comm(comm_event
, _pid
, machine
,
433 if (perf_event__synthesize_fork(tool
, fork_event
, _pid
, tgid
,
434 ppid
, process
, machine
) < 0)
437 * Send the prepared comm event
439 if (process(tool
, comm_event
, &synth_sample
, machine
) != 0)
444 /* process the parent's maps too */
445 rc
= perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
446 process
, machine
, mmap_data
);
456 int perf_event__synthesize_thread_map(struct perf_tool
*tool
,
457 struct thread_map
*threads
,
458 perf_event__handler_t process
,
459 struct machine
*machine
,
462 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
463 int err
= -1, thread
, j
;
465 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
466 if (comm_event
== NULL
)
469 mmap_event
= malloc(sizeof(mmap_event
->mmap
) + machine
->id_hdr_size
);
470 if (mmap_event
== NULL
)
473 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
474 if (fork_event
== NULL
)
478 for (thread
= 0; thread
< threads
->nr
; ++thread
) {
479 if (__event__synthesize_thread(comm_event
, mmap_event
,
481 threads
->map
[thread
], 0,
482 process
, tool
, machine
,
489 * comm.pid is set to thread group id by
490 * perf_event__synthesize_comm
492 if ((int) comm_event
->comm
.pid
!= threads
->map
[thread
]) {
493 bool need_leader
= true;
495 /* is thread group leader in thread_map? */
496 for (j
= 0; j
< threads
->nr
; ++j
) {
497 if ((int) comm_event
->comm
.pid
== threads
->map
[j
]) {
503 /* if not, generate events for it */
505 __event__synthesize_thread(comm_event
, mmap_event
,
507 comm_event
->comm
.pid
, 0,
508 process
, tool
, machine
,
524 int perf_event__synthesize_threads(struct perf_tool
*tool
,
525 perf_event__handler_t process
,
526 struct machine
*machine
, bool mmap_data
)
529 char proc_path
[PATH_MAX
];
530 struct dirent dirent
, *next
;
531 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
534 if (machine__is_default_guest(machine
))
537 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
538 if (comm_event
== NULL
)
541 mmap_event
= malloc(sizeof(mmap_event
->mmap
) + machine
->id_hdr_size
);
542 if (mmap_event
== NULL
)
545 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
546 if (fork_event
== NULL
)
549 snprintf(proc_path
, sizeof(proc_path
), "%s/proc", machine
->root_dir
);
550 proc
= opendir(proc_path
);
555 while (!readdir_r(proc
, &dirent
, &next
) && next
) {
557 pid_t pid
= strtol(dirent
.d_name
, &end
, 10);
559 if (*end
) /* only interested in proper numerical dirents */
562 * We may race with exiting thread, so don't stop just because
563 * one thread couldn't be synthesized.
565 __event__synthesize_thread(comm_event
, mmap_event
, fork_event
, pid
,
566 1, process
, tool
, machine
, mmap_data
);
581 struct process_symbol_args
{
586 static int find_symbol_cb(void *arg
, const char *name
, char type
,
589 struct process_symbol_args
*args
= arg
;
592 * Must be a function or at least an alias, as in PARISC64, where "_text" is
593 * an 'A' to the same address as "_stext".
595 if (!(symbol_type__is_a(type
, MAP__FUNCTION
) ||
596 type
== 'A') || strcmp(name
, args
->name
))
603 u64
kallsyms__get_function_start(const char *kallsyms_filename
,
604 const char *symbol_name
)
606 struct process_symbol_args args
= { .name
= symbol_name
, };
608 if (kallsyms__parse(kallsyms_filename
, &args
, find_symbol_cb
) <= 0)
614 int perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
615 perf_event__handler_t process
,
616 struct machine
*machine
)
619 const char *mmap_name
;
620 char name_buff
[PATH_MAX
];
624 union perf_event
*event
;
626 if (machine
->vmlinux_maps
[0] == NULL
)
630 * We should get this from /sys/kernel/sections/.text, but till that is
631 * available use this, and after it is use this as a fallback for older
634 event
= zalloc((sizeof(event
->mmap
) + machine
->id_hdr_size
));
636 pr_debug("Not enough memory synthesizing mmap event "
637 "for kernel modules\n");
641 mmap_name
= machine__mmap_name(machine
, name_buff
, sizeof(name_buff
));
642 if (machine__is_host(machine
)) {
644 * kernel uses PERF_RECORD_MISC_USER for user space maps,
645 * see kernel/perf_event.c __perf_event_mmap
647 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
649 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
652 map
= machine
->vmlinux_maps
[MAP__FUNCTION
];
653 kmap
= map__kmap(map
);
654 size
= snprintf(event
->mmap
.filename
, sizeof(event
->mmap
.filename
),
655 "%s%s", mmap_name
, kmap
->ref_reloc_sym
->name
) + 1;
656 size
= PERF_ALIGN(size
, sizeof(u64
));
657 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
658 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
659 (sizeof(event
->mmap
.filename
) - size
) + machine
->id_hdr_size
);
660 event
->mmap
.pgoff
= kmap
->ref_reloc_sym
->addr
;
661 event
->mmap
.start
= map
->start
;
662 event
->mmap
.len
= map
->end
- event
->mmap
.start
;
663 event
->mmap
.pid
= machine
->pid
;
665 err
= process(tool
, event
, &synth_sample
, machine
);
671 size_t perf_event__fprintf_comm(union perf_event
*event
, FILE *fp
)
675 if (event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
)
680 return fprintf(fp
, "%s: %s:%d/%d\n", s
, event
->comm
.comm
, event
->comm
.pid
, event
->comm
.tid
);
683 int perf_event__process_comm(struct perf_tool
*tool __maybe_unused
,
684 union perf_event
*event
,
685 struct perf_sample
*sample
,
686 struct machine
*machine
)
688 return machine__process_comm_event(machine
, event
, sample
);
691 int perf_event__process_lost(struct perf_tool
*tool __maybe_unused
,
692 union perf_event
*event
,
693 struct perf_sample
*sample
,
694 struct machine
*machine
)
696 return machine__process_lost_event(machine
, event
, sample
);
699 int perf_event__process_aux(struct perf_tool
*tool __maybe_unused
,
700 union perf_event
*event
,
701 struct perf_sample
*sample __maybe_unused
,
702 struct machine
*machine
)
704 return machine__process_aux_event(machine
, event
);
707 int perf_event__process_itrace_start(struct perf_tool
*tool __maybe_unused
,
708 union perf_event
*event
,
709 struct perf_sample
*sample __maybe_unused
,
710 struct machine
*machine
)
712 return machine__process_itrace_start_event(machine
, event
);
715 size_t perf_event__fprintf_mmap(union perf_event
*event
, FILE *fp
)
717 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
"]: %c %s\n",
718 event
->mmap
.pid
, event
->mmap
.tid
, event
->mmap
.start
,
719 event
->mmap
.len
, event
->mmap
.pgoff
,
720 (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
) ? 'r' : 'x',
721 event
->mmap
.filename
);
724 size_t perf_event__fprintf_mmap2(union perf_event
*event
, FILE *fp
)
726 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
727 " %02x:%02x %"PRIu64
" %"PRIu64
"]: %c%c%c%c %s\n",
728 event
->mmap2
.pid
, event
->mmap2
.tid
, event
->mmap2
.start
,
729 event
->mmap2
.len
, event
->mmap2
.pgoff
, event
->mmap2
.maj
,
730 event
->mmap2
.min
, event
->mmap2
.ino
,
731 event
->mmap2
.ino_generation
,
732 (event
->mmap2
.prot
& PROT_READ
) ? 'r' : '-',
733 (event
->mmap2
.prot
& PROT_WRITE
) ? 'w' : '-',
734 (event
->mmap2
.prot
& PROT_EXEC
) ? 'x' : '-',
735 (event
->mmap2
.flags
& MAP_SHARED
) ? 's' : 'p',
736 event
->mmap2
.filename
);
739 int perf_event__process_mmap(struct perf_tool
*tool __maybe_unused
,
740 union perf_event
*event
,
741 struct perf_sample
*sample
,
742 struct machine
*machine
)
744 return machine__process_mmap_event(machine
, event
, sample
);
747 int perf_event__process_mmap2(struct perf_tool
*tool __maybe_unused
,
748 union perf_event
*event
,
749 struct perf_sample
*sample
,
750 struct machine
*machine
)
752 return machine__process_mmap2_event(machine
, event
, sample
);
755 size_t perf_event__fprintf_task(union perf_event
*event
, FILE *fp
)
757 return fprintf(fp
, "(%d:%d):(%d:%d)\n",
758 event
->fork
.pid
, event
->fork
.tid
,
759 event
->fork
.ppid
, event
->fork
.ptid
);
762 int perf_event__process_fork(struct perf_tool
*tool __maybe_unused
,
763 union perf_event
*event
,
764 struct perf_sample
*sample
,
765 struct machine
*machine
)
767 return machine__process_fork_event(machine
, event
, sample
);
770 int perf_event__process_exit(struct perf_tool
*tool __maybe_unused
,
771 union perf_event
*event
,
772 struct perf_sample
*sample
,
773 struct machine
*machine
)
775 return machine__process_exit_event(machine
, event
, sample
);
778 size_t perf_event__fprintf_aux(union perf_event
*event
, FILE *fp
)
780 return fprintf(fp
, " offset: %#"PRIx64
" size: %#"PRIx64
" flags: %#"PRIx64
" [%s%s]\n",
781 event
->aux
.aux_offset
, event
->aux
.aux_size
,
783 event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
? "T" : "",
784 event
->aux
.flags
& PERF_AUX_FLAG_OVERWRITE
? "O" : "");
787 size_t perf_event__fprintf_itrace_start(union perf_event
*event
, FILE *fp
)
789 return fprintf(fp
, " pid: %u tid: %u\n",
790 event
->itrace_start
.pid
, event
->itrace_start
.tid
);
793 size_t perf_event__fprintf(union perf_event
*event
, FILE *fp
)
795 size_t ret
= fprintf(fp
, "PERF_RECORD_%s",
796 perf_event__name(event
->header
.type
));
798 switch (event
->header
.type
) {
799 case PERF_RECORD_COMM
:
800 ret
+= perf_event__fprintf_comm(event
, fp
);
802 case PERF_RECORD_FORK
:
803 case PERF_RECORD_EXIT
:
804 ret
+= perf_event__fprintf_task(event
, fp
);
806 case PERF_RECORD_MMAP
:
807 ret
+= perf_event__fprintf_mmap(event
, fp
);
809 case PERF_RECORD_MMAP2
:
810 ret
+= perf_event__fprintf_mmap2(event
, fp
);
812 case PERF_RECORD_AUX
:
813 ret
+= perf_event__fprintf_aux(event
, fp
);
815 case PERF_RECORD_ITRACE_START
:
816 ret
+= perf_event__fprintf_itrace_start(event
, fp
);
819 ret
+= fprintf(fp
, "\n");
825 int perf_event__process(struct perf_tool
*tool __maybe_unused
,
826 union perf_event
*event
,
827 struct perf_sample
*sample
,
828 struct machine
*machine
)
830 return machine__process_event(machine
, event
, sample
);
833 void thread__find_addr_map(struct thread
*thread
, u8 cpumode
,
834 enum map_type type
, u64 addr
,
835 struct addr_location
*al
)
837 struct map_groups
*mg
= thread
->mg
;
838 struct machine
*machine
= mg
->machine
;
839 bool load_map
= false;
841 al
->machine
= machine
;
844 al
->cpumode
= cpumode
;
847 if (machine
== NULL
) {
852 if (cpumode
== PERF_RECORD_MISC_KERNEL
&& perf_host
) {
854 mg
= &machine
->kmaps
;
856 } else if (cpumode
== PERF_RECORD_MISC_USER
&& perf_host
) {
858 } else if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
860 mg
= &machine
->kmaps
;
862 } else if (cpumode
== PERF_RECORD_MISC_GUEST_USER
&& perf_guest
) {
868 if ((cpumode
== PERF_RECORD_MISC_GUEST_USER
||
869 cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) &&
871 al
->filtered
|= (1 << HIST_FILTER__GUEST
);
872 if ((cpumode
== PERF_RECORD_MISC_USER
||
873 cpumode
== PERF_RECORD_MISC_KERNEL
) &&
875 al
->filtered
|= (1 << HIST_FILTER__HOST
);
880 al
->map
= map_groups__find(mg
, type
, al
->addr
);
881 if (al
->map
== NULL
) {
883 * If this is outside of all known maps, and is a negative
884 * address, try to look it up in the kernel dso, as it might be
885 * a vsyscall or vdso (which executes in user-mode).
887 * XXX This is nasty, we should have a symbol list in the
888 * "[vdso]" dso, but for now lets use the old trick of looking
889 * in the whole kernel symbol list.
891 if (cpumode
== PERF_RECORD_MISC_USER
&& machine
&&
892 mg
!= &machine
->kmaps
&&
893 machine__kernel_ip(machine
, al
->addr
)) {
894 mg
= &machine
->kmaps
;
900 * Kernel maps might be changed when loading symbols so loading
901 * must be done prior to using kernel maps.
904 map__load(al
->map
, machine
->symbol_filter
);
905 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
909 void thread__find_addr_location(struct thread
*thread
,
910 u8 cpumode
, enum map_type type
, u64 addr
,
911 struct addr_location
*al
)
913 thread__find_addr_map(thread
, cpumode
, type
, addr
, al
);
915 al
->sym
= map__find_symbol(al
->map
, al
->addr
,
916 thread
->mg
->machine
->symbol_filter
);
922 * Callers need to drop the reference to al->thread, obtained in
923 * machine__findnew_thread()
925 int perf_event__preprocess_sample(const union perf_event
*event
,
926 struct machine
*machine
,
927 struct addr_location
*al
,
928 struct perf_sample
*sample
)
930 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
931 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
937 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread
), thread
->tid
);
939 * Have we already created the kernel maps for this machine?
941 * This should have happened earlier, when we processed the kernel MMAP
942 * events, but for older perf.data files there was no such thing, so do
945 if (cpumode
== PERF_RECORD_MISC_KERNEL
&&
946 machine
->vmlinux_maps
[MAP__FUNCTION
] == NULL
)
947 machine__create_kernel_maps(machine
);
949 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, sample
->ip
, al
);
950 dump_printf(" ...... dso: %s\n",
951 al
->map
? al
->map
->dso
->long_name
:
952 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
954 if (thread__is_filtered(thread
))
955 al
->filtered
|= (1 << HIST_FILTER__THREAD
);
958 al
->cpu
= sample
->cpu
;
961 struct dso
*dso
= al
->map
->dso
;
963 if (symbol_conf
.dso_list
&&
964 (!dso
|| !(strlist__has_entry(symbol_conf
.dso_list
,
966 (dso
->short_name
!= dso
->long_name
&&
967 strlist__has_entry(symbol_conf
.dso_list
,
968 dso
->long_name
))))) {
969 al
->filtered
|= (1 << HIST_FILTER__DSO
);
972 al
->sym
= map__find_symbol(al
->map
, al
->addr
,
973 machine
->symbol_filter
);
976 if (symbol_conf
.sym_list
&&
977 (!al
->sym
|| !strlist__has_entry(symbol_conf
.sym_list
,
979 al
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
986 * The preprocess_sample method will return with reference counts for the
987 * in it, when done using (and perhaps getting ref counts if needing to
988 * keep a pointer to one of those entries) it must be paired with
989 * addr_location__put(), so that the refcounts can be decremented.
991 void addr_location__put(struct addr_location
*al
)
993 thread__zput(al
->thread
);
996 bool is_bts_event(struct perf_event_attr
*attr
)
998 return attr
->type
== PERF_TYPE_HARDWARE
&&
999 (attr
->config
& PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
1000 attr
->sample_period
== 1;
1003 bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
1005 if (attr
->type
== PERF_TYPE_SOFTWARE
&&
1006 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
||
1007 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
||
1008 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
))
1011 if (is_bts_event(attr
))
1017 void perf_event__preprocess_sample_addr(union perf_event
*event
,
1018 struct perf_sample
*sample
,
1019 struct thread
*thread
,
1020 struct addr_location
*al
)
1022 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1024 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, sample
->addr
, al
);
1026 thread__find_addr_map(thread
, cpumode
, MAP__VARIABLE
,
1029 al
->cpu
= sample
->cpu
;
1033 al
->sym
= map__find_symbol(al
->map
, al
->addr
, NULL
);