cm_backtrace.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. #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_MAIN_STACK_CFG_ERROR,
  61. PRINT_FIRMWARE_INFO,
  62. PRINT_ASSERT_ON_THREAD,
  63. PRINT_ASSERT_ON_HANDLER,
  64. PRINT_THREAD_STACK_INFO,
  65. PRINT_MAIN_STACK_INFO,
  66. PRINT_THREAD_STACK_OVERFLOW,
  67. PRINT_MAIN_STACK_OVERFLOW,
  68. PRINT_CALL_STACK_INFO,
  69. PRINT_CALL_STACK_ERR,
  70. PRINT_FAULT_ON_THREAD,
  71. PRINT_FAULT_ON_HANDLER,
  72. PRINT_REGS_TITLE,
  73. PRINT_HFSR_VECTBL,
  74. PRINT_MFSR_IACCVIOL,
  75. PRINT_MFSR_DACCVIOL,
  76. PRINT_MFSR_MUNSTKERR,
  77. PRINT_MFSR_MSTKERR,
  78. PRINT_MFSR_MLSPERR,
  79. PRINT_BFSR_IBUSERR,
  80. PRINT_BFSR_PRECISERR,
  81. PRINT_BFSR_IMPREISERR,
  82. PRINT_BFSR_UNSTKERR,
  83. PRINT_BFSR_STKERR,
  84. PRINT_BFSR_LSPERR,
  85. PRINT_UFSR_UNDEFINSTR,
  86. PRINT_UFSR_INVSTATE,
  87. PRINT_UFSR_INVPC,
  88. PRINT_UFSR_NOCP,
  89. PRINT_UFSR_UNALIGNED,
  90. PRINT_UFSR_DIVBYZERO0,
  91. PRINT_DFSR_HALTED,
  92. PRINT_DFSR_BKPT,
  93. PRINT_DFSR_DWTTRAP,
  94. PRINT_DFSR_VCATCH,
  95. PRINT_DFSR_EXTERNAL,
  96. PRINT_MMAR,
  97. PRINT_BFAR,
  98. };
  99. static const char * const print_info[] = {
  100. #if (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_ENGLISH)
  101. [PRINT_MAIN_STACK_CFG_ERROR] = "ERROR: Unable to get the main stack information, please check the configuration of the main stack",
  102. [PRINT_FIRMWARE_INFO] = "Firmware name: %s, hardware version: %s, software version: %s",
  103. [PRINT_ASSERT_ON_THREAD] = "Assert on thread %s",
  104. [PRINT_ASSERT_ON_HANDLER] = "Assert on interrupt or bare metal(no OS) environment",
  105. [PRINT_THREAD_STACK_INFO] = "===== Thread stack information =====",
  106. [PRINT_MAIN_STACK_INFO] = "====== Main stack information ======",
  107. [PRINT_THREAD_STACK_OVERFLOW] = "Error: Thread stack(%08x) was overflow",
  108. [PRINT_MAIN_STACK_OVERFLOW] = "Error: Main stack(%08x) was overflow",
  109. [PRINT_CALL_STACK_INFO] = "Show more call stack info by run: addr2line -e %s%s -a -f %.*s",
  110. [PRINT_CALL_STACK_ERR] = "Dump call stack has an error",
  111. [PRINT_FAULT_ON_THREAD] = "Fault on thread %s",
  112. [PRINT_FAULT_ON_HANDLER] = "Fault on interrupt or bare metal(no OS) environment",
  113. [PRINT_REGS_TITLE] = "=================== Registers information ====================",
  114. [PRINT_HFSR_VECTBL] = "Hard fault is caused by failed vector fetch",
  115. [PRINT_MFSR_IACCVIOL] = "Memory management fault is caused by instruction access violation",
  116. [PRINT_MFSR_DACCVIOL] = "Memory management fault is caused by data access violation",
  117. [PRINT_MFSR_MUNSTKERR] = "Memory management fault is caused by unstacking error",
  118. [PRINT_MFSR_MSTKERR] = "Memory management fault is caused by stacking error",
  119. [PRINT_MFSR_MLSPERR] = "Memory management fault is caused by floating-point lazy state preservation",
  120. [PRINT_BFSR_IBUSERR] = "Bus fault is caused by instruction access violation",
  121. [PRINT_BFSR_PRECISERR] = "Bus fault is caused by precise data access violation",
  122. [PRINT_BFSR_IMPREISERR] = "Bus fault is caused by imprecise data access violation",
  123. [PRINT_BFSR_UNSTKERR] = "Bus fault is caused by unstacking error",
  124. [PRINT_BFSR_STKERR] = "Bus fault is caused by stacking error",
  125. [PRINT_BFSR_LSPERR] = "Bus fault is caused by floating-point lazy state preservation",
  126. [PRINT_UFSR_UNDEFINSTR] = "Usage fault is caused by attempts to execute an undefined instruction",
  127. [PRINT_UFSR_INVSTATE] = "Usage fault is caused by attempts to switch to an invalid state (e.g., ARM)",
  128. [PRINT_UFSR_INVPC] = "Usage fault is caused by attempts to do an exception with a bad value in the EXC_RETURN number",
  129. [PRINT_UFSR_NOCP] = "Usage fault is caused by attempts to execute a coprocessor instruction",
  130. [PRINT_UFSR_UNALIGNED] = "Usage fault is caused by indicates that an unaligned access fault has taken place",
  131. [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)",
  132. [PRINT_DFSR_HALTED] = "Debug fault is caused by halt requested in NVIC",
  133. [PRINT_DFSR_BKPT] = "Debug fault is caused by BKPT instruction executed",
  134. [PRINT_DFSR_DWTTRAP] = "Debug fault is caused by DWT match occurred",
  135. [PRINT_DFSR_VCATCH] = "Debug fault is caused by Vector fetch occurred",
  136. [PRINT_DFSR_EXTERNAL] = "Debug fault is caused by EDBGRQ signal asserted",
  137. [PRINT_MMAR] = "The memory management fault occurred address is %08x",
  138. [PRINT_BFAR] = "The bus fault occurred address is %08x",
  139. #elif (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_CHINESE)
  140. [PRINT_MAIN_STACK_CFG_ERROR] = "�����޷���ȡ��ջ��Ϣ��������ջ���������",
  141. [PRINT_FIRMWARE_INFO] = "�̼����ƣ�%s��Ӳ���汾�ţ�%s�������汾�ţ�%s",
  142. [PRINT_ASSERT_ON_THREAD] = "���߳�(%s)�з�������",
  143. [PRINT_ASSERT_ON_HANDLER] = "���жϻ���������·�������",
  144. [PRINT_THREAD_STACK_INFO] = "=========== �̶߳�ջ��Ϣ ===========",
  145. [PRINT_MAIN_STACK_INFO] = "============ ����ջ��Ϣ ============",
  146. [PRINT_THREAD_STACK_OVERFLOW] = "�����߳�ջ(%08x)�������",
  147. [PRINT_MAIN_STACK_OVERFLOW] = "������ջ(%08x)�������",
  148. [PRINT_CALL_STACK_INFO] = "�鿴���ຯ������ջ��Ϣ�������У�addr2line -e %s%s -a -f %.*s",
  149. [PRINT_CALL_STACK_ERR] = "��ȡ��������ջʧ��",
  150. [PRINT_FAULT_ON_THREAD] = "���߳�(%s)�з��������쳣",
  151. [PRINT_FAULT_ON_HANDLER] = "���жϻ���������·��������쳣",
  152. [PRINT_REGS_TITLE] = "========================= �Ĵ�����Ϣ =========================",
  153. [PRINT_HFSR_VECTBL] = "����Ӳ����ԭ��ȡ�ж�����ʱ����",
  154. [PRINT_MFSR_IACCVIOL] = "�����洢����������ԭ����ͼ�Ӳ��������ʵ�����ȡָ��",
  155. [PRINT_MFSR_DACCVIOL] = "�����洢����������ԭ����ͼ�Ӳ��������ʵ��������д����",
  156. [PRINT_MFSR_MUNSTKERR] = "�����洢����������ԭ�򣺳�ջʱ��ͼ���ʲ�������������",
  157. [PRINT_MFSR_MSTKERR] = "�����洢����������ԭ����ջʱ��ͼ���ʲ�������������",
  158. [PRINT_MFSR_MLSPERR] = "�����洢����������ԭ�򣺶��Ա��渡��״̬ʱ��������",
  159. [PRINT_BFSR_IBUSERR] = "�������ߴ���ԭ��ָ�����ߴ���",
  160. [PRINT_BFSR_PRECISERR] = "�������ߴ���ԭ�򣺾�ȷ���������ߴ���",
  161. [PRINT_BFSR_IMPREISERR] = "�������ߴ���ԭ�򣺲���ȷ���������ߴ���",
  162. [PRINT_BFSR_UNSTKERR] = "�������ߴ���ԭ�򣺳�ջʱ��������",
  163. [PRINT_BFSR_STKERR] = "�������ߴ���ԭ����ջʱ��������",
  164. [PRINT_BFSR_LSPERR] = "�������ߴ���ԭ�򣺶��Ա��渡��״̬ʱ��������",
  165. [PRINT_UFSR_UNDEFINSTR] = "�����÷�����ԭ����ͼִ��δ����ָ��",
  166. [PRINT_UFSR_INVSTATE] = "�����÷�����ԭ����ͼ�л��� ARM ״̬",
  167. [PRINT_UFSR_INVPC] = "�����÷�����ԭ����Ч���쳣������",
  168. [PRINT_UFSR_NOCP] = "�����÷�����ԭ����ͼִ��Э������ָ��",
  169. [PRINT_UFSR_UNALIGNED] = "�����÷�����ԭ����ͼִ�зǶ������",
  170. [PRINT_UFSR_DIVBYZERO0] = "�����÷�����ԭ����ͼִ�г� 0 ����",
  171. [PRINT_DFSR_HALTED] = "�������Դ���ԭ��NVIC ͣ������",
  172. [PRINT_DFSR_BKPT] = "�������Դ���ԭ��ִ�� BKPT ָ��",
  173. [PRINT_DFSR_DWTTRAP] = "�������Դ���ԭ�����ݼ���ƥ��",
  174. [PRINT_DFSR_VCATCH] = "�������Դ���ԭ�򣺷�����������",
  175. [PRINT_DFSR_EXTERNAL] = "�������Դ���ԭ���ⲿ��������",
  176. [PRINT_MMAR] = "�����洢����������ĵ�ַ��%08x",
  177. [PRINT_BFAR] = "�������ߴ���ĵ�ַ��%08x",
  178. #else
  179. #error "CMB_PRINT_LANGUAGE defined error in 'cmb_cfg.h'"
  180. #endif
  181. };
  182. static char fw_name[CMB_NAME_MAX] = {0};
  183. static char hw_ver[CMB_NAME_MAX] = {0};
  184. static char sw_ver[CMB_NAME_MAX] = {0};
  185. static uint32_t main_stack_start_addr = 0;
  186. static size_t main_stack_size = 0;
  187. static uint32_t code_start_addr = 0;
  188. static size_t code_size = 0;
  189. static bool init_ok = false;
  190. static char call_stack_info[CMB_CALL_STACK_MAX_DEPTH * (8 + 1)] = { 0 };
  191. static bool on_fault = false;
  192. static bool stack_is_overflow = false;
  193. static struct cmb_hard_fault_regs regs;
  194. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  195. static bool statck_has_fpu_regs = false;
  196. #endif
  197. static bool on_thread_before_fault = false;
  198. /**
  199. * library initialize
  200. */
  201. void cm_backtrace_init(const char *firmware_name, const char *hardware_ver, const char *software_ver) {
  202. strncpy(fw_name, firmware_name, CMB_NAME_MAX);
  203. strncpy(hw_ver, hardware_ver, CMB_NAME_MAX);
  204. strncpy(sw_ver, software_ver, CMB_NAME_MAX);
  205. #if defined(__CC_ARM)
  206. main_stack_start_addr = (uint32_t)&CSTACK_BLOCK_START(CMB_CSTACK_BLOCK_NAME);
  207. main_stack_size = (uint32_t)&CSTACK_BLOCK_END(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  208. code_start_addr = (uint32_t)&CODE_SECTION_START(CMB_CODE_SECTION_NAME);
  209. code_size = (uint32_t)&CODE_SECTION_END(CMB_CODE_SECTION_NAME) - code_start_addr;
  210. #elif defined(__ICCARM__)
  211. main_stack_start_addr = (uint32_t)__section_begin(CMB_CSTACK_BLOCK_NAME);
  212. main_stack_size = (uint32_t)__section_end(CMB_CSTACK_BLOCK_NAME) - main_stack_start_addr;
  213. code_start_addr = (uint32_t)__section_begin(CMB_CODE_SECTION_NAME);
  214. code_size = (uint32_t)__section_end(CMB_CODE_SECTION_NAME) - code_start_addr;
  215. #elif defined(__GNUC__)
  216. main_stack_start_addr = (uint32_t)(&CMB_CSTACK_BLOCK_START);
  217. main_stack_size = (uint32_t)(&CMB_CSTACK_BLOCK_END) - main_stack_start_addr;
  218. code_start_addr = (uint32_t)(&CMB_CODE_SECTION_START);
  219. code_size = (uint32_t)(&CMB_CODE_SECTION_END) - code_start_addr;
  220. #else
  221. #error "not supported compiler"
  222. #endif
  223. if (main_stack_size == 0) {
  224. cmb_println(print_info[PRINT_MAIN_STACK_CFG_ERROR]);
  225. return;
  226. }
  227. init_ok = true;
  228. }
  229. /**
  230. * print firmware information, such as: firmware name, hardware version, software version
  231. */
  232. void cm_backtrace_firmware_info(void) {
  233. cmb_println(print_info[PRINT_FIRMWARE_INFO], fw_name, hw_ver, sw_ver);
  234. }
  235. #ifdef CMB_USING_OS_PLATFORM
  236. /**
  237. * Get current thread stack information
  238. *
  239. * @param sp stack current pointer
  240. * @param start_addr stack start address
  241. * @param size stack size
  242. */
  243. static void get_cur_thread_stack_info(uint32_t sp, uint32_t *start_addr, size_t *size) {
  244. CMB_ASSERT(start_addr);
  245. CMB_ASSERT(size);
  246. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  247. *start_addr = (uint32_t) rt_thread_self()->stack_addr;
  248. *size = rt_thread_self()->stack_size;
  249. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  250. extern OS_TCB *OSTCBCur;
  251. *start_addr = (uint32_t) OSTCBCur->OSTCBStkBottom;
  252. *size = OSTCBCur->OSTCBStkSize * sizeof(OS_STK);
  253. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  254. extern OS_TCB *OSTCBCurPtr;
  255. *start_addr = (uint32_t) OSTCBCurPtr->StkBasePtr;
  256. *size = OSTCBCurPtr->StkSize * sizeof(CPU_STK_SIZE);
  257. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  258. *start_addr = (uint32_t)vTaskStackAddr();
  259. *size = vTaskStackSize() * sizeof( StackType_t );
  260. #endif
  261. }
  262. /**
  263. * Get current thread name
  264. */
  265. static const char *get_cur_thread_name(void) {
  266. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  267. return rt_thread_self()->name;
  268. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  269. extern OS_TCB *OSTCBCur;
  270. #if OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0
  271. return (const char *)OSTCBCur->OSTCBTaskName;
  272. #else
  273. return NULL;
  274. #endif /* OS_TASK_NAME_SIZE > 0 || OS_TASK_NAME_EN > 0 */
  275. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  276. extern OS_TCB *OSTCBCurPtr;
  277. return (const char *)OSTCBCurPtr->NamePtr;
  278. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  279. return vTaskName();
  280. #endif
  281. }
  282. #endif /* CMB_USING_OS_PLATFORM */
  283. #ifdef CMB_USING_DUMP_STACK_INFO
  284. /**
  285. * dump current stack information
  286. */
  287. static void dump_stack(uint32_t stack_start_addr, size_t stack_size, uint32_t *stack_pointer) {
  288. if (stack_is_overflow) {
  289. if (on_thread_before_fault) {
  290. cmb_println(print_info[PRINT_THREAD_STACK_OVERFLOW], stack_pointer);
  291. } else {
  292. cmb_println(print_info[PRINT_MAIN_STACK_OVERFLOW], stack_pointer);
  293. }
  294. if ((uint32_t) stack_pointer < stack_start_addr) {
  295. stack_pointer = (uint32_t *) stack_start_addr;
  296. } else if ((uint32_t) stack_pointer > stack_start_addr + stack_size) {
  297. stack_pointer = (uint32_t *) (stack_start_addr + stack_size);
  298. }
  299. }
  300. cmb_println(print_info[PRINT_THREAD_STACK_INFO]);
  301. for (; (uint32_t) stack_pointer < stack_start_addr + stack_size; stack_pointer++) {
  302. cmb_println(" addr: %08x data: %08x", stack_pointer, *stack_pointer);
  303. }
  304. cmb_println("====================================");
  305. }
  306. #endif /* CMB_USING_DUMP_STACK_INFO */
  307. /* check the disassembly instruction is 'BL' or 'BLX' */
  308. static bool disassembly_ins_is_bl_blx(uint32_t addr) {
  309. uint16_t ins1 = *((uint16_t *)addr);
  310. uint16_t ins2 = *((uint16_t *)(addr + 2));
  311. #define BL_INS_MASK 0xF800
  312. #define BL_INS_HIGH 0xF800
  313. #define BL_INS_LOW 0xF000
  314. #define BLX_INX_MASK 0xFF00
  315. #define BLX_INX 0x4700
  316. if ((ins2 & BL_INS_MASK) == BL_INS_HIGH && (ins1 & BL_INS_MASK) == BL_INS_LOW) {
  317. return true;
  318. } else if ((ins2 & BLX_INX_MASK) == BLX_INX) {
  319. return true;
  320. } else {
  321. return false;
  322. }
  323. }
  324. /**
  325. * backtrace function call stack
  326. *
  327. * @param buffer call stack buffer
  328. * @param size buffer size
  329. * @param sp stack pointer
  330. *
  331. * @return depth
  332. */
  333. size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) {
  334. uint32_t stack_start_addr = main_stack_start_addr, pc;
  335. size_t depth = 0, stack_size = main_stack_size;
  336. bool regs_saved_lr_is_valid = false;
  337. if (on_fault) {
  338. if (!stack_is_overflow) {
  339. /* first depth is PC */
  340. buffer[depth++] = regs.saved.pc;
  341. /* fix the LR address in thumb mode */
  342. pc = regs.saved.lr - 1;
  343. if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  344. && (depth < size)) {
  345. buffer[depth++] = pc;
  346. regs_saved_lr_is_valid = true;
  347. }
  348. }
  349. #ifdef CMB_USING_OS_PLATFORM
  350. /* program is running on thread before fault */
  351. if (on_thread_before_fault) {
  352. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  353. }
  354. } else {
  355. /* OS environment */
  356. if (cmb_get_sp() == cmb_get_psp()) {
  357. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  358. }
  359. #endif /* CMB_USING_OS_PLATFORM */
  360. }
  361. if (stack_is_overflow) {
  362. if (sp < stack_start_addr) {
  363. sp = stack_start_addr;
  364. } else if (sp > stack_start_addr + stack_size) {
  365. sp = stack_start_addr + stack_size;
  366. }
  367. }
  368. /* copy called function address */
  369. for (; sp < stack_start_addr + stack_size; sp += sizeof(size_t)) {
  370. /* the *sp value may be LR, so need decrease a word to PC */
  371. pc = *((uint32_t *) sp) - sizeof(size_t);
  372. /* the Cortex-M using thumb instruction, so the pc must be an odd number */
  373. if (pc % 2 == 0) {
  374. continue;
  375. }
  376. /* fix the PC address in thumb mode */
  377. pc = *((uint32_t *) sp) - 1;
  378. if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
  379. /* check the the instruction before PC address is 'BL' or 'BLX' */
  380. && disassembly_ins_is_bl_blx(pc - sizeof(size_t)) && (depth < size)) {
  381. /* the second depth function may be already saved, so need ignore repeat */
  382. if ((depth == 2) && regs_saved_lr_is_valid && (pc == buffer[1])) {
  383. continue;
  384. }
  385. buffer[depth++] = pc;
  386. }
  387. }
  388. return depth;
  389. }
  390. /**
  391. * dump function call stack
  392. *
  393. * @param sp stack pointer
  394. */
  395. static void print_call_stack(uint32_t sp) {
  396. size_t i, cur_depth = 0;
  397. uint32_t call_stack_buf[CMB_CALL_STACK_MAX_DEPTH] = {0};
  398. cur_depth = cm_backtrace_call_stack(call_stack_buf, CMB_CALL_STACK_MAX_DEPTH, sp);
  399. for (i = 0; i < cur_depth; i++) {
  400. sprintf(call_stack_info + i * (8 + 1), "%08lx", call_stack_buf[i]);
  401. call_stack_info[i * (8 + 1) + 8] = ' ';
  402. }
  403. if (cur_depth) {
  404. cmb_println(print_info[PRINT_CALL_STACK_INFO], fw_name, CMB_ELF_FILE_EXTENSION_NAME, cur_depth * (8 + 1),
  405. call_stack_info);
  406. } else {
  407. cmb_println(print_info[PRINT_CALL_STACK_ERR]);
  408. }
  409. }
  410. /**
  411. * backtrace for assert
  412. *
  413. * @param sp the stack pointer when on assert occurred
  414. */
  415. void cm_backtrace_assert(uint32_t sp) {
  416. CMB_ASSERT(init_ok);
  417. #ifdef CMB_USING_OS_PLATFORM
  418. uint32_t cur_stack_pointer = cmb_get_sp();
  419. #endif
  420. cmb_println("");
  421. cm_backtrace_firmware_info();
  422. #ifdef CMB_USING_OS_PLATFORM
  423. /* OS environment */
  424. if (cur_stack_pointer == cmb_get_msp()) {
  425. cmb_println(print_info[PRINT_ASSERT_ON_HANDLER]);
  426. #ifdef CMB_USING_DUMP_STACK_INFO
  427. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  428. #endif /* CMB_USING_DUMP_STACK_INFO */
  429. } else if (cur_stack_pointer == cmb_get_psp()) {
  430. cmb_println(print_info[PRINT_ASSERT_ON_THREAD], get_cur_thread_name());
  431. #ifdef CMB_USING_DUMP_STACK_INFO
  432. uint32_t stack_start_addr;
  433. size_t stack_size;
  434. get_cur_thread_stack_info(sp, &stack_start_addr, &stack_size);
  435. dump_stack(stack_start_addr, stack_size, (uint32_t *) sp);
  436. #endif /* CMB_USING_DUMP_STACK_INFO */
  437. }
  438. #else
  439. /* bare metal(no OS) environment */
  440. #ifdef CMB_USING_DUMP_STACK_INFO
  441. dump_stack(main_stack_start_addr, main_stack_size, (uint32_t *) sp);
  442. #endif /* CMB_USING_DUMP_STACK_INFO */
  443. #endif /* CMB_USING_OS_PLATFORM */
  444. print_call_stack(sp);
  445. }
  446. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  447. /**
  448. * fault diagnosis then print cause of fault
  449. */
  450. static void fault_diagnosis(void) {
  451. if (regs.hfsr.bits.VECTBL) {
  452. cmb_println(print_info[PRINT_HFSR_VECTBL]);
  453. }
  454. if (regs.hfsr.bits.FORCED) {
  455. /* Memory Management Fault */
  456. if (regs.mfsr.value) {
  457. if (regs.mfsr.bits.IACCVIOL) {
  458. cmb_println(print_info[PRINT_MFSR_IACCVIOL]);
  459. }
  460. if (regs.mfsr.bits.DACCVIOL) {
  461. cmb_println(print_info[PRINT_MFSR_DACCVIOL]);
  462. }
  463. if (regs.mfsr.bits.MUNSTKERR) {
  464. cmb_println(print_info[PRINT_MFSR_MUNSTKERR]);
  465. }
  466. if (regs.mfsr.bits.MSTKERR) {
  467. cmb_println(print_info[PRINT_MFSR_MSTKERR]);
  468. }
  469. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  470. if (regs.mfsr.bits.MLSPERR) {
  471. cmb_println(print_info[PRINT_MFSR_MLSPERR]);
  472. }
  473. #endif
  474. if (regs.mfsr.bits.MMARVALID) {
  475. if (regs.mfsr.bits.IACCVIOL || regs.mfsr.bits.DACCVIOL) {
  476. cmb_println(print_info[PRINT_MMAR], regs.mmar);
  477. }
  478. }
  479. }
  480. /* Bus Fault */
  481. if (regs.bfsr.value) {
  482. if (regs.bfsr.bits.IBUSERR) {
  483. cmb_println(print_info[PRINT_BFSR_IBUSERR]);
  484. }
  485. if (regs.bfsr.bits.PRECISERR) {
  486. cmb_println(print_info[PRINT_BFSR_PRECISERR]);
  487. }
  488. if (regs.bfsr.bits.IMPREISERR) {
  489. cmb_println(print_info[PRINT_BFSR_IMPREISERR]);
  490. }
  491. if (regs.bfsr.bits.UNSTKERR) {
  492. cmb_println(print_info[PRINT_BFSR_UNSTKERR]);
  493. }
  494. if (regs.bfsr.bits.STKERR) {
  495. cmb_println(print_info[PRINT_BFSR_STKERR]);
  496. }
  497. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  498. if (regs.bfsr.bits.LSPERR) {
  499. cmb_println(print_info[PRINT_BFSR_LSPERR]);
  500. }
  501. #endif
  502. if (regs.bfsr.bits.BFARVALID) {
  503. if (regs.bfsr.bits.PRECISERR) {
  504. cmb_println(print_info[PRINT_BFAR], regs.bfar);
  505. }
  506. }
  507. }
  508. /* Usage Fault */
  509. if (regs.ufsr.value) {
  510. if (regs.ufsr.bits.UNDEFINSTR) {
  511. cmb_println(print_info[PRINT_UFSR_UNDEFINSTR]);
  512. }
  513. if (regs.ufsr.bits.INVSTATE) {
  514. cmb_println(print_info[PRINT_UFSR_INVSTATE]);
  515. }
  516. if (regs.ufsr.bits.INVPC) {
  517. cmb_println(print_info[PRINT_UFSR_INVPC]);
  518. }
  519. if (regs.ufsr.bits.NOCP) {
  520. cmb_println(print_info[PRINT_UFSR_NOCP]);
  521. }
  522. if (regs.ufsr.bits.UNALIGNED) {
  523. cmb_println(print_info[PRINT_UFSR_UNALIGNED]);
  524. }
  525. if (regs.ufsr.bits.DIVBYZERO0) {
  526. cmb_println(print_info[PRINT_UFSR_DIVBYZERO0]);
  527. }
  528. }
  529. }
  530. /* Debug Fault */
  531. if (regs.hfsr.bits.DEBUGEVT) {
  532. if (regs.dfsr.value) {
  533. if (regs.dfsr.bits.HALTED) {
  534. cmb_println(print_info[PRINT_DFSR_HALTED]);
  535. }
  536. if (regs.dfsr.bits.BKPT) {
  537. cmb_println(print_info[PRINT_DFSR_BKPT]);
  538. }
  539. if (regs.dfsr.bits.DWTTRAP) {
  540. cmb_println(print_info[PRINT_DFSR_DWTTRAP]);
  541. }
  542. if (regs.dfsr.bits.VCATCH) {
  543. cmb_println(print_info[PRINT_DFSR_VCATCH]);
  544. }
  545. if (regs.dfsr.bits.EXTERNAL) {
  546. cmb_println(print_info[PRINT_DFSR_EXTERNAL]);
  547. }
  548. }
  549. }
  550. }
  551. #endif /* (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0) */
  552. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  553. static uint32_t statck_del_fpu_regs(uint32_t fault_handler_lr, uint32_t sp) {
  554. statck_has_fpu_regs = (fault_handler_lr & (1UL << 4)) == 0 ? true : false;
  555. /* the stack has S0~S15 and FPSCR registers when statck_has_fpu_regs is true, double word align */
  556. return statck_has_fpu_regs == true ? sp + sizeof(size_t) * 18 : sp;
  557. }
  558. #endif
  559. /**
  560. * backtrace for fault
  561. * @note only call once
  562. *
  563. * @param fault_handler_lr the LR register value on fault handler
  564. * @param fault_handler_sp the stack pointer on fault handler
  565. */
  566. void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp) {
  567. uint32_t stack_pointer = fault_handler_sp, saved_regs_addr = stack_pointer;
  568. const char *regs_name[] = { "R0 ", "R1 ", "R2 ", "R3 ", "R12", "LR ", "PC ", "PSR" };
  569. #ifdef CMB_USING_DUMP_STACK_INFO
  570. uint32_t stack_start_addr = main_stack_start_addr;
  571. size_t stack_size = main_stack_size;
  572. #endif
  573. CMB_ASSERT(init_ok);
  574. /* only call once */
  575. CMB_ASSERT(!on_fault);
  576. on_fault = true;
  577. cmb_println("");
  578. cm_backtrace_firmware_info();
  579. #ifdef CMB_USING_OS_PLATFORM
  580. on_thread_before_fault = fault_handler_lr & (1UL << 2);
  581. /* check which stack was used before (MSP or PSP) */
  582. if (on_thread_before_fault) {
  583. cmb_println(print_info[PRINT_FAULT_ON_THREAD], get_cur_thread_name() != NULL ? get_cur_thread_name() : "NO_NAME");
  584. saved_regs_addr = stack_pointer = cmb_get_psp();
  585. #ifdef CMB_USING_DUMP_STACK_INFO
  586. get_cur_thread_stack_info(stack_pointer, &stack_start_addr, &stack_size);
  587. #endif /* CMB_USING_DUMP_STACK_INFO */
  588. } else {
  589. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  590. }
  591. #else
  592. /* bare metal(no OS) environment */
  593. cmb_println(print_info[PRINT_FAULT_ON_HANDLER]);
  594. #endif /* CMB_USING_OS_PLATFORM */
  595. /* delete saved R0~R3, R12, LR,PC,xPSR registers space */
  596. stack_pointer += sizeof(size_t) * 8;
  597. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7)
  598. stack_pointer = statck_del_fpu_regs(fault_handler_lr, stack_pointer);
  599. #endif /* (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M7) */
  600. #ifdef CMB_USING_DUMP_STACK_INFO
  601. /* check stack overflow */
  602. if (stack_pointer < stack_start_addr || stack_pointer > stack_start_addr + stack_size) {
  603. stack_is_overflow = true;
  604. }
  605. /* dump stack information */
  606. dump_stack(stack_start_addr, stack_size, (uint32_t *) stack_pointer);
  607. #endif /* CMB_USING_DUMP_STACK_INFO */
  608. /* the stack frame may be get failed when it is overflow */
  609. if (!stack_is_overflow) {
  610. /* dump register */
  611. cmb_println(print_info[PRINT_REGS_TITLE]);
  612. regs.saved.r0 = ((uint32_t *)saved_regs_addr)[0]; // Register R0
  613. regs.saved.r1 = ((uint32_t *)saved_regs_addr)[1]; // Register R1
  614. regs.saved.r2 = ((uint32_t *)saved_regs_addr)[2]; // Register R2
  615. regs.saved.r3 = ((uint32_t *)saved_regs_addr)[3]; // Register R3
  616. regs.saved.r12 = ((uint32_t *)saved_regs_addr)[4]; // Register R12
  617. regs.saved.lr = ((uint32_t *)saved_regs_addr)[5]; // Link register LR
  618. regs.saved.pc = ((uint32_t *)saved_regs_addr)[6]; // Program counter PC
  619. regs.saved.psr.value = ((uint32_t *)saved_regs_addr)[7]; // Program status word PSR
  620. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[0], regs.saved.r0,
  621. regs_name[1], regs.saved.r1,
  622. regs_name[2], regs.saved.r2,
  623. regs_name[3], regs.saved.r3);
  624. cmb_println(" %s: %08x %s: %08x %s: %08x %s: %08x", regs_name[4], regs.saved.r12,
  625. regs_name[5], regs.saved.lr,
  626. regs_name[6], regs.saved.pc,
  627. regs_name[7], regs.saved.psr.value);
  628. cmb_println("==============================================================");
  629. }
  630. /* the Cortex-M0 is not support fault diagnosis */
  631. #if (CMB_CPU_PLATFORM_TYPE != CMB_CPU_ARM_CORTEX_M0)
  632. regs.syshndctrl.value = CMB_SYSHND_CTRL; // System Handler Control and State Register
  633. regs.mfsr.value = CMB_NVIC_MFSR; // Memory Fault Status Register
  634. regs.mmar = CMB_NVIC_MMAR; // Memory Management Fault Address Register
  635. regs.bfsr.value = CMB_NVIC_BFSR; // Bus Fault Status Register
  636. regs.bfar = CMB_NVIC_BFAR; // Bus Fault Manage Address Register
  637. regs.ufsr.value = CMB_NVIC_UFSR; // Usage Fault Status Register
  638. regs.hfsr.value = CMB_NVIC_HFSR; // Hard Fault Status Register
  639. regs.dfsr.value = CMB_NVIC_DFSR; // Debug Fault Status Register
  640. regs.afsr = CMB_NVIC_AFSR; // Auxiliary Fault Status Register
  641. fault_diagnosis();
  642. #endif
  643. print_call_stack(stack_pointer);
  644. }