ldebug.c 16 KB

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