ldebug.c 13 KB

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