1 // SPDX-License-Identifier: GPL-2.0
19 #include <sys/types.h>
23 #include "linux/hash.h"
25 #include "bpf-event.h"
27 #include "sane_ctype.h"
28 #include <symbol/kallsyms.h>
29 #include <linux/mman.h>
31 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
);
33 static void dsos__init(struct dsos
*dsos
)
35 INIT_LIST_HEAD(&dsos
->head
);
37 init_rwsem(&dsos
->lock
);
40 static void machine__threads_init(struct machine
*machine
)
44 for (i
= 0; i
< THREADS__TABLE_SIZE
; i
++) {
45 struct threads
*threads
= &machine
->threads
[i
];
46 threads
->entries
= RB_ROOT_CACHED
;
47 init_rwsem(&threads
->lock
);
49 INIT_LIST_HEAD(&threads
->dead
);
50 threads
->last_match
= NULL
;
54 static int machine__set_mmap_name(struct machine
*machine
)
56 if (machine__is_host(machine
))
57 machine
->mmap_name
= strdup("[kernel.kallsyms]");
58 else if (machine__is_default_guest(machine
))
59 machine
->mmap_name
= strdup("[guest.kernel.kallsyms]");
60 else if (asprintf(&machine
->mmap_name
, "[guest.kernel.kallsyms.%d]",
62 machine
->mmap_name
= NULL
;
64 return machine
->mmap_name
? 0 : -ENOMEM
;
67 int machine__init(struct machine
*machine
, const char *root_dir
, pid_t pid
)
71 memset(machine
, 0, sizeof(*machine
));
72 map_groups__init(&machine
->kmaps
, machine
);
73 RB_CLEAR_NODE(&machine
->rb_node
);
74 dsos__init(&machine
->dsos
);
76 machine__threads_init(machine
);
78 machine
->vdso_info
= NULL
;
83 machine
->id_hdr_size
= 0;
84 machine
->kptr_restrict_warned
= false;
85 machine
->comm_exec
= false;
86 machine
->kernel_start
= 0;
87 machine
->vmlinux_map
= NULL
;
89 machine
->root_dir
= strdup(root_dir
);
90 if (machine
->root_dir
== NULL
)
93 if (machine__set_mmap_name(machine
))
96 if (pid
!= HOST_KERNEL_ID
) {
97 struct thread
*thread
= machine__findnew_thread(machine
, -1,
104 snprintf(comm
, sizeof(comm
), "[guest/%d]", pid
);
105 thread__set_comm(thread
, comm
, 0);
109 machine
->current_tid
= NULL
;
114 zfree(&machine
->root_dir
);
115 zfree(&machine
->mmap_name
);
120 struct machine
*machine__new_host(void)
122 struct machine
*machine
= malloc(sizeof(*machine
));
124 if (machine
!= NULL
) {
125 machine__init(machine
, "", HOST_KERNEL_ID
);
127 if (machine__create_kernel_maps(machine
) < 0)
137 struct machine
*machine__new_kallsyms(void)
139 struct machine
*machine
= machine__new_host();
142 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
143 * ask for not using the kcore parsing code, once this one is fixed
144 * to create a map per module.
146 if (machine
&& machine__load_kallsyms(machine
, "/proc/kallsyms") <= 0) {
147 machine__delete(machine
);
154 static void dsos__purge(struct dsos
*dsos
)
158 down_write(&dsos
->lock
);
160 list_for_each_entry_safe(pos
, n
, &dsos
->head
, node
) {
161 RB_CLEAR_NODE(&pos
->rb_node
);
163 list_del_init(&pos
->node
);
167 up_write(&dsos
->lock
);
170 static void dsos__exit(struct dsos
*dsos
)
173 exit_rwsem(&dsos
->lock
);
176 void machine__delete_threads(struct machine
*machine
)
181 for (i
= 0; i
< THREADS__TABLE_SIZE
; i
++) {
182 struct threads
*threads
= &machine
->threads
[i
];
183 down_write(&threads
->lock
);
184 nd
= rb_first_cached(&threads
->entries
);
186 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
189 __machine__remove_thread(machine
, t
, false);
191 up_write(&threads
->lock
);
195 void machine__exit(struct machine
*machine
)
202 machine__destroy_kernel_maps(machine
);
203 map_groups__exit(&machine
->kmaps
);
204 dsos__exit(&machine
->dsos
);
205 machine__exit_vdso(machine
);
206 zfree(&machine
->root_dir
);
207 zfree(&machine
->mmap_name
);
208 zfree(&machine
->current_tid
);
210 for (i
= 0; i
< THREADS__TABLE_SIZE
; i
++) {
211 struct threads
*threads
= &machine
->threads
[i
];
212 exit_rwsem(&threads
->lock
);
216 void machine__delete(struct machine
*machine
)
219 machine__exit(machine
);
224 void machines__init(struct machines
*machines
)
226 machine__init(&machines
->host
, "", HOST_KERNEL_ID
);
227 machines
->guests
= RB_ROOT_CACHED
;
230 void machines__exit(struct machines
*machines
)
232 machine__exit(&machines
->host
);
236 struct machine
*machines__add(struct machines
*machines
, pid_t pid
,
237 const char *root_dir
)
239 struct rb_node
**p
= &machines
->guests
.rb_root
.rb_node
;
240 struct rb_node
*parent
= NULL
;
241 struct machine
*pos
, *machine
= malloc(sizeof(*machine
));
242 bool leftmost
= true;
247 if (machine__init(machine
, root_dir
, pid
) != 0) {
254 pos
= rb_entry(parent
, struct machine
, rb_node
);
263 rb_link_node(&machine
->rb_node
, parent
, p
);
264 rb_insert_color_cached(&machine
->rb_node
, &machines
->guests
, leftmost
);
269 void machines__set_comm_exec(struct machines
*machines
, bool comm_exec
)
273 machines
->host
.comm_exec
= comm_exec
;
275 for (nd
= rb_first_cached(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
276 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
278 machine
->comm_exec
= comm_exec
;
282 struct machine
*machines__find(struct machines
*machines
, pid_t pid
)
284 struct rb_node
**p
= &machines
->guests
.rb_root
.rb_node
;
285 struct rb_node
*parent
= NULL
;
286 struct machine
*machine
;
287 struct machine
*default_machine
= NULL
;
289 if (pid
== HOST_KERNEL_ID
)
290 return &machines
->host
;
294 machine
= rb_entry(parent
, struct machine
, rb_node
);
295 if (pid
< machine
->pid
)
297 else if (pid
> machine
->pid
)
302 default_machine
= machine
;
305 return default_machine
;
308 struct machine
*machines__findnew(struct machines
*machines
, pid_t pid
)
311 const char *root_dir
= "";
312 struct machine
*machine
= machines__find(machines
, pid
);
314 if (machine
&& (machine
->pid
== pid
))
317 if ((pid
!= HOST_KERNEL_ID
) &&
318 (pid
!= DEFAULT_GUEST_KERNEL_ID
) &&
319 (symbol_conf
.guestmount
)) {
320 sprintf(path
, "%s/%d", symbol_conf
.guestmount
, pid
);
321 if (access(path
, R_OK
)) {
322 static struct strlist
*seen
;
325 seen
= strlist__new(NULL
, NULL
);
327 if (!strlist__has_entry(seen
, path
)) {
328 pr_err("Can't access file %s\n", path
);
329 strlist__add(seen
, path
);
337 machine
= machines__add(machines
, pid
, root_dir
);
342 void machines__process_guests(struct machines
*machines
,
343 machine__process_t process
, void *data
)
347 for (nd
= rb_first_cached(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
348 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
353 void machines__set_id_hdr_size(struct machines
*machines
, u16 id_hdr_size
)
355 struct rb_node
*node
;
356 struct machine
*machine
;
358 machines
->host
.id_hdr_size
= id_hdr_size
;
360 for (node
= rb_first_cached(&machines
->guests
); node
;
361 node
= rb_next(node
)) {
362 machine
= rb_entry(node
, struct machine
, rb_node
);
363 machine
->id_hdr_size
= id_hdr_size
;
369 static void machine__update_thread_pid(struct machine
*machine
,
370 struct thread
*th
, pid_t pid
)
372 struct thread
*leader
;
374 if (pid
== th
->pid_
|| pid
== -1 || th
->pid_
!= -1)
379 if (th
->pid_
== th
->tid
)
382 leader
= __machine__findnew_thread(machine
, th
->pid_
, th
->pid_
);
387 leader
->mg
= map_groups__new(machine
);
392 if (th
->mg
== leader
->mg
)
397 * Maps are created from MMAP events which provide the pid and
398 * tid. Consequently there never should be any maps on a thread
399 * with an unknown pid. Just print an error if there are.
401 if (!map_groups__empty(th
->mg
))
402 pr_err("Discarding thread maps for %d:%d\n",
404 map_groups__put(th
->mg
);
407 th
->mg
= map_groups__get(leader
->mg
);
412 pr_err("Failed to join map groups for %d:%d\n", th
->pid_
, th
->tid
);
417 * Front-end cache - TID lookups come in blocks,
418 * so most of the time we dont have to look up
421 static struct thread
*
422 __threads__get_last_match(struct threads
*threads
, struct machine
*machine
,
427 th
= threads
->last_match
;
429 if (th
->tid
== tid
) {
430 machine__update_thread_pid(machine
, th
, pid
);
431 return thread__get(th
);
434 threads
->last_match
= NULL
;
440 static struct thread
*
441 threads__get_last_match(struct threads
*threads
, struct machine
*machine
,
444 struct thread
*th
= NULL
;
446 if (perf_singlethreaded
)
447 th
= __threads__get_last_match(threads
, machine
, pid
, tid
);
453 __threads__set_last_match(struct threads
*threads
, struct thread
*th
)
455 threads
->last_match
= th
;
459 threads__set_last_match(struct threads
*threads
, struct thread
*th
)
461 if (perf_singlethreaded
)
462 __threads__set_last_match(threads
, th
);
466 * Caller must eventually drop thread->refcnt returned with a successful
467 * lookup/new thread inserted.
469 static struct thread
*____machine__findnew_thread(struct machine
*machine
,
470 struct threads
*threads
,
471 pid_t pid
, pid_t tid
,
474 struct rb_node
**p
= &threads
->entries
.rb_root
.rb_node
;
475 struct rb_node
*parent
= NULL
;
477 bool leftmost
= true;
479 th
= threads__get_last_match(threads
, machine
, pid
, tid
);
485 th
= rb_entry(parent
, struct thread
, rb_node
);
487 if (th
->tid
== tid
) {
488 threads__set_last_match(threads
, th
);
489 machine__update_thread_pid(machine
, th
, pid
);
490 return thread__get(th
);
504 th
= thread__new(pid
, tid
);
506 rb_link_node(&th
->rb_node
, parent
, p
);
507 rb_insert_color_cached(&th
->rb_node
, &threads
->entries
, leftmost
);
510 * We have to initialize map_groups separately
511 * after rb tree is updated.
513 * The reason is that we call machine__findnew_thread
514 * within thread__init_map_groups to find the thread
515 * leader and that would screwed the rb tree.
517 if (thread__init_map_groups(th
, machine
)) {
518 rb_erase_cached(&th
->rb_node
, &threads
->entries
);
519 RB_CLEAR_NODE(&th
->rb_node
);
524 * It is now in the rbtree, get a ref
527 threads__set_last_match(threads
, th
);
534 struct thread
*__machine__findnew_thread(struct machine
*machine
, pid_t pid
, pid_t tid
)
536 return ____machine__findnew_thread(machine
, machine__threads(machine
, tid
), pid
, tid
, true);
539 struct thread
*machine__findnew_thread(struct machine
*machine
, pid_t pid
,
542 struct threads
*threads
= machine__threads(machine
, tid
);
545 down_write(&threads
->lock
);
546 th
= __machine__findnew_thread(machine
, pid
, tid
);
547 up_write(&threads
->lock
);
551 struct thread
*machine__find_thread(struct machine
*machine
, pid_t pid
,
554 struct threads
*threads
= machine__threads(machine
, tid
);
557 down_read(&threads
->lock
);
558 th
= ____machine__findnew_thread(machine
, threads
, pid
, tid
, false);
559 up_read(&threads
->lock
);
563 struct comm
*machine__thread_exec_comm(struct machine
*machine
,
564 struct thread
*thread
)
566 if (machine
->comm_exec
)
567 return thread__exec_comm(thread
);
569 return thread__comm(thread
);
572 int machine__process_comm_event(struct machine
*machine
, union perf_event
*event
,
573 struct perf_sample
*sample
)
575 struct thread
*thread
= machine__findnew_thread(machine
,
578 bool exec
= event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
;
582 machine
->comm_exec
= true;
585 perf_event__fprintf_comm(event
, stdout
);
587 if (thread
== NULL
||
588 __thread__set_comm(thread
, event
->comm
.comm
, sample
->time
, exec
)) {
589 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
598 int machine__process_namespaces_event(struct machine
*machine __maybe_unused
,
599 union perf_event
*event
,
600 struct perf_sample
*sample __maybe_unused
)
602 struct thread
*thread
= machine__findnew_thread(machine
,
603 event
->namespaces
.pid
,
604 event
->namespaces
.tid
);
607 WARN_ONCE(event
->namespaces
.nr_namespaces
> NR_NAMESPACES
,
608 "\nWARNING: kernel seems to support more namespaces than perf"
609 " tool.\nTry updating the perf tool..\n\n");
611 WARN_ONCE(event
->namespaces
.nr_namespaces
< NR_NAMESPACES
,
612 "\nWARNING: perf tool seems to support more namespaces than"
613 " the kernel.\nTry updating the kernel..\n\n");
616 perf_event__fprintf_namespaces(event
, stdout
);
618 if (thread
== NULL
||
619 thread__set_namespaces(thread
, sample
->time
, &event
->namespaces
)) {
620 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
629 int machine__process_lost_event(struct machine
*machine __maybe_unused
,
630 union perf_event
*event
, struct perf_sample
*sample __maybe_unused
)
632 dump_printf(": id:%" PRIu64
": lost:%" PRIu64
"\n",
633 event
->lost
.id
, event
->lost
.lost
);
637 int machine__process_lost_samples_event(struct machine
*machine __maybe_unused
,
638 union perf_event
*event
, struct perf_sample
*sample
)
640 dump_printf(": id:%" PRIu64
": lost samples :%" PRIu64
"\n",
641 sample
->id
, event
->lost_samples
.lost
);
645 static struct dso
*machine__findnew_module_dso(struct machine
*machine
,
647 const char *filename
)
651 down_write(&machine
->dsos
.lock
);
653 dso
= __dsos__find(&machine
->dsos
, m
->name
, true);
655 dso
= __dsos__addnew(&machine
->dsos
, m
->name
);
659 dso__set_module_info(dso
, m
, machine
);
660 dso__set_long_name(dso
, strdup(filename
), true);
665 up_write(&machine
->dsos
.lock
);
669 int machine__process_aux_event(struct machine
*machine __maybe_unused
,
670 union perf_event
*event
)
673 perf_event__fprintf_aux(event
, stdout
);
677 int machine__process_itrace_start_event(struct machine
*machine __maybe_unused
,
678 union perf_event
*event
)
681 perf_event__fprintf_itrace_start(event
, stdout
);
685 int machine__process_switch_event(struct machine
*machine __maybe_unused
,
686 union perf_event
*event
)
689 perf_event__fprintf_switch(event
, stdout
);
693 static int machine__process_ksymbol_register(struct machine
*machine
,
694 union perf_event
*event
,
695 struct perf_sample
*sample __maybe_unused
)
700 map
= map_groups__find(&machine
->kmaps
, event
->ksymbol_event
.addr
);
702 map
= dso__new_map(event
->ksymbol_event
.name
);
706 map
->start
= event
->ksymbol_event
.addr
;
707 map
->pgoff
= map
->start
;
708 map
->end
= map
->start
+ event
->ksymbol_event
.len
;
709 map_groups__insert(&machine
->kmaps
, map
);
712 sym
= symbol__new(event
->ksymbol_event
.addr
, event
->ksymbol_event
.len
,
713 0, 0, event
->ksymbol_event
.name
);
716 dso__insert_symbol(map
->dso
, sym
);
720 static int machine__process_ksymbol_unregister(struct machine
*machine
,
721 union perf_event
*event
,
722 struct perf_sample
*sample __maybe_unused
)
726 map
= map_groups__find(&machine
->kmaps
, event
->ksymbol_event
.addr
);
728 map_groups__remove(&machine
->kmaps
, map
);
733 int machine__process_ksymbol(struct machine
*machine __maybe_unused
,
734 union perf_event
*event
,
735 struct perf_sample
*sample
)
738 perf_event__fprintf_ksymbol(event
, stdout
);
740 if (event
->ksymbol_event
.flags
& PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER
)
741 return machine__process_ksymbol_unregister(machine
, event
,
743 return machine__process_ksymbol_register(machine
, event
, sample
);
746 static void dso__adjust_kmod_long_name(struct dso
*dso
, const char *filename
)
748 const char *dup_filename
;
750 if (!filename
|| !dso
|| !dso
->long_name
)
752 if (dso
->long_name
[0] != '[')
754 if (!strchr(filename
, '/'))
757 dup_filename
= strdup(filename
);
761 dso__set_long_name(dso
, dup_filename
, true);
764 struct map
*machine__findnew_module_map(struct machine
*machine
, u64 start
,
765 const char *filename
)
767 struct map
*map
= NULL
;
768 struct dso
*dso
= NULL
;
771 if (kmod_path__parse_name(&m
, filename
))
774 map
= map_groups__find_by_name(&machine
->kmaps
, m
.name
);
777 * If the map's dso is an offline module, give dso__load()
778 * a chance to find the file path of that module by fixing
781 dso__adjust_kmod_long_name(map
->dso
, filename
);
785 dso
= machine__findnew_module_dso(machine
, &m
, filename
);
789 map
= map__new2(start
, dso
);
793 map_groups__insert(&machine
->kmaps
, map
);
795 /* Put the map here because map_groups__insert alread got it */
798 /* put the dso here, corresponding to machine__findnew_module_dso */
804 size_t machines__fprintf_dsos(struct machines
*machines
, FILE *fp
)
807 size_t ret
= __dsos__fprintf(&machines
->host
.dsos
.head
, fp
);
809 for (nd
= rb_first_cached(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
810 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
811 ret
+= __dsos__fprintf(&pos
->dsos
.head
, fp
);
817 size_t machine__fprintf_dsos_buildid(struct machine
*m
, FILE *fp
,
818 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
820 return __dsos__fprintf_buildid(&m
->dsos
.head
, fp
, skip
, parm
);
823 size_t machines__fprintf_dsos_buildid(struct machines
*machines
, FILE *fp
,
824 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
827 size_t ret
= machine__fprintf_dsos_buildid(&machines
->host
, fp
, skip
, parm
);
829 for (nd
= rb_first_cached(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
830 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
831 ret
+= machine__fprintf_dsos_buildid(pos
, fp
, skip
, parm
);
836 size_t machine__fprintf_vmlinux_path(struct machine
*machine
, FILE *fp
)
840 struct dso
*kdso
= machine__kernel_map(machine
)->dso
;
842 if (kdso
->has_build_id
) {
843 char filename
[PATH_MAX
];
844 if (dso__build_id_filename(kdso
, filename
, sizeof(filename
),
846 printed
+= fprintf(fp
, "[0] %s\n", filename
);
849 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
)
850 printed
+= fprintf(fp
, "[%d] %s\n",
851 i
+ kdso
->has_build_id
, vmlinux_path
[i
]);
856 size_t machine__fprintf(struct machine
*machine
, FILE *fp
)
862 for (i
= 0; i
< THREADS__TABLE_SIZE
; i
++) {
863 struct threads
*threads
= &machine
->threads
[i
];
865 down_read(&threads
->lock
);
867 ret
= fprintf(fp
, "Threads: %u\n", threads
->nr
);
869 for (nd
= rb_first_cached(&threads
->entries
); nd
;
871 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
873 ret
+= thread__fprintf(pos
, fp
);
876 up_read(&threads
->lock
);
881 static struct dso
*machine__get_kernel(struct machine
*machine
)
883 const char *vmlinux_name
= machine
->mmap_name
;
886 if (machine__is_host(machine
)) {
887 if (symbol_conf
.vmlinux_name
)
888 vmlinux_name
= symbol_conf
.vmlinux_name
;
890 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
891 "[kernel]", DSO_TYPE_KERNEL
);
893 if (symbol_conf
.default_guest_vmlinux_name
)
894 vmlinux_name
= symbol_conf
.default_guest_vmlinux_name
;
896 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
898 DSO_TYPE_GUEST_KERNEL
);
901 if (kernel
!= NULL
&& (!kernel
->has_build_id
))
902 dso__read_running_kernel_build_id(kernel
, machine
);
907 struct process_args
{
911 void machine__get_kallsyms_filename(struct machine
*machine
, char *buf
,
914 if (machine__is_default_guest(machine
))
915 scnprintf(buf
, bufsz
, "%s", symbol_conf
.default_guest_kallsyms
);
917 scnprintf(buf
, bufsz
, "%s/proc/kallsyms", machine
->root_dir
);
920 const char *ref_reloc_sym_names
[] = {"_text", "_stext", NULL
};
922 /* Figure out the start address of kernel map from /proc/kallsyms.
923 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
924 * symbol_name if it's not that important.
926 static int machine__get_running_kernel_start(struct machine
*machine
,
927 const char **symbol_name
, u64
*start
)
929 char filename
[PATH_MAX
];
934 machine__get_kallsyms_filename(machine
, filename
, PATH_MAX
);
936 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
939 for (i
= 0; (name
= ref_reloc_sym_names
[i
]) != NULL
; i
++) {
940 err
= kallsyms__get_function_start(filename
, name
, &addr
);
955 int machine__create_extra_kernel_map(struct machine
*machine
,
957 struct extra_kernel_map
*xm
)
962 map
= map__new2(xm
->start
, kernel
);
967 map
->pgoff
= xm
->pgoff
;
969 kmap
= map__kmap(map
);
971 kmap
->kmaps
= &machine
->kmaps
;
972 strlcpy(kmap
->name
, xm
->name
, KMAP_NAME_LEN
);
974 map_groups__insert(&machine
->kmaps
, map
);
976 pr_debug2("Added extra kernel map %s %" PRIx64
"-%" PRIx64
"\n",
977 kmap
->name
, map
->start
, map
->end
);
984 static u64
find_entry_trampoline(struct dso
*dso
)
986 /* Duplicates are removed so lookup all aliases */
987 const char *syms
[] = {
989 "__entry_trampoline_start",
990 "entry_SYSCALL_64_trampoline",
992 struct symbol
*sym
= dso__first_symbol(dso
);
995 for (; sym
; sym
= dso__next_symbol(sym
)) {
996 if (sym
->binding
!= STB_GLOBAL
)
998 for (i
= 0; i
< ARRAY_SIZE(syms
); i
++) {
999 if (!strcmp(sym
->name
, syms
[i
]))
1008 * These values can be used for kernels that do not have symbols for the entry
1009 * trampolines in kallsyms.
1011 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1012 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1013 #define X86_64_ENTRY_TRAMPOLINE 0x6000
1015 /* Map x86_64 PTI entry trampolines */
1016 int machine__map_x86_64_entry_trampolines(struct machine
*machine
,
1019 struct map_groups
*kmaps
= &machine
->kmaps
;
1020 struct maps
*maps
= &kmaps
->maps
;
1021 int nr_cpus_avail
, cpu
;
1027 * In the vmlinux case, pgoff is a virtual address which must now be
1028 * mapped to a vmlinux offset.
1030 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
1031 struct kmap
*kmap
= __map__kmap(map
);
1032 struct map
*dest_map
;
1034 if (!kmap
|| !is_entry_trampoline(kmap
->name
))
1037 dest_map
= map_groups__find(kmaps
, map
->pgoff
);
1038 if (dest_map
!= map
)
1039 map
->pgoff
= dest_map
->map_ip(dest_map
, map
->pgoff
);
1042 if (found
|| machine
->trampolines_mapped
)
1045 pgoff
= find_entry_trampoline(kernel
);
1049 nr_cpus_avail
= machine__nr_cpus_avail(machine
);
1051 /* Add a 1 page map for each CPU's entry trampoline */
1052 for (cpu
= 0; cpu
< nr_cpus_avail
; cpu
++) {
1053 u64 va
= X86_64_CPU_ENTRY_AREA_PER_CPU
+
1054 cpu
* X86_64_CPU_ENTRY_AREA_SIZE
+
1055 X86_64_ENTRY_TRAMPOLINE
;
1056 struct extra_kernel_map xm
= {
1058 .end
= va
+ page_size
,
1062 strlcpy(xm
.name
, ENTRY_TRAMPOLINE_NAME
, KMAP_NAME_LEN
);
1064 if (machine__create_extra_kernel_map(machine
, kernel
, &xm
) < 0)
1068 machine
->trampolines_mapped
= nr_cpus_avail
;
1073 int __weak
machine__create_extra_kernel_maps(struct machine
*machine __maybe_unused
,
1074 struct dso
*kernel __maybe_unused
)
1080 __machine__create_kernel_maps(struct machine
*machine
, struct dso
*kernel
)
1085 /* In case of renewal the kernel map, destroy previous one */
1086 machine__destroy_kernel_maps(machine
);
1088 machine
->vmlinux_map
= map__new2(0, kernel
);
1089 if (machine
->vmlinux_map
== NULL
)
1092 machine
->vmlinux_map
->map_ip
= machine
->vmlinux_map
->unmap_ip
= identity__map_ip
;
1093 map
= machine__kernel_map(machine
);
1094 kmap
= map__kmap(map
);
1098 kmap
->kmaps
= &machine
->kmaps
;
1099 map_groups__insert(&machine
->kmaps
, map
);
1104 void machine__destroy_kernel_maps(struct machine
*machine
)
1107 struct map
*map
= machine__kernel_map(machine
);
1112 kmap
= map__kmap(map
);
1113 map_groups__remove(&machine
->kmaps
, map
);
1114 if (kmap
&& kmap
->ref_reloc_sym
) {
1115 zfree((char **)&kmap
->ref_reloc_sym
->name
);
1116 zfree(&kmap
->ref_reloc_sym
);
1119 map__zput(machine
->vmlinux_map
);
1122 int machines__create_guest_kernel_maps(struct machines
*machines
)
1125 struct dirent
**namelist
= NULL
;
1127 char path
[PATH_MAX
];
1131 if (symbol_conf
.default_guest_vmlinux_name
||
1132 symbol_conf
.default_guest_modules
||
1133 symbol_conf
.default_guest_kallsyms
) {
1134 machines__create_kernel_maps(machines
, DEFAULT_GUEST_KERNEL_ID
);
1137 if (symbol_conf
.guestmount
) {
1138 items
= scandir(symbol_conf
.guestmount
, &namelist
, NULL
, NULL
);
1141 for (i
= 0; i
< items
; i
++) {
1142 if (!isdigit(namelist
[i
]->d_name
[0])) {
1143 /* Filter out . and .. */
1146 pid
= (pid_t
)strtol(namelist
[i
]->d_name
, &endp
, 10);
1147 if ((*endp
!= '\0') ||
1148 (endp
== namelist
[i
]->d_name
) ||
1149 (errno
== ERANGE
)) {
1150 pr_debug("invalid directory (%s). Skipping.\n",
1151 namelist
[i
]->d_name
);
1154 sprintf(path
, "%s/%s/proc/kallsyms",
1155 symbol_conf
.guestmount
,
1156 namelist
[i
]->d_name
);
1157 ret
= access(path
, R_OK
);
1159 pr_debug("Can't access file %s\n", path
);
1162 machines__create_kernel_maps(machines
, pid
);
1171 void machines__destroy_kernel_maps(struct machines
*machines
)
1173 struct rb_node
*next
= rb_first_cached(&machines
->guests
);
1175 machine__destroy_kernel_maps(&machines
->host
);
1178 struct machine
*pos
= rb_entry(next
, struct machine
, rb_node
);
1180 next
= rb_next(&pos
->rb_node
);
1181 rb_erase_cached(&pos
->rb_node
, &machines
->guests
);
1182 machine__delete(pos
);
1186 int machines__create_kernel_maps(struct machines
*machines
, pid_t pid
)
1188 struct machine
*machine
= machines__findnew(machines
, pid
);
1190 if (machine
== NULL
)
1193 return machine__create_kernel_maps(machine
);
1196 int machine__load_kallsyms(struct machine
*machine
, const char *filename
)
1198 struct map
*map
= machine__kernel_map(machine
);
1199 int ret
= __dso__load_kallsyms(map
->dso
, filename
, map
, true);
1202 dso__set_loaded(map
->dso
);
1204 * Since /proc/kallsyms will have multiple sessions for the
1205 * kernel, with modules between them, fixup the end of all
1208 map_groups__fixup_end(&machine
->kmaps
);
1214 int machine__load_vmlinux_path(struct machine
*machine
)
1216 struct map
*map
= machine__kernel_map(machine
);
1217 int ret
= dso__load_vmlinux_path(map
->dso
, map
);
1220 dso__set_loaded(map
->dso
);
1225 static char *get_kernel_version(const char *root_dir
)
1227 char version
[PATH_MAX
];
1230 const char *prefix
= "Linux version ";
1232 sprintf(version
, "%s/proc/version", root_dir
);
1233 file
= fopen(version
, "r");
1238 tmp
= fgets(version
, sizeof(version
), file
);
1241 name
= strstr(version
, prefix
);
1244 name
+= strlen(prefix
);
1245 tmp
= strchr(name
, ' ');
1249 return strdup(name
);
1252 static bool is_kmod_dso(struct dso
*dso
)
1254 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
||
1255 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE
;
1258 static int map_groups__set_module_path(struct map_groups
*mg
, const char *path
,
1259 struct kmod_path
*m
)
1262 struct map
*map
= map_groups__find_by_name(mg
, m
->name
);
1267 long_name
= strdup(path
);
1268 if (long_name
== NULL
)
1271 dso__set_long_name(map
->dso
, long_name
, true);
1272 dso__kernel_module_get_build_id(map
->dso
, "");
1275 * Full name could reveal us kmod compression, so
1276 * we need to update the symtab_type if needed.
1278 if (m
->comp
&& is_kmod_dso(map
->dso
)) {
1279 map
->dso
->symtab_type
++;
1280 map
->dso
->comp
= m
->comp
;
1286 static int map_groups__set_modules_path_dir(struct map_groups
*mg
,
1287 const char *dir_name
, int depth
)
1289 struct dirent
*dent
;
1290 DIR *dir
= opendir(dir_name
);
1294 pr_debug("%s: cannot open %s dir\n", __func__
, dir_name
);
1298 while ((dent
= readdir(dir
)) != NULL
) {
1299 char path
[PATH_MAX
];
1302 /*sshfs might return bad dent->d_type, so we have to stat*/
1303 snprintf(path
, sizeof(path
), "%s/%s", dir_name
, dent
->d_name
);
1304 if (stat(path
, &st
))
1307 if (S_ISDIR(st
.st_mode
)) {
1308 if (!strcmp(dent
->d_name
, ".") ||
1309 !strcmp(dent
->d_name
, ".."))
1312 /* Do not follow top-level source and build symlinks */
1314 if (!strcmp(dent
->d_name
, "source") ||
1315 !strcmp(dent
->d_name
, "build"))
1319 ret
= map_groups__set_modules_path_dir(mg
, path
,
1326 ret
= kmod_path__parse_name(&m
, dent
->d_name
);
1331 ret
= map_groups__set_module_path(mg
, path
, &m
);
1345 static int machine__set_modules_path(struct machine
*machine
)
1348 char modules_path
[PATH_MAX
];
1350 version
= get_kernel_version(machine
->root_dir
);
1354 snprintf(modules_path
, sizeof(modules_path
), "%s/lib/modules/%s",
1355 machine
->root_dir
, version
);
1358 return map_groups__set_modules_path_dir(&machine
->kmaps
, modules_path
, 0);
1360 int __weak
arch__fix_module_text_start(u64
*start __maybe_unused
,
1361 const char *name __maybe_unused
)
1366 static int machine__create_module(void *arg
, const char *name
, u64 start
,
1369 struct machine
*machine
= arg
;
1372 if (arch__fix_module_text_start(&start
, name
) < 0)
1375 map
= machine__findnew_module_map(machine
, start
, name
);
1378 map
->end
= start
+ size
;
1380 dso__kernel_module_get_build_id(map
->dso
, machine
->root_dir
);
1385 static int machine__create_modules(struct machine
*machine
)
1387 const char *modules
;
1388 char path
[PATH_MAX
];
1390 if (machine__is_default_guest(machine
)) {
1391 modules
= symbol_conf
.default_guest_modules
;
1393 snprintf(path
, PATH_MAX
, "%s/proc/modules", machine
->root_dir
);
1397 if (symbol__restricted_filename(modules
, "/proc/modules"))
1400 if (modules__parse(modules
, machine
, machine__create_module
))
1403 if (!machine__set_modules_path(machine
))
1406 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1411 static void machine__set_kernel_mmap(struct machine
*machine
,
1414 machine
->vmlinux_map
->start
= start
;
1415 machine
->vmlinux_map
->end
= end
;
1417 * Be a bit paranoid here, some perf.data file came with
1418 * a zero sized synthesized MMAP event for the kernel.
1420 if (start
== 0 && end
== 0)
1421 machine
->vmlinux_map
->end
= ~0ULL;
1424 static void machine__update_kernel_mmap(struct machine
*machine
,
1427 struct map
*map
= machine__kernel_map(machine
);
1430 map_groups__remove(&machine
->kmaps
, map
);
1432 machine__set_kernel_mmap(machine
, start
, end
);
1434 map_groups__insert(&machine
->kmaps
, map
);
1438 int machine__create_kernel_maps(struct machine
*machine
)
1440 struct dso
*kernel
= machine__get_kernel(machine
);
1441 const char *name
= NULL
;
1449 ret
= __machine__create_kernel_maps(machine
, kernel
);
1453 if (symbol_conf
.use_modules
&& machine__create_modules(machine
) < 0) {
1454 if (machine__is_host(machine
))
1455 pr_debug("Problems creating module maps, "
1456 "continuing anyway...\n");
1458 pr_debug("Problems creating module maps for guest %d, "
1459 "continuing anyway...\n", machine
->pid
);
1462 if (!machine__get_running_kernel_start(machine
, &name
, &addr
)) {
1464 map__set_kallsyms_ref_reloc_sym(machine
->vmlinux_map
, name
, addr
)) {
1465 machine__destroy_kernel_maps(machine
);
1471 * we have a real start address now, so re-order the kmaps
1472 * assume it's the last in the kmaps
1474 machine__update_kernel_mmap(machine
, addr
, ~0ULL);
1477 if (machine__create_extra_kernel_maps(machine
, kernel
))
1478 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1480 /* update end address of the kernel map using adjacent module address */
1481 map
= map__next(machine__kernel_map(machine
));
1483 machine__set_kernel_mmap(machine
, addr
, map
->start
);
1489 static bool machine__uses_kcore(struct machine
*machine
)
1493 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1494 if (dso__is_kcore(dso
))
1501 static bool perf_event__is_extra_kernel_mmap(struct machine
*machine
,
1502 union perf_event
*event
)
1504 return machine__is(machine
, "x86_64") &&
1505 is_entry_trampoline(event
->mmap
.filename
);
1508 static int machine__process_extra_kernel_map(struct machine
*machine
,
1509 union perf_event
*event
)
1511 struct map
*kernel_map
= machine__kernel_map(machine
);
1512 struct dso
*kernel
= kernel_map
? kernel_map
->dso
: NULL
;
1513 struct extra_kernel_map xm
= {
1514 .start
= event
->mmap
.start
,
1515 .end
= event
->mmap
.start
+ event
->mmap
.len
,
1516 .pgoff
= event
->mmap
.pgoff
,
1522 strlcpy(xm
.name
, event
->mmap
.filename
, KMAP_NAME_LEN
);
1524 return machine__create_extra_kernel_map(machine
, kernel
, &xm
);
1527 static int machine__process_kernel_mmap_event(struct machine
*machine
,
1528 union perf_event
*event
)
1531 enum dso_kernel_type kernel_type
;
1532 bool is_kernel_mmap
;
1534 /* If we have maps from kcore then we do not need or want any others */
1535 if (machine__uses_kcore(machine
))
1538 if (machine__is_host(machine
))
1539 kernel_type
= DSO_TYPE_KERNEL
;
1541 kernel_type
= DSO_TYPE_GUEST_KERNEL
;
1543 is_kernel_mmap
= memcmp(event
->mmap
.filename
,
1545 strlen(machine
->mmap_name
) - 1) == 0;
1546 if (event
->mmap
.filename
[0] == '/' ||
1547 (!is_kernel_mmap
&& event
->mmap
.filename
[0] == '[')) {
1548 map
= machine__findnew_module_map(machine
, event
->mmap
.start
,
1549 event
->mmap
.filename
);
1553 map
->end
= map
->start
+ event
->mmap
.len
;
1554 } else if (is_kernel_mmap
) {
1555 const char *symbol_name
= (event
->mmap
.filename
+
1556 strlen(machine
->mmap_name
));
1558 * Should be there already, from the build-id table in
1561 struct dso
*kernel
= NULL
;
1564 down_read(&machine
->dsos
.lock
);
1566 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1569 * The cpumode passed to is_kernel_module is not the
1570 * cpumode of *this* event. If we insist on passing
1571 * correct cpumode to is_kernel_module, we should
1572 * record the cpumode when we adding this dso to the
1575 * However we don't really need passing correct
1576 * cpumode. We know the correct cpumode must be kernel
1577 * mode (if not, we should not link it onto kernel_dsos
1580 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1581 * is_kernel_module() treats it as a kernel cpumode.
1585 is_kernel_module(dso
->long_name
,
1586 PERF_RECORD_MISC_CPUMODE_UNKNOWN
))
1594 up_read(&machine
->dsos
.lock
);
1597 kernel
= machine__findnew_dso(machine
, machine
->mmap_name
);
1601 kernel
->kernel
= kernel_type
;
1602 if (__machine__create_kernel_maps(machine
, kernel
) < 0) {
1607 if (strstr(kernel
->long_name
, "vmlinux"))
1608 dso__set_short_name(kernel
, "[kernel.vmlinux]", false);
1610 machine__update_kernel_mmap(machine
, event
->mmap
.start
,
1611 event
->mmap
.start
+ event
->mmap
.len
);
1614 * Avoid using a zero address (kptr_restrict) for the ref reloc
1615 * symbol. Effectively having zero here means that at record
1616 * time /proc/sys/kernel/kptr_restrict was non zero.
1618 if (event
->mmap
.pgoff
!= 0) {
1619 map__set_kallsyms_ref_reloc_sym(machine
->vmlinux_map
,
1624 if (machine__is_default_guest(machine
)) {
1626 * preload dso of guest kernel and modules
1628 dso__load(kernel
, machine__kernel_map(machine
));
1630 } else if (perf_event__is_extra_kernel_mmap(machine
, event
)) {
1631 return machine__process_extra_kernel_map(machine
, event
);
1638 int machine__process_mmap2_event(struct machine
*machine
,
1639 union perf_event
*event
,
1640 struct perf_sample
*sample
)
1642 struct thread
*thread
;
1647 perf_event__fprintf_mmap2(event
, stdout
);
1649 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1650 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1651 ret
= machine__process_kernel_mmap_event(machine
, event
);
1657 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
,
1662 map
= map__new(machine
, event
->mmap2
.start
,
1663 event
->mmap2
.len
, event
->mmap2
.pgoff
,
1665 event
->mmap2
.min
, event
->mmap2
.ino
,
1666 event
->mmap2
.ino_generation
,
1669 event
->mmap2
.filename
, thread
);
1672 goto out_problem_map
;
1674 ret
= thread__insert_map(thread
, map
);
1676 goto out_problem_insert
;
1678 thread__put(thread
);
1685 thread__put(thread
);
1687 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1691 int machine__process_mmap_event(struct machine
*machine
, union perf_event
*event
,
1692 struct perf_sample
*sample
)
1694 struct thread
*thread
;
1700 perf_event__fprintf_mmap(event
, stdout
);
1702 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1703 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1704 ret
= machine__process_kernel_mmap_event(machine
, event
);
1710 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
,
1715 if (!(event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
))
1718 map
= map__new(machine
, event
->mmap
.start
,
1719 event
->mmap
.len
, event
->mmap
.pgoff
,
1720 0, 0, 0, 0, prot
, 0,
1721 event
->mmap
.filename
,
1725 goto out_problem_map
;
1727 ret
= thread__insert_map(thread
, map
);
1729 goto out_problem_insert
;
1731 thread__put(thread
);
1738 thread__put(thread
);
1740 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1744 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
)
1746 struct threads
*threads
= machine__threads(machine
, th
->tid
);
1748 if (threads
->last_match
== th
)
1749 threads__set_last_match(threads
, NULL
);
1751 BUG_ON(refcount_read(&th
->refcnt
) == 0);
1753 down_write(&threads
->lock
);
1754 rb_erase_cached(&th
->rb_node
, &threads
->entries
);
1755 RB_CLEAR_NODE(&th
->rb_node
);
1758 * Move it first to the dead_threads list, then drop the reference,
1759 * if this is the last reference, then the thread__delete destructor
1760 * will be called and we will remove it from the dead_threads list.
1762 list_add_tail(&th
->node
, &threads
->dead
);
1764 up_write(&threads
->lock
);
1768 void machine__remove_thread(struct machine
*machine
, struct thread
*th
)
1770 return __machine__remove_thread(machine
, th
, true);
1773 int machine__process_fork_event(struct machine
*machine
, union perf_event
*event
,
1774 struct perf_sample
*sample
)
1776 struct thread
*thread
= machine__find_thread(machine
,
1779 struct thread
*parent
= machine__findnew_thread(machine
,
1782 bool do_maps_clone
= true;
1786 perf_event__fprintf_task(event
, stdout
);
1789 * There may be an existing thread that is not actually the parent,
1790 * either because we are processing events out of order, or because the
1791 * (fork) event that would have removed the thread was lost. Assume the
1792 * latter case and continue on as best we can.
1794 if (parent
->pid_
!= (pid_t
)event
->fork
.ppid
) {
1795 dump_printf("removing erroneous parent thread %d/%d\n",
1796 parent
->pid_
, parent
->tid
);
1797 machine__remove_thread(machine
, parent
);
1798 thread__put(parent
);
1799 parent
= machine__findnew_thread(machine
, event
->fork
.ppid
,
1803 /* if a thread currently exists for the thread id remove it */
1804 if (thread
!= NULL
) {
1805 machine__remove_thread(machine
, thread
);
1806 thread__put(thread
);
1809 thread
= machine__findnew_thread(machine
, event
->fork
.pid
,
1812 * When synthesizing FORK events, we are trying to create thread
1813 * objects for the already running tasks on the machine.
1815 * Normally, for a kernel FORK event, we want to clone the parent's
1816 * maps because that is what the kernel just did.
1818 * But when synthesizing, this should not be done. If we do, we end up
1819 * with overlapping maps as we process the sythesized MMAP2 events that
1820 * get delivered shortly thereafter.
1822 * Use the FORK event misc flags in an internal way to signal this
1823 * situation, so we can elide the map clone when appropriate.
1825 if (event
->fork
.header
.misc
& PERF_RECORD_MISC_FORK_EXEC
)
1826 do_maps_clone
= false;
1828 if (thread
== NULL
|| parent
== NULL
||
1829 thread__fork(thread
, parent
, sample
->time
, do_maps_clone
) < 0) {
1830 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1833 thread__put(thread
);
1834 thread__put(parent
);
1839 int machine__process_exit_event(struct machine
*machine
, union perf_event
*event
,
1840 struct perf_sample
*sample __maybe_unused
)
1842 struct thread
*thread
= machine__find_thread(machine
,
1847 perf_event__fprintf_task(event
, stdout
);
1849 if (thread
!= NULL
) {
1850 thread__exited(thread
);
1851 thread__put(thread
);
1857 int machine__process_event(struct machine
*machine
, union perf_event
*event
,
1858 struct perf_sample
*sample
)
1862 switch (event
->header
.type
) {
1863 case PERF_RECORD_COMM
:
1864 ret
= machine__process_comm_event(machine
, event
, sample
); break;
1865 case PERF_RECORD_MMAP
:
1866 ret
= machine__process_mmap_event(machine
, event
, sample
); break;
1867 case PERF_RECORD_NAMESPACES
:
1868 ret
= machine__process_namespaces_event(machine
, event
, sample
); break;
1869 case PERF_RECORD_MMAP2
:
1870 ret
= machine__process_mmap2_event(machine
, event
, sample
); break;
1871 case PERF_RECORD_FORK
:
1872 ret
= machine__process_fork_event(machine
, event
, sample
); break;
1873 case PERF_RECORD_EXIT
:
1874 ret
= machine__process_exit_event(machine
, event
, sample
); break;
1875 case PERF_RECORD_LOST
:
1876 ret
= machine__process_lost_event(machine
, event
, sample
); break;
1877 case PERF_RECORD_AUX
:
1878 ret
= machine__process_aux_event(machine
, event
); break;
1879 case PERF_RECORD_ITRACE_START
:
1880 ret
= machine__process_itrace_start_event(machine
, event
); break;
1881 case PERF_RECORD_LOST_SAMPLES
:
1882 ret
= machine__process_lost_samples_event(machine
, event
, sample
); break;
1883 case PERF_RECORD_SWITCH
:
1884 case PERF_RECORD_SWITCH_CPU_WIDE
:
1885 ret
= machine__process_switch_event(machine
, event
); break;
1886 case PERF_RECORD_KSYMBOL
:
1887 ret
= machine__process_ksymbol(machine
, event
, sample
); break;
1888 case PERF_RECORD_BPF_EVENT
:
1889 ret
= machine__process_bpf_event(machine
, event
, sample
); break;
1898 static bool symbol__match_regex(struct symbol
*sym
, regex_t
*regex
)
1900 if (!regexec(regex
, sym
->name
, 0, NULL
, 0))
1905 static void ip__resolve_ams(struct thread
*thread
,
1906 struct addr_map_symbol
*ams
,
1909 struct addr_location al
;
1911 memset(&al
, 0, sizeof(al
));
1913 * We cannot use the header.misc hint to determine whether a
1914 * branch stack address is user, kernel, guest, hypervisor.
1915 * Branches may straddle the kernel/user/hypervisor boundaries.
1916 * Thus, we have to try consecutively until we find a match
1917 * or else, the symbol is unknown
1919 thread__find_cpumode_addr_location(thread
, ip
, &al
);
1922 ams
->al_addr
= al
.addr
;
1928 static void ip__resolve_data(struct thread
*thread
,
1929 u8 m
, struct addr_map_symbol
*ams
,
1930 u64 addr
, u64 phys_addr
)
1932 struct addr_location al
;
1934 memset(&al
, 0, sizeof(al
));
1936 thread__find_symbol(thread
, m
, addr
, &al
);
1939 ams
->al_addr
= al
.addr
;
1942 ams
->phys_addr
= phys_addr
;
1945 struct mem_info
*sample__resolve_mem(struct perf_sample
*sample
,
1946 struct addr_location
*al
)
1948 struct mem_info
*mi
= mem_info__new();
1953 ip__resolve_ams(al
->thread
, &mi
->iaddr
, sample
->ip
);
1954 ip__resolve_data(al
->thread
, al
->cpumode
, &mi
->daddr
,
1955 sample
->addr
, sample
->phys_addr
);
1956 mi
->data_src
.val
= sample
->data_src
;
1961 static char *callchain_srcline(struct map
*map
, struct symbol
*sym
, u64 ip
)
1963 char *srcline
= NULL
;
1965 if (!map
|| callchain_param
.key
== CCKEY_FUNCTION
)
1968 srcline
= srcline__tree_find(&map
->dso
->srclines
, ip
);
1970 bool show_sym
= false;
1971 bool show_addr
= callchain_param
.key
== CCKEY_ADDRESS
;
1973 srcline
= get_srcline(map
->dso
, map__rip_2objdump(map
, ip
),
1974 sym
, show_sym
, show_addr
, ip
);
1975 srcline__tree_insert(&map
->dso
->srclines
, ip
, srcline
);
1986 static int add_callchain_ip(struct thread
*thread
,
1987 struct callchain_cursor
*cursor
,
1988 struct symbol
**parent
,
1989 struct addr_location
*root_al
,
1993 struct branch_flags
*flags
,
1994 struct iterations
*iter
,
1997 struct addr_location al
;
1998 int nr_loop_iter
= 0;
1999 u64 iter_cycles
= 0;
2000 const char *srcline
= NULL
;
2005 thread__find_cpumode_addr_location(thread
, ip
, &al
);
2007 if (ip
>= PERF_CONTEXT_MAX
) {
2009 case PERF_CONTEXT_HV
:
2010 *cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
2012 case PERF_CONTEXT_KERNEL
:
2013 *cpumode
= PERF_RECORD_MISC_KERNEL
;
2015 case PERF_CONTEXT_USER
:
2016 *cpumode
= PERF_RECORD_MISC_USER
;
2019 pr_debug("invalid callchain context: "
2020 "%"PRId64
"\n", (s64
) ip
);
2022 * It seems the callchain is corrupted.
2025 callchain_cursor_reset(cursor
);
2030 thread__find_symbol(thread
, *cpumode
, ip
, &al
);
2033 if (al
.sym
!= NULL
) {
2034 if (perf_hpp_list
.parent
&& !*parent
&&
2035 symbol__match_regex(al
.sym
, &parent_regex
))
2037 else if (have_ignore_callees
&& root_al
&&
2038 symbol__match_regex(al
.sym
, &ignore_callees_regex
)) {
2039 /* Treat this symbol as the root,
2040 forgetting its callees. */
2042 callchain_cursor_reset(cursor
);
2046 if (symbol_conf
.hide_unresolved
&& al
.sym
== NULL
)
2050 nr_loop_iter
= iter
->nr_loop_iter
;
2051 iter_cycles
= iter
->cycles
;
2054 srcline
= callchain_srcline(al
.map
, al
.sym
, al
.addr
);
2055 return callchain_cursor_append(cursor
, ip
, al
.map
, al
.sym
,
2056 branch
, flags
, nr_loop_iter
,
2057 iter_cycles
, branch_from
, srcline
);
2060 struct branch_info
*sample__resolve_bstack(struct perf_sample
*sample
,
2061 struct addr_location
*al
)
2064 const struct branch_stack
*bs
= sample
->branch_stack
;
2065 struct branch_info
*bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
2070 for (i
= 0; i
< bs
->nr
; i
++) {
2071 ip__resolve_ams(al
->thread
, &bi
[i
].to
, bs
->entries
[i
].to
);
2072 ip__resolve_ams(al
->thread
, &bi
[i
].from
, bs
->entries
[i
].from
);
2073 bi
[i
].flags
= bs
->entries
[i
].flags
;
2078 static void save_iterations(struct iterations
*iter
,
2079 struct branch_entry
*be
, int nr
)
2083 iter
->nr_loop_iter
++;
2086 for (i
= 0; i
< nr
; i
++)
2087 iter
->cycles
+= be
[i
].flags
.cycles
;
2092 #define NO_ENTRY 0xff
2094 #define PERF_MAX_BRANCH_DEPTH 127
2097 static int remove_loops(struct branch_entry
*l
, int nr
,
2098 struct iterations
*iter
)
2101 unsigned char chash
[CHASHSZ
];
2103 memset(chash
, NO_ENTRY
, sizeof(chash
));
2105 BUG_ON(PERF_MAX_BRANCH_DEPTH
> 255);
2107 for (i
= 0; i
< nr
; i
++) {
2108 int h
= hash_64(l
[i
].from
, CHASHBITS
) % CHASHSZ
;
2110 /* no collision handling for now */
2111 if (chash
[h
] == NO_ENTRY
) {
2113 } else if (l
[chash
[h
]].from
== l
[i
].from
) {
2114 bool is_loop
= true;
2115 /* check if it is a real loop */
2117 for (j
= chash
[h
]; j
< i
&& i
+ off
< nr
; j
++, off
++)
2118 if (l
[j
].from
!= l
[i
+ off
].from
) {
2125 save_iterations(iter
+ i
+ off
,
2128 memmove(iter
+ i
, iter
+ i
+ off
,
2131 memmove(l
+ i
, l
+ i
+ off
,
2143 * Recolve LBR callstack chain sample
2145 * 1 on success get LBR callchain information
2146 * 0 no available LBR callchain information, should try fp
2147 * negative error code on other errors.
2149 static int resolve_lbr_callchain_sample(struct thread
*thread
,
2150 struct callchain_cursor
*cursor
,
2151 struct perf_sample
*sample
,
2152 struct symbol
**parent
,
2153 struct addr_location
*root_al
,
2156 struct ip_callchain
*chain
= sample
->callchain
;
2157 int chain_nr
= min(max_stack
, (int)chain
->nr
), i
;
2158 u8 cpumode
= PERF_RECORD_MISC_USER
;
2159 u64 ip
, branch_from
= 0;
2161 for (i
= 0; i
< chain_nr
; i
++) {
2162 if (chain
->ips
[i
] == PERF_CONTEXT_USER
)
2166 /* LBR only affects the user callchain */
2167 if (i
!= chain_nr
) {
2168 struct branch_stack
*lbr_stack
= sample
->branch_stack
;
2169 int lbr_nr
= lbr_stack
->nr
, j
, k
;
2171 struct branch_flags
*flags
;
2173 * LBR callstack can only get user call chain.
2174 * The mix_chain_nr is kernel call chain
2175 * number plus LBR user call chain number.
2176 * i is kernel call chain number,
2177 * 1 is PERF_CONTEXT_USER,
2178 * lbr_nr + 1 is the user call chain number.
2179 * For details, please refer to the comments
2180 * in callchain__printf
2182 int mix_chain_nr
= i
+ 1 + lbr_nr
+ 1;
2184 for (j
= 0; j
< mix_chain_nr
; j
++) {
2189 if (callchain_param
.order
== ORDER_CALLEE
) {
2192 else if (j
> i
+ 1) {
2194 ip
= lbr_stack
->entries
[k
].from
;
2196 flags
= &lbr_stack
->entries
[k
].flags
;
2198 ip
= lbr_stack
->entries
[0].to
;
2200 flags
= &lbr_stack
->entries
[0].flags
;
2202 lbr_stack
->entries
[0].from
;
2207 ip
= lbr_stack
->entries
[k
].from
;
2209 flags
= &lbr_stack
->entries
[k
].flags
;
2211 else if (j
> lbr_nr
)
2212 ip
= chain
->ips
[i
+ 1 - (j
- lbr_nr
)];
2214 ip
= lbr_stack
->entries
[0].to
;
2216 flags
= &lbr_stack
->entries
[0].flags
;
2218 lbr_stack
->entries
[0].from
;
2222 err
= add_callchain_ip(thread
, cursor
, parent
,
2223 root_al
, &cpumode
, ip
,
2224 branch
, flags
, NULL
,
2227 return (err
< 0) ? err
: 0;
2235 static int find_prev_cpumode(struct ip_callchain
*chain
, struct thread
*thread
,
2236 struct callchain_cursor
*cursor
,
2237 struct symbol
**parent
,
2238 struct addr_location
*root_al
,
2239 u8
*cpumode
, int ent
)
2243 while (--ent
>= 0) {
2244 u64 ip
= chain
->ips
[ent
];
2246 if (ip
>= PERF_CONTEXT_MAX
) {
2247 err
= add_callchain_ip(thread
, cursor
, parent
,
2248 root_al
, cpumode
, ip
,
2249 false, NULL
, NULL
, 0);
2256 static int thread__resolve_callchain_sample(struct thread
*thread
,
2257 struct callchain_cursor
*cursor
,
2258 struct perf_evsel
*evsel
,
2259 struct perf_sample
*sample
,
2260 struct symbol
**parent
,
2261 struct addr_location
*root_al
,
2264 struct branch_stack
*branch
= sample
->branch_stack
;
2265 struct ip_callchain
*chain
= sample
->callchain
;
2267 u8 cpumode
= PERF_RECORD_MISC_USER
;
2268 int i
, j
, err
, nr_entries
;
2273 chain_nr
= chain
->nr
;
2275 if (perf_evsel__has_branch_callstack(evsel
)) {
2276 err
= resolve_lbr_callchain_sample(thread
, cursor
, sample
, parent
,
2277 root_al
, max_stack
);
2279 return (err
< 0) ? err
: 0;
2283 * Based on DWARF debug information, some architectures skip
2284 * a callchain entry saved by the kernel.
2286 skip_idx
= arch_skip_callchain_idx(thread
, chain
);
2289 * Add branches to call stack for easier browsing. This gives
2290 * more context for a sample than just the callers.
2292 * This uses individual histograms of paths compared to the
2293 * aggregated histograms the normal LBR mode uses.
2295 * Limitations for now:
2296 * - No extra filters
2297 * - No annotations (should annotate somehow)
2300 if (branch
&& callchain_param
.branch_callstack
) {
2301 int nr
= min(max_stack
, (int)branch
->nr
);
2302 struct branch_entry be
[nr
];
2303 struct iterations iter
[nr
];
2305 if (branch
->nr
> PERF_MAX_BRANCH_DEPTH
) {
2306 pr_warning("corrupted branch chain. skipping...\n");
2310 for (i
= 0; i
< nr
; i
++) {
2311 if (callchain_param
.order
== ORDER_CALLEE
) {
2312 be
[i
] = branch
->entries
[i
];
2318 * Check for overlap into the callchain.
2319 * The return address is one off compared to
2320 * the branch entry. To adjust for this
2321 * assume the calling instruction is not longer
2324 if (i
== skip_idx
||
2325 chain
->ips
[first_call
] >= PERF_CONTEXT_MAX
)
2327 else if (be
[i
].from
< chain
->ips
[first_call
] &&
2328 be
[i
].from
>= chain
->ips
[first_call
] - 8)
2331 be
[i
] = branch
->entries
[branch
->nr
- i
- 1];
2334 memset(iter
, 0, sizeof(struct iterations
) * nr
);
2335 nr
= remove_loops(be
, nr
, iter
);
2337 for (i
= 0; i
< nr
; i
++) {
2338 err
= add_callchain_ip(thread
, cursor
, parent
,
2345 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
,
2362 if (callchain_param
.order
!= ORDER_CALLEE
) {
2363 err
= find_prev_cpumode(chain
, thread
, cursor
, parent
, root_al
,
2364 &cpumode
, chain
->nr
- first_call
);
2366 return (err
< 0) ? err
: 0;
2368 for (i
= first_call
, nr_entries
= 0;
2369 i
< chain_nr
&& nr_entries
< max_stack
; i
++) {
2372 if (callchain_param
.order
== ORDER_CALLEE
)
2375 j
= chain
->nr
- i
- 1;
2377 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2382 if (ip
< PERF_CONTEXT_MAX
)
2384 else if (callchain_param
.order
!= ORDER_CALLEE
) {
2385 err
= find_prev_cpumode(chain
, thread
, cursor
, parent
,
2386 root_al
, &cpumode
, j
);
2388 return (err
< 0) ? err
: 0;
2392 err
= add_callchain_ip(thread
, cursor
, parent
,
2393 root_al
, &cpumode
, ip
,
2394 false, NULL
, NULL
, 0);
2397 return (err
< 0) ? err
: 0;
2403 static int append_inlines(struct callchain_cursor
*cursor
,
2404 struct map
*map
, struct symbol
*sym
, u64 ip
)
2406 struct inline_node
*inline_node
;
2407 struct inline_list
*ilist
;
2411 if (!symbol_conf
.inline_name
|| !map
|| !sym
)
2414 addr
= map__map_ip(map
, ip
);
2415 addr
= map__rip_2objdump(map
, addr
);
2417 inline_node
= inlines__tree_find(&map
->dso
->inlined_nodes
, addr
);
2419 inline_node
= dso__parse_addr_inlines(map
->dso
, addr
, sym
);
2422 inlines__tree_insert(&map
->dso
->inlined_nodes
, inline_node
);
2425 list_for_each_entry(ilist
, &inline_node
->val
, list
) {
2426 ret
= callchain_cursor_append(cursor
, ip
, map
,
2427 ilist
->symbol
, false,
2428 NULL
, 0, 0, 0, ilist
->srcline
);
2437 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
2439 struct callchain_cursor
*cursor
= arg
;
2440 const char *srcline
= NULL
;
2441 u64 addr
= entry
->ip
;
2443 if (symbol_conf
.hide_unresolved
&& entry
->sym
== NULL
)
2446 if (append_inlines(cursor
, entry
->map
, entry
->sym
, entry
->ip
) == 0)
2450 * Convert entry->ip from a virtual address to an offset in
2451 * its corresponding binary.
2454 addr
= map__map_ip(entry
->map
, entry
->ip
);
2456 srcline
= callchain_srcline(entry
->map
, entry
->sym
, addr
);
2457 return callchain_cursor_append(cursor
, entry
->ip
,
2458 entry
->map
, entry
->sym
,
2459 false, NULL
, 0, 0, 0, srcline
);
2462 static int thread__resolve_callchain_unwind(struct thread
*thread
,
2463 struct callchain_cursor
*cursor
,
2464 struct perf_evsel
*evsel
,
2465 struct perf_sample
*sample
,
2468 /* Can we do dwarf post unwind? */
2469 if (!((evsel
->attr
.sample_type
& PERF_SAMPLE_REGS_USER
) &&
2470 (evsel
->attr
.sample_type
& PERF_SAMPLE_STACK_USER
)))
2473 /* Bail out if nothing was captured. */
2474 if ((!sample
->user_regs
.regs
) ||
2475 (!sample
->user_stack
.size
))
2478 return unwind__get_entries(unwind_entry
, cursor
,
2479 thread
, sample
, max_stack
);
2482 int thread__resolve_callchain(struct thread
*thread
,
2483 struct callchain_cursor
*cursor
,
2484 struct perf_evsel
*evsel
,
2485 struct perf_sample
*sample
,
2486 struct symbol
**parent
,
2487 struct addr_location
*root_al
,
2492 callchain_cursor_reset(cursor
);
2494 if (callchain_param
.order
== ORDER_CALLEE
) {
2495 ret
= thread__resolve_callchain_sample(thread
, cursor
,
2501 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
2505 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
2510 ret
= thread__resolve_callchain_sample(thread
, cursor
,
2519 int machine__for_each_thread(struct machine
*machine
,
2520 int (*fn
)(struct thread
*thread
, void *p
),
2523 struct threads
*threads
;
2525 struct thread
*thread
;
2529 for (i
= 0; i
< THREADS__TABLE_SIZE
; i
++) {
2530 threads
= &machine
->threads
[i
];
2531 for (nd
= rb_first_cached(&threads
->entries
); nd
;
2533 thread
= rb_entry(nd
, struct thread
, rb_node
);
2534 rc
= fn(thread
, priv
);
2539 list_for_each_entry(thread
, &threads
->dead
, node
) {
2540 rc
= fn(thread
, priv
);
2548 int machines__for_each_thread(struct machines
*machines
,
2549 int (*fn
)(struct thread
*thread
, void *p
),
2555 rc
= machine__for_each_thread(&machines
->host
, fn
, priv
);
2559 for (nd
= rb_first_cached(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
2560 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
2562 rc
= machine__for_each_thread(machine
, fn
, priv
);
2569 int __machine__synthesize_threads(struct machine
*machine
, struct perf_tool
*tool
,
2570 struct target
*target
, struct thread_map
*threads
,
2571 perf_event__handler_t process
, bool data_mmap
,
2572 unsigned int nr_threads_synthesize
)
2574 if (target__has_task(target
))
2575 return perf_event__synthesize_thread_map(tool
, threads
, process
, machine
, data_mmap
);
2576 else if (target__has_cpu(target
))
2577 return perf_event__synthesize_threads(tool
, process
,
2579 nr_threads_synthesize
);
2580 /* command specified */
2584 pid_t
machine__get_current_tid(struct machine
*machine
, int cpu
)
2586 if (cpu
< 0 || cpu
>= MAX_NR_CPUS
|| !machine
->current_tid
)
2589 return machine
->current_tid
[cpu
];
2592 int machine__set_current_tid(struct machine
*machine
, int cpu
, pid_t pid
,
2595 struct thread
*thread
;
2600 if (!machine
->current_tid
) {
2603 machine
->current_tid
= calloc(MAX_NR_CPUS
, sizeof(pid_t
));
2604 if (!machine
->current_tid
)
2606 for (i
= 0; i
< MAX_NR_CPUS
; i
++)
2607 machine
->current_tid
[i
] = -1;
2610 if (cpu
>= MAX_NR_CPUS
) {
2611 pr_err("Requested CPU %d too large. ", cpu
);
2612 pr_err("Consider raising MAX_NR_CPUS\n");
2616 machine
->current_tid
[cpu
] = tid
;
2618 thread
= machine__findnew_thread(machine
, pid
, tid
);
2623 thread__put(thread
);
2629 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2630 * normalized arch is needed.
2632 bool machine__is(struct machine
*machine
, const char *arch
)
2634 return machine
&& !strcmp(perf_env__raw_arch(machine
->env
), arch
);
2637 int machine__nr_cpus_avail(struct machine
*machine
)
2639 return machine
? perf_env__nr_cpus_avail(machine
->env
) : 0;
2642 int machine__get_kernel_start(struct machine
*machine
)
2644 struct map
*map
= machine__kernel_map(machine
);
2648 * The only addresses above 2^63 are kernel addresses of a 64-bit
2649 * kernel. Note that addresses are unsigned so that on a 32-bit system
2650 * all addresses including kernel addresses are less than 2^32. In
2651 * that case (32-bit system), if the kernel mapping is unknown, all
2652 * addresses will be assumed to be in user space - see
2653 * machine__kernel_ip().
2655 machine
->kernel_start
= 1ULL << 63;
2657 err
= map__load(map
);
2659 * On x86_64, PTI entry trampolines are less than the
2660 * start of kernel text, but still above 2^63. So leave
2661 * kernel_start = 1ULL << 63 for x86_64.
2663 if (!err
&& !machine__is(machine
, "x86_64"))
2664 machine
->kernel_start
= map
->start
;
2669 u8
machine__addr_cpumode(struct machine
*machine
, u8 cpumode
, u64 addr
)
2671 u8 addr_cpumode
= cpumode
;
2674 if (!machine
->single_address_space
)
2677 kernel_ip
= machine__kernel_ip(machine
, addr
);
2679 case PERF_RECORD_MISC_KERNEL
:
2680 case PERF_RECORD_MISC_USER
:
2681 addr_cpumode
= kernel_ip
? PERF_RECORD_MISC_KERNEL
:
2682 PERF_RECORD_MISC_USER
;
2684 case PERF_RECORD_MISC_GUEST_KERNEL
:
2685 case PERF_RECORD_MISC_GUEST_USER
:
2686 addr_cpumode
= kernel_ip
? PERF_RECORD_MISC_GUEST_KERNEL
:
2687 PERF_RECORD_MISC_GUEST_USER
;
2693 return addr_cpumode
;
2696 struct dso
*machine__findnew_dso(struct machine
*machine
, const char *filename
)
2698 return dsos__findnew(&machine
->dsos
, filename
);
2701 char *machine__resolve_kernel_addr(void *vmachine
, unsigned long long *addrp
, char **modp
)
2703 struct machine
*machine
= vmachine
;
2705 struct symbol
*sym
= machine__find_kernel_symbol(machine
, *addrp
, &map
);
2710 *modp
= __map__is_kmodule(map
) ? (char *)map
->dso
->short_name
: NULL
;
2711 *addrp
= map
->unmap_ip(map
, sym
->start
);