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