ldebug.c 27 KB

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