aboutsummaryrefslogtreecommitdiff
path: root/src/lua/ldebug.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lua/ldebug.c (renamed from src/lua-5.3/ldebug.c)428
1 files changed, 285 insertions, 143 deletions
diff --git a/src/lua-5.3/ldebug.c b/src/lua/ldebug.c
index e138929..afdc2b7 100644
--- a/src/lua-5.3/ldebug.c
+++ b/src/lua/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.121.1.2 2017/07/10 17:21:50 roberto Exp $ 2** $Id: ldebug.c $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -31,11 +31,11 @@
31 31
32 32
33 33
34#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) 34#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL)
35 35
36 36
37/* Active Lua function (given call info) */ 37/* Active Lua function (given call info) */
38#define ci_func(ci) (clLvalue((ci)->func)) 38#define ci_func(ci) (clLvalue(s2v((ci)->func)))
39 39
40 40
41static const char *funcnamefromcode (lua_State *L, CallInfo *ci, 41static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
@@ -48,29 +48,85 @@ static int currentpc (CallInfo *ci) {
48} 48}
49 49
50 50
51static int currentline (CallInfo *ci) { 51/*
52 return getfuncline(ci_func(ci)->p, currentpc(ci)); 52** Get a "base line" to find the line corresponding to an instruction.
53** For that, search the array of absolute line info for the largest saved
54** instruction smaller or equal to the wanted instruction. A special
55** case is when there is no absolute info or the instruction is before
56** the first absolute one.
57*/
58static int getbaseline (const Proto *f, int pc, int *basepc) {
59 if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
60 *basepc = -1; /* start from the beginning */
61 return f->linedefined;
62 }
63 else {
64 unsigned int i;
65 if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc)
66 i = f->sizeabslineinfo - 1; /* instruction is after last saved one */
67 else { /* binary search */
68 unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */
69 i = 0; /* abslineinfo[i] <= pc */
70 while (i < j - 1) {
71 unsigned int m = (j + i) / 2;
72 if (pc >= f->abslineinfo[m].pc)
73 i = m;
74 else
75 j = m;
76 }
77 }
78 *basepc = f->abslineinfo[i].pc;
79 return f->abslineinfo[i].line;
80 }
53} 81}
54 82
55 83
56/* 84/*
57** If function yielded, its 'func' can be in the 'extra' field. The 85** Get the line corresponding to instruction 'pc' in function 'f';
58** next function restores 'func' to its correct value for debugging 86** first gets a base line and from there does the increments until
59** purposes. (It exchanges 'func' and 'extra'; so, when called again, 87** the desired instruction.
60** after debugging, it also "re-restores" ** 'func' to its altered value.
61*/ 88*/
62static void swapextra (lua_State *L) { 89int luaG_getfuncline (const Proto *f, int pc) {
63 if (L->status == LUA_YIELD) { 90 if (f->lineinfo == NULL) /* no debug information? */
64 CallInfo *ci = L->ci; /* get function that yielded */ 91 return -1;
65 StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ 92 else {
66 ci->func = restorestack(L, ci->extra); 93 int basepc;
67 ci->extra = savestack(L, temp); 94 int baseline = getbaseline(f, pc, &basepc);
95 while (basepc++ < pc) { /* walk until given instruction */
96 lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
97 baseline += f->lineinfo[basepc]; /* correct line */
98 }
99 return baseline;
68 } 100 }
69} 101}
70 102
71 103
104static int getcurrentline (CallInfo *ci) {
105 return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
106}
107
108
72/* 109/*
73** This function can be called asynchronously (e.g. during a signal). 110** Set 'trap' for all active Lua frames.
111** This function can be called during a signal, under "reasonable"
112** assumptions. A new 'ci' is completely linked in the list before it
113** becomes part of the "active" list, and we assume that pointers are
114** atomic; see comment in next function.
115** (A compiler doing interprocedural optimizations could, theoretically,
116** reorder memory writes in such a way that the list could be
117** temporarily broken while inserting a new element. We simply assume it
118** has no good reasons to do that.)
119*/
120static void settraps (CallInfo *ci) {
121 for (; ci != NULL; ci = ci->previous)
122 if (isLua(ci))
123 ci->u.l.trap = 1;
124}
125
126
127/*
128** This function can be called during a signal, under "reasonable"
129** assumptions.
74** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by 130** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
75** 'resethookcount') are for debug only, and it is no problem if they 131** 'resethookcount') are for debug only, and it is no problem if they
76** get arbitrary values (causes at most one wrong hook call). 'hookmask' 132** get arbitrary values (causes at most one wrong hook call). 'hookmask'
@@ -89,6 +145,8 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
89 L->basehookcount = count; 145 L->basehookcount = count;
90 resethookcount(L); 146 resethookcount(L);
91 L->hookmask = cast_byte(mask); 147 L->hookmask = cast_byte(mask);
148 if (mask)
149 settraps(L->ci); /* to trace inside 'luaV_execute' */
92} 150}
93 151
94 152
@@ -124,7 +182,7 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
124} 182}
125 183
126 184
127static const char *upvalname (Proto *p, int uv) { 185static const char *upvalname (const Proto *p, int uv) {
128 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); 186 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
129 if (s == NULL) return "?"; 187 if (s == NULL) return "?";
130 else return getstr(s); 188 else return getstr(s);
@@ -132,38 +190,37 @@ static const char *upvalname (Proto *p, int uv) {
132 190
133 191
134static const char *findvararg (CallInfo *ci, int n, StkId *pos) { 192static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
135 int nparams = clLvalue(ci->func)->p->numparams; 193 if (clLvalue(s2v(ci->func))->p->is_vararg) {
136 if (n >= cast_int(ci->u.l.base - ci->func) - nparams) 194 int nextra = ci->u.l.nextraargs;
137 return NULL; /* no such vararg */ 195 if (n <= nextra) {
138 else { 196 *pos = ci->func - nextra + (n - 1);
139 *pos = ci->func + nparams + n; 197 return "(vararg)"; /* generic name for any vararg */
140 return "(*vararg)"; /* generic name for any vararg */ 198 }
141 } 199 }
200 return NULL; /* no such vararg */
142} 201}
143 202
144 203
145static const char *findlocal (lua_State *L, CallInfo *ci, int n, 204const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
146 StkId *pos) { 205 StkId base = ci->func + 1;
147 const char *name = NULL; 206 const char *name = NULL;
148 StkId base;
149 if (isLua(ci)) { 207 if (isLua(ci)) {
150 if (n < 0) /* access to vararg values? */ 208 if (n < 0) /* access to vararg values? */
151 return findvararg(ci, -n, pos); 209 return findvararg(ci, -n, pos);
152 else { 210 else
153 base = ci->u.l.base;
154 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); 211 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
155 }
156 } 212 }
157 else
158 base = ci->func + 1;
159 if (name == NULL) { /* no 'standard' name? */ 213 if (name == NULL) { /* no 'standard' name? */
160 StkId limit = (ci == L->ci) ? L->top : ci->next->func; 214 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
161 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 215 if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
162 name = "(*temporary)"; /* generic name for any valid slot */ 216 /* generic name for any valid slot */
217 name = isLua(ci) ? "(temporary)" : "(C temporary)";
218 }
163 else 219 else
164 return NULL; /* no name */ 220 return NULL; /* no name */
165 } 221 }
166 *pos = base + (n - 1); 222 if (pos)
223 *pos = base + (n - 1);
167 return name; 224 return name;
168} 225}
169 226
@@ -171,22 +228,20 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
171LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 228LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
172 const char *name; 229 const char *name;
173 lua_lock(L); 230 lua_lock(L);
174 swapextra(L);
175 if (ar == NULL) { /* information about non-active function? */ 231 if (ar == NULL) { /* information about non-active function? */
176 if (!isLfunction(L->top - 1)) /* not a Lua function? */ 232 if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */
177 name = NULL; 233 name = NULL;
178 else /* consider live variables at function start (parameters) */ 234 else /* consider live variables at function start (parameters) */
179 name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); 235 name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0);
180 } 236 }
181 else { /* active function; get information through 'ar' */ 237 else { /* active function; get information through 'ar' */
182 StkId pos = NULL; /* to avoid warnings */ 238 StkId pos = NULL; /* to avoid warnings */
183 name = findlocal(L, ar->i_ci, n, &pos); 239 name = luaG_findlocal(L, ar->i_ci, n, &pos);
184 if (name) { 240 if (name) {
185 setobj2s(L, L->top, pos); 241 setobjs2s(L, L->top, pos);
186 api_incr_top(L); 242 api_incr_top(L);
187 } 243 }
188 } 244 }
189 swapextra(L);
190 lua_unlock(L); 245 lua_unlock(L);
191 return name; 246 return name;
192} 247}
@@ -196,13 +251,11 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
196 StkId pos = NULL; /* to avoid warnings */ 251 StkId pos = NULL; /* to avoid warnings */
197 const char *name; 252 const char *name;
198 lua_lock(L); 253 lua_lock(L);
199 swapextra(L); 254 name = luaG_findlocal(L, ar->i_ci, n, &pos);
200 name = findlocal(L, ar->i_ci, n, &pos);
201 if (name) { 255 if (name) {
202 setobjs2s(L, pos, L->top - 1); 256 setobjs2s(L, pos, L->top - 1);
203 L->top--; /* pop value */ 257 L->top--; /* pop value */
204 } 258 }
205 swapextra(L);
206 lua_unlock(L); 259 lua_unlock(L);
207 return name; 260 return name;
208} 261}
@@ -211,36 +264,55 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
211static void funcinfo (lua_Debug *ar, Closure *cl) { 264static void funcinfo (lua_Debug *ar, Closure *cl) {
212 if (noLuaClosure(cl)) { 265 if (noLuaClosure(cl)) {
213 ar->source = "=[C]"; 266 ar->source = "=[C]";
267 ar->srclen = LL("=[C]");
214 ar->linedefined = -1; 268 ar->linedefined = -1;
215 ar->lastlinedefined = -1; 269 ar->lastlinedefined = -1;
216 ar->what = "C"; 270 ar->what = "C";
217 } 271 }
218 else { 272 else {
219 Proto *p = cl->l.p; 273 const Proto *p = cl->l.p;
220 ar->source = p->source ? getstr(p->source) : "=?"; 274 if (p->source) {
275 ar->source = getstr(p->source);
276 ar->srclen = tsslen(p->source);
277 }
278 else {
279 ar->source = "=?";
280 ar->srclen = LL("=?");
281 }
221 ar->linedefined = p->linedefined; 282 ar->linedefined = p->linedefined;
222 ar->lastlinedefined = p->lastlinedefined; 283 ar->lastlinedefined = p->lastlinedefined;
223 ar->what = (ar->linedefined == 0) ? "main" : "Lua"; 284 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
224 } 285 }
225 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 286 luaO_chunkid(ar->short_src, ar->source, ar->srclen);
287}
288
289
290static int nextline (const Proto *p, int currentline, int pc) {
291 if (p->lineinfo[pc] != ABSLINEINFO)
292 return currentline + p->lineinfo[pc];
293 else
294 return luaG_getfuncline(p, pc);
226} 295}
227 296
228 297
229static void collectvalidlines (lua_State *L, Closure *f) { 298static void collectvalidlines (lua_State *L, Closure *f) {
230 if (noLuaClosure(f)) { 299 if (noLuaClosure(f)) {
231 setnilvalue(L->top); 300 setnilvalue(s2v(L->top));
232 api_incr_top(L); 301 api_incr_top(L);
233 } 302 }
234 else { 303 else {
235 int i; 304 int i;
236 TValue v; 305 TValue v;
237 int *lineinfo = f->l.p->lineinfo; 306 const Proto *p = f->l.p;
307 int currentline = p->linedefined;
238 Table *t = luaH_new(L); /* new table to store active lines */ 308 Table *t = luaH_new(L); /* new table to store active lines */
239 sethvalue(L, L->top, t); /* push it on stack */ 309 sethvalue2s(L, L->top, t); /* push it on stack */
240 api_incr_top(L); 310 api_incr_top(L);
241 setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ 311 setbtvalue(&v); /* boolean 'true' to be the value of all indices */
242 for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ 312 for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */
243 luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ 313 currentline = nextline(p, currentline, i);
314 luaH_setint(L, t, currentline, &v); /* table[line] = true */
315 }
244 } 316 }
245} 317}
246 318
@@ -269,7 +341,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
269 break; 341 break;
270 } 342 }
271 case 'l': { 343 case 'l': {
272 ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; 344 ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
273 break; 345 break;
274 } 346 }
275 case 'u': { 347 case 'u': {
@@ -296,6 +368,15 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
296 } 368 }
297 break; 369 break;
298 } 370 }
371 case 'r': {
372 if (ci == NULL || !(ci->callstatus & CIST_TRAN))
373 ar->ftransfer = ar->ntransfer = 0;
374 else {
375 ar->ftransfer = ci->u2.transferinfo.ftransfer;
376 ar->ntransfer = ci->u2.transferinfo.ntransfer;
377 }
378 break;
379 }
299 case 'L': 380 case 'L':
300 case 'f': /* handled by lua_getinfo */ 381 case 'f': /* handled by lua_getinfo */
301 break; 382 break;
@@ -310,28 +391,26 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
310 int status; 391 int status;
311 Closure *cl; 392 Closure *cl;
312 CallInfo *ci; 393 CallInfo *ci;
313 StkId func; 394 TValue *func;
314 lua_lock(L); 395 lua_lock(L);
315 swapextra(L);
316 if (*what == '>') { 396 if (*what == '>') {
317 ci = NULL; 397 ci = NULL;
318 func = L->top - 1; 398 func = s2v(L->top - 1);
319 api_check(L, ttisfunction(func), "function expected"); 399 api_check(L, ttisfunction(func), "function expected");
320 what++; /* skip the '>' */ 400 what++; /* skip the '>' */
321 L->top--; /* pop function */ 401 L->top--; /* pop function */
322 } 402 }
323 else { 403 else {
324 ci = ar->i_ci; 404 ci = ar->i_ci;
325 func = ci->func; 405 func = s2v(ci->func);
326 lua_assert(ttisfunction(ci->func)); 406 lua_assert(ttisfunction(func));
327 } 407 }
328 cl = ttisclosure(func) ? clvalue(func) : NULL; 408 cl = ttisclosure(func) ? clvalue(func) : NULL;
329 status = auxgetinfo(L, what, ar, cl, ci); 409 status = auxgetinfo(L, what, ar, cl, ci);
330 if (strchr(what, 'f')) { 410 if (strchr(what, 'f')) {
331 setobjs2s(L, L->top, func); 411 setobj2s(L, L->top, func);
332 api_incr_top(L); 412 api_incr_top(L);
333 } 413 }
334 swapextra(L); /* correct before option 'L', which can raise a mem. error */
335 if (strchr(what, 'L')) 414 if (strchr(what, 'L'))
336 collectvalidlines(L, cl); 415 collectvalidlines(L, cl);
337 lua_unlock(L); 416 lua_unlock(L);
@@ -345,30 +424,38 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
345** ======================================================= 424** =======================================================
346*/ 425*/
347 426
348static const char *getobjname (Proto *p, int lastpc, int reg, 427static const char *getobjname (const Proto *p, int lastpc, int reg,
349 const char **name); 428 const char **name);
350 429
351 430
352/* 431/*
353** find a "name" for the RK value 'c' 432** Find a "name" for the constant 'c'.
354*/ 433*/
355static void kname (Proto *p, int pc, int c, const char **name) { 434static void kname (const Proto *p, int c, const char **name) {
356 if (ISK(c)) { /* is 'c' a constant? */ 435 TValue *kvalue = &p->k[c];
357 TValue *kvalue = &p->k[INDEXK(c)]; 436 *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?";
358 if (ttisstring(kvalue)) { /* literal constant? */ 437}
359 *name = svalue(kvalue); /* it is its own name */ 438
360 return; 439
361 } 440/*
362 /* else no reasonable name found */ 441** Find a "name" for the register 'c'.
363 } 442*/
364 else { /* 'c' is a register */ 443static void rname (const Proto *p, int pc, int c, const char **name) {
365 const char *what = getobjname(p, pc, c, name); /* search for 'c' */ 444 const char *what = getobjname(p, pc, c, name); /* search for 'c' */
366 if (what && *what == 'c') { /* found a constant name? */ 445 if (!(what && *what == 'c')) /* did not find a constant name? */
367 return; /* 'name' already filled */ 446 *name = "?";
368 } 447}
369 /* else no reasonable name found */ 448
370 } 449
371 *name = "?"; /* no reasonable name found */ 450/*
451** Find a "name" for a 'C' value in an RK instruction.
452*/
453static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
454 int c = GETARG_C(i); /* key index */
455 if (GETARG_k(i)) /* is 'c' a constant? */
456 kname(p, c, name);
457 else /* 'c' is a register */
458 rname(p, pc, c, name);
372} 459}
373 460
374 461
@@ -380,55 +467,70 @@ static int filterpc (int pc, int jmptarget) {
380 467
381 468
382/* 469/*
383** try to find last instruction before 'lastpc' that modified register 'reg' 470** Try to find last instruction before 'lastpc' that modified register 'reg'.
384*/ 471*/
385static int findsetreg (Proto *p, int lastpc, int reg) { 472static int findsetreg (const Proto *p, int lastpc, int reg) {
386 int pc; 473 int pc;
387 int setreg = -1; /* keep last instruction that changed 'reg' */ 474 int setreg = -1; /* keep last instruction that changed 'reg' */
388 int jmptarget = 0; /* any code before this address is conditional */ 475 int jmptarget = 0; /* any code before this address is conditional */
476 if (testMMMode(GET_OPCODE(p->code[lastpc])))
477 lastpc--; /* previous instruction was not actually executed */
389 for (pc = 0; pc < lastpc; pc++) { 478 for (pc = 0; pc < lastpc; pc++) {
390 Instruction i = p->code[pc]; 479 Instruction i = p->code[pc];
391 OpCode op = GET_OPCODE(i); 480 OpCode op = GET_OPCODE(i);
392 int a = GETARG_A(i); 481 int a = GETARG_A(i);
482 int change; /* true if current instruction changed 'reg' */
393 switch (op) { 483 switch (op) {
394 case OP_LOADNIL: { 484 case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */
395 int b = GETARG_B(i); 485 int b = GETARG_B(i);
396 if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ 486 change = (a <= reg && reg <= a + b);
397 setreg = filterpc(pc, jmptarget);
398 break; 487 break;
399 } 488 }
400 case OP_TFORCALL: { 489 case OP_TFORCALL: { /* affect all regs above its base */
401 if (reg >= a + 2) /* affect all regs above its base */ 490 change = (reg >= a + 2);
402 setreg = filterpc(pc, jmptarget);
403 break; 491 break;
404 } 492 }
405 case OP_CALL: 493 case OP_CALL:
406 case OP_TAILCALL: { 494 case OP_TAILCALL: { /* affect all registers above base */
407 if (reg >= a) /* affect all registers above base */ 495 change = (reg >= a);
408 setreg = filterpc(pc, jmptarget);
409 break; 496 break;
410 } 497 }
411 case OP_JMP: { 498 case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */
412 int b = GETARG_sBx(i); 499 int b = GETARG_sJ(i);
413 int dest = pc + 1 + b; 500 int dest = pc + 1 + b;
414 /* jump is forward and do not skip 'lastpc'? */ 501 /* jump does not skip 'lastpc' and is larger than current one? */
415 if (pc < dest && dest <= lastpc) { 502 if (dest <= lastpc && dest > jmptarget)
416 if (dest > jmptarget) 503 jmptarget = dest; /* update 'jmptarget' */
417 jmptarget = dest; /* update 'jmptarget' */ 504 change = 0;
418 }
419 break; 505 break;
420 } 506 }
421 default: 507 default: /* any instruction that sets A */
422 if (testAMode(op) && reg == a) /* any instruction that set A */ 508 change = (testAMode(op) && reg == a);
423 setreg = filterpc(pc, jmptarget);
424 break; 509 break;
425 } 510 }
511 if (change)
512 setreg = filterpc(pc, jmptarget);
426 } 513 }
427 return setreg; 514 return setreg;
428} 515}
429 516
430 517
431static const char *getobjname (Proto *p, int lastpc, int reg, 518/*
519** Check whether table being indexed by instruction 'i' is the
520** environment '_ENV'
521*/
522static const char *gxf (const Proto *p, int pc, Instruction i, int isup) {
523 int t = GETARG_B(i); /* table index */
524 const char *name; /* name of indexed variable */
525 if (isup) /* is an upvalue? */
526 name = upvalname(p, t);
527 else
528 getobjname(p, pc, t, &name);
529 return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
530}
531
532
533static const char *getobjname (const Proto *p, int lastpc, int reg,
432 const char **name) { 534 const char **name) {
433 int pc; 535 int pc;
434 *name = luaF_getlocalname(p, reg + 1, lastpc); 536 *name = luaF_getlocalname(p, reg + 1, lastpc);
@@ -446,15 +548,24 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
446 return getobjname(p, pc, b, name); /* get name for 'b' */ 548 return getobjname(p, pc, b, name); /* get name for 'b' */
447 break; 549 break;
448 } 550 }
449 case OP_GETTABUP: 551 case OP_GETTABUP: {
552 int k = GETARG_C(i); /* key index */
553 kname(p, k, name);
554 return gxf(p, pc, i, 1);
555 }
450 case OP_GETTABLE: { 556 case OP_GETTABLE: {
451 int k = GETARG_C(i); /* key index */ 557 int k = GETARG_C(i); /* key index */
452 int t = GETARG_B(i); /* table index */ 558 rname(p, pc, k, name);
453 const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ 559 return gxf(p, pc, i, 0);
454 ? luaF_getlocalname(p, t + 1, pc) 560 }
455 : upvalname(p, t); 561 case OP_GETI: {
456 kname(p, pc, k, name); 562 *name = "integer index";
457 return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; 563 return "field";
564 }
565 case OP_GETFIELD: {
566 int k = GETARG_C(i); /* key index */
567 kname(p, k, name);
568 return gxf(p, pc, i, 0);
458 } 569 }
459 case OP_GETUPVAL: { 570 case OP_GETUPVAL: {
460 *name = upvalname(p, GETARG_B(i)); 571 *name = upvalname(p, GETARG_B(i));
@@ -471,8 +582,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
471 break; 582 break;
472 } 583 }
473 case OP_SELF: { 584 case OP_SELF: {
474 int k = GETARG_C(i); /* key index */ 585 rkname(p, pc, i, name);
475 kname(p, pc, k, name);
476 return "method"; 586 return "method";
477 } 587 }
478 default: break; /* go through to return NULL */ 588 default: break; /* go through to return NULL */
@@ -491,7 +601,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
491static const char *funcnamefromcode (lua_State *L, CallInfo *ci, 601static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
492 const char **name) { 602 const char **name) {
493 TMS tm = (TMS)0; /* (initial value avoids warnings) */ 603 TMS tm = (TMS)0; /* (initial value avoids warnings) */
494 Proto *p = ci_func(ci)->p; /* calling function */ 604 const Proto *p = ci_func(ci)->p; /* calling function */
495 int pc = currentpc(ci); /* calling instruction index */ 605 int pc = currentpc(ci); /* calling instruction index */
496 Instruction i = p->code[pc]; /* calling instruction */ 606 Instruction i = p->code[pc]; /* calling instruction */
497 if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ 607 if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
@@ -508,16 +618,14 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
508 } 618 }
509 /* other instructions can do calls through metamethods */ 619 /* other instructions can do calls through metamethods */
510 case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: 620 case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
621 case OP_GETI: case OP_GETFIELD:
511 tm = TM_INDEX; 622 tm = TM_INDEX;
512 break; 623 break;
513 case OP_SETTABUP: case OP_SETTABLE: 624 case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
514 tm = TM_NEWINDEX; 625 tm = TM_NEWINDEX;
515 break; 626 break;
516 case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: 627 case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
517 case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: 628 tm = cast(TMS, GETARG_C(i));
518 case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
519 int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */
520 tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */
521 break; 629 break;
522 } 630 }
523 case OP_UNM: tm = TM_UNM; break; 631 case OP_UNM: tm = TM_UNM; break;
@@ -525,12 +633,16 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
525 case OP_LEN: tm = TM_LEN; break; 633 case OP_LEN: tm = TM_LEN; break;
526 case OP_CONCAT: tm = TM_CONCAT; break; 634 case OP_CONCAT: tm = TM_CONCAT; break;
527 case OP_EQ: tm = TM_EQ; break; 635 case OP_EQ: tm = TM_EQ; break;
528 case OP_LT: tm = TM_LT; break; 636 case OP_LT: case OP_LE: case OP_LTI: case OP_LEI:
529 case OP_LE: tm = TM_LE; break; 637 *name = "order"; /* '<=' can call '__lt', etc. */
638 return "metamethod";
639 case OP_CLOSE: case OP_RETURN:
640 *name = "close";
641 return "metamethod";
530 default: 642 default:
531 return NULL; /* cannot find a reasonable name */ 643 return NULL; /* cannot find a reasonable name */
532 } 644 }
533 *name = getstr(G(L)->tmname[tm]); 645 *name = getstr(G(L)->tmname[tm]) + 2;
534 return "metamethod"; 646 return "metamethod";
535} 647}
536 648
@@ -544,8 +656,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
544** checks are ISO C and ensure a correct result. 656** checks are ISO C and ensure a correct result.
545*/ 657*/
546static int isinstack (CallInfo *ci, const TValue *o) { 658static int isinstack (CallInfo *ci, const TValue *o) {
547 ptrdiff_t i = o - ci->u.l.base; 659 StkId base = ci->func + 1;
548 return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o); 660 ptrdiff_t i = cast(StkId, o) - base;
661 return (0 <= i && i < (ci->top - base) && s2v(base + i) == o);
549} 662}
550 663
551 664
@@ -576,7 +689,7 @@ static const char *varinfo (lua_State *L, const TValue *o) {
576 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ 689 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
577 if (!kind && isinstack(ci, o)) /* no? try a register */ 690 if (!kind && isinstack(ci, o)) /* no? try a register */
578 kind = getobjname(ci_func(ci)->p, currentpc(ci), 691 kind = getobjname(ci_func(ci)->p, currentpc(ci),
579 cast_int(o - ci->u.l.base), &name); 692 cast_int(cast(StkId, o) - (ci->func + 1)), &name);
580 } 693 }
581 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; 694 return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
582} 695}
@@ -588,6 +701,12 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
588} 701}
589 702
590 703
704l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
705 luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
706 what, luaT_objtypename(L, o));
707}
708
709
591l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { 710l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
592 if (ttisstring(p1) || cvt2str(p1)) p1 = p2; 711 if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
593 luaG_typeerror(L, p1, "concatenate"); 712 luaG_typeerror(L, p1, "concatenate");
@@ -596,8 +715,7 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
596 715
597l_noret luaG_opinterror (lua_State *L, const TValue *p1, 716l_noret luaG_opinterror (lua_State *L, const TValue *p1,
598 const TValue *p2, const char *msg) { 717 const TValue *p2, const char *msg) {
599 lua_Number temp; 718 if (!ttisnumber(p1)) /* first operand is wrong? */
600 if (!tonumber(p1, &temp)) /* first operand is wrong? */
601 p2 = p1; /* now second is wrong */ 719 p2 = p1; /* now second is wrong */
602 luaG_typeerror(L, p2, msg); 720 luaG_typeerror(L, p2, msg);
603} 721}
@@ -608,7 +726,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1,
608*/ 726*/
609l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { 727l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
610 lua_Integer temp; 728 lua_Integer temp;
611 if (!tointeger(p1, &temp)) 729 if (!tointegerns(p1, &temp))
612 p2 = p1; 730 p2 = p1;
613 luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); 731 luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
614} 732}
@@ -629,7 +747,7 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
629 int line) { 747 int line) {
630 char buff[LUA_IDSIZE]; 748 char buff[LUA_IDSIZE];
631 if (src) 749 if (src)
632 luaO_chunkid(buff, getstr(src), LUA_IDSIZE); 750 luaO_chunkid(buff, getstr(src), tsslen(src));
633 else { /* no source available; use "?" instead */ 751 else { /* no source available; use "?" instead */
634 buff[0] = '?'; buff[1] = '\0'; 752 buff[0] = '?'; buff[1] = '\0';
635 } 753 }
@@ -640,6 +758,7 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
640l_noret luaG_errormsg (lua_State *L) { 758l_noret luaG_errormsg (lua_State *L) {
641 if (L->errfunc != 0) { /* is there an error handling function? */ 759 if (L->errfunc != 0) { /* is there an error handling function? */
642 StkId errfunc = restorestack(L, L->errfunc); 760 StkId errfunc = restorestack(L, L->errfunc);
761 lua_assert(ttisfunction(s2v(errfunc)));
643 setobjs2s(L, L->top, L->top - 1); /* move argument */ 762 setobjs2s(L, L->top, L->top - 1); /* move argument */
644 setobjs2s(L, L->top - 1, errfunc); /* push function */ 763 setobjs2s(L, L->top - 1, errfunc); /* push function */
645 L->top++; /* assume EXTRA_STACK */ 764 L->top++; /* assume EXTRA_STACK */
@@ -658,42 +777,65 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
658 msg = luaO_pushvfstring(L, fmt, argp); /* format message */ 777 msg = luaO_pushvfstring(L, fmt, argp); /* format message */
659 va_end(argp); 778 va_end(argp);
660 if (isLua(ci)) /* if Lua function, add source:line information */ 779 if (isLua(ci)) /* if Lua function, add source:line information */
661 luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); 780 luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
662 luaG_errormsg(L); 781 luaG_errormsg(L);
663} 782}
664 783
665 784
666void luaG_traceexec (lua_State *L) { 785/*
786** Check whether new instruction 'newpc' is in a different line from
787** previous instruction 'oldpc'.
788*/
789static int changedline (const Proto *p, int oldpc, int newpc) {
790 while (oldpc++ < newpc) {
791 if (p->lineinfo[oldpc] != 0)
792 return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc));
793 }
794 return 0; /* no line changes in the way */
795}
796
797
798int luaG_traceexec (lua_State *L, const Instruction *pc) {
667 CallInfo *ci = L->ci; 799 CallInfo *ci = L->ci;
668 lu_byte mask = L->hookmask; 800 lu_byte mask = L->hookmask;
669 int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); 801 int counthook;
802 if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
803 ci->u.l.trap = 0; /* don't need to stop again */
804 return 0; /* turn off 'trap' */
805 }
806 pc++; /* reference is always next instruction */
807 ci->u.l.savedpc = pc; /* save 'pc' */
808 counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
670 if (counthook) 809 if (counthook)
671 resethookcount(L); /* reset count */ 810 resethookcount(L); /* reset count */
672 else if (!(mask & LUA_MASKLINE)) 811 else if (!(mask & LUA_MASKLINE))
673 return; /* no line hook and count != 0; nothing to be done */ 812 return 1; /* no line hook and count != 0; nothing to be done now */
674 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ 813 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
675 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ 814 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
676 return; /* do not call hook again (VM yielded, so it did not move) */ 815 return 1; /* do not call hook again (VM yielded, so it did not move) */
677 } 816 }
817 if (!isIT(*(ci->u.l.savedpc - 1)))
818 L->top = ci->top; /* prepare top */
678 if (counthook) 819 if (counthook)
679 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ 820 luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
680 if (mask & LUA_MASKLINE) { 821 if (mask & LUA_MASKLINE) {
681 Proto *p = ci_func(ci)->p; 822 const Proto *p = ci_func(ci)->p;
682 int npc = pcRel(ci->u.l.savedpc, p); 823 int npci = pcRel(pc, p);
683 int newline = getfuncline(p, npc); 824 if (npci == 0 || /* call linehook when enter a new function, */
684 if (npc == 0 || /* call linehook when enter a new function, */ 825 pc <= L->oldpc || /* when jump back (loop), or when */
685 ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ 826 changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */
686 newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ 827 int newline = luaG_getfuncline(p, npci);
687 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ 828 luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
688 } 829 }
689 L->oldpc = ci->u.l.savedpc; 830 L->oldpc = pc; /* 'pc' of last call to line hook */
831 }
690 if (L->status == LUA_YIELD) { /* did hook yield? */ 832 if (L->status == LUA_YIELD) { /* did hook yield? */
691 if (counthook) 833 if (counthook)
692 L->hookcount = 1; /* undo decrement to zero */ 834 L->hookcount = 1; /* undo decrement to zero */
693 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ 835 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
694 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ 836 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
695 ci->func = L->top - 1; /* protect stack below results */
696 luaD_throw(L, LUA_YIELD); 837 luaD_throw(L, LUA_YIELD);
697 } 838 }
839 return 1; /* keep 'trap' on */
698} 840}
699 841