ldebug.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. ** $Id: ldebug.c,v 2.137 2017/11/03 17:22:54 roberto Exp roberto $
  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_TCCL)
  26. /* Active Lua function (given stack function) */
  27. #define ci_func(func) (clLvalue(s2v(func)))
  28. static const char *funcnamefromcode (lua_State *L, StkId stkf,
  29. const char **name);
  30. static int currentpc (StkId func) {
  31. lua_assert(isLua(func));
  32. return pcRel(func->stkci.u.l.savedpc, ci_func(func)->p);
  33. }
  34. /*
  35. ** Get a "base line" to find the line corresponding to an instruction.
  36. ** For that, search the array of absolute line info for the largest saved
  37. ** instruction smaller or equal to the wanted instrution. A special
  38. ** case is when there is no absolute info or the instruction is before
  39. ** the first absolute one.
  40. */
  41. static int getbaseline (Proto *f, int pc, int *basepc) {
  42. if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
  43. *basepc = -1; /* start from the beginning */
  44. return f->linedefined;
  45. }
  46. else {
  47. unsigned int i;
  48. if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc)
  49. i = f->sizeabslineinfo - 1; /* instruction is after last saved one */
  50. else { /* binary search */
  51. unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */
  52. i = 0; /* abslineinfo[i] <= pc */
  53. while (i < j - 1) {
  54. unsigned int m = (j + i) / 2;
  55. if (pc >= f->abslineinfo[m].pc)
  56. i = m;
  57. else
  58. j = m;
  59. }
  60. }
  61. *basepc = f->abslineinfo[i].pc;
  62. return f->abslineinfo[i].line;
  63. }
  64. }
  65. /*
  66. ** Get the line corresponding to instruction 'pc' in function 'f';
  67. ** first gets a base line and from there does the increments until
  68. ** the desired instruction.
  69. */
  70. int luaG_getfuncline (Proto *f, int pc) {
  71. if (f->lineinfo == NULL) /* no debug information? */
  72. return -1;
  73. else {
  74. int basepc;
  75. int baseline = getbaseline(f, pc, &basepc);
  76. while (basepc++ < pc) { /* walk until given instruction */
  77. lua_assert(f->lineinfo[basepc] != ABSLINEINFO);
  78. baseline += f->lineinfo[basepc]; /* correct line */
  79. }
  80. return baseline;
  81. }
  82. }
  83. static int currentline (StkId func) {
  84. return luaG_getfuncline(ci_func(func)->p, currentpc(func));
  85. }
  86. /*
  87. ** This function can be called asynchronously (e.g. during a signal).
  88. ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
  89. ** 'resethookcount') are for debug only, and it is no problem if they
  90. ** get arbitrary values (causes at most one wrong hook call). 'hookmask'
  91. ** is an atomic value. We assume that pointers are atomic too (e.g., gcc
  92. ** ensures that for all platforms where it runs). Moreover, 'hook' is
  93. ** always checked before being called (see 'luaD_hook').
  94. */
  95. LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  96. if (func == NULL || mask == 0) { /* turn off hooks? */
  97. mask = 0;
  98. func = NULL;
  99. }
  100. if (isLua(L->func))
  101. L->oldpc = L->func->stkci.u.l.savedpc;
  102. L->hook = func;
  103. L->basehookcount = count;
  104. resethookcount(L);
  105. L->hookmask = cast_byte(mask);
  106. }
  107. LUA_API lua_Hook lua_gethook (lua_State *L) {
  108. return L->hook;
  109. }
  110. LUA_API int lua_gethookmask (lua_State *L) {
  111. return L->hookmask;
  112. }
  113. LUA_API int lua_gethookcount (lua_State *L) {
  114. return L->basehookcount;
  115. }
  116. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  117. int status;
  118. CallInfo *ci;
  119. if (level < 0) return 0; /* invalid (negative) level */
  120. lua_lock(L);
  121. for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
  122. level--;
  123. if (level == 0 && ci != &L->base_ci) { /* level found? */
  124. status = 1;
  125. ar->i_ci = ci;
  126. }
  127. else status = 0; /* no such level */
  128. lua_unlock(L);
  129. return status;
  130. }
  131. static const char *upvalname (Proto *p, int uv) {
  132. TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
  133. if (s == NULL) return "?";
  134. else return getstr(s);
  135. }
  136. static StkId findcalled (lua_State *L, StkId caller) {
  137. StkId func = L->func;
  138. for (;;) {
  139. StkId previous = func - func->stkci.previous;
  140. lua_assert(previous < func);
  141. if (previous == caller)
  142. return func;
  143. else
  144. func = previous;
  145. }
  146. }
  147. static const char *findlocal (lua_State *L, StkId stkf, int n,
  148. StkId *pos) {
  149. const char *name = NULL;
  150. if (isLua(stkf)) {
  151. name = luaF_getlocalname(ci_func(stkf)->p, n, currentpc(stkf));
  152. }
  153. if (name == NULL) { /* no 'standard' name? */
  154. StkId limit = (stkf == L->func) ? L->top : findcalled(L, stkf);
  155. if (limit - stkf > n && n > 0) /* is 'n' inside 'ci' stack? */
  156. name = "(*temporary)"; /* generic name for any valid slot */
  157. else
  158. return NULL; /* no name */
  159. }
  160. *pos = stkf + n;
  161. return name;
  162. }
  163. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  164. const char *name;
  165. lua_lock(L);
  166. if (ar == NULL) { /* information about non-active function? */
  167. if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */
  168. name = NULL;
  169. else /* consider live variables at function start (parameters) */
  170. name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0);
  171. }
  172. else { /* active function; get information through 'ar' */
  173. StkId pos = NULL; /* to avoid warnings */
  174. name = findlocal(L, ar->i_ci->func, n, &pos);
  175. if (name) {
  176. setobjs2s(L, L->top, pos);
  177. api_incr_top(L);
  178. }
  179. }
  180. lua_unlock(L);
  181. return name;
  182. }
  183. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  184. StkId pos = NULL; /* to avoid warnings */
  185. const char *name;
  186. lua_lock(L);
  187. name = findlocal(L, ar->i_ci->func, n, &pos);
  188. if (name) {
  189. setobjs2s(L, pos, L->top - 1);
  190. L->top--; /* pop value */
  191. }
  192. lua_unlock(L);
  193. return name;
  194. }
  195. static void funcinfo (lua_Debug *ar, Closure *cl) {
  196. if (noLuaClosure(cl)) {
  197. ar->source = "=[C]";
  198. ar->linedefined = -1;
  199. ar->lastlinedefined = -1;
  200. ar->what = "C";
  201. }
  202. else {
  203. Proto *p = cl->l.p;
  204. ar->source = p->source ? getstr(p->source) : "=?";
  205. ar->linedefined = p->linedefined;
  206. ar->lastlinedefined = p->lastlinedefined;
  207. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  208. }
  209. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  210. }
  211. static int nextline (Proto *p, int currentline, int pc) {
  212. if (p->lineinfo[pc] != ABSLINEINFO)
  213. return currentline + p->lineinfo[pc];
  214. else
  215. return luaG_getfuncline(p, pc);
  216. }
  217. static void collectvalidlines (lua_State *L, Closure *f) {
  218. if (noLuaClosure(f)) {
  219. setnilvalue(s2v(L->top));
  220. api_incr_top(L);
  221. }
  222. else {
  223. int i;
  224. TValue v;
  225. Proto *p = f->l.p;
  226. int currentline = p->linedefined;
  227. Table *t = luaH_new(L); /* new table to store active lines */
  228. sethvalue2s(L, L->top, t); /* push it on stack */
  229. api_incr_top(L);
  230. setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
  231. for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */
  232. currentline = nextline(p, currentline, i);
  233. luaH_setint(L, t, currentline, &v); /* table[line] = true */
  234. }
  235. }
  236. }
  237. static const char *getfuncname (lua_State *L, StkId stkf, const char **name) {
  238. if (stkf == NULL) /* no function? */
  239. return NULL; /* no info */
  240. else if (callstatus(stkf) & CIST_FIN) { /* is this a finalizer? */
  241. *name = "__gc";
  242. return "metamethod"; /* report it as such */
  243. }
  244. /* calling function is a known Lua function? */
  245. else {
  246. StkId previous = stkf - stkf->stkci.previous;
  247. if (!(callstatus(stkf) & CIST_TAIL) && isLua(previous))
  248. return funcnamefromcode(L, previous, name);
  249. else return NULL; /* no way to find a name */
  250. }
  251. }
  252. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  253. Closure *f, StkId stkf) {
  254. int status = 1;
  255. for (; *what; what++) {
  256. switch (*what) {
  257. case 'S': {
  258. funcinfo(ar, f);
  259. break;
  260. }
  261. case 'l': {
  262. ar->currentline = (stkf && isLua(stkf)) ? currentline(stkf) : -1;
  263. break;
  264. }
  265. case 'u': {
  266. ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
  267. if (noLuaClosure(f)) {
  268. ar->isvararg = 1;
  269. ar->nparams = 0;
  270. }
  271. else {
  272. ar->isvararg = f->l.p->is_vararg;
  273. ar->nparams = f->l.p->numparams;
  274. }
  275. break;
  276. }
  277. case 't': {
  278. ar->istailcall = (stkf) ? callstatus(stkf) & CIST_TAIL : 0;
  279. break;
  280. }
  281. case 'n': {
  282. ar->namewhat = getfuncname(L, stkf, &ar->name);
  283. if (ar->namewhat == NULL) {
  284. ar->namewhat = ""; /* not found */
  285. ar->name = NULL;
  286. }
  287. break;
  288. }
  289. case 'L':
  290. case 'f': /* handled by lua_getinfo */
  291. break;
  292. default: status = 0; /* invalid option */
  293. }
  294. }
  295. return status;
  296. }
  297. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  298. int status;
  299. Closure *cl;
  300. StkId stkf;
  301. TValue *func;
  302. lua_lock(L);
  303. if (*what == '>') {
  304. stkf = NULL;
  305. func = s2v(L->top - 1);
  306. api_check(L, ttisfunction(func), "function expected");
  307. what++; /* skip the '>' */
  308. L->top--; /* pop function */
  309. }
  310. else {
  311. stkf = ar->i_ci->func;
  312. func = s2v(stkf);
  313. lua_assert(ttisfunction(func));
  314. }
  315. cl = ttisclosure(func) ? clvalue(func) : NULL;
  316. status = auxgetinfo(L, what, ar, cl, stkf);
  317. if (strchr(what, 'f')) {
  318. setobj2s(L, L->top, func);
  319. api_incr_top(L);
  320. }
  321. if (strchr(what, 'L'))
  322. collectvalidlines(L, cl);
  323. lua_unlock(L);
  324. return status;
  325. }
  326. /*
  327. ** {======================================================
  328. ** Symbolic Execution
  329. ** =======================================================
  330. */
  331. static const char *getobjname (Proto *p, int lastpc, int reg,
  332. const char **name);
  333. /*
  334. ** Find a "name" for the constant 'c'.
  335. */
  336. static void kname (Proto *p, int c, const char **name) {
  337. TValue *kvalue = &p->k[c];
  338. *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?";
  339. }
  340. /*
  341. ** Find a "name" for the register 'c'.
  342. */
  343. static void rname (Proto *p, int pc, int c, const char **name) {
  344. const char *what = getobjname(p, pc, c, name); /* search for 'c' */
  345. if (!(what && *what == 'c')) /* did not find a constant name? */
  346. *name = "?";
  347. }
  348. /*
  349. ** Find a "name" for a 'C' value in an RK instruction.
  350. */
  351. static void rkname (Proto *p, int pc, Instruction i, const char **name) {
  352. int c = GETARG_C(i); /* key index */
  353. if (GETARG_k(i)) /* is 'c' a constant? */
  354. kname(p, c, name);
  355. else /* 'c' is a register */
  356. rname(p, pc, c, name);
  357. }
  358. static int filterpc (int pc, int jmptarget) {
  359. if (pc < jmptarget) /* is code conditional (inside a jump)? */
  360. return -1; /* cannot know who sets that register */
  361. else return pc; /* current position sets that register */
  362. }
  363. /*
  364. ** try to find last instruction before 'lastpc' that modified register 'reg'
  365. */
  366. static int findsetreg (Proto *p, int lastpc, int reg) {
  367. int pc;
  368. int setreg = -1; /* keep last instruction that changed 'reg' */
  369. int jmptarget = 0; /* any code before this address is conditional */
  370. for (pc = 0; pc < lastpc; pc++) {
  371. Instruction i = p->code[pc];
  372. OpCode op = GET_OPCODE(i);
  373. int a = GETARG_A(i);
  374. int change; /* true if current instruction changed 'reg' */
  375. switch (op) {
  376. case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */
  377. int b = GETARG_B(i);
  378. change = (a <= reg && reg <= a + b);
  379. break;
  380. }
  381. case OP_TFORCALL: { /* affect all regs above its base */
  382. change = (reg >= a + 2);
  383. break;
  384. }
  385. case OP_CALL:
  386. case OP_TAILCALL: { /* affect all registers above base */
  387. change = (reg >= a);
  388. break;
  389. }
  390. case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */
  391. int b = GETARG_sBx(i);
  392. int dest = pc + 1 + b;
  393. /* jump does not skip 'lastpc' and is larger than current one? */
  394. if (dest <= lastpc && dest > jmptarget)
  395. jmptarget = dest; /* update 'jmptarget' */
  396. change = 0;
  397. break;
  398. }
  399. default: /* any instruction that sets A */
  400. change = (testAMode(op) && reg == a);
  401. break;
  402. }
  403. if (change)
  404. setreg = filterpc(pc, jmptarget);
  405. }
  406. return setreg;
  407. }
  408. /*
  409. ** Check whether table being indexed by instruction 'i' is the
  410. ** environment '_ENV'
  411. */
  412. static const char *gxf (Proto *p, int pc, Instruction i, int isup) {
  413. int t = GETARG_B(i); /* table index */
  414. const char *name; /* name of indexed variable */
  415. if (isup) /* is an upvalue? */
  416. name = upvalname(p, t);
  417. else
  418. getobjname(p, pc, t, &name);
  419. return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
  420. }
  421. const char *getobjname (Proto *p, int lastpc, int reg, const char **name) {
  422. int pc;
  423. *name = luaF_getlocalname(p, reg + 1, lastpc);
  424. if (*name) /* is a local? */
  425. return "local";
  426. /* else try symbolic execution */
  427. pc = findsetreg(p, lastpc, reg);
  428. if (pc != -1) { /* could find instruction? */
  429. Instruction i = p->code[pc];
  430. OpCode op = GET_OPCODE(i);
  431. switch (op) {
  432. case OP_MOVE: {
  433. int b = GETARG_B(i); /* move from 'b' to 'a' */
  434. if (b < GETARG_A(i))
  435. return getobjname(p, pc, b, name); /* get name for 'b' */
  436. break;
  437. }
  438. case OP_GETTABUP: {
  439. int k = GETARG_C(i); /* key index */
  440. kname(p, k, name);
  441. return gxf(p, pc, i, 1);
  442. }
  443. case OP_GETTABLE: {
  444. int k = GETARG_C(i); /* key index */
  445. rname(p, pc, k, name);
  446. return gxf(p, pc, i, 0);
  447. }
  448. case OP_GETI: {
  449. *name = "integer index";
  450. return "field";
  451. }
  452. case OP_GETFIELD: {
  453. int k = GETARG_C(i); /* key index */
  454. kname(p, k, name);
  455. return gxf(p, pc, i, 0);
  456. }
  457. case OP_GETUPVAL: {
  458. *name = upvalname(p, GETARG_B(i));
  459. return "upvalue";
  460. }
  461. case OP_LOADK:
  462. case OP_LOADKX: {
  463. int b = (op == OP_LOADK) ? GETARG_Bx(i)
  464. : GETARG_Ax(p->code[pc + 1]);
  465. if (ttisstring(&p->k[b])) {
  466. *name = svalue(&p->k[b]);
  467. return "constant";
  468. }
  469. break;
  470. }
  471. case OP_SELF: {
  472. rkname(p, pc, i, name);
  473. return "method";
  474. }
  475. default: break; /* go through to return NULL */
  476. }
  477. }
  478. return NULL; /* could not find reasonable name */
  479. }
  480. /*
  481. ** Try to find a name for a function based on the code that called it.
  482. ** (Only works when function was called by a Lua function.)
  483. ** Returns what the name is (e.g., "for iterator", "method",
  484. ** "metamethod") and sets '*name' to point to the name.
  485. */
  486. static const char *funcnamefromcode (lua_State *L, StkId stkf,
  487. const char **name) {
  488. TMS tm = (TMS)0; /* (initial value avoids warnings) */
  489. Proto *p = ci_func(stkf)->p; /* calling function */
  490. int pc = currentpc(stkf); /* calling instruction index */
  491. Instruction i = p->code[pc]; /* calling instruction */
  492. if (callstatus(stkf) & CIST_HOOKED) { /* was it called inside a hook? */
  493. *name = "?";
  494. return "hook";
  495. }
  496. switch (GET_OPCODE(i)) {
  497. case OP_CALL:
  498. case OP_TAILCALL:
  499. return getobjname(p, pc, GETARG_A(i), name); /* get function name */
  500. case OP_TFORCALL: { /* for iterator */
  501. *name = "for iterator";
  502. return "for iterator";
  503. }
  504. /* other instructions can do calls through metamethods */
  505. case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
  506. case OP_GETI: case OP_GETFIELD:
  507. tm = TM_INDEX;
  508. break;
  509. case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
  510. tm = TM_NEWINDEX;
  511. break;
  512. case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI:
  513. case OP_POWI: case OP_DIVI: case OP_IDIVI: {
  514. int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */
  515. tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
  516. break;
  517. }
  518. case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
  519. case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
  520. case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
  521. int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */
  522. tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
  523. break;
  524. }
  525. case OP_UNM: tm = TM_UNM; break;
  526. case OP_BNOT: tm = TM_BNOT; break;
  527. case OP_LEN: tm = TM_LEN; break;
  528. case OP_CONCAT: tm = TM_CONCAT; break;
  529. case OP_EQ: tm = TM_EQ; break;
  530. case OP_LT: tm = TM_LT; break;
  531. case OP_LE: tm = TM_LE; break;
  532. default:
  533. return NULL; /* cannot find a reasonable name */
  534. }
  535. *name = getstr(G(L)->tmname[tm]);
  536. return "metamethod";
  537. }
  538. /* }====================================================== */
  539. /*
  540. ** The subtraction of two potentially unrelated pointers is
  541. ** not ISO C, but it should not crash a program; the subsequent
  542. ** checks are ISO C and ensure a correct result.
  543. */
  544. static int isinstack (lua_State *L, const TValue *o) {
  545. StkId base = L->stack;
  546. ptrdiff_t i = cast(StkId, o) - base;
  547. return (0 <= i && i < (L->top - base) && s2v(base + i) == o);
  548. }
  549. /*
  550. ** Checks whether value 'o' came from an upvalue. (That can only happen
  551. ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
  552. ** upvalues.)
  553. */
  554. static const char *getupvalname (StkId stkf, const TValue *o,
  555. const char **name) {
  556. LClosure *c = ci_func(stkf);
  557. int i;
  558. for (i = 0; i < c->nupvalues; i++) {
  559. if (c->upvals[i]->v == o) {
  560. *name = upvalname(c->p, i);
  561. return "upvalue";
  562. }
  563. }
  564. return NULL;
  565. }
  566. static const char *varinfo (lua_State *L, const TValue *o) {
  567. const char *name = NULL; /* to avoid warnings */
  568. StkId stkf = L->func;
  569. const char *kind = NULL;
  570. if (isLua(stkf)) {
  571. kind = getupvalname(stkf, o, &name); /* check whether 'o' is an upvalue */
  572. if (!kind && isinstack(L, o)) /* no? try a register */
  573. kind = getobjname(ci_func(stkf)->p, currentpc(stkf),
  574. cast_int(cast(StkId, o) - (stkf + 1)), &name);
  575. }
  576. return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
  577. }
  578. l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  579. const char *t = luaT_objtypename(L, o);
  580. luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
  581. }
  582. l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
  583. if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
  584. luaG_typeerror(L, p1, "concatenate");
  585. }
  586. l_noret luaG_opinterror (lua_State *L, const TValue *p1,
  587. const TValue *p2, const char *msg) {
  588. if (!ttisnumber(p1)) /* first operand is wrong? */
  589. p2 = p1; /* now second is wrong */
  590. luaG_typeerror(L, p2, msg);
  591. }
  592. /*
  593. ** Error when both values are convertible to numbers, but not to integers
  594. */
  595. l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
  596. lua_Integer temp;
  597. if (!tointeger(p1, &temp))
  598. p2 = p1;
  599. luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
  600. }
  601. l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  602. const char *t1 = luaT_objtypename(L, p1);
  603. const char *t2 = luaT_objtypename(L, p2);
  604. if (strcmp(t1, t2) == 0)
  605. luaG_runerror(L, "attempt to compare two %s values", t1);
  606. else
  607. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  608. }
  609. /* add src:line information to 'msg' */
  610. const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
  611. int line) {
  612. char buff[LUA_IDSIZE];
  613. if (src)
  614. luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
  615. else { /* no source available; use "?" instead */
  616. buff[0] = '?'; buff[1] = '\0';
  617. }
  618. return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  619. }
  620. l_noret luaG_errormsg (lua_State *L) {
  621. if (L->errfunc != 0) { /* is there an error handling function? */
  622. StkId errfunc = restorestack(L, L->errfunc);
  623. setobjs2s(L, L->top, L->top - 1); /* move argument */
  624. setobjs2s(L, L->top - 1, errfunc); /* push function */
  625. L->top++; /* assume EXTRA_STACK */
  626. luaD_callnoyield(L, L->top - 2, 1); /* call it */
  627. }
  628. luaD_throw(L, LUA_ERRRUN);
  629. }
  630. l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
  631. StkId func;
  632. const char *msg;
  633. va_list argp;
  634. luaC_checkGC(L); /* error message uses memory */
  635. va_start(argp, fmt);
  636. msg = luaO_pushvfstring(L, fmt, argp); /* format message */
  637. va_end(argp);
  638. func = L->func; /* previous calls can change the stack */
  639. if (isLua(func)) /* if Lua function, add source:line information */
  640. luaG_addinfo(L, msg, ci_func(func)->p->source, currentline(func));
  641. luaG_errormsg(L);
  642. }
  643. /*
  644. ** Check whether new instruction 'newpc' is in a different line from
  645. ** previous instruction 'oldpc'.
  646. */
  647. static int changedline (Proto *p, int oldpc, int newpc) {
  648. while (oldpc++ < newpc) {
  649. if (p->lineinfo[oldpc] != 0)
  650. return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc));
  651. }
  652. return 0; /* no line changes in the way */
  653. }
  654. void luaG_traceexec (lua_State *L) {
  655. StkId func = L->func;
  656. lu_byte mask = L->hookmask;
  657. int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
  658. if (counthook)
  659. resethookcount(L); /* reset count */
  660. else if (!(mask & LUA_MASKLINE))
  661. return; /* no line hook and count != 0; nothing to be done */
  662. if (callstatus(func) & CIST_HOOKYIELD) { /* called hook last time? */
  663. callstatus(func) &= ~CIST_HOOKYIELD; /* erase mark */
  664. return; /* do not call hook again (VM yielded, so it did not move) */
  665. }
  666. if (counthook)
  667. luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
  668. if (mask & LUA_MASKLINE) {
  669. Proto *p = ci_func(func)->p;
  670. int npc = pcRel(func->stkci.u.l.savedpc, p);
  671. if (npc == 0 || /* call linehook when enter a new function, */
  672. func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */
  673. changedline(p, pcRel(L->oldpc, p), npc)) { /* when enter new line */
  674. int newline = luaG_getfuncline(p, npc); /* new line */
  675. luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
  676. }
  677. }
  678. func = L->func; /* previous calls can reallocate stack */
  679. L->oldpc = func->stkci.u.l.savedpc;
  680. if (L->status == LUA_YIELD) { /* did hook yield? */
  681. if (counthook)
  682. L->hookcount = 1; /* undo decrement to zero */
  683. /* undo increment (resume will increment it again) */
  684. func->stkci.u.l.savedpc--;
  685. callstatus(func) |= CIST_HOOKYIELD; /* mark that it yielded */
  686. luaD_throw(L, LUA_YIELD);
  687. }
  688. }