ldebug.c 20 KB

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