unwind-libunwind-local.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
  318. MAP__FUNCTION, ip, &al);
  319. if (!al.map) {
  320. /*
  321. * We've seen cases (softice) where DWARF unwinder went
  322. * through non executable mmaps, which we need to lookup
  323. * in MAP__VARIABLE tree.
  324. */
  325. thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
  326. MAP__VARIABLE, ip, &al);
  327. }
  328. return al.map;
  329. }
  330. static int
  331. find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
  332. int need_unwind_info, void *arg)
  333. {
  334. struct unwind_info *ui = arg;
  335. struct map *map;
  336. unw_dyn_info_t di;
  337. u64 table_data, segbase, fde_count;
  338. int ret = -EINVAL;
  339. map = find_map(ip, ui);
  340. if (!map || !map->dso)
  341. return -EINVAL;
  342. pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
  343. /* Check the .eh_frame section for unwinding info */
  344. if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
  345. &table_data, &segbase, &fde_count)) {
  346. memset(&di, 0, sizeof(di));
  347. di.format = UNW_INFO_FORMAT_REMOTE_TABLE;
  348. di.start_ip = map->start;
  349. di.end_ip = map->end;
  350. di.u.rti.segbase = map->start + segbase - map->pgoff;
  351. di.u.rti.table_data = map->start + table_data - map->pgoff;
  352. di.u.rti.table_len = fde_count * sizeof(struct table_entry)
  353. / sizeof(unw_word_t);
  354. ret = dwarf_search_unwind_table(as, ip, &di, pi,
  355. need_unwind_info, arg);
  356. }
  357. #ifndef NO_LIBUNWIND_DEBUG_FRAME
  358. /* Check the .debug_frame section for unwinding info */
  359. if (ret < 0 &&
  360. !read_unwind_spec_debug_frame(map->dso, ui->machine, &segbase)) {
  361. int fd = dso__data_get_fd(map->dso, ui->machine);
  362. int is_exec = elf_is_exec(fd, map->dso->name);
  363. unw_word_t base = is_exec ? 0 : map->start;
  364. const char *symfile;
  365. if (fd >= 0)
  366. dso__data_put_fd(map->dso);
  367. symfile = map->dso->symsrc_filename ?: map->dso->name;
  368. memset(&di, 0, sizeof(di));
  369. if (dwarf_find_debug_frame(0, &di, ip, base, symfile,
  370. map->start, map->end))
  371. return dwarf_search_unwind_table(as, ip, &di, pi,
  372. need_unwind_info, arg);
  373. }
  374. #endif
  375. return ret;
  376. }
  377. static int access_fpreg(unw_addr_space_t __maybe_unused as,
  378. unw_regnum_t __maybe_unused num,
  379. unw_fpreg_t __maybe_unused *val,
  380. int __maybe_unused __write,
  381. void __maybe_unused *arg)
  382. {
  383. pr_err("unwind: access_fpreg unsupported\n");
  384. return -UNW_EINVAL;
  385. }
  386. static int get_dyn_info_list_addr(unw_addr_space_t __maybe_unused as,
  387. unw_word_t __maybe_unused *dil_addr,
  388. void __maybe_unused *arg)
  389. {
  390. return -UNW_ENOINFO;
  391. }
  392. static int resume(unw_addr_space_t __maybe_unused as,
  393. unw_cursor_t __maybe_unused *cu,
  394. void __maybe_unused *arg)
  395. {
  396. pr_err("unwind: resume unsupported\n");
  397. return -UNW_EINVAL;
  398. }
  399. static int
  400. get_proc_name(unw_addr_space_t __maybe_unused as,
  401. unw_word_t __maybe_unused addr,
  402. char __maybe_unused *bufp, size_t __maybe_unused buf_len,
  403. unw_word_t __maybe_unused *offp, void __maybe_unused *arg)
  404. {
  405. pr_err("unwind: get_proc_name unsupported\n");
  406. return -UNW_EINVAL;
  407. }
  408. static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
  409. unw_word_t *data)
  410. {
  411. struct map *map;
  412. ssize_t size;
  413. map = find_map(addr, ui);
  414. if (!map) {
  415. pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
  416. return -1;
  417. }
  418. if (!map->dso)
  419. return -1;
  420. size = dso__data_read_addr(map->dso, map, ui->machine,
  421. addr, (u8 *) data, sizeof(*data));
  422. return !(size == sizeof(*data));
  423. }
  424. static int access_mem(unw_addr_space_t __maybe_unused as,
  425. unw_word_t addr, unw_word_t *valp,
  426. int __write, void *arg)
  427. {
  428. struct unwind_info *ui = arg;
  429. struct stack_dump *stack = &ui->sample->user_stack;
  430. u64 start, end;
  431. int offset;
  432. int ret;
  433. /* Don't support write, probably not needed. */
  434. if (__write || !stack || !ui->sample->user_regs.regs) {
  435. *valp = 0;
  436. return 0;
  437. }
  438. ret = perf_reg_value(&start, &ui->sample->user_regs,
  439. LIBUNWIND__ARCH_REG_SP);
  440. if (ret)
  441. return ret;
  442. end = start + stack->size;
  443. /* Check overflow. */
  444. if (addr + sizeof(unw_word_t) < addr)
  445. return -EINVAL;
  446. if (addr < start || addr + sizeof(unw_word_t) >= end) {
  447. ret = access_dso_mem(ui, addr, valp);
  448. if (ret) {
  449. pr_debug("unwind: access_mem %p not inside range"
  450. " 0x%" PRIx64 "-0x%" PRIx64 "\n",
  451. (void *) (uintptr_t) addr, start, end);
  452. *valp = 0;
  453. return ret;
  454. }
  455. return 0;
  456. }
  457. offset = addr - start;
  458. *valp = *(unw_word_t *)&stack->data[offset];
  459. pr_debug("unwind: access_mem addr %p val %lx, offset %d\n",
  460. (void *) (uintptr_t) addr, (unsigned long)*valp, offset);
  461. return 0;
  462. }
  463. static int access_reg(unw_addr_space_t __maybe_unused as,
  464. unw_regnum_t regnum, unw_word_t *valp,
  465. int __write, void *arg)
  466. {
  467. struct unwind_info *ui = arg;
  468. int id, ret;
  469. u64 val;
  470. /* Don't support write, I suspect we don't need it. */
  471. if (__write) {
  472. pr_err("unwind: access_reg w %d\n", regnum);
  473. return 0;
  474. }
  475. if (!ui->sample->user_regs.regs) {
  476. *valp = 0;
  477. return 0;
  478. }
  479. id = LIBUNWIND__ARCH_REG_ID(regnum);
  480. if (id < 0)
  481. return -EINVAL;
  482. ret = perf_reg_value(&val, &ui->sample->user_regs, id);
  483. if (ret) {
  484. pr_err("unwind: can't read reg %d\n", regnum);
  485. return ret;
  486. }
  487. *valp = (unw_word_t) val;
  488. pr_debug("unwind: reg %d, val %lx\n", regnum, (unsigned long)*valp);
  489. return 0;
  490. }
  491. static void put_unwind_info(unw_addr_space_t __maybe_unused as,
  492. unw_proc_info_t *pi __maybe_unused,
  493. void *arg __maybe_unused)
  494. {
  495. pr_debug("unwind: put_unwind_info called\n");
  496. }
  497. static int entry(u64 ip, struct thread *thread,
  498. unwind_entry_cb_t cb, void *arg)
  499. {
  500. struct unwind_entry e;
  501. struct addr_location al;
  502. thread__find_addr_location(thread, PERF_RECORD_MISC_USER,
  503. MAP__FUNCTION, ip, &al);
  504. e.ip = al.addr;
  505. e.map = al.map;
  506. e.sym = al.sym;
  507. pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
  508. al.sym ? al.sym->name : "''",
  509. ip,
  510. al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
  511. return cb(&e, arg);
  512. }
  513. static void display_error(int err)
  514. {
  515. switch (err) {
  516. case UNW_EINVAL:
  517. pr_err("unwind: Only supports local.\n");
  518. break;
  519. case UNW_EUNSPEC:
  520. pr_err("unwind: Unspecified error.\n");
  521. break;
  522. case UNW_EBADREG:
  523. pr_err("unwind: Register unavailable.\n");
  524. break;
  525. default:
  526. break;
  527. }
  528. }
  529. static unw_accessors_t accessors = {
  530. .find_proc_info = find_proc_info,
  531. .put_unwind_info = put_unwind_info,
  532. .get_dyn_info_list_addr = get_dyn_info_list_addr,
  533. .access_mem = access_mem,
  534. .access_reg = access_reg,
  535. .access_fpreg = access_fpreg,
  536. .resume = resume,
  537. .get_proc_name = get_proc_name,
  538. };
  539. static int _unwind__prepare_access(struct thread *thread)
  540. {
  541. if (!dwarf_callchain_users)
  542. return 0;
  543. thread->addr_space = unw_create_addr_space(&accessors, 0);
  544. if (!thread->addr_space) {
  545. pr_err("unwind: Can't create unwind address space.\n");
  546. return -ENOMEM;
  547. }
  548. unw_set_caching_policy(thread->addr_space, UNW_CACHE_GLOBAL);
  549. return 0;
  550. }
  551. static void _unwind__flush_access(struct thread *thread)
  552. {
  553. if (!dwarf_callchain_users)
  554. return;
  555. unw_flush_cache(thread->addr_space, 0, 0);
  556. }
  557. static void _unwind__finish_access(struct thread *thread)
  558. {
  559. if (!dwarf_callchain_users)
  560. return;
  561. unw_destroy_addr_space(thread->addr_space);
  562. }
  563. static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
  564. void *arg, int max_stack)
  565. {
  566. u64 val;
  567. unw_word_t ips[max_stack];
  568. unw_addr_space_t addr_space;
  569. unw_cursor_t c;
  570. int ret, i = 0;
  571. ret = perf_reg_value(&val, &ui->sample->user_regs,
  572. LIBUNWIND__ARCH_REG_IP);
  573. if (ret)
  574. return ret;
  575. ips[i++] = (unw_word_t) val;
  576. /*
  577. * If we need more than one entry, do the DWARF
  578. * unwind itself.
  579. */
  580. if (max_stack - 1 > 0) {
  581. WARN_ONCE(!ui->thread, "WARNING: ui->thread is NULL");
  582. addr_space = ui->thread->addr_space;
  583. if (addr_space == NULL)
  584. return -1;
  585. ret = unw_init_remote(&c, addr_space, ui);
  586. if (ret)
  587. display_error(ret);
  588. while (!ret && (unw_step(&c) > 0) && i < max_stack) {
  589. unw_get_reg(&c, UNW_REG_IP, &ips[i]);
  590. /*
  591. * Decrement the IP for any non-activation frames.
  592. * this is required to properly find the srcline
  593. * for caller frames.
  594. * See also the documentation for dwfl_frame_pc(),
  595. * which this code tries to replicate.
  596. */
  597. if (unw_is_signal_frame(&c) <= 0)
  598. --ips[i];
  599. ++i;
  600. }
  601. max_stack = i;
  602. }
  603. /*
  604. * Display what we got based on the order setup.
  605. */
  606. for (i = 0; i < max_stack && !ret; i++) {
  607. int j = i;
  608. if (callchain_param.order == ORDER_CALLER)
  609. j = max_stack - i - 1;
  610. ret = ips[j] ? entry(ips[j], ui->thread, cb, arg) : 0;
  611. }
  612. return ret;
  613. }
  614. static int _unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  615. struct thread *thread,
  616. struct perf_sample *data, int max_stack)
  617. {
  618. struct unwind_info ui = {
  619. .sample = data,
  620. .thread = thread,
  621. .machine = thread->mg->machine,
  622. };
  623. if (!data->user_regs.regs)
  624. return -EINVAL;
  625. if (max_stack <= 0)
  626. return -EINVAL;
  627. return get_entries(&ui, cb, arg, max_stack);
  628. }
  629. static struct unwind_libunwind_ops
  630. _unwind_libunwind_ops = {
  631. .prepare_access = _unwind__prepare_access,
  632. .flush_access = _unwind__flush_access,
  633. .finish_access = _unwind__finish_access,
  634. .get_entries = _unwind__get_entries,
  635. };
  636. #ifndef REMOTE_UNWIND_LIBUNWIND
  637. struct unwind_libunwind_ops *
  638. local_unwind_libunwind_ops = &_unwind_libunwind_ops;
  639. #endif