ldblib.c 6.6 KB

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