ldblib.c 10 KB

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