Skip to content

Commit d8c2c6f

Browse files
authored
Merge pull request #11332 from andrewc-arm/pr_mbed_error02
mbed_error.c: Better HW fault exceptions and stack dump
2 parents 87cdef9 + c257c5f commit d8c2c6f

File tree

3 files changed

+125
-9
lines changed

3 files changed

+125
-9
lines changed

platform/mbed_lib.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
"value": null
9191
},
9292

93+
"stack-dump-enabled": {
94+
"macro_name": "MBED_STACK_DUMP_ENABLED",
95+
"help": "Set to true to enable stack dump.",
96+
"value": false
97+
},
98+
9399
"cpu-stats-enabled": {
94100
"macro_name": "MBED_CPU_STATS_ENABLED",
95101
"help": "Set to 1 to enable cpu stats. When enabled the function mbed_stats_cpu_get returns non-zero data. See mbed_stats.h for more information",

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: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "platform/mbed_interface.h"
2727
#include "platform/mbed_power_mgmt.h"
2828
#include "platform/mbed_stats.h"
29+
#include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h"
30+
#include "mbed_rtx.h"
2931
#ifdef MBED_CONF_RTOS_PRESENT
3032
#include "rtx_os.h"
3133
#endif
@@ -38,6 +40,10 @@
3840
#endif
3941
#include <inttypes.h>
4042

43+
#ifndef MAX
44+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
45+
#endif
46+
4147
#ifndef NDEBUG
4248
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) print_error_report(ctx, error_msg, error_filename, error_line)
4349
static void print_error_report(const mbed_error_ctx *ctx, const char *, const char *error_filename, int error_line);
@@ -132,6 +138,26 @@ WEAK MBED_NORETURN void error(const char *format, ...)
132138
mbed_halt_system();
133139
}
134140

141+
static inline bool mbed_error_is_hw_fault(mbed_error_status_t error_status)
142+
{
143+
return (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
144+
error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
145+
error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
146+
error_status == MBED_ERROR_HARDFAULT_EXCEPTION);
147+
}
148+
149+
static bool mbed_error_is_handler(const mbed_error_ctx *ctx)
150+
{
151+
bool is_handler = false;
152+
if (ctx && mbed_error_is_hw_fault(ctx->error_status)) {
153+
mbed_fault_context_t *mfc = (mbed_fault_context_t *)ctx->error_value;
154+
if (mfc && !(mfc->EXC_RETURN & 0x8)) {
155+
is_handler = true;
156+
}
157+
}
158+
return is_handler;
159+
}
160+
135161
//Set an error status with the error handling system
136162
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)
137163
{
@@ -146,18 +172,29 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
146172

147173
//Clear the context capturing buffer
148174
memset(&current_error_ctx, 0, sizeof(mbed_error_ctx));
175+
149176
//Capture error information
150177
current_error_ctx.error_status = error_status;
151-
current_error_ctx.error_address = (uint32_t)caller;
152178
current_error_ctx.error_value = error_value;
179+
mbed_fault_context_t *mfc = NULL;
180+
if (mbed_error_is_hw_fault(error_status)) {
181+
mfc = (mbed_fault_context_t *)error_value;
182+
current_error_ctx.error_address = (uint32_t)mfc->PC_reg;
183+
// Note that this SP_reg is the correct SP value of the fault. PSP and MSP are slightly different due to HardFault_Handler.
184+
current_error_ctx.thread_current_sp = (uint32_t)mfc->SP_reg;
185+
// Note that the RTX thread itself is the same even under this fault exception handler.
186+
} else {
187+
current_error_ctx.error_address = (uint32_t)caller;
188+
current_error_ctx.thread_current_sp = (uint32_t)&current_error_ctx; // Address local variable to get a stack pointer
189+
}
190+
153191
#ifdef MBED_CONF_RTOS_PRESENT
154-
//Capture thread info
192+
// Capture thread info in thread mode
155193
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
156194
current_error_ctx.thread_id = (uint32_t)current_thread;
157195
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
158196
current_error_ctx.thread_stack_size = current_thread->stack_size;
159197
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
161198
#endif //MBED_CONF_RTOS_PRESENT
162199

163200
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
@@ -440,16 +477,83 @@ mbed_error_status_t mbed_clear_all_errors(void)
440477
return status;
441478
}
442479

443-
static inline const char *name_or_unnamed(const char *name)
480+
#ifdef MBED_CONF_RTOS_PRESENT
481+
static inline const char *name_or_unnamed(const osRtxThread_t *thread)
482+
{
483+
const char *unnamed = "<unnamed>";
484+
485+
if (!thread) {
486+
return unnamed;
487+
}
488+
489+
const char *name = thread->name;
490+
return name ? name : unnamed;
491+
}
492+
#endif // MBED_CONF_RTOS_PRESENT
493+
494+
#if MBED_STACK_DUMP_ENABLED
495+
/** Prints stack dump from given stack information.
496+
* The arguments should be given in address raw value to check alignment.
497+
* @param stack_start The address of stack start.
498+
* @param stack_size The size of stack
499+
* @param stack_sp The stack pointer currently at. */
500+
static void print_stack_dump_core(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp, const char *postfix)
501+
{
502+
#if MBED_STACK_DUMP_ENABLED
503+
#define STACK_DUMP_WIDTH 8
504+
#define INT_ALIGN_MASK (~(sizeof(int) - 1))
505+
mbed_error_printf("\nStack Dump: %s", postfix);
506+
uint32_t st_end = (stack_start + stack_size) & INT_ALIGN_MASK;
507+
uint32_t st = (stack_sp) & INT_ALIGN_MASK;
508+
for (; st < st_end; st += sizeof(int) * STACK_DUMP_WIDTH) {
509+
mbed_error_printf("\n0x%08" PRIX32 ":", st);
510+
for (int i = 0; i < STACK_DUMP_WIDTH; i++) {
511+
uint32_t st_cur = st + i * sizeof(int);
512+
if (st_cur >= st_end) {
513+
break;
514+
}
515+
uint32_t st_val = *((uint32_t *)st_cur);
516+
mbed_error_printf("0x%08" PRIX32 " ", st_val);
517+
}
518+
}
519+
mbed_error_printf("\n");
520+
#endif // MBED_STACK_DUMP_ENABLED
521+
}
522+
523+
static void print_stack_dump(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp, const mbed_error_ctx *ctx)
444524
{
445-
return name ? name : "<unnamed>";
525+
if (ctx && mbed_error_is_handler(ctx)) {
526+
// Stack dump extra for handler stack which may have accessed MSP.
527+
mbed_fault_context_t *mfc = (mbed_fault_context_t *)ctx->error_value;
528+
uint32_t msp_sp = mfc->MSP;
529+
uint32_t psp_sp = mfc->PSP;
530+
if (mfc && !(mfc->EXC_RETURN & 0x4)) {
531+
// MSP mode. Then SP_reg is more correct.
532+
msp_sp = mfc->SP_reg;
533+
} else {
534+
// PSP mode. Then SP_reg is more correct.
535+
psp_sp = mfc->SP_reg;
536+
}
537+
uint32_t msp_size = MAX(0, (int)INITIAL_SP - (int)msp_sp);
538+
print_stack_dump_core(msp_sp, msp_size, msp_sp, "MSP");
539+
540+
stack_sp = psp_sp;
541+
}
542+
543+
print_stack_dump_core(stack_start, stack_size, stack_sp, "PSP");
446544
}
545+
#endif // MBED_STACK_DUMP_ENABLED
447546

448547
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
449548
/* Prints info of a thread(using osRtxThread_t struct)*/
450549
static void print_thread(const osRtxThread_t *thread)
451550
{
452-
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->name), thread->state, thread->thread_addr, thread->stack_size, (uint32_t)thread->stack_mem, thread->sp);
551+
uint32_t stack_mem = (uint32_t)thread->stack_mem;
552+
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);
553+
554+
#if MBED_STACK_DUMP_ENABLED
555+
print_stack_dump(stack_mem, thread->stack_size, thread->sp, NULL);
556+
#endif
453557
}
454558

455559
/* Prints thread info from a list */
@@ -532,11 +636,16 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
532636

533637
mbed_error_printf("\nError Value: 0x%" PRIX32, ctx->error_value);
534638
#ifdef MBED_CONF_RTOS_PRESENT
535-
mbed_error_printf("\nCurrent Thread: %s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ",
536-
name_or_unnamed(((osRtxThread_t *)ctx->thread_id)->name),
639+
bool is_handler = mbed_error_is_handler(ctx);
640+
mbed_error_printf("\nCurrent Thread: %s%s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ",
641+
name_or_unnamed((osRtxThread_t *)ctx->thread_id), is_handler ? " <handler>" : "",
537642
ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp);
538643
#endif
539644

645+
#if MBED_STACK_DUMP_ENABLED
646+
print_stack_dump(ctx->thread_stack_mem, ctx->thread_stack_size, ctx->thread_current_sp, ctx);
647+
#endif
648+
540649
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
541650
mbed_error_printf("\nNext:");
542651
print_thread(osRtxInfo.thread.run.next);
@@ -557,6 +666,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
557666
mbed_stats_sys_get(&sys_stats);
558667
mbed_error_printf("\nFor more info, visit: https://mbed.com/s/error?error=0x%08X&osver=%" PRId32 "&core=0x%08" PRIX32 "&comp=%d&ver=%" PRIu32 "&tgt=" GET_TARGET_NAME(TARGET_NAME), ctx->error_status, sys_stats.os_version, sys_stats.cpu_id, sys_stats.compiler_id, sys_stats.compiler_version);
559668
#endif
669+
560670
mbed_error_printf("\n-- MbedOS Error Info --\n");
561671
}
562672
#endif //ifndef NDEBUG

0 commit comments

Comments
 (0)