ldebug.c 24 KB

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