1*f0dad708Snikita /* $NetBSD: ldblib.c,v 1.12 2023/04/16 20:46:17 nikita Exp $ */
2dbec5304Smbalmer
3dbec5304Smbalmer /*
4*f0dad708Snikita ** Id: ldblib.c
5dbec5304Smbalmer ** Interface from Lua to its debug API
6dbec5304Smbalmer ** See Copyright Notice in lua.h
7dbec5304Smbalmer */
8dbec5304Smbalmer
973008250Slneto #define ldblib_c
1073008250Slneto #define LUA_LIB
1173008250Slneto
1273008250Slneto #include "lprefix.h"
1373008250Slneto
14dbec5304Smbalmer
156953723dSlneto #ifndef _KERNEL
16dbec5304Smbalmer #include <stdio.h>
17dbec5304Smbalmer #include <stdlib.h>
18dbec5304Smbalmer #include <string.h>
192d6cb6c2Slneto #endif /* _KERNEL */
20dbec5304Smbalmer
21dbec5304Smbalmer #include "lua.h"
22dbec5304Smbalmer
23dbec5304Smbalmer #include "lauxlib.h"
24dbec5304Smbalmer #include "lualib.h"
25dbec5304Smbalmer
26dbec5304Smbalmer
2773008250Slneto /*
28*f0dad708Snikita ** The hook table at registry[HOOKKEY] maps threads to their current
29*f0dad708Snikita ** hook function.
3073008250Slneto */
31*f0dad708Snikita static const char *const HOOKKEY = "_HOOKKEY";
324ab4902eSlneto
33dbec5304Smbalmer
34b2829499Smbalmer /*
352d6cb6c2Slneto ** If L1 != L, L1 can be in any state, and therefore there are no
362d6cb6c2Slneto ** guarantees about its stack space; any push in L1 must be
37b2829499Smbalmer ** checked.
38b2829499Smbalmer */
checkstack(lua_State * L,lua_State * L1,int n)39b2829499Smbalmer static void checkstack (lua_State *L, lua_State *L1, int n) {
40*f0dad708Snikita if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
41b2829499Smbalmer luaL_error(L, "stack overflow");
42b2829499Smbalmer }
43b2829499Smbalmer
44b2829499Smbalmer
db_getregistry(lua_State * L)45dbec5304Smbalmer static int db_getregistry (lua_State *L) {
46dbec5304Smbalmer lua_pushvalue(L, LUA_REGISTRYINDEX);
47dbec5304Smbalmer return 1;
48dbec5304Smbalmer }
49dbec5304Smbalmer
50dbec5304Smbalmer
db_getmetatable(lua_State * L)51dbec5304Smbalmer static int db_getmetatable (lua_State *L) {
52dbec5304Smbalmer luaL_checkany(L, 1);
53dbec5304Smbalmer if (!lua_getmetatable(L, 1)) {
54dbec5304Smbalmer lua_pushnil(L); /* no metatable */
55dbec5304Smbalmer }
56dbec5304Smbalmer return 1;
57dbec5304Smbalmer }
58dbec5304Smbalmer
59dbec5304Smbalmer
db_setmetatable(lua_State * L)60dbec5304Smbalmer static int db_setmetatable (lua_State *L) {
61dbec5304Smbalmer int t = lua_type(L, 2);
62*f0dad708Snikita luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
63dbec5304Smbalmer lua_settop(L, 2);
644ab4902eSlneto lua_setmetatable(L, 1);
654ab4902eSlneto return 1; /* return 1st argument */
664ab4902eSlneto }
674ab4902eSlneto
684ab4902eSlneto
db_getuservalue(lua_State * L)694ab4902eSlneto static int db_getuservalue (lua_State *L) {
70*f0dad708Snikita int n = (int)luaL_optinteger(L, 2, 1);
714ab4902eSlneto if (lua_type(L, 1) != LUA_TUSERDATA)
72*f0dad708Snikita luaL_pushfail(L);
73*f0dad708Snikita else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
74*f0dad708Snikita lua_pushboolean(L, 1);
75*f0dad708Snikita return 2;
76*f0dad708Snikita }
77dbec5304Smbalmer return 1;
78dbec5304Smbalmer }
79dbec5304Smbalmer
80dbec5304Smbalmer
db_setuservalue(lua_State * L)814ab4902eSlneto static int db_setuservalue (lua_State *L) {
82*f0dad708Snikita int n = (int)luaL_optinteger(L, 3, 1);
834ab4902eSlneto luaL_checktype(L, 1, LUA_TUSERDATA);
844ab4902eSlneto luaL_checkany(L, 2);
85dbec5304Smbalmer lua_settop(L, 2);
86*f0dad708Snikita if (!lua_setiuservalue(L, 1, n))
87*f0dad708Snikita luaL_pushfail(L);
88dbec5304Smbalmer return 1;
89dbec5304Smbalmer }
90dbec5304Smbalmer
91dbec5304Smbalmer
924ab4902eSlneto /*
934ab4902eSlneto ** Auxiliary function used by several library functions: check for
944ab4902eSlneto ** an optional thread as function's first argument and set 'arg' with
954ab4902eSlneto ** 1 if this argument is present (so that functions can skip it to
964ab4902eSlneto ** access their other arguments)
974ab4902eSlneto */
getthread(lua_State * L,int * arg)98dbec5304Smbalmer static lua_State *getthread (lua_State *L, int *arg) {
99dbec5304Smbalmer if (lua_isthread(L, 1)) {
100dbec5304Smbalmer *arg = 1;
101dbec5304Smbalmer return lua_tothread(L, 1);
102dbec5304Smbalmer }
103dbec5304Smbalmer else {
104dbec5304Smbalmer *arg = 0;
1054ab4902eSlneto return L; /* function will operate over current thread */
106dbec5304Smbalmer }
107dbec5304Smbalmer }
108dbec5304Smbalmer
109dbec5304Smbalmer
1104ab4902eSlneto /*
1114ab4902eSlneto ** Variations of 'lua_settable', used by 'db_getinfo' to put results
1124ab4902eSlneto ** from 'lua_getinfo' into result table. Key is always a string;
1134ab4902eSlneto ** value can be a string, an int, or a boolean.
1144ab4902eSlneto */
settabss(lua_State * L,const char * k,const char * v)1154ab4902eSlneto static void settabss (lua_State *L, const char *k, const char *v) {
1164ab4902eSlneto lua_pushstring(L, v);
1174ab4902eSlneto lua_setfield(L, -2, k);
1184ab4902eSlneto }
1194ab4902eSlneto
settabsi(lua_State * L,const char * k,int v)1204ab4902eSlneto static void settabsi (lua_State *L, const char *k, int v) {
1214ab4902eSlneto lua_pushinteger(L, v);
1224ab4902eSlneto lua_setfield(L, -2, k);
1234ab4902eSlneto }
1244ab4902eSlneto
settabsb(lua_State * L,const char * k,int v)1254ab4902eSlneto static void settabsb (lua_State *L, const char *k, int v) {
1264ab4902eSlneto lua_pushboolean(L, v);
1274ab4902eSlneto lua_setfield(L, -2, k);
1284ab4902eSlneto }
1294ab4902eSlneto
1304ab4902eSlneto
1314ab4902eSlneto /*
1324ab4902eSlneto ** In function 'db_getinfo', the call to 'lua_getinfo' may push
1334ab4902eSlneto ** results on the stack; later it creates the result table to put
1344ab4902eSlneto ** these objects. Function 'treatstackoption' puts the result from
1354ab4902eSlneto ** 'lua_getinfo' on top of the result table so that it can call
1364ab4902eSlneto ** 'lua_setfield'.
1374ab4902eSlneto */
treatstackoption(lua_State * L,lua_State * L1,const char * fname)138dbec5304Smbalmer static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
1394ab4902eSlneto if (L == L1)
1404ab4902eSlneto lua_rotate(L, -2, 1); /* exchange object and table */
141dbec5304Smbalmer else
1424ab4902eSlneto lua_xmove(L1, L, 1); /* move object to the "main" stack */
1434ab4902eSlneto lua_setfield(L, -2, fname); /* put object into table */
144dbec5304Smbalmer }
145dbec5304Smbalmer
146dbec5304Smbalmer
1474ab4902eSlneto /*
1484ab4902eSlneto ** Calls 'lua_getinfo' and collects all results in a new table.
149b2829499Smbalmer ** L1 needs stack space for an optional input (function) plus
150b2829499Smbalmer ** two optional outputs (function and line table) from function
151b2829499Smbalmer ** 'lua_getinfo'.
1524ab4902eSlneto */
db_getinfo(lua_State * L)153dbec5304Smbalmer static int db_getinfo (lua_State *L) {
154dbec5304Smbalmer lua_Debug ar;
155dbec5304Smbalmer int arg;
156dbec5304Smbalmer lua_State *L1 = getthread(L, &arg);
157*f0dad708Snikita const char *options = luaL_optstring(L, arg+2, "flnSrtu");
158b2829499Smbalmer checkstack(L, L1, 3);
159*f0dad708Snikita luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
1604ab4902eSlneto if (lua_isfunction(L, arg + 1)) { /* info about a function? */
1614ab4902eSlneto options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
1624ab4902eSlneto lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
1634ab4902eSlneto lua_xmove(L, L1, 1);
1644ab4902eSlneto }
1654ab4902eSlneto else { /* stack level */
16673008250Slneto if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
167*f0dad708Snikita luaL_pushfail(L); /* level out of range */
168dbec5304Smbalmer return 1;
169dbec5304Smbalmer }
170dbec5304Smbalmer }
171dbec5304Smbalmer if (!lua_getinfo(L1, options, &ar))
172dbec5304Smbalmer return luaL_argerror(L, arg+2, "invalid option");
1734ab4902eSlneto lua_newtable(L); /* table to collect results */
174dbec5304Smbalmer if (strchr(options, 'S')) {
175*f0dad708Snikita lua_pushlstring(L, ar.source, ar.srclen);
176*f0dad708Snikita lua_setfield(L, -2, "source");
177dbec5304Smbalmer settabss(L, "short_src", ar.short_src);
178dbec5304Smbalmer settabsi(L, "linedefined", ar.linedefined);
179dbec5304Smbalmer settabsi(L, "lastlinedefined", ar.lastlinedefined);
180dbec5304Smbalmer settabss(L, "what", ar.what);
181dbec5304Smbalmer }
182dbec5304Smbalmer if (strchr(options, 'l'))
183dbec5304Smbalmer settabsi(L, "currentline", ar.currentline);
1844ab4902eSlneto if (strchr(options, 'u')) {
185dbec5304Smbalmer settabsi(L, "nups", ar.nups);
1864ab4902eSlneto settabsi(L, "nparams", ar.nparams);
1874ab4902eSlneto settabsb(L, "isvararg", ar.isvararg);
1884ab4902eSlneto }
189dbec5304Smbalmer if (strchr(options, 'n')) {
190dbec5304Smbalmer settabss(L, "name", ar.name);
191dbec5304Smbalmer settabss(L, "namewhat", ar.namewhat);
192dbec5304Smbalmer }
193*f0dad708Snikita if (strchr(options, 'r')) {
194*f0dad708Snikita settabsi(L, "ftransfer", ar.ftransfer);
195*f0dad708Snikita settabsi(L, "ntransfer", ar.ntransfer);
196*f0dad708Snikita }
1974ab4902eSlneto if (strchr(options, 't'))
1984ab4902eSlneto settabsb(L, "istailcall", ar.istailcall);
199dbec5304Smbalmer if (strchr(options, 'L'))
200dbec5304Smbalmer treatstackoption(L, L1, "activelines");
201dbec5304Smbalmer if (strchr(options, 'f'))
202dbec5304Smbalmer treatstackoption(L, L1, "func");
203dbec5304Smbalmer return 1; /* return table */
204dbec5304Smbalmer }
205dbec5304Smbalmer
206dbec5304Smbalmer
db_getlocal(lua_State * L)207dbec5304Smbalmer static int db_getlocal (lua_State *L) {
208dbec5304Smbalmer int arg;
209dbec5304Smbalmer lua_State *L1 = getthread(L, &arg);
21073008250Slneto int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
2114ab4902eSlneto if (lua_isfunction(L, arg + 1)) { /* function argument? */
2124ab4902eSlneto lua_pushvalue(L, arg + 1); /* push function */
2134ab4902eSlneto lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
2144ab4902eSlneto return 1; /* return only name (there is no value) */
2154ab4902eSlneto }
2164ab4902eSlneto else { /* stack-level argument */
217*f0dad708Snikita lua_Debug ar;
218*f0dad708Snikita const char *name;
21973008250Slneto int level = (int)luaL_checkinteger(L, arg + 1);
220*f0dad708Snikita if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
221dbec5304Smbalmer return luaL_argerror(L, arg+1, "level out of range");
222b2829499Smbalmer checkstack(L, L1, 1);
2234ab4902eSlneto name = lua_getlocal(L1, &ar, nvar);
224dbec5304Smbalmer if (name) {
2254ab4902eSlneto lua_xmove(L1, L, 1); /* move local value */
2264ab4902eSlneto lua_pushstring(L, name); /* push name */
2274ab4902eSlneto lua_rotate(L, -2, 1); /* re-order */
228dbec5304Smbalmer return 2;
229dbec5304Smbalmer }
230dbec5304Smbalmer else {
231*f0dad708Snikita luaL_pushfail(L); /* no name (nor value) */
232dbec5304Smbalmer return 1;
233dbec5304Smbalmer }
234dbec5304Smbalmer }
2354ab4902eSlneto }
236dbec5304Smbalmer
237dbec5304Smbalmer
db_setlocal(lua_State * L)238dbec5304Smbalmer static int db_setlocal (lua_State *L) {
239dbec5304Smbalmer int arg;
24073008250Slneto const char *name;
241dbec5304Smbalmer lua_State *L1 = getthread(L, &arg);
242dbec5304Smbalmer lua_Debug ar;
24373008250Slneto int level = (int)luaL_checkinteger(L, arg + 1);
24473008250Slneto int nvar = (int)luaL_checkinteger(L, arg + 2);
245*f0dad708Snikita if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
246dbec5304Smbalmer return luaL_argerror(L, arg+1, "level out of range");
247dbec5304Smbalmer luaL_checkany(L, arg+3);
248dbec5304Smbalmer lua_settop(L, arg+3);
249b2829499Smbalmer checkstack(L, L1, 1);
250dbec5304Smbalmer lua_xmove(L, L1, 1);
25173008250Slneto name = lua_setlocal(L1, &ar, nvar);
25273008250Slneto if (name == NULL)
25373008250Slneto lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
25473008250Slneto lua_pushstring(L, name);
255dbec5304Smbalmer return 1;
256dbec5304Smbalmer }
257dbec5304Smbalmer
258dbec5304Smbalmer
2594ab4902eSlneto /*
2604ab4902eSlneto ** get (if 'get' is true) or set an upvalue from a closure
2614ab4902eSlneto */
auxupvalue(lua_State * L,int get)262dbec5304Smbalmer static int auxupvalue (lua_State *L, int get) {
263dbec5304Smbalmer const char *name;
26473008250Slneto int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
2654ab4902eSlneto luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
266dbec5304Smbalmer name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
267dbec5304Smbalmer if (name == NULL) return 0;
268dbec5304Smbalmer lua_pushstring(L, name);
2694ab4902eSlneto lua_insert(L, -(get+1)); /* no-op if get is false */
270dbec5304Smbalmer return get + 1;
271dbec5304Smbalmer }
272dbec5304Smbalmer
273dbec5304Smbalmer
db_getupvalue(lua_State * L)274dbec5304Smbalmer static int db_getupvalue (lua_State *L) {
275dbec5304Smbalmer return auxupvalue(L, 1);
276dbec5304Smbalmer }
277dbec5304Smbalmer
278dbec5304Smbalmer
db_setupvalue(lua_State * L)279dbec5304Smbalmer static int db_setupvalue (lua_State *L) {
280dbec5304Smbalmer luaL_checkany(L, 3);
281dbec5304Smbalmer return auxupvalue(L, 0);
282dbec5304Smbalmer }
283dbec5304Smbalmer
284dbec5304Smbalmer
2854ab4902eSlneto /*
2864ab4902eSlneto ** Check whether a given upvalue from a given closure exists and
2874ab4902eSlneto ** returns its index
2884ab4902eSlneto */
checkupval(lua_State * L,int argf,int argnup,int * pnup)289*f0dad708Snikita static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
290*f0dad708Snikita void *id;
29173008250Slneto int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
2924ab4902eSlneto luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
293*f0dad708Snikita id = lua_upvalueid(L, argf, nup);
294*f0dad708Snikita if (pnup) {
295*f0dad708Snikita luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
296*f0dad708Snikita *pnup = nup;
297*f0dad708Snikita }
298*f0dad708Snikita return id;
2994ab4902eSlneto }
300dbec5304Smbalmer
301dbec5304Smbalmer
db_upvalueid(lua_State * L)3024ab4902eSlneto static int db_upvalueid (lua_State *L) {
303*f0dad708Snikita void *id = checkupval(L, 1, 2, NULL);
304*f0dad708Snikita if (id != NULL)
305*f0dad708Snikita lua_pushlightuserdata(L, id);
306*f0dad708Snikita else
307*f0dad708Snikita luaL_pushfail(L);
3084ab4902eSlneto return 1;
3094ab4902eSlneto }
3104ab4902eSlneto
3114ab4902eSlneto
db_upvaluejoin(lua_State * L)3124ab4902eSlneto static int db_upvaluejoin (lua_State *L) {
313*f0dad708Snikita int n1, n2;
314*f0dad708Snikita checkupval(L, 1, 2, &n1);
315*f0dad708Snikita checkupval(L, 3, 4, &n2);
3164ab4902eSlneto luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
3174ab4902eSlneto luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
3184ab4902eSlneto lua_upvaluejoin(L, 1, n1, 3, n2);
3194ab4902eSlneto return 0;
3204ab4902eSlneto }
3214ab4902eSlneto
3224ab4902eSlneto
3234ab4902eSlneto /*
3244ab4902eSlneto ** Call hook function registered at hook table for the current
3254ab4902eSlneto ** thread (if there is one)
3264ab4902eSlneto */
hookf(lua_State * L,lua_Debug * ar)327dbec5304Smbalmer static void hookf (lua_State *L, lua_Debug *ar) {
328dbec5304Smbalmer static const char *const hooknames[] =
3294ab4902eSlneto {"call", "return", "line", "count", "tail call"};
330*f0dad708Snikita lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
3314ab4902eSlneto lua_pushthread(L);
3324ab4902eSlneto if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
3334ab4902eSlneto lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
334dbec5304Smbalmer if (ar->currentline >= 0)
3354ab4902eSlneto lua_pushinteger(L, ar->currentline); /* push current line */
336dbec5304Smbalmer else lua_pushnil(L);
337dbec5304Smbalmer lua_assert(lua_getinfo(L, "lS", ar));
3384ab4902eSlneto lua_call(L, 2, 0); /* call hook function */
339dbec5304Smbalmer }
340dbec5304Smbalmer }
341dbec5304Smbalmer
342dbec5304Smbalmer
3434ab4902eSlneto /*
3444ab4902eSlneto ** Convert a string mask (for 'sethook') into a bit mask
3454ab4902eSlneto */
makemask(const char * smask,int count)346dbec5304Smbalmer static int makemask (const char *smask, int count) {
347dbec5304Smbalmer int mask = 0;
348dbec5304Smbalmer if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
349dbec5304Smbalmer if (strchr(smask, 'r')) mask |= LUA_MASKRET;
350dbec5304Smbalmer if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
351dbec5304Smbalmer if (count > 0) mask |= LUA_MASKCOUNT;
352dbec5304Smbalmer return mask;
353dbec5304Smbalmer }
354dbec5304Smbalmer
355dbec5304Smbalmer
3564ab4902eSlneto /*
3574ab4902eSlneto ** Convert a bit mask (for 'gethook') into a string mask
3584ab4902eSlneto */
unmakemask(int mask,char * smask)359dbec5304Smbalmer static char *unmakemask (int mask, char *smask) {
360dbec5304Smbalmer int i = 0;
361dbec5304Smbalmer if (mask & LUA_MASKCALL) smask[i++] = 'c';
362dbec5304Smbalmer if (mask & LUA_MASKRET) smask[i++] = 'r';
363dbec5304Smbalmer if (mask & LUA_MASKLINE) smask[i++] = 'l';
364dbec5304Smbalmer smask[i] = '\0';
365dbec5304Smbalmer return smask;
366dbec5304Smbalmer }
367dbec5304Smbalmer
368dbec5304Smbalmer
db_sethook(lua_State * L)369dbec5304Smbalmer static int db_sethook (lua_State *L) {
370dbec5304Smbalmer int arg, mask, count;
371dbec5304Smbalmer lua_Hook func;
372dbec5304Smbalmer lua_State *L1 = getthread(L, &arg);
3734ab4902eSlneto if (lua_isnoneornil(L, arg+1)) { /* no hook? */
374dbec5304Smbalmer lua_settop(L, arg+1);
375dbec5304Smbalmer func = NULL; mask = 0; count = 0; /* turn off hooks */
376dbec5304Smbalmer }
377dbec5304Smbalmer else {
378dbec5304Smbalmer const char *smask = luaL_checkstring(L, arg+2);
379dbec5304Smbalmer luaL_checktype(L, arg+1, LUA_TFUNCTION);
38073008250Slneto count = (int)luaL_optinteger(L, arg + 3, 0);
381dbec5304Smbalmer func = hookf; mask = makemask(smask, count);
382dbec5304Smbalmer }
383*f0dad708Snikita if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
384*f0dad708Snikita /* table just created; initialize it */
385*f0dad708Snikita lua_pushliteral(L, "k");
3864ab4902eSlneto lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
3874ab4902eSlneto lua_pushvalue(L, -1);
388*f0dad708Snikita lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
3894ab4902eSlneto }
390b2829499Smbalmer checkstack(L, L1, 1);
39173008250Slneto lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
39273008250Slneto lua_pushvalue(L, arg + 1); /* value (hook function) */
3934ab4902eSlneto lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
39473008250Slneto lua_sethook(L1, func, mask, count);
395dbec5304Smbalmer return 0;
396dbec5304Smbalmer }
397dbec5304Smbalmer
398dbec5304Smbalmer
db_gethook(lua_State * L)399dbec5304Smbalmer static int db_gethook (lua_State *L) {
400dbec5304Smbalmer int arg;
401dbec5304Smbalmer lua_State *L1 = getthread(L, &arg);
402dbec5304Smbalmer char buff[5];
403dbec5304Smbalmer int mask = lua_gethookmask(L1);
404dbec5304Smbalmer lua_Hook hook = lua_gethook(L1);
405*f0dad708Snikita if (hook == NULL) { /* no hook? */
406*f0dad708Snikita luaL_pushfail(L);
407*f0dad708Snikita return 1;
408*f0dad708Snikita }
40973008250Slneto else if (hook != hookf) /* external hook? */
410dbec5304Smbalmer lua_pushliteral(L, "external hook");
41173008250Slneto else { /* hook table must exist */
412*f0dad708Snikita lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
413b2829499Smbalmer checkstack(L, L1, 1);
4144ab4902eSlneto lua_pushthread(L1); lua_xmove(L1, L, 1);
4154ab4902eSlneto lua_rawget(L, -2); /* 1st result = hooktable[L1] */
416dbec5304Smbalmer lua_remove(L, -2); /* remove hook table */
417dbec5304Smbalmer }
4184ab4902eSlneto lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
4194ab4902eSlneto lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
420dbec5304Smbalmer return 3;
421dbec5304Smbalmer }
422dbec5304Smbalmer
423dbec5304Smbalmer
4247bdc7842Slneto #ifndef _KERNEL
db_debug(lua_State * L)425dbec5304Smbalmer static int db_debug (lua_State *L) {
426dbec5304Smbalmer for (;;) {
427dbec5304Smbalmer char buffer[250];
42873008250Slneto lua_writestringerror("%s", "lua_debug> ");
429*f0dad708Snikita if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
430dbec5304Smbalmer strcmp(buffer, "cont\n") == 0)
431dbec5304Smbalmer return 0;
432dbec5304Smbalmer if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
4334ab4902eSlneto lua_pcall(L, 0, 0, 0))
434*f0dad708Snikita lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
435dbec5304Smbalmer lua_settop(L, 0); /* remove eventual returns */
436dbec5304Smbalmer }
437dbec5304Smbalmer }
4382d6cb6c2Slneto #endif /* _KERNEL */
439dbec5304Smbalmer
440dbec5304Smbalmer
db_traceback(lua_State * L)4414ab4902eSlneto static int db_traceback (lua_State *L) {
442dbec5304Smbalmer int arg;
443dbec5304Smbalmer lua_State *L1 = getthread(L, &arg);
4444ab4902eSlneto const char *msg = lua_tostring(L, arg + 1);
4454ab4902eSlneto if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
4464ab4902eSlneto lua_pushvalue(L, arg + 1); /* return it untouched */
447dbec5304Smbalmer else {
44873008250Slneto int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
4494ab4902eSlneto luaL_traceback(L, L1, msg, level);
450dbec5304Smbalmer }
451dbec5304Smbalmer return 1;
452dbec5304Smbalmer }
453dbec5304Smbalmer
454dbec5304Smbalmer
db_setcstacklimit(lua_State * L)455*f0dad708Snikita static int db_setcstacklimit (lua_State *L) {
456*f0dad708Snikita int limit = (int)luaL_checkinteger(L, 1);
457*f0dad708Snikita int res = lua_setcstacklimit(L, limit);
458*f0dad708Snikita lua_pushinteger(L, res);
459*f0dad708Snikita return 1;
460*f0dad708Snikita }
461*f0dad708Snikita
462*f0dad708Snikita
463dbec5304Smbalmer static const luaL_Reg dblib[] = {
4647bdc7842Slneto #ifndef _KERNEL
465dbec5304Smbalmer {"debug", db_debug},
4662d6cb6c2Slneto #endif /* _KERNEL */
4674ab4902eSlneto {"getuservalue", db_getuservalue},
468dbec5304Smbalmer {"gethook", db_gethook},
469dbec5304Smbalmer {"getinfo", db_getinfo},
470dbec5304Smbalmer {"getlocal", db_getlocal},
471dbec5304Smbalmer {"getregistry", db_getregistry},
472dbec5304Smbalmer {"getmetatable", db_getmetatable},
473dbec5304Smbalmer {"getupvalue", db_getupvalue},
4744ab4902eSlneto {"upvaluejoin", db_upvaluejoin},
4754ab4902eSlneto {"upvalueid", db_upvalueid},
4764ab4902eSlneto {"setuservalue", db_setuservalue},
477dbec5304Smbalmer {"sethook", db_sethook},
478dbec5304Smbalmer {"setlocal", db_setlocal},
479dbec5304Smbalmer {"setmetatable", db_setmetatable},
480dbec5304Smbalmer {"setupvalue", db_setupvalue},
4814ab4902eSlneto {"traceback", db_traceback},
482*f0dad708Snikita {"setcstacklimit", db_setcstacklimit},
483dbec5304Smbalmer {NULL, NULL}
484dbec5304Smbalmer };
485dbec5304Smbalmer
486dbec5304Smbalmer
luaopen_debug(lua_State * L)4874ab4902eSlneto LUAMOD_API int luaopen_debug (lua_State *L) {
4884ab4902eSlneto luaL_newlib(L, dblib);
489dbec5304Smbalmer return 1;
490dbec5304Smbalmer }
491dbec5304Smbalmer
492