ldebug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. ** $Id: ldebug.c,v 1.41 2000/09/12 18:38:02 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 "lcode.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lobject.h"
  14. #include "lopcodes.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "ltable.h"
  18. #include "ltm.h"
  19. #include "luadebug.h"
  20. static const char *getfuncname (lua_State *L, StkId f, const char **name);
  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;
  77. if (lineinfo == NULL) return -1; /* no line info */
  78. else if (pc == -1) return refline; /* function preamble */
  79. refi = prefi ? *prefi : 0;
  80. if (lineinfo[refi] < 0)
  81. refline += -lineinfo[refi++];
  82. LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
  83. while (lineinfo[refi] > pc) {
  84. refline--;
  85. refi--;
  86. if (lineinfo[refi] < 0)
  87. refline -= -lineinfo[refi--];
  88. LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
  89. }
  90. for (;;) {
  91. int nextline = refline + 1;
  92. int nextref = refi + 1;
  93. if (lineinfo[nextref] < 0)
  94. nextline += -lineinfo[nextref++];
  95. LUA_ASSERT(lineinfo[nextref] >= 0, "invalid line info");
  96. if (lineinfo[nextref] > pc)
  97. break;
  98. refline = nextline;
  99. refi = nextref;
  100. }
  101. if (prefi) *prefi = refi;
  102. return refline;
  103. }
  104. static int lua_currentpc (StkId f) {
  105. CallInfo *ci = infovalue(f);
  106. LUA_ASSERT(ttype(f) == TAG_LMARK, "function has no pc");
  107. return (*ci->pc - ci->func->f.l->code) - 1;
  108. }
  109. static int lua_currentline (StkId f) {
  110. if (ttype(f) != TAG_LMARK)
  111. return -1; /* only active lua functions have current-line information */
  112. else {
  113. CallInfo *ci = infovalue(f);
  114. int *lineinfo = ci->func->f.l->lineinfo;
  115. return luaG_getline(lineinfo, lua_currentpc(f), 1, NULL);
  116. }
  117. }
  118. static Proto *getluaproto (StkId f) {
  119. return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
  120. }
  121. const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum) {
  122. const char *name;
  123. StkId f = ar->_func;
  124. Proto *fp = getluaproto(f);
  125. if (!fp) return NULL; /* `f' is not a Lua function? */
  126. name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
  127. if (!name) return NULL;
  128. luaA_pushobject(L, (f+1)+(localnum-1)); /* push value */
  129. return name;
  130. }
  131. const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum) {
  132. const char *name;
  133. StkId f = ar->_func;
  134. Proto *fp = getluaproto(f);
  135. L->top--; /* pop new value */
  136. if (!fp) return NULL; /* `f' is not a Lua function? */
  137. name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
  138. if (!name || name[0] == '*') return NULL; /* `*' starts private locals */
  139. *((f+1)+(localnum-1)) = *L->top;
  140. return name;
  141. }
  142. static void infoLproto (lua_Debug *ar, Proto *f) {
  143. ar->source = f->source->str;
  144. ar->linedefined = f->lineDefined;
  145. ar->what = "Lua";
  146. }
  147. static void lua_funcinfo (lua_Debug *ar, StkId func) {
  148. switch (ttype(func)) {
  149. case TAG_LCLOSURE:
  150. infoLproto(ar, clvalue(func)->f.l);
  151. break;
  152. case TAG_LMARK:
  153. infoLproto(ar, infovalue(func)->func->f.l);
  154. break;
  155. case TAG_CCLOSURE: case TAG_CMARK:
  156. ar->source = "(C)";
  157. ar->linedefined = -1;
  158. ar->what = "C";
  159. break;
  160. default:
  161. LUA_INTERNALERROR("invalid `func' value");
  162. }
  163. luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
  164. if (ar->linedefined == 0)
  165. ar->what = "main";
  166. }
  167. static const char *travtagmethods (lua_State *L, const TObject *o) {
  168. int e;
  169. for (e=0; e<IM_N; e++) {
  170. int t;
  171. for (t=0; t<=L->last_tag; t++)
  172. if (luaO_equalObj(o, luaT_getim(L, t,e)))
  173. return luaT_eventname[e];
  174. }
  175. return NULL;
  176. }
  177. static const char *travglobals (lua_State *L, const TObject *o) {
  178. Hash *g = L->gt;
  179. int i;
  180. for (i=0; i<g->size; i++) {
  181. if (luaO_equalObj(o, val(node(g, i))) &&
  182. ttype(key(node(g, i))) == TAG_STRING)
  183. return tsvalue(key(node(g, i)))->str;
  184. }
  185. return NULL;
  186. }
  187. static void lua_getname (lua_State *L, StkId f, lua_Debug *ar) {
  188. TObject o;
  189. setnormalized(&o, f);
  190. /* try to find a name for given function */
  191. if ((ar->name = travglobals(L, &o)) != NULL)
  192. ar->namewhat = "global";
  193. /* not found: try tag methods */
  194. else if ((ar->name = travtagmethods(L, &o)) != NULL)
  195. ar->namewhat = "tag-method";
  196. else ar->namewhat = ""; /* not found at all */
  197. }
  198. int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  199. StkId func;
  200. int isactive = (*what != '>');
  201. if (isactive)
  202. func = ar->_func;
  203. else {
  204. what++; /* skip the '>' */
  205. func = L->top - 1;
  206. }
  207. for (; *what; what++) {
  208. switch (*what) {
  209. case 'S': {
  210. lua_funcinfo(ar, func);
  211. break;
  212. }
  213. case 'l': {
  214. ar->currentline = lua_currentline(func);
  215. break;
  216. }
  217. case 'u': {
  218. ar->nups = lua_nups(func);
  219. break;
  220. }
  221. case 'n': {
  222. ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
  223. if (ar->namewhat == NULL)
  224. lua_getname(L, func, ar);
  225. break;
  226. }
  227. case 'f': {
  228. setnormalized(L->top, func);
  229. incr_top; /* push function */
  230. break;
  231. }
  232. default: return 0; /* invalid option */
  233. }
  234. }
  235. if (!isactive) L->top--; /* pop function */
  236. return 1;
  237. }
  238. /*
  239. ** {======================================================
  240. ** Symbolic Execution
  241. ** =======================================================
  242. */
  243. static int pushpc (int *stack, int pc, int top, int n) {
  244. while (n--)
  245. stack[top++] = pc-1;
  246. return top;
  247. }
  248. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
  249. int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
  250. const Instruction *code = pt->code;
  251. int top = pt->numparams;
  252. int pc = 0;
  253. if (pt->is_vararg) /* varargs? */
  254. top++; /* `arg' */
  255. while (pc < lastpc) {
  256. const Instruction i = code[pc++];
  257. LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack");
  258. switch (GET_OPCODE(i)) {
  259. case OP_RETURN: {
  260. LUA_ASSERT(top >= GETARG_U(i), "wrong stack");
  261. top = GETARG_U(i);
  262. break;
  263. }
  264. case OP_TAILCALL: {
  265. LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
  266. top = GETARG_B(i);
  267. break;
  268. }
  269. case OP_CALL: {
  270. int nresults = GETARG_B(i);
  271. if (nresults == MULT_RET) nresults = 1;
  272. LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
  273. top = pushpc(stack, pc, GETARG_A(i), nresults);
  274. break;
  275. }
  276. case OP_PUSHNIL: {
  277. top = pushpc(stack, pc, top, GETARG_U(i));
  278. break;
  279. }
  280. case OP_POP: {
  281. top -= GETARG_U(i);
  282. break;
  283. }
  284. case OP_SETTABLE:
  285. case OP_SETLIST: {
  286. top -= GETARG_B(i);
  287. break;
  288. }
  289. case OP_SETMAP: {
  290. top -= 2*GETARG_U(i);
  291. break;
  292. }
  293. case OP_CONCAT: {
  294. top -= GETARG_U(i);
  295. stack[top++] = pc-1;
  296. break;
  297. }
  298. case OP_CLOSURE: {
  299. top -= GETARG_B(i);
  300. stack[top++] = pc-1;
  301. break;
  302. }
  303. case OP_JMPONT:
  304. case OP_JMPONF: {
  305. int newpc = pc + GETARG_S(i);
  306. /* jump is forward and do not skip `lastpc'? */
  307. if (pc < newpc && newpc <= lastpc) {
  308. stack[top-1] = pc-1; /* value comes from `and'/`or' */
  309. pc = newpc; /* do the jump */
  310. }
  311. else
  312. top--; /* do not jump; pop value */
  313. break;
  314. }
  315. default: {
  316. OpCode op = GET_OPCODE(i);
  317. LUA_ASSERT(luaK_opproperties[op].push != VD,
  318. "invalid opcode for default");
  319. top -= luaK_opproperties[op].pop;
  320. LUA_ASSERT(top >= 0, "wrong stack");
  321. top = pushpc(stack, pc, top, luaK_opproperties[op].push);
  322. }
  323. }
  324. }
  325. return code[stack[stackpos]];
  326. }
  327. static const char *getobjname (lua_State *L, StkId obj, const char **name) {
  328. StkId func = aux_stackedfunction(L, 0, obj);
  329. if (func == NULL || ttype(func) != TAG_LMARK)
  330. return NULL; /* not a Lua function */
  331. else {
  332. Proto *p = infovalue(func)->func->f.l;
  333. int pc = lua_currentpc(func);
  334. int stackpos = obj - (func+1); /* func+1 == function base */
  335. Instruction i = luaG_symbexec(p, pc, stackpos);
  336. LUA_ASSERT(pc != -1, "function must be active");
  337. switch (GET_OPCODE(i)) {
  338. case OP_GETGLOBAL: {
  339. *name = p->kstr[GETARG_U(i)]->str;
  340. return "global";
  341. }
  342. case OP_GETLOCAL: {
  343. *name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
  344. LUA_ASSERT(*name, "local must exist");
  345. return "local";
  346. }
  347. case OP_PUSHSELF:
  348. case OP_GETDOTTED: {
  349. *name = p->kstr[GETARG_U(i)]->str;
  350. return "field";
  351. }
  352. default:
  353. return NULL; /* no useful name found */
  354. }
  355. }
  356. }
  357. static const char *getfuncname (lua_State *L, StkId f, const char **name) {
  358. StkId func = aux_stackedfunction(L, 0, f); /* calling function */
  359. if (func == NULL || ttype(func) != TAG_LMARK)
  360. return NULL; /* not a Lua function */
  361. else {
  362. Proto *p = infovalue(func)->func->f.l;
  363. int pc = lua_currentpc(func);
  364. Instruction i;
  365. if (pc == -1) return NULL; /* function is not activated */
  366. i = p->code[pc];
  367. switch (GET_OPCODE(i)) {
  368. case OP_CALL: case OP_TAILCALL:
  369. return getobjname(L, (func+1)+GETARG_A(i), name);
  370. default:
  371. return NULL; /* no useful name found */
  372. }
  373. }
  374. }
  375. /* }====================================================== */
  376. void luaG_typeerror (lua_State *L, StkId o, const char *op) {
  377. const char *name;
  378. const char *kind = getobjname(L, o, &name);
  379. const char *t = luaO_typename(o);
  380. if (kind)
  381. luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
  382. op, kind, name, t);
  383. else
  384. luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
  385. }
  386. void luaG_binerror (lua_State *L, StkId p1, lua_Type t, const char *op) {
  387. if (ttype(p1) == t) p1++;
  388. LUA_ASSERT(ttype(p1) != t, "must be an error");
  389. luaG_typeerror(L, p1, op);
  390. }
  391. void luaG_ordererror (lua_State *L, StkId top) {
  392. const char *t1 = luaO_typename(top-2);
  393. const char *t2 = luaO_typename(top-1);
  394. if (t1[2] == t2[2])
  395. luaO_verror(L, "attempt to compare two %.10s values", t1);
  396. else
  397. luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
  398. }