2 ** $Id: ldebug.c,v 2.90.1.4 2015/02/19 17:05:13 roberto Exp $
4 ** See Copyright Notice in lua.h
11 #include <sys/lua/lua.h>
28 #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
31 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
);
34 static int currentpc (CallInfo
*ci
) {
35 lua_assert(isLua(ci
));
36 return pcRel(ci
->u
.l
.savedpc
, ci_func(ci
)->p
);
40 static int currentline (CallInfo
*ci
) {
41 return getfuncline(ci_func(ci
)->p
, currentpc(ci
));
45 static void swapextra (lua_State
*L
) {
46 if (L
->status
== LUA_YIELD
) {
47 CallInfo
*ci
= L
->ci
; /* get function that yielded */
48 StkId temp
= ci
->func
; /* exchange its 'func' and 'extra' values */
49 ci
->func
= restorestack(L
, ci
->extra
);
50 ci
->extra
= savestack(L
, temp
);
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
->oldpc
= L
->ci
->u
.l
.savedpc
;
66 L
->basehookcount
= count
;
68 L
->hookmask
= cast_byte(mask
);
73 LUA_API lua_Hook
lua_gethook (lua_State
*L
) {
78 LUA_API
int lua_gethookmask (lua_State
*L
) {
83 LUA_API
int lua_gethookcount (lua_State
*L
) {
84 return L
->basehookcount
;
88 LUA_API
int lua_getstack (lua_State
*L
, int level
, lua_Debug
*ar
) {
91 if (level
< 0) return 0; /* invalid (negative) level */
93 for (ci
= L
->ci
; level
> 0 && ci
!= &L
->base_ci
; ci
= ci
->previous
)
95 if (level
== 0 && ci
!= &L
->base_ci
) { /* level found? */
99 else status
= 0; /* no such level */
105 static const char *upvalname (Proto
*p
, int uv
) {
106 TString
*s
= check_exp(uv
< p
->sizeupvalues
, p
->upvalues
[uv
].name
);
107 if (s
== NULL
) return "?";
108 else return getstr(s
);
112 static const char *findvararg (CallInfo
*ci
, int n
, StkId
*pos
) {
113 int nparams
= clLvalue(ci
->func
)->p
->numparams
;
114 int nvararg
= cast_int(ci
->u
.l
.base
- ci
->func
) - nparams
;
116 return NULL
; /* no such vararg */
118 *pos
= ci
->func
+ nparams
- n
;
119 return "(*vararg)"; /* generic name for any vararg */
124 static const char *findlocal (lua_State
*L
, CallInfo
*ci
, int n
,
126 const char *name
= NULL
;
129 if (n
< 0) /* access to vararg values? */
130 return findvararg(ci
, n
, pos
);
133 name
= luaF_getlocalname(ci_func(ci
)->p
, n
, currentpc(ci
));
138 if (name
== NULL
) { /* no 'standard' name? */
139 StkId limit
= (ci
== L
->ci
) ? L
->top
: ci
->next
->func
;
140 if (limit
- base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
141 name
= "(*temporary)"; /* generic name for any valid slot */
143 return NULL
; /* no name */
145 *pos
= base
+ (n
- 1);
150 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
154 if (ar
== NULL
) { /* information about non-active function? */
155 if (!isLfunction(L
->top
- 1)) /* not a Lua function? */
157 else /* consider live variables at function start (parameters) */
158 name
= luaF_getlocalname(clLvalue(L
->top
- 1)->p
, n
, 0);
160 else { /* active function; get information through 'ar' */
161 StkId pos
= 0; /* to avoid warnings */
162 name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
164 setobj2s(L
, L
->top
, pos
);
174 LUA_API
const char *lua_setlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
175 StkId pos
= 0; /* to avoid warnings */
179 name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
181 setobjs2s(L
, pos
, L
->top
- 1);
182 L
->top
--; /* pop value */
189 static void funcinfo (lua_Debug
*ar
, Closure
*cl
) {
190 if (noLuaClosure(cl
)) {
192 ar
->linedefined
= -1;
193 ar
->lastlinedefined
= -1;
198 ar
->source
= p
->source
? getstr(p
->source
) : "=?";
199 ar
->linedefined
= p
->linedefined
;
200 ar
->lastlinedefined
= p
->lastlinedefined
;
201 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
203 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
207 static void collectvalidlines (lua_State
*L
, Closure
*f
) {
208 if (noLuaClosure(f
)) {
215 int *lineinfo
= f
->l
.p
->lineinfo
;
216 Table
*t
= luaH_new(L
); /* new table to store active lines */
217 sethvalue(L
, L
->top
, t
); /* push it on stack */
219 setbvalue(&v
, 1); /* boolean 'true' to be the value of all indices */
220 for (i
= 0; i
< f
->l
.p
->sizelineinfo
; i
++) /* for all lines with code */
221 luaH_setint(L
, t
, lineinfo
[i
], &v
); /* table[line] = true */
226 static int auxgetinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
,
227 Closure
*f
, CallInfo
*ci
) {
229 for (; *what
; what
++) {
236 ar
->currentline
= (ci
&& isLua(ci
)) ? currentline(ci
) : -1;
240 ar
->nups
= (f
== NULL
) ? 0 : f
->c
.nupvalues
;
241 if (noLuaClosure(f
)) {
246 ar
->isvararg
= f
->l
.p
->is_vararg
;
247 ar
->nparams
= f
->l
.p
->numparams
;
252 ar
->istailcall
= (ci
) ? ci
->callstatus
& CIST_TAIL
: 0;
256 /* calling function is a known Lua function? */
257 if (ci
&& !(ci
->callstatus
& CIST_TAIL
) && isLua(ci
->previous
))
258 ar
->namewhat
= getfuncname(L
, ci
->previous
, &ar
->name
);
261 if (ar
->namewhat
== NULL
) {
262 ar
->namewhat
= ""; /* not found */
268 case 'f': /* handled by lua_getinfo */
270 default: status
= 0; /* invalid option */
277 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
) {
287 api_check(L
, ttisfunction(func
), "function expected");
288 what
++; /* skip the '>' */
289 L
->top
--; /* pop function */
294 lua_assert(ttisfunction(ci
->func
));
296 cl
= ttisclosure(func
) ? clvalue(func
) : NULL
;
297 status
= auxgetinfo(L
, what
, ar
, cl
, ci
);
298 if (strchr(what
, 'f')) {
299 setobjs2s(L
, L
->top
, func
);
303 if (strchr(what
, 'L'))
304 collectvalidlines(L
, cl
);
311 ** {======================================================
312 ** Symbolic Execution
313 ** =======================================================
316 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
321 ** find a "name" for the RK value 'c'
323 static void kname (Proto
*p
, int pc
, int c
, const char **name
) {
324 if (ISK(c
)) { /* is 'c' a constant? */
325 TValue
*kvalue
= &p
->k
[INDEXK(c
)];
326 if (ttisstring(kvalue
)) { /* literal constant? */
327 *name
= svalue(kvalue
); /* it is its own name */
330 /* else no reasonable name found */
332 else { /* 'c' is a register */
333 const char *what
= getobjname(p
, pc
, c
, name
); /* search for 'c' */
334 if (what
&& *what
== 'c') { /* found a constant name? */
335 return; /* 'name' already filled */
337 /* else no reasonable name found */
339 *name
= "?"; /* no reasonable name found */
343 static int filterpc (int pc
, int jmptarget
) {
344 if (pc
< jmptarget
) /* is code conditional (inside a jump)? */
345 return -1; /* cannot know who sets that register */
346 else return pc
; /* current position sets that register */
351 ** try to find last instruction before 'lastpc' that modified register 'reg'
353 static int findsetreg (Proto
*p
, int lastpc
, int reg
) {
355 int setreg
= -1; /* keep last instruction that changed 'reg' */
356 int jmptarget
= 0; /* any code before this address is conditional */
357 for (pc
= 0; pc
< lastpc
; pc
++) {
358 Instruction i
= p
->code
[pc
];
359 OpCode op
= GET_OPCODE(i
);
364 if (a
<= reg
&& reg
<= a
+ b
) /* set registers from 'a' to 'a+b' */
365 setreg
= filterpc(pc
, jmptarget
);
369 if (reg
>= a
+ 2) /* affect all regs above its base */
370 setreg
= filterpc(pc
, jmptarget
);
375 if (reg
>= a
) /* affect all registers above base */
376 setreg
= filterpc(pc
, jmptarget
);
380 int b
= GETARG_sBx(i
);
381 int dest
= pc
+ 1 + b
;
382 /* jump is forward and do not skip `lastpc'? */
383 if (pc
< dest
&& dest
<= lastpc
) {
384 if (dest
> jmptarget
)
385 jmptarget
= dest
; /* update 'jmptarget' */
390 if (reg
== a
) /* jumped code can change 'a' */
391 setreg
= filterpc(pc
, jmptarget
);
395 if (testAMode(op
) && reg
== a
) /* any instruction that set A */
396 setreg
= filterpc(pc
, jmptarget
);
404 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
407 *name
= luaF_getlocalname(p
, reg
+ 1, lastpc
);
408 if (*name
) /* is a local? */
410 /* else try symbolic execution */
411 pc
= findsetreg(p
, lastpc
, reg
);
412 if (pc
!= -1) { /* could find instruction? */
413 Instruction i
= p
->code
[pc
];
414 OpCode op
= GET_OPCODE(i
);
417 int b
= GETARG_B(i
); /* move from 'b' to 'a' */
419 return getobjname(p
, pc
, b
, name
); /* get name for 'b' */
424 int k
= GETARG_C(i
); /* key index */
425 int t
= GETARG_B(i
); /* table index */
426 const char *vn
= (op
== OP_GETTABLE
) /* name of indexed variable */
427 ? luaF_getlocalname(p
, t
+ 1, pc
)
429 kname(p
, pc
, k
, name
);
430 return (vn
&& strcmp(vn
, LUA_ENV
) == 0) ? "global" : "field";
433 *name
= upvalname(p
, GETARG_B(i
));
438 int b
= (op
== OP_LOADK
) ? GETARG_Bx(i
)
439 : GETARG_Ax(p
->code
[pc
+ 1]);
440 if (ttisstring(&p
->k
[b
])) {
441 *name
= svalue(&p
->k
[b
]);
447 int k
= GETARG_C(i
); /* key index */
448 kname(p
, pc
, k
, name
);
451 default: break; /* go through to return NULL */
454 return NULL
; /* could not find reasonable name */
458 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
460 Proto
*p
= ci_func(ci
)->p
; /* calling function */
461 int pc
= currentpc(ci
); /* calling instruction index */
462 Instruction i
= p
->code
[pc
]; /* calling instruction */
463 switch (GET_OPCODE(i
)) {
465 case OP_TAILCALL
: /* get function name */
466 return getobjname(p
, pc
, GETARG_A(i
), name
);
467 case OP_TFORCALL
: { /* for iterator */
468 *name
= "for iterator";
469 return "for iterator";
471 /* all other instructions can call only through metamethods */
474 case OP_GETTABLE
: tm
= TM_INDEX
; break;
476 case OP_SETTABLE
: tm
= TM_NEWINDEX
; break;
477 case OP_EQ
: tm
= TM_EQ
; break;
478 case OP_ADD
: tm
= TM_ADD
; break;
479 case OP_SUB
: tm
= TM_SUB
; break;
480 case OP_MUL
: tm
= TM_MUL
; break;
481 case OP_DIV
: tm
= TM_DIV
; break;
482 case OP_MOD
: tm
= TM_MOD
; break;
483 case OP_POW
: tm
= TM_POW
; break;
484 case OP_UNM
: tm
= TM_UNM
; break;
485 case OP_LEN
: tm
= TM_LEN
; break;
486 case OP_LT
: tm
= TM_LT
; break;
487 case OP_LE
: tm
= TM_LE
; break;
488 case OP_CONCAT
: tm
= TM_CONCAT
; break;
490 return NULL
; /* else no useful name can be found */
492 *name
= getstr(G(L
)->tmname
[tm
]);
496 /* }====================================================== */
501 ** only ANSI way to check whether a pointer points to an array
502 ** (used only for error messages, so efficiency is not a big concern)
504 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
506 for (p
= ci
->u
.l
.base
; p
< ci
->top
; p
++)
507 if (o
== p
) return 1;
512 static const char *getupvalname (CallInfo
*ci
, const TValue
*o
,
514 LClosure
*c
= ci_func(ci
);
516 for (i
= 0; i
< c
->nupvalues
; i
++) {
517 if (c
->upvals
[i
]->v
== o
) {
518 *name
= upvalname(c
->p
, i
);
526 l_noret
luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
527 CallInfo
*ci
= L
->ci
;
528 const char *name
= NULL
;
529 const char *t
= objtypename(o
);
530 const char *kind
= NULL
;
532 kind
= getupvalname(ci
, o
, &name
); /* check whether 'o' is an upvalue */
533 if (!kind
&& isinstack(ci
, o
)) /* no? try a register */
534 kind
= getobjname(ci_func(ci
)->p
, currentpc(ci
),
535 cast_int(o
- ci
->u
.l
.base
), &name
);
538 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
541 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
545 l_noret
luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
546 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
547 lua_assert(!ttisstring(p1
) && !ttisnumber(p1
));
548 luaG_typeerror(L
, p1
, "concatenate");
552 l_noret
luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
554 if (luaV_tonumber(p1
, &temp
) == NULL
)
555 p2
= p1
; /* first operand is wrong */
556 luaG_typeerror(L
, p2
, "perform arithmetic on");
560 l_noret
luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
561 const char *t1
= objtypename(p1
);
562 const char *t2
= objtypename(p2
);
564 luaG_runerror(L
, "attempt to compare two %s values", t1
);
566 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
570 static void addinfo (lua_State
*L
, const char *msg
) {
571 CallInfo
*ci
= L
->ci
;
572 if (isLua(ci
)) { /* is Lua code? */
573 char buff
[LUA_IDSIZE
]; /* add file:line information */
574 int line
= currentline(ci
);
575 TString
*src
= ci_func(ci
)->p
->source
;
577 luaO_chunkid(buff
, getstr(src
), LUA_IDSIZE
);
578 else { /* no source available; use "?" instead */
579 buff
[0] = '?'; buff
[1] = '\0';
581 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
586 l_noret
luaG_errormsg (lua_State
*L
) {
587 if (L
->errfunc
!= 0) { /* is there an error handling function? */
588 StkId errfunc
= restorestack(L
, L
->errfunc
);
589 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
590 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
591 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
593 luaD_call(L
, L
->top
- 2, 1, 0); /* call it */
595 luaD_throw(L
, LUA_ERRRUN
);
599 l_noret
luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
603 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));