fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / util / unwind.c
blob742f23bf35ff7fabaae020100ebe3d359191a3da
1 /*
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
6 * forgotten):
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>
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"
34 extern int
35 UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
36 unw_word_t ip,
37 unw_dyn_info_t *di,
38 unw_proc_info_t *pi,
39 int need_unwind_info, void *arg);
41 #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
43 extern int
44 UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
45 unw_word_t ip,
46 unw_word_t segbase,
47 const char *obj_name, unw_word_t start,
48 unw_word_t end);
50 #define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
52 #define DW_EH_PE_FORMAT_MASK 0x0f /* format of the encoded value */
53 #define DW_EH_PE_APPL_MASK 0x70 /* how the value is to be applied */
55 /* Pointer-encoding formats: */
56 #define DW_EH_PE_omit 0xff
57 #define DW_EH_PE_ptr 0x00 /* pointer-sized unsigned value */
58 #define DW_EH_PE_udata4 0x03 /* unsigned 32-bit value */
59 #define DW_EH_PE_udata8 0x04 /* unsigned 64-bit value */
60 #define DW_EH_PE_sdata4 0x0b /* signed 32-bit value */
61 #define DW_EH_PE_sdata8 0x0c /* signed 64-bit value */
63 /* Pointer-encoding application: */
64 #define DW_EH_PE_absptr 0x00 /* absolute value */
65 #define DW_EH_PE_pcrel 0x10 /* rel. to addr. of encoded value */
68 * The following are not documented by LSB v1.3, yet they are used by
69 * GCC, presumably they aren't documented by LSB since they aren't
70 * used on Linux:
72 #define DW_EH_PE_funcrel 0x40 /* start-of-procedure-relative */
73 #define DW_EH_PE_aligned 0x50 /* aligned pointer */
75 /* Flags intentionaly not handled, since they're not needed:
76 * #define DW_EH_PE_indirect 0x80
77 * #define DW_EH_PE_uleb128 0x01
78 * #define DW_EH_PE_udata2 0x02
79 * #define DW_EH_PE_sleb128 0x09
80 * #define DW_EH_PE_sdata2 0x0a
81 * #define DW_EH_PE_textrel 0x20
82 * #define DW_EH_PE_datarel 0x30
85 struct unwind_info {
86 struct perf_sample *sample;
87 struct machine *machine;
88 struct thread *thread;
89 u64 sample_uregs;
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; \
102 static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val,
103 u8 encoding)
105 u8 *cur = *p;
106 *val = 0;
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;
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;
129 if ((encoding & 0x07) == 0x00)
130 encoding |= DW_EH_PE_udata4;
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;
149 out:
150 *p = cur;
151 return 0;
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; \
159 __v; \
162 static u64 elf_section_offset(int fd, const char *name)
164 Elf *elf;
165 GElf_Ehdr ehdr;
166 GElf_Shdr shdr;
167 u64 offset = 0;
169 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
170 if (elf == NULL)
171 return 0;
173 do {
174 if (gelf_getehdr(elf, &ehdr) == NULL)
175 break;
177 if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL))
178 break;
180 offset = shdr.sh_offset;
181 } while (0);
183 elf_end(elf);
184 return offset;
187 struct table_entry {
188 u32 start_ip_offset;
189 u32 fde_offset;
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;
199 * The rest of the header is variable-length and consists of the
200 * following members:
202 * encoded_t eh_frame_ptr;
203 * encoded_t fde_count;
206 /* A single encoded pointer should not be more than 8 bytes. */
207 u64 enc[2];
210 * struct {
211 * encoded_t start_ip;
212 * encoded_t fde_addr;
213 * } binary_search_table[fde_count];
215 char data[0];
216 } __packed;
218 static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
219 u64 offset, u64 *table_data, u64 *segbase,
220 u64 *fde_count)
222 struct eh_frame_hdr hdr;
223 u8 *enc = (u8 *) &hdr.enc;
224 u8 *end = (u8 *) &hdr.data;
225 ssize_t r;
227 r = dso__data_read_offset(dso, machine, offset,
228 (u8 *) &hdr, sizeof(hdr));
229 if (r != sizeof(hdr))
230 return -EINVAL;
232 /* We dont need eh_frame_ptr, just skip it. */
233 dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc);
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;
241 static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
242 u64 *table_data, u64 *segbase,
243 u64 *fde_count)
245 int ret = -EINVAL, fd;
246 u64 offset;
248 fd = dso__data_fd(dso, machine);
249 if (fd < 0)
250 return -EINVAL;
252 /* Check the .eh_frame section for unwinding info */
253 offset = elf_section_offset(fd, ".eh_frame_hdr");
254 close(fd);
256 if (offset)
257 ret = unwind_spec_ehframe(dso, machine, offset,
258 table_data, segbase,
259 fde_count);
261 return ret;
264 #ifndef NO_LIBUNWIND_DEBUG_FRAME
265 static int read_unwind_spec_debug_frame(struct dso *dso,
266 struct machine *machine, u64 *offset)
268 int fd = dso__data_fd(dso, machine);
270 if (fd < 0)
271 return -EINVAL;
273 /* Check the .debug_frame section for unwinding info */
274 *offset = elf_section_offset(fd, ".debug_frame");
275 close(fd);
277 if (*offset)
278 return 0;
280 return -EINVAL;
282 #endif
284 static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
286 struct addr_location al;
288 thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
289 MAP__FUNCTION, ip, &al);
290 return al.map;
293 static int
294 find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
295 int need_unwind_info, void *arg)
297 struct unwind_info *ui = arg;
298 struct map *map;
299 unw_dyn_info_t di;
300 u64 table_data, segbase, fde_count;
302 map = find_map(ip, ui);
303 if (!map || !map->dso)
304 return -EINVAL;
306 pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
308 /* Check the .eh_frame section for unwinding info */
309 if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
310 &table_data, &segbase, &fde_count)) {
311 memset(&di, 0, sizeof(di));
312 di.format = UNW_INFO_FORMAT_REMOTE_TABLE;
313 di.start_ip = map->start;
314 di.end_ip = map->end;
315 di.u.rti.segbase = map->start + segbase;
316 di.u.rti.table_data = map->start + table_data;
317 di.u.rti.table_len = fde_count * sizeof(struct table_entry)
318 / sizeof(unw_word_t);
319 return dwarf_search_unwind_table(as, ip, &di, pi,
320 need_unwind_info, arg);
323 #ifndef NO_LIBUNWIND_DEBUG_FRAME
324 /* Check the .debug_frame section for unwinding info */
325 if (!read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
326 memset(&di, 0, sizeof(di));
327 if (dwarf_find_debug_frame(0, &di, ip, 0, map->dso->name,
328 map->start, map->end))
329 return dwarf_search_unwind_table(as, ip, &di, pi,
330 need_unwind_info, arg);
332 #endif
334 return -EINVAL;
337 static int access_fpreg(unw_addr_space_t __maybe_unused as,
338 unw_regnum_t __maybe_unused num,
339 unw_fpreg_t __maybe_unused *val,
340 int __maybe_unused __write,
341 void __maybe_unused *arg)
343 pr_err("unwind: access_fpreg unsupported\n");
344 return -UNW_EINVAL;
347 static int get_dyn_info_list_addr(unw_addr_space_t __maybe_unused as,
348 unw_word_t __maybe_unused *dil_addr,
349 void __maybe_unused *arg)
351 return -UNW_ENOINFO;
354 static int resume(unw_addr_space_t __maybe_unused as,
355 unw_cursor_t __maybe_unused *cu,
356 void __maybe_unused *arg)
358 pr_err("unwind: resume unsupported\n");
359 return -UNW_EINVAL;
362 static int
363 get_proc_name(unw_addr_space_t __maybe_unused as,
364 unw_word_t __maybe_unused addr,
365 char __maybe_unused *bufp, size_t __maybe_unused buf_len,
366 unw_word_t __maybe_unused *offp, void __maybe_unused *arg)
368 pr_err("unwind: get_proc_name unsupported\n");
369 return -UNW_EINVAL;
372 static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
373 unw_word_t *data)
375 struct addr_location al;
376 ssize_t size;
378 thread__find_addr_map(ui->thread, ui->machine, PERF_RECORD_MISC_USER,
379 MAP__FUNCTION, addr, &al);
380 if (!al.map) {
381 pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
382 return -1;
385 if (!al.map->dso)
386 return -1;
388 size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
389 addr, (u8 *) data, sizeof(*data));
391 return !(size == sizeof(*data));
394 static int reg_value(unw_word_t *valp, struct regs_dump *regs, int id,
395 u64 sample_regs)
397 int i, idx = 0;
399 if (!(sample_regs & (1 << id)))
400 return -EINVAL;
402 for (i = 0; i < id; i++) {
403 if (sample_regs & (1 << i))
404 idx++;
407 *valp = regs->regs[idx];
408 return 0;
411 static int access_mem(unw_addr_space_t __maybe_unused as,
412 unw_word_t addr, unw_word_t *valp,
413 int __write, void *arg)
415 struct unwind_info *ui = arg;
416 struct stack_dump *stack = &ui->sample->user_stack;
417 unw_word_t start, end;
418 int offset;
419 int ret;
421 /* Don't support write, probably not needed. */
422 if (__write || !stack || !ui->sample->user_regs.regs) {
423 *valp = 0;
424 return 0;
427 ret = reg_value(&start, &ui->sample->user_regs, PERF_REG_SP,
428 ui->sample_uregs);
429 if (ret)
430 return ret;
432 end = start + stack->size;
434 /* Check overflow. */
435 if (addr + sizeof(unw_word_t) < addr)
436 return -EINVAL;
438 if (addr < start || addr + sizeof(unw_word_t) >= end) {
439 ret = access_dso_mem(ui, addr, valp);
440 if (ret) {
441 pr_debug("unwind: access_mem %p not inside range %p-%p\n",
442 (void *)addr, (void *)start, (void *)end);
443 *valp = 0;
444 return ret;
446 return 0;
449 offset = addr - start;
450 *valp = *(unw_word_t *)&stack->data[offset];
451 pr_debug("unwind: access_mem addr %p, val %lx, offset %d\n",
452 (void *)addr, (unsigned long)*valp, offset);
453 return 0;
456 static int access_reg(unw_addr_space_t __maybe_unused as,
457 unw_regnum_t regnum, unw_word_t *valp,
458 int __write, void *arg)
460 struct unwind_info *ui = arg;
461 int id, ret;
463 /* Don't support write, I suspect we don't need it. */
464 if (__write) {
465 pr_err("unwind: access_reg w %d\n", regnum);
466 return 0;
469 if (!ui->sample->user_regs.regs) {
470 *valp = 0;
471 return 0;
474 id = unwind__arch_reg_id(regnum);
475 if (id < 0)
476 return -EINVAL;
478 ret = reg_value(valp, &ui->sample->user_regs, id, ui->sample_uregs);
479 if (ret) {
480 pr_err("unwind: can't read reg %d\n", regnum);
481 return ret;
484 pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp);
485 return 0;
488 static void put_unwind_info(unw_addr_space_t __maybe_unused as,
489 unw_proc_info_t *pi __maybe_unused,
490 void *arg __maybe_unused)
492 pr_debug("unwind: put_unwind_info called\n");
495 static int entry(u64 ip, struct thread *thread, struct machine *machine,
496 unwind_entry_cb_t cb, void *arg)
498 struct unwind_entry e;
499 struct addr_location al;
501 thread__find_addr_location(thread, machine,
502 PERF_RECORD_MISC_USER,
503 MAP__FUNCTION, ip, &al);
505 e.ip = ip;
506 e.map = al.map;
507 e.sym = al.sym;
509 pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
510 al.sym ? al.sym->name : "''",
512 al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
514 return cb(&e, arg);
517 static void display_error(int err)
519 switch (err) {
520 case UNW_EINVAL:
521 pr_err("unwind: Only supports local.\n");
522 break;
523 case UNW_EUNSPEC:
524 pr_err("unwind: Unspecified error.\n");
525 break;
526 case UNW_EBADREG:
527 pr_err("unwind: Register unavailable.\n");
528 break;
529 default:
530 break;
534 static unw_accessors_t accessors = {
535 .find_proc_info = find_proc_info,
536 .put_unwind_info = put_unwind_info,
537 .get_dyn_info_list_addr = get_dyn_info_list_addr,
538 .access_mem = access_mem,
539 .access_reg = access_reg,
540 .access_fpreg = access_fpreg,
541 .resume = resume,
542 .get_proc_name = get_proc_name,
545 static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
546 void *arg, int max_stack)
548 unw_addr_space_t addr_space;
549 unw_cursor_t c;
550 int ret;
552 addr_space = unw_create_addr_space(&accessors, 0);
553 if (!addr_space) {
554 pr_err("unwind: Can't create unwind address space.\n");
555 return -ENOMEM;
558 ret = unw_init_remote(&c, addr_space, ui);
559 if (ret)
560 display_error(ret);
562 while (!ret && (unw_step(&c) > 0) && max_stack--) {
563 unw_word_t ip;
565 unw_get_reg(&c, UNW_REG_IP, &ip);
566 ret = entry(ip, ui->thread, ui->machine, cb, arg);
569 unw_destroy_addr_space(addr_space);
570 return ret;
573 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
574 struct machine *machine, struct thread *thread,
575 u64 sample_uregs, struct perf_sample *data,
576 int max_stack)
578 unw_word_t ip;
579 struct unwind_info ui = {
580 .sample = data,
581 .sample_uregs = sample_uregs,
582 .thread = thread,
583 .machine = machine,
585 int ret;
587 if (!data->user_regs.regs)
588 return -EINVAL;
590 ret = reg_value(&ip, &data->user_regs, PERF_REG_IP, sample_uregs);
591 if (ret)
592 return ret;
594 ret = entry(ip, thread, machine, cb, arg);
595 if (ret)
596 return -ENOMEM;
598 return get_entries(&ui, cb, arg, max_stack);