ldblib.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. ** $Id: ldblib.c,v 1.50 2002/05/06 19:05:10 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. luaL_vstr(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. if (ar.source)
  47. settabss(L, "short_src", ar.short_src);
  48. settabsi(L, "linedefined", ar.linedefined);
  49. settabss(L, "what", ar.what);
  50. break;
  51. case 'l':
  52. settabsi(L, "currentline", ar.currentline);
  53. break;
  54. case 'u':
  55. settabsi(L, "nups", ar.nups);
  56. break;
  57. case 'n':
  58. settabss(L, "name", ar.name);
  59. settabss(L, "namewhat", ar.namewhat);
  60. break;
  61. case 'f':
  62. lua_pushliteral(L, "func");
  63. lua_pushvalue(L, -3);
  64. lua_rawset(L, -3);
  65. break;
  66. }
  67. }
  68. return 1; /* return table */
  69. }
  70. static int getlocal (lua_State *L) {
  71. lua_Debug ar;
  72. const char *name;
  73. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  74. return luaL_argerror(L, 1, "level out of range");
  75. name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
  76. if (name) {
  77. lua_pushstring(L, name);
  78. lua_pushvalue(L, -2);
  79. return 2;
  80. }
  81. else {
  82. lua_pushnil(L);
  83. return 1;
  84. }
  85. }
  86. static int setlocal (lua_State *L) {
  87. lua_Debug ar;
  88. if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
  89. return luaL_argerror(L, 1, "level out of range");
  90. luaL_check_any(L, 3);
  91. lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
  92. return 1;
  93. }
  94. static const char KEY_CALLHOOK = 'c';
  95. static const char KEY_LINEHOOK = 'l';
  96. static void hookf (lua_State *L, void *key) {
  97. lua_pushudataval(L, key);
  98. lua_rawget(L, LUA_REGISTRYINDEX);
  99. if (lua_isfunction(L, -1)) {
  100. lua_pushvalue(L, -2); /* original argument (below function) */
  101. lua_rawcall(L, 1, 0);
  102. }
  103. else
  104. lua_pop(L, 1); /* pop result from gettable */
  105. }
  106. static void callf (lua_State *L, lua_Debug *ar) {
  107. lua_pushstring(L, ar->event);
  108. hookf(L, (void *)&KEY_CALLHOOK);
  109. }
  110. static void linef (lua_State *L, lua_Debug *ar) {
  111. lua_pushnumber(L, ar->currentline);
  112. hookf(L, (void *)&KEY_LINEHOOK);
  113. }
  114. static void sethook (lua_State *L, void *key, lua_Hook hook,
  115. lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
  116. lua_settop(L, 1);
  117. if (lua_isnoneornil(L, 1))
  118. (*sethookf)(L, NULL);
  119. else if (lua_isfunction(L, 1))
  120. (*sethookf)(L, hook);
  121. else
  122. luaL_argerror(L, 1, "function expected");
  123. lua_pushudataval(L, key);
  124. lua_rawget(L, LUA_REGISTRYINDEX); /* get old value */
  125. lua_pushudataval(L, key);
  126. lua_pushvalue(L, 1);
  127. lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */
  128. }
  129. static int setcallhook (lua_State *L) {
  130. sethook(L, (void *)&KEY_CALLHOOK, callf, lua_setcallhook);
  131. return 1;
  132. }
  133. static int setlinehook (lua_State *L) {
  134. sethook(L, (void *)&KEY_LINEHOOK, linef, lua_setlinehook);
  135. return 1;
  136. }
  137. static int debug (lua_State *L) {
  138. for (;;) {
  139. char buffer[250];
  140. fputs("lua_debug> ", stderr);
  141. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  142. strcmp(buffer, "cont\n") == 0)
  143. return 0;
  144. lua_dostring(L, buffer);
  145. lua_settop(L, 0); /* remove eventual returns */
  146. }
  147. }
  148. #define LEVELS1 12 /* size of the first part of the stack */
  149. #define LEVELS2 10 /* size of the second part of the stack */
  150. static int errorfb (lua_State *L) {
  151. int level = 1; /* skip level 0 (it's this function) */
  152. int firstpart = 1; /* still before eventual `...' */
  153. lua_Debug ar;
  154. luaL_check_string(L, 1);
  155. lua_settop(L, 1);
  156. lua_pushliteral(L, "\n");
  157. while (lua_getstack(L, level++, &ar)) {
  158. char buff[10];
  159. if (level == 2)
  160. lua_pushliteral(L, "stack traceback:\n");
  161. else if (level > LEVELS1 && firstpart) {
  162. /* no more than `LEVELS2' more levels? */
  163. if (!lua_getstack(L, level+LEVELS2, &ar))
  164. level--; /* keep going */
  165. else {
  166. lua_pushliteral(L, " ...\n"); /* too many levels */
  167. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  168. level++;
  169. }
  170. firstpart = 0;
  171. continue;
  172. }
  173. sprintf(buff, "%4d: ", level-1);
  174. lua_pushstring(L, buff);
  175. lua_getinfo(L, "Snl", &ar);
  176. switch (*ar.namewhat) {
  177. case 'g': case 'l': /* global, local */
  178. luaL_vstr(L, "function `%s'", ar.name);
  179. break;
  180. case 'f': /* field */
  181. case 'm': /* method */
  182. luaL_vstr(L, "method `%s'", ar.name);
  183. break;
  184. default: {
  185. if (*ar.what == 'm') /* main? */
  186. luaL_vstr(L, "main of %s", ar.short_src);
  187. else if (*ar.what == 'C') /* C function? */
  188. luaL_vstr(L, "%s", ar.short_src);
  189. else
  190. luaL_vstr(L, "function <%d:%s>", ar.linedefined, ar.short_src);
  191. ar.source = NULL; /* do not print source again */
  192. }
  193. }
  194. if (ar.currentline > 0)
  195. luaL_vstr(L, " at line %d", ar.currentline);
  196. if (ar.source)
  197. luaL_vstr(L, " [%s]", ar.short_src);
  198. lua_pushliteral(L, "\n");
  199. lua_concat(L, lua_gettop(L));
  200. }
  201. lua_concat(L, lua_gettop(L));
  202. return 1;
  203. }
  204. static const luaL_reg dblib[] = {
  205. {"getlocal", getlocal},
  206. {"getinfo", getinfo},
  207. {"setcallhook", setcallhook},
  208. {"setlinehook", setlinehook},
  209. {"setlocal", setlocal},
  210. {"debug", debug},
  211. {NULL, NULL}
  212. };
  213. LUALIB_API int lua_dblibopen (lua_State *L) {
  214. luaL_opennamedlib(L, "dbg", dblib, 0);
  215. lua_register(L, "_ERRORMESSAGE", errorfb);
  216. return 0;
  217. }