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 <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <elfutils/libdw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <elfutils/libdwfl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "unwind.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "unwind-libdw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "machine.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "thread.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "perf_regs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "callchain.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static char *debuginfo_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 			    const char *modname __maybe_unused, Dwarf_Addr base __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 			    const char *file_name, const char *debuglink_file __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 			    GElf_Word debuglink_crc __maybe_unused, char **debuginfo_file_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	const struct dso *dso = *userdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	assert(dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (dso->symsrc_filename && strcmp (file_name, dso->symsrc_filename))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		*debuginfo_file_name = strdup(dso->symsrc_filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static const Dwfl_Callbacks offline_callbacks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	.find_debuginfo		= __find_debuginfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	.debuginfo_path		= &debuginfo_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	.section_address	= dwfl_offline_section_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	// .find_elf is not set as we use dwfl_report_elf() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int __report_module(struct addr_location *al, u64 ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			    struct unwind_info *ui)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	Dwfl_Module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct dso *dso = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 * Some callers will use al->sym, so we can't just use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	 * cheaper thread__find_map() here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (al->map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		dso = al->map->dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!dso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	mod = dwfl_addrmodule(ui->dwfl, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		Dwarf_Addr s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (s != al->map->start - al->map->pgoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			mod = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (!mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		mod = dwfl_report_elf(ui->dwfl, dso->short_name, dso->long_name, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				      al->map->start - al->map->pgoff, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		char filename[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (dso__build_id_filename(dso, filename, sizeof(filename), false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			mod = dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 					      al->map->start - al->map->pgoff, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (mod) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		void **userdatap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		dwfl_module_info(mod, &userdatap, NULL, NULL, NULL, NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		*userdatap = dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int report_module(u64 ip, struct unwind_info *ui)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return __report_module(&al, ip, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * Store all entries within entries array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * we will process it after we finish unwind.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int entry(u64 ip, struct unwind_info *ui)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct unwind_entry *e = &ui->entries[ui->idx++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (__report_module(&al, ip, ui))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	e->ip	  = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	e->ms.maps = al.maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	e->ms.map = al.map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	e->ms.sym = al.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		 al.sym ? al.sym->name : "''",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		 ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		 al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/* We want only single thread to be processed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (*thread_argp != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	*thread_argp = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return dwfl_pid(dwfl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			  Dwarf_Word *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (!al.map->dso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				   addr, (u8 *) data, sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return !(size == sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct unwind_info *ui = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct stack_dump *stack = &ui->sample->user_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	end = start + stack->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* Check overflow. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (addr + sizeof(Dwarf_Word) < addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (addr < start || addr + sizeof(Dwarf_Word) > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		ret = access_dso_mem(ui, addr, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				addr, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	offset  = addr - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	*result = *(Dwarf_Word *)&stack->data[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		 addr, (unsigned long)*result, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static const Dwfl_Thread_Callbacks callbacks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	.next_thread		= next_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	.memory_read		= memory_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	.set_initial_registers	= libdw__arch_set_initial_registers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) frame_callback(Dwfl_Frame *state, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct unwind_info *ui = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	Dwarf_Addr pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	bool isactivation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (!dwfl_frame_pc(state, &pc, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		pr_err("%s", dwfl_errmsg(-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return DWARF_CB_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	// report the module before we query for isactivation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	report_module(pc, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (!dwfl_frame_pc(state, &pc, &isactivation)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		pr_err("%s", dwfl_errmsg(-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return DWARF_CB_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (!isactivation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		--pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return entry(pc, ui) || !(--ui->max_stack) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	       DWARF_CB_ABORT : DWARF_CB_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			struct thread *thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			struct perf_sample *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			int max_stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct unwind_info *ui, ui_buf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		.sample		= data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		.thread		= thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		.machine	= thread->maps->machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		.cb		= cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		.arg		= arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		.max_stack	= max_stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	Dwarf_Word ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	int err = -EINVAL, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!data->user_regs.regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	ui = zalloc(sizeof(ui_buf) + sizeof(ui_buf.entries[0]) * max_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!ui)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	*ui = ui_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	ui->dwfl = dwfl_begin(&offline_callbacks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (!ui->dwfl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	err = report_module(ip, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	err = !dwfl_attach_state(ui->dwfl, EM_NONE, thread->tid, &callbacks, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	err = dwfl_getthread_frames(ui->dwfl, thread->tid, frame_callback, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (err && ui->max_stack != max_stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 * Display what we got based on the order setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	for (i = 0; i < ui->idx && !err; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		int j = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (callchain_param.order == ORDER_CALLER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			j = ui->idx - i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	dwfl_end(ui->dwfl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	free(ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }