ldblib.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. ** $Id: ldblib.c,v 1.93 2005/02/18 12:40:02 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_getmetatable (lua_State *L) {
  15. luaL_checkany(L, 1);
  16. if (!lua_getmetatable(L, 1)) {
  17. lua_pushnil(L); /* no metatable */
  18. }
  19. return 1;
  20. }
  21. static int db_setmetatable (lua_State *L) {
  22. int t = lua_type(L, 2);
  23. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  24. "nil or table expected");
  25. lua_settop(L, 2);
  26. lua_pushboolean(L, lua_setmetatable(L, 1));
  27. return 1;
  28. }
  29. static int db_getfenv (lua_State *L) {
  30. lua_getfenv(L, 1);
  31. return 1;
  32. }
  33. static int db_setfenv (lua_State *L) {
  34. luaL_checktype(L, 2, LUA_TTABLE);
  35. lua_settop(L, 2);
  36. if (lua_setfenv(L, 1) == 0)
  37. luaL_error(L, "`setfenv' cannot change environment of given object");
  38. return 1;
  39. }
  40. static void settabss (lua_State *L, const char *i, const char *v) {
  41. lua_pushstring(L, v);
  42. lua_setfield(L, -2, i);
  43. }
  44. static void settabsi (lua_State *L, const char *i, int v) {
  45. lua_pushinteger(L, v);
  46. lua_setfield(L, -2, i);
  47. }
  48. static lua_State *getthread (lua_State *L, int *arg) {
  49. if (lua_isthread(L, 1)) {
  50. *arg = 1;
  51. return lua_tothread(L, 1);
  52. }
  53. else {
  54. *arg = 0;
  55. return L;
  56. }
  57. }
  58. static int db_getinfo (lua_State *L) {
  59. lua_Debug ar;
  60. int arg;
  61. lua_State *L1 = getthread(L, &arg);
  62. const char *options = luaL_optstring(L, arg+2, "flnSu");
  63. if (lua_isnumber(L, arg+1)) {
  64. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
  65. lua_pushnil(L); /* level out of range */
  66. return 1;
  67. }
  68. }
  69. else if (lua_isfunction(L, arg+1)) {
  70. lua_pushfstring(L, ">%s", options);
  71. options = lua_tostring(L, -1);
  72. lua_pushvalue(L, arg+1);
  73. lua_xmove(L, L1, 1);
  74. }
  75. else
  76. return luaL_argerror(L, arg+1, "function or level expected");
  77. if (!lua_getinfo(L1, options, &ar))
  78. return luaL_argerror(L, arg+2, "invalid option");
  79. lua_newtable(L);
  80. for (; *options; options++) {
  81. switch (*options) {
  82. case 'S':
  83. settabss(L, "source", ar.source);
  84. settabss(L, "short_src", ar.short_src);
  85. settabsi(L, "linedefined", ar.linedefined);
  86. settabss(L, "what", ar.what);
  87. break;
  88. case 'l':
  89. settabsi(L, "currentline", ar.currentline);
  90. break;
  91. case 'u':
  92. settabsi(L, "nups", ar.nups);
  93. break;
  94. case 'n':
  95. settabss(L, "name", ar.name);
  96. settabss(L, "namewhat", ar.namewhat);
  97. break;
  98. case 'f':
  99. if (L == L1)
  100. lua_pushvalue(L, -2);
  101. else
  102. lua_xmove(L1, L, 1);
  103. lua_setfield(L, -2, "func");
  104. break;
  105. }
  106. }
  107. return 1; /* return table */
  108. }
  109. static int db_getlocal (lua_State *L) {
  110. int arg;
  111. lua_State *L1 = getthread(L, &arg);
  112. lua_Debug ar;
  113. const char *name;
  114. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  115. return luaL_argerror(L, arg+1, "level out of range");
  116. name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
  117. if (name) {
  118. lua_xmove(L1, L, 1);
  119. lua_pushstring(L, name);
  120. lua_pushvalue(L, -2);
  121. return 2;
  122. }
  123. else {
  124. lua_pushnil(L);
  125. return 1;
  126. }
  127. }
  128. static int db_setlocal (lua_State *L) {
  129. int arg;
  130. lua_State *L1 = getthread(L, &arg);
  131. lua_Debug ar;
  132. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  133. return luaL_argerror(L, arg+1, "level out of range");
  134. luaL_checkany(L, arg+3);
  135. lua_settop(L, arg+3);
  136. lua_xmove(L, L1, 1);
  137. lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  138. return 1;
  139. }
  140. static int auxupvalue (lua_State *L, int get) {
  141. const char *name;
  142. int n = luaL_checkint(L, 2);
  143. luaL_checktype(L, 1, LUA_TFUNCTION);
  144. if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
  145. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  146. if (name == NULL) return 0;
  147. lua_pushstring(L, name);
  148. lua_insert(L, -(get+1));
  149. return get + 1;
  150. }
  151. static int db_getupvalue (lua_State *L) {
  152. return auxupvalue(L, 1);
  153. }
  154. static int db_setupvalue (lua_State *L) {
  155. luaL_checkany(L, 3);
  156. return auxupvalue(L, 0);
  157. }
  158. static const char KEY_HOOK = 'h';
  159. static void hookf (lua_State *L, lua_Debug *ar) {
  160. static const char *const hooknames[] =
  161. {"call", "return", "line", "count", "tail return"};
  162. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  163. lua_rawget(L, LUA_REGISTRYINDEX);
  164. lua_pushlightuserdata(L, L);
  165. lua_rawget(L, -2);
  166. if (lua_isfunction(L, -1)) {
  167. lua_pushstring(L, hooknames[(int)ar->event]);
  168. if (ar->currentline >= 0)
  169. lua_pushinteger(L, ar->currentline);
  170. else lua_pushnil(L);
  171. lua_assert(lua_getinfo(L, "lS", ar));
  172. lua_call(L, 2, 0);
  173. }
  174. }
  175. static int makemask (const char *smask, int count) {
  176. int mask = 0;
  177. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  178. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  179. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  180. if (count > 0) mask |= LUA_MASKCOUNT;
  181. return mask;
  182. }
  183. static char *unmakemask (int mask, char *smask) {
  184. int i = 0;
  185. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  186. if (mask & LUA_MASKRET) smask[i++] = 'r';
  187. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  188. smask[i] = '\0';
  189. return smask;
  190. }
  191. static void gethooktable (lua_State *L) {
  192. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  193. lua_rawget(L, LUA_REGISTRYINDEX);
  194. if (!lua_istable(L, -1)) {
  195. lua_pop(L, 1);
  196. lua_newtable(L);
  197. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  198. lua_pushvalue(L, -2);
  199. lua_rawset(L, LUA_REGISTRYINDEX);
  200. }
  201. }
  202. static int db_sethook (lua_State *L) {
  203. int arg;
  204. lua_State *L1 = getthread(L, &arg);
  205. if (lua_isnoneornil(L, arg+1)) {
  206. lua_settop(L, arg+1);
  207. lua_sethook(L1, NULL, 0, 0); /* turn off hooks */
  208. }
  209. else {
  210. const char *smask = luaL_checkstring(L, arg+2);
  211. int count = luaL_optint(L, arg+3, 0);
  212. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  213. lua_sethook(L1, hookf, makemask(smask, count), count);
  214. }
  215. gethooktable(L1);
  216. lua_pushlightuserdata(L1, L1);
  217. lua_pushvalue(L, arg+1);
  218. lua_xmove(L, L1, 1);
  219. lua_rawset(L1, -3); /* set new hook */
  220. lua_pop(L1, 1); /* remove hook table */
  221. return 0;
  222. }
  223. static int db_gethook (lua_State *L) {
  224. int arg;
  225. lua_State *L1 = getthread(L, &arg);
  226. char buff[5];
  227. int mask = lua_gethookmask(L1);
  228. lua_Hook hook = lua_gethook(L1);
  229. if (hook != NULL && hook != hookf) /* external hook? */
  230. lua_pushliteral(L, "external hook");
  231. else {
  232. gethooktable(L1);
  233. lua_pushlightuserdata(L1, L1);
  234. lua_rawget(L1, -2); /* get hook */
  235. lua_remove(L1, -2); /* remove hook table */
  236. lua_xmove(L1, L, 1);
  237. }
  238. lua_pushstring(L, unmakemask(mask, buff));
  239. lua_pushinteger(L, lua_gethookcount(L1));
  240. return 3;
  241. }
  242. static int db_debug (lua_State *L) {
  243. for (;;) {
  244. char buffer[250];
  245. fputs("lua_debug> ", stderr);
  246. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  247. strcmp(buffer, "cont\n") == 0)
  248. return 0;
  249. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  250. lua_pcall(L, 0, 0, 0)) {
  251. fputs(lua_tostring(L, -1), stderr);
  252. fputs("\n", stderr);
  253. }
  254. lua_settop(L, 0); /* remove eventual returns */
  255. }
  256. }
  257. #define LEVELS1 12 /* size of the first part of the stack */
  258. #define LEVELS2 10 /* size of the second part of the stack */
  259. static int db_errorfb (lua_State *L) {
  260. int level;
  261. int firstpart = 1; /* still before eventual `...' */
  262. int arg;
  263. lua_State *L1 = getthread(L, &arg);
  264. lua_Debug ar;
  265. if (lua_isnumber(L, arg+2)) {
  266. level = lua_tointeger(L, arg+2);
  267. lua_pop(L, 1);
  268. }
  269. else
  270. level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
  271. if (lua_gettop(L) == arg)
  272. lua_pushliteral(L, "");
  273. else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
  274. else lua_pushliteral(L, "\n");
  275. lua_pushliteral(L, "stack traceback:");
  276. while (lua_getstack(L1, level++, &ar)) {
  277. if (level > LEVELS1 && firstpart) {
  278. /* no more than `LEVELS2' more levels? */
  279. if (!lua_getstack(L1, level+LEVELS2, &ar))
  280. level--; /* keep going */
  281. else {
  282. lua_pushliteral(L, "\n\t..."); /* too many levels */
  283. while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
  284. level++;
  285. }
  286. firstpart = 0;
  287. continue;
  288. }
  289. lua_pushliteral(L, "\n\t");
  290. lua_getinfo(L1, "Snl", &ar);
  291. lua_pushfstring(L, "%s:", ar.short_src);
  292. if (ar.currentline > 0)
  293. lua_pushfstring(L, "%d:", ar.currentline);
  294. if (*ar.namewhat != '\0') /* is there a name? */
  295. lua_pushfstring(L, " in function `%s'", ar.name);
  296. else {
  297. if (*ar.what == 'm') /* main? */
  298. lua_pushfstring(L, " in main chunk");
  299. else if (*ar.what == 'C' || *ar.what == 't')
  300. lua_pushliteral(L, " ?"); /* C function or tail call */
  301. else
  302. lua_pushfstring(L, " in function <%s:%d>",
  303. ar.short_src, ar.linedefined);
  304. }
  305. lua_concat(L, lua_gettop(L) - arg);
  306. }
  307. lua_concat(L, lua_gettop(L) - arg);
  308. return 1;
  309. }
  310. static const luaL_reg dblib[] = {
  311. {"getmetatable", db_getmetatable},
  312. {"setmetatable", db_setmetatable},
  313. {"getfenv", db_getfenv},
  314. {"setfenv", db_setfenv},
  315. {"getlocal", db_getlocal},
  316. {"getinfo", db_getinfo},
  317. {"gethook", db_gethook},
  318. {"getupvalue", db_getupvalue},
  319. {"sethook", db_sethook},
  320. {"setlocal", db_setlocal},
  321. {"setupvalue", db_setupvalue},
  322. {"debug", db_debug},
  323. {"traceback", db_errorfb},
  324. {NULL, NULL}
  325. };
  326. LUALIB_API int luaopen_debug (lua_State *L) {
  327. luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
  328. return 1;
  329. }