ldblib.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. ** $Id: ldblib.c,v 1.114 2009/11/05 17:26:00 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_getfenv (lua_State *L) {
  34. luaL_checkany(L, 1);
  35. lua_getfenv(L, 1);
  36. return 1;
  37. }
  38. static int db_setfenv (lua_State *L) {
  39. luaL_checktype(L, 2, LUA_TTABLE);
  40. lua_settop(L, 2);
  41. if (lua_setfenv(L, 1) == 0)
  42. luaL_error(L, LUA_QL("setfenv")
  43. " cannot change environment of given object");
  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 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;
  62. }
  63. }
  64. static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
  65. if (L == L1) {
  66. lua_pushvalue(L, -2);
  67. lua_remove(L, -3);
  68. }
  69. else
  70. lua_xmove(L1, L, 1);
  71. lua_setfield(L, -2, fname);
  72. }
  73. static int db_getinfo (lua_State *L) {
  74. lua_Debug ar;
  75. int arg;
  76. lua_State *L1 = getthread(L, &arg);
  77. const char *options = luaL_optstring(L, arg+2, "flnSu");
  78. if (lua_isnumber(L, arg+1)) {
  79. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
  80. lua_pushnil(L); /* level out of range */
  81. return 1;
  82. }
  83. }
  84. else if (lua_isfunction(L, arg+1)) {
  85. lua_pushfstring(L, ">%s", options);
  86. options = lua_tostring(L, -1);
  87. lua_pushvalue(L, arg+1);
  88. lua_xmove(L, L1, 1);
  89. }
  90. else
  91. return luaL_argerror(L, arg+1, "function or level expected");
  92. if (!lua_getinfo(L1, options, &ar))
  93. return luaL_argerror(L, arg+2, "invalid option");
  94. lua_createtable(L, 0, 2);
  95. if (strchr(options, 'S')) {
  96. settabss(L, "source", ar.source);
  97. settabss(L, "short_src", ar.short_src);
  98. settabsi(L, "linedefined", ar.linedefined);
  99. settabsi(L, "lastlinedefined", ar.lastlinedefined);
  100. settabss(L, "what", ar.what);
  101. }
  102. if (strchr(options, 'l'))
  103. settabsi(L, "currentline", ar.currentline);
  104. if (strchr(options, 'u'))
  105. settabsi(L, "nups", ar.nups);
  106. if (strchr(options, 'n')) {
  107. settabss(L, "name", ar.name);
  108. settabss(L, "namewhat", ar.namewhat);
  109. }
  110. if (strchr(options, 'L'))
  111. treatstackoption(L, L1, "activelines");
  112. if (strchr(options, 'f'))
  113. treatstackoption(L, L1, "func");
  114. return 1; /* return table */
  115. }
  116. static int db_getlocal (lua_State *L) {
  117. int arg;
  118. lua_State *L1 = getthread(L, &arg);
  119. lua_Debug ar;
  120. const char *name;
  121. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  122. return luaL_argerror(L, arg+1, "level out of range");
  123. name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
  124. if (name) {
  125. lua_xmove(L1, L, 1);
  126. lua_pushstring(L, name);
  127. lua_pushvalue(L, -2);
  128. return 2;
  129. }
  130. else {
  131. lua_pushnil(L);
  132. return 1;
  133. }
  134. }
  135. static int db_setlocal (lua_State *L) {
  136. int arg;
  137. lua_State *L1 = getthread(L, &arg);
  138. lua_Debug ar;
  139. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  140. return luaL_argerror(L, arg+1, "level out of range");
  141. luaL_checkany(L, arg+3);
  142. lua_settop(L, arg+3);
  143. lua_xmove(L, L1, 1);
  144. lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  145. return 1;
  146. }
  147. static int auxupvalue (lua_State *L, int get) {
  148. const char *name;
  149. int n = luaL_checkint(L, 2);
  150. luaL_checktype(L, 1, LUA_TFUNCTION);
  151. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  152. if (name == NULL) return 0;
  153. lua_pushstring(L, name);
  154. lua_insert(L, -(get+1));
  155. return get + 1;
  156. }
  157. static int db_getupvalue (lua_State *L) {
  158. return auxupvalue(L, 1);
  159. }
  160. static int db_setupvalue (lua_State *L) {
  161. luaL_checkany(L, 3);
  162. return auxupvalue(L, 0);
  163. }
  164. static int checkupval (lua_State *L, int argf, int argnup) {
  165. lua_Debug ar;
  166. int nup = luaL_checkint(L, argnup);
  167. luaL_checktype(L, argf, LUA_TFUNCTION);
  168. lua_pushvalue(L, argf);
  169. lua_getinfo(L, ">u", &ar);
  170. luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
  171. return nup;
  172. }
  173. static int db_upvalueid (lua_State *L) {
  174. int n = checkupval(L, 1, 2);
  175. lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
  176. return 1;
  177. }
  178. static int db_joinupvalue (lua_State *L) {
  179. int n1 = checkupval(L, 1, 2);
  180. int n2 = checkupval(L, 3, 4);
  181. luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
  182. luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
  183. lua_upvaluejoin(L, 1, n1, 3, n2);
  184. return 0;
  185. }
  186. static const char KEY_HOOK = 'h';
  187. static void hookf (lua_State *L, lua_Debug *ar) {
  188. static const char *const hooknames[] =
  189. {"call", "return", "line", "count", "tail return"};
  190. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  191. lua_rawget(L, LUA_REGISTRYINDEX);
  192. lua_pushlightuserdata(L, L);
  193. lua_rawget(L, -2);
  194. if (lua_isfunction(L, -1)) {
  195. lua_pushstring(L, hooknames[(int)ar->event]);
  196. if (ar->currentline >= 0)
  197. lua_pushinteger(L, ar->currentline);
  198. else lua_pushnil(L);
  199. lua_assert(lua_getinfo(L, "lS", ar));
  200. lua_call(L, 2, 0);
  201. }
  202. }
  203. static int makemask (const char *smask, int count) {
  204. int mask = 0;
  205. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  206. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  207. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  208. if (count > 0) mask |= LUA_MASKCOUNT;
  209. return mask;
  210. }
  211. static char *unmakemask (int mask, char *smask) {
  212. int i = 0;
  213. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  214. if (mask & LUA_MASKRET) smask[i++] = 'r';
  215. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  216. smask[i] = '\0';
  217. return smask;
  218. }
  219. static void gethooktable (lua_State *L) {
  220. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  221. lua_rawget(L, LUA_REGISTRYINDEX);
  222. if (!lua_istable(L, -1)) {
  223. lua_pop(L, 1);
  224. lua_createtable(L, 0, 1);
  225. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  226. lua_pushvalue(L, -2);
  227. lua_rawset(L, LUA_REGISTRYINDEX);
  228. }
  229. }
  230. static int db_sethook (lua_State *L) {
  231. int arg, mask, count;
  232. lua_Hook func;
  233. lua_State *L1 = getthread(L, &arg);
  234. if (lua_isnoneornil(L, arg+1)) {
  235. lua_settop(L, arg+1);
  236. func = NULL; mask = 0; count = 0; /* turn off hooks */
  237. }
  238. else {
  239. const char *smask = luaL_checkstring(L, arg+2);
  240. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  241. count = luaL_optint(L, arg+3, 0);
  242. func = hookf; mask = makemask(smask, count);
  243. }
  244. gethooktable(L);
  245. lua_pushlightuserdata(L, L1);
  246. lua_pushvalue(L, arg+1);
  247. lua_rawset(L, -3); /* set new hook */
  248. lua_pop(L, 1); /* remove hook table */
  249. lua_sethook(L1, func, mask, count); /* set hooks */
  250. return 0;
  251. }
  252. static int db_gethook (lua_State *L) {
  253. int arg;
  254. lua_State *L1 = getthread(L, &arg);
  255. char buff[5];
  256. int mask = lua_gethookmask(L1);
  257. lua_Hook hook = lua_gethook(L1);
  258. if (hook != NULL && hook != hookf) /* external hook? */
  259. lua_pushliteral(L, "external hook");
  260. else {
  261. gethooktable(L);
  262. lua_pushlightuserdata(L, L1);
  263. lua_rawget(L, -2); /* get hook */
  264. lua_remove(L, -2); /* remove hook table */
  265. }
  266. lua_pushstring(L, unmakemask(mask, buff));
  267. lua_pushinteger(L, lua_gethookcount(L1));
  268. return 3;
  269. }
  270. static int db_debug (lua_State *L) {
  271. for (;;) {
  272. char buffer[250];
  273. fputs("lua_debug> ", stderr);
  274. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  275. strcmp(buffer, "cont\n") == 0)
  276. return 0;
  277. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  278. lua_pcall(L, 0, 0, 0)) {
  279. fputs(lua_tostring(L, -1), stderr);
  280. fputs("\n", stderr);
  281. }
  282. lua_settop(L, 0); /* remove eventual returns */
  283. }
  284. }
  285. static int db_traceback (lua_State *L) {
  286. int arg;
  287. lua_State *L1 = getthread(L, &arg);
  288. const char *msg = lua_tostring(L, arg + 1);
  289. if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
  290. lua_pushvalue(L, arg + 1); /* return it untouched */
  291. else {
  292. int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
  293. luaL_traceback(L, L1, msg, level);
  294. }
  295. return 1;
  296. }
  297. static const luaL_Reg dblib[] = {
  298. {"debug", db_debug},
  299. {"getfenv", db_getfenv},
  300. {"gethook", db_gethook},
  301. {"getinfo", db_getinfo},
  302. {"getlocal", db_getlocal},
  303. {"getregistry", db_getregistry},
  304. {"getmetatable", db_getmetatable},
  305. {"getupvalue", db_getupvalue},
  306. {"joinupvalue", db_joinupvalue},
  307. {"upvalueid", db_upvalueid},
  308. {"setfenv", db_setfenv},
  309. {"sethook", db_sethook},
  310. {"setlocal", db_setlocal},
  311. {"setmetatable", db_setmetatable},
  312. {"setupvalue", db_setupvalue},
  313. {"traceback", db_traceback},
  314. {NULL, NULL}
  315. };
  316. LUALIB_API int luaopen_debug (lua_State *L) {
  317. luaL_register(L, LUA_DBLIBNAME, dblib);
  318. return 1;
  319. }