]>
git.saurik.com Git - redis.git/blob - deps/lua/src/ldebug.c
2 ** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 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 check(pt
->numparams
+(pt
->is_vararg
& VARARG_HASARG
) <= pt
->maxstacksize
);
279 check(!(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(pt
->sizecode
> 0 && 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
);
350 /* check that it does not jump to a setlist count; this
351 is tricky, because the count from a previous setlist may
352 have the same value of an invalid setlist; so, we must
353 go all the way back to the first of them (if any) */
354 for (j
= 0; j
< dest
; j
++) {
355 Instruction d
= pt
->code
[dest
-1-j
];
356 if (!(GET_OPCODE(d
) == OP_SETLIST
&& GETARG_C(d
) == 0)) break;
358 /* if 'j' is even, previous value is not a setlist (even if
359 it looks like one) */
367 if (a
== reg
) last
= pc
; /* change register `a' */
370 check(pc
+2 < pt
->sizecode
); /* check skip */
371 check(GET_OPCODE(pt
->code
[pc
+1]) == OP_JMP
);
375 if (c
== 1) { /* does it jump? */
376 check(pc
+2 < pt
->sizecode
); /* check its jump */
377 check(GET_OPCODE(pt
->code
[pc
+1]) != OP_SETLIST
||
378 GETARG_C(pt
->code
[pc
+1]) != 0);
383 if (a
<= reg
&& reg
<= b
)
384 last
= pc
; /* set registers from `a' to `b' */
394 check(ttisstring(&pt
->k
[b
]));
399 if (reg
== a
+1) last
= pc
;
403 check(b
< c
); /* at least two operands */
407 check(c
>= 1); /* at least one result (control variable) */
408 checkreg(pt
, a
+2+c
); /* space for results */
409 if (reg
>= a
+2) last
= pc
; /* affect all regs above its base */
418 /* not full check and jump is forward and do not skip `lastpc'? */
419 if (reg
!= NO_REG
&& pc
< dest
&& dest
<= lastpc
)
420 pc
+= b
; /* do the jump */
428 c
--; /* c = num. returns */
429 if (c
== LUA_MULTRET
) {
430 check(checkopenop(pt
, pc
));
434 if (reg
>= a
) last
= pc
; /* affect all registers above base */
438 b
--; /* b = num. returns */
439 if (b
> 0) checkreg(pt
, a
+b
-1);
443 if (b
> 0) checkreg(pt
, a
+ b
);
446 check(pc
< pt
->sizecode
- 1);
452 check(b
< pt
->sizep
);
453 nup
= pt
->p
[b
]->nups
;
454 check(pc
+ nup
< pt
->sizecode
);
455 for (j
= 1; j
<= nup
; j
++) {
456 OpCode op1
= GET_OPCODE(pt
->code
[pc
+ j
]);
457 check(op1
== OP_GETUPVAL
|| op1
== OP_MOVE
);
459 if (reg
!= NO_REG
) /* tracing? */
460 pc
+= nup
; /* do not 'execute' these pseudo-instructions */
464 check((pt
->is_vararg
& VARARG_ISVARARG
) &&
465 !(pt
->is_vararg
& VARARG_NEEDSARG
));
467 if (b
== LUA_MULTRET
) check(checkopenop(pt
, pc
));
474 return pt
->code
[last
];
481 /* }====================================================== */
484 int luaG_checkcode (const Proto
*pt
) {
485 return (symbexec(pt
, pt
->sizecode
, NO_REG
) != 0);
489 static const char *kname (Proto
*p
, int c
) {
490 if (ISK(c
) && ttisstring(&p
->k
[INDEXK(c
)]))
491 return svalue(&p
->k
[INDEXK(c
)]);
497 static const char *getobjname (lua_State
*L
, CallInfo
*ci
, int stackpos
,
499 if (isLua(ci
)) { /* a Lua function? */
500 Proto
*p
= ci_func(ci
)->l
.p
;
501 int pc
= currentpc(L
, ci
);
503 *name
= luaF_getlocalname(p
, stackpos
+1, pc
);
504 if (*name
) /* is a local? */
506 i
= symbexec(p
, pc
, stackpos
); /* try symbolic execution */
507 lua_assert(pc
!= -1);
508 switch (GET_OPCODE(i
)) {
510 int g
= GETARG_Bx(i
); /* global index */
511 lua_assert(ttisstring(&p
->k
[g
]));
512 *name
= svalue(&p
->k
[g
]);
517 int b
= GETARG_B(i
); /* move from `b' to `a' */
519 return getobjname(L
, ci
, b
, name
); /* get name for `b' */
523 int k
= GETARG_C(i
); /* key index */
528 int u
= GETARG_B(i
); /* upvalue index */
529 *name
= p
->upvalues
? getstr(p
->upvalues
[u
]) : "?";
533 int k
= GETARG_C(i
); /* key index */
540 return NULL
; /* no useful name found */
544 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
546 if ((isLua(ci
) && ci
->tailcalls
> 0) || !isLua(ci
- 1))
547 return NULL
; /* calling function is not Lua (or is unknown) */
548 ci
--; /* calling function */
549 i
= ci_func(ci
)->l
.p
->code
[currentpc(L
, ci
)];
550 if (GET_OPCODE(i
) == OP_CALL
|| GET_OPCODE(i
) == OP_TAILCALL
||
551 GET_OPCODE(i
) == OP_TFORLOOP
)
552 return getobjname(L
, ci
, GETARG_A(i
), name
);
554 return NULL
; /* no useful name can be found */
558 /* only ANSI way to check whether a pointer points to an array */
559 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
561 for (p
= ci
->base
; p
< ci
->top
; p
++)
562 if (o
== p
) return 1;
567 void luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
568 const char *name
= NULL
;
569 const char *t
= luaT_typenames
[ttype(o
)];
570 const char *kind
= (isinstack(L
->ci
, o
)) ?
571 getobjname(L
, L
->ci
, cast_int(o
- L
->base
), &name
) :
574 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
577 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
581 void luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
582 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
583 lua_assert(!ttisstring(p1
) && !ttisnumber(p1
));
584 luaG_typeerror(L
, p1
, "concatenate");
588 void luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
590 if (luaV_tonumber(p1
, &temp
) == NULL
)
591 p2
= p1
; /* first operand is wrong */
592 luaG_typeerror(L
, p2
, "perform arithmetic on");
596 int luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
597 const char *t1
= luaT_typenames
[ttype(p1
)];
598 const char *t2
= luaT_typenames
[ttype(p2
)];
600 luaG_runerror(L
, "attempt to compare two %s values", t1
);
602 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
607 static void addinfo (lua_State
*L
, const char *msg
) {
608 CallInfo
*ci
= L
->ci
;
609 if (isLua(ci
)) { /* is Lua code? */
610 char buff
[LUA_IDSIZE
]; /* add file:line information */
611 int line
= currentline(L
, ci
);
612 luaO_chunkid(buff
, getstr(getluaproto(ci
)->source
), LUA_IDSIZE
);
613 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
618 void luaG_errormsg (lua_State
*L
) {
619 if (L
->errfunc
!= 0) { /* is there an error handling function? */
620 StkId errfunc
= restorestack(L
, L
->errfunc
);
621 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
622 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
623 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
625 luaD_call(L
, L
->top
- 2, 1); /* call it */
627 luaD_throw(L
, LUA_ERRRUN
);
631 void luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
634 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));