ldebug.c 16 KB

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