# Panic hard fault debugging help

**URL:** <https://community.particle.io/t/panic-hard-fault-debugging-help/34893>\
**Category:** Firmware\
**Created:** [July 27, 2017, 4:49pm UTC](https://community.particle.io/t/panic-hard-fault-debugging-help/34893 "2017-07-27T16:49:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dheerajdake](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@dheerajdake](https://community.particle.io/u/dheerajdake)\
**Post date:** [July 27, 2017, 4:49pm UTC](https://community.particle.io/t/panic-hard-fault-debugging-help/34893/1 "2017-07-27T16:49:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cermak](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/cermak/32/14174_2.png) [@cermak](https://community.particle.io/u/cermak)\
**Post date:** [August 12, 2017, 10:34pm UTC](https://community.particle.io/t/panic-hard-fault-debugging-help/34893/2 "2017-08-12T22:34:07Z")

</div>

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](https://docs.particle.io/faq/particle-tools/eclipse-debug/photon/) 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](https://docs.particle.io/reference/firmware/photon/#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.

---

<div class="post-metadata">

**Author:** ![dheerajdake](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@dheerajdake](https://community.particle.io/u/dheerajdake)\
**Post date:** [August 13, 2017, 6:15am UTC](https://community.particle.io/t/panic-hard-fault-debugging-help/34893/3 "2017-08-13T06:15:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 13, 2017, 7:08am UTC](https://community.particle.io/t/panic-hard-fault-debugging-help/34893/4 "2017-08-13T07:08:58Z")

</div>

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

You can have [`retained`](https://docs.particle.io/reference/firmware/photon/#backup-ram-sram-) 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](https://docs.particle.io/reference/firmware/photon/#system-events)
