1*11be35a1SLionel Sambuc /* $NetBSD: ldebug.c,v 1.1.1.2 2012/03/15 00:08:08 alnsn Exp $ */ 2*11be35a1SLionel Sambuc 3*11be35a1SLionel Sambuc /* 4*11be35a1SLionel Sambuc ** $Id: ldebug.c,v 1.1.1.2 2012/03/15 00:08:08 alnsn Exp $ 5*11be35a1SLionel Sambuc ** Debug Interface 6*11be35a1SLionel Sambuc ** See Copyright Notice in lua.h 7*11be35a1SLionel Sambuc */ 8*11be35a1SLionel Sambuc 9*11be35a1SLionel Sambuc 10*11be35a1SLionel Sambuc #include <stdarg.h> 11*11be35a1SLionel Sambuc #include <stddef.h> 12*11be35a1SLionel Sambuc #include <string.h> 13*11be35a1SLionel Sambuc 14*11be35a1SLionel Sambuc 15*11be35a1SLionel Sambuc #define ldebug_c 16*11be35a1SLionel Sambuc #define LUA_CORE 17*11be35a1SLionel Sambuc 18*11be35a1SLionel Sambuc #include "lua.h" 19*11be35a1SLionel Sambuc 20*11be35a1SLionel Sambuc #include "lapi.h" 21*11be35a1SLionel Sambuc #include "lcode.h" 22*11be35a1SLionel Sambuc #include "ldebug.h" 23*11be35a1SLionel Sambuc #include "ldo.h" 24*11be35a1SLionel Sambuc #include "lfunc.h" 25*11be35a1SLionel Sambuc #include "lobject.h" 26*11be35a1SLionel Sambuc #include "lopcodes.h" 27*11be35a1SLionel Sambuc #include "lstate.h" 28*11be35a1SLionel Sambuc #include "lstring.h" 29*11be35a1SLionel Sambuc #include "ltable.h" 30*11be35a1SLionel Sambuc #include "ltm.h" 31*11be35a1SLionel Sambuc #include "lvm.h" 32*11be35a1SLionel Sambuc 33*11be35a1SLionel Sambuc 34*11be35a1SLionel Sambuc 35*11be35a1SLionel Sambuc static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); 36*11be35a1SLionel Sambuc 37*11be35a1SLionel Sambuc 38*11be35a1SLionel Sambuc static int currentpc (lua_State *L, CallInfo *ci) { 39*11be35a1SLionel Sambuc if (!isLua(ci)) return -1; /* function is not a Lua function? */ 40*11be35a1SLionel Sambuc if (ci == L->ci) 41*11be35a1SLionel Sambuc ci->savedpc = L->savedpc; 42*11be35a1SLionel Sambuc return pcRel(ci->savedpc, ci_func(ci)->l.p); 43*11be35a1SLionel Sambuc } 44*11be35a1SLionel Sambuc 45*11be35a1SLionel Sambuc 46*11be35a1SLionel Sambuc static int currentline (lua_State *L, CallInfo *ci) { 47*11be35a1SLionel Sambuc int pc = currentpc(L, ci); 48*11be35a1SLionel Sambuc if (pc < 0) 49*11be35a1SLionel Sambuc return -1; /* only active lua functions have current-line information */ 50*11be35a1SLionel Sambuc else 51*11be35a1SLionel Sambuc return getline(ci_func(ci)->l.p, pc); 52*11be35a1SLionel Sambuc } 53*11be35a1SLionel Sambuc 54*11be35a1SLionel Sambuc 55*11be35a1SLionel Sambuc /* 56*11be35a1SLionel Sambuc ** this function can be called asynchronous (e.g. during a signal) 57*11be35a1SLionel Sambuc */ 58*11be35a1SLionel Sambuc LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { 59*11be35a1SLionel Sambuc if (func == NULL || mask == 0) { /* turn off hooks? */ 60*11be35a1SLionel Sambuc mask = 0; 61*11be35a1SLionel Sambuc func = NULL; 62*11be35a1SLionel Sambuc } 63*11be35a1SLionel Sambuc L->hook = func; 64*11be35a1SLionel Sambuc L->basehookcount = count; 65*11be35a1SLionel Sambuc resethookcount(L); 66*11be35a1SLionel Sambuc L->hookmask = cast_byte(mask); 67*11be35a1SLionel Sambuc return 1; 68*11be35a1SLionel Sambuc } 69*11be35a1SLionel Sambuc 70*11be35a1SLionel Sambuc 71*11be35a1SLionel Sambuc LUA_API lua_Hook lua_gethook (lua_State *L) { 72*11be35a1SLionel Sambuc return L->hook; 73*11be35a1SLionel Sambuc } 74*11be35a1SLionel Sambuc 75*11be35a1SLionel Sambuc 76*11be35a1SLionel Sambuc LUA_API int lua_gethookmask (lua_State *L) { 77*11be35a1SLionel Sambuc return L->hookmask; 78*11be35a1SLionel Sambuc } 79*11be35a1SLionel Sambuc 80*11be35a1SLionel Sambuc 81*11be35a1SLionel Sambuc LUA_API int lua_gethookcount (lua_State *L) { 82*11be35a1SLionel Sambuc return L->basehookcount; 83*11be35a1SLionel Sambuc } 84*11be35a1SLionel Sambuc 85*11be35a1SLionel Sambuc 86*11be35a1SLionel Sambuc LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 87*11be35a1SLionel Sambuc int status; 88*11be35a1SLionel Sambuc CallInfo *ci; 89*11be35a1SLionel Sambuc lua_lock(L); 90*11be35a1SLionel Sambuc for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { 91*11be35a1SLionel Sambuc level--; 92*11be35a1SLionel Sambuc if (f_isLua(ci)) /* Lua function? */ 93*11be35a1SLionel Sambuc level -= ci->tailcalls; /* skip lost tail calls */ 94*11be35a1SLionel Sambuc } 95*11be35a1SLionel Sambuc if (level == 0 && ci > L->base_ci) { /* level found? */ 96*11be35a1SLionel Sambuc status = 1; 97*11be35a1SLionel Sambuc ar->i_ci = cast_int(ci - L->base_ci); 98*11be35a1SLionel Sambuc } 99*11be35a1SLionel Sambuc else if (level < 0) { /* level is of a lost tail call? */ 100*11be35a1SLionel Sambuc status = 1; 101*11be35a1SLionel Sambuc ar->i_ci = 0; 102*11be35a1SLionel Sambuc } 103*11be35a1SLionel Sambuc else status = 0; /* no such level */ 104*11be35a1SLionel Sambuc lua_unlock(L); 105*11be35a1SLionel Sambuc return status; 106*11be35a1SLionel Sambuc } 107*11be35a1SLionel Sambuc 108*11be35a1SLionel Sambuc 109*11be35a1SLionel Sambuc static Proto *getluaproto (CallInfo *ci) { 110*11be35a1SLionel Sambuc return (isLua(ci) ? ci_func(ci)->l.p : NULL); 111*11be35a1SLionel Sambuc } 112*11be35a1SLionel Sambuc 113*11be35a1SLionel Sambuc 114*11be35a1SLionel Sambuc static const char *findlocal (lua_State *L, CallInfo *ci, int n) { 115*11be35a1SLionel Sambuc const char *name; 116*11be35a1SLionel Sambuc Proto *fp = getluaproto(ci); 117*11be35a1SLionel Sambuc if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) 118*11be35a1SLionel Sambuc return name; /* is a local variable in a Lua function */ 119*11be35a1SLionel Sambuc else { 120*11be35a1SLionel Sambuc StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; 121*11be35a1SLionel Sambuc if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 122*11be35a1SLionel Sambuc return "(*temporary)"; 123*11be35a1SLionel Sambuc else 124*11be35a1SLionel Sambuc return NULL; 125*11be35a1SLionel Sambuc } 126*11be35a1SLionel Sambuc } 127*11be35a1SLionel Sambuc 128*11be35a1SLionel Sambuc 129*11be35a1SLionel Sambuc LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 130*11be35a1SLionel Sambuc CallInfo *ci = L->base_ci + ar->i_ci; 131*11be35a1SLionel Sambuc const char *name = findlocal(L, ci, n); 132*11be35a1SLionel Sambuc lua_lock(L); 133*11be35a1SLionel Sambuc if (name) 134*11be35a1SLionel Sambuc luaA_pushobject(L, ci->base + (n - 1)); 135*11be35a1SLionel Sambuc lua_unlock(L); 136*11be35a1SLionel Sambuc return name; 137*11be35a1SLionel Sambuc } 138*11be35a1SLionel Sambuc 139*11be35a1SLionel Sambuc 140*11be35a1SLionel Sambuc LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 141*11be35a1SLionel Sambuc CallInfo *ci = L->base_ci + ar->i_ci; 142*11be35a1SLionel Sambuc const char *name = findlocal(L, ci, n); 143*11be35a1SLionel Sambuc lua_lock(L); 144*11be35a1SLionel Sambuc if (name) 145*11be35a1SLionel Sambuc setobjs2s(L, ci->base + (n - 1), L->top - 1); 146*11be35a1SLionel Sambuc L->top--; /* pop value */ 147*11be35a1SLionel Sambuc lua_unlock(L); 148*11be35a1SLionel Sambuc return name; 149*11be35a1SLionel Sambuc } 150*11be35a1SLionel Sambuc 151*11be35a1SLionel Sambuc 152*11be35a1SLionel Sambuc static void funcinfo (lua_Debug *ar, Closure *cl) { 153*11be35a1SLionel Sambuc if (cl->c.isC) { 154*11be35a1SLionel Sambuc ar->source = "=[C]"; 155*11be35a1SLionel Sambuc ar->linedefined = -1; 156*11be35a1SLionel Sambuc ar->lastlinedefined = -1; 157*11be35a1SLionel Sambuc ar->what = "C"; 158*11be35a1SLionel Sambuc } 159*11be35a1SLionel Sambuc else { 160*11be35a1SLionel Sambuc ar->source = getstr(cl->l.p->source); 161*11be35a1SLionel Sambuc ar->linedefined = cl->l.p->linedefined; 162*11be35a1SLionel Sambuc ar->lastlinedefined = cl->l.p->lastlinedefined; 163*11be35a1SLionel Sambuc ar->what = (ar->linedefined == 0) ? "main" : "Lua"; 164*11be35a1SLionel Sambuc } 165*11be35a1SLionel Sambuc luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 166*11be35a1SLionel Sambuc } 167*11be35a1SLionel Sambuc 168*11be35a1SLionel Sambuc 169*11be35a1SLionel Sambuc static void info_tailcall (lua_Debug *ar) { 170*11be35a1SLionel Sambuc ar->name = ar->namewhat = ""; 171*11be35a1SLionel Sambuc ar->what = "tail"; 172*11be35a1SLionel Sambuc ar->lastlinedefined = ar->linedefined = ar->currentline = -1; 173*11be35a1SLionel Sambuc ar->source = "=(tail call)"; 174*11be35a1SLionel Sambuc luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 175*11be35a1SLionel Sambuc ar->nups = 0; 176*11be35a1SLionel Sambuc } 177*11be35a1SLionel Sambuc 178*11be35a1SLionel Sambuc 179*11be35a1SLionel Sambuc static void collectvalidlines (lua_State *L, Closure *f) { 180*11be35a1SLionel Sambuc if (f == NULL || f->c.isC) { 181*11be35a1SLionel Sambuc setnilvalue(L->top); 182*11be35a1SLionel Sambuc } 183*11be35a1SLionel Sambuc else { 184*11be35a1SLionel Sambuc Table *t = luaH_new(L, 0, 0); 185*11be35a1SLionel Sambuc int *lineinfo = f->l.p->lineinfo; 186*11be35a1SLionel Sambuc int i; 187*11be35a1SLionel Sambuc for (i=0; i<f->l.p->sizelineinfo; i++) 188*11be35a1SLionel Sambuc setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); 189*11be35a1SLionel Sambuc sethvalue(L, L->top, t); 190*11be35a1SLionel Sambuc } 191*11be35a1SLionel Sambuc incr_top(L); 192*11be35a1SLionel Sambuc } 193*11be35a1SLionel Sambuc 194*11be35a1SLionel Sambuc 195*11be35a1SLionel Sambuc static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, 196*11be35a1SLionel Sambuc Closure *f, CallInfo *ci) { 197*11be35a1SLionel Sambuc int status = 1; 198*11be35a1SLionel Sambuc if (f == NULL) { 199*11be35a1SLionel Sambuc info_tailcall(ar); 200*11be35a1SLionel Sambuc return status; 201*11be35a1SLionel Sambuc } 202*11be35a1SLionel Sambuc for (; *what; what++) { 203*11be35a1SLionel Sambuc switch (*what) { 204*11be35a1SLionel Sambuc case 'S': { 205*11be35a1SLionel Sambuc funcinfo(ar, f); 206*11be35a1SLionel Sambuc break; 207*11be35a1SLionel Sambuc } 208*11be35a1SLionel Sambuc case 'l': { 209*11be35a1SLionel Sambuc ar->currentline = (ci) ? currentline(L, ci) : -1; 210*11be35a1SLionel Sambuc break; 211*11be35a1SLionel Sambuc } 212*11be35a1SLionel Sambuc case 'u': { 213*11be35a1SLionel Sambuc ar->nups = f->c.nupvalues; 214*11be35a1SLionel Sambuc break; 215*11be35a1SLionel Sambuc } 216*11be35a1SLionel Sambuc case 'n': { 217*11be35a1SLionel Sambuc ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; 218*11be35a1SLionel Sambuc if (ar->namewhat == NULL) { 219*11be35a1SLionel Sambuc ar->namewhat = ""; /* not found */ 220*11be35a1SLionel Sambuc ar->name = NULL; 221*11be35a1SLionel Sambuc } 222*11be35a1SLionel Sambuc break; 223*11be35a1SLionel Sambuc } 224*11be35a1SLionel Sambuc case 'L': 225*11be35a1SLionel Sambuc case 'f': /* handled by lua_getinfo */ 226*11be35a1SLionel Sambuc break; 227*11be35a1SLionel Sambuc default: status = 0; /* invalid option */ 228*11be35a1SLionel Sambuc } 229*11be35a1SLionel Sambuc } 230*11be35a1SLionel Sambuc return status; 231*11be35a1SLionel Sambuc } 232*11be35a1SLionel Sambuc 233*11be35a1SLionel Sambuc 234*11be35a1SLionel Sambuc LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 235*11be35a1SLionel Sambuc int status; 236*11be35a1SLionel Sambuc Closure *f = NULL; 237*11be35a1SLionel Sambuc CallInfo *ci = NULL; 238*11be35a1SLionel Sambuc lua_lock(L); 239*11be35a1SLionel Sambuc if (*what == '>') { 240*11be35a1SLionel Sambuc StkId func = L->top - 1; 241*11be35a1SLionel Sambuc luai_apicheck(L, ttisfunction(func)); 242*11be35a1SLionel Sambuc what++; /* skip the '>' */ 243*11be35a1SLionel Sambuc f = clvalue(func); 244*11be35a1SLionel Sambuc L->top--; /* pop function */ 245*11be35a1SLionel Sambuc } 246*11be35a1SLionel Sambuc else if (ar->i_ci != 0) { /* no tail call? */ 247*11be35a1SLionel Sambuc ci = L->base_ci + ar->i_ci; 248*11be35a1SLionel Sambuc lua_assert(ttisfunction(ci->func)); 249*11be35a1SLionel Sambuc f = clvalue(ci->func); 250*11be35a1SLionel Sambuc } 251*11be35a1SLionel Sambuc status = auxgetinfo(L, what, ar, f, ci); 252*11be35a1SLionel Sambuc if (strchr(what, 'f')) { 253*11be35a1SLionel Sambuc if (f == NULL) setnilvalue(L->top); 254*11be35a1SLionel Sambuc else setclvalue(L, L->top, f); 255*11be35a1SLionel Sambuc incr_top(L); 256*11be35a1SLionel Sambuc } 257*11be35a1SLionel Sambuc if (strchr(what, 'L')) 258*11be35a1SLionel Sambuc collectvalidlines(L, f); 259*11be35a1SLionel Sambuc lua_unlock(L); 260*11be35a1SLionel Sambuc return status; 261*11be35a1SLionel Sambuc } 262*11be35a1SLionel Sambuc 263*11be35a1SLionel Sambuc 264*11be35a1SLionel Sambuc /* 265*11be35a1SLionel Sambuc ** {====================================================== 266*11be35a1SLionel Sambuc ** Symbolic Execution and code checker 267*11be35a1SLionel Sambuc ** ======================================================= 268*11be35a1SLionel Sambuc */ 269*11be35a1SLionel Sambuc 270*11be35a1SLionel Sambuc #define check(x) if (!(x)) return 0; 271*11be35a1SLionel Sambuc 272*11be35a1SLionel Sambuc #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) 273*11be35a1SLionel Sambuc 274*11be35a1SLionel Sambuc #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) 275*11be35a1SLionel Sambuc 276*11be35a1SLionel Sambuc 277*11be35a1SLionel Sambuc 278*11be35a1SLionel Sambuc static int precheck (const Proto *pt) { 279*11be35a1SLionel Sambuc check(pt->maxstacksize <= MAXSTACK); 280*11be35a1SLionel Sambuc check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); 281*11be35a1SLionel Sambuc check(!(pt->is_vararg & VARARG_NEEDSARG) || 282*11be35a1SLionel Sambuc (pt->is_vararg & VARARG_HASARG)); 283*11be35a1SLionel Sambuc check(pt->sizeupvalues <= pt->nups); 284*11be35a1SLionel Sambuc check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); 285*11be35a1SLionel Sambuc check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); 286*11be35a1SLionel Sambuc return 1; 287*11be35a1SLionel Sambuc } 288*11be35a1SLionel Sambuc 289*11be35a1SLionel Sambuc 290*11be35a1SLionel Sambuc #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) 291*11be35a1SLionel Sambuc 292*11be35a1SLionel Sambuc int luaG_checkopenop (Instruction i) { 293*11be35a1SLionel Sambuc switch (GET_OPCODE(i)) { 294*11be35a1SLionel Sambuc case OP_CALL: 295*11be35a1SLionel Sambuc case OP_TAILCALL: 296*11be35a1SLionel Sambuc case OP_RETURN: 297*11be35a1SLionel Sambuc case OP_SETLIST: { 298*11be35a1SLionel Sambuc check(GETARG_B(i) == 0); 299*11be35a1SLionel Sambuc return 1; 300*11be35a1SLionel Sambuc } 301*11be35a1SLionel Sambuc default: return 0; /* invalid instruction after an open call */ 302*11be35a1SLionel Sambuc } 303*11be35a1SLionel Sambuc } 304*11be35a1SLionel Sambuc 305*11be35a1SLionel Sambuc 306*11be35a1SLionel Sambuc static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { 307*11be35a1SLionel Sambuc switch (mode) { 308*11be35a1SLionel Sambuc case OpArgN: check(r == 0); break; 309*11be35a1SLionel Sambuc case OpArgU: break; 310*11be35a1SLionel Sambuc case OpArgR: checkreg(pt, r); break; 311*11be35a1SLionel Sambuc case OpArgK: 312*11be35a1SLionel Sambuc check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); 313*11be35a1SLionel Sambuc break; 314*11be35a1SLionel Sambuc } 315*11be35a1SLionel Sambuc return 1; 316*11be35a1SLionel Sambuc } 317*11be35a1SLionel Sambuc 318*11be35a1SLionel Sambuc 319*11be35a1SLionel Sambuc static Instruction symbexec (const Proto *pt, int lastpc, int reg) { 320*11be35a1SLionel Sambuc int pc; 321*11be35a1SLionel Sambuc int last; /* stores position of last instruction that changed `reg' */ 322*11be35a1SLionel Sambuc last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ 323*11be35a1SLionel Sambuc check(precheck(pt)); 324*11be35a1SLionel Sambuc for (pc = 0; pc < lastpc; pc++) { 325*11be35a1SLionel Sambuc Instruction i = pt->code[pc]; 326*11be35a1SLionel Sambuc OpCode op = GET_OPCODE(i); 327*11be35a1SLionel Sambuc int a = GETARG_A(i); 328*11be35a1SLionel Sambuc int b = 0; 329*11be35a1SLionel Sambuc int c = 0; 330*11be35a1SLionel Sambuc check(op < NUM_OPCODES); 331*11be35a1SLionel Sambuc checkreg(pt, a); 332*11be35a1SLionel Sambuc switch (getOpMode(op)) { 333*11be35a1SLionel Sambuc case iABC: { 334*11be35a1SLionel Sambuc b = GETARG_B(i); 335*11be35a1SLionel Sambuc c = GETARG_C(i); 336*11be35a1SLionel Sambuc check(checkArgMode(pt, b, getBMode(op))); 337*11be35a1SLionel Sambuc check(checkArgMode(pt, c, getCMode(op))); 338*11be35a1SLionel Sambuc break; 339*11be35a1SLionel Sambuc } 340*11be35a1SLionel Sambuc case iABx: { 341*11be35a1SLionel Sambuc b = GETARG_Bx(i); 342*11be35a1SLionel Sambuc if (getBMode(op) == OpArgK) check(b < pt->sizek); 343*11be35a1SLionel Sambuc break; 344*11be35a1SLionel Sambuc } 345*11be35a1SLionel Sambuc case iAsBx: { 346*11be35a1SLionel Sambuc b = GETARG_sBx(i); 347*11be35a1SLionel Sambuc if (getBMode(op) == OpArgR) { 348*11be35a1SLionel Sambuc int dest = pc+1+b; 349*11be35a1SLionel Sambuc check(0 <= dest && dest < pt->sizecode); 350*11be35a1SLionel Sambuc if (dest > 0) { 351*11be35a1SLionel Sambuc int j; 352*11be35a1SLionel Sambuc /* check that it does not jump to a setlist count; this 353*11be35a1SLionel Sambuc is tricky, because the count from a previous setlist may 354*11be35a1SLionel Sambuc have the same value of an invalid setlist; so, we must 355*11be35a1SLionel Sambuc go all the way back to the first of them (if any) */ 356*11be35a1SLionel Sambuc for (j = 0; j < dest; j++) { 357*11be35a1SLionel Sambuc Instruction d = pt->code[dest-1-j]; 358*11be35a1SLionel Sambuc if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; 359*11be35a1SLionel Sambuc } 360*11be35a1SLionel Sambuc /* if 'j' is even, previous value is not a setlist (even if 361*11be35a1SLionel Sambuc it looks like one) */ 362*11be35a1SLionel Sambuc check((j&1) == 0); 363*11be35a1SLionel Sambuc } 364*11be35a1SLionel Sambuc } 365*11be35a1SLionel Sambuc break; 366*11be35a1SLionel Sambuc } 367*11be35a1SLionel Sambuc } 368*11be35a1SLionel Sambuc if (testAMode(op)) { 369*11be35a1SLionel Sambuc if (a == reg) last = pc; /* change register `a' */ 370*11be35a1SLionel Sambuc } 371*11be35a1SLionel Sambuc if (testTMode(op)) { 372*11be35a1SLionel Sambuc check(pc+2 < pt->sizecode); /* check skip */ 373*11be35a1SLionel Sambuc check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); 374*11be35a1SLionel Sambuc } 375*11be35a1SLionel Sambuc switch (op) { 376*11be35a1SLionel Sambuc case OP_LOADBOOL: { 377*11be35a1SLionel Sambuc if (c == 1) { /* does it jump? */ 378*11be35a1SLionel Sambuc check(pc+2 < pt->sizecode); /* check its jump */ 379*11be35a1SLionel Sambuc check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || 380*11be35a1SLionel Sambuc GETARG_C(pt->code[pc+1]) != 0); 381*11be35a1SLionel Sambuc } 382*11be35a1SLionel Sambuc break; 383*11be35a1SLionel Sambuc } 384*11be35a1SLionel Sambuc case OP_LOADNIL: { 385*11be35a1SLionel Sambuc if (a <= reg && reg <= b) 386*11be35a1SLionel Sambuc last = pc; /* set registers from `a' to `b' */ 387*11be35a1SLionel Sambuc break; 388*11be35a1SLionel Sambuc } 389*11be35a1SLionel Sambuc case OP_GETUPVAL: 390*11be35a1SLionel Sambuc case OP_SETUPVAL: { 391*11be35a1SLionel Sambuc check(b < pt->nups); 392*11be35a1SLionel Sambuc break; 393*11be35a1SLionel Sambuc } 394*11be35a1SLionel Sambuc case OP_GETGLOBAL: 395*11be35a1SLionel Sambuc case OP_SETGLOBAL: { 396*11be35a1SLionel Sambuc check(ttisstring(&pt->k[b])); 397*11be35a1SLionel Sambuc break; 398*11be35a1SLionel Sambuc } 399*11be35a1SLionel Sambuc case OP_SELF: { 400*11be35a1SLionel Sambuc checkreg(pt, a+1); 401*11be35a1SLionel Sambuc if (reg == a+1) last = pc; 402*11be35a1SLionel Sambuc break; 403*11be35a1SLionel Sambuc } 404*11be35a1SLionel Sambuc case OP_CONCAT: { 405*11be35a1SLionel Sambuc check(b < c); /* at least two operands */ 406*11be35a1SLionel Sambuc break; 407*11be35a1SLionel Sambuc } 408*11be35a1SLionel Sambuc case OP_TFORLOOP: { 409*11be35a1SLionel Sambuc check(c >= 1); /* at least one result (control variable) */ 410*11be35a1SLionel Sambuc checkreg(pt, a+2+c); /* space for results */ 411*11be35a1SLionel Sambuc if (reg >= a+2) last = pc; /* affect all regs above its base */ 412*11be35a1SLionel Sambuc break; 413*11be35a1SLionel Sambuc } 414*11be35a1SLionel Sambuc case OP_FORLOOP: 415*11be35a1SLionel Sambuc case OP_FORPREP: 416*11be35a1SLionel Sambuc checkreg(pt, a+3); 417*11be35a1SLionel Sambuc /* go through */ 418*11be35a1SLionel Sambuc case OP_JMP: { 419*11be35a1SLionel Sambuc int dest = pc+1+b; 420*11be35a1SLionel Sambuc /* not full check and jump is forward and do not skip `lastpc'? */ 421*11be35a1SLionel Sambuc if (reg != NO_REG && pc < dest && dest <= lastpc) 422*11be35a1SLionel Sambuc pc += b; /* do the jump */ 423*11be35a1SLionel Sambuc break; 424*11be35a1SLionel Sambuc } 425*11be35a1SLionel Sambuc case OP_CALL: 426*11be35a1SLionel Sambuc case OP_TAILCALL: { 427*11be35a1SLionel Sambuc if (b != 0) { 428*11be35a1SLionel Sambuc checkreg(pt, a+b-1); 429*11be35a1SLionel Sambuc } 430*11be35a1SLionel Sambuc c--; /* c = num. returns */ 431*11be35a1SLionel Sambuc if (c == LUA_MULTRET) { 432*11be35a1SLionel Sambuc check(checkopenop(pt, pc)); 433*11be35a1SLionel Sambuc } 434*11be35a1SLionel Sambuc else if (c != 0) 435*11be35a1SLionel Sambuc checkreg(pt, a+c-1); 436*11be35a1SLionel Sambuc if (reg >= a) last = pc; /* affect all registers above base */ 437*11be35a1SLionel Sambuc break; 438*11be35a1SLionel Sambuc } 439*11be35a1SLionel Sambuc case OP_RETURN: { 440*11be35a1SLionel Sambuc b--; /* b = num. returns */ 441*11be35a1SLionel Sambuc if (b > 0) checkreg(pt, a+b-1); 442*11be35a1SLionel Sambuc break; 443*11be35a1SLionel Sambuc } 444*11be35a1SLionel Sambuc case OP_SETLIST: { 445*11be35a1SLionel Sambuc if (b > 0) checkreg(pt, a + b); 446*11be35a1SLionel Sambuc if (c == 0) { 447*11be35a1SLionel Sambuc pc++; 448*11be35a1SLionel Sambuc check(pc < pt->sizecode - 1); 449*11be35a1SLionel Sambuc } 450*11be35a1SLionel Sambuc break; 451*11be35a1SLionel Sambuc } 452*11be35a1SLionel Sambuc case OP_CLOSURE: { 453*11be35a1SLionel Sambuc int nup, j; 454*11be35a1SLionel Sambuc check(b < pt->sizep); 455*11be35a1SLionel Sambuc nup = pt->p[b]->nups; 456*11be35a1SLionel Sambuc check(pc + nup < pt->sizecode); 457*11be35a1SLionel Sambuc for (j = 1; j <= nup; j++) { 458*11be35a1SLionel Sambuc OpCode op1 = GET_OPCODE(pt->code[pc + j]); 459*11be35a1SLionel Sambuc check(op1 == OP_GETUPVAL || op1 == OP_MOVE); 460*11be35a1SLionel Sambuc } 461*11be35a1SLionel Sambuc if (reg != NO_REG) /* tracing? */ 462*11be35a1SLionel Sambuc pc += nup; /* do not 'execute' these pseudo-instructions */ 463*11be35a1SLionel Sambuc break; 464*11be35a1SLionel Sambuc } 465*11be35a1SLionel Sambuc case OP_VARARG: { 466*11be35a1SLionel Sambuc check((pt->is_vararg & VARARG_ISVARARG) && 467*11be35a1SLionel Sambuc !(pt->is_vararg & VARARG_NEEDSARG)); 468*11be35a1SLionel Sambuc b--; 469*11be35a1SLionel Sambuc if (b == LUA_MULTRET) check(checkopenop(pt, pc)); 470*11be35a1SLionel Sambuc checkreg(pt, a+b-1); 471*11be35a1SLionel Sambuc break; 472*11be35a1SLionel Sambuc } 473*11be35a1SLionel Sambuc default: break; 474*11be35a1SLionel Sambuc } 475*11be35a1SLionel Sambuc } 476*11be35a1SLionel Sambuc return pt->code[last]; 477*11be35a1SLionel Sambuc } 478*11be35a1SLionel Sambuc 479*11be35a1SLionel Sambuc #undef check 480*11be35a1SLionel Sambuc #undef checkjump 481*11be35a1SLionel Sambuc #undef checkreg 482*11be35a1SLionel Sambuc 483*11be35a1SLionel Sambuc /* }====================================================== */ 484*11be35a1SLionel Sambuc 485*11be35a1SLionel Sambuc 486*11be35a1SLionel Sambuc int luaG_checkcode (const Proto *pt) { 487*11be35a1SLionel Sambuc return (symbexec(pt, pt->sizecode, NO_REG) != 0); 488*11be35a1SLionel Sambuc } 489*11be35a1SLionel Sambuc 490*11be35a1SLionel Sambuc 491*11be35a1SLionel Sambuc static const char *kname (Proto *p, int c) { 492*11be35a1SLionel Sambuc if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) 493*11be35a1SLionel Sambuc return svalue(&p->k[INDEXK(c)]); 494*11be35a1SLionel Sambuc else 495*11be35a1SLionel Sambuc return "?"; 496*11be35a1SLionel Sambuc } 497*11be35a1SLionel Sambuc 498*11be35a1SLionel Sambuc 499*11be35a1SLionel Sambuc static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, 500*11be35a1SLionel Sambuc const char **name) { 501*11be35a1SLionel Sambuc if (isLua(ci)) { /* a Lua function? */ 502*11be35a1SLionel Sambuc Proto *p = ci_func(ci)->l.p; 503*11be35a1SLionel Sambuc int pc = currentpc(L, ci); 504*11be35a1SLionel Sambuc Instruction i; 505*11be35a1SLionel Sambuc *name = luaF_getlocalname(p, stackpos+1, pc); 506*11be35a1SLionel Sambuc if (*name) /* is a local? */ 507*11be35a1SLionel Sambuc return "local"; 508*11be35a1SLionel Sambuc i = symbexec(p, pc, stackpos); /* try symbolic execution */ 509*11be35a1SLionel Sambuc lua_assert(pc != -1); 510*11be35a1SLionel Sambuc switch (GET_OPCODE(i)) { 511*11be35a1SLionel Sambuc case OP_GETGLOBAL: { 512*11be35a1SLionel Sambuc int g = GETARG_Bx(i); /* global index */ 513*11be35a1SLionel Sambuc lua_assert(ttisstring(&p->k[g])); 514*11be35a1SLionel Sambuc *name = svalue(&p->k[g]); 515*11be35a1SLionel Sambuc return "global"; 516*11be35a1SLionel Sambuc } 517*11be35a1SLionel Sambuc case OP_MOVE: { 518*11be35a1SLionel Sambuc int a = GETARG_A(i); 519*11be35a1SLionel Sambuc int b = GETARG_B(i); /* move from `b' to `a' */ 520*11be35a1SLionel Sambuc if (b < a) 521*11be35a1SLionel Sambuc return getobjname(L, ci, b, name); /* get name for `b' */ 522*11be35a1SLionel Sambuc break; 523*11be35a1SLionel Sambuc } 524*11be35a1SLionel Sambuc case OP_GETTABLE: { 525*11be35a1SLionel Sambuc int k = GETARG_C(i); /* key index */ 526*11be35a1SLionel Sambuc *name = kname(p, k); 527*11be35a1SLionel Sambuc return "field"; 528*11be35a1SLionel Sambuc } 529*11be35a1SLionel Sambuc case OP_GETUPVAL: { 530*11be35a1SLionel Sambuc int u = GETARG_B(i); /* upvalue index */ 531*11be35a1SLionel Sambuc *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; 532*11be35a1SLionel Sambuc return "upvalue"; 533*11be35a1SLionel Sambuc } 534*11be35a1SLionel Sambuc case OP_SELF: { 535*11be35a1SLionel Sambuc int k = GETARG_C(i); /* key index */ 536*11be35a1SLionel Sambuc *name = kname(p, k); 537*11be35a1SLionel Sambuc return "method"; 538*11be35a1SLionel Sambuc } 539*11be35a1SLionel Sambuc default: break; 540*11be35a1SLionel Sambuc } 541*11be35a1SLionel Sambuc } 542*11be35a1SLionel Sambuc return NULL; /* no useful name found */ 543*11be35a1SLionel Sambuc } 544*11be35a1SLionel Sambuc 545*11be35a1SLionel Sambuc 546*11be35a1SLionel Sambuc static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { 547*11be35a1SLionel Sambuc Instruction i; 548*11be35a1SLionel Sambuc if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) 549*11be35a1SLionel Sambuc return NULL; /* calling function is not Lua (or is unknown) */ 550*11be35a1SLionel Sambuc ci--; /* calling function */ 551*11be35a1SLionel Sambuc i = ci_func(ci)->l.p->code[currentpc(L, ci)]; 552*11be35a1SLionel Sambuc if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || 553*11be35a1SLionel Sambuc GET_OPCODE(i) == OP_TFORLOOP) 554*11be35a1SLionel Sambuc return getobjname(L, ci, GETARG_A(i), name); 555*11be35a1SLionel Sambuc else 556*11be35a1SLionel Sambuc return NULL; /* no useful name can be found */ 557*11be35a1SLionel Sambuc } 558*11be35a1SLionel Sambuc 559*11be35a1SLionel Sambuc 560*11be35a1SLionel Sambuc /* only ANSI way to check whether a pointer points to an array */ 561*11be35a1SLionel Sambuc static int isinstack (CallInfo *ci, const TValue *o) { 562*11be35a1SLionel Sambuc StkId p; 563*11be35a1SLionel Sambuc for (p = ci->base; p < ci->top; p++) 564*11be35a1SLionel Sambuc if (o == p) return 1; 565*11be35a1SLionel Sambuc return 0; 566*11be35a1SLionel Sambuc } 567*11be35a1SLionel Sambuc 568*11be35a1SLionel Sambuc 569*11be35a1SLionel Sambuc void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { 570*11be35a1SLionel Sambuc const char *name = NULL; 571*11be35a1SLionel Sambuc const char *t = luaT_typenames[ttype(o)]; 572*11be35a1SLionel Sambuc const char *kind = (isinstack(L->ci, o)) ? 573*11be35a1SLionel Sambuc getobjname(L, L->ci, cast_int(o - L->base), &name) : 574*11be35a1SLionel Sambuc NULL; 575*11be35a1SLionel Sambuc if (kind) 576*11be35a1SLionel Sambuc luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", 577*11be35a1SLionel Sambuc op, kind, name, t); 578*11be35a1SLionel Sambuc else 579*11be35a1SLionel Sambuc luaG_runerror(L, "attempt to %s a %s value", op, t); 580*11be35a1SLionel Sambuc } 581*11be35a1SLionel Sambuc 582*11be35a1SLionel Sambuc 583*11be35a1SLionel Sambuc void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { 584*11be35a1SLionel Sambuc if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; 585*11be35a1SLionel Sambuc lua_assert(!ttisstring(p1) && !ttisnumber(p1)); 586*11be35a1SLionel Sambuc luaG_typeerror(L, p1, "concatenate"); 587*11be35a1SLionel Sambuc } 588*11be35a1SLionel Sambuc 589*11be35a1SLionel Sambuc 590*11be35a1SLionel Sambuc void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { 591*11be35a1SLionel Sambuc TValue temp; 592*11be35a1SLionel Sambuc if (luaV_tonumber(p1, &temp) == NULL) 593*11be35a1SLionel Sambuc p2 = p1; /* first operand is wrong */ 594*11be35a1SLionel Sambuc luaG_typeerror(L, p2, "perform arithmetic on"); 595*11be35a1SLionel Sambuc } 596*11be35a1SLionel Sambuc 597*11be35a1SLionel Sambuc 598*11be35a1SLionel Sambuc int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { 599*11be35a1SLionel Sambuc const char *t1 = luaT_typenames[ttype(p1)]; 600*11be35a1SLionel Sambuc const char *t2 = luaT_typenames[ttype(p2)]; 601*11be35a1SLionel Sambuc if (t1[2] == t2[2]) 602*11be35a1SLionel Sambuc luaG_runerror(L, "attempt to compare two %s values", t1); 603*11be35a1SLionel Sambuc else 604*11be35a1SLionel Sambuc luaG_runerror(L, "attempt to compare %s with %s", t1, t2); 605*11be35a1SLionel Sambuc return 0; 606*11be35a1SLionel Sambuc } 607*11be35a1SLionel Sambuc 608*11be35a1SLionel Sambuc 609*11be35a1SLionel Sambuc static void addinfo (lua_State *L, const char *msg) { 610*11be35a1SLionel Sambuc CallInfo *ci = L->ci; 611*11be35a1SLionel Sambuc if (isLua(ci)) { /* is Lua code? */ 612*11be35a1SLionel Sambuc char buff[LUA_IDSIZE]; /* add file:line information */ 613*11be35a1SLionel Sambuc int line = currentline(L, ci); 614*11be35a1SLionel Sambuc luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); 615*11be35a1SLionel Sambuc luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); 616*11be35a1SLionel Sambuc } 617*11be35a1SLionel Sambuc } 618*11be35a1SLionel Sambuc 619*11be35a1SLionel Sambuc 620*11be35a1SLionel Sambuc void luaG_errormsg (lua_State *L) { 621*11be35a1SLionel Sambuc if (L->errfunc != 0) { /* is there an error handling function? */ 622*11be35a1SLionel Sambuc StkId errfunc = restorestack(L, L->errfunc); 623*11be35a1SLionel Sambuc if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); 624*11be35a1SLionel Sambuc setobjs2s(L, L->top, L->top - 1); /* move argument */ 625*11be35a1SLionel Sambuc setobjs2s(L, L->top - 1, errfunc); /* push function */ 626*11be35a1SLionel Sambuc incr_top(L); 627*11be35a1SLionel Sambuc luaD_call(L, L->top - 2, 1); /* call it */ 628*11be35a1SLionel Sambuc } 629*11be35a1SLionel Sambuc luaD_throw(L, LUA_ERRRUN); 630*11be35a1SLionel Sambuc } 631*11be35a1SLionel Sambuc 632*11be35a1SLionel Sambuc 633*11be35a1SLionel Sambuc void luaG_runerror (lua_State *L, const char *fmt, ...) { 634*11be35a1SLionel Sambuc va_list argp; 635*11be35a1SLionel Sambuc va_start(argp, fmt); 636*11be35a1SLionel Sambuc addinfo(L, luaO_pushvfstring(L, fmt, argp)); 637*11be35a1SLionel Sambuc va_end(argp); 638*11be35a1SLionel Sambuc luaG_errormsg(L); 639*11be35a1SLionel Sambuc } 640*11be35a1SLionel Sambuc 641