ldblib.c 9.9 KB

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