ldebug.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. ** $Id: ldebug.c,v 1.25 2000/06/28 20:20: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 "lua.h"
  9. #include "lapi.h"
  10. #include "lauxlib.h"
  11. #include "lcode.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lobject.h"
  16. #include "lopcodes.h"
  17. #include "lstate.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "luadebug.h"
  21. static void setnormalized (TObject *d, const TObject *s) {
  22. switch (s->ttype) {
  23. case TAG_CMARK: {
  24. clvalue(d) = clvalue(s);
  25. ttype(d) = TAG_CCLOSURE;
  26. break;
  27. }
  28. case TAG_LMARK: {
  29. clvalue(d) = infovalue(s)->func;
  30. ttype(d) = TAG_LCLOSURE;
  31. break;
  32. }
  33. default: *d = *s;
  34. }
  35. }
  36. lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  37. lua_Hook oldhook = L->callhook;
  38. L->callhook = func;
  39. return oldhook;
  40. }
  41. lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  42. lua_Hook oldhook = L->linehook;
  43. L->linehook = func;
  44. return oldhook;
  45. }
  46. int lua_setdebug (lua_State *L, int debug) {
  47. int old = L->debug;
  48. L->debug = debug;
  49. return old;
  50. }
  51. static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
  52. int i;
  53. for (i = (top-1)-L->stack; i>=0; i--) {
  54. if (is_T_MARK(L->stack[i].ttype)) {
  55. if (level == 0)
  56. return L->stack+i;
  57. level--;
  58. }
  59. }
  60. return NULL;
  61. }
  62. int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  63. StkId f = aux_stackedfunction(L, level, L->top);
  64. if (f == NULL) return 0; /* there is no such level */
  65. else {
  66. ar->_func = f;
  67. return 1;
  68. }
  69. }
  70. static int lua_nups (StkId f) {
  71. switch (ttype(f)) {
  72. case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK:
  73. return clvalue(f)->nupvalues;
  74. case TAG_LMARK:
  75. return infovalue(f)->func->nupvalues;
  76. default:
  77. return 0;
  78. }
  79. }
  80. static int lua_currentpc (StkId f) {
  81. CallInfo *ci = infovalue(f);
  82. LUA_ASSERT(L, ttype(f) == TAG_LMARK, "function has no pc");
  83. return (*ci->pc - 1) - ci->func->f.l->code;
  84. }
  85. static int lua_currentline (StkId f) {
  86. if (ttype(f) != TAG_LMARK)
  87. return -1; /* only active lua functions have current-line information */
  88. else {
  89. CallInfo *ci = infovalue(f);
  90. int *lines = ci->func->f.l->lines;
  91. if (!lines) return -1; /* no static debug information */
  92. else return lines[lua_currentpc(f)];
  93. }
  94. }
  95. static Proto *getluaproto (StkId f) {
  96. return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
  97. }
  98. int lua_getlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
  99. StkId f = ar->_func;
  100. Proto *fp = getluaproto(f);
  101. if (!fp) return 0; /* `f' is not a Lua function? */
  102. v->name = luaF_getlocalname(fp, v->index, lua_currentpc(f));
  103. if (!v->name) return 0;
  104. v->value = luaA_putluaObject(L, (f+1)+(v->index-1));
  105. return 1;
  106. }
  107. int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) {
  108. StkId f = ar->_func;
  109. Proto *fp = getluaproto(f);
  110. UNUSED(L);
  111. if (!fp) return 0; /* `f' is not a Lua function? */
  112. v->name = luaF_getlocalname(fp, v->index, lua_currentpc(f));
  113. if (!v->name || v->name[0] == '*') return 0; /* `*' starts private locals */
  114. *((f+1)+(v->index-1)) = *v->value;
  115. return 1;
  116. }
  117. static void lua_funcinfo (lua_Debug *ar, StkId func) {
  118. switch (ttype(func)) {
  119. case TAG_LCLOSURE:
  120. ar->source = clvalue(func)->f.l->source->str;
  121. ar->linedefined = clvalue(func)->f.l->lineDefined;
  122. ar->what = "Lua";
  123. break;
  124. case TAG_LMARK:
  125. ar->source = infovalue(func)->func->f.l->source->str;
  126. ar->linedefined = infovalue(func)->func->f.l->lineDefined;
  127. ar->what = "Lua";
  128. break;
  129. case TAG_CCLOSURE: case TAG_CMARK:
  130. ar->source = "(C)";
  131. ar->linedefined = -1;
  132. ar->what = "C";
  133. break;
  134. default:
  135. LUA_INTERNALERROR(L, "invalid `func' value");
  136. }
  137. if (ar->linedefined == 0)
  138. ar->what = "main";
  139. }
  140. static int checkfunc (lua_State *L, TObject *o) {
  141. return luaO_equalObj(o, L->top);
  142. }
  143. static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) {
  144. Hash *g = L->gt;
  145. int i;
  146. /* try to find a name for given function */
  147. setnormalized(L->top, f); /* to be used by `checkfunc' */
  148. for (i=0; i<=g->size; i++) {
  149. if (ttype(key(node(g,i))) == TAG_STRING && checkfunc(L, val(node(g,i)))) {
  150. ar->name = tsvalue(key(node(g,i)))->str;
  151. ar->namewhat = "global";
  152. return;
  153. }
  154. }
  155. /* not found: try tag methods */
  156. if ((ar->name = luaT_travtagmethods(L, checkfunc)) != NULL)
  157. ar->namewhat = "tag-method";
  158. else ar->namewhat = ""; /* not found at all */
  159. }
  160. int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  161. StkId func;
  162. if (*what != '>')
  163. func = ar->_func;
  164. else {
  165. what++; /* skip the '>' */
  166. func = ar->func;
  167. }
  168. for (; *what; what++) {
  169. switch (*what) {
  170. case 'S':
  171. lua_funcinfo(ar, func);
  172. break;
  173. case 'l':
  174. ar->currentline = lua_currentline(func);
  175. break;
  176. case 'u':
  177. ar->nups = lua_nups(func);
  178. break;
  179. case 'n':
  180. lua_getobjname(L, func, ar);
  181. break;
  182. case 'f':
  183. setnormalized(L->top, func);
  184. incr_top;
  185. ar->func = lua_pop(L);
  186. break;
  187. default: return 0; /* invalid option */
  188. }
  189. }
  190. return 1;
  191. }
  192. /*
  193. ** {======================================================
  194. ** Symbolic Execution
  195. ** =======================================================
  196. */
  197. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
  198. int stack[MAXSTACK]; /* stores last instruction that changes each value */
  199. const Instruction *code = pt->code;
  200. int top = pt->numparams;
  201. int pc = 0;
  202. if (pt->is_vararg) /* varargs? */
  203. top++; /* `arg' */
  204. while (pc < lastpc) {
  205. const Instruction i = code[pc++];
  206. LUA_ASSERT(NULL, top <= pt->maxstacksize, "wrong stack");
  207. switch (GET_OPCODE(i)) {
  208. case OP_RETURN: {
  209. LUA_ASSERT(NULL, top >= GETARG_U(i), "wrong stack");
  210. top = GETARG_U(i);
  211. break;
  212. }
  213. case OP_CALL: {
  214. int nresults = GETARG_B(i);
  215. if (nresults == MULT_RET) nresults = 1;
  216. LUA_ASSERT(NULL, top >= GETARG_A(i), "wrong stack");
  217. top = GETARG_A(i);
  218. while (nresults--)
  219. stack[top++] = pc-1;
  220. break;
  221. }
  222. case OP_TAILCALL: {
  223. LUA_ASSERT(NULL, top >= GETARG_A(i), "wrong stack");
  224. top = GETARG_B(i);
  225. break;
  226. }
  227. case OP_PUSHNIL: {
  228. int n;
  229. for (n=0; n<GETARG_U(i); n++)
  230. stack[top++] = pc-1;
  231. break;
  232. }
  233. case OP_POP: {
  234. top -= GETARG_U(i);
  235. break;
  236. }
  237. case OP_SETTABLE:
  238. case OP_SETLIST: {
  239. top -= GETARG_B(i);
  240. break;
  241. }
  242. case OP_SETMAP: {
  243. top -= 2*GETARG_U(i);
  244. break;
  245. }
  246. case OP_CONCAT: {
  247. top -= GETARG_U(i);
  248. stack[top++] = pc-1;
  249. break;
  250. }
  251. case OP_JMPONT:
  252. case OP_JMPONF: {
  253. int newpc = pc + GETARG_S(i);
  254. if (lastpc < newpc)
  255. top--; /* original code did not jump; condition was false */
  256. else {
  257. stack[top-1] = pc-1; /* value generated by or-and */
  258. pc = newpc; /* do the jump */
  259. }
  260. break;
  261. }
  262. case OP_PUSHNILJMP: {
  263. break; /* do not `push', to compensate next instruction */
  264. }
  265. case OP_CLOSURE: {
  266. top -= GETARG_B(i);
  267. stack[top++] = pc-1;
  268. break;
  269. }
  270. default: {
  271. int n;
  272. LUA_ASSERT(NULL, luaK_opproperties[GET_OPCODE(i)].push != VD,
  273. "invalid opcode for default");
  274. top -= luaK_opproperties[GET_OPCODE(i)].pop;
  275. LUA_ASSERT(NULL, top >= 0, "wrong stack");
  276. for (n=0; n<luaK_opproperties[GET_OPCODE(i)].push; n++)
  277. stack[top++] = pc-1;
  278. }
  279. }
  280. }
  281. return code[stack[stackpos]];
  282. }
  283. static const char *getname (lua_State *L, StkId obj, const char **name) {
  284. StkId func = aux_stackedfunction(L, 0, obj);
  285. if (func == NULL || ttype(func) != TAG_LMARK)
  286. return NULL; /* not a Lua function */
  287. else {
  288. Proto *p = infovalue(func)->func->f.l;
  289. int pc = lua_currentpc(func);
  290. int stackpos = obj - (func+1); /* func+1 == function base */
  291. Instruction i = luaG_symbexec(p, pc, stackpos);
  292. switch (GET_OPCODE(i)) {
  293. case OP_GETGLOBAL: {
  294. *name = p->kstr[GETARG_U(i)]->str;
  295. return "global";
  296. }
  297. case OP_GETLOCAL: {
  298. *name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
  299. return (*name) ? "local" : NULL;
  300. }
  301. case OP_PUSHSELF:
  302. case OP_GETDOTTED: {
  303. *name = p->kstr[GETARG_U(i)]->str;
  304. return "field";
  305. }
  306. default:
  307. return NULL; /* no usefull name found */
  308. }
  309. }
  310. }
  311. /* }====================================================== */
  312. static void call_index_error (lua_State *L, StkId o, const char *op,
  313. const char *tp) {
  314. const char *name;
  315. const char *kind = getname(L, o, &name);
  316. if (kind)
  317. luaL_verror(L, "%s `%s' is not a %s", kind, name, tp);
  318. else
  319. luaL_verror(L, "attempt to %.10s a %.10s value", op, lua_type(L, o));
  320. }
  321. void luaG_callerror (lua_State *L, StkId func) {
  322. call_index_error(L, func, "call", "function");
  323. }
  324. void luaG_indexerror (lua_State *L, StkId t) {
  325. call_index_error(L, t, "index", "table");
  326. }