ldebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. ** $Id: ldebug.c,v 1.92 2001/10/31 19:58:11 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,
  22. const 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)->c.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)->l.p->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)->l.p->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)->l.p : NULL);
  111. }
  112. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  113. const 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 char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  129. const 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] == '(') /* `(' 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 = "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, "value for `lua_getinfo' is not a function");
  158. cl = NULL; /* to avoid warnings */
  159. }
  160. if (cl->c.isC) {
  161. ar->source = "=C";
  162. ar->linedefined = -1;
  163. ar->what = "C";
  164. }
  165. else
  166. infoLproto(ar, cl->l.p);
  167. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  168. if (ar->linedefined == 0)
  169. ar->what = "main";
  170. }
  171. static const 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 char *travglobals (lua_State *L, const TObject *o) {
  184. Table *g = hvalue(&L->gt);
  185. int i = sizenode(g);
  186. while (i--) {
  187. Node *n = node(g, i);
  188. if (luaO_equalObj(o, val(n)) && ttype(key(n)) == LUA_TSTRING)
  189. return getstr(tsvalue(key(n)));
  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 = "global";
  197. /* not found: try tag methods */
  198. else if ((ar->name = travtagmethods(G(L), f)) != NULL)
  199. ar->namewhat = "tag-method";
  200. else ar->namewhat = ""; /* not found at all */
  201. }
  202. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  203. StkId f;
  204. CallInfo *ci;
  205. int status = 1;
  206. lua_lock(L);
  207. if (*what != '>') { /* 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 'S': {
  219. funcinfo(L, ar, f);
  220. break;
  221. }
  222. case 'l': {
  223. ar->currentline = currentline(ci);
  224. break;
  225. }
  226. case 'u': {
  227. ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->c.nupvalues : 0;
  228. break;
  229. }
  230. case '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 '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. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  255. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  256. static int checklineinfo (const Proto *pt) {
  257. int *lineinfo = pt->lineinfo;
  258. if (lineinfo == NULL) return 1;
  259. check(pt->sizelineinfo >= 2 && lineinfo[pt->sizelineinfo-1] == MAX_INT);
  260. if (*lineinfo < 0) lineinfo++;
  261. check(*lineinfo == 0);
  262. return 1;
  263. }
  264. static int precheck (const Proto *pt) {
  265. check(checklineinfo(pt));
  266. check(pt->maxstacksize <= MAXSTACK);
  267. check(pt->numparams+pt->is_vararg <= pt->maxstacksize);
  268. check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  269. return 1;
  270. }
  271. static int checkopenop (const Proto *pt, int pc) {
  272. Instruction i = pt->code[pc+1];
  273. switch (GET_OPCODE(i)) {
  274. case OP_CALL:
  275. case OP_RETURN: {
  276. check(GETARG_B(i) == NO_REG);
  277. return 1;
  278. }
  279. case OP_SETLISTO: return 1;
  280. default: return 0; /* invalid instruction after an open call */
  281. }
  282. }
  283. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
  284. int pc;
  285. int last; /* stores position of last instruction that changed `reg' */
  286. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  287. if (reg == NO_REG) /* full check? */
  288. check(precheck(pt));
  289. for (pc = 0; pc < lastpc; pc++) {
  290. const Instruction i = pt->code[pc];
  291. OpCode op = GET_OPCODE(i);
  292. int a = GETARG_A(i);
  293. int b = 0;
  294. int c = 0;
  295. checkreg(pt, a);
  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. if (testOpMode(op, OpModeCreg))
  303. check(c < pt->maxstacksize ||
  304. (c >= MAXSTACK && c-MAXSTACK < pt->sizek));
  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, OpModesetA)) {
  318. if (a == reg) last = pc; /* change register `a' */
  319. }
  320. if (testOpMode(op, OpModeT))
  321. check(GET_OPCODE(pt->code[pc+1]) == OP_CJMP);
  322. switch (op) {
  323. case OP_LOADNIL: {
  324. if (a <= reg && reg <= b)
  325. last = pc; /* set registers from `a' to `b' */
  326. break;
  327. }
  328. case OP_GETUPVAL:
  329. case OP_SETUPVAL: {
  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, pc));
  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. check(pc + pt->p[b]->nupvalues < pt->sizecode);
  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 char *getobjname (lua_State *L, StkId obj, const char **name) {
  412. CallInfo *ci = ci_stack(L, obj);
  413. if (isLmark(ci)) { /* an active Lua function? */
  414. Proto *p = ci_func(ci)->l.p;
  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 "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 "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 "field";
  442. }
  443. break;
  444. }
  445. default: break;
  446. }
  447. }
  448. return NULL; /* no useful name found */
  449. }
  450. static const char *getfuncname (lua_State *L, CallInfo *ci,
  451. const 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)->l.p;
  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 char *op) {
  467. const char *name;
  468. const char *kind = getobjname(L, o, &name);
  469. const char *t = luaT_typename(G(L), o);
  470. if (kind)
  471. luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
  472. op, kind, name, t);
  473. else
  474. luaO_verror(L, "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, "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, "perform arithmetic on");
  486. }
  487. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  488. const char *t1 = luaT_typename(G(L), p1);
  489. const char *t2 = luaT_typename(G(L), p2);
  490. if (t1[2] == t2[2])
  491. luaO_verror(L, "attempt to compare two %.10s values", t1);
  492. else
  493. luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
  494. }