ldebug.c 13 KB

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