ldebug.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. ** $Id: ldebug.c,v 2.95 2013/05/06 17:21:59 roberto Exp $
  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. #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
  25. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  26. static int currentpc (CallInfo *ci) {
  27. lua_assert(isLua(ci));
  28. return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
  29. }
  30. static int currentline (CallInfo *ci) {
  31. return getfuncline(ci_func(ci)->p, currentpc(ci));
  32. }
  33. /*
  34. ** this function can be called asynchronous (e.g. during a signal)
  35. */
  36. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  37. if (func == NULL || mask == 0) { /* turn off hooks? */
  38. mask = 0;
  39. func = NULL;
  40. }
  41. if (isLua(L->ci))
  42. L->oldpc = L->ci->u.l.savedpc;
  43. L->hook = func;
  44. L->basehookcount = count;
  45. resethookcount(L);
  46. L->hookmask = cast_byte(mask);
  47. return 1;
  48. }
  49. LUA_API lua_Hook lua_gethook (lua_State *L) {
  50. return L->hook;
  51. }
  52. LUA_API int lua_gethookmask (lua_State *L) {
  53. return L->hookmask;
  54. }
  55. LUA_API int lua_gethookcount (lua_State *L) {
  56. return L->basehookcount;
  57. }
  58. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  59. int status;
  60. CallInfo *ci;
  61. if (level < 0) return 0; /* invalid (negative) level */
  62. lua_lock(L);
  63. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
  64. level--;
  65. if (level == 0 && ci != &L->base_ci) { /* level found? */
  66. status = 1;
  67. ar->i_ci = ci;
  68. }
  69. else status = 0; /* no such level */
  70. lua_unlock(L);
  71. return status;
  72. }
  73. static const char *upvalname (Proto *p, int uv) {
  74. TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
  75. if (s == NULL) return "?";
  76. else return getstr(s);
  77. }
  78. static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
  79. int nparams = clLvalue(ci->func)->p->numparams;
  80. if (n >= ci->u.l.base - ci->func - nparams)
  81. return NULL; /* no such vararg */
  82. else {
  83. *pos = ci->func + nparams + n;
  84. return "(*vararg)"; /* generic name for any vararg */
  85. }
  86. }
  87. static const char *findlocal (lua_State *L, CallInfo *ci, int n,
  88. StkId *pos) {
  89. const char *name = NULL;
  90. StkId base;
  91. if (isLua(ci)) {
  92. if (n < 0) /* access to vararg values? */
  93. return findvararg(ci, -n, pos);
  94. else {
  95. base = ci->u.l.base;
  96. name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
  97. }
  98. }
  99. else
  100. base = ci->func + 1;
  101. if (name == NULL) { /* no 'standard' name? */
  102. StkId limit = (ci == L->ci) ? L->top : ci->next->func;
  103. if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
  104. name = "(*temporary)"; /* generic name for any valid slot */
  105. else
  106. return NULL; /* no name */
  107. }
  108. *pos = base + (n - 1);
  109. return name;
  110. }
  111. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  112. const char *name;
  113. lua_lock(L);
  114. if (ar == NULL) { /* information about non-active function? */
  115. if (!isLfunction(L->top - 1)) /* not a Lua function? */
  116. name = NULL;
  117. else /* consider live variables at function start (parameters) */
  118. name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
  119. }
  120. else { /* active function; get information through 'ar' */
  121. StkId pos = 0; /* to avoid warnings */
  122. name = findlocal(L, ar->i_ci, n, &pos);
  123. if (name) {
  124. setobj2s(L, L->top, pos);
  125. api_incr_top(L);
  126. }
  127. }
  128. lua_unlock(L);
  129. return name;
  130. }
  131. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  132. StkId pos = 0; /* to avoid warnings */
  133. const char *name = findlocal(L, ar->i_ci, n, &pos);
  134. lua_lock(L);
  135. if (name)
  136. setobjs2s(L, pos, L->top - 1);
  137. L->top--; /* pop value */
  138. lua_unlock(L);
  139. return name;
  140. }
  141. static void funcinfo (lua_Debug *ar, Closure *cl) {
  142. if (noLuaClosure(cl)) {
  143. ar->source = "=[C]";
  144. ar->linedefined = -1;
  145. ar->lastlinedefined = -1;
  146. ar->what = "C";
  147. }
  148. else {
  149. Proto *p = cl->l.p;
  150. ar->source = p->source ? getstr(p->source) : "=?";
  151. ar->linedefined = p->linedefined;
  152. ar->lastlinedefined = p->lastlinedefined;
  153. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  154. }
  155. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  156. }
  157. static void collectvalidlines (lua_State *L, Closure *f) {
  158. if (noLuaClosure(f)) {
  159. setnilvalue(L->top);
  160. api_incr_top(L);
  161. }
  162. else {
  163. int i;
  164. TValue v;
  165. int *lineinfo = f->l.p->lineinfo;
  166. Table *t = luaH_new(L); /* new table to store active lines */
  167. sethvalue(L, L->top, t); /* push it on stack */
  168. api_incr_top(L);
  169. setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
  170. for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
  171. luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
  172. }
  173. }
  174. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  175. Closure *f, CallInfo *ci) {
  176. int status = 1;
  177. for (; *what; what++) {
  178. switch (*what) {
  179. case 'S': {
  180. funcinfo(ar, f);
  181. break;
  182. }
  183. case 'l': {
  184. ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
  185. break;
  186. }
  187. case 'u': {
  188. ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
  189. if (noLuaClosure(f)) {
  190. ar->isvararg = 1;
  191. ar->nparams = 0;
  192. }
  193. else {
  194. ar->isvararg = f->l.p->is_vararg;
  195. ar->nparams = f->l.p->numparams;
  196. }
  197. break;
  198. }
  199. case 't': {
  200. ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
  201. break;
  202. }
  203. case 'n': {
  204. /* calling function is a known Lua function? */
  205. if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
  206. ar->namewhat = getfuncname(L, ci->previous, &ar->name);
  207. else
  208. ar->namewhat = NULL;
  209. if (ar->namewhat == NULL) {
  210. ar->namewhat = ""; /* not found */
  211. ar->name = NULL;
  212. }
  213. break;
  214. }
  215. case 'L':
  216. case 'f': /* handled by lua_getinfo */
  217. break;
  218. default: status = 0; /* invalid option */
  219. }
  220. }
  221. return status;
  222. }
  223. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  224. int status;
  225. Closure *cl;
  226. CallInfo *ci;
  227. StkId func;
  228. lua_lock(L);
  229. if (*what == '>') {
  230. ci = NULL;
  231. func = L->top - 1;
  232. api_check(L, ttisfunction(func), "function expected");
  233. what++; /* skip the '>' */
  234. L->top--; /* pop function */
  235. }
  236. else {
  237. ci = ar->i_ci;
  238. func = ci->func;
  239. lua_assert(ttisfunction(ci->func));
  240. }
  241. cl = ttisclosure(func) ? clvalue(func) : NULL;
  242. status = auxgetinfo(L, what, ar, cl, ci);
  243. if (strchr(what, 'f')) {
  244. setobjs2s(L, L->top, func);
  245. api_incr_top(L);
  246. }
  247. if (strchr(what, 'L'))
  248. collectvalidlines(L, cl);
  249. lua_unlock(L);
  250. return status;
  251. }
  252. /*
  253. ** {======================================================
  254. ** Symbolic Execution
  255. ** =======================================================
  256. */
  257. static const char *getobjname (Proto *p, int lastpc, int reg,
  258. const char **name);
  259. /*
  260. ** find a "name" for the RK value 'c'
  261. */
  262. static void kname (Proto *p, int pc, int c, const char **name) {
  263. if (ISK(c)) { /* is 'c' a constant? */
  264. TValue *kvalue = &p->k[INDEXK(c)];
  265. if (ttisstring(kvalue)) { /* literal constant? */
  266. *name = svalue(kvalue); /* it is its own name */
  267. return;
  268. }
  269. /* else no reasonable name found */
  270. }
  271. else { /* 'c' is a register */
  272. const char *what = getobjname(p, pc, c, name); /* search for 'c' */
  273. if (what && *what == 'c') { /* found a constant name? */
  274. return; /* 'name' already filled */
  275. }
  276. /* else no reasonable name found */
  277. }
  278. *name = "?"; /* no reasonable name found */
  279. }
  280. static int filterpc (int pc, int jmptarget) {
  281. if (pc < jmptarget) /* is code conditional (inside a jump)? */
  282. return -1; /* cannot know who sets that register */
  283. else return pc; /* current position sets that register */
  284. }
  285. /*
  286. ** try to find last instruction before 'lastpc' that modified register 'reg'
  287. */
  288. static int findsetreg (Proto *p, int lastpc, int reg) {
  289. int pc;
  290. int setreg = -1; /* keep last instruction that changed 'reg' */
  291. int jmptarget = 0; /* any code before this address is conditional */
  292. for (pc = 0; pc < lastpc; pc++) {
  293. Instruction i = p->code[pc];
  294. OpCode op = GET_OPCODE(i);
  295. int a = GETARG_A(i);
  296. switch (op) {
  297. case OP_LOADNIL: {
  298. int b = GETARG_B(i);
  299. if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
  300. setreg = filterpc(pc, jmptarget);
  301. break;
  302. }
  303. case OP_TFORCALL: {
  304. if (reg >= a + 2) /* affect all regs above its base */
  305. setreg = filterpc(pc, jmptarget);
  306. break;
  307. }
  308. case OP_CALL:
  309. case OP_TAILCALL: {
  310. if (reg >= a) /* affect all registers above base */
  311. setreg = filterpc(pc, jmptarget);
  312. break;
  313. }
  314. case OP_JMP: {
  315. int b = GETARG_sBx(i);
  316. int dest = pc + 1 + b;
  317. /* jump is forward and do not skip `lastpc'? */
  318. if (pc < dest && dest <= lastpc) {
  319. if (dest > jmptarget)
  320. jmptarget = dest; /* update 'jmptarget' */
  321. }
  322. break;
  323. }
  324. default:
  325. if (testAMode(op) && reg == a) /* any instruction that set A */
  326. setreg = filterpc(pc, jmptarget);
  327. break;
  328. }
  329. }
  330. return setreg;
  331. }
  332. static const char *getobjname (Proto *p, int lastpc, int reg,
  333. const char **name) {
  334. int pc;
  335. *name = luaF_getlocalname(p, reg + 1, lastpc);
  336. if (*name) /* is a local? */
  337. return "local";
  338. /* else try symbolic execution */
  339. pc = findsetreg(p, lastpc, reg);
  340. if (pc != -1) { /* could find instruction? */
  341. Instruction i = p->code[pc];
  342. OpCode op = GET_OPCODE(i);
  343. switch (op) {
  344. case OP_MOVE: {
  345. int b = GETARG_B(i); /* move from 'b' to 'a' */
  346. if (b < GETARG_A(i))
  347. return getobjname(p, pc, b, name); /* get name for 'b' */
  348. break;
  349. }
  350. case OP_GETTABUP:
  351. case OP_GETTABLE: {
  352. int k = GETARG_C(i); /* key index */
  353. int t = GETARG_B(i); /* table index */
  354. const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
  355. ? luaF_getlocalname(p, t + 1, pc)
  356. : upvalname(p, t);
  357. kname(p, pc, k, name);
  358. return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
  359. }
  360. case OP_GETUPVAL: {
  361. *name = upvalname(p, GETARG_B(i));
  362. return "upvalue";
  363. }
  364. case OP_LOADK:
  365. case OP_LOADKX: {
  366. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  367. : GETARG_Ax(p->code[pc + 1]);
  368. if (ttisstring(&p->k[b])) {
  369. *name = svalue(&p->k[b]);
  370. return "constant";
  371. }
  372. break;
  373. }
  374. case OP_SELF: {
  375. int k = GETARG_C(i); /* key index */
  376. kname(p, pc, k, name);
  377. return "method";
  378. }
  379. default: break; /* go through to return NULL */
  380. }
  381. }
  382. return NULL; /* could not find reasonable name */
  383. }
  384. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  385. TMS tm;
  386. Proto *p = ci_func(ci)->p; /* calling function */
  387. int pc = currentpc(ci); /* calling instruction index */
  388. Instruction i = p->code[pc]; /* calling instruction */
  389. switch (GET_OPCODE(i)) {
  390. case OP_CALL:
  391. case OP_TAILCALL: /* get function name */
  392. return getobjname(p, pc, GETARG_A(i), name);
  393. case OP_TFORCALL: { /* for iterator */
  394. *name = "for iterator";
  395. return "for iterator";
  396. }
  397. /* all other instructions can call only through metamethods */
  398. case OP_SELF:
  399. case OP_GETTABUP:
  400. case OP_GETTABLE: tm = TM_INDEX; break;
  401. case OP_SETTABUP:
  402. case OP_SETTABLE: tm = TM_NEWINDEX; break;
  403. case OP_EQ: tm = TM_EQ; break;
  404. case OP_ADD: tm = TM_ADD; break;
  405. case OP_SUB: tm = TM_SUB; break;
  406. case OP_MUL: tm = TM_MUL; break;
  407. case OP_DIV: tm = TM_DIV; break;
  408. case OP_IDIV: tm = TM_IDIV; break;
  409. case OP_MOD: tm = TM_MOD; break;
  410. case OP_POW: tm = TM_POW; break;
  411. case OP_UNM: tm = TM_UNM; break;
  412. case OP_LEN: tm = TM_LEN; break;
  413. case OP_LT: tm = TM_LT; break;
  414. case OP_LE: tm = TM_LE; break;
  415. case OP_CONCAT: tm = TM_CONCAT; break;
  416. default:
  417. return NULL; /* else no useful name can be found */
  418. }
  419. *name = getstr(G(L)->tmname[tm]);
  420. return "metamethod";
  421. }
  422. /* }====================================================== */
  423. /*
  424. ** only ANSI way to check whether a pointer points to an array
  425. ** (used only for error messages, so efficiency is not a big concern)
  426. */
  427. static int isinstack (CallInfo *ci, const TValue *o) {
  428. StkId p;
  429. for (p = ci->u.l.base; p < ci->top; p++)
  430. if (o == p) return 1;
  431. return 0;
  432. }
  433. static const char *getupvalname (CallInfo *ci, const TValue *o,
  434. const char **name) {
  435. LClosure *c = ci_func(ci);
  436. int i;
  437. for (i = 0; i < c->nupvalues; i++) {
  438. if (c->upvals[i]->v == o) {
  439. *name = upvalname(c->p, i);
  440. return "upvalue";
  441. }
  442. }
  443. return NULL;
  444. }
  445. static const char *varinfo (lua_State *L, const TValue *o) {
  446. const char *name;
  447. CallInfo *ci = L->ci;
  448. const char *kind = NULL;
  449. if (isLua(ci)) {
  450. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  451. if (!kind && isinstack(ci, o)) /* no? try a register */
  452. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  453. cast_int(o - ci->u.l.base), &name);
  454. }
  455. return (kind) ? luaO_pushfstring(L, " (%s " LUA_QS ")", kind, name) : "";
  456. }
  457. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  458. const char *t = objtypename(o);
  459. luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
  460. }
  461. l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
  462. if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
  463. lua_assert(!ttisstring(p1) && !ttisnumber(p1));
  464. luaG_typeerror(L, p1, "concatenate");
  465. }
  466. l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  467. lua_Number temp;
  468. if (!tonumber(p1, &temp))
  469. p2 = p1; /* first operand is wrong */
  470. luaG_typeerror(L, p2, "perform arithmetic on");
  471. }
  472. l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
  473. lua_Integer temp;
  474. if (!tointeger(p1, &temp))
  475. p2 = p1;
  476. luaG_runerror(L, "attempt to convert an out of range float%s to an integer",
  477. varinfo(L, p2));
  478. }
  479. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  480. const char *t1 = objtypename(p1);
  481. const char *t2 = objtypename(p2);
  482. if (t1 == t2)
  483. luaG_runerror(L, "attempt to compare two %s values", t1);
  484. else
  485. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  486. }
  487. static void addinfo (lua_State *L, const char *msg) {
  488. CallInfo *ci = L->ci;
  489. if (isLua(ci)) { /* is Lua code? */
  490. char buff[LUA_IDSIZE]; /* add file:line information */
  491. int line = currentline(ci);
  492. TString *src = ci_func(ci)->p->source;
  493. if (src)
  494. luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
  495. else { /* no source available; use "?" instead */
  496. buff[0] = '?'; buff[1] = '\0';
  497. }
  498. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  499. }
  500. }
  501. l_noret luaG_errormsg (lua_State *L) {
  502. if (L->errfunc != 0) { /* is there an error handling function? */
  503. StkId errfunc = restorestack(L, L->errfunc);
  504. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  505. setobjs2s(L, L->top, L->top - 1); /* move argument */
  506. setobjs2s(L, L->top - 1, errfunc); /* push function */
  507. L->top++;
  508. luaD_call(L, L->top - 2, 1, 0); /* call it */
  509. }
  510. luaD_throw(L, LUA_ERRRUN);
  511. }
  512. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  513. va_list argp;
  514. va_start(argp, fmt);
  515. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  516. va_end(argp);
  517. luaG_errormsg(L);
  518. }
  519. void luaG_traceexec (lua_State *L) {
  520. CallInfo *ci = L->ci;
  521. lu_byte mask = L->hookmask;
  522. int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
  523. if (counthook)
  524. resethookcount(L); /* reset count */
  525. if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
  526. ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
  527. return; /* do not call hook again (VM yielded, so it did not move) */
  528. }
  529. if (counthook)
  530. luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
  531. if (mask & LUA_MASKLINE) {
  532. Proto *p = ci_func(ci)->p;
  533. int npc = pcRel(ci->u.l.savedpc, p);
  534. int newline = getfuncline(p, npc);
  535. if (npc == 0 || /* call linehook when enter a new function, */
  536. ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
  537. newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
  538. luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
  539. }
  540. L->oldpc = ci->u.l.savedpc;
  541. if (L->status == LUA_YIELD) { /* did hook yield? */
  542. if (counthook)
  543. L->hookcount = 1; /* undo decrement to zero */
  544. ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
  545. ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
  546. ci->func = L->top - 1; /* protect stack below results */
  547. luaD_throw(L, LUA_YIELD);
  548. }
  549. }