ldebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. ** $Id: ldebug.c,v 1.116 2002/05/15 18:57:44 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. luaG_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. check(precheck(pt));
  226. for (pc = 0; pc < lastpc; pc++) {
  227. const Instruction i = pt->code[pc];
  228. OpCode op = GET_OPCODE(i);
  229. int a = GETARG_A(i);
  230. int b = 0;
  231. int c = 0;
  232. checkreg(pt, a);
  233. switch (getOpMode(op)) {
  234. case iABC: {
  235. b = GETARG_B(i);
  236. c = GETARG_C(i);
  237. if (testOpMode(op, OpModeBreg))
  238. checkreg(pt, b);
  239. if (testOpMode(op, OpModeCreg))
  240. check(c < pt->maxstacksize ||
  241. (c >= MAXSTACK && c-MAXSTACK < pt->sizek));
  242. break;
  243. }
  244. case iABx: {
  245. b = GETARG_Bx(i);
  246. if (testOpMode(op, OpModeK)) check(b < pt->sizek);
  247. break;
  248. }
  249. case iAsBx: {
  250. b = GETARG_sBx(i);
  251. break;
  252. }
  253. }
  254. if (testOpMode(op, OpModesetA)) {
  255. if (a == reg) last = pc; /* change register `a' */
  256. }
  257. if (testOpMode(op, OpModeT)) {
  258. check(pc+2 < pt->sizecode); /* check skip */
  259. check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
  260. }
  261. switch (op) {
  262. case OP_LOADBOOL: {
  263. check(c == 0 || pc+2 < pt->sizecode); /* check its jump */
  264. break;
  265. }
  266. case OP_LOADNIL: {
  267. if (a <= reg && reg <= b)
  268. last = pc; /* set registers from `a' to `b' */
  269. break;
  270. }
  271. case OP_GETUPVAL:
  272. case OP_SETUPVAL: {
  273. check(b < pt->nupvalues);
  274. break;
  275. }
  276. case OP_GETGLOBAL:
  277. case OP_SETGLOBAL: {
  278. check(ttype(&pt->k[b]) == LUA_TSTRING);
  279. break;
  280. }
  281. case OP_SELF: {
  282. checkreg(pt, a+1);
  283. if (reg == a+1) last = pc;
  284. break;
  285. }
  286. case OP_CONCAT: {
  287. /* `c' is a register, and at least two operands */
  288. check(c < MAXSTACK && b < c);
  289. break;
  290. }
  291. case OP_TFORLOOP:
  292. checkreg(pt, a+2+c);
  293. /* go through */
  294. case OP_FORLOOP:
  295. checkreg(pt, a+2);
  296. /* go through */
  297. case OP_JMP: {
  298. int dest = pc+1+b;
  299. check(0 <= dest && dest < pt->sizecode);
  300. /* not full check and jump is forward and do not skip `lastpc'? */
  301. if (reg != NO_REG && pc < dest && dest <= lastpc)
  302. pc += b; /* do the jump */
  303. break;
  304. }
  305. case OP_CALL: {
  306. if (b != 0) {
  307. checkreg(pt, a+b-1);
  308. }
  309. c--; /* c = num. returns */
  310. if (c == LUA_MULTRET) {
  311. check(checkopenop(pt, pc));
  312. }
  313. else if (c != 0)
  314. checkreg(pt, a+c-1);
  315. if (reg >= a) last = pc; /* affect all registers above base */
  316. break;
  317. }
  318. case OP_TAILCALL:
  319. case OP_RETURN: {
  320. b--; /* b = num. returns */
  321. if (b > 0) checkreg(pt, a+b-1);
  322. break;
  323. }
  324. case OP_SETLIST: {
  325. checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
  326. break;
  327. }
  328. case OP_CLOSURE: {
  329. int nup;
  330. check(b < pt->sizep);
  331. nup = pt->p[b]->nupvalues;
  332. check(pc + nup < pt->sizecode);
  333. for (; nup>0; nup--) {
  334. OpCode op1 = GET_OPCODE(pt->code[pc+nup]);
  335. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  336. }
  337. break;
  338. }
  339. default: break;
  340. }
  341. }
  342. return pt->code[last];
  343. }
  344. /* }====================================================== */
  345. int luaG_checkcode (const Proto *pt) {
  346. return luaG_symbexec(pt, pt->sizecode, NO_REG);
  347. }
  348. static const char *kname (Proto *p, int c) {
  349. c = c - MAXSTACK;
  350. if (c >= 0 && ttype(&p->k[c]) == LUA_TSTRING)
  351. return svalue(&p->k[c]);
  352. else
  353. return "?";
  354. }
  355. static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
  356. const char **name) {
  357. if (isLmark(ci)) { /* an active Lua function? */
  358. Proto *p = ci_func(ci)->l.p;
  359. int pc = currentpc(L, ci);
  360. Instruction i;
  361. *name = luaF_getlocalname(p, stackpos+1, pc);
  362. if (*name) /* is a local? */
  363. return "local";
  364. i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
  365. lua_assert(pc != -1);
  366. switch (GET_OPCODE(i)) {
  367. case OP_GETGLOBAL: {
  368. lua_assert(ttype(&p->k[GETARG_Bx(i)]) == LUA_TSTRING);
  369. *name = svalue(&p->k[GETARG_Bx(i)]);
  370. return "global";
  371. }
  372. case OP_MOVE: {
  373. int a = GETARG_A(i);
  374. int b = GETARG_B(i); /* move from `b' to `a' */
  375. if (b < a)
  376. return getobjname(L, ci, b, name); /* get name for `b' */
  377. break;
  378. }
  379. case OP_GETTABLE: {
  380. *name = kname(p, GETARG_C(i));
  381. return "field";
  382. break;
  383. }
  384. case OP_SELF: {
  385. *name = kname(p, GETARG_C(i));
  386. return "method";
  387. break;
  388. }
  389. default: break;
  390. }
  391. }
  392. return NULL; /* no useful name found */
  393. }
  394. static Instruction getcurrentinstr (lua_State *L, CallInfo *ci) {
  395. if (ci == L->base_ci || !isLmark(ci))
  396. return (Instruction)(-1); /* not an active Lua function */
  397. else
  398. return ci_func(ci)->l.p->code[currentpc(L, ci)];
  399. }
  400. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  401. Instruction i;
  402. ci--; /* calling function */
  403. i = getcurrentinstr(L, ci);
  404. return (GET_OPCODE(i) == OP_CALL ? getobjname(L, ci, GETARG_A(i), name)
  405. : NULL); /* no useful name found */
  406. }
  407. /* only ANSI way to check whether a pointer points to an array */
  408. static int isinstack (CallInfo *ci, const TObject *o) {
  409. StkId p;
  410. for (p = ci->base; p < ci->top; p++)
  411. if (o == p) return 1;
  412. return 0;
  413. }
  414. void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
  415. const char *name;
  416. const char *t = luaT_typenames[ttype(o)];
  417. const char *kind = NULL;
  418. if (isinstack(L->ci, o))
  419. kind = getobjname(L, L->ci, o - L->ci->base, &name);
  420. if (kind)
  421. luaG_runerror(L, "attempt to %s %s `%s' (a %s value)",
  422. op, kind, name, t);
  423. else
  424. luaG_runerror(L, "attempt to %s a %s value", op, t);
  425. }
  426. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  427. if (ttype(p1) == LUA_TSTRING) p1 = p2;
  428. lua_assert(ttype(p1) != LUA_TSTRING);
  429. luaG_typeerror(L, p1, "concat");
  430. }
  431. void luaG_aritherror (lua_State *L, StkId p1, const TObject *p2) {
  432. TObject temp;
  433. if (luaV_tonumber(p1, &temp) == NULL)
  434. p2 = p1; /* first operand is wrong */
  435. luaG_typeerror(L, p2, "perform arithmetic on");
  436. }
  437. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  438. const char *t1 = luaT_typenames[ttype(p1)];
  439. const char *t2 = luaT_typenames[ttype(p2)];
  440. if (t1[2] == t2[2])
  441. luaG_runerror(L, "attempt to compare two %s values", t1);
  442. else
  443. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  444. }
  445. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  446. const char *msg;
  447. va_list argp;
  448. va_start(argp, fmt);
  449. msg = luaO_pushvfstring(L, fmt, argp);
  450. va_end(argp);
  451. if (isLmark(L->ci)) {
  452. char buff[LUA_IDSIZE];
  453. int line = currentline(L, L->ci);
  454. luaO_chunkid(buff, getstr(getluaproto(L->ci)->source), LUA_IDSIZE);
  455. msg = luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  456. }
  457. luaD_error(L, msg, LUA_ERRRUN);
  458. }