unwind-libunwind.c 15 KB

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