ldebug.c 13 KB

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