"It's Crasing", You Say?
Whenever a computer behaves unexpectedly, there is always a very logical explanation at the root of the issue - however frustrating this may be to hear. The "perfect" software world sits on top of a very imperfect_ physical world, and this starts to become more apparent as you work closer and closer with the hardware.
In this instance, we're running on an ARM Cortex-M0+, and the application is being presented with sporadic HardFault exceptions. Nothing we've done so far has been able to shed any light on the cause of this fault - trying to identify common situations, instrumentation (e.g: printf()), interactive debugging, code reviews, etc... all of these have fallen short of a solution.
Background
When the crash occurs, we see the following output on the serial console:
[00:11:46.346,000]os: ***** HARD FAULT ***** [00:11:46.352,000] os: r0/a1: 0x20002cf4 r1/a2: 0x00000000 r2/a3: 0x00000000 [00:11:46.360,000] os: r3/a4: 0x00000000 r12/ip: 0x0001310f r14/lr: 0x0001311b [00:11:46.369,000] os: xpsr: 0x41000000 [00:11:46.374,000] os: Faulting instruction address (r15/pc): 0x00005e70 [00:11:46.382,000] os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0 [00:11:46.390,000] os: Current thread: 0x200023b8 (idle) [00:11:46.396,000] os: Halting system
If you're concerned that the system only ran for ~11 minutes before faulting, that's not at all unreasonable - this system is currently being held together by the watchdog timer.
Using the above output in conjunction with zephyr.lst (which is generated when you build Zephyr), it's possible to identify exactly where in the application the fault is occurring... above it's at $pc = 0x5e70, which in that build is the isb instruction in Zephyr's idle thread[ref] ... It's important to know that the HardFault always occurs on this isb instruction, regardless of the individual exception or where exactly it's located in the binary. Faulting on an isb like this is odd, and frustratingly, there is virtually nothing else to go on.
A cleaned extract of zephyr.lst showing just the idle thread's loop looks like this:
arch_cpu_idle:
dsb sy
wfi
cpsie i
isb sy // <-- HardFault!
bx lr
idle:
mrs r3, PRIMASK
cpsid i
bl arch_cpu_idle
b.n idle
Stack Overflow?
With an issue like this, it's worth checking that you've not run out of space in the stack or heap - strange things can happen when memory is overwritten. This application doesn't use dynamic memory (thus no heap), but it does have multiple threads, each with their own stack - reviewing a memory dump taken after the fault shows that these are all in tact and well within bounds.
Zephyr can also be configured to keep track of the running threads, and produce output on the console describing the stack usage, CPU time, etc... Additionally, a stack sentinel can be configured, which will permit the scheduler to detect and report overflows.
The following configuration options may be useful if you're in the same situation, but they didn't help here.
STACK_SENTINEL- enable the stack sentinelIDLE_STACK_SIZE- set a custom idle thread stack sizeMAIN_STACK_SIZE- set a custom main thread stack sizeTHREAD_MONITOR- maintain a list of active threadsTHREAD_NAME- record thread namesTHREAD_ANALYZER- enable thread analyserTHREAD_ANALYZER_AUTO- automatically report thread infoTHREAD_ANALYZER_AUTO_INTERVAL- reporting intervalDEBUG_THREAD_INFO- add support for GDB's `info threads` command
Buffer Overflow?
The folks in the [Zephyr Community Discord Server](https://discord.com/invite/Ck7jw53nU2) were keen for me to investigate an overflow in the logging subsystem. I had a fiddle with the following config options, to no avail. After enabling `LOG_MODE_IMMEDIATE`, I was happy enough to rule out the logging subsystem - especially as the application isn't particularly verbose with its output.
LOG_BUFFER_SIZE- adjust the size of the internal bufferLOG_MODE_IMMEDIATE-LOG_*()calls are processed in full before they return
I also checked the various buffers within the application, and they all appeared to be operating well within bounds.