Files
turso/scripts/stack/stack-limit-exec.c
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

32 lines
939 B
C

// Runs a command with the stack size limit set to KIB kibibytes.
//
// Usage: stack-limit-exec KIB COMMAND [ARG]...
//
// stack-profile.sh starts this under lldb instead of setting `ulimit -s` in
// the shell that runs lldb: lldb's own debug server needs more stack than a
// small limit allows, and lldb cannot debug a system shell on macOS.
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <unistd.h>
int main(int argc, char **argv) {
if (argc < 3) {
fprintf(stderr, "usage: %s KIB COMMAND [ARG]...\n", argv[0]);
return 2;
}
struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) != 0) {
perror("getrlimit");
return 2;
}
limit.rlim_cur = (rlim_t)strtoull(argv[1], NULL, 10) * 1024;
if (setrlimit(RLIMIT_STACK, &limit) != 0) {
perror("setrlimit");
return 2;
}
execvp(argv[2], &argv[2]);
perror("execvp");
return 2;
}