ldebug.c 14 KB

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