Commit b84f2ca6

b84f2ca6ba06Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +2 added · ~6 modified

Description

runtime: wire Kernel#caller to the backtrace substrate

caller / caller(start) / caller(start, len) now dispatch to a new sp_caller() over sp_caller_now() (the same frame capture + sp_bt_symbol demangle as the backtrace path), inferred as a String array. Bare caller is caller(1) -- the stack above the running method -- and the (start, length) window matches CRuby's frame structure. Method- granularity (no :lineno: yet -- the shared follow-up with #1300); release builds return [], same as backtrace.

Two substrate gaps had to be closed for the frames to actually resolve:

  • sp_bt_enabled was never set (the comment claimed "set by debug-build main()" but nothing emitted it), so the whole capture path returned empty. The generated main() now sets sp_bt_enabled / sp_bt_srcfile when SPINEL_DEBUG is on (release leaves it 0 -> empty arrays, as designed).
  • User methods are static, so -rdynamic never exposed them to backtrace_symbols and every frame demangled to nothing. In a debug build, instance/class methods now get external linkage (g_debug); toplevel sp_ stays static to avoid colliding with runtime helpers. Release builds are byte-identical (g_debug = 0).

Verified end-to-end in --debug: caller / caller(0) / caller(1,1) across A#a -> B#b -> C#c match CRuby's frame structure (modulo the deferred line column); no symbol collisions in a large multi-class build.

Fixes #1335

Details

Commit
b84f2ca6ba06
Parent
c97227b87e68

lib/sp_runtime.hmodifiedView file

@@ -4634,6 +4634,22 @@
46344634 #endif
46354635 }
46364636
4637+/* Kernel#caller(start=1, length=nil): the current call stack, dropping the
4638+ first `start` frames (the running method itself at index 0, like CRuby's
4639+ `caller` == `caller(1)`), then up to `length` frames. Method-granularity
4640+ (no :lineno:), same substrate as Exception#backtrace; release builds (no
4641+ frame symbols) return []. */
4642+static sp_StrArray *sp_caller(mrb_int start, mrb_bool have_len, mrb_int len) {
4643+ sp_StrArray *full = sp_caller_now();
4644+ SP_GC_ROOT(full);
4645+ sp_StrArray *r = sp_StrArray_new();
4646+ SP_GC_ROOT(r);
4647+ mrb_int from = start < 0 ? 0 : start;
4648+ mrb_int to = have_len ? from + (len < 0 ? 0 : len) : full->len;
4649+ for (mrb_int i = from; i < to && i < full->len; i++) sp_StrArray_push(r, full->data[i]);
4650+ return r;
4651+}
4652+
46374653 SP_NORETURN SP_COLD void sp_raise_cls(const char *cls, const char *msg) {
46384654 #if SP_BT_AVAILABLE
46394655 if (sp_bt_enabled) sp_bt_n = backtrace(sp_bt_buf, 256);