ldebug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. ** $Id: ldebug.c,v 1.48 2000/10/20 16:39:03 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,
  120. int localnum) {
  121. const char *name;
  122. StkId f = ar->_func;
  123. Proto *fp = getluaproto(f);
  124. if (!fp) return NULL; /* `f' is not a Lua function? */
  125. name = luaF_getlocalname(fp, localnum, currentpc(f));
  126. if (!name) return NULL;
  127. luaA_pushobject(L, (f+1)+(localnum-1)); /* push value */
  128. return name;
  129. }
  130. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar,
  131. 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, 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 funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  148. Closure *cl = NULL;
  149. switch (ttype(func)) {
  150. case LUA_TFUNCTION:
  151. cl = clvalue(func);
  152. break;
  153. case LUA_TMARK:
  154. cl = infovalue(func)->func;
  155. break;
  156. default:
  157. lua_error(L, "value for `lua_getinfo' is not a function");
  158. }
  159. if (cl->isC) {
  160. ar->source = "=C";
  161. ar->linedefined = -1;
  162. ar->what = "C";
  163. }
  164. else
  165. infoLproto(ar, cl->f.l);
  166. luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
  167. if (ar->linedefined == 0)
  168. ar->what = "main";
  169. }
  170. static const char *travtagmethods (lua_State *L, const TObject *o) {
  171. if (ttype(o) == LUA_TFUNCTION) {
  172. int e;
  173. for (e=0; e<TM_N; e++) {
  174. int t;
  175. for (t=0; t<=L->last_tag; t++)
  176. if (clvalue(o) == luaT_gettm(L, t, e))
  177. return luaT_eventname[e];
  178. }
  179. }
  180. return NULL;
  181. }
  182. static const char *travglobals (lua_State *L, const TObject *o) {
  183. Hash *g = L->gt;
  184. int i;
  185. for (i=0; i<g->size; i++) {
  186. if (luaO_equalObj(o, val(node(g, i))) &&
  187. ttype(key(node(g, i))) == LUA_TSTRING)
  188. return tsvalue(key(node(g, i)))->str;
  189. }
  190. return NULL;
  191. }
  192. static void getname (lua_State *L, StkId f, lua_Debug *ar) {
  193. TObject o;
  194. setnormalized(&o, f);
  195. /* try to find a name for given function */
  196. if ((ar->name = travglobals(L, &o)) != NULL)
  197. ar->namewhat = "global";
  198. /* not found: try tag methods */
  199. else if ((ar->name = travtagmethods(L, &o)) != NULL)
  200. ar->namewhat = "tag-method";
  201. else ar->namewhat = ""; /* not found at all */
  202. }
  203. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  204. StkId func;
  205. int isactive = (*what != '>');
  206. if (isactive)
  207. func = ar->_func;
  208. else {
  209. what++; /* skip the '>' */
  210. func = L->top - 1;
  211. }
  212. for (; *what; what++) {
  213. switch (*what) {
  214. case 'S': {
  215. funcinfo(L, ar, func);
  216. break;
  217. }
  218. case 'l': {
  219. ar->currentline = currentline(func);
  220. break;
  221. }
  222. case 'u': {
  223. ar->nups = nups(func);
  224. break;
  225. }
  226. case 'n': {
  227. ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
  228. if (ar->namewhat == NULL)
  229. getname(L, func, ar);
  230. break;
  231. }
  232. case 'f': {
  233. setnormalized(L->top, func);
  234. incr_top; /* push function */
  235. break;
  236. }
  237. default: return 0; /* invalid option */
  238. }
  239. }
  240. if (!isactive) L->top--; /* pop function */
  241. return 1;
  242. }
  243. /*
  244. ** {======================================================
  245. ** Symbolic Execution
  246. ** =======================================================
  247. */
  248. static int pushpc (int *stack, int pc, int top, int n) {
  249. while (n--)
  250. stack[top++] = pc-1;
  251. return top;
  252. }
  253. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
  254. int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
  255. const Instruction *code = pt->code;
  256. int top = pt->numparams;
  257. int pc = 0;
  258. if (pt->is_vararg) /* varargs? */
  259. top++; /* `arg' */
  260. while (pc < lastpc) {
  261. const Instruction i = code[pc++];
  262. LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack");
  263. switch (GET_OPCODE(i)) {
  264. case OP_RETURN: {
  265. LUA_ASSERT(top >= GETARG_U(i), "wrong stack");
  266. top = GETARG_U(i);
  267. break;
  268. }
  269. case OP_TAILCALL: {
  270. LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
  271. top = GETARG_B(i);
  272. break;
  273. }
  274. case OP_CALL: {
  275. int nresults = GETARG_B(i);
  276. if (nresults == MULT_RET) nresults = 1;
  277. LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
  278. top = pushpc(stack, pc, GETARG_A(i), nresults);
  279. break;
  280. }
  281. case OP_PUSHNIL: {
  282. top = pushpc(stack, pc, top, GETARG_U(i));
  283. break;
  284. }
  285. case OP_POP: {
  286. top -= GETARG_U(i);
  287. break;
  288. }
  289. case OP_SETTABLE:
  290. case OP_SETLIST: {
  291. top -= GETARG_B(i);
  292. break;
  293. }
  294. case OP_SETMAP: {
  295. top -= 2*GETARG_U(i);
  296. break;
  297. }
  298. case OP_CONCAT: {
  299. top -= GETARG_U(i);
  300. stack[top++] = pc-1;
  301. break;
  302. }
  303. case OP_CLOSURE: {
  304. top -= GETARG_B(i);
  305. stack[top++] = pc-1;
  306. break;
  307. }
  308. case OP_JMPONT:
  309. case OP_JMPONF: {
  310. int newpc = pc + GETARG_S(i);
  311. /* jump is forward and do not skip `lastpc'? */
  312. if (pc < newpc && newpc <= lastpc) {
  313. stack[top-1] = pc-1; /* value comes from `and'/`or' */
  314. pc = newpc; /* do the jump */
  315. }
  316. else
  317. top--; /* do not jump; pop value */
  318. break;
  319. }
  320. default: {
  321. OpCode op = GET_OPCODE(i);
  322. LUA_ASSERT(luaK_opproperties[op].push != VD,
  323. "invalid opcode for default");
  324. top -= luaK_opproperties[op].pop;
  325. LUA_ASSERT(top >= 0, "wrong stack");
  326. top = pushpc(stack, pc, top, luaK_opproperties[op].push);
  327. }
  328. }
  329. }
  330. return code[stack[stackpos]];
  331. }
  332. static const char *getobjname (lua_State *L, StkId obj, const char **name) {
  333. StkId func = aux_stackedfunction(L, 0, obj);
  334. if (!isLmark(func))
  335. return NULL; /* not an active Lua function */
  336. else {
  337. Proto *p = infovalue(func)->func->f.l;
  338. int pc = currentpc(func);
  339. int stackpos = obj - (func+1); /* func+1 == function base */
  340. Instruction i = luaG_symbexec(p, pc, stackpos);
  341. LUA_ASSERT(pc != -1, "function must be active");
  342. switch (GET_OPCODE(i)) {
  343. case OP_GETGLOBAL: {
  344. *name = p->kstr[GETARG_U(i)]->str;
  345. return "global";
  346. }
  347. case OP_GETLOCAL: {
  348. *name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
  349. LUA_ASSERT(*name, "local must exist");
  350. return "local";
  351. }
  352. case OP_PUSHSELF:
  353. case OP_GETDOTTED: {
  354. *name = p->kstr[GETARG_U(i)]->str;
  355. return "field";
  356. }
  357. default:
  358. return NULL; /* no useful name found */
  359. }
  360. }
  361. }
  362. static const char *getfuncname (lua_State *L, StkId f, const char **name) {
  363. StkId func = aux_stackedfunction(L, 0, f); /* calling function */
  364. if (!isLmark(func))
  365. return NULL; /* not an active Lua function */
  366. else {
  367. Proto *p = infovalue(func)->func->f.l;
  368. int pc = currentpc(func);
  369. Instruction i;
  370. if (pc == -1) return NULL; /* function is not activated */
  371. i = p->code[pc];
  372. switch (GET_OPCODE(i)) {
  373. case OP_CALL: case OP_TAILCALL:
  374. return getobjname(L, (func+1)+GETARG_A(i), name);
  375. default:
  376. return NULL; /* no useful name found */
  377. }
  378. }
  379. }
  380. /* }====================================================== */
  381. void luaG_typeerror (lua_State *L, StkId o, const char *op) {
  382. const char *name;
  383. const char *kind = getobjname(L, o, &name);
  384. const char *t = luaO_typename(o);
  385. if (kind)
  386. luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
  387. op, kind, name, t);
  388. else
  389. luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
  390. }
  391. void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
  392. if (ttype(p1) == t) p1++;
  393. LUA_ASSERT(ttype(p1) != t, "must be an error");
  394. luaG_typeerror(L, p1, op);
  395. }
  396. void luaG_ordererror (lua_State *L, StkId top) {
  397. const char *t1 = luaO_typename(top-2);
  398. const char *t2 = luaO_typename(top-1);
  399. if (t1[2] == t2[2])
  400. luaO_verror(L, "attempt to compare two %.10s values", t1);
  401. else
  402. luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
  403. }