renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / deps / liblua / ldebug.c
bloba993ad65f3d30ec1dccd7d27578acfe1f955a815
1 /*
2 ** $Id: ldebug.c,v 2.90 2012/08/16 17:34:28 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));
51 ** this function can be called asynchronous (e.g. during a signal)
53 LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count) {
54 if (func == NULL || mask == 0) { /* turn off hooks? */
55 mask = 0;
56 func = NULL;
58 if (isLua(L->ci))
59 L->oldpc = L->ci->u.l.savedpc;
60 L->hook = func;
61 L->basehookcount = count;
62 resethookcount(L);
63 L->hookmask = cast_byte(mask);
64 return 1;
68 LUA_API lua_Hook lua_gethook(lua_State *L) {
69 return L->hook;
73 LUA_API int lua_gethookmask(lua_State *L) {
74 return L->hookmask;
78 LUA_API int lua_gethookcount(lua_State *L) {
79 return L->basehookcount;
83 LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) {
84 int status;
85 CallInfo *ci;
86 if (level < 0) return 0; /* invalid (negative) level */
87 lua_lock(L);
88 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
89 level--;
90 if (level == 0 && ci != &L->base_ci) { /* level found? */
91 status = 1;
92 ar->i_ci = ci;
93 } else status = 0; /* no such level */
94 lua_unlock(L);
95 return status;
99 static const char *upvalname(Proto *p, int uv) {
100 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
101 if (s == NULL) return "?";
102 else return getstr(s);
106 static const char *findvararg(CallInfo *ci, int n, StkId *pos) {
107 int nparams = clLvalue(ci->func)->p->numparams;
108 if (n >= ci->u.l.base - ci->func - nparams)
109 return NULL; /* no such vararg */
110 else {
111 *pos = ci->func + nparams + n;
112 return "(*vararg)"; /* generic name for any vararg */
117 static const char *findlocal(lua_State *L, CallInfo *ci, int n,
118 StkId *pos) {
119 const char *name = NULL;
120 StkId base;
121 if (isLua(ci)) {
122 if (n < 0) /* access to vararg values? */
123 return findvararg(ci, -n, pos);
124 else {
125 base = ci->u.l.base;
126 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
128 } else
129 base = ci->func + 1;
130 if (name == NULL) { /* no 'standard' name? */
131 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
132 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
133 name = "(*temporary)"; /* generic name for any valid slot */
134 else
135 return NULL; /* no name */
137 *pos = base + (n - 1);
138 return name;
142 LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) {
143 const char *name;
144 lua_lock(L);
145 if (ar == NULL) { /* information about non-active function? */
146 if (!isLfunction(L->top - 1)) /* not a Lua function? */
147 name = NULL;
148 else /* consider live variables at function start (parameters) */
149 name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
150 } else { /* active function; get information through 'ar' */
151 StkId pos = 0; /* to avoid warnings */
152 name = findlocal(L, ar->i_ci, n, &pos);
153 if (name) {
154 setobj2s(L, L->top, pos);
155 api_incr_top(L);
158 lua_unlock(L);
159 return name;
163 LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) {
164 StkId pos = 0; /* to avoid warnings */
165 const char *name = findlocal(L, ar->i_ci, n, &pos);
166 lua_lock(L);
167 if (name)
168 setobjs2s(L, pos, L->top - 1);
169 L->top--; /* pop value */
170 lua_unlock(L);
171 return name;
175 static void funcinfo(lua_Debug *ar, Closure *cl) {
176 if (noLuaClosure(cl)) {
177 ar->source = "=[C]";
178 ar->linedefined = -1;
179 ar->lastlinedefined = -1;
180 ar->what = "C";
181 } else {
182 Proto *p = cl->l.p;
183 ar->source = p->source ? getstr(p->source) : "=?";
184 ar->linedefined = p->linedefined;
185 ar->lastlinedefined = p->lastlinedefined;
186 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
188 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
192 static void collectvalidlines(lua_State *L, Closure *f) {
193 if (noLuaClosure(f)) {
194 setnilvalue(L->top);
195 api_incr_top(L);
196 } else {
197 int i;
198 TValue v;
199 int *lineinfo = f->l.p->lineinfo;
200 Table *t = luaH_new(L); /* new table to store active lines */
201 sethvalue(L, L->top, t); /* push it on stack */
202 api_incr_top(L);
203 setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
204 for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
205 luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
210 static int auxgetinfo(lua_State *L, const char *what, lua_Debug *ar,
211 Closure *f, CallInfo *ci) {
212 int status = 1;
213 for (; *what; what++) {
214 switch (*what) {
215 case 'S': {
216 funcinfo(ar, f);
217 break;
219 case 'l': {
220 ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
221 break;
223 case 'u': {
224 ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
225 if (noLuaClosure(f)) {
226 ar->isvararg = 1;
227 ar->nparams = 0;
228 } else {
229 ar->isvararg = f->l.p->is_vararg;
230 ar->nparams = f->l.p->numparams;
232 break;
234 case 't': {
235 ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
236 break;
238 case 'n': {
239 /* calling function is a known Lua function? */
240 if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
241 ar->namewhat = getfuncname(L, ci->previous, &ar->name);
242 else
243 ar->namewhat = NULL;
244 if (ar->namewhat == NULL) {
245 ar->namewhat = ""; /* not found */
246 ar->name = NULL;
248 break;
250 case 'L':
251 case 'f': /* handled by lua_getinfo */
252 break;
253 default:
254 status = 0; /* invalid option */
257 return status;
261 LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) {
262 int status;
263 Closure *cl;
264 CallInfo *ci;
265 StkId func;
266 lua_lock(L);
267 if (*what == '>') {
268 ci = NULL;
269 func = L->top - 1;
270 api_check(L, ttisfunction(func), "function expected");
271 what++; /* skip the '>' */
272 L->top--; /* pop function */
273 } else {
274 ci = ar->i_ci;
275 func = ci->func;
276 lua_assert(ttisfunction(ci->func));
278 cl = ttisclosure(func) ? clvalue(func) : NULL;
279 status = auxgetinfo(L, what, ar, cl, ci);
280 if (strchr(what, 'f')) {
281 setobjs2s(L, L->top, func);
282 api_incr_top(L);
284 if (strchr(what, 'L'))
285 collectvalidlines(L, cl);
286 lua_unlock(L);
287 return status;
292 ** {======================================================
293 ** Symbolic Execution
294 ** =======================================================
297 static const char *getobjname(Proto *p, int lastpc, int reg,
298 const char **name);
302 ** find a "name" for the RK value 'c'
304 static void kname(Proto *p, int pc, int c, const char **name) {
305 if (ISK(c)) { /* is 'c' a constant? */
306 TValue *kvalue = &p->k[INDEXK(c)];
307 if (ttisstring(kvalue)) { /* literal constant? */
308 *name = svalue(kvalue); /* it is its own name */
309 return;
311 /* else no reasonable name found */
312 } else { /* 'c' is a register */
313 const char *what = getobjname(p, pc, c, name); /* search for 'c' */
314 if (what && *what == 'c') { /* found a constant name? */
315 return; /* 'name' already filled */
317 /* else no reasonable name found */
319 *name = "?"; /* no reasonable name found */
324 ** try to find last instruction before 'lastpc' that modified register 'reg'
326 static int findsetreg(Proto *p, int lastpc, int reg) {
327 int pc;
328 int setreg = -1; /* keep last instruction that changed 'reg' */
329 for (pc = 0; pc < lastpc; pc++) {
330 Instruction i = p->code[pc];
331 OpCode op = GET_OPCODE(i);
332 int a = GETARG_A(i);
333 switch (op) {
334 case OP_LOADNIL: {
335 int b = GETARG_B(i);
336 if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
337 setreg = pc;
338 break;
340 case OP_TFORCALL: {
341 if (reg >= a + 2) setreg = pc; /* affect all regs above its base */
342 break;
344 case OP_CALL:
345 case OP_TAILCALL: {
346 if (reg >= a) setreg = pc; /* affect all registers above base */
347 break;
349 case OP_JMP: {
350 int b = GETARG_sBx(i);
351 int dest = pc + 1 + b;
352 /* jump is forward and do not skip `lastpc'? */
353 if (pc < dest && dest <= lastpc)
354 pc += b; /* do the jump */
355 break;
357 case OP_TEST: {
358 if (reg == a) setreg = pc; /* jumped code can change 'a' */
359 break;
361 default:
362 if (testAMode(op) && reg == a) /* any instruction that set A */
363 setreg = pc;
364 break;
367 return setreg;
371 static const char *getobjname(Proto *p, int lastpc, int reg,
372 const char **name) {
373 int pc;
374 *name = luaF_getlocalname(p, reg + 1, lastpc);
375 if (*name) /* is a local? */
376 return "local";
377 /* else try symbolic execution */
378 pc = findsetreg(p, lastpc, reg);
379 if (pc != -1) { /* could find instruction? */
380 Instruction i = p->code[pc];
381 OpCode op = GET_OPCODE(i);
382 switch (op) {
383 case OP_MOVE: {
384 int b = GETARG_B(i); /* move from 'b' to 'a' */
385 if (b < GETARG_A(i))
386 return getobjname(p, pc, b, name); /* get name for 'b' */
387 break;
389 case OP_GETTABUP:
390 case OP_GETTABLE: {
391 int k = GETARG_C(i); /* key index */
392 int t = GETARG_B(i); /* table index */
393 const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
394 ? luaF_getlocalname(p, t + 1, pc)
395 : upvalname(p, t);
396 kname(p, pc, k, name);
397 return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
399 case OP_GETUPVAL: {
400 *name = upvalname(p, GETARG_B(i));
401 return "upvalue";
403 case OP_LOADK:
404 case OP_LOADKX: {
405 int b = (op == OP_LOADK) ? GETARG_Bx(i)
406 : GETARG_Ax(p->code[pc + 1]);
407 if (ttisstring(&p->k[b])) {
408 *name = svalue(&p->k[b]);
409 return "constant";
411 break;
413 case OP_SELF: {
414 int k = GETARG_C(i); /* key index */
415 kname(p, pc, k, name);
416 return "method";
418 default:
419 break; /* go through to return NULL */
422 return NULL; /* could not find reasonable name */
426 static const char *getfuncname(lua_State *L, CallInfo *ci, const char **name) {
427 TMS tm;
428 Proto *p = ci_func(ci)->p; /* calling function */
429 int pc = currentpc(ci); /* calling instruction index */
430 Instruction i = p->code[pc]; /* calling instruction */
431 switch (GET_OPCODE(i)) {
432 case OP_CALL:
433 case OP_TAILCALL: /* get function name */
434 return getobjname(p, pc, GETARG_A(i), name);
435 case OP_TFORCALL: { /* for iterator */
436 *name = "for iterator";
437 return "for iterator";
439 /* all other instructions can call only through metamethods */
440 case OP_SELF:
441 case OP_GETTABUP:
442 case OP_GETTABLE:
443 tm = TM_INDEX;
444 break;
445 case OP_SETTABUP:
446 case OP_SETTABLE:
447 tm = TM_NEWINDEX;
448 break;
449 case OP_EQ:
450 tm = TM_EQ;
451 break;
452 case OP_ADD:
453 tm = TM_ADD;
454 break;
455 case OP_SUB:
456 tm = TM_SUB;
457 break;
458 case OP_MUL:
459 tm = TM_MUL;
460 break;
461 case OP_DIV:
462 tm = TM_DIV;
463 break;
464 case OP_MOD:
465 tm = TM_MOD;
466 break;
467 case OP_POW:
468 tm = TM_POW;
469 break;
470 case OP_UNM:
471 tm = TM_UNM;
472 break;
473 case OP_LEN:
474 tm = TM_LEN;
475 break;
476 case OP_LT:
477 tm = TM_LT;
478 break;
479 case OP_LE:
480 tm = TM_LE;
481 break;
482 case OP_CONCAT:
483 tm = TM_CONCAT;
484 break;
485 default:
486 return NULL; /* else no useful name can be found */
488 *name = getstr(G(L)->tmname[tm]);
489 return "metamethod";
492 /* }====================================================== */
497 ** only ANSI way to check whether a pointer points to an array
498 ** (used only for error messages, so efficiency is not a big concern)
500 static int isinstack(CallInfo *ci, const TValue *o) {
501 StkId p;
502 for (p = ci->u.l.base; p < ci->top; p++)
503 if (o == p) return 1;
504 return 0;
508 static const char *getupvalname(CallInfo *ci, const TValue *o,
509 const char **name) {
510 LClosure *c = ci_func(ci);
511 int i;
512 for (i = 0; i < c->nupvalues; i++) {
513 if (c->upvals[i]->v == o) {
514 *name = upvalname(c->p, i);
515 return "upvalue";
518 return NULL;
522 l_noret luaG_typeerror(lua_State *L, const TValue *o, const char *op) {
523 CallInfo *ci = L->ci;
524 const char *name = NULL;
525 const char *t = objtypename(o);
526 const char *kind = NULL;
527 if (isLua(ci)) {
528 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
529 if (!kind && isinstack(ci, o)) /* no? try a register */
530 kind = getobjname(ci_func(ci)->p, currentpc(ci),
531 cast_int(o - ci->u.l.base), &name);
533 if (kind)
534 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
535 op, kind, name, t);
536 else
537 luaG_runerror(L, "attempt to %s a %s value", op, t);
541 l_noret luaG_concaterror(lua_State *L, StkId p1, StkId p2) {
542 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
543 lua_assert(!ttisstring(p1) && !ttisnumber(p2));
544 luaG_typeerror(L, p1, "concatenate");
548 l_noret luaG_aritherror(lua_State *L, const TValue *p1, const TValue *p2) {
549 TValue temp;
550 if (luaV_tonumber(p1, &temp) == NULL)
551 p2 = p1; /* first operand is wrong */
552 luaG_typeerror(L, p2, "perform arithmetic on");
556 l_noret luaG_ordererror(lua_State *L, const TValue *p1, const TValue *p2) {
557 const char *t1 = objtypename(p1);
558 const char *t2 = objtypename(p2);
559 if (t1 == t2)
560 luaG_runerror(L, "attempt to compare two %s values", t1);
561 else
562 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
566 static void addinfo(lua_State *L, const char *msg) {
567 CallInfo *ci = L->ci;
568 if (isLua(ci)) { /* is Lua code? */
569 char buff[LUA_IDSIZE]; /* add file:line information */
570 int line = currentline(ci);
571 TString *src = ci_func(ci)->p->source;
572 if (src)
573 luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
574 else { /* no source available; use "?" instead */
575 buff[0] = '?';
576 buff[1] = '\0';
578 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
583 l_noret luaG_errormsg(lua_State *L) {
584 if (L->errfunc != 0) { /* is there an error handling function? */
585 StkId errfunc = restorestack(L, L->errfunc);
586 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
587 setobjs2s(L, L->top, L->top - 1); /* move argument */
588 setobjs2s(L, L->top - 1, errfunc); /* push function */
589 L->top++;
590 luaD_call(L, L->top - 2, 1, 0); /* call it */
592 luaD_throw(L, LUA_ERRRUN);
596 l_noret luaG_runerror(lua_State *L, const char *fmt, ...) {
597 va_list argp;
598 va_start(argp, fmt);
599 addinfo(L, luaO_pushvfstring(L, fmt, argp));
600 va_end(argp);
601 luaG_errormsg(L);