The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/contrib/openzfs/module/lua/ldebug.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: a69ab64b1b29e908aa7b535e4cb46eb7


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.