ldebug.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. ** $Id: ldebug.c,v 1.5 2000/01/19 12:00:45 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUA_REENTRANT
  7. #include "lapi.h"
  8. #include "lauxlib.h"
  9. #include "ldebug.h"
  10. #include "ldo.h"
  11. #include "lfunc.h"
  12. #include "lobject.h"
  13. #include "lstate.h"
  14. #include "ltable.h"
  15. #include "ltm.h"
  16. #include "lua.h"
  17. #include "luadebug.h"
  18. static const lua_Type normtype[] = { /* ORDER LUA_T */
  19. LUA_T_USERDATA, LUA_T_NUMBER, LUA_T_STRING, LUA_T_ARRAY,
  20. LUA_T_LPROTO, LUA_T_CPROTO, LUA_T_NIL,
  21. LUA_T_LCLOSURE, LUA_T_CCLOSURE,
  22. LUA_T_LCLOSURE, LUA_T_CCLOSURE, /* LUA_T_LCLMARK, LUA_T_CCLMARK */
  23. LUA_T_LPROTO, LUA_T_CPROTO /* LUA_T_LMARK, LUA_T_CMARK */
  24. };
  25. static void setnormalized (TObject *d, const TObject *s) {
  26. d->value = s->value;
  27. d->ttype = normtype[-ttype(s)];
  28. }
  29. static int hasdebuginfo (lua_State *L, StkId f) {
  30. return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE);
  31. }
  32. lua_Dbghook lua_setcallhook (lua_State *L, lua_Dbghook func) {
  33. lua_Dbghook oldhook = L->callhook;
  34. L->callhook = func;
  35. return oldhook;
  36. }
  37. lua_Dbghook lua_setlinehook (lua_State *L, lua_Dbghook func) {
  38. lua_Dbghook oldhook = L->linehook;
  39. L->linehook = func;
  40. return oldhook;
  41. }
  42. int lua_setdebug (lua_State *L, int debug) {
  43. int old = L->debug;
  44. L->debug = debug;
  45. return old;
  46. }
  47. static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
  48. int i;
  49. for (i = (top-1)-L->stack; i>=0; i--) {
  50. if (is_T_MARK(L->stack[i].ttype)) {
  51. if (level == 0)
  52. return L->stack+i;
  53. level--;
  54. }
  55. }
  56. return NULL;
  57. }
  58. int lua_getstack (lua_State *L, int level, lua_Dbgactreg *ar) {
  59. StkId f = aux_stackedfunction(L, level, L->top);
  60. if (f == NULL) return 0; /* there is no such level */
  61. else {
  62. ar->_func = f;
  63. return 1;
  64. }
  65. }
  66. static const char *luaG_getname (lua_State *L, const char **name, StkId top) {
  67. StkId f = aux_stackedfunction(L, 0, top);
  68. if (f == NULL || !hasdebuginfo(L, f) || ttype(f+2) == LUA_T_NIL)
  69. return ""; /* no name available */
  70. else {
  71. int i = (f+2)->value.i;
  72. if (ttype(f) == LUA_T_LCLMARK)
  73. f = protovalue(f);
  74. LUA_ASSERT(L, ttype(f) == LUA_T_LMARK, "must be a Lua function");
  75. *name = tfvalue(f)->strcnst[i]->str;
  76. return luaO_typename(f+2);
  77. }
  78. }
  79. static int lua_nups (StkId f) {
  80. switch (ttype(f)) {
  81. case LUA_T_LCLOSURE: case LUA_T_CCLOSURE:
  82. case LUA_T_LCLMARK: case LUA_T_CCLMARK:
  83. return f->value.cl->nelems;
  84. default:
  85. return 0;
  86. }
  87. }
  88. static int lua_currentline (lua_State *L, StkId f) {
  89. return hasdebuginfo(L, f) ? (f+1)->value.i : -1;
  90. }
  91. static TProtoFunc *getluaproto (StkId f) {
  92. if (ttype(f) == LUA_T_LMARK)
  93. return f->value.tf;
  94. else if (ttype(f) == LUA_T_LCLMARK)
  95. return protovalue(f)->value.tf;
  96. else return NULL;
  97. }
  98. int lua_getlocal (lua_State *L, const lua_Dbgactreg *ar, lua_Dbglocvar *v) {
  99. StkId f = ar->_func;
  100. TProtoFunc *fp = getluaproto(f);
  101. if (!fp) return 0; /* `f' is not a Lua function? */
  102. v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
  103. if (!v->name) return 0;
  104. /* if `name', there must be a LUA_T_LINE and a NAME */
  105. /* therefore, f+3 points to function base */
  106. LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, "");
  107. v->value = luaA_putluaObject(L, (f+3)+(v->index-1));
  108. return 1;
  109. }
  110. int lua_setlocal (lua_State *L, const lua_Dbgactreg *ar, lua_Dbglocvar *v) {
  111. StkId f = ar->_func;
  112. TProtoFunc *fp = getluaproto(f);
  113. if (!fp) return 0; /* `f' is not a Lua function? */
  114. v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
  115. if (!v->name) return 0;
  116. LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, "");
  117. *((f+3)+(v->index-1)) = *v->value;
  118. return 1;
  119. }
  120. static void lua_funcinfo (lua_Dbgactreg *ar) {
  121. StkId func = ar->_func;
  122. switch (ttype(func)) {
  123. case LUA_T_LPROTO: case LUA_T_LMARK:
  124. ar->source = tfvalue(func)->source->str;
  125. ar->linedefined = tfvalue(func)->lineDefined;
  126. ar->what = "Lua";
  127. break;
  128. case LUA_T_LCLOSURE: case LUA_T_LCLMARK:
  129. ar->source = tfvalue(protovalue(func))->source->str;
  130. ar->linedefined = tfvalue(protovalue(func))->lineDefined;
  131. ar->what = "Lua";
  132. break;
  133. default:
  134. ar->source = "(C)";
  135. ar->linedefined = -1;
  136. ar->what = "C";
  137. }
  138. if (ar->linedefined == 0)
  139. ar->what = "main";
  140. }
  141. static int checkfunc (lua_State *L, TObject *o) {
  142. return luaO_equalObj(o, L->top);
  143. }
  144. static void lua_getobjname (lua_State *L, StkId f, lua_Dbgactreg *ar) {
  145. GlobalVar *g;
  146. ar->namewhat = luaG_getname(L, &ar->name, f); /* caller debug information */
  147. if (*ar->namewhat) return;
  148. /* try to find a name for given function */
  149. setnormalized(L->top, f); /* to be used by `checkfunc' */
  150. for (g=L->rootglobal; g; g=g->next) {
  151. if (checkfunc(L, &g->value)) {
  152. ar->name = g->name->str;
  153. ar->namewhat = "global";
  154. return;
  155. }
  156. }
  157. /* not found: try tag methods */
  158. if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
  159. ar->namewhat = "tag-method";
  160. else ar->namewhat = ""; /* not found at all */
  161. }
  162. int lua_getinfo (lua_State *L, const char *what, lua_Dbgactreg *ar) {
  163. StkId func = ar->_func;
  164. LUA_ASSERT(L, is_T_MARK(ttype(func)), "invalid activation record");
  165. for ( ;*what; what++) {
  166. switch (*what) {
  167. case 'S':
  168. lua_funcinfo(ar);
  169. break;
  170. case 'l':
  171. ar->currentline = lua_currentline(L, func);
  172. break;
  173. case 'u':
  174. ar->nups = lua_nups(func);
  175. break;
  176. case 'n':
  177. lua_getobjname(L, func, ar);
  178. break;
  179. case 'f':
  180. setnormalized(L->top, func);
  181. incr_top;
  182. ar->func = luaA_putObjectOnTop(L);
  183. break;
  184. default: return 0; /* invalid option */
  185. }
  186. }
  187. return 1;
  188. }
  189. static void call_index_error (lua_State *L, TObject *o, const char *tp,
  190. const char *v) {
  191. const char *name;
  192. const char *kind = luaG_getname(L, &name, L->top);
  193. if (*kind) { /* is there a name? */
  194. luaL_verror(L, "%.10s `%.30s' is not a %.10s", kind, name, tp);
  195. }
  196. else {
  197. luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
  198. }
  199. }
  200. void luaG_callerror (lua_State *L, TObject *func) {
  201. call_index_error(L, func, "function", "call");
  202. }
  203. void luaG_indexerror (lua_State *L, TObject *t) {
  204. call_index_error(L, t, "table", "index");
  205. }