Skip to content

Commit 51964b3

Browse files
committed
mbed_error.c: Now we can stack dump on all possible threads.
1 parent 3cb6dfd commit 51964b3

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

platform/source/mbed_error.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
173173
// handler mode
174174
current_error_ctx.thread_id = 0;
175175
current_error_ctx.thread_entry_address = 0;
176-
current_error_ctx.thread_stack_size = MAX(0, INITIAL_SP - current_error_ctx.thread_current_sp - sizeof(int));
176+
current_error_ctx.thread_stack_size = MAX(0, (int)INITIAL_SP - (int)current_error_ctx.thread_current_sp - (int)sizeof(int));
177177
current_error_ctx.thread_stack_mem = current_error_ctx.thread_current_sp;
178178
} else {
179179
// Capture thread info in thread mode
@@ -475,11 +475,42 @@ static inline const char *name_or_unnamed(const osRtxThread_t *thread)
475475
return name ? name : "<unnamed>";
476476
}
477477

478+
/** Prints stack dump from given stack information.
479+
* The arguments should be given in address raw value to check alignment.
480+
* @param stack_start The address of stack start.
481+
* @param stack_size The size of stack
482+
* @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)
484+
{
485+
#if MBED_STACK_DUMP_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
486+
#define STACK_DUMP_WIDTH 8
487+
#define INT_ALIGN_MASK (~(sizeof(int) - 1))
488+
mbed_error_printf("\n\nStack Dump:");
489+
uint32_t st_end = (stack_start + stack_size) & INT_ALIGN_MASK;
490+
uint32_t st = (stack_sp) & INT_ALIGN_MASK;
491+
for (; st <= st_end; st += sizeof(int) * STACK_DUMP_WIDTH) {
492+
mbed_error_printf("\n0x%08" PRIX32 ":", st);
493+
for (int i = 0; i < STACK_DUMP_WIDTH; i++) {
494+
uint32_t st_cur = st + i * sizeof(int);
495+
if (st_cur > st_end) {
496+
break;
497+
}
498+
uint32_t st_val = *((uint32_t *)st_cur);
499+
mbed_error_printf("0x%08" PRIX32 " ", st_val);
500+
}
501+
}
502+
mbed_error_printf("\n");
503+
#endif // MBED_STACK_DUMP_ENABLED
504+
}
505+
478506
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
479507
/* Prints info of a thread(using osRtxThread_t struct)*/
480508
static void print_thread(const osRtxThread_t *thread)
481509
{
482-
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, (uint32_t)thread->stack_mem, thread->sp);
510+
uint32_t stack_mem = (uint32_t)thread->stack_mem;
511+
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);
512+
513+
print_stack_dump(stack_mem, thread->stack_size, thread->sp);
483514
}
484515

485516
/* Prints thread info from a list */
@@ -588,25 +619,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
588619
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);
589620
#endif
590621

591-
#if MBED_STACK_DUMP_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
592-
#define STACK_DUMP_WIDTH 8
593-
#define INT_ALIGN_MASK (~(sizeof(int) - 1))
594-
mbed_error_printf("\n\nStack Dump:");
595-
uint32_t st_end = (ctx->thread_stack_mem + ctx->thread_stack_size) & INT_ALIGN_MASK;
596-
uint32_t st = (ctx->thread_current_sp) & INT_ALIGN_MASK;
597-
for (; st <= st_end; st += sizeof(int) * STACK_DUMP_WIDTH) {
598-
mbed_error_printf("\n0x%08" PRIX32 ":", st);
599-
for (int i = 0; i < STACK_DUMP_WIDTH; i++) {
600-
uint32_t st_cur = st + i * sizeof(int);
601-
if (st_cur > st_end) {
602-
break;
603-
}
604-
uint32_t st_val = *((uint32_t *)st_cur);
605-
mbed_error_printf("0x%08" PRIX32 " ", st_val);
606-
}
607-
}
608-
mbed_error_printf("\n");
609-
#endif // MBED_STACK_DUMP_ENABLED
622+
print_stack_dump(ctx->thread_stack_mem, ctx->thread_stack_size, ctx->thread_current_sp);
610623

611624
mbed_error_printf("\n-- MbedOS Error Info --\n");
612625
}

0 commit comments

Comments
 (0)