Skip to content

Commit b801a36

Browse files
committed
The candidate code to enable correct crash report for HW fault crashes.
1 parent b005bf2 commit b801a36

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

platform/source/TARGET_CORTEX_M/mbed_fault_handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void mbed_fault_handler(uint32_t fault_type, const mbed_fault_context_t *mbed_fa
7676
mbed_error_printf("\n\n-- MbedOS Fault Handler --\n\n");
7777

7878
//Now call mbed_error, to log the error and halt the system
79-
mbed_error(faultStatus, "Fault exception", mbed_fault_context->PC_reg, NULL, 0);
79+
mbed_error(faultStatus, "Fault exception", (unsigned int)mbed_fault_context_in, NULL, 0);
8080

8181
}
8282

platform/source/mbed_error.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include "platform/mbed_interface.h"
2727
#include "platform/mbed_power_mgmt.h"
2828
#include "platform/mbed_stats.h"
29+
#if defined(__CORTEX_M)
30+
#include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h"
31+
#endif
2932
#ifdef MBED_CONF_RTOS_PRESENT
3033
#include "rtx_os.h"
3134
#endif
@@ -146,18 +149,37 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
146149

147150
//Clear the context capturing buffer
148151
memset(&current_error_ctx, 0, sizeof(mbed_error_ctx));
152+
149153
//Capture error information
150154
current_error_ctx.error_status = error_status;
151-
current_error_ctx.error_address = (uint32_t)caller;
152155
current_error_ctx.error_value = error_value;
156+
if (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
157+
error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
158+
error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
159+
error_status == MBED_ERROR_HARDFAULT_EXCEPTION)
160+
{
161+
#if defined(__CORTEX_M)
162+
mbed_fault_context_t *mfc = (mbed_fault_context_t*)error_value;
163+
current_error_ctx.error_address = (uint32_t)mfc->PC_reg;
164+
current_error_ctx.thread_current_sp = (uint32_t)mfc->SP_reg;
165+
// Note that the RTX thread itself is same even under fault exception handlers.
166+
#else
167+
#warning Please implement non Cortex-M handler for those error cases.
168+
#endif
169+
}
170+
else
171+
{
172+
current_error_ctx.error_address = (uint32_t)caller;
173+
current_error_ctx.thread_current_sp = (uint32_t)&current_error_ctx; // Address local variable to get a stack pointer
174+
}
175+
153176
#ifdef MBED_CONF_RTOS_PRESENT
154177
//Capture thread info
155178
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
156179
current_error_ctx.thread_id = (uint32_t)current_thread;
157180
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
158181
current_error_ctx.thread_stack_size = current_thread->stack_size;
159182
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
160-
current_error_ctx.thread_current_sp = (uint32_t)&current_error_ctx; // Address local variable to get a stack pointer
161183
#endif //MBED_CONF_RTOS_PRESENT
162184

163185
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED

0 commit comments

Comments
 (0)