ldblib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. ** $Id: ldblib.c,v 1.88 2004/09/21 17:58:06 roberto Exp roberto $
  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. static void settabss (lua_State *L, const char *i, const char *v) {
  15. lua_pushstring(L, v);
  16. lua_setfield(L, -2, i);
  17. }
  18. static void settabsi (lua_State *L, const char *i, int v) {
  19. lua_pushinteger(L, v);
  20. lua_setfield(L, -2, i);
  21. }
  22. static lua_State *getthread (lua_State *L, int *arg) {
  23. if (lua_isthread(L, 1)) {
  24. *arg = 1;
  25. return lua_tothread(L, 1);
  26. }
  27. else {
  28. *arg = 0;
  29. return L;
  30. }
  31. }
  32. static int getinfo (lua_State *L) {
  33. lua_Debug ar;
  34. int arg;
  35. lua_State *L1 = getthread(L, &arg);
  36. const char *options = luaL_optstring(L, arg+2, "flnSu");
  37. if (lua_isnumber(L, arg+1)) {
  38. if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
  39. lua_pushnil(L); /* level out of range */
  40. return 1;
  41. }
  42. }
  43. else if (lua_isfunction(L, arg+1)) {
  44. lua_pushfstring(L, ">%s", options);
  45. options = lua_tostring(L, -1);
  46. lua_pushvalue(L, arg+1);
  47. lua_xmove(L, L1, 1);
  48. }
  49. else
  50. return luaL_argerror(L, arg+1, "function or level expected");
  51. if (!lua_getinfo(L1, options, &ar))
  52. return luaL_argerror(L, arg+2, "invalid option");
  53. lua_newtable(L);
  54. for (; *options; options++) {
  55. switch (*options) {
  56. case 'S':
  57. settabss(L, "source", ar.source);
  58. settabss(L, "short_src", ar.short_src);
  59. settabsi(L, "linedefined", ar.linedefined);
  60. settabss(L, "what", ar.what);
  61. break;
  62. case 'l':
  63. settabsi(L, "currentline", ar.currentline);
  64. break;
  65. case 'u':
  66. settabsi(L, "nups", ar.nups);
  67. break;
  68. case 'n':
  69. settabss(L, "name", ar.name);
  70. settabss(L, "namewhat", ar.namewhat);
  71. break;
  72. case 'f':
  73. if (L == L1)
  74. lua_pushvalue(L, -2);
  75. else
  76. lua_xmove(L1, L, 1);
  77. lua_setfield(L, -2, "func");
  78. break;
  79. }
  80. }
  81. return 1; /* return table */
  82. }
  83. static int getlocal (lua_State *L) {
  84. int arg;
  85. lua_State *L1 = getthread(L, &arg);
  86. lua_Debug ar;
  87. const char *name;
  88. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  89. return luaL_argerror(L, arg+1, "level out of range");
  90. name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
  91. if (name) {
  92. lua_xmove(L1, L, 1);
  93. lua_pushstring(L, name);
  94. lua_pushvalue(L, -2);
  95. return 2;
  96. }
  97. else {
  98. lua_pushnil(L);
  99. return 1;
  100. }
  101. }
  102. static int setlocal (lua_State *L) {
  103. int arg;
  104. lua_State *L1 = getthread(L, &arg);
  105. lua_Debug ar;
  106. if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
  107. return luaL_argerror(L, arg+1, "level out of range");
  108. luaL_checkany(L, arg+3);
  109. lua_settop(L, arg+3);
  110. lua_xmove(L, L1, 1);
  111. lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  112. return 1;
  113. }
  114. static int auxupvalue (lua_State *L, int get) {
  115. const char *name;
  116. int n = luaL_checkint(L, 2);
  117. luaL_checktype(L, 1, LUA_TFUNCTION);
  118. if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
  119. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  120. if (name == NULL) return 0;
  121. lua_pushstring(L, name);
  122. lua_insert(L, -(get+1));
  123. return get + 1;
  124. }
  125. static int getupvalue (lua_State *L) {
  126. return auxupvalue(L, 1);
  127. }
  128. static int setupvalue (lua_State *L) {
  129. luaL_checkany(L, 3);
  130. return auxupvalue(L, 0);
  131. }
  132. static const char KEY_HOOK = 'h';
  133. static void hookf (lua_State *L, lua_Debug *ar) {
  134. static const char *const hooknames[] =
  135. {"call", "return", "line", "count", "tail return"};
  136. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  137. lua_rawget(L, LUA_REGISTRYINDEX);
  138. lua_pushlightuserdata(L, L);
  139. lua_rawget(L, -2);
  140. if (lua_isfunction(L, -1)) {
  141. lua_pushstring(L, hooknames[(int)ar->event]);
  142. if (ar->currentline >= 0)
  143. lua_pushinteger(L, ar->currentline);
  144. else lua_pushnil(L);
  145. lua_assert(lua_getinfo(L, "lS", ar));
  146. lua_call(L, 2, 0);
  147. }
  148. }
  149. static int makemask (const char *smask, int count) {
  150. int mask = 0;
  151. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  152. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  153. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  154. if (count > 0) mask |= LUA_MASKCOUNT;
  155. return mask;
  156. }
  157. static char *unmakemask (int mask, char *smask) {
  158. int i = 0;
  159. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  160. if (mask & LUA_MASKRET) smask[i++] = 'r';
  161. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  162. smask[i] = '\0';
  163. return smask;
  164. }
  165. static void gethooktable (lua_State *L) {
  166. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  167. lua_rawget(L, LUA_REGISTRYINDEX);
  168. if (!lua_istable(L, -1)) {
  169. lua_pop(L, 1);
  170. lua_newtable(L);
  171. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  172. lua_pushvalue(L, -2);
  173. lua_rawset(L, LUA_REGISTRYINDEX);
  174. }
  175. }
  176. static int sethook (lua_State *L) {
  177. int arg;
  178. lua_State *L1 = getthread(L, &arg);
  179. if (lua_isnoneornil(L, arg+1)) {
  180. lua_settop(L, arg+1);
  181. lua_sethook(L1, NULL, 0, 0); /* turn off hooks */
  182. }
  183. else {
  184. const char *smask = luaL_checkstring(L, arg+2);
  185. int count = luaL_optint(L, arg+3, 0);
  186. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  187. lua_sethook(L1, hookf, makemask(smask, count), count);
  188. }
  189. gethooktable(L1);
  190. lua_pushlightuserdata(L1, L1);
  191. lua_pushvalue(L, arg+1);
  192. lua_xmove(L, L1, 1);
  193. lua_rawset(L1, -3); /* set new hook */
  194. lua_pop(L1, 1); /* remove hook table */
  195. return 0;
  196. }
  197. static int gethook (lua_State *L) {
  198. int arg;
  199. lua_State *L1 = getthread(L, &arg);
  200. char buff[5];
  201. int mask = lua_gethookmask(L1);
  202. lua_Hook hook = lua_gethook(L1);
  203. if (hook != NULL && hook != hookf) /* external hook? */
  204. lua_pushliteral(L, "external hook");
  205. else {
  206. gethooktable(L1);
  207. lua_pushlightuserdata(L1, L1);
  208. lua_rawget(L1, -2); /* get hook */
  209. lua_remove(L1, -2); /* remove hook table */
  210. lua_xmove(L1, L, 1);
  211. }
  212. lua_pushstring(L, unmakemask(mask, buff));
  213. lua_pushinteger(L, lua_gethookcount(L1));
  214. return 3;
  215. }
  216. static int debug (lua_State *L) {
  217. for (;;) {
  218. char buffer[250];
  219. fputs("lua_debug> ", stderr);
  220. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  221. strcmp(buffer, "cont\n") == 0)
  222. return 0;
  223. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  224. lua_pcall(L, 0, 0, 0)) {
  225. fputs(lua_tostring(L, -1), stderr);
  226. fputs("\n", stderr);
  227. }
  228. lua_settop(L, 0); /* remove eventual returns */
  229. }
  230. }
  231. #define LEVELS1 12 /* size of the first part of the stack */
  232. #define LEVELS2 10 /* size of the second part of the stack */
  233. static int errorfb (lua_State *L) {
  234. int level = 0;
  235. int firstpart = 1; /* still before eventual `...' */
  236. int arg;
  237. lua_State *L1 = getthread(L, &arg);
  238. lua_Debug ar;
  239. if (L == L1) level++; /* skip level 0 (it's this function) */
  240. if (lua_gettop(L) == arg)
  241. lua_pushliteral(L, "");
  242. else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
  243. else lua_pushliteral(L, "\n");
  244. lua_pushliteral(L, "stack traceback:");
  245. while (lua_getstack(L1, level++, &ar)) {
  246. if (level > LEVELS1 && firstpart) {
  247. /* no more than `LEVELS2' more levels? */
  248. if (!lua_getstack(L1, level+LEVELS2, &ar))
  249. level--; /* keep going */
  250. else {
  251. lua_pushliteral(L, "\n\t..."); /* too many levels */
  252. while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
  253. level++;
  254. }
  255. firstpart = 0;
  256. continue;
  257. }
  258. lua_pushliteral(L, "\n\t");
  259. lua_getinfo(L1, "Snl", &ar);
  260. lua_pushfstring(L, "%s:", ar.short_src);
  261. if (ar.currentline > 0)
  262. lua_pushfstring(L, "%d:", ar.currentline);
  263. if (*ar.namewhat != '\0') /* is there a name? */
  264. lua_pushfstring(L, " in function `%s'", ar.name);
  265. else {
  266. if (*ar.what == 'm') /* main? */
  267. lua_pushfstring(L, " in main chunk");
  268. else if (*ar.what == 'C' || *ar.what == 't')
  269. lua_pushliteral(L, " ?"); /* C function or tail call */
  270. else
  271. lua_pushfstring(L, " in function <%s:%d>",
  272. ar.short_src, ar.linedefined);
  273. }
  274. lua_concat(L, lua_gettop(L) - arg);
  275. }
  276. lua_concat(L, lua_gettop(L) - arg);
  277. return 1;
  278. }
  279. static const luaL_reg dblib[] = {
  280. {"getlocal", getlocal},
  281. {"getinfo", getinfo},
  282. {"gethook", gethook},
  283. {"getupvalue", getupvalue},
  284. {"sethook", sethook},
  285. {"setlocal", setlocal},
  286. {"setupvalue", setupvalue},
  287. {"debug", debug},
  288. {"traceback", errorfb},
  289. {NULL, NULL}
  290. };
  291. LUALIB_API int luaopen_debug (lua_State *L) {
  292. luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
  293. lua_pushcfunction(L, errorfb);
  294. lua_setglobal(L, "_TRACEBACK");
  295. return 1;
  296. }