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