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