1 /* $NetBSD: ldblib.c,v 1.4 2014/07/19 19:37:31 lneto Exp $ */ 2 3 /* 4 ** $Id: ldblib.c,v 1.4 2014/07/19 19:37:31 lneto Exp $ 5 ** Interface from Lua to its debug API 6 ** See Copyright Notice in lua.h 7 */ 8 9 10 #ifndef _KERNEL 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #endif 15 16 #define ldblib_c 17 #define LUA_LIB 18 19 #include "lua.h" 20 21 #include "lauxlib.h" 22 #include "lualib.h" 23 24 25 #define HOOKKEY "_HKEY" 26 27 28 static int db_Csize (lua_State *L) { 29 static struct { 30 char c; 31 unsigned char sz; 32 } sizes[] = { 33 {'I', sizeof(lua_Integer)}, 34 {'F', sizeof(lua_Number)}, 35 {'b', CHAR_BIT}, /* here is number of bits (not bytes) */ 36 {'h', sizeof(short)}, 37 {'i', sizeof(int)}, 38 {'l', sizeof(long)}, 39 {'z', sizeof(size_t)}, 40 {'f', sizeof(float)}, 41 {'d', sizeof(double)} 42 }; 43 const char *s = luaL_checkstring(L, 1); 44 int i; 45 for (i = 0; i < (int)(sizeof(sizes)/sizeof(sizes[0])); i++) { 46 if (*s == sizes[i].c) { 47 lua_pushinteger(L, sizes[i].sz); 48 return 1; 49 } 50 } 51 return luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%c'", *s)); 52 } 53 54 55 static int db_getregistry (lua_State *L) { 56 lua_pushvalue(L, LUA_REGISTRYINDEX); 57 return 1; 58 } 59 60 61 static int db_getmetatable (lua_State *L) { 62 luaL_checkany(L, 1); 63 if (!lua_getmetatable(L, 1)) { 64 lua_pushnil(L); /* no metatable */ 65 } 66 return 1; 67 } 68 69 70 static int db_setmetatable (lua_State *L) { 71 int t = lua_type(L, 2); 72 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 73 "nil or table expected"); 74 lua_settop(L, 2); 75 lua_setmetatable(L, 1); 76 return 1; /* return 1st argument */ 77 } 78 79 80 static int db_getuservalue (lua_State *L) { 81 if (lua_type(L, 1) != LUA_TUSERDATA) 82 lua_pushnil(L); 83 else 84 lua_getuservalue(L, 1); 85 return 1; 86 } 87 88 89 static int db_setuservalue (lua_State *L) { 90 luaL_checktype(L, 1, LUA_TUSERDATA); 91 luaL_checkany(L, 2); 92 lua_settop(L, 2); 93 lua_setuservalue(L, 1); 94 return 1; 95 } 96 97 98 /* 99 ** Auxiliary function used by several library functions: check for 100 ** an optional thread as function's first argument and set 'arg' with 101 ** 1 if this argument is present (so that functions can skip it to 102 ** access their other arguments) 103 */ 104 static lua_State *getthread (lua_State *L, int *arg) { 105 if (lua_isthread(L, 1)) { 106 *arg = 1; 107 return lua_tothread(L, 1); 108 } 109 else { 110 *arg = 0; 111 return L; /* function will operate over current thread */ 112 } 113 } 114 115 116 /* 117 ** Variations of 'lua_settable', used by 'db_getinfo' to put results 118 ** from 'lua_getinfo' into result table. Key is always a string; 119 ** value can be a string, an int, or a boolean. 120 */ 121 static void settabss (lua_State *L, const char *k, const char *v) { 122 lua_pushstring(L, v); 123 lua_setfield(L, -2, k); 124 } 125 126 static void settabsi (lua_State *L, const char *k, int v) { 127 lua_pushinteger(L, v); 128 lua_setfield(L, -2, k); 129 } 130 131 static void settabsb (lua_State *L, const char *k, int v) { 132 lua_pushboolean(L, v); 133 lua_setfield(L, -2, k); 134 } 135 136 137 /* 138 ** In function 'db_getinfo', the call to 'lua_getinfo' may push 139 ** results on the stack; later it creates the result table to put 140 ** these objects. Function 'treatstackoption' puts the result from 141 ** 'lua_getinfo' on top of the result table so that it can call 142 ** 'lua_setfield'. 143 */ 144 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { 145 if (L == L1) 146 lua_rotate(L, -2, 1); /* exchange object and table */ 147 else 148 lua_xmove(L1, L, 1); /* move object to the "main" stack */ 149 lua_setfield(L, -2, fname); /* put object into table */ 150 } 151 152 153 /* 154 ** Calls 'lua_getinfo' and collects all results in a new table. 155 */ 156 static int db_getinfo (lua_State *L) { 157 lua_Debug ar; 158 int arg; 159 lua_State *L1 = getthread(L, &arg); 160 const char *options = luaL_optstring(L, arg+2, "flnStu"); 161 if (lua_isfunction(L, arg + 1)) { /* info about a function? */ 162 options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ 163 lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ 164 lua_xmove(L, L1, 1); 165 } 166 else { /* stack level */ 167 if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) { 168 lua_pushnil(L); /* level out of range */ 169 return 1; 170 } 171 } 172 if (!lua_getinfo(L1, options, &ar)) 173 return luaL_argerror(L, arg+2, "invalid option"); 174 lua_newtable(L); /* table to collect results */ 175 if (strchr(options, 'S')) { 176 settabss(L, "source", ar.source); 177 settabss(L, "short_src", ar.short_src); 178 settabsi(L, "linedefined", ar.linedefined); 179 settabsi(L, "lastlinedefined", ar.lastlinedefined); 180 settabss(L, "what", ar.what); 181 } 182 if (strchr(options, 'l')) 183 settabsi(L, "currentline", ar.currentline); 184 if (strchr(options, 'u')) { 185 settabsi(L, "nups", ar.nups); 186 settabsi(L, "nparams", ar.nparams); 187 settabsb(L, "isvararg", ar.isvararg); 188 } 189 if (strchr(options, 'n')) { 190 settabss(L, "name", ar.name); 191 settabss(L, "namewhat", ar.namewhat); 192 } 193 if (strchr(options, 't')) 194 settabsb(L, "istailcall", ar.istailcall); 195 if (strchr(options, 'L')) 196 treatstackoption(L, L1, "activelines"); 197 if (strchr(options, 'f')) 198 treatstackoption(L, L1, "func"); 199 return 1; /* return table */ 200 } 201 202 203 static int db_getlocal (lua_State *L) { 204 int arg; 205 lua_State *L1 = getthread(L, &arg); 206 lua_Debug ar; 207 const char *name; 208 int nvar = luaL_checkint(L, arg+2); /* local-variable index */ 209 if (lua_isfunction(L, arg + 1)) { /* function argument? */ 210 lua_pushvalue(L, arg + 1); /* push function */ 211 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ 212 return 1; /* return only name (there is no value) */ 213 } 214 else { /* stack-level argument */ 215 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 216 return luaL_argerror(L, arg+1, "level out of range"); 217 name = lua_getlocal(L1, &ar, nvar); 218 if (name) { 219 lua_xmove(L1, L, 1); /* move local value */ 220 lua_pushstring(L, name); /* push name */ 221 lua_rotate(L, -2, 1); /* re-order */ 222 return 2; 223 } 224 else { 225 lua_pushnil(L); /* no name (nor value) */ 226 return 1; 227 } 228 } 229 } 230 231 232 static int db_setlocal (lua_State *L) { 233 int arg; 234 lua_State *L1 = getthread(L, &arg); 235 lua_Debug ar; 236 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 237 return luaL_argerror(L, arg+1, "level out of range"); 238 luaL_checkany(L, arg+3); 239 lua_settop(L, arg+3); 240 lua_xmove(L, L1, 1); 241 lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); 242 return 1; 243 } 244 245 246 /* 247 ** get (if 'get' is true) or set an upvalue from a closure 248 */ 249 static int auxupvalue (lua_State *L, int get) { 250 const char *name; 251 int n = luaL_checkint(L, 2); /* upvalue index */ 252 luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ 253 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); 254 if (name == NULL) return 0; 255 lua_pushstring(L, name); 256 lua_insert(L, -(get+1)); /* no-op if get is false */ 257 return get + 1; 258 } 259 260 261 static int db_getupvalue (lua_State *L) { 262 return auxupvalue(L, 1); 263 } 264 265 266 static int db_setupvalue (lua_State *L) { 267 luaL_checkany(L, 3); 268 return auxupvalue(L, 0); 269 } 270 271 272 /* 273 ** Check whether a given upvalue from a given closure exists and 274 ** returns its index 275 */ 276 static int checkupval (lua_State *L, int argf, int argnup) { 277 int nup = luaL_checkint(L, argnup); /* upvalue index */ 278 luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ 279 luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, 280 "invalid upvalue index"); 281 return nup; 282 } 283 284 285 static int db_upvalueid (lua_State *L) { 286 int n = checkupval(L, 1, 2); 287 lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); 288 return 1; 289 } 290 291 292 static int db_upvaluejoin (lua_State *L) { 293 int n1 = checkupval(L, 1, 2); 294 int n2 = checkupval(L, 3, 4); 295 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); 296 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); 297 lua_upvaluejoin(L, 1, n1, 3, n2); 298 return 0; 299 } 300 301 302 /* 303 ** The hook table (at registry[HOOKKEY]) maps threads to their current 304 ** hook function 305 */ 306 #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) 307 308 309 /* 310 ** Call hook function registered at hook table for the current 311 ** thread (if there is one) 312 */ 313 static void hookf (lua_State *L, lua_Debug *ar) { 314 static const char *const hooknames[] = 315 {"call", "return", "line", "count", "tail call"}; 316 gethooktable(L); 317 lua_pushthread(L); 318 if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ 319 lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ 320 if (ar->currentline >= 0) 321 lua_pushinteger(L, ar->currentline); /* push current line */ 322 else lua_pushnil(L); 323 lua_assert(lua_getinfo(L, "lS", ar)); 324 lua_call(L, 2, 0); /* call hook function */ 325 } 326 } 327 328 329 /* 330 ** Convert a string mask (for 'sethook') into a bit mask 331 */ 332 static int makemask (const char *smask, int count) { 333 int mask = 0; 334 if (strchr(smask, 'c')) mask |= LUA_MASKCALL; 335 if (strchr(smask, 'r')) mask |= LUA_MASKRET; 336 if (strchr(smask, 'l')) mask |= LUA_MASKLINE; 337 if (count > 0) mask |= LUA_MASKCOUNT; 338 return mask; 339 } 340 341 342 /* 343 ** Convert a bit mask (for 'gethook') into a string mask 344 */ 345 static char *unmakemask (int mask, char *smask) { 346 int i = 0; 347 if (mask & LUA_MASKCALL) smask[i++] = 'c'; 348 if (mask & LUA_MASKRET) smask[i++] = 'r'; 349 if (mask & LUA_MASKLINE) smask[i++] = 'l'; 350 smask[i] = '\0'; 351 return smask; 352 } 353 354 355 static int db_sethook (lua_State *L) { 356 int arg, mask, count; 357 lua_Hook func; 358 lua_State *L1 = getthread(L, &arg); 359 if (lua_isnoneornil(L, arg+1)) { /* no hook? */ 360 lua_settop(L, arg+1); 361 func = NULL; mask = 0; count = 0; /* turn off hooks */ 362 } 363 else { 364 const char *smask = luaL_checkstring(L, arg+2); 365 luaL_checktype(L, arg+1, LUA_TFUNCTION); 366 count = luaL_optint(L, arg+3, 0); 367 func = hookf; mask = makemask(smask, count); 368 } 369 if (gethooktable(L) == 0) { /* creating hook table? */ 370 lua_pushstring(L, "k"); 371 lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ 372 lua_pushvalue(L, -1); 373 lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ 374 } 375 lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */ 376 lua_pushvalue(L, arg+1); /* value */ 377 lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ 378 lua_sethook(L1, func, mask, count); /* set hooks */ 379 return 0; 380 } 381 382 383 static int db_gethook (lua_State *L) { 384 int arg; 385 lua_State *L1 = getthread(L, &arg); 386 char buff[5]; 387 int mask = lua_gethookmask(L1); 388 lua_Hook hook = lua_gethook(L1); 389 if (hook != NULL && hook != hookf) /* external hook? */ 390 lua_pushliteral(L, "external hook"); 391 else { 392 gethooktable(L); 393 lua_pushthread(L1); lua_xmove(L1, L, 1); 394 lua_rawget(L, -2); /* 1st result = hooktable[L1] */ 395 lua_remove(L, -2); /* remove hook table */ 396 } 397 lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ 398 lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ 399 return 3; 400 } 401 402 403 #ifndef _KERNEL 404 static int db_debug (lua_State *L) { 405 for (;;) { 406 char buffer[250]; 407 luai_writestringerror("%s", "lua_debug> "); 408 if (fgets(buffer, sizeof(buffer), stdin) == 0 || 409 strcmp(buffer, "cont\n") == 0) 410 return 0; 411 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 412 lua_pcall(L, 0, 0, 0)) 413 luai_writestringerror("%s\n", lua_tostring(L, -1)); 414 lua_settop(L, 0); /* remove eventual returns */ 415 } 416 } 417 #endif 418 419 420 static int db_traceback (lua_State *L) { 421 int arg; 422 lua_State *L1 = getthread(L, &arg); 423 const char *msg = lua_tostring(L, arg + 1); 424 if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ 425 lua_pushvalue(L, arg + 1); /* return it untouched */ 426 else { 427 int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); 428 luaL_traceback(L, L1, msg, level); 429 } 430 return 1; 431 } 432 433 434 static const luaL_Reg dblib[] = { 435 {"Csize", db_Csize}, 436 #ifndef _KERNEL 437 {"debug", db_debug}, 438 #endif 439 {"getuservalue", db_getuservalue}, 440 {"gethook", db_gethook}, 441 {"getinfo", db_getinfo}, 442 {"getlocal", db_getlocal}, 443 {"getregistry", db_getregistry}, 444 {"getmetatable", db_getmetatable}, 445 {"getupvalue", db_getupvalue}, 446 {"upvaluejoin", db_upvaluejoin}, 447 {"upvalueid", db_upvalueid}, 448 {"setuservalue", db_setuservalue}, 449 {"sethook", db_sethook}, 450 {"setlocal", db_setlocal}, 451 {"setmetatable", db_setmetatable}, 452 {"setupvalue", db_setupvalue}, 453 {"traceback", db_traceback}, 454 {NULL, NULL} 455 }; 456 457 458 LUAMOD_API int luaopen_debug (lua_State *L) { 459 luaL_newlib(L, dblib); 460 return 1; 461 } 462 463