ldebug.c 18 KB

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