ldebug.c 11 KB

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