2 ** $Id: ldblib.c,v 1.132 2012/01/19 20:14:44 roberto Exp $
3 ** Interface from Lua to its debug API
4 ** See Copyright Notice in lua.h
21 #define HOOKKEY "_HKEY"
25 static int db_getregistry(lua_State
*L
) {
26 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
31 static int db_getmetatable(lua_State
*L
) {
33 if (!lua_getmetatable(L
, 1)) {
34 lua_pushnil(L
); /* no metatable */
40 static int db_setmetatable(lua_State
*L
) {
41 int t
= lua_type(L
, 2);
42 luaL_argcheck(L
, t
== LUA_TNIL
|| t
== LUA_TTABLE
, 2,
43 "nil or table expected");
45 lua_setmetatable(L
, 1);
46 return 1; /* return 1st argument */
50 static int db_getuservalue(lua_State
*L
) {
51 if (lua_type(L
, 1) != LUA_TUSERDATA
)
54 lua_getuservalue(L
, 1);
59 static int db_setuservalue(lua_State
*L
) {
60 if (lua_type(L
, 1) == LUA_TLIGHTUSERDATA
)
61 luaL_argerror(L
, 1, "full userdata expected, got light userdata");
62 luaL_checktype(L
, 1, LUA_TUSERDATA
);
63 if (!lua_isnoneornil(L
, 2))
64 luaL_checktype(L
, 2, LUA_TTABLE
);
66 lua_setuservalue(L
, 1);
71 static void settabss(lua_State
*L
, const char *i
, const char *v
) {
73 lua_setfield(L
, -2, i
);
77 static void settabsi(lua_State
*L
, const char *i
, int v
) {
78 lua_pushinteger(L
, v
);
79 lua_setfield(L
, -2, i
);
83 static void settabsb(lua_State
*L
, const char *i
, int v
) {
84 lua_pushboolean(L
, v
);
85 lua_setfield(L
, -2, i
);
89 static lua_State
*getthread(lua_State
*L
, int *arg
) {
90 if (lua_isthread(L
, 1)) {
92 return lua_tothread(L
, 1);
100 static void treatstackoption(lua_State
*L
, lua_State
*L1
, const char *fname
) {
102 lua_pushvalue(L
, -2);
106 lua_setfield(L
, -2, fname
);
110 static int db_getinfo(lua_State
*L
) {
113 lua_State
*L1
= getthread(L
, &arg
);
114 const char *options
= luaL_optstring(L
, arg
+ 2, "flnStu");
115 if (lua_isnumber(L
, arg
+ 1)) {
116 if (!lua_getstack(L1
, (int)lua_tointeger(L
, arg
+ 1), &ar
)) {
117 lua_pushnil(L
); /* level out of range */
120 } else if (lua_isfunction(L
, arg
+ 1)) {
121 lua_pushfstring(L
, ">%s", options
);
122 options
= lua_tostring(L
, -1);
123 lua_pushvalue(L
, arg
+ 1);
126 return luaL_argerror(L
, arg
+ 1, "function or level expected");
127 if (!lua_getinfo(L1
, options
, &ar
))
128 return luaL_argerror(L
, arg
+ 2, "invalid option");
129 lua_createtable(L
, 0, 2);
130 if (strchr(options
, 'S')) {
131 settabss(L
, "source", ar
.source
);
132 settabss(L
, "short_src", ar
.short_src
);
133 settabsi(L
, "linedefined", ar
.linedefined
);
134 settabsi(L
, "lastlinedefined", ar
.lastlinedefined
);
135 settabss(L
, "what", ar
.what
);
137 if (strchr(options
, 'l'))
138 settabsi(L
, "currentline", ar
.currentline
);
139 if (strchr(options
, 'u')) {
140 settabsi(L
, "nups", ar
.nups
);
141 settabsi(L
, "nparams", ar
.nparams
);
142 settabsb(L
, "isvararg", ar
.isvararg
);
144 if (strchr(options
, 'n')) {
145 settabss(L
, "name", ar
.name
);
146 settabss(L
, "namewhat", ar
.namewhat
);
148 if (strchr(options
, 't'))
149 settabsb(L
, "istailcall", ar
.istailcall
);
150 if (strchr(options
, 'L'))
151 treatstackoption(L
, L1
, "activelines");
152 if (strchr(options
, 'f'))
153 treatstackoption(L
, L1
, "func");
154 return 1; /* return table */
158 static int db_getlocal(lua_State
*L
) {
160 lua_State
*L1
= getthread(L
, &arg
);
163 int nvar
= luaL_checkint(L
, arg
+ 2); /* local-variable index */
164 if (lua_isfunction(L
, arg
+ 1)) { /* function argument? */
165 lua_pushvalue(L
, arg
+ 1); /* push function */
166 lua_pushstring(L
, lua_getlocal(L
, NULL
, nvar
)); /* push local name */
168 } else { /* stack-level argument */
169 if (!lua_getstack(L1
, luaL_checkint(L
, arg
+ 1), &ar
)) /* out of range? */
170 return luaL_argerror(L
, arg
+ 1, "level out of range");
171 name
= lua_getlocal(L1
, &ar
, nvar
);
173 lua_xmove(L1
, L
, 1); /* push local value */
174 lua_pushstring(L
, name
); /* push name */
175 lua_pushvalue(L
, -2); /* re-order */
178 lua_pushnil(L
); /* no name (nor value) */
185 static int db_setlocal(lua_State
*L
) {
187 lua_State
*L1
= getthread(L
, &arg
);
189 if (!lua_getstack(L1
, luaL_checkint(L
, arg
+ 1), &ar
)) /* out of range? */
190 return luaL_argerror(L
, arg
+ 1, "level out of range");
191 luaL_checkany(L
, arg
+ 3);
192 lua_settop(L
, arg
+ 3);
194 lua_pushstring(L
, lua_setlocal(L1
, &ar
, luaL_checkint(L
, arg
+ 2)));
199 static int auxupvalue(lua_State
*L
, int get
) {
201 int n
= luaL_checkint(L
, 2);
202 luaL_checktype(L
, 1, LUA_TFUNCTION
);
203 name
= get
? lua_getupvalue(L
, 1, n
) : lua_setupvalue(L
, 1, n
);
204 if (name
== NULL
) return 0;
205 lua_pushstring(L
, name
);
206 lua_insert(L
, -(get
+ 1));
211 static int db_getupvalue(lua_State
*L
) {
212 return auxupvalue(L
, 1);
216 static int db_setupvalue(lua_State
*L
) {
218 return auxupvalue(L
, 0);
222 static int checkupval(lua_State
*L
, int argf
, int argnup
) {
224 int nup
= luaL_checkint(L
, argnup
);
225 luaL_checktype(L
, argf
, LUA_TFUNCTION
);
226 lua_pushvalue(L
, argf
);
227 lua_getinfo(L
, ">u", &ar
);
228 luaL_argcheck(L
, 1 <= nup
&& nup
<= ar
.nups
, argnup
, "invalid upvalue index");
233 static int db_upvalueid(lua_State
*L
) {
234 int n
= checkupval(L
, 1, 2);
235 lua_pushlightuserdata(L
, lua_upvalueid(L
, 1, n
));
240 static int db_upvaluejoin(lua_State
*L
) {
241 int n1
= checkupval(L
, 1, 2);
242 int n2
= checkupval(L
, 3, 4);
243 luaL_argcheck(L
, !lua_iscfunction(L
, 1), 1, "Lua function expected");
244 luaL_argcheck(L
, !lua_iscfunction(L
, 3), 3, "Lua function expected");
245 lua_upvaluejoin(L
, 1, n1
, 3, n2
);
250 #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
253 static void hookf(lua_State
*L
, lua_Debug
*ar
) {
254 static const char *const hooknames
[] =
255 {"call", "return", "line", "count", "tail call"};
259 if (lua_isfunction(L
, -1)) {
260 lua_pushstring(L
, hooknames
[(int)ar
->event
]);
261 if (ar
->currentline
>= 0)
262 lua_pushinteger(L
, ar
->currentline
);
264 lua_assert(lua_getinfo(L
, "lS", ar
));
270 static int makemask(const char *smask
, int count
) {
272 if (strchr(smask
, 'c')) mask
|= LUA_MASKCALL
;
273 if (strchr(smask
, 'r')) mask
|= LUA_MASKRET
;
274 if (strchr(smask
, 'l')) mask
|= LUA_MASKLINE
;
275 if (count
> 0) mask
|= LUA_MASKCOUNT
;
280 static char *unmakemask(int mask
, char *smask
) {
282 if (mask
& LUA_MASKCALL
) smask
[i
++] = 'c';
283 if (mask
& LUA_MASKRET
) smask
[i
++] = 'r';
284 if (mask
& LUA_MASKLINE
) smask
[i
++] = 'l';
290 static int db_sethook(lua_State
*L
) {
291 int arg
, mask
, count
;
293 lua_State
*L1
= getthread(L
, &arg
);
294 if (lua_isnoneornil(L
, arg
+ 1)) {
295 lua_settop(L
, arg
+ 1);
298 count
= 0; /* turn off hooks */
300 const char *smask
= luaL_checkstring(L
, arg
+ 2);
301 luaL_checktype(L
, arg
+ 1, LUA_TFUNCTION
);
302 count
= luaL_optint(L
, arg
+ 3, 0);
304 mask
= makemask(smask
, count
);
306 if (gethooktable(L
) == 0) { /* creating hook table? */
307 lua_pushstring(L
, "k");
308 lua_setfield(L
, -2, "__mode"); /** hooktable.__mode = "k" */
309 lua_pushvalue(L
, -1);
310 lua_setmetatable(L
, -2); /* setmetatable(hooktable) = hooktable */
314 lua_pushvalue(L
, arg
+ 1);
315 lua_rawset(L
, -3); /* set new hook */
316 lua_sethook(L1
, func
, mask
, count
); /* set hooks */
321 static int db_gethook(lua_State
*L
) {
323 lua_State
*L1
= getthread(L
, &arg
);
325 int mask
= lua_gethookmask(L1
);
326 lua_Hook hook
= lua_gethook(L1
);
327 if (hook
!= NULL
&& hook
!= hookf
) /* external hook? */
328 lua_pushliteral(L
, "external hook");
333 lua_rawget(L
, -2); /* get hook */
334 lua_remove(L
, -2); /* remove hook table */
336 lua_pushstring(L
, unmakemask(mask
, buff
));
337 lua_pushinteger(L
, lua_gethookcount(L1
));
342 static int db_debug(lua_State
*L
) {
345 luai_writestringerror("%s", "lua_debug> ");
346 if (fgets(buffer
, sizeof(buffer
), stdin
) == 0 ||
347 strcmp(buffer
, "cont\n") == 0)
349 if (luaL_loadbuffer(L
, buffer
, strlen(buffer
), "=(debug command)") ||
350 lua_pcall(L
, 0, 0, 0))
351 luai_writestringerror("%s\n", lua_tostring(L
, -1));
352 lua_settop(L
, 0); /* remove eventual returns */
357 static int db_traceback(lua_State
*L
) {
359 lua_State
*L1
= getthread(L
, &arg
);
360 const char *msg
= lua_tostring(L
, arg
+ 1);
361 if (msg
== NULL
&& !lua_isnoneornil(L
, arg
+ 1)) /* non-string 'msg'? */
362 lua_pushvalue(L
, arg
+ 1); /* return it untouched */
364 int level
= luaL_optint(L
, arg
+ 2, (L
== L1
) ? 1 : 0);
365 luaL_traceback(L
, L1
, msg
, level
);
371 static const luaL_Reg dblib
[] = {
373 {"getuservalue", db_getuservalue
},
374 {"gethook", db_gethook
},
375 {"getinfo", db_getinfo
},
376 {"getlocal", db_getlocal
},
377 {"getregistry", db_getregistry
},
378 {"getmetatable", db_getmetatable
},
379 {"getupvalue", db_getupvalue
},
380 {"upvaluejoin", db_upvaluejoin
},
381 {"upvalueid", db_upvalueid
},
382 {"setuservalue", db_setuservalue
},
383 {"sethook", db_sethook
},
384 {"setlocal", db_setlocal
},
385 {"setmetatable", db_setmetatable
},
386 {"setupvalue", db_setupvalue
},
387 {"traceback", db_traceback
},
392 LUAMOD_API
int luaopen_debug(lua_State
*L
) {
393 luaL_newlib(L
, dblib
);