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