ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. ** $Id: ldebug.c,v 1.73 2001/03/07 13:22:55 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lua.h"
  8. #include "lapi.h"
  9. #include "lcode.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lobject.h"
  14. #include "lopcodes.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "ltable.h"
  18. #include "ltm.h"
  19. #include "luadebug.h"
  20. static const l_char *getfuncname (lua_State *L, CallInfo *ci,
  21. const l_char **name);
  22. static int isLmark (CallInfo *ci) {
  23. lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION);
  24. return (ci && ci->prev && !ci_func(ci)->isC);
  25. }
  26. LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  27. lua_Hook oldhook;
  28. lua_lock(L);
  29. oldhook = L->callhook;
  30. L->callhook = func;
  31. lua_unlock(L);
  32. return oldhook;
  33. }
  34. LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  35. lua_Hook oldhook;
  36. lua_lock(L);
  37. oldhook = L->linehook;
  38. L->linehook = func;
  39. lua_unlock(L);
  40. return oldhook;
  41. }
  42. static CallInfo *ci_stack (lua_State *L, StkId obj) {
  43. CallInfo *ci = L->ci;
  44. while (ci->base > obj) ci = ci->prev;
  45. return (ci != &L->basefunc) ? ci : NULL;
  46. }
  47. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  48. CallInfo *ci;
  49. int status;
  50. lua_lock(L);
  51. ci = L->ci;
  52. while (level-- && ci != &L->basefunc) {
  53. lua_assert(ci->base > ci->prev->base);
  54. ci = ci->prev;
  55. }
  56. if (ci == &L->basefunc) status = 0; /* there is no such level */
  57. else {
  58. ar->_ci = ci;
  59. status = 1;
  60. }
  61. lua_unlock(L);
  62. return status;
  63. }
  64. int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
  65. int refi;
  66. if (lineinfo == NULL || pc == -1)
  67. return -1; /* no line info or function is not active */
  68. refi = prefi ? *prefi : 0;
  69. if (lineinfo[refi] < 0)
  70. refline += -lineinfo[refi++];
  71. lua_assert(lineinfo[refi] >= 0);
  72. while (lineinfo[refi] > pc) {
  73. refline--;
  74. refi--;
  75. if (lineinfo[refi] < 0)
  76. refline -= -lineinfo[refi--];
  77. lua_assert(lineinfo[refi] >= 0);
  78. }
  79. for (;;) {
  80. int nextline = refline + 1;
  81. int nextref = refi + 1;
  82. if (lineinfo[nextref] < 0)
  83. nextline += -lineinfo[nextref++];
  84. lua_assert(lineinfo[nextref] >= 0);
  85. if (lineinfo[nextref] > pc)
  86. break;
  87. refline = nextline;
  88. refi = nextref;
  89. }
  90. if (prefi) *prefi = refi;
  91. return refline;
  92. }
  93. static int currentpc (CallInfo *ci) {
  94. lua_assert(isLmark(ci));
  95. if (ci->pc)
  96. return (*ci->pc - ci_func(ci)->f.l->code) - 1;
  97. else
  98. return -1; /* function is not active */
  99. }
  100. static int currentline (CallInfo *ci) {
  101. if (!isLmark(ci))
  102. return -1; /* only active lua functions have current-line information */
  103. else {
  104. int *lineinfo = ci_func(ci)->f.l->lineinfo;
  105. return luaG_getline(lineinfo, currentpc(ci), 1, NULL);
  106. }
  107. }
  108. static Proto *getluaproto (CallInfo *ci) {
  109. return (isLmark(ci) ? ci_func(ci)->f.l : NULL);
  110. }
  111. LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  112. const l_char *name;
  113. CallInfo *ci;
  114. Proto *fp;
  115. lua_lock(L);
  116. name = NULL;
  117. ci = ar->_ci;
  118. fp = getluaproto(ci);
  119. if (fp) { /* is a Lua function? */
  120. name = luaF_getlocalname(fp, n, currentpc(ci));
  121. if (name)
  122. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  123. }
  124. lua_unlock(L);
  125. return name;
  126. }
  127. LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  128. const l_char *name;
  129. CallInfo *ci;
  130. Proto *fp;
  131. lua_lock(L);
  132. name = NULL;
  133. ci = ar->_ci;
  134. fp = getluaproto(ci);
  135. L->top--; /* pop new value */
  136. if (fp) { /* is a Lua function? */
  137. name = luaF_getlocalname(fp, n, currentpc(ci));
  138. if (!name || name[0] == l_c('(')) /* `(' starts private locals */
  139. name = NULL;
  140. else
  141. setobj(ci->base+(n-1), L->top);
  142. }
  143. lua_unlock(L);
  144. return name;
  145. }
  146. static void infoLproto (lua_Debug *ar, Proto *f) {
  147. ar->source = getstr(f->source);
  148. ar->linedefined = f->lineDefined;
  149. ar->what = l_s("Lua");
  150. }
  151. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  152. Closure *cl;
  153. if (ttype(func) == LUA_TFUNCTION)
  154. cl = clvalue(func);
  155. else {
  156. luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
  157. cl = NULL; /* to avoid warnings */
  158. }
  159. if (cl->isC) {
  160. ar->source = l_s("=C");
  161. ar->linedefined = -1;
  162. ar->what = l_s("C");
  163. }
  164. else
  165. infoLproto(ar, cl->f.l);
  166. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  167. if (ar->linedefined == 0)
  168. ar->what = l_s("main");
  169. }
  170. static const l_char *travtagmethods (global_State *G, const TObject *o) {
  171. if (ttype(o) == LUA_TFUNCTION) {
  172. int e;
  173. for (e=0; e<TM_N; e++) {
  174. int t;
  175. for (t=0; t<G->ntag; t++)
  176. if (clvalue(o) == luaT_gettm(G, t, e))
  177. return luaT_eventname[e];
  178. }
  179. }
  180. return NULL;
  181. }
  182. static const l_char *travglobals (lua_State *L, const TObject *o) {
  183. Hash *g = L->gt;
  184. int i;
  185. for (i=0; i<g->size; i++) {
  186. if (luaO_equalObj(o, val(node(g, i))) &&
  187. ttype_key(node(g, i)) == LUA_TSTRING)
  188. return getstr(tsvalue_key(node(g, i)));
  189. }
  190. return NULL;
  191. }
  192. static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
  193. /* try to find a name for given function */
  194. if ((ar->name = travglobals(L, f)) != NULL)
  195. ar->namewhat = l_s("global");
  196. /* not found: try tag methods */
  197. else if ((ar->name = travtagmethods(G(L), f)) != NULL)
  198. ar->namewhat = l_s("tag-method");
  199. else ar->namewhat = l_s(""); /* not found at all */
  200. }
  201. LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) {
  202. StkId f;
  203. CallInfo *ci;
  204. int status = 1;
  205. lua_lock(L);
  206. if (*what != l_c('>')) { /* function is active? */
  207. ci = ar->_ci;
  208. f = ci->base - 1;
  209. }
  210. else {
  211. what++; /* skip the `>' */
  212. ci = NULL;
  213. f = L->top - 1;
  214. }
  215. for (; *what; what++) {
  216. switch (*what) {
  217. case l_c('S'): {
  218. funcinfo(L, ar, f);
  219. break;
  220. }
  221. case l_c('l'): {
  222. ar->currentline = currentline(ci);
  223. break;
  224. }
  225. case l_c('u'): {
  226. ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->nupvalues : 0;
  227. break;
  228. }
  229. case l_c('n'): {
  230. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  231. if (ar->namewhat == NULL)
  232. getname(L, f, ar);
  233. break;
  234. }
  235. case l_c('f'): {
  236. setobj(L->top, f);
  237. incr_top; /* push function */
  238. break;
  239. }
  240. default: status = 0; /* invalid option */
  241. }
  242. }
  243. if (!ci) L->top--; /* pop function */
  244. lua_unlock(L);
  245. return status;
  246. }
  247. /*
  248. ** {======================================================
  249. ** Symbolic Execution and code checker
  250. ** =======================================================
  251. */
  252. #define check(x) if (!(x)) return 0;
  253. static int checklineinfo (const Proto *pt) {
  254. int *lineinfo = pt->lineinfo;
  255. if (lineinfo == NULL) return 1;
  256. check(pt->sizelineinfo >= 2 && lineinfo[pt->sizelineinfo-1] == MAX_INT);
  257. if (*lineinfo < 0) lineinfo++;
  258. check(*lineinfo == 0);
  259. return 1;
  260. }
  261. static int precheck (const Proto *pt) {
  262. check(checklineinfo(pt));
  263. check(pt->maxstacksize <= MAXSTACK);
  264. check(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  265. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  266. return 1;
  267. }
  268. /* value for non-initialized entries in array stacklevel */
  269. #define SL_EMPTY 255
  270. #define checkjump(pt,sl,top,pc) if (!checkjump_aux(pt,sl,top,pc)) return 0;
  271. static int checkjump_aux (const Proto *pt, lu_byte *sl, int top, int pc) {
  272. check(0 <= pc && pc < pt->sizecode);
  273. if (sl == NULL) return 1; /* not full checking */
  274. if (sl[pc] == SL_EMPTY)
  275. sl[pc] = (lu_byte)top;
  276. else
  277. check(sl[pc] == top);
  278. return 1;
  279. }
  280. static Instruction luaG_symbexec (lua_State *L, const Proto *pt,
  281. int lastpc, int stackpos) {
  282. int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
  283. lu_byte *sl = NULL;
  284. int top;
  285. int pc;
  286. if (stackpos < 0) { /* full check? */
  287. int i;
  288. sl = luaO_openspace(L, pt->sizecode, lu_byte);
  289. for (i=0; i<pt->sizecode; i++) /* initialize stack-level array */
  290. sl[i] = SL_EMPTY;
  291. check(precheck(pt));
  292. }
  293. top = pt->numparams;
  294. pc = 0;
  295. if (pt->is_vararg) /* varargs? */
  296. top++; /* `arg' */
  297. if (sl) sl[0] = (lu_byte)top;
  298. while (pc < lastpc) {
  299. const Instruction i = pt->code[pc++];
  300. OpCode op = GET_OPCODE(i);
  301. int arg1 = 0;
  302. int arg2 = 0;
  303. int push, pop;
  304. check(op < NUM_OPCODES);
  305. push = (int)luaK_opproperties[op].push;
  306. pop = (int)luaK_opproperties[op].pop;
  307. switch ((enum Mode)luaK_opproperties[op].mode) {
  308. case iO: break;
  309. case iU: arg1 = GETARG_U(i); check(arg1 >= 0); break;
  310. case iS: arg1 = GETARG_S(i); break;
  311. case iAB:
  312. arg1 = GETARG_A(i); arg2 = GETARG_B(i); check(arg1 >= 0); break;
  313. }
  314. switch (op) {
  315. case OP_RETURN: {
  316. check(arg1 <= top);
  317. pop = top-arg1;
  318. break;
  319. }
  320. case OP_CALL: {
  321. if (arg2 == MULT_RET) arg2 = 1;
  322. check(arg1 < top);
  323. pop = top-arg1;
  324. push = arg2;
  325. break;
  326. }
  327. case OP_PUSHNIL: {
  328. check(arg1 > 0);
  329. push = arg1;
  330. break;
  331. }
  332. case OP_POP: {
  333. pop = arg1;
  334. break;
  335. }
  336. case OP_PUSHSTRING:
  337. case OP_GETGLOBAL:
  338. case OP_GETDOTTED:
  339. case OP_PUSHSELF:
  340. case OP_SETGLOBAL: {
  341. check(arg1 < pt->sizekstr);
  342. break;
  343. }
  344. case OP_PUSHNUM:
  345. case OP_PUSHNEGNUM: {
  346. check(arg1 < pt->sizeknum);
  347. break;
  348. }
  349. case OP_PUSHUPVALUE: {
  350. check(arg1 < pt->nupvalues);
  351. break;
  352. }
  353. case OP_GETLOCAL:
  354. case OP_GETINDEXED:
  355. case OP_SETLOCAL: {
  356. check(arg1 < top);
  357. break;
  358. }
  359. case OP_SETTABLE: {
  360. check(3 <= arg1 && arg1 <= top);
  361. pop = arg2;
  362. break;
  363. }
  364. case OP_SETLIST: {
  365. pop = arg2;
  366. check(top-pop >= 1); /* there must be a table below the list */
  367. break;
  368. }
  369. case OP_SETMAP: {
  370. pop = 2*arg1;
  371. check(top-pop >= 1);
  372. break;
  373. }
  374. case OP_CONCAT: {
  375. pop = arg1;
  376. break;
  377. }
  378. case OP_CLOSURE: {
  379. check(arg1 < pt->sizekproto);
  380. check(arg2 == pt->kproto[arg1]->nupvalues);
  381. pop = arg2;
  382. break;
  383. }
  384. case OP_JMPNE:
  385. case OP_JMPEQ:
  386. case OP_JMPLT:
  387. case OP_JMPLE:
  388. case OP_JMPGT:
  389. case OP_JMPGE:
  390. case OP_JMPT:
  391. case OP_JMPF:
  392. case OP_JMP: {
  393. checkjump(pt, sl, top-pop, pc+arg1);
  394. break;
  395. }
  396. case OP_FORLOOP:
  397. case OP_LFORLOOP:
  398. case OP_JMPONT:
  399. case OP_JMPONF: {
  400. int newpc = pc+arg1;
  401. checkjump(pt, sl, top, newpc);
  402. /* jump is forward and do not skip `lastpc' and not full check? */
  403. if (pc < newpc && newpc <= lastpc && stackpos >= 0) {
  404. stack[top-1] = pc-1; /* value comes from `and'/`or' */
  405. pc = newpc; /* do the jump */
  406. pop = 0; /* do not pop */
  407. }
  408. break;
  409. }
  410. case OP_PUSHNILJMP: {
  411. check(GET_OPCODE(pt->code[pc]) == OP_PUSHINT); /* only valid sequence */
  412. break;
  413. }
  414. case OP_FORPREP: {
  415. int endfor = pc-arg1-1; /* jump is `negative' here */
  416. check(top >= 3);
  417. checkjump(pt, sl, top+push, endfor);
  418. check(GET_OPCODE(pt->code[endfor]) == OP_FORLOOP);
  419. check(GETARG_S(pt->code[endfor]) == arg1);
  420. break;
  421. }
  422. case OP_LFORPREP: {
  423. int endfor = pc-arg1-1; /* jump is `negative' here */
  424. check(top >= 1);
  425. checkjump(pt, sl, top+push, endfor);
  426. check(GET_OPCODE(pt->code[endfor]) == OP_LFORLOOP);
  427. check(GETARG_S(pt->code[endfor]) == arg1);
  428. break;
  429. }
  430. case OP_PUSHINT:
  431. case OP_GETTABLE:
  432. case OP_CREATETABLE:
  433. case OP_ADD:
  434. case OP_ADDI:
  435. case OP_SUB:
  436. case OP_MULT:
  437. case OP_DIV:
  438. case OP_POW:
  439. case OP_MINUS:
  440. case OP_NOT: {
  441. break;
  442. }
  443. }
  444. top -= pop;
  445. check(0 <= top && top+push <= pt->maxstacksize);
  446. while (push--) stack[top++] = pc-1;
  447. checkjump(pt, sl, top, pc);
  448. }
  449. return (stackpos >= 0) ? pt->code[stack[stackpos]] : 1;
  450. }
  451. /* }====================================================== */
  452. int luaG_checkcode (lua_State *L, const Proto *pt) {
  453. return luaG_symbexec(L, pt, pt->sizecode-1, -1);
  454. }
  455. static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
  456. CallInfo *ci = ci_stack(L, obj);
  457. if (!isLmark(ci))
  458. return NULL; /* not an active Lua function */
  459. else {
  460. Proto *p = ci_func(ci)->f.l;
  461. int pc = currentpc(ci);
  462. int stackpos = obj - ci->base;
  463. Instruction i = luaG_symbexec(L, p, pc, stackpos);
  464. lua_assert(pc != -1);
  465. switch (GET_OPCODE(i)) {
  466. case OP_GETGLOBAL: {
  467. *name = getstr(p->kstr[GETARG_U(i)]);
  468. return l_s("global");
  469. }
  470. case OP_GETLOCAL: {
  471. *name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
  472. lua_assert(*name);
  473. return l_s("local");
  474. }
  475. case OP_PUSHSELF:
  476. case OP_GETDOTTED: {
  477. *name = getstr(p->kstr[GETARG_U(i)]);
  478. return l_s("field");
  479. }
  480. default:
  481. return NULL; /* no useful name found */
  482. }
  483. }
  484. }
  485. static const l_char *getfuncname (lua_State *L, CallInfo *ci,
  486. const l_char **name) {
  487. ci = ci->prev; /* calling function */
  488. if (ci == &L->basefunc || !isLmark(ci))
  489. return NULL; /* not an active Lua function */
  490. else {
  491. Proto *p = ci_func(ci)->f.l;
  492. int pc = currentpc(ci);
  493. Instruction i;
  494. if (pc == -1) return NULL; /* function is not activated */
  495. i = p->code[pc];
  496. return (GET_OPCODE(i) == OP_CALL
  497. ? getobjname(L, ci->base+GETARG_A(i), name)
  498. : NULL); /* no useful name found */
  499. }
  500. }
  501. void luaG_typeerror (lua_State *L, StkId o, const l_char *op) {
  502. const l_char *name;
  503. const l_char *kind = getobjname(L, o, &name);
  504. const l_char *t = luaT_typename(G(L), o);
  505. if (kind)
  506. luaO_verror(L, l_s("attempt to %.30s %.20s `%.40s' (a %.10s value)"),
  507. op, kind, name, t);
  508. else
  509. luaO_verror(L, l_s("attempt to %.30s a %.10s value"), op, t);
  510. }
  511. void luaG_binerror (lua_State *L, StkId p1, int t, const l_char *op) {
  512. if (ttype(p1) == t) p1++;
  513. lua_assert(ttype(p1) != t);
  514. luaG_typeerror(L, p1, op);
  515. }
  516. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  517. const l_char *t1 = luaT_typename(G(L), p1);
  518. const l_char *t2 = luaT_typename(G(L), p2);
  519. if (t1[2] == t2[2])
  520. luaO_verror(L, l_s("attempt to compare two %.10s values"), t1);
  521. else
  522. luaO_verror(L, l_s("attempt to compare %.10s with %.10s"), t1, t2);
  523. }