Lua 5.1.4: ldebug.c
L0001
L0006
L0007
L0008 #include <stdarg.h>
L0009 #include <stddef.h>
L0010 #include <string.h>
L0011
L0012
L0013 #define ldebug_c
L0014 #define LUA_CORE
L0015
L0016 #include "lua.h"
L0017
L0018 #include "lapi.h"
L0019 #include "lcode.h"
L0020 #include "ldebug.h"
L0021 #include "ldo.h"
L0022 #include "lfunc.h"
L0023 #include "lobject.h"
L0024 #include "lopcodes.h"
L0025 #include "lstate.h"
L0026 #include "lstring.h"
L0027 #include "ltable.h"
L0028 #include "ltm.h"
L0029 #include "lvm.h"
L0030
L0031
L0032
L0033 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
L0034
L0035
L0036 static int currentpc (lua_State *L, CallInfo *ci) {
L0037 if (!isLua(ci)) return -1;
L0038 if (ci == L->ci)
L0039 ci->savedpc = L->savedpc;
L0040 return pcRel(ci->savedpc, ci_func(ci)->l.p);
L0041 }
L0042
L0043
L0044 static int currentline (lua_State *L, CallInfo *ci) {
L0045 int pc = currentpc(L, ci);
L0046 if (pc < 0)
L0047 return -1;
L0048 else
L0049 return getline(ci_func(ci)->l.p, pc);
L0050 }
L0051
L0052
L0053
L0056 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
L0057 if (func == NULL || mask == 0) {
L0058 mask = 0;
L0059 func = NULL;
L0060 }
L0061 L->hook = func;
L0062 L->basehookcount = count;
L0063 resethookcount(L);
L0064 L->hookmask = cast_byte(mask);
L0065 return 1;
L0066 }
L0067
L0068
L0069 LUA_API lua_Hook lua_gethook (lua_State *L) {
L0070 return L->hook;
L0071 }
L0072
L0073
L0074 LUA_API int lua_gethookmask (lua_State *L) {
L0075 return L->hookmask;
L0076 }
L0077
L0078
L0079 LUA_API int lua_gethookcount (lua_State *L) {
L0080 return L->basehookcount;
L0081 }
L0082
L0083
L0084 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
L0085 int status;
L0086 CallInfo *ci;
L0087 lua_lock(L);
L0088 for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
L0089 level--;
L0090 if (f_isLua(ci))
L0091 level -= ci->tailcalls;
L0092 }
L0093 if (level == 0 && ci > L->base_ci) {
L0094 status = 1;
L0095 ar->i_ci = cast_int(ci - L->base_ci);
L0096 }
L0097 else if (level < 0) {
L0098 status = 1;
L0099 ar->i_ci = 0;
L0100 }
L0101 else status = 0;
L0102 lua_unlock(L);
L0103 return status;
L0104 }
L0105
L0106
L0107 static Proto *getluaproto (CallInfo *ci) {
L0108 return (isLua(ci) ? ci_func(ci)->l.p : NULL);
L0109 }
L0110
L0111
L0112 static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
L0113 const char *name;
L0114 Proto *fp = getluaproto(ci);
L0115 if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
L0116 return name;
L0117 else {
L0118 StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
L0119 if (limit - ci->base >= n && n > 0)
L0120 return "(*temporary)";
L0121 else
L0122 return NULL;
L0123 }
L0124 }
L0125
L0126
L0127 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
L0128 CallInfo *ci = L->base_ci + ar->i_ci;
L0129 const char *name = findlocal(L, ci, n);
L0130 lua_lock(L);
L0131 if (name)
L0132 luaA_pushobject(L, ci->base + (n - 1));
L0133 lua_unlock(L);
L0134 return name;
L0135 }
L0136
L0137
L0138 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
L0139 CallInfo *ci = L->base_ci + ar->i_ci;
L0140 const char *name = findlocal(L, ci, n);
L0141 lua_lock(L);
L0142 if (name)
L0143 setobjs2s(L, ci->base + (n - 1), L->top - 1);
L0144 L->top--;
L0145 lua_unlock(L);
L0146 return name;
L0147 }
L0148
L0149
L0150 static void funcinfo (lua_Debug *ar, Closure *cl) {
L0151 if (cl->c.isC) {
L0152 ar->source = "=[C]";
L0153 ar->linedefined = -1;
L0154 ar->lastlinedefined = -1;
L0155 ar->what = "C";
L0156 }
L0157 else {
L0158 ar->source = getstr(cl->l.p->source);
L0159 ar->linedefined = cl->l.p->linedefined;
L0160 ar->lastlinedefined = cl->l.p->lastlinedefined;
L0161 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
L0162 }
L0163 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
L0164 }
L0165
L0166
L0167 static void info_tailcall (lua_Debug *ar) {
L0168 ar->name = ar->namewhat = "";
L0169 ar->what = "tail";
L0170 ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
L0171 ar->source = "=(tail call)";
L0172 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
L0173 ar->nups = 0;
L0174 }
L0175
L0176
L0177 static void collectvalidlines (lua_State *L, Closure *f) {
L0178 if (f == NULL || f->c.isC) {
L0179 setnilvalue(L->top);
L0180 }
L0181 else {
L0182 Table *t = luaH_new(L, 0, 0);
L0183 int *lineinfo = f->l.p->lineinfo;
L0184 int i;
L0185 for (i=0; i<f->l.p->sizelineinfo; i++)
L0186 setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
L0187 sethvalue(L, L->top, t);
L0188 }
L0189 incr_top(L);
L0190 }
L0191
L0192
L0193 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
L0194 Closure *f, CallInfo *ci) {
L0195 int status = 1;
L0196 if (f == NULL) {
L0197 info_tailcall(ar);
L0198 return status;
L0199 }
L0200 for (; *what; what++) {
L0201 switch (*what) {
L0202 case 'S': {
L0203 funcinfo(ar, f);
L0204 break;
L0205 }
L0206 case 'l': {
L0207 ar->currentline = (ci) ? currentline(L, ci) : -1;
L0208 break;
L0209 }
L0210 case 'u': {
L0211 ar->nups = f->c.nupvalues;
L0212 break;
L0213 }
L0214 case 'n': {
L0215 ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
L0216 if (ar->namewhat == NULL) {
L0217 ar->namewhat = "";
L0218 ar->name = NULL;
L0219 }
L0220 break;
L0221 }
L0222 case 'L':
L0223 case 'f':
L0224 break;
L0225 default: status = 0;
L0226 }
L0227 }
L0228 return status;
L0229 }
L0230
L0231
L0232 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
L0233 int status;
L0234 Closure *f = NULL;
L0235 CallInfo *ci = NULL;
L0236 lua_lock(L);
L0237 if (*what == '>') {
L0238 StkId func = L->top - 1;
L0239 luai_apicheck(L, ttisfunction(func));
L0240 what++;
L0241 f = clvalue(func);
L0242 L->top--;
L0243 }
L0244 else if (ar->i_ci != 0) {
L0245 ci = L->base_ci + ar->i_ci;
L0246 lua_assert(ttisfunction(ci->func));
L0247 f = clvalue(ci->func);
L0248 }
L0249 status = auxgetinfo(L, what, ar, f, ci);
L0250 if (strchr(what, 'f')) {
L0251 if (f == NULL) setnilvalue(L->top);
L0252 else setclvalue(L, L->top, f);
L0253 incr_top(L);
L0254 }
L0255 if (strchr(what, 'L'))
L0256 collectvalidlines(L, f);
L0257 lua_unlock(L);
L0258 return status;
L0259 }
L0260
L0261
L0262
L0267
L0268 #define check(x) if (!(x)) return 0;
L0269
L0270 #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
L0271
L0272 #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
L0273
L0274
L0275
L0276 static int precheck (const Proto *pt) {
L0277 check(pt->maxstacksize <= MAXSTACK);
L0278 check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
L0279 check(!(pt->is_vararg & VARARG_NEEDSARG) ||
L0280 (pt->is_vararg & VARARG_HASARG));
L0281 check(pt->sizeupvalues <= pt->nups);
L0282 check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
L0283 check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
L0284 return 1;
L0285 }
L0286
L0287
L0288 #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
L0289
L0290 int luaG_checkopenop (Instruction i) {
L0291 switch (GET_OPCODE(i)) {
L0292 case OP_CALL:
L0293 case OP_TAILCALL:
L0294 case OP_RETURN:
L0295 case OP_SETLIST: {
L0296 check(GETARG_B(i) == 0);
L0297 return 1;
L0298 }
L0299 default: return 0;
L0300 }
L0301 }
L0302
L0303
L0304 static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
L0305 switch (mode) {
L0306 case OpArgN: check(r == 0); break;
L0307 case OpArgU: break;
L0308 case OpArgR: checkreg(pt, r); break;
L0309 case OpArgK:
L0310 check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
L0311 break;
L0312 }
L0313 return 1;
L0314 }
L0315
L0316
L0317 static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
L0318 int pc;
L0319 int last;
L0320 last = pt->sizecode-1;
L0321 check(precheck(pt));
L0322 for (pc = 0; pc < lastpc; pc++) {
L0323 Instruction i = pt->code[pc];
L0324 OpCode op = GET_OPCODE(i);
L0325 int a = GETARG_A(i);
L0326 int b = 0;
L0327 int c = 0;
L0328 check(op < NUM_OPCODES);
L0329 checkreg(pt, a);
L0330 switch (getOpMode(op)) {
L0331 case iABC: {
L0332 b = GETARG_B(i);
L0333 c = GETARG_C(i);
L0334 check(checkArgMode(pt, b, getBMode(op)));
L0335 check(checkArgMode(pt, c, getCMode(op)));
L0336 break;
L0337 }
L0338 case iABx: {
L0339 b = GETARG_Bx(i);
L0340 if (getBMode(op) == OpArgK) check(b < pt->sizek);
L0341 break;
L0342 }
L0343 case iAsBx: {
L0344 b = GETARG_sBx(i);
L0345 if (getBMode(op) == OpArgR) {
L0346 int dest = pc+1+b;
L0347 check(0 <= dest && dest < pt->sizecode);
L0348 if (dest > 0) {
L0349 int j;
L0350
L0354 for (j = 0; j < dest; j++) {
L0355 Instruction d = pt->code[dest-1-j];
L0356 if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
L0357 }
L0358
L0360 check((j&1) == 0);
L0361 }
L0362 }
L0363 break;
L0364 }
L0365 }
L0366 if (testAMode(op)) {
L0367 if (a == reg) last = pc;
L0368 }
L0369 if (testTMode(op)) {
L0370 check(pc+2 < pt->sizecode);
L0371 check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
L0372 }
L0373 switch (op) {
L0374 case OP_LOADBOOL: {
L0375 if (c == 1) {
L0376 check(pc+2 < pt->sizecode);
L0377 check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
L0378 GETARG_C(pt->code[pc+1]) != 0);
L0379 }
L0380 break;
L0381 }
L0382 case OP_LOADNIL: {
L0383 if (a <= reg && reg <= b)
L0384 last = pc;
L0385 break;
L0386 }
L0387 case OP_GETUPVAL:
L0388 case OP_SETUPVAL: {
L0389 check(b < pt->nups);
L0390 break;
L0391 }
L0392 case OP_GETGLOBAL:
L0393 case OP_SETGLOBAL: {
L0394 check(ttisstring(&pt->k[b]));
L0395 break;
L0396 }
L0397 case OP_SELF: {
L0398 checkreg(pt, a+1);
L0399 if (reg == a+1) last = pc;
L0400 break;
L0401 }
L0402 case OP_CONCAT: {
L0403 check(b < c);
L0404 break;
L0405 }
L0406 case OP_TFORLOOP: {
L0407 check(c >= 1);
L0408 checkreg(pt, a+2+c);
L0409 if (reg >= a+2) last = pc;
L0410 break;
L0411 }
L0412 case OP_FORLOOP:
L0413 case OP_FORPREP:
L0414 checkreg(pt, a+3);
L0415
L0416 case OP_JMP: {
L0417 int dest = pc+1+b;
L0418
L0419 if (reg != NO_REG && pc < dest && dest <= lastpc)
L0420 pc += b;
L0421 break;
L0422 }
L0423 case OP_CALL:
L0424 case OP_TAILCALL: {
L0425 if (b != 0) {
L0426 checkreg(pt, a+b-1);
L0427 }
L0428 c--;
L0429 if (c == LUA_MULTRET) {
L0430 check(checkopenop(pt, pc));
L0431 }
L0432 else if (c != 0)
L0433 checkreg(pt, a+c-1);
L0434 if (reg >= a) last = pc;
L0435 break;
L0436 }
L0437 case OP_RETURN: {
L0438 b--;
L0439 if (b > 0) checkreg(pt, a+b-1);
L0440 break;
L0441 }
L0442 case OP_SETLIST: {
L0443 if (b > 0) checkreg(pt, a + b);
L0444 if (c == 0) {
L0445 pc++;
L0446 check(pc < pt->sizecode - 1);
L0447 }
L0448 break;
L0449 }
L0450 case OP_CLOSURE: {
L0451 int nup, j;
L0452 check(b < pt->sizep);
L0453 nup = pt->p[b]->nups;
L0454 check(pc + nup < pt->sizecode);
L0455 for (j = 1; j <= nup; j++) {
L0456 OpCode op1 = GET_OPCODE(pt->code[pc + j]);
L0457 check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
L0458 }
L0459 if (reg != NO_REG)
L0460 pc += nup;
L0461 break;
L0462 }
L0463 case OP_VARARG: {
L0464 check((pt->is_vararg & VARARG_ISVARARG) &&
L0465 !(pt->is_vararg & VARARG_NEEDSARG));
L0466 b--;
L0467 if (b == LUA_MULTRET) check(checkopenop(pt, pc));
L0468 checkreg(pt, a+b-1);
L0469 break;
L0470 }
L0471 default: break;
L0472 }
L0473 }
L0474 return pt->code[last];
L0475 }
L0476
L0477 #undef check
L0478 #undef checkjump
L0479 #undef checkreg
L0480
L0481
L0482
L0483
L0484 int luaG_checkcode (const Proto *pt) {
L0485 return (symbexec(pt, pt->sizecode, NO_REG) != 0);
L0486 }
L0487
L0488
L0489 static const char *kname (Proto *p, int c) {
L0490 if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
L0491 return svalue(&p->k[INDEXK(c)]);
L0492 else
L0493 return "?";
L0494 }
L0495
L0496
L0497 static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
L0498 const char **name) {
L0499 if (isLua(ci)) {
L0500 Proto *p = ci_func(ci)->l.p;
L0501 int pc = currentpc(L, ci);
L0502 Instruction i;
L0503 *name = luaF_getlocalname(p, stackpos+1, pc);
L0504 if (*name)
L0505 return "local";
L0506 i = symbexec(p, pc, stackpos);
L0507 lua_assert(pc != -1);
L0508 switch (GET_OPCODE(i)) {
L0509 case OP_GETGLOBAL: {
L0510 int g = GETARG_Bx(i);
L0511 lua_assert(ttisstring(&p->k[g]));
L0512 *name = svalue(&p->k[g]);
L0513 return "global";
L0514 }
L0515 case OP_MOVE: {
L0516 int a = GETARG_A(i);
L0517 int b = GETARG_B(i);
L0518 if (b < a)
L0519 return getobjname(L, ci, b, name);
L0520 break;
L0521 }
L0522 case OP_GETTABLE: {
L0523 int k = GETARG_C(i);
L0524 *name = kname(p, k);
L0525 return "field";
L0526 }
L0527 case OP_GETUPVAL: {
L0528 int u = GETARG_B(i);
L0529 *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
L0530 return "upvalue";
L0531 }
L0532 case OP_SELF: {
L0533 int k = GETARG_C(i);
L0534 *name = kname(p, k);
L0535 return "method";
L0536 }
L0537 default: break;
L0538 }
L0539 }
L0540 return NULL;
L0541 }
L0542
L0543
L0544 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
L0545 Instruction i;
L0546 if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
L0547 return NULL;
L0548 ci--;
L0549 i = ci_func(ci)->l.p->code[currentpc(L, ci)];
L0550 if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
L0551 GET_OPCODE(i) == OP_TFORLOOP)
L0552 return getobjname(L, ci, GETARG_A(i), name);
L0553 else
L0554 return NULL;
L0555 }
L0556
L0557
L0558
L0559 static int isinstack (CallInfo *ci, const TValue *o) {
L0560 StkId p;
L0561 for (p = ci->base; p < ci->top; p++)
L0562 if (o == p) return 1;
L0563 return 0;
L0564 }
L0565
L0566
L0567 void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
L0568 const char *name = NULL;
L0569 const char *t = luaT_typenames[ttype(o)];
L0570 const char *kind = (isinstack(L->ci, o)) ?
L0571 getobjname(L, L->ci, cast_int(o - L->base), &name) :
L0572 NULL;
L0573 if (kind)
L0574 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
L0575 op, kind, name, t);
L0576 else
L0577 luaG_runerror(L, "attempt to %s a %s value", op, t);
L0578 }
L0579
L0580
L0581 void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
L0582 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
L0583 lua_assert(!ttisstring(p1) && !ttisnumber(p1));
L0584 luaG_typeerror(L, p1, "concatenate");
L0585 }
L0586
L0587
L0588 void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
L0589 TValue temp;
L0590 if (luaV_tonumber(p1, &temp) == NULL)
L0591 p2 = p1;
L0592 luaG_typeerror(L, p2, "perform arithmetic on");
L0593 }
L0594
L0595
L0596 int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
L0597 const char *t1 = luaT_typenames[ttype(p1)];
L0598 const char *t2 = luaT_typenames[ttype(p2)];
L0599 if (t1[2] == t2[2])
L0600 luaG_runerror(L, "attempt to compare two %s values", t1);
L0601 else
L0602 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
L0603 return 0;
L0604 }
L0605
L0606
L0607 static void addinfo (lua_State *L, const char *msg) {
L0608 CallInfo *ci = L->ci;
L0609 if (isLua(ci)) {
L0610 char buff[LUA_IDSIZE];
L0611 int line = currentline(L, ci);
L0612 luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
L0613 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
L0614 }
L0615 }
L0616
L0617
L0618 void luaG_errormsg (lua_State *L) {
L0619 if (L->errfunc != 0) {
L0620 StkId errfunc = restorestack(L, L->errfunc);
L0621 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
L0622 setobjs2s(L, L->top, L->top - 1);
L0623 setobjs2s(L, L->top - 1, errfunc);
L0624 incr_top(L);
L0625 luaD_call(L, L->top - 2, 1);
L0626 }
L0627 luaD_throw(L, LUA_ERRRUN);
L0628 }
L0629
L0630
L0631 void luaG_runerror (lua_State *L, const char *fmt, ...) {
L0632 va_list argp;
L0633 va_start(argp, fmt);
L0634 addinfo(L, luaO_pushvfstring(L, fmt, argp));
L0635 va_end(argp);
L0636 luaG_errormsg(L);
L0637 }
L0638
Generated by pretty.lua