ldblib.c 9.8 KB

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