00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011
00012 #define ldblib_c
00013 #define LUA_LIB
00014
00015 #include "lua.h"
00016
00017 #include "lauxlib.h"
00018 #include "lualib.h"
00019
00020
00021
00022 static int db_getregistry (lua_State *L) {
00023 lua_pushvalue(L, LUA_REGISTRYINDEX);
00024 return 1;
00025 }
00026
00027
00028 static int db_getmetatable (lua_State *L) {
00029 luaL_checkany(L, 1);
00030 if (!lua_getmetatable(L, 1)) {
00031 lua_pushnil(L);
00032 }
00033 return 1;
00034 }
00035
00036
00037 static int db_setmetatable (lua_State *L) {
00038 int t = lua_type(L, 2);
00039 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
00040 "nil or table expected");
00041 lua_settop(L, 2);
00042 lua_pushboolean(L, lua_setmetatable(L, 1));
00043 return 1;
00044 }
00045
00046
00047 static int db_getfenv (lua_State *L) {
00048 luaL_checkany(L, 1);
00049 lua_getfenv(L, 1);
00050 return 1;
00051 }
00052
00053
00054 static int db_setfenv (lua_State *L) {
00055 luaL_checktype(L, 2, LUA_TTABLE);
00056 lua_settop(L, 2);
00057 if (lua_setfenv(L, 1) == 0)
00058 luaL_error(L, LUA_QL("setfenv")
00059 " cannot change environment of given object");
00060 return 1;
00061 }
00062
00063
00064 static void settabss (lua_State *L, const char *i, const char *v) {
00065 lua_pushstring(L, v);
00066 lua_setfield(L, -2, i);
00067 }
00068
00069
00070 static void settabsi (lua_State *L, const char *i, int v) {
00071 lua_pushinteger(L, v);
00072 lua_setfield(L, -2, i);
00073 }
00074
00075
00076 static lua_State *getthread (lua_State *L, int *arg) {
00077 if (lua_isthread(L, 1)) {
00078 *arg = 1;
00079 return lua_tothread(L, 1);
00080 }
00081 else {
00082 *arg = 0;
00083 return L;
00084 }
00085 }
00086
00087
00088 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
00089 if (L == L1) {
00090 lua_pushvalue(L, -2);
00091 lua_remove(L, -3);
00092 }
00093 else
00094 lua_xmove(L1, L, 1);
00095 lua_setfield(L, -2, fname);
00096 }
00097
00098
00099 static int db_getinfo (lua_State *L) {
00100 lua_Debug ar;
00101 int arg;
00102 lua_State *L1 = getthread(L, &arg);
00103 const char *options = luaL_optstring(L, arg+2, "flnSu");
00104 if (lua_isnumber(L, arg+1)) {
00105 if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
00106 lua_pushnil(L);
00107 return 1;
00108 }
00109 }
00110 else if (lua_isfunction(L, arg+1)) {
00111 lua_pushfstring(L, ">%s", options);
00112 options = lua_tostring(L, -1);
00113 lua_pushvalue(L, arg+1);
00114 lua_xmove(L, L1, 1);
00115 }
00116 else
00117 return luaL_argerror(L, arg+1, "function or level expected");
00118 if (!lua_getinfo(L1, options, &ar))
00119 return luaL_argerror(L, arg+2, "invalid option");
00120 lua_createtable(L, 0, 2);
00121 if (strchr(options, 'S')) {
00122 settabss(L, "source", ar.source);
00123 settabss(L, "short_src", ar.short_src);
00124 settabsi(L, "linedefined", ar.linedefined);
00125 settabsi(L, "lastlinedefined", ar.lastlinedefined);
00126 settabss(L, "what", ar.what);
00127 }
00128 if (strchr(options, 'l'))
00129 settabsi(L, "currentline", ar.currentline);
00130 if (strchr(options, 'u'))
00131 settabsi(L, "nups", ar.nups);
00132 if (strchr(options, 'n')) {
00133 settabss(L, "name", ar.name);
00134 settabss(L, "namewhat", ar.namewhat);
00135 }
00136 if (strchr(options, 'L'))
00137 treatstackoption(L, L1, "activelines");
00138 if (strchr(options, 'f'))
00139 treatstackoption(L, L1, "func");
00140 return 1;
00141 }
00142
00143
00144 static int db_getlocal (lua_State *L) {
00145 int arg;
00146 lua_State *L1 = getthread(L, &arg);
00147 lua_Debug ar;
00148 const char *name;
00149 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))
00150 return luaL_argerror(L, arg+1, "level out of range");
00151 name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
00152 if (name) {
00153 lua_xmove(L1, L, 1);
00154 lua_pushstring(L, name);
00155 lua_pushvalue(L, -2);
00156 return 2;
00157 }
00158 else {
00159 lua_pushnil(L);
00160 return 1;
00161 }
00162 }
00163
00164
00165 static int db_setlocal (lua_State *L) {
00166 int arg;
00167 lua_State *L1 = getthread(L, &arg);
00168 lua_Debug ar;
00169 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))
00170 return luaL_argerror(L, arg+1, "level out of range");
00171 luaL_checkany(L, arg+3);
00172 lua_settop(L, arg+3);
00173 lua_xmove(L, L1, 1);
00174 lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
00175 return 1;
00176 }
00177
00178
00179 static int auxupvalue (lua_State *L, int get) {
00180 const char *name;
00181 int n = luaL_checkint(L, 2);
00182 luaL_checktype(L, 1, LUA_TFUNCTION);
00183 if (lua_iscfunction(L, 1)) return 0;
00184 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
00185 if (name == NULL) return 0;
00186 lua_pushstring(L, name);
00187 lua_insert(L, -(get+1));
00188 return get + 1;
00189 }
00190
00191
00192 static int db_getupvalue (lua_State *L) {
00193 return auxupvalue(L, 1);
00194 }
00195
00196
00197 static int db_setupvalue (lua_State *L) {
00198 luaL_checkany(L, 3);
00199 return auxupvalue(L, 0);
00200 }
00201
00202
00203
00204 static char KEY_HOOK = 'h';
00205
00206
00207 static void hookf (lua_State *L, lua_Debug *ar) {
00208 static const char *const hooknames[] =
00209 {"call", "return", "line", "count", "tail return"};
00210 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
00211 lua_rawget(L, LUA_REGISTRYINDEX);
00212 lua_pushlightuserdata(L, L);
00213 lua_rawget(L, -2);
00214 if (lua_isfunction(L, -1)) {
00215 lua_pushstring(L, hooknames[(int)ar->event]);
00216 if (ar->currentline >= 0)
00217 lua_pushinteger(L, ar->currentline);
00218 else lua_pushnil(L);
00219 lua_assert(lua_getinfo(L, "lS", ar));
00220 lua_call(L, 2, 0);
00221 }
00222 }
00223
00224
00225 static int makemask (const char *smask, int count) {
00226 int mask = 0;
00227 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
00228 if (strchr(smask, 'r')) mask |= LUA_MASKRET;
00229 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
00230 if (count > 0) mask |= LUA_MASKCOUNT;
00231 return mask;
00232 }
00233
00234
00235 static char *unmakemask (int mask, char *smask) {
00236 int i = 0;
00237 if (mask & LUA_MASKCALL) smask[i++] = 'c';
00238 if (mask & LUA_MASKRET) smask[i++] = 'r';
00239 if (mask & LUA_MASKLINE) smask[i++] = 'l';
00240 smask[i] = '\0';
00241 return smask;
00242 }
00243
00244
00245 static void gethooktable (lua_State *L) {
00246 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
00247 lua_rawget(L, LUA_REGISTRYINDEX);
00248 if (!lua_istable(L, -1)) {
00249 lua_pop(L, 1);
00250 lua_createtable(L, 0, 1);
00251 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
00252 lua_pushvalue(L, -2);
00253 lua_rawset(L, LUA_REGISTRYINDEX);
00254 }
00255 }
00256
00257
00258 static int db_sethook (lua_State *L) {
00259 int arg, mask, count;
00260 lua_Hook func;
00261 lua_State *L1 = getthread(L, &arg);
00262 if (lua_isnoneornil(L, arg+1)) {
00263 lua_settop(L, arg+1);
00264 func = NULL; mask = 0; count = 0;
00265 }
00266 else {
00267 const char *smask = luaL_checkstring(L, arg+2);
00268 luaL_checktype(L, arg+1, LUA_TFUNCTION);
00269 count = luaL_optint(L, arg+3, 0);
00270 func = hookf; mask = makemask(smask, count);
00271 }
00272 gethooktable(L);
00273 lua_pushlightuserdata(L, L1);
00274 lua_pushvalue(L, arg+1);
00275 lua_rawset(L, -3);
00276 lua_pop(L, 1);
00277 lua_sethook(L1, func, mask, count);
00278 return 0;
00279 }
00280
00281
00282 static int db_gethook (lua_State *L) {
00283 int arg;
00284 lua_State *L1 = getthread(L, &arg);
00285 char buff[5];
00286 int mask = lua_gethookmask(L1);
00287 lua_Hook hook = lua_gethook(L1);
00288 if (hook != NULL && hook != hookf)
00289 lua_pushliteral(L, "external hook");
00290 else {
00291 gethooktable(L);
00292 lua_pushlightuserdata(L, L1);
00293 lua_rawget(L, -2);
00294 lua_remove(L, -2);
00295 }
00296 lua_pushstring(L, unmakemask(mask, buff));
00297 lua_pushinteger(L, lua_gethookcount(L1));
00298 return 3;
00299 }
00300
00301
00302 static int db_debug (lua_State *L) {
00303 for (;;) {
00304 char buffer[250];
00305 fputs("lua_debug> ", stderr);
00306 if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
00307 strcmp(buffer, "cont\n") == 0)
00308 return 0;
00309 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
00310 lua_pcall(L, 0, 0, 0)) {
00311 fputs(lua_tostring(L, -1), stderr);
00312 fputs("\n", stderr);
00313 }
00314 lua_settop(L, 0);
00315 }
00316 }
00317
00318
00319 #define LEVELS1 12
00320 #define LEVELS2 10
00321
00322 static int db_errorfb (lua_State *L) {
00323 int level;
00324 int firstpart = 1;
00325 int arg;
00326 lua_State *L1 = getthread(L, &arg);
00327 lua_Debug ar;
00328 if (lua_isnumber(L, arg+2)) {
00329 level = (int)lua_tointeger(L, arg+2);
00330 lua_pop(L, 1);
00331 }
00332 else
00333 level = (L == L1) ? 1 : 0;
00334 if (lua_gettop(L) == arg)
00335 lua_pushliteral(L, "");
00336 else if (!lua_isstring(L, arg+1)) return 1;
00337 else lua_pushliteral(L, "\n");
00338 lua_pushliteral(L, "stack traceback:");
00339 while (lua_getstack(L1, level++, &ar)) {
00340 if (level > LEVELS1 && firstpart) {
00341
00342 if (!lua_getstack(L1, level+LEVELS2, &ar))
00343 level--;
00344 else {
00345 lua_pushliteral(L, "\n\t...");
00346 while (lua_getstack(L1, level+LEVELS2, &ar))
00347 level++;
00348 }
00349 firstpart = 0;
00350 continue;
00351 }
00352 lua_pushliteral(L, "\n\t");
00353 lua_getinfo(L1, "Snl", &ar);
00354 lua_pushfstring(L, "%s:", ar.short_src);
00355 if (ar.currentline > 0)
00356 lua_pushfstring(L, "%d:", ar.currentline);
00357 if (*ar.namewhat != '\0')
00358 lua_pushfstring(L, " in function " LUA_QS, ar.name);
00359 else {
00360 if (*ar.what == 'm')
00361 lua_pushfstring(L, " in main chunk");
00362 else if (*ar.what == 'C' || *ar.what == 't')
00363 lua_pushliteral(L, " ?");
00364 else
00365 lua_pushfstring(L, " in function <%s:%d>",
00366 ar.short_src, ar.linedefined);
00367 }
00368 lua_concat(L, lua_gettop(L) - arg);
00369 }
00370 lua_concat(L, lua_gettop(L) - arg);
00371 return 1;
00372 }
00373
00374
00375 static const luaL_Reg dblib[] = {
00376 {"debug", db_debug},
00377 {"getfenv", db_getfenv},
00378 {"gethook", db_gethook},
00379 {"getinfo", db_getinfo},
00380 {"getlocal", db_getlocal},
00381 {"getregistry", db_getregistry},
00382 {"getmetatable", db_getmetatable},
00383 {"getupvalue", db_getupvalue},
00384 {"setfenv", db_setfenv},
00385 {"sethook", db_sethook},
00386 {"setlocal", db_setlocal},
00387 {"setmetatable", db_setmetatable},
00388 {"setupvalue", db_setupvalue},
00389 {"traceback", db_errorfb},
00390 {NULL, NULL}
00391 };
00392
00393
00394 LUALIB_API int luaopen_debug (lua_State *L) {
00395 luaL_register(L, LUA_DBLIBNAME, dblib);
00396 return 1;
00397 }