ldebug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. ** $Id: ldebug.c,v 1.23 2000/06/12 13:52:05 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define LUA_REENTRANT
  8. #include "lua.h"
  9. #include "lapi.h"
  10. #include "lauxlib.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. #include "luadebug.h"
  19. static void setnormalized (TObject *d, const TObject *s) {
  20. switch (s->ttype) {
  21. case TAG_CMARK: {
  22. clvalue(d) = clvalue(s);
  23. ttype(d) = TAG_CCLOSURE;
  24. break;
  25. }
  26. case TAG_LMARK: {
  27. clvalue(d) = infovalue(s)->func;
  28. ttype(d) = TAG_LCLOSURE;
  29. break;
  30. }
  31. default: *d = *s;
  32. }
  33. }
  34. lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  35. lua_Hook oldhook = L->callhook;
  36. L->callhook = func;
  37. return oldhook;
  38. }
  39. lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  40. lua_Hook oldhook = L->linehook;
  41. L->linehook = func;
  42. return oldhook;
  43. }
  44. int lua_setdebug (lua_State *L, int debug) {
  45. int old = L->debug;
  46. L->debug = debug;
  47. return old;
  48. }
  49. static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
  50. int i;
  51. for (i = (top-1)-L->stack; i>=0; i--) {
  52. if (is_T_MARK(L->stack[i].ttype)) {
  53. if (level == 0)
  54. return L->stack+i;
  55. level--;
  56. }
  57. }
  58. return NULL;
  59. }
  60. int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  61. StkId f = aux_stackedfunction(L, level, L->top);
  62. if (f == NULL) return 0; /* there is no such level */
  63. else {
  64. ar->_func = f;
  65. return 1;
  66. }
  67. }
  68. static int lua_nups (StkId f) {
  69. switch (ttype(f)) {
  70. case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK:
  71. return clvalue(f)->nupvalues;
  72. case TAG_LMARK:
  73. return infovalue(f)->func->nupvalues;
  74. default:
  75. return 0;
  76. }
  77. }
  78. static int lua_currentline (StkId f) {
  79. if (ttype(f) != TAG_LMARK)
  80. return -1; /* only active lua functions have current-line information */
  81. else {
  82. CallInfo *ci = infovalue(f);
  83. int *lines = ci->func->f.l->lines;
  84. if (!lines) return -1; /* no static debug information */
  85. else return lines[ci->pc];
  86. }
  87. }
  88. static int lua_currentpc (StkId f) {
  89. return infovalue(f)->pc;
  90. }
  91. static Proto *getluaproto (StkId f) {
  92. return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
  93. }
  94. int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
  95. StkId f = ar->_func;
  96. Proto *fp = getluaproto(f);
  97. if (!fp) return 0; /* `f' is not a Lua function? */
  98. v->name = luaF_getlocalname(fp, v->index, lua_currentpc(f));
  99. if (!v->name) return 0;
  100. v->value = luaA_putluaObject(L, (f+1)+(v->index-1));
  101. return 1;
  102. }
  103. int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
  104. StkId f = ar->_func;
  105. Proto *fp = getluaproto(f);
  106. UNUSED(L);
  107. if (!fp) return 0; /* `f' is not a Lua function? */
  108. v->name = luaF_getlocalname(fp, v->index, lua_currentpc(f));
  109. if (!v->name || v->name[0] == '*') return 0; /* `*' starts private locals */
  110. *((f+1)+(v->index-1)) = *v->value;
  111. return 1;
  112. }
  113. static void lua_funcinfo (lua_Debug *ar, StkId func) {
  114. switch (ttype(func)) {
  115. case TAG_LCLOSURE:
  116. ar->source = clvalue(func)->f.l->source->str;
  117. ar->linedefined = clvalue(func)->f.l->lineDefined;
  118. ar->what = "Lua";
  119. break;
  120. case TAG_LMARK:
  121. ar->source = infovalue(func)->func->f.l->source->str;
  122. ar->linedefined = infovalue(func)->func->f.l->lineDefined;
  123. ar->what = "Lua";
  124. break;
  125. case TAG_CCLOSURE: case TAG_CMARK:
  126. ar->source = "(C)";
  127. ar->linedefined = -1;
  128. ar->what = "C";
  129. break;
  130. default:
  131. LUA_INTERNALERROR(L, "invalid `func' value");
  132. }
  133. if (ar->linedefined == 0)
  134. ar->what = "main";
  135. }
  136. static int checkfunc (lua_State *L, TObject *o) {
  137. return luaO_equalObj(o, L->top);
  138. }
  139. static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) {
  140. Hash *g = L->gt;
  141. int i;
  142. /* try to find a name for given function */
  143. setnormalized(L->top, f); /* to be used by `checkfunc' */
  144. for (i=0; i<=g->size; i++) {
  145. if (ttype(key(node(g,i))) == TAG_STRING && checkfunc(L, val(node(g,i)))) {
  146. ar->name = tsvalue(key(node(g,i)))->str;
  147. ar->namewhat = "global";
  148. return;
  149. }
  150. }
  151. /* not found: try tag methods */
  152. if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
  153. ar->namewhat = "tag-method";
  154. else ar->namewhat = ""; /* not found at all */
  155. }
  156. int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  157. StkId func;
  158. if (*what != '>')
  159. func = ar->_func;
  160. else {
  161. what++; /* skip the '>' */
  162. func = ar->func;
  163. }
  164. for (; *what; what++) {
  165. switch (*what) {
  166. case 'S':
  167. lua_funcinfo(ar, func);
  168. break;
  169. case 'l':
  170. ar->currentline = lua_currentline(func);
  171. break;
  172. case 'u':
  173. ar->nups = lua_nups(func);
  174. break;
  175. case 'n':
  176. lua_getobjname(L, func, ar);
  177. break;
  178. case 'f':
  179. setnormalized(L->top, func);
  180. incr_top;
  181. ar->func = lua_pop(L);
  182. break;
  183. default: return 0; /* invalid option */
  184. }
  185. }
  186. return 1;
  187. }
  188. static void call_index_error (lua_State *L, TObject *o, const char *v) {
  189. luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
  190. }
  191. void luaG_callerror (lua_State *L, TObject *func) {
  192. call_index_error(L, func, "call");
  193. }
  194. void luaG_indexerror (lua_State *L, TObject *t) {
  195. call_index_error(L, t, "index");
  196. }