ldebug.c 14 KB

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