ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. ** $Id: ldebug.c,v 1.136 2002/11/07 15:37:10 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. void luaG_inithooks (lua_State *L) {
  38. CallInfo *ci;
  39. for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
  40. currentpc(ci);
  41. L->hookinit = 1;
  42. }
  43. /*
  44. ** this function can be called asynchronous (e.g. during a signal)
  45. */
  46. LUA_API int lua_sethook (lua_State *L, lua_Hook func, unsigned long mask) {
  47. ls_count count = lua_getmaskcount(mask);
  48. if (func == NULL || mask == 0) { /* turn off hooks? */
  49. mask = 0;
  50. func = NULL;
  51. }
  52. else if (count > 0) mask |= (1<<LUA_HOOKCOUNT);
  53. L->hook = func;
  54. L->basehookcount = count;
  55. resethookcount(L);
  56. L->hookmask = cast(lu_byte, mask & 0xf);
  57. L->hookinit = 0;
  58. return 1;
  59. }
  60. LUA_API lua_Hook lua_gethook (lua_State *L) {
  61. return L->hook;
  62. }
  63. LUA_API unsigned long lua_gethookmask (lua_State *L) {
  64. return L->hookmask | LUA_MASKCOUNT(L->basehookcount);
  65. }
  66. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  67. int status;
  68. lua_lock(L);
  69. if (L->ci - L->base_ci <= level) status = 0; /* there is no such level */
  70. else {
  71. ar->i_ci = (L->ci - L->base_ci) - level;
  72. status = 1;
  73. }
  74. lua_unlock(L);
  75. return status;
  76. }
  77. static Proto *getluaproto (CallInfo *ci) {
  78. return (isLua(ci) ? ci_func(ci)->l.p : NULL);
  79. }
  80. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  81. const char *name;
  82. CallInfo *ci;
  83. Proto *fp;
  84. lua_lock(L);
  85. name = NULL;
  86. ci = L->base_ci + ar->i_ci;
  87. fp = getluaproto(ci);
  88. if (fp) { /* is a Lua function? */
  89. name = luaF_getlocalname(fp, n, currentpc(ci));
  90. if (name)
  91. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  92. }
  93. lua_unlock(L);
  94. return name;
  95. }
  96. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  97. const char *name;
  98. CallInfo *ci;
  99. Proto *fp;
  100. lua_lock(L);
  101. name = NULL;
  102. ci = L->base_ci + ar->i_ci;
  103. fp = getluaproto(ci);
  104. L->top--; /* pop new value */
  105. if (fp) { /* is a Lua function? */
  106. name = luaF_getlocalname(fp, n, currentpc(ci));
  107. if (!name || name[0] == '(') /* `(' starts private locals */
  108. name = NULL;
  109. else
  110. setobjs2s(ci->base+(n-1), L->top);
  111. }
  112. lua_unlock(L);
  113. return name;
  114. }
  115. static void infoLproto (lua_Debug *ar, Proto *f) {
  116. ar->source = getstr(f->source);
  117. ar->linedefined = f->lineDefined;
  118. ar->what = "Lua";
  119. }
  120. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  121. Closure *cl;
  122. if (ttisfunction(func))
  123. cl = clvalue(func);
  124. else {
  125. luaG_runerror(L, "value for `lua_getinfo' is not a function");
  126. cl = NULL; /* to avoid warnings */
  127. }
  128. if (cl->c.isC) {
  129. ar->source = "=[C]";
  130. ar->linedefined = -1;
  131. ar->what = "C";
  132. }
  133. else
  134. infoLproto(ar, cl->l.p);
  135. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  136. if (ar->linedefined == 0)
  137. ar->what = "main";
  138. }
  139. static const char *travglobals (lua_State *L, const TObject *o) {
  140. Table *g = hvalue(gt(L));
  141. int i = sizenode(g);
  142. while (i--) {
  143. Node *n = node(g, i);
  144. if (luaO_rawequalObj(o, val(n)) && ttisstring(key(n)))
  145. return getstr(tsvalue(key(n)));
  146. }
  147. return NULL;
  148. }
  149. static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
  150. /* try to find a name for given function */
  151. if ((ar->name = travglobals(L, f)) != NULL)
  152. ar->namewhat = "global";
  153. else ar->namewhat = ""; /* not found */
  154. }
  155. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  156. StkId f;
  157. CallInfo *ci;
  158. int status = 1;
  159. lua_lock(L);
  160. if (*what != '>') { /* function is active? */
  161. ci = L->base_ci + ar->i_ci;
  162. f = ci->base - 1;
  163. }
  164. else {
  165. what++; /* skip the `>' */
  166. ci = NULL;
  167. f = L->top - 1;
  168. }
  169. for (; *what; what++) {
  170. switch (*what) {
  171. case 'S': {
  172. funcinfo(L, ar, f);
  173. break;
  174. }
  175. case 'l': {
  176. ar->currentline = (ci) ? currentline(ci) : -1;
  177. break;
  178. }
  179. case 'u': {
  180. ar->nups = (ttisfunction(f)) ? clvalue(f)->c.nupvalues : 0;
  181. break;
  182. }
  183. case 'n': {
  184. ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
  185. if (ar->namewhat == NULL)
  186. getname(L, f, ar);
  187. break;
  188. }
  189. case 'f': {
  190. setobj2s(L->top, f);
  191. status = 2;
  192. break;
  193. }
  194. default: status = 0; /* invalid option */
  195. }
  196. }
  197. if (!ci) L->top--; /* pop function */
  198. if (status == 2) incr_top(L);
  199. lua_unlock(L);
  200. return status;
  201. }
  202. /*
  203. ** {======================================================
  204. ** Symbolic Execution and code checker
  205. ** =======================================================
  206. */
  207. #define check(x) if (!(x)) return 0;
  208. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  209. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  210. static int precheck (const Proto *pt) {
  211. check(pt->maxstacksize <= MAXSTACK);
  212. check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
  213. lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  214. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  215. return 1;
  216. }
  217. static int checkopenop (const Proto *pt, int pc) {
  218. Instruction i = pt->code[pc+1];
  219. switch (GET_OPCODE(i)) {
  220. case OP_CALL:
  221. case OP_TAILCALL:
  222. case OP_RETURN: {
  223. check(GETARG_B(i) == 0);
  224. return 1;
  225. }
  226. case OP_SETLISTO: return 1;
  227. default: return 0; /* invalid instruction after an open call */
  228. }
  229. }
  230. static int checkRK (const Proto *pt, int r) {
  231. return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
  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. checkreg(pt, a);
  245. switch (getOpMode(op)) {
  246. case iABC: {
  247. b = GETARG_B(i);
  248. c = GETARG_C(i);
  249. if (testOpMode(op, OpModeBreg)) {
  250. checkreg(pt, b);
  251. }
  252. else if (testOpMode(op, OpModeBrk))
  253. check(checkRK(pt, b));
  254. if (testOpMode(op, OpModeCrk))
  255. check(checkRK(pt, c));
  256. break;
  257. }
  258. case iABx: {
  259. b = GETARG_Bx(i);
  260. if (testOpMode(op, OpModeK)) check(b < pt->sizek);
  261. break;
  262. }
  263. case iAsBx: {
  264. b = GETARG_sBx(i);
  265. break;
  266. }
  267. }
  268. if (testOpMode(op, OpModesetA)) {
  269. if (a == reg) last = pc; /* change register `a' */
  270. }
  271. if (testOpMode(op, OpModeT)) {
  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->nupvalues);
  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. /* `c' is a register, and at least two operands */
  302. check(c < MAXSTACK && b < c);
  303. break;
  304. }
  305. case OP_TFORLOOP:
  306. checkreg(pt, a+2+c);
  307. /* go through */
  308. case OP_FORLOOP:
  309. checkreg(pt, a+2);
  310. /* go through */
  311. case OP_JMP: {
  312. int dest = pc+1+b;
  313. check(0 <= dest && dest < pt->sizecode);
  314. /* not full check and jump is forward and do not skip `lastpc'? */
  315. if (reg != NO_REG && pc < dest && dest <= lastpc)
  316. pc += b; /* do the jump */
  317. break;
  318. }
  319. case OP_CALL:
  320. case OP_TAILCALL: {
  321. if (b != 0) {
  322. checkreg(pt, a+b-1);
  323. }
  324. c--; /* c = num. returns */
  325. if (c == LUA_MULTRET) {
  326. check(checkopenop(pt, pc));
  327. }
  328. else if (c != 0)
  329. checkreg(pt, a+c-1);
  330. if (reg >= a) last = pc; /* affect all registers above base */
  331. break;
  332. }
  333. case OP_RETURN: {
  334. b--; /* b = num. returns */
  335. if (b > 0) checkreg(pt, a+b-1);
  336. break;
  337. }
  338. case OP_SETLIST: {
  339. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  340. break;
  341. }
  342. case OP_CLOSURE: {
  343. int nup;
  344. check(b < pt->sizep);
  345. nup = pt->p[b]->nupvalues;
  346. check(pc + nup < pt->sizecode);
  347. for (; nup>0; nup--) {
  348. OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
  349. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  350. }
  351. break;
  352. }
  353. default: break;
  354. }
  355. }
  356. return pt->code[last];
  357. }
  358. #undef check
  359. #undef checkjump
  360. #undef checkreg
  361. /* }====================================================== */
  362. int luaG_checkcode (const Proto *pt) {
  363. return luaG_symbexec(pt, pt->sizecode, NO_REG);
  364. }
  365. static const char *kname (Proto *p, int c) {
  366. c = c - MAXSTACK;
  367. if (c >= 0 && ttisstring(&p->k[c]))
  368. return svalue(&p->k[c]);
  369. else
  370. return "?";
  371. }
  372. static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
  373. if (isLua(ci)) { /* a Lua function? */
  374. Proto *p = ci_func(ci)->l.p;
  375. int pc = currentpc(ci);
  376. Instruction i;
  377. *name = luaF_getlocalname(p, stackpos+1, pc);
  378. if (*name) /* is a local? */
  379. return "local";
  380. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  381. lua_assert(pc != -1);
  382. switch (GET_OPCODE(i)) {
  383. case OP_GETGLOBAL: {
  384. lua_assert(ttisstring(&p->k[GETARG_Bx(i)]));
  385. *name = svalue(&p->k[GETARG_Bx(i)]);
  386. return "global";
  387. }
  388. case OP_MOVE: {
  389. int a = GETARG_A(i);
  390. int b = GETARG_B(i); /* move from `b' to `a' */
  391. if (b < a)
  392. return getobjname(ci, b, name); /* get name for `b' */
  393. break;
  394. }
  395. case OP_GETTABLE: {
  396. *name = kname(p, GETARG_C(i));
  397. return "field";
  398. }
  399. case OP_SELF: {
  400. *name = kname(p, GETARG_C(i));
  401. return "method";
  402. }
  403. default: break;
  404. }
  405. }
  406. return NULL; /* no useful name found */
  407. }
  408. static Instruction getcurrentinstr (CallInfo *ci) {
  409. return (!isLua(ci)) ? (Instruction)(-1) :
  410. ci_func(ci)->l.p->code[currentpc(ci)];
  411. }
  412. static const char *getfuncname (CallInfo *ci, const char **name) {
  413. Instruction i;
  414. ci--; /* calling function */
  415. i = getcurrentinstr(ci);
  416. return (GET_OPCODE(i) == OP_CALL ? getobjname(ci, GETARG_A(i), name)
  417. : NULL); /* no useful name found */
  418. }
  419. /* only ANSI way to check whether a pointer points to an array */
  420. static int isinstack (CallInfo *ci, const TObject *o) {
  421. StkId p;
  422. for (p = ci->base; p < ci->top; p++)
  423. if (o == p) return 1;
  424. return 0;
  425. }
  426. void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
  427. const char *name = NULL;
  428. const char *t = luaT_typenames[ttype(o)];
  429. const char *kind = (isinstack(L->ci, o)) ?
  430. getobjname(L->ci, o - L->ci->base, &name) : NULL;
  431. if (kind)
  432. luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
  433. op, kind, name, t);
  434. else
  435. luaG_runerror(L, "attempt to %s a %s value", op, t);
  436. }
  437. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  438. if (ttisstring(p1)) p1 = p2;
  439. lua_assert(!ttisstring(p1));
  440. luaG_typeerror(L, p1, "concat");
  441. }
  442. void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2) {
  443. TObject temp;
  444. if (luaV_tonumber(p1, &temp) == NULL)
  445. p2 = p1; /* first operand is wrong */
  446. luaG_typeerror(L, p2, "perform arithmetic on");
  447. }
  448. int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  449. const char *t1 = luaT_typenames[ttype(p1)];
  450. const char *t2 = luaT_typenames[ttype(p2)];
  451. if (t1[2] == t2[2])
  452. luaG_runerror(L, "attempt to compare two %s values", t1);
  453. else
  454. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  455. return 0;
  456. }
  457. static void addinfo (lua_State *L, const char *msg) {
  458. CallInfo *ci = L->ci;
  459. if (isLua(ci)) { /* is Lua code? */
  460. char buff[LUA_IDSIZE]; /* add file:line information */
  461. int line = currentline(ci);
  462. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  463. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  464. }
  465. }
  466. void luaG_errormsg (lua_State *L) {
  467. if (L->errfunc != 0) { /* is there an error handling function? */
  468. StkId errfunc = restorestack(L, L->errfunc);
  469. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  470. setobjs2s(L->top, L->top - 1); /* move argument */
  471. setobjs2s(L->top - 1, errfunc); /* push function */
  472. incr_top(L);
  473. luaD_call(L, L->top - 2, 1); /* call it */
  474. }
  475. luaD_throw(L, LUA_ERRRUN);
  476. }
  477. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  478. va_list argp;
  479. va_start(argp, fmt);
  480. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  481. va_end(argp);
  482. luaG_errormsg(L);
  483. }