diff options
Diffstat (limited to 'src/lj_debug.c')
-rw-r--r-- | src/lj_debug.c | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/src/lj_debug.c b/src/lj_debug.c index be7fb2b1..54f7db74 100644 --- a/src/lj_debug.c +++ b/src/lj_debug.c | |||
@@ -9,11 +9,12 @@ | |||
9 | #include "lj_obj.h" | 9 | #include "lj_obj.h" |
10 | #include "lj_err.h" | 10 | #include "lj_err.h" |
11 | #include "lj_debug.h" | 11 | #include "lj_debug.h" |
12 | #include "lj_str.h" | 12 | #include "lj_buf.h" |
13 | #include "lj_tab.h" | 13 | #include "lj_tab.h" |
14 | #include "lj_state.h" | 14 | #include "lj_state.h" |
15 | #include "lj_frame.h" | 15 | #include "lj_frame.h" |
16 | #include "lj_bc.h" | 16 | #include "lj_bc.h" |
17 | #include "lj_strfmt.h" | ||
17 | #if LJ_HASJIT | 18 | #if LJ_HASJIT |
18 | #include "lj_jit.h" | 19 | #include "lj_jit.h" |
19 | #endif | 20 | #endif |
@@ -133,20 +134,6 @@ static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe) | |||
133 | 134 | ||
134 | /* -- Variable names ------------------------------------------------------ */ | 135 | /* -- Variable names ------------------------------------------------------ */ |
135 | 136 | ||
136 | /* Read ULEB128 value. */ | ||
137 | static uint32_t debug_read_uleb128(const uint8_t **pp) | ||
138 | { | ||
139 | const uint8_t *p = *pp; | ||
140 | uint32_t v = *p++; | ||
141 | if (LJ_UNLIKELY(v >= 0x80)) { | ||
142 | int sh = 0; | ||
143 | v &= 0x7f; | ||
144 | do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80); | ||
145 | } | ||
146 | *pp = p; | ||
147 | return v; | ||
148 | } | ||
149 | |||
150 | /* Get name of a local variable from slot number and PC. */ | 137 | /* Get name of a local variable from slot number and PC. */ |
151 | static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) | 138 | static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) |
152 | { | 139 | { |
@@ -162,9 +149,9 @@ static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) | |||
162 | } else { | 149 | } else { |
163 | while (*p++) ; /* Skip over variable name string. */ | 150 | while (*p++) ; /* Skip over variable name string. */ |
164 | } | 151 | } |
165 | lastpc = startpc = lastpc + debug_read_uleb128(&p); | 152 | lastpc = startpc = lastpc + lj_buf_ruleb128((const char **)&p); |
166 | if (startpc > pc) break; | 153 | if (startpc > pc) break; |
167 | endpc = startpc + debug_read_uleb128(&p); | 154 | endpc = startpc + lj_buf_ruleb128((const char **)&p); |
168 | if (pc < endpc && slot-- == 0) { | 155 | if (pc < endpc && slot-- == 0) { |
169 | if (vn < VARNAME__MAX) { | 156 | if (vn < VARNAME__MAX) { |
170 | #define VARNAMESTR(name, str) str "\0" | 157 | #define VARNAMESTR(name, str) str "\0" |
@@ -321,7 +308,7 @@ const char *lj_debug_funcname(lua_State *L, TValue *frame, const char **name) | |||
321 | /* -- Source code locations ----------------------------------------------- */ | 308 | /* -- Source code locations ----------------------------------------------- */ |
322 | 309 | ||
323 | /* Generate shortened source name. */ | 310 | /* Generate shortened source name. */ |
324 | void lj_debug_shortname(char *out, GCstr *str) | 311 | void lj_debug_shortname(char *out, GCstr *str, BCLine line) |
325 | { | 312 | { |
326 | const char *src = strdata(str); | 313 | const char *src = strdata(str); |
327 | if (*src == '=') { | 314 | if (*src == '=') { |
@@ -335,11 +322,11 @@ void lj_debug_shortname(char *out, GCstr *str) | |||
335 | *out++ = '.'; *out++ = '.'; *out++ = '.'; | 322 | *out++ = '.'; *out++ = '.'; *out++ = '.'; |
336 | } | 323 | } |
337 | strcpy(out, src); | 324 | strcpy(out, src); |
338 | } else { /* Output [string "string"]. */ | 325 | } else { /* Output [string "string"] or [builtin:name]. */ |
339 | size_t len; /* Length, up to first control char. */ | 326 | size_t len; /* Length, up to first control char. */ |
340 | for (len = 0; len < LUA_IDSIZE-12; len++) | 327 | for (len = 0; len < LUA_IDSIZE-12; len++) |
341 | if (((const unsigned char *)src)[len] < ' ') break; | 328 | if (((const unsigned char *)src)[len] < ' ') break; |
342 | strcpy(out, "[string \""); out += 9; | 329 | strcpy(out, line == ~(BCLine)0 ? "[builtin:" : "[string \""); out += 9; |
343 | if (src[len] != '\0') { /* Must truncate? */ | 330 | if (src[len] != '\0') { /* Must truncate? */ |
344 | if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; | 331 | if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; |
345 | strncpy(out, src, len); out += len; | 332 | strncpy(out, src, len); out += len; |
@@ -347,7 +334,7 @@ void lj_debug_shortname(char *out, GCstr *str) | |||
347 | } else { | 334 | } else { |
348 | strcpy(out, src); out += len; | 335 | strcpy(out, src); out += len; |
349 | } | 336 | } |
350 | strcpy(out, "\"]"); | 337 | strcpy(out, line == ~(BCLine)0 ? "]" : "\"]"); |
351 | } | 338 | } |
352 | } | 339 | } |
353 | 340 | ||
@@ -360,14 +347,15 @@ void lj_debug_addloc(lua_State *L, const char *msg, | |||
360 | if (isluafunc(fn)) { | 347 | if (isluafunc(fn)) { |
361 | BCLine line = debug_frameline(L, fn, nextframe); | 348 | BCLine line = debug_frameline(L, fn, nextframe); |
362 | if (line >= 0) { | 349 | if (line >= 0) { |
350 | GCproto *pt = funcproto(fn); | ||
363 | char buf[LUA_IDSIZE]; | 351 | char buf[LUA_IDSIZE]; |
364 | lj_debug_shortname(buf, proto_chunkname(funcproto(fn))); | 352 | lj_debug_shortname(buf, proto_chunkname(pt), pt->firstline); |
365 | lj_str_pushf(L, "%s:%d: %s", buf, line, msg); | 353 | lj_strfmt_pushf(L, "%s:%d: %s", buf, line, msg); |
366 | return; | 354 | return; |
367 | } | 355 | } |
368 | } | 356 | } |
369 | } | 357 | } |
370 | lj_str_pushf(L, "%s", msg); | 358 | lj_strfmt_pushf(L, "%s", msg); |
371 | } | 359 | } |
372 | 360 | ||
373 | /* Push location string for a bytecode position to Lua stack. */ | 361 | /* Push location string for a bytecode position to Lua stack. */ |
@@ -377,20 +365,22 @@ void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) | |||
377 | const char *s = strdata(name); | 365 | const char *s = strdata(name); |
378 | MSize i, len = name->len; | 366 | MSize i, len = name->len; |
379 | BCLine line = lj_debug_line(pt, pc); | 367 | BCLine line = lj_debug_line(pt, pc); |
380 | if (*s == '@') { | 368 | if (pt->firstline == ~(BCLine)0) { |
369 | lj_strfmt_pushf(L, "builtin:%s", s); | ||
370 | } else if (*s == '@') { | ||
381 | s++; len--; | 371 | s++; len--; |
382 | for (i = len; i > 0; i--) | 372 | for (i = len; i > 0; i--) |
383 | if (s[i] == '/' || s[i] == '\\') { | 373 | if (s[i] == '/' || s[i] == '\\') { |
384 | s += i+1; | 374 | s += i+1; |
385 | break; | 375 | break; |
386 | } | 376 | } |
387 | lj_str_pushf(L, "%s:%d", s, line); | 377 | lj_strfmt_pushf(L, "%s:%d", s, line); |
388 | } else if (len > 40) { | 378 | } else if (len > 40) { |
389 | lj_str_pushf(L, "%p:%d", pt, line); | 379 | lj_strfmt_pushf(L, "%p:%d", pt, line); |
390 | } else if (*s == '=') { | 380 | } else if (*s == '=') { |
391 | lj_str_pushf(L, "%s:%d", s+1, line); | 381 | lj_strfmt_pushf(L, "%s:%d", s+1, line); |
392 | } else { | 382 | } else { |
393 | lj_str_pushf(L, "\"%s\":%d", s, line); | 383 | lj_strfmt_pushf(L, "\"%s\":%d", s, line); |
394 | } | 384 | } |
395 | } | 385 | } |
396 | 386 | ||
@@ -453,7 +443,7 @@ int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext) | |||
453 | BCLine firstline = pt->firstline; | 443 | BCLine firstline = pt->firstline; |
454 | GCstr *name = proto_chunkname(pt); | 444 | GCstr *name = proto_chunkname(pt); |
455 | ar->source = strdata(name); | 445 | ar->source = strdata(name); |
456 | lj_debug_shortname(ar->short_src, name); | 446 | lj_debug_shortname(ar->short_src, name, pt->firstline); |
457 | ar->linedefined = (int)firstline; | 447 | ar->linedefined = (int)firstline; |
458 | ar->lastlinedefined = (int)(firstline + pt->numline); | 448 | ar->lastlinedefined = (int)(firstline + pt->numline); |
459 | ar->what = firstline ? "Lua" : "main"; | 449 | ar->what = firstline ? "Lua" : "main"; |