ldebug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. ** $Id: ldebug.c,v 1.7 2000/01/28 16:53:00 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 int lua_nups (StkId f) {
  67. switch (ttype(f)) {
  68. case LUA_T_LCLOSURE: case LUA_T_CCLOSURE:
  69. case LUA_T_LCLMARK: case LUA_T_CCLMARK:
  70. return f->value.cl->nelems;
  71. default:
  72. return 0;
  73. }
  74. }
  75. static int lua_currentline (lua_State *L, StkId f) {
  76. return hasdebuginfo(L, f) ? (f+1)->value.i : -1;
  77. }
  78. static TProtoFunc *getluaproto (StkId f) {
  79. if (ttype(f) == LUA_T_LMARK)
  80. return f->value.tf;
  81. else if (ttype(f) == LUA_T_LCLMARK)
  82. return protovalue(f)->value.tf;
  83. else return NULL;
  84. }
  85. int lua_getlocal (lua_State *L, const lua_Dbgactreg *ar, lua_Dbglocvar *v) {
  86. StkId f = ar->_func;
  87. TProtoFunc *fp = getluaproto(f);
  88. if (!fp) return 0; /* `f' is not a Lua function? */
  89. v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
  90. if (!v->name) return 0;
  91. /* if `name', there must be a LUA_T_LINE */
  92. /* therefore, f+2 points to function base */
  93. LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, "");
  94. v->value = luaA_putluaObject(L, (f+2)+(v->index-1));
  95. return 1;
  96. }
  97. int lua_setlocal (lua_State *L, const lua_Dbgactreg *ar, lua_Dbglocvar *v) {
  98. StkId f = ar->_func;
  99. TProtoFunc *fp = getluaproto(f);
  100. if (!fp) return 0; /* `f' is not a Lua function? */
  101. v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
  102. if (!v->name) return 0;
  103. LUA_ASSERT(L, ttype(f+1) == LUA_T_LINE, "");
  104. *((f+2)+(v->index-1)) = *v->value;
  105. return 1;
  106. }
  107. static void lua_funcinfo (lua_Dbgactreg *ar) {
  108. StkId func = ar->_func;
  109. switch (ttype(func)) {
  110. case LUA_T_LPROTO: case LUA_T_LMARK:
  111. ar->source = tfvalue(func)->source->str;
  112. ar->linedefined = tfvalue(func)->lineDefined;
  113. ar->what = "Lua";
  114. break;
  115. case LUA_T_LCLOSURE: case LUA_T_LCLMARK:
  116. ar->source = tfvalue(protovalue(func))->source->str;
  117. ar->linedefined = tfvalue(protovalue(func))->lineDefined;
  118. ar->what = "Lua";
  119. break;
  120. default:
  121. ar->source = "(C)";
  122. ar->linedefined = -1;
  123. ar->what = "C";
  124. }
  125. if (ar->linedefined == 0)
  126. ar->what = "main";
  127. }
  128. static int checkfunc (lua_State *L, TObject *o) {
  129. return luaO_equalObj(o, L->top);
  130. }
  131. static void lua_getobjname (lua_State *L, StkId f, lua_Dbgactreg *ar) {
  132. GlobalVar *g;
  133. /* try to find a name for given function */
  134. setnormalized(L->top, f); /* to be used by `checkfunc' */
  135. for (g=L->rootglobal; g; g=g->next) {
  136. if (checkfunc(L, &g->value)) {
  137. ar->name = g->name->str;
  138. ar->namewhat = "global";
  139. return;
  140. }
  141. }
  142. /* not found: try tag methods */
  143. if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
  144. ar->namewhat = "tag-method";
  145. else ar->namewhat = ""; /* not found at all */
  146. }
  147. int lua_getinfo (lua_State *L, const char *what, lua_Dbgactreg *ar) {
  148. StkId func = ar->_func;
  149. LUA_ASSERT(L, is_T_MARK(ttype(func)), "invalid activation record");
  150. for ( ;*what; what++) {
  151. switch (*what) {
  152. case 'S':
  153. lua_funcinfo(ar);
  154. break;
  155. case 'l':
  156. ar->currentline = lua_currentline(L, func);
  157. break;
  158. case 'u':
  159. ar->nups = lua_nups(func);
  160. break;
  161. case 'n':
  162. lua_getobjname(L, func, ar);
  163. break;
  164. case 'f':
  165. setnormalized(L->top, func);
  166. incr_top;
  167. ar->func = luaA_putObjectOnTop(L);
  168. break;
  169. default: return 0; /* invalid option */
  170. }
  171. }
  172. return 1;
  173. }
  174. static void call_index_error (lua_State *L, TObject *o, const char *v) {
  175. luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
  176. }
  177. void luaG_callerror (lua_State *L, TObject *func) {
  178. call_index_error(L, func, "call");
  179. }
  180. void luaG_indexerror (lua_State *L, TObject *t) {
  181. call_index_error(L, t, "index");
  182. }