13 #include <symbol/kallsyms.h>
16 int machine__init(struct machine
*machine
, const char *root_dir
, pid_t pid
)
18 map_groups__init(&machine
->kmaps
);
19 RB_CLEAR_NODE(&machine
->rb_node
);
20 INIT_LIST_HEAD(&machine
->user_dsos
.head
);
21 INIT_LIST_HEAD(&machine
->kernel_dsos
.head
);
23 machine
->threads
= RB_ROOT
;
24 INIT_LIST_HEAD(&machine
->dead_threads
);
25 machine
->last_match
= NULL
;
27 machine
->vdso_info
= NULL
;
29 machine
->kmaps
.machine
= machine
;
32 machine
->symbol_filter
= NULL
;
33 machine
->id_hdr_size
= 0;
34 machine
->comm_exec
= false;
35 machine
->kernel_start
= 0;
37 machine
->root_dir
= strdup(root_dir
);
38 if (machine
->root_dir
== NULL
)
41 if (pid
!= HOST_KERNEL_ID
) {
42 struct thread
*thread
= machine__findnew_thread(machine
, -1,
49 snprintf(comm
, sizeof(comm
), "[guest/%d]", pid
);
50 thread__set_comm(thread
, comm
, 0);
53 machine
->current_tid
= NULL
;
58 struct machine
*machine__new_host(void)
60 struct machine
*machine
= malloc(sizeof(*machine
));
62 if (machine
!= NULL
) {
63 machine__init(machine
, "", HOST_KERNEL_ID
);
65 if (machine__create_kernel_maps(machine
) < 0)
75 static void dsos__delete(struct dsos
*dsos
)
79 list_for_each_entry_safe(pos
, n
, &dsos
->head
, node
) {
85 void machine__delete_dead_threads(struct machine
*machine
)
89 list_for_each_entry_safe(t
, n
, &machine
->dead_threads
, node
) {
95 void machine__delete_threads(struct machine
*machine
)
97 struct rb_node
*nd
= rb_first(&machine
->threads
);
100 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
102 rb_erase(&t
->rb_node
, &machine
->threads
);
108 void machine__exit(struct machine
*machine
)
110 map_groups__exit(&machine
->kmaps
);
111 dsos__delete(&machine
->user_dsos
);
112 dsos__delete(&machine
->kernel_dsos
);
114 zfree(&machine
->root_dir
);
115 zfree(&machine
->current_tid
);
118 void machine__delete(struct machine
*machine
)
120 machine__exit(machine
);
124 void machines__init(struct machines
*machines
)
126 machine__init(&machines
->host
, "", HOST_KERNEL_ID
);
127 machines
->guests
= RB_ROOT
;
128 machines
->symbol_filter
= NULL
;
131 void machines__exit(struct machines
*machines
)
133 machine__exit(&machines
->host
);
137 struct machine
*machines__add(struct machines
*machines
, pid_t pid
,
138 const char *root_dir
)
140 struct rb_node
**p
= &machines
->guests
.rb_node
;
141 struct rb_node
*parent
= NULL
;
142 struct machine
*pos
, *machine
= malloc(sizeof(*machine
));
147 if (machine__init(machine
, root_dir
, pid
) != 0) {
152 machine
->symbol_filter
= machines
->symbol_filter
;
156 pos
= rb_entry(parent
, struct machine
, rb_node
);
163 rb_link_node(&machine
->rb_node
, parent
, p
);
164 rb_insert_color(&machine
->rb_node
, &machines
->guests
);
169 void machines__set_symbol_filter(struct machines
*machines
,
170 symbol_filter_t symbol_filter
)
174 machines
->symbol_filter
= symbol_filter
;
175 machines
->host
.symbol_filter
= symbol_filter
;
177 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
178 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
180 machine
->symbol_filter
= symbol_filter
;
184 void machines__set_comm_exec(struct machines
*machines
, bool comm_exec
)
188 machines
->host
.comm_exec
= comm_exec
;
190 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
191 struct machine
*machine
= rb_entry(nd
, struct machine
, rb_node
);
193 machine
->comm_exec
= comm_exec
;
197 struct machine
*machines__find(struct machines
*machines
, pid_t pid
)
199 struct rb_node
**p
= &machines
->guests
.rb_node
;
200 struct rb_node
*parent
= NULL
;
201 struct machine
*machine
;
202 struct machine
*default_machine
= NULL
;
204 if (pid
== HOST_KERNEL_ID
)
205 return &machines
->host
;
209 machine
= rb_entry(parent
, struct machine
, rb_node
);
210 if (pid
< machine
->pid
)
212 else if (pid
> machine
->pid
)
217 default_machine
= machine
;
220 return default_machine
;
223 struct machine
*machines__findnew(struct machines
*machines
, pid_t pid
)
226 const char *root_dir
= "";
227 struct machine
*machine
= machines__find(machines
, pid
);
229 if (machine
&& (machine
->pid
== pid
))
232 if ((pid
!= HOST_KERNEL_ID
) &&
233 (pid
!= DEFAULT_GUEST_KERNEL_ID
) &&
234 (symbol_conf
.guestmount
)) {
235 sprintf(path
, "%s/%d", symbol_conf
.guestmount
, pid
);
236 if (access(path
, R_OK
)) {
237 static struct strlist
*seen
;
240 seen
= strlist__new(true, NULL
);
242 if (!strlist__has_entry(seen
, path
)) {
243 pr_err("Can't access file %s\n", path
);
244 strlist__add(seen
, path
);
252 machine
= machines__add(machines
, pid
, root_dir
);
257 void machines__process_guests(struct machines
*machines
,
258 machine__process_t process
, void *data
)
262 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
263 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
268 char *machine__mmap_name(struct machine
*machine
, char *bf
, size_t size
)
270 if (machine__is_host(machine
))
271 snprintf(bf
, size
, "[%s]", "kernel.kallsyms");
272 else if (machine__is_default_guest(machine
))
273 snprintf(bf
, size
, "[%s]", "guest.kernel.kallsyms");
275 snprintf(bf
, size
, "[%s.%d]", "guest.kernel.kallsyms",
282 void machines__set_id_hdr_size(struct machines
*machines
, u16 id_hdr_size
)
284 struct rb_node
*node
;
285 struct machine
*machine
;
287 machines
->host
.id_hdr_size
= id_hdr_size
;
289 for (node
= rb_first(&machines
->guests
); node
; node
= rb_next(node
)) {
290 machine
= rb_entry(node
, struct machine
, rb_node
);
291 machine
->id_hdr_size
= id_hdr_size
;
297 static void machine__update_thread_pid(struct machine
*machine
,
298 struct thread
*th
, pid_t pid
)
300 struct thread
*leader
;
302 if (pid
== th
->pid_
|| pid
== -1 || th
->pid_
!= -1)
307 if (th
->pid_
== th
->tid
)
310 leader
= machine__findnew_thread(machine
, th
->pid_
, th
->pid_
);
315 leader
->mg
= map_groups__new();
320 if (th
->mg
== leader
->mg
)
325 * Maps are created from MMAP events which provide the pid and
326 * tid. Consequently there never should be any maps on a thread
327 * with an unknown pid. Just print an error if there are.
329 if (!map_groups__empty(th
->mg
))
330 pr_err("Discarding thread maps for %d:%d\n",
332 map_groups__delete(th
->mg
);
335 th
->mg
= map_groups__get(leader
->mg
);
340 pr_err("Failed to join map groups for %d:%d\n", th
->pid_
, th
->tid
);
343 static struct thread
*__machine__findnew_thread(struct machine
*machine
,
344 pid_t pid
, pid_t tid
,
347 struct rb_node
**p
= &machine
->threads
.rb_node
;
348 struct rb_node
*parent
= NULL
;
352 * Front-end cache - TID lookups come in blocks,
353 * so most of the time we dont have to look up
356 th
= machine
->last_match
;
357 if (th
&& th
->tid
== tid
) {
358 machine__update_thread_pid(machine
, th
, pid
);
364 th
= rb_entry(parent
, struct thread
, rb_node
);
366 if (th
->tid
== tid
) {
367 machine
->last_match
= th
;
368 machine__update_thread_pid(machine
, th
, pid
);
381 th
= thread__new(pid
, tid
);
383 rb_link_node(&th
->rb_node
, parent
, p
);
384 rb_insert_color(&th
->rb_node
, &machine
->threads
);
385 machine
->last_match
= th
;
388 * We have to initialize map_groups separately
389 * after rb tree is updated.
391 * The reason is that we call machine__findnew_thread
392 * within thread__init_map_groups to find the thread
393 * leader and that would screwed the rb tree.
395 if (thread__init_map_groups(th
, machine
)) {
404 struct thread
*machine__findnew_thread(struct machine
*machine
, pid_t pid
,
407 return __machine__findnew_thread(machine
, pid
, tid
, true);
410 struct thread
*machine__find_thread(struct machine
*machine
, pid_t pid
,
413 return __machine__findnew_thread(machine
, pid
, tid
, false);
416 struct comm
*machine__thread_exec_comm(struct machine
*machine
,
417 struct thread
*thread
)
419 if (machine
->comm_exec
)
420 return thread__exec_comm(thread
);
422 return thread__comm(thread
);
425 int machine__process_comm_event(struct machine
*machine
, union perf_event
*event
,
426 struct perf_sample
*sample
)
428 struct thread
*thread
= machine__findnew_thread(machine
,
431 bool exec
= event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
;
434 machine
->comm_exec
= true;
437 perf_event__fprintf_comm(event
, stdout
);
439 if (thread
== NULL
||
440 __thread__set_comm(thread
, event
->comm
.comm
, sample
->time
, exec
)) {
441 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
448 int machine__process_lost_event(struct machine
*machine __maybe_unused
,
449 union perf_event
*event
, struct perf_sample
*sample __maybe_unused
)
451 dump_printf(": id:%" PRIu64
": lost:%" PRIu64
"\n",
452 event
->lost
.id
, event
->lost
.lost
);
456 struct map
*machine__new_module(struct machine
*machine
, u64 start
,
457 const char *filename
)
460 struct dso
*dso
= __dsos__findnew(&machine
->kernel_dsos
, filename
);
465 map
= map__new2(start
, dso
, MAP__FUNCTION
);
469 if (machine__is_host(machine
))
470 dso
->symtab_type
= DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
;
472 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KMODULE
;
473 map_groups__insert(&machine
->kmaps
, map
);
477 size_t machines__fprintf_dsos(struct machines
*machines
, FILE *fp
)
480 size_t ret
= __dsos__fprintf(&machines
->host
.kernel_dsos
.head
, fp
) +
481 __dsos__fprintf(&machines
->host
.user_dsos
.head
, fp
);
483 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
484 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
485 ret
+= __dsos__fprintf(&pos
->kernel_dsos
.head
, fp
);
486 ret
+= __dsos__fprintf(&pos
->user_dsos
.head
, fp
);
492 size_t machine__fprintf_dsos_buildid(struct machine
*m
, FILE *fp
,
493 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
495 return __dsos__fprintf_buildid(&m
->kernel_dsos
.head
, fp
, skip
, parm
) +
496 __dsos__fprintf_buildid(&m
->user_dsos
.head
, fp
, skip
, parm
);
499 size_t machines__fprintf_dsos_buildid(struct machines
*machines
, FILE *fp
,
500 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
503 size_t ret
= machine__fprintf_dsos_buildid(&machines
->host
, fp
, skip
, parm
);
505 for (nd
= rb_first(&machines
->guests
); nd
; nd
= rb_next(nd
)) {
506 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
507 ret
+= machine__fprintf_dsos_buildid(pos
, fp
, skip
, parm
);
512 size_t machine__fprintf_vmlinux_path(struct machine
*machine
, FILE *fp
)
516 struct dso
*kdso
= machine
->vmlinux_maps
[MAP__FUNCTION
]->dso
;
518 if (kdso
->has_build_id
) {
519 char filename
[PATH_MAX
];
520 if (dso__build_id_filename(kdso
, filename
, sizeof(filename
)))
521 printed
+= fprintf(fp
, "[0] %s\n", filename
);
524 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
)
525 printed
+= fprintf(fp
, "[%d] %s\n",
526 i
+ kdso
->has_build_id
, vmlinux_path
[i
]);
531 size_t machine__fprintf(struct machine
*machine
, FILE *fp
)
536 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
537 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
539 ret
+= thread__fprintf(pos
, fp
);
545 static struct dso
*machine__get_kernel(struct machine
*machine
)
547 const char *vmlinux_name
= NULL
;
550 if (machine__is_host(machine
)) {
551 vmlinux_name
= symbol_conf
.vmlinux_name
;
553 vmlinux_name
= "[kernel.kallsyms]";
555 kernel
= dso__kernel_findnew(machine
, vmlinux_name
,
561 if (machine__is_default_guest(machine
))
562 vmlinux_name
= symbol_conf
.default_guest_vmlinux_name
;
564 vmlinux_name
= machine__mmap_name(machine
, bf
,
567 kernel
= dso__kernel_findnew(machine
, vmlinux_name
,
569 DSO_TYPE_GUEST_KERNEL
);
572 if (kernel
!= NULL
&& (!kernel
->has_build_id
))
573 dso__read_running_kernel_build_id(kernel
, machine
);
578 struct process_args
{
582 static void machine__get_kallsyms_filename(struct machine
*machine
, char *buf
,
585 if (machine__is_default_guest(machine
))
586 scnprintf(buf
, bufsz
, "%s", symbol_conf
.default_guest_kallsyms
);
588 scnprintf(buf
, bufsz
, "%s/proc/kallsyms", machine
->root_dir
);
591 const char *ref_reloc_sym_names
[] = {"_text", "_stext", NULL
};
593 /* Figure out the start address of kernel map from /proc/kallsyms.
594 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
595 * symbol_name if it's not that important.
597 static u64
machine__get_running_kernel_start(struct machine
*machine
,
598 const char **symbol_name
)
600 char filename
[PATH_MAX
];
605 machine__get_kallsyms_filename(machine
, filename
, PATH_MAX
);
607 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
610 for (i
= 0; (name
= ref_reloc_sym_names
[i
]) != NULL
; i
++) {
611 addr
= kallsyms__get_function_start(filename
, name
);
622 int __machine__create_kernel_maps(struct machine
*machine
, struct dso
*kernel
)
625 u64 start
= machine__get_running_kernel_start(machine
, NULL
);
627 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
630 machine
->vmlinux_maps
[type
] = map__new2(start
, kernel
, type
);
631 if (machine
->vmlinux_maps
[type
] == NULL
)
634 machine
->vmlinux_maps
[type
]->map_ip
=
635 machine
->vmlinux_maps
[type
]->unmap_ip
=
637 kmap
= map__kmap(machine
->vmlinux_maps
[type
]);
638 kmap
->kmaps
= &machine
->kmaps
;
639 map_groups__insert(&machine
->kmaps
,
640 machine
->vmlinux_maps
[type
]);
646 void machine__destroy_kernel_maps(struct machine
*machine
)
650 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
653 if (machine
->vmlinux_maps
[type
] == NULL
)
656 kmap
= map__kmap(machine
->vmlinux_maps
[type
]);
657 map_groups__remove(&machine
->kmaps
,
658 machine
->vmlinux_maps
[type
]);
659 if (kmap
->ref_reloc_sym
) {
661 * ref_reloc_sym is shared among all maps, so free just
664 if (type
== MAP__FUNCTION
) {
665 zfree((char **)&kmap
->ref_reloc_sym
->name
);
666 zfree(&kmap
->ref_reloc_sym
);
668 kmap
->ref_reloc_sym
= NULL
;
671 map__delete(machine
->vmlinux_maps
[type
]);
672 machine
->vmlinux_maps
[type
] = NULL
;
676 int machines__create_guest_kernel_maps(struct machines
*machines
)
679 struct dirent
**namelist
= NULL
;
685 if (symbol_conf
.default_guest_vmlinux_name
||
686 symbol_conf
.default_guest_modules
||
687 symbol_conf
.default_guest_kallsyms
) {
688 machines__create_kernel_maps(machines
, DEFAULT_GUEST_KERNEL_ID
);
691 if (symbol_conf
.guestmount
) {
692 items
= scandir(symbol_conf
.guestmount
, &namelist
, NULL
, NULL
);
695 for (i
= 0; i
< items
; i
++) {
696 if (!isdigit(namelist
[i
]->d_name
[0])) {
697 /* Filter out . and .. */
700 pid
= (pid_t
)strtol(namelist
[i
]->d_name
, &endp
, 10);
701 if ((*endp
!= '\0') ||
702 (endp
== namelist
[i
]->d_name
) ||
704 pr_debug("invalid directory (%s). Skipping.\n",
705 namelist
[i
]->d_name
);
708 sprintf(path
, "%s/%s/proc/kallsyms",
709 symbol_conf
.guestmount
,
710 namelist
[i
]->d_name
);
711 ret
= access(path
, R_OK
);
713 pr_debug("Can't access file %s\n", path
);
716 machines__create_kernel_maps(machines
, pid
);
725 void machines__destroy_kernel_maps(struct machines
*machines
)
727 struct rb_node
*next
= rb_first(&machines
->guests
);
729 machine__destroy_kernel_maps(&machines
->host
);
732 struct machine
*pos
= rb_entry(next
, struct machine
, rb_node
);
734 next
= rb_next(&pos
->rb_node
);
735 rb_erase(&pos
->rb_node
, &machines
->guests
);
736 machine__delete(pos
);
740 int machines__create_kernel_maps(struct machines
*machines
, pid_t pid
)
742 struct machine
*machine
= machines__findnew(machines
, pid
);
747 return machine__create_kernel_maps(machine
);
750 int machine__load_kallsyms(struct machine
*machine
, const char *filename
,
751 enum map_type type
, symbol_filter_t filter
)
753 struct map
*map
= machine
->vmlinux_maps
[type
];
754 int ret
= dso__load_kallsyms(map
->dso
, filename
, map
, filter
);
757 dso__set_loaded(map
->dso
, type
);
759 * Since /proc/kallsyms will have multiple sessions for the
760 * kernel, with modules between them, fixup the end of all
763 __map_groups__fixup_end(&machine
->kmaps
, type
);
769 int machine__load_vmlinux_path(struct machine
*machine
, enum map_type type
,
770 symbol_filter_t filter
)
772 struct map
*map
= machine
->vmlinux_maps
[type
];
773 int ret
= dso__load_vmlinux_path(map
->dso
, map
, filter
);
776 dso__set_loaded(map
->dso
, type
);
781 static void map_groups__fixup_end(struct map_groups
*mg
)
784 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
785 __map_groups__fixup_end(mg
, i
);
788 static char *get_kernel_version(const char *root_dir
)
790 char version
[PATH_MAX
];
793 const char *prefix
= "Linux version ";
795 sprintf(version
, "%s/proc/version", root_dir
);
796 file
= fopen(version
, "r");
801 tmp
= fgets(version
, sizeof(version
), file
);
804 name
= strstr(version
, prefix
);
807 name
+= strlen(prefix
);
808 tmp
= strchr(name
, ' ');
815 static int map_groups__set_modules_path_dir(struct map_groups
*mg
,
816 const char *dir_name
, int depth
)
819 DIR *dir
= opendir(dir_name
);
823 pr_debug("%s: cannot open %s dir\n", __func__
, dir_name
);
827 while ((dent
= readdir(dir
)) != NULL
) {
831 /*sshfs might return bad dent->d_type, so we have to stat*/
832 snprintf(path
, sizeof(path
), "%s/%s", dir_name
, dent
->d_name
);
836 if (S_ISDIR(st
.st_mode
)) {
837 if (!strcmp(dent
->d_name
, ".") ||
838 !strcmp(dent
->d_name
, ".."))
841 /* Do not follow top-level source and build symlinks */
843 if (!strcmp(dent
->d_name
, "source") ||
844 !strcmp(dent
->d_name
, "build"))
848 ret
= map_groups__set_modules_path_dir(mg
, path
,
853 char *dot
= strrchr(dent
->d_name
, '.'),
858 if (dot
== NULL
|| strcmp(dot
, ".ko"))
860 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
861 (int)(dot
- dent
->d_name
), dent
->d_name
);
863 strxfrchar(dso_name
, '-', '_');
864 map
= map_groups__find_by_name(mg
, MAP__FUNCTION
,
869 long_name
= strdup(path
);
870 if (long_name
== NULL
) {
874 dso__set_long_name(map
->dso
, long_name
, true);
875 dso__kernel_module_get_build_id(map
->dso
, "");
884 static int machine__set_modules_path(struct machine
*machine
)
887 char modules_path
[PATH_MAX
];
889 version
= get_kernel_version(machine
->root_dir
);
893 snprintf(modules_path
, sizeof(modules_path
), "%s/lib/modules/%s",
894 machine
->root_dir
, version
);
897 return map_groups__set_modules_path_dir(&machine
->kmaps
, modules_path
, 0);
900 static int machine__create_module(void *arg
, const char *name
, u64 start
)
902 struct machine
*machine
= arg
;
905 map
= machine__new_module(machine
, start
, name
);
909 dso__kernel_module_get_build_id(map
->dso
, machine
->root_dir
);
914 static int machine__create_modules(struct machine
*machine
)
919 if (machine__is_default_guest(machine
)) {
920 modules
= symbol_conf
.default_guest_modules
;
922 snprintf(path
, PATH_MAX
, "%s/proc/modules", machine
->root_dir
);
926 if (symbol__restricted_filename(modules
, "/proc/modules"))
929 if (modules__parse(modules
, machine
, machine__create_module
))
932 if (!machine__set_modules_path(machine
))
935 pr_debug("Problems setting modules path maps, continuing anyway...\n");
940 int machine__create_kernel_maps(struct machine
*machine
)
942 struct dso
*kernel
= machine__get_kernel(machine
);
944 u64 addr
= machine__get_running_kernel_start(machine
, &name
);
948 if (kernel
== NULL
||
949 __machine__create_kernel_maps(machine
, kernel
) < 0)
952 if (symbol_conf
.use_modules
&& machine__create_modules(machine
) < 0) {
953 if (machine__is_host(machine
))
954 pr_debug("Problems creating module maps, "
955 "continuing anyway...\n");
957 pr_debug("Problems creating module maps for guest %d, "
958 "continuing anyway...\n", machine
->pid
);
962 * Now that we have all the maps created, just set the ->end of them:
964 map_groups__fixup_end(&machine
->kmaps
);
966 if (maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
, name
,
968 machine__destroy_kernel_maps(machine
);
975 static void machine__set_kernel_mmap_len(struct machine
*machine
,
976 union perf_event
*event
)
980 for (i
= 0; i
< MAP__NR_TYPES
; i
++) {
981 machine
->vmlinux_maps
[i
]->start
= event
->mmap
.start
;
982 machine
->vmlinux_maps
[i
]->end
= (event
->mmap
.start
+
985 * Be a bit paranoid here, some perf.data file came with
986 * a zero sized synthesized MMAP event for the kernel.
988 if (machine
->vmlinux_maps
[i
]->end
== 0)
989 machine
->vmlinux_maps
[i
]->end
= ~0ULL;
993 static bool machine__uses_kcore(struct machine
*machine
)
997 list_for_each_entry(dso
, &machine
->kernel_dsos
.head
, node
) {
998 if (dso__is_kcore(dso
))
1005 static int machine__process_kernel_mmap_event(struct machine
*machine
,
1006 union perf_event
*event
)
1009 char kmmap_prefix
[PATH_MAX
];
1010 enum dso_kernel_type kernel_type
;
1011 bool is_kernel_mmap
;
1013 /* If we have maps from kcore then we do not need or want any others */
1014 if (machine__uses_kcore(machine
))
1017 machine__mmap_name(machine
, kmmap_prefix
, sizeof(kmmap_prefix
));
1018 if (machine__is_host(machine
))
1019 kernel_type
= DSO_TYPE_KERNEL
;
1021 kernel_type
= DSO_TYPE_GUEST_KERNEL
;
1023 is_kernel_mmap
= memcmp(event
->mmap
.filename
,
1025 strlen(kmmap_prefix
) - 1) == 0;
1026 if (event
->mmap
.filename
[0] == '/' ||
1027 (!is_kernel_mmap
&& event
->mmap
.filename
[0] == '[')) {
1029 char short_module_name
[1024];
1032 if (event
->mmap
.filename
[0] == '/') {
1033 name
= strrchr(event
->mmap
.filename
, '/');
1037 ++name
; /* skip / */
1038 dot
= strrchr(name
, '.');
1041 snprintf(short_module_name
, sizeof(short_module_name
),
1042 "[%.*s]", (int)(dot
- name
), name
);
1043 strxfrchar(short_module_name
, '-', '_');
1045 strcpy(short_module_name
, event
->mmap
.filename
);
1047 map
= machine__new_module(machine
, event
->mmap
.start
,
1048 event
->mmap
.filename
);
1052 name
= strdup(short_module_name
);
1056 dso__set_short_name(map
->dso
, name
, true);
1057 map
->end
= map
->start
+ event
->mmap
.len
;
1058 } else if (is_kernel_mmap
) {
1059 const char *symbol_name
= (event
->mmap
.filename
+
1060 strlen(kmmap_prefix
));
1062 * Should be there already, from the build-id table in
1065 struct dso
*kernel
= __dsos__findnew(&machine
->kernel_dsos
,
1070 kernel
->kernel
= kernel_type
;
1071 if (__machine__create_kernel_maps(machine
, kernel
) < 0)
1074 machine__set_kernel_mmap_len(machine
, event
);
1077 * Avoid using a zero address (kptr_restrict) for the ref reloc
1078 * symbol. Effectively having zero here means that at record
1079 * time /proc/sys/kernel/kptr_restrict was non zero.
1081 if (event
->mmap
.pgoff
!= 0) {
1082 maps__set_kallsyms_ref_reloc_sym(machine
->vmlinux_maps
,
1087 if (machine__is_default_guest(machine
)) {
1089 * preload dso of guest kernel and modules
1091 dso__load(kernel
, machine
->vmlinux_maps
[MAP__FUNCTION
],
1100 int machine__process_mmap2_event(struct machine
*machine
,
1101 union perf_event
*event
,
1102 struct perf_sample
*sample __maybe_unused
)
1104 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1105 struct thread
*thread
;
1111 perf_event__fprintf_mmap2(event
, stdout
);
1113 if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1114 cpumode
== PERF_RECORD_MISC_KERNEL
) {
1115 ret
= machine__process_kernel_mmap_event(machine
, event
);
1121 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
,
1126 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1127 type
= MAP__VARIABLE
;
1129 type
= MAP__FUNCTION
;
1131 map
= map__new(machine
, event
->mmap2
.start
,
1132 event
->mmap2
.len
, event
->mmap2
.pgoff
,
1133 event
->mmap2
.pid
, event
->mmap2
.maj
,
1134 event
->mmap2
.min
, event
->mmap2
.ino
,
1135 event
->mmap2
.ino_generation
,
1138 event
->mmap2
.filename
, type
, thread
);
1143 thread__insert_map(thread
, map
);
1147 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1151 int machine__process_mmap_event(struct machine
*machine
, union perf_event
*event
,
1152 struct perf_sample
*sample __maybe_unused
)
1154 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1155 struct thread
*thread
;
1161 perf_event__fprintf_mmap(event
, stdout
);
1163 if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
||
1164 cpumode
== PERF_RECORD_MISC_KERNEL
) {
1165 ret
= machine__process_kernel_mmap_event(machine
, event
);
1171 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
,
1176 if (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
)
1177 type
= MAP__VARIABLE
;
1179 type
= MAP__FUNCTION
;
1181 map
= map__new(machine
, event
->mmap
.start
,
1182 event
->mmap
.len
, event
->mmap
.pgoff
,
1183 event
->mmap
.pid
, 0, 0, 0, 0, 0, 0,
1184 event
->mmap
.filename
,
1190 thread__insert_map(thread
, map
);
1194 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1198 static void machine__remove_thread(struct machine
*machine
, struct thread
*th
)
1200 machine
->last_match
= NULL
;
1201 rb_erase(&th
->rb_node
, &machine
->threads
);
1203 * We may have references to this thread, for instance in some hist_entry
1204 * instances, so just move them to a separate list.
1206 list_add_tail(&th
->node
, &machine
->dead_threads
);
1209 int machine__process_fork_event(struct machine
*machine
, union perf_event
*event
,
1210 struct perf_sample
*sample
)
1212 struct thread
*thread
= machine__find_thread(machine
,
1215 struct thread
*parent
= machine__findnew_thread(machine
,
1219 /* if a thread currently exists for the thread id remove it */
1221 machine__remove_thread(machine
, thread
);
1223 thread
= machine__findnew_thread(machine
, event
->fork
.pid
,
1226 perf_event__fprintf_task(event
, stdout
);
1228 if (thread
== NULL
|| parent
== NULL
||
1229 thread__fork(thread
, parent
, sample
->time
) < 0) {
1230 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1237 int machine__process_exit_event(struct machine
*machine
, union perf_event
*event
,
1238 struct perf_sample
*sample __maybe_unused
)
1240 struct thread
*thread
= machine__find_thread(machine
,
1245 perf_event__fprintf_task(event
, stdout
);
1248 thread__exited(thread
);
1253 int machine__process_event(struct machine
*machine
, union perf_event
*event
,
1254 struct perf_sample
*sample
)
1258 switch (event
->header
.type
) {
1259 case PERF_RECORD_COMM
:
1260 ret
= machine__process_comm_event(machine
, event
, sample
); break;
1261 case PERF_RECORD_MMAP
:
1262 ret
= machine__process_mmap_event(machine
, event
, sample
); break;
1263 case PERF_RECORD_MMAP2
:
1264 ret
= machine__process_mmap2_event(machine
, event
, sample
); break;
1265 case PERF_RECORD_FORK
:
1266 ret
= machine__process_fork_event(machine
, event
, sample
); break;
1267 case PERF_RECORD_EXIT
:
1268 ret
= machine__process_exit_event(machine
, event
, sample
); break;
1269 case PERF_RECORD_LOST
:
1270 ret
= machine__process_lost_event(machine
, event
, sample
); break;
1279 static bool symbol__match_regex(struct symbol
*sym
, regex_t
*regex
)
1281 if (sym
->name
&& !regexec(regex
, sym
->name
, 0, NULL
, 0))
1286 static void ip__resolve_ams(struct machine
*machine
, struct thread
*thread
,
1287 struct addr_map_symbol
*ams
,
1290 struct addr_location al
;
1292 memset(&al
, 0, sizeof(al
));
1294 * We cannot use the header.misc hint to determine whether a
1295 * branch stack address is user, kernel, guest, hypervisor.
1296 * Branches may straddle the kernel/user/hypervisor boundaries.
1297 * Thus, we have to try consecutively until we find a match
1298 * or else, the symbol is unknown
1300 thread__find_cpumode_addr_location(thread
, machine
, MAP__FUNCTION
, ip
, &al
);
1303 ams
->al_addr
= al
.addr
;
1308 static void ip__resolve_data(struct machine
*machine
, struct thread
*thread
,
1309 u8 m
, struct addr_map_symbol
*ams
, u64 addr
)
1311 struct addr_location al
;
1313 memset(&al
, 0, sizeof(al
));
1315 thread__find_addr_location(thread
, machine
, m
, MAP__VARIABLE
, addr
,
1317 if (al
.map
== NULL
) {
1319 * some shared data regions have execute bit set which puts
1320 * their mapping in the MAP__FUNCTION type array.
1321 * Check there as a fallback option before dropping the sample.
1323 thread__find_addr_location(thread
, machine
, m
, MAP__FUNCTION
, addr
,
1328 ams
->al_addr
= al
.addr
;
1333 struct mem_info
*sample__resolve_mem(struct perf_sample
*sample
,
1334 struct addr_location
*al
)
1336 struct mem_info
*mi
= zalloc(sizeof(*mi
));
1341 ip__resolve_ams(al
->machine
, al
->thread
, &mi
->iaddr
, sample
->ip
);
1342 ip__resolve_data(al
->machine
, al
->thread
, al
->cpumode
,
1343 &mi
->daddr
, sample
->addr
);
1344 mi
->data_src
.val
= sample
->data_src
;
1349 struct branch_info
*sample__resolve_bstack(struct perf_sample
*sample
,
1350 struct addr_location
*al
)
1353 const struct branch_stack
*bs
= sample
->branch_stack
;
1354 struct branch_info
*bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
1359 for (i
= 0; i
< bs
->nr
; i
++) {
1360 ip__resolve_ams(al
->machine
, al
->thread
, &bi
[i
].to
, bs
->entries
[i
].to
);
1361 ip__resolve_ams(al
->machine
, al
->thread
, &bi
[i
].from
, bs
->entries
[i
].from
);
1362 bi
[i
].flags
= bs
->entries
[i
].flags
;
1367 static int machine__resolve_callchain_sample(struct machine
*machine
,
1368 struct thread
*thread
,
1369 struct ip_callchain
*chain
,
1370 struct symbol
**parent
,
1371 struct addr_location
*root_al
,
1374 u8 cpumode
= PERF_RECORD_MISC_USER
;
1375 int chain_nr
= min(max_stack
, (int)chain
->nr
);
1379 int skip_idx __maybe_unused
;
1381 callchain_cursor_reset(&callchain_cursor
);
1383 if (chain
->nr
> PERF_MAX_STACK_DEPTH
) {
1384 pr_warning("corrupted callchain. skipping...\n");
1389 * Based on DWARF debug information, some architectures skip
1390 * a callchain entry saved by the kernel.
1392 skip_idx
= arch_skip_callchain_idx(machine
, thread
, chain
);
1394 for (i
= 0; i
< chain_nr
; i
++) {
1396 struct addr_location al
;
1398 if (callchain_param
.order
== ORDER_CALLEE
)
1401 j
= chain
->nr
- i
- 1;
1403 #ifdef HAVE_SKIP_CALLCHAIN_IDX
1409 if (ip
>= PERF_CONTEXT_MAX
) {
1411 case PERF_CONTEXT_HV
:
1412 cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1414 case PERF_CONTEXT_KERNEL
:
1415 cpumode
= PERF_RECORD_MISC_KERNEL
;
1417 case PERF_CONTEXT_USER
:
1418 cpumode
= PERF_RECORD_MISC_USER
;
1421 pr_debug("invalid callchain context: "
1422 "%"PRId64
"\n", (s64
) ip
);
1424 * It seems the callchain is corrupted.
1427 callchain_cursor_reset(&callchain_cursor
);
1434 thread__find_addr_location(thread
, machine
, cpumode
,
1435 MAP__FUNCTION
, ip
, &al
);
1436 if (al
.sym
!= NULL
) {
1437 if (sort__has_parent
&& !*parent
&&
1438 symbol__match_regex(al
.sym
, &parent_regex
))
1440 else if (have_ignore_callees
&& root_al
&&
1441 symbol__match_regex(al
.sym
, &ignore_callees_regex
)) {
1442 /* Treat this symbol as the root,
1443 forgetting its callees. */
1445 callchain_cursor_reset(&callchain_cursor
);
1449 err
= callchain_cursor_append(&callchain_cursor
,
1450 ip
, al
.map
, al
.sym
);
1458 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
1460 struct callchain_cursor
*cursor
= arg
;
1461 return callchain_cursor_append(cursor
, entry
->ip
,
1462 entry
->map
, entry
->sym
);
1465 int machine__resolve_callchain(struct machine
*machine
,
1466 struct perf_evsel
*evsel
,
1467 struct thread
*thread
,
1468 struct perf_sample
*sample
,
1469 struct symbol
**parent
,
1470 struct addr_location
*root_al
,
1475 ret
= machine__resolve_callchain_sample(machine
, thread
,
1476 sample
->callchain
, parent
,
1477 root_al
, max_stack
);
1481 /* Can we do dwarf post unwind? */
1482 if (!((evsel
->attr
.sample_type
& PERF_SAMPLE_REGS_USER
) &&
1483 (evsel
->attr
.sample_type
& PERF_SAMPLE_STACK_USER
)))
1486 /* Bail out if nothing was captured. */
1487 if ((!sample
->user_regs
.regs
) ||
1488 (!sample
->user_stack
.size
))
1491 return unwind__get_entries(unwind_entry
, &callchain_cursor
, machine
,
1492 thread
, sample
, max_stack
);
1496 int machine__for_each_thread(struct machine
*machine
,
1497 int (*fn
)(struct thread
*thread
, void *p
),
1501 struct thread
*thread
;
1504 for (nd
= rb_first(&machine
->threads
); nd
; nd
= rb_next(nd
)) {
1505 thread
= rb_entry(nd
, struct thread
, rb_node
);
1506 rc
= fn(thread
, priv
);
1511 list_for_each_entry(thread
, &machine
->dead_threads
, node
) {
1512 rc
= fn(thread
, priv
);
1519 int __machine__synthesize_threads(struct machine
*machine
, struct perf_tool
*tool
,
1520 struct target
*target
, struct thread_map
*threads
,
1521 perf_event__handler_t process
, bool data_mmap
)
1523 if (target__has_task(target
))
1524 return perf_event__synthesize_thread_map(tool
, threads
, process
, machine
, data_mmap
);
1525 else if (target__has_cpu(target
))
1526 return perf_event__synthesize_threads(tool
, process
, machine
, data_mmap
);
1527 /* command specified */
1531 pid_t
machine__get_current_tid(struct machine
*machine
, int cpu
)
1533 if (cpu
< 0 || cpu
>= MAX_NR_CPUS
|| !machine
->current_tid
)
1536 return machine
->current_tid
[cpu
];
1539 int machine__set_current_tid(struct machine
*machine
, int cpu
, pid_t pid
,
1542 struct thread
*thread
;
1547 if (!machine
->current_tid
) {
1550 machine
->current_tid
= calloc(MAX_NR_CPUS
, sizeof(pid_t
));
1551 if (!machine
->current_tid
)
1553 for (i
= 0; i
< MAX_NR_CPUS
; i
++)
1554 machine
->current_tid
[i
] = -1;
1557 if (cpu
>= MAX_NR_CPUS
) {
1558 pr_err("Requested CPU %d too large. ", cpu
);
1559 pr_err("Consider raising MAX_NR_CPUS\n");
1563 machine
->current_tid
[cpu
] = tid
;
1565 thread
= machine__findnew_thread(machine
, pid
, tid
);
1574 int machine__get_kernel_start(struct machine
*machine
)
1576 struct map
*map
= machine__kernel_map(machine
, MAP__FUNCTION
);
1580 * The only addresses above 2^63 are kernel addresses of a 64-bit
1581 * kernel. Note that addresses are unsigned so that on a 32-bit system
1582 * all addresses including kernel addresses are less than 2^32. In
1583 * that case (32-bit system), if the kernel mapping is unknown, all
1584 * addresses will be assumed to be in user space - see
1585 * machine__kernel_ip().
1587 machine
->kernel_start
= 1ULL << 63;
1589 err
= map__load(map
, machine
->symbol_filter
);
1591 machine
->kernel_start
= map
->start
;