ldblib.c 6.8 KB

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