ldblib.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. ** $Id: ldblib.c,v 1.60 2002/06/18 17:42:52 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 "luadebug.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_opt_string(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_check_int(L, 1), &ar)) /* level out of range? */
  73. return luaL_argerror(L, 1, "level out of range");
  74. name = lua_getlocal(L, &ar, luaL_check_int(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_check_int(L, 1), &ar)) /* level out of range? */
  88. return luaL_argerror(L, 1, "level out of range");
  89. luaL_check_any(L, 3);
  90. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  91. return 1;
  92. }
  93. static const char KEY_CALLHOOK = 'c';
  94. static const char KEY_LINEHOOK = 'l';
  95. static void hookf (lua_State *L, void *key) {
  96. lua_pushudataval(L, key);
  97. lua_rawget(L, LUA_REGISTRYINDEX);
  98. if (lua_isfunction(L, -1)) {
  99. lua_pushvalue(L, -2); /* original argument (below function) */
  100. lua_call(L, 1, 0);
  101. }
  102. else
  103. lua_pop(L, 1); /* pop result from gettable */
  104. }
  105. static void callf (lua_State *L, lua_Debug *ar) {
  106. lua_pushstring(L, ar->event);
  107. lua_assert(lua_getinfo(L, "lS", ar) && ar->currentline == -1);
  108. hookf(L, (void *)&KEY_CALLHOOK);
  109. }
  110. static void linef (lua_State *L, lua_Debug *ar) {
  111. lua_pushnumber(L, ar->currentline);
  112. lua_assert((ar->currentline = ar->linedefined = -1,
  113. lua_getinfo(L, "lS", ar) &&
  114. ar->currentline == lua_tonumber(L, -1) &&
  115. ar->linedefined >= 0));
  116. hookf(L, (void *)&KEY_LINEHOOK);
  117. }
  118. static void sethook (lua_State *L, void *key, lua_Hook hook,
  119. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  120. lua_settop(L, 1);
  121. if (lua_isnoneornil(L, 1))
  122. (*sethookf)(L, NULL);
  123. else if (lua_isfunction(L, 1))
  124. (*sethookf)(L, hook);
  125. else
  126. luaL_argerror(L, 1, "function expected");
  127. lua_pushudataval(L, key);
  128. lua_rawget(L, LUA_REGISTRYINDEX); /* get old value */
  129. lua_pushudataval(L, key);
  130. lua_pushvalue(L, 1);
  131. lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */
  132. }
  133. static int setcallhook (lua_State *L) {
  134. sethook(L, (void *)&KEY_CALLHOOK, callf, lua_setcallhook);
  135. return 1;
  136. }
  137. static int setlinehook (lua_State *L) {
  138. sethook(L, (void *)&KEY_LINEHOOK, linef, lua_setlinehook);
  139. return 1;
  140. }
  141. static int debug (lua_State *L) {
  142. for (;;) {
  143. char buffer[250];
  144. fputs("lua_debug> ", stderr);
  145. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  146. strcmp(buffer, "cont\n") == 0)
  147. return 0;
  148. lua_dostring(L, buffer);
  149. lua_settop(L, 0); /* remove eventual returns */
  150. }
  151. }
  152. #define LEVELS1 12 /* size of the first part of the stack */
  153. #define LEVELS2 10 /* size of the second part of the stack */
  154. static int errorfb (lua_State *L) {
  155. int level = 1; /* skip level 0 (it's this function) */
  156. int firstpart = 1; /* still before eventual `...' */
  157. int alllevels = 1;
  158. const char *msg = lua_tostring(L, 1);
  159. lua_Debug ar;
  160. lua_settop(L, 0);
  161. if (msg) {
  162. alllevels = 0;
  163. if (!strstr(msg, "stack traceback:\n"))
  164. lua_pushliteral(L, "stack traceback:\n");
  165. }
  166. while (lua_getstack(L, level++, &ar)) {
  167. if (level > LEVELS1 && firstpart) {
  168. /* no more than `LEVELS2' more levels? */
  169. if (!lua_getstack(L, level+LEVELS2, &ar))
  170. level--; /* keep going */
  171. else {
  172. lua_pushliteral(L, "\t...\n"); /* too many levels */
  173. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  174. level++;
  175. }
  176. firstpart = 0;
  177. continue;
  178. }
  179. lua_pushliteral(L, "\t");
  180. lua_getinfo(L, "Snlc", &ar);
  181. lua_pushfstring(L, "%s:", ar.short_src);
  182. if (ar.currentline > 0)
  183. lua_pushfstring(L, "%d:", ar.currentline);
  184. switch (*ar.namewhat) {
  185. case 'g': /* global */
  186. case 'l': /* local */
  187. case 'f': /* field */
  188. case 'm': /* method */
  189. lua_pushfstring(L, " in function `%s'", ar.name);
  190. break;
  191. default: {
  192. if (*ar.what == 'm') /* main? */
  193. lua_pushfstring(L, " in main chunk");
  194. else if (*ar.what == 'C') /* C function? */
  195. lua_pushfstring(L, "%s", ar.short_src);
  196. else
  197. lua_pushfstring(L, " in function <%s:%d>",
  198. ar.short_src, ar.linedefined);
  199. }
  200. }
  201. lua_pushliteral(L, "\n");
  202. lua_concat(L, lua_gettop(L));
  203. if (!alllevels && ar.isprotected) break;
  204. }
  205. lua_concat(L, lua_gettop(L));
  206. return 1;
  207. }
  208. static const luaL_reg dblib[] = {
  209. {"getlocal", getlocal},
  210. {"getinfo", getinfo},
  211. {"setcallhook", setcallhook},
  212. {"setlinehook", setlinehook},
  213. {"setlocal", setlocal},
  214. {"debug", debug},
  215. {"traceback", errorfb},
  216. {NULL, NULL}
  217. };
  218. LUALIB_API int lua_dblibopen (lua_State *L) {
  219. luaL_opennamedlib(L, LUA_DBLIBNAME, dblib, 0);
  220. lua_pushliteral(L, LUA_TRACEBACK);
  221. lua_pushcfunction(L, errorfb);
  222. lua_settable(L, LUA_REGISTRYINDEX);
  223. return 0;
  224. }