unwind-libunwind.c 13 KB

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