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