ldblib.c 10 KB

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