lua/ldebug.c

573 lines
13 KiB
C
Raw Normal View History

/*
** $Id: ldebug.c,v 1.61 2001/02/09 18:37:33 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
2000-02-17 10:30:36 -08:00
#include <stdlib.h>
#include "lua.h"
#include "lapi.h"
2000-06-28 13:21:06 -07:00
#include "lcode.h"
#include "ldebug.h"
2000-01-19 04:00:45 -08:00
#include "ldo.h"
#include "lfunc.h"
#include "lobject.h"
2000-06-28 13:21:06 -07:00
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "luadebug.h"
2000-10-05 05:14:08 -07:00
2000-08-11 09:17:28 -07:00
static const char *getfuncname (lua_State *L, StkId f, const char **name);
2000-01-19 04:00:45 -08:00
static void setnormalized (TObject *d, const TObject *s) {
2000-10-05 05:14:08 -07:00
if (ttype(s) == LUA_TMARK) {
setclvalue(d, infovalue(s)->func);
2000-06-26 12:28:31 -07:00
}
else setobj(d, s);
2000-10-05 05:14:08 -07:00
}
static int isLmark (StkId o) {
return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
}
2000-10-20 09:39:03 -07:00
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
lua_Hook oldhook;
LUA_LOCK(L);
oldhook = L->callhook;
L->callhook = func;
LUA_UNLOCK(L);
2000-01-19 04:00:45 -08:00
return oldhook;
}
2000-10-20 09:39:03 -07:00
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
lua_Hook oldhook;
LUA_LOCK(L);
oldhook = L->linehook;
2000-01-19 04:00:45 -08:00
L->linehook = func;
LUA_UNLOCK(L);
2000-01-19 04:00:45 -08:00
return oldhook;
}
2000-01-19 04:00:45 -08:00
static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
int i;
2000-08-10 12:50:47 -07:00
for (i = (top-1) - L->stack; i>=0; i--) {
if (is_T_MARK(&L->stack[i])) {
if (level == 0)
return L->stack+i;
level--;
}
}
2000-01-19 04:00:45 -08:00
return NULL;
}
2000-10-20 09:39:03 -07:00
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
StkId f;
int status;
LUA_LOCK(L);
f = aux_stackedfunction(L, level, L->top);
if (f == NULL) status = 0; /* there is no such level */
2000-01-19 04:00:45 -08:00
else {
ar->_func = f;
status = 1;
2000-01-19 04:00:45 -08:00
}
LUA_UNLOCK(L);
return status;
}
2000-10-20 09:39:03 -07:00
static int nups (StkId f) {
2000-01-19 04:00:45 -08:00
switch (ttype(f)) {
2000-10-05 05:14:08 -07:00
case LUA_TFUNCTION:
return clvalue(f)->nupvalues;
2000-10-05 05:14:08 -07:00
case LUA_TMARK:
2000-06-26 12:28:31 -07:00
return infovalue(f)->func->nupvalues;
default:
return 0;
}
}
2000-08-08 11:26:05 -07:00
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
int refi;
2000-10-06 05:45:25 -07:00
if (lineinfo == NULL || pc == -1)
return -1; /* no line info or function is not active */
refi = prefi ? *prefi : 0;
2000-08-08 11:26:05 -07:00
if (lineinfo[refi] < 0)
2001-02-09 10:37:33 -08:00
refline += -lineinfo[refi++];
lua_assert(lineinfo[refi] >= 0);
2000-08-08 11:26:05 -07:00
while (lineinfo[refi] > pc) {
refline--;
refi--;
if (lineinfo[refi] < 0)
2001-02-09 10:37:33 -08:00
refline -= -lineinfo[refi--];
lua_assert(lineinfo[refi] >= 0);
2000-08-08 11:26:05 -07:00
}
for (;;) {
int nextline = refline + 1;
int nextref = refi + 1;
if (lineinfo[nextref] < 0)
2001-02-09 10:37:33 -08:00
nextline += -lineinfo[nextref++];
lua_assert(lineinfo[nextref] >= 0);
2000-08-08 11:26:05 -07:00
if (lineinfo[nextref] > pc)
break;
refline = nextline;
refi = nextref;
}
if (prefi) *prefi = refi;
return refline;
}
2000-10-20 09:39:03 -07:00
static int currentpc (StkId f) {
2000-06-28 13:21:06 -07:00
CallInfo *ci = infovalue(f);
lua_assert(isLmark(f));
2000-10-06 05:45:25 -07:00
if (ci->pc)
return (*ci->pc - ci->func->f.l->code) - 1;
else
return -1; /* function is not active */
2000-06-28 13:21:06 -07:00
}
2000-10-20 09:39:03 -07:00
static int currentline (StkId f) {
2000-10-05 05:14:08 -07:00
if (!isLmark(f))
2000-06-26 12:28:31 -07:00
return -1; /* only active lua functions have current-line information */
else {
CallInfo *ci = infovalue(f);
2000-08-08 11:26:05 -07:00
int *lineinfo = ci->func->f.l->lineinfo;
2000-10-20 09:39:03 -07:00
return luaG_getline(lineinfo, currentpc(f), 1, NULL);
2000-06-26 12:28:31 -07:00
}
}
2000-03-10 10:37:44 -08:00
static Proto *getluaproto (StkId f) {
2000-10-05 05:14:08 -07:00
return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
}
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
2000-08-28 10:57:04 -07:00
const char *name;
StkId f;
Proto *fp;
LUA_LOCK(L);
name = NULL;
f = ar->_func;
fp = getluaproto(f);
if (fp) { /* `f' is a Lua function? */
name = luaF_getlocalname(fp, n, currentpc(f));
if (name)
luaA_pushobject(L, (f+1)+(n-1)); /* push value */
}
LUA_UNLOCK(L);
2000-08-28 10:57:04 -07:00
return name;
}
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
2000-08-28 10:57:04 -07:00
const char *name;
StkId f;
Proto *fp;
LUA_LOCK(L);
name = NULL;
f = ar->_func;
fp = getluaproto(f);
L->top--; /* pop new value */
if (fp) { /* `f' is a Lua function? */
name = luaF_getlocalname(fp, n, currentpc(f));
if (!name || name[0] == '(') /* `(' starts private locals */
name = NULL;
else
setobj((f+1)+(n-1), L->top);
}
LUA_UNLOCK(L);
2000-08-28 10:57:04 -07:00
return name;
2000-01-19 04:00:45 -08:00
}
static void infoLproto (lua_Debug *ar, Proto *f) {
ar->source = getstr(f->source);
ar->linedefined = f->lineDefined;
ar->what = "Lua";
}
2000-10-20 09:39:03 -07:00
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
2000-10-05 05:14:08 -07:00
Closure *cl = NULL;
2000-01-19 04:00:45 -08:00
switch (ttype(func)) {
2000-10-05 05:14:08 -07:00
case LUA_TFUNCTION:
cl = clvalue(func);
2000-06-26 12:28:31 -07:00
break;
2000-10-05 05:14:08 -07:00
case LUA_TMARK:
cl = infovalue(func)->func;
break;
default:
luaD_error(L, "value for `lua_getinfo' is not a function");
}
2000-10-05 05:14:08 -07:00
if (cl->isC) {
2000-10-09 06:47:32 -07:00
ar->source = "=C";
2000-10-05 05:14:08 -07:00
ar->linedefined = -1;
ar->what = "C";
}
else
infoLproto(ar, cl->f.l);
2000-09-12 11:38:02 -07:00
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
2000-01-19 04:00:45 -08:00
if (ar->linedefined == 0)
ar->what = "main";
}
static const char *travtagmethods (global_State *G, const TObject *o) {
if (ttype(o) == LUA_TFUNCTION) {
int e;
for (e=0; e<TM_N; e++) {
int t;
for (t=0; t<G->ntag; t++)
if (clvalue(o) == luaT_gettm(G, t, e))
return luaT_eventname[e];
}
}
return NULL;
}
static const char *travglobals (lua_State *L, const TObject *o) {
Hash *g = L->gt;
int i;
2000-09-18 12:39:49 -07:00
for (i=0; i<g->size; i++) {
if (luaO_equalObj(o, val(node(g, i))) &&
2001-02-09 10:37:33 -08:00
ttype_key(node(g, i)) == LUA_TSTRING)
return getstr(tsvalue_key(node(g, i)));
}
return NULL;
}
2000-10-20 09:39:03 -07:00
static void getname (lua_State *L, StkId f, lua_Debug *ar) {
TObject o;
setnormalized(&o, f);
/* try to find a name for given function */
if ((ar->name = travglobals(L, &o)) != NULL)
ar->namewhat = "global";
/* not found: try tag methods */
else if ((ar->name = travtagmethods(G(L), &o)) != NULL)
2000-01-19 04:00:45 -08:00
ar->namewhat = "tag-method";
else ar->namewhat = ""; /* not found at all */
}
2000-10-20 09:39:03 -07:00
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
StkId func;
int isactive;
int status = 1;
LUA_LOCK(L);
isactive = (*what != '>');
2000-08-11 09:17:28 -07:00
if (isactive)
func = ar->_func;
else {
what++; /* skip the '>' */
2000-08-28 10:57:04 -07:00
func = L->top - 1;
}
2000-03-03 06:58:26 -08:00
for (; *what; what++) {
2000-01-19 04:00:45 -08:00
switch (*what) {
2000-08-11 09:17:28 -07:00
case 'S': {
2000-10-20 09:39:03 -07:00
funcinfo(L, ar, func);
2000-01-19 04:00:45 -08:00
break;
2000-08-11 09:17:28 -07:00
}
case 'l': {
2000-10-20 09:39:03 -07:00
ar->currentline = currentline(func);
2000-01-19 04:00:45 -08:00
break;
2000-08-11 09:17:28 -07:00
}
case 'u': {
2000-10-20 09:39:03 -07:00
ar->nups = nups(func);
2000-01-19 04:00:45 -08:00
break;
2000-08-11 09:17:28 -07:00
}
case 'n': {
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
2000-08-11 09:17:28 -07:00
if (ar->namewhat == NULL)
2000-10-20 09:39:03 -07:00
getname(L, func, ar);
2000-01-19 04:00:45 -08:00
break;
2000-08-11 09:17:28 -07:00
}
case 'f': {
2000-01-19 04:00:45 -08:00
setnormalized(L->top, func);
2000-08-28 10:57:04 -07:00
incr_top; /* push function */
2000-01-19 04:00:45 -08:00
break;
2000-08-11 09:17:28 -07:00
}
default: status = 0; /* invalid option */
2000-01-19 04:00:45 -08:00
}
}
2000-08-28 10:57:04 -07:00
if (!isactive) L->top--; /* pop function */
LUA_UNLOCK(L);
return status;
}
2000-01-19 04:00:45 -08:00
2000-06-28 13:21:06 -07:00
/*
** {======================================================
** Symbolic Execution
** =======================================================
*/
2000-08-10 12:50:47 -07:00
2001-02-09 10:37:33 -08:00
#define check(x) if (!(x)) return 0;
#define checkjump(pt, pc) check(0 <= (pc) && (pc) < (pt)->sizecode)
2000-08-10 12:50:47 -07:00
2000-06-28 13:21:06 -07:00
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
2000-08-10 12:50:47 -07:00
int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
2000-06-28 13:21:06 -07:00
const Instruction *code = pt->code;
int top = pt->numparams;
int pc = 0;
if (pt->is_vararg) /* varargs? */
top++; /* `arg' */
2001-02-09 10:37:33 -08:00
check (top <= pt->maxstacksize && pt->maxstacksize <= MAXSTACK);
2000-06-28 13:21:06 -07:00
while (pc < lastpc) {
const Instruction i = code[pc++];
2001-02-09 10:37:33 -08:00
OpCode op = GET_OPCODE(i);
int push = (int)luaK_opproperties[op].push;
int pop = (int)luaK_opproperties[op].pop;
int arg1 = 0;
int arg2 = 0;
switch ((enum Mode)luaK_opproperties[op].mode) {
case iO: break;
case iU: arg1 = GETARG_U(i); break;
case iS: arg1 = GETARG_S(i); break;
case iAB: arg1 = GETARG_A(i); arg2 = GETARG_B(i); break;
}
check(0 <= top && top <= pt->maxstacksize);
switch (op) {
2000-08-14 10:46:27 -07:00
case OP_RETURN: {
2001-02-09 10:37:33 -08:00
pop = top-arg1;
2000-08-14 10:46:27 -07:00
break;
}
2001-02-09 10:37:33 -08:00
case OP_CALL: {
if (arg2 == MULT_RET) arg2 = 1;
pop = top-arg1;
push = arg2;
2000-08-14 10:46:27 -07:00
break;
}
2001-02-09 10:37:33 -08:00
case OP_TAILCALL: {
check(arg1 <= top);
pop = top-arg2;
2000-06-28 13:21:06 -07:00
break;
}
case OP_PUSHNIL: {
2001-02-09 10:37:33 -08:00
check(arg1 > 0);
push = arg1;
2000-06-28 13:21:06 -07:00
break;
}
case OP_POP: {
2001-02-09 10:37:33 -08:00
pop = arg1;
break;
}
case OP_PUSHSTRING:
case OP_GETGLOBAL:
case OP_GETDOTTED:
case OP_PUSHSELF:
case OP_SETGLOBAL: {
check(arg1 < pt->sizekstr);
break;
}
case OP_PUSHNUM:
case OP_PUSHNEGNUM: {
check(arg1 < pt->sizeknum);
break;
}
case OP_PUSHUPVALUE: {
/* ?? */
break;
}
case OP_GETLOCAL:
case OP_GETINDEXED:
case OP_SETLOCAL: {
check(arg1 < top);
break;
}
case OP_SETTABLE: {
check(2 <= arg1 && arg1 <= top);
pop = arg2;
2000-06-28 13:21:06 -07:00
break;
}
case OP_SETLIST: {
2001-02-09 10:37:33 -08:00
pop = arg2;
check(top-pop >= 1); /* there must be a table below the list */
2000-06-28 13:21:06 -07:00
break;
}
case OP_SETMAP: {
2001-02-09 10:37:33 -08:00
pop = 2*arg1;
check(top-pop >= 1);
2000-06-28 13:21:06 -07:00
break;
}
case OP_CONCAT: {
2001-02-09 10:37:33 -08:00
pop = arg1;
2000-06-28 13:21:06 -07:00
break;
}
case OP_CLOSURE: {
2001-02-09 10:37:33 -08:00
/* ?? */
pop = arg2;
break;
}
case OP_JMPNE:
case OP_JMPEQ:
case OP_JMPLT:
case OP_JMPLE:
case OP_JMPGT:
case OP_JMPGE:
case OP_JMPT:
case OP_JMPF:
case OP_JMP:
case OP_FORLOOP:
case OP_LFORLOOP: {
checkjump(pt, pc+arg1);
break;
}
case OP_PUSHNILJMP: {
checkjump(pt, pc+1);
2000-06-28 13:21:06 -07:00
break;
}
2000-08-14 10:46:27 -07:00
case OP_JMPONT:
case OP_JMPONF: {
2001-02-09 10:37:33 -08:00
int newpc = pc + arg1;
checkjump(pt, newpc);
/* jump is forward and do not skip `lastpc' and not full check? */
if (pc < newpc && newpc <= lastpc && stackpos >= 0) {
2000-08-14 10:46:27 -07:00
stack[top-1] = pc-1; /* value comes from `and'/`or' */
pc = newpc; /* do the jump */
2001-02-09 10:37:33 -08:00
pop = 0; /* do not pop */
2000-08-14 10:46:27 -07:00
}
break;
}
2001-02-09 10:37:33 -08:00
case OP_FORPREP: {
check(top >= 3);
checkjump(pt, pc-arg1); /* jump is `negative' here */
break;
}
case OP_LFORPREP: {
check(top >= 1);
checkjump(pt, pc-arg1); /* jump is `negative' here */
break;
}
case OP_PUSHINT:
case OP_GETTABLE:
case OP_CREATETABLE:
case OP_ADD:
case OP_ADDI:
case OP_SUB:
case OP_MULT:
case OP_DIV:
case OP_POW:
case OP_MINUS:
case OP_NOT: {
break;
2000-06-28 13:21:06 -07:00
}
}
2001-02-09 10:37:33 -08:00
check(0 <= pop && 0 <= push);
check(0 <= top-pop && top+(push-pop) <= pt->maxstacksize);
top -= pop;
while (push--) stack[top++] = pc-1;
2000-06-28 13:21:06 -07:00
}
2001-02-09 10:37:33 -08:00
check(GET_OPCODE(code[pt->sizecode-1]) == OP_RETURN);
return (stackpos >= 0) ? code[stack[stackpos]] : 1;
}
int luaG_checkcode (const Proto *pt) {
return luaG_symbexec(pt, pt->sizecode-1, -1);
2000-06-28 13:21:06 -07:00
}
2000-08-10 12:50:47 -07:00
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
2000-06-28 13:21:06 -07:00
StkId func = aux_stackedfunction(L, 0, obj);
2000-10-05 05:14:08 -07:00
if (!isLmark(func))
return NULL; /* not an active Lua function */
2000-06-28 13:21:06 -07:00
else {
Proto *p = infovalue(func)->func->f.l;
2000-10-20 09:39:03 -07:00
int pc = currentpc(func);
2000-06-28 13:21:06 -07:00
int stackpos = obj - (func+1); /* func+1 == function base */
Instruction i = luaG_symbexec(p, pc, stackpos);
lua_assert(pc != -1);
2000-06-28 13:21:06 -07:00
switch (GET_OPCODE(i)) {
case OP_GETGLOBAL: {
*name = getstr(p->kstr[GETARG_U(i)]);
2000-06-28 13:21:06 -07:00
return "global";
}
case OP_GETLOCAL: {
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
lua_assert(*name);
return "local";
2000-06-28 13:21:06 -07:00
}
case OP_PUSHSELF:
case OP_GETDOTTED: {
*name = getstr(p->kstr[GETARG_U(i)]);
2000-06-28 13:21:06 -07:00
return "field";
}
default:
2000-08-15 11:28:48 -07:00
return NULL; /* no useful name found */
2000-06-28 13:21:06 -07:00
}
}
}
2000-08-11 09:17:28 -07:00
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
2000-10-05 05:14:08 -07:00
if (!isLmark(func))
return NULL; /* not an active Lua function */
2000-08-11 09:17:28 -07:00
else {
Proto *p = infovalue(func)->func->f.l;
2000-10-20 09:39:03 -07:00
int pc = currentpc(func);
Instruction i;
if (pc == -1) return NULL; /* function is not activated */
i = p->code[pc];
2000-08-11 09:17:28 -07:00
switch (GET_OPCODE(i)) {
case OP_CALL: case OP_TAILCALL:
return getobjname(L, (func+1)+GETARG_A(i), name);
default:
2000-08-15 11:28:48 -07:00
return NULL; /* no useful name found */
2000-08-11 09:17:28 -07:00
}
}
}
2000-06-28 13:21:06 -07:00
/* }====================================================== */
2000-01-19 04:00:45 -08:00
2000-08-10 12:50:47 -07:00
void luaG_typeerror (lua_State *L, StkId o, const char *op) {
2000-06-28 13:21:06 -07:00
const char *name;
2000-08-10 12:50:47 -07:00
const char *kind = getobjname(L, o, &name);
2001-01-25 08:45:36 -08:00
const char *t = luaT_typename(G(L), o);
2000-06-28 13:21:06 -07:00
if (kind)
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
2000-08-10 12:50:47 -07:00
op, kind, name, t);
2000-06-28 13:21:06 -07:00
else
luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
}
2000-10-05 05:14:08 -07:00
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
2000-08-10 12:50:47 -07:00
if (ttype(p1) == t) p1++;
lua_assert(ttype(p1) != t);
2000-08-10 12:50:47 -07:00
luaG_typeerror(L, p1, op);
}
2000-08-11 09:17:28 -07:00
2001-02-07 10:13:49 -08:00
void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
const char *t1 = luaT_typename(G(L), p1);
const char *t2 = luaT_typename(G(L), p2);
2000-08-11 09:17:28 -07:00
if (t1[2] == t2[2])
luaO_verror(L, "attempt to compare two %.10s values", t1);
2000-08-11 09:17:28 -07:00
else
luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
2000-08-11 09:17:28 -07:00
}