Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <regex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "callchain.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "env.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "hist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "machine.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "map_symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "branch.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "mem-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "srcline.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "sort.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "strlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "target.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "thread.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "vdso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "unwind.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "linux/hash.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include "asm/bug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "bpf-event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <internal/lib.h> // page_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "cgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <symbol/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static struct dso *machine__kernel_dso(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	return machine->vmlinux_map->dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static void dsos__init(struct dsos *dsos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	INIT_LIST_HEAD(&dsos->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	dsos->root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	init_rwsem(&dsos->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) static void machine__threads_init(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	for (i = 0; i < THREADS__TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		struct threads *threads = &machine->threads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		threads->entries = RB_ROOT_CACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		init_rwsem(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		threads->nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		INIT_LIST_HEAD(&threads->dead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		threads->last_match = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) static int machine__set_mmap_name(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (machine__is_host(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		machine->mmap_name = strdup("[kernel.kallsyms]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	else if (machine__is_default_guest(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		machine->mmap_name = strdup("[guest.kernel.kallsyms]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			  machine->pid) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		machine->mmap_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	return machine->mmap_name ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	memset(machine, 0, sizeof(*machine));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	maps__init(&machine->kmaps, machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	RB_CLEAR_NODE(&machine->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	dsos__init(&machine->dsos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	machine__threads_init(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	machine->vdso_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	machine->env = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	machine->pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	machine->id_hdr_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	machine->kptr_restrict_warned = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	machine->comm_exec = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	machine->kernel_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	machine->vmlinux_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	machine->root_dir = strdup(root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	if (machine->root_dir == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	if (machine__set_mmap_name(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (pid != HOST_KERNEL_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		struct thread *thread = machine__findnew_thread(machine, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 								pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		char comm[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		if (thread == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		snprintf(comm, sizeof(comm), "[guest/%d]", pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		thread__set_comm(thread, comm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	machine->current_tid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		zfree(&machine->root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		zfree(&machine->mmap_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) struct machine *machine__new_host(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct machine *machine = malloc(sizeof(*machine));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (machine != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		machine__init(machine, "", HOST_KERNEL_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		if (machine__create_kernel_maps(machine) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 			goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) out_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	free(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) struct machine *machine__new_kallsyms(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	struct machine *machine = machine__new_host();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	 * FIXME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	 *    ask for not using the kcore parsing code, once this one is fixed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	 *    to create a map per module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		machine__delete(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		machine = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	return machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) static void dsos__purge(struct dsos *dsos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct dso *pos, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	down_write(&dsos->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	list_for_each_entry_safe(pos, n, &dsos->head, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		RB_CLEAR_NODE(&pos->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		pos->root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		list_del_init(&pos->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		dso__put(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	up_write(&dsos->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) static void dsos__exit(struct dsos *dsos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	dsos__purge(dsos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	exit_rwsem(&dsos->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) void machine__delete_threads(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	for (i = 0; i < THREADS__TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		struct threads *threads = &machine->threads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		down_write(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		nd = rb_first_cached(&threads->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		while (nd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			struct thread *t = rb_entry(nd, struct thread, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			nd = rb_next(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			__machine__remove_thread(machine, t, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		up_write(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) void machine__exit(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	if (machine == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	machine__destroy_kernel_maps(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	maps__exit(&machine->kmaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	dsos__exit(&machine->dsos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	machine__exit_vdso(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	zfree(&machine->root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	zfree(&machine->mmap_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	zfree(&machine->current_tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	for (i = 0; i < THREADS__TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		struct threads *threads = &machine->threads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		struct thread *thread, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		 * Forget about the dead, at this point whatever threads were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		 * left in the dead lists better have a reference count taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		 * by who is using them, and then, when they drop those references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		 * and it finally hits zero, thread__put() will check and see that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		 * its not in the dead threads list and will not try to remove it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		 * from there, just calling thread__delete() straight away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		list_for_each_entry_safe(thread, n, &threads->dead, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 			list_del_init(&thread->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		exit_rwsem(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) void machine__delete(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (machine) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		machine__exit(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		free(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) void machines__init(struct machines *machines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	machine__init(&machines->host, "", HOST_KERNEL_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	machines->guests = RB_ROOT_CACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) void machines__exit(struct machines *machines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	machine__exit(&machines->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	/* XXX exit guest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) struct machine *machines__add(struct machines *machines, pid_t pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 			      const char *root_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct rb_node **p = &machines->guests.rb_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	struct machine *pos, *machine = malloc(sizeof(*machine));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	bool leftmost = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (machine == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (machine__init(machine, root_dir, pid) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		free(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	while (*p != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		pos = rb_entry(parent, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		if (pid < pos->pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			leftmost = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	rb_link_node(&machine->rb_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	return machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) void machines__set_comm_exec(struct machines *machines, bool comm_exec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	machines->host.comm_exec = comm_exec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		machine->comm_exec = comm_exec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) struct machine *machines__find(struct machines *machines, pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	struct rb_node **p = &machines->guests.rb_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	struct machine *machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct machine *default_machine = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (pid == HOST_KERNEL_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		return &machines->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	while (*p != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		machine = rb_entry(parent, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		if (pid < machine->pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		else if (pid > machine->pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			return machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		if (!machine->pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			default_machine = machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	return default_machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) struct machine *machines__findnew(struct machines *machines, pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	const char *root_dir = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	struct machine *machine = machines__find(machines, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (machine && (machine->pid == pid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if ((pid != HOST_KERNEL_ID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	    (pid != DEFAULT_GUEST_KERNEL_ID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	    (symbol_conf.guestmount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		if (access(path, R_OK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			static struct strlist *seen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			if (!seen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 				seen = strlist__new(NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			if (!strlist__has_entry(seen, path)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				pr_err("Can't access file %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				strlist__add(seen, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			machine = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		root_dir = path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	machine = machines__add(machines, pid, root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	return machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) void machines__process_guests(struct machines *machines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			      machine__process_t process, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		process(pos, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	struct machine *machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	machines->host.id_hdr_size = id_hdr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	for (node = rb_first_cached(&machines->guests); node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	     node = rb_next(node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		machine = rb_entry(node, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		machine->id_hdr_size = id_hdr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) static void machine__update_thread_pid(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 				       struct thread *th, pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	struct thread *leader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	th->pid_ = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	if (th->pid_ == th->tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	if (!leader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (!leader->maps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		leader->maps = maps__new(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	if (!leader->maps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (th->maps == leader->maps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (th->maps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		 * Maps are created from MMAP events which provide the pid and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		 * tid.  Consequently there never should be any maps on a thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		 * with an unknown pid.  Just print an error if there are.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		if (!maps__empty(th->maps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			pr_err("Discarding thread maps for %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			       th->pid_, th->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		maps__put(th->maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	th->maps = maps__get(leader->maps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	thread__put(leader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447)  * Front-end cache - TID lookups come in blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * so most of the time we dont have to look up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * the full rbtree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) static struct thread*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) __threads__get_last_match(struct threads *threads, struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			  int pid, int tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct thread *th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	th = threads->last_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	if (th != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		if (th->tid == tid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			machine__update_thread_pid(machine, th, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			return thread__get(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		threads->last_match = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) static struct thread*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) threads__get_last_match(struct threads *threads, struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			int pid, int tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	struct thread *th = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (perf_singlethreaded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		th = __threads__get_last_match(threads, machine, pid, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	return th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) __threads__set_last_match(struct threads *threads, struct thread *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	threads->last_match = th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) threads__set_last_match(struct threads *threads, struct thread *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (perf_singlethreaded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		__threads__set_last_match(threads, th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * Caller must eventually drop thread->refcnt returned with a successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  * lookup/new thread inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) static struct thread *____machine__findnew_thread(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 						  struct threads *threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 						  pid_t pid, pid_t tid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 						  bool create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	struct rb_node **p = &threads->entries.rb_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	struct thread *th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	bool leftmost = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	th = threads__get_last_match(threads, machine, pid, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		return th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	while (*p != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		th = rb_entry(parent, struct thread, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		if (th->tid == tid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			threads__set_last_match(threads, th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			machine__update_thread_pid(machine, th, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			return thread__get(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		if (tid < th->tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			leftmost = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	if (!create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	th = thread__new(pid, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	if (th != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		rb_link_node(&th->rb_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		 * We have to initialize maps separately after rb tree is updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		 * The reason is that we call machine__findnew_thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		 * within thread__init_maps to find the thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		 * leader and that would screwed the rb tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (thread__init_maps(th, machine)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			rb_erase_cached(&th->rb_node, &threads->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			RB_CLEAR_NODE(&th->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			thread__put(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		 * It is now in the rbtree, get a ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		thread__get(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		threads__set_last_match(threads, th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		++threads->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	return th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 				       pid_t tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	struct threads *threads = machine__threads(machine, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	struct thread *th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	down_write(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	th = __machine__findnew_thread(machine, pid, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	up_write(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	return th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) struct thread *machine__find_thread(struct machine *machine, pid_t pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 				    pid_t tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	struct threads *threads = machine__threads(machine, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct thread *th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	down_read(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	th =  ____machine__findnew_thread(machine, threads, pid, tid, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	up_read(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	return th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) struct comm *machine__thread_exec_comm(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 				       struct thread *thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	if (machine->comm_exec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		return thread__exec_comm(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		return thread__comm(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) int machine__process_comm_event(struct machine *machine, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 				struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	struct thread *thread = machine__findnew_thread(machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 							event->comm.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 							event->comm.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (exec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		machine->comm_exec = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		perf_event__fprintf_comm(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (thread == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	    __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) int machine__process_namespaces_event(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 				      union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 				      struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	struct thread *thread = machine__findnew_thread(machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 							event->namespaces.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 							event->namespaces.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		  "\nWARNING: kernel seems to support more namespaces than perf"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		  " tool.\nTry updating the perf tool..\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		  "\nWARNING: perf tool seems to support more namespaces than"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		  " the kernel.\nTry updating the kernel..\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		perf_event__fprintf_namespaces(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (thread == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	    thread__set_namespaces(thread, sample->time, &event->namespaces)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) int machine__process_cgroup_event(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 				  union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 				  struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	struct cgroup *cgrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		perf_event__fprintf_cgroup(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	if (cgrp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) int machine__process_lost_event(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 				union perf_event *event, struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		    event->lost.id, event->lost.lost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 					union perf_event *event, struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		    sample->id, event->lost_samples.lost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) static struct dso *machine__findnew_module_dso(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 					       struct kmod_path *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 					       const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct dso *dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	down_write(&machine->dsos.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	dso = __dsos__find(&machine->dsos, m->name, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (!dso) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		dso = __dsos__addnew(&machine->dsos, m->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		if (dso == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		dso__set_module_info(dso, m, machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		dso__set_long_name(dso, strdup(filename), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		dso->kernel = DSO_SPACE__KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	dso__get(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	up_write(&machine->dsos.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	return dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) int machine__process_aux_event(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			       union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		perf_event__fprintf_aux(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 					union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		perf_event__fprintf_itrace_start(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) int machine__process_switch_event(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 				  union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		perf_event__fprintf_switch(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) static int machine__process_ksymbol_register(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 					     union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 					     struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	struct symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (!map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		struct dso *dso = dso__new(event->ksymbol.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (dso) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			dso->kernel = DSO_SPACE__KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			map = map__new2(0, dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		if (!dso || !map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			dso__put(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			map->dso->binary_type = DSO_BINARY_TYPE__OOL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			map->dso->data.file_size = event->ksymbol.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			dso__set_loaded(map->dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		map->start = event->ksymbol.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		map->end = map->start + event->ksymbol.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		maps__insert(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		dso__set_loaded(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		if (is_bpf_image(event->ksymbol.name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			dso__set_long_name(dso, "", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	sym = symbol__new(map->map_ip(map, map->start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			  event->ksymbol.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			  0, 0, event->ksymbol.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	if (!sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	dso__insert_symbol(map->dso, sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static int machine__process_ksymbol_unregister(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 					       union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 					       struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	map = maps__find(&machine->kmaps, event->ksymbol.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	if (map != machine->vmlinux_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		maps__remove(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		sym = dso__find_symbol(map->dso, map->map_ip(map, map->start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		if (sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			dso__delete_symbol(map->dso, sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) int machine__process_ksymbol(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			     union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			     struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		perf_event__fprintf_ksymbol(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		return machine__process_ksymbol_unregister(machine, event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 							   sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	return machine__process_ksymbol_register(machine, event, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) int machine__process_text_poke(struct machine *machine, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			       struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	struct map *map = maps__find(&machine->kmaps, event->text_poke.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		perf_event__fprintf_text_poke(event, machine, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	if (!event->text_poke.new_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (cpumode != PERF_RECORD_MISC_KERNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (map && map->dso) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		 * Kernel maps might be changed when loading symbols so loading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		 * must be done prior to using kernel maps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		map__load(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		ret = dso__data_write_cache_addr(map->dso, map, machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 						 event->text_poke.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 						 new_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 						 event->text_poke.new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		if (ret != event->text_poke.new_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 				 event->text_poke.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			 event->text_poke.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 					      const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	struct map *map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct kmod_path m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	struct dso *dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	if (kmod_path__parse_name(&m, filename))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	dso = machine__findnew_module_dso(machine, &m, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (dso == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	map = map__new2(start, dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	maps__insert(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	/* Put the map here because maps__insert alread got it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	/* put the dso here, corresponding to  machine__findnew_module_dso */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	dso__put(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	zfree(&m.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		ret += __dsos__fprintf(&pos->dsos.head, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 				     bool (skip)(struct dso *dso, int parm), int parm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 				     bool (skip)(struct dso *dso, int parm), int parm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	size_t printed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	struct dso *kdso = machine__kernel_dso(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	if (kdso->has_build_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		char filename[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		if (dso__build_id_filename(kdso, filename, sizeof(filename),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 					   false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			printed += fprintf(fp, "[0] %s\n", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	for (i = 0; i < vmlinux_path__nr_entries; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		printed += fprintf(fp, "[%d] %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 				   i + kdso->has_build_id, vmlinux_path[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	return printed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) size_t machine__fprintf(struct machine *machine, FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	size_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	for (i = 0; i < THREADS__TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		struct threads *threads = &machine->threads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		down_read(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		ret = fprintf(fp, "Threads: %u\n", threads->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		for (nd = rb_first_cached(&threads->entries); nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		     nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			struct thread *pos = rb_entry(nd, struct thread, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			ret += thread__fprintf(pos, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		up_read(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) static struct dso *machine__get_kernel(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	const char *vmlinux_name = machine->mmap_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	struct dso *kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	if (machine__is_host(machine)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		if (symbol_conf.vmlinux_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			vmlinux_name = symbol_conf.vmlinux_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		kernel = machine__findnew_kernel(machine, vmlinux_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 						 "[kernel]", DSO_SPACE__KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		if (symbol_conf.default_guest_vmlinux_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			vmlinux_name = symbol_conf.default_guest_vmlinux_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		kernel = machine__findnew_kernel(machine, vmlinux_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 						 "[guest.kernel]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 						 DSO_SPACE__KERNEL_GUEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (kernel != NULL && (!kernel->has_build_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		dso__read_running_kernel_build_id(kernel, machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	return kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) struct process_args {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) void machine__get_kallsyms_filename(struct machine *machine, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 				    size_t bufsz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	if (machine__is_default_guest(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) /* Figure out the start address of kernel map from /proc/kallsyms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  * Returns the name of the start symbol in *symbol_name. Pass in NULL as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)  * symbol_name if it's not that important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) static int machine__get_running_kernel_start(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 					     const char **symbol_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 					     u64 *start, u64 *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	char filename[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	int i, err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	u64 addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	machine__get_kallsyms_filename(machine, filename, PATH_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	if (symbol__restricted_filename(filename, "/proc/kallsyms"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		err = kallsyms__get_function_start(filename, name, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	if (symbol_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		*symbol_name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	*start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	err = kallsyms__get_function_start(filename, "_etext", &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		*end = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) int machine__create_extra_kernel_map(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 				     struct dso *kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 				     struct extra_kernel_map *xm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	struct kmap *kmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	map = map__new2(xm->start, kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	map->end   = xm->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	map->pgoff = xm->pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	kmap = map__kmap(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	maps__insert(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		  kmap->name, map->start, map->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) static u64 find_entry_trampoline(struct dso *dso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	/* Duplicates are removed so lookup all aliases */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	const char *syms[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		"_entry_trampoline",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		"__entry_trampoline_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		"entry_SYSCALL_64_trampoline",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	struct symbol *sym = dso__first_symbol(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	for (; sym; sym = dso__next_symbol(sym)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		if (sym->binding != STB_GLOBAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		for (i = 0; i < ARRAY_SIZE(syms); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			if (!strcmp(sym->name, syms[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				return sym->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  * These values can be used for kernels that do not have symbols for the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  * trampolines in kallsyms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) #define X86_64_CPU_ENTRY_AREA_PER_CPU	0xfffffe0000000000ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) #define X86_64_CPU_ENTRY_AREA_SIZE	0x2c000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) #define X86_64_ENTRY_TRAMPOLINE		0x6000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /* Map x86_64 PTI entry trampolines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) int machine__map_x86_64_entry_trampolines(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 					  struct dso *kernel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	struct maps *kmaps = &machine->kmaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	int nr_cpus_avail, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	u64 pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	 * In the vmlinux case, pgoff is a virtual address which must now be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	 * mapped to a vmlinux offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	maps__for_each_entry(kmaps, map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		struct kmap *kmap = __map__kmap(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		struct map *dest_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		if (!kmap || !is_entry_trampoline(kmap->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		dest_map = maps__find(kmaps, map->pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		if (dest_map != map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (found || machine->trampolines_mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	pgoff = find_entry_trampoline(kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	if (!pgoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	nr_cpus_avail = machine__nr_cpus_avail(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	/* Add a 1 page map for each CPU's entry trampoline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			 X86_64_ENTRY_TRAMPOLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		struct extra_kernel_map xm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 			.start = va,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			.end   = va + page_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			.pgoff = pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	machine->trampolines_mapped = nr_cpus_avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 					     struct dso *kernel __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	/* In case of renewal the kernel map, destroy previous one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	machine__destroy_kernel_maps(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	machine->vmlinux_map = map__new2(0, kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	if (machine->vmlinux_map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	maps__insert(&machine->kmaps, machine->vmlinux_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) void machine__destroy_kernel_maps(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	struct kmap *kmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	struct map *map = machine__kernel_map(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	kmap = map__kmap(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	maps__remove(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (kmap && kmap->ref_reloc_sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		zfree((char **)&kmap->ref_reloc_sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		zfree(&kmap->ref_reloc_sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	map__zput(machine->vmlinux_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) int machines__create_guest_kernel_maps(struct machines *machines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	struct dirent **namelist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	int i, items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	if (symbol_conf.default_guest_vmlinux_name ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	    symbol_conf.default_guest_modules ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	    symbol_conf.default_guest_kallsyms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	if (symbol_conf.guestmount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		if (items <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		for (i = 0; i < items; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			if (!isdigit(namelist[i]->d_name[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 				/* Filter out . and .. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			if ((*endp != '\0') ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			    (endp == namelist[i]->d_name) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			    (errno == ERANGE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 				pr_debug("invalid directory (%s). Skipping.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 					 namelist[i]->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			sprintf(path, "%s/%s/proc/kallsyms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 				symbol_conf.guestmount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				namelist[i]->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			ret = access(path, R_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 				pr_debug("Can't access file %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 				goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			machines__create_kernel_maps(machines, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		free(namelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) void machines__destroy_kernel_maps(struct machines *machines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	struct rb_node *next = rb_first_cached(&machines->guests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	machine__destroy_kernel_maps(&machines->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	while (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		struct machine *pos = rb_entry(next, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		next = rb_next(&pos->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		rb_erase_cached(&pos->rb_node, &machines->guests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		machine__delete(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) int machines__create_kernel_maps(struct machines *machines, pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	struct machine *machine = machines__findnew(machines, pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	if (machine == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	return machine__create_kernel_maps(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) int machine__load_kallsyms(struct machine *machine, const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	struct map *map = machine__kernel_map(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	int ret = __dso__load_kallsyms(map->dso, filename, map, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		dso__set_loaded(map->dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		 * Since /proc/kallsyms will have multiple sessions for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		 * kernel, with modules between them, fixup the end of all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		 * sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		maps__fixup_end(&machine->kmaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) int machine__load_vmlinux_path(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	struct map *map = machine__kernel_map(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	int ret = dso__load_vmlinux_path(map->dso, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		dso__set_loaded(map->dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) static char *get_kernel_version(const char *root_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	char version[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	char *name, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	const char *prefix = "Linux version ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	sprintf(version, "%s/proc/version", root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	file = fopen(version, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	tmp = fgets(version, sizeof(version), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	name = strstr(version, prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	name += strlen(prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	tmp = strchr(name, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		*tmp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	return strdup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) static bool is_kmod_dso(struct dso *dso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	       dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	char *long_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	struct map *map = maps__find_by_name(maps, m->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	long_name = strdup(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	if (long_name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	dso__set_long_name(map->dso, long_name, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	dso__kernel_module_get_build_id(map->dso, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	 * Full name could reveal us kmod compression, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	 * we need to update the symtab_type if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	if (m->comp && is_kmod_dso(map->dso)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		map->dso->symtab_type++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		map->dso->comp = m->comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	struct dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	DIR *dir = opendir(dir_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	while ((dent = readdir(dir)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		/*sshfs might return bad dent->d_type, so we have to stat*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		if (stat(path, &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		if (S_ISDIR(st.st_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 			if (!strcmp(dent->d_name, ".") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			    !strcmp(dent->d_name, ".."))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			/* Do not follow top-level source and build symlinks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			if (depth == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 				if (!strcmp(dent->d_name, "source") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 				    !strcmp(dent->d_name, "build"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 			ret = maps__set_modules_path_dir(maps, path, depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			struct kmod_path m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 			ret = kmod_path__parse_name(&m, dent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			if (m.kmod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 				ret = maps__set_module_path(maps, path, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			zfree(&m.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) static int machine__set_modules_path(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	char *version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	char modules_path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	version = get_kernel_version(machine->root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	if (!version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		 machine->root_dir, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	free(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 				u64 *size __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 				const char *name __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static int machine__create_module(void *arg, const char *name, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 				  u64 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	struct machine *machine = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (arch__fix_module_text_start(&start, &size, name) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	map = machine__addnew_module_map(machine, start, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	map->end = start + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	dso__kernel_module_get_build_id(map->dso, machine->root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) static int machine__create_modules(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	const char *modules;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	if (machine__is_default_guest(machine)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		modules = symbol_conf.default_guest_modules;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		modules = path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	if (symbol__restricted_filename(modules, "/proc/modules"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	if (modules__parse(modules, machine, machine__create_module))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (!machine__set_modules_path(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	pr_debug("Problems setting modules path maps, continuing anyway...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) static void machine__set_kernel_mmap(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 				     u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	machine->vmlinux_map->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	machine->vmlinux_map->end   = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	 * Be a bit paranoid here, some perf.data file came with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	 * a zero sized synthesized MMAP event for the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	if (start == 0 && end == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		machine->vmlinux_map->end = ~0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) static void machine__update_kernel_mmap(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 				     u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	struct map *map = machine__kernel_map(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	map__get(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	maps__remove(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	machine__set_kernel_mmap(machine, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	maps__insert(&machine->kmaps, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) int machine__create_kernel_maps(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	struct dso *kernel = machine__get_kernel(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	u64 start = 0, end = ~0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	if (kernel == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	ret = __machine__create_kernel_maps(machine, kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		if (machine__is_host(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			pr_debug("Problems creating module maps, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 				 "continuing anyway...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 			pr_debug("Problems creating module maps for guest %d, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 				 "continuing anyway...\n", machine->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		if (name &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		    map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			machine__destroy_kernel_maps(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		 * we have a real start address now, so re-order the kmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		 * assume it's the last in the kmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		machine__update_kernel_mmap(machine, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	if (machine__create_extra_kernel_maps(machine, kernel))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (end == ~0ULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		/* update end address of the kernel map using adjacent module address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		map = map__next(machine__kernel_map(machine));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		if (map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			machine__set_kernel_mmap(machine, start, map->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	dso__put(kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) static bool machine__uses_kcore(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	struct dso *dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	list_for_each_entry(dso, &machine->dsos.head, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		if (dso__is_kcore(dso))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 					     union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	return machine__is(machine, "x86_64") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	       is_entry_trampoline(event->mmap.filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) static int machine__process_extra_kernel_map(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 					     union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	struct dso *kernel = machine__kernel_dso(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	struct extra_kernel_map xm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		.start = event->mmap.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		.end   = event->mmap.start + event->mmap.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		.pgoff = event->mmap.pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	if (kernel == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	return machine__create_extra_kernel_map(machine, kernel, &xm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) static int machine__process_kernel_mmap_event(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 					      union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	enum dso_space_type dso_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	bool is_kernel_mmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	/* If we have maps from kcore then we do not need or want any others */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	if (machine__uses_kcore(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	if (machine__is_host(machine))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		dso_space = DSO_SPACE__KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		dso_space = DSO_SPACE__KERNEL_GUEST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	is_kernel_mmap = memcmp(event->mmap.filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 				machine->mmap_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 				strlen(machine->mmap_name) - 1) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	if (event->mmap.filename[0] == '/' ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	    (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		map = machine__addnew_module_map(machine, event->mmap.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 						 event->mmap.filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		map->end = map->start + event->mmap.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	} else if (is_kernel_mmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		const char *symbol_name = (event->mmap.filename +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 				strlen(machine->mmap_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		 * Should be there already, from the build-id table in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		 * the header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		struct dso *kernel = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		struct dso *dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		down_read(&machine->dsos.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		list_for_each_entry(dso, &machine->dsos.head, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 			 * The cpumode passed to is_kernel_module is not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 			 * cpumode of *this* event. If we insist on passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 			 * correct cpumode to is_kernel_module, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			 * record the cpumode when we adding this dso to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			 * linked list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 			 * However we don't really need passing correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			 * cpumode.  We know the correct cpumode must be kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			 * mode (if not, we should not link it onto kernel_dsos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			 * list).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			 * is_kernel_module() treats it as a kernel cpumode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			if (!dso->kernel ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			    is_kernel_module(dso->long_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 					     PERF_RECORD_MISC_CPUMODE_UNKNOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 			kernel = dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		up_read(&machine->dsos.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		if (kernel == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			kernel = machine__findnew_dso(machine, machine->mmap_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		if (kernel == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 			goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		kernel->kernel = dso_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		if (__machine__create_kernel_maps(machine, kernel) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 			dso__put(kernel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 			goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		if (strstr(kernel->long_name, "vmlinux"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 			dso__set_short_name(kernel, "[kernel.vmlinux]", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		machine__update_kernel_mmap(machine, event->mmap.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 					 event->mmap.start + event->mmap.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		 * Avoid using a zero address (kptr_restrict) for the ref reloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		 * symbol. Effectively having zero here means that at record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		 * time /proc/sys/kernel/kptr_restrict was non zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		if (event->mmap.pgoff != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 			map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 							symbol_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 							event->mmap.pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		if (machine__is_default_guest(machine)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 			 * preload dso of guest kernel and modules
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 			dso__load(kernel, machine__kernel_map(machine));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	} else if (perf_event__is_extra_kernel_mmap(machine, event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		return machine__process_extra_kernel_map(machine, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) out_problem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) int machine__process_mmap2_event(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 				 union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 				 struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	struct thread *thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	struct dso_id dso_id = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		.maj = event->mmap2.maj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		.min = event->mmap2.min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		.ino = event->mmap2.ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		.ino_generation = event->mmap2.ino_generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		perf_event__fprintf_mmap2(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	    sample->cpumode == PERF_RECORD_MISC_KERNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		ret = machine__process_kernel_mmap_event(machine, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	thread = machine__findnew_thread(machine, event->mmap2.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 					event->mmap2.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	if (thread == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	map = map__new(machine, event->mmap2.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 			event->mmap2.len, event->mmap2.pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			&dso_id, event->mmap2.prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 			event->mmap2.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			event->mmap2.filename, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		goto out_problem_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	ret = thread__insert_map(thread, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		goto out_problem_insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) out_problem_insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) out_problem_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) out_problem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) int machine__process_mmap_event(struct machine *machine, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 				struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	struct thread *thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	u32 prot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		perf_event__fprintf_mmap(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	    sample->cpumode == PERF_RECORD_MISC_KERNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		ret = machine__process_kernel_mmap_event(machine, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 			goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	thread = machine__findnew_thread(machine, event->mmap.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 					 event->mmap.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	if (thread == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		goto out_problem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		prot = PROT_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	map = map__new(machine, event->mmap.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			event->mmap.len, event->mmap.pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			NULL, prot, 0, event->mmap.filename, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		goto out_problem_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	ret = thread__insert_map(thread, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		goto out_problem_insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) out_problem_insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	map__put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) out_problem_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) out_problem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	struct threads *threads = machine__threads(machine, th->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	if (threads->last_match == th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		threads__set_last_match(threads, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	if (lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		down_write(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	BUG_ON(refcount_read(&th->refcnt) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	rb_erase_cached(&th->rb_node, &threads->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	RB_CLEAR_NODE(&th->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	--threads->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	 * Move it first to the dead_threads list, then drop the reference,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	 * if this is the last reference, then the thread__delete destructor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	 * will be called and we will remove it from the dead_threads list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	list_add_tail(&th->node, &threads->dead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	 * We need to do the put here because if this is the last refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	 * then we will be touching the threads->dead head when removing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	 * thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	thread__put(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	if (lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		up_write(&threads->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) void machine__remove_thread(struct machine *machine, struct thread *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	return __machine__remove_thread(machine, th, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) int machine__process_fork_event(struct machine *machine, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 				struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	struct thread *thread = machine__find_thread(machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 						     event->fork.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 						     event->fork.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	struct thread *parent = machine__findnew_thread(machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 							event->fork.ppid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 							event->fork.ptid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	bool do_maps_clone = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		perf_event__fprintf_task(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	 * There may be an existing thread that is not actually the parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	 * either because we are processing events out of order, or because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	 * (fork) event that would have removed the thread was lost. Assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	 * latter case and continue on as best we can.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	if (parent->pid_ != (pid_t)event->fork.ppid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		dump_printf("removing erroneous parent thread %d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 			    parent->pid_, parent->tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		machine__remove_thread(machine, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		thread__put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		parent = machine__findnew_thread(machine, event->fork.ppid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 						 event->fork.ptid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	/* if a thread currently exists for the thread id remove it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	if (thread != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		machine__remove_thread(machine, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	thread = machine__findnew_thread(machine, event->fork.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 					 event->fork.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	 * When synthesizing FORK events, we are trying to create thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	 * objects for the already running tasks on the machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	 * Normally, for a kernel FORK event, we want to clone the parent's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	 * maps because that is what the kernel just did.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	 * But when synthesizing, this should not be done.  If we do, we end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	 * with overlapping maps as we process the sythesized MMAP2 events that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	 * get delivered shortly thereafter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	 * Use the FORK event misc flags in an internal way to signal this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	 * situation, so we can elide the map clone when appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		do_maps_clone = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	if (thread == NULL || parent == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	    thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	thread__put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) int machine__process_exit_event(struct machine *machine, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 				struct perf_sample *sample __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	struct thread *thread = machine__find_thread(machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 						     event->fork.pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 						     event->fork.tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	if (dump_trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		perf_event__fprintf_task(event, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	if (thread != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		thread__exited(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) int machine__process_event(struct machine *machine, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 			   struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	switch (event->header.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	case PERF_RECORD_COMM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		ret = machine__process_comm_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	case PERF_RECORD_MMAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		ret = machine__process_mmap_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	case PERF_RECORD_NAMESPACES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		ret = machine__process_namespaces_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	case PERF_RECORD_CGROUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		ret = machine__process_cgroup_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	case PERF_RECORD_MMAP2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		ret = machine__process_mmap2_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	case PERF_RECORD_FORK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		ret = machine__process_fork_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	case PERF_RECORD_EXIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		ret = machine__process_exit_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	case PERF_RECORD_LOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		ret = machine__process_lost_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	case PERF_RECORD_AUX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		ret = machine__process_aux_event(machine, event); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	case PERF_RECORD_ITRACE_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		ret = machine__process_itrace_start_event(machine, event); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	case PERF_RECORD_LOST_SAMPLES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		ret = machine__process_lost_samples_event(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	case PERF_RECORD_SWITCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	case PERF_RECORD_SWITCH_CPU_WIDE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		ret = machine__process_switch_event(machine, event); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	case PERF_RECORD_KSYMBOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 		ret = machine__process_ksymbol(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	case PERF_RECORD_BPF_EVENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		ret = machine__process_bpf(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	case PERF_RECORD_TEXT_POKE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		ret = machine__process_text_poke(machine, event, sample); break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (!regexec(regex, sym->name, 0, NULL, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) static void ip__resolve_ams(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 			    struct addr_map_symbol *ams,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 			    u64 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	memset(&al, 0, sizeof(al));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	 * We cannot use the header.misc hint to determine whether a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	 * branch stack address is user, kernel, guest, hypervisor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	 * Branches may straddle the kernel/user/hypervisor boundaries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	 * Thus, we have to try consecutively until we find a match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	 * or else, the symbol is unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	thread__find_cpumode_addr_location(thread, ip, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	ams->addr = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	ams->al_addr = al.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	ams->ms.maps = al.maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	ams->ms.sym = al.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	ams->ms.map = al.map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	ams->phys_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) static void ip__resolve_data(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 			     u8 m, struct addr_map_symbol *ams,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			     u64 addr, u64 phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	memset(&al, 0, sizeof(al));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	thread__find_symbol(thread, m, addr, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	ams->addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	ams->al_addr = al.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	ams->ms.maps = al.maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	ams->ms.sym = al.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	ams->ms.map = al.map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	ams->phys_addr = phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) struct mem_info *sample__resolve_mem(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 				     struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	struct mem_info *mi = mem_info__new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	if (!mi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			 sample->addr, sample->phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	mi->data_src.val = sample->data_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	return mi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) static char *callchain_srcline(struct map_symbol *ms, u64 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	struct map *map = ms->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	char *srcline = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	if (!map || callchain_param.key == CCKEY_FUNCTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		return srcline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	srcline = srcline__tree_find(&map->dso->srclines, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	if (!srcline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		bool show_sym = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		bool show_addr = callchain_param.key == CCKEY_ADDRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 				      ms->sym, show_sym, show_addr, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		srcline__tree_insert(&map->dso->srclines, ip, srcline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	return srcline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) struct iterations {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	int nr_loop_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	u64 cycles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) static int add_callchain_ip(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 			    struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 			    struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 			    struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 			    u8 *cpumode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			    u64 ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			    bool branch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			    struct branch_flags *flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			    struct iterations *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 			    u64 branch_from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	struct map_symbol ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	int nr_loop_iter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	u64 iter_cycles = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	const char *srcline = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	al.filtered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	al.sym = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	al.srcline = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	if (!cpumode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		thread__find_cpumode_addr_location(thread, ip, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		if (ip >= PERF_CONTEXT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			switch (ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 			case PERF_CONTEXT_HV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 				*cpumode = PERF_RECORD_MISC_HYPERVISOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 			case PERF_CONTEXT_KERNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 				*cpumode = PERF_RECORD_MISC_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			case PERF_CONTEXT_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 				*cpumode = PERF_RECORD_MISC_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 				pr_debug("invalid callchain context: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 					 "%"PRId64"\n", (s64) ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 				 * It seems the callchain is corrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 				 * Discard all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 				callchain_cursor_reset(cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		thread__find_symbol(thread, *cpumode, ip, &al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	if (al.sym != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		if (perf_hpp_list.parent && !*parent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		    symbol__match_regex(al.sym, &parent_regex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 			*parent = al.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		else if (have_ignore_callees && root_al &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		  symbol__match_regex(al.sym, &ignore_callees_regex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 			/* Treat this symbol as the root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 			   forgetting its callees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 			*root_al = al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 			callchain_cursor_reset(cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	if (symbol_conf.hide_unresolved && al.sym == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	if (iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		nr_loop_iter = iter->nr_loop_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 		iter_cycles = iter->cycles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	ms.maps = al.maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	ms.map = al.map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	ms.sym = al.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	srcline = callchain_srcline(&ms, al.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	return callchain_cursor_append(cursor, ip, &ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 				       branch, flags, nr_loop_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 				       iter_cycles, branch_from, srcline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 					   struct addr_location *al)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	const struct branch_stack *bs = sample->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	struct branch_entry *entries = perf_sample__branch_entries(sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	if (!bi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	for (i = 0; i < bs->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 		ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 		ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		bi[i].flags = entries[i].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	return bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) static void save_iterations(struct iterations *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			    struct branch_entry *be, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	iter->nr_loop_iter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	iter->cycles = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		iter->cycles += be[i].flags.cycles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) #define CHASHSZ 127
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) #define CHASHBITS 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) #define NO_ENTRY 0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) #define PERF_MAX_BRANCH_DEPTH 127
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) /* Remove loops. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) static int remove_loops(struct branch_entry *l, int nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 			struct iterations *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	int i, j, off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	unsigned char chash[CHASHSZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	memset(chash, NO_ENTRY, sizeof(chash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		/* no collision handling for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		if (chash[h] == NO_ENTRY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 			chash[h] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		} else if (l[chash[h]].from == l[i].from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 			bool is_loop = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 			/* check if it is a real loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 			off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 			for (j = chash[h]; j < i && i + off < nr; j++, off++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 				if (l[j].from != l[i + off].from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 					is_loop = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 			if (is_loop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 				j = nr - (i + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 				if (j > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 					save_iterations(iter + i + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 						l + i, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 					memmove(iter + i, iter + i + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 						j * sizeof(*iter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 					memmove(l + i, l + i + off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 						j * sizeof(*l));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 				nr -= off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	return nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) static int lbr_callchain_add_kernel_ip(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 				       struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 				       struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 				       struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 				       struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 				       u64 branch_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 				       bool callee, int end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	struct ip_callchain *chain = sample->callchain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	u8 cpumode = PERF_RECORD_MISC_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	if (callee) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		for (i = 0; i < end + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 			err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 					       root_al, &cpumode, chain->ips[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 					       false, NULL, NULL, branch_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	for (i = end; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 				       root_al, &cpumode, chain->ips[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 				       false, NULL, NULL, branch_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) static void save_lbr_cursor_node(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 				 struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 				 int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	if (!lbr_stitch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	if (cursor->pos == cursor->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		lbr_stitch->prev_lbr_cursor[idx].valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	if (!cursor->curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		cursor->curr = cursor->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 		cursor->curr = cursor->curr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	       sizeof(struct callchain_cursor_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	lbr_stitch->prev_lbr_cursor[idx].valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	cursor->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) static int lbr_callchain_add_lbr_ip(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 				    struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 				    struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 				    struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 				    struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 				    u64 *branch_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 				    bool callee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	struct branch_stack *lbr_stack = sample->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	struct branch_entry *entries = perf_sample__branch_entries(sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	u8 cpumode = PERF_RECORD_MISC_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	int lbr_nr = lbr_stack->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	struct branch_flags *flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	u64 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	 * The curr and pos are not used in writing session. They are cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	 * in callchain_cursor_commit() when the writing session is closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	 * Using curr and pos to track the current cursor node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	if (thread->lbr_stitch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		cursor->curr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 		cursor->pos = cursor->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		if (cursor->nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 			cursor->curr = cursor->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 			for (i = 0; i < (int)(cursor->nr - 1); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 				cursor->curr = cursor->curr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	if (callee) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 		/* Add LBR ip from first entries.to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 		ip = entries[0].to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		flags = &entries[0].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		*branch_from = entries[0].from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 		err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 				       root_al, &cpumode, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 				       true, flags, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 				       *branch_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		 * The number of cursor node increases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		 * Move the current cursor node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 		 * But does not need to save current cursor node for entry 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		 * It's impossible to stitch the whole LBRs of previous sample.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 			if (!cursor->curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 				cursor->curr = cursor->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 				cursor->curr = cursor->curr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 			cursor->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 		/* Add LBR ip from entries.from one by one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 		for (i = 0; i < lbr_nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 			ip = entries[i].from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 			flags = &entries[i].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 			err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 					       root_al, &cpumode, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 					       true, flags, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 					       *branch_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 			save_lbr_cursor_node(thread, cursor, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	/* Add LBR ip from entries.from one by one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	for (i = lbr_nr - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 		ip = entries[i].from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 		flags = &entries[i].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 		err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 				       root_al, &cpumode, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 				       true, flags, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 				       *branch_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		save_lbr_cursor_node(thread, cursor, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	/* Add LBR ip from first entries.to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	ip = entries[0].to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	flags = &entries[0].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	*branch_from = entries[0].from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 			       root_al, &cpumode, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 			       true, flags, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 			       *branch_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 					     struct callchain_cursor *cursor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	struct callchain_cursor_node *cnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	struct stitch_list *stitch_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		cnode = &stitch_node->cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		err = callchain_cursor_append(cursor, cnode->ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 					      &cnode->ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 					      cnode->branch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 					      &cnode->branch_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 					      cnode->nr_loop_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 					      cnode->iter_cycles,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 					      cnode->branch_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 					      cnode->srcline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) static struct stitch_list *get_stitch_node(struct thread *thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	struct stitch_list *stitch_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	if (!list_empty(&lbr_stitch->free_lists)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		stitch_node = list_first_entry(&lbr_stitch->free_lists,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 					       struct stitch_list, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 		list_del(&stitch_node->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		return stitch_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	return malloc(sizeof(struct stitch_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) static bool has_stitched_lbr(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 			     struct perf_sample *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 			     struct perf_sample *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 			     unsigned int max_lbr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 			     bool callee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	struct branch_stack *cur_stack = cur->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	struct branch_stack *prev_stack = prev->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	int i, j, nr_identical_branches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	struct stitch_list *stitch_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	u64 cur_base, distance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	if (!cur_stack || !prev_stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	/* Find the physical index of the base-of-stack for current sample. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 						     (max_lbr + prev_stack->hw_idx - cur_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	/* Previous sample has shorter stack. Nothing can be stitched. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	if (distance + 1 > prev_stack->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	 * Check if there are identical LBRs between two samples.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	 * Identicall LBRs must have same from, to and flags values. Also,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	 * they have to be saved in the same LBR registers (same physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	 * index).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	 * Starts from the base-of-stack of current sample.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		if ((prev_entries[i].from != cur_entries[j].from) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 		    (prev_entries[i].to != cur_entries[j].to) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 		    (prev_entries[i].flags.value != cur_entries[j].flags.value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 		nr_identical_branches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	if (!nr_identical_branches)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	 * Save the LBRs between the base-of-stack of previous sample
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	 * and the base-of-stack of current sample into lbr_stitch->lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	 * These LBRs will be stitched later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	for (i = prev_stack->nr - 1; i > (int)distance; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		if (!lbr_stitch->prev_lbr_cursor[i].valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		stitch_node = get_stitch_node(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		if (!stitch_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 		memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		       sizeof(struct callchain_cursor_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 		if (callee)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 			list_add(&stitch_node->node, &lbr_stitch->lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 			list_add_tail(&stitch_node->node, &lbr_stitch->lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	if (thread->lbr_stitch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	if (!thread->lbr_stitch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	if (!thread->lbr_stitch->prev_lbr_cursor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 		goto free_lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	INIT_LIST_HEAD(&thread->lbr_stitch->lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) free_lbr_stitch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	zfree(&thread->lbr_stitch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	thread->lbr_stitch_enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544)  * Recolve LBR callstack chain sample
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546)  * 1 on success get LBR callchain information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547)  * 0 no available LBR callchain information, should try fp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548)  * negative error code on other errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) static int resolve_lbr_callchain_sample(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 					struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 					struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 					struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 					struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 					int max_stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 					unsigned int max_lbr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	bool callee = (callchain_param.order == ORDER_CALLEE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	struct ip_callchain *chain = sample->callchain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	int chain_nr = min(max_stack, (int)chain->nr), i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	struct lbr_stitch *lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	bool stitched_lbr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	u64 branch_from = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	for (i = 0; i < chain_nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 		if (chain->ips[i] == PERF_CONTEXT_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	/* LBR only affects the user callchain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	if (i == chain_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	    (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		lbr_stitch = thread->lbr_stitch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 		stitched_lbr = has_stitched_lbr(thread, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 						&lbr_stitch->prev_sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 						max_lbr, callee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 		if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 			list_replace_init(&lbr_stitch->lists,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 					  &lbr_stitch->free_lists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 		memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	if (callee) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		/* Add kernel ip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 		err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 						  parent, root_al, branch_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 						  true, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 		err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 					       root_al, &branch_from, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 		if (stitched_lbr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 			err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 		if (stitched_lbr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 			err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 		err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 					       root_al, &branch_from, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		/* Add kernel ip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 		err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 						  parent, root_al, branch_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 						  false, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	return (err < 0) ? err : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 			     struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 			     struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 			     struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 			     u8 *cpumode, int ent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	while (--ent >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 		u64 ip = chain->ips[ent];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		if (ip >= PERF_CONTEXT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 			err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 					       root_al, cpumode, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 					       false, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) static int thread__resolve_callchain_sample(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 					    struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 					    struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 					    struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 					    struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 					    struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 					    int max_stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	struct branch_stack *branch = sample->branch_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	struct branch_entry *entries = perf_sample__branch_entries(sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	struct ip_callchain *chain = sample->callchain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	int chain_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	u8 cpumode = PERF_RECORD_MISC_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	int i, j, err, nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	int skip_idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	int first_call = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	if (chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 		chain_nr = chain->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	if (evsel__has_branch_callstack(evsel)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 		struct perf_env *env = evsel__env(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 		err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 						   root_al, max_stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 						   !env ? 0 : env->max_branches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			return (err < 0) ? err : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	 * Based on DWARF debug information, some architectures skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	 * a callchain entry saved by the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	skip_idx = arch_skip_callchain_idx(thread, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	 * Add branches to call stack for easier browsing. This gives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	 * more context for a sample than just the callers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	 * This uses individual histograms of paths compared to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	 * aggregated histograms the normal LBR mode uses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	 * Limitations for now:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	 * - No extra filters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	 * - No annotations (should annotate somehow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	if (branch && callchain_param.branch_callstack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 		int nr = min(max_stack, (int)branch->nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 		struct branch_entry be[nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 		struct iterations iter[nr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 		if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 			pr_warning("corrupted branch chain. skipping...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 			goto check_calls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 		for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 			if (callchain_param.order == ORDER_CALLEE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 				be[i] = entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 				if (chain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 				 * Check for overlap into the callchain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 				 * The return address is one off compared to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 				 * the branch entry. To adjust for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 				 * assume the calling instruction is not longer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 				 * than 8 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 				if (i == skip_idx ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 				    chain->ips[first_call] >= PERF_CONTEXT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 					first_call++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 				else if (be[i].from < chain->ips[first_call] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 				    be[i].from >= chain->ips[first_call] - 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 					first_call++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 				be[i] = entries[branch->nr - i - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		memset(iter, 0, sizeof(struct iterations) * nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 		nr = remove_loops(be, nr, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 			err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 					       root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 					       NULL, be[i].to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 					       true, &be[i].flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 					       NULL, be[i].from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 			if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 				err = add_callchain_ip(thread, cursor, parent, root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 						       NULL, be[i].from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 						       true, &be[i].flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 						       &iter[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 			if (err == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 		if (chain_nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 		chain_nr -= nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) check_calls:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	if (chain && callchain_param.order != ORDER_CALLEE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 					&cpumode, chain->nr - first_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 			return (err < 0) ? err : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	for (i = first_call, nr_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	     i < chain_nr && nr_entries < max_stack; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		u64 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 		if (callchain_param.order == ORDER_CALLEE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 			j = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 			j = chain->nr - i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) #ifdef HAVE_SKIP_CALLCHAIN_IDX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 		if (j == skip_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		ip = chain->ips[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 		if (ip < PERF_CONTEXT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785)                        ++nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 		else if (callchain_param.order != ORDER_CALLEE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 			err = find_prev_cpumode(chain, thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 						root_al, &cpumode, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 				return (err < 0) ? err : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 		err = add_callchain_ip(thread, cursor, parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 				       root_al, &cpumode, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 				       false, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 			return (err < 0) ? err : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	struct symbol *sym = ms->sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	struct map *map = ms->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	struct inline_node *inline_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	struct inline_list *ilist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	if (!symbol_conf.inline_name || !map || !sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	addr = map__map_ip(map, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	addr = map__rip_2objdump(map, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	if (!inline_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 		inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 		if (!inline_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 		inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	list_for_each_entry(ilist, &inline_node->val, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 		struct map_symbol ilist_ms = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 			.maps = ms->maps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 			.map = map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 			.sym = ilist->symbol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 		ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 					      NULL, 0, 0, 0, ilist->srcline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) static int unwind_entry(struct unwind_entry *entry, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	struct callchain_cursor *cursor = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	const char *srcline = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	u64 addr = entry->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	 * Convert entry->ip from a virtual address to an offset in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	 * its corresponding binary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	if (entry->ms.map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 		addr = map__map_ip(entry->ms.map, entry->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	srcline = callchain_srcline(&entry->ms, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 	return callchain_cursor_append(cursor, entry->ip, &entry->ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 				       false, NULL, 0, 0, 0, srcline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) static int thread__resolve_callchain_unwind(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 					    struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 					    struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 					    struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 					    int max_stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	/* Can we do dwarf post unwind? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	      (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	/* Bail out if nothing was captured. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	if ((!sample->user_regs.regs) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	    (!sample->user_stack.size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	return unwind__get_entries(unwind_entry, cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 				   thread, sample, max_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) int thread__resolve_callchain(struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 			      struct callchain_cursor *cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 			      struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 			      struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 			      struct symbol **parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 			      struct addr_location *root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 			      int max_stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	callchain_cursor_reset(cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	if (callchain_param.order == ORDER_CALLEE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		ret = thread__resolve_callchain_sample(thread, cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 						       evsel, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 						       parent, root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 						       max_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 		ret = thread__resolve_callchain_unwind(thread, cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 						       evsel, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 						       max_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		ret = thread__resolve_callchain_unwind(thread, cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 						       evsel, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 						       max_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 		ret = thread__resolve_callchain_sample(thread, cursor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 						       evsel, sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 						       parent, root_al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 						       max_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) int machine__for_each_thread(struct machine *machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 			     int (*fn)(struct thread *thread, void *p),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 			     void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 	struct threads *threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 	struct thread *thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 	for (i = 0; i < THREADS__TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 		threads = &machine->threads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		for (nd = rb_first_cached(&threads->entries); nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 		     nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 			thread = rb_entry(nd, struct thread, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 			rc = fn(thread, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 			if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 		list_for_each_entry(thread, &threads->dead, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 			rc = fn(thread, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 			if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) int machines__for_each_thread(struct machines *machines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 			      int (*fn)(struct thread *thread, void *p),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 			      void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	struct rb_node *nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	rc = machine__for_each_thread(&machines->host, fn, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 		rc = machine__for_each_thread(machine, fn, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) pid_t machine__get_current_tid(struct machine *machine, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 	int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 	if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	return machine->current_tid[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 			     pid_t tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	struct thread *thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 	if (cpu < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 	if (!machine->current_tid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 		if (!machine->current_tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 		for (i = 0; i < nr_cpus; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 			machine->current_tid[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	if (cpu >= nr_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 		pr_err("Requested CPU %d too large. ", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 		pr_err("Consider raising MAX_NR_CPUS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	machine->current_tid[cpu] = tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	thread = machine__findnew_thread(machine, pid, tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 	if (!thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 	thread->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	thread__put(thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023)  * Compares the raw arch string. N.B. see instead perf_env__arch() if a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024)  * normalized arch is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) bool machine__is(struct machine *machine, const char *arch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) int machine__nr_cpus_avail(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) int machine__get_kernel_start(struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	struct map *map = machine__kernel_map(machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	 * The only addresses above 2^63 are kernel addresses of a 64-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	 * kernel.  Note that addresses are unsigned so that on a 32-bit system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	 * all addresses including kernel addresses are less than 2^32.  In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 	 * that case (32-bit system), if the kernel mapping is unknown, all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	 * addresses will be assumed to be in user space - see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	 * machine__kernel_ip().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	machine->kernel_start = 1ULL << 63;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	if (map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 		err = map__load(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 		 * On x86_64, PTI entry trampolines are less than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 		 * start of kernel text, but still above 2^63. So leave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 		 * kernel_start = 1ULL << 63 for x86_64.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 		if (!err && !machine__is(machine, "x86_64"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 			machine->kernel_start = map->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 	u8 addr_cpumode = cpumode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 	bool kernel_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	if (!machine->single_address_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 	kernel_ip = machine__kernel_ip(machine, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	switch (cpumode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	case PERF_RECORD_MISC_KERNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	case PERF_RECORD_MISC_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 		addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 					   PERF_RECORD_MISC_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	case PERF_RECORD_MISC_GUEST_KERNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	case PERF_RECORD_MISC_GUEST_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 		addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 					   PERF_RECORD_MISC_GUEST_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	return addr_cpumode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	return dsos__findnew_id(&machine->dsos, filename, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	return machine__findnew_dso_id(machine, filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	struct machine *machine = vmachine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 	struct map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	if (sym == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	*modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	*addrp = map->unmap_ip(map, sym->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	return sym->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	struct dso *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	list_for_each_entry(pos, &machine->dsos.head, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		if (fn(pos, machine, priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) }