ldebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. ** $Id: ldebug.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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. return (ttype(ci->base - 1) == LUA_TFUNCTION && !ci_func(ci)->c.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--;
  45. return ci;
  46. }
  47. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  48. int status;
  49. lua_lock(L);
  50. if (L->ci - L->base_ci <= level) status = 0; /* there is no such level */
  51. else {
  52. ar->_ci = (L->ci - L->base_ci) - level;
  53. status = 1;
  54. }
  55. lua_unlock(L);
  56. return status;
  57. }
  58. int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
  59. int refi;
  60. if (lineinfo == NULL) return -1; /* no line info */
  61. refi = prefi ? *prefi : 0;
  62. if (lineinfo[refi] < 0)
  63. refline += -lineinfo[refi++];
  64. lua_assert(lineinfo[refi] >= 0);
  65. while (lineinfo[refi] > pc) {
  66. refline--;
  67. refi--;
  68. if (lineinfo[refi] < 0)
  69. refline -= -lineinfo[refi--];
  70. lua_assert(lineinfo[refi] >= 0);
  71. }
  72. for (;;) {
  73. int nextline = refline + 1;
  74. int nextref = refi + 1;
  75. if (lineinfo[nextref] < 0)
  76. nextline += -lineinfo[nextref++];
  77. lua_assert(lineinfo[nextref] >= 0);
  78. if (lineinfo[nextref] > pc)
  79. break;
  80. refline = nextline;
  81. refi = nextref;
  82. }
  83. if (prefi) *prefi = refi;
  84. return refline;
  85. }
  86. static int currentpc (CallInfo *ci) {
  87. lua_assert(isLmark(ci));
  88. if (ci->savedpc)
  89. return (ci->savedpc - ci_func(ci)->l.p->code) - 1;
  90. else if (ci->pc)
  91. return (*ci->pc - ci_func(ci)->l.p->code) - 1;
  92. else return 0;
  93. }
  94. static int currentline (CallInfo *ci) {
  95. if (!isLmark(ci))
  96. return -1; /* only active lua functions have current-line information */
  97. else {
  98. int *lineinfo = ci_func(ci)->l.p->lineinfo;
  99. return luaG_getline(lineinfo, currentpc(ci), 1, NULL);
  100. }
  101. }
  102. static Proto *getluaproto (CallInfo *ci) {
  103. return (isLmark(ci) ? ci_func(ci)->l.p : NULL);
  104. }
  105. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  106. const char *name;
  107. CallInfo *ci;
  108. Proto *fp;
  109. lua_lock(L);
  110. name = NULL;
  111. ci = L->base_ci + ar->_ci;
  112. fp = getluaproto(ci);
  113. if (fp) { /* is a Lua function? */
  114. name = luaF_getlocalname(fp, n, currentpc(ci));
  115. if (name)
  116. luaA_pushobject(L, ci->base+(n-1)); /* push value */
  117. }
  118. lua_unlock(L);
  119. return name;
  120. }
  121. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  122. const char *name;
  123. CallInfo *ci;
  124. Proto *fp;
  125. lua_lock(L);
  126. name = NULL;
  127. ci = L->base_ci + ar->_ci;
  128. fp = getluaproto(ci);
  129. L->top--; /* pop new value */
  130. if (fp) { /* is a Lua function? */
  131. name = luaF_getlocalname(fp, n, currentpc(ci));
  132. if (!name || name[0] == '(') /* `(' starts private locals */
  133. name = NULL;
  134. else
  135. setobj(ci->base+(n-1), L->top);
  136. }
  137. lua_unlock(L);
  138. return name;
  139. }
  140. static void infoLproto (lua_Debug *ar, Proto *f) {
  141. ar->source = getstr(f->source);
  142. ar->linedefined = f->lineDefined;
  143. ar->what = "Lua";
  144. }
  145. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  146. Closure *cl;
  147. if (ttype(func) == LUA_TFUNCTION)
  148. cl = clvalue(func);
  149. else {
  150. luaD_error(L, "value for `lua_getinfo' is not a function");
  151. cl = NULL; /* to avoid warnings */
  152. }
  153. if (cl->c.isC) {
  154. ar->source = "=C";
  155. ar->linedefined = -1;
  156. ar->what = "C";
  157. }
  158. else
  159. infoLproto(ar, cl->l.p);
  160. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  161. if (ar->linedefined == 0)
  162. ar->what = "main";
  163. }
  164. static const char *travglobals (lua_State *L, const TObject *o) {
  165. Table *g = hvalue(gt(L));
  166. int i = sizenode(g);
  167. while (i--) {
  168. Node *n = node(g, i);
  169. if (luaO_equalObj(o, val(n)) && ttype(key(n)) == LUA_TSTRING)
  170. return getstr(tsvalue(key(n)));
  171. }
  172. return NULL;
  173. }
  174. static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
  175. /* try to find a name for given function */
  176. if ((ar->name = travglobals(L, f)) != NULL)
  177. ar->namewhat = "global";
  178. else ar->namewhat = ""; /* not found */
  179. }
  180. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  181. StkId f;
  182. CallInfo *ci;
  183. int status = 1;
  184. lua_lock(L);
  185. if (*what != '>') { /* function is active? */
  186. ci = L->base_ci + ar->_ci;
  187. f = ci->base - 1;
  188. }
  189. else {
  190. what++; /* skip the `>' */
  191. ci = NULL;
  192. f = L->top - 1;
  193. }
  194. for (; *what; what++) {
  195. switch (*what) {
  196. case 'S': {
  197. funcinfo(L, ar, f);
  198. break;
  199. }
  200. case 'l': {
  201. ar->currentline = (ci) ? currentline(ci) : -1;
  202. break;
  203. }
  204. case 'u': {
  205. ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->c.nupvalues : 0;
  206. break;
  207. }
  208. case 'n': {
  209. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  210. if (ar->namewhat == NULL)
  211. getname(L, f, ar);
  212. break;
  213. }
  214. case 'f': {
  215. setobj(L->top, f);
  216. incr_top; /* push function */
  217. break;
  218. }
  219. default: status = 0; /* invalid option */
  220. }
  221. }
  222. if (!ci) L->top--; /* pop function */
  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) == NO_REG);
  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(GET_OPCODE(pt->code[pc+1]) == OP_CJMP);
  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. case OP_CJMP: {
  332. int dest = pc+1+b;
  333. check(0 <= dest && dest < pt->sizecode);
  334. /* not full check and jump is forward and do not skip `lastpc'? */
  335. if (reg != NO_REG && pc < dest && dest <= lastpc)
  336. pc += b; /* do the jump */
  337. break;
  338. }
  339. case OP_CALL: {
  340. if (b != NO_REG) {
  341. checkreg(pt, a+b);
  342. }
  343. if (c == NO_REG) {
  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. if (b != NO_REG && b != 0)
  353. 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(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,
  429. const char **name) {
  430. ci--; /* calling function */
  431. if (ci == L->base_ci || !isLmark(ci))
  432. return NULL; /* not an active Lua function */
  433. else {
  434. Proto *p = ci_func(ci)->l.p;
  435. int pc = currentpc(ci);
  436. Instruction i;
  437. i = p->code[pc];
  438. return (GET_OPCODE(i) == OP_CALL
  439. ? getobjname(L, ci->base+GETARG_A(i), name)
  440. : NULL); /* no useful name found */
  441. }
  442. }
  443. void luaG_typeerror (lua_State *L, StkId o, const char *op) {
  444. const char *name;
  445. const char *kind = getobjname(L, o, &name);
  446. const char *t = luaT_typenames[ttype(o)];
  447. if (kind)
  448. luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
  449. op, kind, name, t);
  450. else
  451. luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
  452. }
  453. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  454. if (ttype(p1) == LUA_TSTRING) p1 = p2;
  455. lua_assert(ttype(p1) != LUA_TSTRING);
  456. luaG_typeerror(L, p1, "concat");
  457. }
  458. void luaG_aritherror (lua_State *L, StkId p1, TObject *p2) {
  459. TObject temp;
  460. if (luaV_tonumber(p1, &temp) != NULL)
  461. p1 = p2; /* first operand is OK; error is in the second */
  462. luaG_typeerror(L, p1, "perform arithmetic on");
  463. }
  464. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  465. const char *t1 = luaT_typenames[ttype(p1)];
  466. const char *t2 = luaT_typenames[ttype(p2)];
  467. if (t1[2] == t2[2])
  468. luaO_verror(L, "attempt to compare two %.10s values", t1);
  469. else
  470. luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
  471. }