ldebug.c 13 KB

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