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