1 /* $NetBSD: ldebug.c,v 1.1.1.2 2012/03/15 00:08:08 alnsn Exp $ */
4 ** $Id: ldebug.c,v 1.1.1.2 2012/03/15 00:08:08 alnsn Exp $
6 ** See Copyright Notice in lua.h
35 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
);
38 static int currentpc (lua_State
*L
, CallInfo
*ci
) {
39 if (!isLua(ci
)) return -1; /* function is not a Lua function? */
41 ci
->savedpc
= L
->savedpc
;
42 return pcRel(ci
->savedpc
, ci_func(ci
)->l
.p
);
46 static int currentline (lua_State
*L
, CallInfo
*ci
) {
47 int pc
= currentpc(L
, ci
);
49 return -1; /* only active lua functions have current-line information */
51 return getline(ci_func(ci
)->l
.p
, pc
);
56 ** this function can be called asynchronous (e.g. during a signal)
58 LUA_API
int lua_sethook (lua_State
*L
, lua_Hook func
, int mask
, int count
) {
59 if (func
== NULL
|| mask
== 0) { /* turn off hooks? */
64 L
->basehookcount
= count
;
66 L
->hookmask
= cast_byte(mask
);
71 LUA_API lua_Hook
lua_gethook (lua_State
*L
) {
76 LUA_API
int lua_gethookmask (lua_State
*L
) {
81 LUA_API
int lua_gethookcount (lua_State
*L
) {
82 return L
->basehookcount
;
86 LUA_API
int lua_getstack (lua_State
*L
, int level
, lua_Debug
*ar
) {
90 for (ci
= L
->ci
; level
> 0 && ci
> L
->base_ci
; ci
--) {
92 if (f_isLua(ci
)) /* Lua function? */
93 level
-= ci
->tailcalls
; /* skip lost tail calls */
95 if (level
== 0 && ci
> L
->base_ci
) { /* level found? */
97 ar
->i_ci
= cast_int(ci
- L
->base_ci
);
99 else if (level
< 0) { /* level is of a lost tail call? */
103 else status
= 0; /* no such level */
109 static Proto
*getluaproto (CallInfo
*ci
) {
110 return (isLua(ci
) ? ci_func(ci
)->l
.p
: NULL
);
114 static const char *findlocal (lua_State
*L
, CallInfo
*ci
, int n
) {
116 Proto
*fp
= getluaproto(ci
);
117 if (fp
&& (name
= luaF_getlocalname(fp
, n
, currentpc(L
, ci
))) != NULL
)
118 return name
; /* is a local variable in a Lua function */
120 StkId limit
= (ci
== L
->ci
) ? L
->top
: (ci
+1)->func
;
121 if (limit
- ci
->base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
122 return "(*temporary)";
129 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
130 CallInfo
*ci
= L
->base_ci
+ ar
->i_ci
;
131 const char *name
= findlocal(L
, ci
, n
);
134 luaA_pushobject(L
, ci
->base
+ (n
- 1));
140 LUA_API
const char *lua_setlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
141 CallInfo
*ci
= L
->base_ci
+ ar
->i_ci
;
142 const char *name
= findlocal(L
, ci
, n
);
145 setobjs2s(L
, ci
->base
+ (n
- 1), L
->top
- 1);
146 L
->top
--; /* pop value */
152 static void funcinfo (lua_Debug
*ar
, Closure
*cl
) {
155 ar
->linedefined
= -1;
156 ar
->lastlinedefined
= -1;
160 ar
->source
= getstr(cl
->l
.p
->source
);
161 ar
->linedefined
= cl
->l
.p
->linedefined
;
162 ar
->lastlinedefined
= cl
->l
.p
->lastlinedefined
;
163 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
165 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
169 static void info_tailcall (lua_Debug
*ar
) {
170 ar
->name
= ar
->namewhat
= "";
172 ar
->lastlinedefined
= ar
->linedefined
= ar
->currentline
= -1;
173 ar
->source
= "=(tail call)";
174 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
179 static void collectvalidlines (lua_State
*L
, Closure
*f
) {
180 if (f
== NULL
|| f
->c
.isC
) {
184 Table
*t
= luaH_new(L
, 0, 0);
185 int *lineinfo
= f
->l
.p
->lineinfo
;
187 for (i
=0; i
<f
->l
.p
->sizelineinfo
; i
++)
188 setbvalue(luaH_setnum(L
, t
, lineinfo
[i
]), 1);
189 sethvalue(L
, L
->top
, t
);
195 static int auxgetinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
,
196 Closure
*f
, CallInfo
*ci
) {
202 for (; *what
; what
++) {
209 ar
->currentline
= (ci
) ? currentline(L
, ci
) : -1;
213 ar
->nups
= f
->c
.nupvalues
;
217 ar
->namewhat
= (ci
) ? getfuncname(L
, ci
, &ar
->name
) : NULL
;
218 if (ar
->namewhat
== NULL
) {
219 ar
->namewhat
= ""; /* not found */
225 case 'f': /* handled by lua_getinfo */
227 default: status
= 0; /* invalid option */
234 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
) {
240 StkId func
= L
->top
- 1;
241 luai_apicheck(L
, ttisfunction(func
));
242 what
++; /* skip the '>' */
244 L
->top
--; /* pop function */
246 else if (ar
->i_ci
!= 0) { /* no tail call? */
247 ci
= L
->base_ci
+ ar
->i_ci
;
248 lua_assert(ttisfunction(ci
->func
));
249 f
= clvalue(ci
->func
);
251 status
= auxgetinfo(L
, what
, ar
, f
, ci
);
252 if (strchr(what
, 'f')) {
253 if (f
== NULL
) setnilvalue(L
->top
);
254 else setclvalue(L
, L
->top
, f
);
257 if (strchr(what
, 'L'))
258 collectvalidlines(L
, f
);
265 ** {======================================================
266 ** Symbolic Execution and code checker
267 ** =======================================================
270 #define check(x) if (!(x)) return 0;
272 #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
274 #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
278 static int precheck (const Proto
*pt
) {
279 check(pt
->maxstacksize
<= MAXSTACK
);
280 check(pt
->numparams
+(pt
->is_vararg
& VARARG_HASARG
) <= pt
->maxstacksize
);
281 check(!(pt
->is_vararg
& VARARG_NEEDSARG
) ||
282 (pt
->is_vararg
& VARARG_HASARG
));
283 check(pt
->sizeupvalues
<= pt
->nups
);
284 check(pt
->sizelineinfo
== pt
->sizecode
|| pt
->sizelineinfo
== 0);
285 check(pt
->sizecode
> 0 && GET_OPCODE(pt
->code
[pt
->sizecode
-1]) == OP_RETURN
);
290 #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
292 int luaG_checkopenop (Instruction i
) {
293 switch (GET_OPCODE(i
)) {
298 check(GETARG_B(i
) == 0);
301 default: return 0; /* invalid instruction after an open call */
306 static int checkArgMode (const Proto
*pt
, int r
, enum OpArgMask mode
) {
308 case OpArgN
: check(r
== 0); break;
310 case OpArgR
: checkreg(pt
, r
); break;
312 check(ISK(r
) ? INDEXK(r
) < pt
->sizek
: r
< pt
->maxstacksize
);
319 static Instruction
symbexec (const Proto
*pt
, int lastpc
, int reg
) {
321 int last
; /* stores position of last instruction that changed `reg' */
322 last
= pt
->sizecode
-1; /* points to final return (a `neutral' instruction) */
324 for (pc
= 0; pc
< lastpc
; pc
++) {
325 Instruction i
= pt
->code
[pc
];
326 OpCode op
= GET_OPCODE(i
);
330 check(op
< NUM_OPCODES
);
332 switch (getOpMode(op
)) {
336 check(checkArgMode(pt
, b
, getBMode(op
)));
337 check(checkArgMode(pt
, c
, getCMode(op
)));
342 if (getBMode(op
) == OpArgK
) check(b
< pt
->sizek
);
347 if (getBMode(op
) == OpArgR
) {
349 check(0 <= dest
&& dest
< pt
->sizecode
);
352 /* check that it does not jump to a setlist count; this
353 is tricky, because the count from a previous setlist may
354 have the same value of an invalid setlist; so, we must
355 go all the way back to the first of them (if any) */
356 for (j
= 0; j
< dest
; j
++) {
357 Instruction d
= pt
->code
[dest
-1-j
];
358 if (!(GET_OPCODE(d
) == OP_SETLIST
&& GETARG_C(d
) == 0)) break;
360 /* if 'j' is even, previous value is not a setlist (even if
361 it looks like one) */
369 if (a
== reg
) last
= pc
; /* change register `a' */
372 check(pc
+2 < pt
->sizecode
); /* check skip */
373 check(GET_OPCODE(pt
->code
[pc
+1]) == OP_JMP
);
377 if (c
== 1) { /* does it jump? */
378 check(pc
+2 < pt
->sizecode
); /* check its jump */
379 check(GET_OPCODE(pt
->code
[pc
+1]) != OP_SETLIST
||
380 GETARG_C(pt
->code
[pc
+1]) != 0);
385 if (a
<= reg
&& reg
<= b
)
386 last
= pc
; /* set registers from `a' to `b' */
396 check(ttisstring(&pt
->k
[b
]));
401 if (reg
== a
+1) last
= pc
;
405 check(b
< c
); /* at least two operands */
409 check(c
>= 1); /* at least one result (control variable) */
410 checkreg(pt
, a
+2+c
); /* space for results */
411 if (reg
>= a
+2) last
= pc
; /* affect all regs above its base */
420 /* not full check and jump is forward and do not skip `lastpc'? */
421 if (reg
!= NO_REG
&& pc
< dest
&& dest
<= lastpc
)
422 pc
+= b
; /* do the jump */
430 c
--; /* c = num. returns */
431 if (c
== LUA_MULTRET
) {
432 check(checkopenop(pt
, pc
));
436 if (reg
>= a
) last
= pc
; /* affect all registers above base */
440 b
--; /* b = num. returns */
441 if (b
> 0) checkreg(pt
, a
+b
-1);
445 if (b
> 0) checkreg(pt
, a
+ b
);
448 check(pc
< pt
->sizecode
- 1);
454 check(b
< pt
->sizep
);
455 nup
= pt
->p
[b
]->nups
;
456 check(pc
+ nup
< pt
->sizecode
);
457 for (j
= 1; j
<= nup
; j
++) {
458 OpCode op1
= GET_OPCODE(pt
->code
[pc
+ j
]);
459 check(op1
== OP_GETUPVAL
|| op1
== OP_MOVE
);
461 if (reg
!= NO_REG
) /* tracing? */
462 pc
+= nup
; /* do not 'execute' these pseudo-instructions */
466 check((pt
->is_vararg
& VARARG_ISVARARG
) &&
467 !(pt
->is_vararg
& VARARG_NEEDSARG
));
469 if (b
== LUA_MULTRET
) check(checkopenop(pt
, pc
));
476 return pt
->code
[last
];
483 /* }====================================================== */
486 int luaG_checkcode (const Proto
*pt
) {
487 return (symbexec(pt
, pt
->sizecode
, NO_REG
) != 0);
491 static const char *kname (Proto
*p
, int c
) {
492 if (ISK(c
) && ttisstring(&p
->k
[INDEXK(c
)]))
493 return svalue(&p
->k
[INDEXK(c
)]);
499 static const char *getobjname (lua_State
*L
, CallInfo
*ci
, int stackpos
,
501 if (isLua(ci
)) { /* a Lua function? */
502 Proto
*p
= ci_func(ci
)->l
.p
;
503 int pc
= currentpc(L
, ci
);
505 *name
= luaF_getlocalname(p
, stackpos
+1, pc
);
506 if (*name
) /* is a local? */
508 i
= symbexec(p
, pc
, stackpos
); /* try symbolic execution */
509 lua_assert(pc
!= -1);
510 switch (GET_OPCODE(i
)) {
512 int g
= GETARG_Bx(i
); /* global index */
513 lua_assert(ttisstring(&p
->k
[g
]));
514 *name
= svalue(&p
->k
[g
]);
519 int b
= GETARG_B(i
); /* move from `b' to `a' */
521 return getobjname(L
, ci
, b
, name
); /* get name for `b' */
525 int k
= GETARG_C(i
); /* key index */
530 int u
= GETARG_B(i
); /* upvalue index */
531 *name
= p
->upvalues
? getstr(p
->upvalues
[u
]) : "?";
535 int k
= GETARG_C(i
); /* key index */
542 return NULL
; /* no useful name found */
546 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
548 if ((isLua(ci
) && ci
->tailcalls
> 0) || !isLua(ci
- 1))
549 return NULL
; /* calling function is not Lua (or is unknown) */
550 ci
--; /* calling function */
551 i
= ci_func(ci
)->l
.p
->code
[currentpc(L
, ci
)];
552 if (GET_OPCODE(i
) == OP_CALL
|| GET_OPCODE(i
) == OP_TAILCALL
||
553 GET_OPCODE(i
) == OP_TFORLOOP
)
554 return getobjname(L
, ci
, GETARG_A(i
), name
);
556 return NULL
; /* no useful name can be found */
560 /* only ANSI way to check whether a pointer points to an array */
561 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
563 for (p
= ci
->base
; p
< ci
->top
; p
++)
564 if (o
== p
) return 1;
569 void luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
570 const char *name
= NULL
;
571 const char *t
= luaT_typenames
[ttype(o
)];
572 const char *kind
= (isinstack(L
->ci
, o
)) ?
573 getobjname(L
, L
->ci
, cast_int(o
- L
->base
), &name
) :
576 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
579 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
583 void luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
584 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
585 lua_assert(!ttisstring(p1
) && !ttisnumber(p1
));
586 luaG_typeerror(L
, p1
, "concatenate");
590 void luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
592 if (luaV_tonumber(p1
, &temp
) == NULL
)
593 p2
= p1
; /* first operand is wrong */
594 luaG_typeerror(L
, p2
, "perform arithmetic on");
598 int luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
599 const char *t1
= luaT_typenames
[ttype(p1
)];
600 const char *t2
= luaT_typenames
[ttype(p2
)];
602 luaG_runerror(L
, "attempt to compare two %s values", t1
);
604 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
609 static void addinfo (lua_State
*L
, const char *msg
) {
610 CallInfo
*ci
= L
->ci
;
611 if (isLua(ci
)) { /* is Lua code? */
612 char buff
[LUA_IDSIZE
]; /* add file:line information */
613 int line
= currentline(L
, ci
);
614 luaO_chunkid(buff
, getstr(getluaproto(ci
)->source
), LUA_IDSIZE
);
615 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
620 void luaG_errormsg (lua_State
*L
) {
621 if (L
->errfunc
!= 0) { /* is there an error handling function? */
622 StkId errfunc
= restorestack(L
, L
->errfunc
);
623 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
624 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
625 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
627 luaD_call(L
, L
->top
- 2, 1); /* call it */
629 luaD_throw(L
, LUA_ERRRUN
);
633 void luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
636 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));