2 ** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp $
4 ** See Copyright Notice in lua.h
34 #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
37 /* Active Lua function (given call info) */
38 #define ci_func(ci) (clLvalue((ci)->func))
41 static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
45 static int currentpc (CallInfo *ci) {
46 lua_assert(isLua(ci));
47 return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
51 static int currentline (CallInfo *ci) {
52 return getfuncline(ci_func(ci)->p, currentpc(ci));
57 ** If function yielded, its 'func' can be in the 'extra' field. The
58 ** next function restores 'func' to its correct value for debugging
59 ** purposes. (It exchanges 'func' and 'extra'; so, when called again,
60 ** after debugging, it also "re-restores" ** 'func' to its altered value.
62 static void swapextra (lua_State *L) {
63 if (L->status == LUA_YIELD) {
64 CallInfo *ci = L->ci; /* get function that yielded */
65 StkId temp = ci->func; /* exchange its 'func' and 'extra' values */
66 ci->func = restorestack(L, ci->extra);
67 ci->extra = savestack(L, temp);
73 ** This function can be called asynchronously (e.g. during a signal).
74 ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
75 ** 'resethookcount') are for debug only, and it is no problem if they
76 ** get arbitrary values (causes at most one wrong hook call). 'hookmask'
77 ** is an atomic value. We assume that pointers are atomic too (e.g., gcc
78 ** ensures that for all platforms where it runs). Moreover, 'hook' is
79 ** always checked before being called (see 'luaD_hook').
81 LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
82 if (func == NULL || mask == 0) { /* turn off hooks? */
87 L->oldpc = L->ci->u.l.savedpc;
89 L->basehookcount = count;
91 L->hookmask = cast_byte(mask);
95 LUA_API lua_Hook lua_gethook (lua_State *L) {
100 LUA_API int lua_gethookmask (lua_State *L) {
105 LUA_API int lua_gethookcount (lua_State *L) {
106 return L->basehookcount;
110 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
113 if (level < 0) return 0; /* invalid (negative) level */
115 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
117 if (level == 0 && ci != &L->base_ci) { /* level found? */
121 else status = 0; /* no such level */
127 static const char *upvalname (Proto *p, int uv) {
128 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
129 if (s == NULL) return "?";
130 else return getstr(s);
134 static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
135 int nparams = clLvalue(ci->func)->p->numparams;
136 if (n >= cast_int(ci->u.l.base - ci->func) - nparams)
137 return NULL; /* no such vararg */
139 *pos = ci->func + nparams + n;
140 return "(*vararg)"; /* generic name for any vararg */
145 static const char *findlocal (lua_State *L, CallInfo *ci, int n,
147 const char *name = NULL;
150 if (n < 0) /* access to vararg values? */
151 return findvararg(ci, -n, pos);
154 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
159 if (name == NULL) { /* no 'standard' name? */
160 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
161 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
162 name = "(*temporary)"; /* generic name for any valid slot */
164 return NULL; /* no name */
166 *pos = base + (n - 1);
171 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
175 if (ar == NULL) { /* information about non-active function? */
176 if (!isLfunction(L->top - 1)) /* not a Lua function? */
178 else /* consider live variables at function start (parameters) */
179 name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
181 else { /* active function; get information through 'ar' */
182 StkId pos = NULL; /* to avoid warnings */
183 name = findlocal(L, ar->i_ci, n, &pos);
185 setobj2s(L, L->top, pos);
195 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
196 StkId pos = NULL; /* to avoid warnings */
200 name = findlocal(L, ar->i_ci, n, &pos);
202 setobjs2s(L, pos, L->top - 1);
203 L->top--; /* pop value */
211 static void funcinfo (lua_Debug *ar, Closure *cl) {
212 if (noLuaClosure(cl)) {
214 ar->linedefined = -1;
215 ar->lastlinedefined = -1;
220 ar->source = p->source ? getstr(p->source) : "=?";
221 ar->linedefined = p->linedefined;
222 ar->lastlinedefined = p->lastlinedefined;
223 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
225 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
229 static void collectvalidlines (lua_State *L, Closure *f) {
230 if (noLuaClosure(f)) {
237 int *lineinfo = f->l.p->lineinfo;
238 Table *t = luaH_new(L); /* new table to store active lines */
239 sethvalue(L, L->top, t); /* push it on stack */
241 setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
242 for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
243 luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
248 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
249 if (ci == NULL) /* no 'ci'? */
250 return NULL; /* no info */
251 else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */
253 return "metamethod"; /* report it as such */
255 /* calling function is a known Lua function? */
256 else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
257 return funcnamefromcode(L, ci->previous, name);
258 else return NULL; /* no way to find a name */
262 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
263 Closure *f, CallInfo *ci) {
265 for (; *what; what++) {
272 ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
276 ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
277 if (noLuaClosure(f)) {
282 ar->isvararg = f->l.p->is_vararg;
283 ar->nparams = f->l.p->numparams;
288 ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
292 ar->namewhat = getfuncname(L, ci, &ar->name);
293 if (ar->namewhat == NULL) {
294 ar->namewhat = ""; /* not found */
300 case 'f': /* handled by lua_getinfo */
302 default: status = 0; /* invalid option */
309 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
319 api_check(L, ttisfunction(func), "function expected");
320 what++; /* skip the '>' */
321 L->top--; /* pop function */
326 lua_assert(ttisfunction(ci->func));
328 cl = ttisclosure(func) ? clvalue(func) : NULL;
329 status = auxgetinfo(L, what, ar, cl, ci);
330 if (strchr(what, 'f')) {
331 setobjs2s(L, L->top, func);
334 swapextra(L); /* correct before option 'L', which can raise a mem. error */
335 if (strchr(what, 'L'))
336 collectvalidlines(L, cl);
343 ** {======================================================
344 ** Symbolic Execution
345 ** =======================================================
348 static const char *getobjname (Proto *p, int lastpc, int reg,
353 ** find a "name" for the RK value 'c'
355 static void kname (Proto *p, int pc, int c, const char **name) {
356 if (ISK(c)) { /* is 'c' a constant? */
357 TValue *kvalue = &p->k[INDEXK(c)];
358 if (ttisstring(kvalue)) { /* literal constant? */
359 *name = svalue(kvalue); /* it is its own name */
362 /* else no reasonable name found */
364 else { /* 'c' is a register */
365 const char *what = getobjname(p, pc, c, name); /* search for 'c' */
366 if (what && *what == 'c') { /* found a constant name? */
367 return; /* 'name' already filled */
369 /* else no reasonable name found */
371 *name = "?"; /* no reasonable name found */
375 static int filterpc (int pc, int jmptarget) {
376 if (pc < jmptarget) /* is code conditional (inside a jump)? */
377 return -1; /* cannot know who sets that register */
378 else return pc; /* current position sets that register */
383 ** try to find last instruction before 'lastpc' that modified register 'reg'
385 static int findsetreg (Proto *p, int lastpc, int reg) {
387 int setreg = -1; /* keep last instruction that changed 'reg' */
388 int jmptarget = 0; /* any code before this address is conditional */
389 for (pc = 0; pc < lastpc; pc++) {
390 Instruction i = p->code[pc];
391 OpCode op = GET_OPCODE(i);
396 if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
397 setreg = filterpc(pc, jmptarget);
401 if (reg >= a + 2) /* affect all regs above its base */
402 setreg = filterpc(pc, jmptarget);
407 if (reg >= a) /* affect all registers above base */
408 setreg = filterpc(pc, jmptarget);
412 int b = GETARG_sBx(i);
413 int dest = pc + 1 + b;
414 /* jump is forward and do not skip 'lastpc'? */
415 if (pc < dest && dest <= lastpc) {
416 if (dest > jmptarget)
417 jmptarget = dest; /* update 'jmptarget' */
422 if (testAMode(op) && reg == a) /* any instruction that set A */
423 setreg = filterpc(pc, jmptarget);
431 static const char *getobjname (Proto *p, int lastpc, int reg,
434 *name = luaF_getlocalname(p, reg + 1, lastpc);
435 if (*name) /* is a local? */
437 /* else try symbolic execution */
438 pc = findsetreg(p, lastpc, reg);
439 if (pc != -1) { /* could find instruction? */
440 Instruction i = p->code[pc];
441 OpCode op = GET_OPCODE(i);
444 int b = GETARG_B(i); /* move from 'b' to 'a' */
446 return getobjname(p, pc, b, name); /* get name for 'b' */
451 int k = GETARG_C(i); /* key index */
452 int t = GETARG_B(i); /* table index */
453 const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
454 ? luaF_getlocalname(p, t + 1, pc)
456 kname(p, pc, k, name);
457 return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
460 *name = upvalname(p, GETARG_B(i));
465 int b = (op == OP_LOADK) ? GETARG_Bx(i)
466 : GETARG_Ax(p->code[pc + 1]);
467 if (ttisstring(&p->k[b])) {
468 *name = svalue(&p->k[b]);
474 int k = GETARG_C(i); /* key index */
475 kname(p, pc, k, name);
478 default: break; /* go through to return NULL */
481 return NULL; /* could not find reasonable name */
486 ** Try to find a name for a function based on the code that called it.
487 ** (Only works when function was called by a Lua function.)
488 ** Returns what the name is (e.g., "for iterator", "method",
489 ** "metamethod") and sets '*name' to point to the name.
491 static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
493 TMS tm = (TMS)0; /* (initial value avoids warnings) */
494 Proto *p = ci_func(ci)->p; /* calling function */
495 int pc = currentpc(ci); /* calling instruction index */
496 Instruction i = p->code[pc]; /* calling instruction */
497 if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
501 switch (GET_OPCODE(i)) {
504 return getobjname(p, pc, GETARG_A(i), name); /* get function name */
505 case OP_TFORCALL: { /* for iterator */
506 *name = "for iterator";
507 return "for iterator";
509 /* other instructions can do calls through metamethods */
510 case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
513 case OP_SETTABUP: case OP_SETTABLE:
516 case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
517 case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
518 case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
519 int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */
520 tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */
523 case OP_UNM: tm = TM_UNM; break;
524 case OP_BNOT: tm = TM_BNOT; break;
525 case OP_LEN: tm = TM_LEN; break;
526 case OP_CONCAT: tm = TM_CONCAT; break;
527 case OP_EQ: tm = TM_EQ; break;
528 case OP_LT: tm = TM_LT; break;
529 case OP_LE: tm = TM_LE; break;
531 return NULL; /* cannot find a reasonable name */
533 *name = getstr(G(L)->tmname[tm]);
537 /* }====================================================== */
542 ** The subtraction of two potentially unrelated pointers is
543 ** not ISO C, but it should not crash a program; the subsequent
544 ** checks are ISO C and ensure a correct result.
546 static int isinstack (CallInfo *ci, const TValue *o) {
547 ptrdiff_t i = o - ci->u.l.base;
548 return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o);
553 ** Checks whether value 'o' came from an upvalue. (That can only happen
554 ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
557 static const char *getupvalname (CallInfo *ci, const TValue *o,
559 LClosure *c = ci_func(ci);
561 for (i = 0; i < c->nupvalues; i++) {
562 if (c->upvals[i]->v == o) {
563 *name = upvalname(c->p, i);
571 static const char *varinfo (lua_State *L, const TValue *o) {
572 const char *name = NULL; /* to avoid warnings */
573 CallInfo *ci = L->ci;
574 const char *kind = NULL;
576 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
577 if (!kind && isinstack(ci, o)) /* no? try a register */
578 kind = getobjname(ci_func(ci)->p, currentpc(ci),
579 cast_int(o - ci->u.l.base), &name);
581 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
585 l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
586 const char *t = luaT_objtypename(L, o);
587 luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
591 l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
592 if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
593 luaG_typeerror(L, p1, "concatenate");
597 l_noret luaG_opinterror (lua_State *L, const TValue *p1,
598 const TValue *p2, const char *msg) {
600 if (!tonumber(p1, &temp)) /* first operand is wrong? */
601 p2 = p1; /* now second is wrong */
602 luaG_typeerror(L, p2, msg);
607 ** Error when both values are convertible to numbers, but not to integers
609 l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
611 if (!tointeger(p1, &temp))
613 luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
617 l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
618 const char *t1 = luaT_objtypename(L, p1);
619 const char *t2 = luaT_objtypename(L, p2);
620 if (strcmp(t1, t2) == 0)
621 luaG_runerror(L, "attempt to compare two %s values", t1);
623 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
627 /* add src:line information to 'msg' */
628 const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
630 char buff[LUA_IDSIZE];
632 luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
633 else { /* no source available; use "?" instead */
634 buff[0] = '?'; buff[1] = '\0';
636 return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
640 l_noret luaG_errormsg (lua_State *L) {
641 if (L->errfunc != 0) { /* is there an error handling function? */
642 StkId errfunc = restorestack(L, L->errfunc);
643 setobjs2s(L, L->top, L->top - 1); /* move argument */
644 setobjs2s(L, L->top - 1, errfunc); /* push function */
645 L->top++; /* assume EXTRA_STACK */
646 luaD_callnoyield(L, L->top - 2, 1); /* call it */
648 luaD_throw(L, LUA_ERRRUN);
652 l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
653 CallInfo *ci = L->ci;
657 msg = luaO_pushvfstring(L, fmt, argp); /* format message */
659 if (isLua(ci)) /* if Lua function, add source:line information */
660 luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
665 void luaG_traceexec (lua_State *L) {
666 CallInfo *ci = L->ci;
667 lu_byte mask = L->hookmask;
668 int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
670 resethookcount(L); /* reset count */
671 else if (!(mask & LUA_MASKLINE))
672 return; /* no line hook and count != 0; nothing to be done */
673 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
674 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
675 return; /* do not call hook again (VM yielded, so it did not move) */
678 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
679 if (mask & LUA_MASKLINE) {
680 Proto *p = ci_func(ci)->p;
681 int npc = pcRel(ci->u.l.savedpc, p);
682 int newline = getfuncline(p, npc);
683 if (npc == 0 || /* call linehook when enter a new function, */
684 ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
685 newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
686 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
688 L->oldpc = ci->u.l.savedpc;
689 if (L->status == LUA_YIELD) { /* did hook yield? */
691 L->hookcount = 1; /* undo decrement to zero */
692 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
693 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
694 ci->func = L->top - 1; /* protect stack below results */
695 luaD_throw(L, LUA_YIELD);