aboutsummaryrefslogtreecommitdiff
path: root/ldebug.c
diff options
context:
space:
mode:
Diffstat (limited to 'ldebug.c')
-rw-r--r--ldebug.c116
1 files changed, 64 insertions, 52 deletions
diff --git a/ldebug.c b/ldebug.c
index b212d875..553af071 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.136 2017/11/03 12:12:30 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.137 2017/11/03 17:22:54 roberto Exp roberto $
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 call info) */ 37/* Active Lua function (given stack function) */
38#define ci_func(ci) (clLvalue(s2v((ci)->func))) 38#define ci_func(func) (clLvalue(s2v(func)))
39 39
40 40
41static const char *funcnamefromcode (lua_State *L, CallInfo *ci, 41static const char *funcnamefromcode (lua_State *L, StkId stkf,
42 const char **name); 42 const char **name);
43 43
44 44
45static int currentpc (CallInfo *ci) { 45static int currentpc (StkId func) {
46 lua_assert(isLua(ci->func)); 46 lua_assert(isLua(func));
47 return pcRel(ci->func->stkci.u.l.savedpc, ci_func(ci)->p); 47 return pcRel(func->stkci.u.l.savedpc, ci_func(func)->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 (CallInfo *ci) { 104static int currentline (StkId func) {
105 return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); 105 return luaG_getfuncline(ci_func(func)->p, currentpc(func));
106} 106}
107 107
108 108
@@ -168,24 +168,33 @@ static const char *upvalname (Proto *p, int uv) {
168} 168}
169 169
170 170
171static const char *findlocal (lua_State *L, CallInfo *ci, int n, 171static StkId findcalled (lua_State *L, StkId caller) {
172 StkId func = L->func;
173 for (;;) {
174 StkId previous = func - func->stkci.previous;
175 lua_assert(previous < func);
176 if (previous == caller)
177 return func;
178 else
179 func = previous;
180 }
181}
182
183
184static const char *findlocal (lua_State *L, StkId stkf, int n,
172 StkId *pos) { 185 StkId *pos) {
173 const char *name = NULL; 186 const char *name = NULL;
174 StkId base; 187 if (isLua(stkf)) {
175 if (isLua(ci->func)) { 188 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));
178 } 189 }
179 else
180 base = ci->func + 1;
181 if (name == NULL) { /* no 'standard' name? */ 190 if (name == NULL) { /* no 'standard' name? */
182 StkId limit = (ci == L->ci) ? L->top : ci->next->func; 191 StkId limit = (stkf == L->func) ? L->top : findcalled(L, stkf);
183 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 192 if (limit - stkf > n && n > 0) /* is 'n' inside 'ci' stack? */
184 name = "(*temporary)"; /* generic name for any valid slot */ 193 name = "(*temporary)"; /* generic name for any valid slot */
185 else 194 else
186 return NULL; /* no name */ 195 return NULL; /* no name */
187 } 196 }
188 *pos = base + (n - 1); 197 *pos = stkf + n;
189 return name; 198 return name;
190} 199}
191 200
@@ -201,7 +210,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
201 } 210 }
202 else { /* active function; get information through 'ar' */ 211 else { /* active function; get information through 'ar' */
203 StkId pos = NULL; /* to avoid warnings */ 212 StkId pos = NULL; /* to avoid warnings */
204 name = findlocal(L, ar->i_ci, n, &pos); 213 name = findlocal(L, ar->i_ci->func, n, &pos);
205 if (name) { 214 if (name) {
206 setobjs2s(L, L->top, pos); 215 setobjs2s(L, L->top, pos);
207 api_incr_top(L); 216 api_incr_top(L);
@@ -216,7 +225,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
216 StkId pos = NULL; /* to avoid warnings */ 225 StkId pos = NULL; /* to avoid warnings */
217 const char *name; 226 const char *name;
218 lua_lock(L); 227 lua_lock(L);
219 name = findlocal(L, ar->i_ci, n, &pos); 228 name = findlocal(L, ar->i_ci->func, n, &pos);
220 if (name) { 229 if (name) {
221 setobjs2s(L, pos, L->top - 1); 230 setobjs2s(L, pos, L->top - 1);
222 L->top--; /* pop value */ 231 L->top--; /* pop value */
@@ -274,22 +283,25 @@ static void collectvalidlines (lua_State *L, Closure *f) {
274} 283}
275 284
276 285
277static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { 286static const char *getfuncname (lua_State *L, StkId stkf, const char **name) {
278 if (ci == NULL) /* no 'ci'? */ 287 if (stkf == NULL) /* no function? */
279 return NULL; /* no info */ 288 return NULL; /* no info */
280 else if (callstatus(ci->func) & CIST_FIN) { /* is this a finalizer? */ 289 else if (callstatus(stkf) & CIST_FIN) { /* is this a finalizer? */
281 *name = "__gc"; 290 *name = "__gc";
282 return "metamethod"; /* report it as such */ 291 return "metamethod"; /* report it as such */
283 } 292 }
284 /* calling function is a known Lua function? */ 293 /* calling function is a known Lua function? */
285 else if (!(callstatus(ci->func) & CIST_TAIL) && isLua(ci->previous->func)) 294 else {
286 return funcnamefromcode(L, ci->previous, name); 295 StkId previous = stkf - stkf->stkci.previous;
287 else return NULL; /* no way to find a name */ 296 if (!(callstatus(stkf) & CIST_TAIL) && isLua(previous))
297 return funcnamefromcode(L, previous, name);
298 else return NULL; /* no way to find a name */
299 }
288} 300}
289 301
290 302
291static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, 303static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
292 Closure *f, CallInfo *ci) { 304 Closure *f, StkId stkf) {
293 int status = 1; 305 int status = 1;
294 for (; *what; what++) { 306 for (; *what; what++) {
295 switch (*what) { 307 switch (*what) {
@@ -298,7 +310,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
298 break; 310 break;
299 } 311 }
300 case 'l': { 312 case 'l': {
301 ar->currentline = (ci && isLua(ci->func)) ? currentline(ci) : -1; 313 ar->currentline = (stkf && isLua(stkf)) ? currentline(stkf) : -1;
302 break; 314 break;
303 } 315 }
304 case 'u': { 316 case 'u': {
@@ -314,11 +326,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
314 break; 326 break;
315 } 327 }
316 case 't': { 328 case 't': {
317 ar->istailcall = (ci) ? callstatus(ci->func) & CIST_TAIL : 0; 329 ar->istailcall = (stkf) ? callstatus(stkf) & CIST_TAIL : 0;
318 break; 330 break;
319 } 331 }
320 case 'n': { 332 case 'n': {
321 ar->namewhat = getfuncname(L, ci, &ar->name); 333 ar->namewhat = getfuncname(L, stkf, &ar->name);
322 if (ar->namewhat == NULL) { 334 if (ar->namewhat == NULL) {
323 ar->namewhat = ""; /* not found */ 335 ar->namewhat = ""; /* not found */
324 ar->name = NULL; 336 ar->name = NULL;
@@ -338,23 +350,23 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
338LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { 350LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
339 int status; 351 int status;
340 Closure *cl; 352 Closure *cl;
341 CallInfo *ci; 353 StkId stkf;
342 TValue *func; 354 TValue *func;
343 lua_lock(L); 355 lua_lock(L);
344 if (*what == '>') { 356 if (*what == '>') {
345 ci = NULL; 357 stkf = NULL;
346 func = s2v(L->top - 1); 358 func = s2v(L->top - 1);
347 api_check(L, ttisfunction(func), "function expected"); 359 api_check(L, ttisfunction(func), "function expected");
348 what++; /* skip the '>' */ 360 what++; /* skip the '>' */
349 L->top--; /* pop function */ 361 L->top--; /* pop function */
350 } 362 }
351 else { 363 else {
352 ci = ar->i_ci; 364 stkf = ar->i_ci->func;
353 func = s2v(ci->func); 365 func = s2v(stkf);
354 lua_assert(ttisfunction(func)); 366 lua_assert(ttisfunction(func));
355 } 367 }
356 cl = ttisclosure(func) ? clvalue(func) : NULL; 368 cl = ttisclosure(func) ? clvalue(func) : NULL;
357 status = auxgetinfo(L, what, ar, cl, ci); 369 status = auxgetinfo(L, what, ar, cl, stkf);
358 if (strchr(what, 'f')) { 370 if (strchr(what, 'f')) {
359 setobj2s(L, L->top, func); 371 setobj2s(L, L->top, func);
360 api_incr_top(L); 372 api_incr_top(L);
@@ -543,13 +555,13 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) {
543** Returns what the name is (e.g., "for iterator", "method", 555** Returns what the name is (e.g., "for iterator", "method",
544** "metamethod") and sets '*name' to point to the name. 556** "metamethod") and sets '*name' to point to the name.
545*/ 557*/
546static const char *funcnamefromcode (lua_State *L, CallInfo *ci, 558static const char *funcnamefromcode (lua_State *L, StkId stkf,
547 const char **name) { 559 const char **name) {
548 TMS tm = (TMS)0; /* (initial value avoids warnings) */ 560 TMS tm = (TMS)0; /* (initial value avoids warnings) */
549 Proto *p = ci_func(ci)->p; /* calling function */ 561 Proto *p = ci_func(stkf)->p; /* calling function */
550 int pc = currentpc(ci); /* calling instruction index */ 562 int pc = currentpc(stkf); /* calling instruction index */
551 Instruction i = p->code[pc]; /* calling instruction */ 563 Instruction i = p->code[pc]; /* calling instruction */
552 if (callstatus(ci->func) & CIST_HOOKED) { /* was it called inside a hook? */ 564 if (callstatus(stkf) & CIST_HOOKED) { /* was it called inside a hook? */
553 *name = "?"; 565 *name = "?";
554 return "hook"; 566 return "hook";
555 } 567 }
@@ -617,9 +629,9 @@ static int isinstack (lua_State *L, const TValue *o) {
617** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on 629** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
618** upvalues.) 630** upvalues.)
619*/ 631*/
620static const char *getupvalname (CallInfo *ci, const TValue *o, 632static const char *getupvalname (StkId stkf, const TValue *o,
621 const char **name) { 633 const char **name) {
622 LClosure *c = ci_func(ci); 634 LClosure *c = ci_func(stkf);
623 int i; 635 int i;
624 for (i = 0; i < c->nupvalues; i++) { 636 for (i = 0; i < c->nupvalues; i++) {
625 if (c->upvals[i]->v == o) { 637 if (c->upvals[i]->v == o) {
@@ -633,13 +645,13 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
633 645
634static const char *varinfo (lua_State *L, const TValue *o) { 646static const char *varinfo (lua_State *L, const TValue *o) {
635 const char *name = NULL; /* to avoid warnings */ 647 const char *name = NULL; /* to avoid warnings */
636 CallInfo *ci = L->ci; 648 StkId stkf = L->func;
637 const char *kind = NULL; 649 const char *kind = NULL;
638 if (isLua(L->func)) { 650 if (isLua(stkf)) {
639 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ 651 kind = getupvalname(stkf, o, &name); /* check whether 'o' is an upvalue */
640 if (!kind && isinstack(L, o)) /* no? try a register */ 652 if (!kind && isinstack(L, o)) /* no? try a register */
641 kind = getobjname(ci_func(ci)->p, currentpc(ci), 653 kind = getobjname(ci_func(stkf)->p, currentpc(stkf),
642 cast_int(cast(StkId, o) - (L->func + 1)), &name); 654 cast_int(cast(StkId, o) - (stkf + 1)), &name);
643 } 655 }
644 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; 656 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
645} 657}
@@ -712,15 +724,16 @@ l_noret luaG_errormsg (lua_State *L) {
712 724
713 725
714l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { 726l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
715 CallInfo *ci = L->ci; 727 StkId func;
716 const char *msg; 728 const char *msg;
717 va_list argp; 729 va_list argp;
718 luaC_checkGC(L); /* error message uses memory */ 730 luaC_checkGC(L); /* error message uses memory */
719 va_start(argp, fmt); 731 va_start(argp, fmt);
720 msg = luaO_pushvfstring(L, fmt, argp); /* format message */ 732 msg = luaO_pushvfstring(L, fmt, argp); /* format message */
721 va_end(argp); 733 va_end(argp);
722 if (isLua(L->func)) /* if Lua function, add source:line information */ 734 func = L->func; /* previous calls can change the stack */
723 luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); 735 if (isLua(func)) /* if Lua function, add source:line information */
736 luaG_addinfo(L, msg, ci_func(func)->p->source, currentline(func));
724 luaG_errormsg(L); 737 luaG_errormsg(L);
725} 738}
726 739
@@ -739,7 +752,6 @@ static int changedline (Proto *p, int oldpc, int newpc) {
739 752
740 753
741void luaG_traceexec (lua_State *L) { 754void luaG_traceexec (lua_State *L) {
742 CallInfo *ci = L->ci;
743 StkId func = L->func; 755 StkId func = L->func;
744 lu_byte mask = L->hookmask; 756 lu_byte mask = L->hookmask;
745 int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); 757 int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
@@ -754,7 +766,7 @@ void luaG_traceexec (lua_State *L) {
754 if (counthook) 766 if (counthook)
755 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ 767 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
756 if (mask & LUA_MASKLINE) { 768 if (mask & LUA_MASKLINE) {
757 Proto *p = ci_func(ci)->p; 769 Proto *p = ci_func(func)->p;
758 int npc = pcRel(func->stkci.u.l.savedpc, p); 770 int npc = pcRel(func->stkci.u.l.savedpc, p);
759 if (npc == 0 || /* call linehook when enter a new function, */ 771 if (npc == 0 || /* call linehook when enter a new function, */
760 func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */ 772 func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */