ldebug.c 14 KB

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