ldebug.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. ** $Id: ldebug.c,v 1.15 2000/03/30 17:19:48 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. TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_TABLE,
  21. TAG_LCLOSURE, TAG_CCLOSURE, TAG_NIL,
  22. TAG_LCLOSURE, TAG_CCLOSURE /* TAG_LMARK, TAG_CMARK */
  23. };
  24. static void setnormalized (TObject *d, const TObject *s) {
  25. d->value = s->value;
  26. d->ttype = normtype[ttype(s)];
  27. }
  28. static int hasdebuginfo (lua_State *L, StkId f) {
  29. return (f+1 < L->top && (f+1)->ttype == TAG_LINE);
  30. }
  31. lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  32. lua_Hook oldhook = L->callhook;
  33. L->callhook = func;
  34. return oldhook;
  35. }
  36. lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  37. lua_Hook oldhook = L->linehook;
  38. L->linehook = func;
  39. return oldhook;
  40. }
  41. int lua_setdebug (lua_State *L, int debug) {
  42. int old = L->debug;
  43. L->debug = debug;
  44. return old;
  45. }
  46. static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
  47. int i;
  48. for (i = (top-1)-L->stack; i>=0; i--) {
  49. if (is_T_MARK(L->stack[i].ttype)) {
  50. if (level == 0)
  51. return L->stack+i;
  52. level--;
  53. }
  54. }
  55. return NULL;
  56. }
  57. int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  58. StkId f = aux_stackedfunction(L, level, L->top);
  59. if (f == NULL) return 0; /* there is no such level */
  60. else {
  61. ar->_func = f;
  62. return 1;
  63. }
  64. }
  65. static int lua_nups (StkId f) {
  66. switch (ttype(f)) {
  67. case TAG_LCLOSURE: case TAG_CCLOSURE:
  68. case TAG_LMARK: case TAG_CMARK:
  69. return f->value.cl->nelems;
  70. default:
  71. return 0;
  72. }
  73. }
  74. static int lua_currentline (lua_State *L, StkId f) {
  75. return hasdebuginfo(L, f) ? (f+1)->value.i : -1;
  76. }
  77. static Proto *getluaproto (StkId f) {
  78. return (ttype(f) == TAG_LMARK) ? clvalue(f)->f.l : NULL;
  79. }
  80. int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
  81. StkId f = ar->_func;
  82. Proto *fp = getluaproto(f);
  83. if (!fp) return 0; /* `f' is not a Lua function? */
  84. v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
  85. if (!v->name) return 0;
  86. /* if `name', there must be a TAG_LINE */
  87. /* therefore, f+2 points to function base */
  88. LUA_ASSERT(L, ttype(f+1) == TAG_LINE, "");
  89. v->value = luaA_putluaObject(L, (f+2)+(v->index-1));
  90. return 1;
  91. }
  92. int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
  93. StkId f = ar->_func;
  94. Proto *fp = getluaproto(f);
  95. if (!fp) return 0; /* `f' is not a Lua function? */
  96. v->name = luaF_getlocalname(fp, v->index, lua_currentline(L, f));
  97. if (!v->name) return 0;
  98. LUA_ASSERT(L, ttype(f+1) == TAG_LINE, "");
  99. *((f+2)+(v->index-1)) = *v->value;
  100. return 1;
  101. }
  102. static void lua_funcinfo (lua_Debug *ar) {
  103. StkId func = ar->_func;
  104. switch (ttype(func)) {
  105. case TAG_LCLOSURE: case TAG_LMARK:
  106. ar->source = clvalue(func)->f.l->source->str;
  107. ar->linedefined = clvalue(func)->f.l->lineDefined;
  108. ar->what = "Lua";
  109. break;
  110. case TAG_CCLOSURE: case TAG_CMARK:
  111. ar->source = "(C)";
  112. ar->linedefined = -1;
  113. ar->what = "C";
  114. break;
  115. default:
  116. LUA_INTERNALERROR(L, "invalid `func' value");
  117. }
  118. if (ar->linedefined == 0)
  119. ar->what = "main";
  120. }
  121. static int checkfunc (lua_State *L, TObject *o) {
  122. return luaO_equalObj(o, L->top);
  123. }
  124. static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) {
  125. GlobalVar *g;
  126. /* try to find a name for given function */
  127. setnormalized(L->top, f); /* to be used by `checkfunc' */
  128. for (g=L->rootglobal; g; g=g->next) {
  129. if (checkfunc(L, &g->value)) {
  130. ar->name = g->name->str;
  131. ar->namewhat = "global";
  132. return;
  133. }
  134. }
  135. /* not found: try tag methods */
  136. if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
  137. ar->namewhat = "tag-method";
  138. else ar->namewhat = ""; /* not found at all */
  139. }
  140. int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  141. StkId func = ar->_func;
  142. LUA_ASSERT(L, is_T_MARK(ttype(func)), "invalid activation record");
  143. for (; *what; what++) {
  144. switch (*what) {
  145. case 'S':
  146. lua_funcinfo(ar);
  147. break;
  148. case 'l':
  149. ar->currentline = lua_currentline(L, func);
  150. break;
  151. case 'u':
  152. ar->nups = lua_nups(func);
  153. break;
  154. case 'n':
  155. lua_getobjname(L, func, ar);
  156. break;
  157. case 'f':
  158. setnormalized(L->top, func);
  159. incr_top;
  160. ar->func = luaA_putObjectOnTop(L);
  161. break;
  162. default: return 0; /* invalid option */
  163. }
  164. }
  165. return 1;
  166. }
  167. static void call_index_error (lua_State *L, TObject *o, const char *v) {
  168. luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
  169. }
  170. void luaG_callerror (lua_State *L, TObject *func) {
  171. call_index_error(L, func, "call");
  172. }
  173. void luaG_indexerror (lua_State *L, TObject *t) {
  174. call_index_error(L, t, "index");
  175. }