ldblib.c 13 KB

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