unwind-libunwind-local.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. #include <elf.h>
  19. #include <errno.h>
  20. #include <gelf.h>
  21. #include <fcntl.h>
  22. #include <inttypes.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/mman.h>
  26. #include <linux/list.h>
  27. #include <linux/zalloc.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. extern int
  43. UNW_OBJ(dwarf_search_unwind_table) (unw_addr_space_t as,
  44. unw_word_t ip,
  45. unw_dyn_info_t *di,
  46. unw_proc_info_t *pi,
  47. int need_unwind_info, void *arg);
  48. #define dwarf_search_unwind_table UNW_OBJ(dwarf_search_unwind_table)
  49. extern int
  50. UNW_OBJ(dwarf_find_debug_frame) (int found, unw_dyn_info_t *di_debug,
  51. unw_word_t ip,
  52. unw_word_t segbase,
  53. const char *obj_name, unw_word_t start,
  54. unw_word_t end);
  55. #define dwarf_find_debug_frame UNW_OBJ(dwarf_find_debug_frame)
  56. #define DW_EH_PE_FORMAT_MASK 0x0f /* format of the encoded value */
  57. #define DW_EH_PE_APPL_MASK 0x70 /* how the value is to be applied */
  58. /* Pointer-encoding formats: */
  59. #define DW_EH_PE_omit 0xff
  60. #define DW_EH_PE_ptr 0x00 /* pointer-sized unsigned value */
  61. #define DW_EH_PE_udata4 0x03 /* unsigned 32-bit value */
  62. #define DW_EH_PE_udata8 0x04 /* unsigned 64-bit value */
  63. #define DW_EH_PE_sdata4 0x0b /* signed 32-bit value */
  64. #define DW_EH_PE_sdata8 0x0c /* signed 64-bit value */
  65. /* Pointer-encoding application: */
  66. #define DW_EH_PE_absptr 0x00 /* absolute value */
  67. #define DW_EH_PE_pcrel 0x10 /* rel. to addr. of encoded value */
  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. /* 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
  83. */
  84. struct unwind_info {
  85. struct perf_sample *sample;
  86. struct machine *machine;
  87. struct thread *thread;
  88. };
  89. #define dw_read(ptr, type, end) ({ \
  90. type *__p = (type *) ptr; \
  91. type __v; \
  92. if ((__p + 1) > (type *) end) \
  93. return -EINVAL; \
  94. __v = *__p++; \
  95. ptr = (typeof(ptr)) __p; \
  96. __v; \
  97. })
  98. static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val,
  99. u8 encoding)
  100. {
  101. u8 *cur = *p;
  102. *val = 0;
  103. switch (encoding) {
  104. case DW_EH_PE_omit:
  105. *val = 0;
  106. goto out;
  107. case DW_EH_PE_ptr:
  108. *val = dw_read(cur, unsigned long, end);
  109. goto out;
  110. default:
  111. break;
  112. }
  113. switch (encoding & DW_EH_PE_APPL_MASK) {
  114. case DW_EH_PE_absptr:
  115. break;
  116. case DW_EH_PE_pcrel:
  117. *val = (unsigned long) cur;
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. if ((encoding & 0x07) == 0x00)
  123. encoding |= DW_EH_PE_udata4;
  124. switch (encoding & DW_EH_PE_FORMAT_MASK) {
  125. case DW_EH_PE_sdata4:
  126. *val += dw_read(cur, s32, end);
  127. break;
  128. case DW_EH_PE_udata4:
  129. *val += dw_read(cur, u32, end);
  130. break;
  131. case DW_EH_PE_sdata8:
  132. *val += dw_read(cur, s64, end);
  133. break;
  134. case DW_EH_PE_udata8:
  135. *val += dw_read(cur, u64, end);
  136. break;
  137. default:
  138. return -EINVAL;
  139. }
  140. out:
  141. *p = cur;
  142. return 0;
  143. }
  144. #define dw_read_encoded_value(ptr, end, enc) ({ \
  145. u64 __v; \
  146. if (__dw_read_encoded_value(&ptr, end, &__v, enc)) { \
  147. return -EINVAL; \
  148. } \
  149. __v; \
  150. })
  151. static u64 elf_section_offset(int fd, const char *name)
  152. {
  153. Elf *elf;
  154. GElf_Ehdr ehdr;
  155. GElf_Shdr shdr;
  156. u64 offset = 0;
  157. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  158. if (elf == NULL)
  159. return 0;
  160. do {
  161. if (gelf_getehdr(elf, &ehdr) == NULL)
  162. break;
  163. if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL))
  164. break;
  165. offset = shdr.sh_offset;
  166. } while (0);
  167. elf_end(elf);
  168. return offset;
  169. }
  170. #ifndef NO_LIBUNWIND_DEBUG_FRAME
  171. static int elf_is_exec(int fd, const char *name)
  172. {
  173. Elf *elf;
  174. GElf_Ehdr ehdr;
  175. int retval = 0;
  176. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  177. if (elf == NULL)
  178. return 0;
  179. if (gelf_getehdr(elf, &ehdr) == NULL)
  180. goto out;
  181. retval = (ehdr.e_type == ET_EXEC);
  182. out:
  183. elf_end(elf);
  184. pr_debug("unwind: elf_is_exec(%s): %d\n", name, retval);
  185. return retval;
  186. }
  187. #endif
  188. struct table_entry {
  189. u32 start_ip_offset;
  190. u32 fde_offset;
  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. * The rest of the header is variable-length and consists of the
  199. * following members:
  200. *
  201. * encoded_t eh_frame_ptr;
  202. * encoded_t fde_count;
  203. */
  204. /* A single encoded pointer should not be more than 8 bytes. */
  205. u64 enc[2];
  206. /*
  207. * struct {
  208. * encoded_t start_ip;
  209. * encoded_t fde_addr;
  210. * } binary_search_table[fde_count];
  211. */
  212. char data[];
  213. } __packed;
  214. static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
  215. u64 offset, u64 *table_data, u64 *segbase,
  216. u64 *fde_count)
  217. {
  218. struct eh_frame_hdr hdr;
  219. u8 *enc = (u8 *) &hdr.enc;
  220. u8 *end = (u8 *) &hdr.data;
  221. ssize_t r;
  222. r = dso__data_read_offset(dso, machine, offset,
  223. (u8 *) &hdr, sizeof(hdr));
  224. if (r != sizeof(hdr))
  225. return -EINVAL;
  226. /* We dont need eh_frame_ptr, just skip it. */
  227. dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc);
  228. *fde_count = dw_read_encoded_value(enc, end, hdr.fde_count_enc);
  229. *segbase = offset;
  230. *table_data = (enc - (u8 *) &hdr) + offset;
  231. return 0;
  232. }
  233. static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
  234. u64 *table_data, u64 *segbase,
  235. u64 *fde_count)
  236. {
  237. int ret = -EINVAL, fd;
  238. u64 offset = dso->data.eh_frame_hdr_offset;
  239. if (offset == 0) {
  240. fd = dso__data_get_fd(dso, machine);
  241. if (fd < 0)
  242. return -EINVAL;
  243. /* Check the .eh_frame section for unwinding info */
  244. offset = elf_section_offset(fd, ".eh_frame_hdr");
  245. dso->data.eh_frame_hdr_offset = offset;
  246. dso__data_put_fd(dso);
  247. }
  248. if (offset)
  249. ret = unwind_spec_ehframe(dso, machine, offset,
  250. table_data, segbase,
  251. fde_count);
  252. return ret;
  253. }
  254. #ifndef NO_LIBUNWIND_DEBUG_FRAME
  255. static int read_unwind_spec_debug_frame(struct dso *dso,
  256. struct machine *machine, u64 *offset)
  257. {
  258. int fd;
  259. u64 ofs = dso->data.debug_frame_offset;
  260. /* debug_frame can reside in:
  261. * - dso
  262. * - debug pointed by symsrc_filename
  263. * - gnu_debuglink, which doesn't necessary
  264. * has to be pointed by symsrc_filename
  265. */
  266. if (ofs == 0) {
  267. fd = dso__data_get_fd(dso, machine);
  268. if (fd >= 0) {
  269. ofs = elf_section_offset(fd, ".debug_frame");
  270. dso__data_put_fd(dso);
  271. }
  272. if (ofs <= 0) {
  273. fd = open(dso->symsrc_filename, O_RDONLY);
  274. if (fd >= 0) {
  275. ofs = elf_section_offset(fd, ".debug_frame");
  276. close(fd);
  277. }
  278. }
  279. if (ofs <= 0) {
  280. char *debuglink = malloc(PATH_MAX);
  281. int ret = 0;
  282. ret = dso__read_binary_type_filename(
  283. dso, DSO_BINARY_TYPE__DEBUGLINK,
  284. machine->root_dir, debuglink, PATH_MAX);
  285. if (!ret) {
  286. fd = open(debuglink, O_RDONLY);
  287. if (fd >= 0) {
  288. ofs = elf_section_offset(fd,
  289. ".debug_frame");
  290. close(fd);
  291. }
  292. }
  293. if (ofs > 0) {
  294. if (dso->symsrc_filename != NULL) {
  295. pr_warning(
  296. "%s: overwrite symsrc(%s,%s)\n",
  297. __func__,
  298. dso->symsrc_filename,
  299. debuglink);
  300. zfree(&dso->symsrc_filename);
  301. }
  302. dso->symsrc_filename = debuglink;
  303. } else {
  304. free(debuglink);
  305. }
  306. }
  307. dso->data.debug_frame_offset = ofs;
  308. }
  309. *offset = ofs;
  310. if (*offset)
  311. return 0;
  312. return -EINVAL;
  313. }
  314. #endif
  315. static struct map *find_map(unw_word_t ip, struct unwind_info *ui)
  316. {
  317. struct addr_location al;
  318. return thread__find_map(ui->thread, PERF_RECORD_MISC_USER, ip, &al);
  319. }
  320. static int
  321. find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
  322. int need_unwind_info, void *arg)
  323. {
  324. struct unwind_info *ui = arg;
  325. struct map *map;
  326. unw_dyn_info_t di;
  327. u64 table_data, segbase, fde_count;
  328. int ret = -EINVAL;
  329. map = find_map(ip, ui);
  330. if (!map || !map->dso)
  331. return -EINVAL;
  332. pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
  333. /* Check the .eh_frame section for unwinding info */
  334. if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
  335. &table_data, &segbase, &fde_count)) {
  336. memset(&di, 0, sizeof(di));
  337. di.format = UNW_INFO_FORMAT_REMOTE_TABLE;
  338. di.start_ip = map->start;
  339. di.end_ip = map->end;
  340. di.u.rti.segbase = map->start + segbase - map->pgoff;
  341. di.u.rti.table_data = map->start + table_data - map->pgoff;
  342. di.u.rti.table_len = fde_count * sizeof(struct table_entry)
  343. / sizeof(unw_word_t);
  344. ret = dwarf_search_unwind_table(as, ip, &di, pi,
  345. need_unwind_info, arg);
  346. }
  347. #ifndef NO_LIBUNWIND_DEBUG_FRAME
  348. /* Check the .debug_frame section for unwinding info */
  349. if (ret < 0 &&
  350. !read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
  351. int fd = dso__data_get_fd(map->dso, ui->machine);
  352. int is_exec = elf_is_exec(fd, map->dso->name);
  353. unw_word_t base = is_exec ? 0 : map->start;
  354. const char *symfile;
  355. if (fd >= 0)
  356. dso__data_put_fd(map->dso);
  357. symfile = map->dso->symsrc_filename ?: map->dso->name;
  358. memset(&di, 0, sizeof(di));
  359. if (dwarf_find_debug_frame(0, &di, ip, base, symfile,
  360. map->start, map->end))
  361. return dwarf_search_unwind_table(as, ip, &di, pi,
  362. need_unwind_info, arg);
  363. }
  364. #endif
  365. return ret;
  366. }
  367. static int access_fpreg(unw_addr_space_t __maybe_unused as,
  368. unw_regnum_t __maybe_unused num,
  369. unw_fpreg_t __maybe_unused *val,
  370. int __maybe_unused __write,
  371. void __maybe_unused *arg)
  372. {
  373. pr_err("unwind: access_fpreg unsupported\n");
  374. return -UNW_EINVAL;
  375. }
  376. static int get_dyn_info_list_addr(unw_addr_space_t __maybe_unused as,
  377. unw_word_t __maybe_unused *dil_addr,
  378. void __maybe_unused *arg)
  379. {
  380. return -UNW_ENOINFO;
  381. }
  382. static int resume(unw_addr_space_t __maybe_unused as,
  383. unw_cursor_t __maybe_unused *cu,
  384. void __maybe_unused *arg)
  385. {
  386. pr_err("unwind: resume unsupported\n");
  387. return -UNW_EINVAL;
  388. }
  389. static int
  390. get_proc_name(unw_addr_space_t __maybe_unused as,
  391. unw_word_t __maybe_unused addr,
  392. char __maybe_unused *bufp, size_t __maybe_unused buf_len,
  393. unw_word_t __maybe_unused *offp, void __maybe_unused *arg)
  394. {
  395. pr_err("unwind: get_proc_name unsupported\n");
  396. return -UNW_EINVAL;
  397. }
  398. static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
  399. unw_word_t *data)
  400. {
  401. struct map *map;
  402. ssize_t size;
  403. map = find_map(addr, ui);
  404. if (!map) {
  405. pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
  406. return -1;
  407. }
  408. if (!map->dso)
  409. return -1;
  410. size = dso__data_read_addr(map->dso, map, ui->machine,
  411. addr, (u8 *) data, sizeof(*data));
  412. return !(size == sizeof(*data));
  413. }
  414. static int access_mem(unw_addr_space_t __maybe_unused as,
  415. unw_word_t addr, unw_word_t *valp,
  416. int __write, void *arg)
  417. {
  418. struct unwind_info *ui = arg;
  419. struct stack_dump *stack = &ui->sample->user_stack;
  420. u64 start, end;
  421. int offset;
  422. int ret;
  423. /* Don't support write, probably not needed. */
  424. if (__write || !stack || !ui->sample->user_regs.regs) {
  425. *valp = 0;
  426. return 0;
  427. }
  428. ret = perf_reg_value(&start, &ui->sample->user_regs,
  429. LIBUNWIND__ARCH_REG_SP);
  430. if (ret)
  431. return ret;
  432. end = start + stack->size;
  433. /* Check overflow. */
  434. if (addr + sizeof(unw_word_t) < addr)
  435. return -EINVAL;
  436. if (addr < start || addr + sizeof(unw_word_t) >= end) {
  437. ret = access_dso_mem(ui, addr, valp);
  438. if (ret) {
  439. pr_debug("unwind: access_mem %p not inside range"
  440. " 0x%" PRIx64 "-0x%" PRIx64 "\n",
  441. (void *) (uintptr_t) addr, start, end);
  442. *valp = 0;
  443. return ret;
  444. }
  445. return 0;
  446. }
  447. offset = addr - start;
  448. *valp = *(unw_word_t *)&stack->data[offset];
  449. pr_debug("unwind: access_mem addr %p val %lx, offset %d\n",
  450. (void *) (uintptr_t) addr, (unsigned long)*valp, offset);
  451. return 0;
  452. }
  453. static int access_reg(unw_addr_space_t __maybe_unused as,
  454. unw_regnum_t regnum, unw_word_t *valp,
  455. int __write, void *arg)
  456. {
  457. struct unwind_info *ui = arg;
  458. int id, ret;
  459. u64 val;
  460. /* Don't support write, I suspect we don't need it. */
  461. if (__write) {
  462. pr_err("unwind: access_reg w %d\n", regnum);
  463. return 0;
  464. }
  465. if (!ui->sample->user_regs.regs) {
  466. *valp = 0;
  467. return 0;
  468. }
  469. id = LIBUNWIND__ARCH_REG_ID(regnum);
  470. if (id < 0)
  471. return -EINVAL;
  472. ret = perf_reg_value(&val, &ui->sample->user_regs, id);
  473. if (ret) {
  474. pr_err("unwind: can't read reg %d\n", regnum);
  475. return ret;
  476. }
  477. *valp = (unw_word_t) val;
  478. pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp);
  479. return 0;
  480. }
  481. static void put_unwind_info(unw_addr_space_t __maybe_unused as,
  482. unw_proc_info_t *pi __maybe_unused,
  483. void *arg __maybe_unused)
  484. {
  485. pr_debug("unwind: put_unwind_info called\n");
  486. }
  487. static int entry(u64 ip, struct thread *thread,
  488. unwind_entry_cb_t cb, void *arg)
  489. {
  490. struct unwind_entry e;
  491. struct addr_location al;
  492. e.ms.sym = thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
  493. e.ip = ip;
  494. e.ms.map = al.map;
  495. e.ms.maps = al.maps;
  496. pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
  497. al.sym ? al.sym->name : "''",
  498. ip,
  499. al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
  500. return cb(&e, arg);
  501. }
  502. static void display_error(int err)
  503. {
  504. switch (err) {
  505. case UNW_EINVAL:
  506. pr_err("unwind: Only supports local.\n");
  507. break;
  508. case UNW_EUNSPEC:
  509. pr_err("unwind: Unspecified error.\n");
  510. break;
  511. case UNW_EBADREG:
  512. pr_err("unwind: Register unavailable.\n");
  513. break;
  514. default:
  515. break;
  516. }
  517. }
  518. static unw_accessors_t accessors = {
  519. .find_proc_info = find_proc_info,
  520. .put_unwind_info = put_unwind_info,
  521. .get_dyn_info_list_addr = get_dyn_info_list_addr,
  522. .access_mem = access_mem,
  523. .access_reg = access_reg,
  524. .access_fpreg = access_fpreg,
  525. .resume = resume,
  526. .get_proc_name = get_proc_name,
  527. };
  528. static int _unwind__prepare_access(struct maps *maps)
  529. {
  530. maps->addr_space = unw_create_addr_space(&accessors, 0);
  531. if (!maps->addr_space) {
  532. pr_err("unwind: Can't create unwind address space.\n");
  533. return -ENOMEM;
  534. }
  535. unw_set_caching_policy(maps->addr_space, UNW_CACHE_GLOBAL);
  536. return 0;
  537. }
  538. static void _unwind__flush_access(struct maps *maps)
  539. {
  540. unw_flush_cache(maps->addr_space, 0, 0);
  541. }
  542. static void _unwind__finish_access(struct maps *maps)
  543. {
  544. unw_destroy_addr_space(maps->addr_space);
  545. }
  546. static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
  547. void *arg, int max_stack)
  548. {
  549. u64 val;
  550. unw_word_t ips[max_stack];
  551. unw_addr_space_t addr_space;
  552. unw_cursor_t c;
  553. int ret, i = 0;
  554. ret = perf_reg_value(&val, &ui->sample->user_regs,
  555. LIBUNWIND__ARCH_REG_IP);
  556. if (ret)
  557. return ret;
  558. ips[i++] = (unw_word_t) val;
  559. /*
  560. * If we need more than one entry, do the DWARF
  561. * unwind itself.
  562. */
  563. if (max_stack - 1 > 0) {
  564. WARN_ONCE(!ui->thread, "WARNING: ui->thread is NULL");
  565. addr_space = ui->thread->maps->addr_space;
  566. if (addr_space == NULL)
  567. return -1;
  568. ret = unw_init_remote(&c, addr_space, ui);
  569. if (ret)
  570. display_error(ret);
  571. while (!ret && (unw_step(&c) > 0) && i < max_stack) {
  572. unw_get_reg(&c, UNW_REG_IP, &ips[i]);
  573. /*
  574. * Decrement the IP for any non-activation frames.
  575. * this is required to properly find the srcline
  576. * for caller frames.
  577. * See also the documentation for dwfl_frame_pc(),
  578. * which this code tries to replicate.
  579. */
  580. if (unw_is_signal_frame(&c) <= 0)
  581. --ips[i];
  582. ++i;
  583. }
  584. max_stack = i;
  585. }
  586. /*
  587. * Display what we got based on the order setup.
  588. */
  589. for (i = 0; i < max_stack && !ret; i++) {
  590. int j = i;
  591. if (callchain_param.order == ORDER_CALLER)
  592. j = max_stack - i - 1;
  593. ret = ips[j] ? entry(ips[j], ui->thread, cb, arg) : 0;
  594. }
  595. return ret;
  596. }
  597. static int _unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  598. struct thread *thread,
  599. struct perf_sample *data, int max_stack)
  600. {
  601. struct unwind_info ui = {
  602. .sample = data,
  603. .thread = thread,
  604. .machine = thread->maps->machine,
  605. };
  606. if (!data->user_regs.regs)
  607. return -EINVAL;
  608. if (max_stack <= 0)
  609. return -EINVAL;
  610. return get_entries(&ui, cb, arg, max_stack);
  611. }
  612. static struct unwind_libunwind_ops
  613. _unwind_libunwind_ops = {
  614. .prepare_access = _unwind__prepare_access,
  615. .flush_access = _unwind__flush_access,
  616. .finish_access = _unwind__finish_access,
  617. .get_entries = _unwind__get_entries,
  618. };
  619. #ifndef REMOTE_UNWIND_LIBUNWIND
  620. struct unwind_libunwind_ops *
  621. local_unwind_libunwind_ops = &_unwind_libunwind_ops;
  622. #endif