1 // SPDX-License-Identifier: GPL-2.0
18 #include <sys/types.h>
22 #include "linux/hash.h"
25 #include "sane_ctype.h"
26 #include <symbol/kallsyms.h>
28 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
);
30 static void dsos__init(struct dsos
*dsos
)
32 INIT_LIST_HEAD(&dsos
->head
);
34 pthread_rwlock_init(&dsos
->lock
, NULL
);
37 int machine__init(struct machine
*machine
, const char *root_dir
, pid_t pid
)
39 memset(machine
, 0, sizeof(*machine
));
40 map_groups__init(&machine
->kmaps
, machine
);
41 RB_CLEAR_NODE(&machine
->rb_node
);
42 dsos__init(&machine
->dsos
);
44 machine
->threads
= RB_ROOT
;
45 pthread_rwlock_init(&machine
->threads_lock
, NULL
);
46 machine
->nr_threads
= 0;
47 INIT_LIST_HEAD(&machine
->dead_threads
);
48 machine
->last_match
= NULL
;
50 machine
->vdso_info
= NULL
;
55 machine
->id_hdr_size
= 0;
56 machine
->kptr_restrict_warned
= false;
57 machine
->comm_exec
= false;
58 machine
->kernel_start
= 0;
60 memset(machine
->vmlinux_maps
, 0, sizeof(machine
->vmlinux_maps
));
62 machine
->root_dir
= strdup(root_dir
);
63 if (machine
->root_dir
== NULL
)
66 if (pid
!= HOST_KERNEL_ID
) {
67 struct thread
*thread
= machine__findnew_thread(machine
, -1,
74 snprintf(comm
, sizeof(comm
), "[guest/%d]", pid
);
75 thread__set_comm(thread
, comm
, 0);
79 machine
->current_tid
= NULL
;
84 struct machine
*machine__new_host(void)
86 struct machine
*machine
= malloc(sizeof(*machine
));
88 if (machine
!= NULL
) {
89 machine__init(machine
, "", HOST_KERNEL_ID
);
91 if (machine__create_kernel_maps(machine
) < 0)
101 struct machine
*machine__new_kallsyms(void)
103 struct machine
*machine
= machine__new_host();
106 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
107 * functions and data objects.
108 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
109 * ask for not using the kcore parsing code, once this one is fixed
110 * to create a map per module.
112 if (machine
&& __machine__load_kallsyms(machine
, "/proc/kallsyms", MAP__FUNCTION
, true) <= 0) {
113 machine__delete(machine
);
120 static void dsos__purge(struct dsos
*dsos
)
124 pthread_rwlock_wrlock(&dsos
->lock
);
126 list_for_each_entry_safe(pos
, n
, &dsos
->head
, node
) {
127 RB_CLEAR_NODE(&pos
->rb_node
);
129 list_del_init(&pos
->node
);
133 pthread_rwlock_unlock(&dsos
->lock
);
136 static void dsos__exit(struct dsos
*dsos
)
139 pthread_rwlock_destroy(&dsos
->lock
);
142 void machine__delete_threads(struct machine
*machine
)
146 pthread_rwlock_wrlock(&machine
->threads_lock
);
147 nd
= rb_first(&machine
->threads
);
149 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
152 __machine__remove_thread(machine
, t
, false);
154 pthread_rwlock_unlock(&machine
->threads_lock
);
157 void machine__exit(struct machine
*machine
)
159 machine__destroy_kernel_maps(machine
);
160 map_groups__exit(&machine
->kmaps
);
161 dsos__exit(&machine
->dsos
);
162 machine__exit_vdso(machine
);
163 zfree(&machine
->root_dir
);
164 zfree(&machine
->current_tid
);
165 pthread_rwlock_destroy(&machine
->threads_lock
);
168 void machine__delete(struct machine
*machine
)
171 machine__exit(machine
);
176 void machines__init(struct machines
*machines
)
178 machine__init(&machines
->host
, "", HOST_KERNEL_ID
);
179 machines
->guests
= RB_ROOT
;
182 void machines__exit(struct machines
*machines
)
184 machine__exit(&machines
->host
);
188 struct machine
*machines__add(struct machines
*machines
, pid_t pid
,
189 const char *root_dir
)
191 struct rb_node
**p
= &machines
->guests
.rb_node
;
192 struct rb_node
*parent
= NULL
;
193 struct machine
*pos
, *machine
= malloc(sizeof(*machine
));
198 if (machine__init(machine
, root_dir
, pid
) != 0) {
205 pos
= rb_entry(parent
, struct machine
, rb_node
);
212 rb_link_node(&machine
->rb_node
, parent
, p
);
213 rb_insert_color(&machine
->rb_node
, &machines
->guests
);
218 void machines__set_comm_exec(struct machines
*machines
, bool comm_exec
)
222 machines
->host
.comm_exec
= comm_exec
;
224 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
225 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
227 machine
->comm_exec
= comm_exec
;
231 struct machine
*machines__find(struct machines
*machines
, pid_t pid
)
233 struct rb_node
**p
= &machines
->guests
.rb_node
;
234 struct rb_node
*parent
= NULL
;
235 struct machine
*machine
;
236 struct machine
*default_machine
= NULL
;
238 if (pid
== HOST_KERNEL_ID
)
239 return &machines
->host
;
243 machine
= rb_entry(parent
, struct machine
, rb_node
);
244 if (pid
< machine
->pid
)
246 else if (pid
> machine
->pid
)
251 default_machine
= machine
;
254 return default_machine
;
257 struct machine
*machines__findnew(struct machines
*machines
, pid_t pid
)
260 const char *root_dir
= "";
261 struct machine
*machine
= machines__find(machines
, pid
);
263 if (machine
&& (machine
->pid
== pid
))
266 if ((pid
!= HOST_KERNEL_ID
) &&
267 (pid
!= DEFAULT_GUEST_KERNEL_ID
) &&
268 (symbol_conf
.guestmount
)) {
269 sprintf(path
, "%s/%d", symbol_conf
.guestmount
, pid
);
270 if (access(path
, R_OK
)) {
271 static struct strlist
*seen
;
274 seen
= strlist__new(NULL
, NULL
);
276 if (!strlist__has_entry(seen
, path
)) {
277 pr_err("Can't access file %s\n", path
);
278 strlist__add(seen
, path
);
286 machine
= machines__add(machines
, pid
, root_dir
);
291 void machines__process_guests(struct machines
*machines
,
292 machine__process_t process
, void *data
)
296 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
297 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
302 char *machine__mmap_name(struct machine
*machine
, char *bf
, size_t size
)
304 if (machine__is_host(machine
))
305 snprintf(bf
, size
, "[%s]", "kernel.kallsyms");
306 else if (machine__is_default_guest(machine
))
307 snprintf(bf
, size
, "[%s]", "guest.kernel.kallsyms");
309 snprintf(bf
, size
, "[%s.%d]", "guest.kernel.kallsyms",
316 void machines__set_id_hdr_size(struct machines
*machines
, u16 id_hdr_size
)
318 struct rb_node
*node
;
319 struct machine
*machine
;
321 machines
->host
.id_hdr_size
= id_hdr_size
;
323 for (node
= rb_first(&machines
->guests
); node
; node
= rb_next(node
)) {
324 machine
= rb_entry(node
, struct machine
, rb_node
);
325 machine
->id_hdr_size
= id_hdr_size
;
331 static void machine__update_thread_pid(struct machine
*machine
,
332 struct thread
*th
, pid_t pid
)
334 struct thread
*leader
;
336 if (pid
== th
->pid_
|| pid
== -1 || th
->pid_
!= -1)
341 if (th
->pid_
== th
->tid
)
344 leader
= __machine__findnew_thread(machine
, th
->pid_
, th
->pid_
);
349 leader
->mg
= map_groups__new(machine
);
354 if (th
->mg
== leader
->mg
)
359 * Maps are created from MMAP events which provide the pid and
360 * tid. Consequently there never should be any maps on a thread
361 * with an unknown pid. Just print an error if there are.
363 if (!map_groups__empty(th
->mg
))
364 pr_err("Discarding thread maps for %d:%d\n",
366 map_groups__put(th
->mg
);
369 th
->mg
= map_groups__get(leader
->mg
);
374 pr_err("Failed to join map groups for %d:%d\n", th
->pid_
, th
->tid
);
379 * Caller must eventually drop thread->refcnt returned with a successful
380 * lookup/new thread inserted.
382 static struct thread
*____machine__findnew_thread(struct machine
*machine
,
383 pid_t pid
, pid_t tid
,
386 struct rb_node
**p
= &machine
->threads
.rb_node
;
387 struct rb_node
*parent
= NULL
;
391 * Front-end cache - TID lookups come in blocks,
392 * so most of the time we dont have to look up
395 th
= machine
->last_match
;
397 if (th
->tid
== tid
) {
398 machine__update_thread_pid(machine
, th
, pid
);
399 return thread__get(th
);
402 machine
->last_match
= NULL
;
407 th
= rb_entry(parent
, struct thread
, rb_node
);
409 if (th
->tid
== tid
) {
410 machine
->last_match
= th
;
411 machine__update_thread_pid(machine
, th
, pid
);
412 return thread__get(th
);
424 th
= thread__new(pid
, tid
);
426 rb_link_node(&th
->rb_node
, parent
, p
);
427 rb_insert_color(&th
->rb_node
, &machine
->threads
);
430 * We have to initialize map_groups separately
431 * after rb tree is updated.
433 * The reason is that we call machine__findnew_thread
434 * within thread__init_map_groups to find the thread
435 * leader and that would screwed the rb tree.
437 if (thread__init_map_groups(th
, machine
)) {
438 rb_erase_init(&th
->rb_node
, &machine
->threads
);
439 RB_CLEAR_NODE(&th
->rb_node
);
444 * It is now in the rbtree, get a ref
447 machine
->last_match
= th
;
448 ++machine
->nr_threads
;
454 struct thread
*__machine__findnew_thread(struct machine
*machine
, pid_t pid
, pid_t tid
)
456 return ____machine__findnew_thread(machine
, pid
, tid
, true);
459 struct thread
*machine__findnew_thread(struct machine
*machine
, pid_t pid
,
464 pthread_rwlock_wrlock(&machine
->threads_lock
);
465 th
= __machine__findnew_thread(machine
, pid
, tid
);
466 pthread_rwlock_unlock(&machine
->threads_lock
);
470 struct thread
*machine__find_thread(struct machine
*machine
, pid_t pid
,
474 pthread_rwlock_rdlock(&machine
->threads_lock
);
475 th
= ____machine__findnew_thread(machine
, pid
, tid
, false);
476 pthread_rwlock_unlock(&machine
->threads_lock
);
480 struct comm
*machine__thread_exec_comm(struct machine
*machine
,
481 struct thread
*thread
)
483 if (machine
->comm_exec
)
484 return thread__exec_comm(thread
);
486 return thread__comm(thread
);
489 int machine__process_comm_event(struct machine
*machine
, union perf_event
*event
,
490 struct perf_sample
*sample
)
492 struct thread
*thread
= machine__findnew_thread(machine
,
495 bool exec
= event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
;
499 machine
->comm_exec
= true;
502 perf_event__fprintf_comm(event
, stdout
);
504 if (thread
== NULL
||
505 __thread__set_comm(thread
, event
->comm
.comm
, sample
->time
, exec
)) {
506 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
515 int machine__process_namespaces_event(struct machine
*machine __maybe_unused
,
516 union perf_event
*event
,
517 struct perf_sample
*sample __maybe_unused
)
519 struct thread
*thread
= machine__findnew_thread(machine
,
520 event
->namespaces
.pid
,
521 event
->namespaces
.tid
);
524 WARN_ONCE(event
->namespaces
.nr_namespaces
> NR_NAMESPACES
,
525 "\nWARNING: kernel seems to support more namespaces than perf"
526 " tool.\nTry updating the perf tool..\n\n");
528 WARN_ONCE(event
->namespaces
.nr_namespaces
< NR_NAMESPACES
,
529 "\nWARNING: perf tool seems to support more namespaces than"
530 " the kernel.\nTry updating the kernel..\n\n");
533 perf_event__fprintf_namespaces(event
, stdout
);
535 if (thread
== NULL
||
536 thread__set_namespaces(thread
, sample
->time
, &event
->namespaces
)) {
537 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
546 int machine__process_lost_event(struct machine
*machine __maybe_unused
,
547 union perf_event
*event
, struct perf_sample
*sample __maybe_unused
)
549 dump_printf(": id:%" PRIu64
": lost:%" PRIu64
"\n",
550 event
->lost
.id
, event
->lost
.lost
);
554 int machine__process_lost_samples_event(struct machine
*machine __maybe_unused
,
555 union perf_event
*event
, struct perf_sample
*sample
)
557 dump_printf(": id:%" PRIu64
": lost samples :%" PRIu64
"\n",
558 sample
->id
, event
->lost_samples
.lost
);
562 static struct dso
*machine__findnew_module_dso(struct machine
*machine
,
564 const char *filename
)
568 pthread_rwlock_wrlock(&machine
->dsos
.lock
);
570 dso
= __dsos__find(&machine
->dsos
, m
->name
, true);
572 dso
= __dsos__addnew(&machine
->dsos
, m
->name
);
576 dso__set_module_info(dso
, m
, machine
);
577 dso__set_long_name(dso
, strdup(filename
), true);
582 pthread_rwlock_unlock(&machine
->dsos
.lock
);
586 int machine__process_aux_event(struct machine
*machine __maybe_unused
,
587 union perf_event
*event
)
590 perf_event__fprintf_aux(event
, stdout
);
594 int machine__process_itrace_start_event(struct machine
*machine __maybe_unused
,
595 union perf_event
*event
)
598 perf_event__fprintf_itrace_start(event
, stdout
);
602 int machine__process_switch_event(struct machine
*machine __maybe_unused
,
603 union perf_event
*event
)
606 perf_event__fprintf_switch(event
, stdout
);
610 static void dso__adjust_kmod_long_name(struct dso
*dso
, const char *filename
)
612 const char *dup_filename
;
614 if (!filename
|| !dso
|| !dso
->long_name
)
616 if (dso
->long_name
[0] != '[')
618 if (!strchr(filename
, '/'))
621 dup_filename
= strdup(filename
);
625 dso__set_long_name(dso
, dup_filename
, true);
628 struct map
*machine__findnew_module_map(struct machine
*machine
, u64 start
,
629 const char *filename
)
631 struct map
*map
= NULL
;
632 struct dso
*dso
= NULL
;
635 if (kmod_path__parse_name(&m
, filename
))
638 map
= map_groups__find_by_name(&machine
->kmaps
, MAP__FUNCTION
,
642 * If the map's dso is an offline module, give dso__load()
643 * a chance to find the file path of that module by fixing
646 dso__adjust_kmod_long_name(map
->dso
, filename
);
650 dso
= machine__findnew_module_dso(machine
, &m
, filename
);
654 map
= map__new2(start
, dso
, MAP__FUNCTION
);
658 map_groups__insert(&machine
->kmaps
, map
);
660 /* Put the map here because map_groups__insert alread got it */
663 /* put the dso here, corresponding to machine__findnew_module_dso */
669 size_t machines__fprintf_dsos(struct machines
*machines
, FILE *fp
)
672 size_t ret
= __dsos__fprintf(&machines
->host
.dsos
.head
, fp
);
674 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
675 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
676 ret
+= __dsos__fprintf(&pos
->dsos
.head
, fp
);
682 size_t machine__fprintf_dsos_buildid(struct machine
*m
, FILE *fp
,
683 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
685 return __dsos__fprintf_buildid(&m
->dsos
.head
, fp
, skip
, parm
);
688 size_t machines__fprintf_dsos_buildid(struct machines
*machines
, FILE *fp
,
689 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
692 size_t ret
= machine__fprintf_dsos_buildid(&machines
->host
, fp
, skip
, parm
);
694 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
695 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
696 ret
+= machine__fprintf_dsos_buildid(pos
, fp
, skip
, parm
);
701 size_t machine__fprintf_vmlinux_path(struct machine
*machine
, FILE *fp
)
705 struct dso
*kdso
= machine__kernel_map(machine
)->dso
;
707 if (kdso
->has_build_id
) {
708 char filename
[PATH_MAX
];
709 if (dso__build_id_filename(kdso
, filename
, sizeof(filename
),
711 printed
+= fprintf(fp
, "[0] %s\n", filename
);
714 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
)
715 printed
+= fprintf(fp
, "[%d] %s\n",
716 i
+ kdso
->has_build_id
, vmlinux_path
[i
]);
721 size_t machine__fprintf(struct machine
*machine
, FILE *fp
)
726 pthread_rwlock_rdlock(&machine
->threads_lock
);
728 ret
= fprintf(fp
, "Threads: %u\n", machine
->nr_threads
);
730 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
731 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
733 ret
+= thread__fprintf(pos
, fp
);
736 pthread_rwlock_unlock(&machine
->threads_lock
);
741 static struct dso
*machine__get_kernel(struct machine
*machine
)
743 const char *vmlinux_name
= NULL
;
746 if (machine__is_host(machine
)) {
747 vmlinux_name
= symbol_conf
.vmlinux_name
;
749 vmlinux_name
= DSO__NAME_KALLSYMS
;
751 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
752 "[kernel]", DSO_TYPE_KERNEL
);
756 if (machine__is_default_guest(machine
))
757 vmlinux_name
= symbol_conf
.default_guest_vmlinux_name
;
759 vmlinux_name
= machine__mmap_name(machine
, bf
,
762 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
764 DSO_TYPE_GUEST_KERNEL
);
767 if (kernel
!= NULL
&& (!kernel
->has_build_id
))
768 dso__read_running_kernel_build_id(kernel
, machine
);
773 struct process_args
{
777 static void machine__get_kallsyms_filename(struct machine
*machine
, char *buf
,
780 if (machine__is_default_guest(machine
))
781 scnprintf(buf
, bufsz
, "%s", symbol_conf
.default_guest_kallsyms
);
783 scnprintf(buf
, bufsz
, "%s/proc/kallsyms", machine
->root_dir
);
786 const char *ref_reloc_sym_names
[] = {"_text", "_stext", NULL
};
788 /* Figure out the start address of kernel map from /proc/kallsyms.
789 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
790 * symbol_name if it's not that important.
792 static int machine__get_running_kernel_start(struct machine
*machine
,
793 const char **symbol_name
, u64
*start
)
795 char filename
[PATH_MAX
];
800 machine__get_kallsyms_filename(machine
, filename
, PATH_MAX
);
802 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
805 for (i
= 0; (name
= ref_reloc_sym_names
[i
]) != NULL
; i
++) {
806 err
= kallsyms__get_function_start(filename
, name
, &addr
);
821 int __machine__create_kernel_maps(struct machine
*machine
, struct dso
*kernel
)
826 if (machine__get_running_kernel_start(machine
, NULL
, &start
))
829 /* In case of renewal the kernel map, destroy previous one */
830 machine__destroy_kernel_maps(machine
);
832 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
836 machine
->vmlinux_maps
[type
] = map__new2(start
, kernel
, type
);
837 if (machine
->vmlinux_maps
[type
] == NULL
)
840 machine
->vmlinux_maps
[type
]->map_ip
=
841 machine
->vmlinux_maps
[type
]->unmap_ip
=
843 map
= __machine__kernel_map(machine
, type
);
844 kmap
= map__kmap(map
);
848 kmap
->kmaps
= &machine
->kmaps
;
849 map_groups__insert(&machine
->kmaps
, map
);
855 void machine__destroy_kernel_maps(struct machine
*machine
)
859 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
861 struct map
*map
= __machine__kernel_map(machine
, type
);
866 kmap
= map__kmap(map
);
867 map_groups__remove(&machine
->kmaps
, map
);
868 if (kmap
&& kmap
->ref_reloc_sym
) {
870 * ref_reloc_sym is shared among all maps, so free just
873 if (type
== MAP__FUNCTION
) {
874 zfree((char **)&kmap
->ref_reloc_sym
->name
);
875 zfree(&kmap
->ref_reloc_sym
);
877 kmap
->ref_reloc_sym
= NULL
;
880 map__put(machine
->vmlinux_maps
[type
]);
881 machine
->vmlinux_maps
[type
] = NULL
;
885 int machines__create_guest_kernel_maps(struct machines
*machines
)
888 struct dirent
**namelist
= NULL
;
894 if (symbol_conf
.default_guest_vmlinux_name
||
895 symbol_conf
.default_guest_modules
||
896 symbol_conf
.default_guest_kallsyms
) {
897 machines__create_kernel_maps(machines
, DEFAULT_GUEST_KERNEL_ID
);
900 if (symbol_conf
.guestmount
) {
901 items
= scandir(symbol_conf
.guestmount
, &namelist
, NULL
, NULL
);
904 for (i
= 0; i
< items
; i
++) {
905 if (!isdigit(namelist
[i
]->d_name
[0])) {
906 /* Filter out . and .. */
909 pid
= (pid_t
)strtol(namelist
[i
]->d_name
, &endp
, 10);
910 if ((*endp
!= '\0') ||
911 (endp
== namelist
[i
]->d_name
) ||
913 pr_debug("invalid directory (%s). Skipping.\n",
914 namelist
[i
]->d_name
);
917 sprintf(path
, "%s/%s/proc/kallsyms",
918 symbol_conf
.guestmount
,
919 namelist
[i
]->d_name
);
920 ret
= access(path
, R_OK
);
922 pr_debug("Can't access file %s\n", path
);
925 machines__create_kernel_maps(machines
, pid
);
934 void machines__destroy_kernel_maps(struct machines
*machines
)
936 struct rb_node
*next
= rb_first(&machines
->guests
);
938 machine__destroy_kernel_maps(&machines
->host
);
941 struct machine
*pos
= rb_entry(next
, struct machine
, rb_node
);
943 next
= rb_next(&pos
->rb_node
);
944 rb_erase(&pos
->rb_node
, &machines
->guests
);
945 machine__delete(pos
);
949 int machines__create_kernel_maps(struct machines
*machines
, pid_t pid
)
951 struct machine
*machine
= machines__findnew(machines
, pid
);
956 return machine__create_kernel_maps(machine
);
959 int __machine__load_kallsyms(struct machine
*machine
, const char *filename
,
960 enum map_type type
, bool no_kcore
)
962 struct map
*map
= machine__kernel_map(machine
);
963 int ret
= __dso__load_kallsyms(map
->dso
, filename
, map
, no_kcore
);
966 dso__set_loaded(map
->dso
, type
);
968 * Since /proc/kallsyms will have multiple sessions for the
969 * kernel, with modules between them, fixup the end of all
972 __map_groups__fixup_end(&machine
->kmaps
, type
);
978 int machine__load_kallsyms(struct machine
*machine
, const char *filename
,
981 return __machine__load_kallsyms(machine
, filename
, type
, false);
984 int machine__load_vmlinux_path(struct machine
*machine
, enum map_type type
)
986 struct map
*map
= machine__kernel_map(machine
);
987 int ret
= dso__load_vmlinux_path(map
->dso
, map
);
990 dso__set_loaded(map
->dso
, type
);
995 static void map_groups__fixup_end(struct map_groups
*mg
)
998 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
999 __map_groups__fixup_end(mg
, i
);
1002 static char *get_kernel_version(const char *root_dir
)
1004 char version
[PATH_MAX
];
1007 const char *prefix
= "Linux version ";
1009 sprintf(version
, "%s/proc/version", root_dir
);
1010 file
= fopen(version
, "r");
1015 tmp
= fgets(version
, sizeof(version
), file
);
1018 name
= strstr(version
, prefix
);
1021 name
+= strlen(prefix
);
1022 tmp
= strchr(name
, ' ');
1026 return strdup(name
);
1029 static bool is_kmod_dso(struct dso
*dso
)
1031 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
||
1032 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE
;
1035 static int map_groups__set_module_path(struct map_groups
*mg
, const char *path
,
1036 struct kmod_path
*m
)
1041 map
= map_groups__find_by_name(mg
, MAP__FUNCTION
, m
->name
);
1045 long_name
= strdup(path
);
1046 if (long_name
== NULL
)
1049 dso__set_long_name(map
->dso
, long_name
, true);
1050 dso__kernel_module_get_build_id(map
->dso
, "");
1053 * Full name could reveal us kmod compression, so
1054 * we need to update the symtab_type if needed.
1056 if (m
->comp
&& is_kmod_dso(map
->dso
))
1057 map
->dso
->symtab_type
++;
1062 static int map_groups__set_modules_path_dir(struct map_groups
*mg
,
1063 const char *dir_name
, int depth
)
1065 struct dirent
*dent
;
1066 DIR *dir
= opendir(dir_name
);
1070 pr_debug("%s: cannot open %s dir\n", __func__
, dir_name
);
1074 while ((dent
= readdir(dir
)) != NULL
) {
1075 char path
[PATH_MAX
];
1078 /*sshfs might return bad dent->d_type, so we have to stat*/
1079 snprintf(path
, sizeof(path
), "%s/%s", dir_name
, dent
->d_name
);
1080 if (stat(path
, &st
))
1083 if (S_ISDIR(st
.st_mode
)) {
1084 if (!strcmp(dent
->d_name
, ".") ||
1085 !strcmp(dent
->d_name
, ".."))
1088 /* Do not follow top-level source and build symlinks */
1090 if (!strcmp(dent
->d_name
, "source") ||
1091 !strcmp(dent
->d_name
, "build"))
1095 ret
= map_groups__set_modules_path_dir(mg
, path
,
1102 ret
= kmod_path__parse_name(&m
, dent
->d_name
);
1107 ret
= map_groups__set_module_path(mg
, path
, &m
);
1121 static int machine__set_modules_path(struct machine
*machine
)
1124 char modules_path
[PATH_MAX
];
1126 version
= get_kernel_version(machine
->root_dir
);
1130 snprintf(modules_path
, sizeof(modules_path
), "%s/lib/modules/%s",
1131 machine
->root_dir
, version
);
1134 return map_groups__set_modules_path_dir(&machine
->kmaps
, modules_path
, 0);
1136 int __weak
arch__fix_module_text_start(u64
*start __maybe_unused
,
1137 const char *name __maybe_unused
)
1142 static int machine__create_module(void *arg
, const char *name
, u64 start
,
1145 struct machine
*machine
= arg
;
1148 if (arch__fix_module_text_start(&start
, name
) < 0)
1151 map
= machine__findnew_module_map(machine
, start
, name
);
1154 map
->end
= start
+ size
;
1156 dso__kernel_module_get_build_id(map
->dso
, machine
->root_dir
);
1161 static int machine__create_modules(struct machine
*machine
)
1163 const char *modules
;
1164 char path
[PATH_MAX
];
1166 if (machine__is_default_guest(machine
)) {
1167 modules
= symbol_conf
.default_guest_modules
;
1169 snprintf(path
, PATH_MAX
, "%s/proc/modules", machine
->root_dir
);
1173 if (symbol__restricted_filename(modules
, "/proc/modules"))
1176 if (modules__parse(modules
, machine
, machine__create_module
))
1179 if (!machine__set_modules_path(machine
))
1182 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1187 int machine__create_kernel_maps(struct machine
*machine
)
1189 struct dso
*kernel
= machine__get_kernel(machine
);
1190 const char *name
= NULL
;
1197 ret
= __machine__create_kernel_maps(machine
, kernel
);
1202 if (symbol_conf
.use_modules
&& machine__create_modules(machine
) < 0) {
1203 if (machine__is_host(machine
))
1204 pr_debug("Problems creating module maps, "
1205 "continuing anyway...\n");
1207 pr_debug("Problems creating module maps for guest %d, "
1208 "continuing anyway...\n", machine
->pid
);
1212 * Now that we have all the maps created, just set the ->end of them:
1214 map_groups__fixup_end(&machine
->kmaps
);
1216 if (!machine__get_running_kernel_start(machine
, &name
, &addr
)) {
1218 maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
, name
, addr
)) {
1219 machine__destroy_kernel_maps(machine
);
1227 static void machine__set_kernel_mmap_len(struct machine
*machine
,
1228 union perf_event
*event
)
1232 for (i
= 0; i
< MAP__NR_TYPES
; i
++) {
1233 machine
->vmlinux_maps
[i
]->start
= event
->mmap
.start
;
1234 machine
->vmlinux_maps
[i
]->end
= (event
->mmap
.start
+
1237 * Be a bit paranoid here, some perf.data file came with
1238 * a zero sized synthesized MMAP event for the kernel.
1240 if (machine
->vmlinux_maps
[i
]->end
== 0)
1241 machine
->vmlinux_maps
[i
]->end
= ~0ULL;
1245 static bool machine__uses_kcore(struct machine
*machine
)
1249 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1250 if (dso__is_kcore(dso
))
1257 static int machine__process_kernel_mmap_event(struct machine
*machine
,
1258 union perf_event
*event
)
1261 char kmmap_prefix
[PATH_MAX
];
1262 enum dso_kernel_type kernel_type
;
1263 bool is_kernel_mmap
;
1265 /* If we have maps from kcore then we do not need or want any others */
1266 if (machine__uses_kcore(machine
))
1269 machine__mmap_name(machine
, kmmap_prefix
, sizeof(kmmap_prefix
));
1270 if (machine__is_host(machine
))
1271 kernel_type
= DSO_TYPE_KERNEL
;
1273 kernel_type
= DSO_TYPE_GUEST_KERNEL
;
1275 is_kernel_mmap
= memcmp(event
->mmap
.filename
,
1277 strlen(kmmap_prefix
) - 1) == 0;
1278 if (event
->mmap
.filename
[0] == '/' ||
1279 (!is_kernel_mmap
&& event
->mmap
.filename
[0] == '[')) {
1280 map
= machine__findnew_module_map(machine
, event
->mmap
.start
,
1281 event
->mmap
.filename
);
1285 map
->end
= map
->start
+ event
->mmap
.len
;
1286 } else if (is_kernel_mmap
) {
1287 const char *symbol_name
= (event
->mmap
.filename
+
1288 strlen(kmmap_prefix
));
1290 * Should be there already, from the build-id table in
1293 struct dso
*kernel
= NULL
;
1296 pthread_rwlock_rdlock(&machine
->dsos
.lock
);
1298 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1301 * The cpumode passed to is_kernel_module is not the
1302 * cpumode of *this* event. If we insist on passing
1303 * correct cpumode to is_kernel_module, we should
1304 * record the cpumode when we adding this dso to the
1307 * However we don't really need passing correct
1308 * cpumode. We know the correct cpumode must be kernel
1309 * mode (if not, we should not link it onto kernel_dsos
1312 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1313 * is_kernel_module() treats it as a kernel cpumode.
1317 is_kernel_module(dso
->long_name
,
1318 PERF_RECORD_MISC_CPUMODE_UNKNOWN
))
1326 pthread_rwlock_unlock(&machine
->dsos
.lock
);
1329 kernel
= machine__findnew_dso(machine
, kmmap_prefix
);
1333 kernel
->kernel
= kernel_type
;
1334 if (__machine__create_kernel_maps(machine
, kernel
) < 0) {
1339 if (strstr(kernel
->long_name
, "vmlinux"))
1340 dso__set_short_name(kernel
, "[kernel.vmlinux]", false);
1342 machine__set_kernel_mmap_len(machine
, event
);
1345 * Avoid using a zero address (kptr_restrict) for the ref reloc
1346 * symbol. Effectively having zero here means that at record
1347 * time /proc/sys/kernel/kptr_restrict was non zero.
1349 if (event
->mmap
.pgoff
!= 0) {
1350 maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
,
1355 if (machine__is_default_guest(machine
)) {
1357 * preload dso of guest kernel and modules
1359 dso__load(kernel
, machine__kernel_map(machine
));
1367 int machine__process_mmap2_event(struct machine
*machine
,
1368 union perf_event
*event
,
1369 struct perf_sample
*sample
)
1371 struct thread
*thread
;
1377 perf_event__fprintf_mmap2(event
, stdout
);
1379 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1380 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1381 ret
= machine__process_kernel_mmap_event(machine
, event
);
1387 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
,
1392 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1393 type
= MAP__VARIABLE
;
1395 type
= MAP__FUNCTION
;
1397 map
= map__new(machine
, event
->mmap2
.start
,
1398 event
->mmap2
.len
, event
->mmap2
.pgoff
,
1400 event
->mmap2
.min
, event
->mmap2
.ino
,
1401 event
->mmap2
.ino_generation
,
1404 event
->mmap2
.filename
, type
, thread
);
1407 goto out_problem_map
;
1409 ret
= thread__insert_map(thread
, map
);
1411 goto out_problem_insert
;
1413 thread__put(thread
);
1420 thread__put(thread
);
1422 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1426 int machine__process_mmap_event(struct machine
*machine
, union perf_event
*event
,
1427 struct perf_sample
*sample
)
1429 struct thread
*thread
;
1435 perf_event__fprintf_mmap(event
, stdout
);
1437 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1438 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1439 ret
= machine__process_kernel_mmap_event(machine
, event
);
1445 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
,
1450 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1451 type
= MAP__VARIABLE
;
1453 type
= MAP__FUNCTION
;
1455 map
= map__new(machine
, event
->mmap
.start
,
1456 event
->mmap
.len
, event
->mmap
.pgoff
,
1458 event
->mmap
.filename
,
1462 goto out_problem_map
;
1464 ret
= thread__insert_map(thread
, map
);
1466 goto out_problem_insert
;
1468 thread__put(thread
);
1475 thread__put(thread
);
1477 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1481 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
)
1483 if (machine
->last_match
== th
)
1484 machine
->last_match
= NULL
;
1486 BUG_ON(refcount_read(&th
->refcnt
) == 0);
1488 pthread_rwlock_wrlock(&machine
->threads_lock
);
1489 rb_erase_init(&th
->rb_node
, &machine
->threads
);
1490 RB_CLEAR_NODE(&th
->rb_node
);
1491 --machine
->nr_threads
;
1493 * Move it first to the dead_threads list, then drop the reference,
1494 * if this is the last reference, then the thread__delete destructor
1495 * will be called and we will remove it from the dead_threads list.
1497 list_add_tail(&th
->node
, &machine
->dead_threads
);
1499 pthread_rwlock_unlock(&machine
->threads_lock
);
1503 void machine__remove_thread(struct machine
*machine
, struct thread
*th
)
1505 return __machine__remove_thread(machine
, th
, true);
1508 int machine__process_fork_event(struct machine
*machine
, union perf_event
*event
,
1509 struct perf_sample
*sample
)
1511 struct thread
*thread
= machine__find_thread(machine
,
1514 struct thread
*parent
= machine__findnew_thread(machine
,
1520 perf_event__fprintf_task(event
, stdout
);
1523 * There may be an existing thread that is not actually the parent,
1524 * either because we are processing events out of order, or because the
1525 * (fork) event that would have removed the thread was lost. Assume the
1526 * latter case and continue on as best we can.
1528 if (parent
->pid_
!= (pid_t
)event
->fork
.ppid
) {
1529 dump_printf("removing erroneous parent thread %d/%d\n",
1530 parent
->pid_
, parent
->tid
);
1531 machine__remove_thread(machine
, parent
);
1532 thread__put(parent
);
1533 parent
= machine__findnew_thread(machine
, event
->fork
.ppid
,
1537 /* if a thread currently exists for the thread id remove it */
1538 if (thread
!= NULL
) {
1539 machine__remove_thread(machine
, thread
);
1540 thread__put(thread
);
1543 thread
= machine__findnew_thread(machine
, event
->fork
.pid
,
1546 if (thread
== NULL
|| parent
== NULL
||
1547 thread__fork(thread
, parent
, sample
->time
) < 0) {
1548 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1551 thread__put(thread
);
1552 thread__put(parent
);
1557 int machine__process_exit_event(struct machine
*machine
, union perf_event
*event
,
1558 struct perf_sample
*sample __maybe_unused
)
1560 struct thread
*thread
= machine__find_thread(machine
,
1565 perf_event__fprintf_task(event
, stdout
);
1567 if (thread
!= NULL
) {
1568 thread__exited(thread
);
1569 thread__put(thread
);
1575 int machine__process_event(struct machine
*machine
, union perf_event
*event
,
1576 struct perf_sample
*sample
)
1580 switch (event
->header
.type
) {
1581 case PERF_RECORD_COMM
:
1582 ret
= machine__process_comm_event(machine
, event
, sample
); break;
1583 case PERF_RECORD_MMAP
:
1584 ret
= machine__process_mmap_event(machine
, event
, sample
); break;
1585 case PERF_RECORD_NAMESPACES
:
1586 ret
= machine__process_namespaces_event(machine
, event
, sample
); break;
1587 case PERF_RECORD_MMAP2
:
1588 ret
= machine__process_mmap2_event(machine
, event
, sample
); break;
1589 case PERF_RECORD_FORK
:
1590 ret
= machine__process_fork_event(machine
, event
, sample
); break;
1591 case PERF_RECORD_EXIT
:
1592 ret
= machine__process_exit_event(machine
, event
, sample
); break;
1593 case PERF_RECORD_LOST
:
1594 ret
= machine__process_lost_event(machine
, event
, sample
); break;
1595 case PERF_RECORD_AUX
:
1596 ret
= machine__process_aux_event(machine
, event
); break;
1597 case PERF_RECORD_ITRACE_START
:
1598 ret
= machine__process_itrace_start_event(machine
, event
); break;
1599 case PERF_RECORD_LOST_SAMPLES
:
1600 ret
= machine__process_lost_samples_event(machine
, event
, sample
); break;
1601 case PERF_RECORD_SWITCH
:
1602 case PERF_RECORD_SWITCH_CPU_WIDE
:
1603 ret
= machine__process_switch_event(machine
, event
); break;
1612 static bool symbol__match_regex(struct symbol
*sym
, regex_t
*regex
)
1614 if (!regexec(regex
, sym
->name
, 0, NULL
, 0))
1619 static void ip__resolve_ams(struct thread
*thread
,
1620 struct addr_map_symbol
*ams
,
1623 struct addr_location al
;
1625 memset(&al
, 0, sizeof(al
));
1627 * We cannot use the header.misc hint to determine whether a
1628 * branch stack address is user, kernel, guest, hypervisor.
1629 * Branches may straddle the kernel/user/hypervisor boundaries.
1630 * Thus, we have to try consecutively until we find a match
1631 * or else, the symbol is unknown
1633 thread__find_cpumode_addr_location(thread
, MAP__FUNCTION
, ip
, &al
);
1636 ams
->al_addr
= al
.addr
;
1642 static void ip__resolve_data(struct thread
*thread
,
1643 u8 m
, struct addr_map_symbol
*ams
,
1644 u64 addr
, u64 phys_addr
)
1646 struct addr_location al
;
1648 memset(&al
, 0, sizeof(al
));
1650 thread__find_addr_location(thread
, m
, MAP__VARIABLE
, addr
, &al
);
1651 if (al
.map
== NULL
) {
1653 * some shared data regions have execute bit set which puts
1654 * their mapping in the MAP__FUNCTION type array.
1655 * Check there as a fallback option before dropping the sample.
1657 thread__find_addr_location(thread
, m
, MAP__FUNCTION
, addr
, &al
);
1661 ams
->al_addr
= al
.addr
;
1664 ams
->phys_addr
= phys_addr
;
1667 struct mem_info
*sample__resolve_mem(struct perf_sample
*sample
,
1668 struct addr_location
*al
)
1670 struct mem_info
*mi
= zalloc(sizeof(*mi
));
1675 ip__resolve_ams(al
->thread
, &mi
->iaddr
, sample
->ip
);
1676 ip__resolve_data(al
->thread
, al
->cpumode
, &mi
->daddr
,
1677 sample
->addr
, sample
->phys_addr
);
1678 mi
->data_src
.val
= sample
->data_src
;
1688 static int add_callchain_ip(struct thread
*thread
,
1689 struct callchain_cursor
*cursor
,
1690 struct symbol
**parent
,
1691 struct addr_location
*root_al
,
1695 struct branch_flags
*flags
,
1696 struct iterations
*iter
,
1699 struct addr_location al
;
1700 int nr_loop_iter
= 0;
1701 u64 iter_cycles
= 0;
1706 thread__find_cpumode_addr_location(thread
, MAP__FUNCTION
,
1709 if (ip
>= PERF_CONTEXT_MAX
) {
1711 case PERF_CONTEXT_HV
:
1712 *cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1714 case PERF_CONTEXT_KERNEL
:
1715 *cpumode
= PERF_RECORD_MISC_KERNEL
;
1717 case PERF_CONTEXT_USER
:
1718 *cpumode
= PERF_RECORD_MISC_USER
;
1721 pr_debug("invalid callchain context: "
1722 "%"PRId64
"\n", (s64
) ip
);
1724 * It seems the callchain is corrupted.
1727 callchain_cursor_reset(cursor
);
1732 thread__find_addr_location(thread
, *cpumode
, MAP__FUNCTION
,
1736 if (al
.sym
!= NULL
) {
1737 if (perf_hpp_list
.parent
&& !*parent
&&
1738 symbol__match_regex(al
.sym
, &parent_regex
))
1740 else if (have_ignore_callees
&& root_al
&&
1741 symbol__match_regex(al
.sym
, &ignore_callees_regex
)) {
1742 /* Treat this symbol as the root,
1743 forgetting its callees. */
1745 callchain_cursor_reset(cursor
);
1749 if (symbol_conf
.hide_unresolved
&& al
.sym
== NULL
)
1753 nr_loop_iter
= iter
->nr_loop_iter
;
1754 iter_cycles
= iter
->cycles
;
1757 return callchain_cursor_append(cursor
, al
.addr
, al
.map
, al
.sym
,
1758 branch
, flags
, nr_loop_iter
,
1759 iter_cycles
, branch_from
);
1762 struct branch_info
*sample__resolve_bstack(struct perf_sample
*sample
,
1763 struct addr_location
*al
)
1766 const struct branch_stack
*bs
= sample
->branch_stack
;
1767 struct branch_info
*bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
1772 for (i
= 0; i
< bs
->nr
; i
++) {
1773 ip__resolve_ams(al
->thread
, &bi
[i
].to
, bs
->entries
[i
].to
);
1774 ip__resolve_ams(al
->thread
, &bi
[i
].from
, bs
->entries
[i
].from
);
1775 bi
[i
].flags
= bs
->entries
[i
].flags
;
1780 static void save_iterations(struct iterations
*iter
,
1781 struct branch_entry
*be
, int nr
)
1785 iter
->nr_loop_iter
= nr
;
1788 for (i
= 0; i
< nr
; i
++)
1789 iter
->cycles
+= be
[i
].flags
.cycles
;
1794 #define NO_ENTRY 0xff
1796 #define PERF_MAX_BRANCH_DEPTH 127
1799 static int remove_loops(struct branch_entry
*l
, int nr
,
1800 struct iterations
*iter
)
1803 unsigned char chash
[CHASHSZ
];
1805 memset(chash
, NO_ENTRY
, sizeof(chash
));
1807 BUG_ON(PERF_MAX_BRANCH_DEPTH
> 255);
1809 for (i
= 0; i
< nr
; i
++) {
1810 int h
= hash_64(l
[i
].from
, CHASHBITS
) % CHASHSZ
;
1812 /* no collision handling for now */
1813 if (chash
[h
] == NO_ENTRY
) {
1815 } else if (l
[chash
[h
]].from
== l
[i
].from
) {
1816 bool is_loop
= true;
1817 /* check if it is a real loop */
1819 for (j
= chash
[h
]; j
< i
&& i
+ off
< nr
; j
++, off
++)
1820 if (l
[j
].from
!= l
[i
+ off
].from
) {
1827 save_iterations(iter
+ i
+ off
,
1830 memmove(iter
+ i
, iter
+ i
+ off
,
1833 memmove(l
+ i
, l
+ i
+ off
,
1845 * Recolve LBR callstack chain sample
1847 * 1 on success get LBR callchain information
1848 * 0 no available LBR callchain information, should try fp
1849 * negative error code on other errors.
1851 static int resolve_lbr_callchain_sample(struct thread
*thread
,
1852 struct callchain_cursor
*cursor
,
1853 struct perf_sample
*sample
,
1854 struct symbol
**parent
,
1855 struct addr_location
*root_al
,
1858 struct ip_callchain
*chain
= sample
->callchain
;
1859 int chain_nr
= min(max_stack
, (int)chain
->nr
), i
;
1860 u8 cpumode
= PERF_RECORD_MISC_USER
;
1861 u64 ip
, branch_from
= 0;
1863 for (i
= 0; i
< chain_nr
; i
++) {
1864 if (chain
->ips
[i
] == PERF_CONTEXT_USER
)
1868 /* LBR only affects the user callchain */
1869 if (i
!= chain_nr
) {
1870 struct branch_stack
*lbr_stack
= sample
->branch_stack
;
1871 int lbr_nr
= lbr_stack
->nr
, j
, k
;
1873 struct branch_flags
*flags
;
1875 * LBR callstack can only get user call chain.
1876 * The mix_chain_nr is kernel call chain
1877 * number plus LBR user call chain number.
1878 * i is kernel call chain number,
1879 * 1 is PERF_CONTEXT_USER,
1880 * lbr_nr + 1 is the user call chain number.
1881 * For details, please refer to the comments
1882 * in callchain__printf
1884 int mix_chain_nr
= i
+ 1 + lbr_nr
+ 1;
1886 for (j
= 0; j
< mix_chain_nr
; j
++) {
1891 if (callchain_param
.order
== ORDER_CALLEE
) {
1894 else if (j
> i
+ 1) {
1896 ip
= lbr_stack
->entries
[k
].from
;
1898 flags
= &lbr_stack
->entries
[k
].flags
;
1900 ip
= lbr_stack
->entries
[0].to
;
1902 flags
= &lbr_stack
->entries
[0].flags
;
1904 lbr_stack
->entries
[0].from
;
1909 ip
= lbr_stack
->entries
[k
].from
;
1911 flags
= &lbr_stack
->entries
[k
].flags
;
1913 else if (j
> lbr_nr
)
1914 ip
= chain
->ips
[i
+ 1 - (j
- lbr_nr
)];
1916 ip
= lbr_stack
->entries
[0].to
;
1918 flags
= &lbr_stack
->entries
[0].flags
;
1920 lbr_stack
->entries
[0].from
;
1924 err
= add_callchain_ip(thread
, cursor
, parent
,
1925 root_al
, &cpumode
, ip
,
1926 branch
, flags
, NULL
,
1929 return (err
< 0) ? err
: 0;
1937 static int thread__resolve_callchain_sample(struct thread
*thread
,
1938 struct callchain_cursor
*cursor
,
1939 struct perf_evsel
*evsel
,
1940 struct perf_sample
*sample
,
1941 struct symbol
**parent
,
1942 struct addr_location
*root_al
,
1945 struct branch_stack
*branch
= sample
->branch_stack
;
1946 struct ip_callchain
*chain
= sample
->callchain
;
1948 u8 cpumode
= PERF_RECORD_MISC_USER
;
1949 int i
, j
, err
, nr_entries
;
1954 chain_nr
= chain
->nr
;
1956 if (perf_evsel__has_branch_callstack(evsel
)) {
1957 err
= resolve_lbr_callchain_sample(thread
, cursor
, sample
, parent
,
1958 root_al
, max_stack
);
1960 return (err
< 0) ? err
: 0;
1964 * Based on DWARF debug information, some architectures skip
1965 * a callchain entry saved by the kernel.
1967 skip_idx
= arch_skip_callchain_idx(thread
, chain
);
1970 * Add branches to call stack for easier browsing. This gives
1971 * more context for a sample than just the callers.
1973 * This uses individual histograms of paths compared to the
1974 * aggregated histograms the normal LBR mode uses.
1976 * Limitations for now:
1977 * - No extra filters
1978 * - No annotations (should annotate somehow)
1981 if (branch
&& callchain_param
.branch_callstack
) {
1982 int nr
= min(max_stack
, (int)branch
->nr
);
1983 struct branch_entry be
[nr
];
1984 struct iterations iter
[nr
];
1986 if (branch
->nr
> PERF_MAX_BRANCH_DEPTH
) {
1987 pr_warning("corrupted branch chain. skipping...\n");
1991 for (i
= 0; i
< nr
; i
++) {
1992 if (callchain_param
.order
== ORDER_CALLEE
) {
1993 be
[i
] = branch
->entries
[i
];
1999 * Check for overlap into the callchain.
2000 * The return address is one off compared to
2001 * the branch entry. To adjust for this
2002 * assume the calling instruction is not longer
2005 if (i
== skip_idx
||
2006 chain
->ips
[first_call
] >= PERF_CONTEXT_MAX
)
2008 else if (be
[i
].from
< chain
->ips
[first_call
] &&
2009 be
[i
].from
>= chain
->ips
[first_call
] - 8)
2012 be
[i
] = branch
->entries
[branch
->nr
- i
- 1];
2015 memset(iter
, 0, sizeof(struct iterations
) * nr
);
2016 nr
= remove_loops(be
, nr
, iter
);
2018 for (i
= 0; i
< nr
; i
++) {
2019 err
= add_callchain_ip(thread
, cursor
, parent
,
2026 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
,
2043 for (i
= first_call
, nr_entries
= 0;
2044 i
< chain_nr
&& nr_entries
< max_stack
; i
++) {
2047 if (callchain_param
.order
== ORDER_CALLEE
)
2050 j
= chain
->nr
- i
- 1;
2052 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2058 if (ip
< PERF_CONTEXT_MAX
)
2061 err
= add_callchain_ip(thread
, cursor
, parent
,
2062 root_al
, &cpumode
, ip
,
2063 false, NULL
, NULL
, 0);
2066 return (err
< 0) ? err
: 0;
2072 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
2074 struct callchain_cursor
*cursor
= arg
;
2076 if (symbol_conf
.hide_unresolved
&& entry
->sym
== NULL
)
2078 return callchain_cursor_append(cursor
, entry
->ip
,
2079 entry
->map
, entry
->sym
,
2080 false, NULL
, 0, 0, 0);
2083 static int thread__resolve_callchain_unwind(struct thread
*thread
,
2084 struct callchain_cursor
*cursor
,
2085 struct perf_evsel
*evsel
,
2086 struct perf_sample
*sample
,
2089 /* Can we do dwarf post unwind? */
2090 if (!((evsel
->attr
.sample_type
& PERF_SAMPLE_REGS_USER
) &&
2091 (evsel
->attr
.sample_type
& PERF_SAMPLE_STACK_USER
)))
2094 /* Bail out if nothing was captured. */
2095 if ((!sample
->user_regs
.regs
) ||
2096 (!sample
->user_stack
.size
))
2099 return unwind__get_entries(unwind_entry
, cursor
,
2100 thread
, sample
, max_stack
);
2103 int thread__resolve_callchain(struct thread
*thread
,
2104 struct callchain_cursor
*cursor
,
2105 struct perf_evsel
*evsel
,
2106 struct perf_sample
*sample
,
2107 struct symbol
**parent
,
2108 struct addr_location
*root_al
,
2113 callchain_cursor_reset(&callchain_cursor
);
2115 if (callchain_param
.order
== ORDER_CALLEE
) {
2116 ret
= thread__resolve_callchain_sample(thread
, cursor
,
2122 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
2126 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
2131 ret
= thread__resolve_callchain_sample(thread
, cursor
,
2140 int machine__for_each_thread(struct machine
*machine
,
2141 int (*fn
)(struct thread
*thread
, void *p
),
2145 struct thread
*thread
;
2148 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
2149 thread
= rb_entry(nd
, struct thread
, rb_node
);
2150 rc
= fn(thread
, priv
);
2155 list_for_each_entry(thread
, &machine
->dead_threads
, node
) {
2156 rc
= fn(thread
, priv
);
2163 int machines__for_each_thread(struct machines
*machines
,
2164 int (*fn
)(struct thread
*thread
, void *p
),
2170 rc
= machine__for_each_thread(&machines
->host
, fn
, priv
);
2174 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
2175 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
2177 rc
= machine__for_each_thread(machine
, fn
, priv
);
2184 int __machine__synthesize_threads(struct machine
*machine
, struct perf_tool
*tool
,
2185 struct target
*target
, struct thread_map
*threads
,
2186 perf_event__handler_t process
, bool data_mmap
,
2187 unsigned int proc_map_timeout
)
2189 if (target__has_task(target
))
2190 return perf_event__synthesize_thread_map(tool
, threads
, process
, machine
, data_mmap
, proc_map_timeout
);
2191 else if (target__has_cpu(target
))
2192 return perf_event__synthesize_threads(tool
, process
, machine
, data_mmap
, proc_map_timeout
);
2193 /* command specified */
2197 pid_t
machine__get_current_tid(struct machine
*machine
, int cpu
)
2199 if (cpu
< 0 || cpu
>= MAX_NR_CPUS
|| !machine
->current_tid
)
2202 return machine
->current_tid
[cpu
];
2205 int machine__set_current_tid(struct machine
*machine
, int cpu
, pid_t pid
,
2208 struct thread
*thread
;
2213 if (!machine
->current_tid
) {
2216 machine
->current_tid
= calloc(MAX_NR_CPUS
, sizeof(pid_t
));
2217 if (!machine
->current_tid
)
2219 for (i
= 0; i
< MAX_NR_CPUS
; i
++)
2220 machine
->current_tid
[i
] = -1;
2223 if (cpu
>= MAX_NR_CPUS
) {
2224 pr_err("Requested CPU %d too large. ", cpu
);
2225 pr_err("Consider raising MAX_NR_CPUS\n");
2229 machine
->current_tid
[cpu
] = tid
;
2231 thread
= machine__findnew_thread(machine
, pid
, tid
);
2236 thread__put(thread
);
2241 int machine__get_kernel_start(struct machine
*machine
)
2243 struct map
*map
= machine__kernel_map(machine
);
2247 * The only addresses above 2^63 are kernel addresses of a 64-bit
2248 * kernel. Note that addresses are unsigned so that on a 32-bit system
2249 * all addresses including kernel addresses are less than 2^32. In
2250 * that case (32-bit system), if the kernel mapping is unknown, all
2251 * addresses will be assumed to be in user space - see
2252 * machine__kernel_ip().
2254 machine
->kernel_start
= 1ULL << 63;
2256 err
= map__load(map
);
2258 machine
->kernel_start
= map
->start
;
2263 struct dso
*machine__findnew_dso(struct machine
*machine
, const char *filename
)
2265 return dsos__findnew(&machine
->dsos
, filename
);
2268 char *machine__resolve_kernel_addr(void *vmachine
, unsigned long long *addrp
, char **modp
)
2270 struct machine
*machine
= vmachine
;
2272 struct symbol
*sym
= map_groups__find_symbol(&machine
->kmaps
, MAP__FUNCTION
, *addrp
, &map
);
2277 *modp
= __map__is_kmodule(map
) ? (char *)map
->dso
->short_name
: NULL
;
2278 *addrp
= map
->unmap_ip(map
, sym
->start
);