ldblib.c 10 KB

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