Commit 934cb76e

934cb76eab07Merged

Ori Pekelman <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

debug: native Exception#backtrace on Linux via -rdynamic + external linkage

glibc's backtrace_symbols only resolves frames whose symbols live in the dynamic symbol table, which static functions never reach. Three coupled changes bring Linux native backtraces to macOS parity (debug-only; non-debug output unchanged):

  • sp_runtime.h: sp_bt_symbol now parses the glibc "module(sym+0xoff) [addr]" form (empty symbol = unresolved -> skip) in addition to the macOS space-delimited form. Format auto-detected by the '(' delimiter.
  • spinel_codegen.rb: debug builds emit user methods with external linkage (was static) so their symbols can be exported.
  • spinel: debug builds link with -rdynamic on non-Darwin so backtrace_symbols can resolve the exported user-method symbols.

Verified on gx10 (aarch64, gcc): Foo#bar -> toplevel -> for a raise, and ZeroDivisionError correctly drops the sp_idiv runtime frame via the denylist. make test: 706 pass / 0 fail / 0 err.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
934cb76eab07
Parent
efa9a64231c9

lib/sp_runtime.hmodifiedView file

@@ -3362,20 +3362,34 @@
33623362 return 0;
33633363 }
33643364
3365-/* macOS backtrace_symbols line: "<idx> <image> <addr> <symbol> + <off>".
3366- Extract the symbol token; NULL if it isn't a keepable user frame. */
3365+/* Extract the symbol token from a backtrace_symbols line. Two formats:
3366+ - glibc/Linux: "<module>(<symbol>+0x<off>) [0x<addr>]". The symbol is
3367+ empty for unresolved frames (static or stripped fns) -> skip.
3368+ - macOS: "<idx> <image> <addr> <symbol> + <off>".
3369+ Returns NULL if it isn't a keepable user frame. Detect Linux by the '('
3370+ that delimits the symbol (the macOS format has none). */
33673371 static const char *sp_bt_symbol(const char *line) {
3368- const char *p = strstr(line, "0x"); /* the address */
3369- if (!p) return 0;
3370- while (*p && *p != ' ') p++; /* skip the address */
3371- while (*p == ' ') p++; /* to the symbol */
3372- if (!*p) return 0;
3373- const char *end = p;
3374- while (*end && *end != ' ') end++; /* symbol ends at space/" +" */
3375- size_t len = (size_t)(end - p);
3376- if (len == 0 || len > 250) return 0;
33773372 char sym[256];
3378- memcpy(sym, p, len); sym[len] = 0;
3373+ const char *lp = strchr(line, '(');
3374+ if (lp) { /* glibc/Linux paren form */
3375+ const char *p = lp + 1;
3376+ const char *end = p;
3377+ while (*end && *end != '+' && *end != ')') end++;
3378+ size_t len = (size_t)(end - p);
3379+ if (len == 0 || len > 250) return 0; /* unresolved (static/stripped) */
3380+ memcpy(sym, p, len); sym[len] = 0;
3381+ } else { /* macOS space-delimited form */
3382+ const char *p = strstr(line, "0x"); /* the address */
3383+ if (!p) return 0;
3384+ while (*p && *p != ' ') p++; /* skip the address */
3385+ while (*p == ' ') p++; /* to the symbol */
3386+ if (!*p) return 0;
3387+ const char *end = p;
3388+ while (*end && *end != ' ') end++; /* symbol ends at space/" +" */
3389+ size_t len = (size_t)(end - p);
3390+ if (len == 0 || len > 250) return 0;
3391+ memcpy(sym, p, len); sym[len] = 0;
3392+ }
33793393 if (strcmp(sym, "main") == 0) return strdup("<main>");
33803394 if (strncmp(sym, "sp_", 3) != 0) return 0; /* skip non-Spinel frames */
33813395 const char *name = sym + 3;