ldebug.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. ** $Id: ldebug.c $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ldebug_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stdarg.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "lcode.h"
  15. #include "ldebug.h"
  16. #include "ldo.h"
  17. #include "lfunc.h"
  18. #include "lobject.h"
  19. #include "lopcodes.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "ltm.h"
  24. #include "lvm.h"
  25. #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL)
  26. static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
  27. const char **name);
  28. static int currentpc (CallInfo *ci) {
  29. lua_assert(isLua(ci));
  30. return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
  31. }
  32. /*
  33. ** Get a "base line" to find the line corresponding to an instruction.
  34. ** Base lines are regularly placed at MAXIWTHABS intervals, so usually
  35. ** an integer division gets the right place. When the source file has
  36. ** large sequences of empty/comment lines, it may need extra entries,
  37. ** so the original estimate needs a correction.
  38. ** The assertion that the estimate is a lower bound for the correct base
  39. ** is valid as long as the debug info has been generated with the same
  40. ** value for MAXIWTHABS or smaller. (Previous releases use a little
  41. ** smaller value.)
  42. */
  43. static int getbaseline (const Proto *f, int pc, int *basepc) {
  44. if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
  45. *basepc = -1; /* start from the beginning */
  46. return f->linedefined;
  47. }
  48. else {
  49. int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
  50. /* estimate must be a lower bond of the correct base */
  51. lua_assert(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc);
  52. while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
  53. i++; /* low estimate; adjust it */
  54. *basepc = f->abslineinfo[i].pc;
  55. return f->abslineinfo[i].line;
  56. }
  57. }
  58. /*
  59. ** Get the line corresponding to instruction 'pc' in function 'f';
  60. ** first gets a base line and from there does the increments until
  61. ** the desired instruction.
  62. */
  63. int luaG_getfuncline (const Proto *f, int pc) {
  64. if (f->lineinfo == NULL) /* no debug information? */
  65. return -1;
  66. else {
  67. int basepc;
  68. int baseline = getbaseline(f, pc, &basepc);
  69. while (basepc++ < pc) { /* walk until given instruction */
  70. lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
  71. baseline += f->lineinfo[basepc]; /* correct line */
  72. }
  73. return baseline;
  74. }
  75. }
  76. static int getcurrentline (CallInfo *ci) {
  77. return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
  78. }
  79. /*
  80. ** Set 'trap' for all active Lua frames.
  81. ** This function can be called during a signal, under "reasonable"
  82. ** assumptions. A new 'ci' is completely linked in the list before it
  83. ** becomes part of the "active" list, and we assume that pointers are
  84. ** atomic; see comment in next function.
  85. ** (A compiler doing interprocedural optimizations could, theoretically,
  86. ** reorder memory writes in such a way that the list could be
  87. ** temporarily broken while inserting a new element. We simply assume it
  88. ** has no good reasons to do that.)
  89. */
  90. static void settraps (CallInfo *ci) {
  91. for (; ci != NULL; ci = ci->previous)
  92. if (isLua(ci))
  93. ci->u.l.trap = 1;
  94. }
  95. /*
  96. ** This function can be called during a signal, under "reasonable"
  97. ** assumptions.
  98. ** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount')
  99. ** are for debug only, and it is no problem if they get arbitrary
  100. ** values (causes at most one wrong hook call). 'hookmask' is an atomic
  101. ** value. We assume that pointers are atomic too (e.g., gcc ensures that
  102. ** for all platforms where it runs). Moreover, 'hook' is always checked
  103. ** before being called (see 'luaD_hook').
  104. */
  105. LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  106. if (func == NULL || mask == 0) { /* turn off hooks? */
  107. mask = 0;
  108. func = NULL;
  109. }
  110. L->hook = func;
  111. L->basehookcount = count;
  112. resethookcount(L);
  113. L->hookmask = cast_byte(mask);
  114. if (mask)
  115. settraps(L->ci); /* to trace inside 'luaV_execute' */
  116. }
  117. LUA_API lua_Hook lua_gethook (lua_State *L) {
  118. return L->hook;
  119. }
  120. LUA_API int lua_gethookmask (lua_State *L) {
  121. return L->hookmask;
  122. }
  123. LUA_API int lua_gethookcount (lua_State *L) {
  124. return L->basehookcount;
  125. }
  126. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  127. int status;
  128. CallInfo *ci;
  129. if (level < 0) return 0; /* invalid (negative) level */
  130. lua_lock(L);
  131. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
  132. level--;
  133. if (level == 0 && ci != &L->base_ci) { /* level found? */
  134. status = 1;
  135. ar->i_ci = ci;
  136. }
  137. else status = 0; /* no such level */
  138. lua_unlock(L);
  139. return status;
  140. }
  141. static const char *upvalname (const Proto *p, int uv) {
  142. TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
  143. if (s == NULL) return "?";
  144. else return getstr(s);
  145. }
  146. static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
  147. if (clLvalue(s2v(ci->func))->p->is_vararg) {
  148. int nextra = ci->u.l.nextraargs;
  149. if (n >= -nextra) { /* 'n' is negative */
  150. *pos = ci->func - nextra - (n + 1);
  151. return "(vararg)"; /* generic name for any vararg */
  152. }
  153. }
  154. return NULL; /* no such vararg */
  155. }
  156. const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
  157. StkId base = ci->func + 1;
  158. const char *name = NULL;
  159. if (isLua(ci)) {
  160. if (n < 0) /* access to vararg values? */
  161. return findvararg(ci, n, pos);
  162. else
  163. name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
  164. }
  165. if (name == NULL) { /* no 'standard' name? */
  166. StkId limit = (ci == L->ci) ? L->top : ci->next->func;
  167. if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
  168. /* generic name for any valid slot */
  169. name = isLua(ci) ? "(temporary)" : "(C temporary)";
  170. }
  171. else
  172. return NULL; /* no name */
  173. }
  174. if (pos)
  175. *pos = base + (n - 1);
  176. return name;
  177. }
  178. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  179. const char *name;
  180. lua_lock(L);
  181. if (ar == NULL) { /* information about non-active function? */
  182. if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */
  183. name = NULL;
  184. else /* consider live variables at function start (parameters) */
  185. name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0);
  186. }
  187. else { /* active function; get information through 'ar' */
  188. StkId pos = NULL; /* to avoid warnings */
  189. name = luaG_findlocal(L, ar->i_ci, n, &pos);
  190. if (name) {
  191. setobjs2s(L, L->top, pos);
  192. api_incr_top(L);
  193. }
  194. }
  195. lua_unlock(L);
  196. return name;
  197. }
  198. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  199. StkId pos = NULL; /* to avoid warnings */
  200. const char *name;
  201. lua_lock(L);
  202. name = luaG_findlocal(L, ar->i_ci, n, &pos);
  203. if (name) {
  204. setobjs2s(L, pos, L->top - 1);
  205. L->top--; /* pop value */
  206. }
  207. lua_unlock(L);
  208. return name;
  209. }
  210. static void funcinfo (lua_Debug *ar, Closure *cl) {
  211. if (noLuaClosure(cl)) {
  212. ar->source = "=[C]";
  213. ar->srclen = LL("=[C]");
  214. ar->linedefined = -1;
  215. ar->lastlinedefined = -1;
  216. ar->what = "C";
  217. }
  218. else {
  219. const Proto *p = cl->l.p;
  220. if (p->source) {
  221. ar->source = getstr(p->source);
  222. ar->srclen = tsslen(p->source);
  223. }
  224. else {
  225. ar->source = "=?";
  226. ar->srclen = LL("=?");
  227. }
  228. ar->linedefined = p->linedefined;
  229. ar->lastlinedefined = p->lastlinedefined;
  230. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  231. }
  232. luaO_chunkid(ar->short_src, ar->source, ar->srclen);
  233. }
  234. static int nextline (const Proto *p, int currentline, int pc) {
  235. if (p->lineinfo[pc] != ABSLINEINFO)
  236. return currentline + p->lineinfo[pc];
  237. else
  238. return luaG_getfuncline(p, pc);
  239. }
  240. static void collectvalidlines (lua_State *L, Closure *f) {
  241. if (noLuaClosure(f)) {
  242. setnilvalue(s2v(L->top));
  243. api_incr_top(L);
  244. }
  245. else {
  246. int i;
  247. TValue v;
  248. const Proto *p = f->l.p;
  249. int currentline = p->linedefined;
  250. Table *t = luaH_new(L); /* new table to store active lines */
  251. sethvalue2s(L, L->top, t); /* push it on stack */
  252. api_incr_top(L);
  253. setbtvalue(&v); /* boolean 'true' to be the value of all indices */
  254. for (i = 0; i < p->sizelineinfo; i++) { /* for all instructions */
  255. currentline = nextline(p, currentline, i); /* get its line */
  256. luaH_setint(L, t, currentline, &v); /* table[line] = true */
  257. }
  258. }
  259. }
  260. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  261. if (ci == NULL) /* no 'ci'? */
  262. return NULL; /* no info */
  263. else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */
  264. *name = "__gc";
  265. return "metamethod"; /* report it as such */
  266. }
  267. /* calling function is a known Lua function? */
  268. else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
  269. return funcnamefromcode(L, ci->previous, name);
  270. else return NULL; /* no way to find a name */
  271. }
  272. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  273. Closure *f, CallInfo *ci) {
  274. int status = 1;
  275. for (; *what; what++) {
  276. switch (*what) {
  277. case 'S': {
  278. funcinfo(ar, f);
  279. break;
  280. }
  281. case 'l': {
  282. ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
  283. break;
  284. }
  285. case 'u': {
  286. ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
  287. if (noLuaClosure(f)) {
  288. ar->isvararg = 1;
  289. ar->nparams = 0;
  290. }
  291. else {
  292. ar->isvararg = f->l.p->is_vararg;
  293. ar->nparams = f->l.p->numparams;
  294. }
  295. break;
  296. }
  297. case 't': {
  298. ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
  299. break;
  300. }
  301. case 'n': {
  302. ar->namewhat = getfuncname(L, ci, &ar->name);
  303. if (ar->namewhat == NULL) {
  304. ar->namewhat = ""; /* not found */
  305. ar->name = NULL;
  306. }
  307. break;
  308. }
  309. case 'r': {
  310. if (ci == NULL || !(ci->callstatus & CIST_TRAN))
  311. ar->ftransfer = ar->ntransfer = 0;
  312. else {
  313. ar->ftransfer = ci->u2.transferinfo.ftransfer;
  314. ar->ntransfer = ci->u2.transferinfo.ntransfer;
  315. }
  316. break;
  317. }
  318. case 'L':
  319. case 'f': /* handled by lua_getinfo */
  320. break;
  321. default: status = 0; /* invalid option */
  322. }
  323. }
  324. return status;
  325. }
  326. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  327. int status;
  328. Closure *cl;
  329. CallInfo *ci;
  330. TValue *func;
  331. lua_lock(L);
  332. if (*what == '>') {
  333. ci = NULL;
  334. func = s2v(L->top - 1);
  335. api_check(L, ttisfunction(func), "function expected");
  336. what++; /* skip the '>' */
  337. L->top--; /* pop function */
  338. }
  339. else {
  340. ci = ar->i_ci;
  341. func = s2v(ci->func);
  342. lua_assert(ttisfunction(func));
  343. }
  344. cl = ttisclosure(func) ? clvalue(func) : NULL;
  345. status = auxgetinfo(L, what, ar, cl, ci);
  346. if (strchr(what, 'f')) {
  347. setobj2s(L, L->top, func);
  348. api_incr_top(L);
  349. }
  350. if (strchr(what, 'L'))
  351. collectvalidlines(L, cl);
  352. lua_unlock(L);
  353. return status;
  354. }
  355. /*
  356. ** {======================================================
  357. ** Symbolic Execution
  358. ** =======================================================
  359. */
  360. static const char *getobjname (const Proto *p, int lastpc, int reg,
  361. const char **name);
  362. /*
  363. ** Find a "name" for the constant 'c'.
  364. */
  365. static void kname (const Proto *p, int c, const char **name) {
  366. TValue *kvalue = &p->k[c];
  367. *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?";
  368. }
  369. /*
  370. ** Find a "name" for the register 'c'.
  371. */
  372. static void rname (const Proto *p, int pc, int c, const char **name) {
  373. const char *what = getobjname(p, pc, c, name); /* search for 'c' */
  374. if (!(what && *what == 'c')) /* did not find a constant name? */
  375. *name = "?";
  376. }
  377. /*
  378. ** Find a "name" for a 'C' value in an RK instruction.
  379. */
  380. static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
  381. int c = GETARG_C(i); /* key index */
  382. if (GETARG_k(i)) /* is 'c' a constant? */
  383. kname(p, c, name);
  384. else /* 'c' is a register */
  385. rname(p, pc, c, name);
  386. }
  387. static int filterpc (int pc, int jmptarget) {
  388. if (pc < jmptarget) /* is code conditional (inside a jump)? */
  389. return -1; /* cannot know who sets that register */
  390. else return pc; /* current position sets that register */
  391. }
  392. /*
  393. ** Try to find last instruction before 'lastpc' that modified register 'reg'.
  394. */
  395. static int findsetreg (const Proto *p, int lastpc, int reg) {
  396. int pc;
  397. int setreg = -1; /* keep last instruction that changed 'reg' */
  398. int jmptarget = 0; /* any code before this address is conditional */
  399. if (testMMMode(GET_OPCODE(p->code[lastpc])))
  400. lastpc--; /* previous instruction was not actually executed */
  401. for (pc = 0; pc < lastpc; pc++) {
  402. Instruction i = p->code[pc];
  403. OpCode op = GET_OPCODE(i);
  404. int a = GETARG_A(i);
  405. int change; /* true if current instruction changed 'reg' */
  406. switch (op) {
  407. case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */
  408. int b = GETARG_B(i);
  409. change = (a <= reg && reg <= a + b);
  410. break;
  411. }
  412. case OP_TFORCALL: { /* affect all regs above its base */
  413. change = (reg >= a + 2);
  414. break;
  415. }
  416. case OP_CALL:
  417. case OP_TAILCALL: { /* affect all registers above base */
  418. change = (reg >= a);
  419. break;
  420. }
  421. case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */
  422. int b = GETARG_sJ(i);
  423. int dest = pc + 1 + b;
  424. /* jump does not skip 'lastpc' and is larger than current one? */
  425. if (dest <= lastpc && dest > jmptarget)
  426. jmptarget = dest; /* update 'jmptarget' */
  427. change = 0;
  428. break;
  429. }
  430. default: /* any instruction that sets A */
  431. change = (testAMode(op) && reg == a);
  432. break;
  433. }
  434. if (change)
  435. setreg = filterpc(pc, jmptarget);
  436. }
  437. return setreg;
  438. }
  439. /*
  440. ** Check whether table being indexed by instruction 'i' is the
  441. ** environment '_ENV'
  442. */
  443. static const char *gxf (const Proto *p, int pc, Instruction i, int isup) {
  444. int t = GETARG_B(i); /* table index */
  445. const char *name; /* name of indexed variable */
  446. if (isup) /* is an upvalue? */
  447. name = upvalname(p, t);
  448. else
  449. getobjname(p, pc, t, &name);
  450. return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
  451. }
  452. static const char *getobjname (const Proto *p, int lastpc, int reg,
  453. const char **name) {
  454. int pc;
  455. *name = luaF_getlocalname(p, reg + 1, lastpc);
  456. if (*name) /* is a local? */
  457. return "local";
  458. /* else try symbolic execution */
  459. pc = findsetreg(p, lastpc, reg);
  460. if (pc != -1) { /* could find instruction? */
  461. Instruction i = p->code[pc];
  462. OpCode op = GET_OPCODE(i);
  463. switch (op) {
  464. case OP_MOVE: {
  465. int b = GETARG_B(i); /* move from 'b' to 'a' */
  466. if (b < GETARG_A(i))
  467. return getobjname(p, pc, b, name); /* get name for 'b' */
  468. break;
  469. }
  470. case OP_GETTABUP: {
  471. int k = GETARG_C(i); /* key index */
  472. kname(p, k, name);
  473. return gxf(p, pc, i, 1);
  474. }
  475. case OP_GETTABLE: {
  476. int k = GETARG_C(i); /* key index */
  477. rname(p, pc, k, name);
  478. return gxf(p, pc, i, 0);
  479. }
  480. case OP_GETI: {
  481. *name = "integer index";
  482. return "field";
  483. }
  484. case OP_GETFIELD: {
  485. int k = GETARG_C(i); /* key index */
  486. kname(p, k, name);
  487. return gxf(p, pc, i, 0);
  488. }
  489. case OP_GETUPVAL: {
  490. *name = upvalname(p, GETARG_B(i));
  491. return "upvalue";
  492. }
  493. case OP_LOADK:
  494. case OP_LOADKX: {
  495. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  496. : GETARG_Ax(p->code[pc + 1]);
  497. if (ttisstring(&p->k[b])) {
  498. *name = svalue(&p->k[b]);
  499. return "constant";
  500. }
  501. break;
  502. }
  503. case OP_SELF: {
  504. rkname(p, pc, i, name);
  505. return "method";
  506. }
  507. default: break; /* go through to return NULL */
  508. }
  509. }
  510. return NULL; /* could not find reasonable name */
  511. }
  512. /*
  513. ** Try to find a name for a function based on the code that called it.
  514. ** (Only works when function was called by a Lua function.)
  515. ** Returns what the name is (e.g., "for iterator", "method",
  516. ** "metamethod") and sets '*name' to point to the name.
  517. */
  518. static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
  519. const char **name) {
  520. TMS tm = (TMS)0; /* (initial value avoids warnings) */
  521. const Proto *p = ci_func(ci)->p; /* calling function */
  522. int pc = currentpc(ci); /* calling instruction index */
  523. Instruction i = p->code[pc]; /* calling instruction */
  524. if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
  525. *name = "?";
  526. return "hook";
  527. }
  528. switch (GET_OPCODE(i)) {
  529. case OP_CALL:
  530. case OP_TAILCALL:
  531. return getobjname(p, pc, GETARG_A(i), name); /* get function name */
  532. case OP_TFORCALL: { /* for iterator */
  533. *name = "for iterator";
  534. return "for iterator";
  535. }
  536. /* other instructions can do calls through metamethods */
  537. case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
  538. case OP_GETI: case OP_GETFIELD:
  539. tm = TM_INDEX;
  540. break;
  541. case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
  542. tm = TM_NEWINDEX;
  543. break;
  544. case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
  545. tm = cast(TMS, GETARG_C(i));
  546. break;
  547. }
  548. case OP_UNM: tm = TM_UNM; break;
  549. case OP_BNOT: tm = TM_BNOT; break;
  550. case OP_LEN: tm = TM_LEN; break;
  551. case OP_CONCAT: tm = TM_CONCAT; break;
  552. case OP_EQ: tm = TM_EQ; break;
  553. /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
  554. case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
  555. case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
  556. case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
  557. default:
  558. return NULL; /* cannot find a reasonable name */
  559. }
  560. *name = getstr(G(L)->tmname[tm]) + 2;
  561. return "metamethod";
  562. }
  563. /* }====================================================== */
  564. /*
  565. ** The subtraction of two potentially unrelated pointers is
  566. ** not ISO C, but it should not crash a program; the subsequent
  567. ** checks are ISO C and ensure a correct result.
  568. */
  569. static int isinstack (CallInfo *ci, const TValue *o) {
  570. StkId base = ci->func + 1;
  571. ptrdiff_t i = cast(StkId, o) - base;
  572. return (0 <= i && i < (ci->top - base) && s2v(base + i) == o);
  573. }
  574. /*
  575. ** Checks whether value 'o' came from an upvalue. (That can only happen
  576. ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
  577. ** upvalues.)
  578. */
  579. static const char *getupvalname (CallInfo *ci, const TValue *o,
  580. const char **name) {
  581. LClosure *c = ci_func(ci);
  582. int i;
  583. for (i = 0; i < c->nupvalues; i++) {
  584. if (c->upvals[i]->v == o) {
  585. *name = upvalname(c->p, i);
  586. return "upvalue";
  587. }
  588. }
  589. return NULL;
  590. }
  591. static const char *varinfo (lua_State *L, const TValue *o) {
  592. const char *name = NULL; /* to avoid warnings */
  593. CallInfo *ci = L->ci;
  594. const char *kind = NULL;
  595. if (isLua(ci)) {
  596. kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
  597. if (!kind && isinstack(ci, o)) /* no? try a register */
  598. kind = getobjname(ci_func(ci)->p, currentpc(ci),
  599. cast_int(cast(StkId, o) - (ci->func + 1)), &name);
  600. }
  601. return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
  602. }
  603. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  604. const char *t = luaT_objtypename(L, o);
  605. luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
  606. }
  607. l_noret luaG_callerror (lua_State *L, const TValue *o) {
  608. CallInfo *ci = L->ci;
  609. const char *name = NULL; /* to avoid warnings */
  610. const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL;
  611. if (what != NULL) {
  612. const char *t = luaT_objtypename(L, o);
  613. luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t);
  614. }
  615. else
  616. luaG_typeerror(L, o, "call");
  617. }
  618. l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
  619. luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
  620. what, luaT_objtypename(L, o));
  621. }
  622. l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
  623. if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
  624. luaG_typeerror(L, p1, "concatenate");
  625. }
  626. l_noret luaG_opinterror (lua_State *L, const TValue *p1,
  627. const TValue *p2, const char *msg) {
  628. if (!ttisnumber(p1)) /* first operand is wrong? */
  629. p2 = p1; /* now second is wrong */
  630. luaG_typeerror(L, p2, msg);
  631. }
  632. /*
  633. ** Error when both values are convertible to numbers, but not to integers
  634. */
  635. l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
  636. lua_Integer temp;
  637. if (!tointegerns(p1, &temp))
  638. p2 = p1;
  639. luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
  640. }
  641. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  642. const char *t1 = luaT_objtypename(L, p1);
  643. const char *t2 = luaT_objtypename(L, p2);
  644. if (strcmp(t1, t2) == 0)
  645. luaG_runerror(L, "attempt to compare two %s values", t1);
  646. else
  647. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  648. }
  649. /* add src:line information to 'msg' */
  650. const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
  651. int line) {
  652. char buff[LUA_IDSIZE];
  653. if (src)
  654. luaO_chunkid(buff, getstr(src), tsslen(src));
  655. else { /* no source available; use "?" instead */
  656. buff[0] = '?'; buff[1] = '\0';
  657. }
  658. return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  659. }
  660. l_noret luaG_errormsg (lua_State *L) {
  661. if (L->errfunc != 0) { /* is there an error handling function? */
  662. StkId errfunc = restorestack(L, L->errfunc);
  663. lua_assert(ttisfunction(s2v(errfunc)));
  664. setobjs2s(L, L->top, L->top - 1); /* move argument */
  665. setobjs2s(L, L->top - 1, errfunc); /* push function */
  666. L->top++; /* assume EXTRA_STACK */
  667. luaD_callnoyield(L, L->top - 2, 1); /* call it */
  668. }
  669. luaD_throw(L, LUA_ERRRUN);
  670. }
  671. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  672. CallInfo *ci = L->ci;
  673. const char *msg;
  674. va_list argp;
  675. luaC_checkGC(L); /* error message uses memory */
  676. va_start(argp, fmt);
  677. msg = luaO_pushvfstring(L, fmt, argp); /* format message */
  678. va_end(argp);
  679. if (isLua(ci)) /* if Lua function, add source:line information */
  680. luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
  681. luaG_errormsg(L);
  682. }
  683. /*
  684. ** Check whether new instruction 'newpc' is in a different line from
  685. ** previous instruction 'oldpc'. More often than not, 'newpc' is only
  686. ** one or a few instructions after 'oldpc' (it must be after, see
  687. ** caller), so try to avoid calling 'luaG_getfuncline'. If they are
  688. ** too far apart, there is a good chance of a ABSLINEINFO in the way,
  689. ** so it goes directly to 'luaG_getfuncline'.
  690. */
  691. static int changedline (const Proto *p, int oldpc, int newpc) {
  692. if (p->lineinfo == NULL) /* no debug information? */
  693. return 0;
  694. if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */
  695. int delta = 0; /* line diference */
  696. int pc = oldpc;
  697. for (;;) {
  698. int lineinfo = p->lineinfo[++pc];
  699. if (lineinfo == ABSLINEINFO)
  700. break; /* cannot compute delta; fall through */
  701. delta += lineinfo;
  702. if (pc == newpc)
  703. return (delta != 0); /* delta computed successfully */
  704. }
  705. }
  706. /* either instructions are too far apart or there is an absolute line
  707. info in the way; compute line difference explicitly */
  708. return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc));
  709. }
  710. /*
  711. ** Traces the execution of a Lua function. Called before the execution
  712. ** of each opcode, when debug is on. 'L->oldpc' stores the last
  713. ** instruction traced, to detect line changes. When entering a new
  714. ** function, 'npci' will be zero and will test as a new line whatever
  715. ** the value of 'oldpc'. Some exceptional conditions may return to
  716. ** a function without setting 'oldpc'. In that case, 'oldpc' may be
  717. ** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
  718. ** at most causes an extra call to a line hook.)
  719. ** This function is not "Protected" when called, so it should correct
  720. ** 'L->top' before calling anything that can run the GC.
  721. */
  722. int luaG_traceexec (lua_State *L, const Instruction *pc) {
  723. CallInfo *ci = L->ci;
  724. lu_byte mask = L->hookmask;
  725. const Proto *p = ci_func(ci)->p;
  726. int counthook;
  727. if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
  728. ci->u.l.trap = 0; /* don't need to stop again */
  729. return 0; /* turn off 'trap' */
  730. }
  731. pc++; /* reference is always next instruction */
  732. ci->u.l.savedpc = pc; /* save 'pc' */
  733. counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
  734. if (counthook)
  735. resethookcount(L); /* reset count */
  736. else if (!(mask & LUA_MASKLINE))
  737. return 1; /* no line hook and count != 0; nothing to be done now */
  738. if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
  739. ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
  740. return 1; /* do not call hook again (VM yielded, so it did not move) */
  741. }
  742. if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
  743. L->top = ci->top; /* correct top */
  744. if (counthook)
  745. luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
  746. if (mask & LUA_MASKLINE) {
  747. /* 'L->oldpc' may be invalid; use zero in this case */
  748. int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
  749. int npci = pcRel(pc, p);
  750. if (npci <= oldpc || /* call hook when jump back (loop), */
  751. changedline(p, oldpc, npci)) { /* or when enter new line */
  752. int newline = luaG_getfuncline(p, npci);
  753. luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
  754. }
  755. L->oldpc = npci; /* 'pc' of last call to line hook */
  756. }
  757. if (L->status == LUA_YIELD) { /* did hook yield? */
  758. if (counthook)
  759. L->hookcount = 1; /* undo decrement to zero */
  760. ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
  761. ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
  762. luaD_throw(L, LUA_YIELD);
  763. }
  764. return 1; /* keep 'trap' on */
  765. }