ldebug.c 24 KB

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