ldblib.c 12 KB

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