2 * Post mortem Dwarf CFI based unwinding on top of regs and stack dumps.
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
8 * Copyright (C) 2002-2007 Hewlett-Packard Co
9 * Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
11 * And the bugs have been added by:
13 * Copyright (C) 2010, Frederic Weisbecker <fweisbec@gmail.com>
14 * Copyright (C) 2012, Jiri Olsa <jolsa@redhat.com>
24 #include <linux/list.h>
25 #include <libunwind.h>
26 #include <libunwind-ptrace.h>
29 #include "perf_regs.h"
34 UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
38 int need_unwind_info, void *arg);
40 #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
42 #define DW_EH_PE_FORMAT_MASK 0x0f /* format of the encoded value */
43 #define DW_EH_PE_APPL_MASK 0x70 /* how the value is to be applied */
45 /* Pointer-encoding formats: */
46 #define DW_EH_PE_omit 0xff
47 #define DW_EH_PE_ptr 0x00 /* pointer-sized unsigned value */
48 #define DW_EH_PE_udata4 0x03 /* unsigned 32-bit value */
49 #define DW_EH_PE_udata8 0x04 /* unsigned 64-bit value */
50 #define DW_EH_PE_sdata4 0x0b /* signed 32-bit value */
51 #define DW_EH_PE_sdata8 0x0c /* signed 64-bit value */
53 /* Pointer-encoding application: */
54 #define DW_EH_PE_absptr 0x00 /* absolute value */
55 #define DW_EH_PE_pcrel 0x10 /* rel. to addr. of encoded value */
58 * The following are not documented by LSB v1.3, yet they are used by
59 * GCC, presumably they aren't documented by LSB since they aren't
62 #define DW_EH_PE_funcrel 0x40 /* start-of-procedure-relative */
63 #define DW_EH_PE_aligned 0x50 /* aligned pointer */
65 /* Flags intentionaly not handled, since they're not needed:
66 * #define DW_EH_PE_indirect 0x80
67 * #define DW_EH_PE_uleb128 0x01
68 * #define DW_EH_PE_udata2 0x02
69 * #define DW_EH_PE_sleb128 0x09
70 * #define DW_EH_PE_sdata2 0x0a
71 * #define DW_EH_PE_textrel 0x20
72 * #define DW_EH_PE_datarel 0x30
76 struct perf_sample *sample;
77 struct machine *machine;
78 struct thread *thread;
82 #define dw_read(ptr, type, end) ({ \
83 type *__p = (type *) ptr; \
85 if ((__p + 1) > (type *) end) \
88 ptr = (typeof(ptr)) __p; \
92 static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val,
103 *val = dw_read(cur, unsigned long, end);
109 switch (encoding & DW_EH_PE_APPL_MASK) {
110 case DW_EH_PE_absptr:
113 *val = (unsigned long) cur;
119 if ((encoding & 0x07) == 0x00)
120 encoding |= DW_EH_PE_udata4;
122 switch (encoding & DW_EH_PE_FORMAT_MASK) {
123 case DW_EH_PE_sdata4:
124 *val += dw_read(cur, s32, end);
126 case DW_EH_PE_udata4:
127 *val += dw_read(cur, u32, end);
129 case DW_EH_PE_sdata8:
130 *val += dw_read(cur, s64, end);
132 case DW_EH_PE_udata8:
133 *val += dw_read(cur, u64, end);
144 #define dw_read_encoded_value(ptr, end, enc) ({ \
146 if (__dw_read_encoded_value(&ptr, end, &__v, enc)) { \
152 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
153 GElf_Shdr *shp, const char *name)
157 while ((sec = elf_nextscn(elf, sec)) != NULL) {
160 gelf_getshdr(sec, shp);
161 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
162 if (!strcmp(name, str))
169 static u64 elf_section_offset(int fd, const char *name)
176 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
181 if (gelf_getehdr(elf, &ehdr) == NULL)
184 if (!elf_section_by_name(elf, &ehdr, &shdr, name))
187 offset = shdr.sh_offset;
199 struct eh_frame_hdr {
200 unsigned char version;
201 unsigned char eh_frame_ptr_enc;
202 unsigned char fde_count_enc;
203 unsigned char table_enc;
206 * The rest of the header is variable-length and consists of the
209 * encoded_t eh_frame_ptr;
210 * encoded_t fde_count;
213 /* A single encoded pointer should not be more than 8 bytes. */
218 * encoded_t start_ip;
219 * encoded_t fde_addr;
220 * } binary_search_table[fde_count];
225 static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
226 u64 offset, u64 *table_data, u64 *segbase,
229 struct eh_frame_hdr hdr;
230 u8 *enc = (u8 *) &hdr.enc;
231 u8 *end = (u8 *) &hdr.data;
234 r = dso__data_read_offset(dso, machine, offset,
235 (u8 *) &hdr, sizeof(hdr));
236 if (r != sizeof(hdr))
239 /* We dont need eh_frame_ptr, just skip it. */
240 dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc);
242 *fde_count = dw_read_encoded_value(enc, end, hdr.fde_count_enc);
244 *table_data = (enc - (u8 *) &hdr) + offset;
248 static int read_unwind_spec(struct dso *dso, struct machine *machine,
249 u64 *table_data, u64 *segbase, u64 *fde_count)
251 int ret = -EINVAL, fd;
254 fd = dso__data_fd(dso, machine);
258 offset = elf_section_offset(fd, ".eh_frame_hdr");
262 ret = unwind_spec_ehframe(dso, machine, offset,
266 /* TODO .debug_frame check if eh_frame_hdr fails */
270 static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
272 struct addr_location al;
274 thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
275 MAP__FUNCTION, ip, &al);
280 find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
281 int need_unwind_info, void *arg)
283 struct unwind_info *ui = arg;
286 u64 table_data, segbase, fde_count;
288 map = find_map(ip, ui);
289 if (!map || !map->dso)
292 pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
294 if (read_unwind_spec(map->dso, ui->machine,
295 &table_data, &segbase, &fde_count))
298 memset(&di, 0, sizeof(di));
299 di.format = UNW_INFO_FORMAT_REMOTE_TABLE;
300 di.start_ip = map->start;
301 di.end_ip = map->end;
302 di.u.rti.segbase = map->start + segbase;
303 di.u.rti.table_data = map->start + table_data;
304 di.u.rti.table_len = fde_count * sizeof(struct table_entry)
305 / sizeof(unw_word_t);
306 return dwarf_search_unwind_table(as, ip, &di, pi,
307 need_unwind_info, arg);
310 static int access_fpreg(unw_addr_space_t __maybe_unused as,
311 unw_regnum_t __maybe_unused num,
312 unw_fpreg_t __maybe_unused *val,
313 int __maybe_unused __write,
314 void __maybe_unused *arg)
316 pr_err("unwind: access_fpreg unsupported\n");
320 static int get_dyn_info_list_addr(unw_addr_space_t __maybe_unused as,
321 unw_word_t __maybe_unused *dil_addr,
322 void __maybe_unused *arg)
327 static int resume(unw_addr_space_t __maybe_unused as,
328 unw_cursor_t __maybe_unused *cu,
329 void __maybe_unused *arg)
331 pr_err("unwind: resume unsupported\n");
336 get_proc_name(unw_addr_space_t __maybe_unused as,
337 unw_word_t __maybe_unused addr,
338 char __maybe_unused *bufp, size_t __maybe_unused buf_len,
339 unw_word_t __maybe_unused *offp, void __maybe_unused *arg)
341 pr_err("unwind: get_proc_name unsupported\n");
345 static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
348 struct addr_location al;
351 thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
352 MAP__FUNCTION, addr, &al);
354 pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
361 size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
362 addr, (u8 *) data, sizeof(*data));
364 return !(size == sizeof(*data));
367 static int reg_value(unw_word_t *valp, struct regs_dump *regs, int id,
372 if (!(sample_regs & (1 << id)))
375 for (i = 0; i < id; i++) {
376 if (sample_regs & (1 << i))
380 *valp = regs->regs[idx];
384 static int access_mem(unw_addr_space_t __maybe_unused as,
385 unw_word_t addr, unw_word_t *valp,
386 int __write, void *arg)
388 struct unwind_info *ui = arg;
389 struct stack_dump *stack = &ui->sample->user_stack;
390 unw_word_t start, end;
394 /* Don't support write, probably not needed. */
395 if (__write || !stack || !ui->sample->user_regs.regs) {
400 ret = reg_value(&start, &ui->sample->user_regs, PERF_REG_SP,
405 end = start + stack->size;
407 /* Check overflow. */
408 if (addr + sizeof(unw_word_t) < addr)
411 if (addr < start || addr + sizeof(unw_word_t) >= end) {
412 ret = access_dso_mem(ui, addr, valp);
414 pr_debug("unwind: access_mem %p not inside range %p-%p\n",
415 (void *)addr, (void *)start, (void *)end);
422 offset = addr - start;
423 *valp = *(unw_word_t *)&stack->data[offset];
424 pr_debug("unwind: access_mem addr %p, val %lx, offset %d\n",
425 (void *)addr, (unsigned long)*valp, offset);
429 static int access_reg(unw_addr_space_t __maybe_unused as,
430 unw_regnum_t regnum, unw_word_t *valp,
431 int __write, void *arg)
433 struct unwind_info *ui = arg;
436 /* Don't support write, I suspect we don't need it. */
438 pr_err("unwind: access_reg w %d\n", regnum);
442 if (!ui->sample->user_regs.regs) {
447 id = unwind__arch_reg_id(regnum);
451 ret = reg_value(valp, &ui->sample->user_regs, id, ui->sample_uregs);
453 pr_err("unwind: can't read reg %d\n", regnum);
457 pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp);
461 static void put_unwind_info(unw_addr_space_t __maybe_unused as,
462 unw_proc_info_t *pi __maybe_unused,
463 void *arg __maybe_unused)
465 pr_debug("unwind: put_unwind_info called\n");
468 static int entry(u64 ip, struct thread *thread, struct machine *machine,
469 unwind_entry_cb_t cb, void *arg)
471 struct unwind_entry e;
472 struct addr_location al;
474 thread__find_addr_location(thread, machine,
475 PERF_RECORD_MISC_USER,
476 MAP__FUNCTION, ip, &al, NULL);
482 pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
483 al.sym ? al.sym->name : "''",
485 al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
490 static void display_error(int err)
494 pr_err("unwind: Only supports local.\n");
497 pr_err("unwind: Unspecified error.\n");
500 pr_err("unwind: Register unavailable.\n");
507 static unw_accessors_t accessors = {
508 .find_proc_info = find_proc_info,
509 .put_unwind_info = put_unwind_info,
510 .get_dyn_info_list_addr = get_dyn_info_list_addr,
511 .access_mem = access_mem,
512 .access_reg = access_reg,
513 .access_fpreg = access_fpreg,
515 .get_proc_name = get_proc_name,
518 static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
521 unw_addr_space_t addr_space;
525 addr_space = unw_create_addr_space(&accessors, 0);
527 pr_err("unwind: Can't create unwind address space.\n");
531 ret = unw_init_remote(&c, addr_space, ui);
535 while (!ret && (unw_step(&c) > 0)) {
538 unw_get_reg(&c, UNW_REG_IP, &ip);
539 ret = entry(ip, ui->thread, ui->machine, cb, arg);
542 unw_destroy_addr_space(addr_space);
546 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
547 struct machine *machine, struct thread *thread,
548 u64 sample_uregs, struct perf_sample *data)
551 struct unwind_info ui = {
553 .sample_uregs = sample_uregs,
559 if (!data->user_regs.regs)
562 ret = reg_value(&ip, &data->user_regs, PERF_REG_IP, sample_uregs);
566 ret = entry(ip, thread, machine, cb, arg);
570 return get_entries(&ui, cb, arg);