ldblib.c 13 KB

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