ldblib.c 13 KB

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