Skip to content

Commit 095f205

Browse files
committed
platform.stack-dump-enabled feature is added.
1 parent 9ddd9bf commit 095f205

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

platform/mbed_error.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,6 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
534534
mbed_error_printf("\nCurrent Thread: %s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ",
535535
name_or_unnamed(((osRtxThread_t *)ctx->thread_id)->name),
536536
ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp);
537-
538-
#define STACK_END 0xCCCCCCCC
539-
mbed_error_printf("\nPrinting Stack");
540-
int stack_end_cnt = 0;
541-
for (uint32_t st = ctx->thread_current_sp; st >= ctx->thread_stack_mem; st -= sizeof(int))
542-
{
543-
uint32_t st_val = *((uint32_t*)st);
544-
mbed_error_printf("\n0x%" PRIX32 ": 0x%" PRIX32, st, st_val);
545-
if (st_val == STACK_END)
546-
{
547-
stack_end_cnt++;
548-
}
549-
else
550-
{
551-
stack_end_cnt = 0;
552-
}
553-
if (stack_end_cnt > 2)
554-
break;
555-
}
556537
#endif
557538

558539
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
@@ -575,6 +556,51 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
575556
mbed_stats_sys_get(&sys_stats);
576557
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);
577558
#endif
559+
560+
#if MBED_STACK_DUMP_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
561+
#define STACK_END_MARK 0xCCCCCCCC
562+
#define STACK_END_MARK_CNT 3
563+
#define STACK_DUMP_WIDTH 8
564+
mbed_error_printf("\n\nStack Dump:");
565+
// Find the stack end.
566+
int stack_end_cnt = 0;
567+
uint32_t st_end = ctx->thread_current_sp;
568+
for ( ; st_end >= ctx->thread_stack_mem; st_end -= sizeof(int))
569+
{
570+
uint32_t st_val = *((uint32_t*)st_end);
571+
if (st_val == STACK_END_MARK)
572+
{
573+
stack_end_cnt++;
574+
}
575+
else
576+
{
577+
stack_end_cnt = 0;
578+
}
579+
if (stack_end_cnt >= STACK_END_MARK_CNT)
580+
{
581+
st_end += (STACK_END_MARK_CNT - 1) * sizeof(int);
582+
break;
583+
}
584+
}
585+
for (uint32_t st = st_end; st <= ctx->thread_current_sp; st += sizeof(int) * STACK_DUMP_WIDTH)
586+
{
587+
mbed_error_printf("\n0x%08" PRIX32 ":", st);
588+
for (int i = 0; i < STACK_DUMP_WIDTH; i++)
589+
{
590+
uint32_t st_cur = st + i * sizeof(int);
591+
if (st_cur > ctx->thread_current_sp)
592+
break;
593+
uint32_t st_val = *((uint32_t*)st_cur);
594+
mbed_error_printf("0x%08" PRIX32 " ", st_val);
595+
if (st_val == STACK_END_MARK)
596+
stack_end_cnt++;
597+
else
598+
stack_end_cnt = 0;
599+
}
600+
}
601+
mbed_error_printf("\n");
602+
#endif // MBED_STACK_DUMP_ENABLED
603+
578604
mbed_error_printf("\n-- MbedOS Error Info --\n");
579605
}
580606
#endif //ifndef NDEBUG

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 1 to enable stack dump. Please note that the dump will be decending order.",
96+
"value": null
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",

0 commit comments

Comments
 (0)