ldebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. ** $Id: ldebug.c,v 1.60 2001/02/07 18:13:49 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. setclvalue(d, infovalue(s)->func);
  24. }
  25. else setobj(d, s);
  26. }
  27. static int isLmark (StkId o) {
  28. return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
  29. }
  30. LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  31. lua_Hook oldhook;
  32. LUA_LOCK(L);
  33. oldhook = L->callhook;
  34. L->callhook = func;
  35. LUA_UNLOCK(L);
  36. return oldhook;
  37. }
  38. LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  39. lua_Hook oldhook;
  40. LUA_LOCK(L);
  41. oldhook = L->linehook;
  42. L->linehook = func;
  43. LUA_UNLOCK(L);
  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])) {
  50. if (level == 0)
  51. return L->stack+i;
  52. level--;
  53. }
  54. }
  55. return NULL;
  56. }
  57. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  58. StkId f;
  59. int status;
  60. LUA_LOCK(L);
  61. f = aux_stackedfunction(L, level, L->top);
  62. if (f == NULL) status = 0; /* there is no such level */
  63. else {
  64. ar->_func = f;
  65. status = 1;
  66. }
  67. LUA_UNLOCK(L);
  68. return status;
  69. }
  70. static int nups (StkId f) {
  71. switch (ttype(f)) {
  72. case LUA_TFUNCTION:
  73. return clvalue(f)->nupvalues;
  74. case LUA_TMARK:
  75. return infovalue(f)->func->nupvalues;
  76. default:
  77. return 0;
  78. }
  79. }
  80. int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
  81. int refi;
  82. if (lineinfo == NULL || pc == -1)
  83. return -1; /* no line info or function is not active */
  84. refi = prefi ? *prefi : 0;
  85. if (lineinfo[refi] < 0)
  86. refline += -lineinfo[refi++];
  87. lua_assert(lineinfo[refi] >= 0);
  88. while (lineinfo[refi] > pc) {
  89. refline--;
  90. refi--;
  91. if (lineinfo[refi] < 0)
  92. refline -= -lineinfo[refi--];
  93. lua_assert(lineinfo[refi] >= 0);
  94. }
  95. for (;;) {
  96. int nextline = refline + 1;
  97. int nextref = refi + 1;
  98. if (lineinfo[nextref] < 0)
  99. nextline += -lineinfo[nextref++];
  100. lua_assert(lineinfo[nextref] >= 0);
  101. if (lineinfo[nextref] > pc)
  102. break;
  103. refline = nextline;
  104. refi = nextref;
  105. }
  106. if (prefi) *prefi = refi;
  107. return refline;
  108. }
  109. static int currentpc (StkId f) {
  110. CallInfo *ci = infovalue(f);
  111. lua_assert(isLmark(f));
  112. if (ci->pc)
  113. return (*ci->pc - ci->func->f.l->code) - 1;
  114. else
  115. return -1; /* function is not active */
  116. }
  117. static int currentline (StkId f) {
  118. if (!isLmark(f))
  119. return -1; /* only active lua functions have current-line information */
  120. else {
  121. CallInfo *ci = infovalue(f);
  122. int *lineinfo = ci->func->f.l->lineinfo;
  123. return luaG_getline(lineinfo, currentpc(f), 1, NULL);
  124. }
  125. }
  126. static Proto *getluaproto (StkId f) {
  127. return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
  128. }
  129. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  130. const char *name;
  131. StkId f;
  132. Proto *fp;
  133. LUA_LOCK(L);
  134. name = NULL;
  135. f = ar->_func;
  136. fp = getluaproto(f);
  137. if (fp) { /* `f' is a Lua function? */
  138. name = luaF_getlocalname(fp, n, currentpc(f));
  139. if (name)
  140. luaA_pushobject(L, (f+1)+(n-1)); /* push value */
  141. }
  142. LUA_UNLOCK(L);
  143. return name;
  144. }
  145. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  146. const char *name;
  147. StkId f;
  148. Proto *fp;
  149. LUA_LOCK(L);
  150. name = NULL;
  151. f = ar->_func;
  152. fp = getluaproto(f);
  153. L->top--; /* pop new value */
  154. if (fp) { /* `f' is a Lua function? */
  155. name = luaF_getlocalname(fp, n, currentpc(f));
  156. if (!name || name[0] == '(') /* `(' starts private locals */
  157. name = NULL;
  158. else
  159. setobj((f+1)+(n-1), L->top);
  160. }
  161. LUA_UNLOCK(L);
  162. return name;
  163. }
  164. static void infoLproto (lua_Debug *ar, Proto *f) {
  165. ar->source = f->source->str;
  166. ar->linedefined = f->lineDefined;
  167. ar->what = "Lua";
  168. }
  169. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  170. Closure *cl = NULL;
  171. switch (ttype(func)) {
  172. case LUA_TFUNCTION:
  173. cl = clvalue(func);
  174. break;
  175. case LUA_TMARK:
  176. cl = infovalue(func)->func;
  177. break;
  178. default:
  179. luaD_error(L, "value for `lua_getinfo' is not a function");
  180. }
  181. if (cl->isC) {
  182. ar->source = "=C";
  183. ar->linedefined = -1;
  184. ar->what = "C";
  185. }
  186. else
  187. infoLproto(ar, cl->f.l);
  188. luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
  189. if (ar->linedefined == 0)
  190. ar->what = "main";
  191. }
  192. static const char *travtagmethods (global_State *G, const TObject *o) {
  193. if (ttype(o) == LUA_TFUNCTION) {
  194. int e;
  195. for (e=0; e<TM_N; e++) {
  196. int t;
  197. for (t=0; t<G->ntag; t++)
  198. if (clvalue(o) == luaT_gettm(G, t, e))
  199. return luaT_eventname[e];
  200. }
  201. }
  202. return NULL;
  203. }
  204. static const char *travglobals (lua_State *L, const TObject *o) {
  205. Hash *g = L->gt;
  206. int i;
  207. for (i=0; i<g->size; i++) {
  208. if (luaO_equalObj(o, val(node(g, i))) &&
  209. ttype_key(node(g, i)) == LUA_TSTRING)
  210. return tsvalue_key(node(g, i))->str;
  211. }
  212. return NULL;
  213. }
  214. static void getname (lua_State *L, StkId f, lua_Debug *ar) {
  215. TObject o;
  216. setnormalized(&o, f);
  217. /* try to find a name for given function */
  218. if ((ar->name = travglobals(L, &o)) != NULL)
  219. ar->namewhat = "global";
  220. /* not found: try tag methods */
  221. else if ((ar->name = travtagmethods(G(L), &o)) != NULL)
  222. ar->namewhat = "tag-method";
  223. else ar->namewhat = ""; /* not found at all */
  224. }
  225. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  226. StkId func;
  227. int isactive;
  228. int status = 1;
  229. LUA_LOCK(L);
  230. isactive = (*what != '>');
  231. if (isactive)
  232. func = ar->_func;
  233. else {
  234. what++; /* skip the '>' */
  235. func = L->top - 1;
  236. }
  237. for (; *what; what++) {
  238. switch (*what) {
  239. case 'S': {
  240. funcinfo(L, ar, func);
  241. break;
  242. }
  243. case 'l': {
  244. ar->currentline = currentline(func);
  245. break;
  246. }
  247. case 'u': {
  248. ar->nups = nups(func);
  249. break;
  250. }
  251. case 'n': {
  252. ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
  253. if (ar->namewhat == NULL)
  254. getname(L, func, ar);
  255. break;
  256. }
  257. case 'f': {
  258. setnormalized(L->top, func);
  259. incr_top; /* push function */
  260. break;
  261. }
  262. default: status = 0; /* invalid option */
  263. }
  264. }
  265. if (!isactive) L->top--; /* pop function */
  266. LUA_UNLOCK(L);
  267. return status;
  268. }
  269. /*
  270. ** {======================================================
  271. ** Symbolic Execution
  272. ** =======================================================
  273. */
  274. #define check(x) if (!(x)) return 0;
  275. #define checkjump(pt, pc) check(0 <= (pc) && (pc) < (pt)->sizecode)
  276. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
  277. int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
  278. const Instruction *code = pt->code;
  279. int top = pt->numparams;
  280. int pc = 0;
  281. if (pt->is_vararg) /* varargs? */
  282. top++; /* `arg' */
  283. check (top <= pt->maxstacksize && pt->maxstacksize <= MAXSTACK);
  284. while (pc < lastpc) {
  285. const Instruction i = code[pc++];
  286. OpCode op = GET_OPCODE(i);
  287. int push = (int)luaK_opproperties[op].push;
  288. int pop = (int)luaK_opproperties[op].pop;
  289. int arg1 = 0;
  290. int arg2 = 0;
  291. switch ((enum Mode)luaK_opproperties[op].mode) {
  292. case iO: break;
  293. case iU: arg1 = GETARG_U(i); break;
  294. case iS: arg1 = GETARG_S(i); break;
  295. case iAB: arg1 = GETARG_A(i); arg2 = GETARG_B(i); break;
  296. }
  297. check(0 <= top && top <= pt->maxstacksize);
  298. switch (op) {
  299. case OP_RETURN: {
  300. pop = top-arg1;
  301. break;
  302. }
  303. case OP_CALL: {
  304. if (arg2 == MULT_RET) arg2 = 1;
  305. pop = top-arg1;
  306. push = arg2;
  307. break;
  308. }
  309. case OP_TAILCALL: {
  310. check(arg1 <= top);
  311. pop = top-arg2;
  312. break;
  313. }
  314. case OP_PUSHNIL: {
  315. check(arg1 > 0);
  316. push = arg1;
  317. break;
  318. }
  319. case OP_POP: {
  320. pop = arg1;
  321. break;
  322. }
  323. case OP_PUSHSTRING:
  324. case OP_GETGLOBAL:
  325. case OP_GETDOTTED:
  326. case OP_PUSHSELF:
  327. case OP_SETGLOBAL: {
  328. check(arg1 < pt->sizekstr);
  329. break;
  330. }
  331. case OP_PUSHNUM:
  332. case OP_PUSHNEGNUM: {
  333. check(arg1 < pt->sizeknum);
  334. break;
  335. }
  336. case OP_PUSHUPVALUE: {
  337. /* ?? */
  338. break;
  339. }
  340. case OP_GETLOCAL:
  341. case OP_GETINDEXED:
  342. case OP_SETLOCAL: {
  343. check(arg1 < top);
  344. break;
  345. }
  346. case OP_SETTABLE: {
  347. check(2 <= arg1 && arg1 <= top);
  348. pop = arg2;
  349. break;
  350. }
  351. case OP_SETLIST: {
  352. pop = arg2;
  353. check(top-pop >= 1); /* there must be a table below the list */
  354. break;
  355. }
  356. case OP_SETMAP: {
  357. pop = 2*arg1;
  358. check(top-pop >= 1);
  359. break;
  360. }
  361. case OP_CONCAT: {
  362. pop = arg1;
  363. break;
  364. }
  365. case OP_CLOSURE: {
  366. /* ?? */
  367. pop = arg2;
  368. break;
  369. }
  370. case OP_JMPNE:
  371. case OP_JMPEQ:
  372. case OP_JMPLT:
  373. case OP_JMPLE:
  374. case OP_JMPGT:
  375. case OP_JMPGE:
  376. case OP_JMPT:
  377. case OP_JMPF:
  378. case OP_JMP:
  379. case OP_FORLOOP:
  380. case OP_LFORLOOP: {
  381. checkjump(pt, pc+arg1);
  382. break;
  383. }
  384. case OP_PUSHNILJMP: {
  385. checkjump(pt, pc+1);
  386. break;
  387. }
  388. case OP_JMPONT:
  389. case OP_JMPONF: {
  390. int newpc = pc + arg1;
  391. checkjump(pt, newpc);
  392. /* jump is forward and do not skip `lastpc' and not full check? */
  393. if (pc < newpc && newpc <= lastpc && stackpos >= 0) {
  394. stack[top-1] = pc-1; /* value comes from `and'/`or' */
  395. pc = newpc; /* do the jump */
  396. pop = 0; /* do not pop */
  397. }
  398. break;
  399. }
  400. case OP_FORPREP: {
  401. check(top >= 3);
  402. checkjump(pt, pc-arg1); /* jump is `negative' here */
  403. break;
  404. }
  405. case OP_LFORPREP: {
  406. check(top >= 1);
  407. checkjump(pt, pc-arg1); /* jump is `negative' here */
  408. break;
  409. }
  410. case OP_PUSHINT:
  411. case OP_GETTABLE:
  412. case OP_CREATETABLE:
  413. case OP_ADD:
  414. case OP_ADDI:
  415. case OP_SUB:
  416. case OP_MULT:
  417. case OP_DIV:
  418. case OP_POW:
  419. case OP_MINUS:
  420. case OP_NOT: {
  421. break;
  422. }
  423. }
  424. check(0 <= pop && 0 <= push);
  425. check(0 <= top-pop && top+(push-pop) <= pt->maxstacksize);
  426. top -= pop;
  427. while (push--) stack[top++] = pc-1;
  428. }
  429. check(GET_OPCODE(code[pt->sizecode-1]) == OP_RETURN);
  430. return (stackpos >= 0) ? code[stack[stackpos]] : 1;
  431. }
  432. int luaG_checkcode (const Proto *pt) {
  433. return luaG_symbexec(pt, pt->sizecode-1, -1);
  434. }
  435. static const char *getobjname (lua_State *L, StkId obj, const char **name) {
  436. StkId func = aux_stackedfunction(L, 0, obj);
  437. if (!isLmark(func))
  438. return NULL; /* not an active Lua function */
  439. else {
  440. Proto *p = infovalue(func)->func->f.l;
  441. int pc = currentpc(func);
  442. int stackpos = obj - (func+1); /* func+1 == function base */
  443. Instruction i = luaG_symbexec(p, pc, stackpos);
  444. lua_assert(pc != -1);
  445. switch (GET_OPCODE(i)) {
  446. case OP_GETGLOBAL: {
  447. *name = p->kstr[GETARG_U(i)]->str;
  448. return "global";
  449. }
  450. case OP_GETLOCAL: {
  451. *name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
  452. lua_assert(*name);
  453. return "local";
  454. }
  455. case OP_PUSHSELF:
  456. case OP_GETDOTTED: {
  457. *name = p->kstr[GETARG_U(i)]->str;
  458. return "field";
  459. }
  460. default:
  461. return NULL; /* no useful name found */
  462. }
  463. }
  464. }
  465. static const char *getfuncname (lua_State *L, StkId f, const char **name) {
  466. StkId func = aux_stackedfunction(L, 0, f); /* calling function */
  467. if (!isLmark(func))
  468. return NULL; /* not an active Lua function */
  469. else {
  470. Proto *p = infovalue(func)->func->f.l;
  471. int pc = currentpc(func);
  472. Instruction i;
  473. if (pc == -1) return NULL; /* function is not activated */
  474. i = p->code[pc];
  475. switch (GET_OPCODE(i)) {
  476. case OP_CALL: case OP_TAILCALL:
  477. return getobjname(L, (func+1)+GETARG_A(i), name);
  478. default:
  479. return NULL; /* no useful name found */
  480. }
  481. }
  482. }
  483. /* }====================================================== */
  484. void luaG_typeerror (lua_State *L, StkId o, const char *op) {
  485. const char *name;
  486. const char *kind = getobjname(L, o, &name);
  487. const char *t = luaT_typename(G(L), o);
  488. if (kind)
  489. luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
  490. op, kind, name, t);
  491. else
  492. luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
  493. }
  494. void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
  495. if (ttype(p1) == t) p1++;
  496. lua_assert(ttype(p1) != t);
  497. luaG_typeerror(L, p1, op);
  498. }
  499. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  500. const char *t1 = luaT_typename(G(L), p1);
  501. const char *t2 = luaT_typename(G(L), p2);
  502. if (t1[2] == t2[2])
  503. luaO_verror(L, "attempt to compare two %.10s values", t1);
  504. else
  505. luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
  506. }