ldblib.c 9.7 KB

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