Rust embedded stack analyzer question

Continuing the discussion from Using linker-defined symbols in `const` context as integers:

I'm exploring development of Rust embedded code (a crate), which when invoked at any point in one's Rust code will scan (read-only) the current stack memory contents and returns the stack's high-water' mark (depth), and how much remaining available stack space in bytes exists beyond the current 'high-water' mark. My initial target is the STM32 family of ARM processors. As envisioned it relies upon some initial startup code filling the app's (currently) unused remaining stack ram-space with a distinct six byte repeating byte-pattern very early after the embedded Rust code launches following a hard reset or power-up. It allows an embedded Rust developer to assess how much stack space has actually been used (high-water mark), and correspondingly how much more free stack-space remains following the high-water mark. May require a small amount of assembly or 'c' FFI code. Will likely only be plausible/useful for embedded boards with at least 32K of SRAM ... Currently I can only ascertain the board's stack region (high/low address) from the Rust link map, so I've linked this post to an associated post which asked how to use link map symbols programmatically. I'm hoping that someone might have some useful tips or information or suggestions (and not just on the link map aspect). Thanks in advance!

You may be able to use a linker script to declare your stack boundaries. This gives you global symbols that can be referenced with extern blocks. [1]

There is also the nightly-only -Z emit-stack-sizes which is used by cargo-call-stack. (Its associated blog post is also very informative. EDIT: Changed link to Internet Archive, since the live blog omits the call graph images.)


  1. The panic-persist project demonstrates how to create global symbols in a linker script. Along with the extern block in its implementation. â†Šī¸Ž

1 Like

Excellent. I'll check these references out. Cheers.