Files
turso/scripts/stack/lib.sh
Jussi Saurio e74f903c2f scripts/stack: add tools to measure stack frames and stack use
Parsing and translating an expression recurse once per level of nesting,
so the frame size of those functions decides how deep an expression can
be on a given stack. There was no way to see those sizes or where the
stack goes at the deepest point.

- frame-sizes.sh reads the frame size of every function from its prologue
  (aarch64 and x86_64), and compares two binaries with -c.
- min-stack.sh finds the smallest main-thread stack that runs SQL scripts,
  per binary, with sqlite3 as a reference.
- stack-profile.sh runs a script under lldb with a stack just too small,
  so it overflows at its deepest point, and sums the stack bytes per
  function from the stack pointers of the backtrace.

stack-profile.sh starts the binary through a small C launcher that sets
the stack limit and then execs, because lldb's debug server cannot run
under a small `ulimit -s` and lldb cannot debug a system shell on macOS.
2026-10-05 15:52:51 +03:00

27 lines
1000 B
Bash

# Shared helpers for the scripts in scripts/stack/. Source this file; do not run it.
# Demangles Rust symbol names on stdin and drops their `::h<hash>` suffix.
# Uses rustfilt when it is installed, otherwise c++filt. c++filt leaves the
# escapes of the legacy Rust mangling in place, e.g. `_$u7b$$u7b$closure$u7d$$u7d$`
# for `{{closure}}`, so they are decoded here.
demangle_rust() {
if command -v rustfilt > /dev/null; then
rustfilt | strip_rust_hash
return
fi
if ! command -v c++filt > /dev/null; then
echo "error: need rustfilt or c++filt on PATH to demangle symbols" >&2
exit 1
fi
c++filt | sed -E \
-e 's/\$u7b\$/{/g' -e 's/\$u7d\$/}/g' -e 's/\$LT\$/</g' -e 's/\$GT\$/>/g' \
-e 's/\$u20\$/ /g' -e 's/\$u27\$/'"'"'/g' -e 's/\$u5b\$/[/g' -e 's/\$u5d\$/]/g' \
-e 's/\$RF\$/\&/g' -e 's/\$BP\$/*/g' -e 's/\$C\$/,/g' -e 's/\.\./::/g' \
-e 's/::_\{/::{/g' -e 's/(^|\t)_</\1</' \
| strip_rust_hash
}
strip_rust_hash() {
sed -E 's/::h[0-9a-f]{16}$//'
}