ldblib.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. ** $Id: ldblib.c,v 1.123 2010/07/02 11:38:13 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. static int db_getregistry (lua_State *L) {
  15. lua_pushvalue(L, LUA_REGISTRYINDEX);
  16. return 1;
  17. }
  18. static int db_getmetatable (lua_State *L) {
  19. luaL_checkany(L, 1);
  20. if (!lua_getmetatable(L, 1)) {
  21. lua_pushnil(L); /* no metatable */
  22. }
  23. return 1;
  24. }
  25. static int db_setmetatable (lua_State *L) {
  26. int t = lua_type(L, 2);
  27. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  28. "nil or table expected");
  29. lua_settop(L, 2);
  30. lua_pushboolean(L, lua_setmetatable(L, 1));
  31. return 1;
  32. }
  33. static int db_getuservalue (lua_State *L) {
  34. luaL_checktype(L, 1, LUA_TUSERDATA);
  35. lua_getuservalue(L, 1);
  36. return 1;
  37. }
  38. static int db_setuservalue (lua_State *L) {
  39. luaL_checktype(L, 1, LUA_TUSERDATA);
  40. if (!lua_isnoneornil(L, 2))
  41. luaL_checktype(L, 2, LUA_TTABLE);
  42. lua_settop(L, 2);
  43. lua_setuservalue(L, 1);
  44. return 1;
  45. }
  46. static void settabss (lua_State *L, const char *i, const char *v) {
  47. lua_pushstring(L, v);
  48. lua_setfield(L, -2, i);
  49. }
  50. static void settabsi (lua_State *L, const char *i, int v) {
  51. lua_pushinteger(L, v);
  52. lua_setfield(L, -2, i);
  53. }
  54. static void settabsb (lua_State *L, const char *i, int v) {
  55. lua_pushboolean(L, v);
  56. lua_setfield(L, -2, i);
  57. }
  58. static lua_State *getthread (lua_State *L, int *arg) {
  59. if (lua_isthread(L, 1)) {
  60. *arg = 1;
  61. return lua_tothread(L, 1);
  62. }
  63. else {
  64. *arg = 0;
  65. return L;
  66. }
  67. }
  68. static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
  69. if (L == L1) {
  70. lua_pushvalue(L, -2);
  71. lua_remove(L, -3);
  72. }
  73. else
  74. lua_xmove(L1, L, 1);
  75. lua_setfield(L, -2, fname);
  76. }
  77. static int db_getinfo (lua_State *L) {
  78. lua_Debug ar;
  79. int arg;
  80. lua_State *L1 = getthread(L, &arg);
  81. const char *options = luaL_optstring(L, arg+2, "flnStu");
  82. if (lua_isnumber(L, arg+1)) {
  83. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
  84. lua_pushnil(L); /* level out of range */
  85. return 1;
  86. }
  87. }
  88. else if (lua_isfunction(L, arg+1)) {
  89. lua_pushfstring(L, ">%s", options);
  90. options = lua_tostring(L, -1);
  91. lua_pushvalue(L, arg+1);
  92. lua_xmove(L, L1, 1);
  93. }
  94. else
  95. return luaL_argerror(L, arg+1, "function or level expected");
  96. if (!lua_getinfo(L1, options, &ar))
  97. return luaL_argerror(L, arg+2, "invalid option");
  98. lua_createtable(L, 0, 2);
  99. if (strchr(options, 'S')) {
  100. settabss(L, "source", ar.source);
  101. settabss(L, "short_src", ar.short_src);
  102. settabsi(L, "linedefined", ar.linedefined);
  103. settabsi(L, "lastlinedefined", ar.lastlinedefined);
  104. settabss(L, "what", ar.what);
  105. }
  106. if (strchr(options, 'l'))
  107. settabsi(L, "currentline", ar.currentline);
  108. if (strchr(options, 'u')) {
  109. settabsi(L, "nups", ar.nups);
  110. settabsi(L, "nparams", ar.nparams);
  111. settabsb(L, "isvararg", ar.isvararg);
  112. }
  113. if (strchr(options, 'n')) {
  114. settabss(L, "name", ar.name);
  115. settabss(L, "namewhat", ar.namewhat);
  116. }
  117. if (strchr(options, 't'))
  118. settabsb(L, "istailcall", ar.istailcall);
  119. if (strchr(options, 'L'))
  120. treatstackoption(L, L1, "activelines");
  121. if (strchr(options, 'f'))
  122. treatstackoption(L, L1, "func");
  123. return 1; /* return table */
  124. }
  125. static int db_getlocal (lua_State *L) {
  126. int arg;
  127. lua_State *L1 = getthread(L, &arg);
  128. lua_Debug ar;
  129. const char *name;
  130. int nvar = luaL_checkint(L, arg+2); /* local-variable index */
  131. if (lua_isfunction(L, arg + 1)) { /* function argument? */
  132. lua_pushvalue(L, arg + 1); /* push function */
  133. lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
  134. return 1;
  135. }
  136. else { /* stack-level argument */
  137. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  138. return luaL_argerror(L, arg+1, "level out of range");
  139. name = lua_getlocal(L1, &ar, nvar);
  140. if (name) {
  141. lua_xmove(L1, L, 1); /* push local value */
  142. lua_pushstring(L, name); /* push name */
  143. lua_pushvalue(L, -2); /* re-order */
  144. return 2;
  145. }
  146. else {
  147. lua_pushnil(L); /* no name (nor value) */
  148. return 1;
  149. }
  150. }
  151. }
  152. static int db_setlocal (lua_State *L) {
  153. int arg;
  154. lua_State *L1 = getthread(L, &arg);
  155. lua_Debug ar;
  156. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  157. return luaL_argerror(L, arg+1, "level out of range");
  158. luaL_checkany(L, arg+3);
  159. lua_settop(L, arg+3);
  160. lua_xmove(L, L1, 1);
  161. lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  162. return 1;
  163. }
  164. static int auxupvalue (lua_State *L, int get) {
  165. const char *name;
  166. int n = luaL_checkint(L, 2);
  167. luaL_checktype(L, 1, LUA_TFUNCTION);
  168. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  169. if (name == NULL) return 0;
  170. lua_pushstring(L, name);
  171. lua_insert(L, -(get+1));
  172. return get + 1;
  173. }
  174. static int db_getupvalue (lua_State *L) {
  175. return auxupvalue(L, 1);
  176. }
  177. static int db_setupvalue (lua_State *L) {
  178. luaL_checkany(L, 3);
  179. return auxupvalue(L, 0);
  180. }
  181. static int checkupval (lua_State *L, int argf, int argnup) {
  182. lua_Debug ar;
  183. int nup = luaL_checkint(L, argnup);
  184. luaL_checktype(L, argf, LUA_TFUNCTION);
  185. lua_pushvalue(L, argf);
  186. lua_getinfo(L, ">u", &ar);
  187. luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
  188. return nup;
  189. }
  190. static int db_upvalueid (lua_State *L) {
  191. int n = checkupval(L, 1, 2);
  192. lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
  193. return 1;
  194. }
  195. static int db_upvaluejoin (lua_State *L) {
  196. int n1 = checkupval(L, 1, 2);
  197. int n2 = checkupval(L, 3, 4);
  198. luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
  199. luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
  200. lua_upvaluejoin(L, 1, n1, 3, n2);
  201. return 0;
  202. }
  203. static const char KEY_HOOK = 'h';
  204. static void hookf (lua_State *L, lua_Debug *ar) {
  205. static const char *const hooknames[] =
  206. {"call", "return", "line", "count", "tail call"};
  207. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  208. lua_rawget(L, LUA_REGISTRYINDEX);
  209. lua_pushlightuserdata(L, L);
  210. lua_rawget(L, -2);
  211. if (lua_isfunction(L, -1)) {
  212. lua_pushstring(L, hooknames[(int)ar->event]);
  213. if (ar->currentline >= 0)
  214. lua_pushinteger(L, ar->currentline);
  215. else lua_pushnil(L);
  216. lua_assert(lua_getinfo(L, "lS", ar));
  217. lua_call(L, 2, 0);
  218. }
  219. }
  220. static int makemask (const char *smask, int count) {
  221. int mask = 0;
  222. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  223. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  224. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  225. if (count > 0) mask |= LUA_MASKCOUNT;
  226. return mask;
  227. }
  228. static char *unmakemask (int mask, char *smask) {
  229. int i = 0;
  230. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  231. if (mask & LUA_MASKRET) smask[i++] = 'r';
  232. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  233. smask[i] = '\0';
  234. return smask;
  235. }
  236. static void gethooktable (lua_State *L) {
  237. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  238. lua_rawget(L, LUA_REGISTRYINDEX);
  239. if (!lua_istable(L, -1)) {
  240. lua_pop(L, 1);
  241. lua_createtable(L, 0, 1);
  242. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  243. lua_pushvalue(L, -2);
  244. lua_rawset(L, LUA_REGISTRYINDEX);
  245. }
  246. }
  247. static int db_sethook (lua_State *L) {
  248. int arg, mask, count;
  249. lua_Hook func;
  250. lua_State *L1 = getthread(L, &arg);
  251. if (lua_isnoneornil(L, arg+1)) {
  252. lua_settop(L, arg+1);
  253. func = NULL; mask = 0; count = 0; /* turn off hooks */
  254. }
  255. else {
  256. const char *smask = luaL_checkstring(L, arg+2);
  257. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  258. count = luaL_optint(L, arg+3, 0);
  259. func = hookf; mask = makemask(smask, count);
  260. }
  261. gethooktable(L);
  262. lua_pushlightuserdata(L, L1);
  263. lua_pushvalue(L, arg+1);
  264. lua_rawset(L, -3); /* set new hook */
  265. lua_pop(L, 1); /* remove hook table */
  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_pushlightuserdata(L, L1);
  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. }