Skip to content

Commit d0ca786

Browse files
committed
platform.stack-dump-enabled feature is added.
1 parent 979f26f commit d0ca786

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

platform/mbed_error.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,49 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
556556
mbed_stats_sys_get(&sys_stats);
557557
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);
558558
#endif
559+
560+
#if MBED_STACK_DUMP_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
561+
/** The internal threshold to detect the end of the stack.
562+
* The stack is filled with osRtxStackFillPattern at the end.
563+
* However, it is possible that the call stack parameters can theoretically have consecutive osRtxStackFillPattern instances.
564+
* For the best effort stack end detection, we will consider STACK_END_MARK_CNT consecutive osRtxStackFillPattern instances as the stack end. */
565+
#define STACK_END_MARK_CNT 3
566+
#define STACK_DUMP_WIDTH 8
567+
mbed_error_printf("\n\nStack Dump:");
568+
// Find the stack end.
569+
int stack_end_cnt = 0;
570+
uint32_t st_end = ctx->thread_current_sp;
571+
for (; st_end >= ctx->thread_stack_mem; st_end -= sizeof(int)) {
572+
uint32_t st_val = *((uint32_t *)st_end);
573+
if (st_val == osRtxStackFillPattern) {
574+
stack_end_cnt++;
575+
} else {
576+
stack_end_cnt = 0;
577+
}
578+
if (stack_end_cnt >= STACK_END_MARK_CNT) {
579+
st_end += (STACK_END_MARK_CNT - 1) * sizeof(int);
580+
break;
581+
}
582+
}
583+
for (uint32_t st = st_end; st <= ctx->thread_current_sp; st += sizeof(int) * STACK_DUMP_WIDTH) {
584+
mbed_error_printf("\n0x%08" PRIX32 ":", st);
585+
for (int i = 0; i < STACK_DUMP_WIDTH; i++) {
586+
uint32_t st_cur = st + i * sizeof(int);
587+
if (st_cur > ctx->thread_current_sp) {
588+
break;
589+
}
590+
uint32_t st_val = *((uint32_t *)st_cur);
591+
mbed_error_printf("0x%08" PRIX32 " ", st_val);
592+
if (st_val == osRtxStackFillPattern) {
593+
stack_end_cnt++;
594+
} else {
595+
stack_end_cnt = 0;
596+
}
597+
}
598+
}
599+
mbed_error_printf("\n");
600+
#endif // MBED_STACK_DUMP_ENABLED
601+
559602
mbed_error_printf("\n-- MbedOS Error Info --\n");
560603
}
561604
#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)