ldblib.c 13 KB

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