From 50daa7e40fb9533b4eb48f7a3b8831d62b4128fb Mon Sep 17 00:00:00 2001 From: Andrew Chong Date: Wed, 26 Jun 2019 15:28:19 +0800 Subject: [PATCH] platform.stack-dump-enabled feature is added. --- platform/mbed_lib.json | 6 ++++++ platform/source/mbed_error.c | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/platform/mbed_lib.json b/platform/mbed_lib.json index 0f5facfc43..7eb8ffddc2 100644 --- a/platform/mbed_lib.json +++ b/platform/mbed_lib.json @@ -90,6 +90,12 @@ "value": null }, + "stack-dump-enabled": { + "macro_name": "MBED_STACK_DUMP_ENABLED", + "help": "Set to 1 or true to enable stack dump.", + "value": null + }, + "cpu-stats-enabled": { "macro_name": "MBED_CPU_STATS_ENABLED", "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", diff --git a/platform/source/mbed_error.c b/platform/source/mbed_error.c index 7d99830e0d..cfe4559622 100644 --- a/platform/source/mbed_error.c +++ b/platform/source/mbed_error.c @@ -575,6 +575,44 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg, mbed_stats_sys_get(&sys_stats); 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); #endif + +#if MBED_STACK_DUMP_ENABLED && defined(MBED_CONF_RTOS_PRESENT) + /** The internal threshold to detect the end of the stack. + * The stack is filled with osRtxStackFillPattern at the end. + * However, it is possible that the call stack parameters can theoretically have consecutive osRtxStackFillPattern instances. + * For the best effort stack end detection, we will consider STACK_END_MARK_CNT consecutive osRtxStackFillPattern instances as the stack end. */ +#define STACK_END_MARK_CNT 3 +#define STACK_DUMP_WIDTH 8 + mbed_error_printf("\n\nStack Dump:"); + // Find the stack end. + int stack_end_cnt = 0; + uint32_t st_end = ctx->thread_current_sp; + for (; st_end >= ctx->thread_stack_mem; st_end -= sizeof(int)) { + uint32_t st_val = *((uint32_t *)st_end); + if (st_val == osRtxStackFillPattern) { + stack_end_cnt++; + } else { + stack_end_cnt = 0; + } + if (stack_end_cnt >= STACK_END_MARK_CNT) { + st_end += (STACK_END_MARK_CNT - 1) * sizeof(int); + break; + } + } + for (uint32_t st = st_end; st <= ctx->thread_current_sp; st += sizeof(int) * STACK_DUMP_WIDTH) { + mbed_error_printf("\n0x%08" PRIX32 ":", st); + for (int i = 0; i < STACK_DUMP_WIDTH; i++) { + uint32_t st_cur = st + i * sizeof(int); + if (st_cur > ctx->thread_current_sp) { + break; + } + uint32_t st_val = *((uint32_t *)st_cur); + mbed_error_printf("0x%08" PRIX32 " ", st_val); + } + } + mbed_error_printf("\n"); +#endif // MBED_STACK_DUMP_ENABLED + mbed_error_printf("\n-- MbedOS Error Info --\n"); } #endif //ifndef NDEBUG