1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy ** $Id: ldebug.c,v 2.90.1.4 2015/02/19 17:05:13 roberto Exp $
3eda14cbcSMatt Macy ** Debug Interface
4eda14cbcSMatt Macy ** See Copyright Notice in lua.h
5eda14cbcSMatt Macy */
6eda14cbcSMatt Macy
7eda14cbcSMatt Macy
8eda14cbcSMatt Macy #define ldebug_c
9eda14cbcSMatt Macy #define LUA_CORE
10eda14cbcSMatt Macy
11eda14cbcSMatt Macy #include <sys/lua/lua.h>
12eda14cbcSMatt Macy
13eda14cbcSMatt Macy #include "lapi.h"
14eda14cbcSMatt Macy #include "lcode.h"
15eda14cbcSMatt Macy #include "ldebug.h"
16eda14cbcSMatt Macy #include "ldo.h"
17eda14cbcSMatt Macy #include "lfunc.h"
18eda14cbcSMatt Macy #include "lobject.h"
19eda14cbcSMatt Macy #include "lopcodes.h"
20eda14cbcSMatt Macy #include "lstate.h"
21eda14cbcSMatt Macy #include "lstring.h"
22eda14cbcSMatt Macy #include "ltable.h"
23eda14cbcSMatt Macy #include "ltm.h"
24eda14cbcSMatt Macy #include "lvm.h"
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy
31eda14cbcSMatt Macy static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
32eda14cbcSMatt Macy
33eda14cbcSMatt Macy
currentpc(CallInfo * ci)34eda14cbcSMatt Macy static int currentpc (CallInfo *ci) {
35eda14cbcSMatt Macy lua_assert(isLua(ci));
36eda14cbcSMatt Macy return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
37eda14cbcSMatt Macy }
38eda14cbcSMatt Macy
39eda14cbcSMatt Macy
currentline(CallInfo * ci)40eda14cbcSMatt Macy static int currentline (CallInfo *ci) {
41eda14cbcSMatt Macy return getfuncline(ci_func(ci)->p, currentpc(ci));
42eda14cbcSMatt Macy }
43eda14cbcSMatt Macy
44eda14cbcSMatt Macy
swapextra(lua_State * L)45eda14cbcSMatt Macy static void swapextra (lua_State *L) {
46eda14cbcSMatt Macy if (L->status == LUA_YIELD) {
47eda14cbcSMatt Macy CallInfo *ci = L->ci; /* get function that yielded */
48eda14cbcSMatt Macy StkId temp = ci->func; /* exchange its 'func' and 'extra' values */
49eda14cbcSMatt Macy ci->func = restorestack(L, ci->extra);
50eda14cbcSMatt Macy ci->extra = savestack(L, temp);
51eda14cbcSMatt Macy }
52eda14cbcSMatt Macy }
53eda14cbcSMatt Macy
54eda14cbcSMatt Macy
55eda14cbcSMatt Macy /*
56eda14cbcSMatt Macy ** this function can be called asynchronous (e.g. during a signal)
57eda14cbcSMatt Macy */
lua_sethook(lua_State * L,lua_Hook func,int mask,int count)58eda14cbcSMatt Macy LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
59eda14cbcSMatt Macy if (func == NULL || mask == 0) { /* turn off hooks? */
60eda14cbcSMatt Macy mask = 0;
61eda14cbcSMatt Macy func = NULL;
62eda14cbcSMatt Macy }
63eda14cbcSMatt Macy if (isLua(L->ci))
64eda14cbcSMatt Macy L->oldpc = L->ci->u.l.savedpc;
65eda14cbcSMatt Macy L->hook = func;
66eda14cbcSMatt Macy L->basehookcount = count;
67eda14cbcSMatt Macy resethookcount(L);
68eda14cbcSMatt Macy L->hookmask = cast_byte(mask);
69eda14cbcSMatt Macy return 1;
70eda14cbcSMatt Macy }
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy
lua_gethook(lua_State * L)73eda14cbcSMatt Macy LUA_API lua_Hook lua_gethook (lua_State *L) {
74eda14cbcSMatt Macy return L->hook;
75eda14cbcSMatt Macy }
76eda14cbcSMatt Macy
77eda14cbcSMatt Macy
lua_gethookmask(lua_State * L)78eda14cbcSMatt Macy LUA_API int lua_gethookmask (lua_State *L) {
79eda14cbcSMatt Macy return L->hookmask;
80eda14cbcSMatt Macy }
81eda14cbcSMatt Macy
82eda14cbcSMatt Macy
lua_gethookcount(lua_State * L)83eda14cbcSMatt Macy LUA_API int lua_gethookcount (lua_State *L) {
84eda14cbcSMatt Macy return L->basehookcount;
85eda14cbcSMatt Macy }
86eda14cbcSMatt Macy
87eda14cbcSMatt Macy
lua_getstack(lua_State * L,int level,lua_Debug * ar)88eda14cbcSMatt Macy LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
89eda14cbcSMatt Macy int status;
90eda14cbcSMatt Macy CallInfo *ci;
91eda14cbcSMatt Macy if (level < 0) return 0; /* invalid (negative) level */
92eda14cbcSMatt Macy lua_lock(L);
93eda14cbcSMatt Macy for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
94eda14cbcSMatt Macy level--;
95eda14cbcSMatt Macy if (level == 0 && ci != &L->base_ci) { /* level found? */
96eda14cbcSMatt Macy status = 1;
97eda14cbcSMatt Macy ar->i_ci = ci;
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy else status = 0; /* no such level */
100eda14cbcSMatt Macy lua_unlock(L);
101eda14cbcSMatt Macy return status;
102eda14cbcSMatt Macy }
103eda14cbcSMatt Macy
104eda14cbcSMatt Macy
upvalname(Proto * p,int uv)105eda14cbcSMatt Macy static const char *upvalname (Proto *p, int uv) {
106eda14cbcSMatt Macy TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
107eda14cbcSMatt Macy if (s == NULL) return "?";
108eda14cbcSMatt Macy else return getstr(s);
109eda14cbcSMatt Macy }
110eda14cbcSMatt Macy
111eda14cbcSMatt Macy
findvararg(CallInfo * ci,int n,StkId * pos)112eda14cbcSMatt Macy static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
113eda14cbcSMatt Macy int nparams = clLvalue(ci->func)->p->numparams;
114*a4e5e010SMartin Matuska int nvararg = cast_int(ci->u.l.base - ci->func) - nparams;
115*a4e5e010SMartin Matuska if (n <= -nvararg)
116eda14cbcSMatt Macy return NULL; /* no such vararg */
117eda14cbcSMatt Macy else {
118*a4e5e010SMartin Matuska *pos = ci->func + nparams - n;
119eda14cbcSMatt Macy return "(*vararg)"; /* generic name for any vararg */
120eda14cbcSMatt Macy }
121eda14cbcSMatt Macy }
122eda14cbcSMatt Macy
123eda14cbcSMatt Macy
findlocal(lua_State * L,CallInfo * ci,int n,StkId * pos)124eda14cbcSMatt Macy static const char *findlocal (lua_State *L, CallInfo *ci, int n,
125eda14cbcSMatt Macy StkId *pos) {
126eda14cbcSMatt Macy const char *name = NULL;
127eda14cbcSMatt Macy StkId base;
128eda14cbcSMatt Macy if (isLua(ci)) {
129eda14cbcSMatt Macy if (n < 0) /* access to vararg values? */
130*a4e5e010SMartin Matuska return findvararg(ci, n, pos);
131eda14cbcSMatt Macy else {
132eda14cbcSMatt Macy base = ci->u.l.base;
133eda14cbcSMatt Macy name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
134eda14cbcSMatt Macy }
135eda14cbcSMatt Macy }
136eda14cbcSMatt Macy else
137eda14cbcSMatt Macy base = ci->func + 1;
138eda14cbcSMatt Macy if (name == NULL) { /* no 'standard' name? */
139eda14cbcSMatt Macy StkId limit = (ci == L->ci) ? L->top : ci->next->func;
140eda14cbcSMatt Macy if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
141eda14cbcSMatt Macy name = "(*temporary)"; /* generic name for any valid slot */
142eda14cbcSMatt Macy else
143eda14cbcSMatt Macy return NULL; /* no name */
144eda14cbcSMatt Macy }
145eda14cbcSMatt Macy *pos = base + (n - 1);
146eda14cbcSMatt Macy return name;
147eda14cbcSMatt Macy }
148eda14cbcSMatt Macy
149eda14cbcSMatt Macy
lua_getlocal(lua_State * L,const lua_Debug * ar,int n)150eda14cbcSMatt Macy LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
151eda14cbcSMatt Macy const char *name;
152eda14cbcSMatt Macy lua_lock(L);
153eda14cbcSMatt Macy swapextra(L);
154eda14cbcSMatt Macy if (ar == NULL) { /* information about non-active function? */
155eda14cbcSMatt Macy if (!isLfunction(L->top - 1)) /* not a Lua function? */
156eda14cbcSMatt Macy name = NULL;
157eda14cbcSMatt Macy else /* consider live variables at function start (parameters) */
158eda14cbcSMatt Macy name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
159eda14cbcSMatt Macy }
160eda14cbcSMatt Macy else { /* active function; get information through 'ar' */
161eda14cbcSMatt Macy StkId pos = 0; /* to avoid warnings */
162eda14cbcSMatt Macy name = findlocal(L, ar->i_ci, n, &pos);
163eda14cbcSMatt Macy if (name) {
164eda14cbcSMatt Macy setobj2s(L, L->top, pos);
165eda14cbcSMatt Macy api_incr_top(L);
166eda14cbcSMatt Macy }
167eda14cbcSMatt Macy }
168eda14cbcSMatt Macy swapextra(L);
169eda14cbcSMatt Macy lua_unlock(L);
170eda14cbcSMatt Macy return name;
171eda14cbcSMatt Macy }
172eda14cbcSMatt Macy
173eda14cbcSMatt Macy
lua_setlocal(lua_State * L,const lua_Debug * ar,int n)174eda14cbcSMatt Macy LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
175eda14cbcSMatt Macy StkId pos = 0; /* to avoid warnings */
176eda14cbcSMatt Macy const char *name;
177eda14cbcSMatt Macy lua_lock(L);
178eda14cbcSMatt Macy swapextra(L);
179eda14cbcSMatt Macy name = findlocal(L, ar->i_ci, n, &pos);
180eda14cbcSMatt Macy if (name)
181eda14cbcSMatt Macy setobjs2s(L, pos, L->top - 1);
182eda14cbcSMatt Macy L->top--; /* pop value */
183eda14cbcSMatt Macy swapextra(L);
184eda14cbcSMatt Macy lua_unlock(L);
185eda14cbcSMatt Macy return name;
186eda14cbcSMatt Macy }
187eda14cbcSMatt Macy
188eda14cbcSMatt Macy
funcinfo(lua_Debug * ar,Closure * cl)189eda14cbcSMatt Macy static void funcinfo (lua_Debug *ar, Closure *cl) {
190eda14cbcSMatt Macy if (noLuaClosure(cl)) {
191eda14cbcSMatt Macy ar->source = "=[C]";
192eda14cbcSMatt Macy ar->linedefined = -1;
193eda14cbcSMatt Macy ar->lastlinedefined = -1;
194eda14cbcSMatt Macy ar->what = "C";
195eda14cbcSMatt Macy }
196eda14cbcSMatt Macy else {
197eda14cbcSMatt Macy Proto *p = cl->l.p;
198eda14cbcSMatt Macy ar->source = p->source ? getstr(p->source) : "=?";
199eda14cbcSMatt Macy ar->linedefined = p->linedefined;
200eda14cbcSMatt Macy ar->lastlinedefined = p->lastlinedefined;
201eda14cbcSMatt Macy ar->what = (ar->linedefined == 0) ? "main" : "Lua";
202eda14cbcSMatt Macy }
203eda14cbcSMatt Macy luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
204eda14cbcSMatt Macy }
205eda14cbcSMatt Macy
206eda14cbcSMatt Macy
collectvalidlines(lua_State * L,Closure * f)207eda14cbcSMatt Macy static void collectvalidlines (lua_State *L, Closure *f) {
208eda14cbcSMatt Macy if (noLuaClosure(f)) {
209eda14cbcSMatt Macy setnilvalue(L->top);
210eda14cbcSMatt Macy api_incr_top(L);
211eda14cbcSMatt Macy }
212eda14cbcSMatt Macy else {
213eda14cbcSMatt Macy int i;
214eda14cbcSMatt Macy TValue v;
215eda14cbcSMatt Macy int *lineinfo = f->l.p->lineinfo;
216eda14cbcSMatt Macy Table *t = luaH_new(L); /* new table to store active lines */
217eda14cbcSMatt Macy sethvalue(L, L->top, t); /* push it on stack */
218eda14cbcSMatt Macy api_incr_top(L);
219eda14cbcSMatt Macy setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
220eda14cbcSMatt Macy for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
221eda14cbcSMatt Macy luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
222eda14cbcSMatt Macy }
223eda14cbcSMatt Macy }
224eda14cbcSMatt Macy
225eda14cbcSMatt Macy
auxgetinfo(lua_State * L,const char * what,lua_Debug * ar,Closure * f,CallInfo * ci)226eda14cbcSMatt Macy static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
227eda14cbcSMatt Macy Closure *f, CallInfo *ci) {
228eda14cbcSMatt Macy int status = 1;
229eda14cbcSMatt Macy for (; *what; what++) {
230eda14cbcSMatt Macy switch (*what) {
231eda14cbcSMatt Macy case 'S': {
232eda14cbcSMatt Macy funcinfo(ar, f);
233eda14cbcSMatt Macy break;
234eda14cbcSMatt Macy }
235eda14cbcSMatt Macy case 'l': {
236eda14cbcSMatt Macy ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
237eda14cbcSMatt Macy break;
238eda14cbcSMatt Macy }
239eda14cbcSMatt Macy case 'u': {
240eda14cbcSMatt Macy ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
241eda14cbcSMatt Macy if (noLuaClosure(f)) {
242eda14cbcSMatt Macy ar->isvararg = 1;
243eda14cbcSMatt Macy ar->nparams = 0;
244eda14cbcSMatt Macy }
245eda14cbcSMatt Macy else {
246eda14cbcSMatt Macy ar->isvararg = f->l.p->is_vararg;
247eda14cbcSMatt Macy ar->nparams = f->l.p->numparams;
248eda14cbcSMatt Macy }
249eda14cbcSMatt Macy break;
250eda14cbcSMatt Macy }
251eda14cbcSMatt Macy case 't': {
252eda14cbcSMatt Macy ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
253eda14cbcSMatt Macy break;
254eda14cbcSMatt Macy }
255eda14cbcSMatt Macy case 'n': {
256eda14cbcSMatt Macy /* calling function is a known Lua function? */
257eda14cbcSMatt Macy if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
258eda14cbcSMatt Macy ar->namewhat = getfuncname(L, ci->previous, &ar->name);
259eda14cbcSMatt Macy else
260eda14cbcSMatt Macy ar->namewhat = NULL;
261eda14cbcSMatt Macy if (ar->namewhat == NULL) {
262eda14cbcSMatt Macy ar->namewhat = ""; /* not found */
263eda14cbcSMatt Macy ar->name = NULL;
264eda14cbcSMatt Macy }
265eda14cbcSMatt Macy break;
266eda14cbcSMatt Macy }
267eda14cbcSMatt Macy case 'L':
268eda14cbcSMatt Macy case 'f': /* handled by lua_getinfo */
269eda14cbcSMatt Macy break;
270eda14cbcSMatt Macy default: status = 0; /* invalid option */
271eda14cbcSMatt Macy }
272eda14cbcSMatt Macy }
273eda14cbcSMatt Macy return status;
274eda14cbcSMatt Macy }
275eda14cbcSMatt Macy
276eda14cbcSMatt Macy
lua_getinfo(lua_State * L,const char * what,lua_Debug * ar)277eda14cbcSMatt Macy LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
278eda14cbcSMatt Macy int status;
279eda14cbcSMatt Macy Closure *cl;
280eda14cbcSMatt Macy CallInfo *ci;
281eda14cbcSMatt Macy StkId func;
282eda14cbcSMatt Macy lua_lock(L);
283eda14cbcSMatt Macy swapextra(L);
284eda14cbcSMatt Macy if (*what == '>') {
285eda14cbcSMatt Macy ci = NULL;
286eda14cbcSMatt Macy func = L->top - 1;
287eda14cbcSMatt Macy api_check(L, ttisfunction(func), "function expected");
288eda14cbcSMatt Macy what++; /* skip the '>' */
289eda14cbcSMatt Macy L->top--; /* pop function */
290eda14cbcSMatt Macy }
291eda14cbcSMatt Macy else {
292eda14cbcSMatt Macy ci = ar->i_ci;
293eda14cbcSMatt Macy func = ci->func;
294eda14cbcSMatt Macy lua_assert(ttisfunction(ci->func));
295eda14cbcSMatt Macy }
296eda14cbcSMatt Macy cl = ttisclosure(func) ? clvalue(func) : NULL;
297eda14cbcSMatt Macy status = auxgetinfo(L, what, ar, cl, ci);
298eda14cbcSMatt Macy if (strchr(what, 'f')) {
299eda14cbcSMatt Macy setobjs2s(L, L->top, func);
300eda14cbcSMatt Macy api_incr_top(L);
301eda14cbcSMatt Macy }
302eda14cbcSMatt Macy swapextra(L);
303eda14cbcSMatt Macy if (strchr(what, 'L'))
304eda14cbcSMatt Macy collectvalidlines(L, cl);
305eda14cbcSMatt Macy lua_unlock(L);
306eda14cbcSMatt Macy return status;
307eda14cbcSMatt Macy }
308eda14cbcSMatt Macy
309eda14cbcSMatt Macy
310eda14cbcSMatt Macy /*
311eda14cbcSMatt Macy ** {======================================================
312eda14cbcSMatt Macy ** Symbolic Execution
313eda14cbcSMatt Macy ** =======================================================
314eda14cbcSMatt Macy */
315eda14cbcSMatt Macy
316eda14cbcSMatt Macy static const char *getobjname (Proto *p, int lastpc, int reg,
317eda14cbcSMatt Macy const char **name);
318eda14cbcSMatt Macy
319eda14cbcSMatt Macy
320eda14cbcSMatt Macy /*
321eda14cbcSMatt Macy ** find a "name" for the RK value 'c'
322eda14cbcSMatt Macy */
kname(Proto * p,int pc,int c,const char ** name)323eda14cbcSMatt Macy static void kname (Proto *p, int pc, int c, const char **name) {
324eda14cbcSMatt Macy if (ISK(c)) { /* is 'c' a constant? */
325eda14cbcSMatt Macy TValue *kvalue = &p->k[INDEXK(c)];
326eda14cbcSMatt Macy if (ttisstring(kvalue)) { /* literal constant? */
327eda14cbcSMatt Macy *name = svalue(kvalue); /* it is its own name */
328eda14cbcSMatt Macy return;
329eda14cbcSMatt Macy }
330eda14cbcSMatt Macy /* else no reasonable name found */
331eda14cbcSMatt Macy }
332eda14cbcSMatt Macy else { /* 'c' is a register */
333eda14cbcSMatt Macy const char *what = getobjname(p, pc, c, name); /* search for 'c' */
334eda14cbcSMatt Macy if (what && *what == 'c') { /* found a constant name? */
335eda14cbcSMatt Macy return; /* 'name' already filled */
336eda14cbcSMatt Macy }
337eda14cbcSMatt Macy /* else no reasonable name found */
338eda14cbcSMatt Macy }
339eda14cbcSMatt Macy *name = "?"; /* no reasonable name found */
340eda14cbcSMatt Macy }
341eda14cbcSMatt Macy
342eda14cbcSMatt Macy
filterpc(int pc,int jmptarget)343eda14cbcSMatt Macy static int filterpc (int pc, int jmptarget) {
344eda14cbcSMatt Macy if (pc < jmptarget) /* is code conditional (inside a jump)? */
345eda14cbcSMatt Macy return -1; /* cannot know who sets that register */
346eda14cbcSMatt Macy else return pc; /* current position sets that register */
347eda14cbcSMatt Macy }
348eda14cbcSMatt Macy
349eda14cbcSMatt Macy
350eda14cbcSMatt Macy /*
351eda14cbcSMatt Macy ** try to find last instruction before 'lastpc' that modified register 'reg'
352eda14cbcSMatt Macy */
findsetreg(Proto * p,int lastpc,int reg)353eda14cbcSMatt Macy static int findsetreg (Proto *p, int lastpc, int reg) {
354eda14cbcSMatt Macy int pc;
355eda14cbcSMatt Macy int setreg = -1; /* keep last instruction that changed 'reg' */
356eda14cbcSMatt Macy int jmptarget = 0; /* any code before this address is conditional */
357eda14cbcSMatt Macy for (pc = 0; pc < lastpc; pc++) {
358eda14cbcSMatt Macy Instruction i = p->code[pc];
359eda14cbcSMatt Macy OpCode op = GET_OPCODE(i);
360eda14cbcSMatt Macy int a = GETARG_A(i);
361eda14cbcSMatt Macy switch (op) {
362eda14cbcSMatt Macy case OP_LOADNIL: {
363eda14cbcSMatt Macy int b = GETARG_B(i);
364eda14cbcSMatt Macy if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
365eda14cbcSMatt Macy setreg = filterpc(pc, jmptarget);
366eda14cbcSMatt Macy break;
367eda14cbcSMatt Macy }
368eda14cbcSMatt Macy case OP_TFORCALL: {
369eda14cbcSMatt Macy if (reg >= a + 2) /* affect all regs above its base */
370eda14cbcSMatt Macy setreg = filterpc(pc, jmptarget);
371eda14cbcSMatt Macy break;
372eda14cbcSMatt Macy }
373eda14cbcSMatt Macy case OP_CALL:
374eda14cbcSMatt Macy case OP_TAILCALL: {
375eda14cbcSMatt Macy if (reg >= a) /* affect all registers above base */
376eda14cbcSMatt Macy setreg = filterpc(pc, jmptarget);
377eda14cbcSMatt Macy break;
378eda14cbcSMatt Macy }
379eda14cbcSMatt Macy case OP_JMP: {
380eda14cbcSMatt Macy int b = GETARG_sBx(i);
381eda14cbcSMatt Macy int dest = pc + 1 + b;
382eda14cbcSMatt Macy /* jump is forward and do not skip `lastpc'? */
383eda14cbcSMatt Macy if (pc < dest && dest <= lastpc) {
384eda14cbcSMatt Macy if (dest > jmptarget)
385eda14cbcSMatt Macy jmptarget = dest; /* update 'jmptarget' */
386eda14cbcSMatt Macy }
387eda14cbcSMatt Macy break;
388eda14cbcSMatt Macy }
389eda14cbcSMatt Macy case OP_TEST: {
390eda14cbcSMatt Macy if (reg == a) /* jumped code can change 'a' */
391eda14cbcSMatt Macy setreg = filterpc(pc, jmptarget);
392eda14cbcSMatt Macy break;
393eda14cbcSMatt Macy }
394eda14cbcSMatt Macy default:
395eda14cbcSMatt Macy if (testAMode(op) && reg == a) /* any instruction that set A */
396eda14cbcSMatt Macy setreg = filterpc(pc, jmptarget);
397eda14cbcSMatt Macy break;
398eda14cbcSMatt Macy }
399eda14cbcSMatt Macy }
400eda14cbcSMatt Macy return setreg;
401eda14cbcSMatt Macy }
402eda14cbcSMatt Macy
403eda14cbcSMatt Macy
getobjname(Proto * p,int lastpc,int reg,const char ** name)404eda14cbcSMatt Macy static const char *getobjname (Proto *p, int lastpc, int reg,
405eda14cbcSMatt Macy const char **name) {
406eda14cbcSMatt Macy int pc;
407eda14cbcSMatt Macy *name = luaF_getlocalname(p, reg + 1, lastpc);
408eda14cbcSMatt Macy if (*name) /* is a local? */
409eda14cbcSMatt Macy return "local";
410eda14cbcSMatt Macy /* else try symbolic execution */
411eda14cbcSMatt Macy pc = findsetreg(p, lastpc, reg);
412eda14cbcSMatt Macy if (pc != -1) { /* could find instruction? */
413eda14cbcSMatt Macy Instruction i = p->code[pc];
414eda14cbcSMatt Macy OpCode op = GET_OPCODE(i);
415eda14cbcSMatt Macy switch (op) {
416eda14cbcSMatt Macy case OP_MOVE: {
417eda14cbcSMatt Macy int b = GETARG_B(i); /* move from 'b' to 'a' */
418eda14cbcSMatt Macy if (b < GETARG_A(i))
419eda14cbcSMatt Macy return getobjname(p, pc, b, name); /* get name for 'b' */
420eda14cbcSMatt Macy break;
421eda14cbcSMatt Macy }
422eda14cbcSMatt Macy case OP_GETTABUP:
423eda14cbcSMatt Macy case OP_GETTABLE: {
424eda14cbcSMatt Macy int k = GETARG_C(i); /* key index */
425eda14cbcSMatt Macy int t = GETARG_B(i); /* table index */
426eda14cbcSMatt Macy const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
427eda14cbcSMatt Macy ? luaF_getlocalname(p, t + 1, pc)
428eda14cbcSMatt Macy : upvalname(p, t);
429eda14cbcSMatt Macy kname(p, pc, k, name);
430eda14cbcSMatt Macy return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
431eda14cbcSMatt Macy }
432eda14cbcSMatt Macy case OP_GETUPVAL: {
433eda14cbcSMatt Macy *name = upvalname(p, GETARG_B(i));
434eda14cbcSMatt Macy return "upvalue";
435eda14cbcSMatt Macy }
436eda14cbcSMatt Macy case OP_LOADK:
437eda14cbcSMatt Macy case OP_LOADKX: {
438eda14cbcSMatt Macy int b = (op == OP_LOADK) ? GETARG_Bx(i)
439eda14cbcSMatt Macy : GETARG_Ax(p->code[pc + 1]);
440eda14cbcSMatt Macy if (ttisstring(&p->k[b])) {
441eda14cbcSMatt Macy *name = svalue(&p->k[b]);
442eda14cbcSMatt Macy return "constant";
443eda14cbcSMatt Macy }
444eda14cbcSMatt Macy break;
445eda14cbcSMatt Macy }
446eda14cbcSMatt Macy case OP_SELF: {
447eda14cbcSMatt Macy int k = GETARG_C(i); /* key index */
448eda14cbcSMatt Macy kname(p, pc, k, name);
449eda14cbcSMatt Macy return "method";
450eda14cbcSMatt Macy }
451eda14cbcSMatt Macy default: break; /* go through to return NULL */
452eda14cbcSMatt Macy }
453eda14cbcSMatt Macy }
454eda14cbcSMatt Macy return NULL; /* could not find reasonable name */
455eda14cbcSMatt Macy }
456eda14cbcSMatt Macy
457eda14cbcSMatt Macy
getfuncname(lua_State * L,CallInfo * ci,const char ** name)458eda14cbcSMatt Macy static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
459eda14cbcSMatt Macy TMS tm;
460eda14cbcSMatt Macy Proto *p = ci_func(ci)->p; /* calling function */
461eda14cbcSMatt Macy int pc = currentpc(ci); /* calling instruction index */
462eda14cbcSMatt Macy Instruction i = p->code[pc]; /* calling instruction */
463eda14cbcSMatt Macy switch (GET_OPCODE(i)) {
464eda14cbcSMatt Macy case OP_CALL:
465eda14cbcSMatt Macy case OP_TAILCALL: /* get function name */
466eda14cbcSMatt Macy return getobjname(p, pc, GETARG_A(i), name);
467eda14cbcSMatt Macy case OP_TFORCALL: { /* for iterator */
468eda14cbcSMatt Macy *name = "for iterator";
469eda14cbcSMatt Macy return "for iterator";
470eda14cbcSMatt Macy }
471eda14cbcSMatt Macy /* all other instructions can call only through metamethods */
472eda14cbcSMatt Macy case OP_SELF:
473eda14cbcSMatt Macy case OP_GETTABUP:
474eda14cbcSMatt Macy case OP_GETTABLE: tm = TM_INDEX; break;
475eda14cbcSMatt Macy case OP_SETTABUP:
476eda14cbcSMatt Macy case OP_SETTABLE: tm = TM_NEWINDEX; break;
477eda14cbcSMatt Macy case OP_EQ: tm = TM_EQ; break;
478eda14cbcSMatt Macy case OP_ADD: tm = TM_ADD; break;
479eda14cbcSMatt Macy case OP_SUB: tm = TM_SUB; break;
480eda14cbcSMatt Macy case OP_MUL: tm = TM_MUL; break;
481eda14cbcSMatt Macy case OP_DIV: tm = TM_DIV; break;
482eda14cbcSMatt Macy case OP_MOD: tm = TM_MOD; break;
483eda14cbcSMatt Macy case OP_POW: tm = TM_POW; break;
484eda14cbcSMatt Macy case OP_UNM: tm = TM_UNM; break;
485eda14cbcSMatt Macy case OP_LEN: tm = TM_LEN; break;
486eda14cbcSMatt Macy case OP_LT: tm = TM_LT; break;
487eda14cbcSMatt Macy case OP_LE: tm = TM_LE; break;
488eda14cbcSMatt Macy case OP_CONCAT: tm = TM_CONCAT; break;
489eda14cbcSMatt Macy default:
490eda14cbcSMatt Macy return NULL; /* else no useful name can be found */
491eda14cbcSMatt Macy }
492eda14cbcSMatt Macy *name = getstr(G(L)->tmname[tm]);
493eda14cbcSMatt Macy return "metamethod";
494eda14cbcSMatt Macy }
495eda14cbcSMatt Macy
496eda14cbcSMatt Macy /* }====================================================== */
497eda14cbcSMatt Macy
498eda14cbcSMatt Macy
499eda14cbcSMatt Macy
500eda14cbcSMatt Macy /*
501eda14cbcSMatt Macy ** only ANSI way to check whether a pointer points to an array
502eda14cbcSMatt Macy ** (used only for error messages, so efficiency is not a big concern)
503eda14cbcSMatt Macy */
isinstack(CallInfo * ci,const TValue * o)504eda14cbcSMatt Macy static int isinstack (CallInfo *ci, const TValue *o) {
505eda14cbcSMatt Macy StkId p;
506eda14cbcSMatt Macy for (p = ci->u.l.base; p < ci->top; p++)
507eda14cbcSMatt Macy if (o == p) return 1;
508eda14cbcSMatt Macy return 0;
509eda14cbcSMatt Macy }
510eda14cbcSMatt Macy
511eda14cbcSMatt Macy
getupvalname(CallInfo * ci,const TValue * o,const char ** name)512eda14cbcSMatt Macy static const char *getupvalname (CallInfo *ci, const TValue *o,
513eda14cbcSMatt Macy const char **name) {
514eda14cbcSMatt Macy LClosure *c = ci_func(ci);
515eda14cbcSMatt Macy int i;
516eda14cbcSMatt Macy for (i = 0; i < c->nupvalues; i++) {
517eda14cbcSMatt Macy if (c->upvals[i]->v == o) {
518eda14cbcSMatt Macy *name = upvalname(c->p, i);
519eda14cbcSMatt Macy return "upvalue";
520eda14cbcSMatt Macy }
521eda14cbcSMatt Macy }
522eda14cbcSMatt Macy return NULL;
523eda14cbcSMatt Macy }
524eda14cbcSMatt Macy
525eda14cbcSMatt Macy
luaG_typeerror(lua_State * L,const TValue * o,const char * op)526eda14cbcSMatt Macy l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
527eda14cbcSMatt Macy CallInfo *ci = L->ci;
528eda14cbcSMatt Macy const char *name = NULL;
529eda14cbcSMatt Macy const char *t = objtypename(o);
530eda14cbcSMatt Macy const char *kind = NULL;
531eda14cbcSMatt Macy if (isLua(ci)) {
532eda14cbcSMatt Macy kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
533eda14cbcSMatt Macy if (!kind && isinstack(ci, o)) /* no? try a register */
534eda14cbcSMatt Macy kind = getobjname(ci_func(ci)->p, currentpc(ci),
535eda14cbcSMatt Macy cast_int(o - ci->u.l.base), &name);
536eda14cbcSMatt Macy }
537eda14cbcSMatt Macy if (kind)
538eda14cbcSMatt Macy luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
539eda14cbcSMatt Macy op, kind, name, t);
540eda14cbcSMatt Macy else
541eda14cbcSMatt Macy luaG_runerror(L, "attempt to %s a %s value", op, t);
542eda14cbcSMatt Macy }
543eda14cbcSMatt Macy
544eda14cbcSMatt Macy
luaG_concaterror(lua_State * L,StkId p1,StkId p2)545eda14cbcSMatt Macy l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
546eda14cbcSMatt Macy if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
547eda14cbcSMatt Macy lua_assert(!ttisstring(p1) && !ttisnumber(p1));
548eda14cbcSMatt Macy luaG_typeerror(L, p1, "concatenate");
549eda14cbcSMatt Macy }
550eda14cbcSMatt Macy
551eda14cbcSMatt Macy
luaG_aritherror(lua_State * L,const TValue * p1,const TValue * p2)552eda14cbcSMatt Macy l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
553eda14cbcSMatt Macy TValue temp;
554eda14cbcSMatt Macy if (luaV_tonumber(p1, &temp) == NULL)
555eda14cbcSMatt Macy p2 = p1; /* first operand is wrong */
556eda14cbcSMatt Macy luaG_typeerror(L, p2, "perform arithmetic on");
557eda14cbcSMatt Macy }
558eda14cbcSMatt Macy
559eda14cbcSMatt Macy
luaG_ordererror(lua_State * L,const TValue * p1,const TValue * p2)560eda14cbcSMatt Macy l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
561eda14cbcSMatt Macy const char *t1 = objtypename(p1);
562eda14cbcSMatt Macy const char *t2 = objtypename(p2);
563eda14cbcSMatt Macy if (t1 == t2)
564eda14cbcSMatt Macy luaG_runerror(L, "attempt to compare two %s values", t1);
565eda14cbcSMatt Macy else
566eda14cbcSMatt Macy luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
567eda14cbcSMatt Macy }
568eda14cbcSMatt Macy
569eda14cbcSMatt Macy
addinfo(lua_State * L,const char * msg)570eda14cbcSMatt Macy static void addinfo (lua_State *L, const char *msg) {
571eda14cbcSMatt Macy CallInfo *ci = L->ci;
572eda14cbcSMatt Macy if (isLua(ci)) { /* is Lua code? */
573eda14cbcSMatt Macy char buff[LUA_IDSIZE]; /* add file:line information */
574eda14cbcSMatt Macy int line = currentline(ci);
575eda14cbcSMatt Macy TString *src = ci_func(ci)->p->source;
576eda14cbcSMatt Macy if (src)
577eda14cbcSMatt Macy luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
578eda14cbcSMatt Macy else { /* no source available; use "?" instead */
579eda14cbcSMatt Macy buff[0] = '?'; buff[1] = '\0';
580eda14cbcSMatt Macy }
581eda14cbcSMatt Macy luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
582eda14cbcSMatt Macy }
583eda14cbcSMatt Macy }
584eda14cbcSMatt Macy
585eda14cbcSMatt Macy
luaG_errormsg(lua_State * L)586eda14cbcSMatt Macy l_noret luaG_errormsg (lua_State *L) {
587eda14cbcSMatt Macy if (L->errfunc != 0) { /* is there an error handling function? */
588eda14cbcSMatt Macy StkId errfunc = restorestack(L, L->errfunc);
589eda14cbcSMatt Macy if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
590eda14cbcSMatt Macy setobjs2s(L, L->top, L->top - 1); /* move argument */
591eda14cbcSMatt Macy setobjs2s(L, L->top - 1, errfunc); /* push function */
592eda14cbcSMatt Macy L->top++;
593eda14cbcSMatt Macy luaD_call(L, L->top - 2, 1, 0); /* call it */
594eda14cbcSMatt Macy }
595eda14cbcSMatt Macy luaD_throw(L, LUA_ERRRUN);
596eda14cbcSMatt Macy }
597eda14cbcSMatt Macy
598eda14cbcSMatt Macy
luaG_runerror(lua_State * L,const char * fmt,...)599eda14cbcSMatt Macy l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
600eda14cbcSMatt Macy L->runerror++;
601eda14cbcSMatt Macy va_list argp;
602eda14cbcSMatt Macy va_start(argp, fmt);
603eda14cbcSMatt Macy addinfo(L, luaO_pushvfstring(L, fmt, argp));
604eda14cbcSMatt Macy va_end(argp);
605eda14cbcSMatt Macy luaG_errormsg(L);
606eda14cbcSMatt Macy L->runerror--;
607eda14cbcSMatt Macy }
608