Skip to content

Enable stack dump #10872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions platform/mbed_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,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)) {
Copy link
Contributor

@TeroJaasko TeroJaasko Jun 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the current stack pointer verified somewhere before stored to context? It might be worth checking that it is within RAM at least to avoid faulting again here. Also the value should be checked for misalingment to avoid accessing data from non-32bit aligned addresses. A bit of caution on the data collected from crashed thread will not hurt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Good point. I will add this feature in the next candidate code for this PR.

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;
}
Copy link
Contributor

@TeroJaasko TeroJaasko Jun 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stack size calculation logic differs from RTX one: https://github.com/ARMmbed/mbed-os/blob/master/rtos/TARGET_CORTEX/rtx5/RTX/Source/rtx_thread.c#L901-L908. Why not just stop at the first non-osRtxStackFillPattern value? Nevermind, this makes more sense.

}
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
Expand Down
6 changes: 6 additions & 0 deletions platform/mbed_lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down