unwind-libunwind.c 13 KB

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