How to debug IACCESS violation memfault in Arm CortexM

Viewed 28

When the PC value gets corrupted due to any reason, there can be an IACCESS fault. How can we find out which was the previous instruction (address of previous instruction) being executed before the fault occurred? Because, by the time the fault is caught, the PC is already containing some invalid value. How to know the location in code which caused the fault?

1 Answers

In my experience PC corruption is the hardest of the "common" faults to debug, for this exact reason.

By far the easiest way in the long run is to use tracing, but this can be nontrivial to set up depending on which device you're using and your hardware and software setup. It can even be impossible - not all devices support it, though all Cortex-M devices that I've used do (I think). A trace would allow you to dump out recent PC values following the fault. If you're doing any significant amount of development work on this platform, I would recommend setting up tracing as a worthwhile time investment.

Failing that, generally the best you can do is some sleuthing on the stack. In particular, on entry to handler mode the value of LR will have been stacked; this will usually tell you where the last call (BL) was issued, which can give you a clue. You can then set breakpoints and try to narrow it down. It's not a foolproof technique though.

Related