402 rindas
9.9 KiB

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