ldebug.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. ** $Id: ldebug.c,v 2.119 2016/02/26 19:20:15 roberto Exp roberto $
  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((ci)->func))
  28. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  29. static int currentpc (CallInfo *ci) {
  30. lua_assert(isLua(ci));
  31. return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
  32. }
  33. static int currentline (CallInfo *ci) {
  34. return getfuncline(ci_func(ci)->p, currentpc(ci));
  35. }
  36. /*
  37. ** If function yielded, its 'func' can be in the 'extra' field. The
  38. ** next function restores 'func' to its correct value for debugging
  39. ** purposes. (It exchanges 'func' and 'extra'; so, when called again,
  40. ** after debugging, it also "re-restores" ** 'func' to its altered value.
  41. */
  42. static void swapextra (lua_State *L) {
  43. if (L->status == LUA_YIELD) {
  44. CallInfo *ci = L->ci; /* get function that yielded */
  45. StkId temp = ci->func; /* exchange its 'func' and 'extra' values */
  46. ci->func = restorestack(L, ci->extra);
  47. ci->extra = savestack(L, temp);
  48. }
  49. }
  50. /*
  51. ** This function can be called asynchronously (e.g. during a signal).
  52. ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
  53. ** 'resethookcount') are for debug only, and it is no problem if they
  54. ** get arbitrary values (causes at most one wrong hook call). 'hookmask'
  55. ** is an atomic value. We assume that pointers are atomic too (e.g., gcc
  56. ** ensures that for all platforms where it runs). Moreover, 'hook' is
  57. ** always checked before being called (see 'luaD_hook').
  58. */
  59. LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  60. if (func == NULL || mask == 0) { /* turn off hooks? */
  61. mask = 0;
  62. func = NULL;
  63. }
  64. if (isLua(L->ci))
  65. L->oldpc = L->ci->u.l.savedpc;
  66. L->hook = func;
  67. L->basehookcount = count;
  68. resethookcount(L);
  69. L->hookmask = cast_byte(mask);
  70. }
  71. LUA_API lua_Hook lua_gethook (lua_State *L) {
  72. return L->hook;
  73. }
  74. LUA_API int lua_gethookmask (lua_State *L) {
  75. return L->hookmask;
  76. }
  77. LUA_API int lua_gethookcount (lua_State *L) {
  78. return L->basehookcount;
  79. }
  80. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  81. int status;
  82. CallInfo *ci;
  83. if (level < 0) return 0; /* invalid (negative) level */
  84. lua_lock(L);
  85. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
  86. level--;
  87. if (level == 0 && ci != &L->base_ci) { /* level found? */
  88. status = 1;
  89. ar->i_ci = ci;
  90. }
  91. else status = 0; /* no such level */
  92. lua_unlock(L);
  93. return status;
  94. }
  95. static const char *upvalname (Proto *p, int uv) {
  96. TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
  97. if (s == NULL) return "?";
  98. else return getstr(s);
  99. }
  100. static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
  101. int nparams = clLvalue(ci->func)->p->numparams;
  102. if (n >= cast_int(ci->u.l.base - ci->func) - nparams)
  103. return NULL; /* no such vararg */
  104. else {
  105. *pos = ci->func + nparams + n;
  106. return "(*vararg)"; /* generic name for any vararg */
  107. }
  108. }
  109. static const char *findlocal (lua_State *L, CallInfo *ci, int n,
  110. StkId *pos) {
  111. const char *name = NULL;
  112. StkId base;
  113. if (isLua(ci)) {
  114. if (n < 0) /* access to vararg values? */
  115. return findvararg(ci, -n, pos);
  116. else {
  117. base = ci->u.l.base;
  118. name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
  119. }
  120. }
  121. else
  122. base = ci->func + 1;
  123. if (name == NULL) { /* no 'standard' name? */
  124. StkId limit = (ci == L->ci) ? L->top : ci->next->func;
  125. if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
  126. name = "(*temporary)"; /* generic name for any valid slot */
  127. else
  128. return NULL; /* no name */
  129. }
  130. *pos = base + (n - 1);
  131. return name;
  132. }
  133. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  134. const char *name;
  135. lua_lock(L);
  136. swapextra(L);
  137. if (ar == NULL) { /* information about non-active function? */
  138. if (!isLfunction(L->top - 1)) /* not a Lua function? */
  139. name = NULL;
  140. else /* consider live variables at function start (parameters) */
  141. name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
  142. }
  143. else { /* active function; get information through 'ar' */
  144. StkId pos = NULL; /* to avoid warnings */
  145. name = findlocal(L, ar->i_ci, n, &pos);
  146. if (name) {
  147. setobj2s(L, L->top, pos);
  148. api_incr_top(L);
  149. }
  150. }
  151. swapextra(L);
  152. lua_unlock(L);
  153. return name;
  154. }
  155. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  156. StkId pos = NULL; /* to avoid warnings */
  157. const char *name;
  158. lua_lock(L);
  159. swapextra(L);
  160. name = findlocal(L, ar->i_ci, n, &pos);
  161. if (name) {
  162. setobjs2s(L, pos, L->top - 1);
  163. L->top--; /* pop value */
  164. }
  165. swapextra(L);
  166. lua_unlock(L);
  167. return name;
  168. }
  169. static void funcinfo (lua_Debug *ar, Closure *cl) {
  170. if (noLuaClosure(cl)) {
  171. ar->source = "=[C]";
  172. ar->linedefined = -1;
  173. ar->lastlinedefined = -1;
  174. ar->what = "C";
  175. }
  176. else {
  177. Proto *p = cl->l.p;
  178. ar->source = p->source ? getstr(p->source) : "=?";
  179. ar->linedefined = p->linedefined;
  180. ar->lastlinedefined = p->lastlinedefined;
  181. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  182. }
  183. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  184. }
  185. static void collectvalidlines (lua_State *L, Closure *f) {
  186. if (noLuaClosure(f)) {
  187. setnilvalue(L->top);
  188. api_incr_top(L);
  189. }
  190. else {
  191. int i;
  192. TValue v;
  193. int *lineinfo = f->l.p->lineinfo;
  194. Table *t = luaH_new(L); /* new table to store active lines */
  195. sethvalue(L, L->top, t); /* push it on stack */
  196. api_incr_top(L);
  197. setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
  198. for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
  199. luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
  200. }
  201. }
  202. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  203. Closure *f, CallInfo *ci) {
  204. int status = 1;
  205. for (; *what; what++) {
  206. switch (*what) {
  207. case 'S': {
  208. funcinfo(ar, f);
  209. break;
  210. }
  211. case 'l': {
  212. ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
  213. break;
  214. }
  215. case 'u': {
  216. ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
  217. if (noLuaClosure(f)) {
  218. ar->isvararg = 1;
  219. ar->nparams = 0;
  220. }
  221. else {
  222. ar->isvararg = f->l.p->is_vararg;
  223. ar->nparams = f->l.p->numparams;
  224. }
  225. break;
  226. }
  227. case 't': {
  228. ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
  229. break;
  230. }
  231. case 'n': {
  232. /* calling function is a known Lua function? */
  233. if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
  234. ar->namewhat = getfuncname(L, ci->previous, &ar->name);
  235. else
  236. ar->namewhat = NULL;
  237. if (ar->namewhat == NULL) {
  238. ar->namewhat = ""; /* not found */
  239. ar->name = NULL;
  240. }
  241. break;
  242. }
  243. case 'L':
  244. case 'f': /* handled by lua_getinfo */
  245. break;
  246. default: status = 0; /* invalid option */
  247. }
  248. }
  249. return status;
  250. }
  251. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  252. int status;
  253. Closure *cl;
  254. CallInfo *ci;
  255. StkId func;
  256. lua_lock(L);
  257. swapextra(L);
  258. if (*what == '>') {
  259. ci = NULL;
  260. func = L->top - 1;
  261. api_check(L, ttisfunction(func), "function expected");
  262. what++; /* skip the '>' */
  263. L->top--; /* pop function */
  264. }
  265. else {
  266. ci = ar->i_ci;
  267. func = ci->func;
  268. lua_assert(ttisfunction(ci->func));
  269. }
  270. cl = ttisclosure(func) ? clvalue(func) : NULL;
  271. status = auxgetinfo(L, what, ar, cl, ci);
  272. if (strchr(what, 'f')) {
  273. setobjs2s(L, L->top, func);
  274. api_incr_top(L);
  275. }
  276. swapextra(L); /* correct before option 'L', which can raise a mem. error */
  277. if (strchr(what, 'L'))
  278. collectvalidlines(L, cl);
  279. lua_unlock(L);
  280. return status;
  281. }
  282. /*
  283. ** {======================================================
  284. ** Symbolic Execution
  285. ** =======================================================
  286. */
  287. static const char *getobjname (Proto *p, int lastpc, int reg,
  288. const char **name);
  289. /*
  290. ** find a "name" for the RK value 'c'
  291. */
  292. static void kname (Proto *p, int pc, int c, const char **name) {
  293. if (ISK(c)) { /* is 'c' a constant? */
  294. TValue *kvalue = &p->k[INDEXK(c)];
  295. if (ttisstring(kvalue)) { /* literal constant? */
  296. *name = svalue(kvalue); /* it is its own name */
  297. return;
  298. }
  299. /* else no reasonable name found */
  300. }
  301. else { /* 'c' is a register */
  302. const char *what = getobjname(p, pc, c, name); /* search for 'c' */
  303. if (what && *what == 'c') { /* found a constant name? */
  304. return; /* 'name' already filled */
  305. }
  306. /* else no reasonable name found */
  307. }
  308. *name = "?"; /* no reasonable name found */
  309. }
  310. static int filterpc (int pc, int jmptarget) {
  311. if (pc < jmptarget) /* is code conditional (inside a jump)? */
  312. return -1; /* cannot know who sets that register */
  313. else return pc; /* current position sets that register */
  314. }
  315. /*
  316. ** try to find last instruction before 'lastpc' that modified register 'reg'
  317. */
  318. static int findsetreg (Proto *p, int lastpc, int reg) {
  319. int pc;
  320. int setreg = -1; /* keep last instruction that changed 'reg' */
  321. int jmptarget = 0; /* any code before this address is conditional */
  322. for (pc = 0; pc < lastpc; pc++) {
  323. Instruction i = p->code[pc];
  324. OpCode op = GET_OPCODE(i);
  325. int a = GETARG_A(i);
  326. switch (op) {
  327. case OP_LOADNIL: {
  328. int b = GETARG_B(i);
  329. if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
  330. setreg = filterpc(pc, jmptarget);
  331. break;
  332. }
  333. case OP_TFORCALL: {
  334. if (reg >= a + 2) /* affect all regs above its base */
  335. setreg = filterpc(pc, jmptarget);
  336. break;
  337. }
  338. case OP_CALL:
  339. case OP_TAILCALL: {
  340. if (reg >= a) /* affect all registers above base */
  341. setreg = filterpc(pc, jmptarget);
  342. break;
  343. }
  344. case OP_JMP: {
  345. int b = GETARG_sBx(i);
  346. int dest = pc + 1 + b;
  347. /* jump is forward and do not skip 'lastpc'? */
  348. if (pc < dest && dest <= lastpc) {
  349. if (dest > jmptarget)
  350. jmptarget = dest; /* update 'jmptarget' */
  351. }
  352. break;
  353. }
  354. default:
  355. if (testAMode(op) && reg == a) /* any instruction that set A */
  356. setreg = filterpc(pc, jmptarget);
  357. break;
  358. }
  359. }
  360. return setreg;
  361. }
  362. static const char *getobjname (Proto *p, int lastpc, int reg,
  363. const char **name) {
  364. int pc;
  365. *name = luaF_getlocalname(p, reg + 1, lastpc);
  366. if (*name) /* is a local? */
  367. return "local";
  368. /* else try symbolic execution */
  369. pc = findsetreg(p, lastpc, reg);
  370. if (pc != -1) { /* could find instruction? */
  371. Instruction i = p->code[pc];
  372. OpCode op = GET_OPCODE(i);
  373. switch (op) {
  374. case OP_MOVE: {
  375. int b = GETARG_B(i); /* move from 'b' to 'a' */
  376. if (b < GETARG_A(i))
  377. return getobjname(p, pc, b, name); /* get name for 'b' */
  378. break;
  379. }
  380. case OP_GETTABUP:
  381. case OP_GETTABLE: {
  382. int k = GETARG_C(i); /* key index */
  383. int t = GETARG_B(i); /* table index */
  384. const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
  385. ? luaF_getlocalname(p, t + 1, pc)
  386. : upvalname(p, t);
  387. kname(p, pc, k, name);
  388. return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
  389. }
  390. case OP_GETUPVAL: {
  391. *name = upvalname(p, GETARG_B(i));
  392. return "upvalue";
  393. }
  394. case OP_LOADK:
  395. case OP_LOADKX: {
  396. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  397. : GETARG_Ax(p->code[pc + 1]);
  398. if (ttisstring(&p->k[b])) {
  399. *name = svalue(&p->k[b]);
  400. return "constant";
  401. }
  402. break;
  403. }
  404. case OP_SELF: {
  405. int k = GETARG_C(i); /* key index */
  406. kname(p, pc, k, name);
  407. return "method";
  408. }
  409. default: break; /* go through to return NULL */
  410. }
  411. }
  412. return NULL; /* could not find reasonable name */
  413. }
  414. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  415. TMS tm = (TMS)0; /* to avoid warnings */
  416. Proto *p = ci_func(ci)->p; /* calling function */
  417. int pc = currentpc(ci); /* calling instruction index */
  418. Instruction i = p->code[pc]; /* calling instruction */
  419. if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
  420. *name = "?";
  421. return "hook";
  422. }
  423. switch (GET_OPCODE(i)) {
  424. case OP_CALL:
  425. case OP_TAILCALL: /* get function name */
  426. return getobjname(p, pc, GETARG_A(i), name);
  427. case OP_TFORCALL: { /* for iterator */
  428. *name = "for iterator";
  429. return "for iterator";
  430. }
  431. /* all other instructions can call only through metamethods */
  432. case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
  433. tm = TM_INDEX;
  434. break;
  435. case OP_SETTABUP: case OP_SETTABLE:
  436. tm = TM_NEWINDEX;
  437. break;
  438. case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
  439. case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
  440. case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
  441. int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */
  442. tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */
  443. break;
  444. }
  445. case OP_UNM: tm = TM_UNM; break;
  446. case OP_BNOT: tm = TM_BNOT; break;
  447. case OP_LEN: tm = TM_LEN; break;
  448. case OP_CONCAT: tm = TM_CONCAT; break;
  449. case OP_EQ: tm = TM_EQ; break;
  450. case OP_LT: tm = TM_LT; break;
  451. case OP_LE: tm = TM_LE; break;
  452. default: lua_assert(0); /* other instructions cannot call a function */
  453. }
  454. *name = getstr(G(L)->tmname[tm]);
  455. return "metamethod";
  456. }
  457. /* }====================================================== */
  458. /*
  459. ** The subtraction of two potentially unrelated pointers is
  460. ** not ISO C, but it should not crash a program; the subsequent
  461. ** checks are ISO C and ensure a correct result.
  462. */
  463. static int isinstack (CallInfo *ci, const TValue *o) {
  464. ptrdiff_t i = o - ci->u.l.base;
  465. return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o);
  466. }
  467. /*
  468. ** Checks whether value 'o' came from an upvalue. (That can only happen
  469. ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
  470. ** upvalues.)
  471. */
  472. static const char *getupvalname (CallInfo *ci, const TValue *o,
  473. const char **name) {
  474. LClosure *c = ci_func(ci);
  475. int i;
  476. for (i = 0; i < c->nupvalues; i++) {
  477. if (c->upvals[i]->v == o) {
  478. *name = upvalname(c->p, i);
  479. return "upvalue";
  480. }
  481. }
  482. return NULL;
  483. }
  484. static const char *varinfo (lua_State *L, const TValue *o) {
  485. const char *name = NULL; /* to avoid warnings */
  486. CallInfo *ci = L->ci;
  487. const char *kind = NULL;
  488. if (isLua(ci)) {
  489. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  490. if (!kind && isinstack(ci, o)) /* no? try a register */
  491. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  492. cast_int(o - ci->u.l.base), &name);
  493. }
  494. return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
  495. }
  496. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  497. const char *t = luaT_objtypename(L, o);
  498. luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
  499. }
  500. l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
  501. if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
  502. luaG_typeerror(L, p1, "concatenate");
  503. }
  504. l_noret luaG_opinterror (lua_State *L, const TValue *p1,
  505. const TValue *p2, const char *msg) {
  506. lua_Number temp;
  507. if (!tonumber(p1, &temp)) /* first operand is wrong? */
  508. p2 = p1; /* now second is wrong */
  509. luaG_typeerror(L, p2, msg);
  510. }
  511. /*
  512. ** Error when both values are convertible to numbers, but not to integers
  513. */
  514. l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
  515. lua_Integer temp;
  516. if (!tointeger(p1, &temp))
  517. p2 = p1;
  518. luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
  519. }
  520. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  521. const char *t1 = luaT_objtypename(L, p1);
  522. const char *t2 = luaT_objtypename(L, p2);
  523. if (strcmp(t1, t2) == 0)
  524. luaG_runerror(L, "attempt to compare two %s values", t1);
  525. else
  526. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  527. }
  528. /* add src:line information to 'msg' */
  529. const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
  530. int line) {
  531. char buff[LUA_IDSIZE];
  532. if (src)
  533. luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
  534. else { /* no source available; use "?" instead */
  535. buff[0] = '?'; buff[1] = '\0';
  536. }
  537. return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  538. }
  539. l_noret luaG_errormsg (lua_State *L) {
  540. if (L->errfunc != 0) { /* is there an error handling function? */
  541. StkId errfunc = restorestack(L, L->errfunc);
  542. setobjs2s(L, L->top, L->top - 1); /* move argument */
  543. setobjs2s(L, L->top - 1, errfunc); /* push function */
  544. L->top++; /* assume EXTRA_STACK */
  545. luaD_callnoyield(L, L->top - 2, 1); /* call it */
  546. }
  547. luaD_throw(L, LUA_ERRRUN);
  548. }
  549. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  550. CallInfo *ci = L->ci;
  551. const char *msg;
  552. va_list argp;
  553. va_start(argp, fmt);
  554. msg = luaO_pushvfstring(L, fmt, argp); /* format message */
  555. va_end(argp);
  556. if (isLua(ci)) /* if Lua function, add source:line information */
  557. luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
  558. luaG_errormsg(L);
  559. }
  560. void luaG_traceexec (lua_State *L) {
  561. CallInfo *ci = L->ci;
  562. lu_byte mask = L->hookmask;
  563. int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
  564. if (counthook)
  565. resethookcount(L); /* reset count */
  566. else if (!(mask & LUA_MASKLINE))
  567. return; /* no line hook and count != 0; nothing to be done */
  568. if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
  569. ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
  570. return; /* do not call hook again (VM yielded, so it did not move) */
  571. }
  572. if (counthook)
  573. luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
  574. if (mask & LUA_MASKLINE) {
  575. Proto *p = ci_func(ci)->p;
  576. int npc = pcRel(ci->u.l.savedpc, p);
  577. int newline = getfuncline(p, npc);
  578. if (npc == 0 || /* call linehook when enter a new function, */
  579. ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
  580. newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
  581. luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
  582. }
  583. L->oldpc = ci->u.l.savedpc;
  584. if (L->status == LUA_YIELD) { /* did hook yield? */
  585. if (counthook)
  586. L->hookcount = 1; /* undo decrement to zero */
  587. ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
  588. ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
  589. ci->func = L->top - 1; /* protect stack below results */
  590. luaD_throw(L, LUA_YIELD);
  591. }
  592. }