unwind.c 13 KB

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