ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. ** $Id: ldebug.c,v 1.86 2001/06/28 19:58:57 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. checkreg(pt, a);
  297. switch (getOpMode(op)) {
  298. case iABC: {
  299. b = GETARG_B(i);
  300. c = GETARG_C(i);
  301. if (testOpMode(op, OpModeBreg))
  302. checkreg(pt, b);
  303. if (testOpMode(op, OpModeCreg))
  304. check(c < pt->maxstacksize ||
  305. (c >= MAXSTACK && c-MAXSTACK < pt->sizek));
  306. break;
  307. }
  308. case iABc: {
  309. b = GETARG_Bc(i);
  310. if (testOpMode(op, OpModeK)) check(b < pt->sizek);
  311. break;
  312. }
  313. case iAsBc: {
  314. b = GETARG_sBc(i);
  315. break;
  316. }
  317. }
  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. /* `c' is a register, and at least two operands */
  345. check(c < MAXSTACK && b < c);
  346. break;
  347. }
  348. case OP_JMP:
  349. case OP_CJMP: {
  350. int dest = pc+1+b;
  351. check(0 <= dest && dest < pt->sizecode);
  352. /* not full check and jump is forward and do not skip `lastpc'? */
  353. if (reg != NO_REG && pc < dest && dest <= lastpc)
  354. pc += b; /* do the jump */
  355. break;
  356. }
  357. case OP_NILJMP: {
  358. check(pc+2 < pt->sizecode); /* check its jump */
  359. break;
  360. }
  361. case OP_CALL: {
  362. if (b != NO_REG) {
  363. checkreg(pt, a+b);
  364. }
  365. if (c == NO_REG) {
  366. check(checkopenop(pt->code[pc+1]));
  367. }
  368. else if (c != 0)
  369. checkreg(pt, a+c-1);
  370. if (reg >= a) last = pc; /* affect all registers above base */
  371. break;
  372. }
  373. case OP_RETURN: {
  374. if (b != NO_REG && b != 0)
  375. checkreg(pt, a+b-1);
  376. break;
  377. }
  378. case OP_FORPREP:
  379. case OP_TFORPREP: {
  380. int dest = pc-b; /* jump is negated here */
  381. check(0 <= dest && dest < pt->sizecode &&
  382. GET_OPCODE(pt->code[dest]) == op+1);
  383. break;
  384. }
  385. case OP_FORLOOP:
  386. case OP_TFORLOOP: {
  387. int dest = pc+b;
  388. check(0 <= dest && dest < pt->sizecode &&
  389. pt->code[dest] == SET_OPCODE(i, op-1));
  390. checkreg(pt, a + ((op == OP_FORLOOP) ? 2 : 3));
  391. break;
  392. }
  393. case OP_SETLIST: {
  394. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  395. break;
  396. }
  397. case OP_CLOSURE: {
  398. check(b < pt->sizep);
  399. checkreg(pt, a + pt->p[b]->nupvalues - 1);
  400. break;
  401. }
  402. default: break;
  403. }
  404. }
  405. return pt->code[last];
  406. }
  407. /* }====================================================== */
  408. int luaG_checkcode (const Proto *pt) {
  409. return luaG_symbexec(pt, pt->sizecode, NO_REG);
  410. }
  411. static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
  412. CallInfo *ci = ci_stack(L, obj);
  413. if (isLmark(ci)) { /* an active Lua function? */
  414. Proto *p = ci_func(ci)->f.l;
  415. int pc = currentpc(ci);
  416. int stackpos = obj - ci->base;
  417. Instruction i;
  418. *name = luaF_getlocalname(p, stackpos+1, pc);
  419. if (*name) /* is a local? */
  420. return l_s("local");
  421. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  422. lua_assert(pc != -1);
  423. switch (GET_OPCODE(i)) {
  424. case OP_GETGLOBAL: {
  425. lua_assert(ttype(&p->k[GETARG_Bc(i)]) == LUA_TSTRING);
  426. *name = svalue(&p->k[GETARG_Bc(i)]);
  427. return l_s("global");
  428. }
  429. case OP_MOVE: {
  430. int a = GETARG_A(i);
  431. int b = GETARG_B(i); /* move from `b' to `a' */
  432. if (b < a)
  433. return getobjname(L, ci->base+b, name); /* get name for `b' */
  434. break;
  435. }
  436. case OP_GETTABLE:
  437. case OP_SELF: {
  438. int c = GETARG_C(i) - MAXSTACK;
  439. if (c >= 0 && ttype(&p->k[c]) == LUA_TSTRING) {
  440. *name = svalue(&p->k[c]);
  441. return l_s("field");
  442. }
  443. break;
  444. }
  445. default: break;
  446. }
  447. }
  448. return NULL; /* no useful name found */
  449. }
  450. static const l_char *getfuncname (lua_State *L, CallInfo *ci,
  451. const l_char **name) {
  452. ci = ci->prev; /* calling function */
  453. if (ci == &L->basefunc || !isLmark(ci))
  454. return NULL; /* not an active Lua function */
  455. else {
  456. Proto *p = ci_func(ci)->f.l;
  457. int pc = currentpc(ci);
  458. Instruction i;
  459. if (pc == -1) return NULL; /* function is not activated */
  460. i = p->code[pc];
  461. return (GET_OPCODE(i) == OP_CALL
  462. ? getobjname(L, ci->base+GETARG_A(i), name)
  463. : NULL); /* no useful name found */
  464. }
  465. }
  466. void luaG_typeerror (lua_State *L, StkId o, const l_char *op) {
  467. const l_char *name;
  468. const l_char *kind = getobjname(L, o, &name);
  469. const l_char *t = luaT_typename(G(L), o);
  470. if (kind)
  471. luaO_verror(L, l_s("attempt to %.30s %.20s `%.40s' (a %.10s value)"),
  472. op, kind, name, t);
  473. else
  474. luaO_verror(L, l_s("attempt to %.30s a %.10s value"), op, t);
  475. }
  476. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  477. if (ttype(p1) == LUA_TSTRING) p1 = p2;
  478. lua_assert(ttype(p1) != LUA_TSTRING);
  479. luaG_typeerror(L, p1, l_s("concat"));
  480. }
  481. void luaG_aritherror (lua_State *L, StkId p1, TObject *p2) {
  482. TObject temp;
  483. if (luaV_tonumber(p1, &temp) != NULL)
  484. p1 = p2; /* first operand is OK; error is in the second */
  485. luaG_typeerror(L, p1, l_s("perform arithmetic on"));
  486. }
  487. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  488. const l_char *t1 = luaT_typename(G(L), p1);
  489. const l_char *t2 = luaT_typename(G(L), p2);
  490. if (t1[2] == t2[2])
  491. luaO_verror(L, l_s("attempt to compare two %.10s values"), t1);
  492. else
  493. luaO_verror(L, l_s("attempt to compare %.10s with %.10s"), t1, t2);
  494. }