ldblib.c 11 KB

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