ldebug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. ** $Id: ldebug.c,v 2.90.1.2 2013/05/06 17:20:22 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 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. case OP_TEST: {
  325. if (reg == a) /* jumped code can change 'a' */
  326. setreg = filterpc(pc, jmptarget);
  327. break;
  328. }
  329. default:
  330. if (testAMode(op) && reg == a) /* any instruction that set A */
  331. setreg = filterpc(pc, jmptarget);
  332. break;
  333. }
  334. }
  335. return setreg;
  336. }
  337. static const char *getobjname (Proto *p, int lastpc, int reg,
  338. const char **name) {
  339. int pc;
  340. *name = luaF_getlocalname(p, reg + 1, lastpc);
  341. if (*name) /* is a local? */
  342. return "local";
  343. /* else try symbolic execution */
  344. pc = findsetreg(p, lastpc, reg);
  345. if (pc != -1) { /* could find instruction? */
  346. Instruction i = p->code[pc];
  347. OpCode op = GET_OPCODE(i);
  348. switch (op) {
  349. case OP_MOVE: {
  350. int b = GETARG_B(i); /* move from 'b' to 'a' */
  351. if (b < GETARG_A(i))
  352. return getobjname(p, pc, b, name); /* get name for 'b' */
  353. break;
  354. }
  355. case OP_GETTABUP:
  356. case OP_GETTABLE: {
  357. int k = GETARG_C(i); /* key index */
  358. int t = GETARG_B(i); /* table index */
  359. const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
  360. ? luaF_getlocalname(p, t + 1, pc)
  361. : upvalname(p, t);
  362. kname(p, pc, k, name);
  363. return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
  364. }
  365. case OP_GETUPVAL: {
  366. *name = upvalname(p, GETARG_B(i));
  367. return "upvalue";
  368. }
  369. case OP_LOADK:
  370. case OP_LOADKX: {
  371. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  372. : GETARG_Ax(p->code[pc + 1]);
  373. if (ttisstring(&p->k[b])) {
  374. *name = svalue(&p->k[b]);
  375. return "constant";
  376. }
  377. break;
  378. }
  379. case OP_SELF: {
  380. int k = GETARG_C(i); /* key index */
  381. kname(p, pc, k, name);
  382. return "method";
  383. }
  384. default: break; /* go through to return NULL */
  385. }
  386. }
  387. return NULL; /* could not find reasonable name */
  388. }
  389. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  390. TMS tm;
  391. Proto *p = ci_func(ci)->p; /* calling function */
  392. int pc = currentpc(ci); /* calling instruction index */
  393. Instruction i = p->code[pc]; /* calling instruction */
  394. switch (GET_OPCODE(i)) {
  395. case OP_CALL:
  396. case OP_TAILCALL: /* get function name */
  397. return getobjname(p, pc, GETARG_A(i), name);
  398. case OP_TFORCALL: { /* for iterator */
  399. *name = "for iterator";
  400. return "for iterator";
  401. }
  402. /* all other instructions can call only through metamethods */
  403. case OP_SELF:
  404. case OP_GETTABUP:
  405. case OP_GETTABLE: tm = TM_INDEX; break;
  406. case OP_SETTABUP:
  407. case OP_SETTABLE: tm = TM_NEWINDEX; break;
  408. case OP_EQ: tm = TM_EQ; break;
  409. case OP_ADD: tm = TM_ADD; break;
  410. case OP_SUB: tm = TM_SUB; break;
  411. case OP_MUL: tm = TM_MUL; break;
  412. case OP_DIV: tm = TM_DIV; break;
  413. case OP_MOD: tm = TM_MOD; break;
  414. case OP_POW: tm = TM_POW; break;
  415. case OP_UNM: tm = TM_UNM; break;
  416. case OP_LEN: tm = TM_LEN; break;
  417. case OP_LT: tm = TM_LT; break;
  418. case OP_LE: tm = TM_LE; break;
  419. case OP_CONCAT: tm = TM_CONCAT; break;
  420. default:
  421. return NULL; /* else no useful name can be found */
  422. }
  423. *name = getstr(G(L)->tmname[tm]);
  424. return "metamethod";
  425. }
  426. /* }====================================================== */
  427. /*
  428. ** only ANSI way to check whether a pointer points to an array
  429. ** (used only for error messages, so efficiency is not a big concern)
  430. */
  431. static int isinstack (CallInfo *ci, const TValue *o) {
  432. StkId p;
  433. for (p = ci->u.l.base; p < ci->top; p++)
  434. if (o == p) return 1;
  435. return 0;
  436. }
  437. static const char *getupvalname (CallInfo *ci, const TValue *o,
  438. const char **name) {
  439. LClosure *c = ci_func(ci);
  440. int i;
  441. for (i = 0; i < c->nupvalues; i++) {
  442. if (c->upvals[i]->v == o) {
  443. *name = upvalname(c->p, i);
  444. return "upvalue";
  445. }
  446. }
  447. return NULL;
  448. }
  449. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  450. CallInfo *ci = L->ci;
  451. const char *name = NULL;
  452. const char *t = objtypename(o);
  453. const char *kind = NULL;
  454. if (isLua(ci)) {
  455. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  456. if (!kind && isinstack(ci, o)) /* no? try a register */
  457. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  458. cast_int(o - ci->u.l.base), &name);
  459. }
  460. if (kind)
  461. luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
  462. op, kind, name, t);
  463. else
  464. luaG_runerror(L, "attempt to %s a %s value", op, t);
  465. }
  466. l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  467. if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
  468. lua_assert(!ttisstring(p1) && !ttisnumber(p1));
  469. luaG_typeerror(L, p1, "concatenate");
  470. }
  471. l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  472. TValue temp;
  473. if (luaV_tonumber(p1, &temp) == NULL)
  474. p2 = p1; /* first operand is wrong */
  475. luaG_typeerror(L, p2, "perform arithmetic on");
  476. }
  477. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  478. const char *t1 = objtypename(p1);
  479. const char *t2 = objtypename(p2);
  480. if (t1 == t2)
  481. luaG_runerror(L, "attempt to compare two %s values", t1);
  482. else
  483. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  484. }
  485. static void addinfo (lua_State *L, const char *msg) {
  486. CallInfo *ci = L->ci;
  487. if (isLua(ci)) { /* is Lua code? */
  488. char buff[LUA_IDSIZE]; /* add file:line information */
  489. int line = currentline(ci);
  490. TString *src = ci_func(ci)->p->source;
  491. if (src)
  492. luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
  493. else { /* no source available; use "?" instead */
  494. buff[0] = '?'; buff[1] = '\0';
  495. }
  496. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  497. }
  498. }
  499. l_noret luaG_errormsg (lua_State *L) {
  500. if (L->errfunc != 0) { /* is there an error handling function? */
  501. StkId errfunc = restorestack(L, L->errfunc);
  502. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  503. setobjs2s(L, L->top, L->top - 1); /* move argument */
  504. setobjs2s(L, L->top - 1, errfunc); /* push function */
  505. L->top++;
  506. luaD_call(L, L->top - 2, 1, 0); /* call it */
  507. }
  508. luaD_throw(L, LUA_ERRRUN);
  509. }
  510. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  511. va_list argp;
  512. va_start(argp, fmt);
  513. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  514. va_end(argp);
  515. luaG_errormsg(L);
  516. }