ldebug.c 17 KB

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