ldblib.c 10.0 KB

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