ldebug.c 11 KB

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