ldblib.c 8.5 KB

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