ldebug.c 23 KB

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