1 /* $NetBSD: ldblib.c,v 1.1.1.2 2012/03/15 00:08:10 alnsn Exp $ */
4 ** $Id: ldblib.c,v 1.1.1.2 2012/03/15 00:08:10 alnsn Exp $
5 ** Interface from Lua to its debug API
6 ** See Copyright Notice in lua.h
24 static int db_getregistry (lua_State
*L
) {
25 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
30 static int db_getmetatable (lua_State
*L
) {
32 if (!lua_getmetatable(L
, 1)) {
33 lua_pushnil(L
); /* no metatable */
39 static int db_setmetatable (lua_State
*L
) {
40 int t
= lua_type(L
, 2);
41 luaL_argcheck(L
, t
== LUA_TNIL
|| t
== LUA_TTABLE
, 2,
42 "nil or table expected");
44 lua_pushboolean(L
, lua_setmetatable(L
, 1));
49 static int db_getfenv (lua_State
*L
) {
56 static int db_setfenv (lua_State
*L
) {
57 luaL_checktype(L
, 2, LUA_TTABLE
);
59 if (lua_setfenv(L
, 1) == 0)
60 luaL_error(L
, LUA_QL("setfenv")
61 " cannot change environment of given object");
66 static void settabss (lua_State
*L
, const char *i
, const char *v
) {
68 lua_setfield(L
, -2, i
);
72 static void settabsi (lua_State
*L
, const char *i
, int v
) {
73 lua_pushinteger(L
, v
);
74 lua_setfield(L
, -2, i
);
78 static lua_State
*getthread (lua_State
*L
, int *arg
) {
79 if (lua_isthread(L
, 1)) {
81 return lua_tothread(L
, 1);
90 static void treatstackoption (lua_State
*L
, lua_State
*L1
, const char *fname
) {
97 lua_setfield(L
, -2, fname
);
101 static int db_getinfo (lua_State
*L
) {
104 lua_State
*L1
= getthread(L
, &arg
);
105 const char *options
= luaL_optstring(L
, arg
+2, "flnSu");
106 if (lua_isnumber(L
, arg
+1)) {
107 if (!lua_getstack(L1
, (int)lua_tointeger(L
, arg
+1), &ar
)) {
108 lua_pushnil(L
); /* level out of range */
112 else if (lua_isfunction(L
, arg
+1)) {
113 lua_pushfstring(L
, ">%s", options
);
114 options
= lua_tostring(L
, -1);
115 lua_pushvalue(L
, arg
+1);
119 return luaL_argerror(L
, arg
+1, "function or level expected");
120 if (!lua_getinfo(L1
, options
, &ar
))
121 return luaL_argerror(L
, arg
+2, "invalid option");
122 lua_createtable(L
, 0, 2);
123 if (strchr(options
, 'S')) {
124 settabss(L
, "source", ar
.source
);
125 settabss(L
, "short_src", ar
.short_src
);
126 settabsi(L
, "linedefined", ar
.linedefined
);
127 settabsi(L
, "lastlinedefined", ar
.lastlinedefined
);
128 settabss(L
, "what", ar
.what
);
130 if (strchr(options
, 'l'))
131 settabsi(L
, "currentline", ar
.currentline
);
132 if (strchr(options
, 'u'))
133 settabsi(L
, "nups", ar
.nups
);
134 if (strchr(options
, 'n')) {
135 settabss(L
, "name", ar
.name
);
136 settabss(L
, "namewhat", ar
.namewhat
);
138 if (strchr(options
, 'L'))
139 treatstackoption(L
, L1
, "activelines");
140 if (strchr(options
, 'f'))
141 treatstackoption(L
, L1
, "func");
142 return 1; /* return table */
146 static int db_getlocal (lua_State
*L
) {
148 lua_State
*L1
= getthread(L
, &arg
);
151 if (!lua_getstack(L1
, luaL_checkint(L
, arg
+1), &ar
)) /* out of range? */
152 return luaL_argerror(L
, arg
+1, "level out of range");
153 name
= lua_getlocal(L1
, &ar
, luaL_checkint(L
, arg
+2));
156 lua_pushstring(L
, name
);
157 lua_pushvalue(L
, -2);
167 static int db_setlocal (lua_State
*L
) {
169 lua_State
*L1
= getthread(L
, &arg
);
171 if (!lua_getstack(L1
, luaL_checkint(L
, arg
+1), &ar
)) /* out of range? */
172 return luaL_argerror(L
, arg
+1, "level out of range");
173 luaL_checkany(L
, arg
+3);
174 lua_settop(L
, arg
+3);
176 lua_pushstring(L
, lua_setlocal(L1
, &ar
, luaL_checkint(L
, arg
+2)));
181 static int auxupvalue (lua_State
*L
, int get
) {
183 int n
= luaL_checkint(L
, 2);
184 luaL_checktype(L
, 1, LUA_TFUNCTION
);
185 if (lua_iscfunction(L
, 1)) return 0; /* cannot touch C upvalues from Lua */
186 name
= get
? lua_getupvalue(L
, 1, n
) : lua_setupvalue(L
, 1, n
);
187 if (name
== NULL
) return 0;
188 lua_pushstring(L
, name
);
189 lua_insert(L
, -(get
+1));
194 static int db_getupvalue (lua_State
*L
) {
195 return auxupvalue(L
, 1);
199 static int db_setupvalue (lua_State
*L
) {
201 return auxupvalue(L
, 0);
206 static const char KEY_HOOK
= 'h';
209 static void hookf (lua_State
*L
, lua_Debug
*ar
) {
210 static const char *const hooknames
[] =
211 {"call", "return", "line", "count", "tail return"};
212 lua_pushlightuserdata(L
, (void *)&KEY_HOOK
);
213 lua_rawget(L
, LUA_REGISTRYINDEX
);
214 lua_pushlightuserdata(L
, L
);
216 if (lua_isfunction(L
, -1)) {
217 lua_pushstring(L
, hooknames
[(int)ar
->event
]);
218 if (ar
->currentline
>= 0)
219 lua_pushinteger(L
, ar
->currentline
);
221 lua_assert(lua_getinfo(L
, "lS", ar
));
227 static int makemask (const char *smask
, int count
) {
229 if (strchr(smask
, 'c')) mask
|= LUA_MASKCALL
;
230 if (strchr(smask
, 'r')) mask
|= LUA_MASKRET
;
231 if (strchr(smask
, 'l')) mask
|= LUA_MASKLINE
;
232 if (count
> 0) mask
|= LUA_MASKCOUNT
;
237 static char *unmakemask (int mask
, char *smask
) {
239 if (mask
& LUA_MASKCALL
) smask
[i
++] = 'c';
240 if (mask
& LUA_MASKRET
) smask
[i
++] = 'r';
241 if (mask
& LUA_MASKLINE
) smask
[i
++] = 'l';
247 static void gethooktable (lua_State
*L
) {
248 lua_pushlightuserdata(L
, (void *)&KEY_HOOK
);
249 lua_rawget(L
, LUA_REGISTRYINDEX
);
250 if (!lua_istable(L
, -1)) {
252 lua_createtable(L
, 0, 1);
253 lua_pushlightuserdata(L
, (void *)&KEY_HOOK
);
254 lua_pushvalue(L
, -2);
255 lua_rawset(L
, LUA_REGISTRYINDEX
);
260 static int db_sethook (lua_State
*L
) {
261 int arg
, mask
, count
;
263 lua_State
*L1
= getthread(L
, &arg
);
264 if (lua_isnoneornil(L
, arg
+1)) {
265 lua_settop(L
, arg
+1);
266 func
= NULL
; mask
= 0; count
= 0; /* turn off hooks */
269 const char *smask
= luaL_checkstring(L
, arg
+2);
270 luaL_checktype(L
, arg
+1, LUA_TFUNCTION
);
271 count
= luaL_optint(L
, arg
+3, 0);
272 func
= hookf
; mask
= makemask(smask
, count
);
275 lua_pushlightuserdata(L
, L1
);
276 lua_pushvalue(L
, arg
+1);
277 lua_rawset(L
, -3); /* set new hook */
278 lua_pop(L
, 1); /* remove hook table */
279 lua_sethook(L1
, func
, mask
, count
); /* set hooks */
284 static int db_gethook (lua_State
*L
) {
286 lua_State
*L1
= getthread(L
, &arg
);
288 int mask
= lua_gethookmask(L1
);
289 lua_Hook hook
= lua_gethook(L1
);
290 if (hook
!= NULL
&& hook
!= hookf
) /* external hook? */
291 lua_pushliteral(L
, "external hook");
294 lua_pushlightuserdata(L
, L1
);
295 lua_rawget(L
, -2); /* get hook */
296 lua_remove(L
, -2); /* remove hook table */
298 lua_pushstring(L
, unmakemask(mask
, buff
));
299 lua_pushinteger(L
, lua_gethookcount(L1
));
304 static int db_debug (lua_State
*L
) {
307 fputs("lua_debug> ", stderr
);
308 if (fgets(buffer
, sizeof(buffer
), stdin
) == 0 ||
309 strcmp(buffer
, "cont\n") == 0)
311 if (luaL_loadbuffer(L
, buffer
, strlen(buffer
), "=(debug command)") ||
312 lua_pcall(L
, 0, 0, 0)) {
313 fputs(lua_tostring(L
, -1), stderr
);
316 lua_settop(L
, 0); /* remove eventual returns */
321 #define LEVELS1 12 /* size of the first part of the stack */
322 #define LEVELS2 10 /* size of the second part of the stack */
324 static int db_errorfb (lua_State
*L
) {
326 int firstpart
= 1; /* still before eventual `...' */
328 lua_State
*L1
= getthread(L
, &arg
);
330 if (lua_isnumber(L
, arg
+2)) {
331 level
= (int)lua_tointeger(L
, arg
+2);
335 level
= (L
== L1
) ? 1 : 0; /* level 0 may be this own function */
336 if (lua_gettop(L
) == arg
)
337 lua_pushliteral(L
, "");
338 else if (!lua_isstring(L
, arg
+1)) return 1; /* message is not a string */
339 else lua_pushliteral(L
, "\n");
340 lua_pushliteral(L
, "stack traceback:");
341 while (lua_getstack(L1
, level
++, &ar
)) {
342 if (level
> LEVELS1
&& firstpart
) {
343 /* no more than `LEVELS2' more levels? */
344 if (!lua_getstack(L1
, level
+LEVELS2
, &ar
))
345 level
--; /* keep going */
347 lua_pushliteral(L
, "\n\t..."); /* too many levels */
348 while (lua_getstack(L1
, level
+LEVELS2
, &ar
)) /* find last levels */
354 lua_pushliteral(L
, "\n\t");
355 lua_getinfo(L1
, "Snl", &ar
);
356 lua_pushfstring(L
, "%s:", ar
.short_src
);
357 if (ar
.currentline
> 0)
358 lua_pushfstring(L
, "%d:", ar
.currentline
);
359 if (*ar
.namewhat
!= '\0') /* is there a name? */
360 lua_pushfstring(L
, " in function " LUA_QS
, ar
.name
);
362 if (*ar
.what
== 'm') /* main? */
363 lua_pushfstring(L
, " in main chunk");
364 else if (*ar
.what
== 'C' || *ar
.what
== 't')
365 lua_pushliteral(L
, " ?"); /* C function or tail call */
367 lua_pushfstring(L
, " in function <%s:%d>",
368 ar
.short_src
, ar
.linedefined
);
370 lua_concat(L
, lua_gettop(L
) - arg
);
372 lua_concat(L
, lua_gettop(L
) - arg
);
377 static const luaL_Reg dblib
[] = {
379 {"getfenv", db_getfenv
},
380 {"gethook", db_gethook
},
381 {"getinfo", db_getinfo
},
382 {"getlocal", db_getlocal
},
383 {"getregistry", db_getregistry
},
384 {"getmetatable", db_getmetatable
},
385 {"getupvalue", db_getupvalue
},
386 {"setfenv", db_setfenv
},
387 {"sethook", db_sethook
},
388 {"setlocal", db_setlocal
},
389 {"setmetatable", db_setmetatable
},
390 {"setupvalue", db_setupvalue
},
391 {"traceback", db_errorfb
},
396 LUALIB_API
int luaopen_debug (lua_State
*L
) {
397 luaL_register(L
, LUA_DBLIBNAME
, dblib
);