ldebug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. ** $Id: ldebug.c,v 1.152 2003/05/13 20:15:59 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define ldebug_c
  9. #include "lua.h"
  10. #include "lapi.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 "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #include "lvm.h"
  22. static const char *getfuncname (CallInfo *ci, const char **name);
  23. #define isLua(ci) (!((ci)->state & CI_C))
  24. static int currentpc (CallInfo *ci) {
  25. if (!isLua(ci)) return -1; /* function is not a Lua function? */
  26. if (ci->state & CI_HASFRAME) /* function has a frame? */
  27. ci->u.l.savedpc = *ci->u.l.pc; /* use `pc' from there */
  28. /* function's pc is saved */
  29. return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
  30. }
  31. static int currentline (CallInfo *ci) {
  32. int pc = currentpc(ci);
  33. if (pc < 0)
  34. return -1; /* only active lua functions have current-line information */
  35. else
  36. return getline(ci_func(ci)->l.p, pc);
  37. }
  38. void luaG_inithooks (lua_State *L) {
  39. CallInfo *ci;
  40. for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
  41. currentpc(ci);
  42. L->hookinit = 1;
  43. }
  44. /*
  45. ** this function can be called asynchronous (e.g. during a signal)
  46. */
  47. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  48. if (func == NULL || mask == 0) { /* turn off hooks? */
  49. mask = 0;
  50. func = NULL;
  51. }
  52. L->hook = func;
  53. L->basehookcount = count;
  54. resethookcount(L);
  55. L->hookmask = cast(lu_byte, mask);
  56. L->hookinit = 0;
  57. return 1;
  58. }
  59. LUA_API lua_Hook lua_gethook (lua_State *L) {
  60. return L->hook;
  61. }
  62. LUA_API int lua_gethookmask (lua_State *L) {
  63. return L->hookmask;
  64. }
  65. LUA_API int lua_gethookcount (lua_State *L) {
  66. return L->basehookcount;
  67. }
  68. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  69. int status;
  70. CallInfo *ci;
  71. lua_lock(L);
  72. for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
  73. level--;
  74. if (!(ci->state & CI_C)) /* Lua function? */
  75. level -= ci->u.l.tailcalls; /* skip lost tail calls */
  76. }
  77. if (level > 0 || ci == L->base_ci) status = 0; /* there is no such level */
  78. else if (level < 0) { /* level is of a lost tail call */
  79. status = 1;
  80. ar->i_ci = 0;
  81. }
  82. else {
  83. status = 1;
  84. ar->i_ci = ci - L->base_ci;
  85. }
  86. lua_unlock(L);
  87. return status;
  88. }
  89. static Proto *getluaproto (CallInfo *ci) {
  90. return (isLua(ci) ? ci_func(ci)->l.p : NULL);
  91. }
  92. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  93. const char *name;
  94. CallInfo *ci;
  95. Proto *fp;
  96. lua_lock(L);
  97. name = NULL;
  98. ci = L->base_ci + ar->i_ci;
  99. fp = getluaproto(ci);
  100. if (fp) { /* is a Lua function? */
  101. name = luaF_getlocalname(fp, n, currentpc(ci));
  102. if (name)
  103. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  104. }
  105. lua_unlock(L);
  106. return name;
  107. }
  108. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  109. const char *name;
  110. CallInfo *ci;
  111. Proto *fp;
  112. lua_lock(L);
  113. name = NULL;
  114. ci = L->base_ci + ar->i_ci;
  115. fp = getluaproto(ci);
  116. L->top--; /* pop new value */
  117. if (fp) { /* is a Lua function? */
  118. name = luaF_getlocalname(fp, n, currentpc(ci));
  119. if (!name || name[0] == '(') /* `(' starts private locals */
  120. name = NULL;
  121. else
  122. setobjs2s(ci->base+(n-1), L->top);
  123. }
  124. lua_unlock(L);
  125. return name;
  126. }
  127. static void funcinfo (lua_Debug *ar, StkId func) {
  128. Closure *cl = clvalue(func);
  129. if (cl->c.isC) {
  130. ar->source = "=[C]";
  131. ar->linedefined = -1;
  132. ar->what = "C";
  133. }
  134. else {
  135. ar->source = getstr(cl->l.p->source);
  136. ar->linedefined = cl->l.p->lineDefined;
  137. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  138. }
  139. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  140. }
  141. static const char *travglobals (lua_State *L, const TObject *o) {
  142. Table *g = hvalue(gt(L));
  143. int i = sizenode(g);
  144. while (i--) {
  145. Node *n = gnode(g, i);
  146. if (luaO_rawequalObj(o, gval(n)) && ttisstring(gkey(n)))
  147. return getstr(tsvalue(gkey(n)));
  148. }
  149. return NULL;
  150. }
  151. static void info_tailcall (lua_State *L, lua_Debug *ar) {
  152. ar->name = ar->namewhat = "";
  153. ar->what = "tail";
  154. ar->linedefined = ar->currentline = -1;
  155. ar->source = "=(tail call)";
  156. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  157. ar->nups = 0;
  158. setnilvalue(L->top);
  159. }
  160. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  161. StkId f, CallInfo *ci) {
  162. int status = 1;
  163. for (; *what; what++) {
  164. switch (*what) {
  165. case 'S': {
  166. funcinfo(ar, f);
  167. break;
  168. }
  169. case 'l': {
  170. ar->currentline = (ci) ? currentline(ci) : -1;
  171. break;
  172. }
  173. case 'u': {
  174. ar->nups = clvalue(f)->c.nupvalues;
  175. break;
  176. }
  177. case 'n': {
  178. ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
  179. if (ar->namewhat == NULL) {
  180. /* try to find a global name */
  181. if ((ar->name = travglobals(L, f)) != NULL)
  182. ar->namewhat = "global";
  183. else ar->namewhat = ""; /* not found */
  184. }
  185. break;
  186. }
  187. case 'f': {
  188. setobj2s(L->top, f);
  189. break;
  190. }
  191. default: status = 0; /* invalid option */
  192. }
  193. }
  194. return status;
  195. }
  196. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  197. int status = 1;
  198. lua_lock(L);
  199. if (*what == '>') {
  200. StkId f = L->top - 1;
  201. if (!ttisfunction(f))
  202. luaG_runerror(L, "value for `lua_getinfo' is not a function");
  203. status = auxgetinfo(L, what + 1, ar, f, NULL);
  204. L->top--; /* pop function */
  205. }
  206. else if (ar->i_ci != 0) { /* no tail call? */
  207. CallInfo *ci = L->base_ci + ar->i_ci;
  208. lua_assert(ttisfunction(ci->base - 1));
  209. status = auxgetinfo(L, what, ar, ci->base - 1, ci);
  210. }
  211. else
  212. info_tailcall(L, ar);
  213. if (strchr(what, 'f')) incr_top(L);
  214. lua_unlock(L);
  215. return status;
  216. }
  217. /*
  218. ** {======================================================
  219. ** Symbolic Execution and code checker
  220. ** =======================================================
  221. */
  222. #define check(x) if (!(x)) return 0;
  223. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  224. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  225. static int precheck (const Proto *pt) {
  226. check(pt->maxstacksize <= MAXSTACK);
  227. check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
  228. lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  229. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  230. return 1;
  231. }
  232. static int checkopenop (const Proto *pt, int pc) {
  233. Instruction i = pt->code[pc+1];
  234. switch (GET_OPCODE(i)) {
  235. case OP_CALL:
  236. case OP_TAILCALL:
  237. case OP_RETURN: {
  238. check(GETARG_B(i) == 0);
  239. return 1;
  240. }
  241. case OP_SETLISTO: return 1;
  242. default: return 0; /* invalid instruction after an open call */
  243. }
  244. }
  245. static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
  246. switch (mode) {
  247. case OpArgN: check(r == 0); break;
  248. case OpArgU: break;
  249. case OpArgR: checkreg(pt, r); break;
  250. case OpArgK:
  251. check(r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
  252. break;
  253. }
  254. return 1;
  255. }
  256. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
  257. int pc;
  258. int last; /* stores position of last instruction that changed `reg' */
  259. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  260. check(precheck(pt));
  261. for (pc = 0; pc < lastpc; pc++) {
  262. const Instruction i = pt->code[pc];
  263. OpCode op = GET_OPCODE(i);
  264. int a = GETARG_A(i);
  265. int b = 0;
  266. int c = 0;
  267. checkreg(pt, a);
  268. switch (getOpMode(op)) {
  269. case iABC: {
  270. b = GETARG_B(i);
  271. c = GETARG_C(i);
  272. check(checkArgMode(pt, b, getBMode(op)));
  273. check(checkArgMode(pt, c, getCMode(op)));
  274. break;
  275. }
  276. case iABx: {
  277. b = GETARG_Bx(i);
  278. if (getBMode(op) == OpArgK) check(b < pt->sizek);
  279. break;
  280. }
  281. case iAsBx: {
  282. b = GETARG_sBx(i);
  283. if (getBMode(op) == OpArgR) {
  284. int dest = pc+1+b;
  285. check(0 <= dest && dest < pt->sizecode);
  286. }
  287. break;
  288. }
  289. }
  290. if (testAMode(op)) {
  291. if (a == reg) last = pc; /* change register `a' */
  292. }
  293. if (testTMode(op)) {
  294. check(pc+2 < pt->sizecode); /* check skip */
  295. check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
  296. }
  297. switch (op) {
  298. case OP_LOADBOOL: {
  299. check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
  300. break;
  301. }
  302. case OP_LOADNIL: {
  303. if (a <= reg && reg <= b)
  304. last = pc; /* set registers from `a' to `b' */
  305. break;
  306. }
  307. case OP_GETUPVAL:
  308. case OP_SETUPVAL: {
  309. check(b < pt->nups);
  310. break;
  311. }
  312. case OP_GETGLOBAL:
  313. case OP_SETGLOBAL: {
  314. check(ttisstring(&pt->k[b]));
  315. break;
  316. }
  317. case OP_SELF: {
  318. checkreg(pt, a+1);
  319. if (reg == a+1) last = pc;
  320. break;
  321. }
  322. case OP_CONCAT: {
  323. check(b < c); /* at least two operands */
  324. break;
  325. }
  326. case OP_TFORLOOP: {
  327. checkreg(pt, a+5); /* space for control variables */
  328. if (reg >= a) last = pc; /* affect all registers above base */
  329. break;
  330. }
  331. case OP_TFORPREP:
  332. case OP_FORLOOP:
  333. case OP_FORPREP:
  334. checkreg(pt, a+3);
  335. /* go through */
  336. case OP_JMP: {
  337. int dest = pc+1+b;
  338. /* not full check and jump is forward and do not skip `lastpc'? */
  339. if (reg != NO_REG && pc < dest && dest <= lastpc)
  340. pc += b; /* do the jump */
  341. break;
  342. }
  343. case OP_CALL:
  344. case OP_TAILCALL: {
  345. if (b != 0) {
  346. checkreg(pt, a+b-1);
  347. }
  348. c--; /* c = num. returns */
  349. if (c == LUA_MULTRET) {
  350. check(checkopenop(pt, pc));
  351. }
  352. else if (c != 0)
  353. checkreg(pt, a+c-1);
  354. if (reg >= a) last = pc; /* affect all registers above base */
  355. break;
  356. }
  357. case OP_RETURN: {
  358. b--; /* b = num. returns */
  359. if (b > 0) checkreg(pt, a+b-1);
  360. break;
  361. }
  362. case OP_SETLIST: {
  363. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  364. break;
  365. }
  366. case OP_CLOSURE: {
  367. int nup;
  368. check(b < pt->sizep);
  369. nup = pt->p[b]->nups;
  370. check(pc + nup < pt->sizecode);
  371. for (; nup>0; nup--) {
  372. OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
  373. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  374. }
  375. break;
  376. }
  377. default: break;
  378. }
  379. }
  380. return pt->code[last];
  381. }
  382. #undef check
  383. #undef checkjump
  384. #undef checkreg
  385. /* }====================================================== */
  386. int luaG_checkcode (const Proto *pt) {
  387. return (luaG_symbexec(pt, pt->sizecode, NO_REG) != 0);
  388. }
  389. static const char *kname (Proto *p, int c) {
  390. c = c - MAXSTACK;
  391. if (c >= 0 && ttisstring(&p->k[c]))
  392. return svalue(&p->k[c]);
  393. else
  394. return "?";
  395. }
  396. static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
  397. if (isLua(ci)) { /* a Lua function? */
  398. Proto *p = ci_func(ci)->l.p;
  399. int pc = currentpc(ci);
  400. Instruction i;
  401. *name = luaF_getlocalname(p, stackpos+1, pc);
  402. if (*name) /* is a local? */
  403. return "local";
  404. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  405. lua_assert(pc != -1);
  406. switch (GET_OPCODE(i)) {
  407. case OP_GETGLOBAL: {
  408. int g = GETARG_Bx(i); /* global index */
  409. lua_assert(ttisstring(&p->k[g]));
  410. *name = svalue(&p->k[g]);
  411. return "global";
  412. }
  413. case OP_MOVE: {
  414. int a = GETARG_A(i);
  415. int b = GETARG_B(i); /* move from `b' to `a' */
  416. if (b < a)
  417. return getobjname(ci, b, name); /* get name for `b' */
  418. break;
  419. }
  420. case OP_GETTABLE: {
  421. int k = GETARG_C(i); /* key index */
  422. *name = kname(p, k);
  423. return "field";
  424. }
  425. case OP_SELF: {
  426. int k = GETARG_C(i); /* key index */
  427. *name = kname(p, k);
  428. return "method";
  429. }
  430. default: break;
  431. }
  432. }
  433. return NULL; /* no useful name found */
  434. }
  435. static const char *getfuncname (CallInfo *ci, const char **name) {
  436. Instruction i;
  437. if ((isLua(ci) && ci->u.l.tailcalls > 0) || !isLua(ci - 1))
  438. return NULL; /* calling function is not Lua (or is unknown) */
  439. ci--; /* calling function */
  440. i = ci_func(ci)->l.p->code[currentpc(ci)];
  441. if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL)
  442. return getobjname(ci, GETARG_A(i), name);
  443. else
  444. return NULL; /* no useful name can be found */
  445. }
  446. /* only ANSI way to check whether a pointer points to an array */
  447. static int isinstack (CallInfo *ci, const TObject *o) {
  448. StkId p;
  449. for (p = ci->base; p < ci->top; p++)
  450. if (o == p) return 1;
  451. return 0;
  452. }
  453. void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
  454. const char *name = NULL;
  455. const char *t = luaT_typenames[ttype(o)];
  456. const char *kind = (isinstack(L->ci, o)) ?
  457. getobjname(L->ci, o - L->base, &name) : NULL;
  458. if (kind)
  459. luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
  460. op, kind, name, t);
  461. else
  462. luaG_runerror(L, "attempt to %s a %s value", op, t);
  463. }
  464. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  465. if (ttisstring(p1)) p1 = p2;
  466. lua_assert(!ttisstring(p1));
  467. luaG_typeerror(L, p1, "concatenate");
  468. }
  469. void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2) {
  470. TObject temp;
  471. if (luaV_tonumber(p1, &temp) == NULL)
  472. p2 = p1; /* first operand is wrong */
  473. luaG_typeerror(L, p2, "perform arithmetic on");
  474. }
  475. int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  476. const char *t1 = luaT_typenames[ttype(p1)];
  477. const char *t2 = luaT_typenames[ttype(p2)];
  478. if (t1[2] == t2[2])
  479. luaG_runerror(L, "attempt to compare two %s values", t1);
  480. else
  481. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  482. return 0;
  483. }
  484. static void addinfo (lua_State *L, const char *msg) {
  485. CallInfo *ci = L->ci;
  486. if (isLua(ci)) { /* is Lua code? */
  487. char buff[LUA_IDSIZE]; /* add file:line information */
  488. int line = currentline(ci);
  489. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  490. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  491. }
  492. }
  493. void luaG_errormsg (lua_State *L) {
  494. if (L->errfunc != 0) { /* is there an error handling function? */
  495. StkId errfunc = restorestack(L, L->errfunc);
  496. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  497. setobjs2s(L->top, L->top - 1); /* move argument */
  498. setobjs2s(L->top - 1, errfunc); /* push function */
  499. incr_top(L);
  500. luaD_call(L, L->top - 2, 1); /* call it */
  501. }
  502. luaD_throw(L, LUA_ERRRUN);
  503. }
  504. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  505. va_list argp;
  506. va_start(argp, fmt);
  507. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  508. va_end(argp);
  509. luaG_errormsg(L);
  510. }