Commit b861755e
b861755e4333Merged
Ori Pekelman <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
debug: class-method backtrace frames + null-receiver guard (spinel-dev#2, #3)
Two opt-in (--debug-gated) improvements; release output byte-for-byte unchanged.
#2 backtrace cosmetics:
- sp_bt_symbol now de-mangles correctly: a class path is CamelCase segments
joined from
::, the method is the first lowercase segment, and aclssegment marks a singleton. Sosp_Helper_cls_boom->Helper.boom(wasHelper#cls_boom) andsp_Tep_Url_parse_query->Tep::Url#parse_query. - class/singleton methods are emitted with external linkage in debug (were
always
static, so -rdynamic couldn't export them and they vanished from backtraces). Now they appear AND render asClass.method.
#3 null-receiver guard:
- instance methods get a debug-only prologue
if (!self) sp_raise_cls( "NoMethodError", "undefined method '<m>' for nil"), so a method call on a nil heap receiver faults loudly with the Ruby error instead of a silent NULL deref deep in the callee — the matz/spinel#1259 silent-crash class.
Verified: Helper.boom -> Helper.risky -> top -> <main> for a class-method
raise; Calc#helper -> Calc#compute -> <main> for instance; a null Widget
receiver raises "undefined method 'show' for nil" under --debug. make test: 775
pass / 0 fail / 0 err.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b861755e4333
- Parent
- 934cb76eab07
lib/sp_runtime.hmodifiedView file
@@ -3378,14 +3378,16 @@
33783378
size_t len = (size_t)(end - p);33793379
if (len == 0 || len > 250) return 0; /* unresolved (static/stripped) */33803380
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/" +" */3381+
} else { /* macOS: "<idx> <image> <addr> <symbol> + <off>" */3382+
/* The symbol is the token just before the " + <off>" delimiter. Parse3383+
backward from the last " + " rather than forward from "0x" — an image3384+
path containing "0x" (e.g. /path/0x_proj/bin) would otherwise misparse. */3385+
const char *plus = 0, *q = line;3386+
while ((q = strstr(q, " + ")) != 0) { plus = q; q += 3; }3387+
if (!plus) return 0;3388+
const char *end = plus;3389+
const char *p = end;3390+
while (p > line && p[-1] != ' ') p--; /* back up to the symbol's start */33893391
size_t len = (size_t)(end - p);33903392
if (len == 0 || len > 250) return 0;33913393
memcpy(sym, p, len); sym[len] = 0;@@ -3394,20 +3396,39 @@
33943396
if (strncmp(sym, "sp_", 3) != 0) return 0; /* skip non-Spinel frames */33953397
const char *name = sym + 3;33963398
if (sp_bt_is_runtime(name)) return 0;3397-
/* sp_Class_method -> Class#method (heuristic: split first '_' after an3398-
initial uppercase segment); else just the stripped name. */3399-
char out[256];3400-
if (name[0] >= 'A' && name[0] <= 'Z') {3401-
const char *u = strchr(name, '_');3402-
if (u) {3403-
size_t cl = (size_t)(u - name);3404-
memcpy(out, name, cl);3405-
out[cl] = '#';3406-
strcpy(out + cl + 1, u + 1);3407-
return strdup(out);3408-
}3399+
/* De-mangle sp_<Class>_<method> back to Ruby. A Spinel symbol is a path of3400+
CamelCase class segments (each from a `::`, joined by `_`) followed by the3401+
method; the method is the first segment that starts lowercase. A literal3402+
`cls` segment marks a singleton method:3403+
sp_Helper_cls_boom -> Helper.boom3404+
sp_Tep_Url_parse_query -> Tep::Url#parse_query3405+
sp_Tep_AuthOAuth2_cls_find -> Tep::AuthOAuth2.find3406+
sp_toplevel -> toplevel (top-level method, no class)3407+
(Method names stay sanitized — e.g. enabled? is enabled_p; reversing that3408+
needs the emitted name table, a separate refinement.) */3409+
const char *mstart = 0; /* first lowercase-starting segment = the method */3410+
for (const char *p = name; *p; p++) {3411+
int seg_start = (p == name) || (p[-1] == '_');3412+
if (seg_start && *p >= 'a' && *p <= 'z') { mstart = p; break; }3413+
}3414+
if (!mstart) return strdup(name); /* all-uppercase: leave as-is */3415+
if (mstart == name) { /* no class path: top-level */3416+
if (strncmp(name, "cls_", 4) == 0) return strdup(name + 4); /* top-level singleton */3417+
return strdup(name);3418+
}3419+
char out[256]; size_t o = 0;3420+
const char *meth; char sep;3421+
if (strncmp(mstart, "cls_", 4) == 0) { meth = mstart + 4; sep = '.'; } /* singleton */3422+
else { meth = mstart; sep = '#'; } /* instance */3423+
for (const char *p = name; p < mstart - 1 && o + 2 < sizeof(out); p++) {3424+
if (*p == '_') { out[o++] = ':'; out[o++] = ':'; } /* class-path `_` was a `::` */3425+
else out[o++] = *p;34093426
}3410-
return strdup(name);3427+
if (o + 1 < sizeof(out)) out[o++] = sep;3428+
size_t ml = strlen(meth);3429+
if (o + ml < sizeof(out)) { memcpy(out + o, meth, ml); o += ml; }3430+
out[o] = 0;3431+
return strdup(out);34113432
}34123433
34133434
static sp_StrArray *sp_bt_format(void **buf, int n) {@@ -3417,9 +3438,10 @@
34173438
if (!syms) return a;34183439
const char *src = (sp_bt_srcfile && sp_bt_srcfile[0]) ? sp_bt_srcfile : "(spinel)";34193440
for (int i = 0; i < n; i++) {3420-
const char *name = sp_bt_symbol(syms[i]);3441+
char *name = (char *)sp_bt_symbol(syms[i]); /* always strdup'd; free after use */34213442
if (!name) continue;34223443
sp_StrArray_push(a, sp_sprintf("%s:in `%s'", src, name));3444+
free(name);34233445
}34243446
free(syms);34253447
return a;spinelmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…