summaryrefslogtreecommitdiff
path: root/py/stackctrl.c
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2017-07-23 15:32:05 -0400
committerScott Shawcroft <scott@tannewt.org>2017-07-23 15:32:05 -0400
commitf91493c97e20eef1d39a9cdc6c42a25cfd511734 (patch)
tree7e7062c9c12220d798cfa154e0729b7cf3e6447b /py/stackctrl.c
parentaf1ede930ba643d68850c541ef4075d5f7fd4f02 (diff)
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
Diffstat (limited to 'py/stackctrl.c')
-rw-r--r--py/stackctrl.c25
1 files changed, 25 insertions, 0 deletions
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