summaryrefslogtreecommitdiff
path: root/ldebug.c
diff options
context:
space:
mode:
Diffstat (limited to 'ldebug.c')
-rw-r--r--ldebug.c174
1 files changed, 75 insertions, 99 deletions
diff --git a/ldebug.c b/ldebug.c
index ea11cefd..c3bf8756 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 1.72 2001/03/06 14:46:54 roberto Exp roberto $ 2** $Id: ldebug.c,v 1.73 2001/03/07 13:22:55 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*/
@@ -24,19 +24,14 @@
24 24
25 25
26 26
27static const l_char *getfuncname (lua_State *L, StkId f, const l_char **name); 27static const l_char *getfuncname (lua_State *L, CallInfo *ci,
28 const l_char **name);
28 29
29 30
30static void setnormalized (TObject *d, const TObject *s) {
31 if (ttype(s) == LUA_TMARK) {
32 setclvalue(d, infovalue(s)->func);
33 }
34 else setobj(d, s);
35}
36
37 31
38static int isLmark (StkId o) { 32static int isLmark (CallInfo *ci) {
39 return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC); 33 lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION);
34 return (ci && ci->prev && !ci_func(ci)->isC);
40} 35}
41 36
42 37
@@ -60,27 +55,25 @@ LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
60} 55}
61 56
62 57
63static StkId aux_stackedfunction (lua_State *L, int level, StkId top) { 58static CallInfo *ci_stack (lua_State *L, StkId obj) {
64 int i; 59 CallInfo *ci = L->ci;
65 for (i = (top-1) - L->stack; i>=0; i--) { 60 while (ci->base > obj) ci = ci->prev;
66 if (is_T_MARK(&L->stack[i])) { 61 return (ci != &L->basefunc) ? ci : NULL;
67 if (level == 0)
68 return L->stack+i;
69 level--;
70 }
71 }
72 return NULL;
73} 62}
74 63
75 64
76LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { 65LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
77 StkId f; 66 CallInfo *ci;
78 int status; 67 int status;
79 lua_lock(L); 68 lua_lock(L);
80 f = aux_stackedfunction(L, level, L->top); 69 ci = L->ci;
81 if (f == NULL) status = 0; /* there is no such level */ 70 while (level-- && ci != &L->basefunc) {
71 lua_assert(ci->base > ci->prev->base);
72 ci = ci->prev;
73 }
74 if (ci == &L->basefunc) status = 0; /* there is no such level */
82 else { 75 else {
83 ar->_func = f; 76 ar->_ci = ci;
84 status = 1; 77 status = 1;
85 } 78 }
86 lua_unlock(L); 79 lua_unlock(L);
@@ -88,18 +81,6 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
88} 81}
89 82
90 83
91static int nups (StkId f) {
92 switch (ttype(f)) {
93 case LUA_TFUNCTION:
94 return clvalue(f)->nupvalues;
95 case LUA_TMARK:
96 return infovalue(f)->func->nupvalues;
97 default:
98 return 0;
99 }
100}
101
102
103int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { 84int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
104 int refi; 85 int refi;
105 if (lineinfo == NULL || pc == -1) 86 if (lineinfo == NULL || pc == -1)
@@ -131,45 +112,43 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
131} 112}
132 113
133 114
134static int currentpc (StkId f) { 115static int currentpc (CallInfo *ci) {
135 CallInfo *ci = infovalue(f); 116 lua_assert(isLmark(ci));
136 lua_assert(isLmark(f));
137 if (ci->pc) 117 if (ci->pc)
138 return (*ci->pc - ci->func->f.l->code) - 1; 118 return (*ci->pc - ci_func(ci)->f.l->code) - 1;
139 else 119 else
140 return -1; /* function is not active */ 120 return -1; /* function is not active */
141} 121}
142 122
143 123
144static int currentline (StkId f) { 124static int currentline (CallInfo *ci) {
145 if (!isLmark(f)) 125 if (!isLmark(ci))
146 return -1; /* only active lua functions have current-line information */ 126 return -1; /* only active lua functions have current-line information */
147 else { 127 else {
148 CallInfo *ci = infovalue(f); 128 int *lineinfo = ci_func(ci)->f.l->lineinfo;
149 int *lineinfo = ci->func->f.l->lineinfo; 129 return luaG_getline(lineinfo, currentpc(ci), 1, NULL);
150 return luaG_getline(lineinfo, currentpc(f), 1, NULL);
151 } 130 }
152} 131}
153 132
154 133
155 134
156static Proto *getluaproto (StkId f) { 135static Proto *getluaproto (CallInfo *ci) {
157 return (isLmark(f) ? infovalue(f)->func->f.l : NULL); 136 return (isLmark(ci) ? ci_func(ci)->f.l : NULL);
158} 137}
159 138
160 139
161LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 140LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
162 const l_char *name; 141 const l_char *name;
163 StkId f; 142 CallInfo *ci;
164 Proto *fp; 143 Proto *fp;
165 lua_lock(L); 144 lua_lock(L);
166 name = NULL; 145 name = NULL;
167 f = ar->_func; 146 ci = ar->_ci;
168 fp = getluaproto(f); 147 fp = getluaproto(ci);
169 if (fp) { /* `f' is a Lua function? */ 148 if (fp) { /* is a Lua function? */
170 name = luaF_getlocalname(fp, n, currentpc(f)); 149 name = luaF_getlocalname(fp, n, currentpc(ci));
171 if (name) 150 if (name)
172 luaA_pushobject(L, (f+1)+(n-1)); /* push value */ 151 luaA_pushobject(L, ci->base+(n-1)); /* push value */
173 } 152 }
174 lua_unlock(L); 153 lua_unlock(L);
175 return name; 154 return name;
@@ -178,19 +157,19 @@ LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
178 157
179LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 158LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
180 const l_char *name; 159 const l_char *name;
181 StkId f; 160 CallInfo *ci;
182 Proto *fp; 161 Proto *fp;
183 lua_lock(L); 162 lua_lock(L);
184 name = NULL; 163 name = NULL;
185 f = ar->_func; 164 ci = ar->_ci;
186 fp = getluaproto(f); 165 fp = getluaproto(ci);
187 L->top--; /* pop new value */ 166 L->top--; /* pop new value */
188 if (fp) { /* `f' is a Lua function? */ 167 if (fp) { /* is a Lua function? */
189 name = luaF_getlocalname(fp, n, currentpc(f)); 168 name = luaF_getlocalname(fp, n, currentpc(ci));
190 if (!name || name[0] == l_c('(')) /* `(' starts private locals */ 169 if (!name || name[0] == l_c('(')) /* `(' starts private locals */
191 name = NULL; 170 name = NULL;
192 else 171 else
193 setobj((f+1)+(n-1), L->top); 172 setobj(ci->base+(n-1), L->top);
194 } 173 }
195 lua_unlock(L); 174 lua_unlock(L);
196 return name; 175 return name;
@@ -205,16 +184,12 @@ static void infoLproto (lua_Debug *ar, Proto *f) {
205 184
206 185
207static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) { 186static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
208 Closure *cl = NULL; 187 Closure *cl;
209 switch (ttype(func)) { 188 if (ttype(func) == LUA_TFUNCTION)
210 case LUA_TFUNCTION: 189 cl = clvalue(func);
211 cl = clvalue(func); 190 else {
212 break; 191 luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
213 case LUA_TMARK: 192 cl = NULL; /* to avoid warnings */
214 cl = infovalue(func)->func;
215 break;
216 default:
217 luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
218 } 193 }
219 if (cl->isC) { 194 if (cl->isC) {
220 ar->source = l_s("=C"); 195 ar->source = l_s("=C");
@@ -255,60 +230,60 @@ static const l_char *travglobals (lua_State *L, const TObject *o) {
255} 230}
256 231
257 232
258static void getname (lua_State *L, StkId f, lua_Debug *ar) { 233static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
259 TObject o;
260 setnormalized(&o, f);
261 /* try to find a name for given function */ 234 /* try to find a name for given function */
262 if ((ar->name = travglobals(L, &o)) != NULL) 235 if ((ar->name = travglobals(L, f)) != NULL)
263 ar->namewhat = l_s("global"); 236 ar->namewhat = l_s("global");
264 /* not found: try tag methods */ 237 /* not found: try tag methods */
265 else if ((ar->name = travtagmethods(G(L), &o)) != NULL) 238 else if ((ar->name = travtagmethods(G(L), f)) != NULL)
266 ar->namewhat = l_s("tag-method"); 239 ar->namewhat = l_s("tag-method");
267 else ar->namewhat = l_s(""); /* not found at all */ 240 else ar->namewhat = l_s(""); /* not found at all */
268} 241}
269 242
270 243
271LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) { 244LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) {
272 StkId func; 245 StkId f;
273 int isactive; 246 CallInfo *ci;
274 int status = 1; 247 int status = 1;
275 lua_lock(L); 248 lua_lock(L);
276 isactive = (*what != l_c('>')); 249 if (*what != l_c('>')) { /* function is active? */
277 if (isactive) 250 ci = ar->_ci;
278 func = ar->_func; 251 f = ci->base - 1;
252 }
279 else { 253 else {
280 what++; /* skip the `>' */ 254 what++; /* skip the `>' */
281 func = L->top - 1; 255 ci = NULL;
256 f = L->top - 1;
282 } 257 }
283 for (; *what; what++) { 258 for (; *what; what++) {
284 switch (*what) { 259 switch (*what) {
285 case l_c('S'): { 260 case l_c('S'): {
286 funcinfo(L, ar, func); 261 funcinfo(L, ar, f);
287 break; 262 break;
288 } 263 }
289 case l_c('l'): { 264 case l_c('l'): {
290 ar->currentline = currentline(func); 265 ar->currentline = currentline(ci);
291 break; 266 break;
292 } 267 }
293 case l_c('u'): { 268 case l_c('u'): {
294 ar->nups = nups(func); 269 ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->nupvalues : 0;
295 break; 270 break;
296 } 271 }
297 case l_c('n'): { 272 case l_c('n'): {
298 ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL; 273 ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
299 if (ar->namewhat == NULL) 274 if (ar->namewhat == NULL)
300 getname(L, func, ar); 275 getname(L, f, ar);
301 break; 276 break;
302 } 277 }
303 case l_c('f'): { 278 case l_c('f'): {
304 setnormalized(L->top, func); 279 setobj(L->top, f);
305 incr_top; /* push function */ 280 incr_top; /* push function */
306 break; 281 break;
307 } 282 }
308 default: status = 0; /* invalid option */ 283 default: status = 0; /* invalid option */
309 } 284 }
310 } 285 }
311 if (!isactive) L->top--; /* pop function */ 286 if (!ci) L->top--; /* pop function */
312 lua_unlock(L); 287 lua_unlock(L);
313 return status; 288 return status;
314} 289}
@@ -539,13 +514,13 @@ int luaG_checkcode (lua_State *L, const Proto *pt) {
539 514
540 515
541static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) { 516static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
542 StkId func = aux_stackedfunction(L, 0, obj); 517 CallInfo *ci = ci_stack(L, obj);
543 if (!isLmark(func)) 518 if (!isLmark(ci))
544 return NULL; /* not an active Lua function */ 519 return NULL; /* not an active Lua function */
545 else { 520 else {
546 Proto *p = infovalue(func)->func->f.l; 521 Proto *p = ci_func(ci)->f.l;
547 int pc = currentpc(func); 522 int pc = currentpc(ci);
548 int stackpos = obj - (func+1); /* func+1 == function base */ 523 int stackpos = obj - ci->base;
549 Instruction i = luaG_symbexec(L, p, pc, stackpos); 524 Instruction i = luaG_symbexec(L, p, pc, stackpos);
550 lua_assert(pc != -1); 525 lua_assert(pc != -1);
551 switch (GET_OPCODE(i)) { 526 switch (GET_OPCODE(i)) {
@@ -570,18 +545,19 @@ static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
570} 545}
571 546
572 547
573static const l_char *getfuncname (lua_State *L, StkId f, const l_char **name) { 548static const l_char *getfuncname (lua_State *L, CallInfo *ci,
574 StkId func = aux_stackedfunction(L, 0, f); /* calling function */ 549 const l_char **name) {
575 if (!isLmark(func)) 550 ci = ci->prev; /* calling function */
551 if (ci == &L->basefunc || !isLmark(ci))
576 return NULL; /* not an active Lua function */ 552 return NULL; /* not an active Lua function */
577 else { 553 else {
578 Proto *p = infovalue(func)->func->f.l; 554 Proto *p = ci_func(ci)->f.l;
579 int pc = currentpc(func); 555 int pc = currentpc(ci);
580 Instruction i; 556 Instruction i;
581 if (pc == -1) return NULL; /* function is not activated */ 557 if (pc == -1) return NULL; /* function is not activated */
582 i = p->code[pc]; 558 i = p->code[pc];
583 return (GET_OPCODE(i) == OP_CALL 559 return (GET_OPCODE(i) == OP_CALL
584 ? getobjname(L, (func+1)+GETARG_A(i), name) 560 ? getobjname(L, ci->base+GETARG_A(i), name)
585 : NULL); /* no useful name found */ 561 : NULL); /* no useful name found */
586 } 562 }
587} 563}