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
)
141 machine__exit(machine
);
145 void machines__init(struct machines
*machines
)
147 machine__init(&machines
->host
, "", HOST_KERNEL_ID
);
148 machines
->guests
= RB_ROOT
;
149 machines
->symbol_filter
= NULL
;
152 void machines__exit(struct machines
*machines
)
154 machine__exit(&machines
->host
);
158 struct machine
*machines__add(struct machines
*machines
, pid_t pid
,
159 const char *root_dir
)
161 struct rb_node
**p
= &machines
->guests
.rb_node
;
162 struct rb_node
*parent
= NULL
;
163 struct machine
*pos
, *machine
= malloc(sizeof(*machine
));
168 if (machine__init(machine
, root_dir
, pid
) != 0) {
173 machine
->symbol_filter
= machines
->symbol_filter
;
177 pos
= rb_entry(parent
, struct machine
, rb_node
);
184 rb_link_node(&machine
->rb_node
, parent
, p
);
185 rb_insert_color(&machine
->rb_node
, &machines
->guests
);
190 void machines__set_symbol_filter(struct machines
*machines
,
191 symbol_filter_t symbol_filter
)
195 machines
->symbol_filter
= symbol_filter
;
196 machines
->host
.symbol_filter
= symbol_filter
;
198 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
199 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
201 machine
->symbol_filter
= symbol_filter
;
205 void machines__set_comm_exec(struct machines
*machines
, bool comm_exec
)
209 machines
->host
.comm_exec
= comm_exec
;
211 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
212 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
214 machine
->comm_exec
= comm_exec
;
218 struct machine
*machines__find(struct machines
*machines
, pid_t pid
)
220 struct rb_node
**p
= &machines
->guests
.rb_node
;
221 struct rb_node
*parent
= NULL
;
222 struct machine
*machine
;
223 struct machine
*default_machine
= NULL
;
225 if (pid
== HOST_KERNEL_ID
)
226 return &machines
->host
;
230 machine
= rb_entry(parent
, struct machine
, rb_node
);
231 if (pid
< machine
->pid
)
233 else if (pid
> machine
->pid
)
238 default_machine
= machine
;
241 return default_machine
;
244 struct machine
*machines__findnew(struct machines
*machines
, pid_t pid
)
247 const char *root_dir
= "";
248 struct machine
*machine
= machines__find(machines
, pid
);
250 if (machine
&& (machine
->pid
== pid
))
253 if ((pid
!= HOST_KERNEL_ID
) &&
254 (pid
!= DEFAULT_GUEST_KERNEL_ID
) &&
255 (symbol_conf
.guestmount
)) {
256 sprintf(path
, "%s/%d", symbol_conf
.guestmount
, pid
);
257 if (access(path
, R_OK
)) {
258 static struct strlist
*seen
;
261 seen
= strlist__new(NULL
, NULL
);
263 if (!strlist__has_entry(seen
, path
)) {
264 pr_err("Can't access file %s\n", path
);
265 strlist__add(seen
, path
);
273 machine
= machines__add(machines
, pid
, root_dir
);
278 void machines__process_guests(struct machines
*machines
,
279 machine__process_t process
, void *data
)
283 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
284 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
289 char *machine__mmap_name(struct machine
*machine
, char *bf
, size_t size
)
291 if (machine__is_host(machine
))
292 snprintf(bf
, size
, "[%s]", "kernel.kallsyms");
293 else if (machine__is_default_guest(machine
))
294 snprintf(bf
, size
, "[%s]", "guest.kernel.kallsyms");
296 snprintf(bf
, size
, "[%s.%d]", "guest.kernel.kallsyms",
303 void machines__set_id_hdr_size(struct machines
*machines
, u16 id_hdr_size
)
305 struct rb_node
*node
;
306 struct machine
*machine
;
308 machines
->host
.id_hdr_size
= id_hdr_size
;
310 for (node
= rb_first(&machines
->guests
); node
; node
= rb_next(node
)) {
311 machine
= rb_entry(node
, struct machine
, rb_node
);
312 machine
->id_hdr_size
= id_hdr_size
;
318 static void machine__update_thread_pid(struct machine
*machine
,
319 struct thread
*th
, pid_t pid
)
321 struct thread
*leader
;
323 if (pid
== th
->pid_
|| pid
== -1 || th
->pid_
!= -1)
328 if (th
->pid_
== th
->tid
)
331 leader
= __machine__findnew_thread(machine
, th
->pid_
, th
->pid_
);
336 leader
->mg
= map_groups__new(machine
);
341 if (th
->mg
== leader
->mg
)
346 * Maps are created from MMAP events which provide the pid and
347 * tid. Consequently there never should be any maps on a thread
348 * with an unknown pid. Just print an error if there are.
350 if (!map_groups__empty(th
->mg
))
351 pr_err("Discarding thread maps for %d:%d\n",
353 map_groups__put(th
->mg
);
356 th
->mg
= map_groups__get(leader
->mg
);
361 pr_err("Failed to join map groups for %d:%d\n", th
->pid_
, th
->tid
);
366 * Caller must eventually drop thread->refcnt returned with a successfull
367 * lookup/new thread inserted.
369 static struct thread
*____machine__findnew_thread(struct machine
*machine
,
370 pid_t pid
, pid_t tid
,
373 struct rb_node
**p
= &machine
->threads
.rb_node
;
374 struct rb_node
*parent
= NULL
;
378 * Front-end cache - TID lookups come in blocks,
379 * so most of the time we dont have to look up
382 th
= machine
->last_match
;
384 if (th
->tid
== tid
) {
385 machine__update_thread_pid(machine
, th
, pid
);
386 return thread__get(th
);
389 machine
->last_match
= NULL
;
394 th
= rb_entry(parent
, struct thread
, rb_node
);
396 if (th
->tid
== tid
) {
397 machine
->last_match
= th
;
398 machine__update_thread_pid(machine
, th
, pid
);
399 return thread__get(th
);
411 th
= thread__new(pid
, tid
);
413 rb_link_node(&th
->rb_node
, parent
, p
);
414 rb_insert_color(&th
->rb_node
, &machine
->threads
);
417 * We have to initialize map_groups separately
418 * after rb tree is updated.
420 * The reason is that we call machine__findnew_thread
421 * within thread__init_map_groups to find the thread
422 * leader and that would screwed the rb tree.
424 if (thread__init_map_groups(th
, machine
)) {
425 rb_erase_init(&th
->rb_node
, &machine
->threads
);
426 RB_CLEAR_NODE(&th
->rb_node
);
431 * It is now in the rbtree, get a ref
434 machine
->last_match
= th
;
435 ++machine
->nr_threads
;
441 struct thread
*__machine__findnew_thread(struct machine
*machine
, pid_t pid
, pid_t tid
)
443 return ____machine__findnew_thread(machine
, pid
, tid
, true);
446 struct thread
*machine__findnew_thread(struct machine
*machine
, pid_t pid
,
451 pthread_rwlock_wrlock(&machine
->threads_lock
);
452 th
= __machine__findnew_thread(machine
, pid
, tid
);
453 pthread_rwlock_unlock(&machine
->threads_lock
);
457 struct thread
*machine__find_thread(struct machine
*machine
, pid_t pid
,
461 pthread_rwlock_rdlock(&machine
->threads_lock
);
462 th
= ____machine__findnew_thread(machine
, pid
, tid
, false);
463 pthread_rwlock_unlock(&machine
->threads_lock
);
467 struct comm
*machine__thread_exec_comm(struct machine
*machine
,
468 struct thread
*thread
)
470 if (machine
->comm_exec
)
471 return thread__exec_comm(thread
);
473 return thread__comm(thread
);
476 int machine__process_comm_event(struct machine
*machine
, union perf_event
*event
,
477 struct perf_sample
*sample
)
479 struct thread
*thread
= machine__findnew_thread(machine
,
482 bool exec
= event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
;
486 machine
->comm_exec
= true;
489 perf_event__fprintf_comm(event
, stdout
);
491 if (thread
== NULL
||
492 __thread__set_comm(thread
, event
->comm
.comm
, sample
->time
, exec
)) {
493 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
502 int machine__process_lost_event(struct machine
*machine __maybe_unused
,
503 union perf_event
*event
, struct perf_sample
*sample __maybe_unused
)
505 dump_printf(": id:%" PRIu64
": lost:%" PRIu64
"\n",
506 event
->lost
.id
, event
->lost
.lost
);
510 int machine__process_lost_samples_event(struct machine
*machine __maybe_unused
,
511 union perf_event
*event
, struct perf_sample
*sample
)
513 dump_printf(": id:%" PRIu64
": lost samples :%" PRIu64
"\n",
514 sample
->id
, event
->lost_samples
.lost
);
518 static struct dso
*machine__findnew_module_dso(struct machine
*machine
,
520 const char *filename
)
524 pthread_rwlock_wrlock(&machine
->dsos
.lock
);
526 dso
= __dsos__find(&machine
->dsos
, m
->name
, true);
528 dso
= __dsos__addnew(&machine
->dsos
, m
->name
);
532 if (machine__is_host(machine
))
533 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
535 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
537 /* _KMODULE_COMP should be next to _KMODULE */
538 if (m
->kmod
&& m
->comp
)
541 dso__set_short_name(dso
, strdup(m
->name
), true);
542 dso__set_long_name(dso
, strdup(filename
), true);
547 pthread_rwlock_unlock(&machine
->dsos
.lock
);
551 int machine__process_aux_event(struct machine
*machine __maybe_unused
,
552 union perf_event
*event
)
555 perf_event__fprintf_aux(event
, stdout
);
559 int machine__process_itrace_start_event(struct machine
*machine __maybe_unused
,
560 union perf_event
*event
)
563 perf_event__fprintf_itrace_start(event
, stdout
);
567 int machine__process_switch_event(struct machine
*machine __maybe_unused
,
568 union perf_event
*event
)
571 perf_event__fprintf_switch(event
, stdout
);
575 static void dso__adjust_kmod_long_name(struct dso
*dso
, const char *filename
)
577 const char *dup_filename
;
579 if (!filename
|| !dso
|| !dso
->long_name
)
581 if (dso
->long_name
[0] != '[')
583 if (!strchr(filename
, '/'))
586 dup_filename
= strdup(filename
);
590 dso__set_long_name(dso
, dup_filename
, true);
593 struct map
*machine__findnew_module_map(struct machine
*machine
, u64 start
,
594 const char *filename
)
596 struct map
*map
= NULL
;
597 struct dso
*dso
= NULL
;
600 if (kmod_path__parse_name(&m
, filename
))
603 map
= map_groups__find_by_name(&machine
->kmaps
, MAP__FUNCTION
,
607 * If the map's dso is an offline module, give dso__load()
608 * a chance to find the file path of that module by fixing
611 dso__adjust_kmod_long_name(map
->dso
, filename
);
615 dso
= machine__findnew_module_dso(machine
, &m
, filename
);
619 map
= map__new2(start
, dso
, MAP__FUNCTION
);
623 map_groups__insert(&machine
->kmaps
, map
);
625 /* Put the map here because map_groups__insert alread got it */
628 /* put the dso here, corresponding to machine__findnew_module_dso */
634 size_t machines__fprintf_dsos(struct machines
*machines
, FILE *fp
)
637 size_t ret
= __dsos__fprintf(&machines
->host
.dsos
.head
, fp
);
639 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
640 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
641 ret
+= __dsos__fprintf(&pos
->dsos
.head
, fp
);
647 size_t machine__fprintf_dsos_buildid(struct machine
*m
, FILE *fp
,
648 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
650 return __dsos__fprintf_buildid(&m
->dsos
.head
, fp
, skip
, parm
);
653 size_t machines__fprintf_dsos_buildid(struct machines
*machines
, FILE *fp
,
654 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
657 size_t ret
= machine__fprintf_dsos_buildid(&machines
->host
, fp
, skip
, parm
);
659 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
660 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
661 ret
+= machine__fprintf_dsos_buildid(pos
, fp
, skip
, parm
);
666 size_t machine__fprintf_vmlinux_path(struct machine
*machine
, FILE *fp
)
670 struct dso
*kdso
= machine__kernel_map(machine
)->dso
;
672 if (kdso
->has_build_id
) {
673 char filename
[PATH_MAX
];
674 if (dso__build_id_filename(kdso
, filename
, sizeof(filename
)))
675 printed
+= fprintf(fp
, "[0] %s\n", filename
);
678 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
)
679 printed
+= fprintf(fp
, "[%d] %s\n",
680 i
+ kdso
->has_build_id
, vmlinux_path
[i
]);
685 size_t machine__fprintf(struct machine
*machine
, FILE *fp
)
690 pthread_rwlock_rdlock(&machine
->threads_lock
);
692 ret
= fprintf(fp
, "Threads: %u\n", machine
->nr_threads
);
694 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
695 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
697 ret
+= thread__fprintf(pos
, fp
);
700 pthread_rwlock_unlock(&machine
->threads_lock
);
705 static struct dso
*machine__get_kernel(struct machine
*machine
)
707 const char *vmlinux_name
= NULL
;
710 if (machine__is_host(machine
)) {
711 vmlinux_name
= symbol_conf
.vmlinux_name
;
713 vmlinux_name
= DSO__NAME_KALLSYMS
;
715 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
716 "[kernel]", DSO_TYPE_KERNEL
);
720 if (machine__is_default_guest(machine
))
721 vmlinux_name
= symbol_conf
.default_guest_vmlinux_name
;
723 vmlinux_name
= machine__mmap_name(machine
, bf
,
726 kernel
= machine__findnew_kernel(machine
, vmlinux_name
,
728 DSO_TYPE_GUEST_KERNEL
);
731 if (kernel
!= NULL
&& (!kernel
->has_build_id
))
732 dso__read_running_kernel_build_id(kernel
, machine
);
737 struct process_args
{
741 static void machine__get_kallsyms_filename(struct machine
*machine
, char *buf
,
744 if (machine__is_default_guest(machine
))
745 scnprintf(buf
, bufsz
, "%s", symbol_conf
.default_guest_kallsyms
);
747 scnprintf(buf
, bufsz
, "%s/proc/kallsyms", machine
->root_dir
);
750 const char *ref_reloc_sym_names
[] = {"_text", "_stext", NULL
};
752 /* Figure out the start address of kernel map from /proc/kallsyms.
753 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
754 * symbol_name if it's not that important.
756 static u64
machine__get_running_kernel_start(struct machine
*machine
,
757 const char **symbol_name
)
759 char filename
[PATH_MAX
];
764 machine__get_kallsyms_filename(machine
, filename
, PATH_MAX
);
766 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
769 for (i
= 0; (name
= ref_reloc_sym_names
[i
]) != NULL
; i
++) {
770 addr
= kallsyms__get_function_start(filename
, name
);
781 int __machine__create_kernel_maps(struct machine
*machine
, struct dso
*kernel
)
784 u64 start
= machine__get_running_kernel_start(machine
, NULL
);
786 /* In case of renewal the kernel map, destroy previous one */
787 machine__destroy_kernel_maps(machine
);
789 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
793 machine
->vmlinux_maps
[type
] = map__new2(start
, kernel
, type
);
794 if (machine
->vmlinux_maps
[type
] == NULL
)
797 machine
->vmlinux_maps
[type
]->map_ip
=
798 machine
->vmlinux_maps
[type
]->unmap_ip
=
800 map
= __machine__kernel_map(machine
, type
);
801 kmap
= map__kmap(map
);
805 kmap
->kmaps
= &machine
->kmaps
;
806 map_groups__insert(&machine
->kmaps
, map
);
812 void machine__destroy_kernel_maps(struct machine
*machine
)
816 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
818 struct map
*map
= __machine__kernel_map(machine
, type
);
823 kmap
= map__kmap(map
);
824 map_groups__remove(&machine
->kmaps
, map
);
825 if (kmap
&& kmap
->ref_reloc_sym
) {
827 * ref_reloc_sym is shared among all maps, so free just
830 if (type
== MAP__FUNCTION
) {
831 zfree((char **)&kmap
->ref_reloc_sym
->name
);
832 zfree(&kmap
->ref_reloc_sym
);
834 kmap
->ref_reloc_sym
= NULL
;
837 map__put(machine
->vmlinux_maps
[type
]);
838 machine
->vmlinux_maps
[type
] = NULL
;
842 int machines__create_guest_kernel_maps(struct machines
*machines
)
845 struct dirent
**namelist
= NULL
;
851 if (symbol_conf
.default_guest_vmlinux_name
||
852 symbol_conf
.default_guest_modules
||
853 symbol_conf
.default_guest_kallsyms
) {
854 machines__create_kernel_maps(machines
, DEFAULT_GUEST_KERNEL_ID
);
857 if (symbol_conf
.guestmount
) {
858 items
= scandir(symbol_conf
.guestmount
, &namelist
, NULL
, NULL
);
861 for (i
= 0; i
< items
; i
++) {
862 if (!isdigit(namelist
[i
]->d_name
[0])) {
863 /* Filter out . and .. */
866 pid
= (pid_t
)strtol(namelist
[i
]->d_name
, &endp
, 10);
867 if ((*endp
!= '\0') ||
868 (endp
== namelist
[i
]->d_name
) ||
870 pr_debug("invalid directory (%s). Skipping.\n",
871 namelist
[i
]->d_name
);
874 sprintf(path
, "%s/%s/proc/kallsyms",
875 symbol_conf
.guestmount
,
876 namelist
[i
]->d_name
);
877 ret
= access(path
, R_OK
);
879 pr_debug("Can't access file %s\n", path
);
882 machines__create_kernel_maps(machines
, pid
);
891 void machines__destroy_kernel_maps(struct machines
*machines
)
893 struct rb_node
*next
= rb_first(&machines
->guests
);
895 machine__destroy_kernel_maps(&machines
->host
);
898 struct machine
*pos
= rb_entry(next
, struct machine
, rb_node
);
900 next
= rb_next(&pos
->rb_node
);
901 rb_erase(&pos
->rb_node
, &machines
->guests
);
902 machine__delete(pos
);
906 int machines__create_kernel_maps(struct machines
*machines
, pid_t pid
)
908 struct machine
*machine
= machines__findnew(machines
, pid
);
913 return machine__create_kernel_maps(machine
);
916 int __machine__load_kallsyms(struct machine
*machine
, const char *filename
,
917 enum map_type type
, bool no_kcore
, symbol_filter_t filter
)
919 struct map
*map
= machine__kernel_map(machine
);
920 int ret
= __dso__load_kallsyms(map
->dso
, filename
, map
, no_kcore
, filter
);
923 dso__set_loaded(map
->dso
, type
);
925 * Since /proc/kallsyms will have multiple sessions for the
926 * kernel, with modules between them, fixup the end of all
929 __map_groups__fixup_end(&machine
->kmaps
, type
);
935 int machine__load_kallsyms(struct machine
*machine
, const char *filename
,
936 enum map_type type
, symbol_filter_t filter
)
938 return __machine__load_kallsyms(machine
, filename
, type
, false, filter
);
941 int machine__load_vmlinux_path(struct machine
*machine
, enum map_type type
,
942 symbol_filter_t filter
)
944 struct map
*map
= machine__kernel_map(machine
);
945 int ret
= dso__load_vmlinux_path(map
->dso
, map
, filter
);
948 dso__set_loaded(map
->dso
, type
);
953 static void map_groups__fixup_end(struct map_groups
*mg
)
956 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
957 __map_groups__fixup_end(mg
, i
);
960 static char *get_kernel_version(const char *root_dir
)
962 char version
[PATH_MAX
];
965 const char *prefix
= "Linux version ";
967 sprintf(version
, "%s/proc/version", root_dir
);
968 file
= fopen(version
, "r");
973 tmp
= fgets(version
, sizeof(version
), file
);
976 name
= strstr(version
, prefix
);
979 name
+= strlen(prefix
);
980 tmp
= strchr(name
, ' ');
987 static bool is_kmod_dso(struct dso
*dso
)
989 return dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
||
990 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE
;
993 static int map_groups__set_module_path(struct map_groups
*mg
, const char *path
,
999 map
= map_groups__find_by_name(mg
, MAP__FUNCTION
, m
->name
);
1003 long_name
= strdup(path
);
1004 if (long_name
== NULL
)
1007 dso__set_long_name(map
->dso
, long_name
, true);
1008 dso__kernel_module_get_build_id(map
->dso
, "");
1011 * Full name could reveal us kmod compression, so
1012 * we need to update the symtab_type if needed.
1014 if (m
->comp
&& is_kmod_dso(map
->dso
))
1015 map
->dso
->symtab_type
++;
1020 static int map_groups__set_modules_path_dir(struct map_groups
*mg
,
1021 const char *dir_name
, int depth
)
1023 struct dirent
*dent
;
1024 DIR *dir
= opendir(dir_name
);
1028 pr_debug("%s: cannot open %s dir\n", __func__
, dir_name
);
1032 while ((dent
= readdir(dir
)) != NULL
) {
1033 char path
[PATH_MAX
];
1036 /*sshfs might return bad dent->d_type, so we have to stat*/
1037 snprintf(path
, sizeof(path
), "%s/%s", dir_name
, dent
->d_name
);
1038 if (stat(path
, &st
))
1041 if (S_ISDIR(st
.st_mode
)) {
1042 if (!strcmp(dent
->d_name
, ".") ||
1043 !strcmp(dent
->d_name
, ".."))
1046 /* Do not follow top-level source and build symlinks */
1048 if (!strcmp(dent
->d_name
, "source") ||
1049 !strcmp(dent
->d_name
, "build"))
1053 ret
= map_groups__set_modules_path_dir(mg
, path
,
1060 ret
= kmod_path__parse_name(&m
, dent
->d_name
);
1065 ret
= map_groups__set_module_path(mg
, path
, &m
);
1079 static int machine__set_modules_path(struct machine
*machine
)
1082 char modules_path
[PATH_MAX
];
1084 version
= get_kernel_version(machine
->root_dir
);
1088 snprintf(modules_path
, sizeof(modules_path
), "%s/lib/modules/%s",
1089 machine
->root_dir
, version
);
1092 return map_groups__set_modules_path_dir(&machine
->kmaps
, modules_path
, 0);
1095 static int machine__create_module(void *arg
, const char *name
, u64 start
)
1097 struct machine
*machine
= arg
;
1100 map
= machine__findnew_module_map(machine
, start
, name
);
1104 dso__kernel_module_get_build_id(map
->dso
, machine
->root_dir
);
1109 static int machine__create_modules(struct machine
*machine
)
1111 const char *modules
;
1112 char path
[PATH_MAX
];
1114 if (machine__is_default_guest(machine
)) {
1115 modules
= symbol_conf
.default_guest_modules
;
1117 snprintf(path
, PATH_MAX
, "%s/proc/modules", machine
->root_dir
);
1121 if (symbol__restricted_filename(modules
, "/proc/modules"))
1124 if (modules__parse(modules
, machine
, machine__create_module
))
1127 if (!machine__set_modules_path(machine
))
1130 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1135 int machine__create_kernel_maps(struct machine
*machine
)
1137 struct dso
*kernel
= machine__get_kernel(machine
);
1145 ret
= __machine__create_kernel_maps(machine
, kernel
);
1150 if (symbol_conf
.use_modules
&& machine__create_modules(machine
) < 0) {
1151 if (machine__is_host(machine
))
1152 pr_debug("Problems creating module maps, "
1153 "continuing anyway...\n");
1155 pr_debug("Problems creating module maps for guest %d, "
1156 "continuing anyway...\n", machine
->pid
);
1160 * Now that we have all the maps created, just set the ->end of them:
1162 map_groups__fixup_end(&machine
->kmaps
);
1164 addr
= machine__get_running_kernel_start(machine
, &name
);
1166 } else if (maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
, name
, addr
)) {
1167 machine__destroy_kernel_maps(machine
);
1174 static void machine__set_kernel_mmap_len(struct machine
*machine
,
1175 union perf_event
*event
)
1179 for (i
= 0; i
< MAP__NR_TYPES
; i
++) {
1180 machine
->vmlinux_maps
[i
]->start
= event
->mmap
.start
;
1181 machine
->vmlinux_maps
[i
]->end
= (event
->mmap
.start
+
1184 * Be a bit paranoid here, some perf.data file came with
1185 * a zero sized synthesized MMAP event for the kernel.
1187 if (machine
->vmlinux_maps
[i
]->end
== 0)
1188 machine
->vmlinux_maps
[i
]->end
= ~0ULL;
1192 static bool machine__uses_kcore(struct machine
*machine
)
1196 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1197 if (dso__is_kcore(dso
))
1204 static int machine__process_kernel_mmap_event(struct machine
*machine
,
1205 union perf_event
*event
)
1208 char kmmap_prefix
[PATH_MAX
];
1209 enum dso_kernel_type kernel_type
;
1210 bool is_kernel_mmap
;
1212 /* If we have maps from kcore then we do not need or want any others */
1213 if (machine__uses_kcore(machine
))
1216 machine__mmap_name(machine
, kmmap_prefix
, sizeof(kmmap_prefix
));
1217 if (machine__is_host(machine
))
1218 kernel_type
= DSO_TYPE_KERNEL
;
1220 kernel_type
= DSO_TYPE_GUEST_KERNEL
;
1222 is_kernel_mmap
= memcmp(event
->mmap
.filename
,
1224 strlen(kmmap_prefix
) - 1) == 0;
1225 if (event
->mmap
.filename
[0] == '/' ||
1226 (!is_kernel_mmap
&& event
->mmap
.filename
[0] == '[')) {
1227 map
= machine__findnew_module_map(machine
, event
->mmap
.start
,
1228 event
->mmap
.filename
);
1232 map
->end
= map
->start
+ event
->mmap
.len
;
1233 } else if (is_kernel_mmap
) {
1234 const char *symbol_name
= (event
->mmap
.filename
+
1235 strlen(kmmap_prefix
));
1237 * Should be there already, from the build-id table in
1240 struct dso
*kernel
= NULL
;
1243 pthread_rwlock_rdlock(&machine
->dsos
.lock
);
1245 list_for_each_entry(dso
, &machine
->dsos
.head
, node
) {
1248 * The cpumode passed to is_kernel_module is not the
1249 * cpumode of *this* event. If we insist on passing
1250 * correct cpumode to is_kernel_module, we should
1251 * record the cpumode when we adding this dso to the
1254 * However we don't really need passing correct
1255 * cpumode. We know the correct cpumode must be kernel
1256 * mode (if not, we should not link it onto kernel_dsos
1259 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1260 * is_kernel_module() treats it as a kernel cpumode.
1264 is_kernel_module(dso
->long_name
,
1265 PERF_RECORD_MISC_CPUMODE_UNKNOWN
))
1273 pthread_rwlock_unlock(&machine
->dsos
.lock
);
1276 kernel
= machine__findnew_dso(machine
, kmmap_prefix
);
1280 kernel
->kernel
= kernel_type
;
1281 if (__machine__create_kernel_maps(machine
, kernel
) < 0) {
1286 if (strstr(kernel
->long_name
, "vmlinux"))
1287 dso__set_short_name(kernel
, "[kernel.vmlinux]", false);
1289 machine__set_kernel_mmap_len(machine
, event
);
1292 * Avoid using a zero address (kptr_restrict) for the ref reloc
1293 * symbol. Effectively having zero here means that at record
1294 * time /proc/sys/kernel/kptr_restrict was non zero.
1296 if (event
->mmap
.pgoff
!= 0) {
1297 maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
,
1302 if (machine__is_default_guest(machine
)) {
1304 * preload dso of guest kernel and modules
1306 dso__load(kernel
, machine__kernel_map(machine
), NULL
);
1314 int machine__process_mmap2_event(struct machine
*machine
,
1315 union perf_event
*event
,
1316 struct perf_sample
*sample
)
1318 struct thread
*thread
;
1324 perf_event__fprintf_mmap2(event
, stdout
);
1326 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1327 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1328 ret
= machine__process_kernel_mmap_event(machine
, event
);
1334 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
,
1339 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1340 type
= MAP__VARIABLE
;
1342 type
= MAP__FUNCTION
;
1344 map
= map__new(machine
, event
->mmap2
.start
,
1345 event
->mmap2
.len
, event
->mmap2
.pgoff
,
1346 event
->mmap2
.pid
, event
->mmap2
.maj
,
1347 event
->mmap2
.min
, event
->mmap2
.ino
,
1348 event
->mmap2
.ino_generation
,
1351 event
->mmap2
.filename
, type
, thread
);
1354 goto out_problem_map
;
1356 ret
= thread__insert_map(thread
, map
);
1358 goto out_problem_insert
;
1360 thread__put(thread
);
1367 thread__put(thread
);
1369 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1373 int machine__process_mmap_event(struct machine
*machine
, union perf_event
*event
,
1374 struct perf_sample
*sample
)
1376 struct thread
*thread
;
1382 perf_event__fprintf_mmap(event
, stdout
);
1384 if (sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1385 sample
->cpumode
== PERF_RECORD_MISC_KERNEL
) {
1386 ret
= machine__process_kernel_mmap_event(machine
, event
);
1392 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
,
1397 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1398 type
= MAP__VARIABLE
;
1400 type
= MAP__FUNCTION
;
1402 map
= map__new(machine
, event
->mmap
.start
,
1403 event
->mmap
.len
, event
->mmap
.pgoff
,
1404 event
->mmap
.pid
, 0, 0, 0, 0, 0, 0,
1405 event
->mmap
.filename
,
1409 goto out_problem_map
;
1411 ret
= thread__insert_map(thread
, map
);
1413 goto out_problem_insert
;
1415 thread__put(thread
);
1422 thread__put(thread
);
1424 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1428 static void __machine__remove_thread(struct machine
*machine
, struct thread
*th
, bool lock
)
1430 if (machine
->last_match
== th
)
1431 machine
->last_match
= NULL
;
1433 BUG_ON(atomic_read(&th
->refcnt
) == 0);
1435 pthread_rwlock_wrlock(&machine
->threads_lock
);
1436 rb_erase_init(&th
->rb_node
, &machine
->threads
);
1437 RB_CLEAR_NODE(&th
->rb_node
);
1438 --machine
->nr_threads
;
1440 * Move it first to the dead_threads list, then drop the reference,
1441 * if this is the last reference, then the thread__delete destructor
1442 * will be called and we will remove it from the dead_threads list.
1444 list_add_tail(&th
->node
, &machine
->dead_threads
);
1446 pthread_rwlock_unlock(&machine
->threads_lock
);
1450 void machine__remove_thread(struct machine
*machine
, struct thread
*th
)
1452 return __machine__remove_thread(machine
, th
, true);
1455 int machine__process_fork_event(struct machine
*machine
, union perf_event
*event
,
1456 struct perf_sample
*sample
)
1458 struct thread
*thread
= machine__find_thread(machine
,
1461 struct thread
*parent
= machine__findnew_thread(machine
,
1467 perf_event__fprintf_task(event
, stdout
);
1470 * There may be an existing thread that is not actually the parent,
1471 * either because we are processing events out of order, or because the
1472 * (fork) event that would have removed the thread was lost. Assume the
1473 * latter case and continue on as best we can.
1475 if (parent
->pid_
!= (pid_t
)event
->fork
.ppid
) {
1476 dump_printf("removing erroneous parent thread %d/%d\n",
1477 parent
->pid_
, parent
->tid
);
1478 machine__remove_thread(machine
, parent
);
1479 thread__put(parent
);
1480 parent
= machine__findnew_thread(machine
, event
->fork
.ppid
,
1484 /* if a thread currently exists for the thread id remove it */
1485 if (thread
!= NULL
) {
1486 machine__remove_thread(machine
, thread
);
1487 thread__put(thread
);
1490 thread
= machine__findnew_thread(machine
, event
->fork
.pid
,
1493 if (thread
== NULL
|| parent
== NULL
||
1494 thread__fork(thread
, parent
, sample
->time
) < 0) {
1495 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1498 thread__put(thread
);
1499 thread__put(parent
);
1504 int machine__process_exit_event(struct machine
*machine
, union perf_event
*event
,
1505 struct perf_sample
*sample __maybe_unused
)
1507 struct thread
*thread
= machine__find_thread(machine
,
1512 perf_event__fprintf_task(event
, stdout
);
1514 if (thread
!= NULL
) {
1515 thread__exited(thread
);
1516 thread__put(thread
);
1522 int machine__process_event(struct machine
*machine
, union perf_event
*event
,
1523 struct perf_sample
*sample
)
1527 switch (event
->header
.type
) {
1528 case PERF_RECORD_COMM
:
1529 ret
= machine__process_comm_event(machine
, event
, sample
); break;
1530 case PERF_RECORD_MMAP
:
1531 ret
= machine__process_mmap_event(machine
, event
, sample
); break;
1532 case PERF_RECORD_MMAP2
:
1533 ret
= machine__process_mmap2_event(machine
, event
, sample
); break;
1534 case PERF_RECORD_FORK
:
1535 ret
= machine__process_fork_event(machine
, event
, sample
); break;
1536 case PERF_RECORD_EXIT
:
1537 ret
= machine__process_exit_event(machine
, event
, sample
); break;
1538 case PERF_RECORD_LOST
:
1539 ret
= machine__process_lost_event(machine
, event
, sample
); break;
1540 case PERF_RECORD_AUX
:
1541 ret
= machine__process_aux_event(machine
, event
); break;
1542 case PERF_RECORD_ITRACE_START
:
1543 ret
= machine__process_itrace_start_event(machine
, event
); break;
1544 case PERF_RECORD_LOST_SAMPLES
:
1545 ret
= machine__process_lost_samples_event(machine
, event
, sample
); break;
1546 case PERF_RECORD_SWITCH
:
1547 case PERF_RECORD_SWITCH_CPU_WIDE
:
1548 ret
= machine__process_switch_event(machine
, event
); break;
1557 static bool symbol__match_regex(struct symbol
*sym
, regex_t
*regex
)
1559 if (sym
->name
&& !regexec(regex
, sym
->name
, 0, NULL
, 0))
1564 static void ip__resolve_ams(struct thread
*thread
,
1565 struct addr_map_symbol
*ams
,
1568 struct addr_location al
;
1570 memset(&al
, 0, sizeof(al
));
1572 * We cannot use the header.misc hint to determine whether a
1573 * branch stack address is user, kernel, guest, hypervisor.
1574 * Branches may straddle the kernel/user/hypervisor boundaries.
1575 * Thus, we have to try consecutively until we find a match
1576 * or else, the symbol is unknown
1578 thread__find_cpumode_addr_location(thread
, MAP__FUNCTION
, ip
, &al
);
1581 ams
->al_addr
= al
.addr
;
1586 static void ip__resolve_data(struct thread
*thread
,
1587 u8 m
, struct addr_map_symbol
*ams
, u64 addr
)
1589 struct addr_location al
;
1591 memset(&al
, 0, sizeof(al
));
1593 thread__find_addr_location(thread
, m
, MAP__VARIABLE
, addr
, &al
);
1594 if (al
.map
== NULL
) {
1596 * some shared data regions have execute bit set which puts
1597 * their mapping in the MAP__FUNCTION type array.
1598 * Check there as a fallback option before dropping the sample.
1600 thread__find_addr_location(thread
, m
, MAP__FUNCTION
, addr
, &al
);
1604 ams
->al_addr
= al
.addr
;
1609 struct mem_info
*sample__resolve_mem(struct perf_sample
*sample
,
1610 struct addr_location
*al
)
1612 struct mem_info
*mi
= zalloc(sizeof(*mi
));
1617 ip__resolve_ams(al
->thread
, &mi
->iaddr
, sample
->ip
);
1618 ip__resolve_data(al
->thread
, al
->cpumode
, &mi
->daddr
, sample
->addr
);
1619 mi
->data_src
.val
= sample
->data_src
;
1624 static int add_callchain_ip(struct thread
*thread
,
1625 struct callchain_cursor
*cursor
,
1626 struct symbol
**parent
,
1627 struct addr_location
*root_al
,
1631 struct addr_location al
;
1636 thread__find_cpumode_addr_location(thread
, MAP__FUNCTION
,
1639 if (ip
>= PERF_CONTEXT_MAX
) {
1641 case PERF_CONTEXT_HV
:
1642 *cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1644 case PERF_CONTEXT_KERNEL
:
1645 *cpumode
= PERF_RECORD_MISC_KERNEL
;
1647 case PERF_CONTEXT_USER
:
1648 *cpumode
= PERF_RECORD_MISC_USER
;
1651 pr_debug("invalid callchain context: "
1652 "%"PRId64
"\n", (s64
) ip
);
1654 * It seems the callchain is corrupted.
1657 callchain_cursor_reset(cursor
);
1662 thread__find_addr_location(thread
, *cpumode
, MAP__FUNCTION
,
1666 if (al
.sym
!= NULL
) {
1667 if (perf_hpp_list
.parent
&& !*parent
&&
1668 symbol__match_regex(al
.sym
, &parent_regex
))
1670 else if (have_ignore_callees
&& root_al
&&
1671 symbol__match_regex(al
.sym
, &ignore_callees_regex
)) {
1672 /* Treat this symbol as the root,
1673 forgetting its callees. */
1675 callchain_cursor_reset(cursor
);
1679 if (symbol_conf
.hide_unresolved
&& al
.sym
== NULL
)
1681 return callchain_cursor_append(cursor
, al
.addr
, al
.map
, al
.sym
);
1684 struct branch_info
*sample__resolve_bstack(struct perf_sample
*sample
,
1685 struct addr_location
*al
)
1688 const struct branch_stack
*bs
= sample
->branch_stack
;
1689 struct branch_info
*bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
1694 for (i
= 0; i
< bs
->nr
; i
++) {
1695 ip__resolve_ams(al
->thread
, &bi
[i
].to
, bs
->entries
[i
].to
);
1696 ip__resolve_ams(al
->thread
, &bi
[i
].from
, bs
->entries
[i
].from
);
1697 bi
[i
].flags
= bs
->entries
[i
].flags
;
1704 #define NO_ENTRY 0xff
1706 #define PERF_MAX_BRANCH_DEPTH 127
1709 static int remove_loops(struct branch_entry
*l
, int nr
)
1712 unsigned char chash
[CHASHSZ
];
1714 memset(chash
, NO_ENTRY
, sizeof(chash
));
1716 BUG_ON(PERF_MAX_BRANCH_DEPTH
> 255);
1718 for (i
= 0; i
< nr
; i
++) {
1719 int h
= hash_64(l
[i
].from
, CHASHBITS
) % CHASHSZ
;
1721 /* no collision handling for now */
1722 if (chash
[h
] == NO_ENTRY
) {
1724 } else if (l
[chash
[h
]].from
== l
[i
].from
) {
1725 bool is_loop
= true;
1726 /* check if it is a real loop */
1728 for (j
= chash
[h
]; j
< i
&& i
+ off
< nr
; j
++, off
++)
1729 if (l
[j
].from
!= l
[i
+ off
].from
) {
1734 memmove(l
+ i
, l
+ i
+ off
,
1735 (nr
- (i
+ off
)) * sizeof(*l
));
1744 * Recolve LBR callstack chain sample
1746 * 1 on success get LBR callchain information
1747 * 0 no available LBR callchain information, should try fp
1748 * negative error code on other errors.
1750 static int resolve_lbr_callchain_sample(struct thread
*thread
,
1751 struct callchain_cursor
*cursor
,
1752 struct perf_sample
*sample
,
1753 struct symbol
**parent
,
1754 struct addr_location
*root_al
,
1757 struct ip_callchain
*chain
= sample
->callchain
;
1758 int chain_nr
= min(max_stack
, (int)chain
->nr
);
1759 u8 cpumode
= PERF_RECORD_MISC_USER
;
1763 for (i
= 0; i
< chain_nr
; i
++) {
1764 if (chain
->ips
[i
] == PERF_CONTEXT_USER
)
1768 /* LBR only affects the user callchain */
1769 if (i
!= chain_nr
) {
1770 struct branch_stack
*lbr_stack
= sample
->branch_stack
;
1771 int lbr_nr
= lbr_stack
->nr
;
1773 * LBR callstack can only get user call chain.
1774 * The mix_chain_nr is kernel call chain
1775 * number plus LBR user call chain number.
1776 * i is kernel call chain number,
1777 * 1 is PERF_CONTEXT_USER,
1778 * lbr_nr + 1 is the user call chain number.
1779 * For details, please refer to the comments
1780 * in callchain__printf
1782 int mix_chain_nr
= i
+ 1 + lbr_nr
+ 1;
1784 for (j
= 0; j
< mix_chain_nr
; j
++) {
1785 if (callchain_param
.order
== ORDER_CALLEE
) {
1789 ip
= lbr_stack
->entries
[j
- i
- 2].from
;
1791 ip
= lbr_stack
->entries
[0].to
;
1794 ip
= lbr_stack
->entries
[lbr_nr
- j
- 1].from
;
1795 else if (j
> lbr_nr
)
1796 ip
= chain
->ips
[i
+ 1 - (j
- lbr_nr
)];
1798 ip
= lbr_stack
->entries
[0].to
;
1801 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
, &cpumode
, ip
);
1803 return (err
< 0) ? err
: 0;
1811 static int thread__resolve_callchain_sample(struct thread
*thread
,
1812 struct callchain_cursor
*cursor
,
1813 struct perf_evsel
*evsel
,
1814 struct perf_sample
*sample
,
1815 struct symbol
**parent
,
1816 struct addr_location
*root_al
,
1819 struct branch_stack
*branch
= sample
->branch_stack
;
1820 struct ip_callchain
*chain
= sample
->callchain
;
1821 int chain_nr
= chain
->nr
;
1822 u8 cpumode
= PERF_RECORD_MISC_USER
;
1823 int i
, j
, err
, nr_entries
;
1827 if (perf_evsel__has_branch_callstack(evsel
)) {
1828 err
= resolve_lbr_callchain_sample(thread
, cursor
, sample
, parent
,
1829 root_al
, max_stack
);
1831 return (err
< 0) ? err
: 0;
1835 * Based on DWARF debug information, some architectures skip
1836 * a callchain entry saved by the kernel.
1838 skip_idx
= arch_skip_callchain_idx(thread
, chain
);
1841 * Add branches to call stack for easier browsing. This gives
1842 * more context for a sample than just the callers.
1844 * This uses individual histograms of paths compared to the
1845 * aggregated histograms the normal LBR mode uses.
1847 * Limitations for now:
1848 * - No extra filters
1849 * - No annotations (should annotate somehow)
1852 if (branch
&& callchain_param
.branch_callstack
) {
1853 int nr
= min(max_stack
, (int)branch
->nr
);
1854 struct branch_entry be
[nr
];
1856 if (branch
->nr
> PERF_MAX_BRANCH_DEPTH
) {
1857 pr_warning("corrupted branch chain. skipping...\n");
1861 for (i
= 0; i
< nr
; i
++) {
1862 if (callchain_param
.order
== ORDER_CALLEE
) {
1863 be
[i
] = branch
->entries
[i
];
1865 * Check for overlap into the callchain.
1866 * The return address is one off compared to
1867 * the branch entry. To adjust for this
1868 * assume the calling instruction is not longer
1871 if (i
== skip_idx
||
1872 chain
->ips
[first_call
] >= PERF_CONTEXT_MAX
)
1874 else if (be
[i
].from
< chain
->ips
[first_call
] &&
1875 be
[i
].from
>= chain
->ips
[first_call
] - 8)
1878 be
[i
] = branch
->entries
[branch
->nr
- i
- 1];
1881 nr
= remove_loops(be
, nr
);
1883 for (i
= 0; i
< nr
; i
++) {
1884 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
,
1887 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
,
1898 for (i
= first_call
, nr_entries
= 0;
1899 i
< chain_nr
&& nr_entries
< max_stack
; i
++) {
1902 if (callchain_param
.order
== ORDER_CALLEE
)
1905 j
= chain
->nr
- i
- 1;
1907 #ifdef HAVE_SKIP_CALLCHAIN_IDX
1913 if (ip
< PERF_CONTEXT_MAX
)
1916 err
= add_callchain_ip(thread
, cursor
, parent
, root_al
, &cpumode
, ip
);
1919 return (err
< 0) ? err
: 0;
1925 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
1927 struct callchain_cursor
*cursor
= arg
;
1929 if (symbol_conf
.hide_unresolved
&& entry
->sym
== NULL
)
1931 return callchain_cursor_append(cursor
, entry
->ip
,
1932 entry
->map
, entry
->sym
);
1935 static int thread__resolve_callchain_unwind(struct thread
*thread
,
1936 struct callchain_cursor
*cursor
,
1937 struct perf_evsel
*evsel
,
1938 struct perf_sample
*sample
,
1941 /* Can we do dwarf post unwind? */
1942 if (!((evsel
->attr
.sample_type
& PERF_SAMPLE_REGS_USER
) &&
1943 (evsel
->attr
.sample_type
& PERF_SAMPLE_STACK_USER
)))
1946 /* Bail out if nothing was captured. */
1947 if ((!sample
->user_regs
.regs
) ||
1948 (!sample
->user_stack
.size
))
1951 return unwind__get_entries(unwind_entry
, cursor
,
1952 thread
, sample
, max_stack
);
1955 int thread__resolve_callchain(struct thread
*thread
,
1956 struct callchain_cursor
*cursor
,
1957 struct perf_evsel
*evsel
,
1958 struct perf_sample
*sample
,
1959 struct symbol
**parent
,
1960 struct addr_location
*root_al
,
1965 callchain_cursor_reset(&callchain_cursor
);
1967 if (callchain_param
.order
== ORDER_CALLEE
) {
1968 ret
= thread__resolve_callchain_sample(thread
, cursor
,
1974 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
1978 ret
= thread__resolve_callchain_unwind(thread
, cursor
,
1983 ret
= thread__resolve_callchain_sample(thread
, cursor
,
1992 int machine__for_each_thread(struct machine
*machine
,
1993 int (*fn
)(struct thread
*thread
, void *p
),
1997 struct thread
*thread
;
2000 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
2001 thread
= rb_entry(nd
, struct thread
, rb_node
);
2002 rc
= fn(thread
, priv
);
2007 list_for_each_entry(thread
, &machine
->dead_threads
, node
) {
2008 rc
= fn(thread
, priv
);
2015 int machines__for_each_thread(struct machines
*machines
,
2016 int (*fn
)(struct thread
*thread
, void *p
),
2022 rc
= machine__for_each_thread(&machines
->host
, fn
, priv
);
2026 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
2027 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
2029 rc
= machine__for_each_thread(machine
, fn
, priv
);
2036 int __machine__synthesize_threads(struct machine
*machine
, struct perf_tool
*tool
,
2037 struct target
*target
, struct thread_map
*threads
,
2038 perf_event__handler_t process
, bool data_mmap
,
2039 unsigned int proc_map_timeout
)
2041 if (target__has_task(target
))
2042 return perf_event__synthesize_thread_map(tool
, threads
, process
, machine
, data_mmap
, proc_map_timeout
);
2043 else if (target__has_cpu(target
))
2044 return perf_event__synthesize_threads(tool
, process
, machine
, data_mmap
, proc_map_timeout
);
2045 /* command specified */
2049 pid_t
machine__get_current_tid(struct machine
*machine
, int cpu
)
2051 if (cpu
< 0 || cpu
>= MAX_NR_CPUS
|| !machine
->current_tid
)
2054 return machine
->current_tid
[cpu
];
2057 int machine__set_current_tid(struct machine
*machine
, int cpu
, pid_t pid
,
2060 struct thread
*thread
;
2065 if (!machine
->current_tid
) {
2068 machine
->current_tid
= calloc(MAX_NR_CPUS
, sizeof(pid_t
));
2069 if (!machine
->current_tid
)
2071 for (i
= 0; i
< MAX_NR_CPUS
; i
++)
2072 machine
->current_tid
[i
] = -1;
2075 if (cpu
>= MAX_NR_CPUS
) {
2076 pr_err("Requested CPU %d too large. ", cpu
);
2077 pr_err("Consider raising MAX_NR_CPUS\n");
2081 machine
->current_tid
[cpu
] = tid
;
2083 thread
= machine__findnew_thread(machine
, pid
, tid
);
2088 thread__put(thread
);
2093 int machine__get_kernel_start(struct machine
*machine
)
2095 struct map
*map
= machine__kernel_map(machine
);
2099 * The only addresses above 2^63 are kernel addresses of a 64-bit
2100 * kernel. Note that addresses are unsigned so that on a 32-bit system
2101 * all addresses including kernel addresses are less than 2^32. In
2102 * that case (32-bit system), if the kernel mapping is unknown, all
2103 * addresses will be assumed to be in user space - see
2104 * machine__kernel_ip().
2106 machine
->kernel_start
= 1ULL << 63;
2108 err
= map__load(map
, machine
->symbol_filter
);
2110 machine
->kernel_start
= map
->start
;
2115 struct dso
*machine__findnew_dso(struct machine
*machine
, const char *filename
)
2117 return dsos__findnew(&machine
->dsos
, filename
);
2120 char *machine__resolve_kernel_addr(void *vmachine
, unsigned long long *addrp
, char **modp
)
2122 struct machine
*machine
= vmachine
;
2124 struct symbol
*sym
= map_groups__find_symbol(&machine
->kmaps
, MAP__FUNCTION
, *addrp
, &map
, NULL
);
2129 *modp
= __map__is_kmodule(map
) ? (char *)map
->dso
->short_name
: NULL
;
2130 *addrp
= map
->unmap_ip(map
, sym
->start
);