ldebug.c 29 KB

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