ldblib.c 12 KB

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