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
#endif}/* Kernel#caller(start=1, length=nil): the current call stack, dropping the first `start` frames (the running method itself at index 0, like CRuby's `caller` == `caller(1)`), then up to `length` frames. Method-granularity (no :lineno:), same substrate as Exception#backtrace; release builds (no frame symbols) return []. */static sp_StrArray *sp_caller(mrb_int start, mrb_bool have_len, mrb_int len) { sp_StrArray *full = sp_caller_now(); SP_GC_ROOT(full); sp_StrArray *r = sp_StrArray_new(); SP_GC_ROOT(r); mrb_int from = start < 0 ? 0 : start; mrb_int to = have_len ? from + (len < 0 ? 0 : len) : full->len; for (mrb_int i = from; i < to && i < full->len; i++) sp_StrArray_push(r, full->data[i]); return r;}SP_NORETURN SP_COLD void sp_raise_cls(const char *cls, const char *msg) {#if SP_BT_AVAILABLE if (sp_bt_enabled) sp_bt_n = backtrace(sp_bt_buf, 256);src/analyze_infer.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
test/kernel_caller_wired.rbaddedView file
Loading diff…
test/kernel_caller_wired.rb.expectedaddedView file
Loading diff…