-
Notifications
You must be signed in to change notification settings - Fork 2
/
lbci-5.1.c
187 lines (172 loc) · 4.43 KB
/
lbci-5.1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* lbci.c
* Lua bytecode inspector
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* 06 Mar 2009 07:59:10
* This code is hereby placed in the public domain.
*/
#define LUA_CORE
#include "lua.h"
#include "lauxlib.h"
#include "ljdetect/ljdetect.h"
#include "ldebug.h"
#include "lobject.h"
#include "lstate.h"
/* this allows lbci to be a shared library */
#define luaP_opnames lbci_opnames
#define luaP_opmodes lbci_opmodes
#include "lopcodes.c"
static const Proto* Pget(lua_State *L, int i)
{
if (lua_isuserdata(L,i)) return lua_touserdata(L,i);
if (!lua_isfunction(L,i) || lua_iscfunction(L,i))
luaL_typerror(L,i,"Lua function");
return ((Closure*)lua_topointer(L,i))->l.p;
}
static int do_getupvalue(lua_State *L) /** getupvalue(f,i) */
{
const Proto* f=Pget(L,1);
int i=luaL_checkinteger(L,2);
if (i<=0 || i>f->sizeupvalues || f->upvalues==NULL) return 0;
i--;
lua_pushstring(L,getstr(f->upvalues[i]));
return 1;
}
static int do_getlocal(lua_State *L) /** getlocal(f,i) */
{
const Proto* f=Pget(L,1);
int i=luaL_checkinteger(L,2);
if (i<=0 || i>f->sizelocvars || f->locvars==NULL) return 0;
i--;
lua_pushstring(L,getstr(f->locvars[i].varname));
lua_pushinteger(L,f->locvars[i].startpc+1);
lua_pushinteger(L,f->locvars[i].endpc+1);
return 3;
}
static int do_getconstant(lua_State *L) /** getconstant(f,i) */
{
const Proto* f=Pget(L,1);
int i=luaL_checkinteger(L,2);
if (i<=0 || i>f->sizek || f->k==NULL) return 0;
i--;
lua_pushnil(L);
L->top[-1]=f->k[i];
return 1;
}
static int do_getfunction(lua_State *L) /** getfunction(f,i) */
{
const Proto* f=Pget(L,1);
int i=luaL_checkinteger(L,2);
if (i<=0 || i>f->sizep) return 0;
i--;
lua_pushlightuserdata(L,f->p[i]);
return 1;
}
static int do_getinstruction(lua_State *L) /** getinstruction(f,i) */
{
const Proto* f=Pget(L,1);
int pc=luaL_checkinteger(L,2);
if (pc<=0 || pc>f->sizecode || f->code==NULL) return 0;
pc--;
{
const Instruction* code=f->code;
Instruction i=code[pc];
OpCode o=GET_OPCODE(i);
int a=GETARG_A(i);
int b=GETARG_B(i);
int c=GETARG_C(i);
int bx=GETARG_Bx(i);
int sbx=GETARG_sBx(i);
int line=getline(f,pc);
if (line>0) lua_pushinteger(L,line); else lua_pushnil(L);
lua_pushstring(L,luaP_opnames[o]);
switch (getOpMode(o))
{
case iABC:
lua_pushinteger(L,a);
if (getBMode(o)!=OpArgN) lua_pushinteger(L,ISK(b) ? (-1-INDEXK(b)) : b);
else lua_pushnil(L);
if (getCMode(o)!=OpArgN) lua_pushinteger(L,ISK(c) ? (-1-INDEXK(c)) : c);
else lua_pushnil(L);
break;
case iABx:
lua_pushinteger(L,a);
if (getBMode(o)==OpArgK) lua_pushinteger(L,-1-bx); else lua_pushinteger(L,bx);
lua_pushnil(L);
break;
case iAsBx:
if (o!=OP_JMP) lua_pushinteger(L,a);
lua_pushinteger(L,sbx);
lua_pushnil(L);
break;
}
switch (o)
{
case OP_JMP:
case OP_FORLOOP:
case OP_FORPREP:
lua_pop(L,1);
lua_pushinteger(L,sbx+pc+2);
lua_pushnil(L);
break;
default:
break;
}
}
return 5;
}
#define setsfield(L,n,v) lua_pushstring(L,v); lua_setfield(L,-2,n)
#define setifield(L,n,v) lua_pushinteger(L,v); lua_setfield(L,-2,n)
#define setbfield(L,n,v) lua_pushboolean(L,v); lua_setfield(L,-2,n)
static int do_getheader(lua_State *L) /** getheader(f,i) */
{
const Proto* f=Pget(L,1);
const char* s=getstr(f->source);
if (*s=='@' || *s=='=')
s++;
else if (*s==LUA_SIGNATURE[0])
s="(bstring)";
else
s="(string)";
lua_newtable(L);
setsfield(L,"source",s);
setifield(L,"line",f->linedefined);
setifield(L,"lastline",f->lastlinedefined);
setifield(L,"instructions",f->sizecode);
setifield(L,"params",f->numparams);
setbfield(L,"isvararg",f->is_vararg);
setifield(L,"slots",f->maxstacksize);
setifield(L,"upvalues",f->nups);
setifield(L,"locals",f->sizelocvars);
setifield(L,"constants",f->sizek);
setifield(L,"functions",f->sizep);
return 1;
}
static int do_setconstant(lua_State *L) /** setconstant(f,i,v) */
{
const Proto* f=Pget(L,1);
int i=luaL_checkinteger(L,2);
if (i<=0 || i>f->sizek || f->k==NULL) return 0;
i--;
lua_settop(L,3);
f->k[i]=L->top[-1];
return 0;
}
static const luaL_reg R[] =
{
{ "getconstant", do_getconstant },
{ "getfunction", do_getfunction },
{ "getheader", do_getheader },
{ "getinstruction", do_getinstruction },
{ "getlocal", do_getlocal },
{ "getupvalue", do_getupvalue },
{ "setconstant", do_setconstant },
{ NULL, NULL }
};
LUALIB_API int luaopen_bci(lua_State *L)
{
if (isluajit(L))
luaL_error(L, "LuaJIT is not supported by lbci");
luaL_register(L,"inspector",R);
return 1;
}