ldebug.c 16 KB

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