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
currentpc(CallInfo * ci)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
currentline(CallInfo * ci)40 static int currentline (CallInfo *ci) {
41 return getfuncline(ci_func(ci)->p, currentpc(ci));
42 }
43
44
swapextra(lua_State * L)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 */
lua_sethook(lua_State * L,lua_Hook func,int mask,int count)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
lua_gethook(lua_State * L)73 LUA_API lua_Hook lua_gethook (lua_State *L) {
74 return L->hook;
75 }
76
77
lua_gethookmask(lua_State * L)78 LUA_API int lua_gethookmask (lua_State *L) {
79 return L->hookmask;
80 }
81
82
lua_gethookcount(lua_State * L)83 LUA_API int lua_gethookcount (lua_State *L) {
84 return L->basehookcount;
85 }
86
87
lua_getstack(lua_State * L,int level,lua_Debug * ar)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
upvalname(Proto * p,int uv)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
findvararg(CallInfo * ci,int n,StkId * pos)112 static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
113 int nparams = clLvalue(ci->func)->p->numparams;
114 int nvararg = cast_int(ci->u.l.base - ci->func) - nparams;
115 if (n <= -nvararg)
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
findlocal(lua_State * L,CallInfo * ci,int n,StkId * pos)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
lua_getlocal(lua_State * L,const lua_Debug * ar,int n)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
lua_setlocal(lua_State * L,const lua_Debug * ar,int n)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
funcinfo(lua_Debug * ar,Closure * cl)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
collectvalidlines(lua_State * L,Closure * f)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
auxgetinfo(lua_State * L,const char * what,lua_Debug * ar,Closure * f,CallInfo * ci)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
lua_getinfo(lua_State * L,const char * what,lua_Debug * ar)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 */
kname(Proto * p,int pc,int c,const char ** name)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 *name = svalue(kvalue); /* it is its own name */
328 return;
329 }
330 /* else no reasonable name found */
331 }
332 else { /* 'c' is a register */
333 const char *what = getobjname(p, pc, c, name); /* search for 'c' */
334 if (what && *what == 'c') { /* found a constant name? */
335 return; /* 'name' already filled */
336 }
337 /* else no reasonable name found */
338 }
339 *name = "?"; /* no reasonable name found */
340 }
341
342
filterpc(int pc,int jmptarget)343 static int filterpc (int pc, int jmptarget) {
344 if (pc < jmptarget) /* is code conditional (inside a jump)? */
345 return -1; /* cannot know who sets that register */
346 else return pc; /* current position sets that register */
347 }
348
349
350 /*
351 ** try to find last instruction before 'lastpc' that modified register 'reg'
352 */
findsetreg(Proto * p,int lastpc,int reg)353 static int findsetreg (Proto *p, int lastpc, int reg) {
354 int pc;
355 int setreg = -1; /* keep last instruction that changed 'reg' */
356 int jmptarget = 0; /* any code before this address is conditional */
357 for (pc = 0; pc < lastpc; pc++) {
358 Instruction i = p->code[pc];
359 OpCode op = GET_OPCODE(i);
360 int a = GETARG_A(i);
361 switch (op) {
362 case OP_LOADNIL: {
363 int b = GETARG_B(i);
364 if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
365 setreg = filterpc(pc, jmptarget);
366 break;
367 }
368 case OP_TFORCALL: {
369 if (reg >= a + 2) /* affect all regs above its base */
370 setreg = filterpc(pc, jmptarget);
371 break;
372 }
373 case OP_CALL:
374 case OP_TAILCALL: {
375 if (reg >= a) /* affect all registers above base */
376 setreg = filterpc(pc, jmptarget);
377 break;
378 }
379 case OP_JMP: {
380 int b = GETARG_sBx(i);
381 int dest = pc + 1 + b;
382 /* jump is forward and do not skip `lastpc'? */
383 if (pc < dest && dest <= lastpc) {
384 if (dest > jmptarget)
385 jmptarget = dest; /* update 'jmptarget' */
386 }
387 break;
388 }
389 case OP_TEST: {
390 if (reg == a) /* jumped code can change 'a' */
391 setreg = filterpc(pc, jmptarget);
392 break;
393 }
394 default:
395 if (testAMode(op) && reg == a) /* any instruction that set A */
396 setreg = filterpc(pc, jmptarget);
397 break;
398 }
399 }
400 return setreg;
401 }
402
403
getobjname(Proto * p,int lastpc,int reg,const char ** name)404 static const char *getobjname (Proto *p, int lastpc, int reg,
405 const char **name) {
406 int pc;
407 *name = luaF_getlocalname(p, reg + 1, lastpc);
408 if (*name) /* is a local? */
409 return "local";
410 /* else try symbolic execution */
411 pc = findsetreg(p, lastpc, reg);
412 if (pc != -1) { /* could find instruction? */
413 Instruction i = p->code[pc];
414 OpCode op = GET_OPCODE(i);
415 switch (op) {
416 case OP_MOVE: {
417 int b = GETARG_B(i); /* move from 'b' to 'a' */
418 if (b < GETARG_A(i))
419 return getobjname(p, pc, b, name); /* get name for 'b' */
420 break;
421 }
422 case OP_GETTABUP:
423 case OP_GETTABLE: {
424 int k = GETARG_C(i); /* key index */
425 int t = GETARG_B(i); /* table index */
426 const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
427 ? luaF_getlocalname(p, t + 1, pc)
428 : upvalname(p, t);
429 kname(p, pc, k, name);
430 return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
431 }
432 case OP_GETUPVAL: {
433 *name = upvalname(p, GETARG_B(i));
434 return "upvalue";
435 }
436 case OP_LOADK:
437 case OP_LOADKX: {
438 int b = (op == OP_LOADK) ? GETARG_Bx(i)
439 : GETARG_Ax(p->code[pc + 1]);
440 if (ttisstring(&p->k[b])) {
441 *name = svalue(&p->k[b]);
442 return "constant";
443 }
444 break;
445 }
446 case OP_SELF: {
447 int k = GETARG_C(i); /* key index */
448 kname(p, pc, k, name);
449 return "method";
450 }
451 default: break; /* go through to return NULL */
452 }
453 }
454 return NULL; /* could not find reasonable name */
455 }
456
457
getfuncname(lua_State * L,CallInfo * ci,const char ** name)458 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
459 TMS tm;
460 Proto *p = ci_func(ci)->p; /* calling function */
461 int pc = currentpc(ci); /* calling instruction index */
462 Instruction i = p->code[pc]; /* calling instruction */
463 switch (GET_OPCODE(i)) {
464 case OP_CALL:
465 case OP_TAILCALL: /* get function name */
466 return getobjname(p, pc, GETARG_A(i), name);
467 case OP_TFORCALL: { /* for iterator */
468 *name = "for iterator";
469 return "for iterator";
470 }
471 /* all other instructions can call only through metamethods */
472 case OP_SELF:
473 case OP_GETTABUP:
474 case OP_GETTABLE: tm = TM_INDEX; break;
475 case OP_SETTABUP:
476 case OP_SETTABLE: tm = TM_NEWINDEX; break;
477 case OP_EQ: tm = TM_EQ; break;
478 case OP_ADD: tm = TM_ADD; break;
479 case OP_SUB: tm = TM_SUB; break;
480 case OP_MUL: tm = TM_MUL; break;
481 case OP_DIV: tm = TM_DIV; break;
482 case OP_MOD: tm = TM_MOD; break;
483 case OP_POW: tm = TM_POW; break;
484 case OP_UNM: tm = TM_UNM; break;
485 case OP_LEN: tm = TM_LEN; break;
486 case OP_LT: tm = TM_LT; break;
487 case OP_LE: tm = TM_LE; break;
488 case OP_CONCAT: tm = TM_CONCAT; break;
489 default:
490 return NULL; /* else no useful name can be found */
491 }
492 *name = getstr(G(L)->tmname[tm]);
493 return "metamethod";
494 }
495
496 /* }====================================================== */
497
498
499
500 /*
501 ** only ANSI way to check whether a pointer points to an array
502 ** (used only for error messages, so efficiency is not a big concern)
503 */
isinstack(CallInfo * ci,const TValue * o)504 static int isinstack (CallInfo *ci, const TValue *o) {
505 StkId p;
506 for (p = ci->u.l.base; p < ci->top; p++)
507 if (o == p) return 1;
508 return 0;
509 }
510
511
getupvalname(CallInfo * ci,const TValue * o,const char ** name)512 static const char *getupvalname (CallInfo *ci, const TValue *o,
513 const char **name) {
514 LClosure *c = ci_func(ci);
515 int i;
516 for (i = 0; i < c->nupvalues; i++) {
517 if (c->upvals[i]->v == o) {
518 *name = upvalname(c->p, i);
519 return "upvalue";
520 }
521 }
522 return NULL;
523 }
524
525
luaG_typeerror(lua_State * L,const TValue * o,const char * op)526 l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
527 CallInfo *ci = L->ci;
528 const char *name = NULL;
529 const char *t = objtypename(o);
530 const char *kind = NULL;
531 if (isLua(ci)) {
532 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
533 if (!kind && isinstack(ci, o)) /* no? try a register */
534 kind = getobjname(ci_func(ci)->p, currentpc(ci),
535 cast_int(o - ci->u.l.base), &name);
536 }
537 if (kind)
538 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
539 op, kind, name, t);
540 else
541 luaG_runerror(L, "attempt to %s a %s value", op, t);
542 }
543
544
luaG_concaterror(lua_State * L,StkId p1,StkId p2)545 l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
546 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
547 lua_assert(!ttisstring(p1) && !ttisnumber(p1));
548 luaG_typeerror(L, p1, "concatenate");
549 }
550
551
luaG_aritherror(lua_State * L,const TValue * p1,const TValue * p2)552 l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
553 TValue temp;
554 if (luaV_tonumber(p1, &temp) == NULL)
555 p2 = p1; /* first operand is wrong */
556 luaG_typeerror(L, p2, "perform arithmetic on");
557 }
558
559
luaG_ordererror(lua_State * L,const TValue * p1,const TValue * p2)560 l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
561 const char *t1 = objtypename(p1);
562 const char *t2 = objtypename(p2);
563 if (t1 == t2)
564 luaG_runerror(L, "attempt to compare two %s values", t1);
565 else
566 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
567 }
568
569
addinfo(lua_State * L,const char * msg)570 static void addinfo (lua_State *L, const char *msg) {
571 CallInfo *ci = L->ci;
572 if (isLua(ci)) { /* is Lua code? */
573 char buff[LUA_IDSIZE]; /* add file:line information */
574 int line = currentline(ci);
575 TString *src = ci_func(ci)->p->source;
576 if (src)
577 luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
578 else { /* no source available; use "?" instead */
579 buff[0] = '?'; buff[1] = '\0';
580 }
581 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
582 }
583 }
584
585
luaG_errormsg(lua_State * L)586 l_noret luaG_errormsg (lua_State *L) {
587 if (L->errfunc != 0) { /* is there an error handling function? */
588 StkId errfunc = restorestack(L, L->errfunc);
589 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
590 setobjs2s(L, L->top, L->top - 1); /* move argument */
591 setobjs2s(L, L->top - 1, errfunc); /* push function */
592 L->top++;
593 luaD_call(L, L->top - 2, 1, 0); /* call it */
594 }
595 luaD_throw(L, LUA_ERRRUN);
596 }
597
598
luaG_runerror(lua_State * L,const char * fmt,...)599 l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
600 L->runerror++;
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 L->runerror--;
607 }
608