cm_backtrace.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * This file is part of the CmBacktrace Library.
  3. *
  4. * Copyright (c) 2016-2017, 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. #if __STDC_VERSION__ < 199901L
  33. #error "must be C99 or higher. try to add '-std=c99' to compile parameters"
  34. #endif
  35. #if defined(__CC_ARM)
  36. #define SECTION_START(_name_) _name_##$$Base
  37. #define SECTION_END(_name_) _name_##$$Limit
  38. #define IMAGE_SECTION_START(_name_) Image$$##_name_##$$Base
  39. #define IMAGE_SECTION_END(_name_) Image$$##_name_##$$Limit
  40. #define CSTACK_BLOCK_START(_name_) SECTION_START(_name_)
  41. #define CSTACK_BLOCK_END(_name_) SECTION_END(_name_)
  42. #define CODE_SECTION_START(_name_) IMAGE_SECTION_START(_name_)
  43. #define CODE_SECTION_END(_name_) IMAGE_SECTION_END(_name_)
  44. extern const int CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  45. extern const int CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME);
  46. extern const int CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  47. extern const int CODE_SECTION_END(CMB_CODE_SECTION_NAME);
  48. #elif defined(__ICCARM__)
  49. #pragma section=CMB_CSTACK_BLOCK_NAME
  50. #pragma section=CMB_CODE_SECTION_NAME
  51. #elif defined(__GNUC__)
  52. extern const int CMB_CSTACK_BLOCK_START;
  53. extern const int CMB_CSTACK_BLOCK_END;
  54. extern const int CMB_CODE_SECTION_START;
  55. extern const int CMB_CODE_SECTION_END;
  56. #else
  57. #error "not supported compiler"
  58. #endif
  59. enum {
  60. PRINT_FIRMWARE_INFO,
  61. PRINT_ASSERT_ON_THREAD,
  62. PRINT_ASSERT_ON_HANDLER,
  63. PRINT_THREAD_STACK_INFO,
  64. PRINT_MAIN_STACK_INFO,
  65. PRINT_THREAD_STACK_OVERFLOW,
  66. PRINT_MAIN_STACK_OVERFLOW,
  67. PRINT_CALL_STACK_INFO,
  68. PRINT_CALL_STACK_ERR,
  69. PRINT_FAULT_ON_THREAD,
  70. PRINT_FAULT_ON_HANDLER,
  71. PRINT_REGS_TITLE,
  72. PRINT_HFSR_VECTBL,
  73. PRINT_MFSR_IACCVIOL,
  74. PRINT_MFSR_DACCVIOL,
  75. PRINT_MFSR_MUNSTKERR,
  76. PRINT_MFSR_MSTKERR,
  77. PRINT_MFSR_MLSPERR,
  78. PRINT_BFSR_IBUSERR,
  79. PRINT_BFSR_PRECISERR,
  80. PRINT_BFSR_IMPREISERR,
  81. PRINT_BFSR_UNSTKERR,
  82. PRINT_BFSR_STKERR,
  83. PRINT_BFSR_LSPERR,
  84. PRINT_UFSR_UNDEFINSTR,
  85. PRINT_UFSR_INVSTATE,
  86. PRINT_UFSR_INVPC,
  87. PRINT_UFSR_NOCP,
  88. PRINT_UFSR_UNALIGNED,
  89. PRINT_UFSR_DIVBYZERO0,
  90. PRINT_DFSR_HALTED,
  91. PRINT_DFSR_BKPT,
  92. PRINT_DFSR_DWTTRAP,
  93. PRINT_DFSR_VCATCH,
  94. PRINT_DFSR_EXTERNAL,
  95. PRINT_MMAR,
  96. PRINT_BFAR,
  97. };
  98. static const char * const print_info[] = {
  99. #if (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_ENGLISH)
  100. [PRINT_FIRMWARE_INFO] = "Firmware name: %s, hardware version: %s, software version: %s",
  101. [PRINT_ASSERT_ON_THREAD] = "Assert on thread %s",
  102. [PRINT_ASSERT_ON_HANDLER] = "Assert on interrupt or bare metal(no OS) environment",
  103. [PRINT_THREAD_STACK_INFO] = "===== Thread stack information =====",
  104. [PRINT_MAIN_STACK_INFO] = "====== Main stack information ======",
  105. [PRINT_THREAD_STACK_OVERFLOW] = "Error: Thread stack(%08x) was overflow",
  106. [PRINT_MAIN_STACK_OVERFLOW] = "Error: Main stack(%08x) was overflow",
  107. [PRINT_CALL_STACK_INFO] = "Show more call stack info by run: addr2line -e %s%s -a -f %.*s",
  108. [PRINT_CALL_STACK_ERR] = "Dump call stack has an error",
  109. [PRINT_FAULT_ON_THREAD] = "Fault on thread %s",
  110. [PRINT_FAULT_ON_HANDLER] = "Fault on interrupt or bare metal(no OS) environment",
  111. [PRINT_REGS_TITLE] = "=================== Registers information ====================",
  112. [PRINT_HFSR_VECTBL] = "Hard fault is caused by failed vector fetch",
  113. [PRINT_MFSR_IACCVIOL] = "Memory management fault is caused by instruction access violation",
  114. [PRINT_MFSR_DACCVIOL] = "Memory management fault is caused by data access violation",
  115. [PRINT_MFSR_MUNSTKERR] = "Memory management fault is caused by unstacking error",
  116. [PRINT_MFSR_MSTKERR] = "Memory management fault is caused by stacking error",
  117. [PRINT_MFSR_MLSPERR] = "Memory management fault is caused by floating-point lazy state preservation",
  118. [PRINT_BFSR_IBUSERR] = "Bus fault is caused by instruction access violation",
  119. [PRINT_BFSR_PRECISERR] = "Bus fault is caused by precise data access violation",
  120. [PRINT_BFSR_IMPREISERR] = "Bus fault is caused by imprecise data access violation",
  121. [PRINT_BFSR_UNSTKERR] = "Bus fault is caused by unstacking error",
  122. [PRINT_BFSR_STKERR] = "Bus fault is caused by stacking error",
  123. [PRINT_BFSR_LSPERR] = "Bus fault is caused by floating-point lazy state preservation",
  124. [PRINT_UFSR_UNDEFINSTR] = "Usage fault is caused by attempts to execute an undefined instruction",
  125. [PRINT_UFSR_INVSTATE] = "Usage fault is caused by attempts to switch to an invalid state (e.g., ARM)",
  126. [PRINT_UFSR_INVPC] = "Usage fault is caused by attempts to do an exception with a bad value in the EXC_RETURN number",
  127. [PRINT_UFSR_NOCP] = "Usage fault is caused by attempts to execute a coprocessor instruction",
  128. [PRINT_UFSR_UNALIGNED] = "Usage fault is caused by indicates that an unaligned access fault has taken place",
  129. [PRINT_UFSR_DIVBYZERO0] = "Usage fault is caused by Indicates a divide by zero has taken place (can be set only if DIV_0_TRP is set)",
  130. [PRINT_DFSR_HALTED] = "Debug fault is caused by halt requested in NVIC",
  131. [PRINT_DFSR_BKPT] = "Debug fault is caused by BKPT instruction executed",
  132. [PRINT_DFSR_DWTTRAP] = "Debug fault is caused by DWT match occurred",
  133. [PRINT_DFSR_VCATCH] = "Debug fault is caused by Vector fetch occurred",
  134. [PRINT_DFSR_EXTERNAL] = "Debug fault is caused by EDBGRQ signal asserted",
  135. [PRINT_MMAR] = "The memory management fault occurred address is %08x",
  136. [PRINT_BFAR] = "The bus fault occurred address is %08x",
  137. #elif (CMB_PRINT_LANGUAGE == CMB_PRINT_LANUUAGE_CHINESE)
  138. [PRINT_FIRMWARE_INFO] = "固件名称:%s,硬件版本号:%s,软件版本号:%s",
  139. [PRINT_ASSERT_ON_THREAD] = "在线程(%s)中发生断言",
  140. [PRINT_ASSERT_ON_HANDLER] = "在中断或裸机环境下发生断言",
  141. [PRINT_THREAD_STACK_INFO] = "=========== 线程堆栈信息 ===========",
  142. [PRINT_MAIN_STACK_INFO] = "============ 主堆栈信息 ============",
  143. [PRINT_THREAD_STACK_OVERFLOW] = "错误:线程栈(%08x)发生溢出",
  144. [PRINT_MAIN_STACK_OVERFLOW] = "错误:主栈(%08x)发生溢出",
  145. [PRINT_CALL_STACK_INFO] = "查看更多函数调用栈信息,请运行:addr2line -e %s%s -a -f %.*s",
  146. [PRINT_CALL_STACK_ERR] = "获取函数调用栈失败",
  147. [PRINT_FAULT_ON_THREAD] = "在线程(%s)中发生错误异常",
  148. [PRINT_FAULT_ON_HANDLER] = "在中断或裸机环境下发生错误异常",
  149. [PRINT_REGS_TITLE] = "========================= 寄存器信息 =========================",
  150. [PRINT_HFSR_VECTBL] = "发生硬错误,原因:取中断向量时出错",
  151. [PRINT_MFSR_IACCVIOL] = "发生存储器管理错误,原因:企图从不允许访问的区域取指令",
  152. [PRINT_MFSR_DACCVIOL] = "发生存储器管理错误,原因:企图从不允许访问的区域读、写数据",
  153. [PRINT_MFSR_MUNSTKERR] = "发生存储器管理错误,原因:出栈时企图访问不被允许的区域",
  154. [PRINT_MFSR_MSTKERR] = "发生存储器管理错误,原因:入栈时企图访问不被允许的区域",
  155. [PRINT_MFSR_MLSPERR] = "发生存储器管理错误,原因:惰性保存浮点状态时发生错误",
  156. [PRINT_BFSR_IBUSERR] = "发生总线错误,原因:指令总线错误",
  157. [PRINT_BFSR_PRECISERR] = "发生总线错误,原因:精确的数据总线错误",
  158. [PRINT_BFSR_IMPREISERR] = "发生总线错误,原因:不精确的数据总线错误",
  159. [PRINT_BFSR_UNSTKERR] = "发生总线错误,原因:出栈时发生错误",
  160. [PRINT_BFSR_STKERR] = "发生总线错误,原因:入栈时发生错误",
  161. [PRINT_BFSR_LSPERR] = "发生总线错误,原因:惰性保存浮点状态时发生错误",
  162. [PRINT_UFSR_UNDEFINSTR] = "发生用法错误,原因:企图执行未定义指令",
  163. [PRINT_UFSR_INVSTATE] = "发生用法错误,原因:试图切换到 ARM 状态",
  164. [PRINT_UFSR_INVPC] = "发生用法错误,原因:无效的异常返回码",
  165. [PRINT_UFSR_NOCP] = "发生用法错误,原因:企图执行协处理器指令",
  166. [PRINT_UFSR_UNALIGNED] = "发生用法错误,原因:企图执行非对齐访问",
  167. [PRINT_UFSR_DIVBYZERO0] = "发生用法错误,原因:企图执行除 0 操作",
  168. [PRINT_DFSR_HALTED] = "发生调试错误,原因:NVIC 停机请求",
  169. [PRINT_DFSR_BKPT] = "发生调试错误,原因:执行 BKPT 指令",
  170. [PRINT_DFSR_DWTTRAP] = "发生调试错误,原因:数据监测点匹配",
  171. [PRINT_DFSR_VCATCH] = "发生调试错误,原因:发生向量捕获",
  172. [PRINT_DFSR_EXTERNAL] = "发生调试错误,原因:外部调试请求",
  173. [PRINT_MMAR] = "发生存储器管理错误的地址:%08x",
  174. [PRINT_BFAR] = "发生总线错误的地址:%08x",
  175. #else
  176. #error "CMB_PRINT_LANGUAGE defined error in 'cmb_cfg.h'"
  177. #endif
  178. };
  179. static char fw_name[CMB_NAME_MAX] = {0};
  180. static char hw_ver[CMB_NAME_MAX] = {0};
  181. static char sw_ver[CMB_NAME_MAX] = {0};
  182. static uint32_t main_stack_start_addr = 0;
  183. static size_t main_stack_size = 0;
  184. static uint32_t code_start_addr = 0;
  185. static size_t code_size = 0;
  186. static bool init_ok = false;
  187. static char call_stack_info[CMB_CALL_STACK_MAX_DEPTH * (8 + 1)] = { 0 };
  188. static bool on_fault = false;
  189. static bool stack_is_overflow = false;
  190. static struct cmb_hard_fault_regs regs;
  191. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  192. static bool statck_has_fpu_regs = false;
  193. #endif
  194. static bool on_thread_before_fault = false;
  195. /**
  196. * library initialize
  197. */
  198. void cm_backtrace_init(const char *firmware_name, const char *hardware_ver, const char *software_ver) {
  199. strncpy(fw_name, firmware_name, CMB_NAME_MAX);
  200. strncpy(hw_ver, hardware_ver, CMB_NAME_MAX);
  201. strncpy(sw_ver, software_ver, CMB_NAME_MAX);
  202. #if defined(__CC_ARM)
  203. main_stack_start_addr = (uint32_t)&CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  204. main_stack_size = (uint32_t)&CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  205. code_start_addr = (uint32_t)&CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  206. code_size = (uint32_t)&CODE_SECTION_END(CMB_CODE_SECTION_NAME) - code_start_addr;
  207. #elif defined(__ICCARM__)
  208. main_stack_start_addr = (uint32_t)__section_begin(CMB_CSTACK_BLOCK_NAME);
  209. main_stack_size = (uint32_t)__section_end(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  210. code_start_addr = (uint32_t)__section_begin(CMB_CODE_SECTION_NAME);
  211. code_size = (uint32_t)__section_end(CMB_CODE_SECTION_NAME) - code_start_addr;
  212. #elif defined(__GNUC__)
  213. main_stack_start_addr = (uint32_t)(&CMB_CSTACK_BLOCK_START);
  214. main_stack_size = (uint32_t)(&CMB_CSTACK_BLOCK_END) - main_stack_start_addr;
  215. code_start_addr = (uint32_t)(&CMB_CODE_SECTION_START);
  216. code_size = (uint32_t)(&CMB_CODE_SECTION_END) - code_start_addr;
  217. #else
  218. #error "not supported compiler"
  219. #endif
  220. init_ok = true;
  221. }
  222. /**
  223. * print firmware information, such as: firmware name, hardware version, software version
  224. */
  225. void cm_backtrace_firmware_info(void) {
  226. cmb_println(print_info[PRINT_FIRMWARE_INFO], fw_name, hw_ver, sw_ver);
  227. }
  228. #ifdef CMB_USING_OS_PLATFORM
  229. /**
  230. * Get current thread stack information
  231. *
  232. * @param sp stack current pointer
  233. * @param start_addr stack start address
  234. * @param size stack size
  235. */
  236. static void get_cur_thread_stack_info(uint32_t sp, uint32_t *start_addr, size_t *size) {
  237. CMB_ASSERT(start_addr);
  238. CMB_ASSERT(size);
  239. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  240. *start_addr = (uint32_t) rt_thread_self()->stack_addr;
  241. *size = rt_thread_self()->stack_size;
  242. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  243. extern OS_TCB *OSTCBCur;
  244. *start_addr = (uint32_t) OSTCBCur->OSTCBStkBottom;
  245. *size = OSTCBCur->OSTCBStkSize * sizeof(OS_STK);
  246. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  247. extern OS_TCB *OSTCBCurPtr;
  248. *start_addr = (uint32_t) OSTCBCurPtr->StkBasePtr;
  249. *size = OSTCBCurPtr->StkSize * sizeof(CPU_STK_SIZE);
  250. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  251. *start_addr = (uint32_t)vTaskStackAddr();
  252. *size = vTaskStackSize() * sizeof( StackType_t );
  253. #endif
  254. }
  255. /**
  256. * Get current thread name
  257. */
  258. static const char *get_cur_thread_name(void) {
  259. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  260. return rt_thread_self()->name;
  261. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  262. extern OS_TCB *OSTCBCur;
  263. #if OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0
  264. return (const char *)OSTCBCur->OSTCBTaskName;
  265. #else
  266. return NULL;
  267. #endif /* OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0 */
  268. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  269. extern OS_TCB *OSTCBCurPtr;
  270. return (const char *)OSTCBCurPtr->NamePtr;
  271. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  272. return vTaskName();
  273. #endif
  274. }
  275. #endif /* CMB_USING_OS_PLATFORM */
  276. #ifdef CMB_USING_DUMP_STACK_INFO
  277. /**
  278. * dump current stack information
  279. */
  280. static void dump_stack(uint32_t stack_start_addr, size_t stack_size, uint32_t *stack_pointer) {
  281. if (stack_is_overflow) {
  282. if (on_thread_before_fault) {
  283. cmb_println(print_info[PRINT_THREAD_STACK_OVERFLOW], stack_pointer);
  284. } else {
  285. cmb_println(print_info[PRINT_MAIN_STACK_OVERFLOW], stack_pointer);
  286. }
  287. if ((uint32_t) stack_pointer < stack_start_addr) {
  288. stack_pointer = (uint32_t *) stack_start_addr;
  289. } else if ((uint32_t) stack_pointer > stack_start_addr + stack_size) {
  290. stack_pointer = (uint32_t *) (stack_start_addr + stack_size);
  291. }
  292. }
  293. cmb_println(print_info[PRINT_THREAD_STACK_INFO]);
  294. for (; (uint32_t) stack_pointer < stack_start_addr + stack_size; stack_pointer++) {
  295. cmb_println(" addr: %08x data: %08x", stack_pointer, *stack_pointer);
  296. }
  297. cmb_println("====================================");
  298. }
  299. #endif /* CMB_USING_DUMP_STACK_INFO */
  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. /* second depth is from LR, so need decrease a word to PC */
  318. pc = regs.saved.lr - sizeof(size_t);
  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. if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  353. && (depth < size)) {
  354. /* the second depth function may be already saved, so need ignore repeat */
  355. if ((depth == 2) && regs_saved_lr_is_valid && (pc == buffer[1])) {
  356. continue;
  357. }
  358. buffer[depth++] = pc;
  359. }
  360. }
  361. return depth;
  362. }
  363. /**
  364. * dump function call stack
  365. *
  366. * @param sp stack pointer
  367. */
  368. static void print_call_stack(uint32_t sp) {
  369. size_t i, cur_depth = 0;
  370. uint32_t call_stack_buf[CMB_CALL_STACK_MAX_DEPTH] = {0};
  371. cur_depth = cm_backtrace_call_stack(call_stack_buf, CMB_CALL_STACK_MAX_DEPTH, sp);
  372. for (i = 0; i < cur_depth; i++) {
  373. sprintf(call_stack_info + i * (8 + 1), "%08lx", call_stack_buf[i]);
  374. call_stack_info[i * (8 + 1) + 8] = ' ';
  375. }
  376. if (cur_depth) {
  377. cmb_println(print_info[PRINT_CALL_STACK_INFO], fw_name, CMB_ELF_FILE_EXTENSION_NAME, cur_depth * (8 + 1),
  378. call_stack_info);
  379. } else {
  380. cmb_println(print_info[PRINT_CALL_STACK_ERR]);
  381. }
  382. }
  383. /**
  384. * backtrace for assert
  385. *
  386. * @param sp the stack pointer when on assert occurred
  387. */
  388. void cm_backtrace_assert(uint32_t sp) {
  389. CMB_ASSERT(init_ok);
  390. #ifdef CMB_USING_OS_PLATFORM
  391. uint32_t cur_stack_pointer = cmb_get_sp();
  392. #endif
  393. cmb_println("");
  394. cm_backtrace_firmware_info();
  395. #ifdef CMB_USING_OS_PLATFORM
  396. /* OS environment */
  397. if (cur_stack_pointer == cmb_get_msp()) {
  398. cmb_println(print_info[PRINT_ASSERT_ON_HANDLER]);
  399. #ifdef CMB_USING_DUMP_STACK_INFO
  400. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  401. #endif /* CMB_USING_DUMP_STACK_INFO */
  402. } else if (cur_stack_pointer == cmb_get_psp()) {
  403. cmb_println(print_info[PRINT_ASSERT_ON_THREAD], get_cur_thread_name());
  404. #ifdef CMB_USING_DUMP_STACK_INFO
  405. uint32_t stack_start_addr;
  406. size_t stack_size;
  407. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  408. dump_stack(stack_start_addr, stack_size, (uint32_t *) sp);
  409. #endif /* CMB_USING_DUMP_STACK_INFO */
  410. }
  411. #else
  412. /* bare metal(no OS) environment */
  413. #ifdef CMB_USING_DUMP_STACK_INFO
  414. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  415. #endif /* CMB_USING_DUMP_STACK_INFO */
  416. #endif /* CMB_USING_OS_PLATFORM */
  417. print_call_stack(sp);
  418. }
  419. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  420. /**
  421. * fault diagnosis then print cause of fault
  422. */
  423. static void fault_diagnosis(void) {
  424. if (regs.hfsr.bits.VECTBL) {
  425. cmb_println(print_info[PRINT_HFSR_VECTBL]);
  426. }
  427. if (regs.hfsr.bits.FORCED) {
  428. /* Memory Management Fault */
  429. if (regs.mfsr.value) {
  430. if (regs.mfsr.bits.IACCVIOL) {
  431. cmb_println(print_info[PRINT_MFSR_IACCVIOL]);
  432. }
  433. if (regs.mfsr.bits.DACCVIOL) {
  434. cmb_println(print_info[PRINT_MFSR_DACCVIOL]);
  435. }
  436. if (regs.mfsr.bits.MUNSTKERR) {
  437. cmb_println(print_info[PRINT_MFSR_MUNSTKERR]);
  438. }
  439. if (regs.mfsr.bits.MSTKERR) {
  440. cmb_println(print_info[PRINT_MFSR_MSTKERR]);
  441. }
  442. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  443. if (regs.mfsr.bits.MLSPERR) {
  444. cmb_println(print_info[PRINT_MFSR_MLSPERR]);
  445. }
  446. #endif
  447. if (regs.mfsr.bits.MMARVALID) {
  448. if (regs.mfsr.bits.IACCVIOL || regs.mfsr.bits.DACCVIOL) {
  449. cmb_println(print_info[PRINT_MMAR], regs.mmar);
  450. }
  451. }
  452. }
  453. /* Bus Fault */
  454. if (regs.bfsr.value) {
  455. if (regs.bfsr.bits.IBUSERR) {
  456. cmb_println(print_info[PRINT_BFSR_IBUSERR]);
  457. }
  458. if (regs.bfsr.bits.PRECISERR) {
  459. cmb_println(print_info[PRINT_BFSR_PRECISERR]);
  460. }
  461. if (regs.bfsr.bits.IMPREISERR) {
  462. cmb_println(print_info[PRINT_BFSR_IMPREISERR]);
  463. }
  464. if (regs.bfsr.bits.UNSTKERR) {
  465. cmb_println(print_info[PRINT_BFSR_UNSTKERR]);
  466. }
  467. if (regs.bfsr.bits.STKERR) {
  468. cmb_println(print_info[PRINT_BFSR_STKERR]);
  469. }
  470. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  471. if (regs.bfsr.bits.LSPERR) {
  472. cmb_println(print_info[PRINT_BFSR_LSPERR]);
  473. }
  474. #endif
  475. if (regs.bfsr.bits.BFARVALID) {
  476. if (regs.bfsr.bits.PRECISERR) {
  477. cmb_println(print_info[PRINT_BFAR], regs.bfar);
  478. }
  479. }
  480. }
  481. /* Usage Fault */
  482. if (regs.ufsr.value) {
  483. if (regs.ufsr.bits.UNDEFINSTR) {
  484. cmb_println(print_info[PRINT_UFSR_UNDEFINSTR]);
  485. }
  486. if (regs.ufsr.bits.INVSTATE) {
  487. cmb_println(print_info[PRINT_UFSR_INVSTATE]);
  488. }
  489. if (regs.ufsr.bits.INVPC) {
  490. cmb_println(print_info[PRINT_UFSR_INVPC]);
  491. }
  492. if (regs.ufsr.bits.NOCP) {
  493. cmb_println(print_info[PRINT_UFSR_NOCP]);
  494. }
  495. if (regs.ufsr.bits.UNALIGNED) {
  496. cmb_println(print_info[PRINT_UFSR_UNALIGNED]);
  497. }
  498. if (regs.ufsr.bits.DIVBYZERO0) {
  499. cmb_println(print_info[PRINT_UFSR_DIVBYZERO0]);
  500. }
  501. }
  502. }
  503. /* Debug Fault */
  504. if (regs.hfsr.bits.DEBUGEVT) {
  505. if (regs.dfsr.value) {
  506. if (regs.dfsr.bits.HALTED) {
  507. cmb_println(print_info[PRINT_DFSR_HALTED]);
  508. }
  509. if (regs.dfsr.bits.BKPT) {
  510. cmb_println(print_info[PRINT_DFSR_BKPT]);
  511. }
  512. if (regs.dfsr.bits.DWTTRAP) {
  513. cmb_println(print_info[PRINT_DFSR_DWTTRAP]);
  514. }
  515. if (regs.dfsr.bits.VCATCH) {
  516. cmb_println(print_info[PRINT_DFSR_VCATCH]);
  517. }
  518. if (regs.dfsr.bits.EXTERNAL) {
  519. cmb_println(print_info[PRINT_DFSR_EXTERNAL]);
  520. }
  521. }
  522. }
  523. }
  524. #endif /* (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0) */
  525. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  526. static uint32_t statck_del_fpu_regs(uint32_t fault_handler_lr, uint32_t sp) {
  527. statck_has_fpu_regs = (fault_handler_lr & (1UL << 4)) == 0 ? true : false;
  528. /* the stack has S0~S15 and FPSCR registers when statck_has_fpu_regs is true, double word align */
  529. return statck_has_fpu_regs == true ? sp + sizeof(size_t) * 18 : sp;
  530. }
  531. #endif
  532. /**
  533. * backtrace for fault
  534. * @note only call once
  535. *
  536. * @param fault_handler_lr the LR register value on fault handler
  537. * @param fault_handler_sp the stack pointer on fault handler
  538. */
  539. void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp) {
  540. uint32_t stack_pointer = fault_handler_sp, saved_regs_addr = stack_pointer;
  541. const char *regs_name[] = { "R0 ", "R1 ", "R2 ", "R3 ", "R12", "LR ", "PC ", "PSR" };
  542. #ifdef CMB_USING_DUMP_STACK_INFO
  543. uint32_t stack_start_addr = main_stack_start_addr;
  544. size_t stack_size = main_stack_size;
  545. #endif
  546. CMB_ASSERT(init_ok);
  547. /* only call once */
  548. CMB_ASSERT(!on_fault);
  549. on_fault = true;
  550. cmb_println("");
  551. cm_backtrace_firmware_info();
  552. #ifdef CMB_USING_OS_PLATFORM
  553. on_thread_before_fault = fault_handler_lr & (1UL << 2);
  554. /* check which stack was used before (MSP or PSP) */
  555. if (on_thread_before_fault) {
  556. cmb_println(print_info[PRINT_FAULT_ON_THREAD], get_cur_thread_name() != NULL ? get_cur_thread_name() : "NO_NAME");
  557. saved_regs_addr = stack_pointer = cmb_get_psp();
  558. #ifdef CMB_USING_DUMP_STACK_INFO
  559. get_cur_thread_stack_info(stack_pointer, &stack_start_addr, &stack_size);
  560. #endif /* CMB_USING_DUMP_STACK_INFO */
  561. } else {
  562. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  563. }
  564. #else
  565. /* bare metal(no OS) environment */
  566. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  567. #endif /* CMB_USING_OS_PLATFORM */
  568. /* delete saved R0~R3, R12, LR,PC,xPSR registers space */
  569. stack_pointer += sizeof(size_t) * 8;
  570. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  571. stack_pointer = statck_del_fpu_regs(fault_handler_lr, stack_pointer);
  572. #endif /* (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) */
  573. #ifdef CMB_USING_DUMP_STACK_INFO
  574. /* check stack overflow */
  575. if (stack_pointer < stack_start_addr || stack_pointer > stack_start_addr + stack_size) {
  576. stack_is_overflow = true;
  577. }
  578. /* dump stack information */
  579. dump_stack(stack_start_addr, stack_size, (uint32_t *) stack_pointer);
  580. #endif /* CMB_USING_DUMP_STACK_INFO */
  581. /* the stack frame may be get failed when it is overflow */
  582. if (!stack_is_overflow) {
  583. /* dump register */
  584. cmb_println(print_info[PRINT_REGS_TITLE]);
  585. regs.saved.r0 = ((uint32_t *)saved_regs_addr)[0]; // Register R0
  586. regs.saved.r1 = ((uint32_t *)saved_regs_addr)[1]; // Register R1
  587. regs.saved.r2 = ((uint32_t *)saved_regs_addr)[2]; // Register R2
  588. regs.saved.r3 = ((uint32_t *)saved_regs_addr)[3]; // Register R3
  589. regs.saved.r12 = ((uint32_t *)saved_regs_addr)[4]; // Register R12
  590. regs.saved.lr = ((uint32_t *)saved_regs_addr)[5]; // Link register LR
  591. regs.saved.pc = ((uint32_t *)saved_regs_addr)[6]; // Program counter PC
  592. regs.saved.psr.value = ((uint32_t *)saved_regs_addr)[7]; // Program status word PSR
  593. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[0], regs.saved.r0,
  594. regs_name[1], regs.saved.r1,
  595. regs_name[2], regs.saved.r2,
  596. regs_name[3], regs.saved.r3);
  597. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[4], regs.saved.r12,
  598. regs_name[5], regs.saved.lr,
  599. regs_name[6], regs.saved.pc,
  600. regs_name[7], regs.saved.psr.value);
  601. cmb_println("==============================================================");
  602. }
  603. /* the Cortex-M0 is not support fault diagnosis */
  604. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  605. regs.syshndctrl.value = CMB_SYSHND_CTRL; // System Handler Control and State Register
  606. regs.mfsr.value = CMB_NVIC_MFSR; // Memory Fault Status Register
  607. regs.mmar = CMB_NVIC_MMAR; // Memory Management Fault Address Register
  608. regs.bfsr.value = CMB_NVIC_BFSR; // Bus Fault Status Register
  609. regs.bfar = CMB_NVIC_BFAR; // Bus Fault Manage Address Register
  610. regs.ufsr.value = CMB_NVIC_UFSR; // Usage Fault Status Register
  611. regs.hfsr.value = CMB_NVIC_HFSR; // Hard Fault Status Register
  612. regs.dfsr.value = CMB_NVIC_DFSR; // Debug Fault Status Register
  613. regs.afsr = CMB_NVIC_AFSR; // Auxiliary Fault Status Register
  614. fault_diagnosis();
  615. #endif
  616. print_call_stack(stack_pointer);
  617. }