ldebug.c 22 KB

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