2 ** $Id: ldebug.c,v 2.88 2011/11/30 12:43:51 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 (CallInfo
*ci
) {
37 lua_assert(isLua(ci
));
38 return pcRel(ci
->u
.l
.savedpc
, ci_func(ci
)->p
);
42 static int currentline (CallInfo
*ci
) {
43 return getfuncline(ci_func(ci
)->p
, currentpc(ci
));
48 ** this function can be called asynchronous (e.g. during a signal)
50 LUA_API
int lua_sethook (lua_State
*L
, lua_Hook func
, int mask
, int count
) {
51 if (func
== NULL
|| mask
== 0) { /* turn off hooks? */
56 L
->oldpc
= L
->ci
->u
.l
.savedpc
;
58 L
->basehookcount
= count
;
60 L
->hookmask
= cast_byte(mask
);
65 LUA_API lua_Hook
lua_gethook (lua_State
*L
) {
70 LUA_API
int lua_gethookmask (lua_State
*L
) {
75 LUA_API
int lua_gethookcount (lua_State
*L
) {
76 return L
->basehookcount
;
80 LUA_API
int lua_getstack (lua_State
*L
, int level
, lua_Debug
*ar
) {
83 if (level
< 0) return 0; /* invalid (negative) level */
85 for (ci
= L
->ci
; level
> 0 && ci
!= &L
->base_ci
; ci
= ci
->previous
)
87 if (level
== 0 && ci
!= &L
->base_ci
) { /* level found? */
91 else status
= 0; /* no such level */
97 static const char *upvalname (Proto
*p
, int uv
) {
98 TString
*s
= check_exp(uv
< p
->sizeupvalues
, p
->upvalues
[uv
].name
);
99 if (s
== NULL
) return "?";
100 else return getstr(s
);
104 static const char *findvararg (CallInfo
*ci
, int n
, StkId
*pos
) {
105 int nparams
= clLvalue(ci
->func
)->p
->numparams
;
106 if (n
>= ci
->u
.l
.base
- ci
->func
- nparams
)
107 return NULL
; /* no such vararg */
109 *pos
= ci
->func
+ nparams
+ n
;
110 return "(*vararg)"; /* generic name for any vararg */
115 static const char *findlocal (lua_State
*L
, CallInfo
*ci
, int n
,
117 const char *name
= NULL
;
120 if (n
< 0) /* access to vararg values? */
121 return findvararg(ci
, -n
, pos
);
124 name
= luaF_getlocalname(ci_func(ci
)->p
, n
, currentpc(ci
));
129 if (name
== NULL
) { /* no 'standard' name? */
130 StkId limit
= (ci
== L
->ci
) ? L
->top
: ci
->next
->func
;
131 if (limit
- base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
132 name
= "(*temporary)"; /* generic name for any valid slot */
134 return NULL
; /* no name */
136 *pos
= base
+ (n
- 1);
141 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
144 if (ar
== NULL
) { /* information about non-active function? */
145 if (!isLfunction(L
->top
- 1)) /* not a Lua function? */
147 else /* consider live variables at function start (parameters) */
148 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 (cl
== NULL
|| cl
->c
.isC
) {
178 ar
->linedefined
= -1;
179 ar
->lastlinedefined
= -1;
184 ar
->source
= p
->source
? getstr(p
->source
) : "=?";
185 ar
->linedefined
= p
->linedefined
;
186 ar
->lastlinedefined
= p
->lastlinedefined
;
187 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
189 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
193 static void collectvalidlines (lua_State
*L
, Closure
*f
) {
194 if (f
== NULL
|| f
->c
.isC
) {
201 int *lineinfo
= f
->l
.p
->lineinfo
;
202 Table
*t
= luaH_new(L
); /* new table to store active lines */
203 sethvalue(L
, L
->top
, t
); /* push it on stack */
205 setbvalue(&v
, 1); /* boolean 'true' to be the value of all indices */
206 for (i
= 0; i
< f
->l
.p
->sizelineinfo
; i
++) /* for all lines with code */
207 luaH_setint(L
, t
, lineinfo
[i
], &v
); /* table[line] = true */
212 static int auxgetinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
,
213 Closure
*f
, CallInfo
*ci
) {
215 for (; *what
; what
++) {
222 ar
->currentline
= (ci
&& isLua(ci
)) ? currentline(ci
) : -1;
226 ar
->nups
= (f
== NULL
) ? 0 : f
->c
.nupvalues
;
227 if (f
== NULL
|| f
->c
.isC
) {
232 ar
->isvararg
= f
->l
.p
->is_vararg
;
233 ar
->nparams
= f
->l
.p
->numparams
;
238 ar
->istailcall
= (ci
) ? ci
->callstatus
& CIST_TAIL
: 0;
242 /* calling function is a known Lua function? */
243 if (ci
&& !(ci
->callstatus
& CIST_TAIL
) && isLua(ci
->previous
))
244 ar
->namewhat
= getfuncname(L
, ci
->previous
, &ar
->name
);
247 if (ar
->namewhat
== NULL
) {
248 ar
->namewhat
= ""; /* not found */
254 case 'f': /* handled by lua_getinfo */
256 default: status
= 0; /* invalid option */
263 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
) {
272 api_check(L
, ttisfunction(func
), "function expected");
273 what
++; /* skip the '>' */
274 L
->top
--; /* pop function */
279 lua_assert(ttisfunction(ci
->func
));
281 cl
= ttisclosure(func
) ? clvalue(func
) : NULL
;
282 status
= auxgetinfo(L
, what
, ar
, cl
, ci
);
283 if (strchr(what
, 'f')) {
284 setobjs2s(L
, L
->top
, func
);
287 if (strchr(what
, 'L'))
288 collectvalidlines(L
, cl
);
295 ** {======================================================
296 ** Symbolic Execution
297 ** =======================================================
300 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
305 ** find a "name" for the RK value 'c'
307 static void kname (Proto
*p
, int pc
, int c
, const char **name
) {
308 if (ISK(c
)) { /* is 'c' a constant? */
309 TValue
*kvalue
= &p
->k
[INDEXK(c
)];
310 if (ttisstring(kvalue
)) { /* literal constant? */
311 *name
= svalue(kvalue
); /* it is its own name */
314 /* else no reasonable name found */
316 else { /* 'c' is a register */
317 const char *what
= getobjname(p
, pc
, c
, name
); /* search for 'c' */
318 if (what
&& *what
== 'c') { /* found a constant name? */
319 return; /* 'name' already filled */
321 /* else no reasonable name found */
323 *name
= "?"; /* no reasonable name found */
328 ** try to find last instruction before 'lastpc' that modified register 'reg'
330 static int findsetreg (Proto
*p
, int lastpc
, int reg
) {
332 int setreg
= -1; /* keep last instruction that changed 'reg' */
333 for (pc
= 0; pc
< lastpc
; pc
++) {
334 Instruction i
= p
->code
[pc
];
335 OpCode op
= GET_OPCODE(i
);
340 if (a
<= reg
&& reg
<= a
+ b
) /* set registers from 'a' to 'a+b' */
345 if (reg
>= a
+ 2) setreg
= pc
; /* affect all regs above its base */
350 if (reg
>= a
) setreg
= pc
; /* affect all registers above base */
354 int b
= GETARG_sBx(i
);
355 int dest
= pc
+ 1 + b
;
356 /* jump is forward and do not skip `lastpc'? */
357 if (pc
< dest
&& dest
<= lastpc
)
358 pc
+= b
; /* do the jump */
362 if (reg
== a
) setreg
= pc
; /* jumped code can change 'a' */
366 if (testAMode(op
) && reg
== a
) /* any instruction that set A */
375 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
378 *name
= luaF_getlocalname(p
, reg
+ 1, lastpc
);
379 if (*name
) /* is a local? */
381 /* else try symbolic execution */
382 pc
= findsetreg(p
, lastpc
, reg
);
383 if (pc
!= -1) { /* could find instruction? */
384 Instruction i
= p
->code
[pc
];
385 OpCode op
= GET_OPCODE(i
);
388 int b
= GETARG_B(i
); /* move from 'b' to 'a' */
390 return getobjname(p
, pc
, b
, name
); /* get name for 'b' */
395 int k
= GETARG_C(i
); /* key index */
396 int t
= GETARG_B(i
); /* table index */
397 const char *vn
= (op
== OP_GETTABLE
) /* name of indexed variable */
398 ? luaF_getlocalname(p
, t
+ 1, pc
)
400 kname(p
, pc
, k
, name
);
401 return (vn
&& strcmp(vn
, LUA_ENV
) == 0) ? "global" : "field";
404 *name
= upvalname(p
, GETARG_B(i
));
409 int b
= (op
== OP_LOADK
) ? GETARG_Bx(i
)
410 : GETARG_Ax(p
->code
[pc
+ 1]);
411 if (ttisstring(&p
->k
[b
])) {
412 *name
= svalue(&p
->k
[b
]);
418 int k
= GETARG_C(i
); /* key index */
419 kname(p
, pc
, k
, name
);
422 default: break; /* go through to return NULL */
425 return NULL
; /* could not find reasonable name */
429 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
431 Proto
*p
= ci_func(ci
)->p
; /* calling function */
432 int pc
= currentpc(ci
); /* calling instruction index */
433 Instruction i
= p
->code
[pc
]; /* calling instruction */
434 switch (GET_OPCODE(i
)) {
436 case OP_TAILCALL
: /* get function name */
437 return getobjname(p
, pc
, GETARG_A(i
), name
);
438 case OP_TFORCALL
: { /* for iterator */
439 *name
= "for iterator";
440 return "for iterator";
442 /* all other instructions can call only through metamethods */
445 case OP_GETTABLE
: tm
= TM_INDEX
; break;
447 case OP_SETTABLE
: tm
= TM_NEWINDEX
; break;
448 case OP_EQ
: tm
= TM_EQ
; break;
449 case OP_ADD
: tm
= TM_ADD
; break;
450 case OP_SUB
: tm
= TM_SUB
; break;
451 case OP_MUL
: tm
= TM_MUL
; break;
452 case OP_DIV
: tm
= TM_DIV
; break;
453 case OP_MOD
: tm
= TM_MOD
; break;
454 case OP_POW
: tm
= TM_POW
; break;
455 case OP_UNM
: tm
= TM_UNM
; break;
456 case OP_LEN
: tm
= TM_LEN
; break;
457 case OP_LT
: tm
= TM_LT
; break;
458 case OP_LE
: tm
= TM_LE
; break;
459 case OP_CONCAT
: tm
= TM_CONCAT
; break;
461 return NULL
; /* else no useful name can be found */
463 *name
= getstr(G(L
)->tmname
[tm
]);
467 /* }====================================================== */
472 ** only ANSI way to check whether a pointer points to an array
473 ** (used only for error messages, so efficiency is not a big concern)
475 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
477 for (p
= ci
->u
.l
.base
; p
< ci
->top
; p
++)
478 if (o
== p
) return 1;
483 static const char *getupvalname (CallInfo
*ci
, const TValue
*o
,
485 LClosure
*c
= ci_func(ci
);
487 for (i
= 0; i
< c
->nupvalues
; i
++) {
488 if (c
->upvals
[i
]->v
== o
) {
489 *name
= upvalname(c
->p
, i
);
497 l_noret
luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
498 CallInfo
*ci
= L
->ci
;
499 const char *name
= NULL
;
500 const char *t
= objtypename(o
);
501 const char *kind
= NULL
;
503 kind
= getupvalname(ci
, o
, &name
); /* check whether 'o' is an upvalue */
504 if (!kind
&& isinstack(ci
, o
)) /* no? try a register */
505 kind
= getobjname(ci_func(ci
)->p
, currentpc(ci
),
506 cast_int(o
- ci
->u
.l
.base
), &name
);
509 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
512 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
516 l_noret
luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
517 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
518 lua_assert(!ttisstring(p1
) && !ttisnumber(p2
));
519 luaG_typeerror(L
, p1
, "concatenate");
523 l_noret
luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
525 if (luaV_tonumber(p1
, &temp
) == NULL
)
526 p2
= p1
; /* first operand is wrong */
527 luaG_typeerror(L
, p2
, "perform arithmetic on");
531 l_noret
luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
532 const char *t1
= objtypename(p1
);
533 const char *t2
= objtypename(p2
);
535 luaG_runerror(L
, "attempt to compare two %s values", t1
);
537 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
541 static void addinfo (lua_State
*L
, const char *msg
) {
542 CallInfo
*ci
= L
->ci
;
543 if (isLua(ci
)) { /* is Lua code? */
544 char buff
[LUA_IDSIZE
]; /* add file:line information */
545 int line
= currentline(ci
);
546 TString
*src
= ci_func(ci
)->p
->source
;
548 luaO_chunkid(buff
, getstr(src
), LUA_IDSIZE
);
549 else { /* no source available; use "?" instead */
550 buff
[0] = '?'; buff
[1] = '\0';
552 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
557 l_noret
luaG_errormsg (lua_State
*L
) {
558 if (L
->errfunc
!= 0) { /* is there an error handling function? */
559 StkId errfunc
= restorestack(L
, L
->errfunc
);
560 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
561 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
562 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
564 luaD_call(L
, L
->top
- 2, 1, 0); /* call it */
566 luaD_throw(L
, LUA_ERRRUN
);
570 l_noret
luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
573 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));