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