ldebug.c 14 KB

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