• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

lua/ldebug.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: ldebug.c,v 1.3 2007/07/15 17:56:10 rse Exp $
00003 ** Debug Interface
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #include <stdarg.h>
00009 #include <stddef.h>
00010 #include <string.h>
00011 
00012 
00013 #define ldebug_c
00014 #define LUA_CORE
00015 
00016 #include "lua.h"
00017 
00018 #include "lapi.h"
00019 #include "lcode.h"
00020 #include "ldebug.h"
00021 #include "ldo.h"
00022 #include "lfunc.h"
00023 #include "lobject.h"
00024 #include "lopcodes.h"
00025 #include "lstate.h"
00026 #include "lstring.h"
00027 #include "ltable.h"
00028 #include "ltm.h"
00029 #include "lvm.h"
00030 
00031 
00032 
00033 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
00034 
00035 
00036 static int currentpc (lua_State *L, CallInfo *ci) {
00037   if (!isLua(ci)) return -1;  /* function is not a Lua function? */
00038   if (ci == L->ci)
00039     ci->savedpc = L->savedpc;
00040   return pcRel(ci->savedpc, ci_func(ci)->l.p);
00041 }
00042 
00043 
00044 static int currentline (lua_State *L, CallInfo *ci) {
00045   int pc = currentpc(L, ci);
00046   if (pc < 0)
00047     return -1;  /* only active lua functions have current-line information */
00048   else
00049     return getline(ci_func(ci)->l.p, pc);
00050 }
00051 
00052 
00053 /*
00054 ** this function can be called asynchronous (e.g. during a signal)
00055 */
00056 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
00057   if (func == NULL || mask == 0) {  /* turn off hooks? */
00058     mask = 0;
00059     func = NULL;
00060   }
00061   L->hook = func;
00062   L->basehookcount = count;
00063   resethookcount(L);
00064   L->hookmask = cast_byte(mask);
00065   return 1;
00066 }
00067 
00068 
00069 LUA_API lua_Hook lua_gethook (lua_State *L) {
00070   return L->hook;
00071 }
00072 
00073 
00074 LUA_API int lua_gethookmask (lua_State *L) {
00075   return L->hookmask;
00076 }
00077 
00078 
00079 LUA_API int lua_gethookcount (lua_State *L) {
00080   return L->basehookcount;
00081 }
00082 
00083 
00084 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
00085   int status;
00086   CallInfo *ci;
00087   lua_lock(L);
00088   for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
00089     level--;
00090     if (f_isLua(ci))  /* Lua function? */
00091       level -= ci->tailcalls;  /* skip lost tail calls */
00092   }
00093   if (level == 0 && ci > L->base_ci) {  /* level found? */
00094     status = 1;
00095     ar->i_ci = cast_int(ci - L->base_ci);
00096   }
00097   else if (level < 0) {  /* level is of a lost tail call? */
00098     status = 1;
00099     ar->i_ci = 0;
00100   }
00101   else status = 0;  /* no such level */
00102   lua_unlock(L);
00103   return status;
00104 }
00105 
00106 
00107 static Proto *getluaproto (CallInfo *ci) {
00108   return (isLua(ci) ? ci_func(ci)->l.p : NULL);
00109 }
00110 
00111 
00112 static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
00113   const char *name;
00114   Proto *fp = getluaproto(ci);
00115   if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
00116     return name;  /* is a local variable in a Lua function */
00117   else {
00118     StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
00119     if (limit - ci->base >= n && n > 0)  /* is 'n' inside 'ci' stack? */
00120       return "(*temporary)";
00121     else
00122       return NULL;
00123   }
00124 }
00125 
00126 
00127 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
00128   CallInfo *ci = L->base_ci + ar->i_ci;
00129   const char *name = findlocal(L, ci, n);
00130   lua_lock(L);
00131   if (name)
00132       luaA_pushobject(L, ci->base + (n - 1));
00133   lua_unlock(L);
00134   return name;
00135 }
00136 
00137 
00138 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
00139   CallInfo *ci = L->base_ci + ar->i_ci;
00140   const char *name = findlocal(L, ci, n);
00141   lua_lock(L);
00142   if (name)
00143       setobjs2s(L, ci->base + (n - 1), L->top - 1);
00144   L->top--;  /* pop value */
00145   lua_unlock(L);
00146   return name;
00147 }
00148 
00149 
00150 static void funcinfo (lua_Debug *ar, Closure *cl) {
00151   if (cl->c.isC) {
00152     ar->source = "=[C]";
00153     ar->linedefined = -1;
00154     ar->lastlinedefined = -1;
00155     ar->what = "C";
00156   }
00157   else {
00158     ar->source = getstr(cl->l.p->source);
00159     ar->linedefined = cl->l.p->linedefined;
00160     ar->lastlinedefined = cl->l.p->lastlinedefined;
00161     ar->what = (ar->linedefined == 0) ? "main" : "Lua";
00162   }
00163   luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
00164 }
00165 
00166 
00167 static void info_tailcall (lua_Debug *ar) {
00168   ar->name = ar->namewhat = "";
00169   ar->what = "tail";
00170   ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
00171   ar->source = "=(tail call)";
00172   luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
00173   ar->nups = 0;
00174 }
00175 
00176 
00177 static void collectvalidlines (lua_State *L, Closure *f) {
00178   if (f == NULL || f->c.isC) {
00179     setnilvalue(L->top);
00180   }
00181   else {
00182     Table *t = luaH_new(L, 0, 0);
00183     int *lineinfo = f->l.p->lineinfo;
00184     int i;
00185     for (i=0; i<f->l.p->sizelineinfo; i++)
00186       setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
00187     sethvalue(L, L->top, t); 
00188   }
00189   incr_top(L);
00190 }
00191 
00192 
00193 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
00194                     Closure *f, CallInfo *ci) {
00195   int status = 1;
00196   if (f == NULL) {
00197     info_tailcall(ar);
00198     return status;
00199   }
00200   for (; *what; what++) {
00201     switch (*what) {
00202       case 'S': {
00203         funcinfo(ar, f);
00204         break;
00205       }
00206       case 'l': {
00207         ar->currentline = (ci) ? currentline(L, ci) : -1;
00208         break;
00209       }
00210       case 'u': {
00211         ar->nups = f->c.nupvalues;
00212         break;
00213       }
00214       case 'n': {
00215         ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
00216         if (ar->namewhat == NULL) {
00217           ar->namewhat = "";  /* not found */
00218           ar->name = NULL;
00219         }
00220         break;
00221       }
00222       case 'L':
00223       case 'f':  /* handled by lua_getinfo */
00224         break;
00225       default: status = 0;  /* invalid option */
00226     }
00227   }
00228   return status;
00229 }
00230 
00231 
00232 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
00233   int status;
00234   Closure *f = NULL;
00235   CallInfo *ci = NULL;
00236   lua_lock(L);
00237   if (*what == '>') {
00238     StkId func = L->top - 1;
00239     luai_apicheck(L, ttisfunction(func));
00240     what++;  /* skip the '>' */
00241     f = clvalue(func);
00242     L->top--;  /* pop function */
00243   }
00244   else if (ar->i_ci != 0) {  /* no tail call? */
00245     ci = L->base_ci + ar->i_ci;
00246     lua_assert(ttisfunction(ci->func));
00247     f = clvalue(ci->func);
00248   }
00249   status = auxgetinfo(L, what, ar, f, ci);
00250   if (strchr(what, 'f')) {
00251     if (f == NULL) setnilvalue(L->top);
00252     else setclvalue(L, L->top, f);
00253     incr_top(L);
00254   }
00255   if (strchr(what, 'L'))
00256     collectvalidlines(L, f);
00257   lua_unlock(L);
00258   return status;
00259 }
00260 
00261 
00262 /*
00263 ** {======================================================
00264 ** Symbolic Execution and code checker
00265 ** =======================================================
00266 */
00267 
00268 #define check(x)                if (!(x)) return 0;
00269 
00270 #define checkjump(pt,pc)        check(0 <= pc && pc < pt->sizecode)
00271 
00272 #define checkreg(pt,reg)        check((reg) < (pt)->maxstacksize)
00273 
00274 
00275 
00276 static int precheck (const Proto *pt) {
00277   check(pt->maxstacksize <= MAXSTACK);
00278   lua_assert(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
00279   lua_assert(!(pt->is_vararg & VARARG_NEEDSARG) ||
00280               (pt->is_vararg & VARARG_HASARG));
00281   check(pt->sizeupvalues <= pt->nups);
00282   check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
00283   check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
00284   return 1;
00285 }
00286 
00287 
00288 #define checkopenop(pt,pc)      luaG_checkopenop((pt)->code[(pc)+1])
00289 
00290 int luaG_checkopenop (Instruction i) {
00291   switch (GET_OPCODE(i)) {
00292     case OP_CALL:
00293     case OP_TAILCALL:
00294     case OP_RETURN:
00295     case OP_SETLIST: {
00296       check(GETARG_B(i) == 0);
00297       return 1;
00298     }
00299     default: return 0;  /* invalid instruction after an open call */
00300   }
00301 }
00302 
00303 
00304 static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
00305   switch (mode) {
00306     case OpArgN: check(r == 0); break;
00307     case OpArgU: break;
00308     case OpArgR: checkreg(pt, r); break;
00309     case OpArgK:
00310       check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
00311       break;
00312   }
00313   return 1;
00314 }
00315 
00316 
00317 static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
00318   int pc;
00319   int last;  /* stores position of last instruction that changed `reg' */
00320   last = pt->sizecode-1;  /* points to final return (a `neutral' instruction) */
00321   check(precheck(pt));
00322   for (pc = 0; pc < lastpc; pc++) {
00323     Instruction i = pt->code[pc];
00324     OpCode op = GET_OPCODE(i);
00325     int a = GETARG_A(i);
00326     int b = 0;
00327     int c = 0;
00328     check(op < NUM_OPCODES);
00329     checkreg(pt, a);
00330     switch (getOpMode(op)) {
00331       case iABC: {
00332         b = GETARG_B(i);
00333         c = GETARG_C(i);
00334         check(checkArgMode(pt, b, getBMode(op)));
00335         check(checkArgMode(pt, c, getCMode(op)));
00336         break;
00337       }
00338       case iABx: {
00339         b = GETARG_Bx(i);
00340         if (getBMode(op) == OpArgK) check(b < pt->sizek);
00341         break;
00342       }
00343       case iAsBx: {
00344         b = GETARG_sBx(i);
00345         if (getBMode(op) == OpArgR) {
00346           int dest = pc+1+b;
00347           check(0 <= dest && dest < pt->sizecode);
00348           if (dest > 0) {
00349             /* cannot jump to a setlist count */
00350             Instruction d = pt->code[dest-1];
00351             check(!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0));
00352           }
00353         }
00354         break;
00355       }
00356     }
00357     if (testAMode(op)) {
00358       if (a == reg) last = pc;  /* change register `a' */
00359     }
00360     if (testTMode(op)) {
00361       check(pc+2 < pt->sizecode);  /* check skip */
00362       check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
00363     }
00364     switch (op) {
00365       case OP_LOADBOOL: {
00366         check(c == 0 || pc+2 < pt->sizecode);  /* check its jump */
00367         break;
00368       }
00369       case OP_LOADNIL: {
00370         if (a <= reg && reg <= b)
00371           last = pc;  /* set registers from `a' to `b' */
00372         break;
00373       }
00374       case OP_GETUPVAL:
00375       case OP_SETUPVAL: {
00376         check(b < pt->nups);
00377         break;
00378       }
00379       case OP_GETGLOBAL:
00380       case OP_SETGLOBAL: {
00381         check(ttisstring(&pt->k[b]));
00382         break;
00383       }
00384       case OP_SELF: {
00385         checkreg(pt, a+1);
00386         if (reg == a+1) last = pc;
00387         break;
00388       }
00389       case OP_CONCAT: {
00390         check(b < c);  /* at least two operands */
00391         break;
00392       }
00393       case OP_TFORLOOP: {
00394         check(c >= 1);  /* at least one result (control variable) */
00395         checkreg(pt, a+2+c);  /* space for results */
00396         if (reg >= a+2) last = pc;  /* affect all regs above its base */
00397         break;
00398       }
00399       case OP_FORLOOP:
00400       case OP_FORPREP:
00401         checkreg(pt, a+3);
00402         /* go through */
00403       case OP_JMP: {
00404         int dest = pc+1+b;
00405         /* not full check and jump is forward and do not skip `lastpc'? */
00406         if (reg != NO_REG && pc < dest && dest <= lastpc)
00407           pc += b;  /* do the jump */
00408         break;
00409       }
00410       case OP_CALL:
00411       case OP_TAILCALL: {
00412         if (b != 0) {
00413           checkreg(pt, a+b-1);
00414         }
00415         c--;  /* c = num. returns */
00416         if (c == LUA_MULTRET) {
00417           check(checkopenop(pt, pc));
00418         }
00419         else if (c != 0)
00420           checkreg(pt, a+c-1);
00421         if (reg >= a) last = pc;  /* affect all registers above base */
00422         break;
00423       }
00424       case OP_RETURN: {
00425         b--;  /* b = num. returns */
00426         if (b > 0) checkreg(pt, a+b-1);
00427         break;
00428       }
00429       case OP_SETLIST: {
00430         if (b > 0) checkreg(pt, a + b);
00431         if (c == 0) pc++;
00432         break;
00433       }
00434       case OP_CLOSURE: {
00435         int nup, j;
00436         check(b < pt->sizep);
00437         nup = pt->p[b]->nups;
00438         check(pc + nup < pt->sizecode);
00439         for (j = 1; j <= nup; j++) {
00440           OpCode op1 = GET_OPCODE(pt->code[pc + j]);
00441           check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
00442         }
00443         if (reg != NO_REG)  /* tracing? */
00444           pc += nup;  /* do not 'execute' these pseudo-instructions */
00445         break;
00446       }
00447       case OP_VARARG: {
00448         check((pt->is_vararg & VARARG_ISVARARG) &&
00449              !(pt->is_vararg & VARARG_NEEDSARG));
00450         b--;
00451         if (b == LUA_MULTRET) check(checkopenop(pt, pc));
00452         checkreg(pt, a+b-1);
00453         break;
00454       }
00455       default: break;
00456     }
00457   }
00458   return pt->code[last];
00459 }
00460 
00461 #undef check
00462 #undef checkjump
00463 #undef checkreg
00464 
00465 /* }====================================================== */
00466 
00467 
00468 int luaG_checkcode (const Proto *pt) {
00469   return (symbexec(pt, pt->sizecode, NO_REG) != 0);
00470 }
00471 
00472 
00473 static const char *kname (Proto *p, int c) {
00474   if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
00475     return svalue(&p->k[INDEXK(c)]);
00476   else
00477     return "?";
00478 }
00479 
00480 
00481 static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
00482                                const char **name) {
00483   if (isLua(ci)) {  /* a Lua function? */
00484     Proto *p = ci_func(ci)->l.p;
00485     int pc = currentpc(L, ci);
00486     Instruction i;
00487     *name = luaF_getlocalname(p, stackpos+1, pc);
00488     if (*name)  /* is a local? */
00489       return "local";
00490     i = symbexec(p, pc, stackpos);  /* try symbolic execution */
00491     lua_assert(pc != -1);
00492     switch (GET_OPCODE(i)) {
00493       case OP_GETGLOBAL: {
00494         int g = GETARG_Bx(i);  /* global index */
00495         lua_assert(ttisstring(&p->k[g]));
00496         *name = svalue(&p->k[g]);
00497         return "global";
00498       }
00499       case OP_MOVE: {
00500         int a = GETARG_A(i);
00501         int b = GETARG_B(i);  /* move from `b' to `a' */
00502         if (b < a)
00503           return getobjname(L, ci, b, name);  /* get name for `b' */
00504         break;
00505       }
00506       case OP_GETTABLE: {
00507         int k = GETARG_C(i);  /* key index */
00508         *name = kname(p, k);
00509         return "field";
00510       }
00511       case OP_GETUPVAL: {
00512         int u = GETARG_B(i);  /* upvalue index */
00513         *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
00514         return "upvalue";
00515       }
00516       case OP_SELF: {
00517         int k = GETARG_C(i);  /* key index */
00518         *name = kname(p, k);
00519         return "method";
00520       }
00521       default: break;
00522     }
00523   }
00524   return NULL;  /* no useful name found */
00525 }
00526 
00527 
00528 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
00529   Instruction i;
00530   if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
00531     return NULL;  /* calling function is not Lua (or is unknown) */
00532   ci--;  /* calling function */
00533   i = ci_func(ci)->l.p->code[currentpc(L, ci)];
00534   if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
00535       GET_OPCODE(i) == OP_TFORLOOP)
00536     return getobjname(L, ci, GETARG_A(i), name);
00537   else
00538     return NULL;  /* no useful name can be found */
00539 }
00540 
00541 
00542 /* only ANSI way to check whether a pointer points to an array */
00543 static int isinstack (CallInfo *ci, const TValue *o) {
00544   StkId p;
00545   for (p = ci->base; p < ci->top; p++)
00546     if (o == p) return 1;
00547   return 0;
00548 }
00549 
00550 
00551 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
00552   const char *name = NULL;
00553   const char *t = luaT_typenames[ttype(o)];
00554   const char *kind = (isinstack(L->ci, o)) ?
00555                          getobjname(L, L->ci, cast_int(o - L->base), &name) :
00556                          NULL;
00557   if (kind)
00558     luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
00559                 op, kind, name, t);
00560   else
00561     luaG_runerror(L, "attempt to %s a %s value", op, t);
00562 }
00563 
00564 
00565 void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
00566   if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
00567   lua_assert(!ttisstring(p1) && !ttisnumber(p1));
00568   luaG_typeerror(L, p1, "concatenate");
00569 }
00570 
00571 
00572 void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
00573   TValue temp;
00574   if (luaV_tonumber(p1, &temp) == NULL)
00575     p2 = p1;  /* first operand is wrong */
00576   luaG_typeerror(L, p2, "perform arithmetic on");
00577 }
00578 
00579 
00580 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
00581   const char *t1 = luaT_typenames[ttype(p1)];
00582   const char *t2 = luaT_typenames[ttype(p2)];
00583   if (t1[2] == t2[2])
00584     luaG_runerror(L, "attempt to compare two %s values", t1);
00585   else
00586     luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
00587   return 0;
00588 }
00589 
00590 
00591 static void addinfo (lua_State *L, const char *msg) {
00592   CallInfo *ci = L->ci;
00593   if (isLua(ci)) {  /* is Lua code? */
00594     char buff[LUA_IDSIZE];  /* add file:line information */
00595     int line = currentline(L, ci);
00596     luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
00597     luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
00598   }
00599 }
00600 
00601 
00602 void luaG_errormsg (lua_State *L) {
00603   if (L->errfunc != 0) {  /* is there an error handling function? */
00604     StkId errfunc = restorestack(L, L->errfunc);
00605     if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
00606     setobjs2s(L, L->top, L->top - 1);  /* move argument */
00607     setobjs2s(L, L->top - 1, errfunc);  /* push function */
00608     incr_top(L);
00609     luaD_call(L, L->top - 2, 1);  /* call it */
00610   }
00611   luaD_throw(L, LUA_ERRRUN);
00612 }
00613 
00614 
00615 void luaG_runerror (lua_State *L, const char *fmt, ...) {
00616   va_list argp;
00617   va_start(argp, fmt);
00618   addinfo(L, luaO_pushvfstring(L, fmt, argp));
00619   va_end(argp);
00620   luaG_errormsg(L);
00621 }
00622 

Generated on Mon Nov 29 2010 05:18:45 for rpm by  doxygen 1.7.2