ldblib.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. ** $Id: ldblib.c,v 1.54 2002/06/03 17:47:18 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_rawcall(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. hookf(L, (void *)&KEY_CALLHOOK);
  108. }
  109. static void linef (lua_State *L, lua_Debug *ar) {
  110. lua_pushnumber(L, ar->currentline);
  111. hookf(L, (void *)&KEY_LINEHOOK);
  112. }
  113. static void sethook (lua_State *L, void *key, lua_Hook hook,
  114. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  115. lua_settop(L, 1);
  116. if (lua_isnoneornil(L, 1))
  117. (*sethookf)(L, NULL);
  118. else if (lua_isfunction(L, 1))
  119. (*sethookf)(L, hook);
  120. else
  121. luaL_argerror(L, 1, "function expected");
  122. lua_pushudataval(L, key);
  123. lua_rawget(L, LUA_REGISTRYINDEX); /* get old value */
  124. lua_pushudataval(L, key);
  125. lua_pushvalue(L, 1);
  126. lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */
  127. }
  128. static int setcallhook (lua_State *L) {
  129. sethook(L, (void *)&KEY_CALLHOOK, callf, lua_setcallhook);
  130. return 1;
  131. }
  132. static int setlinehook (lua_State *L) {
  133. sethook(L, (void *)&KEY_LINEHOOK, linef, lua_setlinehook);
  134. return 1;
  135. }
  136. static int debug (lua_State *L) {
  137. for (;;) {
  138. char buffer[250];
  139. fputs("lua_debug> ", stderr);
  140. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  141. strcmp(buffer, "cont\n") == 0)
  142. return 0;
  143. lua_dostring(L, buffer);
  144. lua_settop(L, 0); /* remove eventual returns */
  145. }
  146. }
  147. #define LEVELS1 12 /* size of the first part of the stack */
  148. #define LEVELS2 10 /* size of the second part of the stack */
  149. static int errorfb (lua_State *L) {
  150. int level = 1; /* skip level 0 (it's this function) */
  151. int firstpart = 1; /* still before eventual `...' */
  152. lua_Debug ar;
  153. luaL_check_string(L, 1);
  154. lua_settop(L, 1);
  155. lua_pushliteral(L, "\n");
  156. lua_pushliteral(L, "stack traceback:\n");
  157. while (lua_getstack(L, level++, &ar)) {
  158. char buff[10];
  159. if (level > LEVELS1 && firstpart) {
  160. /* no more than `LEVELS2' more levels? */
  161. if (!lua_getstack(L, level+LEVELS2, &ar))
  162. level--; /* keep going */
  163. else {
  164. lua_pushliteral(L, " ...\n"); /* too many levels */
  165. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  166. level++;
  167. }
  168. firstpart = 0;
  169. continue;
  170. }
  171. sprintf(buff, "%4d- ", level-1);
  172. lua_pushstring(L, buff);
  173. lua_getinfo(L, "Snl", &ar);
  174. lua_pushfstring(L, "%s:", ar.short_src);
  175. if (ar.currentline > 0)
  176. lua_pushfstring(L, "%d:", ar.currentline);
  177. switch (*ar.namewhat) {
  178. case 'g': /* global */
  179. case 'l': /* local */
  180. case 'f': /* field */
  181. case 'm': /* method */
  182. lua_pushfstring(L, " in function `%s'", ar.name);
  183. break;
  184. default: {
  185. if (*ar.what == 'm') /* main? */
  186. lua_pushfstring(L, " in main chunk");
  187. else if (*ar.what == 'C') /* C function? */
  188. lua_pushfstring(L, "%s", ar.short_src);
  189. else
  190. lua_pushfstring(L, " in function <%s:%d>",
  191. ar.short_src, ar.linedefined);
  192. }
  193. }
  194. lua_pushliteral(L, "\n");
  195. lua_concat(L, lua_gettop(L));
  196. }
  197. lua_concat(L, lua_gettop(L));
  198. return 1;
  199. }
  200. static const luaL_reg dblib[] = {
  201. {"getlocal", getlocal},
  202. {"getinfo", getinfo},
  203. {"setcallhook", setcallhook},
  204. {"setlinehook", setlinehook},
  205. {"setlocal", setlocal},
  206. {"debug", debug},
  207. {NULL, NULL}
  208. };
  209. LUALIB_API int lua_dblibopen (lua_State *L) {
  210. luaL_opennamedlib(L, LUA_DBLIBNAME, dblib, 0);
  211. lua_register(L, "_ERRORMESSAGE", errorfb);
  212. return 0;
  213. }