lib_debug.c 9.0 KB

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