ldebug.c 9.9 KB

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