cm_backtrace.c 32 KB

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