ldebug.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. ** $Id: ldebug.c,v 1.19 2000/05/12 19:49:18 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 || v->name[0] == '*') return 0; /* `*' starts private locals */
  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, StkId func) {
  103. switch (ttype(func)) {
  104. case TAG_LCLOSURE: case TAG_LMARK:
  105. ar->source = clvalue(func)->f.l->source->str;
  106. ar->linedefined = clvalue(func)->f.l->lineDefined;
  107. ar->what = "Lua";
  108. break;
  109. case TAG_CCLOSURE: case TAG_CMARK:
  110. ar->source = "(C)";
  111. ar->linedefined = -1;
  112. ar->what = "C";
  113. break;
  114. default:
  115. LUA_INTERNALERROR(L, "invalid `func' value");
  116. }
  117. if (ar->linedefined == 0)
  118. ar->what = "main";
  119. }
  120. static int checkfunc (lua_State *L, TObject *o) {
  121. return luaO_equalObj(o, L->top);
  122. }
  123. static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) {
  124. Hash *g = L->gt;
  125. int i;
  126. /* try to find a name for given function */
  127. setnormalized(L->top, f); /* to be used by `checkfunc' */
  128. for (i=0; i<=g->size; i++) {
  129. if (ttype(key(node(g,i))) == TAG_STRING && checkfunc(L, val(node(g,i)))) {
  130. ar->name = tsvalue(key(node(g,i)))->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;
  142. if (*what != '>')
  143. func = ar->_func;
  144. else {
  145. what++; /* skip the '>' */
  146. func = ar->func;
  147. }
  148. for (; *what; what++) {
  149. switch (*what) {
  150. case 'S':
  151. lua_funcinfo(ar, func);
  152. break;
  153. case 'l':
  154. ar->currentline = lua_currentline(L, func);
  155. break;
  156. case 'u':
  157. ar->nups = lua_nups(func);
  158. break;
  159. case 'n':
  160. lua_getobjname(L, func, ar);
  161. break;
  162. case 'f':
  163. setnormalized(L->top, func);
  164. incr_top;
  165. ar->func = lua_pop(L);
  166. break;
  167. default: return 0; /* invalid option */
  168. }
  169. }
  170. return 1;
  171. }
  172. static void call_index_error (lua_State *L, TObject *o, const char *v) {
  173. luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
  174. }
  175. void luaG_callerror (lua_State *L, TObject *func) {
  176. call_index_error(L, func, "call");
  177. }
  178. void luaG_indexerror (lua_State *L, TObject *t) {
  179. call_index_error(L, t, "index");
  180. }