2 ** $Id: ldebug.c,v 2.90.1.4 2015/02/19 17:05:13 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
));
50 static void swapextra (lua_State
*L
) {
51 if (L
->status
== LUA_YIELD
) {
52 CallInfo
*ci
= L
->ci
; /* get function that yielded */
53 StkId temp
= ci
->func
; /* exchange its 'func' and 'extra' values */
54 ci
->func
= restorestack(L
, ci
->extra
);
55 ci
->extra
= savestack(L
, temp
);
61 ** this function can be called asynchronous (e.g. during a signal)
63 LUA_API
int lua_sethook (lua_State
*L
, lua_Hook func
, int mask
, int count
) {
64 if (func
== NULL
|| mask
== 0) { /* turn off hooks? */
69 L
->oldpc
= L
->ci
->u
.l
.savedpc
;
71 L
->basehookcount
= count
;
73 L
->hookmask
= cast_byte(mask
);
78 LUA_API lua_Hook
lua_gethook (lua_State
*L
) {
83 LUA_API
int lua_gethookmask (lua_State
*L
) {
88 LUA_API
int lua_gethookcount (lua_State
*L
) {
89 return L
->basehookcount
;
93 LUA_API
int lua_getstack (lua_State
*L
, int level
, lua_Debug
*ar
) {
96 if (level
< 0) return 0; /* invalid (negative) level */
98 for (ci
= L
->ci
; level
> 0 && ci
!= &L
->base_ci
; ci
= ci
->previous
)
100 if (level
== 0 && ci
!= &L
->base_ci
) { /* level found? */
104 else status
= 0; /* no such level */
110 static const char *upvalname (Proto
*p
, int uv
) {
111 TString
*s
= check_exp(uv
< p
->sizeupvalues
, p
->upvalues
[uv
].name
);
112 if (s
== NULL
) return "?";
113 else return getstr(s
);
117 static const char *findvararg (CallInfo
*ci
, int n
, StkId
*pos
) {
118 int nparams
= clLvalue(ci
->func
)->p
->numparams
;
119 if (n
>= ci
->u
.l
.base
- ci
->func
- nparams
)
120 return NULL
; /* no such vararg */
122 *pos
= ci
->func
+ nparams
+ n
;
123 return "(*vararg)"; /* generic name for any vararg */
128 static const char *findlocal (lua_State
*L
, CallInfo
*ci
, int n
,
130 const char *name
= NULL
;
133 if (n
< 0) /* access to vararg values? */
134 return findvararg(ci
, -n
, pos
);
137 name
= luaF_getlocalname(ci_func(ci
)->p
, n
, currentpc(ci
));
142 if (name
== NULL
) { /* no 'standard' name? */
143 StkId limit
= (ci
== L
->ci
) ? L
->top
: ci
->next
->func
;
144 if (limit
- base
>= n
&& n
> 0) /* is 'n' inside 'ci' stack? */
145 name
= "(*temporary)"; /* generic name for any valid slot */
147 return NULL
; /* no name */
149 *pos
= base
+ (n
- 1);
154 LUA_API
const char *lua_getlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
158 if (ar
== NULL
) { /* information about non-active function? */
159 if (!isLfunction(L
->top
- 1)) /* not a Lua function? */
161 else /* consider live variables at function start (parameters) */
162 name
= luaF_getlocalname(clLvalue(L
->top
- 1)->p
, n
, 0);
164 else { /* active function; get information through 'ar' */
165 StkId pos
= 0; /* to avoid warnings */
166 name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
168 setobj2s(L
, L
->top
, pos
);
178 LUA_API
const char *lua_setlocal (lua_State
*L
, const lua_Debug
*ar
, int n
) {
179 StkId pos
= 0; /* to avoid warnings */
183 name
= findlocal(L
, ar
->i_ci
, n
, &pos
);
185 setobjs2s(L
, pos
, L
->top
- 1);
186 L
->top
--; /* pop value */
193 static void funcinfo (lua_Debug
*ar
, Closure
*cl
) {
194 if (noLuaClosure(cl
)) {
196 ar
->linedefined
= -1;
197 ar
->lastlinedefined
= -1;
202 ar
->source
= p
->source
? getstr(p
->source
) : "=?";
203 ar
->linedefined
= p
->linedefined
;
204 ar
->lastlinedefined
= p
->lastlinedefined
;
205 ar
->what
= (ar
->linedefined
== 0) ? "main" : "Lua";
207 luaO_chunkid(ar
->short_src
, ar
->source
, LUA_IDSIZE
);
211 static void collectvalidlines (lua_State
*L
, Closure
*f
) {
212 if (noLuaClosure(f
)) {
219 int *lineinfo
= f
->l
.p
->lineinfo
;
220 Table
*t
= luaH_new(L
); /* new table to store active lines */
221 sethvalue(L
, L
->top
, t
); /* push it on stack */
223 setbvalue(&v
, 1); /* boolean 'true' to be the value of all indices */
224 for (i
= 0; i
< f
->l
.p
->sizelineinfo
; i
++) /* for all lines with code */
225 luaH_setint(L
, t
, lineinfo
[i
], &v
); /* table[line] = true */
230 static int auxgetinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
,
231 Closure
*f
, CallInfo
*ci
) {
233 for (; *what
; what
++) {
240 ar
->currentline
= (ci
&& isLua(ci
)) ? currentline(ci
) : -1;
244 ar
->nups
= (f
== NULL
) ? 0 : f
->c
.nupvalues
;
245 if (noLuaClosure(f
)) {
250 ar
->isvararg
= f
->l
.p
->is_vararg
;
251 ar
->nparams
= f
->l
.p
->numparams
;
256 ar
->istailcall
= (ci
) ? ci
->callstatus
& CIST_TAIL
: 0;
260 /* calling function is a known Lua function? */
261 if (ci
&& !(ci
->callstatus
& CIST_TAIL
) && isLua(ci
->previous
))
262 ar
->namewhat
= getfuncname(L
, ci
->previous
, &ar
->name
);
265 if (ar
->namewhat
== NULL
) {
266 ar
->namewhat
= ""; /* not found */
272 case 'f': /* handled by lua_getinfo */
274 default: status
= 0; /* invalid option */
281 LUA_API
int lua_getinfo (lua_State
*L
, const char *what
, lua_Debug
*ar
) {
291 api_check(L
, ttisfunction(func
), "function expected");
292 what
++; /* skip the '>' */
293 L
->top
--; /* pop function */
298 lua_assert(ttisfunction(ci
->func
));
300 cl
= ttisclosure(func
) ? clvalue(func
) : NULL
;
301 status
= auxgetinfo(L
, what
, ar
, cl
, ci
);
302 if (strchr(what
, 'f')) {
303 setobjs2s(L
, L
->top
, func
);
307 if (strchr(what
, 'L'))
308 collectvalidlines(L
, cl
);
315 ** {======================================================
316 ** Symbolic Execution
317 ** =======================================================
320 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
325 ** find a "name" for the RK value 'c'
327 static void kname (Proto
*p
, int pc
, int c
, const char **name
) {
328 if (ISK(c
)) { /* is 'c' a constant? */
329 TValue
*kvalue
= &p
->k
[INDEXK(c
)];
330 if (ttisstring(kvalue
)) { /* literal constant? */
331 *name
= svalue(kvalue
); /* it is its own name */
334 /* else no reasonable name found */
336 else { /* 'c' is a register */
337 const char *what
= getobjname(p
, pc
, c
, name
); /* search for 'c' */
338 if (what
&& *what
== 'c') { /* found a constant name? */
339 return; /* 'name' already filled */
341 /* else no reasonable name found */
343 *name
= "?"; /* no reasonable name found */
347 static int filterpc (int pc
, int jmptarget
) {
348 if (pc
< jmptarget
) /* is code conditional (inside a jump)? */
349 return -1; /* cannot know who sets that register */
350 else return pc
; /* current position sets that register */
355 ** try to find last instruction before 'lastpc' that modified register 'reg'
357 static int findsetreg (Proto
*p
, int lastpc
, int reg
) {
359 int setreg
= -1; /* keep last instruction that changed 'reg' */
360 int jmptarget
= 0; /* any code before this address is conditional */
361 for (pc
= 0; pc
< lastpc
; pc
++) {
362 Instruction i
= p
->code
[pc
];
363 OpCode op
= GET_OPCODE(i
);
368 if (a
<= reg
&& reg
<= a
+ b
) /* set registers from 'a' to 'a+b' */
369 setreg
= filterpc(pc
, jmptarget
);
373 if (reg
>= a
+ 2) /* affect all regs above its base */
374 setreg
= filterpc(pc
, jmptarget
);
379 if (reg
>= a
) /* affect all registers above base */
380 setreg
= filterpc(pc
, jmptarget
);
384 int b
= GETARG_sBx(i
);
385 int dest
= pc
+ 1 + b
;
386 /* jump is forward and do not skip `lastpc'? */
387 if (pc
< dest
&& dest
<= lastpc
) {
388 if (dest
> jmptarget
)
389 jmptarget
= dest
; /* update 'jmptarget' */
394 if (reg
== a
) /* jumped code can change 'a' */
395 setreg
= filterpc(pc
, jmptarget
);
399 if (testAMode(op
) && reg
== a
) /* any instruction that set A */
400 setreg
= filterpc(pc
, jmptarget
);
408 static const char *getobjname (Proto
*p
, int lastpc
, int reg
,
411 *name
= luaF_getlocalname(p
, reg
+ 1, lastpc
);
412 if (*name
) /* is a local? */
414 /* else try symbolic execution */
415 pc
= findsetreg(p
, lastpc
, reg
);
416 if (pc
!= -1) { /* could find instruction? */
417 Instruction i
= p
->code
[pc
];
418 OpCode op
= GET_OPCODE(i
);
421 int b
= GETARG_B(i
); /* move from 'b' to 'a' */
423 return getobjname(p
, pc
, b
, name
); /* get name for 'b' */
428 int k
= GETARG_C(i
); /* key index */
429 int t
= GETARG_B(i
); /* table index */
430 const char *vn
= (op
== OP_GETTABLE
) /* name of indexed variable */
431 ? luaF_getlocalname(p
, t
+ 1, pc
)
433 kname(p
, pc
, k
, name
);
434 return (vn
&& strcmp(vn
, LUA_ENV
) == 0) ? "global" : "field";
437 *name
= upvalname(p
, GETARG_B(i
));
442 int b
= (op
== OP_LOADK
) ? GETARG_Bx(i
)
443 : GETARG_Ax(p
->code
[pc
+ 1]);
444 if (ttisstring(&p
->k
[b
])) {
445 *name
= svalue(&p
->k
[b
]);
451 int k
= GETARG_C(i
); /* key index */
452 kname(p
, pc
, k
, name
);
455 default: break; /* go through to return NULL */
458 return NULL
; /* could not find reasonable name */
462 static const char *getfuncname (lua_State
*L
, CallInfo
*ci
, const char **name
) {
464 Proto
*p
= ci_func(ci
)->p
; /* calling function */
465 int pc
= currentpc(ci
); /* calling instruction index */
466 Instruction i
= p
->code
[pc
]; /* calling instruction */
467 switch (GET_OPCODE(i
)) {
469 case OP_TAILCALL
: /* get function name */
470 return getobjname(p
, pc
, GETARG_A(i
), name
);
471 case OP_TFORCALL
: { /* for iterator */
472 *name
= "for iterator";
473 return "for iterator";
475 /* all other instructions can call only through metamethods */
478 case OP_GETTABLE
: tm
= TM_INDEX
; break;
480 case OP_SETTABLE
: tm
= TM_NEWINDEX
; break;
481 case OP_EQ
: tm
= TM_EQ
; break;
482 case OP_ADD
: tm
= TM_ADD
; break;
483 case OP_SUB
: tm
= TM_SUB
; break;
484 case OP_MUL
: tm
= TM_MUL
; break;
485 case OP_DIV
: tm
= TM_DIV
; break;
486 case OP_MOD
: tm
= TM_MOD
; break;
487 case OP_POW
: tm
= TM_POW
; break;
488 case OP_UNM
: tm
= TM_UNM
; break;
489 case OP_LEN
: tm
= TM_LEN
; break;
490 case OP_LT
: tm
= TM_LT
; break;
491 case OP_LE
: tm
= TM_LE
; break;
492 case OP_CONCAT
: tm
= TM_CONCAT
; break;
494 return NULL
; /* else no useful name can be found */
496 *name
= getstr(G(L
)->tmname
[tm
]);
500 /* }====================================================== */
505 ** only ANSI way to check whether a pointer points to an array
506 ** (used only for error messages, so efficiency is not a big concern)
508 static int isinstack (CallInfo
*ci
, const TValue
*o
) {
510 for (p
= ci
->u
.l
.base
; p
< ci
->top
; p
++)
511 if (o
== p
) return 1;
516 static const char *getupvalname (CallInfo
*ci
, const TValue
*o
,
518 LClosure
*c
= ci_func(ci
);
520 for (i
= 0; i
< c
->nupvalues
; i
++) {
521 if (c
->upvals
[i
]->v
== o
) {
522 *name
= upvalname(c
->p
, i
);
530 l_noret
luaG_typeerror (lua_State
*L
, const TValue
*o
, const char *op
) {
531 CallInfo
*ci
= L
->ci
;
532 const char *name
= NULL
;
533 const char *t
= objtypename(o
);
534 const char *kind
= NULL
;
536 kind
= getupvalname(ci
, o
, &name
); /* check whether 'o' is an upvalue */
537 if (!kind
&& isinstack(ci
, o
)) /* no? try a register */
538 kind
= getobjname(ci_func(ci
)->p
, currentpc(ci
),
539 cast_int(o
- ci
->u
.l
.base
), &name
);
542 luaG_runerror(L
, "attempt to %s %s " LUA_QS
" (a %s value)",
545 luaG_runerror(L
, "attempt to %s a %s value", op
, t
);
549 l_noret
luaG_concaterror (lua_State
*L
, StkId p1
, StkId p2
) {
550 if (ttisstring(p1
) || ttisnumber(p1
)) p1
= p2
;
551 lua_assert(!ttisstring(p1
) && !ttisnumber(p1
));
552 luaG_typeerror(L
, p1
, "concatenate");
556 l_noret
luaG_aritherror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
558 if (luaV_tonumber(p1
, &temp
) == NULL
)
559 p2
= p1
; /* first operand is wrong */
560 luaG_typeerror(L
, p2
, "perform arithmetic on");
564 l_noret
luaG_ordererror (lua_State
*L
, const TValue
*p1
, const TValue
*p2
) {
565 const char *t1
= objtypename(p1
);
566 const char *t2
= objtypename(p2
);
568 luaG_runerror(L
, "attempt to compare two %s values", t1
);
570 luaG_runerror(L
, "attempt to compare %s with %s", t1
, t2
);
574 static void addinfo (lua_State
*L
, const char *msg
) {
575 CallInfo
*ci
= L
->ci
;
576 if (isLua(ci
)) { /* is Lua code? */
577 char buff
[LUA_IDSIZE
]; /* add file:line information */
578 int line
= currentline(ci
);
579 TString
*src
= ci_func(ci
)->p
->source
;
581 luaO_chunkid(buff
, getstr(src
), LUA_IDSIZE
);
582 else { /* no source available; use "?" instead */
583 buff
[0] = '?'; buff
[1] = '\0';
585 luaO_pushfstring(L
, "%s:%d: %s", buff
, line
, msg
);
590 l_noret
luaG_errormsg (lua_State
*L
) {
591 if (L
->errfunc
!= 0) { /* is there an error handling function? */
592 StkId errfunc
= restorestack(L
, L
->errfunc
);
593 if (!ttisfunction(errfunc
)) luaD_throw(L
, LUA_ERRERR
);
594 setobjs2s(L
, L
->top
, L
->top
- 1); /* move argument */
595 setobjs2s(L
, L
->top
- 1, errfunc
); /* push function */
597 luaD_call(L
, L
->top
- 2, 1, 0); /* call it */
599 luaD_throw(L
, LUA_ERRRUN
);
603 l_noret
luaG_runerror (lua_State
*L
, const char *fmt
, ...) {
606 addinfo(L
, luaO_pushvfstring(L
, fmt
, argp
));