ldebug.c 20 KB

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