ldebug.c 14 KB

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