ldebug.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. ** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $
  3. ** Debug Interface
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ldebug_c
  7. #define LUA_CORE
  8. #define LUAC_CROSS_FILE
  9. #include "lua.h"
  10. #include <string.h>
  11. #include "lapi.h"
  12. #include "lcode.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lvm.h"
  23. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  24. static int currentpc (lua_State *L, CallInfo *ci) {
  25. if (!isLua(ci)) return -1; /* function is not a Lua function? */
  26. if (ci == L->ci)
  27. ci->savedpc = L->savedpc;
  28. return pcRel(ci->savedpc, ci_func(ci)->l.p);
  29. }
  30. static int currentline (lua_State *L, CallInfo *ci) {
  31. int pc = currentpc(L, ci);
  32. if (pc < 0)
  33. return -1; /* only active lua functions have current-line information */
  34. else
  35. return getline(ci_func(ci)->l.p, pc);
  36. }
  37. /*
  38. ** this function can be called asynchronous (e.g. during a signal)
  39. */
  40. LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
  41. if (func == NULL || mask == 0) { /* turn off hooks? */
  42. mask = 0;
  43. func = NULL;
  44. }
  45. L->hook = func;
  46. L->basehookcount = count;
  47. resethookcount(L);
  48. L->hookmask = cast_byte(mask);
  49. return 1;
  50. }
  51. LUA_API lua_Hook lua_gethook (lua_State *L) {
  52. return L->hook;
  53. }
  54. LUA_API int lua_gethookmask (lua_State *L) {
  55. return L->hookmask;
  56. }
  57. LUA_API int lua_gethookcount (lua_State *L) {
  58. return L->basehookcount;
  59. }
  60. LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
  61. int status;
  62. CallInfo *ci;
  63. lua_lock(L);
  64. for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
  65. level--;
  66. if (f_isLua(ci)) /* Lua function? */
  67. level -= ci->tailcalls; /* skip lost tail calls */
  68. }
  69. if (level == 0 && ci > L->base_ci) { /* level found? */
  70. status = 1;
  71. ar->i_ci = cast_int(ci - L->base_ci);
  72. }
  73. else if (level < 0) { /* level is of a lost tail call? */
  74. status = 1;
  75. ar->i_ci = 0;
  76. }
  77. else status = 0; /* no such level */
  78. lua_unlock(L);
  79. return status;
  80. }
  81. static Proto *getluaproto (CallInfo *ci) {
  82. return (isLua(ci) ? ci_func(ci)->l.p : NULL);
  83. }
  84. static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
  85. const char *name;
  86. Proto *fp = getluaproto(ci);
  87. if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
  88. return name; /* is a local variable in a Lua function */
  89. else {
  90. StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
  91. if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
  92. return "(*temporary)";
  93. else
  94. return NULL;
  95. }
  96. }
  97. LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
  98. CallInfo *ci = L->base_ci + ar->i_ci;
  99. const char *name = findlocal(L, ci, n);
  100. lua_lock(L);
  101. if (name)
  102. luaA_pushobject(L, ci->base + (n - 1));
  103. lua_unlock(L);
  104. return name;
  105. }
  106. LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
  107. CallInfo *ci = L->base_ci + ar->i_ci;
  108. const char *name = findlocal(L, ci, n);
  109. lua_lock(L);
  110. if (name)
  111. setobjs2s(L, ci->base + (n - 1), L->top - 1);
  112. L->top--; /* pop value */
  113. lua_unlock(L);
  114. return name;
  115. }
  116. static void funcinfo (lua_Debug *ar, Closure *cl, void *plight) {
  117. if (plight || cl->c.isC) {
  118. ar->source = "=[C]";
  119. ar->linedefined = -1;
  120. ar->lastlinedefined = -1;
  121. ar->what = "C";
  122. }
  123. else {
  124. ar->source = getstr(cl->l.p->source);
  125. ar->linedefined = cl->l.p->linedefined;
  126. ar->lastlinedefined = cl->l.p->lastlinedefined;
  127. ar->what = (ar->linedefined == 0) ? "main" : "Lua";
  128. }
  129. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  130. }
  131. static void info_tailcall (lua_Debug *ar) {
  132. ar->name = ar->namewhat = "";
  133. ar->what = "tail";
  134. ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
  135. ar->source = "=(tail call)";
  136. luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
  137. ar->nups = 0;
  138. }
  139. static void collectvalidlines (lua_State *L, Closure *f) {
  140. if (f == NULL || f->c.isC) {
  141. setnilvalue(L->top);
  142. }
  143. else {
  144. # define INFO_FILL_BYTE 0x7F
  145. # define INFO_DELTA_MASK 0x80
  146. # define INFO_SIGN_MASK 0x40
  147. # define INFO_DELTA_6BITS 0x3F
  148. # define INFO_DELTA_7BITS 0x7F
  149. # define INFO_MAX_LINECNT 126
  150. Table *t = luaH_new(L, 0, 0);
  151. #ifdef LUA_OPTIMIZE_DEBUG
  152. int line = 0;
  153. unsigned char *p = f->l.p->packedlineinfo;
  154. if (p) {
  155. for (; *p && *p != INFO_FILL_BYTE; ) {
  156. if (*p & INFO_DELTA_MASK) { /* line delta */
  157. int delta = *p & INFO_DELTA_6BITS;
  158. unsigned char sign = *p++ & INFO_SIGN_MASK;
  159. int shift;
  160. for (shift = 6; *p & INFO_DELTA_MASK; p++, shift += 7) {
  161. delta += (*p & INFO_DELTA_7BITS)<<shift;
  162. }
  163. line += sign ? -delta : delta+2;
  164. } else {
  165. line++;
  166. }
  167. p++;
  168. setbvalue(luaH_setnum(L, t, line), 1);
  169. }
  170. }
  171. #else
  172. int *lineinfo = f->l.p->lineinfo;
  173. int i;
  174. for (i=0; i<f->l.p->sizelineinfo; i++)
  175. setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
  176. #endif
  177. sethvalue(L, L->top, t);
  178. }
  179. incr_top(L);
  180. }
  181. #ifdef LUA_OPTIMIZE_DEBUG
  182. /*
  183. * This may seem expensive but this is only accessed frequently in traceexec
  184. * and the while loop will be executed roughly half the number of non-blank
  185. * source lines in the Lua function and these tend to be short.
  186. */
  187. int luaG_getline (const Proto *f, int pc) {
  188. int line = 0, thispc = 0, nextpc;
  189. unsigned char *p;
  190. for (p = f->packedlineinfo; *p && *p != INFO_FILL_BYTE;) {
  191. if (*p & INFO_DELTA_MASK) { /* line delta */
  192. int delta = *p & INFO_DELTA_6BITS;
  193. unsigned char sign = *p++ & INFO_SIGN_MASK;
  194. int shift;
  195. for (shift = 6; *p & INFO_DELTA_MASK; p++, shift += 7) {
  196. delta += (*p & INFO_DELTA_7BITS)<<shift;
  197. }
  198. line += sign ? -delta : delta+2;
  199. } else {
  200. line++;
  201. }
  202. lua_assert(*p<127);
  203. nextpc = thispc + *p++;
  204. if (thispc <= pc && pc < nextpc) {
  205. return line;
  206. }
  207. thispc = nextpc;
  208. }
  209. lua_assert(0);
  210. return 0;
  211. }
  212. static int stripdebug (lua_State *L, Proto *f, int level) {
  213. int len = 0, sizepackedlineinfo;
  214. TString* dummy;
  215. switch (level) {
  216. case 3:
  217. sizepackedlineinfo = strlen(cast(char *, f->packedlineinfo))+1;
  218. f->packedlineinfo = luaM_freearray(L, f->packedlineinfo, sizepackedlineinfo, unsigned char);
  219. len += sizepackedlineinfo;
  220. case 2:
  221. len += f->sizelocvars * (sizeof(struct LocVar) + sizeof(dummy->tsv) + sizeof(struct LocVar *));
  222. f->locvars = luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  223. f->upvalues = luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
  224. len += f->sizelocvars * (sizeof(struct LocVar) + sizeof(dummy->tsv) + sizeof(struct LocVar *)) +
  225. f->sizeupvalues * (sizeof(dummy->tsv) + sizeof(TString *));
  226. f->sizelocvars = 0;
  227. f->sizeupvalues = 0;
  228. }
  229. return len;
  230. }
  231. /* This is a recursive function so it's stack size has been kept to a minimum! */
  232. LUA_API int luaG_stripdebug (lua_State *L, Proto *f, int level, int recv){
  233. int len = 0, i;
  234. if (recv != 0 && f->sizep != 0) {
  235. for(i=0;i<f->sizep;i++) len += luaG_stripdebug(L, f->p[i], level, recv);
  236. }
  237. len += stripdebug (L, f, level);
  238. return len;
  239. }
  240. #endif
  241. static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
  242. Closure *f, void *plight, CallInfo *ci) {
  243. int status = 1;
  244. if (plight == NULL && f == NULL) {
  245. info_tailcall(ar);
  246. return status;
  247. }
  248. for (; *what; what++) {
  249. switch (*what) {
  250. case 'S': {
  251. funcinfo(ar, f, plight);
  252. break;
  253. }
  254. case 'l': {
  255. ar->currentline = (ci) ? currentline(L, ci) : -1;
  256. break;
  257. }
  258. case 'u': {
  259. ar->nups = f ? f->c.nupvalues : 0;
  260. break;
  261. }
  262. case 'n': {
  263. ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
  264. if (ar->namewhat == NULL) {
  265. ar->namewhat = ""; /* not found */
  266. ar->name = NULL;
  267. }
  268. break;
  269. }
  270. case 'L':
  271. case 'f': /* handled by lua_getinfo */
  272. break;
  273. default: status = 0; /* invalid option */
  274. }
  275. }
  276. return status;
  277. }
  278. LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
  279. int status;
  280. Closure *f = NULL;
  281. CallInfo *ci = NULL;
  282. void *plight = NULL;
  283. lua_lock(L);
  284. if (*what == '>') {
  285. StkId func = L->top - 1;
  286. luai_apicheck(L, ttisfunction(func) || ttislightfunction(func));
  287. what++; /* skip the '>' */
  288. if (ttisfunction(func))
  289. f = clvalue(func);
  290. else
  291. plight = fvalue(func);
  292. L->top--; /* pop function */
  293. }
  294. else if (ar->i_ci != 0) { /* no tail call? */
  295. ci = L->base_ci + ar->i_ci;
  296. lua_assert(ttisfunction(ci->func) || ttislightfunction(ci->func));
  297. if (ttisfunction(ci->func))
  298. f = clvalue(ci->func);
  299. else
  300. plight = fvalue(ci->func);
  301. }
  302. status = auxgetinfo(L, what, ar, f, plight, ci);
  303. if (strchr(what, 'f')) {
  304. if (f != NULL)
  305. setclvalue(L, L->top, f)
  306. else if (plight != NULL)
  307. setfvalue(L->top, plight)
  308. else
  309. setnilvalue(L->top);
  310. incr_top(L);
  311. }
  312. if (strchr(what, 'L'))
  313. collectvalidlines(L, f);
  314. lua_unlock(L);
  315. return status;
  316. }
  317. /*
  318. ** {======================================================
  319. ** Symbolic Execution and code checker
  320. ** =======================================================
  321. */
  322. #define check(x) if (!(x)) return 0;
  323. #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
  324. #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
  325. static int precheck (const Proto *pt) {
  326. check(pt->maxstacksize <= MAXSTACK);
  327. check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
  328. check(!(pt->is_vararg & VARARG_NEEDSARG) ||
  329. (pt->is_vararg & VARARG_HASARG));
  330. check(pt->sizeupvalues <= pt->nups);
  331. #ifndef LUA_OPTIMIZE_DEBUG
  332. check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
  333. #endif
  334. check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
  335. return 1;
  336. }
  337. #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
  338. int luaG_checkopenop (Instruction i) {
  339. switch (GET_OPCODE(i)) {
  340. case OP_CALL:
  341. case OP_TAILCALL:
  342. case OP_RETURN:
  343. case OP_SETLIST: {
  344. check(GETARG_B(i) == 0);
  345. return 1;
  346. }
  347. default: return 0; /* invalid instruction after an open call */
  348. }
  349. }
  350. static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
  351. switch (mode) {
  352. case OpArgN: check(r == 0); break;
  353. case OpArgU: break;
  354. case OpArgR: checkreg(pt, r); break;
  355. case OpArgK:
  356. check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
  357. break;
  358. }
  359. return 1;
  360. }
  361. static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
  362. int pc;
  363. int last; /* stores position of last instruction that changed `reg' */
  364. last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
  365. check(precheck(pt));
  366. for (pc = 0; pc < lastpc; pc++) {
  367. Instruction i = pt->code[pc];
  368. OpCode op = GET_OPCODE(i);
  369. int a = GETARG_A(i);
  370. int b = 0;
  371. int c = 0;
  372. check(op < NUM_OPCODES);
  373. checkreg(pt, a);
  374. switch (getOpMode(op)) {
  375. case iABC: {
  376. b = GETARG_B(i);
  377. c = GETARG_C(i);
  378. check(checkArgMode(pt, b, getBMode(op)));
  379. check(checkArgMode(pt, c, getCMode(op)));
  380. break;
  381. }
  382. case iABx: {
  383. b = GETARG_Bx(i);
  384. if (getBMode(op) == OpArgK) check(b < pt->sizek);
  385. break;
  386. }
  387. case iAsBx: {
  388. b = GETARG_sBx(i);
  389. if (getBMode(op) == OpArgR) {
  390. int dest = pc+1+b;
  391. check(0 <= dest && dest < pt->sizecode);
  392. if (dest > 0) {
  393. int j;
  394. /* check that it does not jump to a setlist count; this
  395. is tricky, because the count from a previous setlist may
  396. have the same value of an invalid setlist; so, we must
  397. go all the way back to the first of them (if any) */
  398. for (j = 0; j < dest; j++) {
  399. Instruction d = pt->code[dest-1-j];
  400. if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
  401. }
  402. /* if 'j' is even, previous value is not a setlist (even if
  403. it looks like one) */
  404. check((j&1) == 0);
  405. }
  406. }
  407. break;
  408. }
  409. }
  410. if (testAMode(op)) {
  411. if (a == reg) last = pc; /* change register `a' */
  412. }
  413. if (testTMode(op)) {
  414. check(pc+2 < pt->sizecode); /* check skip */
  415. check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
  416. }
  417. switch (op) {
  418. case OP_LOADBOOL: {
  419. if (c == 1) { /* does it jump? */
  420. check(pc+2 < pt->sizecode); /* check its jump */
  421. check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
  422. GETARG_C(pt->code[pc+1]) != 0);
  423. }
  424. break;
  425. }
  426. case OP_LOADNIL: {
  427. if (a <= reg && reg <= b)
  428. last = pc; /* set registers from `a' to `b' */
  429. break;
  430. }
  431. case OP_GETUPVAL:
  432. case OP_SETUPVAL: {
  433. check(b < pt->nups);
  434. break;
  435. }
  436. case OP_GETGLOBAL:
  437. case OP_SETGLOBAL: {
  438. check(ttisstring(&pt->k[b]));
  439. break;
  440. }
  441. case OP_SELF: {
  442. checkreg(pt, a+1);
  443. if (reg == a+1) last = pc;
  444. break;
  445. }
  446. case OP_CONCAT: {
  447. check(b < c); /* at least two operands */
  448. break;
  449. }
  450. case OP_TFORLOOP: {
  451. check(c >= 1); /* at least one result (control variable) */
  452. checkreg(pt, a+2+c); /* space for results */
  453. if (reg >= a+2) last = pc; /* affect all regs above its base */
  454. break;
  455. }
  456. case OP_FORLOOP:
  457. case OP_FORPREP:
  458. checkreg(pt, a+3);
  459. /* go through */
  460. case OP_JMP: {
  461. int dest = pc+1+b;
  462. /* not full check and jump is forward and do not skip `lastpc'? */
  463. if (reg != NO_REG && pc < dest && dest <= lastpc)
  464. pc += b; /* do the jump */
  465. break;
  466. }
  467. case OP_CALL:
  468. case OP_TAILCALL: {
  469. if (b != 0) {
  470. checkreg(pt, a+b-1);
  471. }
  472. c--; /* c = num. returns */
  473. if (c == LUA_MULTRET) {
  474. check(checkopenop(pt, pc));
  475. }
  476. else if (c != 0)
  477. checkreg(pt, a+c-1);
  478. if (reg >= a) last = pc; /* affect all registers above base */
  479. break;
  480. }
  481. case OP_RETURN: {
  482. b--; /* b = num. returns */
  483. if (b > 0) checkreg(pt, a+b-1);
  484. break;
  485. }
  486. case OP_SETLIST: {
  487. if (b > 0) checkreg(pt, a + b);
  488. if (c == 0) {
  489. pc++;
  490. check(pc < pt->sizecode - 1);
  491. }
  492. break;
  493. }
  494. case OP_CLOSURE: {
  495. int nup, j;
  496. check(b < pt->sizep);
  497. nup = pt->p[b]->nups;
  498. check(pc + nup < pt->sizecode);
  499. for (j = 1; j <= nup; j++) {
  500. OpCode op1 = GET_OPCODE(pt->code[pc + j]);
  501. check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
  502. }
  503. if (reg != NO_REG) /* tracing? */
  504. pc += nup; /* do not 'execute' these pseudo-instructions */
  505. break;
  506. }
  507. case OP_VARARG: {
  508. check((pt->is_vararg & VARARG_ISVARARG) &&
  509. !(pt->is_vararg & VARARG_NEEDSARG));
  510. b--;
  511. if (b == LUA_MULTRET) check(checkopenop(pt, pc));
  512. checkreg(pt, a+b-1);
  513. break;
  514. }
  515. default: break;
  516. }
  517. }
  518. return pt->code[last];
  519. }
  520. #undef check
  521. #undef checkjump
  522. #undef checkreg
  523. /* }====================================================== */
  524. int luaG_checkcode (const Proto *pt) {
  525. return (symbexec(pt, pt->sizecode, NO_REG) != 0);
  526. }
  527. static const char *kname (Proto *p, int c) {
  528. if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
  529. return svalue(&p->k[INDEXK(c)]);
  530. else
  531. return "?";
  532. }
  533. static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
  534. const char **name) {
  535. if (isLua(ci)) { /* a Lua function? */
  536. Proto *p = ci_func(ci)->l.p;
  537. int pc = currentpc(L, ci);
  538. Instruction i;
  539. *name = luaF_getlocalname(p, stackpos+1, pc);
  540. if (*name) /* is a local? */
  541. return "local";
  542. i = symbexec(p, pc, stackpos); /* try symbolic execution */
  543. lua_assert(pc != -1);
  544. switch (GET_OPCODE(i)) {
  545. case OP_GETGLOBAL: {
  546. int g = GETARG_Bx(i); /* global index */
  547. lua_assert(ttisstring(&p->k[g]));
  548. *name = svalue(&p->k[g]);
  549. return "global";
  550. }
  551. case OP_MOVE: {
  552. int a = GETARG_A(i);
  553. int b = GETARG_B(i); /* move from `b' to `a' */
  554. if (b < a)
  555. return getobjname(L, ci, b, name); /* get name for `b' */
  556. break;
  557. }
  558. case OP_GETTABLE: {
  559. int k = GETARG_C(i); /* key index */
  560. *name = kname(p, k);
  561. return "field";
  562. }
  563. case OP_GETUPVAL: {
  564. int u = GETARG_B(i); /* upvalue index */
  565. *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
  566. return "upvalue";
  567. }
  568. case OP_SELF: {
  569. int k = GETARG_C(i); /* key index */
  570. *name = kname(p, k);
  571. return "method";
  572. }
  573. default: break;
  574. }
  575. }
  576. return NULL; /* no useful name found */
  577. }
  578. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  579. Instruction i;
  580. if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
  581. return NULL; /* calling function is not Lua (or is unknown) */
  582. ci--; /* calling function */
  583. i = ci_func(ci)->l.p->code[currentpc(L, ci)];
  584. if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  585. GET_OPCODE(i) == OP_TFORLOOP)
  586. return getobjname(L, ci, GETARG_A(i), name);
  587. else
  588. return NULL; /* no useful name can be found */
  589. }
  590. /* only ANSI way to check whether a pointer points to an array */
  591. static int isinstack (CallInfo *ci, const TValue *o) {
  592. StkId p;
  593. for (p = ci->base; p < ci->top; p++)
  594. if (o == p) return 1;
  595. return 0;
  596. }
  597. void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
  598. const char *name = NULL;
  599. const char *t = luaT_typenames[ttype(o)];
  600. const char *kind = (isinstack(L->ci, o)) ?
  601. getobjname(L, L->ci, cast_int(o - L->base), &name) :
  602. NULL;
  603. if (kind)
  604. luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
  605. op, kind, name, t);
  606. else
  607. luaG_runerror(L, "attempt to %s a %s value", op, t);
  608. }
  609. void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
  610. if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
  611. lua_assert(!ttisstring(p1) && !ttisnumber(p1));
  612. luaG_typeerror(L, p1, "concatenate");
  613. }
  614. void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
  615. TValue temp;
  616. if (luaV_tonumber(p1, &temp) == NULL)
  617. p2 = p1; /* first operand is wrong */
  618. luaG_typeerror(L, p2, "perform arithmetic on");
  619. }
  620. int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  621. const char *t1 = luaT_typenames[ttype(p1)];
  622. const char *t2 = luaT_typenames[ttype(p2)];
  623. if (t1[2] == t2[2])
  624. luaG_runerror(L, "attempt to compare two %s values", t1);
  625. else
  626. luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
  627. return 0;
  628. }
  629. static void addinfo (lua_State *L, const char *msg) {
  630. CallInfo *ci = L->ci;
  631. if (isLua(ci)) { /* is Lua code? */
  632. char buff[LUA_IDSIZE]; /* add file:line information */
  633. int line = currentline(L, ci);
  634. luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
  635. luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
  636. }
  637. }
  638. void luaG_errormsg (lua_State *L) {
  639. if (L->errfunc != 0) { /* is there an error handling function? */
  640. StkId errfunc = restorestack(L, L->errfunc);
  641. if (!ttisfunction(errfunc) && !ttislightfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
  642. setobjs2s(L, L->top, L->top - 1); /* move argument */
  643. setobjs2s(L, L->top - 1, errfunc); /* push function */
  644. incr_top(L);
  645. luaD_call(L, L->top - 2, 1); /* call it */
  646. }
  647. luaD_throw(L, LUA_ERRRUN);
  648. }
  649. void luaG_runerror (lua_State *L, const char *fmt, ...) {
  650. va_list argp;
  651. va_start(argp, fmt);
  652. addinfo(L, luaO_pushvfstring(L, fmt, argp));
  653. va_end(argp);
  654. luaG_errormsg(L);
  655. }