1*0a6a1f1dSLionel Sambuc /* $NetBSD: ldblib.c,v 1.6 2015/10/08 12:40:05 mbalmer Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: ldblib.c,v 1.149 2015/02/19 17:06:21 roberto Exp
511be35a1SLionel Sambuc ** Interface from Lua to its debug API
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc
9*0a6a1f1dSLionel Sambuc #define ldblib_c
10*0a6a1f1dSLionel Sambuc #define LUA_LIB
1111be35a1SLionel Sambuc
12*0a6a1f1dSLionel Sambuc #include "lprefix.h"
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
1611be35a1SLionel Sambuc #include <stdio.h>
1711be35a1SLionel Sambuc #include <stdlib.h>
1811be35a1SLionel Sambuc #include <string.h>
19*0a6a1f1dSLionel Sambuc #endif
2011be35a1SLionel Sambuc
2111be35a1SLionel Sambuc #include "lua.h"
2211be35a1SLionel Sambuc
2311be35a1SLionel Sambuc #include "lauxlib.h"
2411be35a1SLionel Sambuc #include "lualib.h"
2511be35a1SLionel Sambuc
2611be35a1SLionel Sambuc
27*0a6a1f1dSLionel Sambuc /*
28*0a6a1f1dSLionel Sambuc ** The hook table at registry[&HOOKKEY] maps threads to their current
29*0a6a1f1dSLionel Sambuc ** hook function. (We only need the unique address of 'HOOKKEY'.)
30*0a6a1f1dSLionel Sambuc */
31*0a6a1f1dSLionel Sambuc static const int HOOKKEY = 0;
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc /*
35*0a6a1f1dSLionel Sambuc ** If L1 != L, L1 can be in any state, and therefore there is no
36*0a6a1f1dSLionel Sambuc ** garanties about its stack space; any push in L1 must be
37*0a6a1f1dSLionel Sambuc ** checked.
38*0a6a1f1dSLionel Sambuc */
checkstack(lua_State * L,lua_State * L1,int n)39*0a6a1f1dSLionel Sambuc static void checkstack (lua_State *L, lua_State *L1, int n) {
40*0a6a1f1dSLionel Sambuc if (L != L1 && !lua_checkstack(L1, n))
41*0a6a1f1dSLionel Sambuc luaL_error(L, "stack overflow");
42*0a6a1f1dSLionel Sambuc }
43*0a6a1f1dSLionel Sambuc
4411be35a1SLionel Sambuc
db_getregistry(lua_State * L)4511be35a1SLionel Sambuc static int db_getregistry (lua_State *L) {
4611be35a1SLionel Sambuc lua_pushvalue(L, LUA_REGISTRYINDEX);
4711be35a1SLionel Sambuc return 1;
4811be35a1SLionel Sambuc }
4911be35a1SLionel Sambuc
5011be35a1SLionel Sambuc
db_getmetatable(lua_State * L)5111be35a1SLionel Sambuc static int db_getmetatable (lua_State *L) {
5211be35a1SLionel Sambuc luaL_checkany(L, 1);
5311be35a1SLionel Sambuc if (!lua_getmetatable(L, 1)) {
5411be35a1SLionel Sambuc lua_pushnil(L); /* no metatable */
5511be35a1SLionel Sambuc }
5611be35a1SLionel Sambuc return 1;
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc
db_setmetatable(lua_State * L)6011be35a1SLionel Sambuc static int db_setmetatable (lua_State *L) {
6111be35a1SLionel Sambuc int t = lua_type(L, 2);
6211be35a1SLionel Sambuc luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
6311be35a1SLionel Sambuc "nil or table expected");
6411be35a1SLionel Sambuc lua_settop(L, 2);
65*0a6a1f1dSLionel Sambuc lua_setmetatable(L, 1);
66*0a6a1f1dSLionel Sambuc return 1; /* return 1st argument */
67*0a6a1f1dSLionel Sambuc }
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc
db_getuservalue(lua_State * L)70*0a6a1f1dSLionel Sambuc static int db_getuservalue (lua_State *L) {
71*0a6a1f1dSLionel Sambuc if (lua_type(L, 1) != LUA_TUSERDATA)
72*0a6a1f1dSLionel Sambuc lua_pushnil(L);
73*0a6a1f1dSLionel Sambuc else
74*0a6a1f1dSLionel Sambuc lua_getuservalue(L, 1);
7511be35a1SLionel Sambuc return 1;
7611be35a1SLionel Sambuc }
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc
db_setuservalue(lua_State * L)79*0a6a1f1dSLionel Sambuc static int db_setuservalue (lua_State *L) {
80*0a6a1f1dSLionel Sambuc luaL_checktype(L, 1, LUA_TUSERDATA);
81*0a6a1f1dSLionel Sambuc luaL_checkany(L, 2);
8211be35a1SLionel Sambuc lua_settop(L, 2);
83*0a6a1f1dSLionel Sambuc lua_setuservalue(L, 1);
8411be35a1SLionel Sambuc return 1;
8511be35a1SLionel Sambuc }
8611be35a1SLionel Sambuc
8711be35a1SLionel Sambuc
88*0a6a1f1dSLionel Sambuc /*
89*0a6a1f1dSLionel Sambuc ** Auxiliary function used by several library functions: check for
90*0a6a1f1dSLionel Sambuc ** an optional thread as function's first argument and set 'arg' with
91*0a6a1f1dSLionel Sambuc ** 1 if this argument is present (so that functions can skip it to
92*0a6a1f1dSLionel Sambuc ** access their other arguments)
93*0a6a1f1dSLionel Sambuc */
getthread(lua_State * L,int * arg)9411be35a1SLionel Sambuc static lua_State *getthread (lua_State *L, int *arg) {
9511be35a1SLionel Sambuc if (lua_isthread(L, 1)) {
9611be35a1SLionel Sambuc *arg = 1;
9711be35a1SLionel Sambuc return lua_tothread(L, 1);
9811be35a1SLionel Sambuc }
9911be35a1SLionel Sambuc else {
10011be35a1SLionel Sambuc *arg = 0;
101*0a6a1f1dSLionel Sambuc return L; /* function will operate over current thread */
10211be35a1SLionel Sambuc }
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc
10511be35a1SLionel Sambuc
106*0a6a1f1dSLionel Sambuc /*
107*0a6a1f1dSLionel Sambuc ** Variations of 'lua_settable', used by 'db_getinfo' to put results
108*0a6a1f1dSLionel Sambuc ** from 'lua_getinfo' into result table. Key is always a string;
109*0a6a1f1dSLionel Sambuc ** value can be a string, an int, or a boolean.
110*0a6a1f1dSLionel Sambuc */
settabss(lua_State * L,const char * k,const char * v)111*0a6a1f1dSLionel Sambuc static void settabss (lua_State *L, const char *k, const char *v) {
112*0a6a1f1dSLionel Sambuc lua_pushstring(L, v);
113*0a6a1f1dSLionel Sambuc lua_setfield(L, -2, k);
114*0a6a1f1dSLionel Sambuc }
115*0a6a1f1dSLionel Sambuc
settabsi(lua_State * L,const char * k,int v)116*0a6a1f1dSLionel Sambuc static void settabsi (lua_State *L, const char *k, int v) {
117*0a6a1f1dSLionel Sambuc lua_pushinteger(L, v);
118*0a6a1f1dSLionel Sambuc lua_setfield(L, -2, k);
119*0a6a1f1dSLionel Sambuc }
120*0a6a1f1dSLionel Sambuc
settabsb(lua_State * L,const char * k,int v)121*0a6a1f1dSLionel Sambuc static void settabsb (lua_State *L, const char *k, int v) {
122*0a6a1f1dSLionel Sambuc lua_pushboolean(L, v);
123*0a6a1f1dSLionel Sambuc lua_setfield(L, -2, k);
124*0a6a1f1dSLionel Sambuc }
125*0a6a1f1dSLionel Sambuc
126*0a6a1f1dSLionel Sambuc
127*0a6a1f1dSLionel Sambuc /*
128*0a6a1f1dSLionel Sambuc ** In function 'db_getinfo', the call to 'lua_getinfo' may push
129*0a6a1f1dSLionel Sambuc ** results on the stack; later it creates the result table to put
130*0a6a1f1dSLionel Sambuc ** these objects. Function 'treatstackoption' puts the result from
131*0a6a1f1dSLionel Sambuc ** 'lua_getinfo' on top of the result table so that it can call
132*0a6a1f1dSLionel Sambuc ** 'lua_setfield'.
133*0a6a1f1dSLionel Sambuc */
treatstackoption(lua_State * L,lua_State * L1,const char * fname)13411be35a1SLionel Sambuc static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
135*0a6a1f1dSLionel Sambuc if (L == L1)
136*0a6a1f1dSLionel Sambuc lua_rotate(L, -2, 1); /* exchange object and table */
13711be35a1SLionel Sambuc else
138*0a6a1f1dSLionel Sambuc lua_xmove(L1, L, 1); /* move object to the "main" stack */
139*0a6a1f1dSLionel Sambuc lua_setfield(L, -2, fname); /* put object into table */
14011be35a1SLionel Sambuc }
14111be35a1SLionel Sambuc
14211be35a1SLionel Sambuc
143*0a6a1f1dSLionel Sambuc /*
144*0a6a1f1dSLionel Sambuc ** Calls 'lua_getinfo' and collects all results in a new table.
145*0a6a1f1dSLionel Sambuc ** L1 needs stack space for an optional input (function) plus
146*0a6a1f1dSLionel Sambuc ** two optional outputs (function and line table) from function
147*0a6a1f1dSLionel Sambuc ** 'lua_getinfo'.
148*0a6a1f1dSLionel Sambuc */
db_getinfo(lua_State * L)14911be35a1SLionel Sambuc static int db_getinfo (lua_State *L) {
15011be35a1SLionel Sambuc lua_Debug ar;
15111be35a1SLionel Sambuc int arg;
15211be35a1SLionel Sambuc lua_State *L1 = getthread(L, &arg);
153*0a6a1f1dSLionel Sambuc const char *options = luaL_optstring(L, arg+2, "flnStu");
154*0a6a1f1dSLionel Sambuc checkstack(L, L1, 3);
155*0a6a1f1dSLionel Sambuc if (lua_isfunction(L, arg + 1)) { /* info about a function? */
156*0a6a1f1dSLionel Sambuc options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
157*0a6a1f1dSLionel Sambuc lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
158*0a6a1f1dSLionel Sambuc lua_xmove(L, L1, 1);
159*0a6a1f1dSLionel Sambuc }
160*0a6a1f1dSLionel Sambuc else { /* stack level */
161*0a6a1f1dSLionel Sambuc if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
16211be35a1SLionel Sambuc lua_pushnil(L); /* level out of range */
16311be35a1SLionel Sambuc return 1;
16411be35a1SLionel Sambuc }
16511be35a1SLionel Sambuc }
16611be35a1SLionel Sambuc if (!lua_getinfo(L1, options, &ar))
16711be35a1SLionel Sambuc return luaL_argerror(L, arg+2, "invalid option");
168*0a6a1f1dSLionel Sambuc lua_newtable(L); /* table to collect results */
16911be35a1SLionel Sambuc if (strchr(options, 'S')) {
17011be35a1SLionel Sambuc settabss(L, "source", ar.source);
17111be35a1SLionel Sambuc settabss(L, "short_src", ar.short_src);
17211be35a1SLionel Sambuc settabsi(L, "linedefined", ar.linedefined);
17311be35a1SLionel Sambuc settabsi(L, "lastlinedefined", ar.lastlinedefined);
17411be35a1SLionel Sambuc settabss(L, "what", ar.what);
17511be35a1SLionel Sambuc }
17611be35a1SLionel Sambuc if (strchr(options, 'l'))
17711be35a1SLionel Sambuc settabsi(L, "currentline", ar.currentline);
178*0a6a1f1dSLionel Sambuc if (strchr(options, 'u')) {
17911be35a1SLionel Sambuc settabsi(L, "nups", ar.nups);
180*0a6a1f1dSLionel Sambuc settabsi(L, "nparams", ar.nparams);
181*0a6a1f1dSLionel Sambuc settabsb(L, "isvararg", ar.isvararg);
182*0a6a1f1dSLionel Sambuc }
18311be35a1SLionel Sambuc if (strchr(options, 'n')) {
18411be35a1SLionel Sambuc settabss(L, "name", ar.name);
18511be35a1SLionel Sambuc settabss(L, "namewhat", ar.namewhat);
18611be35a1SLionel Sambuc }
187*0a6a1f1dSLionel Sambuc if (strchr(options, 't'))
188*0a6a1f1dSLionel Sambuc settabsb(L, "istailcall", ar.istailcall);
18911be35a1SLionel Sambuc if (strchr(options, 'L'))
19011be35a1SLionel Sambuc treatstackoption(L, L1, "activelines");
19111be35a1SLionel Sambuc if (strchr(options, 'f'))
19211be35a1SLionel Sambuc treatstackoption(L, L1, "func");
19311be35a1SLionel Sambuc return 1; /* return table */
19411be35a1SLionel Sambuc }
19511be35a1SLionel Sambuc
19611be35a1SLionel Sambuc
db_getlocal(lua_State * L)19711be35a1SLionel Sambuc static int db_getlocal (lua_State *L) {
19811be35a1SLionel Sambuc int arg;
19911be35a1SLionel Sambuc lua_State *L1 = getthread(L, &arg);
20011be35a1SLionel Sambuc lua_Debug ar;
20111be35a1SLionel Sambuc const char *name;
202*0a6a1f1dSLionel Sambuc int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
203*0a6a1f1dSLionel Sambuc if (lua_isfunction(L, arg + 1)) { /* function argument? */
204*0a6a1f1dSLionel Sambuc lua_pushvalue(L, arg + 1); /* push function */
205*0a6a1f1dSLionel Sambuc lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
206*0a6a1f1dSLionel Sambuc return 1; /* return only name (there is no value) */
207*0a6a1f1dSLionel Sambuc }
208*0a6a1f1dSLionel Sambuc else { /* stack-level argument */
209*0a6a1f1dSLionel Sambuc int level = (int)luaL_checkinteger(L, arg + 1);
210*0a6a1f1dSLionel Sambuc if (!lua_getstack(L1, level, &ar)) /* out of range? */
21111be35a1SLionel Sambuc return luaL_argerror(L, arg+1, "level out of range");
212*0a6a1f1dSLionel Sambuc checkstack(L, L1, 1);
213*0a6a1f1dSLionel Sambuc name = lua_getlocal(L1, &ar, nvar);
21411be35a1SLionel Sambuc if (name) {
215*0a6a1f1dSLionel Sambuc lua_xmove(L1, L, 1); /* move local value */
216*0a6a1f1dSLionel Sambuc lua_pushstring(L, name); /* push name */
217*0a6a1f1dSLionel Sambuc lua_rotate(L, -2, 1); /* re-order */
21811be35a1SLionel Sambuc return 2;
21911be35a1SLionel Sambuc }
22011be35a1SLionel Sambuc else {
221*0a6a1f1dSLionel Sambuc lua_pushnil(L); /* no name (nor value) */
22211be35a1SLionel Sambuc return 1;
22311be35a1SLionel Sambuc }
22411be35a1SLionel Sambuc }
225*0a6a1f1dSLionel Sambuc }
22611be35a1SLionel Sambuc
22711be35a1SLionel Sambuc
db_setlocal(lua_State * L)22811be35a1SLionel Sambuc static int db_setlocal (lua_State *L) {
22911be35a1SLionel Sambuc int arg;
230*0a6a1f1dSLionel Sambuc const char *name;
23111be35a1SLionel Sambuc lua_State *L1 = getthread(L, &arg);
23211be35a1SLionel Sambuc lua_Debug ar;
233*0a6a1f1dSLionel Sambuc int level = (int)luaL_checkinteger(L, arg + 1);
234*0a6a1f1dSLionel Sambuc int nvar = (int)luaL_checkinteger(L, arg + 2);
235*0a6a1f1dSLionel Sambuc if (!lua_getstack(L1, level, &ar)) /* out of range? */
23611be35a1SLionel Sambuc return luaL_argerror(L, arg+1, "level out of range");
23711be35a1SLionel Sambuc luaL_checkany(L, arg+3);
23811be35a1SLionel Sambuc lua_settop(L, arg+3);
239*0a6a1f1dSLionel Sambuc checkstack(L, L1, 1);
24011be35a1SLionel Sambuc lua_xmove(L, L1, 1);
241*0a6a1f1dSLionel Sambuc name = lua_setlocal(L1, &ar, nvar);
242*0a6a1f1dSLionel Sambuc if (name == NULL)
243*0a6a1f1dSLionel Sambuc lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
244*0a6a1f1dSLionel Sambuc lua_pushstring(L, name);
24511be35a1SLionel Sambuc return 1;
24611be35a1SLionel Sambuc }
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc
249*0a6a1f1dSLionel Sambuc /*
250*0a6a1f1dSLionel Sambuc ** get (if 'get' is true) or set an upvalue from a closure
251*0a6a1f1dSLionel Sambuc */
auxupvalue(lua_State * L,int get)25211be35a1SLionel Sambuc static int auxupvalue (lua_State *L, int get) {
25311be35a1SLionel Sambuc const char *name;
254*0a6a1f1dSLionel Sambuc int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
255*0a6a1f1dSLionel Sambuc luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
25611be35a1SLionel Sambuc name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
25711be35a1SLionel Sambuc if (name == NULL) return 0;
25811be35a1SLionel Sambuc lua_pushstring(L, name);
259*0a6a1f1dSLionel Sambuc lua_insert(L, -(get+1)); /* no-op if get is false */
26011be35a1SLionel Sambuc return get + 1;
26111be35a1SLionel Sambuc }
26211be35a1SLionel Sambuc
26311be35a1SLionel Sambuc
db_getupvalue(lua_State * L)26411be35a1SLionel Sambuc static int db_getupvalue (lua_State *L) {
26511be35a1SLionel Sambuc return auxupvalue(L, 1);
26611be35a1SLionel Sambuc }
26711be35a1SLionel Sambuc
26811be35a1SLionel Sambuc
db_setupvalue(lua_State * L)26911be35a1SLionel Sambuc static int db_setupvalue (lua_State *L) {
27011be35a1SLionel Sambuc luaL_checkany(L, 3);
27111be35a1SLionel Sambuc return auxupvalue(L, 0);
27211be35a1SLionel Sambuc }
27311be35a1SLionel Sambuc
27411be35a1SLionel Sambuc
275*0a6a1f1dSLionel Sambuc /*
276*0a6a1f1dSLionel Sambuc ** Check whether a given upvalue from a given closure exists and
277*0a6a1f1dSLionel Sambuc ** returns its index
278*0a6a1f1dSLionel Sambuc */
checkupval(lua_State * L,int argf,int argnup)279*0a6a1f1dSLionel Sambuc static int checkupval (lua_State *L, int argf, int argnup) {
280*0a6a1f1dSLionel Sambuc int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
281*0a6a1f1dSLionel Sambuc luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
282*0a6a1f1dSLionel Sambuc luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
283*0a6a1f1dSLionel Sambuc "invalid upvalue index");
284*0a6a1f1dSLionel Sambuc return nup;
285*0a6a1f1dSLionel Sambuc }
28611be35a1SLionel Sambuc
28711be35a1SLionel Sambuc
db_upvalueid(lua_State * L)288*0a6a1f1dSLionel Sambuc static int db_upvalueid (lua_State *L) {
289*0a6a1f1dSLionel Sambuc int n = checkupval(L, 1, 2);
290*0a6a1f1dSLionel Sambuc lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
291*0a6a1f1dSLionel Sambuc return 1;
292*0a6a1f1dSLionel Sambuc }
293*0a6a1f1dSLionel Sambuc
294*0a6a1f1dSLionel Sambuc
db_upvaluejoin(lua_State * L)295*0a6a1f1dSLionel Sambuc static int db_upvaluejoin (lua_State *L) {
296*0a6a1f1dSLionel Sambuc int n1 = checkupval(L, 1, 2);
297*0a6a1f1dSLionel Sambuc int n2 = checkupval(L, 3, 4);
298*0a6a1f1dSLionel Sambuc luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
299*0a6a1f1dSLionel Sambuc luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
300*0a6a1f1dSLionel Sambuc lua_upvaluejoin(L, 1, n1, 3, n2);
301*0a6a1f1dSLionel Sambuc return 0;
302*0a6a1f1dSLionel Sambuc }
303*0a6a1f1dSLionel Sambuc
304*0a6a1f1dSLionel Sambuc
305*0a6a1f1dSLionel Sambuc /*
306*0a6a1f1dSLionel Sambuc ** Call hook function registered at hook table for the current
307*0a6a1f1dSLionel Sambuc ** thread (if there is one)
308*0a6a1f1dSLionel Sambuc */
hookf(lua_State * L,lua_Debug * ar)30911be35a1SLionel Sambuc static void hookf (lua_State *L, lua_Debug *ar) {
31011be35a1SLionel Sambuc static const char *const hooknames[] =
311*0a6a1f1dSLionel Sambuc {"call", "return", "line", "count", "tail call"};
312*0a6a1f1dSLionel Sambuc lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
313*0a6a1f1dSLionel Sambuc lua_pushthread(L);
314*0a6a1f1dSLionel Sambuc if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
315*0a6a1f1dSLionel Sambuc lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
31611be35a1SLionel Sambuc if (ar->currentline >= 0)
317*0a6a1f1dSLionel Sambuc lua_pushinteger(L, ar->currentline); /* push current line */
31811be35a1SLionel Sambuc else lua_pushnil(L);
31911be35a1SLionel Sambuc lua_assert(lua_getinfo(L, "lS", ar));
320*0a6a1f1dSLionel Sambuc lua_call(L, 2, 0); /* call hook function */
32111be35a1SLionel Sambuc }
32211be35a1SLionel Sambuc }
32311be35a1SLionel Sambuc
32411be35a1SLionel Sambuc
325*0a6a1f1dSLionel Sambuc /*
326*0a6a1f1dSLionel Sambuc ** Convert a string mask (for 'sethook') into a bit mask
327*0a6a1f1dSLionel Sambuc */
makemask(const char * smask,int count)32811be35a1SLionel Sambuc static int makemask (const char *smask, int count) {
32911be35a1SLionel Sambuc int mask = 0;
33011be35a1SLionel Sambuc if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
33111be35a1SLionel Sambuc if (strchr(smask, 'r')) mask |= LUA_MASKRET;
33211be35a1SLionel Sambuc if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
33311be35a1SLionel Sambuc if (count > 0) mask |= LUA_MASKCOUNT;
33411be35a1SLionel Sambuc return mask;
33511be35a1SLionel Sambuc }
33611be35a1SLionel Sambuc
33711be35a1SLionel Sambuc
338*0a6a1f1dSLionel Sambuc /*
339*0a6a1f1dSLionel Sambuc ** Convert a bit mask (for 'gethook') into a string mask
340*0a6a1f1dSLionel Sambuc */
unmakemask(int mask,char * smask)34111be35a1SLionel Sambuc static char *unmakemask (int mask, char *smask) {
34211be35a1SLionel Sambuc int i = 0;
34311be35a1SLionel Sambuc if (mask & LUA_MASKCALL) smask[i++] = 'c';
34411be35a1SLionel Sambuc if (mask & LUA_MASKRET) smask[i++] = 'r';
34511be35a1SLionel Sambuc if (mask & LUA_MASKLINE) smask[i++] = 'l';
34611be35a1SLionel Sambuc smask[i] = '\0';
34711be35a1SLionel Sambuc return smask;
34811be35a1SLionel Sambuc }
34911be35a1SLionel Sambuc
35011be35a1SLionel Sambuc
db_sethook(lua_State * L)35111be35a1SLionel Sambuc static int db_sethook (lua_State *L) {
35211be35a1SLionel Sambuc int arg, mask, count;
35311be35a1SLionel Sambuc lua_Hook func;
35411be35a1SLionel Sambuc lua_State *L1 = getthread(L, &arg);
355*0a6a1f1dSLionel Sambuc if (lua_isnoneornil(L, arg+1)) { /* no hook? */
35611be35a1SLionel Sambuc lua_settop(L, arg+1);
35711be35a1SLionel Sambuc func = NULL; mask = 0; count = 0; /* turn off hooks */
35811be35a1SLionel Sambuc }
35911be35a1SLionel Sambuc else {
36011be35a1SLionel Sambuc const char *smask = luaL_checkstring(L, arg+2);
36111be35a1SLionel Sambuc luaL_checktype(L, arg+1, LUA_TFUNCTION);
362*0a6a1f1dSLionel Sambuc count = (int)luaL_optinteger(L, arg + 3, 0);
36311be35a1SLionel Sambuc func = hookf; mask = makemask(smask, count);
36411be35a1SLionel Sambuc }
365*0a6a1f1dSLionel Sambuc if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
366*0a6a1f1dSLionel Sambuc lua_createtable(L, 0, 2); /* create a hook table */
367*0a6a1f1dSLionel Sambuc lua_pushvalue(L, -1);
368*0a6a1f1dSLionel Sambuc lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
369*0a6a1f1dSLionel Sambuc lua_pushstring(L, "k");
370*0a6a1f1dSLionel Sambuc lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
371*0a6a1f1dSLionel Sambuc lua_pushvalue(L, -1);
372*0a6a1f1dSLionel Sambuc lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
373*0a6a1f1dSLionel Sambuc }
374*0a6a1f1dSLionel Sambuc checkstack(L, L1, 1);
375*0a6a1f1dSLionel Sambuc lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
376*0a6a1f1dSLionel Sambuc lua_pushvalue(L, arg + 1); /* value (hook function) */
377*0a6a1f1dSLionel Sambuc lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
378*0a6a1f1dSLionel Sambuc lua_sethook(L1, func, mask, count);
37911be35a1SLionel Sambuc return 0;
38011be35a1SLionel Sambuc }
38111be35a1SLionel Sambuc
38211be35a1SLionel Sambuc
db_gethook(lua_State * L)38311be35a1SLionel Sambuc static int db_gethook (lua_State *L) {
38411be35a1SLionel Sambuc int arg;
38511be35a1SLionel Sambuc lua_State *L1 = getthread(L, &arg);
38611be35a1SLionel Sambuc char buff[5];
38711be35a1SLionel Sambuc int mask = lua_gethookmask(L1);
38811be35a1SLionel Sambuc lua_Hook hook = lua_gethook(L1);
389*0a6a1f1dSLionel Sambuc if (hook == NULL) /* no hook? */
390*0a6a1f1dSLionel Sambuc lua_pushnil(L);
391*0a6a1f1dSLionel Sambuc else if (hook != hookf) /* external hook? */
39211be35a1SLionel Sambuc lua_pushliteral(L, "external hook");
393*0a6a1f1dSLionel Sambuc else { /* hook table must exist */
394*0a6a1f1dSLionel Sambuc lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
395*0a6a1f1dSLionel Sambuc checkstack(L, L1, 1);
396*0a6a1f1dSLionel Sambuc lua_pushthread(L1); lua_xmove(L1, L, 1);
397*0a6a1f1dSLionel Sambuc lua_rawget(L, -2); /* 1st result = hooktable[L1] */
39811be35a1SLionel Sambuc lua_remove(L, -2); /* remove hook table */
39911be35a1SLionel Sambuc }
400*0a6a1f1dSLionel Sambuc lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
401*0a6a1f1dSLionel Sambuc lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
40211be35a1SLionel Sambuc return 3;
40311be35a1SLionel Sambuc }
40411be35a1SLionel Sambuc
40511be35a1SLionel Sambuc
406*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
db_debug(lua_State * L)40711be35a1SLionel Sambuc static int db_debug (lua_State *L) {
40811be35a1SLionel Sambuc for (;;) {
40911be35a1SLionel Sambuc char buffer[250];
410*0a6a1f1dSLionel Sambuc lua_writestringerror("%s", "lua_debug> ");
41111be35a1SLionel Sambuc if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
41211be35a1SLionel Sambuc strcmp(buffer, "cont\n") == 0)
41311be35a1SLionel Sambuc return 0;
41411be35a1SLionel Sambuc if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
415*0a6a1f1dSLionel Sambuc lua_pcall(L, 0, 0, 0))
416*0a6a1f1dSLionel Sambuc lua_writestringerror("%s\n", lua_tostring(L, -1));
41711be35a1SLionel Sambuc lua_settop(L, 0); /* remove eventual returns */
41811be35a1SLionel Sambuc }
41911be35a1SLionel Sambuc }
420*0a6a1f1dSLionel Sambuc #endif
42111be35a1SLionel Sambuc
42211be35a1SLionel Sambuc
db_traceback(lua_State * L)423*0a6a1f1dSLionel Sambuc static int db_traceback (lua_State *L) {
42411be35a1SLionel Sambuc int arg;
42511be35a1SLionel Sambuc lua_State *L1 = getthread(L, &arg);
426*0a6a1f1dSLionel Sambuc const char *msg = lua_tostring(L, arg + 1);
427*0a6a1f1dSLionel Sambuc if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
428*0a6a1f1dSLionel Sambuc lua_pushvalue(L, arg + 1); /* return it untouched */
42911be35a1SLionel Sambuc else {
430*0a6a1f1dSLionel Sambuc int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
431*0a6a1f1dSLionel Sambuc luaL_traceback(L, L1, msg, level);
43211be35a1SLionel Sambuc }
43311be35a1SLionel Sambuc return 1;
43411be35a1SLionel Sambuc }
43511be35a1SLionel Sambuc
43611be35a1SLionel Sambuc
43711be35a1SLionel Sambuc static const luaL_Reg dblib[] = {
438*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
43911be35a1SLionel Sambuc {"debug", db_debug},
440*0a6a1f1dSLionel Sambuc #endif
441*0a6a1f1dSLionel Sambuc {"getuservalue", db_getuservalue},
44211be35a1SLionel Sambuc {"gethook", db_gethook},
44311be35a1SLionel Sambuc {"getinfo", db_getinfo},
44411be35a1SLionel Sambuc {"getlocal", db_getlocal},
44511be35a1SLionel Sambuc {"getregistry", db_getregistry},
44611be35a1SLionel Sambuc {"getmetatable", db_getmetatable},
44711be35a1SLionel Sambuc {"getupvalue", db_getupvalue},
448*0a6a1f1dSLionel Sambuc {"upvaluejoin", db_upvaluejoin},
449*0a6a1f1dSLionel Sambuc {"upvalueid", db_upvalueid},
450*0a6a1f1dSLionel Sambuc {"setuservalue", db_setuservalue},
45111be35a1SLionel Sambuc {"sethook", db_sethook},
45211be35a1SLionel Sambuc {"setlocal", db_setlocal},
45311be35a1SLionel Sambuc {"setmetatable", db_setmetatable},
45411be35a1SLionel Sambuc {"setupvalue", db_setupvalue},
455*0a6a1f1dSLionel Sambuc {"traceback", db_traceback},
45611be35a1SLionel Sambuc {NULL, NULL}
45711be35a1SLionel Sambuc };
45811be35a1SLionel Sambuc
45911be35a1SLionel Sambuc
luaopen_debug(lua_State * L)460*0a6a1f1dSLionel Sambuc LUAMOD_API int luaopen_debug (lua_State *L) {
461*0a6a1f1dSLionel Sambuc luaL_newlib(L, dblib);
46211be35a1SLionel Sambuc return 1;
46311be35a1SLionel Sambuc }
46411be35a1SLionel Sambuc
465