ldebug.c 11 KB

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