ldebug.c 14 KB

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