ldblib.c 11 KB

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