ldblib.c 9.8 KB

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