ldebug.c 14 KB

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