ldblib.c 12 KB

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