ldebug.c 22 KB

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