1 /*
2  * Post mortem Dwarf CFI based unwinding on top of regs and stack dumps.
3  *
4  * Lots of this code have been borrowed or heavily inspired from parts of
5  * the libunwind 0.99 code which are (amongst other contributors I may have
6  * forgotten):
7  *
8  * Copyright (C) 2002-2007 Hewlett-Packard Co
9  *	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
10  *
11  * And the bugs have been added by:
12  *
13  * Copyright (C) 2010, Frederic Weisbecker <fweisbec@gmail.com>
14  * Copyright (C) 2012, Jiri Olsa <jolsa@redhat.com>
15  *
16  */
17 
18 #include <elf.h>
19 #include <gelf.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/mman.h>
24 #include <linux/list.h>
25 #include <libunwind.h>
26 #include <libunwind-ptrace.h>
27 #include "thread.h"
28 #include "session.h"
29 #include "perf_regs.h"
30 #include "unwind.h"
31 #include "symbol.h"
32 #include "util.h"
33 #include "debug.h"
34 
35 extern int
36 UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
37 				    unw_word_t ip,
38 				    unw_dyn_info_t *di,
39 				    unw_proc_info_t *pi,
40 				    int need_unwind_info, void *arg);
41 
42 #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
43 
44 extern int
45 UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
46 				 unw_word_t ip,
47 				 unw_word_t segbase,
48 				 const char *obj_name, unw_word_t start,
49 				 unw_word_t end);
50 
51 #define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
52 
53 #define DW_EH_PE_FORMAT_MASK	0x0f	/* format of the encoded value */
54 #define DW_EH_PE_APPL_MASK	0x70	/* how the value is to be applied */
55 
56 /* Pointer-encoding formats: */
57 #define DW_EH_PE_omit		0xff
58 #define DW_EH_PE_ptr		0x00	/* pointer-sized unsigned value */
59 #define DW_EH_PE_udata4		0x03	/* unsigned 32-bit value */
60 #define DW_EH_PE_udata8		0x04	/* unsigned 64-bit value */
61 #define DW_EH_PE_sdata4		0x0b	/* signed 32-bit value */
62 #define DW_EH_PE_sdata8		0x0c	/* signed 64-bit value */
63 
64 /* Pointer-encoding application: */
65 #define DW_EH_PE_absptr		0x00	/* absolute value */
66 #define DW_EH_PE_pcrel		0x10	/* rel. to addr. of encoded value */
67 
68 /*
69  * The following are not documented by LSB v1.3, yet they are used by
70  * GCC, presumably they aren't documented by LSB since they aren't
71  * used on Linux:
72  */
73 #define DW_EH_PE_funcrel	0x40	/* start-of-procedure-relative */
74 #define DW_EH_PE_aligned	0x50	/* aligned pointer */
75 
76 /* Flags intentionaly not handled, since they're not needed:
77  * #define DW_EH_PE_indirect      0x80
78  * #define DW_EH_PE_uleb128       0x01
79  * #define DW_EH_PE_udata2        0x02
80  * #define DW_EH_PE_sleb128       0x09
81  * #define DW_EH_PE_sdata2        0x0a
82  * #define DW_EH_PE_textrel       0x20
83  * #define DW_EH_PE_datarel       0x30
84  */
85 
86 struct unwind_info {
87 	struct perf_sample	*sample;
88 	struct machine		*machine;
89 	struct thread		*thread;
90 };
91 
92 #define dw_read(ptr, type, end) ({	\
93 	type *__p = (type *) ptr;	\
94 	type  __v;			\
95 	if ((__p + 1) > (type *) end)	\
96 		return -EINVAL;		\
97 	__v = *__p++;			\
98 	ptr = (typeof(ptr)) __p;	\
99 	__v;				\
100 	})
101 
102 static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val,
103 				   u8 encoding)
104 {
105 	u8 *cur = *p;
106 	*val = 0;
107 
108 	switch (encoding) {
109 	case DW_EH_PE_omit:
110 		*val = 0;
111 		goto out;
112 	case DW_EH_PE_ptr:
113 		*val = dw_read(cur, unsigned long, end);
114 		goto out;
115 	default:
116 		break;
117 	}
118 
119 	switch (encoding & DW_EH_PE_APPL_MASK) {
120 	case DW_EH_PE_absptr:
121 		break;
122 	case DW_EH_PE_pcrel:
123 		*val = (unsigned long) cur;
124 		break;
125 	default:
126 		return -EINVAL;
127 	}
128 
129 	if ((encoding & 0x07) == 0x00)
130 		encoding |= DW_EH_PE_udata4;
131 
132 	switch (encoding & DW_EH_PE_FORMAT_MASK) {
133 	case DW_EH_PE_sdata4:
134 		*val += dw_read(cur, s32, end);
135 		break;
136 	case DW_EH_PE_udata4:
137 		*val += dw_read(cur, u32, end);
138 		break;
139 	case DW_EH_PE_sdata8:
140 		*val += dw_read(cur, s64, end);
141 		break;
142 	case DW_EH_PE_udata8:
143 		*val += dw_read(cur, u64, end);
144 		break;
145 	default:
146 		return -EINVAL;
147 	}
148 
149  out:
150 	*p = cur;
151 	return 0;
152 }
153 
154 #define dw_read_encoded_value(ptr, end, enc) ({			\
155 	u64 __v;						\
156 	if (__dw_read_encoded_value(&ptr, end, &__v, enc)) {	\
157 		return -EINVAL;                                 \
158 	}                                                       \
159 	__v;                                                    \
160 	})
161 
162 static u64 elf_section_offset(int fd, const char *name)
163 {
164 	Elf *elf;
165 	GElf_Ehdr ehdr;
166 	GElf_Shdr shdr;
167 	u64 offset = 0;
168 
169 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
170 	if (elf == NULL)
171 		return 0;
172 
173 	do {
174 		if (gelf_getehdr(elf, &ehdr) == NULL)
175 			break;
176 
177 		if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL))
178 			break;
179 
180 		offset = shdr.sh_offset;
181 	} while (0);
182 
183 	elf_end(elf);
184 	return offset;
185 }
186 
187 struct table_entry {
188 	u32 start_ip_offset;
189 	u32 fde_offset;
190 };
191 
192 struct eh_frame_hdr {
193 	unsigned char version;
194 	unsigned char eh_frame_ptr_enc;
195 	unsigned char fde_count_enc;
196 	unsigned char table_enc;
197 
198 	/*
199 	 * The rest of the header is variable-length and consists of the
200 	 * following members:
201 	 *
202 	 *	encoded_t eh_frame_ptr;
203 	 *	encoded_t fde_count;
204 	 */
205 
206 	/* A single encoded pointer should not be more than 8 bytes. */
207 	u64 enc[2];
208 
209 	/*
210 	 * struct {
211 	 *    encoded_t start_ip;
212 	 *    encoded_t fde_addr;
213 	 * } binary_search_table[fde_count];
214 	 */
215 	char data[0];
216 } __packed;
217 
218 static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
219 			       u64 offset, u64 *table_data, u64 *segbase,
220 			       u64 *fde_count)
221 {
222 	struct eh_frame_hdr hdr;
223 	u8 *enc = (u8 *) &hdr.enc;
224 	u8 *end = (u8 *) &hdr.data;
225 	ssize_t r;
226 
227 	r = dso__data_read_offset(dso, machine, offset,
228 				  (u8 *) &hdr, sizeof(hdr));
229 	if (r != sizeof(hdr))
230 		return -EINVAL;
231 
232 	/* We dont need eh_frame_ptr, just skip it. */
233 	dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc);
234 
235 	*fde_count  = dw_read_encoded_value(enc, end, hdr.fde_count_enc);
236 	*segbase    = offset;
237 	*table_data = (enc - (u8 *) &hdr) + offset;
238 	return 0;
239 }
240 
241 static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
242 				     u64 *table_data, u64 *segbase,
243 				     u64 *fde_count)
244 {
245 	int ret = -EINVAL, fd;
246 	u64 offset;
247 
248 	fd = dso__data_fd(dso, machine);
249 	if (fd < 0)
250 		return -EINVAL;
251 
252 	/* Check the .eh_frame section for unwinding info */
253 	offset = elf_section_offset(fd, ".eh_frame_hdr");
254 
255 	if (offset)
256 		ret = unwind_spec_ehframe(dso, machine, offset,
257 					  table_data, segbase,
258 					  fde_count);
259 
260 	return ret;
261 }
262 
263 #ifndef NO_LIBUNWIND_DEBUG_FRAME
264 static int read_unwind_spec_debug_frame(struct dso *dso,
265 					struct machine *machine, u64 *offset)
266 {
267 	int fd = dso__data_fd(dso, machine);
268 
269 	if (fd < 0)
270 		return -EINVAL;
271 
272 	/* Check the .debug_frame section for unwinding info */
273 	*offset = elf_section_offset(fd, ".debug_frame");
274 
275 	if (*offset)
276 		return 0;
277 
278 	return -EINVAL;
279 }
280 #endif
281 
282 static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
283 {
284 	struct addr_location al;
285 
286 	thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
287 			      MAP__FUNCTION, ip, &al);
288 	return al.map;
289 }
290 
291 static int
292 find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
293 	       int need_unwind_info, void *arg)
294 {
295 	struct unwind_info *ui = arg;
296 	struct map *map;
297 	unw_dyn_info_t di;
298 	u64 table_data, segbase, fde_count;
299 
300 	map = find_map(ip, ui);
301 	if (!map || !map->dso)
302 		return -EINVAL;
303 
304 	pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
305 
306 	/* Check the .eh_frame section for unwinding info */
307 	if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
308 				       &table_data, &segbase, &fde_count)) {
309 		memset(&di, 0, sizeof(di));
310 		di.format   = UNW_INFO_FORMAT_REMOTE_TABLE;
311 		di.start_ip = map->start;
312 		di.end_ip   = map->end;
313 		di.u.rti.segbase    = map->start + segbase;
314 		di.u.rti.table_data = map->start + table_data;
315 		di.u.rti.table_len  = fde_count * sizeof(struct table_entry)
316 				      / sizeof(unw_word_t);
317 		return dwarf_search_unwind_table(as, ip, &di, pi,
318 						 need_unwind_info, arg);
319 	}
320 
321 #ifndef NO_LIBUNWIND_DEBUG_FRAME
322 	/* Check the .debug_frame section for unwinding info */
323 	if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
324 		memset(&di, 0, sizeof(di));
325 		if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
326 					   map->start, map->end))
327 			return dwarf_search_unwind_table(as, ip, &di, pi,
328 							 need_unwind_info, arg);
329 	}
330 #endif
331 
332 	return -EINVAL;
333 }
334 
335 static int access_fpreg(unw_addr_space_t __maybe_unused as,
336 			unw_regnum_t __maybe_unused num,
337 			unw_fpreg_t __maybe_unused *val,
338 			int __maybe_unused __write,
339 			void __maybe_unused *arg)
340 {
341 	pr_err("unwind: access_fpreg unsupported\n");
342 	return -UNW_EINVAL;
343 }
344 
345 static int get_dyn_info_list_addr(unw_addr_space_t __maybe_unused as,
346 				  unw_word_t __maybe_unused *dil_addr,
347 				  void __maybe_unused *arg)
348 {
349 	return -UNW_ENOINFO;
350 }
351 
352 static int resume(unw_addr_space_t __maybe_unused as,
353 		  unw_cursor_t __maybe_unused *cu,
354 		  void __maybe_unused *arg)
355 {
356 	pr_err("unwind: resume unsupported\n");
357 	return -UNW_EINVAL;
358 }
359 
360 static int
361 get_proc_name(unw_addr_space_t __maybe_unused as,
362 	      unw_word_t __maybe_unused addr,
363 		char __maybe_unused *bufp, size_t __maybe_unused buf_len,
364 		unw_word_t __maybe_unused *offp, void __maybe_unused *arg)
365 {
366 	pr_err("unwind: get_proc_name unsupported\n");
367 	return -UNW_EINVAL;
368 }
369 
370 static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
371 			  unw_word_t *data)
372 {
373 	struct addr_location al;
374 	ssize_t size;
375 
376 	thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
377 			      MAP__FUNCTION, addr, &al);
378 	if (!al.map) {
379 		pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
380 		return -1;
381 	}
382 
383 	if (!al.map->dso)
384 		return -1;
385 
386 	size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
387 				   addr, (u8 *) data, sizeof(*data));
388 
389 	return !(size == sizeof(*data));
390 }
391 
392 static int access_mem(unw_addr_space_t __maybe_unused as,
393 		      unw_word_t addr, unw_word_t *valp,
394 		      int __write, void *arg)
395 {
396 	struct unwind_info *ui = arg;
397 	struct stack_dump *stack = &ui->sample->user_stack;
398 	u64 start, end;
399 	int offset;
400 	int ret;
401 
402 	/* Don't support write, probably not needed. */
403 	if (__write || !stack || !ui->sample->user_regs.regs) {
404 		*valp = 0;
405 		return 0;
406 	}
407 
408 	ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
409 	if (ret)
410 		return ret;
411 
412 	end = start + stack->size;
413 
414 	/* Check overflow. */
415 	if (addr + sizeof(unw_word_t) < addr)
416 		return -EINVAL;
417 
418 	if (addr < start || addr + sizeof(unw_word_t) >= end) {
419 		ret = access_dso_mem(ui, addr, valp);
420 		if (ret) {
421 			pr_debug("unwind: access_mem %p not inside range"
422 				 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
423 				 (void *) addr, start, end);
424 			*valp = 0;
425 			return ret;
426 		}
427 		return 0;
428 	}
429 
430 	offset = addr - start;
431 	*valp  = *(unw_word_t *)&stack->data[offset];
432 	pr_debug("unwind: access_mem addr %p val %lx, offset %d\n",
433 		 (void *) addr, (unsigned long)*valp, offset);
434 	return 0;
435 }
436 
437 static int access_reg(unw_addr_space_t __maybe_unused as,
438 		      unw_regnum_t regnum, unw_word_t *valp,
439 		      int __write, void *arg)
440 {
441 	struct unwind_info *ui = arg;
442 	int id, ret;
443 	u64 val;
444 
445 	/* Don't support write, I suspect we don't need it. */
446 	if (__write) {
447 		pr_err("unwind: access_reg w %d\n", regnum);
448 		return 0;
449 	}
450 
451 	if (!ui->sample->user_regs.regs) {
452 		*valp = 0;
453 		return 0;
454 	}
455 
456 	id = libunwind__arch_reg_id(regnum);
457 	if (id < 0)
458 		return -EINVAL;
459 
460 	ret = perf_reg_value(&val, &ui->sample->user_regs, id);
461 	if (ret) {
462 		pr_err("unwind: can't read reg %d\n", regnum);
463 		return ret;
464 	}
465 
466 	*valp = (unw_word_t) val;
467 	pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp);
468 	return 0;
469 }
470 
471 static void put_unwind_info(unw_addr_space_t __maybe_unused as,
472 			    unw_proc_info_t *pi __maybe_unused,
473 			    void *arg __maybe_unused)
474 {
475 	pr_debug("unwind: put_unwind_info called\n");
476 }
477 
478 static int entry(u64 ip, struct thread *thread, struct machine *machine,
479 		 unwind_entry_cb_t cb, void *arg)
480 {
481 	struct unwind_entry e;
482 	struct addr_location al;
483 
484 	thread__find_addr_location(thread, machine,
485 				   PERF_RECORD_MISC_USER,
486 				   MAP__FUNCTION, ip, &al);
487 
488 	e.ip = ip;
489 	e.map = al.map;
490 	e.sym = al.sym;
491 
492 	pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
493 		 al.sym ? al.sym->name : "''",
494 		 ip,
495 		 al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
496 
497 	return cb(&e, arg);
498 }
499 
500 static void display_error(int err)
501 {
502 	switch (err) {
503 	case UNW_EINVAL:
504 		pr_err("unwind: Only supports local.\n");
505 		break;
506 	case UNW_EUNSPEC:
507 		pr_err("unwind: Unspecified error.\n");
508 		break;
509 	case UNW_EBADREG:
510 		pr_err("unwind: Register unavailable.\n");
511 		break;
512 	default:
513 		break;
514 	}
515 }
516 
517 static unw_accessors_t accessors = {
518 	.find_proc_info		= find_proc_info,
519 	.put_unwind_info	= put_unwind_info,
520 	.get_dyn_info_list_addr	= get_dyn_info_list_addr,
521 	.access_mem		= access_mem,
522 	.access_reg		= access_reg,
523 	.access_fpreg		= access_fpreg,
524 	.resume			= resume,
525 	.get_proc_name		= get_proc_name,
526 };
527 
528 static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
529 		       void *arg, int max_stack)
530 {
531 	unw_addr_space_t addr_space;
532 	unw_cursor_t c;
533 	int ret;
534 
535 	addr_space = unw_create_addr_space(&accessors, 0);
536 	if (!addr_space) {
537 		pr_err("unwind: Can't create unwind address space.\n");
538 		return -ENOMEM;
539 	}
540 
541 	ret = unw_init_remote(&c, addr_space, ui);
542 	if (ret)
543 		display_error(ret);
544 
545 	while (!ret && (unw_step(&c) > 0) && max_stack--) {
546 		unw_word_t ip;
547 
548 		unw_get_reg(&c, UNW_REG_IP, &ip);
549 		ret = ip ? entry(ip, ui->thread, ui->machine, cb, arg) : 0;
550 	}
551 
552 	unw_destroy_addr_space(addr_space);
553 	return ret;
554 }
555 
556 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
557 			struct machine *machine, struct thread *thread,
558 			struct perf_sample *data, int max_stack)
559 {
560 	u64 ip;
561 	struct unwind_info ui = {
562 		.sample       = data,
563 		.thread       = thread,
564 		.machine      = machine,
565 	};
566 	int ret;
567 
568 	if (!data->user_regs.regs)
569 		return -EINVAL;
570 
571 	ret = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
572 	if (ret)
573 		return ret;
574 
575 	ret = entry(ip, thread, machine, cb, arg);
576 	if (ret)
577 		return -ENOMEM;
578 
579 	return --max_stack > 0 ? get_entries(&ui, cb, arg, max_stack) : 0;
580 }
581