ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. ** $Id: ldebug.c,v 1.132 2002/08/12 17:23:12 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 int checkRK (const Proto *pt, int r) {
  223. return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
  224. }
  225. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
  226. int pc;
  227. int last; /* stores position of last instruction that changed `reg' */
  228. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  229. check(precheck(pt));
  230. for (pc = 0; pc < lastpc; pc++) {
  231. const Instruction i = pt->code[pc];
  232. OpCode op = GET_OPCODE(i);
  233. int a = GETARG_A(i);
  234. int b = 0;
  235. int c = 0;
  236. checkreg(pt, a);
  237. switch (getOpMode(op)) {
  238. case iABC: {
  239. b = GETARG_B(i);
  240. c = GETARG_C(i);
  241. if (testOpMode(op, OpModeBreg)) {
  242. checkreg(pt, b);
  243. }
  244. else if (testOpMode(op, OpModeBrk))
  245. check(checkRK(pt, b));
  246. if (testOpMode(op, OpModeCrk))
  247. check(checkRK(pt, c));
  248. break;
  249. }
  250. case iABx: {
  251. b = GETARG_Bx(i);
  252. if (testOpMode(op, OpModeK)) check(b < pt->sizek);
  253. break;
  254. }
  255. case iAsBx: {
  256. b = GETARG_sBx(i);
  257. break;
  258. }
  259. }
  260. if (testOpMode(op, OpModesetA)) {
  261. if (a == reg) last = pc; /* change register `a' */
  262. }
  263. if (testOpMode(op, OpModeT)) {
  264. check(pc+2 < pt->sizecode); /* check skip */
  265. check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
  266. }
  267. switch (op) {
  268. case OP_LOADBOOL: {
  269. check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
  270. break;
  271. }
  272. case OP_LOADNIL: {
  273. if (a <= reg && reg <= b)
  274. last = pc; /* set registers from `a' to `b' */
  275. break;
  276. }
  277. case OP_GETUPVAL:
  278. case OP_SETUPVAL: {
  279. check(b < pt->nupvalues);
  280. break;
  281. }
  282. case OP_GETGLOBAL:
  283. case OP_SETGLOBAL: {
  284. check(ttisstring(&pt->k[b]));
  285. break;
  286. }
  287. case OP_SELF: {
  288. checkreg(pt, a+1);
  289. if (reg == a+1) last = pc;
  290. break;
  291. }
  292. case OP_CONCAT: {
  293. /* `c' is a register, and at least two operands */
  294. check(c < MAXSTACK && b < c);
  295. break;
  296. }
  297. case OP_TFORLOOP:
  298. checkreg(pt, a+2+c);
  299. /* go through */
  300. case OP_FORLOOP:
  301. checkreg(pt, a+2);
  302. /* go through */
  303. case OP_JMP: {
  304. int dest = pc+1+b;
  305. check(0 <= dest && dest < pt->sizecode);
  306. /* not full check and jump is forward and do not skip `lastpc'? */
  307. if (reg != NO_REG && pc < dest && dest <= lastpc)
  308. pc += b; /* do the jump */
  309. break;
  310. }
  311. case OP_CALL:
  312. case OP_TAILCALL: {
  313. if (b != 0) {
  314. checkreg(pt, a+b-1);
  315. }
  316. c--; /* c = num. returns */
  317. if (c == LUA_MULTRET) {
  318. check(checkopenop(pt, pc));
  319. }
  320. else if (c != 0)
  321. checkreg(pt, a+c-1);
  322. if (reg >= a) last = pc; /* affect all registers above base */
  323. break;
  324. }
  325. case OP_RETURN: {
  326. b--; /* b = num. returns */
  327. if (b > 0) checkreg(pt, a+b-1);
  328. break;
  329. }
  330. case OP_SETLIST: {
  331. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  332. break;
  333. }
  334. case OP_CLOSURE: {
  335. int nup;
  336. check(b < pt->sizep);
  337. nup = pt->p[b]->nupvalues;
  338. check(pc + nup < pt->sizecode);
  339. for (; nup>0; nup--) {
  340. OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
  341. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  342. }
  343. break;
  344. }
  345. default: break;
  346. }
  347. }
  348. return pt->code[last];
  349. }
  350. #undef check
  351. #undef checkjump
  352. #undef checkreg
  353. /* }====================================================== */
  354. int luaG_checkcode (const Proto *pt) {
  355. return luaG_symbexec(pt, pt->sizecode, NO_REG);
  356. }
  357. static const char *kname (Proto *p, int c) {
  358. c = c - MAXSTACK;
  359. if (c >= 0 && ttisstring(&p->k[c]))
  360. return svalue(&p->k[c]);
  361. else
  362. return "?";
  363. }
  364. static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
  365. if (isLua(ci)) { /* a Lua function? */
  366. Proto *p = ci_func(ci)->l.p;
  367. int pc = currentpc(ci);
  368. Instruction i;
  369. *name = luaF_getlocalname(p, stackpos+1, pc);
  370. if (*name) /* is a local? */
  371. return "local";
  372. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  373. lua_assert(pc != -1);
  374. switch (GET_OPCODE(i)) {
  375. case OP_GETGLOBAL: {
  376. lua_assert(ttisstring(&p->k[GETARG_Bx(i)]));
  377. *name = svalue(&p->k[GETARG_Bx(i)]);
  378. return "global";
  379. }
  380. case OP_MOVE: {
  381. int a = GETARG_A(i);
  382. int b = GETARG_B(i); /* move from `b' to `a' */
  383. if (b < a)
  384. return getobjname(ci, b, name); /* get name for `b' */
  385. break;
  386. }
  387. case OP_GETTABLE: {
  388. *name = kname(p, GETARG_C(i));
  389. return "field";
  390. }
  391. case OP_SELF: {
  392. *name = kname(p, GETARG_C(i));
  393. return "method";
  394. }
  395. default: break;
  396. }
  397. }
  398. return NULL; /* no useful name found */
  399. }
  400. static Instruction getcurrentinstr (CallInfo *ci) {
  401. return (!isLua(ci)) ? (Instruction)(-1) :
  402. ci_func(ci)->l.p->code[currentpc(ci)];
  403. }
  404. static const char *getfuncname (CallInfo *ci, const char **name) {
  405. Instruction i;
  406. ci--; /* calling function */
  407. i = getcurrentinstr(ci);
  408. return (GET_OPCODE(i) == OP_CALL ? getobjname(ci, GETARG_A(i), name)
  409. : NULL); /* no useful name found */
  410. }
  411. /* only ANSI way to check whether a pointer points to an array */
  412. static int isinstack (CallInfo *ci, const TObject *o) {
  413. StkId p;
  414. for (p = ci->base; p < ci->top; p++)
  415. if (o == p) return 1;
  416. return 0;
  417. }
  418. void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
  419. const char *name = NULL;
  420. const char *t = luaT_typenames[ttype(o)];
  421. const char *kind = (isinstack(L->ci, o)) ?
  422. getobjname(L->ci, o - L->ci->base, &name) : NULL;
  423. if (kind)
  424. luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
  425. op, kind, name, t);
  426. else
  427. luaG_runerror(L, "attempt to %s a %s value", op, t);
  428. }
  429. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  430. if (ttisstring(p1)) p1 = p2;
  431. lua_assert(!ttisstring(p1));
  432. luaG_typeerror(L, p1, "concat");
  433. }
  434. void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2) {
  435. TObject temp;
  436. if (luaV_tonumber(p1, &temp) == NULL)
  437. p2 = p1; /* first operand is wrong */
  438. luaG_typeerror(L, p2, "perform arithmetic on");
  439. }
  440. int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  441. const char *t1 = luaT_typenames[ttype(p1)];
  442. const char *t2 = luaT_typenames[ttype(p2)];
  443. if (t1[2] == t2[2])
  444. luaG_runerror(L, "attempt to compare two %s values", t1);
  445. else
  446. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  447. return 0;
  448. }
  449. static void addinfo (lua_State *L, const char *msg) {
  450. CallInfo *ci = L->ci;
  451. if (!isLua(ci)) { /* no Lua code? */
  452. luaO_pushfstring(L, "%s\n", msg); /* no extra info; just add '\n' */
  453. }
  454. else { /* add file:line information */
  455. char buff[LUA_IDSIZE];
  456. int line = currentline(ci);
  457. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  458. luaO_pushfstring(L, "%s:%d: %s\n", buff, line, msg);
  459. }
  460. }
  461. void luaG_errormsg (lua_State *L) {
  462. if (L->errfunc != 0) { /* is there an error handling function? */
  463. StkId errfunc = restorestack(L, L->errfunc);
  464. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  465. setobj(L->top, L->top - 1); /* move argument */
  466. setobj(L->top - 1, errfunc); /* push function */
  467. incr_top(L);
  468. luaD_call(L, L->top - 2, 1); /* call it */
  469. }
  470. luaD_throw(L, LUA_ERRRUN);
  471. }
  472. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  473. va_list argp;
  474. va_start(argp, fmt);
  475. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  476. va_end(argp);
  477. luaG_errormsg(L);
  478. }