ldblib.c 7.5 KB

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