2 ** $Id: ldebug.c,v 2.29a 2005/12/22 16:19:56 roberto Exp $
4 ** See Copyright Notice in lua.h
33 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
);
36 static int currentpc (lua_State
*L
, CallInfo
*ci
) {
37 if (!isLua(ci
)) return -1; /* function is not a Lua function? */
39 ci
->savedpc
= L
->savedpc
;
40 return pcRel(ci
->savedpc
, ci_func(ci
)->l
.p
);
44 static int currentline (lua_State
*L
, CallInfo
*ci
) {
45 int pc
= currentpc(L
, ci
);
47 return -1; /* only active lua functions have current-line information */
49 return getline(ci_func(ci
)->l
.p
, pc
);
54 ** this function can be called asynchronous (e.g. during a signal)
56 LUA_API
int lua_sethook (lua_State
*L
, lua_Hook func
, int mask
, int count
) {
57 if (func
== NULL
|| mask
== 0) { /* turn off hooks? */
62 L
->basehookcount
= count
;
64 L
->hookmask
= cast_byte(mask
);
69 LUA_API lua_Hook
lua_gethook (lua_State
*L
) {
74 LUA_API
int lua_gethookmask (lua_State
*L
) {
79 LUA_API
int lua_gethookcount (lua_State
*L
) {
80 return L
->basehookcount
;
84 LUA_API
int lua_getstack (lua_State
*L
, int level
, lua_Debug
*ar
) {
88 for (ci
= L
->ci
; level
> 0 && ci
> L
->base_ci
; ci
--) {
90 if (f_isLua(ci
)) /* Lua function? */
91 level
-= ci
->tailcalls
; /* skip lost tail calls */
93 if (level
== 0 && ci
> L
->base_ci
) { /* level found? */
95 ar
->i_ci
= cast_int(ci
- L
->base_ci
);
97 else if (level
< 0) { /* level is of a lost tail call? */
101 else status
= 0; /* no such level */
107 static Proto
*getluaproto (CallInfo
*ci
) {
108 return (isLua(ci
) ? ci_func(ci
)->l
.p
: NULL
);
112 static const char *findlocal (lua_State
*L
, CallInfo
*ci
, int n
) {
114 Proto
*fp
= getluaproto(ci
);
115 if (fp
&& (name
= luaF_getlocalname(fp
, n
, currentpc(L
, ci
))) != NULL
)
116 return name
; /* is a local variable in a Lua function */
118 StkId limit
= (ci
== L
->ci
) ? L
->top
: (ci
+1)->func
;
119 if (limit
- ci
->base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
120 return "(*temporary)";
127 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
128 CallInfo
*ci
= L
->base_ci
+ ar
->i_ci
;
129 const char *name
= findlocal(L
, ci
, n
);
132 luaA_pushobject(L
, ci
->base
+ (n
- 1));
138 LUA_API
const char *lua_setlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
139 CallInfo
*ci
= L
->base_ci
+ ar
->i_ci
;
140 const char *name
= findlocal(L
, ci
, n
);
143 setobjs2s(L
, ci
->base
+ (n
- 1), L
->top
- 1);
144 L
->top
--; /* pop value */
150 static void funcinfo (lua_Debug
*ar
, Closure
*cl
) {
153 ar
->linedefined
= -1;
154 ar
->lastlinedefined
= -1;
158 ar
->source
= getstr(cl
->l
.p
->source
);
159 ar
->linedefined
= cl
->l
.p
->linedefined
;
160 ar
->lastlinedefined
= cl
->l
.p
->lastlinedefined
;
161 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
163 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
167 static void info_tailcall (lua_Debug
*ar
) {
168 ar
->name
= ar
->namewhat
= "";
170 ar
->lastlinedefined
= ar
->linedefined
= ar
->currentline
= -1;
171 ar
->source
= "=(tail call)";
172 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
177 static void collectvalidlines (lua_State
*L
, Closure
*f
) {
178 if (f
== NULL
|| f
->c
.isC
) {
182 Table
*t
= luaH_new(L
, 0, 0);
183 int *lineinfo
= f
->l
.p
->lineinfo
;
185 for (i
=0; i
<f
->l
.p
->sizelineinfo
; i
++)
186 setbvalue(luaH_setnum(L
, t
, lineinfo
[i
]), 1);
187 sethvalue(L
, L
->top
, t
);
193 static int auxgetinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
,
194 Closure
*f
, CallInfo
*ci
) {
200 for (; *what
; what
++) {
207 ar
->currentline
= (ci
) ? currentline(L
, ci
) : -1;
211 ar
->nups
= f
->c
.nupvalues
;
215 ar
->namewhat
= (ci
) ? getfuncname(L
, ci
, &ar
->name
) : NULL
;
216 if (ar
->namewhat
== NULL
) {
217 ar
->namewhat
= ""; /* not found */
223 case 'f': /* handled by lua_getinfo */
225 default: status
= 0; /* invalid option */
232 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
) {
238 StkId func
= L
->top
- 1;
239 luai_apicheck(L
, ttisfunction(func
));
240 what
++; /* skip the '>' */
242 L
->top
--; /* pop function */
244 else if (ar
->i_ci
!= 0) { /* no tail call? */
245 ci
= L
->base_ci
+ ar
->i_ci
;
246 lua_assert(ttisfunction(ci
->func
));
247 f
= clvalue(ci
->func
);
249 status
= auxgetinfo(L
, what
, ar
, f
, ci
);
250 if (strchr(what
, 'f')) {
251 if (f
== NULL
) setnilvalue(L
->top
);
252 else setclvalue(L
, L
->top
, f
);
255 if (strchr(what
, 'L'))
256 collectvalidlines(L
, f
);
263 ** {======================================================
264 ** Symbolic Execution and code checker
265 ** =======================================================
268 #define check(x) if (!(x)) return 0;
270 #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
272 #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
276 static int precheck (const Proto
*pt
) {
277 check(pt
->maxstacksize
<= MAXSTACK
);
278 lua_assert(pt
->numparams
+(pt
->is_vararg
& VARARG_HASARG
) <= pt
->maxstacksize
);
279 lua_assert(!(pt
->is_vararg
& VARARG_NEEDSARG
) ||
280 (pt
->is_vararg
& VARARG_HASARG
));
281 check(pt
->sizeupvalues
<= pt
->nups
);
282 check(pt
->sizelineinfo
== pt
->sizecode
|| pt
->sizelineinfo
== 0);
283 check(GET_OPCODE(pt
->code
[pt
->sizecode
-1]) == OP_RETURN
);
288 #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
290 int luaG_checkopenop (Instruction i
) {
291 switch (GET_OPCODE(i
)) {
296 check(GETARG_B(i
) == 0);
299 default: return 0; /* invalid instruction after an open call */
304 static int checkArgMode (const Proto
*pt
, int r
, enum OpArgMask mode
) {
306 case OpArgN
: check(r
== 0); break;
308 case OpArgR
: checkreg(pt
, r
); break;
310 check(ISK(r
) ? INDEXK(r
) < pt
->sizek
: r
< pt
->maxstacksize
);
317 static Instruction
symbexec (const Proto
*pt
, int lastpc
, int reg
) {
319 int last
; /* stores position of last instruction that changed `reg' */
320 last
= pt
->sizecode
-1; /* points to final return (a `neutral' instruction) */
322 for (pc
= 0; pc
< lastpc
; pc
++) {
323 Instruction i
= pt
->code
[pc
];
324 OpCode op
= GET_OPCODE(i
);
328 check(op
< NUM_OPCODES
);
330 switch (getOpMode(op
)) {
334 check(checkArgMode(pt
, b
, getBMode(op
)));
335 check(checkArgMode(pt
, c
, getCMode(op
)));
340 if (getBMode(op
) == OpArgK
) check(b
< pt
->sizek
);
345 if (getBMode(op
) == OpArgR
) {
347 check(0 <= dest
&& dest
< pt
->sizecode
);
349 /* cannot jump to a setlist count */
350 Instruction d
= pt
->code
[dest
-1];
351 check(!(GET_OPCODE(d
) == OP_SETLIST
&& GETARG_C(d
) == 0));
358 if (a
== reg
) last
= pc
; /* change register `a' */
361 check(pc
+2 < pt
->sizecode
); /* check skip */
362 check(GET_OPCODE(pt
->code
[pc
+1]) == OP_JMP
);
366 check(c
== 0 || pc
+2 < pt
->sizecode
); /* check its jump */
370 if (a
<= reg
&& reg
<= b
)
371 last
= pc
; /* set registers from `a' to `b' */
381 check(ttisstring(&pt
->k
[b
]));
386 if (reg
== a
+1) last
= pc
;
390 check(b
< c
); /* at least two operands */
394 check(c
>= 1); /* at least one result (control variable) */
395 checkreg(pt
, a
+2+c
); /* space for results */
396 if (reg
>= a
+2) last
= pc
; /* affect all regs above its base */
405 /* not full check and jump is forward and do not skip `lastpc'? */
406 if (reg
!= NO_REG
&& pc
< dest
&& dest
<= lastpc
)
407 pc
+= b
; /* do the jump */
415 c
--; /* c = num. returns */
416 if (c
== LUA_MULTRET
) {
417 check(checkopenop(pt
, pc
));
421 if (reg
>= a
) last
= pc
; /* affect all registers above base */
425 b
--; /* b = num. returns */
426 if (b
> 0) checkreg(pt
, a
+b
-1);
430 if (b
> 0) checkreg(pt
, a
+ b
);
436 check(b
< pt
->sizep
);
437 nup
= pt
->p
[b
]->nups
;
438 check(pc
+ nup
< pt
->sizecode
);
439 for (j
= 1; j
<= nup
; j
++) {
440 OpCode op1
= GET_OPCODE(pt
->code
[pc
+ j
]);
441 check(op1
== OP_GETUPVAL
|| op1
== OP_MOVE
);
443 if (reg
!= NO_REG
) /* tracing? */
444 pc
+= nup
; /* do not 'execute' these pseudo-instructions */
448 check((pt
->is_vararg
& VARARG_ISVARARG
) &&
449 !(pt
->is_vararg
& VARARG_NEEDSARG
));
451 if (b
== LUA_MULTRET
) check(checkopenop(pt
, pc
));
458 return pt
->code
[last
];
465 /* }====================================================== */
468 int luaG_checkcode (const Proto
*pt
) {
469 return (symbexec(pt
, pt
->sizecode
, NO_REG
) != 0);
473 static const char *kname (Proto
*p
, int c
) {
474 if (ISK(c
) && ttisstring(&p
->k
[INDEXK(c
)]))
475 return svalue(&p
->k
[INDEXK(c
)]);
481 static const char *getobjname (lua_State
*L
, CallInfo
*ci
, int stackpos
,
483 if (isLua(ci
)) { /* a Lua function? */
484 Proto
*p
= ci_func(ci
)->l
.p
;
485 int pc
= currentpc(L
, ci
);
487 *name
= luaF_getlocalname(p
, stackpos
+1, pc
);
488 if (*name
) /* is a local? */
490 i
= symbexec(p
, pc
, stackpos
); /* try symbolic execution */
491 lua_assert(pc
!= -1);
492 switch (GET_OPCODE(i
)) {
494 int g
= GETARG_Bx(i
); /* global index */
495 lua_assert(ttisstring(&p
->k
[g
]));
496 *name
= svalue(&p
->k
[g
]);
501 int b
= GETARG_B(i
); /* move from `b' to `a' */
503 return getobjname(L
, ci
, b
, name
); /* get name for `b' */
507 int k
= GETARG_C(i
); /* key index */
512 int u
= GETARG_B(i
); /* upvalue index */
513 *name
= p
->upvalues
? getstr(p
->upvalues
[u
]) : "?";
517 int k
= GETARG_C(i
); /* key index */
524 return NULL
; /* no useful name found */
528 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
530 if ((isLua(ci
) && ci
->tailcalls
> 0) || !isLua(ci
- 1))
531 return NULL
; /* calling function is not Lua (or is unknown) */
532 ci
--; /* calling function */
533 i
= ci_func(ci
)->l
.p
->code
[currentpc(L
, ci
)];
534 if (GET_OPCODE(i
) == OP_CALL
|| GET_OPCODE(i
) == OP_TAILCALL
||
535 GET_OPCODE(i
) == OP_TFORLOOP
)
536 return getobjname(L
, ci
, GETARG_A(i
), name
);
538 return NULL
; /* no useful name can be found */
542 /* only ANSI way to check whether a pointer points to an array */
543 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
545 for (p
= ci
->base
; p
< ci
->top
; p
++)
546 if (o
== p
) return 1;
551 void luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
552 const char *name
= NULL
;
553 const char *t
= luaT_typenames
[ttype(o
)];
554 const char *kind
= (isinstack(L
->ci
, o
)) ?
555 getobjname(L
, L
->ci
, cast_int(o
- L
->base
), &name
) :
558 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
561 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
565 void luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
566 if (ttisstring(p1
)) p1
= p2
;
567 lua_assert(!ttisstring(p1
));
568 luaG_typeerror(L
, p1
, "concatenate");
572 void luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
574 if (luaV_tonumber(p1
, &temp
) == NULL
)
575 p2
= p1
; /* first operand is wrong */
576 luaG_typeerror(L
, p2
, "perform arithmetic on");
580 int luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
581 const char *t1
= luaT_typenames
[ttype(p1
)];
582 const char *t2
= luaT_typenames
[ttype(p2
)];
584 luaG_runerror(L
, "attempt to compare two %s values", t1
);
586 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
591 static void addinfo (lua_State
*L
, const char *msg
) {
592 CallInfo
*ci
= L
->ci
;
593 if (isLua(ci
)) { /* is Lua code? */
594 char buff
[LUA_IDSIZE
]; /* add file:line information */
595 int line
= currentline(L
, ci
);
596 luaO_chunkid(buff
, getstr(getluaproto(ci
)->source
), LUA_IDSIZE
);
597 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
602 void luaG_errormsg (lua_State
*L
) {
603 if (L
->errfunc
!= 0) { /* is there an error handling function? */
604 StkId errfunc
= restorestack(L
, L
->errfunc
);
605 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
606 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
607 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
609 luaD_call(L
, L
->top
- 2, 1); /* call it */
611 luaD_throw(L
, LUA_ERRRUN
);
615 void luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
618 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));