From f91493c97e20eef1d39a9cdc6c42a25cfd511734 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 23 Jul 2017 15:32:05 -0400 Subject: Measure and report maximum stack usage. (#175) Add max stack usage tracking, visible via debug module ustack. Add separate cpp flag for enabling modules: MICROPY_DEBUG_MODULES --- py/stackctrl.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'py/stackctrl.c') diff --git a/py/stackctrl.c b/py/stackctrl.c index d9969b56c..202ae3d48 100644 --- a/py/stackctrl.c +++ b/py/stackctrl.c @@ -66,3 +66,28 @@ void mp_stack_check(void) { } #endif // MICROPY_STACK_CHECK + +#if MICROPY_MAX_STACK_USAGE + +// Fill stack space with this unusual value. +const char MP_MAX_STACK_USAGE_SENTINEL_BYTE = 0xEE; + +// Record absolute bottom (logical limit) of stack. +void mp_stack_set_bottom(void* stack_bottom) { + MP_STATE_THREAD(stack_bottom) = stack_bottom; +} + +// Fill stack space down toward the stack limit with a known unusual value. +void mp_stack_fill_with_sentinel(void) { + // Force routine to not be inlined. Better guarantee than MP_NOINLINE for -flto. + __asm volatile (""); + volatile char* volatile p; + // Start filling stack just below the last variable in the current stack frame, which is p. + // Continue until we've hit the bottom of the stack (lowest address, logical "ceiling" of stack). + p = (char *) (&p - 1); + while(p >= MP_STATE_THREAD(stack_bottom)) { + *p-- = MP_MAX_STACK_USAGE_SENTINEL_BYTE; + } +} + +#endif // MICROPY_MAX_STACK_USAGE -- cgit v1.2.3