ldebug.c 13 KB

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