ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. ** $Id: ldebug.c,v 1.124 2002/07/08 18:21:33 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 "luadebug.h"
  21. #include "lvm.h"
  22. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  23. static int currentpc (lua_State *L, CallInfo *ci) {
  24. if (!isLua(ci)) return -1; /* function is not a Lua function? */
  25. /* next function is not using the same `pc'? (not a Lua->Lua call?) */
  26. if (ci == L->ci || !isLua(ci+1) || ci->u.l.pc != (ci+1)->u.l.pc)
  27. ci->savedpc = *ci->u.l.pc; /* may not be saved; save it */
  28. /* function's pc is saved */
  29. return pcRel(ci->savedpc, ci_func(ci)->l.p);
  30. }
  31. static int currentline (lua_State *L, CallInfo *ci) {
  32. int pc = currentpc(L, 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. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask) {
  39. CallInfo *ci;
  40. int allow;
  41. lua_lock(L);
  42. allow = allowhook(L);
  43. if (func == NULL) mask = 0;
  44. else if (mask == 0) func = NULL;
  45. L->hook = func;
  46. L->hookmask = mask;
  47. setallowhook(L, allow);
  48. resethookcount(L);
  49. for (ci = L->base_ci; ci <= L->ci; ci++)
  50. currentpc(L, ci); /* update `savedpc' */
  51. lua_unlock(L);
  52. return 1;
  53. }
  54. LUA_API lua_Hook lua_gethook (lua_State *L) {
  55. return L->hook;
  56. }
  57. LUA_API int lua_gethookmask (lua_State *L) {
  58. return L->hookmask;
  59. }
  60. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  61. int status;
  62. lua_lock(L);
  63. if (L->ci - L->base_ci <= level) status = 0; /* there is no such level */
  64. else {
  65. ar->i_ci = (L->ci - L->base_ci) - level;
  66. status = 1;
  67. }
  68. lua_unlock(L);
  69. return status;
  70. }
  71. static Proto *getluaproto (CallInfo *ci) {
  72. return (isLua(ci) ? ci_func(ci)->l.p : NULL);
  73. }
  74. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  75. const char *name;
  76. CallInfo *ci;
  77. Proto *fp;
  78. lua_lock(L);
  79. name = NULL;
  80. ci = L->base_ci + ar->i_ci;
  81. fp = getluaproto(ci);
  82. if (fp) { /* is a Lua function? */
  83. name = luaF_getlocalname(fp, n, currentpc(L, ci));
  84. if (name)
  85. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  86. }
  87. lua_unlock(L);
  88. return name;
  89. }
  90. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  91. const char *name;
  92. CallInfo *ci;
  93. Proto *fp;
  94. lua_lock(L);
  95. name = NULL;
  96. ci = L->base_ci + ar->i_ci;
  97. fp = getluaproto(ci);
  98. L->top--; /* pop new value */
  99. if (fp) { /* is a Lua function? */
  100. name = luaF_getlocalname(fp, n, currentpc(L, ci));
  101. if (!name || name[0] == '(') /* `(' starts private locals */
  102. name = NULL;
  103. else
  104. setobj(ci->base+(n-1), L->top);
  105. }
  106. lua_unlock(L);
  107. return name;
  108. }
  109. static void infoLproto (lua_Debug *ar, Proto *f) {
  110. ar->source = getstr(f->source);
  111. ar->linedefined = f->lineDefined;
  112. ar->what = "Lua";
  113. }
  114. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  115. Closure *cl;
  116. if (ttype(func) == LUA_TFUNCTION)
  117. cl = clvalue(func);
  118. else {
  119. luaG_runerror(L, "value for `lua_getinfo' is not a function");
  120. cl = NULL; /* to avoid warnings */
  121. }
  122. if (cl->c.isC) {
  123. ar->source = "=[C]";
  124. ar->linedefined = -1;
  125. ar->what = "C";
  126. }
  127. else
  128. infoLproto(ar, cl->l.p);
  129. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  130. if (ar->linedefined == 0)
  131. ar->what = "main";
  132. }
  133. static const char *travglobals (lua_State *L, const TObject *o) {
  134. Table *g = hvalue(gt(L));
  135. int i = sizenode(g);
  136. while (i--) {
  137. Node *n = node(g, i);
  138. if (luaO_rawequalObj(o, val(n)) && ttype(key(n)) == LUA_TSTRING)
  139. return getstr(tsvalue(key(n)));
  140. }
  141. return NULL;
  142. }
  143. static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
  144. /* try to find a name for given function */
  145. if ((ar->name = travglobals(L, f)) != NULL)
  146. ar->namewhat = "global";
  147. else ar->namewhat = ""; /* not found */
  148. }
  149. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  150. StkId f;
  151. CallInfo *ci;
  152. int status = 1;
  153. lua_lock(L);
  154. if (*what != '>') { /* function is active? */
  155. ci = L->base_ci + ar->i_ci;
  156. f = ci->base - 1;
  157. }
  158. else {
  159. what++; /* skip the `>' */
  160. ci = NULL;
  161. f = L->top - 1;
  162. }
  163. for (; *what; what++) {
  164. switch (*what) {
  165. case 'S': {
  166. funcinfo(L, ar, f);
  167. break;
  168. }
  169. case 'l': {
  170. ar->currentline = (ci) ? currentline(L, ci) : -1;
  171. break;
  172. }
  173. case 'u': {
  174. ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->c.nupvalues : 0;
  175. break;
  176. }
  177. case 'n': {
  178. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  179. if (ar->namewhat == NULL)
  180. getname(L, f, ar);
  181. break;
  182. }
  183. case 'f': {
  184. setobj(L->top, f);
  185. status = 2;
  186. break;
  187. }
  188. case 'c': {
  189. ar->isprotected = (ci && luaD_isprotected(L, ci));
  190. break;
  191. }
  192. default: status = 0; /* invalid option */
  193. }
  194. }
  195. if (!ci) L->top--; /* pop function */
  196. if (status == 2) incr_top(L);
  197. lua_unlock(L);
  198. return status;
  199. }
  200. /*
  201. ** {======================================================
  202. ** Symbolic Execution and code checker
  203. ** =======================================================
  204. */
  205. #define check(x) if (!(x)) return 0;
  206. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  207. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  208. static int precheck (const Proto *pt) {
  209. check(pt->maxstacksize <= MAXSTACK);
  210. lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  211. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  212. return 1;
  213. }
  214. static int checkopenop (const Proto *pt, int pc) {
  215. Instruction i = pt->code[pc+1];
  216. switch (GET_OPCODE(i)) {
  217. case OP_CALL:
  218. case OP_TAILCALL:
  219. case OP_RETURN: {
  220. check(GETARG_B(i) == 0);
  221. return 1;
  222. }
  223. case OP_SETLISTO: return 1;
  224. default: return 0; /* invalid instruction after an open call */
  225. }
  226. }
  227. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
  228. int pc;
  229. int last; /* stores position of last instruction that changed `reg' */
  230. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  231. check(precheck(pt));
  232. for (pc = 0; pc < lastpc; pc++) {
  233. const Instruction i = pt->code[pc];
  234. OpCode op = GET_OPCODE(i);
  235. int a = GETARG_A(i);
  236. int b = 0;
  237. int c = 0;
  238. checkreg(pt, a);
  239. switch (getOpMode(op)) {
  240. case iABC: {
  241. b = GETARG_B(i);
  242. c = GETARG_C(i);
  243. if (testOpMode(op, OpModeBreg))
  244. checkreg(pt, b);
  245. if (testOpMode(op, OpModeCreg))
  246. check(c < pt->maxstacksize ||
  247. (c >= MAXSTACK && c-MAXSTACK < pt->sizek));
  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(ttype(&pt->k[b]) == LUA_TSTRING);
  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. if (b != 0) {
  313. checkreg(pt, a+b-1);
  314. }
  315. c--; /* c = num. returns */
  316. if (c == LUA_MULTRET) {
  317. check(checkopenop(pt, pc));
  318. }
  319. else if (c != 0)
  320. checkreg(pt, a+c-1);
  321. if (reg >= a) last = pc; /* affect all registers above base */
  322. break;
  323. }
  324. case OP_TAILCALL:
  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 && ttype(&p->k[c]) == LUA_TSTRING)
  360. return svalue(&p->k[c]);
  361. else
  362. return "?";
  363. }
  364. static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
  365. const char **name) {
  366. if (isLua(ci)) { /* an active Lua function? */
  367. Proto *p = ci_func(ci)->l.p;
  368. int pc = currentpc(L, ci);
  369. Instruction i;
  370. *name = luaF_getlocalname(p, stackpos+1, pc);
  371. if (*name) /* is a local? */
  372. return "local";
  373. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  374. lua_assert(pc != -1);
  375. switch (GET_OPCODE(i)) {
  376. case OP_GETGLOBAL: {
  377. lua_assert(ttype(&p->k[GETARG_Bx(i)]) == LUA_TSTRING);
  378. *name = svalue(&p->k[GETARG_Bx(i)]);
  379. return "global";
  380. }
  381. case OP_MOVE: {
  382. int a = GETARG_A(i);
  383. int b = GETARG_B(i); /* move from `b' to `a' */
  384. if (b < a)
  385. return getobjname(L, ci, b, name); /* get name for `b' */
  386. break;
  387. }
  388. case OP_GETTABLE: {
  389. *name = kname(p, GETARG_C(i));
  390. return "field";
  391. }
  392. case OP_SELF: {
  393. *name = kname(p, GETARG_C(i));
  394. return "method";
  395. }
  396. default: break;
  397. }
  398. }
  399. return NULL; /* no useful name found */
  400. }
  401. static Instruction getcurrentinstr (lua_State *L, CallInfo *ci) {
  402. if (ci == L->base_ci || !isLua(ci))
  403. return (Instruction)(-1); /* not an active Lua function */
  404. else
  405. return ci_func(ci)->l.p->code[currentpc(L, ci)];
  406. }
  407. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  408. Instruction i;
  409. ci--; /* calling function */
  410. i = getcurrentinstr(L, ci);
  411. return (GET_OPCODE(i) == OP_CALL ? getobjname(L, ci, GETARG_A(i), name)
  412. : NULL); /* no useful name found */
  413. }
  414. /* only ANSI way to check whether a pointer points to an array */
  415. static int isinstack (CallInfo *ci, const TObject *o) {
  416. StkId p;
  417. for (p = ci->base; p < ci->top; p++)
  418. if (o == p) return 1;
  419. return 0;
  420. }
  421. void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
  422. const char *name = NULL;
  423. const char *t = luaT_typenames[ttype(o)];
  424. const char *kind = (isinstack(L->ci, o)) ?
  425. getobjname(L, L->ci, o - L->ci->base, &name) : NULL;
  426. if (kind)
  427. luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
  428. op, kind, name, t);
  429. else
  430. luaG_runerror(L, "attempt to %s a %s value", op, t);
  431. }
  432. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  433. if (ttype(p1) == LUA_TSTRING) p1 = p2;
  434. lua_assert(ttype(p1) != LUA_TSTRING);
  435. luaG_typeerror(L, p1, "concat");
  436. }
  437. void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2) {
  438. TObject temp;
  439. if (luaV_tonumber(p1, &temp) == NULL)
  440. p2 = p1; /* first operand is wrong */
  441. luaG_typeerror(L, p2, "perform arithmetic on");
  442. }
  443. int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  444. const char *t1 = luaT_typenames[ttype(p1)];
  445. const char *t2 = luaT_typenames[ttype(p2)];
  446. if (t1[2] == t2[2])
  447. luaG_runerror(L, "attempt to compare two %s values", t1);
  448. else
  449. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  450. return 0;
  451. }
  452. static void addinfo (lua_State *L, int internal) {
  453. const char *msg = svalue(L->top - 1);
  454. CallInfo *ci = L->ci;
  455. if (!internal && ci > L->base_ci) ci--;
  456. if (strchr(msg, '\n')) return; /* message already `formatted' */
  457. if (!isLua(ci)) { /* no Lua code? */
  458. luaO_pushfstring(L, "%s\n", msg); /* no extra info */
  459. }
  460. else { /* add file:line information */
  461. char buff[LUA_IDSIZE];
  462. int line = currentline(L, ci);
  463. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  464. luaO_pushfstring(L, "%s:%d: %s\n", buff, line, msg);
  465. }
  466. }
  467. void luaG_errormsg (lua_State *L, int internal) {
  468. const TObject *errfunc;
  469. if (ttype(L->top - 1) == LUA_TSTRING)
  470. addinfo(L, internal);
  471. errfunc = luaH_getstr(hvalue(registry(L)), luaS_new(L, LUA_TRACEBACK));
  472. if (ttype(errfunc) != LUA_TNIL) { /* is there an error function? */
  473. setobj(L->top, errfunc); /* push function */
  474. setobj(L->top + 1, L->top - 1); /* push error message */
  475. L->top += 2;
  476. luaD_call(L, L->top - 2, 1); /* call error function? */
  477. }
  478. else {
  479. setnilvalue(L->top++);
  480. }
  481. luaD_throw(L, LUA_ERRRUN);
  482. }
  483. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  484. va_list argp;
  485. va_start(argp, fmt);
  486. luaO_pushvfstring(L, fmt, argp);
  487. va_end(argp);
  488. luaG_errormsg(L, 1);
  489. }