ldebug.c 22 KB

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