1 /* $NetBSD: ldebug.c,v 1.2 2014/07/19 18:38:34 lneto Exp $ */ 2 3 /* 4 ** $Id: ldebug.c,v 1.2 2014/07/19 18:38:34 lneto Exp $ 5 ** Debug Interface 6 ** See Copyright Notice in lua.h 7 */ 8 9 10 #ifndef _KERNEL 11 #include <stdarg.h> 12 #include <stddef.h> 13 #include <string.h> 14 #endif 15 16 17 #define ldebug_c 18 #define LUA_CORE 19 20 #include "lua.h" 21 22 #include "lapi.h" 23 #include "lcode.h" 24 #include "ldebug.h" 25 #include "ldo.h" 26 #include "lfunc.h" 27 #include "lobject.h" 28 #include "lopcodes.h" 29 #include "lstate.h" 30 #include "lstring.h" 31 #include "ltable.h" 32 #include "ltm.h" 33 #include "lvm.h" 34 35 36 37 #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) 38 39 40 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); 41 42 43 static int currentpc (CallInfo *ci) { 44 lua_assert(isLua(ci)); 45 return pcRel(ci->u.l.savedpc, ci_func(ci)->p); 46 } 47 48 49 static int currentline (CallInfo *ci) { 50 return getfuncline(ci_func(ci)->p, currentpc(ci)); 51 } 52 53 54 /* 55 ** this function can be called asynchronous (e.g. during a signal) 56 */ 57 LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { 58 if (func == NULL || mask == 0) { /* turn off hooks? */ 59 mask = 0; 60 func = NULL; 61 } 62 if (isLua(L->ci)) 63 L->oldpc = L->ci->u.l.savedpc; 64 L->hook = func; 65 L->basehookcount = count; 66 resethookcount(L); 67 L->hookmask = cast_byte(mask); 68 } 69 70 71 LUA_API lua_Hook lua_gethook (lua_State *L) { 72 return L->hook; 73 } 74 75 76 LUA_API int lua_gethookmask (lua_State *L) { 77 return L->hookmask; 78 } 79 80 81 LUA_API int lua_gethookcount (lua_State *L) { 82 return L->basehookcount; 83 } 84 85 86 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 87 int status; 88 CallInfo *ci; 89 if (level < 0) return 0; /* invalid (negative) level */ 90 lua_lock(L); 91 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) 92 level--; 93 if (level == 0 && ci != &L->base_ci) { /* level found? */ 94 status = 1; 95 ar->i_ci = ci; 96 } 97 else status = 0; /* no such level */ 98 lua_unlock(L); 99 return status; 100 } 101 102 103 static const char *upvalname (Proto *p, int uv) { 104 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); 105 if (s == NULL) return "?"; 106 else return getstr(s); 107 } 108 109 110 static const char *findvararg (CallInfo *ci, int n, StkId *pos) { 111 int nparams = clLvalue(ci->func)->p->numparams; 112 if (n >= ci->u.l.base - ci->func - nparams) 113 return NULL; /* no such vararg */ 114 else { 115 *pos = ci->func + nparams + n; 116 return "(*vararg)"; /* generic name for any vararg */ 117 } 118 } 119 120 121 static const char *findlocal (lua_State *L, CallInfo *ci, int n, 122 StkId *pos) { 123 const char *name = NULL; 124 StkId base; 125 if (isLua(ci)) { 126 if (n < 0) /* access to vararg values? */ 127 return findvararg(ci, -n, pos); 128 else { 129 base = ci->u.l.base; 130 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); 131 } 132 } 133 else 134 base = ci->func + 1; 135 if (name == NULL) { /* no 'standard' name? */ 136 StkId limit = (ci == L->ci) ? L->top : ci->next->func; 137 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 138 name = "(*temporary)"; /* generic name for any valid slot */ 139 else 140 return NULL; /* no name */ 141 } 142 *pos = base + (n - 1); 143 return name; 144 } 145 146 147 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 148 const char *name; 149 lua_lock(L); 150 if (ar == NULL) { /* information about non-active function? */ 151 if (!isLfunction(L->top - 1)) /* not a Lua function? */ 152 name = NULL; 153 else /* consider live variables at function start (parameters) */ 154 name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); 155 } 156 else { /* active function; get information through 'ar' */ 157 StkId pos = 0; /* to avoid warnings */ 158 name = findlocal(L, ar->i_ci, n, &pos); 159 if (name) { 160 setobj2s(L, L->top, pos); 161 api_incr_top(L); 162 } 163 } 164 lua_unlock(L); 165 return name; 166 } 167 168 169 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 170 StkId pos = 0; /* to avoid warnings */ 171 const char *name = findlocal(L, ar->i_ci, n, &pos); 172 lua_lock(L); 173 if (name) 174 setobjs2s(L, pos, L->top - 1); 175 L->top--; /* pop value */ 176 lua_unlock(L); 177 return name; 178 } 179 180 181 static void funcinfo (lua_Debug *ar, Closure *cl) { 182 if (noLuaClosure(cl)) { 183 ar->source = "=[C]"; 184 ar->linedefined = -1; 185 ar->lastlinedefined = -1; 186 ar->what = "C"; 187 } 188 else { 189 Proto *p = cl->l.p; 190 ar->source = p->source ? getstr(p->source) : "=?"; 191 ar->linedefined = p->linedefined; 192 ar->lastlinedefined = p->lastlinedefined; 193 ar->what = (ar->linedefined == 0) ? "main" : "Lua"; 194 } 195 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 196 } 197 198 199 static void collectvalidlines (lua_State *L, Closure *f) { 200 if (noLuaClosure(f)) { 201 setnilvalue(L->top); 202 api_incr_top(L); 203 } 204 else { 205 int i; 206 TValue v; 207 int *lineinfo = f->l.p->lineinfo; 208 Table *t = luaH_new(L); /* new table to store active lines */ 209 sethvalue(L, L->top, t); /* push it on stack */ 210 api_incr_top(L); 211 setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ 212 for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ 213 luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ 214 } 215 } 216 217 218 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, 219 Closure *f, CallInfo *ci) { 220 int status = 1; 221 for (; *what; what++) { 222 switch (*what) { 223 case 'S': { 224 funcinfo(ar, f); 225 break; 226 } 227 case 'l': { 228 ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; 229 break; 230 } 231 case 'u': { 232 ar->nups = (f == NULL) ? 0 : f->c.nupvalues; 233 if (noLuaClosure(f)) { 234 ar->isvararg = 1; 235 ar->nparams = 0; 236 } 237 else { 238 ar->isvararg = f->l.p->is_vararg; 239 ar->nparams = f->l.p->numparams; 240 } 241 break; 242 } 243 case 't': { 244 ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; 245 break; 246 } 247 case 'n': { 248 /* calling function is a known Lua function? */ 249 if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) 250 ar->namewhat = getfuncname(L, ci->previous, &ar->name); 251 else 252 ar->namewhat = NULL; 253 if (ar->namewhat == NULL) { 254 ar->namewhat = ""; /* not found */ 255 ar->name = NULL; 256 } 257 break; 258 } 259 case 'L': 260 case 'f': /* handled by lua_getinfo */ 261 break; 262 default: status = 0; /* invalid option */ 263 } 264 } 265 return status; 266 } 267 268 269 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 270 int status; 271 Closure *cl; 272 CallInfo *ci; 273 StkId func; 274 lua_lock(L); 275 if (*what == '>') { 276 ci = NULL; 277 func = L->top - 1; 278 api_check(L, ttisfunction(func), "function expected"); 279 what++; /* skip the '>' */ 280 L->top--; /* pop function */ 281 } 282 else { 283 ci = ar->i_ci; 284 func = ci->func; 285 lua_assert(ttisfunction(ci->func)); 286 } 287 cl = ttisclosure(func) ? clvalue(func) : NULL; 288 status = auxgetinfo(L, what, ar, cl, ci); 289 if (strchr(what, 'f')) { 290 setobjs2s(L, L->top, func); 291 api_incr_top(L); 292 } 293 if (strchr(what, 'L')) 294 collectvalidlines(L, cl); 295 lua_unlock(L); 296 return status; 297 } 298 299 300 /* 301 ** {====================================================== 302 ** Symbolic Execution 303 ** ======================================================= 304 */ 305 306 static const char *getobjname (Proto *p, int lastpc, int reg, 307 const char **name); 308 309 310 /* 311 ** find a "name" for the RK value 'c' 312 */ 313 static void kname (Proto *p, int pc, int c, const char **name) { 314 if (ISK(c)) { /* is 'c' a constant? */ 315 TValue *kvalue = &p->k[INDEXK(c)]; 316 if (ttisstring(kvalue)) { /* literal constant? */ 317 *name = svalue(kvalue); /* it is its own name */ 318 return; 319 } 320 /* else no reasonable name found */ 321 } 322 else { /* 'c' is a register */ 323 const char *what = getobjname(p, pc, c, name); /* search for 'c' */ 324 if (what && *what == 'c') { /* found a constant name? */ 325 return; /* 'name' already filled */ 326 } 327 /* else no reasonable name found */ 328 } 329 *name = "?"; /* no reasonable name found */ 330 } 331 332 333 static int filterpc (int pc, int jmptarget) { 334 if (pc < jmptarget) /* is code conditional (inside a jump)? */ 335 return -1; /* cannot know who sets that register */ 336 else return pc; /* current position sets that register */ 337 } 338 339 340 /* 341 ** try to find last instruction before 'lastpc' that modified register 'reg' 342 */ 343 static int findsetreg (Proto *p, int lastpc, int reg) { 344 int pc; 345 int setreg = -1; /* keep last instruction that changed 'reg' */ 346 int jmptarget = 0; /* any code before this address is conditional */ 347 for (pc = 0; pc < lastpc; pc++) { 348 Instruction i = p->code[pc]; 349 OpCode op = GET_OPCODE(i); 350 int a = GETARG_A(i); 351 switch (op) { 352 case OP_LOADNIL: { 353 int b = GETARG_B(i); 354 if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ 355 setreg = filterpc(pc, jmptarget); 356 break; 357 } 358 case OP_TFORCALL: { 359 if (reg >= a + 2) /* affect all regs above its base */ 360 setreg = filterpc(pc, jmptarget); 361 break; 362 } 363 case OP_CALL: 364 case OP_TAILCALL: { 365 if (reg >= a) /* affect all registers above base */ 366 setreg = filterpc(pc, jmptarget); 367 break; 368 } 369 case OP_JMP: { 370 int b = GETARG_sBx(i); 371 int dest = pc + 1 + b; 372 /* jump is forward and do not skip `lastpc'? */ 373 if (pc < dest && dest <= lastpc) { 374 if (dest > jmptarget) 375 jmptarget = dest; /* update 'jmptarget' */ 376 } 377 break; 378 } 379 default: 380 if (testAMode(op) && reg == a) /* any instruction that set A */ 381 setreg = filterpc(pc, jmptarget); 382 break; 383 } 384 } 385 return setreg; 386 } 387 388 389 static const char *getobjname (Proto *p, int lastpc, int reg, 390 const char **name) { 391 int pc; 392 *name = luaF_getlocalname(p, reg + 1, lastpc); 393 if (*name) /* is a local? */ 394 return "local"; 395 /* else try symbolic execution */ 396 pc = findsetreg(p, lastpc, reg); 397 if (pc != -1) { /* could find instruction? */ 398 Instruction i = p->code[pc]; 399 OpCode op = GET_OPCODE(i); 400 switch (op) { 401 case OP_MOVE: { 402 int b = GETARG_B(i); /* move from 'b' to 'a' */ 403 if (b < GETARG_A(i)) 404 return getobjname(p, pc, b, name); /* get name for 'b' */ 405 break; 406 } 407 case OP_GETTABUP: 408 case OP_GETTABLE: { 409 int k = GETARG_C(i); /* key index */ 410 int t = GETARG_B(i); /* table index */ 411 const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ 412 ? luaF_getlocalname(p, t + 1, pc) 413 : upvalname(p, t); 414 kname(p, pc, k, name); 415 return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; 416 } 417 case OP_GETUPVAL: { 418 *name = upvalname(p, GETARG_B(i)); 419 return "upvalue"; 420 } 421 case OP_LOADK: 422 case OP_LOADKX: { 423 int b = (op == OP_LOADK) ? GETARG_Bx(i) 424 : GETARG_Ax(p->code[pc + 1]); 425 if (ttisstring(&p->k[b])) { 426 *name = svalue(&p->k[b]); 427 return "constant"; 428 } 429 break; 430 } 431 case OP_SELF: { 432 int k = GETARG_C(i); /* key index */ 433 kname(p, pc, k, name); 434 return "method"; 435 } 436 default: break; /* go through to return NULL */ 437 } 438 } 439 return NULL; /* could not find reasonable name */ 440 } 441 442 443 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { 444 TMS tm; 445 Proto *p = ci_func(ci)->p; /* calling function */ 446 int pc = currentpc(ci); /* calling instruction index */ 447 Instruction i = p->code[pc]; /* calling instruction */ 448 switch (GET_OPCODE(i)) { 449 case OP_CALL: 450 case OP_TAILCALL: /* get function name */ 451 return getobjname(p, pc, GETARG_A(i), name); 452 case OP_TFORCALL: { /* for iterator */ 453 *name = "for iterator"; 454 return "for iterator"; 455 } 456 /* all other instructions can call only through metamethods */ 457 case OP_SELF: 458 case OP_GETTABUP: 459 case OP_GETTABLE: tm = TM_INDEX; break; 460 case OP_SETTABUP: 461 case OP_SETTABLE: tm = TM_NEWINDEX; break; 462 case OP_EQ: tm = TM_EQ; break; 463 case OP_ADD: tm = TM_ADD; break; 464 case OP_SUB: tm = TM_SUB; break; 465 case OP_MUL: tm = TM_MUL; break; 466 #ifndef _KERNEL 467 case OP_DIV: tm = TM_DIV; break; 468 #endif 469 case OP_IDIV: tm = TM_IDIV; break; 470 case OP_MOD: tm = TM_MOD; break; 471 #ifndef _KERNEL 472 case OP_POW: tm = TM_POW; break; 473 #endif 474 case OP_UNM: tm = TM_UNM; break; 475 case OP_LEN: tm = TM_LEN; break; 476 case OP_LT: tm = TM_LT; break; 477 case OP_LE: tm = TM_LE; break; 478 case OP_CONCAT: tm = TM_CONCAT; break; 479 default: 480 return NULL; /* else no useful name can be found */ 481 } 482 *name = getstr(G(L)->tmname[tm]); 483 return "metamethod"; 484 } 485 486 /* }====================================================== */ 487 488 489 490 /* 491 ** only ANSI way to check whether a pointer points to an array 492 ** (used only for error messages, so efficiency is not a big concern) 493 */ 494 static int isinstack (CallInfo *ci, const TValue *o) { 495 StkId p; 496 for (p = ci->u.l.base; p < ci->top; p++) 497 if (o == p) return 1; 498 return 0; 499 } 500 501 502 static const char *getupvalname (CallInfo *ci, const TValue *o, 503 const char **name) { 504 LClosure *c = ci_func(ci); 505 int i; 506 for (i = 0; i < c->nupvalues; i++) { 507 if (c->upvals[i]->v == o) { 508 *name = upvalname(c->p, i); 509 return "upvalue"; 510 } 511 } 512 return NULL; 513 } 514 515 516 static const char *varinfo (lua_State *L, const TValue *o) { 517 const char *name; 518 CallInfo *ci = L->ci; 519 const char *kind = NULL; 520 if (isLua(ci)) { 521 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ 522 if (!kind && isinstack(ci, o)) /* no? try a register */ 523 kind = getobjname(ci_func(ci)->p, currentpc(ci), 524 cast_int(o - ci->u.l.base), &name); 525 } 526 return (kind) ? luaO_pushfstring(L, " (%s " LUA_QS ")", kind, name) : ""; 527 } 528 529 530 l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { 531 const char *t = objtypename(o); 532 luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); 533 } 534 535 536 l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { 537 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; 538 lua_assert(!ttisstring(p1) && !ttisnumber(p1)); 539 luaG_typeerror(L, p1, "concatenate"); 540 } 541 542 543 l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { 544 lua_Number temp; 545 if (!tonumber(p1, &temp)) 546 p2 = p1; /* first operand is wrong */ 547 luaG_typeerror(L, p2, "perform arithmetic on"); 548 } 549 550 551 l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { 552 lua_Integer temp; 553 if (!tointeger(p1, &temp)) 554 p2 = p1; 555 luaG_runerror(L, "attempt to convert an out of range float%s to an integer", 556 varinfo(L, p2)); 557 } 558 559 560 l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { 561 const char *t1 = objtypename(p1); 562 const char *t2 = objtypename(p2); 563 if (t1 == t2) 564 luaG_runerror(L, "attempt to compare two %s values", t1); 565 else 566 luaG_runerror(L, "attempt to compare %s with %s", t1, t2); 567 } 568 569 570 static void addinfo (lua_State *L, const char *msg) { 571 CallInfo *ci = L->ci; 572 if (isLua(ci)) { /* is Lua code? */ 573 char buff[LUA_IDSIZE]; /* add file:line information */ 574 int line = currentline(ci); 575 TString *src = ci_func(ci)->p->source; 576 if (src) 577 luaO_chunkid(buff, getstr(src), LUA_IDSIZE); 578 else { /* no source available; use "?" instead */ 579 buff[0] = '?'; buff[1] = '\0'; 580 } 581 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); 582 } 583 } 584 585 586 l_noret luaG_errormsg (lua_State *L) { 587 if (L->errfunc != 0) { /* is there an error handling function? */ 588 StkId errfunc = restorestack(L, L->errfunc); 589 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); 590 setobjs2s(L, L->top, L->top - 1); /* move argument */ 591 setobjs2s(L, L->top - 1, errfunc); /* push function */ 592 L->top++; 593 luaD_call(L, L->top - 2, 1, 0); /* call it */ 594 } 595 luaD_throw(L, LUA_ERRRUN); 596 } 597 598 599 l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { 600 va_list argp; 601 va_start(argp, fmt); 602 addinfo(L, luaO_pushvfstring(L, fmt, argp)); 603 va_end(argp); 604 luaG_errormsg(L); 605 } 606 607 608 void luaG_traceexec (lua_State *L) { 609 CallInfo *ci = L->ci; 610 lu_byte mask = L->hookmask; 611 int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); 612 if (counthook) 613 resethookcount(L); /* reset count */ 614 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ 615 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ 616 return; /* do not call hook again (VM yielded, so it did not move) */ 617 } 618 if (counthook) 619 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ 620 if (mask & LUA_MASKLINE) { 621 Proto *p = ci_func(ci)->p; 622 int npc = pcRel(ci->u.l.savedpc, p); 623 int newline = getfuncline(p, npc); 624 if (npc == 0 || /* call linehook when enter a new function, */ 625 ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ 626 newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ 627 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ 628 } 629 L->oldpc = ci->u.l.savedpc; 630 if (L->status == LUA_YIELD) { /* did hook yield? */ 631 if (counthook) 632 L->hookcount = 1; /* undo decrement to zero */ 633 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ 634 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ 635 ci->func = L->top - 1; /* protect stack below results */ 636 luaD_throw(L, LUA_YIELD); 637 } 638 } 639 640