ldebug.c 17 KB

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