lib_debug.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. ** Debug library.
  3. ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lib_debug_c
  9. #define LUA_LIB
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "lualib.h"
  13. #include "lj_obj.h"
  14. #include "lj_err.h"
  15. #include "lj_lib.h"
  16. /* ------------------------------------------------------------------------ */
  17. #define LJLIB_MODULE_debug
  18. LJLIB_CF(debug_getregistry)
  19. {
  20. copyTV(L, L->top++, registry(L));
  21. return 1;
  22. }
  23. LJLIB_CF(debug_getmetatable)
  24. {
  25. lj_lib_checkany(L, 1);
  26. if (!lua_getmetatable(L, 1)) {
  27. setnilV(L->top-1);
  28. }
  29. return 1;
  30. }
  31. LJLIB_CF(debug_setmetatable)
  32. {
  33. lj_lib_checktabornil(L, 2);
  34. L->top = L->base+2;
  35. lua_setmetatable(L, 1);
  36. setboolV(L->top-1, 1);
  37. return 1;
  38. }
  39. LJLIB_CF(debug_getfenv)
  40. {
  41. lj_lib_checkany(L, 1);
  42. lua_getfenv(L, 1);
  43. return 1;
  44. }
  45. LJLIB_CF(debug_setfenv)
  46. {
  47. lj_lib_checktab(L, 2);
  48. L->top = L->base+2;
  49. if (!lua_setfenv(L, 1))
  50. lj_err_caller(L, LJ_ERR_SETFENV);
  51. return 1;
  52. }
  53. /* ------------------------------------------------------------------------ */
  54. static void settabss(lua_State *L, const char *i, const char *v)
  55. {
  56. lua_pushstring(L, v);
  57. lua_setfield(L, -2, i);
  58. }
  59. static void settabsi(lua_State *L, const char *i, int v)
  60. {
  61. lua_pushinteger(L, v);
  62. lua_setfield(L, -2, i);
  63. }
  64. static lua_State *getthread(lua_State *L, int *arg)
  65. {
  66. if (L->base < L->top && tvisthread(L->base)) {
  67. *arg = 1;
  68. return threadV(L->base);
  69. } else {
  70. *arg = 0;
  71. return L;
  72. }
  73. }
  74. static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)
  75. {
  76. if (L == L1) {
  77. lua_pushvalue(L, -2);
  78. lua_remove(L, -3);
  79. }
  80. else
  81. lua_xmove(L1, L, 1);
  82. lua_setfield(L, -2, fname);
  83. }
  84. LJLIB_CF(debug_getinfo)
  85. {
  86. lua_Debug ar;
  87. int arg;
  88. lua_State *L1 = getthread(L, &arg);
  89. const char *options = luaL_optstring(L, arg+2, "flnSu");
  90. if (lua_isnumber(L, arg+1)) {
  91. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
  92. setnilV(L->top-1);
  93. return 1;
  94. }
  95. } else if (L->base+arg < L->top && tvisfunc(L->base+arg)) {
  96. options = lua_pushfstring(L, ">%s", options);
  97. setfuncV(L1, L1->top++, funcV(L->base+arg));
  98. } else {
  99. lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL);
  100. }
  101. if (!lua_getinfo(L1, options, &ar))
  102. lj_err_arg(L, arg+2, LJ_ERR_INVOPT);
  103. lua_createtable(L, 0, 16);
  104. if (strchr(options, 'S')) {
  105. settabss(L, "source", ar.source);
  106. settabss(L, "short_src", ar.short_src);
  107. settabsi(L, "linedefined", ar.linedefined);
  108. settabsi(L, "lastlinedefined", ar.lastlinedefined);
  109. settabss(L, "what", ar.what);
  110. }
  111. if (strchr(options, 'l'))
  112. settabsi(L, "currentline", ar.currentline);
  113. if (strchr(options, 'u'))
  114. settabsi(L, "nups", ar.nups);
  115. if (strchr(options, 'n')) {
  116. settabss(L, "name", ar.name);
  117. settabss(L, "namewhat", ar.namewhat);
  118. }
  119. if (strchr(options, 'L'))
  120. treatstackoption(L, L1, "activelines");
  121. if (strchr(options, 'f'))
  122. treatstackoption(L, L1, "func");
  123. return 1; /* return table */
  124. }
  125. LJLIB_CF(debug_getlocal)
  126. {
  127. int arg;
  128. lua_State *L1 = getthread(L, &arg);
  129. lua_Debug ar;
  130. const char *name;
  131. if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))
  132. lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);
  133. name = lua_getlocal(L1, &ar, lj_lib_checkint(L, arg+2));
  134. if (name) {
  135. lua_xmove(L1, L, 1);
  136. lua_pushstring(L, name);
  137. lua_pushvalue(L, -2);
  138. return 2;
  139. } else {
  140. setnilV(L->top-1);
  141. return 1;
  142. }
  143. }
  144. LJLIB_CF(debug_setlocal)
  145. {
  146. int arg;
  147. lua_State *L1 = getthread(L, &arg);
  148. lua_Debug ar;
  149. TValue *tv;
  150. if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))
  151. lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);
  152. tv = lj_lib_checkany(L, arg+3);
  153. copyTV(L1, L1->top++, tv);
  154. lua_pushstring(L, lua_setlocal(L1, &ar, lj_lib_checkint(L, arg+2)));
  155. return 1;
  156. }
  157. static int debug_getupvalue(lua_State *L, int get)
  158. {
  159. int32_t n = lj_lib_checkint(L, 2);
  160. if (isluafunc(lj_lib_checkfunc(L, 1))) {
  161. const char *name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  162. if (name) {
  163. lua_pushstring(L, name);
  164. if (!get) return 1;
  165. copyTV(L, L->top, L->top-2);
  166. L->top++;
  167. return 2;
  168. }
  169. }
  170. return 0;
  171. }
  172. LJLIB_CF(debug_getupvalue)
  173. {
  174. return debug_getupvalue(L, 1);
  175. }
  176. LJLIB_CF(debug_setupvalue)
  177. {
  178. lj_lib_checkany(L, 3);
  179. return debug_getupvalue(L, 0);
  180. }
  181. /* ------------------------------------------------------------------------ */
  182. static const char KEY_HOOK = 'h';
  183. static void hookf(lua_State *L, lua_Debug *ar)
  184. {
  185. static const char *const hooknames[] =
  186. {"call", "return", "line", "count", "tail return"};
  187. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  188. lua_rawget(L, LUA_REGISTRYINDEX);
  189. if (lua_isfunction(L, -1)) {
  190. lua_pushstring(L, hooknames[(int)ar->event]);
  191. if (ar->currentline >= 0)
  192. lua_pushinteger(L, ar->currentline);
  193. else lua_pushnil(L);
  194. lua_call(L, 2, 0);
  195. }
  196. }
  197. static int makemask(const char *smask, int count)
  198. {
  199. int mask = 0;
  200. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  201. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  202. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  203. if (count > 0) mask |= LUA_MASKCOUNT;
  204. return mask;
  205. }
  206. static char *unmakemask(int mask, char *smask)
  207. {
  208. int i = 0;
  209. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  210. if (mask & LUA_MASKRET) smask[i++] = 'r';
  211. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  212. smask[i] = '\0';
  213. return smask;
  214. }
  215. LJLIB_CF(debug_sethook)
  216. {
  217. int arg, mask, count;
  218. lua_Hook func;
  219. (void)getthread(L, &arg);
  220. if (lua_isnoneornil(L, arg+1)) {
  221. lua_settop(L, arg+1);
  222. func = NULL; mask = 0; count = 0; /* turn off hooks */
  223. } else {
  224. const char *smask = luaL_checkstring(L, arg+2);
  225. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  226. count = luaL_optint(L, arg+3, 0);
  227. func = hookf; mask = makemask(smask, count);
  228. }
  229. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  230. lua_pushvalue(L, arg+1);
  231. lua_rawset(L, LUA_REGISTRYINDEX);
  232. lua_sethook(L, func, mask, count);
  233. return 0;
  234. }
  235. LJLIB_CF(debug_gethook)
  236. {
  237. char buff[5];
  238. int mask = lua_gethookmask(L);
  239. lua_Hook hook = lua_gethook(L);
  240. if (hook != NULL && hook != hookf) { /* external hook? */
  241. lua_pushliteral(L, "external hook");
  242. } else {
  243. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  244. lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
  245. }
  246. lua_pushstring(L, unmakemask(mask, buff));
  247. lua_pushinteger(L, lua_gethookcount(L));
  248. return 3;
  249. }
  250. /* ------------------------------------------------------------------------ */
  251. LJLIB_CF(debug_debug)
  252. {
  253. for (;;) {
  254. char buffer[250];
  255. fputs("lua_debug> ", stderr);
  256. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  257. strcmp(buffer, "cont\n") == 0)
  258. return 0;
  259. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  260. lua_pcall(L, 0, 0, 0)) {
  261. fputs(lua_tostring(L, -1), stderr);
  262. fputs("\n", stderr);
  263. }
  264. lua_settop(L, 0); /* remove eventual returns */
  265. }
  266. }
  267. /* ------------------------------------------------------------------------ */
  268. #define LEVELS1 12 /* size of the first part of the stack */
  269. #define LEVELS2 10 /* size of the second part of the stack */
  270. LJLIB_CF(debug_traceback)
  271. {
  272. int level;
  273. int firstpart = 1; /* still before eventual `...' */
  274. int arg;
  275. lua_State *L1 = getthread(L, &arg);
  276. lua_Debug ar;
  277. if (lua_isnumber(L, arg+2)) {
  278. level = (int)lua_tointeger(L, arg+2);
  279. lua_pop(L, 1);
  280. }
  281. else
  282. level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
  283. if (lua_gettop(L) == arg)
  284. lua_pushliteral(L, "");
  285. else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
  286. else lua_pushliteral(L, "\n");
  287. lua_pushliteral(L, "stack traceback:");
  288. while (lua_getstack(L1, level++, &ar)) {
  289. if (level > LEVELS1 && firstpart) {
  290. /* no more than `LEVELS2' more levels? */
  291. if (!lua_getstack(L1, level+LEVELS2, &ar)) {
  292. level--; /* keep going */
  293. } else {
  294. lua_pushliteral(L, "\n\t..."); /* too many levels */
  295. /* This only works with LuaJIT 2.x. Avoids O(n^2) behaviour. */
  296. lua_getstack(L1, -10, &ar);
  297. level = ar.i_ci - LEVELS2;
  298. }
  299. firstpart = 0;
  300. continue;
  301. }
  302. lua_pushliteral(L, "\n\t");
  303. lua_getinfo(L1, "Snl", &ar);
  304. lua_pushfstring(L, "%s:", ar.short_src);
  305. if (ar.currentline > 0)
  306. lua_pushfstring(L, "%d:", ar.currentline);
  307. if (*ar.namewhat != '\0') { /* is there a name? */
  308. lua_pushfstring(L, " in function " LUA_QS, ar.name);
  309. } else {
  310. if (*ar.what == 'm') /* main? */
  311. lua_pushfstring(L, " in main chunk");
  312. else if (*ar.what == 'C' || *ar.what == 't')
  313. lua_pushliteral(L, " ?"); /* C function or tail call */
  314. else
  315. lua_pushfstring(L, " in function <%s:%d>",
  316. ar.short_src, ar.linedefined);
  317. }
  318. lua_concat(L, lua_gettop(L) - arg);
  319. }
  320. lua_concat(L, lua_gettop(L) - arg);
  321. return 1;
  322. }
  323. /* ------------------------------------------------------------------------ */
  324. #include "lj_libdef.h"
  325. LUALIB_API int luaopen_debug(lua_State *L)
  326. {
  327. LJ_LIB_REG(L, LUA_DBLIBNAME, debug);
  328. return 1;
  329. }