backtrace.c 14 KB

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