ldblib.c 11 KB

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