3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
20 /* ------------------------------------------------------------------------ */
22 #define LJLIB_MODULE_debug
24 LJLIB_CF(debug_getregistry
)
26 copyTV(L
, L
->top
++, registry(L
));
30 LJLIB_CF(debug_getmetatable
)
32 lj_lib_checkany(L
, 1);
33 if (!lua_getmetatable(L
, 1)) {
39 LJLIB_CF(debug_setmetatable
)
41 lj_lib_checktabornil(L
, 2);
43 lua_setmetatable(L
, 1);
44 setboolV(L
->top
-1, 1);
48 LJLIB_CF(debug_getfenv
)
50 lj_lib_checkany(L
, 1);
55 LJLIB_CF(debug_setfenv
)
57 lj_lib_checktab(L
, 2);
59 if (!lua_setfenv(L
, 1))
60 lj_err_caller(L
, LJ_ERR_SETFENV
);
64 /* ------------------------------------------------------------------------ */
66 static void settabss(lua_State
*L
, const char *i
, const char *v
)
69 lua_setfield(L
, -2, i
);
72 static void settabsi(lua_State
*L
, const char *i
, int v
)
74 lua_pushinteger(L
, v
);
75 lua_setfield(L
, -2, i
);
78 static lua_State
*getthread(lua_State
*L
, int *arg
)
80 if (L
->base
< L
->top
&& tvisthread(L
->base
)) {
82 return threadV(L
->base
);
89 static void treatstackoption(lua_State
*L
, lua_State
*L1
, const char *fname
)
97 lua_setfield(L
, -2, fname
);
100 LJLIB_CF(debug_getinfo
)
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
)) {
111 } else if (L
->base
+arg
< L
->top
&& tvisfunc(L
->base
+arg
)) {
112 options
= lua_pushfstring(L
, ">%s", options
);
113 setfuncV(L1
, L1
->top
++, funcV(L
->base
+arg
));
115 lj_err_arg(L
, arg
+1, LJ_ERR_NOFUNCL
);
117 if (!lua_getinfo(L1
, options
, &ar
))
118 lj_err_arg(L
, arg
+2, LJ_ERR_INVOPT
);
119 lua_createtable(L
, 0, 16);
120 if (strchr(options
, 'S')) {
121 settabss(L
, "source", ar
.source
);
122 settabss(L
, "short_src", ar
.short_src
);
123 settabsi(L
, "linedefined", ar
.linedefined
);
124 settabsi(L
, "lastlinedefined", ar
.lastlinedefined
);
125 settabss(L
, "what", ar
.what
);
127 if (strchr(options
, 'l'))
128 settabsi(L
, "currentline", ar
.currentline
);
129 if (strchr(options
, 'u'))
130 settabsi(L
, "nups", ar
.nups
);
131 if (strchr(options
, 'n')) {
132 settabss(L
, "name", ar
.name
);
133 settabss(L
, "namewhat", ar
.namewhat
);
135 if (strchr(options
, 'L'))
136 treatstackoption(L
, L1
, "activelines");
137 if (strchr(options
, 'f'))
138 treatstackoption(L
, L1
, "func");
139 return 1; /* return table */
142 LJLIB_CF(debug_getlocal
)
145 lua_State
*L1
= getthread(L
, &arg
);
148 if (!lua_getstack(L1
, lj_lib_checkint(L
, arg
+1), &ar
))
149 lj_err_arg(L
, arg
+1, LJ_ERR_LVLRNG
);
150 name
= lua_getlocal(L1
, &ar
, lj_lib_checkint(L
, arg
+2));
153 lua_pushstring(L
, name
);
154 lua_pushvalue(L
, -2);
162 LJLIB_CF(debug_setlocal
)
165 lua_State
*L1
= getthread(L
, &arg
);
168 if (!lua_getstack(L1
, lj_lib_checkint(L
, arg
+1), &ar
))
169 lj_err_arg(L
, arg
+1, LJ_ERR_LVLRNG
);
170 tv
= lj_lib_checkany(L
, arg
+3);
171 copyTV(L1
, L1
->top
++, tv
);
172 lua_pushstring(L
, lua_setlocal(L1
, &ar
, lj_lib_checkint(L
, arg
+2)));
176 static int debug_getupvalue(lua_State
*L
, int get
)
178 int32_t n
= lj_lib_checkint(L
, 2);
179 if (isluafunc(lj_lib_checkfunc(L
, 1))) {
180 const char *name
= get
? lua_getupvalue(L
, 1, n
) : lua_setupvalue(L
, 1, n
);
182 lua_pushstring(L
, name
);
184 copyTV(L
, L
->top
, L
->top
-2);
192 LJLIB_CF(debug_getupvalue
)
194 return debug_getupvalue(L
, 1);
197 LJLIB_CF(debug_setupvalue
)
199 lj_lib_checkany(L
, 3);
200 return debug_getupvalue(L
, 0);
203 /* ------------------------------------------------------------------------ */
205 static const char KEY_HOOK
= 'h';
207 static void hookf(lua_State
*L
, lua_Debug
*ar
)
209 static const char *const hooknames
[] =
210 {"call", "return", "line", "count", "tail return"};
211 lua_pushlightuserdata(L
, (void *)&KEY_HOOK
);
212 lua_rawget(L
, LUA_REGISTRYINDEX
);
213 if (lua_isfunction(L
, -1)) {
214 lua_pushstring(L
, hooknames
[(int)ar
->event
]);
215 if (ar
->currentline
>= 0)
216 lua_pushinteger(L
, ar
->currentline
);
222 static int makemask(const char *smask
, int count
)
225 if (strchr(smask
, 'c')) mask
|= LUA_MASKCALL
;
226 if (strchr(smask
, 'r')) mask
|= LUA_MASKRET
;
227 if (strchr(smask
, 'l')) mask
|= LUA_MASKLINE
;
228 if (count
> 0) mask
|= LUA_MASKCOUNT
;
232 static char *unmakemask(int mask
, char *smask
)
235 if (mask
& LUA_MASKCALL
) smask
[i
++] = 'c';
236 if (mask
& LUA_MASKRET
) smask
[i
++] = 'r';
237 if (mask
& LUA_MASKLINE
) smask
[i
++] = 'l';
242 LJLIB_CF(debug_sethook
)
244 int arg
, mask
, count
;
246 (void)getthread(L
, &arg
);
247 if (lua_isnoneornil(L
, arg
+1)) {
248 lua_settop(L
, arg
+1);
249 func
= NULL
; mask
= 0; count
= 0; /* turn off hooks */
251 const char *smask
= luaL_checkstring(L
, arg
+2);
252 luaL_checktype(L
, arg
+1, LUA_TFUNCTION
);
253 count
= luaL_optint(L
, arg
+3, 0);
254 func
= hookf
; mask
= makemask(smask
, count
);
256 lua_pushlightuserdata(L
, (void *)&KEY_HOOK
);
257 lua_pushvalue(L
, arg
+1);
258 lua_rawset(L
, LUA_REGISTRYINDEX
);
259 lua_sethook(L
, func
, mask
, count
);
263 LJLIB_CF(debug_gethook
)
266 int mask
= lua_gethookmask(L
);
267 lua_Hook hook
= lua_gethook(L
);
268 if (hook
!= NULL
&& hook
!= hookf
) { /* external hook? */
269 lua_pushliteral(L
, "external hook");
271 lua_pushlightuserdata(L
, (void *)&KEY_HOOK
);
272 lua_rawget(L
, LUA_REGISTRYINDEX
); /* get hook */
274 lua_pushstring(L
, unmakemask(mask
, buff
));
275 lua_pushinteger(L
, lua_gethookcount(L
));
279 /* ------------------------------------------------------------------------ */
281 LJLIB_CF(debug_debug
)
285 fputs("lua_debug> ", stderr
);
286 if (fgets(buffer
, sizeof(buffer
), stdin
) == 0 ||
287 strcmp(buffer
, "cont\n") == 0)
289 if (luaL_loadbuffer(L
, buffer
, strlen(buffer
), "=(debug command)") ||
290 lua_pcall(L
, 0, 0, 0)) {
291 fputs(lua_tostring(L
, -1), stderr
);
294 lua_settop(L
, 0); /* remove eventual returns */
298 /* ------------------------------------------------------------------------ */
300 #define LEVELS1 12 /* size of the first part of the stack */
301 #define LEVELS2 10 /* size of the second part of the stack */
303 LJLIB_CF(debug_traceback
)
306 int firstpart
= 1; /* still before eventual `...' */
308 lua_State
*L1
= getthread(L
, &arg
);
310 if (lua_isnumber(L
, arg
+2)) {
311 level
= (int)lua_tointeger(L
, arg
+2);
315 level
= (L
== L1
) ? 1 : 0; /* level 0 may be this own function */
316 if (lua_gettop(L
) == arg
)
317 lua_pushliteral(L
, "");
318 else if (!lua_isstring(L
, arg
+1)) return 1; /* message is not a string */
319 else lua_pushliteral(L
, "\n");
320 lua_pushliteral(L
, "stack traceback:");
321 while (lua_getstack(L1
, level
++, &ar
)) {
322 if (level
> LEVELS1
&& firstpart
) {
323 /* no more than `LEVELS2' more levels? */
324 if (!lua_getstack(L1
, level
+LEVELS2
, &ar
)) {
325 level
--; /* keep going */
327 lua_pushliteral(L
, "\n\t..."); /* too many levels */
328 /* This only works with LuaJIT 2.x. Avoids O(n^2) behaviour. */
329 lua_getstack(L1
, -10, &ar
);
330 level
= ar
.i_ci
- LEVELS2
;
335 lua_pushliteral(L
, "\n\t");
336 lua_getinfo(L1
, "Snl", &ar
);
337 lua_pushfstring(L
, "%s:", ar
.short_src
);
338 if (ar
.currentline
> 0)
339 lua_pushfstring(L
, "%d:", ar
.currentline
);
340 if (*ar
.namewhat
!= '\0') { /* is there a name? */
341 lua_pushfstring(L
, " in function " LUA_QS
, ar
.name
);
343 if (*ar
.what
== 'm') /* main? */
344 lua_pushfstring(L
, " in main chunk");
345 else if (*ar
.what
== 'C' || *ar
.what
== 't')
346 lua_pushliteral(L
, " ?"); /* C function or tail call */
348 lua_pushfstring(L
, " in function <%s:%d>",
349 ar
.short_src
, ar
.linedefined
);
351 lua_concat(L
, lua_gettop(L
) - arg
);
353 lua_concat(L
, lua_gettop(L
) - arg
);
357 /* ------------------------------------------------------------------------ */
359 #include "lj_libdef.h"
361 LUALIB_API
int luaopen_debug(lua_State
*L
)
363 LJ_LIB_REG(L
, LUA_DBLIBNAME
, debug
);