ldebug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. ** $Id: ldebug.c,v 1.80 2001/06/08 12:29:27 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define LUA_PRIVATE
  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 l_char *getfuncname (lua_State *L, CallInfo *ci,
  23. const l_char **name);
  24. static int isLmark (CallInfo *ci) {
  25. lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION);
  26. return (ci && ci->prev && !ci_func(ci)->isC);
  27. }
  28. LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  29. lua_Hook oldhook;
  30. lua_lock(L);
  31. oldhook = L->callhook;
  32. L->callhook = func;
  33. lua_unlock(L);
  34. return oldhook;
  35. }
  36. LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  37. lua_Hook oldhook;
  38. lua_lock(L);
  39. oldhook = L->linehook;
  40. L->linehook = func;
  41. lua_unlock(L);
  42. return oldhook;
  43. }
  44. static CallInfo *ci_stack (lua_State *L, StkId obj) {
  45. CallInfo *ci = L->ci;
  46. while (ci->base > obj) ci = ci->prev;
  47. return (ci != &L->basefunc) ? ci : NULL;
  48. }
  49. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  50. CallInfo *ci;
  51. int status;
  52. lua_lock(L);
  53. ci = L->ci;
  54. while (level-- && ci != &L->basefunc) {
  55. lua_assert(ci->base > ci->prev->base);
  56. ci = ci->prev;
  57. }
  58. if (ci == &L->basefunc) status = 0; /* there is no such level */
  59. else {
  60. ar->_ci = ci;
  61. status = 1;
  62. }
  63. lua_unlock(L);
  64. return status;
  65. }
  66. int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
  67. int refi;
  68. if (lineinfo == NULL || pc == -1)
  69. return -1; /* no line info or function is not active */
  70. refi = prefi ? *prefi : 0;
  71. if (lineinfo[refi] < 0)
  72. refline += -lineinfo[refi++];
  73. lua_assert(lineinfo[refi] >= 0);
  74. while (lineinfo[refi] > pc) {
  75. refline--;
  76. refi--;
  77. if (lineinfo[refi] < 0)
  78. refline -= -lineinfo[refi--];
  79. lua_assert(lineinfo[refi] >= 0);
  80. }
  81. for (;;) {
  82. int nextline = refline + 1;
  83. int nextref = refi + 1;
  84. if (lineinfo[nextref] < 0)
  85. nextline += -lineinfo[nextref++];
  86. lua_assert(lineinfo[nextref] >= 0);
  87. if (lineinfo[nextref] > pc)
  88. break;
  89. refline = nextline;
  90. refi = nextref;
  91. }
  92. if (prefi) *prefi = refi;
  93. return refline;
  94. }
  95. static int currentpc (CallInfo *ci) {
  96. lua_assert(isLmark(ci));
  97. if (ci->pc)
  98. return (*ci->pc - ci_func(ci)->f.l->code) - 1;
  99. else
  100. return -1; /* function is not active */
  101. }
  102. static int currentline (CallInfo *ci) {
  103. if (!isLmark(ci))
  104. return -1; /* only active lua functions have current-line information */
  105. else {
  106. int *lineinfo = ci_func(ci)->f.l->lineinfo;
  107. return luaG_getline(lineinfo, currentpc(ci), 1, NULL);
  108. }
  109. }
  110. static Proto *getluaproto (CallInfo *ci) {
  111. return (isLmark(ci) ? ci_func(ci)->f.l : NULL);
  112. }
  113. LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  114. const l_char *name;
  115. CallInfo *ci;
  116. Proto *fp;
  117. lua_lock(L);
  118. name = NULL;
  119. ci = ar->_ci;
  120. fp = getluaproto(ci);
  121. if (fp) { /* is a Lua function? */
  122. name = luaF_getlocalname(fp, n, currentpc(ci));
  123. if (name)
  124. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  125. }
  126. lua_unlock(L);
  127. return name;
  128. }
  129. LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  130. const l_char *name;
  131. CallInfo *ci;
  132. Proto *fp;
  133. lua_lock(L);
  134. name = NULL;
  135. ci = ar->_ci;
  136. fp = getluaproto(ci);
  137. L->top--; /* pop new value */
  138. if (fp) { /* is a Lua function? */
  139. name = luaF_getlocalname(fp, n, currentpc(ci));
  140. if (!name || name[0] == l_c('(')) /* `(' starts private locals */
  141. name = NULL;
  142. else
  143. setobj(ci->base+(n-1), L->top);
  144. }
  145. lua_unlock(L);
  146. return name;
  147. }
  148. static void infoLproto (lua_Debug *ar, Proto *f) {
  149. ar->source = getstr(f->source);
  150. ar->linedefined = f->lineDefined;
  151. ar->what = l_s("Lua");
  152. }
  153. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  154. Closure *cl;
  155. if (ttype(func) == LUA_TFUNCTION)
  156. cl = clvalue(func);
  157. else {
  158. luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
  159. cl = NULL; /* to avoid warnings */
  160. }
  161. if (cl->isC) {
  162. ar->source = l_s("=C");
  163. ar->linedefined = -1;
  164. ar->what = l_s("C");
  165. }
  166. else
  167. infoLproto(ar, cl->f.l);
  168. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  169. if (ar->linedefined == 0)
  170. ar->what = l_s("main");
  171. }
  172. static const l_char *travtagmethods (global_State *G, const TObject *o) {
  173. if (ttype(o) == LUA_TFUNCTION) {
  174. int e;
  175. for (e=0; e<TM_N; e++) {
  176. int t;
  177. for (t=0; t<G->ntag; t++)
  178. if (clvalue(o) == luaT_gettm(G, t, e))
  179. return luaT_eventname[e];
  180. }
  181. }
  182. return NULL;
  183. }
  184. static const l_char *travglobals (lua_State *L, const TObject *o) {
  185. Hash *g = L->gt;
  186. int i;
  187. for (i=0; i<g->size; i++) {
  188. if (luaO_equalObj(o, val(node(g, i))) &&
  189. ttype_key(node(g, i)) == LUA_TSTRING)
  190. return getstr(tsvalue_key(node(g, i)));
  191. }
  192. return NULL;
  193. }
  194. static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
  195. /* try to find a name for given function */
  196. if ((ar->name = travglobals(L, f)) != NULL)
  197. ar->namewhat = l_s("global");
  198. /* not found: try tag methods */
  199. else if ((ar->name = travtagmethods(G(L), f)) != NULL)
  200. ar->namewhat = l_s("tag-method");
  201. else ar->namewhat = l_s(""); /* not found at all */
  202. }
  203. LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) {
  204. StkId f;
  205. CallInfo *ci;
  206. int status = 1;
  207. lua_lock(L);
  208. if (*what != l_c('>')) { /* function is active? */
  209. ci = ar->_ci;
  210. f = ci->base - 1;
  211. }
  212. else {
  213. what++; /* skip the `>' */
  214. ci = NULL;
  215. f = L->top - 1;
  216. }
  217. for (; *what; what++) {
  218. switch (*what) {
  219. case l_c('S'): {
  220. funcinfo(L, ar, f);
  221. break;
  222. }
  223. case l_c('l'): {
  224. ar->currentline = currentline(ci);
  225. break;
  226. }
  227. case l_c('u'): {
  228. ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->nupvalues : 0;
  229. break;
  230. }
  231. case l_c('n'): {
  232. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  233. if (ar->namewhat == NULL)
  234. getname(L, f, ar);
  235. break;
  236. }
  237. case l_c('f'): {
  238. setobj(L->top, f);
  239. incr_top; /* push function */
  240. break;
  241. }
  242. default: status = 0; /* invalid option */
  243. }
  244. }
  245. if (!ci) L->top--; /* pop function */
  246. lua_unlock(L);
  247. return status;
  248. }
  249. /*
  250. ** {======================================================
  251. ** Symbolic Execution and code checker
  252. ** =======================================================
  253. */
  254. #define check(x) if (!(x)) return 0;
  255. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  256. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  257. static int checklineinfo (const Proto *pt) {
  258. int *lineinfo = pt->lineinfo;
  259. if (lineinfo == NULL) return 1;
  260. check(pt->sizelineinfo >= 2 && lineinfo[pt->sizelineinfo-1] == MAX_INT);
  261. if (*lineinfo < 0) lineinfo++;
  262. check(*lineinfo == 0);
  263. return 1;
  264. }
  265. static int precheck (const Proto *pt) {
  266. check(checklineinfo(pt));
  267. check(pt->maxstacksize <= MAXSTACK);
  268. check(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  269. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  270. return 1;
  271. }
  272. static int checkopenop (Instruction i) {
  273. OpCode op = GET_OPCODE(i);
  274. switch (op) {
  275. case OP_CALL:
  276. case OP_RETURN: {
  277. check(GETARG_B(i) == NO_REG);
  278. return 1;
  279. }
  280. case OP_SETLISTO: return 1;
  281. default: return 0; /* invalid instruction after an open call */
  282. }
  283. }
  284. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
  285. int pc;
  286. int last; /* stores position of last instruction that changed `reg' */
  287. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  288. if (reg == NO_REG) /* full check? */
  289. check(precheck(pt));
  290. for (pc = 0; pc < lastpc; pc++) {
  291. const Instruction i = pt->code[pc];
  292. OpCode op = GET_OPCODE(i);
  293. int a = GETARG_A(i);
  294. int b = 0;
  295. int c = 0;
  296. switch (getOpMode(op)) {
  297. case iABC: {
  298. b = GETARG_B(i);
  299. c = GETARG_C(i);
  300. if (testOpMode(op, OpModeBreg)) {
  301. checkreg(pt, b);
  302. check(c < pt->maxstacksize ||
  303. (c >= MAXSTACK && c-MAXSTACK < pt->sizek));
  304. }
  305. break;
  306. }
  307. case iABc: {
  308. b = GETARG_Bc(i);
  309. if (testOpMode(op, OpModeK)) check(b < pt->sizek);
  310. break;
  311. }
  312. case iAsBc: {
  313. b = GETARG_sBc(i);
  314. break;
  315. }
  316. }
  317. if (testOpMode(op, OpModeAreg)) checkreg(pt, a);
  318. if (testOpMode(op, OpModesetA)) {
  319. if (a == reg) last = pc; /* change register `a' */
  320. }
  321. if (testOpMode(op, OpModeT))
  322. check(GET_OPCODE(pt->code[pc+1]) == OP_CJMP);
  323. switch (op) {
  324. case OP_LOADNIL: {
  325. if (a <= reg && reg <= b)
  326. last = pc; /* set registers from `a' to `b' */
  327. break;
  328. }
  329. case OP_LOADUPVAL: {
  330. check(b < pt->nupvalues);
  331. break;
  332. }
  333. case OP_GETGLOBAL:
  334. case OP_SETGLOBAL: {
  335. check(ttype(&pt->k[b]) == LUA_TSTRING);
  336. break;
  337. }
  338. case OP_SELF: {
  339. checkreg(pt, a+1);
  340. if (reg == a+1) last = pc;
  341. break;
  342. }
  343. case OP_CONCAT: {
  344. check(b < c); /* at least two operands */
  345. break;
  346. }
  347. case OP_JMP:
  348. case OP_CJMP: {
  349. int dest = pc+1+b;
  350. check(0 <= dest && dest < pt->sizecode);
  351. /* not full check and jump is forward and do not skip `lastpc'? */
  352. if (reg != NO_REG && pc < dest && dest <= lastpc)
  353. pc += b; /* do the jump */
  354. break;
  355. }
  356. case OP_NILJMP: {
  357. check(pc+2 < pt->sizecode); /* check its jump */
  358. break;
  359. }
  360. case OP_CALL: {
  361. if (b == NO_REG) b = pt->maxstacksize;
  362. if (c == NO_REG) {
  363. check(checkopenop(pt->code[pc+1]));
  364. c = 1;
  365. }
  366. check(b > a);
  367. checkreg(pt, b-1);
  368. checkreg(pt, c-1);
  369. if (reg >= a) last = pc; /* affect all registers above base */
  370. break;
  371. }
  372. case OP_RETURN: {
  373. if (b == NO_REG) b = pt->maxstacksize;
  374. checkreg(pt, b-1);
  375. break;
  376. }
  377. case OP_FORPREP:
  378. case OP_TFORPREP: {
  379. int dest = pc-b; /* jump is negated here */
  380. check(0 <= dest && dest < pt->sizecode &&
  381. GET_OPCODE(pt->code[dest]) == op+1);
  382. break;
  383. }
  384. case OP_FORLOOP:
  385. case OP_TFORLOOP: {
  386. int dest = pc+b;
  387. check(0 <= dest && dest < pt->sizecode &&
  388. pt->code[dest] == SET_OPCODE(i, op-1));
  389. checkreg(pt, a + ((op == OP_FORLOOP) ? 2 : 3));
  390. break;
  391. }
  392. case OP_SETLIST: {
  393. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  394. break;
  395. }
  396. case OP_CLOSURE: {
  397. check(b < pt->sizekproto);
  398. checkreg(pt, a + pt->kproto[b]->nupvalues - 1);
  399. break;
  400. }
  401. default: break;
  402. }
  403. }
  404. return pt->code[last];
  405. }
  406. /* }====================================================== */
  407. int luaG_checkcode (const Proto *pt) {
  408. return luaG_symbexec(pt, pt->sizecode, NO_REG);
  409. }
  410. static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
  411. CallInfo *ci = ci_stack(L, obj);
  412. if (isLmark(ci)) { /* an active Lua function? */
  413. Proto *p = ci_func(ci)->f.l;
  414. int pc = currentpc(ci);
  415. int stackpos = obj - ci->base;
  416. Instruction i;
  417. *name = luaF_getlocalname(p, stackpos+1, pc);
  418. if (*name) /* is a local? */
  419. return l_s("local");
  420. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  421. lua_assert(pc != -1);
  422. switch (GET_OPCODE(i)) {
  423. case OP_GETGLOBAL: {
  424. lua_assert(ttype(&p->k[GETARG_Bc(i)]) == LUA_TSTRING);
  425. *name = getstr(tsvalue(&p->k[GETARG_Bc(i)]));
  426. return l_s("global");
  427. }
  428. case OP_MOVE: {
  429. int a = GETARG_A(i);
  430. int b = GETARG_B(i); /* move from `b' to `a' */
  431. if (b < a)
  432. return getobjname(L, ci->base+b, name); /* get name for `b' */
  433. break;
  434. }
  435. case OP_GETTABLE:
  436. case OP_SELF: {
  437. int c = GETARG_C(i) - MAXSTACK;
  438. if (c >= 0 && ttype(&p->k[c]) == LUA_TSTRING) {
  439. *name = getstr(tsvalue(&p->k[c]));
  440. return l_s("field");
  441. }
  442. break;
  443. }
  444. default: break;
  445. }
  446. }
  447. return NULL; /* no useful name found */
  448. }
  449. static const l_char *getfuncname (lua_State *L, CallInfo *ci,
  450. const l_char **name) {
  451. ci = ci->prev; /* calling function */
  452. if (ci == &L->basefunc || !isLmark(ci))
  453. return NULL; /* not an active Lua function */
  454. else {
  455. Proto *p = ci_func(ci)->f.l;
  456. int pc = currentpc(ci);
  457. Instruction i;
  458. if (pc == -1) return NULL; /* function is not activated */
  459. i = p->code[pc];
  460. return (GET_OPCODE(i) == OP_CALL
  461. ? getobjname(L, ci->base+GETARG_A(i), name)
  462. : NULL); /* no useful name found */
  463. }
  464. }
  465. void luaG_typeerror (lua_State *L, StkId o, const l_char *op) {
  466. const l_char *name;
  467. const l_char *kind = getobjname(L, o, &name);
  468. const l_char *t = luaT_typename(G(L), o);
  469. if (kind)
  470. luaO_verror(L, l_s("attempt to %.30s %.20s `%.40s' (a %.10s value)"),
  471. op, kind, name, t);
  472. else
  473. luaO_verror(L, l_s("attempt to %.30s a %.10s value"), op, t);
  474. }
  475. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  476. if (ttype(p1) == LUA_TSTRING) p1 = p2;
  477. lua_assert(ttype(p1) != LUA_TSTRING);
  478. luaG_typeerror(L, p1, l_s("concat"));
  479. }
  480. void luaG_aritherror (lua_State *L, StkId p1, TObject *p2) {
  481. TObject temp;
  482. if (luaV_tonumber(p1, &temp) != NULL)
  483. p1 = p2; /* first operand is OK; error is in the second */
  484. luaG_typeerror(L, p1, l_s("perform arithmetic on"));
  485. }
  486. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  487. const l_char *t1 = luaT_typename(G(L), p1);
  488. const l_char *t2 = luaT_typename(G(L), p2);
  489. if (t1[2] == t2[2])
  490. luaO_verror(L, l_s("attempt to compare two %.10s values"), t1);
  491. else
  492. luaO_verror(L, l_s("attempt to compare %.10s with %.10s"), t1, t2);
  493. }
  494. #define opmode(t,a,b,c,sa,k,m) (((t)<<OpModeT) | \
  495. ((a)<<OpModeAreg) | ((b)<<OpModeBreg) | ((c)<<OpModeCreg) | \
  496. ((sa)<<OpModesetA) | ((k)<<OpModeK) | (m))
  497. const lu_byte luaG_opmodes[] = {
  498. /* T A B C sA K mode opcode */
  499. opmode(0,1,1,0, 1,0,iABC), /* OP_MOVE */
  500. opmode(0,1,0,0, 1,1,iABc), /* OP_LOADK */
  501. opmode(0,1,0,0, 1,0,iAsBc), /* OP_LOADINT */
  502. opmode(0,1,1,0, 1,0,iABC), /* OP_LOADNIL */
  503. opmode(0,1,0,0, 1,0,iABc), /* OP_LOADUPVAL */
  504. opmode(0,1,0,0, 1,1,iABc), /* OP_GETGLOBAL */
  505. opmode(0,1,1,1, 1,0,iABC), /* OP_GETTABLE */
  506. opmode(0,1,0,0, 0,1,iABc), /* OP_SETGLOBAL */
  507. opmode(0,1,1,1, 0,0,iABC), /* OP_SETTABLE */
  508. opmode(0,1,0,0, 1,0,iABc), /* OP_NEWTABLE */
  509. opmode(0,1,1,1, 1,0,iABC), /* OP_SELF */
  510. opmode(0,1,1,1, 1,0,iABC), /* OP_ADD */
  511. opmode(0,1,1,1, 1,0,iABC), /* OP_SUB */
  512. opmode(0,1,1,1, 1,0,iABC), /* OP_MUL */
  513. opmode(0,1,1,1, 1,0,iABC), /* OP_DIV */
  514. opmode(0,1,1,1, 1,0,iABC), /* OP_POW */
  515. opmode(0,1,1,0, 1,0,iABC), /* OP_UNM */
  516. opmode(0,1,1,0, 1,0,iABC), /* OP_NOT */
  517. opmode(0,1,1,1, 1,0,iABC), /* OP_CONCAT */
  518. opmode(0,0,0,0, 0,0,iAsBc), /* OP_JMP */
  519. opmode(0,0,0,0, 0,0,iAsBc), /* OP_CJMP */
  520. opmode(1,0,1,1, 0,0,iABC), /* OP_TESTEQ */
  521. opmode(1,0,1,1, 0,0,iABC), /* OP_TESTNE */
  522. opmode(1,0,1,1, 0,0,iABC), /* OP_TESTLT */
  523. opmode(1,0,1,1, 0,0,iABC), /* OP_TESTLE */
  524. opmode(1,0,1,1, 0,0,iABC), /* OP_TESTGT */
  525. opmode(1,0,1,1, 0,0,iABC), /* OP_TESTGE */
  526. opmode(1,1,1,0, 1,0,iABC), /* OP_TESTT */
  527. opmode(1,1,1,0, 1,0,iABC), /* OP_TESTF */
  528. opmode(0,1,0,0, 1,0,iAsBc), /* OP_NILJMP */
  529. opmode(0,1,0,0, 0,0,iABC), /* OP_CALL */
  530. opmode(0,1,0,0, 0,0,iABC), /* OP_RETURN */
  531. opmode(0,1,0,0, 0,0,iAsBc), /* OP_FORPREP */
  532. opmode(0,1,0,0, 0,0,iAsBc), /* OP_FORLOOP */
  533. opmode(0,1,0,0, 0,0,iAsBc), /* OP_TFORPREP */
  534. opmode(0,1,0,0, 0,0,iAsBc), /* OP_TFORLOOP */
  535. opmode(0,1,0,0, 0,0,iABc), /* OP_SETLIST */
  536. opmode(0,1,0,0, 0,0,iABc), /* OP_SETLIST0 */
  537. opmode(0,1,0,0, 0,0,iABc) /* OP_CLOSURE */
  538. };