2
0

ldebug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. ** $Id: ldebug.c,v 2.87 2011/10/07 20:45:19 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. lua_assert(isLua(ci));
  27. return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
  28. }
  29. static int currentline (CallInfo *ci) {
  30. return getfuncline(ci_func(ci)->p, currentpc(ci));
  31. }
  32. /*
  33. ** this function can be called asynchronous (e.g. during a signal)
  34. */
  35. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  36. if (func == NULL || mask == 0) { /* turn off hooks? */
  37. mask = 0;
  38. func = NULL;
  39. }
  40. if (isLua(L->ci))
  41. L->oldpc = L->ci->u.l.savedpc;
  42. L->hook = func;
  43. L->basehookcount = count;
  44. resethookcount(L);
  45. L->hookmask = cast_byte(mask);
  46. return 1;
  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 (cl == NULL || cl->c.isC) {
  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 (f == NULL || f->c.isC) {
  158. setnilvalue(L->top);
  159. 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. 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 (f == NULL || f->c.isC) {
  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(L, 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. 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. /*
  280. ** try to find last instruction before 'lastpc' that modified register 'reg'
  281. */
  282. static int findsetreg (Proto *p, int lastpc, int reg) {
  283. int pc;
  284. int setreg = -1; /* keep last instruction that changed 'reg' */
  285. for (pc = 0; pc < lastpc; pc++) {
  286. Instruction i = p->code[pc];
  287. OpCode op = GET_OPCODE(i);
  288. int a = GETARG_A(i);
  289. switch (op) {
  290. case OP_LOADNIL: {
  291. int b = GETARG_B(i);
  292. if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
  293. setreg = pc;
  294. break;
  295. }
  296. case OP_TFORCALL: {
  297. if (reg >= a + 2) setreg = pc; /* affect all regs above its base */
  298. break;
  299. }
  300. case OP_CALL:
  301. case OP_TAILCALL: {
  302. if (reg >= a) setreg = pc; /* affect all registers above base */
  303. break;
  304. }
  305. case OP_JMP: {
  306. int b = GETARG_sBx(i);
  307. int dest = pc + 1 + b;
  308. /* jump is forward and do not skip `lastpc'? */
  309. if (pc < dest && dest <= lastpc)
  310. pc += b; /* do the jump */
  311. break;
  312. }
  313. case OP_TEST: {
  314. if (reg == a) setreg = pc; /* jumped code can change 'a' */
  315. break;
  316. }
  317. default:
  318. if (testAMode(op) && reg == a) /* any instruction that set A */
  319. setreg = pc;
  320. break;
  321. }
  322. }
  323. return setreg;
  324. }
  325. static const char *getobjname (Proto *p, int lastpc, int reg,
  326. const char **name) {
  327. int pc;
  328. *name = luaF_getlocalname(p, reg + 1, lastpc);
  329. if (*name) /* is a local? */
  330. return "local";
  331. /* else try symbolic execution */
  332. pc = findsetreg(p, lastpc, reg);
  333. if (pc != -1) { /* could find instruction? */
  334. Instruction i = p->code[pc];
  335. OpCode op = GET_OPCODE(i);
  336. switch (op) {
  337. case OP_MOVE: {
  338. int b = GETARG_B(i); /* move from 'b' to 'a' */
  339. if (b < GETARG_A(i))
  340. return getobjname(p, pc, b, name); /* get name for 'b' */
  341. break;
  342. }
  343. case OP_GETTABUP:
  344. case OP_GETTABLE: {
  345. int k = GETARG_C(i); /* key index */
  346. int t = GETARG_B(i); /* table index */
  347. const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
  348. ? luaF_getlocalname(p, t + 1, pc)
  349. : upvalname(p, t);
  350. kname(p, pc, k, name);
  351. return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
  352. }
  353. case OP_GETUPVAL: {
  354. *name = upvalname(p, GETARG_B(i));
  355. return "upvalue";
  356. }
  357. case OP_LOADK:
  358. case OP_LOADKX: {
  359. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  360. : GETARG_Ax(p->code[pc + 1]);
  361. if (ttisstring(&p->k[b])) {
  362. *name = svalue(&p->k[b]);
  363. return "constant";
  364. }
  365. break;
  366. }
  367. case OP_SELF: {
  368. int k = GETARG_C(i); /* key index */
  369. kname(p, pc, k, name);
  370. return "method";
  371. }
  372. default: break; /* go through to return NULL */
  373. }
  374. }
  375. return NULL; /* could not find reasonable name */
  376. }
  377. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  378. TMS tm;
  379. Proto *p = ci_func(ci)->p; /* calling function */
  380. int pc = currentpc(ci); /* calling instruction index */
  381. Instruction i = p->code[pc]; /* calling instruction */
  382. switch (GET_OPCODE(i)) {
  383. case OP_CALL:
  384. case OP_TAILCALL: /* get function name */
  385. return getobjname(p, pc, GETARG_A(i), name);
  386. case OP_TFORCALL: { /* for iterator */
  387. *name = "for iterator";
  388. return "for iterator";
  389. }
  390. /* all other instructions can call only through metamethods */
  391. case OP_SELF:
  392. case OP_GETTABUP:
  393. case OP_GETTABLE: tm = TM_INDEX; break;
  394. case OP_SETTABUP:
  395. case OP_SETTABLE: tm = TM_NEWINDEX; break;
  396. case OP_EQ: tm = TM_EQ; break;
  397. case OP_ADD: tm = TM_ADD; break;
  398. case OP_SUB: tm = TM_SUB; break;
  399. case OP_MUL: tm = TM_MUL; break;
  400. case OP_DIV: tm = TM_DIV; break;
  401. case OP_MOD: tm = TM_MOD; break;
  402. case OP_POW: tm = TM_POW; break;
  403. case OP_UNM: tm = TM_UNM; break;
  404. case OP_LEN: tm = TM_LEN; break;
  405. case OP_LT: tm = TM_LT; break;
  406. case OP_LE: tm = TM_LE; break;
  407. case OP_CONCAT: tm = TM_CONCAT; break;
  408. default:
  409. return NULL; /* else no useful name can be found */
  410. }
  411. *name = getstr(G(L)->tmname[tm]);
  412. return "metamethod";
  413. }
  414. /* }====================================================== */
  415. /*
  416. ** only ANSI way to check whether a pointer points to an array
  417. ** (used only for error messages, so efficiency is not a big concern)
  418. */
  419. static int isinstack (CallInfo *ci, const TValue *o) {
  420. StkId p;
  421. for (p = ci->u.l.base; p < ci->top; p++)
  422. if (o == p) return 1;
  423. return 0;
  424. }
  425. static const char *getupvalname (CallInfo *ci, const TValue *o,
  426. const char **name) {
  427. LClosure *c = ci_func(ci);
  428. int i;
  429. for (i = 0; i < c->nupvalues; i++) {
  430. if (c->upvals[i]->v == o) {
  431. *name = upvalname(c->p, i);
  432. return "upvalue";
  433. }
  434. }
  435. return NULL;
  436. }
  437. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  438. CallInfo *ci = L->ci;
  439. const char *name = NULL;
  440. const char *t = objtypename(o);
  441. const char *kind = NULL;
  442. if (isLua(ci)) {
  443. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  444. if (!kind && isinstack(ci, o)) /* no? try a register */
  445. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  446. cast_int(o - ci->u.l.base), &name);
  447. }
  448. if (kind)
  449. luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
  450. op, kind, name, t);
  451. else
  452. luaG_runerror(L, "attempt to %s a %s value", op, t);
  453. }
  454. l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  455. if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
  456. lua_assert(!ttisstring(p1) && !ttisnumber(p2));
  457. luaG_typeerror(L, p1, "concatenate");
  458. }
  459. l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  460. TValue temp;
  461. if (luaV_tonumber(p1, &temp) == NULL)
  462. p2 = p1; /* first operand is wrong */
  463. luaG_typeerror(L, p2, "perform arithmetic on");
  464. }
  465. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  466. const char *t1 = objtypename(p1);
  467. const char *t2 = objtypename(p2);
  468. if (t1 == t2)
  469. luaG_runerror(L, "attempt to compare two %s values", t1);
  470. else
  471. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  472. }
  473. static void addinfo (lua_State *L, const char *msg) {
  474. CallInfo *ci = L->ci;
  475. if (isLua(ci)) { /* is Lua code? */
  476. char buff[LUA_IDSIZE]; /* add file:line information */
  477. int line = currentline(ci);
  478. TString *src = ci_func(ci)->p->source;
  479. if (src)
  480. luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
  481. else { /* no source available; use "?" instead */
  482. buff[0] = '?'; buff[1] = '\0';
  483. }
  484. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  485. }
  486. }
  487. l_noret luaG_errormsg (lua_State *L) {
  488. if (L->errfunc != 0) { /* is there an error handling function? */
  489. StkId errfunc = restorestack(L, L->errfunc);
  490. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  491. setobjs2s(L, L->top, L->top - 1); /* move argument */
  492. setobjs2s(L, L->top - 1, errfunc); /* push function */
  493. incr_top(L);
  494. luaD_call(L, L->top - 2, 1, 0); /* call it */
  495. }
  496. luaD_throw(L, LUA_ERRRUN);
  497. }
  498. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  499. va_list argp;
  500. va_start(argp, fmt);
  501. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  502. va_end(argp);
  503. luaG_errormsg(L);
  504. }