| 1 |
/* |
| 2 |
** Debugging and introspection. |
| 3 |
** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h |
| 4 |
*/ |
| 5 |
|
| 6 |
#define lj_debug_c |
| 7 |
#define LUA_CORE |
| 8 |
|
| 9 |
#include "lj_obj.h" |
| 10 |
#include "lj_err.h" |
| 11 |
#include "lj_debug.h" |
| 12 |
#include "lj_buf.h" |
| 13 |
#include "lj_tab.h" |
| 14 |
#include "lj_state.h" |
| 15 |
#include "lj_frame.h" |
| 16 |
#include "lj_bc.h" |
| 17 |
#include "lj_strfmt.h" |
| 18 |
#if LJ_HASJIT |
| 19 |
#include "lj_jit.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
/* -- Frames -------------------------------------------------------------- */ |
| 23 |
|
| 24 |
/* Get frame corresponding to a level. */ |
| 25 |
cTValue *lj_debug_frame(lua_State *L, int level, int *size) |
| 26 |
{ |
| 27 |
cTValue *frame, *nextframe, *bot = tvref(L->stack)+LJ_FR2; |
| 28 |
/* Traverse frames backwards. */ |
| 29 |
for (nextframe = frame = L->base-1; frame > bot; ) { |
| 30 |
if (frame_gc(frame) == obj2gco(L)) |
| 31 |
level++; /* Skip dummy frames. See lj_err_optype_call(). */ |
| 32 |
if (level-- == 0) { |
| 33 |
*size = (int)(nextframe - frame); |
| 34 |
return frame; /* Level found. */ |
| 35 |
} |
| 36 |
nextframe = frame; |
| 37 |
if (frame_islua(frame)) { |
| 38 |
frame = frame_prevl(frame); |
| 39 |
} else { |
| 40 |
if (frame_isvarg(frame)) |
| 41 |
level++; /* Skip vararg pseudo-frame. */ |
| 42 |
frame = frame_prevd(frame); |
| 43 |
} |
| 44 |
} |
| 45 |
*size = level; |
| 46 |
return NULL; /* Level not found. */ |
| 47 |
} |
| 48 |
|
| 49 |
/* Invalid bytecode position. */ |
| 50 |
#define NO_BCPOS (~(BCPos)0) |
| 51 |
|
| 52 |
/* Return bytecode position for function/frame or NO_BCPOS. */ |
| 53 |
static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe) |
| 54 |
{ |
| 55 |
const BCIns *ins; |
| 56 |
GCproto *pt; |
| 57 |
BCPos pos; |
| 58 |
lj_assertL(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD, |
| 59 |
"function or frame expected"); |
| 60 |
if (!isluafunc(fn)) { /* Cannot derive a PC for non-Lua functions. */ |
| 61 |
return NO_BCPOS; |
| 62 |
} else if (nextframe == NULL) { /* Lua function on top. */ |
| 63 |
void *cf = cframe_raw(L->cframe); |
| 64 |
if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf)) |
| 65 |
return NO_BCPOS; |
| 66 |
ins = cframe_pc(cf); /* Only happens during error/hook handling. */ |
| 67 |
if (!ins) return NO_BCPOS; |
| 68 |
} else { |
| 69 |
if (frame_islua(nextframe)) { |
| 70 |
ins = frame_pc(nextframe); |
| 71 |
} else if (frame_iscont(nextframe)) { |
| 72 |
ins = frame_contpc(nextframe); |
| 73 |
} else { |
| 74 |
/* Lua function below errfunc/gc/hook: find cframe to get the PC. */ |
| 75 |
void *cf = cframe_raw(L->cframe); |
| 76 |
TValue *f = L->base-1; |
| 77 |
for (;;) { |
| 78 |
if (cf == NULL) |
| 79 |
return NO_BCPOS; |
| 80 |
while (cframe_nres(cf) < 0) { |
| 81 |
if (f >= restorestack(L, -cframe_nres(cf))) |
| 82 |
break; |
| 83 |
cf = cframe_raw(cframe_prev(cf)); |
| 84 |
if (cf == NULL) |
| 85 |
return NO_BCPOS; |
| 86 |
} |
| 87 |
if (f < nextframe) |
| 88 |
break; |
| 89 |
if (frame_islua(f)) { |
| 90 |
f = frame_prevl(f); |
| 91 |
} else { |
| 92 |
if (frame_isc(f) || (frame_iscont(f) && frame_iscont_fficb(f))) |
| 93 |
cf = cframe_raw(cframe_prev(cf)); |
| 94 |
f = frame_prevd(f); |
| 95 |
} |
| 96 |
} |
| 97 |
ins = cframe_pc(cf); |
| 98 |
if (!ins) return NO_BCPOS; |
| 99 |
} |
| 100 |
} |
| 101 |
pt = funcproto(fn); |
| 102 |
pos = proto_bcpos(pt, ins) - 1; |
| 103 |
#if LJ_HASJIT |
| 104 |
if (pos > pt->sizebc) { /* Undo the effects of lj_trace_exit for JLOOP. */ |
| 105 |
if (bc_isret(bc_op(ins[-1]))) { |
| 106 |
GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins)); |
| 107 |
pos = proto_bcpos(pt, mref(T->startpc, const BCIns)); |
| 108 |
} else { |
| 109 |
pos = NO_BCPOS; /* Punt in case of stack overflow for stitched trace. */ |
| 110 |
} |
| 111 |
} |
| 112 |
#endif |
| 113 |
return pos; |
| 114 |
} |
| 115 |
|
| 116 |
/* -- Line numbers -------------------------------------------------------- */ |
| 117 |
|
| 118 |
/* Get line number for a bytecode position. */ |
| 119 |
BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc) |
| 120 |
{ |
| 121 |
const void *lineinfo = proto_lineinfo(pt); |
| 122 |
if (pc <= pt->sizebc && lineinfo) { |
| 123 |
BCLine first = pt->firstline; |
| 124 |
if (pc == pt->sizebc) return first + pt->numline; |
| 125 |
if (pc-- == 0) return first; |
| 126 |
if (pt->numline < 256) |
| 127 |
return first + (BCLine)((const uint8_t *)lineinfo)[pc]; |
| 128 |
else if (pt->numline < 65536) |
| 129 |
return first + (BCLine)((const uint16_t *)lineinfo)[pc]; |
| 130 |
else |
| 131 |
return first + (BCLine)((const uint32_t *)lineinfo)[pc]; |
| 132 |
} |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
/* Get line number for function/frame. */ |
| 137 |
static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe) |
| 138 |
{ |
| 139 |
BCPos pc = debug_framepc(L, fn, nextframe); |
| 140 |
if (pc != NO_BCPOS) { |
| 141 |
GCproto *pt = funcproto(fn); |
| 142 |
lj_assertL(pc <= pt->sizebc, "PC out of range"); |
| 143 |
return lj_debug_line(pt, pc); |
| 144 |
} |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
/* -- Variable names ------------------------------------------------------ */ |
| 149 |
|
| 150 |
/* Get name of a local variable from slot number and PC. */ |
| 151 |
static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) |
| 152 |
{ |
| 153 |
const char *p = (const char *)proto_varinfo(pt); |
| 154 |
if (p) { |
| 155 |
BCPos lastpc = 0; |
| 156 |
for (;;) { |
| 157 |
const char *name = p; |
| 158 |
uint32_t vn = *(const uint8_t *)p; |
| 159 |
BCPos startpc, endpc; |
| 160 |
if (vn < VARNAME__MAX) { |
| 161 |
if (vn == VARNAME_END) break; /* End of varinfo. */ |
| 162 |
} else { |
| 163 |
do { p++; } while (*(const uint8_t *)p); /* Skip over variable name. */ |
| 164 |
} |
| 165 |
p++; |
| 166 |
lastpc = startpc = lastpc + lj_buf_ruleb128(&p); |
| 167 |
if (startpc > pc) break; |
| 168 |
endpc = startpc + lj_buf_ruleb128(&p); |
| 169 |
if (pc < endpc && slot-- == 0) { |
| 170 |
if (vn < VARNAME__MAX) { |
| 171 |
#define VARNAMESTR(name, str) str "\0" |
| 172 |
name = VARNAMEDEF(VARNAMESTR); |
| 173 |
#undef VARNAMESTR |
| 174 |
if (--vn) while (*name++ || --vn) ; |
| 175 |
} |
| 176 |
return name; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
/* Get name of local variable from 1-based slot number and function/frame. */ |
| 184 |
static TValue *debug_localname(lua_State *L, const lua_Debug *ar, |
| 185 |
const char **name, BCReg slot1) |
| 186 |
{ |
| 187 |
uint32_t offset = (uint32_t)ar->i_ci & 0xffff; |
| 188 |
uint32_t size = (uint32_t)ar->i_ci >> 16; |
| 189 |
TValue *frame = tvref(L->stack) + offset; |
| 190 |
TValue *nextframe = size ? frame + size : NULL; |
| 191 |
GCfunc *fn = frame_func(frame); |
| 192 |
BCPos pc = debug_framepc(L, fn, nextframe); |
| 193 |
if (!nextframe) nextframe = L->top+LJ_FR2; |
| 194 |
if ((int)slot1 < 0) { /* Negative slot number is for varargs. */ |
| 195 |
if (pc != NO_BCPOS) { |
| 196 |
GCproto *pt = funcproto(fn); |
| 197 |
if ((pt->flags & PROTO_VARARG)) { |
| 198 |
slot1 = pt->numparams + (BCReg)(-(int)slot1); |
| 199 |
if (frame_isvarg(frame)) { /* Vararg frame has been set up? (pc!=0) */ |
| 200 |
nextframe = frame; |
| 201 |
frame = frame_prevd(frame); |
| 202 |
} |
| 203 |
if (frame + slot1+LJ_FR2 < nextframe) { |
| 204 |
*name = "(*vararg)"; |
| 205 |
return frame+slot1; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
if (pc != NO_BCPOS && |
| 212 |
(*name = debug_varname(funcproto(fn), pc, slot1-1)) != NULL) |
| 213 |
; |
| 214 |
else if (slot1 > 0 && frame + slot1+LJ_FR2 < nextframe) |
| 215 |
*name = "(*temporary)"; |
| 216 |
return frame+slot1; |
| 217 |
} |
| 218 |
|
| 219 |
/* Get name of upvalue. */ |
| 220 |
const char *lj_debug_uvname(GCproto *pt, uint32_t idx) |
| 221 |
{ |
| 222 |
const uint8_t *p = proto_uvinfo(pt); |
| 223 |
lj_assertX(idx < pt->sizeuv, "bad upvalue index"); |
| 224 |
if (!p) return ""; |
| 225 |
if (idx) while (*p++ || --idx) ; |
| 226 |
return (const char *)p; |
| 227 |
} |
| 228 |
|
| 229 |
/* Get name and value of upvalue. */ |
| 230 |
const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp, GCobj **op) |
| 231 |
{ |
| 232 |
if (tvisfunc(o)) { |
| 233 |
GCfunc *fn = funcV(o); |
| 234 |
if (isluafunc(fn)) { |
| 235 |
GCproto *pt = funcproto(fn); |
| 236 |
if (idx < pt->sizeuv) { |
| 237 |
GCobj *uvo = gcref(fn->l.uvptr[idx]); |
| 238 |
*tvp = uvval(&uvo->uv); |
| 239 |
*op = uvo; |
| 240 |
return lj_debug_uvname(pt, idx); |
| 241 |
} |
| 242 |
} else { |
| 243 |
if (idx < fn->c.nupvalues) { |
| 244 |
*tvp = &fn->c.upvalue[idx]; |
| 245 |
*op = obj2gco(fn); |
| 246 |
return ""; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
/* Deduce name of an object from slot number and PC. */ |
| 254 |
const char *lj_debug_slotname(GCproto *pt, const BCIns *ip, BCReg slot, |
| 255 |
const char **name) |
| 256 |
{ |
| 257 |
const char *lname; |
| 258 |
restart: |
| 259 |
lname = debug_varname(pt, proto_bcpos(pt, ip), slot); |
| 260 |
if (lname != NULL) { *name = lname; return "local"; } |
| 261 |
while (--ip > proto_bc(pt)) { |
| 262 |
BCIns ins = *ip; |
| 263 |
BCOp op = bc_op(ins); |
| 264 |
BCReg ra = bc_a(ins); |
| 265 |
if (bcmode_a(op) == BCMbase) { |
| 266 |
if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins))) |
| 267 |
return NULL; |
| 268 |
} else if (bcmode_a(op) == BCMdst && ra == slot) { |
| 269 |
switch (bc_op(ins)) { |
| 270 |
case BC_MOV: |
| 271 |
if (ra == slot) { slot = bc_d(ins); goto restart; } |
| 272 |
break; |
| 273 |
case BC_GGET: |
| 274 |
*name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins)))); |
| 275 |
return "global"; |
| 276 |
case BC_TGETS: |
| 277 |
*name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins)))); |
| 278 |
if (ip > proto_bc(pt)) { |
| 279 |
BCIns insp = ip[-1]; |
| 280 |
if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1+LJ_FR2 && |
| 281 |
bc_d(insp) == bc_b(ins)) |
| 282 |
return "method"; |
| 283 |
} |
| 284 |
return "field"; |
| 285 |
case BC_UGET: |
| 286 |
*name = lj_debug_uvname(pt, bc_d(ins)); |
| 287 |
return "upvalue"; |
| 288 |
default: |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
|
| 296 |
/* Deduce function name from caller of a frame. */ |
| 297 |
const char *lj_debug_funcname(lua_State *L, cTValue *frame, const char **name) |
| 298 |
{ |
| 299 |
cTValue *pframe; |
| 300 |
GCfunc *fn; |
| 301 |
BCPos pc; |
| 302 |
if (frame <= tvref(L->stack)+LJ_FR2) |
| 303 |
return NULL; |
| 304 |
if (frame_isvarg(frame)) |
| 305 |
frame = frame_prevd(frame); |
| 306 |
pframe = frame_prev(frame); |
| 307 |
fn = frame_func(pframe); |
| 308 |
pc = debug_framepc(L, fn, frame); |
| 309 |
if (pc != NO_BCPOS) { |
| 310 |
GCproto *pt = funcproto(fn); |
| 311 |
const BCIns *ip = &proto_bc(pt)[check_exp(pc < pt->sizebc, pc)]; |
| 312 |
MMS mm = bcmode_mm(bc_op(*ip)); |
| 313 |
if (mm == MM_call) { |
| 314 |
BCReg slot = bc_a(*ip); |
| 315 |
if (bc_op(*ip) == BC_ITERC) slot -= 3; |
| 316 |
return lj_debug_slotname(pt, ip, slot, name); |
| 317 |
} else if (mm != MM__MAX) { |
| 318 |
*name = strdata(mmname_str(G(L), mm)); |
| 319 |
return "metamethod"; |
| 320 |
} |
| 321 |
} |
| 322 |
return NULL; |
| 323 |
} |
| 324 |
|
| 325 |
/* -- Source code locations ----------------------------------------------- */ |
| 326 |
|
| 327 |
/* Generate shortened source name. */ |
| 328 |
void lj_debug_shortname(char *out, GCstr *str, BCLine line) |
| 329 |
{ |
| 330 |
const char *src = strdata(str); |
| 331 |
if (*src == '=') { |
| 332 |
strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */ |
| 333 |
out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */ |
| 334 |
} else if (*src == '@') { /* Output "source", or "...source". */ |
| 335 |
size_t len = str->len-1; |
| 336 |
src++; /* Skip the `@' */ |
| 337 |
if (len >= LUA_IDSIZE) { |
| 338 |
src += len-(LUA_IDSIZE-4); /* Get last part of file name. */ |
| 339 |
*out++ = '.'; *out++ = '.'; *out++ = '.'; |
| 340 |
} |
| 341 |
strcpy(out, src); |
| 342 |
} else { /* Output [string "string"] or [builtin:name]. */ |
| 343 |
size_t len; /* Length, up to first control char. */ |
| 344 |
for (len = 0; len < LUA_IDSIZE-12; len++) |
| 345 |
if (((const unsigned char *)src)[len] < ' ') break; |
| 346 |
strcpy(out, line == ~(BCLine)0 ? "[builtin:" : "[string \""); out += 9; |
| 347 |
if (src[len] != '\0') { /* Must truncate? */ |
| 348 |
if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; |
| 349 |
strncpy(out, src, len); out += len; |
| 350 |
strcpy(out, "..."); out += 3; |
| 351 |
} else { |
| 352 |
strcpy(out, src); out += len; |
| 353 |
} |
| 354 |
strcpy(out, line == ~(BCLine)0 ? "]" : "\"]"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/* Add current location of a frame to error message. */ |
| 359 |
void lj_debug_addloc(lua_State *L, const char *msg, |
| 360 |
cTValue *frame, cTValue *nextframe) |
| 361 |
{ |
| 362 |
if (frame) { |
| 363 |
GCfunc *fn = frame_func(frame); |
| 364 |
if (isluafunc(fn)) { |
| 365 |
BCLine line = debug_frameline(L, fn, nextframe); |
| 366 |
if (line >= 0) { |
| 367 |
GCproto *pt = funcproto(fn); |
| 368 |
char buf[LUA_IDSIZE]; |
| 369 |
lj_debug_shortname(buf, proto_chunkname(pt), pt->firstline); |
| 370 |
lj_strfmt_pushf(L, "%s:%d: %s", buf, line, msg); |
| 371 |
return; |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
lj_strfmt_pushf(L, "%s", msg); |
| 376 |
} |
| 377 |
|
| 378 |
/* Push location string for a bytecode position to Lua stack. */ |
| 379 |
void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) |
| 380 |
{ |
| 381 |
GCstr *name = proto_chunkname(pt); |
| 382 |
const char *s = strdata(name); |
| 383 |
MSize i, len = name->len; |
| 384 |
BCLine line = lj_debug_line(pt, pc); |
| 385 |
if (pt->firstline == ~(BCLine)0) { |
| 386 |
lj_strfmt_pushf(L, "builtin:%s", s); |
| 387 |
} else if (*s == '@') { |
| 388 |
s++; len--; |
| 389 |
for (i = len; i > 0; i--) |
| 390 |
if (s[i] == '/' || s[i] == '\\') { |
| 391 |
s += i+1; |
| 392 |
break; |
| 393 |
} |
| 394 |
lj_strfmt_pushf(L, "%s:%d", s, line); |
| 395 |
} else if (len > 40) { |
| 396 |
lj_strfmt_pushf(L, "%p:%d", pt, line); |
| 397 |
} else if (*s == '=') { |
| 398 |
lj_strfmt_pushf(L, "%s:%d", s+1, line); |
| 399 |
} else { |
| 400 |
lj_strfmt_pushf(L, "\"%s\":%d", s, line); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
/* -- Public debug API ---------------------------------------------------- */ |
| 405 |
|
| 406 |
/* lua_getupvalue() and lua_setupvalue() are in lj_api.c. */ |
| 407 |
|
| 408 |
LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) |
| 409 |
{ |
| 410 |
const char *name = NULL; |
| 411 |
if (ar) { |
| 412 |
TValue *o = debug_localname(L, ar, &name, (BCReg)n); |
| 413 |
if (name) { |
| 414 |
copyTV(L, L->top, o); |
| 415 |
incr_top(L); |
| 416 |
} |
| 417 |
} else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) { |
| 418 |
name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1); |
| 419 |
} |
| 420 |
return name; |
| 421 |
} |
| 422 |
|
| 423 |
LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) |
| 424 |
{ |
| 425 |
const char *name = NULL; |
| 426 |
TValue *o = debug_localname(L, ar, &name, (BCReg)n); |
| 427 |
if (name) |
| 428 |
copyTV(L, o, L->top-1); |
| 429 |
L->top--; |
| 430 |
return name; |
| 431 |
} |
| 432 |
|
| 433 |
int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext) |
| 434 |
{ |
| 435 |
int opt_f = 0, opt_L = 0; |
| 436 |
TValue *frame = NULL; |
| 437 |
TValue *nextframe = NULL; |
| 438 |
GCfunc *fn; |
| 439 |
if (*what == '>') { |
| 440 |
TValue *func = L->top - 1; |
| 441 |
if (!tvisfunc(func)) return 0; |
| 442 |
fn = funcV(func); |
| 443 |
L->top--; |
| 444 |
what++; |
| 445 |
} else { |
| 446 |
uint32_t offset = (uint32_t)ar->i_ci & 0xffff; |
| 447 |
uint32_t size = (uint32_t)ar->i_ci >> 16; |
| 448 |
lj_assertL(offset != 0, "bad frame offset"); |
| 449 |
frame = tvref(L->stack) + offset; |
| 450 |
if (size) nextframe = frame + size; |
| 451 |
lj_assertL(frame <= tvref(L->maxstack) && |
| 452 |
(!nextframe || nextframe <= tvref(L->maxstack)), |
| 453 |
"broken frame chain"); |
| 454 |
fn = frame_func(frame); |
| 455 |
lj_assertL(fn->c.gct == ~LJ_TFUNC, "bad frame function"); |
| 456 |
} |
| 457 |
for (; *what; what++) { |
| 458 |
if (*what == 'S') { |
| 459 |
if (isluafunc(fn)) { |
| 460 |
GCproto *pt = funcproto(fn); |
| 461 |
BCLine firstline = pt->firstline; |
| 462 |
GCstr *name = proto_chunkname(pt); |
| 463 |
ar->source = strdata(name); |
| 464 |
lj_debug_shortname(ar->short_src, name, pt->firstline); |
| 465 |
ar->linedefined = (int)firstline; |
| 466 |
ar->lastlinedefined = (int)(firstline + pt->numline); |
| 467 |
ar->what = (firstline || !pt->numline) ? "Lua" : "main"; |
| 468 |
} else { |
| 469 |
ar->source = "=[C]"; |
| 470 |
ar->short_src[0] = '['; |
| 471 |
ar->short_src[1] = 'C'; |
| 472 |
ar->short_src[2] = ']'; |
| 473 |
ar->short_src[3] = '\0'; |
| 474 |
ar->linedefined = -1; |
| 475 |
ar->lastlinedefined = -1; |
| 476 |
ar->what = "C"; |
| 477 |
} |
| 478 |
} else if (*what == 'l') { |
| 479 |
ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1; |
| 480 |
} else if (*what == 'u') { |
| 481 |
ar->nups = fn->c.nupvalues; |
| 482 |
if (ext) { |
| 483 |
if (isluafunc(fn)) { |
| 484 |
GCproto *pt = funcproto(fn); |
| 485 |
ar->nparams = pt->numparams; |
| 486 |
ar->isvararg = !!(pt->flags & PROTO_VARARG); |
| 487 |
} else { |
| 488 |
ar->nparams = 0; |
| 489 |
ar->isvararg = 1; |
| 490 |
} |
| 491 |
} |
| 492 |
} else if (*what == 'n') { |
| 493 |
ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL; |
| 494 |
if (ar->namewhat == NULL) { |
| 495 |
ar->namewhat = ""; |
| 496 |
ar->name = NULL; |
| 497 |
} |
| 498 |
} else if (*what == 'f') { |
| 499 |
opt_f = 1; |
| 500 |
} else if (*what == 'L') { |
| 501 |
opt_L = 1; |
| 502 |
} else { |
| 503 |
return 0; /* Bad option. */ |
| 504 |
} |
| 505 |
} |
| 506 |
if (opt_f) { |
| 507 |
setfuncV(L, L->top, fn); |
| 508 |
incr_top(L); |
| 509 |
} |
| 510 |
if (opt_L) { |
| 511 |
if (isluafunc(fn)) { |
| 512 |
GCtab *t = lj_tab_new(L, 0, 0); |
| 513 |
GCproto *pt = funcproto(fn); |
| 514 |
const void *lineinfo = proto_lineinfo(pt); |
| 515 |
if (lineinfo) { |
| 516 |
BCLine first = pt->firstline; |
| 517 |
int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4; |
| 518 |
MSize i, szl = pt->sizebc-1; |
| 519 |
for (i = 0; i < szl; i++) { |
| 520 |
BCLine line = first + |
| 521 |
(sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] : |
| 522 |
sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] : |
| 523 |
(BCLine)((const uint32_t *)lineinfo)[i]); |
| 524 |
setboolV(lj_tab_setint(L, t, line), 1); |
| 525 |
} |
| 526 |
} |
| 527 |
settabV(L, L->top, t); |
| 528 |
} else { |
| 529 |
setnilV(L->top); |
| 530 |
} |
| 531 |
incr_top(L); |
| 532 |
} |
| 533 |
return 1; /* Ok. */ |
| 534 |
} |
| 535 |
|
| 536 |
LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) |
| 537 |
{ |
| 538 |
return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0); |
| 539 |
} |
| 540 |
|
| 541 |
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) |
| 542 |
{ |
| 543 |
int size; |
| 544 |
cTValue *frame = lj_debug_frame(L, level, &size); |
| 545 |
if (frame) { |
| 546 |
ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack)); |
| 547 |
return 1; |
| 548 |
} else { |
| 549 |
ar->i_ci = level - size; |
| 550 |
return 0; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
#if LJ_HASPROFILE |
| 555 |
/* Put the chunkname into a buffer. */ |
| 556 |
static int debug_putchunkname(SBuf *sb, GCproto *pt, int pathstrip) |
| 557 |
{ |
| 558 |
GCstr *name = proto_chunkname(pt); |
| 559 |
const char *p = strdata(name); |
| 560 |
if (pt->firstline == ~(BCLine)0) { |
| 561 |
lj_buf_putmem(sb, "[builtin:", 9); |
| 562 |
lj_buf_putstr(sb, name); |
| 563 |
lj_buf_putb(sb, ']'); |
| 564 |
return 0; |
| 565 |
} |
| 566 |
if (*p == '=' || *p == '@') { |
| 567 |
MSize len = name->len-1; |
| 568 |
p++; |
| 569 |
if (pathstrip) { |
| 570 |
int i; |
| 571 |
for (i = len-1; i >= 0; i--) |
| 572 |
if (p[i] == '/' || p[i] == '\\') { |
| 573 |
len -= i+1; |
| 574 |
p = p+i+1; |
| 575 |
break; |
| 576 |
} |
| 577 |
} |
| 578 |
lj_buf_putmem(sb, p, len); |
| 579 |
} else { |
| 580 |
lj_buf_putmem(sb, "[string]", 8); |
| 581 |
} |
| 582 |
return 1; |
| 583 |
} |
| 584 |
|
| 585 |
/* Put a compact stack dump into a buffer. */ |
| 586 |
void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt, int depth) |
| 587 |
{ |
| 588 |
int level = 0, dir = 1, pathstrip = 1; |
| 589 |
MSize lastlen = 0; |
| 590 |
if (depth < 0) { level = ~depth; depth = dir = -1; } /* Reverse frames. */ |
| 591 |
while (level != depth) { /* Loop through all frame. */ |
| 592 |
int size; |
| 593 |
cTValue *frame = lj_debug_frame(L, level, &size); |
| 594 |
if (frame) { |
| 595 |
cTValue *nextframe = size ? frame+size : NULL; |
| 596 |
GCfunc *fn = frame_func(frame); |
| 597 |
const uint8_t *p = (const uint8_t *)fmt; |
| 598 |
int c; |
| 599 |
while ((c = *p++)) { |
| 600 |
switch (c) { |
| 601 |
case 'p': /* Preserve full path. */ |
| 602 |
pathstrip = 0; |
| 603 |
break; |
| 604 |
case 'F': case 'f': { /* Dump function name. */ |
| 605 |
const char *name; |
| 606 |
const char *what = lj_debug_funcname(L, frame, &name); |
| 607 |
if (what) { |
| 608 |
if (c == 'F' && isluafunc(fn)) { /* Dump module:name for 'F'. */ |
| 609 |
GCproto *pt = funcproto(fn); |
| 610 |
if (pt->firstline != ~(BCLine)0) { /* Not a bytecode builtin. */ |
| 611 |
debug_putchunkname(sb, pt, pathstrip); |
| 612 |
lj_buf_putb(sb, ':'); |
| 613 |
} |
| 614 |
} |
| 615 |
lj_buf_putmem(sb, name, (MSize)strlen(name)); |
| 616 |
break; |
| 617 |
} /* else: can't derive a name, dump module:line. */ |
| 618 |
} |
| 619 |
/* fallthrough */ |
| 620 |
case 'l': /* Dump module:line. */ |
| 621 |
if (isluafunc(fn)) { |
| 622 |
GCproto *pt = funcproto(fn); |
| 623 |
if (debug_putchunkname(sb, pt, pathstrip)) { |
| 624 |
/* Regular Lua function. */ |
| 625 |
BCLine line = c == 'l' ? debug_frameline(L, fn, nextframe) : |
| 626 |
pt->firstline; |
| 627 |
lj_buf_putb(sb, ':'); |
| 628 |
lj_strfmt_putint(sb, line >= 0 ? line : pt->firstline); |
| 629 |
} |
| 630 |
} else if (isffunc(fn)) { /* Dump numbered builtins. */ |
| 631 |
lj_buf_putmem(sb, "[builtin#", 9); |
| 632 |
lj_strfmt_putint(sb, fn->c.ffid); |
| 633 |
lj_buf_putb(sb, ']'); |
| 634 |
} else { /* Dump C function address. */ |
| 635 |
lj_buf_putb(sb, '@'); |
| 636 |
lj_strfmt_putptr(sb, fn->c.f); |
| 637 |
} |
| 638 |
break; |
| 639 |
case 'Z': /* Zap trailing separator. */ |
| 640 |
lastlen = sbuflen(sb); |
| 641 |
break; |
| 642 |
default: |
| 643 |
lj_buf_putb(sb, c); |
| 644 |
break; |
| 645 |
} |
| 646 |
} |
| 647 |
} else if (dir == 1) { |
| 648 |
break; |
| 649 |
} else { |
| 650 |
level -= size; /* Reverse frame order: quickly skip missing level. */ |
| 651 |
} |
| 652 |
level += dir; |
| 653 |
} |
| 654 |
if (lastlen) |
| 655 |
sb->w = sb->b + lastlen; /* Zap trailing separator. */ |
| 656 |
} |
| 657 |
#endif |
| 658 |
|
| 659 |
/* Number of frames for the leading and trailing part of a traceback. */ |
| 660 |
#define TRACEBACK_LEVELS1 12 |
| 661 |
#define TRACEBACK_LEVELS2 10 |
| 662 |
|
| 663 |
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, |
| 664 |
int level) |
| 665 |
{ |
| 666 |
int top = (int)(L->top - L->base); |
| 667 |
int lim = TRACEBACK_LEVELS1; |
| 668 |
lua_Debug ar; |
| 669 |
if (msg) lua_pushfstring(L, "%s\n", msg); |
| 670 |
lua_pushliteral(L, "stack traceback:"); |
| 671 |
while (lua_getstack(L1, level++, &ar)) { |
| 672 |
GCfunc *fn; |
| 673 |
if (level > lim) { |
| 674 |
if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) { |
| 675 |
level--; |
| 676 |
} else { |
| 677 |
lua_pushliteral(L, "\n\t..."); |
| 678 |
lua_getstack(L1, -10, &ar); |
| 679 |
level = ar.i_ci - TRACEBACK_LEVELS2; |
| 680 |
} |
| 681 |
lim = 2147483647; |
| 682 |
continue; |
| 683 |
} |
| 684 |
lua_getinfo(L1, "Snlf", &ar); |
| 685 |
fn = funcV(L1->top-1); L1->top--; |
| 686 |
if (isffunc(fn) && !*ar.namewhat) |
| 687 |
lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid); |
| 688 |
else |
| 689 |
lua_pushfstring(L, "\n\t%s:", ar.short_src); |
| 690 |
if (ar.currentline > 0) |
| 691 |
lua_pushfstring(L, "%d:", ar.currentline); |
| 692 |
if (*ar.namewhat) { |
| 693 |
lua_pushfstring(L, " in function " LUA_QS, ar.name); |
| 694 |
} else { |
| 695 |
if (*ar.what == 'm') { |
| 696 |
lua_pushliteral(L, " in main chunk"); |
| 697 |
} else if (*ar.what == 'C') { |
| 698 |
lua_pushfstring(L, " at %p", fn->c.f); |
| 699 |
} else { |
| 700 |
lua_pushfstring(L, " in function <%s:%d>", |
| 701 |
ar.short_src, ar.linedefined); |
| 702 |
} |
| 703 |
} |
| 704 |
if ((int)(L->top - L->base) - top >= 15) |
| 705 |
lua_concat(L, (int)(L->top - L->base) - top); |
| 706 |
} |
| 707 |
lua_concat(L, (int)(L->top - L->base) - top); |
| 708 |
} |
| 709 |
|