ldebug.c 14 KB

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