2
0

ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. ** $Id: ldebug.c,v 1.142 2002/12/06 17:15:35 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. #define isLua(ci) (!((ci)->state & CI_C))
  24. static int currentpc (CallInfo *ci) {
  25. if (!isLua(ci)) return -1; /* function is not a Lua function? */
  26. if (ci->state & CI_HASFRAME) /* function has a frame? */
  27. ci->u.l.savedpc = *ci->u.l.pc; /* use `pc' from there */
  28. /* function's pc is saved */
  29. return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
  30. }
  31. static int currentline (CallInfo *ci) {
  32. int pc = currentpc(ci);
  33. if (pc < 0)
  34. return -1; /* only active lua functions have current-line information */
  35. else
  36. return getline(ci_func(ci)->l.p, pc);
  37. }
  38. void luaG_inithooks (lua_State *L) {
  39. CallInfo *ci;
  40. for (ci = L->ci; ci != L->base_ci; ci--) /* update all `savedpc's */
  41. currentpc(ci);
  42. L->hookinit = 1;
  43. }
  44. /*
  45. ** this function can be called asynchronous (e.g. during a signal)
  46. */
  47. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  48. if (func == NULL || mask == 0) { /* turn off hooks? */
  49. mask = 0;
  50. func = NULL;
  51. }
  52. L->hook = func;
  53. L->basehookcount = count;
  54. resethookcount(L);
  55. L->hookmask = cast(lu_byte, mask);
  56. L->hookinit = 0;
  57. return 1;
  58. }
  59. LUA_API lua_Hook lua_gethook (lua_State *L) {
  60. return L->hook;
  61. }
  62. LUA_API int lua_gethookmask (lua_State *L) {
  63. return L->hookmask;
  64. }
  65. LUA_API int lua_gethookcount (lua_State *L) {
  66. return L->basehookcount;
  67. }
  68. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  69. int status;
  70. int ci;
  71. lua_lock(L);
  72. ci = (L->ci - L->base_ci) - level;
  73. if (ci <= 0) status = 0; /* there is no such level */
  74. else {
  75. ar->i_ci = ci;
  76. status = 1;
  77. }
  78. lua_unlock(L);
  79. return status;
  80. }
  81. static Proto *getluaproto (CallInfo *ci) {
  82. return (isLua(ci) ? ci_func(ci)->l.p : NULL);
  83. }
  84. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  85. const char *name;
  86. CallInfo *ci;
  87. Proto *fp;
  88. lua_lock(L);
  89. name = NULL;
  90. ci = L->base_ci + ar->i_ci;
  91. fp = getluaproto(ci);
  92. if (fp) { /* is a Lua function? */
  93. name = luaF_getlocalname(fp, n, currentpc(ci));
  94. if (name)
  95. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  96. }
  97. lua_unlock(L);
  98. return name;
  99. }
  100. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  101. const char *name;
  102. CallInfo *ci;
  103. Proto *fp;
  104. lua_lock(L);
  105. name = NULL;
  106. ci = L->base_ci + ar->i_ci;
  107. fp = getluaproto(ci);
  108. L->top--; /* pop new value */
  109. if (fp) { /* is a Lua function? */
  110. name = luaF_getlocalname(fp, n, currentpc(ci));
  111. if (!name || name[0] == '(') /* `(' starts private locals */
  112. name = NULL;
  113. else
  114. setobjs2s(ci->base+(n-1), L->top);
  115. }
  116. lua_unlock(L);
  117. return name;
  118. }
  119. static void infoLproto (lua_Debug *ar, Proto *f) {
  120. ar->source = getstr(f->source);
  121. ar->linedefined = f->lineDefined;
  122. ar->what = "Lua";
  123. }
  124. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  125. Closure *cl;
  126. if (ttisfunction(func))
  127. cl = clvalue(func);
  128. else {
  129. luaG_runerror(L, "value for `lua_getinfo' is not a function");
  130. cl = NULL; /* to avoid warnings */
  131. }
  132. if (cl->c.isC) {
  133. ar->source = "=[C]";
  134. ar->linedefined = -1;
  135. ar->what = "C";
  136. }
  137. else
  138. infoLproto(ar, cl->l.p);
  139. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  140. if (ar->linedefined == 0)
  141. ar->what = "main";
  142. }
  143. static const char *travglobals (lua_State *L, const TObject *o) {
  144. Table *g = hvalue(gt(L));
  145. int i = sizenode(g);
  146. while (i--) {
  147. Node *n = node(g, i);
  148. if (luaO_rawequalObj(o, val(n)) && ttisstring(key(n)))
  149. return getstr(tsvalue(key(n)));
  150. }
  151. return NULL;
  152. }
  153. static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
  154. /* try to find a name for given function */
  155. if ((ar->name = travglobals(L, f)) != NULL)
  156. ar->namewhat = "global";
  157. else ar->namewhat = ""; /* not found */
  158. }
  159. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  160. StkId f;
  161. CallInfo *ci;
  162. int status = 1;
  163. lua_lock(L);
  164. if (*what != '>') { /* function is active? */
  165. ci = L->base_ci + ar->i_ci;
  166. f = ci->base - 1;
  167. }
  168. else {
  169. what++; /* skip the `>' */
  170. ci = NULL;
  171. f = L->top - 1;
  172. }
  173. for (; *what; what++) {
  174. switch (*what) {
  175. case 'S': {
  176. funcinfo(L, ar, f);
  177. break;
  178. }
  179. case 'l': {
  180. ar->currentline = (ci) ? currentline(ci) : -1;
  181. break;
  182. }
  183. case 'u': {
  184. ar->nups = (ttisfunction(f)) ? clvalue(f)->c.nupvalues : 0;
  185. break;
  186. }
  187. case 'n': {
  188. ar->namewhat = (ci) ? getfuncname(ci, &ar->name) : NULL;
  189. if (ar->namewhat == NULL)
  190. getname(L, f, ar);
  191. break;
  192. }
  193. case 'f': {
  194. setobj2s(L->top, f);
  195. status = 2;
  196. break;
  197. }
  198. default: status = 0; /* invalid option */
  199. }
  200. }
  201. if (!ci) L->top--; /* pop function */
  202. if (status == 2) 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 checkRK (const Proto *pt, int r) {
  235. return (r < pt->maxstacksize || (r >= MAXSTACK && r-MAXSTACK < pt->sizek));
  236. }
  237. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
  238. int pc;
  239. int last; /* stores position of last instruction that changed `reg' */
  240. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  241. check(precheck(pt));
  242. for (pc = 0; pc < lastpc; pc++) {
  243. const Instruction i = pt->code[pc];
  244. OpCode op = GET_OPCODE(i);
  245. int a = GETARG_A(i);
  246. int b = 0;
  247. int c = 0;
  248. checkreg(pt, a);
  249. switch (getOpMode(op)) {
  250. case iABC: {
  251. b = GETARG_B(i);
  252. c = GETARG_C(i);
  253. if (testOpMode(op, OpModeBreg)) {
  254. checkreg(pt, b);
  255. }
  256. else if (testOpMode(op, OpModeBrk))
  257. check(checkRK(pt, b));
  258. if (testOpMode(op, OpModeCrk))
  259. check(checkRK(pt, c));
  260. break;
  261. }
  262. case iABx: {
  263. b = GETARG_Bx(i);
  264. if (testOpMode(op, OpModeK)) check(b < pt->sizek);
  265. break;
  266. }
  267. case iAsBx: {
  268. b = GETARG_sBx(i);
  269. break;
  270. }
  271. }
  272. if (testOpMode(op, OpModesetA)) {
  273. if (a == reg) last = pc; /* change register `a' */
  274. }
  275. if (testOpMode(op, OpModeT)) {
  276. check(pc+2 < pt->sizecode); /* check skip */
  277. check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
  278. }
  279. switch (op) {
  280. case OP_LOADBOOL: {
  281. check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
  282. break;
  283. }
  284. case OP_LOADNIL: {
  285. if (a <= reg && reg <= b)
  286. last = pc; /* set registers from `a' to `b' */
  287. break;
  288. }
  289. case OP_GETUPVAL:
  290. case OP_SETUPVAL: {
  291. check(b < pt->nupvalues);
  292. break;
  293. }
  294. case OP_GETGLOBAL:
  295. case OP_SETGLOBAL: {
  296. check(ttisstring(&pt->k[b]));
  297. break;
  298. }
  299. case OP_SELF: {
  300. checkreg(pt, a+1);
  301. if (reg == a+1) last = pc;
  302. break;
  303. }
  304. case OP_CONCAT: {
  305. /* `c' is a register, and at least two operands */
  306. check(c < MAXSTACK && b < c);
  307. break;
  308. }
  309. case OP_TFORLOOP:
  310. checkreg(pt, a+c+5);
  311. if (reg >= a) last = pc; /* affect all registers above base */
  312. /* go through */
  313. case OP_FORLOOP:
  314. checkreg(pt, a+2);
  315. /* go through */
  316. case OP_JMP: {
  317. int dest = pc+1+b;
  318. check(0 <= dest && dest < pt->sizecode);
  319. /* not full check and jump is forward and do not skip `lastpc'? */
  320. if (reg != NO_REG && pc < dest && dest <= lastpc)
  321. pc += b; /* do the jump */
  322. break;
  323. }
  324. case OP_CALL:
  325. case OP_TAILCALL: {
  326. if (b != 0) {
  327. checkreg(pt, a+b-1);
  328. }
  329. c--; /* c = num. returns */
  330. if (c == LUA_MULTRET) {
  331. check(checkopenop(pt, pc));
  332. }
  333. else if (c != 0)
  334. checkreg(pt, a+c-1);
  335. if (reg >= a) last = pc; /* affect all registers above base */
  336. break;
  337. }
  338. case OP_RETURN: {
  339. b--; /* b = num. returns */
  340. if (b > 0) checkreg(pt, a+b-1);
  341. break;
  342. }
  343. case OP_SETLIST: {
  344. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  345. break;
  346. }
  347. case OP_CLOSURE: {
  348. int nup;
  349. check(b < pt->sizep);
  350. nup = pt->p[b]->nupvalues;
  351. check(pc + nup < pt->sizecode);
  352. for (; nup>0; nup--) {
  353. OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
  354. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  355. }
  356. break;
  357. }
  358. default: break;
  359. }
  360. }
  361. return pt->code[last];
  362. }
  363. #undef check
  364. #undef checkjump
  365. #undef checkreg
  366. /* }====================================================== */
  367. int luaG_checkcode (const Proto *pt) {
  368. return luaG_symbexec(pt, pt->sizecode, NO_REG);
  369. }
  370. static const char *kname (Proto *p, int c) {
  371. c = c - MAXSTACK;
  372. if (c >= 0 && ttisstring(&p->k[c]))
  373. return svalue(&p->k[c]);
  374. else
  375. return "?";
  376. }
  377. static const char *getobjname (CallInfo *ci, int stackpos, const char **name) {
  378. if (isLua(ci)) { /* a Lua function? */
  379. Proto *p = ci_func(ci)->l.p;
  380. int pc = currentpc(ci);
  381. Instruction i;
  382. *name = luaF_getlocalname(p, stackpos+1, pc);
  383. if (*name) /* is a local? */
  384. return "local";
  385. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  386. lua_assert(pc != -1);
  387. switch (GET_OPCODE(i)) {
  388. case OP_GETGLOBAL: {
  389. lua_assert(ttisstring(&p->k[GETARG_Bx(i)]));
  390. *name = svalue(&p->k[GETARG_Bx(i)]);
  391. return "global";
  392. }
  393. case OP_MOVE: {
  394. int a = GETARG_A(i);
  395. int b = GETARG_B(i); /* move from `b' to `a' */
  396. if (b < a)
  397. return getobjname(ci, b, name); /* get name for `b' */
  398. break;
  399. }
  400. case OP_GETTABLE: {
  401. *name = kname(p, GETARG_C(i));
  402. return "field";
  403. }
  404. case OP_SELF: {
  405. *name = kname(p, GETARG_C(i));
  406. return "method";
  407. }
  408. default: break;
  409. }
  410. }
  411. return NULL; /* no useful name found */
  412. }
  413. static Instruction getcurrentinstr (CallInfo *ci) {
  414. return (!isLua(ci)) ? (Instruction)(-1) :
  415. ci_func(ci)->l.p->code[currentpc(ci)];
  416. }
  417. static const char *getfuncname (CallInfo *ci, const char **name) {
  418. Instruction i;
  419. ci--; /* calling function */
  420. i = getcurrentinstr(ci);
  421. return (GET_OPCODE(i) == OP_CALL ? getobjname(ci, GETARG_A(i), name)
  422. : NULL); /* no useful name 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, "concat");
  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. }