unwind-libunwind-local.c 17 KB

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