Friday, August 5, 2022

GCC ARM Cortex M4 backtrace print from target

To do a more verbose crash logging from HardFault handler (or assert statement) the Backtrace library can be used: https://github.com/red-rocket-computing/backtrace

Don't forget to add -funwind-tables to compiler flags. For function names to be available -mpoke-function-name needs to be added as well (increases binary size!).

Backtrace only records function name and execution address but it can be easily extended to include the stack pointer information which can help to detect stack overflow related problems.

 

Additional resources to consider:

https://alexkalmuk.medium.com/how-stack-trace-on-arm-works-5634b35ddca1
https://maskray.me/blog/2020-11-08-stack-unwinding
https://github.com/ccoffing/airbag_fd/
https://github.com/bakerstu/openmrn/blob/62683863e8621cef35e94c9dcfe5abcaf996d7a2/src/freertos_drivers/common/cpu_profile.hxx#L162
https://stackoverflow.com/questions/6254058/how-to-get-fullstacktrace-using-unwind-backtrace-on-sigsegv
https://stackoverflow.com/questions/47331426/stack-backtrace-for-arm-core-using-gcc-compiler-when-there-is-a-msp-to-psp-swit
https://stackoverflow.com/questions/70652306/how-does-stack-unwinding-in-cortex-m-devices-works
https://stackoverflow.com/questions/59855643/unwind-backtrace-for-different-context-on-freertos
https://gcc.gnu.org/onlinedocs/gcc-10.3.0/gcc/ARM-Options.html#ARM-Options