1/*
2** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $
3** Debug Interface
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdarg.h>
9#include <stddef.h>
10#include <string.h>
11
12
13#define ldebug_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "lapi.h"
19#include "lcode.h"
20#include "ldebug.h"
21#include "ldo.h"
22#include "lfunc.h"
23#include "lobject.h"
24#include "lopcodes.h"
25#include "lstate.h"
26#include "lstring.h"
27#include "ltable.h"
28#include "ltm.h"
29#include "lvm.h"
30
31
32
33static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
34
35
36static int currentpc (lua_State *L, CallInfo *ci) {
37 if (!isLua(ci)) return -1; /* function is not a Lua function? */
38 if (ci == L->ci)
39 ci->savedpc = L->savedpc;
40 return pcRel(ci->savedpc, ci_func(ci)->l.p);
41}
42
43
44static int currentline (lua_State *L, CallInfo *ci) {
45 int pc = currentpc(L, ci);
46 if (pc < 0)
47 return -1; /* only active lua functions have current-line information */
48 else
49 return getline(ci_func(ci)->l.p, pc);
50}
51
52
53/*
54** this function can be called asynchronous (e.g. during a signal)
55*/
56LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
57 if (func == NULL || mask == 0) { /* turn off hooks? */
58 mask = 0;
59 func = NULL;
60 }
61 L->hook = func;
62 L->basehookcount = count;
63 resethookcount(L);
64 L->hookmask = cast_byte(mask);
65 return 1;
66}
67
68
69LUA_API lua_Hook lua_gethook (lua_State *L) {
70 return L->hook;
71}
72
73
74LUA_API int lua_gethookmask (lua_State *L) {
75 return L->hookmask;
76}
77
78
79LUA_API int lua_gethookcount (lua_State *L) {
80 return L->basehookcount;
81}
82
83LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
84 int status;
85 CallInfo *ci;
86 lua_lock(L);
87 for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
88 level--;
89 if (f_isLua(ci)) /* Lua function? */
90 level -= ci->tailcalls; /* skip lost tail calls */
91 }
92 if (level == 0 && ci > L->base_ci) { /* level found? */
93 status = 1;
94 ar->i_ci = cast_int(ci - L->base_ci);
95 }
96 else if (level < 0) { /* level is of a lost tail call? */
97 status = 1;
98 ar->i_ci = 0;
99 }
100 else status = 0; /* no such level */
101 lua_unlock(L);
102 return status;
103}
104
105
106static Proto *getluaproto (CallInfo *ci) {
107 return (isLua(ci) ? ci_func(ci)->l.p : NULL);
108}
109
110
111static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
112 const char *name;
113 Proto *fp = getluaproto(ci);
114 if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
115 return name; /* is a local variable in a Lua function */
116 else {
117 StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
118 if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
119 return "(*temporary)";
120 else
121 return NULL;
122 }
123}
124
125
126LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
127 CallInfo *ci = L->base_ci + ar->i_ci;
128 const char *name = findlocal(L, ci, n);
129 lua_lock(L);
130 if (name)
131 luaA_pushobject(L, ci->base + (n - 1));
132 lua_unlock(L);
133 return name;
134}
135
136
137LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
138 CallInfo *ci = L->base_ci + ar->i_ci;
139 const char *name = findlocal(L, ci, n);
140 lua_lock(L);
141 if (name)
142 setobjs2s(L, ci->base + (n - 1), L->top - 1);
143 L->top--; /* pop value */
144 lua_unlock(L);
145 return name;
146}
147
148
149static void funcinfo (lua_Debug *ar, Closure *cl) {
150 if (cl->c.isC) {
151 ar->source = "=[C]";
152 ar->linedefined = -1;
153 ar->lastlinedefined = -1;
154 ar->what = "C";
155 }
156 else {
157 ar->source = getstr(cl->l.p->source);
158 ar->linedefined = cl->l.p->linedefined;
159 ar->lastlinedefined = cl->l.p->lastlinedefined;
160 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
161 }
162 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
163}
164
165
166static void info_tailcall (lua_Debug *ar) {
167 ar->name = ar->namewhat = "";
168 ar->what = "tail";
169 ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
170 ar->source = "=(tail call)";
171 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
172 ar->nups = 0;
173}
174
175
176static void collectvalidlines (lua_State *L, Closure *f) {
177 if (f == NULL || f->c.isC) {
178 setnilvalue(L->top);
179 }
180 else {
181 Table *t = luaH_new(L, 0, 0);
182 int *lineinfo = f->l.p->lineinfo;
183 int i;
184 for (i=0; i<f->l.p->sizelineinfo; i++)
185 setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
186 sethvalue(L, L->top, t);
187 }
188 incr_top(L);
189}
190
191
192static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
193 Closure *f, CallInfo *ci) {
194 int status = 1;
195 if (f == NULL) {
196 info_tailcall(ar);
197 return status;
198 }
199 for (; *what; what++) {
200 switch (*what) {
201 case 'S': {
202 funcinfo(ar, f);
203 break;
204 }
205 case 'l': {
206 ar->currentline = (ci) ? currentline(L, ci) : -1;
207 break;
208 }
209 case 'u': {
210 ar->nups = f->c.nupvalues;
211 break;
212 }
213 case 'n': {
214 ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
215 if (ar->namewhat == NULL) {
216 ar->namewhat = ""; /* not found */
217 ar->name = NULL;
218 }
219 break;
220 }
221 case 'L':
222 case 'f': /* handled by lua_getinfo */
223 break;
224 default: status = 0; /* invalid option */
225 }
226 }
227 return status;
228}
229
230
231LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
232 int status;
233 Closure *f = NULL;
234 CallInfo *ci = NULL;
235 lua_lock(L);
236 if (*what == '>') {
237 StkId func = L->top - 1;
238 luai_apicheck(L, ttisfunction(func));
239 what++; /* skip the '>' */
240 f = clvalue(func);
241 L->top--; /* pop function */
242 }
243 else if (ar->i_ci != 0) { /* no tail call? */
244 ci = L->base_ci + ar->i_ci;
245 lua_assert(ttisfunction(ci->func));
246 f = clvalue(ci->func);
247 }
248 status = auxgetinfo(L, what, ar, f, ci);
249 if (strchr(what, 'f')) {
250 if (f == NULL) setnilvalue(L->top);
251 else setclvalue(L, L->top, f);
252 incr_top(L);
253 }
254 if (strchr(what, 'L'))
255 collectvalidlines(L, f);
256 lua_unlock(L);
257 return status;
258}
259
260
261/*
262** {======================================================
263** Symbolic Execution and code checker
264** =======================================================
265*/
266
267#define check(x) if (!(x)) return 0;
268
269#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
270
271#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
272
273
274
275static int precheck (const Proto *pt) {
276 check(pt->maxstacksize <= MAXSTACK);
277 check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
278 check(!(pt->is_vararg & VARARG_NEEDSARG) ||
279 (pt->is_vararg & VARARG_HASARG));
280 check(pt->sizeupvalues <= pt->nups);
281 check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
282 check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
283 return 1;
284}
285
286
287#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
288
289int luaG_checkopenop (Instruction i) {
290 switch (GET_OPCODE(i)) {
291 case OP_CALL:
292 case OP_TAILCALL:
293 case OP_RETURN:
294 case OP_SETLIST: {
295 check(GETARG_B(i) == 0);
296 return 1;
297 }
298 default: return 0; /* invalid instruction after an open call */
299 }
300}
301
302
303static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
304 switch (mode) {
305 case OpArgN: check(r == 0); break;
306 case OpArgU: break;
307 case OpArgR: checkreg(pt, r); break;
308 case OpArgK:
309 check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
310 break;
311 }
312 return 1;
313}
314
315
316static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
317 int pc;
318 int last; /* stores position of last instruction that changed `reg' */
319 last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
320 check(precheck(pt));
321 for (pc = 0; pc < lastpc; pc++) {
322 Instruction i = pt->code[pc];
323 OpCode op = GET_OPCODE(i);
324 int a = GETARG_A(i);
325 int b = 0;
326 int c = 0;
327 check(op < NUM_OPCODES);
328 checkreg(pt, a);
329 switch (getOpMode(op)) {
330 case iABC: {
331 b = GETARG_B(i);
332 c = GETARG_C(i);
333 check(checkArgMode(pt, b, getBMode(op)));
334 check(checkArgMode(pt, c, getCMode(op)));
335 break;
336 }
337 case iABx: {
338 b = GETARG_Bx(i);
339 if (getBMode(op) == OpArgK) check(b < pt->sizek);
340 break;
341 }
342 case iAsBx: {
343 b = GETARG_sBx(i);
344 if (getBMode(op) == OpArgR) {
345 int dest = pc+1+b;
346 check(0 <= dest && dest < pt->sizecode);
347 if (dest > 0) {
348 int j;
349 /* check that it does not jump to a setlist count; this
350 is tricky, because the count from a previous setlist may
351 have the same value of an invalid setlist; so, we must
352 go all the way back to the first of them (if any) */
353 for (j = 0; j < dest; j++) {
354 Instruction d = pt->code[dest-1-j];
355 if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
356 }
357 /* if 'j' is even, previous value is not a setlist (even if
358 it looks like one) */
359 check((j&1) == 0);
360 }
361 }
362 break;
363 }
364 }
365 if (testAMode(op)) {
366 if (a == reg) last = pc; /* change register `a' */
367 }
368 if (testTMode(op)) {
369 check(pc+2 < pt->sizecode); /* check skip */
370 check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
371 }
372 switch (op) {
373 case OP_LOADBOOL: {
374 if (c == 1) { /* does it jump? */
375 check(pc+2 < pt->sizecode); /* check its jump */
376 check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
377 GETARG_C(pt->code[pc+1]) != 0);
378 }
379 break;
380 }
381 case OP_LOADNIL: {
382 if (a <= reg && reg <= b)
383 last = pc; /* set registers from `a' to `b' */
384 break;
385 }
386 case OP_GETUPVAL:
387 case OP_SETUPVAL: {
388 check(b < pt->nups);
389 break;
390 }
391 case OP_GETGLOBAL:
392 case OP_SETGLOBAL: {
393 check(ttisstring(&pt->k[b]));
394 break;
395 }
396 case OP_SELF: {
397 checkreg(pt, a+1);
398 if (reg == a+1) last = pc;
399 break;
400 }
401 case OP_CONCAT: {
402 check(b < c); /* at least two operands */
403 break;
404 }
405 case OP_TFORLOOP: {
406 check(c >= 1); /* at least one result (control variable) */
407 checkreg(pt, a+2+c); /* space for results */
408 if (reg >= a+2) last = pc; /* affect all regs above its base */
409 break;
410 }
411 case OP_FORLOOP:
412 case OP_FORPREP:
413 checkreg(pt, a+3);
414 /* go through */
415 case OP_JMP: {
416 int dest = pc+1+b;
417 /* not full check and jump is forward and do not skip `lastpc'? */
418 if (reg != NO_REG && pc < dest && dest <= lastpc)
419 pc += b; /* do the jump */
420 break;
421 }
422 case OP_CALL:
423 case OP_TAILCALL: {
424 if (b != 0) {
425 checkreg(pt, a+b-1);
426 }
427 c--; /* c = num. returns */
428 if (c == LUA_MULTRET) {
429 check(checkopenop(pt, pc));
430 }
431 else if (c != 0)
432 checkreg(pt, a+c-1);
433 if (reg >= a) last = pc; /* affect all registers above base */
434 break;
435 }
436 case OP_RETURN: {
437 b--; /* b = num. returns */
438 if (b > 0) checkreg(pt, a+b-1);
439 break;
440 }
441 case OP_SETLIST: {
442 if (b > 0) checkreg(pt, a + b);
443 if (c == 0) {
444 pc++;
445 check(pc < pt->sizecode - 1);
446 }
447 break;
448 }
449 case OP_CLOSURE: {
450 int nup, j;
451 check(b < pt->sizep);
452 nup = pt->p[b]->nups;
453 check(pc + nup < pt->sizecode);
454 for (j = 1; j <= nup; j++) {
455 OpCode op1 = GET_OPCODE(pt->code[pc + j]);
456 check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
457 }
458 if (reg != NO_REG) /* tracing? */
459 pc += nup; /* do not 'execute' these pseudo-instructions */
460 break;
461 }
462 case OP_VARARG: {
463 check((pt->is_vararg & VARARG_ISVARARG) &&
464 !(pt->is_vararg & VARARG_NEEDSARG));
465 b--;
466 if (b == LUA_MULTRET) check(checkopenop(pt, pc));
467 checkreg(pt, a+b-1);
468 break;
469 }
470 default: break;
471 }
472 }
473 return pt->code[last];
474}
475
476#undef check
477#undef checkjump
478#undef checkreg
479
480/* }====================================================== */
481
482
483int luaG_checkcode (const Proto *pt) {
484 return (symbexec(pt, pt->sizecode, NO_REG) != 0);
485}
486
487
488static const char *kname (Proto *p, int c) {
489 if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
490 return svalue(&p->k[INDEXK(c)]);
491 else
492 return "?";
493}
494
495
496static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
497 const char **name) {
498 if (isLua(ci)) { /* a Lua function? */
499 Proto *p = ci_func(ci)->l.p;
500 int pc = currentpc(L, ci);
501 Instruction i;
502 *name = luaF_getlocalname(p, stackpos+1, pc);
503 if (*name) /* is a local? */
504 return "local";
505 i = symbexec(p, pc, stackpos); /* try symbolic execution */
506 lua_assert(pc != -1);
507 switch (GET_OPCODE(i)) {
508 case OP_GETGLOBAL: {
509 int g = GETARG_Bx(i); /* global index */
510 lua_assert(ttisstring(&p->k[g]));
511 *name = svalue(&p->k[g]);
512 return "global";
513 }
514 case OP_MOVE: {
515 int a = GETARG_A(i);
516 int b = GETARG_B(i); /* move from `b' to `a' */
517 if (b < a)
518 return getobjname(L, ci, b, name); /* get name for `b' */
519 break;
520 }
521 case OP_GETTABLE: {
522 int k = GETARG_C(i); /* key index */
523 *name = kname(p, k);
524 return "field";
525 }
526 case OP_GETUPVAL: {
527 int u = GETARG_B(i); /* upvalue index */
528 *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
529 return "upvalue";
530 }
531 case OP_SELF: {
532 int k = GETARG_C(i); /* key index */
533 *name = kname(p, k);
534 return "method";
535 }
536 default: break;
537 }
538 }
539 return NULL; /* no useful name found */
540}
541
542
543static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
544 Instruction i;
545 if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
546 return NULL; /* calling function is not Lua (or is unknown) */
547 ci--; /* calling function */
548 i = ci_func(ci)->l.p->code[currentpc(L, ci)];
549 if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
550 GET_OPCODE(i) == OP_TFORLOOP)
551 return getobjname(L, ci, GETARG_A(i), name);
552 else
553 return NULL; /* no useful name can be found */
554}
555
556
557/* only ANSI way to check whether a pointer points to an array */
558static int isinstack (CallInfo *ci, const TValue *o) {
559 StkId p;
560 for (p = ci->base; p < ci->top; p++)
561 if (o == p) return 1;
562 return 0;
563}
564
565
566void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
567 const char *name = NULL;
568 const char *t = luaT_typenames[ttype(o)];
569 const char *kind = (isinstack(L->ci, o)) ?
570 getobjname(L, L->ci, cast_int(o - L->base), &name) :
571 NULL;
572 if (kind)
573 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
574 op, kind, name, t);
575 else
576 luaG_runerror(L, "attempt to %s a %s value", op, t);
577}
578
579
580void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
581 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
582 lua_assert(!ttisstring(p1) && !ttisnumber(p1));
583 luaG_typeerror(L, p1, "concatenate");
584}
585
586
587void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
588 TValue temp;
589 if (luaV_tonumber(p1, &temp) == NULL)
590 p2 = p1; /* first operand is wrong */
591 luaG_typeerror(L, p2, "perform arithmetic on");
592}
593
594
595int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
596 const char *t1 = luaT_typenames[ttype(p1)];
597 const char *t2 = luaT_typenames[ttype(p2)];
598 if (t1[2] == t2[2])
599 luaG_runerror(L, "attempt to compare two %s values", t1);
600 else
601 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
602 return 0;
603}
604
605
606static void addinfo (lua_State *L, const char *msg) {
607 CallInfo *ci = L->ci;
608 if (isLua(ci)) { /* is Lua code? */
609 char buff[LUA_IDSIZE]; /* add file:line information */
610 int line = currentline(L, ci);
611 luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
612 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
613 }
614}
615
616
617void luaG_errormsg (lua_State *L) {
618 if (L->errfunc != 0) { /* is there an error handling function? */
619 StkId errfunc = restorestack(L, L->errfunc);
620 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
621 setobjs2s(L, L->top, L->top - 1); /* move argument */
622 setobjs2s(L, L->top - 1, errfunc); /* push function */
623 incr_top(L);
624 luaD_call(L, L->top - 2, 1); /* call it */
625 }
626 luaD_throw(L, LUA_ERRRUN);
627}
628
629
630void luaG_runerror (lua_State *L, const char *fmt, ...) {
631 va_list argp;
632 va_start(argp, fmt);
633 addinfo(L, luaO_pushvfstring(L, fmt, argp));
634 va_end(argp);
635 luaG_errormsg(L);
636}
637
638