backtrace.c 14 KB

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