ldebug.c 5.4 KB

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