summaryrefslogtreecommitdiff
path: root/ldebug.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-11-07 11:25:26 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-11-07 11:25:26 -0200
commitad0704e40cc7b3135fedc6d40a522addb039e090 (patch)
tree4bcd104de4941239e09316efcee5e5e3566b8b81 /ldebug.c
parent5a3f26f85558bedfa439027919d928abfdd00b6d (diff)
downloadlua-ad0704e40cc7b3135fedc6d40a522addb039e090.tar.gz
lua-ad0704e40cc7b3135fedc6d40a522addb039e090.tar.bz2
lua-ad0704e40cc7b3135fedc6d40a522addb039e090.zip
back to 'CallInfo' (no gains with its removal)
Diffstat (limited to 'ldebug.c')
-rw-r--r--ldebug.c161
1 files changed, 71 insertions, 90 deletions
diff --git a/ldebug.c b/ldebug.c
index 100b9b7a..83ddde72 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.138 2017/11/03 19:33:22 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.135 2017/11/02 11:28:56 roberto Exp $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -34,17 +34,17 @@
34#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) 34#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
35 35
36 36
37/* Active Lua function (given stack function) */ 37/* Active Lua function (given call info) */
38#define ci_func(func) (clLvalue(s2v(func))) 38#define ci_func(ci) (clLvalue(s2v((ci)->func)))
39 39
40 40
41static const char *funcnamefromcode (lua_State *L, StkId stkf, 41static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
42 const char **name); 42 const char **name);
43 43
44 44
45static int currentpc (StkId func) { 45static int currentpc (CallInfo *ci) {
46 lua_assert(isLua(func)); 46 lua_assert(isLua(ci));
47 return pcRel(func->stkci.u.l.savedpc, ci_func(func)->p); 47 return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
48} 48}
49 49
50 50
@@ -101,8 +101,8 @@ int luaG_getfuncline (Proto *f, int pc) {
101} 101}
102 102
103 103
104static int currentline (StkId func) { 104static int currentline (CallInfo *ci) {
105 return luaG_getfuncline(ci_func(func)->p, currentpc(func)); 105 return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
106} 106}
107 107
108 108
@@ -120,8 +120,8 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
120 mask = 0; 120 mask = 0;
121 func = NULL; 121 func = NULL;
122 } 122 }
123 if (isLua(L->func)) 123 if (isLua(L->ci))
124 L->oldpc = L->func->stkci.u.l.savedpc; 124 L->oldpc = L->ci->u.l.savedpc;
125 L->hook = func; 125 L->hook = func;
126 L->basehookcount = count; 126 L->basehookcount = count;
127 resethookcount(L); 127 resethookcount(L);
@@ -146,17 +146,14 @@ LUA_API int lua_gethookcount (lua_State *L) {
146 146
147LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 147LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
148 int status; 148 int status;
149 StkId func; 149 CallInfo *ci;
150 if (level < 0) return 0; /* invalid (negative) level */ 150 if (level < 0) return 0; /* invalid (negative) level */
151 lua_lock(L); 151 lua_lock(L);
152 for (func = L->func; 152 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
153 level > 0 && func->stkci.previous != 0;
154 func -= func->stkci.previous)
155 level--; 153 level--;
156 if (level == 0 && func->stkci.previous != 0) { /* level found? */ 154 if (level == 0 && ci != &L->base_ci) { /* level found? */
157 status = 1; 155 status = 1;
158 ar->i_actf = func - L->stack; 156 ar->i_ci = ci;
159 ar->i_actL = L;
160 } 157 }
161 else status = 0; /* no such level */ 158 else status = 0; /* no such level */
162 lua_unlock(L); 159 lua_unlock(L);
@@ -171,34 +168,24 @@ static const char *upvalname (Proto *p, int uv) {
171} 168}
172 169
173 170
174static StkId findcalled (lua_State *L, StkId caller) { 171static const char *findlocal (lua_State *L, CallInfo *ci, int n,
175 StkId func = L->func; 172 StkId *pos) {
176 for (;;) {
177 StkId previous = func - func->stkci.previous;
178 lua_assert(previous < func);
179 if (previous == caller)
180 return func;
181 else
182 func = previous;
183 }
184}
185
186
187static const char *findlocal (lua_State *L, const lua_Debug *ar,
188 int n, StkId *pos) {
189 const char *name = NULL; 173 const char *name = NULL;
190 StkId stkf = ar->i_actL->stack + ar->i_actf; 174 StkId base;
191 if (isLua(stkf)) { 175 if (isLua(ci)) {
192 name = luaF_getlocalname(ci_func(stkf)->p, n, currentpc(stkf)); 176 base = ci->func + 1;
177 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
193 } 178 }
179 else
180 base = ci->func + 1;
194 if (name == NULL) { /* no 'standard' name? */ 181 if (name == NULL) { /* no 'standard' name? */
195 StkId limit = (stkf == L->func) ? L->top : findcalled(L, stkf); 182 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
196 if (limit - stkf > n && n > 0) /* is 'n' inside 'ci' stack? */ 183 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
197 name = "(*temporary)"; /* generic name for any valid slot */ 184 name = "(*temporary)"; /* generic name for any valid slot */
198 else 185 else
199 return NULL; /* no name */ 186 return NULL; /* no name */
200 } 187 }
201 *pos = stkf + n; 188 *pos = base + (n - 1);
202 return name; 189 return name;
203} 190}
204 191
@@ -214,7 +201,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
214 } 201 }
215 else { /* active function; get information through 'ar' */ 202 else { /* active function; get information through 'ar' */
216 StkId pos = NULL; /* to avoid warnings */ 203 StkId pos = NULL; /* to avoid warnings */
217 name = findlocal(L, ar, n, &pos); 204 name = findlocal(L, ar->i_ci, n, &pos);
218 if (name) { 205 if (name) {
219 setobjs2s(L, L->top, pos); 206 setobjs2s(L, L->top, pos);
220 api_incr_top(L); 207 api_incr_top(L);
@@ -229,7 +216,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
229 StkId pos = NULL; /* to avoid warnings */ 216 StkId pos = NULL; /* to avoid warnings */
230 const char *name; 217 const char *name;
231 lua_lock(L); 218 lua_lock(L);
232 name = findlocal(L, ar, n, &pos); 219 name = findlocal(L, ar->i_ci, n, &pos);
233 if (name) { 220 if (name) {
234 setobjs2s(L, pos, L->top - 1); 221 setobjs2s(L, pos, L->top - 1);
235 L->top--; /* pop value */ 222 L->top--; /* pop value */
@@ -287,25 +274,22 @@ static void collectvalidlines (lua_State *L, Closure *f) {
287} 274}
288 275
289 276
290static const char *getfuncname (lua_State *L, StkId stkf, const char **name) { 277static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
291 if (stkf == NULL) /* no function? */ 278 if (ci == NULL) /* no 'ci'? */
292 return NULL; /* no info */ 279 return NULL; /* no info */
293 else if (callstatus(stkf) & CIST_FIN) { /* is this a finalizer? */ 280 else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */
294 *name = "__gc"; 281 *name = "__gc";
295 return "metamethod"; /* report it as such */ 282 return "metamethod"; /* report it as such */
296 } 283 }
297 /* calling function is a known Lua function? */ 284 /* calling function is a known Lua function? */
298 else { 285 else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
299 StkId previous = stkf - stkf->stkci.previous; 286 return funcnamefromcode(L, ci->previous, name);
300 if (!(callstatus(stkf) & CIST_TAIL) && isLua(previous)) 287 else return NULL; /* no way to find a name */
301 return funcnamefromcode(L, previous, name);
302 else return NULL; /* no way to find a name */
303 }
304} 288}
305 289
306 290
307static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, 291static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
308 Closure *f, StkId stkf) { 292 Closure *f, CallInfo *ci) {
309 int status = 1; 293 int status = 1;
310 for (; *what; what++) { 294 for (; *what; what++) {
311 switch (*what) { 295 switch (*what) {
@@ -314,7 +298,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
314 break; 298 break;
315 } 299 }
316 case 'l': { 300 case 'l': {
317 ar->currentline = (stkf && isLua(stkf)) ? currentline(stkf) : -1; 301 ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
318 break; 302 break;
319 } 303 }
320 case 'u': { 304 case 'u': {
@@ -330,11 +314,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
330 break; 314 break;
331 } 315 }
332 case 't': { 316 case 't': {
333 ar->istailcall = (stkf) ? callstatus(stkf) & CIST_TAIL : 0; 317 ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
334 break; 318 break;
335 } 319 }
336 case 'n': { 320 case 'n': {
337 ar->namewhat = getfuncname(L, stkf, &ar->name); 321 ar->namewhat = getfuncname(L, ci, &ar->name);
338 if (ar->namewhat == NULL) { 322 if (ar->namewhat == NULL) {
339 ar->namewhat = ""; /* not found */ 323 ar->namewhat = ""; /* not found */
340 ar->name = NULL; 324 ar->name = NULL;
@@ -354,23 +338,23 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
354LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 338LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
355 int status; 339 int status;
356 Closure *cl; 340 Closure *cl;
357 StkId stkf; 341 CallInfo *ci;
358 TValue *func; 342 TValue *func;
359 lua_lock(L); 343 lua_lock(L);
360 if (*what == '>') { 344 if (*what == '>') {
361 stkf = NULL; 345 ci = NULL;
362 func = s2v(L->top - 1); 346 func = s2v(L->top - 1);
363 api_check(L, ttisfunction(func), "function expected"); 347 api_check(L, ttisfunction(func), "function expected");
364 what++; /* skip the '>' */ 348 what++; /* skip the '>' */
365 L->top--; /* pop function */ 349 L->top--; /* pop function */
366 } 350 }
367 else { 351 else {
368 stkf = ar->i_actL->stack + ar->i_actf; 352 ci = ar->i_ci;
369 func = s2v(stkf); 353 func = s2v(ci->func);
370 lua_assert(ttisfunction(func)); 354 lua_assert(ttisfunction(func));
371 } 355 }
372 cl = ttisclosure(func) ? clvalue(func) : NULL; 356 cl = ttisclosure(func) ? clvalue(func) : NULL;
373 status = auxgetinfo(L, what, ar, cl, stkf); 357 status = auxgetinfo(L, what, ar, cl, ci);
374 if (strchr(what, 'f')) { 358 if (strchr(what, 'f')) {
375 setobj2s(L, L->top, func); 359 setobj2s(L, L->top, func);
376 api_incr_top(L); 360 api_incr_top(L);
@@ -559,13 +543,13 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) {
559** Returns what the name is (e.g., "for iterator", "method", 543** Returns what the name is (e.g., "for iterator", "method",
560** "metamethod") and sets '*name' to point to the name. 544** "metamethod") and sets '*name' to point to the name.
561*/ 545*/
562static const char *funcnamefromcode (lua_State *L, StkId stkf, 546static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
563 const char **name) { 547 const char **name) {
564 TMS tm = (TMS)0; /* (initial value avoids warnings) */ 548 TMS tm = (TMS)0; /* (initial value avoids warnings) */
565 Proto *p = ci_func(stkf)->p; /* calling function */ 549 Proto *p = ci_func(ci)->p; /* calling function */
566 int pc = currentpc(stkf); /* calling instruction index */ 550 int pc = currentpc(ci); /* calling instruction index */
567 Instruction i = p->code[pc]; /* calling instruction */ 551 Instruction i = p->code[pc]; /* calling instruction */
568 if (callstatus(stkf) & CIST_HOOKED) { /* was it called inside a hook? */ 552 if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
569 *name = "?"; 553 *name = "?";
570 return "hook"; 554 return "hook";
571 } 555 }
@@ -621,10 +605,10 @@ static const char *funcnamefromcode (lua_State *L, StkId stkf,
621** not ISO C, but it should not crash a program; the subsequent 605** not ISO C, but it should not crash a program; the subsequent
622** checks are ISO C and ensure a correct result. 606** checks are ISO C and ensure a correct result.
623*/ 607*/
624static int isinstack (lua_State *L, const TValue *o) { 608static int isinstack (CallInfo *ci, const TValue *o) {
625 StkId base = L->stack; 609 StkId base = ci->func + 1;
626 ptrdiff_t i = cast(StkId, o) - base; 610 ptrdiff_t i = cast(StkId, o) - base;
627 return (0 <= i && i < (L->top - base) && s2v(base + i) == o); 611 return (0 <= i && i < (ci->top - base) && s2v(base + i) == o);
628} 612}
629 613
630 614
@@ -633,9 +617,9 @@ static int isinstack (lua_State *L, const TValue *o) {
633** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on 617** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
634** upvalues.) 618** upvalues.)
635*/ 619*/
636static const char *getupvalname (StkId stkf, const TValue *o, 620static const char *getupvalname (CallInfo *ci, const TValue *o,
637 const char **name) { 621 const char **name) {
638 LClosure *c = ci_func(stkf); 622 LClosure *c = ci_func(ci);
639 int i; 623 int i;
640 for (i = 0; i < c->nupvalues; i++) { 624 for (i = 0; i < c->nupvalues; i++) {
641 if (c->upvals[i]->v == o) { 625 if (c->upvals[i]->v == o) {
@@ -649,13 +633,13 @@ static const char *getupvalname (StkId stkf, const TValue *o,
649 633
650static const char *varinfo (lua_State *L, const TValue *o) { 634static const char *varinfo (lua_State *L, const TValue *o) {
651 const char *name = NULL; /* to avoid warnings */ 635 const char *name = NULL; /* to avoid warnings */
652 StkId stkf = L->func; 636 CallInfo *ci = L->ci;
653 const char *kind = NULL; 637 const char *kind = NULL;
654 if (isLua(stkf)) { 638 if (isLua(ci)) {
655 kind = getupvalname(stkf, o, &name); /* check whether 'o' is an upvalue */ 639 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
656 if (!kind && isinstack(L, o)) /* no? try a register */ 640 if (!kind && isinstack(ci, o)) /* no? try a register */
657 kind = getobjname(ci_func(stkf)->p, currentpc(stkf), 641 kind = getobjname(ci_func(ci)->p, currentpc(ci),
658 cast_int(cast(StkId, o) - (stkf + 1)), &name); 642 cast_int(cast(StkId, o) - (ci->func + 1)), &name);
659 } 643 }
660 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; 644 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
661} 645}
@@ -728,16 +712,15 @@ l_noret luaG_errormsg (lua_State *L) {
728 712
729 713
730l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { 714l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
731 StkId func; 715 CallInfo *ci = L->ci;
732 const char *msg; 716 const char *msg;
733 va_list argp; 717 va_list argp;
734 luaC_checkGC(L); /* error message uses memory */ 718 luaC_checkGC(L); /* error message uses memory */
735 va_start(argp, fmt); 719 va_start(argp, fmt);
736 msg = luaO_pushvfstring(L, fmt, argp); /* format message */ 720 msg = luaO_pushvfstring(L, fmt, argp); /* format message */
737 va_end(argp); 721 va_end(argp);
738 func = L->func; /* previous calls can change the stack */ 722 if (isLua(ci)) /* if Lua function, add source:line information */
739 if (isLua(func)) /* if Lua function, add source:line information */ 723 luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
740 luaG_addinfo(L, msg, ci_func(func)->p->source, currentline(func));
741 luaG_errormsg(L); 724 luaG_errormsg(L);
742} 725}
743 726
@@ -756,37 +739,35 @@ static int changedline (Proto *p, int oldpc, int newpc) {
756 739
757 740
758void luaG_traceexec (lua_State *L) { 741void luaG_traceexec (lua_State *L) {
759 StkId func = L->func; 742 CallInfo *ci = L->ci;
760 lu_byte mask = L->hookmask; 743 lu_byte mask = L->hookmask;
761 int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); 744 int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
762 if (counthook) 745 if (counthook)
763 resethookcount(L); /* reset count */ 746 resethookcount(L); /* reset count */
764 else if (!(mask & LUA_MASKLINE)) 747 else if (!(mask & LUA_MASKLINE))
765 return; /* no line hook and count != 0; nothing to be done */ 748 return; /* no line hook and count != 0; nothing to be done */
766 if (callstatus(func) & CIST_HOOKYIELD) { /* called hook last time? */ 749 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
767 callstatus(func) &= ~CIST_HOOKYIELD; /* erase mark */ 750 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
768 return; /* do not call hook again (VM yielded, so it did not move) */ 751 return; /* do not call hook again (VM yielded, so it did not move) */
769 } 752 }
770 if (counthook) 753 if (counthook)
771 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ 754 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
772 if (mask & LUA_MASKLINE) { 755 if (mask & LUA_MASKLINE) {
773 Proto *p = ci_func(func)->p; 756 Proto *p = ci_func(ci)->p;
774 int npc = pcRel(func->stkci.u.l.savedpc, p); 757 int npc = pcRel(ci->u.l.savedpc, p);
775 if (npc == 0 || /* call linehook when enter a new function, */ 758 if (npc == 0 || /* call linehook when enter a new function, */
776 func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */ 759 ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
777 changedline(p, pcRel(L->oldpc, p), npc)) { /* when enter new line */ 760 changedline(p, pcRel(L->oldpc, p), npc)) { /* enter new line */
778 int newline = luaG_getfuncline(p, npc); /* new line */ 761 int newline = luaG_getfuncline(p, npc); /* new line */
779 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ 762 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
780 } 763 }
781 } 764 }
782 func = L->func; /* previous calls can reallocate stack */ 765 L->oldpc = ci->u.l.savedpc;
783 L->oldpc = func->stkci.u.l.savedpc;
784 if (L->status == LUA_YIELD) { /* did hook yield? */ 766 if (L->status == LUA_YIELD) { /* did hook yield? */
785 if (counthook) 767 if (counthook)
786 L->hookcount = 1; /* undo decrement to zero */ 768 L->hookcount = 1; /* undo decrement to zero */
787 /* undo increment (resume will increment it again) */ 769 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
788 func->stkci.u.l.savedpc--; 770 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
789 callstatus(func) |= CIST_HOOKYIELD; /* mark that it yielded */
790 luaD_throw(L, LUA_YIELD); 771 luaD_throw(L, LUA_YIELD);
791 } 772 }
792} 773}