3 ** Interface from Lua to its debug API
4 ** See Copyright Notice in lua.h
24 ** The hook table at registry[HOOKKEY] maps threads to their current
27 static const char *const HOOKKEY
= "_HOOKKEY";
31 ** If L1 != L, L1 can be in any state, and therefore there are no
32 ** guarantees about its stack space; any push in L1 must be
35 static void checkstack (lua_State
*L
, lua_State
*L1
, int n
) {
36 if (l_unlikely(L
!= L1
&& !lua_checkstack(L1
, n
)))
37 luaL_error(L
, "stack overflow");
41 static int db_getregistry (lua_State
*L
) {
42 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
47 static int db_getmetatable (lua_State
*L
) {
49 if (!lua_getmetatable(L
, 1)) {
50 lua_pushnil(L
); /* no metatable */
56 static int db_setmetatable (lua_State
*L
) {
57 int t
= lua_type(L
, 2);
58 luaL_argexpected(L
, t
== LUA_TNIL
|| t
== LUA_TTABLE
, 2, "nil or table");
60 lua_setmetatable(L
, 1);
61 return 1; /* return 1st argument */
65 static int db_getuservalue (lua_State
*L
) {
66 int n
= (int)luaL_optinteger(L
, 2, 1);
67 if (lua_type(L
, 1) != LUA_TUSERDATA
)
69 else if (lua_getiuservalue(L
, 1, n
) != LUA_TNONE
) {
70 lua_pushboolean(L
, 1);
77 static int db_setuservalue (lua_State
*L
) {
78 int n
= (int)luaL_optinteger(L
, 3, 1);
79 luaL_checktype(L
, 1, LUA_TUSERDATA
);
82 if (!lua_setiuservalue(L
, 1, n
))
89 ** Auxiliary function used by several library functions: check for
90 ** an optional thread as function's first argument and set 'arg' with
91 ** 1 if this argument is present (so that functions can skip it to
92 ** access their other arguments)
94 static lua_State
*getthread (lua_State
*L
, int *arg
) {
95 if (lua_isthread(L
, 1)) {
97 return lua_tothread(L
, 1);
101 return L
; /* function will operate over current thread */
107 ** Variations of 'lua_settable', used by 'db_getinfo' to put results
108 ** from 'lua_getinfo' into result table. Key is always a string;
109 ** value can be a string, an int, or a boolean.
111 static void settabss (lua_State
*L
, const char *k
, const char *v
) {
112 lua_pushstring(L
, v
);
113 lua_setfield(L
, -2, k
);
116 static void settabsi (lua_State
*L
, const char *k
, int v
) {
117 lua_pushinteger(L
, v
);
118 lua_setfield(L
, -2, k
);
121 static void settabsb (lua_State
*L
, const char *k
, int v
) {
122 lua_pushboolean(L
, v
);
123 lua_setfield(L
, -2, k
);
128 ** In function 'db_getinfo', the call to 'lua_getinfo' may push
129 ** results on the stack; later it creates the result table to put
130 ** these objects. Function 'treatstackoption' puts the result from
131 ** 'lua_getinfo' on top of the result table so that it can call
134 static void treatstackoption (lua_State
*L
, lua_State
*L1
, const char *fname
) {
136 lua_rotate(L
, -2, 1); /* exchange object and table */
138 lua_xmove(L1
, L
, 1); /* move object to the "main" stack */
139 lua_setfield(L
, -2, fname
); /* put object into table */
144 ** Calls 'lua_getinfo' and collects all results in a new table.
145 ** L1 needs stack space for an optional input (function) plus
146 ** two optional outputs (function and line table) from function
149 static int db_getinfo (lua_State
*L
) {
152 lua_State
*L1
= getthread(L
, &arg
);
153 const char *options
= luaL_optstring(L
, arg
+2, "flnSrtu");
154 checkstack(L
, L1
, 3);
155 luaL_argcheck(L
, options
[0] != '>', arg
+ 2, "invalid option '>'");
156 if (lua_isfunction(L
, arg
+ 1)) { /* info about a function? */
157 options
= lua_pushfstring(L
, ">%s", options
); /* add '>' to 'options' */
158 lua_pushvalue(L
, arg
+ 1); /* move function to 'L1' stack */
161 else { /* stack level */
162 if (!lua_getstack(L1
, (int)luaL_checkinteger(L
, arg
+ 1), &ar
)) {
163 luaL_pushfail(L
); /* level out of range */
167 if (!lua_getinfo(L1
, options
, &ar
))
168 return luaL_argerror(L
, arg
+2, "invalid option");
169 lua_newtable(L
); /* table to collect results */
170 if (strchr(options
, 'S')) {
171 lua_pushlstring(L
, ar
.source
, ar
.srclen
);
172 lua_setfield(L
, -2, "source");
173 settabss(L
, "short_src", ar
.short_src
);
174 settabsi(L
, "linedefined", ar
.linedefined
);
175 settabsi(L
, "lastlinedefined", ar
.lastlinedefined
);
176 settabss(L
, "what", ar
.what
);
178 if (strchr(options
, 'l'))
179 settabsi(L
, "currentline", ar
.currentline
);
180 if (strchr(options
, 'u')) {
181 settabsi(L
, "nups", ar
.nups
);
182 settabsi(L
, "nparams", ar
.nparams
);
183 settabsb(L
, "isvararg", ar
.isvararg
);
185 if (strchr(options
, 'n')) {
186 settabss(L
, "name", ar
.name
);
187 settabss(L
, "namewhat", ar
.namewhat
);
189 if (strchr(options
, 'r')) {
190 settabsi(L
, "ftransfer", ar
.ftransfer
);
191 settabsi(L
, "ntransfer", ar
.ntransfer
);
193 if (strchr(options
, 't'))
194 settabsb(L
, "istailcall", ar
.istailcall
);
195 if (strchr(options
, 'L'))
196 treatstackoption(L
, L1
, "activelines");
197 if (strchr(options
, 'f'))
198 treatstackoption(L
, L1
, "func");
199 return 1; /* return table */
203 static int db_getlocal (lua_State
*L
) {
205 lua_State
*L1
= getthread(L
, &arg
);
206 int nvar
= (int)luaL_checkinteger(L
, arg
+ 2); /* local-variable index */
207 if (lua_isfunction(L
, arg
+ 1)) { /* function argument? */
208 lua_pushvalue(L
, arg
+ 1); /* push function */
209 lua_pushstring(L
, lua_getlocal(L
, NULL
, nvar
)); /* push local name */
210 return 1; /* return only name (there is no value) */
212 else { /* stack-level argument */
215 int level
= (int)luaL_checkinteger(L
, arg
+ 1);
216 if (l_unlikely(!lua_getstack(L1
, level
, &ar
))) /* out of range? */
217 return luaL_argerror(L
, arg
+1, "level out of range");
218 checkstack(L
, L1
, 1);
219 name
= lua_getlocal(L1
, &ar
, nvar
);
221 lua_xmove(L1
, L
, 1); /* move local value */
222 lua_pushstring(L
, name
); /* push name */
223 lua_rotate(L
, -2, 1); /* re-order */
227 luaL_pushfail(L
); /* no name (nor value) */
234 static int db_setlocal (lua_State
*L
) {
237 lua_State
*L1
= getthread(L
, &arg
);
239 int level
= (int)luaL_checkinteger(L
, arg
+ 1);
240 int nvar
= (int)luaL_checkinteger(L
, arg
+ 2);
241 if (l_unlikely(!lua_getstack(L1
, level
, &ar
))) /* out of range? */
242 return luaL_argerror(L
, arg
+1, "level out of range");
243 luaL_checkany(L
, arg
+3);
244 lua_settop(L
, arg
+3);
245 checkstack(L
, L1
, 1);
247 name
= lua_setlocal(L1
, &ar
, nvar
);
249 lua_pop(L1
, 1); /* pop value (if not popped by 'lua_setlocal') */
250 lua_pushstring(L
, name
);
256 ** get (if 'get' is true) or set an upvalue from a closure
258 static int auxupvalue (lua_State
*L
, int get
) {
260 int n
= (int)luaL_checkinteger(L
, 2); /* upvalue index */
261 luaL_checktype(L
, 1, LUA_TFUNCTION
); /* closure */
262 name
= get
? lua_getupvalue(L
, 1, n
) : lua_setupvalue(L
, 1, n
);
263 if (name
== NULL
) return 0;
264 lua_pushstring(L
, name
);
265 lua_insert(L
, -(get
+1)); /* no-op if get is false */
270 static int db_getupvalue (lua_State
*L
) {
271 return auxupvalue(L
, 1);
275 static int db_setupvalue (lua_State
*L
) {
277 return auxupvalue(L
, 0);
282 ** Check whether a given upvalue from a given closure exists and
285 static void *checkupval (lua_State
*L
, int argf
, int argnup
, int *pnup
) {
287 int nup
= (int)luaL_checkinteger(L
, argnup
); /* upvalue index */
288 luaL_checktype(L
, argf
, LUA_TFUNCTION
); /* closure */
289 id
= lua_upvalueid(L
, argf
, nup
);
291 luaL_argcheck(L
, id
!= NULL
, argnup
, "invalid upvalue index");
298 static int db_upvalueid (lua_State
*L
) {
299 void *id
= checkupval(L
, 1, 2, NULL
);
301 lua_pushlightuserdata(L
, id
);
308 static int db_upvaluejoin (lua_State
*L
) {
310 checkupval(L
, 1, 2, &n1
);
311 checkupval(L
, 3, 4, &n2
);
312 luaL_argcheck(L
, !lua_iscfunction(L
, 1), 1, "Lua function expected");
313 luaL_argcheck(L
, !lua_iscfunction(L
, 3), 3, "Lua function expected");
314 lua_upvaluejoin(L
, 1, n1
, 3, n2
);
320 ** Call hook function registered at hook table for the current
321 ** thread (if there is one)
323 static void hookf (lua_State
*L
, lua_Debug
*ar
) {
324 static const char *const hooknames
[] =
325 {"call", "return", "line", "count", "tail call"};
326 lua_getfield(L
, LUA_REGISTRYINDEX
, HOOKKEY
);
328 if (lua_rawget(L
, -2) == LUA_TFUNCTION
) { /* is there a hook function? */
329 lua_pushstring(L
, hooknames
[(int)ar
->event
]); /* push event name */
330 if (ar
->currentline
>= 0)
331 lua_pushinteger(L
, ar
->currentline
); /* push current line */
333 lua_assert(lua_getinfo(L
, "lS", ar
));
334 lua_call(L
, 2, 0); /* call hook function */
340 ** Convert a string mask (for 'sethook') into a bit mask
342 static int makemask (const char *smask
, int count
) {
344 if (strchr(smask
, 'c')) mask
|= LUA_MASKCALL
;
345 if (strchr(smask
, 'r')) mask
|= LUA_MASKRET
;
346 if (strchr(smask
, 'l')) mask
|= LUA_MASKLINE
;
347 if (count
> 0) mask
|= LUA_MASKCOUNT
;
353 ** Convert a bit mask (for 'gethook') into a string mask
355 static char *unmakemask (int mask
, char *smask
) {
357 if (mask
& LUA_MASKCALL
) smask
[i
++] = 'c';
358 if (mask
& LUA_MASKRET
) smask
[i
++] = 'r';
359 if (mask
& LUA_MASKLINE
) smask
[i
++] = 'l';
365 static int db_sethook (lua_State
*L
) {
366 int arg
, mask
, count
;
368 lua_State
*L1
= getthread(L
, &arg
);
369 if (lua_isnoneornil(L
, arg
+1)) { /* no hook? */
370 lua_settop(L
, arg
+1);
371 func
= NULL
; mask
= 0; count
= 0; /* turn off hooks */
374 const char *smask
= luaL_checkstring(L
, arg
+2);
375 luaL_checktype(L
, arg
+1, LUA_TFUNCTION
);
376 count
= (int)luaL_optinteger(L
, arg
+ 3, 0);
377 func
= hookf
; mask
= makemask(smask
, count
);
379 if (!luaL_getsubtable(L
, LUA_REGISTRYINDEX
, HOOKKEY
)) {
380 /* table just created; initialize it */
381 lua_pushliteral(L
, "k");
382 lua_setfield(L
, -2, "__mode"); /** hooktable.__mode = "k" */
383 lua_pushvalue(L
, -1);
384 lua_setmetatable(L
, -2); /* metatable(hooktable) = hooktable */
386 checkstack(L
, L1
, 1);
387 lua_pushthread(L1
); lua_xmove(L1
, L
, 1); /* key (thread) */
388 lua_pushvalue(L
, arg
+ 1); /* value (hook function) */
389 lua_rawset(L
, -3); /* hooktable[L1] = new Lua hook */
390 lua_sethook(L1
, func
, mask
, count
);
395 static int db_gethook (lua_State
*L
) {
397 lua_State
*L1
= getthread(L
, &arg
);
399 int mask
= lua_gethookmask(L1
);
400 lua_Hook hook
= lua_gethook(L1
);
401 if (hook
== NULL
) { /* no hook? */
405 else if (hook
!= hookf
) /* external hook? */
406 lua_pushliteral(L
, "external hook");
407 else { /* hook table must exist */
408 lua_getfield(L
, LUA_REGISTRYINDEX
, HOOKKEY
);
409 checkstack(L
, L1
, 1);
410 lua_pushthread(L1
); lua_xmove(L1
, L
, 1);
411 lua_rawget(L
, -2); /* 1st result = hooktable[L1] */
412 lua_remove(L
, -2); /* remove hook table */
414 lua_pushstring(L
, unmakemask(mask
, buff
)); /* 2nd result = mask */
415 lua_pushinteger(L
, lua_gethookcount(L1
)); /* 3rd result = count */
420 static int db_debug (lua_State
*L
) {
423 lua_writestringerror("%s", "lua_debug> ");
424 if (fgets(buffer
, sizeof(buffer
), stdin
) == NULL
||
425 strcmp(buffer
, "cont\n") == 0)
427 if (luaL_loadbuffer(L
, buffer
, strlen(buffer
), "=(debug command)") ||
428 lua_pcall(L
, 0, 0, 0))
429 lua_writestringerror("%s\n", luaL_tolstring(L
, -1, NULL
));
430 lua_settop(L
, 0); /* remove eventual returns */
435 static int db_traceback (lua_State
*L
) {
437 lua_State
*L1
= getthread(L
, &arg
);
438 const char *msg
= lua_tostring(L
, arg
+ 1);
439 if (msg
== NULL
&& !lua_isnoneornil(L
, arg
+ 1)) /* non-string 'msg'? */
440 lua_pushvalue(L
, arg
+ 1); /* return it untouched */
442 int level
= (int)luaL_optinteger(L
, arg
+ 2, (L
== L1
) ? 1 : 0);
443 luaL_traceback(L
, L1
, msg
, level
);
449 static int db_setcstacklimit (lua_State
*L
) {
450 int limit
= (int)luaL_checkinteger(L
, 1);
451 int res
= lua_setcstacklimit(L
, limit
);
452 lua_pushinteger(L
, res
);
457 static const luaL_Reg dblib
[] = {
459 {"getuservalue", db_getuservalue
},
460 {"gethook", db_gethook
},
461 {"getinfo", db_getinfo
},
462 {"getlocal", db_getlocal
},
463 {"getregistry", db_getregistry
},
464 {"getmetatable", db_getmetatable
},
465 {"getupvalue", db_getupvalue
},
466 {"upvaluejoin", db_upvaluejoin
},
467 {"upvalueid", db_upvalueid
},
468 {"setuservalue", db_setuservalue
},
469 {"sethook", db_sethook
},
470 {"setlocal", db_setlocal
},
471 {"setmetatable", db_setmetatable
},
472 {"setupvalue", db_setupvalue
},
473 {"traceback", db_traceback
},
474 {"setcstacklimit", db_setcstacklimit
},
479 LUAMOD_API
int luaopen_debug (lua_State
*L
) {
480 luaL_newlib(L
, dblib
);