diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
| commit | cd2b60b101a398cb9356d746364e70eaed1860f1 (patch) | |
| tree | a1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/ldebug.c | |
| parent | 88c1052e700f38cf3d8ad82d469da4c487760b7e (diff) | |
| download | yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2 yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip | |
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to 'src/lua/ldebug.c')
| -rw-r--r-- | src/lua/ldebug.c | 841 |
1 files changed, 841 insertions, 0 deletions
diff --git a/src/lua/ldebug.c b/src/lua/ldebug.c new file mode 100644 index 0000000..afdc2b7 --- /dev/null +++ b/src/lua/ldebug.c | |||
| @@ -0,0 +1,841 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: ldebug.c $ | ||
| 3 | ** Debug Interface | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #define ldebug_c | ||
| 8 | #define LUA_CORE | ||
| 9 | |||
| 10 | #include "lprefix.h" | ||
| 11 | |||
| 12 | |||
| 13 | #include <stdarg.h> | ||
| 14 | #include <stddef.h> | ||
| 15 | #include <string.h> | ||
| 16 | |||
| 17 | #include "lua.h" | ||
| 18 | |||
| 19 | #include "lapi.h" | ||
| 20 | #include "lcode.h" | ||
| 21 | #include "ldebug.h" | ||
| 22 | #include "ldo.h" | ||
| 23 | #include "lfunc.h" | ||
| 24 | #include "lobject.h" | ||
| 25 | #include "lopcodes.h" | ||
| 26 | #include "lstate.h" | ||
| 27 | #include "lstring.h" | ||
| 28 | #include "ltable.h" | ||
| 29 | #include "ltm.h" | ||
| 30 | #include "lvm.h" | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) | ||
| 35 | |||
| 36 | |||
| 37 | /* Active Lua function (given call info) */ | ||
| 38 | #define ci_func(ci) (clLvalue(s2v((ci)->func))) | ||
| 39 | |||
| 40 | |||
| 41 | static const char *funcnamefromcode (lua_State *L, CallInfo *ci, | ||
| 42 | const char **name); | ||
| 43 | |||
| 44 | |||
| 45 | static int currentpc (CallInfo *ci) { | ||
| 46 | lua_assert(isLua(ci)); | ||
| 47 | return pcRel(ci->u.l.savedpc, ci_func(ci)->p); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | /* | ||
| 52 | ** Get a "base line" to find the line corresponding to an instruction. | ||
| 53 | ** For that, search the array of absolute line info for the largest saved | ||
| 54 | ** instruction smaller or equal to the wanted instruction. A special | ||
| 55 | ** case is when there is no absolute info or the instruction is before | ||
| 56 | ** the first absolute one. | ||
| 57 | */ | ||
| 58 | static int getbaseline (const Proto *f, int pc, int *basepc) { | ||
| 59 | if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { | ||
| 60 | *basepc = -1; /* start from the beginning */ | ||
| 61 | return f->linedefined; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | unsigned int i; | ||
| 65 | if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc) | ||
| 66 | i = f->sizeabslineinfo - 1; /* instruction is after last saved one */ | ||
| 67 | else { /* binary search */ | ||
| 68 | unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */ | ||
| 69 | i = 0; /* abslineinfo[i] <= pc */ | ||
| 70 | while (i < j - 1) { | ||
| 71 | unsigned int m = (j + i) / 2; | ||
| 72 | if (pc >= f->abslineinfo[m].pc) | ||
| 73 | i = m; | ||
| 74 | else | ||
| 75 | j = m; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | *basepc = f->abslineinfo[i].pc; | ||
| 79 | return f->abslineinfo[i].line; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | /* | ||
| 85 | ** Get the line corresponding to instruction 'pc' in function 'f'; | ||
| 86 | ** first gets a base line and from there does the increments until | ||
| 87 | ** the desired instruction. | ||
| 88 | */ | ||
| 89 | int luaG_getfuncline (const Proto *f, int pc) { | ||
| 90 | if (f->lineinfo == NULL) /* no debug information? */ | ||
| 91 | return -1; | ||
| 92 | else { | ||
| 93 | int basepc; | ||
| 94 | int baseline = getbaseline(f, pc, &basepc); | ||
| 95 | while (basepc++ < pc) { /* walk until given instruction */ | ||
| 96 | lua_assert(f->lineinfo[basepc] != ABSLINEINFO); | ||
| 97 | baseline += f->lineinfo[basepc]; /* correct line */ | ||
| 98 | } | ||
| 99 | return baseline; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | static int getcurrentline (CallInfo *ci) { | ||
| 105 | return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | /* | ||
| 110 | ** Set 'trap' for all active Lua frames. | ||
| 111 | ** This function can be called during a signal, under "reasonable" | ||
| 112 | ** assumptions. A new 'ci' is completely linked in the list before it | ||
| 113 | ** becomes part of the "active" list, and we assume that pointers are | ||
| 114 | ** atomic; see comment in next function. | ||
| 115 | ** (A compiler doing interprocedural optimizations could, theoretically, | ||
| 116 | ** reorder memory writes in such a way that the list could be | ||
| 117 | ** temporarily broken while inserting a new element. We simply assume it | ||
| 118 | ** has no good reasons to do that.) | ||
| 119 | */ | ||
| 120 | static void settraps (CallInfo *ci) { | ||
| 121 | for (; ci != NULL; ci = ci->previous) | ||
| 122 | if (isLua(ci)) | ||
| 123 | ci->u.l.trap = 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | /* | ||
| 128 | ** This function can be called during a signal, under "reasonable" | ||
| 129 | ** assumptions. | ||
| 130 | ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by | ||
| 131 | ** 'resethookcount') are for debug only, and it is no problem if they | ||
| 132 | ** get arbitrary values (causes at most one wrong hook call). 'hookmask' | ||
| 133 | ** is an atomic value. We assume that pointers are atomic too (e.g., gcc | ||
| 134 | ** ensures that for all platforms where it runs). Moreover, 'hook' is | ||
| 135 | ** always checked before being called (see 'luaD_hook'). | ||
| 136 | */ | ||
| 137 | LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { | ||
| 138 | if (func == NULL || mask == 0) { /* turn off hooks? */ | ||
| 139 | mask = 0; | ||
| 140 | func = NULL; | ||
| 141 | } | ||
| 142 | if (isLua(L->ci)) | ||
| 143 | L->oldpc = L->ci->u.l.savedpc; | ||
| 144 | L->hook = func; | ||
| 145 | L->basehookcount = count; | ||
| 146 | resethookcount(L); | ||
| 147 | L->hookmask = cast_byte(mask); | ||
| 148 | if (mask) | ||
| 149 | settraps(L->ci); /* to trace inside 'luaV_execute' */ | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | LUA_API lua_Hook lua_gethook (lua_State *L) { | ||
| 154 | return L->hook; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | LUA_API int lua_gethookmask (lua_State *L) { | ||
| 159 | return L->hookmask; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | LUA_API int lua_gethookcount (lua_State *L) { | ||
| 164 | return L->basehookcount; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { | ||
| 169 | int status; | ||
| 170 | CallInfo *ci; | ||
| 171 | if (level < 0) return 0; /* invalid (negative) level */ | ||
| 172 | lua_lock(L); | ||
| 173 | for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) | ||
| 174 | level--; | ||
| 175 | if (level == 0 && ci != &L->base_ci) { /* level found? */ | ||
| 176 | status = 1; | ||
| 177 | ar->i_ci = ci; | ||
| 178 | } | ||
| 179 | else status = 0; /* no such level */ | ||
| 180 | lua_unlock(L); | ||
| 181 | return status; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | static const char *upvalname (const Proto *p, int uv) { | ||
| 186 | TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); | ||
| 187 | if (s == NULL) return "?"; | ||
| 188 | else return getstr(s); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | static const char *findvararg (CallInfo *ci, int n, StkId *pos) { | ||
| 193 | if (clLvalue(s2v(ci->func))->p->is_vararg) { | ||
| 194 | int nextra = ci->u.l.nextraargs; | ||
| 195 | if (n <= nextra) { | ||
| 196 | *pos = ci->func - nextra + (n - 1); | ||
| 197 | return "(vararg)"; /* generic name for any vararg */ | ||
| 198 | } | ||
| 199 | } | ||
| 200 | return NULL; /* no such vararg */ | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { | ||
| 205 | StkId base = ci->func + 1; | ||
| 206 | const char *name = NULL; | ||
| 207 | if (isLua(ci)) { | ||
| 208 | if (n < 0) /* access to vararg values? */ | ||
| 209 | return findvararg(ci, -n, pos); | ||
| 210 | else | ||
| 211 | name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); | ||
| 212 | } | ||
| 213 | if (name == NULL) { /* no 'standard' name? */ | ||
| 214 | StkId limit = (ci == L->ci) ? L->top : ci->next->func; | ||
| 215 | if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ | ||
| 216 | /* generic name for any valid slot */ | ||
| 217 | name = isLua(ci) ? "(temporary)" : "(C temporary)"; | ||
| 218 | } | ||
| 219 | else | ||
| 220 | return NULL; /* no name */ | ||
| 221 | } | ||
| 222 | if (pos) | ||
| 223 | *pos = base + (n - 1); | ||
| 224 | return name; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { | ||
| 229 | const char *name; | ||
| 230 | lua_lock(L); | ||
| 231 | if (ar == NULL) { /* information about non-active function? */ | ||
| 232 | if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ | ||
| 233 | name = NULL; | ||
| 234 | else /* consider live variables at function start (parameters) */ | ||
| 235 | name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); | ||
| 236 | } | ||
| 237 | else { /* active function; get information through 'ar' */ | ||
| 238 | StkId pos = NULL; /* to avoid warnings */ | ||
| 239 | name = luaG_findlocal(L, ar->i_ci, n, &pos); | ||
| 240 | if (name) { | ||
| 241 | setobjs2s(L, L->top, pos); | ||
| 242 | api_incr_top(L); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | lua_unlock(L); | ||
| 246 | return name; | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { | ||
| 251 | StkId pos = NULL; /* to avoid warnings */ | ||
| 252 | const char *name; | ||
| 253 | lua_lock(L); | ||
| 254 | name = luaG_findlocal(L, ar->i_ci, n, &pos); | ||
| 255 | if (name) { | ||
| 256 | setobjs2s(L, pos, L->top - 1); | ||
| 257 | L->top--; /* pop value */ | ||
| 258 | } | ||
| 259 | lua_unlock(L); | ||
| 260 | return name; | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | static void funcinfo (lua_Debug *ar, Closure *cl) { | ||
| 265 | if (noLuaClosure(cl)) { | ||
| 266 | ar->source = "=[C]"; | ||
| 267 | ar->srclen = LL("=[C]"); | ||
| 268 | ar->linedefined = -1; | ||
| 269 | ar->lastlinedefined = -1; | ||
| 270 | ar->what = "C"; | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | const Proto *p = cl->l.p; | ||
| 274 | if (p->source) { | ||
| 275 | ar->source = getstr(p->source); | ||
| 276 | ar->srclen = tsslen(p->source); | ||
| 277 | } | ||
| 278 | else { | ||
| 279 | ar->source = "=?"; | ||
| 280 | ar->srclen = LL("=?"); | ||
| 281 | } | ||
| 282 | ar->linedefined = p->linedefined; | ||
| 283 | ar->lastlinedefined = p->lastlinedefined; | ||
| 284 | ar->what = (ar->linedefined == 0) ? "main" : "Lua"; | ||
| 285 | } | ||
| 286 | luaO_chunkid(ar->short_src, ar->source, ar->srclen); | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | static int nextline (const Proto *p, int currentline, int pc) { | ||
| 291 | if (p->lineinfo[pc] != ABSLINEINFO) | ||
| 292 | return currentline + p->lineinfo[pc]; | ||
| 293 | else | ||
| 294 | return luaG_getfuncline(p, pc); | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | static void collectvalidlines (lua_State *L, Closure *f) { | ||
| 299 | if (noLuaClosure(f)) { | ||
| 300 | setnilvalue(s2v(L->top)); | ||
| 301 | api_incr_top(L); | ||
| 302 | } | ||
| 303 | else { | ||
| 304 | int i; | ||
| 305 | TValue v; | ||
| 306 | const Proto *p = f->l.p; | ||
| 307 | int currentline = p->linedefined; | ||
| 308 | Table *t = luaH_new(L); /* new table to store active lines */ | ||
| 309 | sethvalue2s(L, L->top, t); /* push it on stack */ | ||
| 310 | api_incr_top(L); | ||
| 311 | setbtvalue(&v); /* boolean 'true' to be the value of all indices */ | ||
| 312 | for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ | ||
| 313 | currentline = nextline(p, currentline, i); | ||
| 314 | luaH_setint(L, t, currentline, &v); /* table[line] = true */ | ||
| 315 | } | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { | ||
| 321 | if (ci == NULL) /* no 'ci'? */ | ||
| 322 | return NULL; /* no info */ | ||
| 323 | else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ | ||
| 324 | *name = "__gc"; | ||
| 325 | return "metamethod"; /* report it as such */ | ||
| 326 | } | ||
| 327 | /* calling function is a known Lua function? */ | ||
| 328 | else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) | ||
| 329 | return funcnamefromcode(L, ci->previous, name); | ||
| 330 | else return NULL; /* no way to find a name */ | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, | ||
| 335 | Closure *f, CallInfo *ci) { | ||
| 336 | int status = 1; | ||
| 337 | for (; *what; what++) { | ||
| 338 | switch (*what) { | ||
| 339 | case 'S': { | ||
| 340 | funcinfo(ar, f); | ||
| 341 | break; | ||
| 342 | } | ||
| 343 | case 'l': { | ||
| 344 | ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; | ||
| 345 | break; | ||
| 346 | } | ||
| 347 | case 'u': { | ||
| 348 | ar->nups = (f == NULL) ? 0 : f->c.nupvalues; | ||
| 349 | if (noLuaClosure(f)) { | ||
| 350 | ar->isvararg = 1; | ||
| 351 | ar->nparams = 0; | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | ar->isvararg = f->l.p->is_vararg; | ||
| 355 | ar->nparams = f->l.p->numparams; | ||
| 356 | } | ||
| 357 | break; | ||
| 358 | } | ||
| 359 | case 't': { | ||
| 360 | ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; | ||
| 361 | break; | ||
| 362 | } | ||
| 363 | case 'n': { | ||
| 364 | ar->namewhat = getfuncname(L, ci, &ar->name); | ||
| 365 | if (ar->namewhat == NULL) { | ||
| 366 | ar->namewhat = ""; /* not found */ | ||
| 367 | ar->name = NULL; | ||
| 368 | } | ||
| 369 | break; | ||
| 370 | } | ||
| 371 | case 'r': { | ||
| 372 | if (ci == NULL || !(ci->callstatus & CIST_TRAN)) | ||
| 373 | ar->ftransfer = ar->ntransfer = 0; | ||
| 374 | else { | ||
| 375 | ar->ftransfer = ci->u2.transferinfo.ftransfer; | ||
| 376 | ar->ntransfer = ci->u2.transferinfo.ntransfer; | ||
| 377 | } | ||
| 378 | break; | ||
| 379 | } | ||
| 380 | case 'L': | ||
| 381 | case 'f': /* handled by lua_getinfo */ | ||
| 382 | break; | ||
| 383 | default: status = 0; /* invalid option */ | ||
| 384 | } | ||
| 385 | } | ||
| 386 | return status; | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { | ||
| 391 | int status; | ||
| 392 | Closure *cl; | ||
| 393 | CallInfo *ci; | ||
| 394 | TValue *func; | ||
| 395 | lua_lock(L); | ||
| 396 | if (*what == '>') { | ||
| 397 | ci = NULL; | ||
| 398 | func = s2v(L->top - 1); | ||
| 399 | api_check(L, ttisfunction(func), "function expected"); | ||
| 400 | what++; /* skip the '>' */ | ||
| 401 | L->top--; /* pop function */ | ||
| 402 | } | ||
| 403 | else { | ||
| 404 | ci = ar->i_ci; | ||
| 405 | func = s2v(ci->func); | ||
| 406 | lua_assert(ttisfunction(func)); | ||
| 407 | } | ||
| 408 | cl = ttisclosure(func) ? clvalue(func) : NULL; | ||
| 409 | status = auxgetinfo(L, what, ar, cl, ci); | ||
| 410 | if (strchr(what, 'f')) { | ||
| 411 | setobj2s(L, L->top, func); | ||
| 412 | api_incr_top(L); | ||
| 413 | } | ||
| 414 | if (strchr(what, 'L')) | ||
| 415 | collectvalidlines(L, cl); | ||
| 416 | lua_unlock(L); | ||
| 417 | return status; | ||
| 418 | } | ||
| 419 | |||
| 420 | |||
| 421 | /* | ||
| 422 | ** {====================================================== | ||
| 423 | ** Symbolic Execution | ||
| 424 | ** ======================================================= | ||
| 425 | */ | ||
| 426 | |||
| 427 | static const char *getobjname (const Proto *p, int lastpc, int reg, | ||
| 428 | const char **name); | ||
| 429 | |||
| 430 | |||
| 431 | /* | ||
| 432 | ** Find a "name" for the constant 'c'. | ||
| 433 | */ | ||
| 434 | static void kname (const Proto *p, int c, const char **name) { | ||
| 435 | TValue *kvalue = &p->k[c]; | ||
| 436 | *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | /* | ||
| 441 | ** Find a "name" for the register 'c'. | ||
| 442 | */ | ||
| 443 | static void rname (const Proto *p, int pc, int c, const char **name) { | ||
| 444 | const char *what = getobjname(p, pc, c, name); /* search for 'c' */ | ||
| 445 | if (!(what && *what == 'c')) /* did not find a constant name? */ | ||
| 446 | *name = "?"; | ||
| 447 | } | ||
| 448 | |||
| 449 | |||
| 450 | /* | ||
| 451 | ** Find a "name" for a 'C' value in an RK instruction. | ||
| 452 | */ | ||
| 453 | static void rkname (const Proto *p, int pc, Instruction i, const char **name) { | ||
| 454 | int c = GETARG_C(i); /* key index */ | ||
| 455 | if (GETARG_k(i)) /* is 'c' a constant? */ | ||
| 456 | kname(p, c, name); | ||
| 457 | else /* 'c' is a register */ | ||
| 458 | rname(p, pc, c, name); | ||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | static int filterpc (int pc, int jmptarget) { | ||
| 463 | if (pc < jmptarget) /* is code conditional (inside a jump)? */ | ||
| 464 | return -1; /* cannot know who sets that register */ | ||
| 465 | else return pc; /* current position sets that register */ | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | /* | ||
| 470 | ** Try to find last instruction before 'lastpc' that modified register 'reg'. | ||
| 471 | */ | ||
| 472 | static int findsetreg (const Proto *p, int lastpc, int reg) { | ||
| 473 | int pc; | ||
| 474 | int setreg = -1; /* keep last instruction that changed 'reg' */ | ||
| 475 | int jmptarget = 0; /* any code before this address is conditional */ | ||
| 476 | if (testMMMode(GET_OPCODE(p->code[lastpc]))) | ||
| 477 | lastpc--; /* previous instruction was not actually executed */ | ||
| 478 | for (pc = 0; pc < lastpc; pc++) { | ||
| 479 | Instruction i = p->code[pc]; | ||
| 480 | OpCode op = GET_OPCODE(i); | ||
| 481 | int a = GETARG_A(i); | ||
| 482 | int change; /* true if current instruction changed 'reg' */ | ||
| 483 | switch (op) { | ||
| 484 | case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */ | ||
| 485 | int b = GETARG_B(i); | ||
| 486 | change = (a <= reg && reg <= a + b); | ||
| 487 | break; | ||
| 488 | } | ||
| 489 | case OP_TFORCALL: { /* affect all regs above its base */ | ||
| 490 | change = (reg >= a + 2); | ||
| 491 | break; | ||
| 492 | } | ||
| 493 | case OP_CALL: | ||
| 494 | case OP_TAILCALL: { /* affect all registers above base */ | ||
| 495 | change = (reg >= a); | ||
| 496 | break; | ||
| 497 | } | ||
| 498 | case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */ | ||
| 499 | int b = GETARG_sJ(i); | ||
| 500 | int dest = pc + 1 + b; | ||
| 501 | /* jump does not skip 'lastpc' and is larger than current one? */ | ||
| 502 | if (dest <= lastpc && dest > jmptarget) | ||
| 503 | jmptarget = dest; /* update 'jmptarget' */ | ||
| 504 | change = 0; | ||
| 505 | break; | ||
| 506 | } | ||
| 507 | default: /* any instruction that sets A */ | ||
| 508 | change = (testAMode(op) && reg == a); | ||
| 509 | break; | ||
| 510 | } | ||
| 511 | if (change) | ||
| 512 | setreg = filterpc(pc, jmptarget); | ||
| 513 | } | ||
| 514 | return setreg; | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | /* | ||
| 519 | ** Check whether table being indexed by instruction 'i' is the | ||
| 520 | ** environment '_ENV' | ||
| 521 | */ | ||
| 522 | static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { | ||
| 523 | int t = GETARG_B(i); /* table index */ | ||
| 524 | const char *name; /* name of indexed variable */ | ||
| 525 | if (isup) /* is an upvalue? */ | ||
| 526 | name = upvalname(p, t); | ||
| 527 | else | ||
| 528 | getobjname(p, pc, t, &name); | ||
| 529 | return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; | ||
| 530 | } | ||
| 531 | |||
| 532 | |||
| 533 | static const char *getobjname (const Proto *p, int lastpc, int reg, | ||
| 534 | const char **name) { | ||
| 535 | int pc; | ||
| 536 | *name = luaF_getlocalname(p, reg + 1, lastpc); | ||
| 537 | if (*name) /* is a local? */ | ||
| 538 | return "local"; | ||
| 539 | /* else try symbolic execution */ | ||
| 540 | pc = findsetreg(p, lastpc, reg); | ||
| 541 | if (pc != -1) { /* could find instruction? */ | ||
| 542 | Instruction i = p->code[pc]; | ||
| 543 | OpCode op = GET_OPCODE(i); | ||
| 544 | switch (op) { | ||
| 545 | case OP_MOVE: { | ||
| 546 | int b = GETARG_B(i); /* move from 'b' to 'a' */ | ||
| 547 | if (b < GETARG_A(i)) | ||
| 548 | return getobjname(p, pc, b, name); /* get name for 'b' */ | ||
| 549 | break; | ||
| 550 | } | ||
| 551 | case OP_GETTABUP: { | ||
| 552 | int k = GETARG_C(i); /* key index */ | ||
| 553 | kname(p, k, name); | ||
| 554 | return gxf(p, pc, i, 1); | ||
| 555 | } | ||
| 556 | case OP_GETTABLE: { | ||
| 557 | int k = GETARG_C(i); /* key index */ | ||
| 558 | rname(p, pc, k, name); | ||
| 559 | return gxf(p, pc, i, 0); | ||
| 560 | } | ||
| 561 | case OP_GETI: { | ||
| 562 | *name = "integer index"; | ||
| 563 | return "field"; | ||
| 564 | } | ||
| 565 | case OP_GETFIELD: { | ||
| 566 | int k = GETARG_C(i); /* key index */ | ||
| 567 | kname(p, k, name); | ||
| 568 | return gxf(p, pc, i, 0); | ||
| 569 | } | ||
| 570 | case OP_GETUPVAL: { | ||
| 571 | *name = upvalname(p, GETARG_B(i)); | ||
| 572 | return "upvalue"; | ||
| 573 | } | ||
| 574 | case OP_LOADK: | ||
| 575 | case OP_LOADKX: { | ||
| 576 | int b = (op == OP_LOADK) ? GETARG_Bx(i) | ||
| 577 | : GETARG_Ax(p->code[pc + 1]); | ||
| 578 | if (ttisstring(&p->k[b])) { | ||
| 579 | *name = svalue(&p->k[b]); | ||
| 580 | return "constant"; | ||
| 581 | } | ||
| 582 | break; | ||
| 583 | } | ||
| 584 | case OP_SELF: { | ||
| 585 | rkname(p, pc, i, name); | ||
| 586 | return "method"; | ||
| 587 | } | ||
| 588 | default: break; /* go through to return NULL */ | ||
| 589 | } | ||
| 590 | } | ||
| 591 | return NULL; /* could not find reasonable name */ | ||
| 592 | } | ||
| 593 | |||
| 594 | |||
| 595 | /* | ||
| 596 | ** Try to find a name for a function based on the code that called it. | ||
| 597 | ** (Only works when function was called by a Lua function.) | ||
| 598 | ** Returns what the name is (e.g., "for iterator", "method", | ||
| 599 | ** "metamethod") and sets '*name' to point to the name. | ||
| 600 | */ | ||
| 601 | static const char *funcnamefromcode (lua_State *L, CallInfo *ci, | ||
| 602 | const char **name) { | ||
| 603 | TMS tm = (TMS)0; /* (initial value avoids warnings) */ | ||
| 604 | const Proto *p = ci_func(ci)->p; /* calling function */ | ||
| 605 | int pc = currentpc(ci); /* calling instruction index */ | ||
| 606 | Instruction i = p->code[pc]; /* calling instruction */ | ||
| 607 | if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ | ||
| 608 | *name = "?"; | ||
| 609 | return "hook"; | ||
| 610 | } | ||
| 611 | switch (GET_OPCODE(i)) { | ||
| 612 | case OP_CALL: | ||
| 613 | case OP_TAILCALL: | ||
| 614 | return getobjname(p, pc, GETARG_A(i), name); /* get function name */ | ||
| 615 | case OP_TFORCALL: { /* for iterator */ | ||
| 616 | *name = "for iterator"; | ||
| 617 | return "for iterator"; | ||
| 618 | } | ||
| 619 | /* other instructions can do calls through metamethods */ | ||
| 620 | case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: | ||
| 621 | case OP_GETI: case OP_GETFIELD: | ||
| 622 | tm = TM_INDEX; | ||
| 623 | break; | ||
| 624 | case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: | ||
| 625 | tm = TM_NEWINDEX; | ||
| 626 | break; | ||
| 627 | case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { | ||
| 628 | tm = cast(TMS, GETARG_C(i)); | ||
| 629 | break; | ||
| 630 | } | ||
| 631 | case OP_UNM: tm = TM_UNM; break; | ||
| 632 | case OP_BNOT: tm = TM_BNOT; break; | ||
| 633 | case OP_LEN: tm = TM_LEN; break; | ||
| 634 | case OP_CONCAT: tm = TM_CONCAT; break; | ||
| 635 | case OP_EQ: tm = TM_EQ; break; | ||
| 636 | case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: | ||
| 637 | *name = "order"; /* '<=' can call '__lt', etc. */ | ||
| 638 | return "metamethod"; | ||
| 639 | case OP_CLOSE: case OP_RETURN: | ||
| 640 | *name = "close"; | ||
| 641 | return "metamethod"; | ||
| 642 | default: | ||
| 643 | return NULL; /* cannot find a reasonable name */ | ||
| 644 | } | ||
| 645 | *name = getstr(G(L)->tmname[tm]) + 2; | ||
| 646 | return "metamethod"; | ||
| 647 | } | ||
| 648 | |||
| 649 | /* }====================================================== */ | ||
| 650 | |||
| 651 | |||
| 652 | |||
| 653 | /* | ||
| 654 | ** The subtraction of two potentially unrelated pointers is | ||
| 655 | ** not ISO C, but it should not crash a program; the subsequent | ||
| 656 | ** checks are ISO C and ensure a correct result. | ||
| 657 | */ | ||
| 658 | static int isinstack (CallInfo *ci, const TValue *o) { | ||
| 659 | StkId base = ci->func + 1; | ||
| 660 | ptrdiff_t i = cast(StkId, o) - base; | ||
| 661 | return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); | ||
| 662 | } | ||
| 663 | |||
| 664 | |||
| 665 | /* | ||
| 666 | ** Checks whether value 'o' came from an upvalue. (That can only happen | ||
| 667 | ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on | ||
| 668 | ** upvalues.) | ||
| 669 | */ | ||
| 670 | static const char *getupvalname (CallInfo *ci, const TValue *o, | ||
| 671 | const char **name) { | ||
| 672 | LClosure *c = ci_func(ci); | ||
| 673 | int i; | ||
| 674 | for (i = 0; i < c->nupvalues; i++) { | ||
| 675 | if (c->upvals[i]->v == o) { | ||
| 676 | *name = upvalname(c->p, i); | ||
| 677 | return "upvalue"; | ||
| 678 | } | ||
| 679 | } | ||
| 680 | return NULL; | ||
| 681 | } | ||
| 682 | |||
| 683 | |||
| 684 | static const char *varinfo (lua_State *L, const TValue *o) { | ||
| 685 | const char *name = NULL; /* to avoid warnings */ | ||
| 686 | CallInfo *ci = L->ci; | ||
| 687 | const char *kind = NULL; | ||
| 688 | if (isLua(ci)) { | ||
| 689 | kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ | ||
| 690 | if (!kind && isinstack(ci, o)) /* no? try a register */ | ||
| 691 | kind = getobjname(ci_func(ci)->p, currentpc(ci), | ||
| 692 | cast_int(cast(StkId, o) - (ci->func + 1)), &name); | ||
| 693 | } | ||
| 694 | return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; | ||
| 695 | } | ||
| 696 | |||
| 697 | |||
| 698 | l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { | ||
| 699 | const char *t = luaT_objtypename(L, o); | ||
| 700 | luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { | ||
| 705 | luaG_runerror(L, "bad 'for' %s (number expected, got %s)", | ||
| 706 | what, luaT_objtypename(L, o)); | ||
| 707 | } | ||
| 708 | |||
| 709 | |||
| 710 | l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { | ||
| 711 | if (ttisstring(p1) || cvt2str(p1)) p1 = p2; | ||
| 712 | luaG_typeerror(L, p1, "concatenate"); | ||
| 713 | } | ||
| 714 | |||
| 715 | |||
| 716 | l_noret luaG_opinterror (lua_State *L, const TValue *p1, | ||
| 717 | const TValue *p2, const char *msg) { | ||
| 718 | if (!ttisnumber(p1)) /* first operand is wrong? */ | ||
| 719 | p2 = p1; /* now second is wrong */ | ||
| 720 | luaG_typeerror(L, p2, msg); | ||
| 721 | } | ||
| 722 | |||
| 723 | |||
| 724 | /* | ||
| 725 | ** Error when both values are convertible to numbers, but not to integers | ||
| 726 | */ | ||
| 727 | l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { | ||
| 728 | lua_Integer temp; | ||
| 729 | if (!tointegerns(p1, &temp)) | ||
| 730 | p2 = p1; | ||
| 731 | luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); | ||
| 732 | } | ||
| 733 | |||
| 734 | |||
| 735 | l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { | ||
| 736 | const char *t1 = luaT_objtypename(L, p1); | ||
| 737 | const char *t2 = luaT_objtypename(L, p2); | ||
| 738 | if (strcmp(t1, t2) == 0) | ||
| 739 | luaG_runerror(L, "attempt to compare two %s values", t1); | ||
| 740 | else | ||
| 741 | luaG_runerror(L, "attempt to compare %s with %s", t1, t2); | ||
| 742 | } | ||
| 743 | |||
| 744 | |||
| 745 | /* add src:line information to 'msg' */ | ||
| 746 | const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, | ||
| 747 | int line) { | ||
| 748 | char buff[LUA_IDSIZE]; | ||
| 749 | if (src) | ||
| 750 | luaO_chunkid(buff, getstr(src), tsslen(src)); | ||
| 751 | else { /* no source available; use "?" instead */ | ||
| 752 | buff[0] = '?'; buff[1] = '\0'; | ||
| 753 | } | ||
| 754 | return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); | ||
| 755 | } | ||
| 756 | |||
| 757 | |||
| 758 | l_noret luaG_errormsg (lua_State *L) { | ||
| 759 | if (L->errfunc != 0) { /* is there an error handling function? */ | ||
| 760 | StkId errfunc = restorestack(L, L->errfunc); | ||
| 761 | lua_assert(ttisfunction(s2v(errfunc))); | ||
| 762 | setobjs2s(L, L->top, L->top - 1); /* move argument */ | ||
| 763 | setobjs2s(L, L->top - 1, errfunc); /* push function */ | ||
| 764 | L->top++; /* assume EXTRA_STACK */ | ||
| 765 | luaD_callnoyield(L, L->top - 2, 1); /* call it */ | ||
| 766 | } | ||
| 767 | luaD_throw(L, LUA_ERRRUN); | ||
| 768 | } | ||
| 769 | |||
| 770 | |||
| 771 | l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { | ||
| 772 | CallInfo *ci = L->ci; | ||
| 773 | const char *msg; | ||
| 774 | va_list argp; | ||
| 775 | luaC_checkGC(L); /* error message uses memory */ | ||
| 776 | va_start(argp, fmt); | ||
| 777 | msg = luaO_pushvfstring(L, fmt, argp); /* format message */ | ||
| 778 | va_end(argp); | ||
| 779 | if (isLua(ci)) /* if Lua function, add source:line information */ | ||
| 780 | luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); | ||
| 781 | luaG_errormsg(L); | ||
| 782 | } | ||
| 783 | |||
| 784 | |||
| 785 | /* | ||
| 786 | ** Check whether new instruction 'newpc' is in a different line from | ||
| 787 | ** previous instruction 'oldpc'. | ||
| 788 | */ | ||
| 789 | static int changedline (const Proto *p, int oldpc, int newpc) { | ||
| 790 | while (oldpc++ < newpc) { | ||
| 791 | if (p->lineinfo[oldpc] != 0) | ||
| 792 | return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); | ||
| 793 | } | ||
| 794 | return 0; /* no line changes in the way */ | ||
| 795 | } | ||
| 796 | |||
| 797 | |||
| 798 | int luaG_traceexec (lua_State *L, const Instruction *pc) { | ||
| 799 | CallInfo *ci = L->ci; | ||
| 800 | lu_byte mask = L->hookmask; | ||
| 801 | int counthook; | ||
| 802 | if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ | ||
| 803 | ci->u.l.trap = 0; /* don't need to stop again */ | ||
| 804 | return 0; /* turn off 'trap' */ | ||
| 805 | } | ||
| 806 | pc++; /* reference is always next instruction */ | ||
| 807 | ci->u.l.savedpc = pc; /* save 'pc' */ | ||
| 808 | counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); | ||
| 809 | if (counthook) | ||
| 810 | resethookcount(L); /* reset count */ | ||
| 811 | else if (!(mask & LUA_MASKLINE)) | ||
| 812 | return 1; /* no line hook and count != 0; nothing to be done now */ | ||
| 813 | if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ | ||
| 814 | ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ | ||
| 815 | return 1; /* do not call hook again (VM yielded, so it did not move) */ | ||
| 816 | } | ||
| 817 | if (!isIT(*(ci->u.l.savedpc - 1))) | ||
| 818 | L->top = ci->top; /* prepare top */ | ||
| 819 | if (counthook) | ||
| 820 | luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ | ||
| 821 | if (mask & LUA_MASKLINE) { | ||
| 822 | const Proto *p = ci_func(ci)->p; | ||
| 823 | int npci = pcRel(pc, p); | ||
| 824 | if (npci == 0 || /* call linehook when enter a new function, */ | ||
| 825 | pc <= L->oldpc || /* when jump back (loop), or when */ | ||
| 826 | changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ | ||
| 827 | int newline = luaG_getfuncline(p, npci); | ||
| 828 | luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ | ||
| 829 | } | ||
| 830 | L->oldpc = pc; /* 'pc' of last call to line hook */ | ||
| 831 | } | ||
| 832 | if (L->status == LUA_YIELD) { /* did hook yield? */ | ||
| 833 | if (counthook) | ||
| 834 | L->hookcount = 1; /* undo decrement to zero */ | ||
| 835 | ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ | ||
| 836 | ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ | ||
| 837 | luaD_throw(L, LUA_YIELD); | ||
| 838 | } | ||
| 839 | return 1; /* keep 'trap' on */ | ||
| 840 | } | ||
| 841 | |||
