1 /* $NetBSD: ldblib.c,v 1.1.1.2 2012/03/15 00:08:10 alnsn Exp $ */ 2 3 /* 4 ** $Id: ldblib.c,v 1.1.1.2 2012/03/15 00:08:10 alnsn Exp $ 5 ** Interface from Lua to its debug API 6 ** See Copyright Notice in lua.h 7 */ 8 9 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #define ldblib_c 15 #define LUA_LIB 16 17 #include "lua.h" 18 19 #include "lauxlib.h" 20 #include "lualib.h" 21 22 23 24 static int db_getregistry (lua_State *L) { 25 lua_pushvalue(L, LUA_REGISTRYINDEX); 26 return 1; 27 } 28 29 30 static int db_getmetatable (lua_State *L) { 31 luaL_checkany(L, 1); 32 if (!lua_getmetatable(L, 1)) { 33 lua_pushnil(L); /* no metatable */ 34 } 35 return 1; 36 } 37 38 39 static int db_setmetatable (lua_State *L) { 40 int t = lua_type(L, 2); 41 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 42 "nil or table expected"); 43 lua_settop(L, 2); 44 lua_pushboolean(L, lua_setmetatable(L, 1)); 45 return 1; 46 } 47 48 49 static int db_getfenv (lua_State *L) { 50 luaL_checkany(L, 1); 51 lua_getfenv(L, 1); 52 return 1; 53 } 54 55 56 static int db_setfenv (lua_State *L) { 57 luaL_checktype(L, 2, LUA_TTABLE); 58 lua_settop(L, 2); 59 if (lua_setfenv(L, 1) == 0) 60 luaL_error(L, LUA_QL("setfenv") 61 " cannot change environment of given object"); 62 return 1; 63 } 64 65 66 static void settabss (lua_State *L, const char *i, const char *v) { 67 lua_pushstring(L, v); 68 lua_setfield(L, -2, i); 69 } 70 71 72 static void settabsi (lua_State *L, const char *i, int v) { 73 lua_pushinteger(L, v); 74 lua_setfield(L, -2, i); 75 } 76 77 78 static lua_State *getthread (lua_State *L, int *arg) { 79 if (lua_isthread(L, 1)) { 80 *arg = 1; 81 return lua_tothread(L, 1); 82 } 83 else { 84 *arg = 0; 85 return L; 86 } 87 } 88 89 90 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { 91 if (L == L1) { 92 lua_pushvalue(L, -2); 93 lua_remove(L, -3); 94 } 95 else 96 lua_xmove(L1, L, 1); 97 lua_setfield(L, -2, fname); 98 } 99 100 101 static int db_getinfo (lua_State *L) { 102 lua_Debug ar; 103 int arg; 104 lua_State *L1 = getthread(L, &arg); 105 const char *options = luaL_optstring(L, arg+2, "flnSu"); 106 if (lua_isnumber(L, arg+1)) { 107 if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { 108 lua_pushnil(L); /* level out of range */ 109 return 1; 110 } 111 } 112 else if (lua_isfunction(L, arg+1)) { 113 lua_pushfstring(L, ">%s", options); 114 options = lua_tostring(L, -1); 115 lua_pushvalue(L, arg+1); 116 lua_xmove(L, L1, 1); 117 } 118 else 119 return luaL_argerror(L, arg+1, "function or level expected"); 120 if (!lua_getinfo(L1, options, &ar)) 121 return luaL_argerror(L, arg+2, "invalid option"); 122 lua_createtable(L, 0, 2); 123 if (strchr(options, 'S')) { 124 settabss(L, "source", ar.source); 125 settabss(L, "short_src", ar.short_src); 126 settabsi(L, "linedefined", ar.linedefined); 127 settabsi(L, "lastlinedefined", ar.lastlinedefined); 128 settabss(L, "what", ar.what); 129 } 130 if (strchr(options, 'l')) 131 settabsi(L, "currentline", ar.currentline); 132 if (strchr(options, 'u')) 133 settabsi(L, "nups", ar.nups); 134 if (strchr(options, 'n')) { 135 settabss(L, "name", ar.name); 136 settabss(L, "namewhat", ar.namewhat); 137 } 138 if (strchr(options, 'L')) 139 treatstackoption(L, L1, "activelines"); 140 if (strchr(options, 'f')) 141 treatstackoption(L, L1, "func"); 142 return 1; /* return table */ 143 } 144 145 146 static int db_getlocal (lua_State *L) { 147 int arg; 148 lua_State *L1 = getthread(L, &arg); 149 lua_Debug ar; 150 const char *name; 151 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 152 return luaL_argerror(L, arg+1, "level out of range"); 153 name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); 154 if (name) { 155 lua_xmove(L1, L, 1); 156 lua_pushstring(L, name); 157 lua_pushvalue(L, -2); 158 return 2; 159 } 160 else { 161 lua_pushnil(L); 162 return 1; 163 } 164 } 165 166 167 static int db_setlocal (lua_State *L) { 168 int arg; 169 lua_State *L1 = getthread(L, &arg); 170 lua_Debug ar; 171 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 172 return luaL_argerror(L, arg+1, "level out of range"); 173 luaL_checkany(L, arg+3); 174 lua_settop(L, arg+3); 175 lua_xmove(L, L1, 1); 176 lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); 177 return 1; 178 } 179 180 181 static int auxupvalue (lua_State *L, int get) { 182 const char *name; 183 int n = luaL_checkint(L, 2); 184 luaL_checktype(L, 1, LUA_TFUNCTION); 185 if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ 186 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); 187 if (name == NULL) return 0; 188 lua_pushstring(L, name); 189 lua_insert(L, -(get+1)); 190 return get + 1; 191 } 192 193 194 static int db_getupvalue (lua_State *L) { 195 return auxupvalue(L, 1); 196 } 197 198 199 static int db_setupvalue (lua_State *L) { 200 luaL_checkany(L, 3); 201 return auxupvalue(L, 0); 202 } 203 204 205 206 static const char KEY_HOOK = 'h'; 207 208 209 static void hookf (lua_State *L, lua_Debug *ar) { 210 static const char *const hooknames[] = 211 {"call", "return", "line", "count", "tail return"}; 212 lua_pushlightuserdata(L, (void *)&KEY_HOOK); 213 lua_rawget(L, LUA_REGISTRYINDEX); 214 lua_pushlightuserdata(L, L); 215 lua_rawget(L, -2); 216 if (lua_isfunction(L, -1)) { 217 lua_pushstring(L, hooknames[(int)ar->event]); 218 if (ar->currentline >= 0) 219 lua_pushinteger(L, ar->currentline); 220 else lua_pushnil(L); 221 lua_assert(lua_getinfo(L, "lS", ar)); 222 lua_call(L, 2, 0); 223 } 224 } 225 226 227 static int makemask (const char *smask, int count) { 228 int mask = 0; 229 if (strchr(smask, 'c')) mask |= LUA_MASKCALL; 230 if (strchr(smask, 'r')) mask |= LUA_MASKRET; 231 if (strchr(smask, 'l')) mask |= LUA_MASKLINE; 232 if (count > 0) mask |= LUA_MASKCOUNT; 233 return mask; 234 } 235 236 237 static char *unmakemask (int mask, char *smask) { 238 int i = 0; 239 if (mask & LUA_MASKCALL) smask[i++] = 'c'; 240 if (mask & LUA_MASKRET) smask[i++] = 'r'; 241 if (mask & LUA_MASKLINE) smask[i++] = 'l'; 242 smask[i] = '\0'; 243 return smask; 244 } 245 246 247 static void gethooktable (lua_State *L) { 248 lua_pushlightuserdata(L, (void *)&KEY_HOOK); 249 lua_rawget(L, LUA_REGISTRYINDEX); 250 if (!lua_istable(L, -1)) { 251 lua_pop(L, 1); 252 lua_createtable(L, 0, 1); 253 lua_pushlightuserdata(L, (void *)&KEY_HOOK); 254 lua_pushvalue(L, -2); 255 lua_rawset(L, LUA_REGISTRYINDEX); 256 } 257 } 258 259 260 static int db_sethook (lua_State *L) { 261 int arg, mask, count; 262 lua_Hook func; 263 lua_State *L1 = getthread(L, &arg); 264 if (lua_isnoneornil(L, arg+1)) { 265 lua_settop(L, arg+1); 266 func = NULL; mask = 0; count = 0; /* turn off hooks */ 267 } 268 else { 269 const char *smask = luaL_checkstring(L, arg+2); 270 luaL_checktype(L, arg+1, LUA_TFUNCTION); 271 count = luaL_optint(L, arg+3, 0); 272 func = hookf; mask = makemask(smask, count); 273 } 274 gethooktable(L); 275 lua_pushlightuserdata(L, L1); 276 lua_pushvalue(L, arg+1); 277 lua_rawset(L, -3); /* set new hook */ 278 lua_pop(L, 1); /* remove hook table */ 279 lua_sethook(L1, func, mask, count); /* set hooks */ 280 return 0; 281 } 282 283 284 static int db_gethook (lua_State *L) { 285 int arg; 286 lua_State *L1 = getthread(L, &arg); 287 char buff[5]; 288 int mask = lua_gethookmask(L1); 289 lua_Hook hook = lua_gethook(L1); 290 if (hook != NULL && hook != hookf) /* external hook? */ 291 lua_pushliteral(L, "external hook"); 292 else { 293 gethooktable(L); 294 lua_pushlightuserdata(L, L1); 295 lua_rawget(L, -2); /* get hook */ 296 lua_remove(L, -2); /* remove hook table */ 297 } 298 lua_pushstring(L, unmakemask(mask, buff)); 299 lua_pushinteger(L, lua_gethookcount(L1)); 300 return 3; 301 } 302 303 304 static int db_debug (lua_State *L) { 305 for (;;) { 306 char buffer[250]; 307 fputs("lua_debug> ", stderr); 308 if (fgets(buffer, sizeof(buffer), stdin) == 0 || 309 strcmp(buffer, "cont\n") == 0) 310 return 0; 311 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 312 lua_pcall(L, 0, 0, 0)) { 313 fputs(lua_tostring(L, -1), stderr); 314 fputs("\n", stderr); 315 } 316 lua_settop(L, 0); /* remove eventual returns */ 317 } 318 } 319 320 321 #define LEVELS1 12 /* size of the first part of the stack */ 322 #define LEVELS2 10 /* size of the second part of the stack */ 323 324 static int db_errorfb (lua_State *L) { 325 int level; 326 int firstpart = 1; /* still before eventual `...' */ 327 int arg; 328 lua_State *L1 = getthread(L, &arg); 329 lua_Debug ar; 330 if (lua_isnumber(L, arg+2)) { 331 level = (int)lua_tointeger(L, arg+2); 332 lua_pop(L, 1); 333 } 334 else 335 level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ 336 if (lua_gettop(L) == arg) 337 lua_pushliteral(L, ""); 338 else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ 339 else lua_pushliteral(L, "\n"); 340 lua_pushliteral(L, "stack traceback:"); 341 while (lua_getstack(L1, level++, &ar)) { 342 if (level > LEVELS1 && firstpart) { 343 /* no more than `LEVELS2' more levels? */ 344 if (!lua_getstack(L1, level+LEVELS2, &ar)) 345 level--; /* keep going */ 346 else { 347 lua_pushliteral(L, "\n\t..."); /* too many levels */ 348 while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ 349 level++; 350 } 351 firstpart = 0; 352 continue; 353 } 354 lua_pushliteral(L, "\n\t"); 355 lua_getinfo(L1, "Snl", &ar); 356 lua_pushfstring(L, "%s:", ar.short_src); 357 if (ar.currentline > 0) 358 lua_pushfstring(L, "%d:", ar.currentline); 359 if (*ar.namewhat != '\0') /* is there a name? */ 360 lua_pushfstring(L, " in function " LUA_QS, ar.name); 361 else { 362 if (*ar.what == 'm') /* main? */ 363 lua_pushfstring(L, " in main chunk"); 364 else if (*ar.what == 'C' || *ar.what == 't') 365 lua_pushliteral(L, " ?"); /* C function or tail call */ 366 else 367 lua_pushfstring(L, " in function <%s:%d>", 368 ar.short_src, ar.linedefined); 369 } 370 lua_concat(L, lua_gettop(L) - arg); 371 } 372 lua_concat(L, lua_gettop(L) - arg); 373 return 1; 374 } 375 376 377 static const luaL_Reg dblib[] = { 378 {"debug", db_debug}, 379 {"getfenv", db_getfenv}, 380 {"gethook", db_gethook}, 381 {"getinfo", db_getinfo}, 382 {"getlocal", db_getlocal}, 383 {"getregistry", db_getregistry}, 384 {"getmetatable", db_getmetatable}, 385 {"getupvalue", db_getupvalue}, 386 {"setfenv", db_setfenv}, 387 {"sethook", db_sethook}, 388 {"setlocal", db_setlocal}, 389 {"setmetatable", db_setmetatable}, 390 {"setupvalue", db_setupvalue}, 391 {"traceback", db_errorfb}, 392 {NULL, NULL} 393 }; 394 395 396 LUALIB_API int luaopen_debug (lua_State *L) { 397 luaL_register(L, LUA_DBLIBNAME, dblib); 398 return 1; 399 } 400 401