From 9af7ea58d45811cdace202b0223ac1aa598d1b7a Mon Sep 17 00:00:00 2001 From: Andrew Chong Date: Mon, 26 Aug 2019 15:35:42 +0800 Subject: [PATCH] The candidate code to enable correct crash report for HW fault crashes. --- .../TARGET_CORTEX_M/mbed_fault_handler.c | 2 +- platform/source/mbed_error.c | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/platform/source/TARGET_CORTEX_M/mbed_fault_handler.c b/platform/source/TARGET_CORTEX_M/mbed_fault_handler.c index 819edb228d..615842fc6f 100644 --- a/platform/source/TARGET_CORTEX_M/mbed_fault_handler.c +++ b/platform/source/TARGET_CORTEX_M/mbed_fault_handler.c @@ -76,7 +76,7 @@ void mbed_fault_handler(uint32_t fault_type, const mbed_fault_context_t *mbed_fa mbed_error_printf("\n\n-- MbedOS Fault Handler --\n\n"); //Now call mbed_error, to log the error and halt the system - mbed_error(faultStatus, "Fault exception", mbed_fault_context->PC_reg, NULL, 0); + mbed_error(faultStatus, "Fault exception", (unsigned int)mbed_fault_context_in, NULL, 0); } diff --git a/platform/source/mbed_error.c b/platform/source/mbed_error.c index c00c86a550..c44bacb872 100644 --- a/platform/source/mbed_error.c +++ b/platform/source/mbed_error.c @@ -26,6 +26,9 @@ #include "platform/mbed_interface.h" #include "platform/mbed_power_mgmt.h" #include "platform/mbed_stats.h" +#if defined(__CORTEX_M) +#include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h" +#endif #ifdef MBED_CONF_RTOS_PRESENT #include "rtx_os.h" #endif @@ -146,10 +149,30 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign //Clear the context capturing buffer memset(¤t_error_ctx, 0, sizeof(mbed_error_ctx)); + //Capture error information current_error_ctx.error_status = error_status; - current_error_ctx.error_address = (uint32_t)caller; current_error_ctx.error_value = error_value; + if (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION || + error_status == MBED_ERROR_BUSFAULT_EXCEPTION || + error_status == MBED_ERROR_USAGEFAULT_EXCEPTION || + error_status == MBED_ERROR_HARDFAULT_EXCEPTION) + { +#if defined(__CORTEX_M) + mbed_fault_context_t *mfc = (mbed_fault_context_t*)error_value; + current_error_ctx.error_address = (uint32_t)mfc->PC_reg; + current_error_ctx.thread_current_sp = (uint32_t)mfc->SP_reg; + // Note that the RTX thread itself is same even under fault exception handlers. +#else +#warning Please implement non Cortex-M handler for those error cases. +#endif + } + else + { + current_error_ctx.error_address = (uint32_t)caller; + current_error_ctx.thread_current_sp = (uint32_t)¤t_error_ctx; // Address local variable to get a stack pointer + } + #ifdef MBED_CONF_RTOS_PRESENT //Capture thread info osRtxThread_t *current_thread = osRtxInfo.thread.run.curr; @@ -157,7 +180,6 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr; current_error_ctx.thread_stack_size = current_thread->stack_size; current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem; - current_error_ctx.thread_current_sp = (uint32_t)¤t_error_ctx; // Address local variable to get a stack pointer #endif //MBED_CONF_RTOS_PRESENT #if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED