2
0

ldebug.c 15 KB

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