選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

584 行
15 KiB

  1. /*
  2. ** $Id: ldebug.c,v 2.89 2012/01/20 22:05:50 roberto Exp $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stdarg.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #define ldebug_c
  13. #define LUA_CORE
  14. #include "lua.h"
  15. #include "lapi.h"
  16. #include "lcode.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lfunc.h"
  20. #include "lobject.h"
  21. #include "lopcodes.h"
  22. #include "lstate.h"
  23. #include "lstring.h"
  24. #include "ltable.h"
  25. #include "ltm.h"
  26. #include "lvm.h"
  27. #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
  28. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  29. static int currentpc (CallInfo *ci) {
  30. lua_assert(isLua(ci));
  31. return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
  32. }
  33. static int currentline (CallInfo *ci) {
  34. return getfuncline(ci_func(ci)->p, currentpc(ci));
  35. }
  36. /*
  37. ** this function can be called asynchronous (e.g. during a signal)
  38. */
  39. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  40. if (func == NULL || mask == 0) { /* turn off hooks? */
  41. mask = 0;
  42. func = NULL;
  43. }
  44. if (isLua(L->ci))
  45. L->oldpc = L->ci->u.l.savedpc;
  46. L->hook = func;
  47. L->basehookcount = count;
  48. resethookcount(L);
  49. L->hookmask = cast_byte(mask);
  50. return 1;
  51. }
  52. LUA_API lua_Hook lua_gethook (lua_State *L) {
  53. return L->hook;
  54. }
  55. LUA_API int lua_gethookmask (lua_State *L) {
  56. return L->hookmask;
  57. }
  58. LUA_API int lua_gethookcount (lua_State *L) {
  59. return L->basehookcount;
  60. }
  61. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  62. int status;
  63. CallInfo *ci;
  64. if (level < 0) return 0; /* invalid (negative) level */
  65. lua_lock(L);
  66. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
  67. level--;
  68. if (level == 0 && ci != &L->base_ci) { /* level found? */
  69. status = 1;
  70. ar->i_ci = ci;
  71. }
  72. else status = 0; /* no such level */
  73. lua_unlock(L);
  74. return status;
  75. }
  76. static const char *upvalname (Proto *p, int uv) {
  77. TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
  78. if (s == NULL) return "?";
  79. else return getstr(s);
  80. }
  81. static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
  82. int nparams = clLvalue(ci->func)->p->numparams;
  83. if (n >= ci->u.l.base - ci->func - nparams)
  84. return NULL; /* no such vararg */
  85. else {
  86. *pos = ci->func + nparams + n;
  87. return "(*vararg)"; /* generic name for any vararg */
  88. }
  89. }
  90. static const char *findlocal (lua_State *L, CallInfo *ci, int n,
  91. StkId *pos) {
  92. const char *name = NULL;
  93. StkId base;
  94. if (isLua(ci)) {
  95. if (n < 0) /* access to vararg values? */
  96. return findvararg(ci, -n, pos);
  97. else {
  98. base = ci->u.l.base;
  99. name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
  100. }
  101. }
  102. else
  103. base = ci->func + 1;
  104. if (name == NULL) { /* no 'standard' name? */
  105. StkId limit = (ci == L->ci) ? L->top : ci->next->func;
  106. if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
  107. name = "(*temporary)"; /* generic name for any valid slot */
  108. else
  109. return NULL; /* no name */
  110. }
  111. *pos = base + (n - 1);
  112. return name;
  113. }
  114. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  115. const char *name;
  116. lua_lock(L);
  117. if (ar == NULL) { /* information about non-active function? */
  118. if (!isLfunction(L->top - 1)) /* not a Lua function? */
  119. name = NULL;
  120. else /* consider live variables at function start (parameters) */
  121. name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
  122. }
  123. else { /* active function; get information through 'ar' */
  124. StkId pos = 0; /* to avoid warnings */
  125. name = findlocal(L, ar->i_ci, n, &pos);
  126. if (name) {
  127. setobj2s(L, L->top, pos);
  128. api_incr_top(L);
  129. }
  130. }
  131. lua_unlock(L);
  132. return name;
  133. }
  134. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  135. StkId pos = 0; /* to avoid warnings */
  136. const char *name = findlocal(L, ar->i_ci, n, &pos);
  137. lua_lock(L);
  138. if (name)
  139. setobjs2s(L, pos, L->top - 1);
  140. L->top--; /* pop value */
  141. lua_unlock(L);
  142. return name;
  143. }
  144. static void funcinfo (lua_Debug *ar, Closure *cl) {
  145. if (noLuaClosure(cl)) {
  146. ar->source = "=[C]";
  147. ar->linedefined = -1;
  148. ar->lastlinedefined = -1;
  149. ar->what = "C";
  150. }
  151. else {
  152. Proto *p = cl->l.p;
  153. ar->source = p->source ? getstr(p->source) : "=?";
  154. ar->linedefined = p->linedefined;
  155. ar->lastlinedefined = p->lastlinedefined;
  156. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  157. }
  158. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  159. }
  160. static void collectvalidlines (lua_State *L, Closure *f) {
  161. if (noLuaClosure(f)) {
  162. setnilvalue(L->top);
  163. incr_top(L);
  164. }
  165. else {
  166. int i;
  167. TValue v;
  168. int *lineinfo = f->l.p->lineinfo;
  169. Table *t = luaH_new(L); /* new table to store active lines */
  170. sethvalue(L, L->top, t); /* push it on stack */
  171. incr_top(L);
  172. setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
  173. for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
  174. luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
  175. }
  176. }
  177. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  178. Closure *f, CallInfo *ci) {
  179. int status = 1;
  180. for (; *what; what++) {
  181. switch (*what) {
  182. case 'S': {
  183. funcinfo(ar, f);
  184. break;
  185. }
  186. case 'l': {
  187. ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
  188. break;
  189. }
  190. case 'u': {
  191. ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
  192. if (noLuaClosure(f)) {
  193. ar->isvararg = 1;
  194. ar->nparams = 0;
  195. }
  196. else {
  197. ar->isvararg = f->l.p->is_vararg;
  198. ar->nparams = f->l.p->numparams;
  199. }
  200. break;
  201. }
  202. case 't': {
  203. ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
  204. break;
  205. }
  206. case 'n': {
  207. /* calling function is a known Lua function? */
  208. if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
  209. ar->namewhat = getfuncname(L, ci->previous, &ar->name);
  210. else
  211. ar->namewhat = NULL;
  212. if (ar->namewhat == NULL) {
  213. ar->namewhat = ""; /* not found */
  214. ar->name = NULL;
  215. }
  216. break;
  217. }
  218. case 'L':
  219. case 'f': /* handled by lua_getinfo */
  220. break;
  221. default: status = 0; /* invalid option */
  222. }
  223. }
  224. return status;
  225. }
  226. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  227. int status;
  228. Closure *cl;
  229. CallInfo *ci;
  230. StkId func;
  231. lua_lock(L);
  232. if (*what == '>') {
  233. ci = NULL;
  234. func = L->top - 1;
  235. api_check(L, ttisfunction(func), "function expected");
  236. what++; /* skip the '>' */
  237. L->top--; /* pop function */
  238. }
  239. else {
  240. ci = ar->i_ci;
  241. func = ci->func;
  242. lua_assert(ttisfunction(ci->func));
  243. }
  244. cl = ttisclosure(func) ? clvalue(func) : NULL;
  245. status = auxgetinfo(L, what, ar, cl, ci);
  246. if (strchr(what, 'f')) {
  247. setobjs2s(L, L->top, func);
  248. incr_top(L);
  249. }
  250. if (strchr(what, 'L'))
  251. collectvalidlines(L, cl);
  252. lua_unlock(L);
  253. return status;
  254. }
  255. /*
  256. ** {======================================================
  257. ** Symbolic Execution
  258. ** =======================================================
  259. */
  260. static const char *getobjname (Proto *p, int lastpc, int reg,
  261. const char **name);
  262. /*
  263. ** find a "name" for the RK value 'c'
  264. */
  265. static void kname (Proto *p, int pc, int c, const char **name) {
  266. if (ISK(c)) { /* is 'c' a constant? */
  267. TValue *kvalue = &p->k[INDEXK(c)];
  268. if (ttisstring(kvalue)) { /* literal constant? */
  269. *name = svalue(kvalue); /* it is its own name */
  270. return;
  271. }
  272. /* else no reasonable name found */
  273. }
  274. else { /* 'c' is a register */
  275. const char *what = getobjname(p, pc, c, name); /* search for 'c' */
  276. if (what && *what == 'c') { /* found a constant name? */
  277. return; /* 'name' already filled */
  278. }
  279. /* else no reasonable name found */
  280. }
  281. *name = "?"; /* no reasonable name found */
  282. }
  283. /*
  284. ** try to find last instruction before 'lastpc' that modified register 'reg'
  285. */
  286. static int findsetreg (Proto *p, int lastpc, int reg) {
  287. int pc;
  288. int setreg = -1; /* keep last instruction that changed 'reg' */
  289. for (pc = 0; pc < lastpc; pc++) {
  290. Instruction i = p->code[pc];
  291. OpCode op = GET_OPCODE(i);
  292. int a = GETARG_A(i);
  293. switch (op) {
  294. case OP_LOADNIL: {
  295. int b = GETARG_B(i);
  296. if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
  297. setreg = pc;
  298. break;
  299. }
  300. case OP_TFORCALL: {
  301. if (reg >= a + 2) setreg = pc; /* affect all regs above its base */
  302. break;
  303. }
  304. case OP_CALL:
  305. case OP_TAILCALL: {
  306. if (reg >= a) setreg = pc; /* affect all registers above base */
  307. break;
  308. }
  309. case OP_JMP: {
  310. int b = GETARG_sBx(i);
  311. int dest = pc + 1 + b;
  312. /* jump is forward and do not skip `lastpc'? */
  313. if (pc < dest && dest <= lastpc)
  314. pc += b; /* do the jump */
  315. break;
  316. }
  317. case OP_TEST: {
  318. if (reg == a) setreg = pc; /* jumped code can change 'a' */
  319. break;
  320. }
  321. default:
  322. if (testAMode(op) && reg == a) /* any instruction that set A */
  323. setreg = pc;
  324. break;
  325. }
  326. }
  327. return setreg;
  328. }
  329. static const char *getobjname (Proto *p, int lastpc, int reg,
  330. const char **name) {
  331. int pc;
  332. *name = luaF_getlocalname(p, reg + 1, lastpc);
  333. if (*name) /* is a local? */
  334. return "local";
  335. /* else try symbolic execution */
  336. pc = findsetreg(p, lastpc, reg);
  337. if (pc != -1) { /* could find instruction? */
  338. Instruction i = p->code[pc];
  339. OpCode op = GET_OPCODE(i);
  340. switch (op) {
  341. case OP_MOVE: {
  342. int b = GETARG_B(i); /* move from 'b' to 'a' */
  343. if (b < GETARG_A(i))
  344. return getobjname(p, pc, b, name); /* get name for 'b' */
  345. break;
  346. }
  347. case OP_GETTABUP:
  348. case OP_GETTABLE: {
  349. int k = GETARG_C(i); /* key index */
  350. int t = GETARG_B(i); /* table index */
  351. const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
  352. ? luaF_getlocalname(p, t + 1, pc)
  353. : upvalname(p, t);
  354. kname(p, pc, k, name);
  355. return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
  356. }
  357. case OP_GETUPVAL: {
  358. *name = upvalname(p, GETARG_B(i));
  359. return "upvalue";
  360. }
  361. case OP_LOADK:
  362. case OP_LOADKX: {
  363. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  364. : GETARG_Ax(p->code[pc + 1]);
  365. if (ttisstring(&p->k[b])) {
  366. *name = svalue(&p->k[b]);
  367. return "constant";
  368. }
  369. break;
  370. }
  371. case OP_SELF: {
  372. int k = GETARG_C(i); /* key index */
  373. kname(p, pc, k, name);
  374. return "method";
  375. }
  376. default: break; /* go through to return NULL */
  377. }
  378. }
  379. return NULL; /* could not find reasonable name */
  380. }
  381. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  382. TMS tm;
  383. Proto *p = ci_func(ci)->p; /* calling function */
  384. int pc = currentpc(ci); /* calling instruction index */
  385. Instruction i = p->code[pc]; /* calling instruction */
  386. switch (GET_OPCODE(i)) {
  387. case OP_CALL:
  388. case OP_TAILCALL: /* get function name */
  389. return getobjname(p, pc, GETARG_A(i), name);
  390. case OP_TFORCALL: { /* for iterator */
  391. *name = "for iterator";
  392. return "for iterator";
  393. }
  394. /* all other instructions can call only through metamethods */
  395. case OP_SELF:
  396. case OP_GETTABUP:
  397. case OP_GETTABLE: tm = TM_INDEX; break;
  398. case OP_SETTABUP:
  399. case OP_SETTABLE: tm = TM_NEWINDEX; break;
  400. case OP_EQ: tm = TM_EQ; break;
  401. case OP_ADD: tm = TM_ADD; break;
  402. case OP_SUB: tm = TM_SUB; break;
  403. case OP_MUL: tm = TM_MUL; break;
  404. case OP_DIV: tm = TM_DIV; break;
  405. case OP_MOD: tm = TM_MOD; break;
  406. case OP_POW: tm = TM_POW; break;
  407. case OP_UNM: tm = TM_UNM; break;
  408. case OP_LEN: tm = TM_LEN; break;
  409. case OP_LT: tm = TM_LT; break;
  410. case OP_LE: tm = TM_LE; break;
  411. case OP_CONCAT: tm = TM_CONCAT; break;
  412. default:
  413. return NULL; /* else no useful name can be found */
  414. }
  415. *name = getstr(G(L)->tmname[tm]);
  416. return "metamethod";
  417. }
  418. /* }====================================================== */
  419. /*
  420. ** only ANSI way to check whether a pointer points to an array
  421. ** (used only for error messages, so efficiency is not a big concern)
  422. */
  423. static int isinstack (CallInfo *ci, const TValue *o) {
  424. StkId p;
  425. for (p = ci->u.l.base; p < ci->top; p++)
  426. if (o == p) return 1;
  427. return 0;
  428. }
  429. static const char *getupvalname (CallInfo *ci, const TValue *o,
  430. const char **name) {
  431. LClosure *c = ci_func(ci);
  432. int i;
  433. for (i = 0; i < c->nupvalues; i++) {
  434. if (c->upvals[i]->v == o) {
  435. *name = upvalname(c->p, i);
  436. return "upvalue";
  437. }
  438. }
  439. return NULL;
  440. }
  441. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  442. CallInfo *ci = L->ci;
  443. const char *name = NULL;
  444. const char *t = objtypename(o);
  445. const char *kind = NULL;
  446. if (isLua(ci)) {
  447. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  448. if (!kind && isinstack(ci, o)) /* no? try a register */
  449. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  450. cast_int(o - ci->u.l.base), &name);
  451. }
  452. if (kind)
  453. luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
  454. op, kind, name, t);
  455. else
  456. luaG_runerror(L, "attempt to %s a %s value", op, t);
  457. }
  458. l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  459. if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
  460. lua_assert(!ttisstring(p1) && !ttisnumber(p2));
  461. luaG_typeerror(L, p1, "concatenate");
  462. }
  463. l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  464. TValue temp;
  465. if (luaV_tonumber(p1, &temp) == NULL)
  466. p2 = p1; /* first operand is wrong */
  467. luaG_typeerror(L, p2, "perform arithmetic on");
  468. }
  469. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  470. const char *t1 = objtypename(p1);
  471. const char *t2 = objtypename(p2);
  472. if (t1 == t2)
  473. luaG_runerror(L, "attempt to compare two %s values", t1);
  474. else
  475. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  476. }
  477. static void addinfo (lua_State *L, const char *msg) {
  478. CallInfo *ci = L->ci;
  479. if (isLua(ci)) { /* is Lua code? */
  480. char buff[LUA_IDSIZE]; /* add file:line information */
  481. int line = currentline(ci);
  482. TString *src = ci_func(ci)->p->source;
  483. if (src)
  484. luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
  485. else { /* no source available; use "?" instead */
  486. buff[0] = '?'; buff[1] = '\0';
  487. }
  488. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  489. }
  490. }
  491. l_noret luaG_errormsg (lua_State *L) {
  492. if (L->errfunc != 0) { /* is there an error handling function? */
  493. StkId errfunc = restorestack(L, L->errfunc);
  494. if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  495. setobjs2s(L, L->top, L->top - 1); /* move argument */
  496. setobjs2s(L, L->top - 1, errfunc); /* push function */
  497. incr_top(L);
  498. luaD_call(L, L->top - 2, 1, 0); /* call it */
  499. }
  500. luaD_throw(L, LUA_ERRRUN);
  501. }
  502. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  503. va_list argp;
  504. va_start(argp, fmt);
  505. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  506. va_end(argp);
  507. luaG_errormsg(L);
  508. }