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? */
94 else status
= 0; /* no such level */
100 static const char *upvalname (Proto
*p
, int uv
) {
101 TString
*s
= check_exp(uv
< p
->sizeupvalues
, p
->upvalues
[uv
].name
);
102 if (s
== NULL
) return "?";
103 else return getstr(s
);
107 static const char *findvararg (CallInfo
*ci
, int n
, StkId
*pos
) {
108 int nparams
= clLvalue(ci
->func
)->p
->numparams
;
109 if (n
>= ci
->u
.l
.base
- ci
->func
- nparams
)
110 return NULL
; /* no such vararg */
112 *pos
= ci
->func
+ nparams
+ n
;
113 return "(*vararg)"; /* generic name for any vararg */
118 static const char *findlocal (lua_State
*L
, CallInfo
*ci
, int n
,
120 const char *name
= NULL
;
123 if (n
< 0) /* access to vararg values? */
124 return findvararg(ci
, -n
, pos
);
127 name
= luaF_getlocalname(ci_func(ci
)->p
, n
, currentpc(ci
));
132 if (name
== NULL
) { /* no 'standard' name? */
133 StkId limit
= (ci
== L
->ci
) ? L
->top
: ci
->next
->func
;
134 if (limit
- base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
135 name
= "(*temporary)"; /* generic name for any valid slot */
137 return NULL
; /* no name */
139 *pos
= base
+ (n
- 1);
144 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
147 if (ar
== NULL
) { /* information about non-active function? */
148 if (!isLfunction(L
->top
- 1)) /* not a Lua function? */
150 else /* consider live variables at function start (parameters) */
151 name
= luaF_getlocalname(clLvalue(L
->top
- 1)->p
, n
, 0);
153 else { /* active function; get information through 'ar' */
154 StkId pos
= 0; /* to avoid warnings */
155 name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
157 setobj2s(L
, L
->top
, pos
);
166 LUA_API
const char *lua_setlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
167 StkId pos
= 0; /* to avoid warnings */
168 const char *name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
171 setobjs2s(L
, pos
, L
->top
- 1);
172 L
->top
--; /* pop value */
178 static void funcinfo (lua_Debug
*ar
, Closure
*cl
) {
179 if (noLuaClosure(cl
)) {
181 ar
->linedefined
= -1;
182 ar
->lastlinedefined
= -1;
187 ar
->source
= p
->source
? getstr(p
->source
) : "=?";
188 ar
->linedefined
= p
->linedefined
;
189 ar
->lastlinedefined
= p
->lastlinedefined
;
190 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
192 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
196 static void collectvalidlines (lua_State
*L
, Closure
*f
) {
197 if (noLuaClosure(f
)) {
204 int *lineinfo
= f
->l
.p
->lineinfo
;
205 Table
*t
= luaH_new(L
); /* new table to store active lines */
206 sethvalue(L
, L
->top
, t
); /* push it on stack */
208 setbvalue(&v
, 1); /* boolean 'true' to be the value of all indices */
209 for (i
= 0; i
< f
->l
.p
->sizelineinfo
; i
++) /* for all lines with code */
210 luaH_setint(L
, t
, lineinfo
[i
], &v
); /* table[line] = true */
215 static int auxgetinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
,
216 Closure
*f
, CallInfo
*ci
) {
218 for (; *what
; what
++) {
225 ar
->currentline
= (ci
&& isLua(ci
)) ? currentline(ci
) : -1;
229 ar
->nups
= (f
== NULL
) ? 0 : f
->c
.nupvalues
;
230 if (noLuaClosure(f
)) {
235 ar
->isvararg
= f
->l
.p
->is_vararg
;
236 ar
->nparams
= f
->l
.p
->numparams
;
241 ar
->istailcall
= (ci
) ? ci
->callstatus
& CIST_TAIL
: 0;
245 /* calling function is a known Lua function? */
246 if (ci
&& !(ci
->callstatus
& CIST_TAIL
) && isLua(ci
->previous
))
247 ar
->namewhat
= getfuncname(L
, ci
->previous
, &ar
->name
);
250 if (ar
->namewhat
== NULL
) {
251 ar
->namewhat
= ""; /* not found */
257 case 'f': /* handled by lua_getinfo */
259 default: status
= 0; /* invalid option */
266 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
) {
275 api_check(L
, ttisfunction(func
), "function expected");
276 what
++; /* skip the '>' */
277 L
->top
--; /* pop function */
282 lua_assert(ttisfunction(ci
->func
));
284 cl
= ttisclosure(func
) ? clvalue(func
) : NULL
;
285 status
= auxgetinfo(L
, what
, ar
, cl
, ci
);
286 if (strchr(what
, 'f')) {
287 setobjs2s(L
, L
->top
, func
);
290 if (strchr(what
, 'L'))
291 collectvalidlines(L
, cl
);
298 ** {======================================================
299 ** Symbolic Execution
300 ** =======================================================
303 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
308 ** find a "name" for the RK value 'c'
310 static void kname (Proto
*p
, int pc
, int c
, const char **name
) {
311 if (ISK(c
)) { /* is 'c' a constant? */
312 TValue
*kvalue
= &p
->k
[INDEXK(c
)];
313 if (ttisstring(kvalue
)) { /* literal constant? */
314 *name
= svalue(kvalue
); /* it is its own name */
317 /* else no reasonable name found */
319 else { /* 'c' is a register */
320 const char *what
= getobjname(p
, pc
, c
, name
); /* search for 'c' */
321 if (what
&& *what
== 'c') { /* found a constant name? */
322 return; /* 'name' already filled */
324 /* else no reasonable name found */
326 *name
= "?"; /* no reasonable name found */
331 ** try to find last instruction before 'lastpc' that modified register 'reg'
333 static int findsetreg (Proto
*p
, int lastpc
, int reg
) {
335 int setreg
= -1; /* keep last instruction that changed 'reg' */
336 for (pc
= 0; pc
< lastpc
; pc
++) {
337 Instruction i
= p
->code
[pc
];
338 OpCode op
= GET_OPCODE(i
);
343 if (a
<= reg
&& reg
<= a
+ b
) /* set registers from 'a' to 'a+b' */
348 if (reg
>= a
+ 2) setreg
= pc
; /* affect all regs above its base */
353 if (reg
>= a
) setreg
= pc
; /* affect all registers above base */
357 int b
= GETARG_sBx(i
);
358 int dest
= pc
+ 1 + b
;
359 /* jump is forward and do not skip `lastpc'? */
360 if (pc
< dest
&& dest
<= lastpc
)
361 pc
+= b
; /* do the jump */
365 if (reg
== a
) setreg
= pc
; /* jumped code can change 'a' */
369 if (testAMode(op
) && reg
== a
) /* any instruction that set A */
378 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
381 *name
= luaF_getlocalname(p
, reg
+ 1, lastpc
);
382 if (*name
) /* is a local? */
384 /* else try symbolic execution */
385 pc
= findsetreg(p
, lastpc
, reg
);
386 if (pc
!= -1) { /* could find instruction? */
387 Instruction i
= p
->code
[pc
];
388 OpCode op
= GET_OPCODE(i
);
391 int b
= GETARG_B(i
); /* move from 'b' to 'a' */
393 return getobjname(p
, pc
, b
, name
); /* get name for 'b' */
398 int k
= GETARG_C(i
); /* key index */
399 int t
= GETARG_B(i
); /* table index */
400 const char *vn
= (op
== OP_GETTABLE
) /* name of indexed variable */
401 ? luaF_getlocalname(p
, t
+ 1, pc
)
403 kname(p
, pc
, k
, name
);
404 return (vn
&& strcmp(vn
, LUA_ENV
) == 0) ? "global" : "field";
407 *name
= upvalname(p
, GETARG_B(i
));
412 int b
= (op
== OP_LOADK
) ? GETARG_Bx(i
)
413 : GETARG_Ax(p
->code
[pc
+ 1]);
414 if (ttisstring(&p
->k
[b
])) {
415 *name
= svalue(&p
->k
[b
]);
421 int k
= GETARG_C(i
); /* key index */
422 kname(p
, pc
, k
, name
);
425 default: break; /* go through to return NULL */
428 return NULL
; /* could not find reasonable name */
432 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
434 Proto
*p
= ci_func(ci
)->p
; /* calling function */
435 int pc
= currentpc(ci
); /* calling instruction index */
436 Instruction i
= p
->code
[pc
]; /* calling instruction */
437 switch (GET_OPCODE(i
)) {
439 case OP_TAILCALL
: /* get function name */
440 return getobjname(p
, pc
, GETARG_A(i
), name
);
441 case OP_TFORCALL
: { /* for iterator */
442 *name
= "for iterator";
443 return "for iterator";
445 /* all other instructions can call only through metamethods */
448 case OP_GETTABLE
: tm
= TM_INDEX
; break;
450 case OP_SETTABLE
: tm
= TM_NEWINDEX
; break;
451 case OP_EQ
: tm
= TM_EQ
; break;
452 case OP_ADD
: tm
= TM_ADD
; break;
453 case OP_SUB
: tm
= TM_SUB
; break;
454 case OP_MUL
: tm
= TM_MUL
; break;
455 case OP_DIV
: tm
= TM_DIV
; break;
456 case OP_MOD
: tm
= TM_MOD
; break;
457 case OP_POW
: tm
= TM_POW
; break;
458 case OP_UNM
: tm
= TM_UNM
; break;
459 case OP_LEN
: tm
= TM_LEN
; break;
460 case OP_LT
: tm
= TM_LT
; break;
461 case OP_LE
: tm
= TM_LE
; break;
462 case OP_CONCAT
: tm
= TM_CONCAT
; break;
464 return NULL
; /* else no useful name can be found */
466 *name
= getstr(G(L
)->tmname
[tm
]);
470 /* }====================================================== */
475 ** only ANSI way to check whether a pointer points to an array
476 ** (used only for error messages, so efficiency is not a big concern)
478 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
480 for (p
= ci
->u
.l
.base
; p
< ci
->top
; p
++)
481 if (o
== p
) return 1;
486 static const char *getupvalname (CallInfo
*ci
, const TValue
*o
,
488 LClosure
*c
= ci_func(ci
);
490 for (i
= 0; i
< c
->nupvalues
; i
++) {
491 if (c
->upvals
[i
]->v
== o
) {
492 *name
= upvalname(c
->p
, i
);
500 l_noret
luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
501 CallInfo
*ci
= L
->ci
;
502 const char *name
= NULL
;
503 const char *t
= objtypename(o
);
504 const char *kind
= NULL
;
506 kind
= getupvalname(ci
, o
, &name
); /* check whether 'o' is an upvalue */
507 if (!kind
&& isinstack(ci
, o
)) /* no? try a register */
508 kind
= getobjname(ci_func(ci
)->p
, currentpc(ci
),
509 cast_int(o
- ci
->u
.l
.base
), &name
);
512 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
515 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
519 l_noret
luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
520 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
521 lua_assert(!ttisstring(p1
) && !ttisnumber(p2
));
522 luaG_typeerror(L
, p1
, "concatenate");
526 l_noret
luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
528 if (luaV_tonumber(p1
, &temp
) == NULL
)
529 p2
= p1
; /* first operand is wrong */
530 luaG_typeerror(L
, p2
, "perform arithmetic on");
534 l_noret
luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
535 const char *t1
= objtypename(p1
);
536 const char *t2
= objtypename(p2
);
538 luaG_runerror(L
, "attempt to compare two %s values", t1
);
540 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
544 static void addinfo (lua_State
*L
, const char *msg
) {
545 CallInfo
*ci
= L
->ci
;
546 if (isLua(ci
)) { /* is Lua code? */
547 char buff
[LUA_IDSIZE
]; /* add file:line information */
548 int line
= currentline(ci
);
549 TString
*src
= ci_func(ci
)->p
->source
;
551 luaO_chunkid(buff
, getstr(src
), LUA_IDSIZE
);
552 else { /* no source available; use "?" instead */
553 buff
[0] = '?'; buff
[1] = '\0';
555 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
560 l_noret
luaG_errormsg (lua_State
*L
) {
561 if (L
->errfunc
!= 0) { /* is there an error handling function? */
562 StkId errfunc
= restorestack(L
, L
->errfunc
);
563 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
564 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
565 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
567 luaD_call(L
, L
->top
- 2, 1, 0); /* call it */
569 luaD_throw(L
, LUA_ERRRUN
);
573 l_noret
luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
576 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));