ldebug.c 15 KB

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