ldebug.c 14 KB

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