13 #include <symbol/kallsyms.h>
15 #include "linux/hash.h"
17 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
);
19 static void dsos__init(struct dsos
*dsos
)
21 INIT_LIST_HEAD(&dsos
->head
);
23 pthread_rwlock_init(&dsos
->lock
, NULL
);
26 int machine__init(struct machine
*machine
, const char *root_dir
, pid_t pid
)
28 memset(machine
, 0, sizeof(*machine
));
29 map_groups__init(&machine
->kmaps
, machine
);
30 RB_CLEAR_NODE(&machine
->rb_node
);
31 dsos__init(&machine
->dsos
);
33 machine
->threads
= RB_ROOT
;
34 pthread_rwlock_init(&machine
->threads_lock
, NULL
);
35 machine
->nr_threads
= 0;
36 INIT_LIST_HEAD(&machine
->dead_threads
);
37 machine
->last_match
= NULL
;
39 machine
->vdso_info
= NULL
;
44 machine
->symbol_filter
= NULL
;
45 machine
->id_hdr_size
= 0;
46 machine
->kptr_restrict_warned
= false;
47 machine
->comm_exec
= false;
48 machine
->kernel_start
= 0;
50 memset(machine
->vmlinux_maps
, 0, sizeof(machine
->vmlinux_maps
));
52 machine
->root_dir
= strdup(root_dir
);
53 if (machine
->root_dir
== NULL
)
56 if (pid
!= HOST_KERNEL_ID
) {
57 struct thread
*thread
= machine__findnew_thread(machine
, -1,
64 snprintf(comm
, sizeof(comm
), "[guest/%d]", pid
);
65 thread__set_comm(thread
, comm
, 0);
69 machine
->current_tid
= NULL
;
74 struct machine
*machine__new_host(void)
76 struct machine
*machine
= malloc(sizeof(*machine
));
78 if (machine
!= NULL
) {
79 machine__init(machine
, "", HOST_KERNEL_ID
);
81 if (machine__create_kernel_maps(machine
) < 0)
91 static void dsos__purge(struct dsos
*dsos
)
95 pthread_rwlock_wrlock(&dsos
->lock
);
97 list_for_each_entry_safe(pos
, n
, &dsos
->head
, node
) {
98 RB_CLEAR_NODE(&pos
->rb_node
);
100 list_del_init(&pos
->node
);
104 pthread_rwlock_unlock(&dsos
->lock
);
107 static void dsos__exit(struct dsos
*dsos
)
110 pthread_rwlock_destroy(&dsos
->lock
);
113 void machine__delete_threads(struct machine
*machine
)
117 pthread_rwlock_wrlock(&machine
->threads_lock
);
118 nd
= rb_first(&machine
->threads
);
120 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
123 __machine__remove_thread(machine
, t
, false);
125 pthread_rwlock_unlock(&machine
->threads_lock
);
128 void machine__exit(struct machine
*machine
)
130 machine__destroy_kernel_maps(machine
);
131 map_groups__exit(&machine
->kmaps
);
132 dsos__exit(&machine
->dsos
);
133 machine__exit_vdso(machine
);
134 zfree(&machine
->root_dir
);
135 zfree(&machine
->current_tid
);
136 pthread_rwlock_destroy(&machine
->threads_lock
);
139 void machine__delete(struct machine
*machine
)
142 machine__exit(machine
);
147 void machines__init(struct machines
*machines
)
149 machine__init(&machines
->host
, "", HOST_KERNEL_ID
);
150 machines
->guests
= RB_ROOT
;
151 machines
->symbol_filter
= NULL
;
154 void machines__exit(struct machines
*machines
)
156 machine__exit(&machines
->host
);
160 struct machine
*machines__add(struct machines
*machines
, pid_t pid
,
161 const char *root_dir
)
163 struct rb_node
**p
= &machines
->guests
.rb_node
;
164 struct rb_node
*parent
= NULL
;
165 struct machine
*pos
, *machine
= malloc(sizeof(*machine
));
170 if (machine__init(machine
, root_dir
, pid
) != 0) {
175 machine
->symbol_filter
= machines
->symbol_filter
;
179 pos
= rb_entry(parent
, struct machine
, rb_node
);
186 rb_link_node(&machine
->rb_node
, parent
, p
);
187 rb_insert_color(&machine
->rb_node
, &machines
->guests
);
192 void machines__set_symbol_filter(struct machines
*machines
,
193 symbol_filter_t symbol_filter
)
197 machines
->symbol_filter
= symbol_filter
;
198 machines
->host
.symbol_filter
= symbol_filter
;
200 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
201 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
203 machine
->symbol_filter
= symbol_filter
;
207 void machines__set_comm_exec(struct machines
*machines
, bool comm_exec
)
211 machines
->host
.comm_exec
= comm_exec
;
213 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
214 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
216 machine
->comm_exec
= comm_exec
;
220 struct machine
*machines__find(struct machines
*machines
, pid_t pid
)
222 struct rb_node
**p
= &machines
->guests
.rb_node
;
223 struct rb_node
*parent
= NULL
;
224 struct machine
*machine
;
225 struct machine
*default_machine
= NULL
;
227 if (pid
== HOST_KERNEL_ID
)
228 return &machines
->host
;
232 machine
= rb_entry(parent
, struct machine
, rb_node
);
233 if (pid
< machine
->pid
)
235 else if (pid
> machine
->pid
)
240 default_machine
= machine
;
243 return default_machine
;
246 struct machine
*machines__findnew(struct machines
*machines
, pid_t pid
)
249 const char *root_dir
= "";
250 struct machine
*machine
= machines__find(machines
, pid
);
252 if (machine
&& (machine
->pid
== pid
))
255 if ((pid
!= HOST_KERNEL_ID
) &&
256 (pid
!= DEFAULT_GUEST_KERNEL_ID
) &&
257 (symbol_conf
.guestmount
)) {
258 sprintf(path
, "%s/%d", symbol_conf
.guestmount
, pid
);
259 if (access(path
, R_OK
)) {
260 static struct strlist
*seen
;
263 seen
= strlist__new(NULL
, NULL
);
265 if (!strlist__has_entry(seen
, path
)) {
266 pr_err("Can't access file %s\n", path
);
267 strlist__add(seen
, path
);
275 machine
= machines__add(machines
, pid
, root_dir
);
280 void machines__process_guests(struct machines
*machines
,
281 machine__process_t process
, void *data
)
285 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
286 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
291 char *machine__mmap_name(struct machine
*machine
, char *bf
, size_t size
)
293 if (machine__is_host(machine
))
294 snprintf(bf
, size
, "[%s]", "kernel.kallsyms");
295 else if (machine__is_default_guest(machine
))
296 snprintf(bf
, size
, "[%s]", "guest.kernel.kallsyms");
298 snprintf(bf
, size
, "[%s.%d]", "guest.kernel.kallsyms",
305 void machines__set_id_hdr_size(struct machines
*machines
, u16 id_hdr_size
)
307 struct rb_node
*node
;
308 struct machine
*machine
;
310 machines
->host
.id_hdr_size
= id_hdr_size
;
312 for (node
= rb_first(&machines
->guests
); node
; node
= rb_next(node
)) {
313 machine
= rb_entry(node
, struct machine
, rb_node
);
314 machine
->id_hdr_size
= id_hdr_size
;
320 static void machine__update_thread_pid(struct machine
*machine
,
321 struct thread
*th
, pid_t pid
)
323 struct thread
*leader
;
325 if (pid
== th
->pid_
|| pid
== -1 || th
->pid_
!= -1)
330 if (th
->pid_
== th
->tid
)
333 leader
= __machine__findnew_thread(machine
, th
->pid_
, th
->pid_
);
338 leader
->mg
= map_groups__new(machine
);
343 if (th
->mg
== leader
->mg
)
348 * Maps are created from MMAP events which provide the pid and
349 * tid. Consequently there never should be any maps on a thread
350 * with an unknown pid. Just print an error if there are.
352 if (!map_groups__empty(th
->mg
))
353 pr_err("Discarding thread maps for %d:%d\n",
355 map_groups__put(th
->mg
);
358 th
->mg
= map_groups__get(leader
->mg
);
363 pr_err("Failed to join map groups for %d:%d\n", th
->pid_
, th
->tid
);
368 * Caller must eventually drop thread->refcnt returned with a successful
369 * lookup/new thread inserted.
371 static struct thread
*____machine__findnew_thread(struct machine
*machine
,
372 pid_t pid
, pid_t tid
,
375 struct rb_node
**p
= &machine
->threads
.rb_node
;
376 struct rb_node
*parent
= NULL
;
380 * Front-end cache - TID lookups come in blocks,
381 * so most of the time we dont have to look up
384 th
= machine
->last_match
;
386 if (th
->tid
== tid
) {
387 machine__update_thread_pid(machine
, th
, pid
);
388 return thread__get(th
);
391 machine
->last_match
= NULL
;
396 th
= rb_entry(parent
, struct thread
, rb_node
);
398 if (th
->tid
== tid
) {
399 machine
->last_match
= th
;
400 machine__update_thread_pid(machine
, th
, pid
);
401 return thread__get(th
);
413 th
= thread__new(pid
, tid
);
415 rb_link_node(&th
->rb_node
, parent
, p
);
416 rb_insert_color(&th
->rb_node
, &machine
->threads
);
419 * We have to initialize map_groups separately
420 * after rb tree is updated.
422 * The reason is that we call machine__findnew_thread
423 * within thread__init_map_groups to find the thread
424 * leader and that would screwed the rb tree.
426 if (thread__init_map_groups(th
, machine
)) {
427 rb_erase_init(&th
->rb_node
, &machine
->threads
);
428 RB_CLEAR_NODE(&th
->rb_node
);
433 * It is now in the rbtree, get a ref
436 machine
->last_match
= th
;
437 ++machine
->nr_threads
;
443 struct thread
*__machine__findnew_thread(struct machine
*machine
, pid_t pid
, pid_t tid
)
445 return ____machine__findnew_thread(machine
, pid
, tid
, true);
448 struct thread
*machine__findnew_thread(struct machine
*machine
, pid_t pid
,
453 pthread_rwlock_wrlock(&machine
->threads_lock
);
454 th
= __machine__findnew_thread(machine
, pid
, tid
);
455 pthread_rwlock_unlock(&machine
->threads_lock
);
459 struct thread
*machine__find_thread(struct machine
*machine
, pid_t pid
,
463 pthread_rwlock_rdlock(&machine
->threads_lock
);
464 th
= ____machine__findnew_thread(machine
, pid
, tid
, false);
465 pthread_rwlock_unlock(&machine
->threads_lock
);
469 struct comm
*machine__thread_exec_comm(struct machine
*machine
,
470 struct thread
*thread
)
472 if (machine
->comm_exec
)
473 return thread__exec_comm(thread
);
475 return thread__comm(thread
);
478 int machine__process_comm_event(struct machine
*machine
, union perf_event
*event
,
479 struct perf_sample
*sample
)
481 struct thread
*thread
= machine__findnew_thread(machine
,
484 bool exec
= event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
;
488 machine
->comm_exec
= true;
491 perf_event__fprintf_comm(event
, stdout
);
493 if (thread
== NULL
||
494 __thread__set_comm(thread
, event
->comm
.comm
, sample
->time
, exec
)) {
495 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
504 int machine__process_lost_event(struct machine
*machine __maybe_unused
,
505 union perf_event
*event
, struct perf_sample
*sample __maybe_unused
)
507 dump_printf(": id:%" PRIu64
": lost:%" PRIu64
"\n",
508 event
->lost
.id
, event
->lost
.lost
);
512 int machine__process_lost_samples_event(struct machine
*machine __maybe_unused
,
513 union perf_event
*event
, struct perf_sample
*sample
)
515 dump_printf(": id:%" PRIu64
": lost samples :%" PRIu64
"\n",
516 sample
->id
, event
->lost_samples
.lost
);
520 static struct dso
*machine__findnew_module_dso(struct machine
*machine
,
522 const char *filename
)
526 pthread_rwlock_wrlock(&machine
->dsos
.lock
);
528 dso
= __dsos__find(&machine
->dsos
, m
->name
, true);
530 dso
= __dsos__addnew(&machine
->dsos
, m
->name
);
534 if (machine__is_host(machine
))
535 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
537 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
539 /* _KMODULE_COMP should be next to _KMODULE */
540 if (m
->kmod
&& m
->comp
)
543 dso__set_short_name(dso
, strdup(m
->name
), true);
544 dso__set_long_name(dso
, strdup(filename
), true);
549 pthread_rwlock_unlock(&machine
->dsos
.lock
);
553 int machine__process_aux_event(struct machine
*machine __maybe_unused
,
554 union perf_event
*event
)
557 perf_event__fprintf_aux(event
, stdout
);
561 int machine__process_itrace_start_event(struct machine
*machine __maybe_unused
,
562 union perf_event
*event
)
565 perf_event__fprintf_itrace_start(event
, stdout
);
569 int machine__process_switch_event(struct machine
*machine __maybe_unused
,
570 union perf_event
*event
)
573 perf_event__fprintf_switch(event
, stdout
);
577 static void dso__adjust_kmod_long_name(struct dso
*dso
, const char *filename
)
579 const char *dup_filename
;
581 if (!filename
|| !dso
|| !dso
->long_name
)
583 if (dso
->long_name
[0] != '[')
585 if (!strchr(filename
, '/'))
588 dup_filename
= strdup(filename
);
592 dso__set_long_name(dso
, dup_filename
, true);
595 struct map
*machine__findnew_module_map(struct machine
*machine
, u64 start
,
596 const char *filename
)
598 struct map
*map
= NULL
;
599 struct dso
*dso
= NULL
;
602 if (kmod_path__parse_name(&m
, filename
))
605 map
= map_groups__find_by_name(&machine
->kmaps
, MAP__FUNCTION
,
609 * If the map's dso is an offline module, give dso__load()
610 * a chance to find the file path of that module by fixing
613 dso__adjust_kmod_long_name(map
->dso
, filename
);
617 dso
= machine__findnew_module_dso(machine
, &m
, filename
);
621 map
= map__new2(start
, dso
, MAP__FUNCTION
);
625 map_groups__insert(&machine
->kmaps
, map
);
627 /* Put the map here because map_groups__insert alread got it */
630 /* put the dso here, corresponding to machine__findnew_module_dso */
636 size_t machines__fprintf_dsos(struct machines
*machines
, FILE *fp
)
639 size_t ret
= __dsos__fprintf(&machines
->host
.dsos
.head
, fp
);
641 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
642 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
643 ret
+= __dsos__fprintf(&pos
->dsos
.head
, fp
);
649 size_t machine__fprintf_dsos_buildid(struct machine
*m
, FILE *fp
,
650 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
652 return __dsos__fprintf_buildid(&m
->dsos
.head
, fp
, skip
, parm
);
655 size_t machines__fprintf_dsos_buildid(struct machines
*machines
, FILE *fp
,
656 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
659 size_t ret
= machine__fprintf_dsos_buildid(&machines
->host
, fp
, skip
, parm
);
661 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
662 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
663 ret
+= machine__fprintf_dsos_buildid(pos
, fp
, skip
, parm
);
668 size_t machine__fprintf_vmlinux_path(struct machine
*machine
, FILE *fp
)
672 struct dso
*kdso
= machine__kernel_map(machine
)->dso
;
674 if (kdso
->has_build_id
) {
675 char filename
[PATH_MAX
];
676 if (dso__build_id_filename(kdso
, filename
, sizeof(filename
)))
677 printed
+= fprintf(fp
, "[0] %s\n", filename
);
680 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
)
681 printed
+= fprintf(fp
, "[%d] %s\n",
682 i
+ kdso
->has_build_id
, vmlinux_path
[i
]);
687 size_t machine__fprintf(struct machine
*machine
, FILE *fp
)
692 pthread_rwlock_rdlock(&machine
->threads_lock
);
694 ret
= fprintf(fp
, "Threads: %u\n", machine
->nr_threads
);
696 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
697 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
699 ret
+= thread__fprintf(pos
, fp
);
702 pthread_rwlock_unlock(&machine
->threads_lock
);
707 static struct dso
*machine__get_kernel(struct machine
*machine
)
709 const char *vmlinux_name
= NULL
;
712 if (machine__is_host(machine
)) {
713 vmlinux_name
= symbol_conf
.vmlinux_name
;
715 vmlinux_name
= DSO__NAME_KALLSYMS
;
717 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
718 "[kernel]", DSO_TYPE_KERNEL
);
722 if (machine__is_default_guest(machine
))
723 vmlinux_name
= symbol_conf
.default_guest_vmlinux_name
;
725 vmlinux_name
= machine__mmap_name(machine
, bf
,
728 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
730 DSO_TYPE_GUEST_KERNEL
);
733 if (kernel
!= NULL
&& (!kernel
->has_build_id
))
734 dso__read_running_kernel_build_id(kernel
, machine
);
739 struct process_args
{
743 static void machine__get_kallsyms_filename(struct machine
*machine
, char *buf
,
746 if (machine__is_default_guest(machine
))
747 scnprintf(buf
, bufsz
, "%s", symbol_conf
.default_guest_kallsyms
);
749 scnprintf(buf
, bufsz
, "%s/proc/kallsyms", machine
->root_dir
);
752 const char *ref_reloc_sym_names
[] = {"_text", "_stext", NULL
};
754 /* Figure out the start address of kernel map from /proc/kallsyms.
755 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
756 * symbol_name if it's not that important.
758 static u64
machine__get_running_kernel_start(struct machine
*machine
,
759 const char **symbol_name
)
761 char filename
[PATH_MAX
];
766 machine__get_kallsyms_filename(machine
, filename
, PATH_MAX
);
768 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
771 for (i
= 0; (name
= ref_reloc_sym_names
[i
]) != NULL
; i
++) {
772 addr
= kallsyms__get_function_start(filename
, name
);
783 int __machine__create_kernel_maps(struct machine
*machine
, struct dso
*kernel
)
786 u64 start
= machine__get_running_kernel_start(machine
, NULL
);
788 /* In case of renewal the kernel map, destroy previous one */
789 machine__destroy_kernel_maps(machine
);
791 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
795 machine
->vmlinux_maps
[type
] = map__new2(start
, kernel
, type
);
796 if (machine
->vmlinux_maps
[type
] == NULL
)
799 machine
->vmlinux_maps
[type
]->map_ip
=
800 machine
->vmlinux_maps
[type
]->unmap_ip
=
802 map
= __machine__kernel_map(machine
, type
);
803 kmap
= map__kmap(map
);
807 kmap
->kmaps
= &machine
->kmaps
;
808 map_groups__insert(&machine
->kmaps
, map
);
814 void machine__destroy_kernel_maps(struct machine
*machine
)
818 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
820 struct map
*map
= __machine__kernel_map(machine
, type
);
825 kmap
= map__kmap(map
);
826 map_groups__remove(&machine
->kmaps
, map
);
827 if (kmap
&& kmap
->ref_reloc_sym
) {
829 * ref_reloc_sym is shared among all maps, so free just
832 if (type
== MAP__FUNCTION
) {
833 zfree((char **)&kmap
->ref_reloc_sym
->name
);
834 zfree(&kmap
->ref_reloc_sym
);
836 kmap
->ref_reloc_sym
= NULL
;
839 map__put(machine
->vmlinux_maps
[type
]);
840 machine
->vmlinux_maps
[type
] = NULL
;
844 int machines__create_guest_kernel_maps(struct machines
*machines
)
847 struct dirent
**namelist
= NULL
;
853 if (symbol_conf
.default_guest_vmlinux_name
||
854 symbol_conf
.default_guest_modules
||
855 symbol_conf
.default_guest_kallsyms
) {
856 machines__create_kernel_maps(machines
, DEFAULT_GUEST_KERNEL_ID
);
859 if (symbol_conf
.guestmount
) {
860 items
= scandir(symbol_conf
.guestmount
, &namelist
, NULL
, NULL
);
863 for (i
= 0; i
< items
; i
++) {
864 if (!isdigit(namelist
[i
]->d_name
[0])) {
865 /* Filter out . and .. */
868 pid
= (pid_t
)strtol(namelist
[i
]->d_name
, &endp
, 10);
869 if ((*endp
!= '\0') ||
870 (endp
== namelist
[i
]->d_name
) ||
872 pr_debug("invalid directory (%s). Skipping.\n",
873 namelist
[i
]->d_name
);
876 sprintf(path
, "%s/%s/proc/kallsyms",
877 symbol_conf
.guestmount
,
878 namelist
[i
]->d_name
);
879 ret
= access(path
, R_OK
);
881 pr_debug("Can't access file %s\n", path
);
884 machines__create_kernel_maps(machines
, pid
);
893 void machines__destroy_kernel_maps(struct machines
*machines
)
895 struct rb_node
*next
= rb_first(&machines
->guests
);
897 machine__destroy_kernel_maps(&machines
->host
);
900 struct machine
*pos
= rb_entry(next
, struct machine
, rb_node
);
902 next
= rb_next(&pos
->rb_node
);
903 rb_erase(&pos
->rb_node
, &machines
->guests
);
904 machine__delete(pos
);
908 int machines__create_kernel_maps(struct machines
*machines
, pid_t pid
)
910 struct machine
*machine
= machines__findnew(machines
, pid
);
915 return machine__create_kernel_maps(machine
);
918 int __machine__load_kallsyms(struct machine
*machine
, const char *filename
,
919 enum map_type type
, bool no_kcore
, symbol_filter_t filter
)
921 struct map
*map
= machine__kernel_map(machine
);
922 int ret
= __dso__load_kallsyms(map
->dso
, filename
, map
, no_kcore
, filter
);
925 dso__set_loaded(map
->dso
, type
);
927 * Since /proc/kallsyms will have multiple sessions for the
928 * kernel, with modules between them, fixup the end of all
931 __map_groups__fixup_end(&machine
->kmaps
, type
);
937 int machine__load_kallsyms(struct machine
*machine
, const char *filename
,
938 enum map_type type
, symbol_filter_t filter
)
940 return __machine__load_kallsyms(machine
, filename
, type
, false, filter
);
943 int machine__load_vmlinux_path(struct machine
*machine
, enum map_type type
,
944 symbol_filter_t filter
)
946 struct map
*map
= machine__kernel_map(machine
);
947 int ret
= dso__load_vmlinux_path(map
->dso
, map
, filter
);
950 dso__set_loaded(map
->dso
, type
);
955 static void map_groups__fixup_end(struct map_groups
*mg
)
958 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
959 __map_groups__fixup_end(mg
, i
);
962 static char *get_kernel_version(const char *root_dir
)
964 char version
[PATH_MAX
];
967 const char *prefix
= "Linux version ";
969 sprintf(version
, "%s/proc/version", root_dir
);
970 file
= fopen(version
, "r");
975 tmp
= fgets(version
, sizeof(version
), file
);
978 name
= strstr(version
, prefix
);
981 name
+= strlen(prefix
);
982 tmp
= strchr(name
, ' ');
989 static bool is_kmod_dso(struct dso
*dso
)
991 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
||
992 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE
;
995 static int map_groups__set_module_path(struct map_groups
*mg
, const char *path
,
1001 map
= map_groups__find_by_name(mg
, MAP__FUNCTION
, m
->name
);
1005 long_name
= strdup(path
);
1006 if (long_name
== NULL
)
1009 dso__set_long_name(map
->dso
, long_name
, true);
1010 dso__kernel_module_get_build_id(map
->dso
, "");
1013 * Full name could reveal us kmod compression, so
1014 * we need to update the symtab_type if needed.
1016 if (m
->comp
&& is_kmod_dso(map
->dso
))
1017 map
->dso
->symtab_type
++;
1022 static int map_groups__set_modules_path_dir(struct map_groups
*mg
,
1023 const char *dir_name
, int depth
)
1025 struct dirent
*dent
;
1026 DIR *dir
= opendir(dir_name
);
1030 pr_debug("%s: cannot open %s dir\n", __func__
, dir_name
);
1034 while ((dent
= readdir(dir
)) != NULL
) {
1035 char path
[PATH_MAX
];
1038 /*sshfs might return bad dent->d_type, so we have to stat*/
1039 snprintf(path
, sizeof(path
), "%s/%s", dir_name
, dent
->d_name
);
1040 if (stat(path
, &st
))
1043 if (S_ISDIR(st
.st_mode
)) {
1044 if (!strcmp(dent
->d_name
, ".") ||
1045 !strcmp(dent
->d_name
, ".."))
1048 /* Do not follow top-level source and build symlinks */
1050 if (!strcmp(dent
->d_name
, "source") ||
1051 !strcmp(dent
->d_name
, "build"))
1055 ret
= map_groups__set_modules_path_dir(mg
, path
,
1062 ret
= kmod_path__parse_name(&m
, dent
->d_name
);
1067 ret
= map_groups__set_module_path(mg
, path
, &m
);
1081 static int machine__set_modules_path(struct machine
*machine
)
1084 char modules_path
[PATH_MAX
];
1086 version
= get_kernel_version(machine
->root_dir
);
1090 snprintf(modules_path
, sizeof(modules_path
), "%s/lib/modules/%s",
1091 machine
->root_dir
, version
);
1094 return map_groups__set_modules_path_dir(&machine
->kmaps
, modules_path
, 0);
1096 int __weak
arch__fix_module_text_start(u64
*start __maybe_unused
,
1097 const char *name __maybe_unused
)
1102 static int machine__create_module(void *arg
, const char *name
, u64 start
)
1104 struct machine
*machine
= arg
;
1107 if (arch__fix_module_text_start(&start
, name
) < 0)
1110 map
= machine__findnew_module_map(machine
, start
, name
);
1114 dso__kernel_module_get_build_id(map
->dso
, machine
->root_dir
);
1119 static int machine__create_modules(struct machine
*machine
)
1121 const char *modules
;
1122 char path
[PATH_MAX
];
1124 if (machine__is_default_guest(machine
)) {
1125 modules
= symbol_conf
.default_guest_modules
;
1127 snprintf(path
, PATH_MAX
, "%s/proc/modules", machine
->root_dir
);
1131 if (symbol__restricted_filename(modules
, "/proc/modules"))
1134 if (modules__parse(modules
, machine
, machine__create_module
))
1137 if (!machine__set_modules_path(machine
))
1140 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1145 int machine__create_kernel_maps(struct machine
*machine
)
1147 struct dso
*kernel
= machine__get_kernel(machine
);
1155 ret
= __machine__create_kernel_maps(machine
, kernel
);
1160 if (symbol_conf
.use_modules
&& machine__create_modules(machine
) < 0) {
1161 if (machine__is_host(machine
))
1162 pr_debug("Problems creating module maps, "
1163 "continuing anyway...\n");
1165 pr_debug("Problems creating module maps for guest %d, "
1166 "continuing anyway...\n", machine
->pid
);
1170 * Now that we have all the maps created, just set the ->end of them:
1172 map_groups__fixup_end(&machine
->kmaps
);
1174 addr
= machine__get_running_kernel_start(machine
, &name
);
1176 } else if (maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
, name
, addr
)) {
1177 machine__destroy_kernel_maps(machine
);
1184 static void machine__set_kernel_mmap_len(struct machine
*machine
,
1185 union perf_event
*event
)
1189 for (i
= 0; i
< MAP__NR_TYPES
; i
++) {
1190 machine
->vmlinux_maps
[i
]->start
= event
->mmap
.start
;
1191 machine
->vmlinux_maps
[i
]->end
= (event
->mmap
.start
+
1194 * Be a bit paranoid here, some perf.data file came with
1195 * a zero sized synthesized MMAP event for the kernel.
1197 if (machine
->vmlinux_maps
[i
]->end
== 0)
1198 machine
->vmlinux_maps
[i
]->end
= ~0ULL;
1202 static bool machine__uses_kcore(struct machine
*machine
)
1206 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1207 if (dso__is_kcore(dso
))
1214 static int machine__process_kernel_mmap_event(struct machine
*machine
,
1215 union perf_event
*event
)
1218 char kmmap_prefix
[PATH_MAX
];
1219 enum dso_kernel_type kernel_type
;
1220 bool is_kernel_mmap
;
1222 /* If we have maps from kcore then we do not need or want any others */
1223 if (machine__uses_kcore(machine
))
1226 machine__mmap_name(machine
, kmmap_prefix
, sizeof(kmmap_prefix
));
1227 if (machine__is_host(machine
))
1228 kernel_type
= DSO_TYPE_KERNEL
;
1230 kernel_type
= DSO_TYPE_GUEST_KERNEL
;
1232 is_kernel_mmap
= memcmp(event
->mmap
.filename
,
1234 strlen(kmmap_prefix
) - 1) == 0;
1235 if (event
->mmap
.filename
[0] == '/' ||
1236 (!is_kernel_mmap
&& event
->mmap
.filename
[0] == '[')) {
1237 map
= machine__findnew_module_map(machine
, event
->mmap
.start
,
1238 event
->mmap
.filename
);
1242 map
->end
= map
->start
+ event
->mmap
.len
;
1243 } else if (is_kernel_mmap
) {
1244 const char *symbol_name
= (event
->mmap
.filename
+
1245 strlen(kmmap_prefix
));
1247 * Should be there already, from the build-id table in
1250 struct dso
*kernel
= NULL
;
1253 pthread_rwlock_rdlock(&machine
->dsos
.lock
);
1255 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1258 * The cpumode passed to is_kernel_module is not the
1259 * cpumode of *this* event. If we insist on passing
1260 * correct cpumode to is_kernel_module, we should
1261 * record the cpumode when we adding this dso to the
1264 * However we don't really need passing correct
1265 * cpumode. We know the correct cpumode must be kernel
1266 * mode (if not, we should not link it onto kernel_dsos
1269 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1270 * is_kernel_module() treats it as a kernel cpumode.
1274 is_kernel_module(dso
->long_name
,
1275 PERF_RECORD_MISC_CPUMODE_UNKNOWN
))
1283 pthread_rwlock_unlock(&machine
->dsos
.lock
);
1286 kernel
= machine__findnew_dso(machine
, kmmap_prefix
);
1290 kernel
->kernel
= kernel_type
;
1291 if (__machine__create_kernel_maps(machine
, kernel
) < 0) {
1296 if (strstr(kernel
->long_name
, "vmlinux"))
1297 dso__set_short_name(kernel
, "[kernel.vmlinux]", false);
1299 machine__set_kernel_mmap_len(machine
, event
);
1302 * Avoid using a zero address (kptr_restrict) for the ref reloc
1303 * symbol. Effectively having zero here means that at record
1304 * time /proc/sys/kernel/kptr_restrict was non zero.
1306 if (event
->mmap
.pgoff
!= 0) {
1307 maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
,
1312 if (machine__is_default_guest(machine
)) {
1314 * preload dso of guest kernel and modules
1316 dso__load(kernel
, machine__kernel_map(machine
), NULL
);
1324 int machine__process_mmap2_event(struct machine
*machine
,
1325 union perf_event
*event
,
1326 struct perf_sample
*sample
)
1328 struct thread
*thread
;
1334 perf_event__fprintf_mmap2(event
, stdout
);
1336 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1337 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1338 ret
= machine__process_kernel_mmap_event(machine
, event
);
1344 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
,
1349 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1350 type
= MAP__VARIABLE
;
1352 type
= MAP__FUNCTION
;
1354 map
= map__new(machine
, event
->mmap2
.start
,
1355 event
->mmap2
.len
, event
->mmap2
.pgoff
,
1356 event
->mmap2
.pid
, event
->mmap2
.maj
,
1357 event
->mmap2
.min
, event
->mmap2
.ino
,
1358 event
->mmap2
.ino_generation
,
1361 event
->mmap2
.filename
, type
, thread
);
1364 goto out_problem_map
;
1366 ret
= thread__insert_map(thread
, map
);
1368 goto out_problem_insert
;
1370 thread__put(thread
);
1377 thread__put(thread
);
1379 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1383 int machine__process_mmap_event(struct machine
*machine
, union perf_event
*event
,
1384 struct perf_sample
*sample
)
1386 struct thread
*thread
;
1392 perf_event__fprintf_mmap(event
, stdout
);
1394 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1395 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1396 ret
= machine__process_kernel_mmap_event(machine
, event
);
1402 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
,
1407 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1408 type
= MAP__VARIABLE
;
1410 type
= MAP__FUNCTION
;
1412 map
= map__new(machine
, event
->mmap
.start
,
1413 event
->mmap
.len
, event
->mmap
.pgoff
,
1414 event
->mmap
.pid
, 0, 0, 0, 0, 0, 0,
1415 event
->mmap
.filename
,
1419 goto out_problem_map
;
1421 ret
= thread__insert_map(thread
, map
);
1423 goto out_problem_insert
;
1425 thread__put(thread
);
1432 thread__put(thread
);
1434 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1438 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
)
1440 if (machine
->last_match
== th
)
1441 machine
->last_match
= NULL
;
1443 BUG_ON(atomic_read(&th
->refcnt
) == 0);
1445 pthread_rwlock_wrlock(&machine
->threads_lock
);
1446 rb_erase_init(&th
->rb_node
, &machine
->threads
);
1447 RB_CLEAR_NODE(&th
->rb_node
);
1448 --machine
->nr_threads
;
1450 * Move it first to the dead_threads list, then drop the reference,
1451 * if this is the last reference, then the thread__delete destructor
1452 * will be called and we will remove it from the dead_threads list.
1454 list_add_tail(&th
->node
, &machine
->dead_threads
);
1456 pthread_rwlock_unlock(&machine
->threads_lock
);
1460 void machine__remove_thread(struct machine
*machine
, struct thread
*th
)
1462 return __machine__remove_thread(machine
, th
, true);
1465 int machine__process_fork_event(struct machine
*machine
, union perf_event
*event
,
1466 struct perf_sample
*sample
)
1468 struct thread
*thread
= machine__find_thread(machine
,
1471 struct thread
*parent
= machine__findnew_thread(machine
,
1477 perf_event__fprintf_task(event
, stdout
);
1480 * There may be an existing thread that is not actually the parent,
1481 * either because we are processing events out of order, or because the
1482 * (fork) event that would have removed the thread was lost. Assume the
1483 * latter case and continue on as best we can.
1485 if (parent
->pid_
!= (pid_t
)event
->fork
.ppid
) {
1486 dump_printf("removing erroneous parent thread %d/%d\n",
1487 parent
->pid_
, parent
->tid
);
1488 machine__remove_thread(machine
, parent
);
1489 thread__put(parent
);
1490 parent
= machine__findnew_thread(machine
, event
->fork
.ppid
,
1494 /* if a thread currently exists for the thread id remove it */
1495 if (thread
!= NULL
) {
1496 machine__remove_thread(machine
, thread
);
1497 thread__put(thread
);
1500 thread
= machine__findnew_thread(machine
, event
->fork
.pid
,
1503 if (thread
== NULL
|| parent
== NULL
||
1504 thread__fork(thread
, parent
, sample
->time
) < 0) {
1505 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1508 thread__put(thread
);
1509 thread__put(parent
);
1514 int machine__process_exit_event(struct machine
*machine
, union perf_event
*event
,
1515 struct perf_sample
*sample __maybe_unused
)
1517 struct thread
*thread
= machine__find_thread(machine
,
1522 perf_event__fprintf_task(event
, stdout
);
1524 if (thread
!= NULL
) {
1525 thread__exited(thread
);
1526 thread__put(thread
);
1532 int machine__process_event(struct machine
*machine
, union perf_event
*event
,
1533 struct perf_sample
*sample
)
1537 switch (event
->header
.type
) {
1538 case PERF_RECORD_COMM
:
1539 ret
= machine__process_comm_event(machine
, event
, sample
); break;
1540 case PERF_RECORD_MMAP
:
1541 ret
= machine__process_mmap_event(machine
, event
, sample
); break;
1542 case PERF_RECORD_MMAP2
:
1543 ret
= machine__process_mmap2_event(machine
, event
, sample
); break;
1544 case PERF_RECORD_FORK
:
1545 ret
= machine__process_fork_event(machine
, event
, sample
); break;
1546 case PERF_RECORD_EXIT
:
1547 ret
= machine__process_exit_event(machine
, event
, sample
); break;
1548 case PERF_RECORD_LOST
:
1549 ret
= machine__process_lost_event(machine
, event
, sample
); break;
1550 case PERF_RECORD_AUX
:
1551 ret
= machine__process_aux_event(machine
, event
); break;
1552 case PERF_RECORD_ITRACE_START
:
1553 ret
= machine__process_itrace_start_event(machine
, event
); break;
1554 case PERF_RECORD_LOST_SAMPLES
:
1555 ret
= machine__process_lost_samples_event(machine
, event
, sample
); break;
1556 case PERF_RECORD_SWITCH
:
1557 case PERF_RECORD_SWITCH_CPU_WIDE
:
1558 ret
= machine__process_switch_event(machine
, event
); break;
1567 static bool symbol__match_regex(struct symbol
*sym
, regex_t
*regex
)
1569 if (sym
->name
&& !regexec(regex
, sym
->name
, 0, NULL
, 0))
1574 static void ip__resolve_ams(struct thread
*thread
,
1575 struct addr_map_symbol
*ams
,
1578 struct addr_location al
;
1580 memset(&al
, 0, sizeof(al
));
1582 * We cannot use the header.misc hint to determine whether a
1583 * branch stack address is user, kernel, guest, hypervisor.
1584 * Branches may straddle the kernel/user/hypervisor boundaries.
1585 * Thus, we have to try consecutively until we find a match
1586 * or else, the symbol is unknown
1588 thread__find_cpumode_addr_location(thread
, MAP__FUNCTION
, ip
, &al
);
1591 ams
->al_addr
= al
.addr
;
1596 static void ip__resolve_data(struct thread
*thread
,
1597 u8 m
, struct addr_map_symbol
*ams
, u64 addr
)
1599 struct addr_location al
;
1601 memset(&al
, 0, sizeof(al
));
1603 thread__find_addr_location(thread
, m
, MAP__VARIABLE
, addr
, &al
);
1604 if (al
.map
== NULL
) {
1606 * some shared data regions have execute bit set which puts
1607 * their mapping in the MAP__FUNCTION type array.
1608 * Check there as a fallback option before dropping the sample.
1610 thread__find_addr_location(thread
, m
, MAP__FUNCTION
, addr
, &al
);
1614 ams
->al_addr
= al
.addr
;
1619 struct mem_info
*sample__resolve_mem(struct perf_sample
*sample
,
1620 struct addr_location
*al
)
1622 struct mem_info
*mi
= zalloc(sizeof(*mi
));
1627 ip__resolve_ams(al
->thread
, &mi
->iaddr
, sample
->ip
);
1628 ip__resolve_data(al
->thread
, al
->cpumode
, &mi
->daddr
, sample
->addr
);
1629 mi
->data_src
.val
= sample
->data_src
;
1634 static int add_callchain_ip(struct thread
*thread
,
1635 struct callchain_cursor
*cursor
,
1636 struct symbol
**parent
,
1637 struct addr_location
*root_al
,
1641 struct addr_location al
;
1646 thread__find_cpumode_addr_location(thread
, MAP__FUNCTION
,
1649 if (ip
>= PERF_CONTEXT_MAX
) {
1651 case PERF_CONTEXT_HV
:
1652 *cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1654 case PERF_CONTEXT_KERNEL
:
1655 *cpumode
= PERF_RECORD_MISC_KERNEL
;
1657 case PERF_CONTEXT_USER
:
1658 *cpumode
= PERF_RECORD_MISC_USER
;
1661 pr_debug("invalid callchain context: "
1662 "%"PRId64
"\n", (s64
) ip
);
1664 * It seems the callchain is corrupted.
1667 callchain_cursor_reset(cursor
);
1672 thread__find_addr_location(thread
, *cpumode
, MAP__FUNCTION
,
1676 if (al
.sym
!= NULL
) {
1677 if (perf_hpp_list
.parent
&& !*parent
&&
1678 symbol__match_regex(al
.sym
, &parent_regex
))
1680 else if (have_ignore_callees
&& root_al
&&
1681 symbol__match_regex(al
.sym
, &ignore_callees_regex
)) {
1682 /* Treat this symbol as the root,
1683 forgetting its callees. */
1685 callchain_cursor_reset(cursor
);
1689 if (symbol_conf
.hide_unresolved
&& al
.sym
== NULL
)
1691 return callchain_cursor_append(cursor
, al
.addr
, al
.map
, al
.sym
);
1694 struct branch_info
*sample__resolve_bstack(struct perf_sample
*sample
,
1695 struct addr_location
*al
)
1698 const struct branch_stack
*bs
= sample
->branch_stack
;
1699 struct branch_info
*bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
1704 for (i
= 0; i
< bs
->nr
; i
++) {
1705 ip__resolve_ams(al
->thread
, &bi
[i
].to
, bs
->entries
[i
].to
);
1706 ip__resolve_ams(al
->thread
, &bi
[i
].from
, bs
->entries
[i
].from
);
1707 bi
[i
].flags
= bs
->entries
[i
].flags
;
1714 #define NO_ENTRY 0xff
1716 #define PERF_MAX_BRANCH_DEPTH 127
1719 static int remove_loops(struct branch_entry
*l
, int nr
)
1722 unsigned char chash
[CHASHSZ
];
1724 memset(chash
, NO_ENTRY
, sizeof(chash
));
1726 BUG_ON(PERF_MAX_BRANCH_DEPTH
> 255);
1728 for (i
= 0; i
< nr
; i
++) {
1729 int h
= hash_64(l
[i
].from
, CHASHBITS
) % CHASHSZ
;
1731 /* no collision handling for now */
1732 if (chash
[h
] == NO_ENTRY
) {
1734 } else if (l
[chash
[h
]].from
== l
[i
].from
) {
1735 bool is_loop
= true;
1736 /* check if it is a real loop */
1738 for (j
= chash
[h
]; j
< i
&& i
+ off
< nr
; j
++, off
++)
1739 if (l
[j
].from
!= l
[i
+ off
].from
) {
1744 memmove(l
+ i
, l
+ i
+ off
,
1745 (nr
- (i
+ off
)) * sizeof(*l
));
1754 * Recolve LBR callstack chain sample
1756 * 1 on success get LBR callchain information
1757 * 0 no available LBR callchain information, should try fp
1758 * negative error code on other errors.
1760 static int resolve_lbr_callchain_sample(struct thread
*thread
,
1761 struct callchain_cursor
*cursor
,
1762 struct perf_sample
*sample
,
1763 struct symbol
**parent
,
1764 struct addr_location
*root_al
,
1767 struct ip_callchain
*chain
= sample
->callchain
;
1768 int chain_nr
= min(max_stack
, (int)chain
->nr
);
1769 u8 cpumode
= PERF_RECORD_MISC_USER
;
1773 for (i
= 0; i
< chain_nr
; i
++) {
1774 if (chain
->ips
[i
] == PERF_CONTEXT_USER
)
1778 /* LBR only affects the user callchain */
1779 if (i
!= chain_nr
) {
1780 struct branch_stack
*lbr_stack
= sample
->branch_stack
;
1781 int lbr_nr
= lbr_stack
->nr
;
1783 * LBR callstack can only get user call chain.
1784 * The mix_chain_nr is kernel call chain
1785 * number plus LBR user call chain number.
1786 * i is kernel call chain number,
1787 * 1 is PERF_CONTEXT_USER,
1788 * lbr_nr + 1 is the user call chain number.
1789 * For details, please refer to the comments
1790 * in callchain__printf
1792 int mix_chain_nr
= i
+ 1 + lbr_nr
+ 1;
1794 for (j
= 0; j
< mix_chain_nr
; j
++) {
1795 if (callchain_param
.order
== ORDER_CALLEE
) {
1799 ip
= lbr_stack
->entries
[j
- i
- 2].from
;
1801 ip
= lbr_stack
->entries
[0].to
;
1804 ip
= lbr_stack
->entries
[lbr_nr
- j
- 1].from
;
1805 else if (j
> lbr_nr
)
1806 ip
= chain
->ips
[i
+ 1 - (j
- lbr_nr
)];
1808 ip
= lbr_stack
->entries
[0].to
;
1811 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
, &cpumode
, ip
);
1813 return (err
< 0) ? err
: 0;
1821 static int thread__resolve_callchain_sample(struct thread
*thread
,
1822 struct callchain_cursor
*cursor
,
1823 struct perf_evsel
*evsel
,
1824 struct perf_sample
*sample
,
1825 struct symbol
**parent
,
1826 struct addr_location
*root_al
,
1829 struct branch_stack
*branch
= sample
->branch_stack
;
1830 struct ip_callchain
*chain
= sample
->callchain
;
1831 int chain_nr
= chain
->nr
;
1832 u8 cpumode
= PERF_RECORD_MISC_USER
;
1833 int i
, j
, err
, nr_entries
;
1837 if (perf_evsel__has_branch_callstack(evsel
)) {
1838 err
= resolve_lbr_callchain_sample(thread
, cursor
, sample
, parent
,
1839 root_al
, max_stack
);
1841 return (err
< 0) ? err
: 0;
1845 * Based on DWARF debug information, some architectures skip
1846 * a callchain entry saved by the kernel.
1848 skip_idx
= arch_skip_callchain_idx(thread
, chain
);
1851 * Add branches to call stack for easier browsing. This gives
1852 * more context for a sample than just the callers.
1854 * This uses individual histograms of paths compared to the
1855 * aggregated histograms the normal LBR mode uses.
1857 * Limitations for now:
1858 * - No extra filters
1859 * - No annotations (should annotate somehow)
1862 if (branch
&& callchain_param
.branch_callstack
) {
1863 int nr
= min(max_stack
, (int)branch
->nr
);
1864 struct branch_entry be
[nr
];
1866 if (branch
->nr
> PERF_MAX_BRANCH_DEPTH
) {
1867 pr_warning("corrupted branch chain. skipping...\n");
1871 for (i
= 0; i
< nr
; i
++) {
1872 if (callchain_param
.order
== ORDER_CALLEE
) {
1873 be
[i
] = branch
->entries
[i
];
1875 * Check for overlap into the callchain.
1876 * The return address is one off compared to
1877 * the branch entry. To adjust for this
1878 * assume the calling instruction is not longer
1881 if (i
== skip_idx
||
1882 chain
->ips
[first_call
] >= PERF_CONTEXT_MAX
)
1884 else if (be
[i
].from
< chain
->ips
[first_call
] &&
1885 be
[i
].from
>= chain
->ips
[first_call
] - 8)
1888 be
[i
] = branch
->entries
[branch
->nr
- i
- 1];
1891 nr
= remove_loops(be
, nr
);
1893 for (i
= 0; i
< nr
; i
++) {
1894 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
,
1897 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
,
1908 for (i
= first_call
, nr_entries
= 0;
1909 i
< chain_nr
&& nr_entries
< max_stack
; i
++) {
1912 if (callchain_param
.order
== ORDER_CALLEE
)
1915 j
= chain
->nr
- i
- 1;
1917 #ifdef HAVE_SKIP_CALLCHAIN_IDX
1923 if (ip
< PERF_CONTEXT_MAX
)
1926 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
, &cpumode
, ip
);
1929 return (err
< 0) ? err
: 0;
1935 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
1937 struct callchain_cursor
*cursor
= arg
;
1939 if (symbol_conf
.hide_unresolved
&& entry
->sym
== NULL
)
1941 return callchain_cursor_append(cursor
, entry
->ip
,
1942 entry
->map
, entry
->sym
);
1945 static int thread__resolve_callchain_unwind(struct thread
*thread
,
1946 struct callchain_cursor
*cursor
,
1947 struct perf_evsel
*evsel
,
1948 struct perf_sample
*sample
,
1951 /* Can we do dwarf post unwind? */
1952 if (!((evsel
->attr
.sample_type
& PERF_SAMPLE_REGS_USER
) &&
1953 (evsel
->attr
.sample_type
& PERF_SAMPLE_STACK_USER
)))
1956 /* Bail out if nothing was captured. */
1957 if ((!sample
->user_regs
.regs
) ||
1958 (!sample
->user_stack
.size
))
1961 return unwind__get_entries(unwind_entry
, cursor
,
1962 thread
, sample
, max_stack
);
1965 int thread__resolve_callchain(struct thread
*thread
,
1966 struct callchain_cursor
*cursor
,
1967 struct perf_evsel
*evsel
,
1968 struct perf_sample
*sample
,
1969 struct symbol
**parent
,
1970 struct addr_location
*root_al
,
1975 callchain_cursor_reset(&callchain_cursor
);
1977 if (callchain_param
.order
== ORDER_CALLEE
) {
1978 ret
= thread__resolve_callchain_sample(thread
, cursor
,
1984 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
1988 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
1993 ret
= thread__resolve_callchain_sample(thread
, cursor
,
2002 int machine__for_each_thread(struct machine
*machine
,
2003 int (*fn
)(struct thread
*thread
, void *p
),
2007 struct thread
*thread
;
2010 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
2011 thread
= rb_entry(nd
, struct thread
, rb_node
);
2012 rc
= fn(thread
, priv
);
2017 list_for_each_entry(thread
, &machine
->dead_threads
, node
) {
2018 rc
= fn(thread
, priv
);
2025 int machines__for_each_thread(struct machines
*machines
,
2026 int (*fn
)(struct thread
*thread
, void *p
),
2032 rc
= machine__for_each_thread(&machines
->host
, fn
, priv
);
2036 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
2037 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
2039 rc
= machine__for_each_thread(machine
, fn
, priv
);
2046 int __machine__synthesize_threads(struct machine
*machine
, struct perf_tool
*tool
,
2047 struct target
*target
, struct thread_map
*threads
,
2048 perf_event__handler_t process
, bool data_mmap
,
2049 unsigned int proc_map_timeout
)
2051 if (target__has_task(target
))
2052 return perf_event__synthesize_thread_map(tool
, threads
, process
, machine
, data_mmap
, proc_map_timeout
);
2053 else if (target__has_cpu(target
))
2054 return perf_event__synthesize_threads(tool
, process
, machine
, data_mmap
, proc_map_timeout
);
2055 /* command specified */
2059 pid_t
machine__get_current_tid(struct machine
*machine
, int cpu
)
2061 if (cpu
< 0 || cpu
>= MAX_NR_CPUS
|| !machine
->current_tid
)
2064 return machine
->current_tid
[cpu
];
2067 int machine__set_current_tid(struct machine
*machine
, int cpu
, pid_t pid
,
2070 struct thread
*thread
;
2075 if (!machine
->current_tid
) {
2078 machine
->current_tid
= calloc(MAX_NR_CPUS
, sizeof(pid_t
));
2079 if (!machine
->current_tid
)
2081 for (i
= 0; i
< MAX_NR_CPUS
; i
++)
2082 machine
->current_tid
[i
] = -1;
2085 if (cpu
>= MAX_NR_CPUS
) {
2086 pr_err("Requested CPU %d too large. ", cpu
);
2087 pr_err("Consider raising MAX_NR_CPUS\n");
2091 machine
->current_tid
[cpu
] = tid
;
2093 thread
= machine__findnew_thread(machine
, pid
, tid
);
2098 thread__put(thread
);
2103 int machine__get_kernel_start(struct machine
*machine
)
2105 struct map
*map
= machine__kernel_map(machine
);
2109 * The only addresses above 2^63 are kernel addresses of a 64-bit
2110 * kernel. Note that addresses are unsigned so that on a 32-bit system
2111 * all addresses including kernel addresses are less than 2^32. In
2112 * that case (32-bit system), if the kernel mapping is unknown, all
2113 * addresses will be assumed to be in user space - see
2114 * machine__kernel_ip().
2116 machine
->kernel_start
= 1ULL << 63;
2118 err
= map__load(map
, machine
->symbol_filter
);
2120 machine
->kernel_start
= map
->start
;
2125 struct dso
*machine__findnew_dso(struct machine
*machine
, const char *filename
)
2127 return dsos__findnew(&machine
->dsos
, filename
);
2130 char *machine__resolve_kernel_addr(void *vmachine
, unsigned long long *addrp
, char **modp
)
2132 struct machine
*machine
= vmachine
;
2134 struct symbol
*sym
= map_groups__find_symbol(&machine
->kmaps
, MAP__FUNCTION
, *addrp
, &map
, NULL
);
2139 *modp
= __map__is_kmodule(map
) ? (char *)map
->dso
->short_name
: NULL
;
2140 *addrp
= map
->unmap_ip(map
, sym
->start
);