Panic hard fault debugging help

Hello,
I am getting panic hard fault after some time in my code. On the particle console, I can see that the device reports panic hard fault when it comes online. How do I write a callback function which will be called upon panic hard fault?

I see that the system firmware is doing this already like setting a variable in memory and reading it upon boot which publishes the panic hard fault message. I would like to write a call back which publishes a stack trace(or something similar) which tells why the hard fault happened. Any ideas on doing this?

Thanks
Dheeraj

Hard faults are evil. Any number of things can cause this from a div by zero to accessing something out of bounds or freeing an already freed pointer.

A few options.

You could throw in a few serial.print statements and log the activity as best as possible. That would require a logging device. You would see activity leading up to the crash.

There is live debugging that should point to the instant of the panic. Again, this would have to be attached to equipment at the time of the panic.

You might be able to use your idea to store a bit of stack trace in EEPROM that will survive a reset. On your next panic or reboot, you can check this region and send a message as you described. There isn’t a lot of room here.

That are my initial thoughts if I were to pursue a bug hunt.

Hello @cermak,
I thought of using EEPROM. But I need a callback function which occurs when a hard fault occurs. Particle console reports hard faults when the lamp comes online after a restart. I believe it is based on an EEPROM based approach. I am compiling against cloud so I can’t make any modifications to the system firmware.

Thanks
Dheeraj

These console messages are actually based on a flag stored in backup SRAM, not EEPROM.

You can have retained variables in SRAM too.

But there are no other callbacks officially available than the ones listed here
https://docs.particle.io/reference/firmware/photon/#system-events

1 Like