ldebug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. ** $Id: ldebug.c,v 1.59 2001/02/02 15:13:05 roberto Exp roberto $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lua.h"
  8. #include "lapi.h"
  9. #include "lcode.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lobject.h"
  14. #include "lopcodes.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "ltable.h"
  18. #include "ltm.h"
  19. #include "luadebug.h"
  20. static const char *getfuncname (lua_State *L, StkId f, const char **name);
  21. static void setnormalized (TObject *d, const TObject *s) {
  22. if (ttype(s) == LUA_TMARK) {
  23. setclvalue(d, infovalue(s)->func);
  24. }
  25. else setobj(d, s);
  26. }
  27. static int isLmark (StkId o) {
  28. return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
  29. }
  30. LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
  31. lua_Hook oldhook;
  32. LUA_LOCK(L);
  33. oldhook = L->callhook;
  34. L->callhook = func;
  35. LUA_UNLOCK(L);
  36. return oldhook;
  37. }
  38. LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
  39. lua_Hook oldhook;
  40. LUA_LOCK(L);
  41. oldhook = L->linehook;
  42. L->linehook = func;
  43. LUA_UNLOCK(L);
  44. return oldhook;
  45. }
  46. static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
  47. int i;
  48. for (i = (top-1) - L->stack; i>=0; i--) {
  49. if (is_T_MARK(&L->stack[i])) {
  50. if (level == 0)
  51. return L->stack+i;
  52. level--;
  53. }
  54. }
  55. return NULL;
  56. }
  57. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  58. StkId f;
  59. int status;
  60. LUA_LOCK(L);
  61. f = aux_stackedfunction(L, level, L->top);
  62. if (f == NULL) status = 0; /* there is no such level */
  63. else {
  64. ar->_func = f;
  65. status = 1;
  66. }
  67. LUA_UNLOCK(L);
  68. return status;
  69. }
  70. static int nups (StkId f) {
  71. switch (ttype(f)) {
  72. case LUA_TFUNCTION:
  73. return clvalue(f)->nupvalues;
  74. case LUA_TMARK:
  75. return infovalue(f)->func->nupvalues;
  76. default:
  77. return 0;
  78. }
  79. }
  80. int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
  81. int refi;
  82. if (lineinfo == NULL || pc == -1)
  83. return -1; /* no line info or function is not active */
  84. refi = prefi ? *prefi : 0;
  85. if (lineinfo[refi] < 0)
  86. refline += -lineinfo[refi++];
  87. lua_assert(lineinfo[refi] >= 0);
  88. while (lineinfo[refi] > pc) {
  89. refline--;
  90. refi--;
  91. if (lineinfo[refi] < 0)
  92. refline -= -lineinfo[refi--];
  93. lua_assert(lineinfo[refi] >= 0);
  94. }
  95. for (;;) {
  96. int nextline = refline + 1;
  97. int nextref = refi + 1;
  98. if (lineinfo[nextref] < 0)
  99. nextline += -lineinfo[nextref++];
  100. lua_assert(lineinfo[nextref] >= 0);
  101. if (lineinfo[nextref] > pc)
  102. break;
  103. refline = nextline;
  104. refi = nextref;
  105. }
  106. if (prefi) *prefi = refi;
  107. return refline;
  108. }
  109. static int currentpc (StkId f) {
  110. CallInfo *ci = infovalue(f);
  111. lua_assert(isLmark(f));
  112. if (ci->pc)
  113. return (*ci->pc - ci->func->f.l->code) - 1;
  114. else
  115. return -1; /* function is not active */
  116. }
  117. static int currentline (StkId f) {
  118. if (!isLmark(f))
  119. return -1; /* only active lua functions have current-line information */
  120. else {
  121. CallInfo *ci = infovalue(f);
  122. int *lineinfo = ci->func->f.l->lineinfo;
  123. return luaG_getline(lineinfo, currentpc(f), 1, NULL);
  124. }
  125. }
  126. static Proto *getluaproto (StkId f) {
  127. return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
  128. }
  129. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  130. const char *name;
  131. StkId f;
  132. Proto *fp;
  133. LUA_LOCK(L);
  134. name = NULL;
  135. f = ar->_func;
  136. fp = getluaproto(f);
  137. if (fp) { /* `f' is a Lua function? */
  138. name = luaF_getlocalname(fp, n, currentpc(f));
  139. if (name)
  140. luaA_pushobject(L, (f+1)+(n-1)); /* push value */
  141. }
  142. LUA_UNLOCK(L);
  143. return name;
  144. }
  145. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  146. const char *name;
  147. StkId f;
  148. Proto *fp;
  149. LUA_LOCK(L);
  150. name = NULL;
  151. f = ar->_func;
  152. fp = getluaproto(f);
  153. L->top--; /* pop new value */
  154. if (fp) { /* `f' is a Lua function? */
  155. name = luaF_getlocalname(fp, n, currentpc(f));
  156. if (!name || name[0] == '(') /* `(' starts private locals */
  157. name = NULL;
  158. else
  159. setobj((f+1)+(n-1), L->top);
  160. }
  161. LUA_UNLOCK(L);
  162. return name;
  163. }
  164. static void infoLproto (lua_Debug *ar, Proto *f) {
  165. ar->source = f->source->str;
  166. ar->linedefined = f->lineDefined;
  167. ar->what = "Lua";
  168. }
  169. static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
  170. Closure *cl = NULL;
  171. switch (ttype(func)) {
  172. case LUA_TFUNCTION:
  173. cl = clvalue(func);
  174. break;
  175. case LUA_TMARK:
  176. cl = infovalue(func)->func;
  177. break;
  178. default:
  179. luaD_error(L, "value for `lua_getinfo' is not a function");
  180. }
  181. if (cl->isC) {
  182. ar->source = "=C";
  183. ar->linedefined = -1;
  184. ar->what = "C";
  185. }
  186. else
  187. infoLproto(ar, cl->f.l);
  188. luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
  189. if (ar->linedefined == 0)
  190. ar->what = "main";
  191. }
  192. static const char *travtagmethods (global_State *G, const TObject *o) {
  193. if (ttype(o) == LUA_TFUNCTION) {
  194. int e;
  195. for (e=0; e<TM_N; e++) {
  196. int t;
  197. for (t=0; t<G->ntag; t++)
  198. if (clvalue(o) == luaT_gettm(G, t, e))
  199. return luaT_eventname[e];
  200. }
  201. }
  202. return NULL;
  203. }
  204. static const char *travglobals (lua_State *L, const TObject *o) {
  205. Hash *g = L->gt;
  206. int i;
  207. for (i=0; i<g->size; i++) {
  208. if (luaO_equalObj(o, val(node(g, i))) &&
  209. ttype_key(node(g, i)) == LUA_TSTRING)
  210. return tsvalue_key(node(g, i))->str;
  211. }
  212. return NULL;
  213. }
  214. static void getname (lua_State *L, StkId f, lua_Debug *ar) {
  215. TObject o;
  216. setnormalized(&o, f);
  217. /* try to find a name for given function */
  218. if ((ar->name = travglobals(L, &o)) != NULL)
  219. ar->namewhat = "global";
  220. /* not found: try tag methods */
  221. else if ((ar->name = travtagmethods(G(L), &o)) != NULL)
  222. ar->namewhat = "tag-method";
  223. else ar->namewhat = ""; /* not found at all */
  224. }
  225. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  226. StkId func;
  227. int isactive;
  228. int status = 1;
  229. LUA_LOCK(L);
  230. isactive = (*what != '>');
  231. if (isactive)
  232. func = ar->_func;
  233. else {
  234. what++; /* skip the '>' */
  235. func = L->top - 1;
  236. }
  237. for (; *what; what++) {
  238. switch (*what) {
  239. case 'S': {
  240. funcinfo(L, ar, func);
  241. break;
  242. }
  243. case 'l': {
  244. ar->currentline = currentline(func);
  245. break;
  246. }
  247. case 'u': {
  248. ar->nups = nups(func);
  249. break;
  250. }
  251. case 'n': {
  252. ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
  253. if (ar->namewhat == NULL)
  254. getname(L, func, ar);
  255. break;
  256. }
  257. case 'f': {
  258. setnormalized(L->top, func);
  259. incr_top; /* push function */
  260. break;
  261. }
  262. default: status = 0; /* invalid option */
  263. }
  264. }
  265. if (!isactive) L->top--; /* pop function */
  266. LUA_UNLOCK(L);
  267. return status;
  268. }
  269. /*
  270. ** {======================================================
  271. ** Symbolic Execution
  272. ** =======================================================
  273. */
  274. static int pushpc (int *stack, int pc, int top, int n) {
  275. while (n--)
  276. stack[top++] = pc-1;
  277. return top;
  278. }
  279. static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
  280. int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
  281. const Instruction *code = pt->code;
  282. int top = pt->numparams;
  283. int pc = 0;
  284. if (pt->is_vararg) /* varargs? */
  285. top++; /* `arg' */
  286. while (pc < lastpc) {
  287. const Instruction i = code[pc++];
  288. lua_assert(0 <= top && top <= pt->maxstacksize);
  289. switch (GET_OPCODE(i)) {
  290. case OP_RETURN: {
  291. lua_assert(top >= GETARG_U(i));
  292. top = GETARG_U(i);
  293. break;
  294. }
  295. case OP_TAILCALL: {
  296. lua_assert(top >= GETARG_A(i));
  297. top = GETARG_B(i);
  298. break;
  299. }
  300. case OP_CALL: {
  301. int nresults = GETARG_B(i);
  302. if (nresults == MULT_RET) nresults = 1;
  303. lua_assert(top >= GETARG_A(i));
  304. top = pushpc(stack, pc, GETARG_A(i), nresults);
  305. break;
  306. }
  307. case OP_PUSHNIL: {
  308. top = pushpc(stack, pc, top, GETARG_U(i));
  309. break;
  310. }
  311. case OP_POP: {
  312. top -= GETARG_U(i);
  313. break;
  314. }
  315. case OP_SETTABLE:
  316. case OP_SETLIST: {
  317. top -= GETARG_B(i);
  318. break;
  319. }
  320. case OP_SETMAP: {
  321. top -= 2*GETARG_U(i);
  322. break;
  323. }
  324. case OP_CONCAT: {
  325. top -= GETARG_U(i);
  326. stack[top++] = pc-1;
  327. break;
  328. }
  329. case OP_CLOSURE: {
  330. top -= GETARG_B(i);
  331. stack[top++] = pc-1;
  332. break;
  333. }
  334. case OP_JMPONT:
  335. case OP_JMPONF: {
  336. int newpc = pc + GETARG_S(i);
  337. /* jump is forward and do not skip `lastpc'? */
  338. if (pc < newpc && newpc <= lastpc) {
  339. stack[top-1] = pc-1; /* value comes from `and'/`or' */
  340. pc = newpc; /* do the jump */
  341. }
  342. else
  343. top--; /* do not jump; pop value */
  344. break;
  345. }
  346. default: {
  347. OpCode op = GET_OPCODE(i);
  348. int push = (int)luaK_opproperties[op].push;
  349. int pop = (int)luaK_opproperties[op].pop;
  350. lua_assert(push != VD && pop != VD);
  351. lua_assert(0 <= top-pop && top+push <= pt->maxstacksize);
  352. top -= pop;
  353. top = pushpc(stack, pc, top, push);
  354. }
  355. }
  356. }
  357. return code[stack[stackpos]];
  358. }
  359. static const char *getobjname (lua_State *L, StkId obj, const char **name) {
  360. StkId func = aux_stackedfunction(L, 0, obj);
  361. if (!isLmark(func))
  362. return NULL; /* not an active Lua function */
  363. else {
  364. Proto *p = infovalue(func)->func->f.l;
  365. int pc = currentpc(func);
  366. int stackpos = obj - (func+1); /* func+1 == function base */
  367. Instruction i = luaG_symbexec(p, pc, stackpos);
  368. lua_assert(pc != -1);
  369. switch (GET_OPCODE(i)) {
  370. case OP_GETGLOBAL: {
  371. *name = p->kstr[GETARG_U(i)]->str;
  372. return "global";
  373. }
  374. case OP_GETLOCAL: {
  375. *name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
  376. lua_assert(*name);
  377. return "local";
  378. }
  379. case OP_PUSHSELF:
  380. case OP_GETDOTTED: {
  381. *name = p->kstr[GETARG_U(i)]->str;
  382. return "field";
  383. }
  384. default:
  385. return NULL; /* no useful name found */
  386. }
  387. }
  388. }
  389. static const char *getfuncname (lua_State *L, StkId f, const char **name) {
  390. StkId func = aux_stackedfunction(L, 0, f); /* calling function */
  391. if (!isLmark(func))
  392. return NULL; /* not an active Lua function */
  393. else {
  394. Proto *p = infovalue(func)->func->f.l;
  395. int pc = currentpc(func);
  396. Instruction i;
  397. if (pc == -1) return NULL; /* function is not activated */
  398. i = p->code[pc];
  399. switch (GET_OPCODE(i)) {
  400. case OP_CALL: case OP_TAILCALL:
  401. return getobjname(L, (func+1)+GETARG_A(i), name);
  402. default:
  403. return NULL; /* no useful name found */
  404. }
  405. }
  406. }
  407. /* }====================================================== */
  408. void luaG_typeerror (lua_State *L, StkId o, const char *op) {
  409. const char *name;
  410. const char *kind = getobjname(L, o, &name);
  411. const char *t = luaT_typename(G(L), o);
  412. if (kind)
  413. luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
  414. op, kind, name, t);
  415. else
  416. luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
  417. }
  418. void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
  419. if (ttype(p1) == t) p1++;
  420. lua_assert(ttype(p1) != t);
  421. luaG_typeerror(L, p1, op);
  422. }
  423. void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
  424. const char *t1 = luaT_typename(G(L), p1);
  425. const char *t2 = luaT_typename(G(L), p2);
  426. if (t1[2] == t2[2])
  427. luaO_verror(L, "attempt to compare two %.10s values", t1);
  428. else
  429. luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
  430. }