ldebug.c 10.0 KB

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