ldblib.c 8.7 KB

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