1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Post mortem Dwarf CFI based unwinding on top of regs and stack dumps. 4 * 5 * Lots of this code have been borrowed or heavily inspired from parts of 6 * the libunwind 0.99 code which are (amongst other contributors I may have 7 * forgotten): 8 * 9 * Copyright (C) 2002-2007 Hewlett-Packard Co 10 * Contributed by David Mosberger-Tang <davidm@hpl.hp.com> 11 * 12 * And the bugs have been added by: 13 * 14 * Copyright (C) 2010, Frederic Weisbecker <fweisbec@gmail.com> 15 * Copyright (C) 2012, Jiri Olsa <jolsa@redhat.com> 16 * 17 */ 18 19 #include <elf.h> 20 #include <errno.h> 21 #include <gelf.h> 22 #include <fcntl.h> 23 #include <inttypes.h> 24 #include <string.h> 25 #include <unistd.h> 26 #include <sys/mman.h> 27 #include <linux/list.h> 28 #ifndef REMOTE_UNWIND_LIBUNWIND 29 #include <libunwind.h> 30 #include <libunwind-ptrace.h> 31 #endif 32 #include "callchain.h" 33 #include "thread.h" 34 #include "session.h" 35 #include "perf_regs.h" 36 #include "unwind.h" 37 #include "map.h" 38 #include "symbol.h" 39 #include "debug.h" 40 #include "asm/bug.h" 41 #include "dso.h" 42 43 extern int 44 UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as, 45 unw_word_t ip, 46 unw_dyn_info_t *di, 47 unw_proc_info_t *pi, 48 int need_unwind_info, void *arg); 49 50 #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table) 51 52 extern int 53 UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug, 54 unw_word_t ip, 55 unw_word_t segbase, 56 const char *obj_name, unw_word_t start, 57 unw_word_t end); 58 59 #define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame) 60 61 #define DW_EH_PE_FORMAT_MASK 0x0f /* format of the encoded value */ 62 #define DW_EH_PE_APPL_MASK 0x70 /* how the value is to be applied */ 63 64 /* Pointer-encoding formats: */ 65 #define DW_EH_PE_omit 0xff 66 #define DW_EH_PE_ptr 0x00 /* pointer-sized unsigned value */ 67 #define DW_EH_PE_udata4 0x03 /* unsigned 32-bit value */ 68 #define DW_EH_PE_udata8 0x04 /* unsigned 64-bit value */ 69 #define DW_EH_PE_sdata4 0x0b /* signed 32-bit value */ 70 #define DW_EH_PE_sdata8 0x0c /* signed 64-bit value */ 71 72 /* Pointer-encoding application: */ 73 #define DW_EH_PE_absptr 0x00 /* absolute value */ 74 #define DW_EH_PE_pcrel 0x10 /* rel. to addr. of encoded value */ 75 76 /* 77 * The following are not documented by LSB v1.3, yet they are used by 78 * GCC, presumably they aren't documented by LSB since they aren't 79 * used on Linux: 80 */ 81 #define DW_EH_PE_funcrel 0x40 /* start-of-procedure-relative */ 82 #define DW_EH_PE_aligned 0x50 /* aligned pointer */ 83 84 /* Flags intentionally not handled, since they're not needed: 85 * #define DW_EH_PE_indirect 0x80 86 * #define DW_EH_PE_uleb128 0x01 87 * #define DW_EH_PE_udata2 0x02 88 * #define DW_EH_PE_sleb128 0x09 89 * #define DW_EH_PE_sdata2 0x0a 90 * #define DW_EH_PE_textrel 0x20 91 * #define DW_EH_PE_datarel 0x30 92 */ 93 94 struct unwind_info { 95 struct perf_sample *sample; 96 struct machine *machine; 97 struct thread *thread; 98 bool best_effort; 99 }; 100 101 #define dw_read(ptr, type, end) ({ \ 102 type *__p = (type *) ptr; \ 103 type __v; \ 104 if ((__p + 1) > (type *) end) \ 105 return -EINVAL; \ 106 __v = *__p++; \ 107 ptr = (typeof(ptr)) __p; \ 108 __v; \ 109 }) 110 111 static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val, 112 u8 encoding) 113 { 114 u8 *cur = *p; 115 *val = 0; 116 117 switch (encoding) { 118 case DW_EH_PE_omit: 119 *val = 0; 120 goto out; 121 case DW_EH_PE_ptr: 122 *val = dw_read(cur, unsigned long, end); 123 goto out; 124 default: 125 break; 126 } 127 128 switch (encoding & DW_EH_PE_APPL_MASK) { 129 case DW_EH_PE_absptr: 130 break; 131 case DW_EH_PE_pcrel: 132 *val = (unsigned long) cur; 133 break; 134 default: 135 return -EINVAL; 136 } 137 138 if ((encoding & 0x07) == 0x00) 139 encoding |= DW_EH_PE_udata4; 140 141 switch (encoding & DW_EH_PE_FORMAT_MASK) { 142 case DW_EH_PE_sdata4: 143 *val += dw_read(cur, s32, end); 144 break; 145 case DW_EH_PE_udata4: 146 *val += dw_read(cur, u32, end); 147 break; 148 case DW_EH_PE_sdata8: 149 *val += dw_read(cur, s64, end); 150 break; 151 case DW_EH_PE_udata8: 152 *val += dw_read(cur, u64, end); 153 break; 154 default: 155 return -EINVAL; 156 } 157 158 out: 159 *p = cur; 160 return 0; 161 } 162 163 #define dw_read_encoded_value(ptr, end, enc) ({ \ 164 u64 __v; \ 165 if (__dw_read_encoded_value(&ptr, end, &__v, enc)) { \ 166 return -EINVAL; \ 167 } \ 168 __v; \ 169 }) 170 171 static int elf_section_address_and_offset(int fd, const char *name, u64 *address, u64 *offset) 172 { 173 Elf *elf; 174 GElf_Ehdr ehdr; 175 GElf_Shdr shdr; 176 int ret = -1; 177 178 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 179 if (elf == NULL) 180 return -1; 181 182 if (gelf_getehdr(elf, &ehdr) == NULL) 183 goto out_err; 184 185 if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL)) 186 goto out_err; 187 188 *address = shdr.sh_addr; 189 *offset = shdr.sh_offset; 190 ret = 0; 191 out_err: 192 elf_end(elf); 193 return ret; 194 } 195 196 #ifndef NO_LIBUNWIND_DEBUG_FRAME 197 static u64 elf_section_offset(int fd, const char *name) 198 { 199 u64 address, offset = 0; 200 201 if (elf_section_address_and_offset(fd, name, &address, &offset)) 202 return 0; 203 204 return offset; 205 } 206 #endif 207 208 static u64 elf_base_address(int fd) 209 { 210 Elf *elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 211 GElf_Phdr phdr; 212 u64 retval = 0; 213 size_t i, phdrnum = 0; 214 215 if (elf == NULL) 216 return 0; 217 (void)elf_getphdrnum(elf, &phdrnum); 218 /* PT_LOAD segments are sorted by p_vaddr, so the first has the minimum p_vaddr. */ 219 for (i = 0; i < phdrnum; i++) { 220 if (gelf_getphdr(elf, i, &phdr) && phdr.p_type == PT_LOAD) { 221 retval = phdr.p_vaddr & -getpagesize(); 222 break; 223 } 224 } 225 226 elf_end(elf); 227 return retval; 228 } 229 230 #ifndef NO_LIBUNWIND_DEBUG_FRAME 231 static int elf_is_exec(int fd, const char *name) 232 { 233 Elf *elf; 234 GElf_Ehdr ehdr; 235 int retval = 0; 236 237 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 238 if (elf == NULL) 239 return 0; 240 if (gelf_getehdr(elf, &ehdr) == NULL) 241 goto out; 242 243 retval = (ehdr.e_type == ET_EXEC); 244 245 out: 246 elf_end(elf); 247 pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval); 248 return retval; 249 } 250 #endif 251 252 struct table_entry { 253 u32 start_ip_offset; 254 u32 fde_offset; 255 }; 256 257 struct eh_frame_hdr { 258 unsigned char version; 259 unsigned char eh_frame_ptr_enc; 260 unsigned char fde_count_enc; 261 unsigned char table_enc; 262 263 /* 264 * The rest of the header is variable-length and consists of the 265 * following members: 266 * 267 * encoded_t eh_frame_ptr; 268 * encoded_t fde_count; 269 */ 270 271 /* A single encoded pointer should not be more than 8 bytes. */ 272 u64 enc[2]; 273 274 /* 275 * struct { 276 * encoded_t start_ip; 277 * encoded_t fde_addr; 278 * } binary_search_table[fde_count]; 279 */ 280 char data[]; 281 } __packed; 282 283 static int unwind_spec_ehframe(struct dso *dso, struct machine *machine, 284 u64 offset, u64 *table_data_offset, u64 *fde_count) 285 { 286 struct eh_frame_hdr hdr; 287 u8 *enc = (u8 *) &hdr.enc; 288 u8 *end = (u8 *) &hdr.data; 289 ssize_t r; 290 291 r = dso__data_read_offset(dso, machine, offset, 292 (u8 *) &hdr, sizeof(hdr)); 293 if (r != sizeof(hdr)) 294 return -EINVAL; 295 296 /* We dont need eh_frame_ptr, just skip it. */ 297 dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc); 298 299 *fde_count = dw_read_encoded_value(enc, end, hdr.fde_count_enc); 300 *table_data_offset = enc - (u8 *) &hdr; 301 return 0; 302 } 303 304 struct read_unwind_spec_eh_frame_maps_cb_args { 305 struct dso *dso; 306 u64 base_addr; 307 }; 308 309 static int read_unwind_spec_eh_frame_maps_cb(struct map *map, void *data) 310 { 311 312 struct read_unwind_spec_eh_frame_maps_cb_args *args = data; 313 314 if (map__dso(map) == args->dso && map__start(map) - map__pgoff(map) < args->base_addr) 315 args->base_addr = map__start(map) - map__pgoff(map); 316 317 return 0; 318 } 319 320 321 static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui, 322 u64 *table_data, u64 *segbase, 323 u64 *fde_count) 324 { 325 struct read_unwind_spec_eh_frame_maps_cb_args args = { 326 .dso = dso, 327 .base_addr = UINT64_MAX, 328 }; 329 int ret, fd; 330 331 if (dso__data(dso)->eh_frame_hdr_offset == 0) { 332 if (!dso__data_get_fd(dso, ui->machine, &fd)) 333 return -EINVAL; 334 335 /* Check the .eh_frame section for unwinding info */ 336 ret = elf_section_address_and_offset(fd, ".eh_frame_hdr", 337 &dso__data(dso)->eh_frame_hdr_addr, 338 &dso__data(dso)->eh_frame_hdr_offset); 339 dso__data(dso)->elf_base_addr = elf_base_address(fd); 340 dso__data_put_fd(dso); 341 if (ret || dso__data(dso)->eh_frame_hdr_offset == 0) 342 return -EINVAL; 343 } 344 345 maps__for_each_map(thread__maps(ui->thread), read_unwind_spec_eh_frame_maps_cb, &args); 346 347 args.base_addr -= dso__data(dso)->elf_base_addr; 348 /* Address of .eh_frame_hdr */ 349 *segbase = args.base_addr + dso__data(dso)->eh_frame_hdr_addr; 350 ret = unwind_spec_ehframe(dso, ui->machine, dso__data(dso)->eh_frame_hdr_offset, 351 table_data, fde_count); 352 if (ret) 353 return ret; 354 /* binary_search_table offset plus .eh_frame_hdr address */ 355 *table_data += *segbase; 356 return 0; 357 } 358 359 #ifndef NO_LIBUNWIND_DEBUG_FRAME 360 static int read_unwind_spec_debug_frame(struct dso *dso, 361 struct machine *machine, u64 *offset) 362 { 363 int fd; 364 u64 ofs = dso__data(dso)->debug_frame_offset; 365 366 /* debug_frame can reside in: 367 * - dso 368 * - debug pointed by symsrc_filename 369 * - gnu_debuglink, which doesn't necessary 370 * has to be pointed by symsrc_filename 371 */ 372 if (ofs == 0) { 373 if (dso__data_get_fd(dso, machine, &fd)) { 374 ofs = elf_section_offset(fd, ".debug_frame"); 375 dso__data_put_fd(dso); 376 } 377 378 if (ofs <= 0) { 379 fd = open(dso__symsrc_filename(dso), O_RDONLY); 380 if (fd >= 0) { 381 ofs = elf_section_offset(fd, ".debug_frame"); 382 close(fd); 383 } 384 } 385 386 if (ofs <= 0) { 387 char *debuglink = malloc(PATH_MAX); 388 int ret = 0; 389 390 if (debuglink == NULL) { 391 pr_err("unwind: Can't read unwind spec debug frame.\n"); 392 return -ENOMEM; 393 } 394 395 ret = dso__read_binary_type_filename( 396 dso, DSO_BINARY_TYPE__DEBUGLINK, 397 machine->root_dir, debuglink, PATH_MAX); 398 if (!ret) { 399 fd = open(debuglink, O_RDONLY); 400 if (fd >= 0) { 401 ofs = elf_section_offset(fd, 402 ".debug_frame"); 403 close(fd); 404 } 405 } 406 if (ofs > 0) { 407 if (dso__symsrc_filename(dso) != NULL) { 408 pr_warning( 409 "%s: overwrite symsrc(%s,%s)\n", 410 __func__, 411 dso__symsrc_filename(dso), 412 debuglink); 413 dso__free_symsrc_filename(dso); 414 } 415 dso__set_symsrc_filename(dso, debuglink); 416 } else { 417 free(debuglink); 418 } 419 } 420 421 dso__data(dso)->debug_frame_offset = ofs; 422 } 423 424 *offset = ofs; 425 if (*offset) 426 return 0; 427 428 return -EINVAL; 429 } 430 #endif 431 432 static struct map *find_map(unw_word_t ip, struct unwind_info *ui) 433 { 434 struct addr_location al; 435 struct map *ret; 436 437 addr_location__init(&al); 438 thread__find_map(ui->thread, PERF_RECORD_MISC_USER, ip, &al); 439 ret = map__get(al.map); 440 addr_location__exit(&al); 441 return ret; 442 } 443 444 static int 445 find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, 446 int need_unwind_info, void *arg) 447 { 448 struct unwind_info *ui = arg; 449 struct map *map; 450 struct dso *dso; 451 unw_dyn_info_t di; 452 u64 table_data, segbase, fde_count; 453 int ret = -EINVAL; 454 455 map = find_map(ip, ui); 456 if (!map) 457 return -EINVAL; 458 459 dso = map__dso(map); 460 if (!dso) { 461 map__put(map); 462 return -EINVAL; 463 } 464 465 pr_debug("unwind: find_proc_info dso %s\n", dso__name(dso)); 466 467 /* Check the .eh_frame section for unwinding info */ 468 if (!read_unwind_spec_eh_frame(dso, ui, &table_data, &segbase, &fde_count)) { 469 memset(&di, 0, sizeof(di)); 470 di.format = UNW_INFO_FORMAT_REMOTE_TABLE; 471 di.start_ip = map__start(map); 472 di.end_ip = map__end(map); 473 di.u.rti.segbase = segbase; 474 di.u.rti.table_data = table_data; 475 di.u.rti.table_len = fde_count * sizeof(struct table_entry) 476 / sizeof(unw_word_t); 477 ret = dwarf_search_unwind_table(as, ip, &di, pi, 478 need_unwind_info, arg); 479 } 480 481 #ifndef NO_LIBUNWIND_DEBUG_FRAME 482 /* Check the .debug_frame section for unwinding info */ 483 if (ret < 0 && 484 !read_unwind_spec_debug_frame(dso, ui->machine, &segbase)) { 485 int fd; 486 u64 start = map__start(map); 487 unw_word_t base = start; 488 const char *symfile; 489 490 if (dso__data_get_fd(dso, ui->machine, &fd)) { 491 if (elf_is_exec(fd, dso__name(dso))) 492 base = 0; 493 dso__data_put_fd(dso); 494 } 495 496 symfile = dso__symsrc_filename(dso) ?: dso__name(dso); 497 498 memset(&di, 0, sizeof(di)); 499 if (dwarf_find_debug_frame(0, &di, ip, base, symfile, start, map__end(map))) 500 ret = dwarf_search_unwind_table(as, ip, &di, pi, 501 need_unwind_info, arg); 502 } 503 #endif 504 map__put(map); 505 return ret; 506 } 507 508 static int access_fpreg(unw_addr_space_t __maybe_unused as, 509 unw_regnum_t __maybe_unused num, 510 unw_fpreg_t __maybe_unused *val, 511 int __maybe_unused __write, 512 void __maybe_unused *arg) 513 { 514 pr_err("unwind: access_fpreg unsupported\n"); 515 return -UNW_EINVAL; 516 } 517 518 static int get_dyn_info_list_addr(unw_addr_space_t __maybe_unused as, 519 unw_word_t __maybe_unused *dil_addr, 520 void __maybe_unused *arg) 521 { 522 return -UNW_ENOINFO; 523 } 524 525 static int resume(unw_addr_space_t __maybe_unused as, 526 unw_cursor_t __maybe_unused *cu, 527 void __maybe_unused *arg) 528 { 529 pr_err("unwind: resume unsupported\n"); 530 return -UNW_EINVAL; 531 } 532 533 static int 534 get_proc_name(unw_addr_space_t __maybe_unused as, 535 unw_word_t __maybe_unused addr, 536 char __maybe_unused *bufp, size_t __maybe_unused buf_len, 537 unw_word_t __maybe_unused *offp, void __maybe_unused *arg) 538 { 539 pr_err("unwind: get_proc_name unsupported\n"); 540 return -UNW_EINVAL; 541 } 542 543 static int access_dso_mem(struct unwind_info *ui, unw_word_t addr, 544 unw_word_t *data) 545 { 546 struct map *map; 547 struct dso *dso; 548 ssize_t size; 549 550 map = find_map(addr, ui); 551 if (!map) { 552 pr_debug("unwind: no map for %lx\n", (unsigned long)addr); 553 return -1; 554 } 555 556 dso = map__dso(map); 557 558 if (!dso) { 559 map__put(map); 560 return -1; 561 } 562 563 size = dso__data_read_addr(dso, map, ui->machine, 564 addr, (u8 *) data, sizeof(*data)); 565 map__put(map); 566 return !(size == sizeof(*data)); 567 } 568 569 static int access_mem(unw_addr_space_t __maybe_unused as, 570 unw_word_t addr, unw_word_t *valp, 571 int __write, void *arg) 572 { 573 struct unwind_info *ui = arg; 574 struct stack_dump *stack = &ui->sample->user_stack; 575 u64 start, end; 576 int offset; 577 int ret; 578 579 /* Don't support write, probably not needed. */ 580 if (__write || !stack || !ui->sample->user_regs || !ui->sample->user_regs->regs) { 581 *valp = 0; 582 return 0; 583 } 584 585 ret = perf_reg_value(&start, perf_sample__user_regs(ui->sample), 586 perf_arch_reg_sp(thread__e_machine(ui->thread, 587 ui->machine, 588 /*e_flags=*/NULL))); 589 if (ret) 590 return ret; 591 592 end = start + stack->size; 593 594 /* Check overflow. */ 595 if (addr + sizeof(unw_word_t) < addr) 596 return -EINVAL; 597 598 if (addr < start || addr + sizeof(unw_word_t) >= end) { 599 ret = access_dso_mem(ui, addr, valp); 600 if (ret) { 601 pr_debug("unwind: access_mem %p not inside range" 602 " 0x%" PRIx64 "-0x%" PRIx64 "\n", 603 (void *) (uintptr_t) addr, start, end); 604 *valp = 0; 605 return ret; 606 } 607 return 0; 608 } 609 610 offset = addr - start; 611 *valp = *(unw_word_t *)&stack->data[offset]; 612 pr_debug("unwind: access_mem addr %p val %lx, offset %d\n", 613 (void *) (uintptr_t) addr, (unsigned long)*valp, offset); 614 return 0; 615 } 616 617 static int access_reg(unw_addr_space_t __maybe_unused as, 618 unw_regnum_t regnum, unw_word_t *valp, 619 int __write, void *arg) 620 { 621 struct unwind_info *ui = arg; 622 int id, ret; 623 u64 val; 624 625 /* Don't support write, I suspect we don't need it. */ 626 if (__write) { 627 pr_err("unwind: access_reg w %d\n", regnum); 628 return 0; 629 } 630 631 if (!ui->sample->user_regs || !ui->sample->user_regs->regs) { 632 *valp = 0; 633 return 0; 634 } 635 636 id = LIBUNWIND__ARCH_REG_ID(regnum); 637 if (id < 0) 638 return -EINVAL; 639 640 ret = perf_reg_value(&val, perf_sample__user_regs(ui->sample), id); 641 if (ret) { 642 if (!ui->best_effort) 643 pr_err("unwind: can't read reg %d\n", regnum); 644 return ret; 645 } 646 647 *valp = (unw_word_t) val; 648 pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp); 649 return 0; 650 } 651 652 static void put_unwind_info(unw_addr_space_t __maybe_unused as, 653 unw_proc_info_t *pi __maybe_unused, 654 void *arg __maybe_unused) 655 { 656 pr_debug("unwind: put_unwind_info called\n"); 657 } 658 659 static int entry(u64 ip, struct thread *thread, 660 unwind_entry_cb_t cb, void *arg) 661 { 662 struct unwind_entry e; 663 struct addr_location al; 664 int ret; 665 666 addr_location__init(&al); 667 e.ms.sym = thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al); 668 e.ip = ip; 669 e.ms.map = al.map; 670 e.ms.thread = thread__get(al.thread); 671 672 pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n", 673 al.sym ? al.sym->name : "''", 674 ip, 675 al.map ? map__map_ip(al.map, ip) : (u64) 0); 676 677 ret = cb(&e, arg); 678 addr_location__exit(&al); 679 return ret; 680 } 681 682 static void display_error(int err) 683 { 684 switch (err) { 685 case UNW_EINVAL: 686 pr_err("unwind: Only supports local.\n"); 687 break; 688 case UNW_EUNSPEC: 689 pr_err("unwind: Unspecified error.\n"); 690 break; 691 case UNW_EBADREG: 692 pr_err("unwind: Register unavailable.\n"); 693 break; 694 default: 695 break; 696 } 697 } 698 699 static unw_accessors_t accessors = { 700 .find_proc_info = find_proc_info, 701 .put_unwind_info = put_unwind_info, 702 .get_dyn_info_list_addr = get_dyn_info_list_addr, 703 .access_mem = access_mem, 704 .access_reg = access_reg, 705 .access_fpreg = access_fpreg, 706 .resume = resume, 707 .get_proc_name = get_proc_name, 708 }; 709 710 static int _unwind__prepare_access(struct maps *maps) 711 { 712 void *addr_space = unw_create_addr_space(&accessors, 0); 713 714 maps__set_addr_space(maps, addr_space); 715 if (!addr_space) { 716 pr_err("unwind: Can't create unwind address space.\n"); 717 return -ENOMEM; 718 } 719 720 unw_set_caching_policy(addr_space, UNW_CACHE_GLOBAL); 721 return 0; 722 } 723 724 static void _unwind__flush_access(struct maps *maps) 725 { 726 unw_flush_cache(maps__addr_space(maps), 0, 0); 727 } 728 729 static void _unwind__finish_access(struct maps *maps) 730 { 731 unw_destroy_addr_space(maps__addr_space(maps)); 732 } 733 734 static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, 735 void *arg, int max_stack) 736 { 737 uint16_t e_machine = thread__e_machine(ui->thread, ui->machine, /*e_flags=*/NULL); 738 u64 val; 739 unw_word_t ips[max_stack]; 740 unw_addr_space_t addr_space; 741 unw_cursor_t c; 742 int ret, i = 0; 743 744 ret = perf_reg_value(&val, perf_sample__user_regs(ui->sample), 745 perf_arch_reg_ip(e_machine)); 746 if (ret) 747 return ret; 748 749 ips[i++] = (unw_word_t) val; 750 751 /* 752 * If we need more than one entry, do the DWARF 753 * unwind itself. 754 */ 755 if (max_stack - 1 > 0) { 756 WARN_ONCE(!ui->thread, "WARNING: ui->thread is NULL"); 757 addr_space = maps__addr_space(thread__maps(ui->thread)); 758 759 if (addr_space == NULL) 760 return -1; 761 762 ret = unw_init_remote(&c, addr_space, ui); 763 if (ret && !ui->best_effort) 764 display_error(ret); 765 766 while (!ret && (unw_step(&c) > 0) && i < max_stack) { 767 unw_get_reg(&c, UNW_REG_IP, &ips[i]); 768 769 /* 770 * Decrement the IP for any non-activation frames. 771 * this is required to properly find the srcline 772 * for caller frames. 773 * See also the documentation for dwfl_frame_pc(), 774 * which this code tries to replicate. 775 */ 776 if (unw_is_signal_frame(&c) <= 0) 777 --ips[i]; 778 779 ++i; 780 } 781 782 max_stack = i; 783 } 784 785 /* 786 * Display what we got based on the order setup. 787 */ 788 for (i = 0; i < max_stack && !ret; i++) { 789 int j = i; 790 791 if (callchain_param.order == ORDER_CALLER) 792 j = max_stack - i - 1; 793 ret = ips[j] ? entry(ips[j], ui->thread, cb, arg) : 0; 794 } 795 796 return ret; 797 } 798 799 static int _unwind__get_entries(unwind_entry_cb_t cb, void *arg, 800 struct thread *thread, 801 struct perf_sample *data, int max_stack, 802 bool best_effort) 803 { 804 struct unwind_info ui = { 805 .sample = data, 806 .thread = thread, 807 .machine = maps__machine(thread__maps(thread)), 808 .best_effort = best_effort 809 }; 810 811 if (!data->user_regs || !data->user_regs->regs) 812 return -EINVAL; 813 814 if (max_stack <= 0) 815 return -EINVAL; 816 817 return get_entries(&ui, cb, arg, max_stack); 818 } 819 820 static struct unwind_libunwind_ops 821 _unwind_libunwind_ops = { 822 .prepare_access = _unwind__prepare_access, 823 .flush_access = _unwind__flush_access, 824 .finish_access = _unwind__finish_access, 825 .get_entries = _unwind__get_entries, 826 }; 827 828 #ifndef REMOTE_UNWIND_LIBUNWIND 829 struct unwind_libunwind_ops * 830 local_unwind_libunwind_ops = &_unwind_libunwind_ops; 831 #endif 832