How to save stack when Cortex-M3 went into hardfault?

Using the following setting:

Using JLinkGDBServer and eclipse as the debugging interface, I always have a good stop trace when passing through my code. When using the FreeForms Code Confidence tools (eclipse plugin), I also see a stack stack of all threads that currently do not work (without this plugin I only see a stack from the active thread). So far so good.

But now, when my application gets into hardfault, stacktrace is lost. Well, I know a technique how to find out the address of a code that calls hardfault (as seen here ). But this is very bad information compared to the full stacktrace.

Well, several times when you get into hardfault, there is no way to save stacktrace, for example. when the stack is corrupted by a faulty code. But if the stack is healed, I think getting a stacktrace is possible (isn't it?).

I think the reason for the loss of stacktrace when in hardfault is that the stack pointer will be automatically copied from PSP to Cortex-M3 MSP architecture. One idea is to (possibly) set the MSP to the previous PSP value (and you may have to do additional additional stack preparation?).

, stacktrace, hardfault?

2015-07-07, .

, :

__attribute__((optimize("O0"))) static void checkHardfault() {
    volatile uint32_t* varAtOddAddress = (uint32_t*)-1;
    (*varAtOddAddress)++;
}

checkHardfault() :

gdb-> backtrace
#0  checkHardfault () at Main.cxx:179
#1  0x100360f6 in GetOneEvent () at Main.cxx:185
#2  0x1003604e in executeMainLoop () at Main.cxx:121
#3  0x1001783a in vMainTask (pvParameters=0x0) at Main.cxx:408
#4  0x00000000 in ?? ()

hardfault (at (*varAtOddAddress)++;) HardFault_Handler(), stacktrace:

gdb-> backtrace
#0  HardFault_Handler () at Hardfault.c:312
#1  <signal handler called>
#2  0x10015f36 in prvPortStartFirstTask () at freertos/portable/GCC/ARM_CM3/port.c:224
#3  0x10015fd6 in xPortStartScheduler () at freertos/portable/GCC/ARM_CM3/port.c:301
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
+4
1

- .

script, , LR, R0-R14 , .

, , - , . , - , , . , , (?) .

+1

All Articles