2 ** $Id: ldebug.c,v 2.90 2012/08/16 17:34:28 roberto Exp $
4 ** See Copyright Notice in lua.h
33 #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
36 static const char *getfuncname(lua_State
*L
, CallInfo
*ci
, const char **name
);
39 static int currentpc(CallInfo
*ci
) {
40 lua_assert(isLua(ci
));
41 return pcRel(ci
->u
.l
.savedpc
, ci_func(ci
)->p
);
45 static int currentline(CallInfo
*ci
) {
46 return getfuncline(ci_func(ci
)->p
, currentpc(ci
));
51 ** this function can be called asynchronous (e.g. during a signal)
53 LUA_API
int lua_sethook(lua_State
*L
, lua_Hook func
, int mask
, int count
) {
54 if (func
== NULL
|| mask
== 0) { /* turn off hooks? */
59 L
->oldpc
= L
->ci
->u
.l
.savedpc
;
61 L
->basehookcount
= count
;
63 L
->hookmask
= cast_byte(mask
);
68 LUA_API lua_Hook
lua_gethook(lua_State
*L
) {
73 LUA_API
int lua_gethookmask(lua_State
*L
) {
78 LUA_API
int lua_gethookcount(lua_State
*L
) {
79 return L
->basehookcount
;
83 LUA_API
int lua_getstack(lua_State
*L
, int level
, lua_Debug
*ar
) {
86 if (level
< 0) return 0; /* invalid (negative) level */
88 for (ci
= L
->ci
; level
> 0 && ci
!= &L
->base_ci
; ci
= ci
->previous
)
90 if (level
== 0 && ci
!= &L
->base_ci
) { /* level found? */
93 } else status
= 0; /* no such level */
99 static const char *upvalname(Proto
*p
, int uv
) {
100 TString
*s
= check_exp(uv
< p
->sizeupvalues
, p
->upvalues
[uv
].name
);
101 if (s
== NULL
) return "?";
102 else return getstr(s
);
106 static const char *findvararg(CallInfo
*ci
, int n
, StkId
*pos
) {
107 int nparams
= clLvalue(ci
->func
)->p
->numparams
;
108 if (n
>= ci
->u
.l
.base
- ci
->func
- nparams
)
109 return NULL
; /* no such vararg */
111 *pos
= ci
->func
+ nparams
+ n
;
112 return "(*vararg)"; /* generic name for any vararg */
117 static const char *findlocal(lua_State
*L
, CallInfo
*ci
, int n
,
119 const char *name
= NULL
;
122 if (n
< 0) /* access to vararg values? */
123 return findvararg(ci
, -n
, pos
);
126 name
= luaF_getlocalname(ci_func(ci
)->p
, n
, currentpc(ci
));
130 if (name
== NULL
) { /* no 'standard' name? */
131 StkId limit
= (ci
== L
->ci
) ? L
->top
: ci
->next
->func
;
132 if (limit
- base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
133 name
= "(*temporary)"; /* generic name for any valid slot */
135 return NULL
; /* no name */
137 *pos
= base
+ (n
- 1);
142 LUA_API
const char *lua_getlocal(lua_State
*L
, const lua_Debug
*ar
, int n
) {
145 if (ar
== NULL
) { /* information about non-active function? */
146 if (!isLfunction(L
->top
- 1)) /* not a Lua function? */
148 else /* consider live variables at function start (parameters) */
149 name
= luaF_getlocalname(clLvalue(L
->top
- 1)->p
, n
, 0);
150 } else { /* active function; get information through 'ar' */
151 StkId pos
= 0; /* to avoid warnings */
152 name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
154 setobj2s(L
, L
->top
, pos
);
163 LUA_API
const char *lua_setlocal(lua_State
*L
, const lua_Debug
*ar
, int n
) {
164 StkId pos
= 0; /* to avoid warnings */
165 const char *name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
168 setobjs2s(L
, pos
, L
->top
- 1);
169 L
->top
--; /* pop value */
175 static void funcinfo(lua_Debug
*ar
, Closure
*cl
) {
176 if (noLuaClosure(cl
)) {
178 ar
->linedefined
= -1;
179 ar
->lastlinedefined
= -1;
183 ar
->source
= p
->source
? getstr(p
->source
) : "=?";
184 ar
->linedefined
= p
->linedefined
;
185 ar
->lastlinedefined
= p
->lastlinedefined
;
186 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
188 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
192 static void collectvalidlines(lua_State
*L
, Closure
*f
) {
193 if (noLuaClosure(f
)) {
199 int *lineinfo
= f
->l
.p
->lineinfo
;
200 Table
*t
= luaH_new(L
); /* new table to store active lines */
201 sethvalue(L
, L
->top
, t
); /* push it on stack */
203 setbvalue(&v
, 1); /* boolean 'true' to be the value of all indices */
204 for (i
= 0; i
< f
->l
.p
->sizelineinfo
; i
++) /* for all lines with code */
205 luaH_setint(L
, t
, lineinfo
[i
], &v
); /* table[line] = true */
210 static int auxgetinfo(lua_State
*L
, const char *what
, lua_Debug
*ar
,
211 Closure
*f
, CallInfo
*ci
) {
213 for (; *what
; what
++) {
220 ar
->currentline
= (ci
&& isLua(ci
)) ? currentline(ci
) : -1;
224 ar
->nups
= (f
== NULL
) ? 0 : f
->c
.nupvalues
;
225 if (noLuaClosure(f
)) {
229 ar
->isvararg
= f
->l
.p
->is_vararg
;
230 ar
->nparams
= f
->l
.p
->numparams
;
235 ar
->istailcall
= (ci
) ? ci
->callstatus
& CIST_TAIL
: 0;
239 /* calling function is a known Lua function? */
240 if (ci
&& !(ci
->callstatus
& CIST_TAIL
) && isLua(ci
->previous
))
241 ar
->namewhat
= getfuncname(L
, ci
->previous
, &ar
->name
);
244 if (ar
->namewhat
== NULL
) {
245 ar
->namewhat
= ""; /* not found */
251 case 'f': /* handled by lua_getinfo */
254 status
= 0; /* invalid option */
261 LUA_API
int lua_getinfo(lua_State
*L
, const char *what
, lua_Debug
*ar
) {
270 api_check(L
, ttisfunction(func
), "function expected");
271 what
++; /* skip the '>' */
272 L
->top
--; /* pop function */
276 lua_assert(ttisfunction(ci
->func
));
278 cl
= ttisclosure(func
) ? clvalue(func
) : NULL
;
279 status
= auxgetinfo(L
, what
, ar
, cl
, ci
);
280 if (strchr(what
, 'f')) {
281 setobjs2s(L
, L
->top
, func
);
284 if (strchr(what
, 'L'))
285 collectvalidlines(L
, cl
);
292 ** {======================================================
293 ** Symbolic Execution
294 ** =======================================================
297 static const char *getobjname(Proto
*p
, int lastpc
, int reg
,
302 ** find a "name" for the RK value 'c'
304 static void kname(Proto
*p
, int pc
, int c
, const char **name
) {
305 if (ISK(c
)) { /* is 'c' a constant? */
306 TValue
*kvalue
= &p
->k
[INDEXK(c
)];
307 if (ttisstring(kvalue
)) { /* literal constant? */
308 *name
= svalue(kvalue
); /* it is its own name */
311 /* else no reasonable name found */
312 } else { /* 'c' is a register */
313 const char *what
= getobjname(p
, pc
, c
, name
); /* search for 'c' */
314 if (what
&& *what
== 'c') { /* found a constant name? */
315 return; /* 'name' already filled */
317 /* else no reasonable name found */
319 *name
= "?"; /* no reasonable name found */
324 ** try to find last instruction before 'lastpc' that modified register 'reg'
326 static int findsetreg(Proto
*p
, int lastpc
, int reg
) {
328 int setreg
= -1; /* keep last instruction that changed 'reg' */
329 for (pc
= 0; pc
< lastpc
; pc
++) {
330 Instruction i
= p
->code
[pc
];
331 OpCode op
= GET_OPCODE(i
);
336 if (a
<= reg
&& reg
<= a
+ b
) /* set registers from 'a' to 'a+b' */
341 if (reg
>= a
+ 2) setreg
= pc
; /* affect all regs above its base */
346 if (reg
>= a
) setreg
= pc
; /* affect all registers above base */
350 int b
= GETARG_sBx(i
);
351 int dest
= pc
+ 1 + b
;
352 /* jump is forward and do not skip `lastpc'? */
353 if (pc
< dest
&& dest
<= lastpc
)
354 pc
+= b
; /* do the jump */
358 if (reg
== a
) setreg
= pc
; /* jumped code can change 'a' */
362 if (testAMode(op
) && reg
== a
) /* any instruction that set A */
371 static const char *getobjname(Proto
*p
, int lastpc
, int reg
,
374 *name
= luaF_getlocalname(p
, reg
+ 1, lastpc
);
375 if (*name
) /* is a local? */
377 /* else try symbolic execution */
378 pc
= findsetreg(p
, lastpc
, reg
);
379 if (pc
!= -1) { /* could find instruction? */
380 Instruction i
= p
->code
[pc
];
381 OpCode op
= GET_OPCODE(i
);
384 int b
= GETARG_B(i
); /* move from 'b' to 'a' */
386 return getobjname(p
, pc
, b
, name
); /* get name for 'b' */
391 int k
= GETARG_C(i
); /* key index */
392 int t
= GETARG_B(i
); /* table index */
393 const char *vn
= (op
== OP_GETTABLE
) /* name of indexed variable */
394 ? luaF_getlocalname(p
, t
+ 1, pc
)
396 kname(p
, pc
, k
, name
);
397 return (vn
&& strcmp(vn
, LUA_ENV
) == 0) ? "global" : "field";
400 *name
= upvalname(p
, GETARG_B(i
));
405 int b
= (op
== OP_LOADK
) ? GETARG_Bx(i
)
406 : GETARG_Ax(p
->code
[pc
+ 1]);
407 if (ttisstring(&p
->k
[b
])) {
408 *name
= svalue(&p
->k
[b
]);
414 int k
= GETARG_C(i
); /* key index */
415 kname(p
, pc
, k
, name
);
419 break; /* go through to return NULL */
422 return NULL
; /* could not find reasonable name */
426 static const char *getfuncname(lua_State
*L
, CallInfo
*ci
, const char **name
) {
428 Proto
*p
= ci_func(ci
)->p
; /* calling function */
429 int pc
= currentpc(ci
); /* calling instruction index */
430 Instruction i
= p
->code
[pc
]; /* calling instruction */
431 switch (GET_OPCODE(i
)) {
433 case OP_TAILCALL
: /* get function name */
434 return getobjname(p
, pc
, GETARG_A(i
), name
);
435 case OP_TFORCALL
: { /* for iterator */
436 *name
= "for iterator";
437 return "for iterator";
439 /* all other instructions can call only through metamethods */
486 return NULL
; /* else no useful name can be found */
488 *name
= getstr(G(L
)->tmname
[tm
]);
492 /* }====================================================== */
497 ** only ANSI way to check whether a pointer points to an array
498 ** (used only for error messages, so efficiency is not a big concern)
500 static int isinstack(CallInfo
*ci
, const TValue
*o
) {
502 for (p
= ci
->u
.l
.base
; p
< ci
->top
; p
++)
503 if (o
== p
) return 1;
508 static const char *getupvalname(CallInfo
*ci
, const TValue
*o
,
510 LClosure
*c
= ci_func(ci
);
512 for (i
= 0; i
< c
->nupvalues
; i
++) {
513 if (c
->upvals
[i
]->v
== o
) {
514 *name
= upvalname(c
->p
, i
);
522 l_noret
luaG_typeerror(lua_State
*L
, const TValue
*o
, const char *op
) {
523 CallInfo
*ci
= L
->ci
;
524 const char *name
= NULL
;
525 const char *t
= objtypename(o
);
526 const char *kind
= NULL
;
528 kind
= getupvalname(ci
, o
, &name
); /* check whether 'o' is an upvalue */
529 if (!kind
&& isinstack(ci
, o
)) /* no? try a register */
530 kind
= getobjname(ci_func(ci
)->p
, currentpc(ci
),
531 cast_int(o
- ci
->u
.l
.base
), &name
);
534 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
537 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
541 l_noret
luaG_concaterror(lua_State
*L
, StkId p1
, StkId p2
) {
542 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
543 lua_assert(!ttisstring(p1
) && !ttisnumber(p2
));
544 luaG_typeerror(L
, p1
, "concatenate");
548 l_noret
luaG_aritherror(lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
550 if (luaV_tonumber(p1
, &temp
) == NULL
)
551 p2
= p1
; /* first operand is wrong */
552 luaG_typeerror(L
, p2
, "perform arithmetic on");
556 l_noret
luaG_ordererror(lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
557 const char *t1
= objtypename(p1
);
558 const char *t2
= objtypename(p2
);
560 luaG_runerror(L
, "attempt to compare two %s values", t1
);
562 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
566 static void addinfo(lua_State
*L
, const char *msg
) {
567 CallInfo
*ci
= L
->ci
;
568 if (isLua(ci
)) { /* is Lua code? */
569 char buff
[LUA_IDSIZE
]; /* add file:line information */
570 int line
= currentline(ci
);
571 TString
*src
= ci_func(ci
)->p
->source
;
573 luaO_chunkid(buff
, getstr(src
), LUA_IDSIZE
);
574 else { /* no source available; use "?" instead */
578 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
583 l_noret
luaG_errormsg(lua_State
*L
) {
584 if (L
->errfunc
!= 0) { /* is there an error handling function? */
585 StkId errfunc
= restorestack(L
, L
->errfunc
);
586 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
587 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
588 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
590 luaD_call(L
, L
->top
- 2, 1, 0); /* call it */
592 luaD_throw(L
, LUA_ERRRUN
);
596 l_noret
luaG_runerror(lua_State
*L
, const char *fmt
, ...) {
599 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));