2
0

ldebug.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. ** $Id: ldebug.c,v 2.100 2014/07/30 14:00:14 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. #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 void 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. }
  48. LUA_API lua_Hook lua_gethook (lua_State *L) {
  49. return L->hook;
  50. }
  51. LUA_API int lua_gethookmask (lua_State *L) {
  52. return L->hookmask;
  53. }
  54. LUA_API int lua_gethookcount (lua_State *L) {
  55. return L->basehookcount;
  56. }
  57. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  58. int status;
  59. CallInfo *ci;
  60. if (level < 0) return 0; /* invalid (negative) level */
  61. lua_lock(L);
  62. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
  63. level--;
  64. if (level == 0 && ci != &L->base_ci) { /* level found? */
  65. status = 1;
  66. ar->i_ci = ci;
  67. }
  68. else status = 0; /* no such level */
  69. lua_unlock(L);
  70. return status;
  71. }
  72. static const char *upvalname (Proto *p, int uv) {
  73. TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
  74. if (s == NULL) return "?";
  75. else return getstr(s);
  76. }
  77. static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
  78. int nparams = clLvalue(ci->func)->p->numparams;
  79. if (n >= ci->u.l.base - ci->func - nparams)
  80. return NULL; /* no such vararg */
  81. else {
  82. *pos = ci->func + nparams + n;
  83. return "(*vararg)"; /* generic name for any vararg */
  84. }
  85. }
  86. static const char *findlocal (lua_State *L, CallInfo *ci, int n,
  87. StkId *pos) {
  88. const char *name = NULL;
  89. StkId base;
  90. if (isLua(ci)) {
  91. if (n < 0) /* access to vararg values? */
  92. return findvararg(ci, -n, pos);
  93. else {
  94. base = ci->u.l.base;
  95. name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
  96. }
  97. }
  98. else
  99. base = ci->func + 1;
  100. if (name == NULL) { /* no 'standard' name? */
  101. StkId limit = (ci == L->ci) ? L->top : ci->next->func;
  102. if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
  103. name = "(*temporary)"; /* generic name for any valid slot */
  104. else
  105. return NULL; /* no name */
  106. }
  107. *pos = base + (n - 1);
  108. return name;
  109. }
  110. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  111. const char *name;
  112. lua_lock(L);
  113. if (ar == NULL) { /* information about non-active function? */
  114. if (!isLfunction(L->top - 1)) /* not a Lua function? */
  115. name = NULL;
  116. else /* consider live variables at function start (parameters) */
  117. name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
  118. }
  119. else { /* active function; get information through 'ar' */
  120. StkId pos = 0; /* to avoid warnings */
  121. name = findlocal(L, ar->i_ci, n, &pos);
  122. if (name) {
  123. setobj2s(L, L->top, pos);
  124. api_incr_top(L);
  125. }
  126. }
  127. lua_unlock(L);
  128. return name;
  129. }
  130. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  131. StkId pos = 0; /* to avoid warnings */
  132. const char *name = findlocal(L, ar->i_ci, n, &pos);
  133. lua_lock(L);
  134. if (name)
  135. setobjs2s(L, pos, L->top - 1);
  136. L->top--; /* pop value */
  137. lua_unlock(L);
  138. return name;
  139. }
  140. static void funcinfo (lua_Debug *ar, Closure *cl) {
  141. if (noLuaClosure(cl)) {
  142. ar->source = "=[C]";
  143. ar->linedefined = -1;
  144. ar->lastlinedefined = -1;
  145. ar->what = "C";
  146. }
  147. else {
  148. Proto *p = cl->l.p;
  149. ar->source = p->source ? getstr(p->source) : "=?";
  150. ar->linedefined = p->linedefined;
  151. ar->lastlinedefined = p->lastlinedefined;
  152. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  153. }
  154. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  155. }
  156. static void collectvalidlines (lua_State *L, Closure *f) {
  157. if (noLuaClosure(f)) {
  158. setnilvalue(L->top);
  159. api_incr_top(L);
  160. }
  161. else {
  162. int i;
  163. TValue v;
  164. int *lineinfo = f->l.p->lineinfo;
  165. Table *t = luaH_new(L); /* new table to store active lines */
  166. sethvalue(L, L->top, t); /* push it on stack */
  167. api_incr_top(L);
  168. setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
  169. for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
  170. luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
  171. }
  172. }
  173. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  174. Closure *f, CallInfo *ci) {
  175. int status = 1;
  176. for (; *what; what++) {
  177. switch (*what) {
  178. case 'S': {
  179. funcinfo(ar, f);
  180. break;
  181. }
  182. case 'l': {
  183. ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
  184. break;
  185. }
  186. case 'u': {
  187. ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
  188. if (noLuaClosure(f)) {
  189. ar->isvararg = 1;
  190. ar->nparams = 0;
  191. }
  192. else {
  193. ar->isvararg = f->l.p->is_vararg;
  194. ar->nparams = f->l.p->numparams;
  195. }
  196. break;
  197. }
  198. case 't': {
  199. ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
  200. break;
  201. }
  202. case 'n': {
  203. /* calling function is a known Lua function? */
  204. if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
  205. ar->namewhat = getfuncname(L, ci->previous, &ar->name);
  206. else
  207. ar->namewhat = NULL;
  208. if (ar->namewhat == NULL) {
  209. ar->namewhat = ""; /* not found */
  210. ar->name = NULL;
  211. }
  212. break;
  213. }
  214. case 'L':
  215. case 'f': /* handled by lua_getinfo */
  216. break;
  217. default: status = 0; /* invalid option */
  218. }
  219. }
  220. return status;
  221. }
  222. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  223. int status;
  224. Closure *cl;
  225. CallInfo *ci;
  226. StkId func;
  227. lua_lock(L);
  228. if (*what == '>') {
  229. ci = NULL;
  230. func = L->top - 1;
  231. api_check(ttisfunction(func), "function expected");
  232. what++; /* skip the '>' */
  233. L->top--; /* pop function */
  234. }
  235. else {
  236. ci = ar->i_ci;
  237. func = ci->func;
  238. lua_assert(ttisfunction(ci->func));
  239. }
  240. cl = ttisclosure(func) ? clvalue(func) : NULL;
  241. status = auxgetinfo(L, what, ar, cl, ci);
  242. if (strchr(what, 'f')) {
  243. setobjs2s(L, L->top, func);
  244. api_incr_top(L);
  245. }
  246. if (strchr(what, 'L'))
  247. collectvalidlines(L, cl);
  248. lua_unlock(L);
  249. return status;
  250. }
  251. /*
  252. ** {======================================================
  253. ** Symbolic Execution
  254. ** =======================================================
  255. */
  256. static const char *getobjname (Proto *p, int lastpc, int reg,
  257. const char **name);
  258. /*
  259. ** find a "name" for the RK value 'c'
  260. */
  261. static void kname (Proto *p, int pc, int c, const char **name) {
  262. if (ISK(c)) { /* is 'c' a constant? */
  263. TValue *kvalue = &p->k[INDEXK(c)];
  264. if (ttisstring(kvalue)) { /* literal constant? */
  265. *name = svalue(kvalue); /* it is its own name */
  266. return;
  267. }
  268. /* else no reasonable name found */
  269. }
  270. else { /* 'c' is a register */
  271. const char *what = getobjname(p, pc, c, name); /* search for 'c' */
  272. if (what && *what == 'c') { /* found a constant name? */
  273. return; /* 'name' already filled */
  274. }
  275. /* else no reasonable name found */
  276. }
  277. *name = "?"; /* no reasonable name found */
  278. }
  279. static int filterpc (int pc, int jmptarget) {
  280. if (pc < jmptarget) /* is code conditional (inside a jump)? */
  281. return -1; /* cannot know who sets that register */
  282. else return pc; /* current position sets that register */
  283. }
  284. /*
  285. ** try to find last instruction before 'lastpc' that modified register 'reg'
  286. */
  287. static int findsetreg (Proto *p, int lastpc, int reg) {
  288. int pc;
  289. int setreg = -1; /* keep last instruction that changed 'reg' */
  290. int jmptarget = 0; /* any code before this address is conditional */
  291. for (pc = 0; pc < lastpc; pc++) {
  292. Instruction i = p->code[pc];
  293. OpCode op = GET_OPCODE(i);
  294. int a = GETARG_A(i);
  295. switch (op) {
  296. case OP_LOADNIL: {
  297. int b = GETARG_B(i);
  298. if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
  299. setreg = filterpc(pc, jmptarget);
  300. break;
  301. }
  302. case OP_TFORCALL: {
  303. if (reg >= a + 2) /* affect all regs above its base */
  304. setreg = filterpc(pc, jmptarget);
  305. break;
  306. }
  307. case OP_CALL:
  308. case OP_TAILCALL: {
  309. if (reg >= a) /* affect all registers above base */
  310. setreg = filterpc(pc, jmptarget);
  311. break;
  312. }
  313. case OP_JMP: {
  314. int b = GETARG_sBx(i);
  315. int dest = pc + 1 + b;
  316. /* jump is forward and do not skip `lastpc'? */
  317. if (pc < dest && dest <= lastpc) {
  318. if (dest > jmptarget)
  319. jmptarget = dest; /* update 'jmptarget' */
  320. }
  321. break;
  322. }
  323. default:
  324. if (testAMode(op) && reg == a) /* any instruction that set A */
  325. setreg = filterpc(pc, jmptarget);
  326. break;
  327. }
  328. }
  329. return setreg;
  330. }
  331. static const char *getobjname (Proto *p, int lastpc, int reg,
  332. const char **name) {
  333. int pc;
  334. *name = luaF_getlocalname(p, reg + 1, lastpc);
  335. if (*name) /* is a local? */
  336. return "local";
  337. /* else try symbolic execution */
  338. pc = findsetreg(p, lastpc, reg);
  339. if (pc != -1) { /* could find instruction? */
  340. Instruction i = p->code[pc];
  341. OpCode op = GET_OPCODE(i);
  342. switch (op) {
  343. case OP_MOVE: {
  344. int b = GETARG_B(i); /* move from 'b' to 'a' */
  345. if (b < GETARG_A(i))
  346. return getobjname(p, pc, b, name); /* get name for 'b' */
  347. break;
  348. }
  349. case OP_GETTABUP:
  350. case OP_GETTABLE: {
  351. int k = GETARG_C(i); /* key index */
  352. int t = GETARG_B(i); /* table index */
  353. const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
  354. ? luaF_getlocalname(p, t + 1, pc)
  355. : upvalname(p, t);
  356. kname(p, pc, k, name);
  357. return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
  358. }
  359. case OP_GETUPVAL: {
  360. *name = upvalname(p, GETARG_B(i));
  361. return "upvalue";
  362. }
  363. case OP_LOADK:
  364. case OP_LOADKX: {
  365. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  366. : GETARG_Ax(p->code[pc + 1]);
  367. if (ttisstring(&p->k[b])) {
  368. *name = svalue(&p->k[b]);
  369. return "constant";
  370. }
  371. break;
  372. }
  373. case OP_SELF: {
  374. int k = GETARG_C(i); /* key index */
  375. kname(p, pc, k, name);
  376. return "method";
  377. }
  378. default: break; /* go through to return NULL */
  379. }
  380. }
  381. return NULL; /* could not find reasonable name */
  382. }
  383. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  384. TMS tm;
  385. Proto *p = ci_func(ci)->p; /* calling function */
  386. int pc = currentpc(ci); /* calling instruction index */
  387. Instruction i = p->code[pc]; /* calling instruction */
  388. switch (GET_OPCODE(i)) {
  389. case OP_CALL:
  390. case OP_TAILCALL: /* get function name */
  391. return getobjname(p, pc, GETARG_A(i), name);
  392. case OP_TFORCALL: { /* for iterator */
  393. *name = "for iterator";
  394. return "for iterator";
  395. }
  396. /* all other instructions can call only through metamethods */
  397. case OP_SELF:
  398. case OP_GETTABUP:
  399. case OP_GETTABLE: tm = TM_INDEX; break;
  400. case OP_SETTABUP:
  401. case OP_SETTABLE: tm = TM_NEWINDEX; break;
  402. case OP_EQ: tm = TM_EQ; break;
  403. case OP_ADD: tm = TM_ADD; break;
  404. case OP_SUB: tm = TM_SUB; break;
  405. case OP_MUL: tm = TM_MUL; break;
  406. case OP_DIV: tm = TM_DIV; break;
  407. case OP_IDIV: tm = TM_IDIV; break;
  408. case OP_MOD: tm = TM_MOD; break;
  409. case OP_POW: tm = TM_POW; break;
  410. case OP_UNM: tm = TM_UNM; break;
  411. case OP_LEN: tm = TM_LEN; break;
  412. case OP_LT: tm = TM_LT; break;
  413. case OP_LE: tm = TM_LE; break;
  414. case OP_CONCAT: tm = TM_CONCAT; break;
  415. default:
  416. return NULL; /* else no useful name can be found */
  417. }
  418. *name = getstr(G(L)->tmname[tm]);
  419. return "metamethod";
  420. }
  421. /* }====================================================== */
  422. /*
  423. ** only ANSI way to check whether a pointer points to an array
  424. ** (used only for error messages, so efficiency is not a big concern)
  425. */
  426. static int isinstack (CallInfo *ci, const TValue *o) {
  427. StkId p;
  428. for (p = ci->u.l.base; p < ci->top; p++)
  429. if (o == p) return 1;
  430. return 0;
  431. }
  432. static const char *getupvalname (CallInfo *ci, const TValue *o,
  433. const char **name) {
  434. LClosure *c = ci_func(ci);
  435. int i;
  436. for (i = 0; i < c->nupvalues; i++) {
  437. if (c->upvals[i]->v == o) {
  438. *name = upvalname(c->p, i);
  439. return "upvalue";
  440. }
  441. }
  442. return NULL;
  443. }
  444. static const char *varinfo (lua_State *L, const TValue *o) {
  445. const char *name;
  446. CallInfo *ci = L->ci;
  447. const char *kind = NULL;
  448. if (isLua(ci)) {
  449. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  450. if (!kind && isinstack(ci, o)) /* no? try a register */
  451. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  452. cast_int(o - ci->u.l.base), &name);
  453. }
  454. return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
  455. }
  456. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  457. const char *t = objtypename(o);
  458. luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
  459. }
  460. l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
  461. if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
  462. luaG_typeerror(L, p1, "concatenate");
  463. }
  464. l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  465. lua_Number temp;
  466. if (!tonumber(p1, &temp)) /* first operand is wrong? */
  467. p2 = p1; /* now second is wrong */
  468. luaG_typeerror(L, p2, "perform arithmetic on");
  469. }
  470. /*
  471. ** Error when both values are convertible to numbers, but not to integers
  472. */
  473. l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
  474. lua_Integer temp;
  475. if (!tointeger(p1, &temp))
  476. p2 = p1;
  477. luaG_runerror(L, "number%s has no integer representation", 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. }