My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

backtrace.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Libbacktrace
  3. * Copyright 2015 Stephen Street <stephen@redrocketcomputing.com>
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * This library was modified, some bugs fixed, stack address validated
  10. * and adapted to be used in Marlin 3D printer firmware as backtracer
  11. * for exceptions for debugging purposes in 2018 by Eduardo José Tagle.
  12. */
  13. #ifdef ARDUINO_ARCH_SAM
  14. #include "backtrace.h"
  15. #include <stdint.h>
  16. #include <string.h>
  17. typedef struct unwind_control_block {
  18. uint32_t vrs[16];
  19. const uint32_t *current;
  20. int remaining;
  21. int byte;
  22. } unwind_control_block_t;
  23. typedef struct unwind_index {
  24. uint32_t addr_offset;
  25. uint32_t insn;
  26. } unwind_index_t;
  27. /* These symbols point to the unwind index and should be provide by the linker script */
  28. extern const unwind_index_t __exidx_start[];
  29. extern const unwind_index_t __exidx_end[];
  30. /* This prevents the linking of libgcc unwinder code */
  31. void __aeabi_unwind_cpp_pr0(void) {};
  32. void __aeabi_unwind_cpp_pr1(void) {};
  33. void __aeabi_unwind_cpp_pr2(void) {};
  34. /* These symbols point to the start and end of stack */
  35. extern const int _sstack;
  36. extern const int _estack;
  37. /* These symbols point to the start and end of the code section */
  38. extern const int _sfixed;
  39. extern const int _efixed;
  40. /* These symbols point to the start and end of initialized data (could be SRAM functions!) */
  41. extern const int _srelocate;
  42. extern const int _erelocate;
  43. /* Validate stack pointer (SP): It must be in the stack area */
  44. static inline __attribute__((always_inline)) int validate_sp(const void* sp) {
  45. // SP must point into the allocated stack area
  46. if ((uint32_t)sp >= (uint32_t)&_sstack && (uint32_t)sp <= (uint32_t)&_estack)
  47. return 0;
  48. return -1;
  49. }
  50. /* Validate code pointer (PC): It must be either in TEXT or in SRAM */
  51. static inline __attribute__((always_inline)) int validate_pc(const void* pc) {
  52. // PC must point into the text (CODE) area
  53. if ((uint32_t)pc >= (uint32_t)&_sfixed && (uint32_t)pc <= (uint32_t)&_efixed)
  54. return 0;
  55. // Or into the SRAM function area
  56. if ((uint32_t)pc >= (uint32_t)&_srelocate && (uint32_t)pc <= (uint32_t)&_erelocate)
  57. return 0;
  58. return 0;
  59. }
  60. static inline __attribute__((always_inline)) uint32_t prel31_to_addr(const uint32_t *prel31) {
  61. int32_t offset = (((int32_t)(*prel31)) << 1) >> 1;
  62. return ((uint32_t)prel31 + offset) & 0x7fffffff;
  63. }
  64. static const struct unwind_index *unwind_search_index(const unwind_index_t *start, const unwind_index_t *end, uint32_t ip) {
  65. const struct unwind_index *middle;
  66. /* Perform a binary search of the unwind index */
  67. while (start < end - 1) {
  68. middle = start + ((end - start + 1) >> 1);
  69. if (ip < prel31_to_addr(&middle->addr_offset))
  70. end = middle;
  71. else
  72. start = middle;
  73. }
  74. return start;
  75. }
  76. static const char *unwind_get_function_name(void *address) {
  77. uint32_t flag_word = *(uint32_t *)(address - 4);
  78. if ((flag_word & 0xff000000) == 0xff000000) {
  79. return (const char *)(address - 4 - (flag_word & 0x00ffffff));
  80. }
  81. return "unknown";
  82. }
  83. static int unwind_get_next_byte(unwind_control_block_t *ucb) {
  84. int instruction;
  85. /* Are there more instructions */
  86. if (ucb->remaining == 0)
  87. return -1;
  88. /* Extract the current instruction */
  89. instruction = ((*ucb->current) >> (ucb->byte << 3)) & 0xff;
  90. /* Move the next byte */
  91. --ucb->byte;
  92. if (ucb->byte < 0) {
  93. ++ucb->current;
  94. ucb->byte = 3;
  95. }
  96. --ucb->remaining;
  97. return instruction;
  98. }
  99. static int unwind_control_block_init(unwind_control_block_t *ucb, const uint32_t *instructions, const backtrace_frame_t *frame) {
  100. /* Initialize control block */
  101. memset(ucb, 0, sizeof(unwind_control_block_t));
  102. ucb->current = instructions;
  103. /* Is a short unwind description */
  104. if ((*instructions & 0xff000000) == 0x80000000) {
  105. ucb->remaining = 3;
  106. ucb->byte = 2;
  107. /* Is a long unwind description */
  108. } else if ((*instructions & 0xff000000) == 0x81000000) {
  109. ucb->remaining = ((*instructions & 0x00ff0000) >> 14) + 2;
  110. ucb->byte = 1;
  111. } else
  112. return -1;
  113. /* Initialize the virtual register set */
  114. ucb->vrs[7] = frame->fp;
  115. ucb->vrs[13] = frame->sp;
  116. ucb->vrs[14] = frame->lr;
  117. ucb->vrs[15] = 0;
  118. /* All good */
  119. return 0;
  120. }
  121. static int unwind_execute_instruction(unwind_control_block_t *ucb) {
  122. int instruction;
  123. uint32_t mask;
  124. uint32_t reg;
  125. uint32_t *vsp;
  126. /* Consume all instruction byte */
  127. while ((instruction = unwind_get_next_byte(ucb)) != -1) {
  128. if ((instruction & 0xc0) == 0x00) { // ARM_EXIDX_CMD_DATA_POP
  129. /* vsp = vsp + (xxxxxx << 2) + 4 */
  130. ucb->vrs[13] += ((instruction & 0x3f) << 2) + 4;
  131. } else
  132. if ((instruction & 0xc0) == 0x40) { // ARM_EXIDX_CMD_DATA_PUSH
  133. /* vsp = vsp - (xxxxxx << 2) - 4 */
  134. ucb->vrs[13] -= ((instruction & 0x3f) << 2) - 4;
  135. } else
  136. if ((instruction & 0xf0) == 0x80) {
  137. /* pop under mask {r15-r12},{r11-r4} or refuse to unwind */
  138. instruction = instruction << 8 | unwind_get_next_byte(ucb);
  139. /* Check for refuse to unwind */
  140. if (instruction == 0x8000) // ARM_EXIDX_CMD_REFUSED
  141. return 0;
  142. /* Pop registers using mask */ // ARM_EXIDX_CMD_REG_POP
  143. vsp = (uint32_t *)ucb->vrs[13];
  144. mask = instruction & 0xfff;
  145. reg = 4;
  146. while (mask) {
  147. if ((mask & 1) != 0) {
  148. if (validate_sp(vsp))
  149. return -1;
  150. ucb->vrs[reg] = *vsp++;
  151. }
  152. mask >>= 1;
  153. ++reg;
  154. }
  155. /* Patch up the vrs sp if it was in the mask */
  156. if ((instruction & (1 << (13 - 4))) != 0)
  157. ucb->vrs[13] = (uint32_t)vsp;
  158. } else
  159. if ((instruction & 0xf0) == 0x90 && // ARM_EXIDX_CMD_REG_TO_SP
  160. instruction != 0x9d &&
  161. instruction != 0x9f) {
  162. /* vsp = r[nnnn] */
  163. ucb->vrs[13] = ucb->vrs[instruction & 0x0f];
  164. } else
  165. if ((instruction & 0xf0) == 0xa0) { // ARM_EXIDX_CMD_REG_POP
  166. /* pop r4-r[4+nnn] or pop r4-r[4+nnn], r14*/
  167. vsp = (uint32_t *)ucb->vrs[13];
  168. for (reg = 4; reg <= (instruction & 0x07) + 4; ++reg) {
  169. if (validate_sp(vsp))
  170. return -1;
  171. ucb->vrs[reg] = *vsp++;
  172. }
  173. if (instruction & 0x08) { // ARM_EXIDX_CMD_REG_POP
  174. if (validate_sp(vsp))
  175. return -1;
  176. ucb->vrs[14] = *vsp++;
  177. }
  178. ucb->vrs[13] = (uint32_t)vsp;
  179. } else
  180. if (instruction == 0xb0) { // ARM_EXIDX_CMD_FINISH
  181. /* finished */
  182. if (ucb->vrs[15] == 0)
  183. ucb->vrs[15] = ucb->vrs[14];
  184. /* All done unwinding */
  185. return 0;
  186. } else
  187. if (instruction == 0xb1) { // ARM_EXIDX_CMD_REG_POP
  188. /* pop register under mask {r3,r2,r1,r0} */
  189. vsp = (uint32_t *)ucb->vrs[13];
  190. mask = unwind_get_next_byte(ucb);
  191. reg = 0;
  192. while (mask) {
  193. if ((mask & 1) != 0) {
  194. if (validate_sp(vsp))
  195. return -1;
  196. ucb->vrs[reg] = *vsp++;
  197. }
  198. mask >>= 1;
  199. ++reg;
  200. }
  201. ucb->vrs[13] = (uint32_t)vsp;
  202. } else
  203. if (instruction == 0xb2) { // ARM_EXIDX_CMD_DATA_POP
  204. /* vps = vsp + 0x204 + (uleb128 << 2) */
  205. ucb->vrs[13] += 0x204 + (unwind_get_next_byte(ucb) << 2);
  206. } else
  207. if (instruction == 0xb3 || // ARM_EXIDX_CMD_VFP_POP
  208. instruction == 0xc8 ||
  209. instruction == 0xc9) {
  210. /* pop VFP double-precision registers */
  211. vsp = (uint32_t *)ucb->vrs[13];
  212. /* D[ssss]-D[ssss+cccc] */
  213. if (validate_sp(vsp))
  214. return -1;
  215. ucb->vrs[14] = *vsp++;
  216. if (instruction == 0xc8) {
  217. /* D[16+sssss]-D[16+ssss+cccc] */
  218. ucb->vrs[14] |= 1 << 16;
  219. }
  220. if (instruction != 0xb3) {
  221. /* D[sssss]-D[ssss+cccc] */
  222. ucb->vrs[14] |= 1 << 17;
  223. }
  224. ucb->vrs[13] = (uint32_t)vsp;
  225. } else
  226. if ((instruction & 0xf8) == 0xb8 ||
  227. (instruction & 0xf8) == 0xd0) {
  228. /* Pop VFP double precision registers D[8]-D[8+nnn] */
  229. ucb->vrs[14] = 0x80 | (instruction & 0x07);
  230. if ((instruction & 0xf8) == 0xd0) {
  231. ucb->vrs[14] = 1 << 17;
  232. }
  233. } else
  234. return -1;
  235. }
  236. return instruction != -1;
  237. }
  238. static inline __attribute__((always_inline)) uint32_t *read_psp(void) {
  239. /* Read the current PSP and return its value as a pointer */
  240. uint32_t psp;
  241. __asm volatile (
  242. " mrs %0, psp \n"
  243. : "=r" (psp) : :
  244. );
  245. return (uint32_t*)psp;
  246. }
  247. static int unwind_frame(backtrace_frame_t *frame) {
  248. unwind_control_block_t ucb;
  249. const unwind_index_t *index;
  250. const uint32_t *instructions;
  251. int execution_result;
  252. /* Search the unwind index for the matching unwind table */
  253. index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
  254. if (index == NULL)
  255. return -1;
  256. /* Make sure we can unwind this frame */
  257. if (index->insn == 0x00000001)
  258. return 0;
  259. /* Get the pointer to the first unwind instruction */
  260. if (index->insn & 0x80000000)
  261. instructions = &index->insn;
  262. else
  263. instructions = (uint32_t *)prel31_to_addr(&index->insn);
  264. /* Initialize the unwind control block */
  265. if (unwind_control_block_init(&ucb, instructions, frame) < 0)
  266. return -1;
  267. /* Execute the unwind instructions */
  268. while ((execution_result = unwind_execute_instruction(&ucb)) > 0);
  269. if (execution_result == -1)
  270. return -1;
  271. /* Set the virtual pc to the virtual lr if this is the first unwind */
  272. if (ucb.vrs[15] == 0)
  273. ucb.vrs[15] = ucb.vrs[14];
  274. /* Check for exception return */
  275. /* TODO Test with other ARM processors to verify this method. */
  276. if ((ucb.vrs[15] & 0xf0000000) == 0xf0000000) {
  277. /* According to the Cortex Programming Manual (p.44), the stack address is always 8-byte aligned (Cortex-M7).
  278. Depending on where the exception came from (MSP or PSP), we need the right SP value to work with.
  279. ucb.vrs[7] contains the right value, so take it and align it by 8 bytes, store it as the current
  280. SP to work with (ucb.vrs[13]) which is then saved as the current (virtual) frame's SP.
  281. */
  282. uint32_t *stack;
  283. ucb.vrs[13] = (ucb.vrs[7] & ~7);
  284. /* If we need to start from the MSP, we need to go down X words to find the PC, where:
  285. X=2 if it was a non-floating-point exception
  286. X=20 if it was a floating-point (VFP) exception
  287. If we need to start from the PSP, we need to go up exactly 6 words to find the PC.
  288. See the ARMv7-M Architecture Reference Manual p.594 and Cortex-M7 Processor Programming Manual p.44/p.45 for details.
  289. */
  290. if ((ucb.vrs[15] & 0xc) == 0) {
  291. /* Return to Handler Mode: MSP (0xffffff-1) */
  292. stack = (uint32_t*)(ucb.vrs[13]);
  293. /* The PC is always 2 words down from the MSP, if it was a non-floating-point exception */
  294. stack -= 2;
  295. /* If there was a VFP exception (0xffffffe1), the PC is located another 18 words down */
  296. if ((ucb.vrs[15] & 0xf0) == 0xe0) {
  297. stack -= 18;
  298. }
  299. }
  300. else {
  301. /* Return to Thread Mode: PSP (0xffffff-d) */
  302. stack = read_psp();
  303. /* The PC is always 6 words up from the PSP */
  304. stack += 6;
  305. }
  306. /* Store the PC */
  307. ucb.vrs[15] = *stack--;
  308. /* Store the LR */
  309. ucb.vrs[14] = *stack--;
  310. }
  311. /* We are done if current frame pc is equal to the virtual pc, prevent infinite loop */
  312. if (frame->pc == ucb.vrs[15])
  313. return 0;
  314. /* Update the frame */
  315. frame->fp = ucb.vrs[7];
  316. frame->sp = ucb.vrs[13];
  317. frame->lr = ucb.vrs[14];
  318. frame->pc = ucb.vrs[15];
  319. /* All good */
  320. return 1;
  321. }
  322. // Detect if function names are available
  323. static int __attribute__ ((noinline)) has_function_names(void) {
  324. uint32_t flag_word = ((uint32_t*)&has_function_names)[-1];
  325. return ((flag_word & 0xff000000) == 0xff000000) ? 1 : 0;
  326. }
  327. // Detect if unwind information is present or not
  328. static int has_unwind_info(void) {
  329. return ((char*)(&__exidx_end) - (char*)(&__exidx_start)) > 16 ? 1 : 0; // 16 because there are default entries we can´t supress
  330. }
  331. int backtrace_dump(backtrace_frame_t *frame, backtrace_dump_fn_t dump_entry, void* ctx )
  332. {
  333. backtrace_t entry;
  334. int count = 1;
  335. /* If there is no unwind information, perform a RAW try at it. Idea was taken from
  336. * https://stackoverflow.com/questions/3398664/how-to-get-a-call-stack-backtrace-deeply-embedded-no-library-support
  337. *
  338. * And requires code to be compiled with the following flags:
  339. * -mtpcs-frame -mtpcs-leaf-frame -fno-omit-frame-pointer
  340. * With these options, the Stack pointer is automatically
  341. * pushed to the stack at the beginning of each function.
  342. */
  343. if (!has_unwind_info()) {
  344. /*
  345. * We basically iterate through the current stack finding the
  346. * following combination of values:
  347. * - <Frame Address>
  348. * - <Link Address>
  349. * This combination will occur for each function in the call stack
  350. */
  351. uint32_t previous_frame_address = (uint32_t)frame->sp;
  352. uint32_t* stack_pointer = (uint32_t*)frame->sp;
  353. // loop following stack frames
  354. while (1) {
  355. // Validate stack address
  356. if (validate_sp(stack_pointer))
  357. break;
  358. // Attempt to obtain next stack pointer
  359. // The link address should come immediately after
  360. const uint32_t possible_frame_address = *stack_pointer;
  361. const uint32_t possible_link_address = *(stack_pointer+1);
  362. // Next check that the frame addresss (i.e. stack pointer for the function)
  363. // and Link address are within an acceptable range
  364. if(possible_frame_address > previous_frame_address &&
  365. validate_sp((const void *)possible_frame_address) == 0 &&
  366. (possible_link_address & 1) != 0 && // in THUMB mode the address will be odd
  367. validate_pc((const void *)possible_link_address) == 0) {
  368. // We found two acceptable values.
  369. entry.name = "unknown";
  370. entry.address = (void*)possible_link_address;
  371. entry.function = 0;
  372. // If there are function names, try to solve name
  373. if (has_function_names()) {
  374. // Lets find the function name, if possible
  375. // Align address to 4 bytes
  376. uint32_t* pf = (uint32_t*) (((uint32_t)possible_link_address) & (-4));
  377. // Scan backwards until we find the function name
  378. while(validate_pc(pf-1) == 0) {
  379. // Get name descriptor value
  380. uint32_t v = pf[-1];
  381. // Check if name descriptor is valid and name is terminated in 0.
  382. if ((v & 0xffffff00) == 0xff000000 &&
  383. (v & 0xff) > 1) {
  384. // Assume the name was found!
  385. entry.name = ((const char*)pf) - 4 - (v & 0xff);
  386. entry.function = (void*)pf;
  387. break;
  388. }
  389. // Go backwards to the previous word
  390. --pf;
  391. }
  392. }
  393. dump_entry(count, &entry, ctx);
  394. ++count;
  395. // Update the book-keeping registers for the next search
  396. previous_frame_address = possible_frame_address;
  397. stack_pointer = (uint32_t*)(possible_frame_address + 4);
  398. } else {
  399. // Keep iterating through the stack until we find an acceptable combination
  400. ++stack_pointer;
  401. }
  402. }
  403. } else {
  404. /* Otherwise, unwind information is present. Use it to unwind frames */
  405. do {
  406. if (frame->pc == 0) {
  407. /* Reached __exidx_end. */
  408. entry.name = "<reached end of unwind table>";
  409. entry.address = 0;
  410. entry.function = 0;
  411. dump_entry(count, &entry, ctx);
  412. break;
  413. }
  414. if (frame->pc == 0x00000001) {
  415. /* Reached .cantunwind instruction. */
  416. entry.name = "<reached .cantunwind>";
  417. entry.address = 0;
  418. entry.function = 0;
  419. dump_entry(count, &entry, ctx);
  420. break;
  421. }
  422. /* Find the unwind index of the current frame pc */
  423. const unwind_index_t *index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
  424. /* Clear last bit (Thumb indicator) */
  425. frame->pc &= 0xfffffffeU;
  426. /* Generate the backtrace information */
  427. entry.address = (void *)frame->pc;
  428. entry.function = (void *)prel31_to_addr(&index->addr_offset);
  429. entry.name = unwind_get_function_name(entry.function);
  430. dump_entry(count, &entry, ctx);
  431. /* Next backtrace frame */
  432. ++count;
  433. } while (unwind_frame(frame) == 1);
  434. }
  435. /* All done */
  436. return count;
  437. }
  438. #endif