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