xref: /freebsd/sys/contrib/openzfs/module/lua/ldebug.c (revision 6132212808e8dccedc9e5d85fea4390c2f38059a)
1 /* BEGIN CSTYLED */
2 /*
3 ** $Id: ldebug.c,v 2.90.1.4 2015/02/19 17:05:13 roberto Exp $
4 ** Debug Interface
5 ** See Copyright Notice in lua.h
6 */
7 
8 
9 #define ldebug_c
10 #define LUA_CORE
11 
12 #include <sys/lua/lua.h>
13 
14 #include "lapi.h"
15 #include "lcode.h"
16 #include "ldebug.h"
17 #include "ldo.h"
18 #include "lfunc.h"
19 #include "lobject.h"
20 #include "lopcodes.h"
21 #include "lstate.h"
22 #include "lstring.h"
23 #include "ltable.h"
24 #include "ltm.h"
25 #include "lvm.h"
26 
27 
28 
29 #define noLuaClosure(f)		((f) == NULL || (f)->c.tt == LUA_TCCL)
30 
31 
32 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
33 
34 
35 static int currentpc (CallInfo *ci) {
36   lua_assert(isLua(ci));
37   return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
38 }
39 
40 
41 static int currentline (CallInfo *ci) {
42   return getfuncline(ci_func(ci)->p, currentpc(ci));
43 }
44 
45 
46 static void swapextra (lua_State *L) {
47   if (L->status == LUA_YIELD) {
48     CallInfo *ci = L->ci;  /* get function that yielded */
49     StkId temp = ci->func;  /* exchange its 'func' and 'extra' values */
50     ci->func = restorestack(L, ci->extra);
51     ci->extra = savestack(L, temp);
52   }
53 }
54 
55 
56 /*
57 ** this function can be called asynchronous (e.g. during a signal)
58 */
59 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
60   if (func == NULL || mask == 0) {  /* turn off hooks? */
61     mask = 0;
62     func = NULL;
63   }
64   if (isLua(L->ci))
65     L->oldpc = L->ci->u.l.savedpc;
66   L->hook = func;
67   L->basehookcount = count;
68   resethookcount(L);
69   L->hookmask = cast_byte(mask);
70   return 1;
71 }
72 
73 
74 LUA_API lua_Hook lua_gethook (lua_State *L) {
75   return L->hook;
76 }
77 
78 
79 LUA_API int lua_gethookmask (lua_State *L) {
80   return L->hookmask;
81 }
82 
83 
84 LUA_API int lua_gethookcount (lua_State *L) {
85   return L->basehookcount;
86 }
87 
88 
89 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
90   int status;
91   CallInfo *ci;
92   if (level < 0) return 0;  /* invalid (negative) level */
93   lua_lock(L);
94   for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
95     level--;
96   if (level == 0 && ci != &L->base_ci) {  /* level found? */
97     status = 1;
98     ar->i_ci = ci;
99   }
100   else status = 0;  /* no such level */
101   lua_unlock(L);
102   return status;
103 }
104 
105 
106 static const char *upvalname (Proto *p, int uv) {
107   TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
108   if (s == NULL) return "?";
109   else return getstr(s);
110 }
111 
112 
113 static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
114   int nparams = clLvalue(ci->func)->p->numparams;
115   if (n >= ci->u.l.base - ci->func - nparams)
116     return NULL;  /* no such vararg */
117   else {
118     *pos = ci->func + nparams + n;
119     return "(*vararg)";  /* generic name for any vararg */
120   }
121 }
122 
123 
124 static const char *findlocal (lua_State *L, CallInfo *ci, int n,
125                               StkId *pos) {
126   const char *name = NULL;
127   StkId base;
128   if (isLua(ci)) {
129     if (n < 0)  /* access to vararg values? */
130       return findvararg(ci, -n, pos);
131     else {
132       base = ci->u.l.base;
133       name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
134     }
135   }
136   else
137     base = ci->func + 1;
138   if (name == NULL) {  /* no 'standard' name? */
139     StkId limit = (ci == L->ci) ? L->top : ci->next->func;
140     if (limit - base >= n && n > 0)  /* is 'n' inside 'ci' stack? */
141       name = "(*temporary)";  /* generic name for any valid slot */
142     else
143       return NULL;  /* no name */
144   }
145   *pos = base + (n - 1);
146   return name;
147 }
148 
149 
150 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
151   const char *name;
152   lua_lock(L);
153   swapextra(L);
154   if (ar == NULL) {  /* information about non-active function? */
155     if (!isLfunction(L->top - 1))  /* not a Lua function? */
156       name = NULL;
157     else  /* consider live variables at function start (parameters) */
158       name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
159   }
160   else {  /* active function; get information through 'ar' */
161     StkId pos = 0;  /* to avoid warnings */
162     name = findlocal(L, ar->i_ci, n, &pos);
163     if (name) {
164       setobj2s(L, L->top, pos);
165       api_incr_top(L);
166     }
167   }
168   swapextra(L);
169   lua_unlock(L);
170   return name;
171 }
172 
173 
174 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
175   StkId pos = 0;  /* to avoid warnings */
176   const char *name;
177   lua_lock(L);
178   swapextra(L);
179   name = findlocal(L, ar->i_ci, n, &pos);
180   if (name)
181     setobjs2s(L, pos, L->top - 1);
182   L->top--;  /* pop value */
183   swapextra(L);
184   lua_unlock(L);
185   return name;
186 }
187 
188 
189 static void funcinfo (lua_Debug *ar, Closure *cl) {
190   if (noLuaClosure(cl)) {
191     ar->source = "=[C]";
192     ar->linedefined = -1;
193     ar->lastlinedefined = -1;
194     ar->what = "C";
195   }
196   else {
197     Proto *p = cl->l.p;
198     ar->source = p->source ? getstr(p->source) : "=?";
199     ar->linedefined = p->linedefined;
200     ar->lastlinedefined = p->lastlinedefined;
201     ar->what = (ar->linedefined == 0) ? "main" : "Lua";
202   }
203   luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
204 }
205 
206 
207 static void collectvalidlines (lua_State *L, Closure *f) {
208   if (noLuaClosure(f)) {
209     setnilvalue(L->top);
210     api_incr_top(L);
211   }
212   else {
213     int i;
214     TValue v;
215     int *lineinfo = f->l.p->lineinfo;
216     Table *t = luaH_new(L);  /* new table to store active lines */
217     sethvalue(L, L->top, t);  /* push it on stack */
218     api_incr_top(L);
219     setbvalue(&v, 1);  /* boolean 'true' to be the value of all indices */
220     for (i = 0; i < f->l.p->sizelineinfo; i++)  /* for all lines with code */
221       luaH_setint(L, t, lineinfo[i], &v);  /* table[line] = true */
222   }
223 }
224 
225 
226 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
227                        Closure *f, CallInfo *ci) {
228   int status = 1;
229   for (; *what; what++) {
230     switch (*what) {
231       case 'S': {
232         funcinfo(ar, f);
233         break;
234       }
235       case 'l': {
236         ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
237         break;
238       }
239       case 'u': {
240         ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
241         if (noLuaClosure(f)) {
242           ar->isvararg = 1;
243           ar->nparams = 0;
244         }
245         else {
246           ar->isvararg = f->l.p->is_vararg;
247           ar->nparams = f->l.p->numparams;
248         }
249         break;
250       }
251       case 't': {
252         ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
253         break;
254       }
255       case 'n': {
256         /* calling function is a known Lua function? */
257         if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
258           ar->namewhat = getfuncname(L, ci->previous, &ar->name);
259         else
260           ar->namewhat = NULL;
261         if (ar->namewhat == NULL) {
262           ar->namewhat = "";  /* not found */
263           ar->name = NULL;
264         }
265         break;
266       }
267       case 'L':
268       case 'f':  /* handled by lua_getinfo */
269         break;
270       default: status = 0;  /* invalid option */
271     }
272   }
273   return status;
274 }
275 
276 
277 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
278   int status;
279   Closure *cl;
280   CallInfo *ci;
281   StkId func;
282   lua_lock(L);
283   swapextra(L);
284   if (*what == '>') {
285     ci = NULL;
286     func = L->top - 1;
287     api_check(L, ttisfunction(func), "function expected");
288     what++;  /* skip the '>' */
289     L->top--;  /* pop function */
290   }
291   else {
292     ci = ar->i_ci;
293     func = ci->func;
294     lua_assert(ttisfunction(ci->func));
295   }
296   cl = ttisclosure(func) ? clvalue(func) : NULL;
297   status = auxgetinfo(L, what, ar, cl, ci);
298   if (strchr(what, 'f')) {
299     setobjs2s(L, L->top, func);
300     api_incr_top(L);
301   }
302   swapextra(L);
303   if (strchr(what, 'L'))
304     collectvalidlines(L, cl);
305   lua_unlock(L);
306   return status;
307 }
308 
309 
310 /*
311 ** {======================================================
312 ** Symbolic Execution
313 ** =======================================================
314 */
315 
316 static const char *getobjname (Proto *p, int lastpc, int reg,
317                                const char **name);
318 
319 
320 /*
321 ** find a "name" for the RK value 'c'
322 */
323 static void kname (Proto *p, int pc, int c, const char **name) {
324   if (ISK(c)) {  /* is 'c' a constant? */
325     TValue *kvalue = &p->k[INDEXK(c)];
326     if (ttisstring(kvalue)) {  /* literal constant? */
327       // cppcheck-suppress autoVariables
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   L->runerror++;
602   va_list argp;
603   va_start(argp, fmt);
604   addinfo(L, luaO_pushvfstring(L, fmt, argp));
605   va_end(argp);
606   luaG_errormsg(L);
607   L->runerror--;
608 }
609 /* END CSTYLED */
610