cm_backtrace.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * This file is part of the CmBacktrace Library.
  3. *
  4. * Copyright (c) 2016-2019, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Initialize function and other general function.
  26. * Created on: 2016-12-15
  27. */
  28. #include <cm_backtrace.h>
  29. #include <stdbool.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #ifdef __RAMRUN__
  33. void cm_backtrace_init(const char *firmware_name, const char *hardware_ver, const char *software_ver) {}
  34. void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp){}
  35. #else
  36. #if __STDC_VERSION__ < 199901L
  37. #error "must be C99 or higher. try to add '-std=c99' to compile parameters"
  38. #endif
  39. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  40. #define SECTION_START(_name_) _name_##$$Base
  41. #define SECTION_END(_name_) _name_##$$Limit
  42. #define IMAGE_SECTION_START(_name_) Image$$##_name_##$$Base
  43. #define IMAGE_SECTION_END(_name_) Image$$##_name_##$$Limit
  44. #define CSTACK_BLOCK_START(_name_) SECTION_START(_name_)
  45. #define CSTACK_BLOCK_END(_name_) SECTION_END(_name_)
  46. #define CODE_SECTION_START(_name_) IMAGE_SECTION_START(_name_)
  47. #define CODE_SECTION_END(_name_) IMAGE_SECTION_END(_name_)
  48. extern const int CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  49. extern const int CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME);
  50. extern const int CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  51. extern const int CODE_SECTION_END(CMB_CODE_SECTION_NAME);
  52. #elif defined(__ICCARM__)
  53. #pragma section=CMB_CSTACK_BLOCK_NAME
  54. #pragma section=CMB_CODE_SECTION_NAME
  55. #elif defined(__GNUC__)
  56. extern const int CMB_CSTACK_BLOCK_START;
  57. extern const int CMB_CSTACK_BLOCK_END;
  58. extern const int CMB_CODE_SECTION_START;
  59. extern const int CMB_CODE_SECTION_END;
  60. #else
  61. #error "not supported compiler"
  62. #endif
  63. enum {
  64. PRINT_MAIN_STACK_CFG_ERROR,
  65. PRINT_FIRMWARE_INFO,
  66. PRINT_ASSERT_ON_THREAD,
  67. PRINT_ASSERT_ON_HANDLER,
  68. PRINT_THREAD_STACK_INFO,
  69. PRINT_MAIN_STACK_INFO,
  70. PRINT_THREAD_STACK_OVERFLOW,
  71. PRINT_MAIN_STACK_OVERFLOW,
  72. PRINT_CALL_STACK_INFO,
  73. PRINT_CALL_STACK_ERR,
  74. PRINT_FAULT_ON_THREAD,
  75. PRINT_FAULT_ON_HANDLER,
  76. PRINT_REGS_TITLE,
  77. PRINT_HFSR_VECTBL,
  78. PRINT_MFSR_IACCVIOL,
  79. PRINT_MFSR_DACCVIOL,
  80. PRINT_MFSR_MUNSTKERR,
  81. PRINT_MFSR_MSTKERR,
  82. PRINT_MFSR_MLSPERR,
  83. PRINT_BFSR_IBUSERR,
  84. PRINT_BFSR_PRECISERR,
  85. PRINT_BFSR_IMPREISERR,
  86. PRINT_BFSR_UNSTKERR,
  87. PRINT_BFSR_STKERR,
  88. PRINT_BFSR_LSPERR,
  89. PRINT_UFSR_UNDEFINSTR,
  90. PRINT_UFSR_INVSTATE,
  91. PRINT_UFSR_INVPC,
  92. PRINT_UFSR_NOCP,
  93. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  94. PRINT_UFSR_STKOF,
  95. #endif
  96. PRINT_UFSR_UNALIGNED,
  97. PRINT_UFSR_DIVBYZERO0,
  98. PRINT_DFSR_HALTED,
  99. PRINT_DFSR_BKPT,
  100. PRINT_DFSR_DWTTRAP,
  101. PRINT_DFSR_VCATCH,
  102. PRINT_DFSR_EXTERNAL,
  103. PRINT_MMAR,
  104. PRINT_BFAR,
  105. };
  106. static const char * const print_info[] = {
  107. [PRINT_MAIN_STACK_CFG_ERROR] = "错误:无法获取主栈信息,请检查主栈的相关配置",
  108. [PRINT_FIRMWARE_INFO] = "固件名称:%s,硬件版本号:%s,软件版本号:%s",
  109. [PRINT_ASSERT_ON_THREAD] = "在线程(%s)中发生断言",
  110. [PRINT_ASSERT_ON_HANDLER] = "在中断或裸机环境下发生断言",
  111. [PRINT_THREAD_STACK_INFO] = "=========== 线程堆栈信息 ===========",
  112. [PRINT_MAIN_STACK_INFO] = "============ 主堆栈信息 ============",
  113. [PRINT_THREAD_STACK_OVERFLOW] = "错误:线程栈(%08x)发生溢出",
  114. [PRINT_MAIN_STACK_OVERFLOW] = "错误:主栈(%08x)发生溢出",
  115. [PRINT_CALL_STACK_INFO] = "查看更多函数调用栈信息,请运行:addr2line -e %s%s -a -f %.*s",
  116. [PRINT_CALL_STACK_ERR] = "获取函数调用栈失败",
  117. [PRINT_FAULT_ON_THREAD] = "在线程(%s)中发生错误异常",
  118. [PRINT_FAULT_ON_HANDLER] = "在中断或裸机环境下发生错误异常",
  119. [PRINT_REGS_TITLE] = "========================= 寄存器信息 =========================",
  120. [PRINT_HFSR_VECTBL] = "发生硬错误,原因:取中断向量时出错",
  121. [PRINT_MFSR_IACCVIOL] = "发生存储器管理错误,原因:企图从不允许访问的区域取指令",
  122. [PRINT_MFSR_DACCVIOL] = "发生存储器管理错误,原因:企图从不允许访问的区域读、写数据",
  123. [PRINT_MFSR_MUNSTKERR] = "发生存储器管理错误,原因:出栈时企图访问不被允许的区域",
  124. [PRINT_MFSR_MSTKERR] = "发生存储器管理错误,原因:入栈时企图访问不被允许的区域",
  125. [PRINT_MFSR_MLSPERR] = "发生存储器管理错误,原因:惰性保存浮点状态时发生错误",
  126. [PRINT_BFSR_IBUSERR] = "发生总线错误,原因:指令总线错误",
  127. [PRINT_BFSR_PRECISERR] = "发生总线错误,原因:精确的数据总线错误",
  128. [PRINT_BFSR_IMPREISERR] = "发生总线错误,原因:不精确的数据总线错误",
  129. [PRINT_BFSR_UNSTKERR] = "发生总线错误,原因:出栈时发生错误",
  130. [PRINT_BFSR_STKERR] = "发生总线错误,原因:入栈时发生错误",
  131. [PRINT_BFSR_LSPERR] = "发生总线错误,原因:惰性保存浮点状态时发生错误",
  132. [PRINT_UFSR_UNDEFINSTR] = "发生用法错误,原因:企图执行未定义指令",
  133. [PRINT_UFSR_INVSTATE] = "发生用法错误,原因:试图切换到 ARM 状态",
  134. [PRINT_UFSR_INVPC] = "发生用法错误,原因:无效的异常返回码",
  135. [PRINT_UFSR_NOCP] = "发生用法错误,原因:企图执行协处理器指令",
  136. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  137. [PRINT_UFSR_STKOF] = "发生用法错误,原因:硬件检测到栈溢出",
  138. #endif
  139. [PRINT_UFSR_UNALIGNED] = "发生用法错误,原因:企图执行非对齐访问",
  140. [PRINT_UFSR_DIVBYZERO0] = "发生用法错误,原因:企图执行除 0 操作",
  141. [PRINT_DFSR_HALTED] = "发生调试错误,原因:NVIC 停机请求",
  142. [PRINT_DFSR_BKPT] = "发生调试错误,原因:执行 BKPT 指令",
  143. [PRINT_DFSR_DWTTRAP] = "发生调试错误,原因:数据监测点匹配",
  144. [PRINT_DFSR_VCATCH] = "发生调试错误,原因:发生向量捕获",
  145. [PRINT_DFSR_EXTERNAL] = "发生调试错误,原因:外部调试请求",
  146. [PRINT_MMAR] = "发生存储器管理错误的地址:%08x",
  147. [PRINT_BFAR] = "发生总线错误的地址:%08x",
  148. };
  149. //static char fw_name[CMB_NAME_MAX] = {0};
  150. //static char hw_ver[CMB_NAME_MAX] = {0};
  151. //static char sw_ver[CMB_NAME_MAX] = {0};
  152. static char *fw_name = "";
  153. static char *hw_ver = "";
  154. static char *sw_ver = "";
  155. static uint32_t main_stack_start_addr = 0;
  156. static size_t main_stack_size = 0;
  157. static uint32_t code_start_addr = 0;
  158. static size_t code_size = 0;
  159. static bool init_ok = false;
  160. static char call_stack_info[CMB_CALL_STACK_MAX_DEPTH * (8 + 1)] = { 0 };
  161. static bool on_fault = false;
  162. static bool stack_is_overflow = false;
  163. static struct cmb_hard_fault_regs regs;
  164. static void (*user_run_code)(void);
  165. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  166. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  167. static bool statck_has_fpu_regs = false;
  168. #endif
  169. static bool on_thread_before_fault = false;
  170. /**
  171. * library initialize
  172. */
  173. void cm_backtrace_init(const char *firmware_name, const char *hardware_ver, const char *software_ver) {
  174. if (firmware_name) fw_name = (char *)firmware_name;
  175. if (hardware_ver) hw_ver = (char *)hardware_ver;
  176. if (software_ver) sw_ver = (char *)software_ver;
  177. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  178. main_stack_start_addr = (uint32_t)&CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  179. main_stack_size = (uint32_t)&CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  180. code_start_addr = (uint32_t)&CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  181. code_size = (uint32_t)&CODE_SECTION_END(CMB_CODE_SECTION_NAME) - code_start_addr;
  182. #elif defined(__ICCARM__)
  183. main_stack_start_addr = (uint32_t)__section_begin(CMB_CSTACK_BLOCK_NAME);
  184. main_stack_size = (uint32_t)__section_end(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  185. code_start_addr = (uint32_t)__section_begin(CMB_CODE_SECTION_NAME);
  186. code_size = (uint32_t)__section_end(CMB_CODE_SECTION_NAME) - code_start_addr;
  187. #elif defined(__GNUC__)
  188. main_stack_start_addr = (uint32_t)(&CMB_CSTACK_BLOCK_START);
  189. main_stack_size = (uint32_t)(&CMB_CSTACK_BLOCK_END) - main_stack_start_addr;
  190. code_start_addr = (uint32_t)(&CMB_CODE_SECTION_START);
  191. code_size = (uint32_t)(&CMB_CODE_SECTION_END) - code_start_addr;
  192. #else
  193. #error "not supported compiler"
  194. #endif
  195. if (main_stack_size == 0) {
  196. cmb_println(print_info[PRINT_MAIN_STACK_CFG_ERROR]);
  197. return;
  198. }
  199. init_ok = true;
  200. }
  201. void cm_backtrace_init_ex(const char *firmware_name, const char *hardware_ver, const char *software_ver, void *user_code) {
  202. cm_backtrace_init(firmware_name, hardware_ver, software_ver);
  203. user_run_code = user_code;
  204. }
  205. /**
  206. * print firmware information, such as: firmware name, hardware version, software version
  207. */
  208. void cm_backtrace_firmware_info(void) {
  209. cmb_println(print_info[PRINT_FIRMWARE_INFO], fw_name, hw_ver, sw_ver);
  210. }
  211. #ifdef CMB_USING_OS_PLATFORM
  212. /**
  213. * Get current thread stack information
  214. *
  215. * @param sp stack current pointer
  216. * @param start_addr stack start address
  217. * @param size stack size
  218. */
  219. static void get_cur_thread_stack_info(uint32_t sp, uint32_t *start_addr, size_t *size) {
  220. CMB_ASSERT(start_addr);
  221. CMB_ASSERT(size);
  222. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  223. *start_addr = (uint32_t) rt_thread_self()->stack_addr;
  224. *size = rt_thread_self()->stack_size;
  225. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  226. extern OS_TCB *OSTCBCur;
  227. *start_addr = (uint32_t) OSTCBCur->OSTCBStkBottom;
  228. *size = OSTCBCur->OSTCBStkSize * sizeof(OS_STK);
  229. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  230. extern OS_TCB *OSTCBCurPtr;
  231. *start_addr = (uint32_t) OSTCBCurPtr->StkBasePtr;
  232. *size = OSTCBCurPtr->StkSize * sizeof(CPU_STK_SIZE);
  233. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  234. *start_addr = (uint32_t)vTaskStackAddr();
  235. *size = vTaskStackSize() * sizeof( StackType_t );
  236. #endif
  237. }
  238. /**
  239. * Get current thread name
  240. */
  241. static const char *get_cur_thread_name(void) {
  242. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  243. return rt_thread_self()->name;
  244. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  245. extern OS_TCB *OSTCBCur;
  246. #if OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0
  247. return (const char *)OSTCBCur->OSTCBTaskName;
  248. #else
  249. return NULL;
  250. #endif /* OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0 */
  251. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  252. extern OS_TCB *OSTCBCurPtr;
  253. return (const char *)OSTCBCurPtr->NamePtr;
  254. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  255. return vTaskName();
  256. #endif
  257. }
  258. #endif /* CMB_USING_OS_PLATFORM */
  259. #ifdef CMB_USING_DUMP_STACK_INFO
  260. /**
  261. * dump current stack information
  262. */
  263. static void dump_stack(uint32_t stack_start_addr, size_t stack_size, uint32_t *stack_pointer) {
  264. if (stack_is_overflow) {
  265. if (on_thread_before_fault) {
  266. cmb_println(print_info[PRINT_THREAD_STACK_OVERFLOW], stack_pointer);
  267. } else {
  268. cmb_println(print_info[PRINT_MAIN_STACK_OVERFLOW], stack_pointer);
  269. }
  270. if ((uint32_t) stack_pointer < stack_start_addr) {
  271. stack_pointer = (uint32_t *) stack_start_addr;
  272. } else if ((uint32_t) stack_pointer > stack_start_addr + stack_size) {
  273. stack_pointer = (uint32_t *) (stack_start_addr + stack_size);
  274. }
  275. }
  276. cmb_println(print_info[PRINT_THREAD_STACK_INFO]);
  277. for (; (uint32_t) stack_pointer < stack_start_addr + stack_size; stack_pointer++) {
  278. cmb_println(" addr: %08x data: %08x", stack_pointer, *stack_pointer);
  279. }
  280. cmb_println("====================================");
  281. }
  282. #endif /* CMB_USING_DUMP_STACK_INFO */
  283. /* check the disassembly instruction is 'BL' or 'BLX' */
  284. static bool disassembly_ins_is_bl_blx(uint32_t addr) {
  285. uint16_t ins1 = *((uint16_t *)addr);
  286. uint16_t ins2 = *((uint16_t *)(addr + 2));
  287. #define BL_INS_MASK 0xF800
  288. #define BL_INS_HIGH 0xF800
  289. #define BL_INS_LOW 0xF000
  290. #define BLX_INX_MASK 0xFF00
  291. #define BLX_INX 0x4700
  292. if ((ins2 & BL_INS_MASK) == BL_INS_HIGH && (ins1 & BL_INS_MASK) == BL_INS_LOW) {
  293. return true;
  294. } else if ((ins2 & BLX_INX_MASK) == BLX_INX) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * backtrace function call stack
  302. *
  303. * @param buffer call stack buffer
  304. * @param size buffer size
  305. * @param sp stack pointer
  306. *
  307. * @return depth
  308. */
  309. size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) {
  310. uint32_t stack_start_addr = main_stack_start_addr, pc;
  311. size_t depth = 0, stack_size = main_stack_size;
  312. bool regs_saved_lr_is_valid = false;
  313. if (on_fault) {
  314. if (!stack_is_overflow) {
  315. /* first depth is PC */
  316. buffer[depth++] = regs.saved.pc;
  317. /* fix the LR address in thumb mode */
  318. pc = regs.saved.lr - 1;
  319. if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  320. && (depth < size)) {
  321. buffer[depth++] = pc;
  322. regs_saved_lr_is_valid = true;
  323. }
  324. }
  325. #ifdef CMB_USING_OS_PLATFORM
  326. /* program is running on thread before fault */
  327. if (on_thread_before_fault) {
  328. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  329. }
  330. } else {
  331. /* OS environment */
  332. if (cmb_get_sp() == cmb_get_psp()) {
  333. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  334. }
  335. #endif /* CMB_USING_OS_PLATFORM */
  336. }
  337. if (stack_is_overflow) {
  338. if (sp < stack_start_addr) {
  339. sp = stack_start_addr;
  340. } else if (sp > stack_start_addr + stack_size) {
  341. sp = stack_start_addr + stack_size;
  342. }
  343. }
  344. /* copy called function address */
  345. for (; sp < stack_start_addr + stack_size; sp += sizeof(size_t)) {
  346. /* the *sp value may be LR, so need decrease a word to PC */
  347. pc = *((uint32_t *) sp) - sizeof(size_t);
  348. /* the Cortex-M using thumb instruction, so the pc must be an odd number */
  349. if (pc % 2 == 0) {
  350. continue;
  351. }
  352. /* fix the PC address in thumb mode */
  353. pc = *((uint32_t *) sp) - 1;
  354. if ((pc >= code_start_addr + sizeof(size_t)) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  355. /* check the the instruction before PC address is 'BL' or 'BLX' */
  356. && disassembly_ins_is_bl_blx(pc - sizeof(size_t)) && (depth < size)) {
  357. /* the second depth function may be already saved, so need ignore repeat */
  358. if ((depth == 2) && regs_saved_lr_is_valid && (pc == buffer[1])) {
  359. continue;
  360. }
  361. buffer[depth++] = pc;
  362. }
  363. }
  364. return depth;
  365. }
  366. /**
  367. * dump function call stack
  368. *
  369. * @param sp stack pointer
  370. */
  371. static void print_call_stack(uint32_t sp) {
  372. size_t i, cur_depth = 0;
  373. uint32_t call_stack_buf[CMB_CALL_STACK_MAX_DEPTH] = {0};
  374. cur_depth = cm_backtrace_call_stack(call_stack_buf, CMB_CALL_STACK_MAX_DEPTH, sp);
  375. for (i = 0; i < cur_depth; i++) {
  376. #ifdef __BUILD_APP__
  377. sprintf_(call_stack_info + i * (8 + 1), "%x", call_stack_buf[i]);
  378. #else
  379. sprintf(call_stack_info + i * (8 + 1), "%08lx", call_stack_buf[i]);
  380. #endif
  381. call_stack_info[i * (8 + 1) + 8] = ' ';
  382. }
  383. if (cur_depth) {
  384. cmb_println(print_info[PRINT_CALL_STACK_INFO], fw_name, CMB_ELF_FILE_EXTENSION_NAME, cur_depth * (8 + 1),
  385. call_stack_info);
  386. } else {
  387. cmb_println(print_info[PRINT_CALL_STACK_ERR]);
  388. }
  389. #ifdef __BUILD_OS__
  390. Core_PrintServiceStack();
  391. #endif
  392. }
  393. /**
  394. * backtrace for assert
  395. *
  396. * @param sp the stack pointer when on assert occurred
  397. */
  398. void cm_backtrace_assert(uint32_t sp) {
  399. CMB_ASSERT(init_ok);
  400. #ifdef CMB_USING_OS_PLATFORM
  401. uint32_t cur_stack_pointer = cmb_get_sp();
  402. #endif
  403. cmb_println("");
  404. cm_backtrace_firmware_info();
  405. #ifdef CMB_USING_OS_PLATFORM
  406. /* OS environment */
  407. if (cur_stack_pointer == cmb_get_msp()) {
  408. cmb_println(print_info[PRINT_ASSERT_ON_HANDLER]);
  409. #ifdef CMB_USING_DUMP_STACK_INFO
  410. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  411. #endif /* CMB_USING_DUMP_STACK_INFO */
  412. } else if (cur_stack_pointer == cmb_get_psp()) {
  413. cmb_println(print_info[PRINT_ASSERT_ON_THREAD], get_cur_thread_name());
  414. #ifdef CMB_USING_DUMP_STACK_INFO
  415. uint32_t stack_start_addr;
  416. size_t stack_size;
  417. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  418. dump_stack(stack_start_addr, stack_size, (uint32_t *) sp);
  419. #endif /* CMB_USING_DUMP_STACK_INFO */
  420. }
  421. #else
  422. /* bare metal(no OS) environment */
  423. #ifdef CMB_USING_DUMP_STACK_INFO
  424. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  425. #endif /* CMB_USING_DUMP_STACK_INFO */
  426. #endif /* CMB_USING_OS_PLATFORM */
  427. print_call_stack(sp);
  428. }
  429. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  430. /**
  431. * fault diagnosis then print cause of fault
  432. */
  433. static void fault_diagnosis(void) {
  434. if (regs.hfsr.bits.VECTBL) {
  435. cmb_println(print_info[PRINT_HFSR_VECTBL]);
  436. }
  437. if (regs.hfsr.bits.FORCED) {
  438. /* Memory Management Fault */
  439. if (regs.mfsr.value) {
  440. if (regs.mfsr.bits.IACCVIOL) {
  441. cmb_println(print_info[PRINT_MFSR_IACCVIOL]);
  442. }
  443. if (regs.mfsr.bits.DACCVIOL) {
  444. cmb_println(print_info[PRINT_MFSR_DACCVIOL]);
  445. }
  446. if (regs.mfsr.bits.MUNSTKERR) {
  447. cmb_println(print_info[PRINT_MFSR_MUNSTKERR]);
  448. }
  449. if (regs.mfsr.bits.MSTKERR) {
  450. cmb_println(print_info[PRINT_MFSR_MSTKERR]);
  451. }
  452. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  453. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  454. if (regs.mfsr.bits.MLSPERR) {
  455. cmb_println(print_info[PRINT_MFSR_MLSPERR]);
  456. }
  457. #endif
  458. if (regs.mfsr.bits.MMARVALID) {
  459. if (regs.mfsr.bits.IACCVIOL || regs.mfsr.bits.DACCVIOL) {
  460. cmb_println(print_info[PRINT_MMAR], regs.mmar);
  461. }
  462. }
  463. }
  464. /* Bus Fault */
  465. if (regs.bfsr.value) {
  466. if (regs.bfsr.bits.IBUSERR) {
  467. cmb_println(print_info[PRINT_BFSR_IBUSERR]);
  468. }
  469. if (regs.bfsr.bits.PRECISERR) {
  470. cmb_println(print_info[PRINT_BFSR_PRECISERR]);
  471. }
  472. if (regs.bfsr.bits.IMPREISERR) {
  473. cmb_println(print_info[PRINT_BFSR_IMPREISERR]);
  474. }
  475. if (regs.bfsr.bits.UNSTKERR) {
  476. cmb_println(print_info[PRINT_BFSR_UNSTKERR]);
  477. }
  478. if (regs.bfsr.bits.STKERR) {
  479. cmb_println(print_info[PRINT_BFSR_STKERR]);
  480. }
  481. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  482. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  483. if (regs.bfsr.bits.LSPERR) {
  484. cmb_println(print_info[PRINT_BFSR_LSPERR]);
  485. }
  486. #endif
  487. if (regs.bfsr.bits.BFARVALID) {
  488. if (regs.bfsr.bits.PRECISERR) {
  489. cmb_println(print_info[PRINT_BFAR], regs.bfar);
  490. }
  491. }
  492. }
  493. /* Usage Fault */
  494. if (regs.ufsr.value) {
  495. if (regs.ufsr.bits.UNDEFINSTR) {
  496. cmb_println(print_info[PRINT_UFSR_UNDEFINSTR]);
  497. }
  498. if (regs.ufsr.bits.INVSTATE) {
  499. cmb_println(print_info[PRINT_UFSR_INVSTATE]);
  500. }
  501. if (regs.ufsr.bits.INVPC) {
  502. cmb_println(print_info[PRINT_UFSR_INVPC]);
  503. }
  504. if (regs.ufsr.bits.NOCP) {
  505. cmb_println(print_info[PRINT_UFSR_NOCP]);
  506. }
  507. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  508. if (regs.ufsr.bits.STKOF) {
  509. cmb_println(print_info[PRINT_UFSR_STKOF]);
  510. }
  511. #endif
  512. if (regs.ufsr.bits.UNALIGNED) {
  513. cmb_println(print_info[PRINT_UFSR_UNALIGNED]);
  514. }
  515. if (regs.ufsr.bits.DIVBYZERO0) {
  516. cmb_println(print_info[PRINT_UFSR_DIVBYZERO0]);
  517. }
  518. }
  519. }
  520. /* Debug Fault */
  521. if (regs.hfsr.bits.DEBUGEVT) {
  522. if (regs.dfsr.value) {
  523. if (regs.dfsr.bits.HALTED) {
  524. cmb_println(print_info[PRINT_DFSR_HALTED]);
  525. }
  526. if (regs.dfsr.bits.BKPT) {
  527. cmb_println(print_info[PRINT_DFSR_BKPT]);
  528. }
  529. if (regs.dfsr.bits.DWTTRAP) {
  530. cmb_println(print_info[PRINT_DFSR_DWTTRAP]);
  531. }
  532. if (regs.dfsr.bits.VCATCH) {
  533. cmb_println(print_info[PRINT_DFSR_VCATCH]);
  534. }
  535. if (regs.dfsr.bits.EXTERNAL) {
  536. cmb_println(print_info[PRINT_DFSR_EXTERNAL]);
  537. }
  538. }
  539. }
  540. }
  541. #endif /* (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0) */
  542. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  543. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  544. static uint32_t statck_del_fpu_regs(uint32_t fault_handler_lr, uint32_t sp) {
  545. statck_has_fpu_regs = (fault_handler_lr & (1UL << 4)) == 0 ? true : false;
  546. /* the stack has S0~S15 and FPSCR registers when statck_has_fpu_regs is true, double word align */
  547. return statck_has_fpu_regs == true ? sp + sizeof(size_t) * 18 : sp;
  548. }
  549. #endif
  550. /**
  551. * backtrace for fault
  552. * @note only call once
  553. *
  554. * @param fault_handler_lr the LR register value on fault handler
  555. * @param fault_handler_sp the stack pointer on fault handler
  556. */
  557. void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp) {
  558. uint32_t stack_pointer = fault_handler_sp, saved_regs_addr = stack_pointer;
  559. const char *regs_name[] = { "R0 ", "R1 ", "R2 ", "R3 ", "R12", "LR ", "PC ", "PSR" };
  560. #ifdef CMB_USING_DUMP_STACK_INFO
  561. uint32_t stack_start_addr = main_stack_start_addr;
  562. size_t stack_size = main_stack_size;
  563. #endif
  564. CMB_ASSERT(init_ok);
  565. /* only call once */
  566. CMB_ASSERT(!on_fault);
  567. on_fault = true;
  568. cmb_println("");
  569. cm_backtrace_firmware_info();
  570. #ifdef CMB_USING_OS_PLATFORM
  571. on_thread_before_fault = fault_handler_lr & (1UL << 2);
  572. /* check which stack was used before (MSP or PSP) */
  573. if (on_thread_before_fault) {
  574. cmb_println(print_info[PRINT_FAULT_ON_THREAD], get_cur_thread_name() != NULL ? get_cur_thread_name() : "NO_NAME");
  575. saved_regs_addr = stack_pointer = cmb_get_psp();
  576. #ifdef CMB_USING_DUMP_STACK_INFO
  577. get_cur_thread_stack_info(stack_pointer, &stack_start_addr, &stack_size);
  578. #endif /* CMB_USING_DUMP_STACK_INFO */
  579. } else {
  580. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  581. }
  582. #else
  583. /* bare metal(no OS) environment */
  584. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  585. #endif /* CMB_USING_OS_PLATFORM */
  586. /* delete saved R0~R3, R12, LR,PC,xPSR registers space */
  587. stack_pointer += sizeof(size_t) * 8;
  588. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) || \
  589. (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M33)
  590. stack_pointer = statck_del_fpu_regs(fault_handler_lr, stack_pointer);
  591. #endif /* (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) */
  592. #ifdef CMB_USING_DUMP_STACK_INFO
  593. /* check stack overflow */
  594. if (stack_pointer < stack_start_addr || stack_pointer > stack_start_addr + stack_size) {
  595. stack_is_overflow = true;
  596. }
  597. /* dump stack information */
  598. dump_stack(stack_start_addr, stack_size, (uint32_t *) stack_pointer);
  599. #endif /* CMB_USING_DUMP_STACK_INFO */
  600. /* the stack frame may be get failed when it is overflow */
  601. if (!stack_is_overflow) {
  602. /* dump register */
  603. cmb_println(print_info[PRINT_REGS_TITLE]);
  604. regs.saved.r0 = ((uint32_t *)saved_regs_addr)[0]; // Register R0
  605. regs.saved.r1 = ((uint32_t *)saved_regs_addr)[1]; // Register R1
  606. regs.saved.r2 = ((uint32_t *)saved_regs_addr)[2]; // Register R2
  607. regs.saved.r3 = ((uint32_t *)saved_regs_addr)[3]; // Register R3
  608. regs.saved.r12 = ((uint32_t *)saved_regs_addr)[4]; // Register R12
  609. regs.saved.lr = ((uint32_t *)saved_regs_addr)[5]; // Link register LR
  610. regs.saved.pc = ((uint32_t *)saved_regs_addr)[6]; // Program counter PC
  611. regs.saved.psr.value = ((uint32_t *)saved_regs_addr)[7]; // Program status word PSR
  612. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[0], regs.saved.r0,
  613. regs_name[1], regs.saved.r1,
  614. regs_name[2], regs.saved.r2,
  615. regs_name[3], regs.saved.r3);
  616. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[4], regs.saved.r12,
  617. regs_name[5], regs.saved.lr,
  618. regs_name[6], regs.saved.pc,
  619. regs_name[7], regs.saved.psr.value);
  620. cmb_println("==============================================================");
  621. }
  622. /* the Cortex-M0 is not support fault diagnosis */
  623. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  624. regs.syshndctrl.value = CMB_SYSHND_CTRL; // System Handler Control and State Register
  625. regs.mfsr.value = CMB_NVIC_MFSR; // Memory Fault Status Register
  626. regs.mmar = CMB_NVIC_MMAR; // Memory Management Fault Address Register
  627. regs.bfsr.value = CMB_NVIC_BFSR; // Bus Fault Status Register
  628. regs.bfar = CMB_NVIC_BFAR; // Bus Fault Manage Address Register
  629. regs.ufsr.value = CMB_NVIC_UFSR; // Usage Fault Status Register
  630. regs.hfsr.value = CMB_NVIC_HFSR; // Hard Fault Status Register
  631. regs.dfsr.value = CMB_NVIC_DFSR; // Debug Fault Status Register
  632. regs.afsr = CMB_NVIC_AFSR; // Auxiliary Fault Status Register
  633. #ifdef __BUILD_APP__
  634. fault_diagnosis();
  635. #endif
  636. #endif
  637. print_call_stack(stack_pointer);
  638. if (user_run_code)
  639. {
  640. user_run_code();
  641. }
  642. }
  643. #endif