2
0

ldebug.c 22 KB

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