Skip to content

Commit 0da589e

Browse files
committed
mbed_error.c: handler mode failures will dump MSP/PSP stacks.
1 parent da2f8e5 commit 0da589e

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

platform/source/mbed_error.c

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,26 @@ WEAK MBED_NORETURN void error(const char *format, ...)
134134
mbed_halt_system();
135135
}
136136

137+
static inline bool mbed_error_is_hw_fault(mbed_error_status_t error_status)
138+
{
139+
return (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
140+
error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
141+
error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
142+
error_status == MBED_ERROR_HARDFAULT_EXCEPTION);
143+
}
144+
145+
static bool mbed_error_is_handler(const mbed_error_ctx *ctx)
146+
{
147+
bool is_handler = false;
148+
if (ctx && mbed_error_is_hw_fault(ctx->error_status)) {
149+
mbed_fault_context_t *mfc = (mbed_fault_context_t *)ctx->error_value;
150+
if (mfc && mfc->EXC_RETURN & 0x8) {
151+
is_handler = true;
152+
}
153+
}
154+
return is_handler;
155+
}
156+
137157
//Set an error status with the error handling system
138158
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number, void *caller)
139159
{
@@ -152,12 +172,8 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
152172
//Capture error information
153173
current_error_ctx.error_status = error_status;
154174
current_error_ctx.error_value = error_value;
155-
bool bHWException = (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
156-
error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
157-
error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
158-
error_status == MBED_ERROR_HARDFAULT_EXCEPTION);
159175
mbed_fault_context_t *mfc = NULL;
160-
if (bHWException) {
176+
if (mbed_error_is_hw_fault(error_status)) {
161177
mfc = (mbed_fault_context_t *)error_value;
162178
current_error_ctx.error_address = (uint32_t)mfc->PC_reg;
163179
// Note that this SP_reg is the correct SP value of the fault. PSP and MSP are slightly different due to HardFault_Handler.
@@ -168,22 +184,14 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
168184
current_error_ctx.thread_current_sp = (uint32_t)&current_error_ctx; // Address local variable to get a stack pointer
169185
}
170186

171-
if (mfc && !(mfc->EXC_RETURN & 0x4)) {
172-
// handler mode
173-
current_error_ctx.thread_id = 0;
174-
current_error_ctx.thread_entry_address = 0;
175-
current_error_ctx.thread_stack_size = MAX(0, (int)INITIAL_SP - (int)current_error_ctx.thread_current_sp - (int)sizeof(int));
176-
current_error_ctx.thread_stack_mem = current_error_ctx.thread_current_sp;
177-
} else {
178187
#ifdef MBED_CONF_RTOS_PRESENT
179-
// Capture thread info in thread mode
180-
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
181-
current_error_ctx.thread_id = (uint32_t)current_thread;
182-
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
183-
current_error_ctx.thread_stack_size = current_thread->stack_size;
184-
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
188+
// Capture thread info in thread mode
189+
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
190+
current_error_ctx.thread_id = (uint32_t)current_thread;
191+
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
192+
current_error_ctx.thread_stack_size = current_thread->stack_size;
193+
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
185194
#endif //MBED_CONF_RTOS_PRESENT
186-
}
187195

188196
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
189197
//Capture filename/linenumber if provided
@@ -467,25 +475,27 @@ mbed_error_status_t mbed_clear_all_errors(void)
467475

468476
static inline const char *name_or_unnamed(const osRtxThread_t *thread)
469477
{
478+
const char *unnamed = "<unnamed>";
479+
470480
if (!thread) {
471-
return "<handler>";
481+
return unnamed;
472482
}
473483

474484
const char *name = thread->name;
475-
return name ? name : "<unnamed>";
485+
return name ? name : unnamed;
476486
}
477487

478488
/** Prints stack dump from given stack information.
479489
* The arguments should be given in address raw value to check alignment.
480490
* @param stack_start The address of stack start.
481491
* @param stack_size The size of stack
482492
* @param stack_sp The stack pointer currently at. */
483-
static void print_stack_dump(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp)
493+
static void print_stack_dump_core(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp, const char *postfix)
484494
{
485495
#if MBED_STACK_DUMP_ENABLED
486496
#define STACK_DUMP_WIDTH 8
487497
#define INT_ALIGN_MASK (~(sizeof(int) - 1))
488-
mbed_error_printf("\n\nStack Dump:");
498+
mbed_error_printf("\n\nStack Dump: %s", postfix);
489499
uint32_t st_end = (stack_start + stack_size) & INT_ALIGN_MASK;
490500
uint32_t st = (stack_sp) & INT_ALIGN_MASK;
491501
for (; st <= st_end; st += sizeof(int) * STACK_DUMP_WIDTH) {
@@ -503,14 +513,35 @@ static void print_stack_dump(uint32_t stack_start, uint32_t stack_size, uint32_t
503513
#endif // MBED_STACK_DUMP_ENABLED
504514
}
505515

516+
static void print_stack_dump(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp, const mbed_error_ctx *ctx)
517+
{
518+
#if MBED_STACK_DUMP_ENABLED
519+
if (ctx && mbed_error_is_handler(ctx)) {
520+
// Stack dump extra for handler stack which may have accessed MSP.
521+
mbed_fault_context_t *mfc = (mbed_fault_context_t *)ctx->error_value;
522+
uint32_t msp_sp = mfc->MSP;
523+
if (mfc && !(mfc->EXC_RETURN & 0x4)) {
524+
// MSP mode. Then SP_reg is more correct.
525+
msp_sp = mfc->SP_reg;
526+
}
527+
// Do not access beyond INITIAL_SP.
528+
uint32_t msp_size = MAX(0, (int)INITIAL_SP - (int)msp_sp - (int)sizeof(int));
529+
print_stack_dump_core(msp_sp, msp_size, msp_sp, "MSP");
530+
}
531+
532+
// Handler thread may have accessed PSP.
533+
print_stack_dump_core(stack_start, stack_size, stack_sp, "PSP");
534+
#endif // MBED_STACK_DUMP_ENABLED
535+
}
536+
506537
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
507538
/* Prints info of a thread(using osRtxThread_t struct)*/
508539
static void print_thread(const osRtxThread_t *thread)
509540
{
510541
uint32_t stack_mem = (uint32_t)thread->stack_mem;
511542
mbed_error_printf("\n%s State: 0x%" PRIX8 " Entry: 0x%08" PRIX32 " Stack Size: 0x%08" PRIX32 " Mem: 0x%08" PRIX32 " SP: 0x%08" PRIX32, name_or_unnamed(thread), thread->state, thread->thread_addr, thread->stack_size, stack_mem, thread->sp);
512543

513-
print_stack_dump(stack_mem, thread->stack_size, thread->sp);
544+
print_stack_dump(stack_mem, thread->stack_size, thread->sp, NULL);
514545
}
515546

516547
/* Prints thread info from a list */
@@ -592,13 +623,14 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
592623
}
593624

594625
mbed_error_printf("\nError Value: 0x%" PRIX32, ctx->error_value);
626+
bool is_handler = mbed_error_is_handler(ctx);
595627
#ifdef MBED_CONF_RTOS_PRESENT
596-
mbed_error_printf("\nCurrent Thread: %s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ",
597-
name_or_unnamed((osRtxThread_t *)ctx->thread_id),
628+
mbed_error_printf("\nCurrent Thread: %s%s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ",
629+
name_or_unnamed((osRtxThread_t *)ctx->thread_id), is_handler ? " <handler>" : "",
598630
ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp);
599631
#endif
600632

601-
print_stack_dump(ctx->thread_stack_mem, ctx->thread_stack_size, ctx->thread_current_sp);
633+
print_stack_dump(ctx->thread_stack_mem, ctx->thread_stack_size, ctx->thread_current_sp, ctx);
602634

603635
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
604636
mbed_error_printf("\nNext:");

0 commit comments

Comments
 (0)