9
3

ldblib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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.
  18. */
  19. static const char *const HOOKKEY = "_HOOKKEY";
  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. luaL_pushfail(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. luaL_pushfail(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. luaL_pushfail(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. lua_pushlstring(L, ar.source, ar.srclen);
  141. lua_setfield(L, -2, "source");
  142. settabss(L, "short_src", ar.short_src);
  143. settabsi(L, "linedefined", ar.linedefined);
  144. settabsi(L, "lastlinedefined", ar.lastlinedefined);
  145. settabss(L, "what", ar.what);
  146. }
  147. if (strchr(options, 'l'))
  148. settabsi(L, "currentline", ar.currentline);
  149. if (strchr(options, 'u')) {
  150. settabsi(L, "nups", ar.nups);
  151. settabsi(L, "nparams", ar.nparams);
  152. settabsb(L, "isvararg", ar.isvararg);
  153. }
  154. if (strchr(options, 'n')) {
  155. settabss(L, "name", ar.name);
  156. settabss(L, "namewhat", ar.namewhat);
  157. }
  158. if (strchr(options, 'r')) {
  159. settabsi(L, "ftransfer", ar.ftransfer);
  160. settabsi(L, "ntransfer", ar.ntransfer);
  161. }
  162. if (strchr(options, 't'))
  163. settabsb(L, "istailcall", ar.istailcall);
  164. if (strchr(options, 'L'))
  165. treatstackoption(L, L1, "activelines");
  166. if (strchr(options, 'f'))
  167. treatstackoption(L, L1, "func");
  168. return 1; /* return table */
  169. }
  170. static int db_getlocal (lua_State *L) {
  171. int arg;
  172. lua_State *L1 = getthread(L, &arg);
  173. int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
  174. if (lua_isfunction(L, arg + 1)) { /* function argument? */
  175. lua_pushvalue(L, arg + 1); /* push function */
  176. lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
  177. return 1; /* return only name (there is no value) */
  178. }
  179. else { /* stack-level argument */
  180. lua_Debug ar;
  181. const char *name;
  182. int level = (int)luaL_checkinteger(L, arg + 1);
  183. if (!lua_getstack(L1, level, &ar)) /* out of range? */
  184. return luaL_argerror(L, arg+1, "level out of range");
  185. checkstack(L, L1, 1);
  186. name = lua_getlocal(L1, &ar, nvar);
  187. if (name) {
  188. lua_xmove(L1, L, 1); /* move local value */
  189. lua_pushstring(L, name); /* push name */
  190. lua_rotate(L, -2, 1); /* re-order */
  191. return 2;
  192. }
  193. else {
  194. luaL_pushfail(L); /* no name (nor value) */
  195. return 1;
  196. }
  197. }
  198. }
  199. static int db_setlocal (lua_State *L) {
  200. int arg;
  201. const char *name;
  202. lua_State *L1 = getthread(L, &arg);
  203. lua_Debug ar;
  204. int level = (int)luaL_checkinteger(L, arg + 1);
  205. int nvar = (int)luaL_checkinteger(L, arg + 2);
  206. if (!lua_getstack(L1, level, &ar)) /* out of range? */
  207. return luaL_argerror(L, arg+1, "level out of range");
  208. luaL_checkany(L, arg+3);
  209. lua_settop(L, arg+3);
  210. checkstack(L, L1, 1);
  211. lua_xmove(L, L1, 1);
  212. name = lua_setlocal(L1, &ar, nvar);
  213. if (name == NULL)
  214. lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
  215. lua_pushstring(L, name);
  216. return 1;
  217. }
  218. /*
  219. ** get (if 'get' is true) or set an upvalue from a closure
  220. */
  221. static int auxupvalue (lua_State *L, int get) {
  222. const char *name;
  223. int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
  224. luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
  225. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  226. if (name == NULL) return 0;
  227. lua_pushstring(L, name);
  228. lua_insert(L, -(get+1)); /* no-op if get is false */
  229. return get + 1;
  230. }
  231. static int db_getupvalue (lua_State *L) {
  232. return auxupvalue(L, 1);
  233. }
  234. static int db_setupvalue (lua_State *L) {
  235. luaL_checkany(L, 3);
  236. return auxupvalue(L, 0);
  237. }
  238. /*
  239. ** Check whether a given upvalue from a given closure exists and
  240. ** returns its index
  241. */
  242. static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
  243. void *id;
  244. int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
  245. luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
  246. id = lua_upvalueid(L, argf, nup);
  247. if (pnup) {
  248. luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
  249. *pnup = nup;
  250. }
  251. return id;
  252. }
  253. static int db_upvalueid (lua_State *L) {
  254. void *id = checkupval(L, 1, 2, NULL);
  255. if (id != NULL)
  256. lua_pushlightuserdata(L, id);
  257. else
  258. luaL_pushfail(L);
  259. return 1;
  260. }
  261. static int db_upvaluejoin (lua_State *L) {
  262. int n1, n2;
  263. checkupval(L, 1, 2, &n1);
  264. checkupval(L, 3, 4, &n2);
  265. luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
  266. luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
  267. lua_upvaluejoin(L, 1, n1, 3, n2);
  268. return 0;
  269. }
  270. /*
  271. ** Call hook function registered at hook table for the current
  272. ** thread (if there is one)
  273. */
  274. static void hookf (lua_State *L, lua_Debug *ar) {
  275. static const char *const hooknames[] =
  276. {"call", "return", "line", "count", "tail call"};
  277. lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
  278. lua_pushthread(L);
  279. if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
  280. lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
  281. if (ar->currentline >= 0)
  282. lua_pushinteger(L, ar->currentline); /* push current line */
  283. else lua_pushnil(L);
  284. lua_assert(lua_getinfo(L, "lS", ar));
  285. lua_call(L, 2, 0); /* call hook function */
  286. }
  287. }
  288. /*
  289. ** Convert a string mask (for 'sethook') into a bit mask
  290. */
  291. static int makemask (const char *smask, int count) {
  292. int mask = 0;
  293. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  294. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  295. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  296. if (count > 0) mask |= LUA_MASKCOUNT;
  297. return mask;
  298. }
  299. /*
  300. ** Convert a bit mask (for 'gethook') into a string mask
  301. */
  302. static char *unmakemask (int mask, char *smask) {
  303. int i = 0;
  304. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  305. if (mask & LUA_MASKRET) smask[i++] = 'r';
  306. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  307. smask[i] = '\0';
  308. return smask;
  309. }
  310. static int db_sethook (lua_State *L) {
  311. int arg, mask, count;
  312. lua_Hook func;
  313. lua_State *L1 = getthread(L, &arg);
  314. if (lua_isnoneornil(L, arg+1)) { /* no hook? */
  315. lua_settop(L, arg+1);
  316. func = NULL; mask = 0; count = 0; /* turn off hooks */
  317. }
  318. else {
  319. const char *smask = luaL_checkstring(L, arg+2);
  320. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  321. count = (int)luaL_optinteger(L, arg + 3, 0);
  322. func = hookf; mask = makemask(smask, count);
  323. }
  324. if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
  325. /* table just created; initialize it */
  326. lua_pushstring(L, "k");
  327. lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
  328. lua_pushvalue(L, -1);
  329. lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
  330. }
  331. checkstack(L, L1, 1);
  332. lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
  333. lua_pushvalue(L, arg + 1); /* value (hook function) */
  334. lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
  335. lua_sethook(L1, func, mask, count);
  336. return 0;
  337. }
  338. static int db_gethook (lua_State *L) {
  339. int arg;
  340. lua_State *L1 = getthread(L, &arg);
  341. char buff[5];
  342. int mask = lua_gethookmask(L1);
  343. lua_Hook hook = lua_gethook(L1);
  344. if (hook == NULL) { /* no hook? */
  345. luaL_pushfail(L);
  346. return 1;
  347. }
  348. else if (hook != hookf) /* external hook? */
  349. lua_pushliteral(L, "external hook");
  350. else { /* hook table must exist */
  351. lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
  352. checkstack(L, L1, 1);
  353. lua_pushthread(L1); lua_xmove(L1, L, 1);
  354. lua_rawget(L, -2); /* 1st result = hooktable[L1] */
  355. lua_remove(L, -2); /* remove hook table */
  356. }
  357. lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
  358. lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
  359. return 3;
  360. }
  361. static int db_debug (lua_State *L) {
  362. for (;;) {
  363. char buffer[250];
  364. lua_writestringerror("%s", "lua_debug> ");
  365. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  366. strcmp(buffer, "cont\n") == 0)
  367. return 0;
  368. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  369. lua_pcall(L, 0, 0, 0))
  370. lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
  371. lua_settop(L, 0); /* remove eventual returns */
  372. }
  373. }
  374. static int db_traceback (lua_State *L) {
  375. int arg;
  376. lua_State *L1 = getthread(L, &arg);
  377. const char *msg = lua_tostring(L, arg + 1);
  378. if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
  379. lua_pushvalue(L, arg + 1); /* return it untouched */
  380. else {
  381. int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
  382. luaL_traceback(L, L1, msg, level);
  383. }
  384. return 1;
  385. }
  386. static int db_setcstacklimit (lua_State *L) {
  387. int limit = (int)luaL_checkinteger(L, 1);
  388. int res = lua_setcstacklimit(L, limit);
  389. lua_pushinteger(L, res);
  390. return 1;
  391. }
  392. static const luaL_Reg dblib[] = {
  393. {"debug", db_debug},
  394. {"getuservalue", db_getuservalue},
  395. {"gethook", db_gethook},
  396. {"getinfo", db_getinfo},
  397. {"getlocal", db_getlocal},
  398. {"getregistry", db_getregistry},
  399. {"getmetatable", db_getmetatable},
  400. {"getupvalue", db_getupvalue},
  401. {"upvaluejoin", db_upvaluejoin},
  402. {"upvalueid", db_upvalueid},
  403. {"setuservalue", db_setuservalue},
  404. {"sethook", db_sethook},
  405. {"setlocal", db_setlocal},
  406. {"setmetatable", db_setmetatable},
  407. {"setupvalue", db_setupvalue},
  408. {"traceback", db_traceback},
  409. {"setcstacklimit", db_setcstacklimit},
  410. {NULL, NULL}
  411. };
  412. LUAMOD_API int luaopen_debug (lua_State *L) {
  413. luaL_newlib(L, dblib);
  414. return 1;
  415. }