ldebug.c 23 KB

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