ldebug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. ** $Id: ldebug.c,v 2.14 2005/04/05 13:41:29 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 = 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, StkId func) {
  121. Closure *cl = clvalue(func);
  122. if (cl->c.isC) {
  123. ar->source = "=[C]";
  124. ar->linedefined = -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->what = (ar->linedefined == 0) ? "main" : "Lua";
  131. }
  132. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  133. }
  134. static void info_tailcall (lua_State *L, lua_Debug *ar) {
  135. ar->name = ar->namewhat = "";
  136. ar->what = "tail";
  137. 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. setnilvalue(L->top);
  142. }
  143. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  144. StkId f, CallInfo *ci) {
  145. int status = 1;
  146. for (; *what; what++) {
  147. switch (*what) {
  148. case 'S': {
  149. funcinfo(ar, f);
  150. break;
  151. }
  152. case 'l': {
  153. ar->currentline = (ci) ? currentline(L, ci) : -1;
  154. break;
  155. }
  156. case 'u': {
  157. ar->nups = clvalue(f)->c.nupvalues;
  158. break;
  159. }
  160. case 'n': {
  161. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  162. if (ar->namewhat == NULL) {
  163. ar->namewhat = ""; /* not found */
  164. ar->name = NULL;
  165. }
  166. break;
  167. }
  168. case 'f': {
  169. setobj2s(L, L->top, f);
  170. break;
  171. }
  172. default: status = 0; /* invalid option */
  173. }
  174. }
  175. return status;
  176. }
  177. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  178. int status = 1;
  179. lua_lock(L);
  180. if (*what == '>') {
  181. StkId f = L->top - 1;
  182. if (!ttisfunction(f))
  183. luaG_runerror(L, "value for `lua_getinfo' is not a function");
  184. status = auxgetinfo(L, what + 1, ar, f, NULL);
  185. L->top--; /* pop function */
  186. }
  187. else if (ar->i_ci != 0) { /* no tail call? */
  188. CallInfo *ci = L->base_ci + ar->i_ci;
  189. lua_assert(ttisfunction(ci->func));
  190. status = auxgetinfo(L, what, ar, ci->func, ci);
  191. }
  192. else
  193. info_tailcall(L, ar);
  194. if (strchr(what, 'f')) incr_top(L);
  195. lua_unlock(L);
  196. return status;
  197. }
  198. /*
  199. ** {======================================================
  200. ** Symbolic Execution and code checker
  201. ** =======================================================
  202. */
  203. #define check(x) if (!(x)) return 0;
  204. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  205. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  206. static int precheck (const Proto *pt) {
  207. check(pt->maxstacksize <= MAXSTACK);
  208. check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
  209. lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  210. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  211. return 1;
  212. }
  213. #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
  214. int luaG_checkopenop (Instruction i) {
  215. switch (GET_OPCODE(i)) {
  216. case OP_CALL:
  217. case OP_TAILCALL:
  218. case OP_RETURN:
  219. case OP_SETLIST: {
  220. check(GETARG_B(i) == 0);
  221. return 1;
  222. }
  223. default: return 0; /* invalid instruction after an open call */
  224. }
  225. }
  226. static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
  227. switch (mode) {
  228. case OpArgN: check(r == 0); break;
  229. case OpArgU: break;
  230. case OpArgR: checkreg(pt, r); break;
  231. case OpArgK:
  232. check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
  233. break;
  234. }
  235. return 1;
  236. }
  237. static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
  238. int pc;
  239. int last; /* stores position of last instruction that changed `reg' */
  240. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  241. check(precheck(pt));
  242. for (pc = 0; pc < lastpc; pc++) {
  243. const Instruction i = pt->code[pc];
  244. OpCode op = GET_OPCODE(i);
  245. int a = GETARG_A(i);
  246. int b = 0;
  247. int c = 0;
  248. check(op < NUM_OPCODES);
  249. checkreg(pt, a);
  250. switch (getOpMode(op)) {
  251. case iABC: {
  252. b = GETARG_B(i);
  253. c = GETARG_C(i);
  254. check(checkArgMode(pt, b, getBMode(op)));
  255. check(checkArgMode(pt, c, getCMode(op)));
  256. break;
  257. }
  258. case iABx: {
  259. b = GETARG_Bx(i);
  260. if (getBMode(op) == OpArgK) check(b < pt->sizek);
  261. break;
  262. }
  263. case iAsBx: {
  264. b = GETARG_sBx(i);
  265. if (getBMode(op) == OpArgR) {
  266. int dest = pc+1+b;
  267. check(0 <= dest && dest < pt->sizecode);
  268. if (dest > 0) {
  269. /* cannot jump to a setlist count */
  270. const Instruction d = pt->code[dest-1];
  271. check(!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0));
  272. }
  273. }
  274. break;
  275. }
  276. }
  277. if (testAMode(op)) {
  278. if (a == reg) last = pc; /* change register `a' */
  279. }
  280. if (testTMode(op)) {
  281. check(pc+2 < pt->sizecode); /* check skip */
  282. check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
  283. }
  284. switch (op) {
  285. case OP_LOADBOOL: {
  286. check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
  287. break;
  288. }
  289. case OP_LOADNIL: {
  290. if (a <= reg && reg <= b)
  291. last = pc; /* set registers from `a' to `b' */
  292. break;
  293. }
  294. case OP_GETUPVAL:
  295. case OP_SETUPVAL: {
  296. check(b < pt->nups);
  297. break;
  298. }
  299. case OP_GETGLOBAL:
  300. case OP_SETGLOBAL: {
  301. check(ttisstring(&pt->k[b]));
  302. break;
  303. }
  304. case OP_SELF: {
  305. checkreg(pt, a+1);
  306. if (reg == a+1) last = pc;
  307. break;
  308. }
  309. case OP_CONCAT: {
  310. check(b < c); /* at least two operands */
  311. break;
  312. }
  313. case OP_TFORLOOP: {
  314. check(c >= 1); /* at least one result (control variable) */
  315. checkreg(pt, a+3+c); /* space for results */
  316. if (reg >= a+3) last = pc; /* affect all regs above its call base */
  317. break;
  318. }
  319. case OP_TFORPREP:
  320. case OP_FORLOOP:
  321. case OP_FORPREP:
  322. checkreg(pt, a+3);
  323. /* go through */
  324. case OP_JMP: {
  325. int dest = pc+1+b;
  326. /* not full check and jump is forward and do not skip `lastpc'? */
  327. if (reg != NO_REG && pc < dest && dest <= lastpc)
  328. pc += b; /* do the jump */
  329. break;
  330. }
  331. case OP_CALL:
  332. case OP_TAILCALL: {
  333. if (b != 0) {
  334. checkreg(pt, a+b-1);
  335. }
  336. c--; /* c = num. returns */
  337. if (c == LUA_MULTRET) {
  338. check(checkopenop(pt, pc));
  339. }
  340. else if (c != 0)
  341. checkreg(pt, a+c-1);
  342. if (reg >= a) last = pc; /* affect all registers above base */
  343. break;
  344. }
  345. case OP_RETURN: {
  346. b--; /* b = num. returns */
  347. if (b > 0) checkreg(pt, a+b-1);
  348. break;
  349. }
  350. case OP_SETLIST: {
  351. if (b > 0) checkreg(pt, a + b);
  352. if (c == 0) pc++;
  353. break;
  354. }
  355. case OP_CLOSURE: {
  356. int nup;
  357. check(b < pt->sizep);
  358. nup = pt->p[b]->nups;
  359. check(pc + nup < pt->sizecode);
  360. for (; nup>0; nup--) {
  361. OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
  362. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  363. }
  364. break;
  365. }
  366. case OP_VARARG: {
  367. check(pt->is_vararg & NEWSTYLEVARARG);
  368. b--;
  369. if (b == LUA_MULTRET) check(checkopenop(pt, pc));
  370. checkreg(pt, a+b-1);
  371. break;
  372. }
  373. default: break;
  374. }
  375. }
  376. return pt->code[last];
  377. }
  378. #undef check
  379. #undef checkjump
  380. #undef checkreg
  381. /* }====================================================== */
  382. int luaG_checkcode (const Proto *pt) {
  383. return (symbexec(pt, pt->sizecode, NO_REG) != 0);
  384. }
  385. static const char *kname (Proto *p, int c) {
  386. if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
  387. return svalue(&p->k[INDEXK(c)]);
  388. else
  389. return "?";
  390. }
  391. static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
  392. const char **name) {
  393. if (isLua(ci)) { /* a Lua function? */
  394. Proto *p = ci_func(ci)->l.p;
  395. int pc = currentpc(L, ci);
  396. Instruction i;
  397. *name = luaF_getlocalname(p, stackpos+1, pc);
  398. if (*name) /* is a local? */
  399. return "local";
  400. i = symbexec(p, pc, stackpos); /* try symbolic execution */
  401. lua_assert(pc != -1);
  402. switch (GET_OPCODE(i)) {
  403. case OP_GETGLOBAL: {
  404. int g = GETARG_Bx(i); /* global index */
  405. lua_assert(ttisstring(&p->k[g]));
  406. *name = svalue(&p->k[g]);
  407. return "global";
  408. }
  409. case OP_MOVE: {
  410. int a = GETARG_A(i);
  411. int b = GETARG_B(i); /* move from `b' to `a' */
  412. if (b < a)
  413. return getobjname(L, ci, b, name); /* get name for `b' */
  414. break;
  415. }
  416. case OP_GETTABLE: {
  417. int k = GETARG_C(i); /* key index */
  418. *name = kname(p, k);
  419. return "field";
  420. }
  421. case OP_GETUPVAL: {
  422. int u = GETARG_B(i); /* upvalue index */
  423. *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
  424. return "upvalue";
  425. }
  426. case OP_SELF: {
  427. int k = GETARG_C(i); /* key index */
  428. *name = kname(p, k);
  429. return "method";
  430. }
  431. default: break;
  432. }
  433. }
  434. return NULL; /* no useful name found */
  435. }
  436. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  437. Instruction i;
  438. if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
  439. return NULL; /* calling function is not Lua (or is unknown) */
  440. ci--; /* calling function */
  441. i = ci_func(ci)->l.p->code[currentpc(L, ci)];
  442. if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  443. GET_OPCODE(i) == OP_TFORLOOP)
  444. return getobjname(L, ci, GETARG_A(i), name);
  445. else
  446. return NULL; /* no useful name can be found */
  447. }
  448. /* only ANSI way to check whether a pointer points to an array */
  449. static int isinstack (CallInfo *ci, const TValue *o) {
  450. StkId p;
  451. for (p = ci->base; p < ci->top; p++)
  452. if (o == p) return 1;
  453. return 0;
  454. }
  455. void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  456. const char *name = NULL;
  457. const char *t = luaT_typenames[ttype(o)];
  458. const char *kind = (isinstack(L->ci, o)) ?
  459. getobjname(L, L->ci, o - L->base, &name) : NULL;
  460. if (kind)
  461. luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
  462. op, kind, name, t);
  463. else
  464. luaG_runerror(L, "attempt to %s a %s value", op, t);
  465. }
  466. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  467. if (ttisstring(p1)) p1 = p2;
  468. lua_assert(!ttisstring(p1));
  469. luaG_typeerror(L, p1, "concatenate");
  470. }
  471. void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  472. TValue temp;
  473. if (luaV_tonumber(p1, &temp) == NULL)
  474. p2 = p1; /* first operand is wrong */
  475. luaG_typeerror(L, p2, "perform arithmetic on");
  476. }
  477. int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  478. const char *t1 = luaT_typenames[ttype(p1)];
  479. const char *t2 = luaT_typenames[ttype(p2)];
  480. if (t1[2] == t2[2])
  481. luaG_runerror(L, "attempt to compare two %s values", t1);
  482. else
  483. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  484. return 0;
  485. }
  486. static void addinfo (lua_State *L, const char *msg) {
  487. CallInfo *ci = L->ci;
  488. if (isLua(ci)) { /* is Lua code? */
  489. char buff[LUA_IDSIZE]; /* add file:line information */
  490. int line = currentline(L, ci);
  491. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  492. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  493. }
  494. }
  495. void luaG_errormsg (lua_State *L) {
  496. if (L->errfunc != 0) { /* is there an error handling function? */
  497. StkId errfunc = restorestack(L, L->errfunc);
  498. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  499. setobjs2s(L, L->top, L->top - 1); /* move argument */
  500. setobjs2s(L, L->top - 1, errfunc); /* push function */
  501. incr_top(L);
  502. luaD_call(L, L->top - 2, 1); /* call it */
  503. }
  504. luaD_throw(L, LUA_ERRRUN);
  505. }
  506. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  507. va_list argp;
  508. va_start(argp, fmt);
  509. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  510. va_end(argp);
  511. luaG_errormsg(L);
  512. }