ldblib.c 9.6 KB

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