ldblib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #include "lrotable.h"
  15. static int db_getregistry (lua_State *L) {
  16. lua_pushvalue(L, LUA_REGISTRYINDEX);
  17. return 1;
  18. }
  19. static int db_getmetatable (lua_State *L) {
  20. luaL_checkany(L, 1);
  21. if (!lua_getmetatable(L, 1)) {
  22. lua_pushnil(L); /* no metatable */
  23. }
  24. return 1;
  25. }
  26. static int db_setmetatable (lua_State *L) {
  27. int t = lua_type(L, 2);
  28. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  29. "nil or table expected");
  30. lua_settop(L, 2);
  31. lua_pushboolean(L, lua_setmetatable(L, 1));
  32. return 1;
  33. }
  34. static int db_getfenv (lua_State *L) {
  35. luaL_checkany(L, 1);
  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) || lua_islightfunction(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_checkanyfunction(L, arg+1);
  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[LUA_MAXINPUT];
  253. #if defined(LUA_USE_STDIO)
  254. fputs("lua_debug> ", stderr);
  255. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  256. #else
  257. // luai_writestringerror("%s", "lua_debug>");
  258. if (lua_readline(L, buffer, "lua_debug>") == 0 ||
  259. #endif
  260. strcmp(buffer, "cont\n") == 0)
  261. return 0;
  262. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  263. lua_pcall(L, 0, 0, 0)) {
  264. #if defined(LUA_USE_STDIO)
  265. fputs(lua_tostring(L, -1), stderr);
  266. fputs("\n", stderr);
  267. #else
  268. luai_writestringerror("%s\n", lua_tostring(L, -1));
  269. #endif
  270. }
  271. lua_settop(L, 0); /* remove eventual returns */
  272. }
  273. }
  274. #define LEVELS1 12 /* size of the first part of the stack */
  275. #define LEVELS2 10 /* size of the second part of the stack */
  276. static int db_errorfb (lua_State *L) {
  277. int level;
  278. int firstpart = 1; /* still before eventual `...' */
  279. int arg;
  280. lua_State *L1 = getthread(L, &arg);
  281. lua_Debug ar;
  282. if (lua_isnumber(L, arg+2)) {
  283. level = (int)lua_tointeger(L, arg+2);
  284. lua_pop(L, 1);
  285. }
  286. else
  287. level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
  288. if (lua_gettop(L) == arg)
  289. lua_pushliteral(L, "");
  290. else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
  291. else lua_pushliteral(L, "\n");
  292. lua_pushliteral(L, "stack traceback:");
  293. while (lua_getstack(L1, level++, &ar)) {
  294. if (level > LEVELS1 && firstpart) {
  295. /* no more than `LEVELS2' more levels? */
  296. if (!lua_getstack(L1, level+LEVELS2, &ar))
  297. level--; /* keep going */
  298. else {
  299. lua_pushliteral(L, "\n\t..."); /* too many levels */
  300. while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
  301. level++;
  302. }
  303. firstpart = 0;
  304. continue;
  305. }
  306. lua_pushliteral(L, "\n\t");
  307. lua_getinfo(L1, "Snl", &ar);
  308. lua_pushfstring(L, "%s:", ar.short_src);
  309. if (ar.currentline > 0)
  310. lua_pushfstring(L, "%d:", ar.currentline);
  311. if (*ar.namewhat != '\0') /* is there a name? */
  312. lua_pushfstring(L, " in function " LUA_QS, ar.name);
  313. else {
  314. if (*ar.what == 'm') /* main? */
  315. lua_pushfstring(L, " in main chunk");
  316. else if (*ar.what == 'C' || *ar.what == 't')
  317. lua_pushliteral(L, " ?"); /* C function or tail call */
  318. else
  319. lua_pushfstring(L, " in function <%s:%d>",
  320. ar.short_src, ar.linedefined);
  321. }
  322. lua_concat(L, lua_gettop(L) - arg);
  323. }
  324. lua_concat(L, lua_gettop(L) - arg);
  325. return 1;
  326. }
  327. #define MIN_OPT_LEVEL 1
  328. #include "lrodefs.h"
  329. const LUA_REG_TYPE dblib[] = {
  330. {LSTRKEY("debug"), LFUNCVAL(db_debug)},
  331. {LSTRKEY("getfenv"), LFUNCVAL(db_getfenv)},
  332. {LSTRKEY("gethook"), LFUNCVAL(db_gethook)},
  333. {LSTRKEY("getinfo"), LFUNCVAL(db_getinfo)},
  334. {LSTRKEY("getlocal"), LFUNCVAL(db_getlocal)},
  335. {LSTRKEY("getregistry"), LFUNCVAL(db_getregistry)},
  336. {LSTRKEY("getmetatable"), LFUNCVAL(db_getmetatable)},
  337. {LSTRKEY("getupvalue"), LFUNCVAL(db_getupvalue)},
  338. {LSTRKEY("setfenv"), LFUNCVAL(db_setfenv)},
  339. {LSTRKEY("sethook"), LFUNCVAL(db_sethook)},
  340. {LSTRKEY("setlocal"), LFUNCVAL(db_setlocal)},
  341. {LSTRKEY("setmetatable"), LFUNCVAL(db_setmetatable)},
  342. {LSTRKEY("setupvalue"), LFUNCVAL(db_setupvalue)},
  343. {LSTRKEY("traceback"), LFUNCVAL(db_errorfb)},
  344. {LNILKEY, LNILVAL}
  345. };
  346. LUALIB_API int luaopen_debug (lua_State *L) {
  347. LREGISTER(L, LUA_DBLIBNAME, dblib);
  348. }