ldebug.c 19 KB

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