interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / ldebug.c

979 lines · 30069 bytes Raw Blame History
1/*
2** $Id: ldebug.c $
3** Debug Interface
4** See Copyright Notice in lua.h
5*/
6
7#define ldebug_c
8#define LUA_CORE
9
10#include "lprefix.h"
11
12
13#include <stdarg.h>
14#include <stddef.h>
15#include <string.h>
16
17#include "lua.h"
18
19#include "lapi.h"
20#include "lcode.h"
21#include "ldebug.h"
22#include "ldo.h"
23#include "lfunc.h"
24#include "lobject.h"
25#include "lopcodes.h"
26#include "lstate.h"
27#include "lstring.h"
28#include "ltable.h"
29#include "ltm.h"
30#include "lvm.h"
31
32
33
34#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL)
35
36static const char strlocal[] = "local";
37static const char strupval[] = "upvalue";
38
39static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
40 const char **name);
41
42
43static int currentpc (CallInfo *ci) {
44 lua_assert(isLua(ci));
45 return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
46}
47
48
49/*
50** Get a "base line" to find the line corresponding to an instruction.
51** Base lines are regularly placed at MAXIWTHABS intervals, so usually
52** an integer division gets the right place. When the source file has
53** large sequences of empty/comment lines, it may need extra entries,
54** so the original estimate needs a correction.
55** If the original estimate is -1, the initial 'if' ensures that the
56** 'while' will run at least once.
57** The assertion that the estimate is a lower bound for the correct base
58** is valid as long as the debug info has been generated with the same
59** value for MAXIWTHABS or smaller. (Previous releases use a little
60** smaller value.)
61*/
62static int getbaseline (const Proto *f, int pc, int *basepc) {
63 if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
64 *basepc = -1; /* start from the beginning */
65 return f->linedefined;
66 }
67 else {
68 int i = pc / MAXIWTHABS - 1; /* get an estimate */
69 /* estimate must be a lower bound of the correct base */
70 lua_assert(i < 0 ||
71 (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
72 while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
73 i++; /* low estimate; adjust it */
74 *basepc = f->abslineinfo[i].pc;
75 return f->abslineinfo[i].line;
76 }
77}
78
79
80/*
81** Get the line corresponding to instruction 'pc' in function 'f';
82** first gets a base line and from there does the increments until
83** the desired instruction.
84*/
85int luaG_getfuncline (const Proto *f, int pc) {
86 if (f->lineinfo == NULL) /* no debug information? */
87 return -1;
88 else {
89 int basepc;
90 int baseline = getbaseline(f, pc, &basepc);
91 while (basepc++ < pc) { /* walk until given instruction */
92 lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
93 baseline += f->lineinfo[basepc]; /* correct line */
94 }
95 return baseline;
96 }
97}
98
99
100static int getcurrentline (CallInfo *ci) {
101 return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
102}
103
104
105/*
106** Set 'trap' for all active Lua frames.
107** This function can be called during a signal, under "reasonable"
108** assumptions. A new 'ci' is completely linked in the list before it
109** becomes part of the "active" list, and we assume that pointers are
110** atomic; see comment in next function.
111** (A compiler doing interprocedural optimizations could, theoretically,
112** reorder memory writes in such a way that the list could be
113** temporarily broken while inserting a new element. We simply assume it
114** has no good reasons to do that.)
115*/
116static void settraps (CallInfo *ci) {
117 for (; ci != NULL; ci = ci->previous)
118 if (isLua(ci))
119 ci->u.l.trap = 1;
120}
121
122
123/*
124** This function can be called during a signal, under "reasonable"
125** assumptions.
126** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount')
127** are for debug only, and it is no problem if they get arbitrary
128** values (causes at most one wrong hook call). 'hookmask' is an atomic
129** value. We assume that pointers are atomic too (e.g., gcc ensures that
130** for all platforms where it runs). Moreover, 'hook' is always checked
131** before being called (see 'luaD_hook').
132*/
133LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
134 if (func == NULL || mask == 0) { /* turn off hooks? */
135 mask = 0;
136 func = NULL;
137 }
138 L->hook = func;
139 L->basehookcount = count;
140 resethookcount(L);
141 L->hookmask = cast_byte(mask);
142 if (mask)
143 settraps(L->ci); /* to trace inside 'luaV_execute' */
144}
145
146
147LUA_API lua_Hook lua_gethook (lua_State *L) {
148 return L->hook;
149}
150
151
152LUA_API int lua_gethookmask (lua_State *L) {
153 return cast_int(L->hookmask);
154}
155
156
157LUA_API int lua_gethookcount (lua_State *L) {
158 return L->basehookcount;
159}
160
161
162LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
163 int status;
164 CallInfo *ci;
165 if (level < 0) return 0; /* invalid (negative) level */
166 lua_lock(L);
167 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
168 level--;
169 if (level == 0 && ci != &L->base_ci) { /* level found? */
170 status = 1;
171 ar->i_ci = ci;
172 }
173 else status = 0; /* no such level */
174 lua_unlock(L);
175 return status;
176}
177
178
179static const char *upvalname (const Proto *p, int uv) {
180 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
181 if (s == NULL) return "?";
182 else return getstr(s);
183}
184
185
186static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
187 if (clLvalue(s2v(ci->func.p))->p->flag & PF_VAHID) {
188 int nextra = ci->u.l.nextraargs;
189 if (n >= -nextra) { /* 'n' is negative */
190 *pos = ci->func.p - nextra - (n + 1);
191 return "(vararg)"; /* generic name for any vararg */
192 }
193 }
194 return NULL; /* no such vararg */
195}
196
197
198const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
199 StkId base = ci->func.p + 1;
200 const char *name = NULL;
201 if (isLua(ci)) {
202 if (n < 0) /* access to vararg values? */
203 return findvararg(ci, n, pos);
204 else
205 name = luaF_getlocalname(ci_func(ci)->p, cast_uint(n), currentpc(ci));
206 }
207 if (name == NULL) { /* no 'standard' name? */
208 StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
209 if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
210 /* generic name for any valid slot */
211 name = isLua(ci) ? "(temporary)" : "(C temporary)";
212 }
213 else
214 return NULL; /* no name */
215 }
216 if (pos)
217 *pos = base + (n - 1);
218 return name;
219}
220
221
222LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
223 const char *name;
224 lua_lock(L);
225 if (ar == NULL) { /* information about non-active function? */
226 if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */
227 name = NULL;
228 else /* consider live variables at function start (parameters) */
229 name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, cast_uint(n), 0);
230 }
231 else { /* active function; get information through 'ar' */
232 StkId pos = NULL; /* to avoid warnings */
233 name = luaG_findlocal(L, ar->i_ci, n, &pos);
234 if (name) {
235 setobjs2s(L, L->top.p, pos);
236 api_incr_top(L);
237 }
238 }
239 lua_unlock(L);
240 return name;
241}
242
243
244LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
245 StkId pos = NULL; /* to avoid warnings */
246 const char *name;
247 lua_lock(L);
248 name = luaG_findlocal(L, ar->i_ci, n, &pos);
249 if (name) {
250 api_checkpop(L, 1);
251 setobjs2s(L, pos, L->top.p - 1);
252 L->top.p--; /* pop value */
253 }
254 lua_unlock(L);
255 return name;
256}
257
258
259static void funcinfo (lua_Debug *ar, Closure *cl) {
260 if (!LuaClosure(cl)) {
261 ar->source = "=[C]";
262 ar->srclen = LL("=[C]");
263 ar->linedefined = -1;
264 ar->lastlinedefined = -1;
265 ar->what = "C";
266 }
267 else {
268 const Proto *p = cl->l.p;
269 if (p->source) {
270 ar->source = getlstr(p->source, ar->srclen);
271 }
272 else {
273 ar->source = "=?";
274 ar->srclen = LL("=?");
275 }
276 ar->linedefined = p->linedefined;
277 ar->lastlinedefined = p->lastlinedefined;
278 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
279 }
280 luaO_chunkid(ar->short_src, ar->source, ar->srclen);
281}
282
283
284static int nextline (const Proto *p, int currentline, int pc) {
285 if (p->lineinfo[pc] != ABSLINEINFO)
286 return currentline + p->lineinfo[pc];
287 else
288 return luaG_getfuncline(p, pc);
289}
290
291
292static void collectvalidlines (lua_State *L, Closure *f) {
293 if (!LuaClosure(f)) {
294 setnilvalue2s(L->top.p);
295 api_incr_top(L);
296 }
297 else {
298 const Proto *p = f->l.p;
299 int currentline = p->linedefined;
300 Table *t = luaH_new(L); /* new table to store active lines */
301 sethvalue2s(L, L->top.p, t); /* push it on stack */
302 api_incr_top(L);
303 if (p->lineinfo != NULL) { /* proto with debug information? */
304 int i;
305 TValue v;
306 setbtvalue(&v); /* boolean 'true' to be the value of all indices */
307 if (!(isvararg(p))) /* regular function? */
308 i = 0; /* consider all instructions */
309 else { /* vararg function */
310 lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
311 currentline = nextline(p, currentline, 0);
312 i = 1; /* skip first instruction (OP_VARARGPREP) */
313 }
314 for (; i < p->sizelineinfo; i++) { /* for each instruction */
315 currentline = nextline(p, currentline, i); /* get its line */
316 luaH_setint(L, t, currentline, &v); /* table[line] = true */
317 }
318 }
319 }
320}
321
322
323static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
324 /* calling function is a known function? */
325 if (ci != NULL && !(ci->callstatus & CIST_TAIL))
326 return funcnamefromcall(L, ci->previous, name);
327 else return NULL; /* no way to find a name */
328}
329
330
331static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
332 Closure *f, CallInfo *ci) {
333 int status = 1;
334 for (; *what; what++) {
335 switch (*what) {
336 case 'S': {
337 funcinfo(ar, f);
338 break;
339 }
340 case 'l': {
341 ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
342 break;
343 }
344 case 'u': {
345 ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
346 if (!LuaClosure(f)) {
347 ar->isvararg = 1;
348 ar->nparams = 0;
349 }
350 else {
351 ar->isvararg = (isvararg(f->l.p)) ? 1 : 0;
352 ar->nparams = f->l.p->numparams;
353 }
354 break;
355 }
356 case 't': {
357 if (ci != NULL) {
358 ar->istailcall = !!(ci->callstatus & CIST_TAIL);
359 ar->extraargs =
360 cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT);
361 }
362 else {
363 ar->istailcall = 0;
364 ar->extraargs = 0;
365 }
366 break;
367 }
368 case 'n': {
369 ar->namewhat = getfuncname(L, ci, &ar->name);
370 if (ar->namewhat == NULL) {
371 ar->namewhat = ""; /* not found */
372 ar->name = NULL;
373 }
374 break;
375 }
376 case 'r': {
377 if (ci == NULL || !(ci->callstatus & CIST_HOOKED))
378 ar->ftransfer = ar->ntransfer = 0;
379 else {
380 ar->ftransfer = L->transferinfo.ftransfer;
381 ar->ntransfer = L->transferinfo.ntransfer;
382 }
383 break;
384 }
385 case 'L':
386 case 'f': /* handled by lua_getinfo */
387 break;
388 default: status = 0; /* invalid option */
389 }
390 }
391 return status;
392}
393
394
395LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
396 int status;
397 Closure *cl;
398 CallInfo *ci;
399 TValue *func;
400 lua_lock(L);
401 if (*what == '>') {
402 ci = NULL;
403 func = s2v(L->top.p - 1);
404 api_check(L, ttisfunction(func), "function expected");
405 what++; /* skip the '>' */
406 L->top.p--; /* pop function */
407 }
408 else {
409 ci = ar->i_ci;
410 func = s2v(ci->func.p);
411 lua_assert(ttisfunction(func));
412 }
413 cl = ttisclosure(func) ? clvalue(func) : NULL;
414 status = auxgetinfo(L, what, ar, cl, ci);
415 if (strchr(what, 'f')) {
416 setobj2s(L, L->top.p, func);
417 api_incr_top(L);
418 }
419 if (strchr(what, 'L'))
420 collectvalidlines(L, cl);
421 lua_unlock(L);
422 return status;
423}
424
425
426/*
427** {======================================================
428** Symbolic Execution
429** =======================================================
430*/
431
432
433static int filterpc (int pc, int jmptarget) {
434 if (pc < jmptarget) /* is code conditional (inside a jump)? */
435 return -1; /* cannot know who sets that register */
436 else return pc; /* current position sets that register */
437}
438
439
440/*
441** Try to find last instruction before 'lastpc' that modified register 'reg'.
442*/
443static int findsetreg (const Proto *p, int lastpc, int reg) {
444 int pc;
445 int setreg = -1; /* keep last instruction that changed 'reg' */
446 int jmptarget = 0; /* any code before this address is conditional */
447 if (testMMMode(GET_OPCODE(p->code[lastpc])))
448 lastpc--; /* previous instruction was not actually executed */
449 for (pc = 0; pc < lastpc; pc++) {
450 Instruction i = p->code[pc];
451 OpCode op = GET_OPCODE(i);
452 int a = GETARG_A(i);
453 int change; /* true if current instruction changed 'reg' */
454 switch (op) {
455 case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */
456 int b = GETARG_B(i);
457 change = (a <= reg && reg <= a + b);
458 break;
459 }
460 case OP_TFORCALL: { /* affect all regs above its base */
461 change = (reg >= a + 2);
462 break;
463 }
464 case OP_CALL:
465 case OP_TAILCALL: { /* affect all registers above base */
466 change = (reg >= a);
467 break;
468 }
469 case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */
470 int b = GETARG_sJ(i);
471 int dest = pc + 1 + b;
472 /* jump does not skip 'lastpc' and is larger than current one? */
473 if (dest <= lastpc && dest > jmptarget)
474 jmptarget = dest; /* update 'jmptarget' */
475 change = 0;
476 break;
477 }
478 default: /* any instruction that sets A */
479 change = (testAMode(op) && reg == a);
480 break;
481 }
482 if (change)
483 setreg = filterpc(pc, jmptarget);
484 }
485 return setreg;
486}
487
488
489/*
490** Find a "name" for the constant 'c'.
491*/
492static const char *kname (const Proto *p, int index, const char **name) {
493 TValue *kvalue = &p->k[index];
494 if (ttisstring(kvalue)) {
495 *name = getstr(tsvalue(kvalue));
496 return "constant";
497 }
498 else {
499 *name = "?";
500 return NULL;
501 }
502}
503
504
505static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
506 const char **name) {
507 int pc = *ppc;
508 *name = luaF_getlocalname(p, cast_uint(reg + 1), pc);
509 if (*name) /* is a local? */
510 return strlocal;
511 /* else try symbolic execution */
512 *ppc = pc = findsetreg(p, pc, reg);
513 if (pc != -1) { /* could find instruction? */
514 Instruction i = p->code[pc];
515 OpCode op = GET_OPCODE(i);
516 switch (op) {
517 case OP_MOVE: {
518 int b = GETARG_B(i); /* move from 'b' to 'a' */
519 if (b < GETARG_A(i))
520 return basicgetobjname(p, ppc, b, name); /* get name for 'b' */
521 break;
522 }
523 case OP_GETUPVAL: {
524 *name = upvalname(p, GETARG_B(i));
525 return strupval;
526 }
527 case OP_LOADK: return kname(p, GETARG_Bx(i), name);
528 case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
529 default: break;
530 }
531 }
532 return NULL; /* could not find reasonable name */
533}
534
535
536/*
537** Find a "name" for the register 'c'.
538*/
539static void rname (const Proto *p, int pc, int c, const char **name) {
540 const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
541 if (!(what && *what == 'c')) /* did not find a constant name? */
542 *name = "?";
543}
544
545
546/*
547** Check whether table being indexed by instruction 'i' is the
548** environment '_ENV'
549*/
550static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
551 int t = GETARG_B(i); /* table index */
552 const char *name; /* name of indexed variable */
553 if (isup) /* is 't' an upvalue? */
554 name = upvalname(p, t);
555 else { /* 't' is a register */
556 const char *what = basicgetobjname(p, &pc, t, &name);
557 /* 'name' must be the name of a local variable (at the current
558 level or an upvalue) */
559 if (what != strlocal && what != strupval)
560 name = NULL; /* cannot be the variable _ENV */
561 }
562 return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
563}
564
565
566/*
567** Extend 'basicgetobjname' to handle table accesses
568*/
569static const char *getobjname (const Proto *p, int lastpc, int reg,
570 const char **name) {
571 const char *kind = basicgetobjname(p, &lastpc, reg, name);
572 if (kind != NULL)
573 return kind;
574 else if (lastpc != -1) { /* could find instruction? */
575 Instruction i = p->code[lastpc];
576 OpCode op = GET_OPCODE(i);
577 switch (op) {
578 case OP_GETTABUP: {
579 int k = GETARG_C(i); /* key index */
580 kname(p, k, name);
581 return isEnv(p, lastpc, i, 1);
582 }
583 case OP_GETTABLE: case OP_GETVARG: {
584 int k = GETARG_C(i); /* key index */
585 rname(p, lastpc, k, name);
586 return isEnv(p, lastpc, i, 0);
587 }
588 case OP_GETI: {
589 *name = "integer index";
590 return "field";
591 }
592 case OP_GETFIELD: {
593 int k = GETARG_C(i); /* key index */
594 kname(p, k, name);
595 return isEnv(p, lastpc, i, 0);
596 }
597 case OP_SELF: {
598 int k = GETARG_C(i); /* key index */
599 kname(p, k, name);
600 return "method";
601 }
602 default: break; /* go through to return NULL */
603 }
604 }
605 return NULL; /* could not find reasonable name */
606}
607
608
609/*
610** Try to find a name for a function based on the code that called it.
611** (Only works when function was called by a Lua function.)
612** Returns what the name is (e.g., "for iterator", "method",
613** "metamethod") and sets '*name' to point to the name.
614*/
615static const char *funcnamefromcode (lua_State *L, const Proto *p,
616 int pc, const char **name) {
617 TMS tm = (TMS)0; /* (initial value avoids warnings) */
618 Instruction i = p->code[pc]; /* calling instruction */
619 switch (GET_OPCODE(i)) {
620 case OP_CALL:
621 case OP_TAILCALL:
622 return getobjname(p, pc, GETARG_A(i), name); /* get function name */
623 case OP_TFORCALL: { /* for iterator */
624 *name = "for iterator";
625 return "for iterator";
626 }
627 /* other instructions can do calls through metamethods */
628 case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
629 case OP_GETI: case OP_GETFIELD:
630 tm = TM_INDEX;
631 break;
632 case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
633 tm = TM_NEWINDEX;
634 break;
635 case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
636 tm = cast(TMS, GETARG_C(i));
637 break;
638 }
639 case OP_UNM: tm = TM_UNM; break;
640 case OP_BNOT: tm = TM_BNOT; break;
641 case OP_LEN: tm = TM_LEN; break;
642 case OP_CONCAT: tm = TM_CONCAT; break;
643 case OP_EQ: tm = TM_EQ; break;
644 /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
645 case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
646 case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
647 case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
648 default:
649 return NULL; /* cannot find a reasonable name */
650 }
651 *name = getshrstr(G(L)->tmname[tm]) + 2;
652 return "metamethod";
653}
654
655
656/*
657** Try to find a name for a function based on how it was called.
658*/
659static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
660 const char **name) {
661 if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
662 *name = "?";
663 return "hook";
664 }
665 else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */
666 *name = "__gc";
667 return "metamethod"; /* report it as such */
668 }
669 else if (isLua(ci))
670 return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
671 else
672 return NULL;
673}
674
675/* }====================================================== */
676
677
678
679/*
680** Check whether pointer 'o' points to some value in the stack frame of
681** the current function and, if so, returns its index. Because 'o' may
682** not point to a value in this stack, we cannot compare it with the
683** region boundaries (undefined behavior in ISO C).
684*/
685static int instack (CallInfo *ci, const TValue *o) {
686 int pos;
687 StkId base = ci->func.p + 1;
688 for (pos = 0; base + pos < ci->top.p; pos++) {
689 if (o == s2v(base + pos))
690 return pos;
691 }
692 return -1; /* not found */
693}
694
695
696/*
697** Checks whether value 'o' came from an upvalue. (That can only happen
698** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
699** upvalues.)
700*/
701static const char *getupvalname (CallInfo *ci, const TValue *o,
702 const char **name) {
703 LClosure *c = ci_func(ci);
704 int i;
705 for (i = 0; i < c->nupvalues; i++) {
706 if (c->upvals[i]->v.p == o) {
707 *name = upvalname(c->p, i);
708 return strupval;
709 }
710 }
711 return NULL;
712}
713
714
715static const char *formatvarinfo (lua_State *L, const char *kind,
716 const char *name) {
717 if (kind == NULL)
718 return ""; /* no information */
719 else
720 return luaO_pushfstring(L, " (%s '%s')", kind, name);
721}
722
723/*
724** Build a string with a "description" for the value 'o', such as
725** "variable 'x'" or "upvalue 'y'".
726*/
727static const char *varinfo (lua_State *L, const TValue *o) {
728 CallInfo *ci = L->ci;
729 const char *name = NULL; /* to avoid warnings */
730 const char *kind = NULL;
731 if (isLua(ci)) {
732 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
733 if (!kind) { /* not an upvalue? */
734 int reg = instack(ci, o); /* try a register */
735 if (reg >= 0) /* is 'o' a register? */
736 kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
737 }
738 }
739 return formatvarinfo(L, kind, name);
740}
741
742
743/*
744** Raise a type error
745*/
746static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
747 const char *extra) {
748 const char *t = luaT_objtypename(L, o);
749 luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
750}
751
752
753/*
754** Raise a type error with "standard" information about the faulty
755** object 'o' (using 'varinfo').
756*/
757l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
758 typeerror(L, o, op, varinfo(L, o));
759}
760
761
762/*
763** Raise an error for calling a non-callable object. Try to find a name
764** for the object based on how it was called ('funcnamefromcall'); if it
765** cannot get a name there, try 'varinfo'.
766*/
767l_noret luaG_callerror (lua_State *L, const TValue *o) {
768 CallInfo *ci = L->ci;
769 const char *name = NULL; /* to avoid warnings */
770 const char *kind = funcnamefromcall(L, ci, &name);
771 const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
772 typeerror(L, o, "call", extra);
773}
774
775
776l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
777 luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
778 what, luaT_objtypename(L, o));
779}
780
781
782l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
783 if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
784 luaG_typeerror(L, p1, "concatenate");
785}
786
787
788l_noret luaG_opinterror (lua_State *L, const TValue *p1,
789 const TValue *p2, const char *msg) {
790 if (!ttisnumber(p1)) /* first operand is wrong? */
791 p2 = p1; /* now second is wrong */
792 luaG_typeerror(L, p2, msg);
793}
794
795
796/*
797** Error when both values are convertible to numbers, but not to integers
798*/
799l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
800 lua_Integer temp;
801 if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
802 p2 = p1;
803 luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
804}
805
806
807l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
808 const char *t1 = luaT_objtypename(L, p1);
809 const char *t2 = luaT_objtypename(L, p2);
810 if (strcmp(t1, t2) == 0)
811 luaG_runerror(L, "attempt to compare two %s values", t1);
812 else
813 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
814}
815
816
817l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k) {
818 const char *globalname = "?"; /* default name if k == 0 */
819 if (k > 0)
820 kname(cl->p, k - 1, &globalname);
821 luaG_runerror(L, "global '%s' already defined", globalname);
822}
823
824
825/* add src:line information to 'msg' */
826const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
827 int line) {
828 if (src == NULL) /* no debug information? */
829 return luaO_pushfstring(L, "?:?: %s", msg);
830 else {
831 char buff[LUA_IDSIZE];
832 size_t idlen;
833 const char *id = getlstr(src, idlen);
834 luaO_chunkid(buff, id, idlen);
835 return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
836 }
837}
838
839
840l_noret luaG_errormsg (lua_State *L) {
841 if (L->errfunc != 0) { /* is there an error handling function? */
842 StkId errfunc = restorestack(L, L->errfunc);
843 lua_assert(ttisfunction(s2v(errfunc)));
844 setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */
845 setobjs2s(L, L->top.p - 1, errfunc); /* push function */
846 L->top.p++; /* assume EXTRA_STACK */
847 luaD_callnoyield(L, L->top.p - 2, 1); /* call it */
848 }
849 if (ttisnil(s2v(L->top.p - 1))) { /* error object is nil? */
850 /* change it to a proper message */
851 setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "<no error object>"));
852 }
853 luaD_throw(L, LUA_ERRRUN);
854}
855
856
857l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
858 CallInfo *ci = L->ci;
859 const char *msg;
860 va_list argp;
861 luaC_checkGC(L); /* error message uses memory */
862 pushvfstring(L, argp, fmt, msg);
863 if (isLua(ci)) { /* Lua function? */
864 /* add source:line information */
865 luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
866 setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */
867 L->top.p--;
868 }
869 luaG_errormsg(L);
870}
871
872
873/*
874** Check whether new instruction 'newpc' is in a different line from
875** previous instruction 'oldpc'. More often than not, 'newpc' is only
876** one or a few instructions after 'oldpc' (it must be after, see
877** caller), so try to avoid calling 'luaG_getfuncline'. If they are
878** too far apart, there is a good chance of a ABSLINEINFO in the way,
879** so it goes directly to 'luaG_getfuncline'.
880*/
881static int changedline (const Proto *p, int oldpc, int newpc) {
882 if (p->lineinfo == NULL) /* no debug information? */
883 return 0;
884 if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */
885 int delta = 0; /* line difference */
886 int pc = oldpc;
887 for (;;) {
888 int lineinfo = p->lineinfo[++pc];
889 if (lineinfo == ABSLINEINFO)
890 break; /* cannot compute delta; fall through */
891 delta += lineinfo;
892 if (pc == newpc)
893 return (delta != 0); /* delta computed successfully */
894 }
895 }
896 /* either instructions are too far apart or there is an absolute line
897 info in the way; compute line difference explicitly */
898 return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
899}
900
901
902/*
903** Traces Lua calls. If code is running the first instruction of a function,
904** and function is not vararg, and it is not coming from an yield,
905** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall'
906** after adjusting its variable arguments; otherwise, they could call
907** a line/count hook before the call hook. Functions coming from
908** an yield already called 'luaD_hookcall' before yielding.)
909*/
910int luaG_tracecall (lua_State *L) {
911 CallInfo *ci = L->ci;
912 Proto *p = ci_func(ci)->p;
913 ci->u.l.trap = 1; /* ensure hooks will be checked */
914 if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */
915 if (isvararg(p))
916 return 0; /* hooks will start at VARARGPREP instruction */
917 else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */
918 luaD_hookcall(L, ci); /* check 'call' hook */
919 }
920 return 1; /* keep 'trap' on */
921}
922
923
924/*
925** Traces the execution of a Lua function. Called before the execution
926** of each opcode, when debug is on. 'L->oldpc' stores the last
927** instruction traced, to detect line changes. When entering a new
928** function, 'npci' will be zero and will test as a new line whatever
929** the value of 'oldpc'. Some exceptional conditions may return to
930** a function without setting 'oldpc'. In that case, 'oldpc' may be
931** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
932** at most causes an extra call to a line hook.)
933** This function is not "Protected" when called, so it should correct
934** 'L->top.p' before calling anything that can run the GC.
935*/
936int luaG_traceexec (lua_State *L, const Instruction *pc) {
937 CallInfo *ci = L->ci;
938 lu_byte mask = cast_byte(L->hookmask);
939 const Proto *p = ci_func(ci)->p;
940 int counthook;
941 if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
942 ci->u.l.trap = 0; /* don't need to stop again */
943 return 0; /* turn off 'trap' */
944 }
945 pc++; /* reference is always next instruction */
946 ci->u.l.savedpc = pc; /* save 'pc' */
947 counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
948 if (counthook)
949 resethookcount(L); /* reset count */
950 else if (!(mask & LUA_MASKLINE))
951 return 1; /* no line hook and count != 0; nothing to be done now */
952 if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */
953 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
954 return 1; /* do not call hook again (VM yielded, so it did not move) */
955 }
956 if (!luaP_isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
957 L->top.p = ci->top.p; /* correct top */
958 if (counthook)
959 luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
960 if (mask & LUA_MASKLINE) {
961 /* 'L->oldpc' may be invalid; use zero in this case */
962 int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
963 int npci = pcRel(pc, p);
964 if (npci <= oldpc || /* call hook when jump back (loop), */
965 changedline(p, oldpc, npci)) { /* or when enter new line */
966 int newline = luaG_getfuncline(p, npci);
967 luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
968 }
969 L->oldpc = npci; /* 'pc' of last call to line hook */
970 }
971 if (L->status == LUA_YIELD) { /* did hook yield? */
972 if (counthook)
973 L->hookcount = 1; /* undo decrement to zero */
974 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
975 luaD_throw(L, LUA_YIELD);
976 }
977 return 1; /* keep 'trap' on */
978}
979