ldblib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. ** $Id: ldblib.c,v 1.120 2010/02/18 19:18:41 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_getenv (lua_State *L) {
  34. luaL_checktype(L, 1, LUA_TUSERDATA);
  35. lua_getenv(L, 1);
  36. return 1;
  37. }
  38. static int db_setenv (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_setenv(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. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  131. return luaL_argerror(L, arg+1, "level out of range");
  132. name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
  133. if (name) {
  134. lua_xmove(L1, L, 1);
  135. lua_pushstring(L, name);
  136. lua_pushvalue(L, -2);
  137. return 2;
  138. }
  139. else {
  140. lua_pushnil(L);
  141. return 1;
  142. }
  143. }
  144. static int db_setlocal (lua_State *L) {
  145. int arg;
  146. lua_State *L1 = getthread(L, &arg);
  147. lua_Debug ar;
  148. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  149. return luaL_argerror(L, arg+1, "level out of range");
  150. luaL_checkany(L, arg+3);
  151. lua_settop(L, arg+3);
  152. lua_xmove(L, L1, 1);
  153. lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  154. return 1;
  155. }
  156. static int auxupvalue (lua_State *L, int get) {
  157. const char *name;
  158. int n = luaL_checkint(L, 2);
  159. luaL_checktype(L, 1, LUA_TFUNCTION);
  160. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  161. if (name == NULL) return 0;
  162. lua_pushstring(L, name);
  163. lua_insert(L, -(get+1));
  164. return get + 1;
  165. }
  166. static int db_getupvalue (lua_State *L) {
  167. return auxupvalue(L, 1);
  168. }
  169. static int db_setupvalue (lua_State *L) {
  170. luaL_checkany(L, 3);
  171. return auxupvalue(L, 0);
  172. }
  173. static int checkupval (lua_State *L, int argf, int argnup) {
  174. lua_Debug ar;
  175. int nup = luaL_checkint(L, argnup);
  176. luaL_checktype(L, argf, LUA_TFUNCTION);
  177. lua_pushvalue(L, argf);
  178. lua_getinfo(L, ">u", &ar);
  179. luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
  180. return nup;
  181. }
  182. static int db_upvalueid (lua_State *L) {
  183. int n = checkupval(L, 1, 2);
  184. lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
  185. return 1;
  186. }
  187. static int db_upvaluejoin (lua_State *L) {
  188. int n1 = checkupval(L, 1, 2);
  189. int n2 = checkupval(L, 3, 4);
  190. luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
  191. luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
  192. lua_upvaluejoin(L, 1, n1, 3, n2);
  193. return 0;
  194. }
  195. static const char KEY_HOOK = 'h';
  196. static void hookf (lua_State *L, lua_Debug *ar) {
  197. static const char *const hooknames[] =
  198. {"call", "return", "line", "count", "tail call"};
  199. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  200. lua_rawget(L, LUA_REGISTRYINDEX);
  201. lua_pushlightuserdata(L, L);
  202. lua_rawget(L, -2);
  203. if (lua_isfunction(L, -1)) {
  204. lua_pushstring(L, hooknames[(int)ar->event]);
  205. if (ar->currentline >= 0)
  206. lua_pushinteger(L, ar->currentline);
  207. else lua_pushnil(L);
  208. lua_assert(lua_getinfo(L, "lS", ar));
  209. lua_call(L, 2, 0);
  210. }
  211. }
  212. static int makemask (const char *smask, int count) {
  213. int mask = 0;
  214. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  215. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  216. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  217. if (count > 0) mask |= LUA_MASKCOUNT;
  218. return mask;
  219. }
  220. static char *unmakemask (int mask, char *smask) {
  221. int i = 0;
  222. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  223. if (mask & LUA_MASKRET) smask[i++] = 'r';
  224. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  225. smask[i] = '\0';
  226. return smask;
  227. }
  228. static void gethooktable (lua_State *L) {
  229. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  230. lua_rawget(L, LUA_REGISTRYINDEX);
  231. if (!lua_istable(L, -1)) {
  232. lua_pop(L, 1);
  233. lua_createtable(L, 0, 1);
  234. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  235. lua_pushvalue(L, -2);
  236. lua_rawset(L, LUA_REGISTRYINDEX);
  237. }
  238. }
  239. static int db_sethook (lua_State *L) {
  240. int arg, mask, count;
  241. lua_Hook func;
  242. lua_State *L1 = getthread(L, &arg);
  243. if (lua_isnoneornil(L, arg+1)) {
  244. lua_settop(L, arg+1);
  245. func = NULL; mask = 0; count = 0; /* turn off hooks */
  246. }
  247. else {
  248. const char *smask = luaL_checkstring(L, arg+2);
  249. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  250. count = luaL_optint(L, arg+3, 0);
  251. func = hookf; mask = makemask(smask, count);
  252. }
  253. gethooktable(L);
  254. lua_pushlightuserdata(L, L1);
  255. lua_pushvalue(L, arg+1);
  256. lua_rawset(L, -3); /* set new hook */
  257. lua_pop(L, 1); /* remove hook table */
  258. lua_sethook(L1, func, mask, count); /* set hooks */
  259. return 0;
  260. }
  261. static int db_gethook (lua_State *L) {
  262. int arg;
  263. lua_State *L1 = getthread(L, &arg);
  264. char buff[5];
  265. int mask = lua_gethookmask(L1);
  266. lua_Hook hook = lua_gethook(L1);
  267. if (hook != NULL && hook != hookf) /* external hook? */
  268. lua_pushliteral(L, "external hook");
  269. else {
  270. gethooktable(L);
  271. lua_pushlightuserdata(L, L1);
  272. lua_rawget(L, -2); /* get hook */
  273. lua_remove(L, -2); /* remove hook table */
  274. }
  275. lua_pushstring(L, unmakemask(mask, buff));
  276. lua_pushinteger(L, lua_gethookcount(L1));
  277. return 3;
  278. }
  279. static int db_debug (lua_State *L) {
  280. for (;;) {
  281. char buffer[250];
  282. luai_writestringerror("%s", "lua_debug> ");
  283. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  284. strcmp(buffer, "cont\n") == 0)
  285. return 0;
  286. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  287. lua_pcall(L, 0, 0, 0))
  288. luai_writestringerror("%s\n", lua_tostring(L, -1));
  289. lua_settop(L, 0); /* remove eventual returns */
  290. }
  291. }
  292. static int db_traceback (lua_State *L) {
  293. int arg;
  294. lua_State *L1 = getthread(L, &arg);
  295. const char *msg = lua_tostring(L, arg + 1);
  296. if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
  297. lua_pushvalue(L, arg + 1); /* return it untouched */
  298. else {
  299. int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
  300. luaL_traceback(L, L1, msg, level);
  301. }
  302. return 1;
  303. }
  304. static const luaL_Reg dblib[] = {
  305. {"debug", db_debug},
  306. {"getenv", db_getenv},
  307. {"gethook", db_gethook},
  308. {"getinfo", db_getinfo},
  309. {"getlocal", db_getlocal},
  310. {"getregistry", db_getregistry},
  311. {"getmetatable", db_getmetatable},
  312. {"getupvalue", db_getupvalue},
  313. {"upvaluejoin", db_upvaluejoin},
  314. {"upvalueid", db_upvalueid},
  315. {"setenv", db_setenv},
  316. {"sethook", db_sethook},
  317. {"setlocal", db_setlocal},
  318. {"setmetatable", db_setmetatable},
  319. {"setupvalue", db_setupvalue},
  320. {"traceback", db_traceback},
  321. {NULL, NULL}
  322. };
  323. LUAMOD_API int luaopen_debug (lua_State *L) {
  324. luaL_register(L, LUA_DBLIBNAME, dblib);
  325. return 1;
  326. }