ldebug.c 12 KB

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