ldebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. ** $Id: ldebug.c,v 2.48 2009/04/27 18:58:31 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stddef.h>
  8. #include <string.h>
  9. #define ldebug_c
  10. #define LUA_CORE
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "lcode.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lobject.h"
  18. #include "lopcodes.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lvm.h"
  24. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  25. static int currentpc (CallInfo *ci) {
  26. if (!isLua(ci)) return -1; /* function is not a Lua function? */
  27. return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
  28. }
  29. static int currentline (CallInfo *ci) {
  30. int pc = currentpc(ci);
  31. if (pc < 0)
  32. return -1; /* only active lua functions have current-line information */
  33. else
  34. return getline(ci_func(ci)->l.p, pc);
  35. }
  36. /*
  37. ** this function can be called asynchronous (e.g. during a signal)
  38. */
  39. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  40. if (func == NULL || mask == 0) { /* turn off hooks? */
  41. mask = 0;
  42. func = NULL;
  43. }
  44. L->oldpc = NULL;
  45. L->hook = func;
  46. L->basehookcount = count;
  47. resethookcount(L);
  48. L->hookmask = cast_byte(mask);
  49. return 1;
  50. }
  51. LUA_API lua_Hook lua_gethook (lua_State *L) {
  52. return L->hook;
  53. }
  54. LUA_API int lua_gethookmask (lua_State *L) {
  55. return L->hookmask;
  56. }
  57. LUA_API int lua_gethookcount (lua_State *L) {
  58. return L->basehookcount;
  59. }
  60. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  61. int status;
  62. CallInfo *ci;
  63. lua_lock(L);
  64. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) {
  65. level--;
  66. if (isLua(ci)) /* Lua function? */
  67. level -= ci->u.l.tailcalls; /* skip lost tail calls */
  68. }
  69. if (level == 0 && ci != &L->base_ci) { /* level found? */
  70. status = 1;
  71. ar->i_ci = ci;
  72. }
  73. else if (level < 0) { /* level is of a lost tail call? */
  74. status = 1;
  75. ar->i_ci = NULL;
  76. }
  77. else status = 0; /* no such level */
  78. lua_unlock(L);
  79. return status;
  80. }
  81. static Proto *getluaproto (CallInfo *ci) {
  82. return (isLua(ci) ? ci_func(ci)->l.p : NULL);
  83. }
  84. static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
  85. const char *name;
  86. Proto *fp = getluaproto(ci);
  87. if (fp && (name = luaF_getlocalname(fp, n, currentpc(ci))) != NULL)
  88. return name; /* is a local variable in a Lua function */
  89. else {
  90. StkId limit = (ci == L->ci) ? L->top : ci->next->func;
  91. if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
  92. return "(*temporary)";
  93. else
  94. return NULL;
  95. }
  96. }
  97. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  98. CallInfo *ci = ar->i_ci;
  99. const char *name = findlocal(L, ci, n);
  100. lua_lock(L);
  101. if (name) {
  102. setobj2s(L, L->top, ci->base + (n - 1));
  103. api_incr_top(L);
  104. }
  105. lua_unlock(L);
  106. return name;
  107. }
  108. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  109. CallInfo *ci = ar->i_ci;
  110. const char *name = findlocal(L, ci, n);
  111. lua_lock(L);
  112. if (name)
  113. setobjs2s(L, ci->base + (n - 1), L->top - 1);
  114. L->top--; /* pop value */
  115. lua_unlock(L);
  116. return name;
  117. }
  118. static void funcinfo (lua_Debug *ar, Closure *cl) {
  119. if (cl->c.isC) {
  120. ar->source = "=[C]";
  121. ar->linedefined = -1;
  122. ar->lastlinedefined = -1;
  123. ar->what = "C";
  124. }
  125. else {
  126. ar->source = getstr(cl->l.p->source);
  127. ar->linedefined = cl->l.p->linedefined;
  128. ar->lastlinedefined = cl->l.p->lastlinedefined;
  129. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  130. }
  131. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  132. }
  133. static void info_tailcall (lua_Debug *ar) {
  134. ar->name = NULL;
  135. ar->namewhat = "";
  136. ar->what = "tail";
  137. ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
  138. ar->source = "=(tail call)";
  139. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  140. ar->nups = 0;
  141. }
  142. static void collectvalidlines (lua_State *L, Closure *f) {
  143. if (f == NULL || f->c.isC) {
  144. setnilvalue(L->top);
  145. incr_top(L);
  146. }
  147. else {
  148. int i;
  149. int *lineinfo = f->l.p->lineinfo;
  150. Table *t = luaH_new(L);
  151. sethvalue(L, L->top, t);
  152. incr_top(L);
  153. for (i=0; i<f->l.p->sizelineinfo; i++)
  154. setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
  155. }
  156. }
  157. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  158. Closure *f, CallInfo *ci) {
  159. int status = 1;
  160. if (f == NULL) {
  161. info_tailcall(ar);
  162. return status;
  163. }
  164. for (; *what; what++) {
  165. switch (*what) {
  166. case 'S': {
  167. funcinfo(ar, f);
  168. break;
  169. }
  170. case 'l': {
  171. ar->currentline = (ci) ? currentline(ci) : -1;
  172. break;
  173. }
  174. case 'u': {
  175. ar->nups = f->c.nupvalues;
  176. break;
  177. }
  178. case 'n': {
  179. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  180. if (ar->namewhat == NULL) {
  181. ar->namewhat = ""; /* not found */
  182. ar->name = NULL;
  183. }
  184. break;
  185. }
  186. case 'L':
  187. case 'f': /* handled by lua_getinfo */
  188. break;
  189. default: status = 0; /* invalid option */
  190. }
  191. }
  192. return status;
  193. }
  194. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  195. int status;
  196. Closure *f = NULL;
  197. CallInfo *ci = NULL;
  198. lua_lock(L);
  199. if (*what == '>') {
  200. StkId func = L->top - 1;
  201. luai_apicheck(L, ttisfunction(func));
  202. what++; /* skip the '>' */
  203. f = clvalue(func);
  204. L->top--; /* pop function */
  205. }
  206. else if (ar->i_ci != NULL) { /* no tail call? */
  207. ci = ar->i_ci;
  208. lua_assert(ttisfunction(ci->func));
  209. f = clvalue(ci->func);
  210. }
  211. status = auxgetinfo(L, what, ar, f, ci);
  212. if (strchr(what, 'f')) {
  213. if (f == NULL) setnilvalue(L->top);
  214. else setclvalue(L, L->top, f);
  215. incr_top(L);
  216. }
  217. if (strchr(what, 'L'))
  218. collectvalidlines(L, f);
  219. lua_unlock(L);
  220. return status;
  221. }
  222. /*
  223. ** {======================================================
  224. ** Symbolic Execution
  225. ** =======================================================
  226. */
  227. static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
  228. int pc;
  229. int last; /* stores position of last instruction that changed `reg' */
  230. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  231. for (pc = 0; pc < lastpc; pc++) {
  232. 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. switch (getOpMode(op)) {
  238. case iABC: {
  239. b = GETARG_B(i);
  240. c = GETARG_C(i);
  241. break;
  242. }
  243. case iABx: {
  244. b = GETARG_Bx(i);
  245. break;
  246. }
  247. case iAsBx: {
  248. b = GETARG_sBx(i);
  249. break;
  250. }
  251. case iAx: break;
  252. }
  253. if (testAMode(op)) {
  254. if (a == reg) last = pc; /* change register `a' */
  255. }
  256. switch (op) {
  257. case OP_LOADNIL: {
  258. if (a <= reg && reg <= b)
  259. last = pc; /* set registers from `a' to `b' */
  260. break;
  261. }
  262. case OP_SELF: {
  263. if (reg == a+1) last = pc;
  264. break;
  265. }
  266. case OP_TFORCALL: {
  267. if (reg >= a+2) last = pc; /* affect all regs above its base */
  268. break;
  269. }
  270. case OP_TFORLOOP:
  271. case OP_FORLOOP:
  272. case OP_FORPREP:
  273. case OP_JMP: {
  274. int dest = pc+1+b;
  275. /* not full check and jump is forward and do not skip `lastpc'? */
  276. if (reg != NO_REG && pc < dest && dest <= lastpc)
  277. pc += b; /* do the jump */
  278. break;
  279. }
  280. case OP_CALL:
  281. case OP_TAILCALL: {
  282. if (reg >= a) last = pc; /* affect all registers above base */
  283. break;
  284. }
  285. case OP_CLOSURE: {
  286. int nup = pt->p[b]->nups;
  287. pc += nup; /* do not 'execute' pseudo-instructions */
  288. break;
  289. }
  290. case OP_VARARG: {
  291. b--; /* ??? */
  292. break;
  293. }
  294. default: break;
  295. }
  296. }
  297. return pt->code[last];
  298. }
  299. #undef check
  300. #undef checkreg
  301. /* }====================================================== */
  302. static const char *kname (Proto *p, int c) {
  303. if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
  304. return svalue(&p->k[INDEXK(c)]);
  305. else
  306. return "?";
  307. }
  308. static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
  309. const char **name) {
  310. if (isLua(ci)) { /* a Lua function? */
  311. Proto *p = ci_func(ci)->l.p;
  312. int pc = currentpc(ci);
  313. Instruction i;
  314. *name = luaF_getlocalname(p, stackpos+1, pc);
  315. if (*name) /* is a local? */
  316. return "local";
  317. i = symbexec(p, pc, stackpos); /* try symbolic execution */
  318. lua_assert(pc != -1);
  319. switch (GET_OPCODE(i)) {
  320. case OP_GETGLOBAL: {
  321. int g = GETARG_Bx(i); /* global index */
  322. lua_assert(ttisstring(&p->k[g]));
  323. *name = svalue(&p->k[g]);
  324. return "global";
  325. }
  326. case OP_MOVE: {
  327. int a = GETARG_A(i);
  328. int b = GETARG_B(i); /* move from `b' to `a' */
  329. if (b < a)
  330. return getobjname(L, ci, b, name); /* get name for `b' */
  331. break;
  332. }
  333. case OP_GETTABLE: {
  334. int k = GETARG_C(i); /* key index */
  335. *name = kname(p, k);
  336. return "field";
  337. }
  338. case OP_GETUPVAL: {
  339. int u = GETARG_B(i); /* upvalue index */
  340. *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
  341. return "upvalue";
  342. }
  343. case OP_SELF: {
  344. int k = GETARG_C(i); /* key index */
  345. *name = kname(p, k);
  346. return "method";
  347. }
  348. default: break;
  349. }
  350. }
  351. return NULL; /* no useful name found */
  352. }
  353. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  354. TMS tm = 0;
  355. Instruction i;
  356. if ((isLua(ci) && ci->u.l.tailcalls > 0) || !isLua(ci->previous))
  357. return NULL; /* calling function is not Lua (or is unknown) */
  358. ci = ci->previous; /* calling function */
  359. i = ci_func(ci)->l.p->code[currentpc(ci)];
  360. switch (GET_OPCODE(i)) {
  361. case OP_CALL:
  362. case OP_TAILCALL:
  363. case OP_TFORLOOP:
  364. return getobjname(L, ci, GETARG_A(i), name);
  365. case OP_GETGLOBAL:
  366. case OP_SELF:
  367. case OP_GETTABLE: tm = TM_INDEX; break;
  368. case OP_SETGLOBAL:
  369. case OP_SETTABLE: tm = TM_NEWINDEX; break;
  370. case OP_EQ: tm = TM_EQ; break;
  371. case OP_ADD: tm = TM_ADD; break;
  372. case OP_SUB: tm = TM_SUB; break;
  373. case OP_MUL: tm = TM_MUL; break;
  374. case OP_DIV: tm = TM_DIV; break;
  375. case OP_MOD: tm = TM_MOD; break;
  376. case OP_POW: tm = TM_POW; break;
  377. case OP_UNM: tm = TM_UNM; break;
  378. case OP_LEN: tm = TM_LEN; break;
  379. case OP_LT: tm = TM_LT; break;
  380. case OP_LE: tm = TM_LE; break;
  381. case OP_CONCAT: tm = TM_CONCAT; break;
  382. default:
  383. return NULL; /* else no useful name can be found */
  384. }
  385. *name = getstr(G(L)->tmname[tm]);
  386. return "metamethod";
  387. }
  388. /* only ANSI way to check whether a pointer points to an array */
  389. static int isinstack (CallInfo *ci, const TValue *o) {
  390. StkId p;
  391. for (p = ci->base; p < ci->top; p++)
  392. if (o == p) return 1;
  393. return 0;
  394. }
  395. void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  396. const char *name = NULL;
  397. const char *t = luaT_typenames[ttype(o)];
  398. const char *kind = (isinstack(L->ci, o)) ?
  399. getobjname(L, L->ci, cast_int(o - L->base), &name) :
  400. NULL;
  401. if (kind)
  402. luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
  403. op, kind, name, t);
  404. else
  405. luaG_runerror(L, "attempt to %s a %s value", op, t);
  406. }
  407. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  408. if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
  409. lua_assert(!ttisstring(p1) && !ttisnumber(p2));
  410. luaG_typeerror(L, p1, "concatenate");
  411. }
  412. void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  413. TValue temp;
  414. if (luaV_tonumber(p1, &temp) == NULL)
  415. p2 = p1; /* first operand is wrong */
  416. luaG_typeerror(L, p2, "perform arithmetic on");
  417. }
  418. int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  419. const char *t1 = luaT_typenames[ttype(p1)];
  420. const char *t2 = luaT_typenames[ttype(p2)];
  421. if (t1[2] == t2[2])
  422. luaG_runerror(L, "attempt to compare two %s values", t1);
  423. else
  424. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  425. return 0;
  426. }
  427. static void addinfo (lua_State *L, const char *msg) {
  428. CallInfo *ci = L->ci;
  429. if (isLua(ci)) { /* is Lua code? */
  430. char buff[LUA_IDSIZE]; /* add file:line information */
  431. int line = currentline(ci);
  432. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  433. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  434. }
  435. }
  436. void luaG_errormsg (lua_State *L) {
  437. if (L->errfunc != 0) { /* is there an error handling function? */
  438. StkId errfunc = restorestack(L, L->errfunc);
  439. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  440. setobjs2s(L, L->top, L->top - 1); /* move argument */
  441. setobjs2s(L, L->top - 1, errfunc); /* push function */
  442. incr_top(L);
  443. luaD_call(L, L->top - 2, 1, 0); /* call it */
  444. }
  445. luaD_throw(L, LUA_ERRRUN);
  446. }
  447. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  448. va_list argp;
  449. va_start(argp, fmt);
  450. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  451. va_end(argp);
  452. luaG_errormsg(L);
  453. }