ldebug.c 15 KB

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