backtrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #ifndef __CHECKER__
  2. #if !defined (__ARM_EABI__)
  3. #warning Your compiler does not have EABI support.
  4. #warning ARM unwind is known to compile only with EABI compilers.
  5. #warning Change compiler or disable ARM_UNWIND option.
  6. #elif (__GNUC__ == 4 && __GNUC_MINOR__ <= 2) && !defined(__clang__)
  7. #warning Your compiler is too buggy; it is known to not compile ARM unwind support.
  8. #warning Change compiler or disable ARM_UNWIND option.
  9. #endif
  10. #endif /* __CHECKER__ */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <backtrace.h>
  14. #define DBG_TAG "BACKTRACE"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. rt_inline void arm_get_current_stackframe(struct pt_regs *regs, struct stackframe *frame)
  18. {
  19. frame->fp = frame_pointer(regs);
  20. frame->sp = regs->ARM_sp;
  21. frame->lr = regs->ARM_lr;
  22. frame->pc = regs->ARM_pc;
  23. }
  24. /* Dummy functions to avoid linker complaints */
  25. void __aeabi_unwind_cpp_pr0(void)
  26. {
  27. };
  28. void __aeabi_unwind_cpp_pr1(void)
  29. {
  30. };
  31. void __aeabi_unwind_cpp_pr2(void)
  32. {
  33. };
  34. struct unwind_ctrl_block {
  35. unsigned long vrs[16]; /* virtual register set */
  36. const unsigned long *insn; /* pointer to the current instructions word */
  37. unsigned long sp_high; /* highest value of sp allowed */
  38. /*
  39. * 1 : check for stack overflow for each register pop.
  40. * 0 : save overhead if there is plenty of stack remaining.
  41. */
  42. int check_each_pop;
  43. int entries; /* number of entries left to interpret */
  44. int byte; /* current byte number in the instructions word */
  45. };
  46. enum regs
  47. {
  48. #ifdef CONFIG_THUMB2_KERNEL
  49. FP = 7,
  50. #else
  51. FP = 11,
  52. #endif
  53. SP = 13,
  54. LR = 14,
  55. PC = 15
  56. };
  57. static int core_kernel_text(unsigned long addr)
  58. {
  59. return 1;
  60. }
  61. /* Convert a prel31 symbol to an absolute address */
  62. #define prel31_to_addr(ptr) \
  63. ({ \
  64. /* sign-extend to 32 bits */ \
  65. long offset = (((long)*(ptr)) << 1) >> 1; \
  66. (unsigned long)(ptr) + offset; \
  67. })
  68. /*
  69. * Binary search in the unwind index. The entries are
  70. * guaranteed to be sorted in ascending order by the linker.
  71. *
  72. * start = first entry
  73. * origin = first entry with positive offset (or stop if there is no such entry)
  74. * stop - 1 = last entry
  75. */
  76. static const struct unwind_idx *search_index(unsigned long addr,
  77. const struct unwind_idx *start,
  78. const struct unwind_idx *origin,
  79. const struct unwind_idx *stop)
  80. {
  81. unsigned long addr_prel31;
  82. LOG_D("%s(%08lx, %x, %x, %x)",
  83. __func__, addr, start, origin, stop);
  84. /*
  85. * only search in the section with the matching sign. This way the
  86. * prel31 numbers can be compared as unsigned longs.
  87. */
  88. if (addr < (unsigned long)start)
  89. /* negative offsets: [start; origin) */
  90. stop = origin;
  91. else
  92. /* positive offsets: [origin; stop) */
  93. start = origin;
  94. /* prel31 for address relavive to start */
  95. addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
  96. while (start < stop - 1)
  97. {
  98. const struct unwind_idx *mid = start + ((stop - start) >> 1);
  99. /*
  100. * As addr_prel31 is relative to start an offset is needed to
  101. * make it relative to mid.
  102. */
  103. if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
  104. mid->addr_offset)
  105. stop = mid;
  106. else
  107. {
  108. /* keep addr_prel31 relative to start */
  109. addr_prel31 -= ((unsigned long)mid -
  110. (unsigned long)start);
  111. start = mid;
  112. }
  113. }
  114. if (start->addr_offset <= addr_prel31)
  115. return start;
  116. else
  117. {
  118. LOG_W("unwind: Unknown symbol address %08lx", addr);
  119. return RT_NULL;
  120. }
  121. }
  122. static const struct unwind_idx *unwind_find_origin(
  123. const struct unwind_idx *start, const struct unwind_idx *stop)
  124. {
  125. LOG_D("%s(%x, %x)", __func__, start, stop);
  126. while (start < stop)
  127. {
  128. const struct unwind_idx *mid = start + ((stop - start) >> 1);
  129. if (mid->addr_offset >= 0x40000000)
  130. /* negative offset */
  131. start = mid + 1;
  132. else
  133. /* positive offset */
  134. stop = mid;
  135. }
  136. LOG_D("%s -> %x", __func__, stop);
  137. return stop;
  138. }
  139. static const struct unwind_idx *unwind_find_idx(unsigned long addr, const struct unwind_idx **origin_idx, const struct unwind_idx exidx_start[], const struct unwind_idx exidx_end[])
  140. {
  141. const struct unwind_idx *idx = RT_NULL;
  142. LOG_D("%s(%08lx)", __func__, addr);
  143. if (core_kernel_text(addr))
  144. {
  145. if (!*origin_idx)
  146. *origin_idx =
  147. unwind_find_origin(exidx_start,
  148. exidx_end);
  149. /* main unwind table */
  150. idx = search_index(addr, exidx_start,
  151. *origin_idx,
  152. exidx_end);
  153. }
  154. LOG_D("%s: idx = %x", __func__, idx);
  155. return idx;
  156. }
  157. static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
  158. {
  159. unsigned long ret;
  160. if (ctrl->entries <= 0)
  161. {
  162. LOG_W("unwind: Corrupt unwind table");
  163. return 0;
  164. }
  165. ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
  166. if (ctrl->byte == 0)
  167. {
  168. ctrl->insn++;
  169. ctrl->entries--;
  170. ctrl->byte = 3;
  171. }
  172. else
  173. ctrl->byte--;
  174. return ret;
  175. }
  176. /* Before poping a register check whether it is feasible or not */
  177. static int unwind_pop_register(struct unwind_ctrl_block *ctrl,
  178. unsigned long **vsp, unsigned int reg)
  179. {
  180. if (ctrl->check_each_pop)
  181. if (*vsp >= (unsigned long *)ctrl->sp_high)
  182. return -URC_FAILURE;
  183. ctrl->vrs[reg] = *(*vsp)++;
  184. return URC_OK;
  185. }
  186. /* Helper functions to execute the instructions */
  187. static int unwind_exec_pop_subset_r4_to_r13(struct unwind_ctrl_block *ctrl,
  188. unsigned long mask)
  189. {
  190. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  191. int load_sp, reg = 4;
  192. load_sp = mask & (1 << (13 - 4));
  193. while (mask)
  194. {
  195. if (mask & 1)
  196. if (unwind_pop_register(ctrl, &vsp, reg))
  197. return -URC_FAILURE;
  198. mask >>= 1;
  199. reg++;
  200. }
  201. if (!load_sp)
  202. ctrl->vrs[SP] = (unsigned long)vsp;
  203. return URC_OK;
  204. }
  205. static int unwind_exec_pop_r4_to_rN(struct unwind_ctrl_block *ctrl,
  206. unsigned long insn)
  207. {
  208. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  209. int reg;
  210. /* pop R4-R[4+bbb] */
  211. for (reg = 4; reg <= 4 + (insn & 7); reg++)
  212. if (unwind_pop_register(ctrl, &vsp, reg))
  213. return -URC_FAILURE;
  214. if (insn & 0x8)
  215. if (unwind_pop_register(ctrl, &vsp, 14))
  216. return -URC_FAILURE;
  217. ctrl->vrs[SP] = (unsigned long)vsp;
  218. return URC_OK;
  219. }
  220. static int unwind_exec_pop_subset_r0_to_r3(struct unwind_ctrl_block *ctrl,
  221. unsigned long mask)
  222. {
  223. unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
  224. int reg = 0;
  225. /* pop R0-R3 according to mask */
  226. while (mask)
  227. {
  228. if (mask & 1)
  229. if (unwind_pop_register(ctrl, &vsp, reg))
  230. return -URC_FAILURE;
  231. mask >>= 1;
  232. reg++;
  233. }
  234. ctrl->vrs[SP] = (unsigned long)vsp;
  235. return URC_OK;
  236. }
  237. /*
  238. * Execute the current unwind instruction.
  239. */
  240. static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
  241. {
  242. unsigned long insn = unwind_get_byte(ctrl);
  243. int ret = URC_OK;
  244. LOG_D("%s: insn = %08lx", __func__, insn);
  245. if ((insn & 0xc0) == 0x00)
  246. ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
  247. else if ((insn & 0xc0) == 0x40)
  248. ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
  249. else if ((insn & 0xf0) == 0x80)
  250. {
  251. unsigned long mask;
  252. insn = (insn << 8) | unwind_get_byte(ctrl);
  253. mask = insn & 0x0fff;
  254. if (mask == 0)
  255. {
  256. LOG_W("unwind: 'Refuse to unwind' instruction %04lx",
  257. insn);
  258. return -URC_FAILURE;
  259. }
  260. ret = unwind_exec_pop_subset_r4_to_r13(ctrl, mask);
  261. if (ret)
  262. goto error;
  263. }
  264. else if ((insn & 0xf0) == 0x90 &&
  265. (insn & 0x0d) != 0x0d)
  266. ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
  267. else if ((insn & 0xf0) == 0xa0)
  268. {
  269. ret = unwind_exec_pop_r4_to_rN(ctrl, insn);
  270. if (ret)
  271. goto error;
  272. }
  273. else if (insn == 0xb0)
  274. {
  275. if (ctrl->vrs[PC] == 0)
  276. ctrl->vrs[PC] = ctrl->vrs[LR];
  277. /* no further processing */
  278. ctrl->entries = 0;
  279. }
  280. else if (insn == 0xb1)
  281. {
  282. unsigned long mask = unwind_get_byte(ctrl);
  283. if (mask == 0 || mask & 0xf0)
  284. {
  285. LOG_W("unwind: Spare encoding %04lx",
  286. (insn << 8) | mask);
  287. return -URC_FAILURE;
  288. }
  289. ret = unwind_exec_pop_subset_r0_to_r3(ctrl, mask);
  290. if (ret)
  291. goto error;
  292. }
  293. else if (insn == 0xb2)
  294. {
  295. unsigned long uleb128 = unwind_get_byte(ctrl);
  296. ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
  297. }
  298. else
  299. {
  300. LOG_W("unwind: Unhandled instruction %02lx", insn);
  301. return -URC_FAILURE;
  302. }
  303. LOG_D("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx", __func__,
  304. ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
  305. error:
  306. return ret;
  307. }
  308. #ifdef RT_BACKTRACE_FUNCTION_NAME
  309. static char *unwind_get_function_name(void *address)
  310. {
  311. uint32_t flag_word = *(uint32_t *)((char*)address - 4);
  312. if ((flag_word & 0xff000000) == 0xff000000)
  313. {
  314. return (char *)((char*)address - 4 - (flag_word & 0x00ffffff));
  315. }
  316. return RT_NULL;
  317. }
  318. #endif
  319. /*
  320. * Unwind a single frame starting with *sp for the symbol at *pc. It
  321. * updates the *pc and *sp with the new values.
  322. */
  323. int unwind_frame(struct stackframe *frame, const struct unwind_idx **origin_idx, const struct unwind_idx exidx_start[], const struct unwind_idx exidx_end[])
  324. {
  325. unsigned long low;
  326. const struct unwind_idx *idx;
  327. struct unwind_ctrl_block ctrl;
  328. struct rt_thread *rt_c_thread;
  329. /* store the highest address on the stack to avoid crossing it*/
  330. low = frame->sp;
  331. rt_c_thread = rt_thread_self();
  332. ctrl.sp_high = (unsigned long)((char*)rt_c_thread->stack_addr + rt_c_thread->stack_size);
  333. LOG_D("%s(pc = %08lx lr = %08lx sp = %08lx)", __func__,
  334. frame->pc, frame->lr, frame->sp);
  335. idx = unwind_find_idx(frame->pc, origin_idx, exidx_start, exidx_end);
  336. if (!idx)
  337. {
  338. LOG_W("unwind: Index not found %08lx", frame->pc);
  339. return -URC_FAILURE;
  340. }
  341. #ifdef RT_BACKTRACE_FUNCTION_NAME
  342. {
  343. char *fun_name;
  344. fun_name = unwind_get_function_name((void *)prel31_to_addr(&idx->addr_offset));
  345. if (fun_name)
  346. {
  347. rt_kprintf("0x%08x @ %s\n", frame->pc, fun_name);
  348. }
  349. }
  350. #endif
  351. ctrl.vrs[FP] = frame->fp;
  352. ctrl.vrs[SP] = frame->sp;
  353. ctrl.vrs[LR] = frame->lr;
  354. ctrl.vrs[PC] = 0;
  355. if (idx->insn == 1)
  356. /* can't unwind */
  357. return -URC_FAILURE;
  358. else if ((idx->insn & 0x80000000) == 0)
  359. /* prel31 to the unwind table */
  360. ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
  361. else if ((idx->insn & 0xff000000) == 0x80000000)
  362. /* only personality routine 0 supported in the index */
  363. ctrl.insn = &idx->insn;
  364. else
  365. {
  366. LOG_W("unwind: Unsupported personality routine %08lx in the index at %x",
  367. idx->insn, idx);
  368. return -URC_FAILURE;
  369. }
  370. /* check the personality routine */
  371. if ((*ctrl.insn & 0xff000000) == 0x80000000)
  372. {
  373. ctrl.byte = 2;
  374. ctrl.entries = 1;
  375. }
  376. else if ((*ctrl.insn & 0xff000000) == 0x81000000)
  377. {
  378. ctrl.byte = 1;
  379. ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
  380. }
  381. else
  382. {
  383. LOG_W("unwind: Unsupported personality routine %08lx at %x",
  384. *ctrl.insn, ctrl.insn);
  385. return -URC_FAILURE;
  386. }
  387. ctrl.check_each_pop = 0;
  388. while (ctrl.entries > 0)
  389. {
  390. int urc;
  391. if ((ctrl.sp_high - ctrl.vrs[SP]) < sizeof(ctrl.vrs))
  392. ctrl.check_each_pop = 1;
  393. urc = unwind_exec_insn(&ctrl);
  394. if (urc < 0)
  395. return urc;
  396. if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= ctrl.sp_high)
  397. return -URC_FAILURE;
  398. }
  399. if (ctrl.vrs[PC] == 0)
  400. ctrl.vrs[PC] = ctrl.vrs[LR];
  401. /* check for infinite loop */
  402. if (frame->pc == ctrl.vrs[PC])
  403. return -URC_FAILURE;
  404. frame->fp = ctrl.vrs[FP];
  405. frame->sp = ctrl.vrs[SP];
  406. frame->lr = ctrl.vrs[LR];
  407. frame->pc = ctrl.vrs[PC];
  408. return URC_OK;
  409. }
  410. void unwind_backtrace(struct pt_regs *regs, const struct unwind_idx exidx_start[], const struct unwind_idx exidx_end[])
  411. {
  412. struct stackframe frame;
  413. const struct unwind_idx *origin_idx = RT_NULL;
  414. LOG_D("%s(regs = %x)", __func__, regs);
  415. arm_get_current_stackframe(regs, &frame);
  416. #ifndef RT_BACKTRACE_FUNCTION_NAME
  417. rt_kprintf("please use: addr2line -e rtthread.elf -a -f %08x", frame.pc);
  418. #endif
  419. LOG_D("pc = %08x, sp = %08x", frame.pc, frame.sp);
  420. while (1)
  421. {
  422. int urc;
  423. urc = unwind_frame(&frame, &origin_idx, exidx_start, exidx_end);
  424. if (urc < 0)
  425. break;
  426. //dump_backtrace_entry(where, frame.pc, frame.sp - 4);
  427. #ifndef RT_BACKTRACE_FUNCTION_NAME
  428. rt_kprintf(" %08x", frame.pc);
  429. #endif
  430. LOG_D("from: pc = %08x, frame = %08x", frame.pc, frame.sp - 4);
  431. }
  432. rt_kprintf("\n");
  433. }
  434. extern const struct unwind_idx __exidx_start[];
  435. extern const struct unwind_idx __exidx_end[];
  436. void rt_unwind(struct rt_hw_exp_stack *regs, unsigned int pc_adj)
  437. {
  438. struct pt_regs e_regs;
  439. e_regs.ARM_fp = regs->fp;
  440. e_regs.ARM_sp = regs->sp;
  441. e_regs.ARM_lr = regs->lr;
  442. e_regs.ARM_pc = regs->pc - pc_adj;
  443. rt_kprintf("backtrace:\n");
  444. unwind_backtrace(&e_regs, __exidx_start, __exidx_end);
  445. }
  446. void rt_backtrace(void)
  447. {
  448. struct rt_hw_exp_stack regs;
  449. asm volatile ("mov %0, fp":"=r"(regs.fp));
  450. asm volatile ("mov %0, sp":"=r"(regs.sp));
  451. asm volatile ("mov %0, lr":"=r"(regs.lr));
  452. asm volatile ("mov %0, pc":"=r"(regs.pc));
  453. rt_unwind(&regs, 8);
  454. }