2
0

ldblib.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. ** $Id: ldblib.c,v 1.74 2002/12/04 17:38:31 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, 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 const char KEY_HOOK = 'h';
  94. static void hookf (lua_State *L, lua_Debug *ar) {
  95. static const char *const hooknames[] = {"call", "return", "line", "count"};
  96. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  97. lua_rawget(L, LUA_REGISTRYINDEX);
  98. if (lua_isfunction(L, -1)) {
  99. lua_pushstring(L, hooknames[(int)ar->event]);
  100. if (ar->currentline >= 0) lua_pushnumber(L, ar->currentline);
  101. else lua_pushnil(L);
  102. lua_assert(lua_getinfo(L, "lS", ar));
  103. lua_call(L, 2, 0);
  104. }
  105. else
  106. lua_pop(L, 1); /* pop result from gettable */
  107. }
  108. static int makemask (const char *smask, int count) {
  109. int mask = 0;
  110. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  111. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  112. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  113. if (count > 0) mask |= LUA_MASKCOUNT;
  114. return mask;
  115. }
  116. static char *unmakemask (int mask, char *smask) {
  117. int i = 0;
  118. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  119. if (mask & LUA_MASKRET) smask[i++] = 'r';
  120. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  121. smask[i] = '\0';
  122. return smask;
  123. }
  124. static int sethook (lua_State *L) {
  125. if (lua_isnoneornil(L, 1)) {
  126. lua_settop(L, 1);
  127. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  128. }
  129. else {
  130. const char *smask = luaL_checkstring(L, 2);
  131. int count = luaL_optint(L, 3, 0);
  132. luaL_checktype(L, 1, LUA_TFUNCTION);
  133. lua_sethook(L, hookf, makemask(smask, count), count);
  134. }
  135. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  136. lua_pushvalue(L, 1);
  137. lua_rawset(L, LUA_REGISTRYINDEX); /* set new hook */
  138. return 0;
  139. }
  140. static int gethook (lua_State *L) {
  141. char buff[5];
  142. int mask = lua_gethookmask(L);
  143. lua_Hook hook = lua_gethook(L);
  144. if (hook != NULL && hook != hookf) /* external hook? */
  145. lua_pushliteral(L, "external hook");
  146. else {
  147. lua_pushlightuserdata(L, (void *)&KEY_HOOK);
  148. lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */
  149. }
  150. lua_pushstring(L, unmakemask(mask, buff));
  151. lua_pushnumber(L, lua_gethookcount(L));
  152. return 3;
  153. }
  154. static int debug (lua_State *L) {
  155. for (;;) {
  156. char buffer[250];
  157. fputs("lua_debug> ", stderr);
  158. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  159. strcmp(buffer, "cont\n") == 0)
  160. return 0;
  161. lua_dostring(L, buffer);
  162. lua_settop(L, 0); /* remove eventual returns */
  163. }
  164. }
  165. #define LEVELS1 12 /* size of the first part of the stack */
  166. #define LEVELS2 10 /* size of the second part of the stack */
  167. static int errorfb (lua_State *L) {
  168. int level = 1; /* skip level 0 (it's this function) */
  169. int firstpart = 1; /* still before eventual `...' */
  170. lua_Debug ar;
  171. if (lua_gettop(L) == 0)
  172. lua_pushliteral(L, "");
  173. else if (!lua_isstring(L, 1)) return 1; /* no string message */
  174. else lua_pushliteral(L, "\n");
  175. lua_pushliteral(L, "stack traceback:");
  176. while (lua_getstack(L, level++, &ar)) {
  177. if (level > LEVELS1 && firstpart) {
  178. /* no more than `LEVELS2' more levels? */
  179. if (!lua_getstack(L, level+LEVELS2, &ar))
  180. level--; /* keep going */
  181. else {
  182. lua_pushliteral(L, "\n\t..."); /* too many levels */
  183. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  184. level++;
  185. }
  186. firstpart = 0;
  187. continue;
  188. }
  189. lua_pushliteral(L, "\n\t");
  190. lua_getinfo(L, "Snl", &ar);
  191. lua_pushfstring(L, "%s:", ar.short_src);
  192. if (ar.currentline > 0)
  193. lua_pushfstring(L, "%d:", ar.currentline);
  194. switch (*ar.namewhat) {
  195. case 'g': /* global */
  196. case 'l': /* local */
  197. case 'f': /* field */
  198. case 'm': /* method */
  199. lua_pushfstring(L, " in function `%s'", ar.name);
  200. break;
  201. default: {
  202. if (*ar.what == 'm') /* main? */
  203. lua_pushfstring(L, " in main chunk");
  204. else if (*ar.what == 'C') /* C function? */
  205. lua_pushfstring(L, "%s", ar.short_src);
  206. else
  207. lua_pushfstring(L, " in function <%s:%d>",
  208. ar.short_src, ar.linedefined);
  209. }
  210. }
  211. lua_concat(L, lua_gettop(L));
  212. }
  213. lua_concat(L, lua_gettop(L));
  214. return 1;
  215. }
  216. static const luaL_reg dblib[] = {
  217. {"getlocal", getlocal},
  218. {"getinfo", getinfo},
  219. {"gethook", gethook},
  220. {"sethook", sethook},
  221. {"setlocal", setlocal},
  222. {"debug", debug},
  223. {"traceback", errorfb},
  224. {NULL, NULL}
  225. };
  226. LUALIB_API int lua_dblibopen (lua_State *L) {
  227. luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
  228. lua_pushliteral(L, "_TRACEBACK");
  229. lua_pushcfunction(L, errorfb);
  230. lua_settable(L, LUA_GLOBALSINDEX);
  231. return 0;
  232. }