unwind-libunwind.c 16 KB

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