rlm@1: /* rlm@1: ** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $ rlm@1: ** Interface from Lua to its debug API rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: rlm@1: #define ldblib_c rlm@1: #define LUA_LIB rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lauxlib.h" rlm@1: #include "lualib.h" rlm@1: rlm@1: rlm@1: rlm@1: static int db_getregistry (lua_State *L) { rlm@1: lua_pushvalue(L, LUA_REGISTRYINDEX); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_getmetatable (lua_State *L) { rlm@1: luaL_checkany(L, 1); rlm@1: if (!lua_getmetatable(L, 1)) { rlm@1: lua_pushnil(L); /* no metatable */ rlm@1: } rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_setmetatable (lua_State *L) { rlm@1: int t = lua_type(L, 2); rlm@1: luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, rlm@1: "nil or table expected"); rlm@1: lua_settop(L, 2); rlm@1: lua_pushboolean(L, lua_setmetatable(L, 1)); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_getfenv (lua_State *L) { rlm@1: lua_getfenv(L, 1); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_setfenv (lua_State *L) { rlm@1: luaL_checktype(L, 2, LUA_TTABLE); rlm@1: lua_settop(L, 2); rlm@1: if (lua_setfenv(L, 1) == 0) rlm@1: luaL_error(L, LUA_QL("setfenv") rlm@1: " cannot change environment of given object"); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static void settabss (lua_State *L, const char *i, const char *v) { rlm@1: lua_pushstring(L, v); rlm@1: lua_setfield(L, -2, i); rlm@1: } rlm@1: rlm@1: rlm@1: static void settabsi (lua_State *L, const char *i, int v) { rlm@1: lua_pushinteger(L, v); rlm@1: lua_setfield(L, -2, i); rlm@1: } rlm@1: rlm@1: rlm@1: static lua_State *getthread (lua_State *L, int *arg) { rlm@1: if (lua_isthread(L, 1)) { rlm@1: *arg = 1; rlm@1: return lua_tothread(L, 1); rlm@1: } rlm@1: else { rlm@1: *arg = 0; rlm@1: return L; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { rlm@1: if (L == L1) { rlm@1: lua_pushvalue(L, -2); rlm@1: lua_remove(L, -3); rlm@1: } rlm@1: else rlm@1: lua_xmove(L1, L, 1); rlm@1: lua_setfield(L, -2, fname); rlm@1: } rlm@1: rlm@1: rlm@1: static int db_getinfo (lua_State *L) { rlm@1: lua_Debug ar; rlm@1: int arg; rlm@1: lua_State *L1 = getthread(L, &arg); rlm@1: const char *options = luaL_optstring(L, arg+2, "flnSu"); rlm@1: if (lua_isnumber(L, arg+1)) { rlm@1: if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { rlm@1: lua_pushnil(L); /* level out of range */ rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: else if (lua_isfunction(L, arg+1)) { rlm@1: lua_pushfstring(L, ">%s", options); rlm@1: options = lua_tostring(L, -1); rlm@1: lua_pushvalue(L, arg+1); rlm@1: lua_xmove(L, L1, 1); rlm@1: } rlm@1: else rlm@1: return luaL_argerror(L, arg+1, "function or level expected"); rlm@1: if (!lua_getinfo(L1, options, &ar)) rlm@1: return luaL_argerror(L, arg+2, "invalid option"); rlm@1: lua_createtable(L, 0, 2); rlm@1: if (strchr(options, 'S')) { rlm@1: settabss(L, "source", ar.source); rlm@1: settabss(L, "short_src", ar.short_src); rlm@1: settabsi(L, "linedefined", ar.linedefined); rlm@1: settabsi(L, "lastlinedefined", ar.lastlinedefined); rlm@1: settabss(L, "what", ar.what); rlm@1: } rlm@1: if (strchr(options, 'l')) rlm@1: settabsi(L, "currentline", ar.currentline); rlm@1: if (strchr(options, 'u')) rlm@1: settabsi(L, "nups", ar.nups); rlm@1: if (strchr(options, 'n')) { rlm@1: settabss(L, "name", ar.name); rlm@1: settabss(L, "namewhat", ar.namewhat); rlm@1: } rlm@1: if (strchr(options, 'L')) rlm@1: treatstackoption(L, L1, "activelines"); rlm@1: if (strchr(options, 'f')) rlm@1: treatstackoption(L, L1, "func"); rlm@1: return 1; /* return table */ rlm@1: } rlm@1: rlm@1: rlm@1: static int db_getlocal (lua_State *L) { rlm@1: int arg; rlm@1: lua_State *L1 = getthread(L, &arg); rlm@1: lua_Debug ar; rlm@1: const char *name; rlm@1: if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ rlm@1: return luaL_argerror(L, arg+1, "level out of range"); rlm@1: name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); rlm@1: if (name) { rlm@1: lua_xmove(L1, L, 1); rlm@1: lua_pushstring(L, name); rlm@1: lua_pushvalue(L, -2); rlm@1: return 2; rlm@1: } rlm@1: else { rlm@1: lua_pushnil(L); rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int db_setlocal (lua_State *L) { rlm@1: int arg; rlm@1: lua_State *L1 = getthread(L, &arg); rlm@1: lua_Debug ar; rlm@1: if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ rlm@1: return luaL_argerror(L, arg+1, "level out of range"); rlm@1: luaL_checkany(L, arg+3); rlm@1: lua_settop(L, arg+3); rlm@1: lua_xmove(L, L1, 1); rlm@1: lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int auxupvalue (lua_State *L, int get) { rlm@1: const char *name; rlm@1: int n = luaL_checkint(L, 2); rlm@1: luaL_checktype(L, 1, LUA_TFUNCTION); rlm@1: if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ rlm@1: name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); rlm@1: if (name == NULL) return 0; rlm@1: lua_pushstring(L, name); rlm@1: lua_insert(L, -(get+1)); rlm@1: return get + 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_getupvalue (lua_State *L) { rlm@1: return auxupvalue(L, 1); rlm@1: } rlm@1: rlm@1: rlm@1: static int db_setupvalue (lua_State *L) { rlm@1: luaL_checkany(L, 3); rlm@1: return auxupvalue(L, 0); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: static const char KEY_HOOK = 'h'; rlm@1: rlm@1: rlm@1: static void hookf (lua_State *L, lua_Debug *ar) { rlm@1: static const char *const hooknames[] = rlm@1: {"call", "return", "line", "count", "tail return"}; rlm@1: lua_pushlightuserdata(L, (void *)&KEY_HOOK); rlm@1: lua_rawget(L, LUA_REGISTRYINDEX); rlm@1: lua_pushlightuserdata(L, L); rlm@1: lua_rawget(L, -2); rlm@1: if (lua_isfunction(L, -1)) { rlm@1: lua_pushstring(L, hooknames[(int)ar->event]); rlm@1: if (ar->currentline >= 0) rlm@1: lua_pushinteger(L, ar->currentline); rlm@1: else lua_pushnil(L); rlm@1: lua_assert(lua_getinfo(L, "lS", ar)); rlm@1: lua_call(L, 2, 0); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int makemask (const char *smask, int count) { rlm@1: int mask = 0; rlm@1: if (strchr(smask, 'c')) mask |= LUA_MASKCALL; rlm@1: if (strchr(smask, 'r')) mask |= LUA_MASKRET; rlm@1: if (strchr(smask, 'l')) mask |= LUA_MASKLINE; rlm@1: if (count > 0) mask |= LUA_MASKCOUNT; rlm@1: return mask; rlm@1: } rlm@1: rlm@1: rlm@1: static char *unmakemask (int mask, char *smask) { rlm@1: int i = 0; rlm@1: if (mask & LUA_MASKCALL) smask[i++] = 'c'; rlm@1: if (mask & LUA_MASKRET) smask[i++] = 'r'; rlm@1: if (mask & LUA_MASKLINE) smask[i++] = 'l'; rlm@1: smask[i] = '\0'; rlm@1: return smask; rlm@1: } rlm@1: rlm@1: rlm@1: static void gethooktable (lua_State *L) { rlm@1: lua_pushlightuserdata(L, (void *)&KEY_HOOK); rlm@1: lua_rawget(L, LUA_REGISTRYINDEX); rlm@1: if (!lua_istable(L, -1)) { rlm@1: lua_pop(L, 1); rlm@1: lua_createtable(L, 0, 1); rlm@1: lua_pushlightuserdata(L, (void *)&KEY_HOOK); rlm@1: lua_pushvalue(L, -2); rlm@1: lua_rawset(L, LUA_REGISTRYINDEX); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int db_sethook (lua_State *L) { rlm@1: int arg, mask, count; rlm@1: lua_Hook func; rlm@1: lua_State *L1 = getthread(L, &arg); rlm@1: if (lua_isnoneornil(L, arg+1)) { rlm@1: lua_settop(L, arg+1); rlm@1: func = NULL; mask = 0; count = 0; /* turn off hooks */ rlm@1: } rlm@1: else { rlm@1: const char *smask = luaL_checkstring(L, arg+2); rlm@1: luaL_checktype(L, arg+1, LUA_TFUNCTION); rlm@1: count = luaL_optint(L, arg+3, 0); rlm@1: func = hookf; mask = makemask(smask, count); rlm@1: } rlm@1: gethooktable(L); rlm@1: lua_pushlightuserdata(L, L1); rlm@1: lua_pushvalue(L, arg+1); rlm@1: lua_rawset(L, -3); /* set new hook */ rlm@1: lua_pop(L, 1); /* remove hook table */ rlm@1: lua_sethook(L1, func, mask, count); /* set hooks */ rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_gethook (lua_State *L) { rlm@1: int arg; rlm@1: lua_State *L1 = getthread(L, &arg); rlm@1: char buff[5]; rlm@1: int mask = lua_gethookmask(L1); rlm@1: lua_Hook hook = lua_gethook(L1); rlm@1: if (hook != NULL && hook != hookf) /* external hook? */ rlm@1: lua_pushliteral(L, "external hook"); rlm@1: else { rlm@1: gethooktable(L); rlm@1: lua_pushlightuserdata(L, L1); rlm@1: lua_rawget(L, -2); /* get hook */ rlm@1: lua_remove(L, -2); /* remove hook table */ rlm@1: } rlm@1: lua_pushstring(L, unmakemask(mask, buff)); rlm@1: lua_pushinteger(L, lua_gethookcount(L1)); rlm@1: return 3; rlm@1: } rlm@1: rlm@1: rlm@1: static int db_debug (lua_State *L) { rlm@1: for (;;) { rlm@1: char buffer[250]; rlm@1: fputs("lua_debug> ", stderr); rlm@1: if (fgets(buffer, sizeof(buffer), stdin) == 0 || rlm@1: strcmp(buffer, "cont\n") == 0) rlm@1: return 0; rlm@1: if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || rlm@1: lua_pcall(L, 0, 0, 0)) { rlm@1: fputs(lua_tostring(L, -1), stderr); rlm@1: fputs("\n", stderr); rlm@1: } rlm@1: lua_settop(L, 0); /* remove eventual returns */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: #define LEVELS1 12 /* size of the first part of the stack */ rlm@1: #define LEVELS2 10 /* size of the second part of the stack */ rlm@1: rlm@1: static int db_errorfb (lua_State *L) { rlm@1: int level; rlm@1: int firstpart = 1; /* still before eventual `...' */ rlm@1: int arg; rlm@1: lua_State *L1 = getthread(L, &arg); rlm@1: lua_Debug ar; rlm@1: if (lua_isnumber(L, arg+2)) { rlm@1: level = (int)lua_tointeger(L, arg+2); rlm@1: lua_pop(L, 1); rlm@1: } rlm@1: else rlm@1: level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ rlm@1: if (lua_gettop(L) == arg) rlm@1: lua_pushliteral(L, ""); rlm@1: else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ rlm@1: else lua_pushliteral(L, "\n"); rlm@1: lua_pushliteral(L, "stack traceback:"); rlm@1: while (lua_getstack(L1, level++, &ar)) { rlm@1: if (level > LEVELS1 && firstpart) { rlm@1: /* no more than `LEVELS2' more levels? */ rlm@1: if (!lua_getstack(L1, level+LEVELS2, &ar)) rlm@1: level--; /* keep going */ rlm@1: else { rlm@1: lua_pushliteral(L, "\n\t..."); /* too many levels */ rlm@1: while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ rlm@1: level++; rlm@1: } rlm@1: firstpart = 0; rlm@1: continue; rlm@1: } rlm@1: lua_pushliteral(L, "\n\t"); rlm@1: lua_getinfo(L1, "Snl", &ar); rlm@1: lua_pushfstring(L, "%s:", ar.short_src); rlm@1: if (ar.currentline > 0) rlm@1: lua_pushfstring(L, "%d:", ar.currentline); rlm@1: if (*ar.namewhat != '\0') /* is there a name? */ rlm@1: lua_pushfstring(L, " in function " LUA_QS, ar.name); rlm@1: else { rlm@1: if (*ar.what == 'm') /* main? */ rlm@1: lua_pushfstring(L, " in main chunk"); rlm@1: else if (*ar.what == 'C' || *ar.what == 't') rlm@1: lua_pushliteral(L, " ?"); /* C function or tail call */ rlm@1: else rlm@1: lua_pushfstring(L, " in function <%s:%d>", rlm@1: ar.short_src, ar.linedefined); rlm@1: } rlm@1: lua_concat(L, lua_gettop(L) - arg); rlm@1: } rlm@1: lua_concat(L, lua_gettop(L) - arg); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static const luaL_Reg dblib[] = { rlm@1: {"debug", db_debug}, rlm@1: {"getfenv", db_getfenv}, rlm@1: {"gethook", db_gethook}, rlm@1: {"getinfo", db_getinfo}, rlm@1: {"getlocal", db_getlocal}, rlm@1: {"getregistry", db_getregistry}, rlm@1: {"getmetatable", db_getmetatable}, rlm@1: {"getupvalue", db_getupvalue}, rlm@1: {"setfenv", db_setfenv}, rlm@1: {"sethook", db_sethook}, rlm@1: {"setlocal", db_setlocal}, rlm@1: {"setmetatable", db_setmetatable}, rlm@1: {"setupvalue", db_setupvalue}, rlm@1: {"traceback", db_errorfb}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: rlm@1: LUALIB_API int luaopen_debug (lua_State *L) { rlm@1: luaL_register(L, LUA_DBLIBNAME, dblib); rlm@1: return 1; rlm@1: } rlm@1: