]>
git.proxmox.com Git - ceph.git/blob - ceph/src/lua/src/ldblib.c
2 ** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp $
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
25 ** hook function. (We only need the unique address of 'HOOKKEY'.)
27 static const int HOOKKEY
= 0;
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
!= 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_argcheck(L
, t
== LUA_TNIL
|| t
== LUA_TTABLE
, 2,
59 "nil or table expected");
61 lua_setmetatable(L
, 1);
62 return 1; /* return 1st argument */
66 static int db_getuservalue (lua_State
*L
) {
67 if (lua_type(L
, 1) != LUA_TUSERDATA
)
70 lua_getuservalue(L
, 1);
75 static int db_setuservalue (lua_State
*L
) {
76 luaL_checktype(L
, 1, LUA_TUSERDATA
);
79 lua_setuservalue(L
, 1);
85 ** Auxiliary function used by several library functions: check for
86 ** an optional thread as function's first argument and set 'arg' with
87 ** 1 if this argument is present (so that functions can skip it to
88 ** access their other arguments)
90 static lua_State
*getthread (lua_State
*L
, int *arg
) {
91 if (lua_isthread(L
, 1)) {
93 return lua_tothread(L
, 1);
97 return L
; /* function will operate over current thread */
103 ** Variations of 'lua_settable', used by 'db_getinfo' to put results
104 ** from 'lua_getinfo' into result table. Key is always a string;
105 ** value can be a string, an int, or a boolean.
107 static void settabss (lua_State
*L
, const char *k
, const char *v
) {
108 lua_pushstring(L
, v
);
109 lua_setfield(L
, -2, k
);
112 static void settabsi (lua_State
*L
, const char *k
, int v
) {
113 lua_pushinteger(L
, v
);
114 lua_setfield(L
, -2, k
);
117 static void settabsb (lua_State
*L
, const char *k
, int v
) {
118 lua_pushboolean(L
, v
);
119 lua_setfield(L
, -2, k
);
124 ** In function 'db_getinfo', the call to 'lua_getinfo' may push
125 ** results on the stack; later it creates the result table to put
126 ** these objects. Function 'treatstackoption' puts the result from
127 ** 'lua_getinfo' on top of the result table so that it can call
130 static void treatstackoption (lua_State
*L
, lua_State
*L1
, const char *fname
) {
132 lua_rotate(L
, -2, 1); /* exchange object and table */
134 lua_xmove(L1
, L
, 1); /* move object to the "main" stack */
135 lua_setfield(L
, -2, fname
); /* put object into table */
140 ** Calls 'lua_getinfo' and collects all results in a new table.
141 ** L1 needs stack space for an optional input (function) plus
142 ** two optional outputs (function and line table) from function
145 static int db_getinfo (lua_State
*L
) {
148 lua_State
*L1
= getthread(L
, &arg
);
149 const char *options
= luaL_optstring(L
, arg
+2, "flnStu");
150 checkstack(L
, L1
, 3);
151 if (lua_isfunction(L
, arg
+ 1)) { /* info about a function? */
152 options
= lua_pushfstring(L
, ">%s", options
); /* add '>' to 'options' */
153 lua_pushvalue(L
, arg
+ 1); /* move function to 'L1' stack */
156 else { /* stack level */
157 if (!lua_getstack(L1
, (int)luaL_checkinteger(L
, arg
+ 1), &ar
)) {
158 lua_pushnil(L
); /* level out of range */
162 if (!lua_getinfo(L1
, options
, &ar
))
163 return luaL_argerror(L
, arg
+2, "invalid option");
164 lua_newtable(L
); /* table to collect results */
165 if (strchr(options
, 'S')) {
166 settabss(L
, "source", ar
.source
);
167 settabss(L
, "short_src", ar
.short_src
);
168 settabsi(L
, "linedefined", ar
.linedefined
);
169 settabsi(L
, "lastlinedefined", ar
.lastlinedefined
);
170 settabss(L
, "what", ar
.what
);
172 if (strchr(options
, 'l'))
173 settabsi(L
, "currentline", ar
.currentline
);
174 if (strchr(options
, 'u')) {
175 settabsi(L
, "nups", ar
.nups
);
176 settabsi(L
, "nparams", ar
.nparams
);
177 settabsb(L
, "isvararg", ar
.isvararg
);
179 if (strchr(options
, 'n')) {
180 settabss(L
, "name", ar
.name
);
181 settabss(L
, "namewhat", ar
.namewhat
);
183 if (strchr(options
, 't'))
184 settabsb(L
, "istailcall", ar
.istailcall
);
185 if (strchr(options
, 'L'))
186 treatstackoption(L
, L1
, "activelines");
187 if (strchr(options
, 'f'))
188 treatstackoption(L
, L1
, "func");
189 return 1; /* return table */
193 static int db_getlocal (lua_State
*L
) {
195 lua_State
*L1
= getthread(L
, &arg
);
198 int nvar
= (int)luaL_checkinteger(L
, arg
+ 2); /* local-variable index */
199 if (lua_isfunction(L
, arg
+ 1)) { /* function argument? */
200 lua_pushvalue(L
, arg
+ 1); /* push function */
201 lua_pushstring(L
, lua_getlocal(L
, NULL
, nvar
)); /* push local name */
202 return 1; /* return only name (there is no value) */
204 else { /* stack-level argument */
205 int level
= (int)luaL_checkinteger(L
, arg
+ 1);
206 if (!lua_getstack(L1
, level
, &ar
)) /* out of range? */
207 return luaL_argerror(L
, arg
+1, "level out of range");
208 checkstack(L
, L1
, 1);
209 name
= lua_getlocal(L1
, &ar
, nvar
);
211 lua_xmove(L1
, L
, 1); /* move local value */
212 lua_pushstring(L
, name
); /* push name */
213 lua_rotate(L
, -2, 1); /* re-order */
217 lua_pushnil(L
); /* no name (nor value) */
224 static int db_setlocal (lua_State
*L
) {
227 lua_State
*L1
= getthread(L
, &arg
);
229 int level
= (int)luaL_checkinteger(L
, arg
+ 1);
230 int nvar
= (int)luaL_checkinteger(L
, arg
+ 2);
231 if (!lua_getstack(L1
, level
, &ar
)) /* out of range? */
232 return luaL_argerror(L
, arg
+1, "level out of range");
233 luaL_checkany(L
, arg
+3);
234 lua_settop(L
, arg
+3);
235 checkstack(L
, L1
, 1);
237 name
= lua_setlocal(L1
, &ar
, nvar
);
239 lua_pop(L1
, 1); /* pop value (if not popped by 'lua_setlocal') */
240 lua_pushstring(L
, name
);
246 ** get (if 'get' is true) or set an upvalue from a closure
248 static int auxupvalue (lua_State
*L
, int get
) {
250 int n
= (int)luaL_checkinteger(L
, 2); /* upvalue index */
251 luaL_checktype(L
, 1, LUA_TFUNCTION
); /* closure */
252 name
= get
? lua_getupvalue(L
, 1, n
) : lua_setupvalue(L
, 1, n
);
253 if (name
== NULL
) return 0;
254 lua_pushstring(L
, name
);
255 lua_insert(L
, -(get
+1)); /* no-op if get is false */
260 static int db_getupvalue (lua_State
*L
) {
261 return auxupvalue(L
, 1);
265 static int db_setupvalue (lua_State
*L
) {
267 return auxupvalue(L
, 0);
272 ** Check whether a given upvalue from a given closure exists and
275 static int checkupval (lua_State
*L
, int argf
, int argnup
) {
276 int nup
= (int)luaL_checkinteger(L
, argnup
); /* upvalue index */
277 luaL_checktype(L
, argf
, LUA_TFUNCTION
); /* closure */
278 luaL_argcheck(L
, (lua_getupvalue(L
, argf
, nup
) != NULL
), argnup
,
279 "invalid upvalue index");
284 static int db_upvalueid (lua_State
*L
) {
285 int n
= checkupval(L
, 1, 2);
286 lua_pushlightuserdata(L
, lua_upvalueid(L
, 1, n
));
291 static int db_upvaluejoin (lua_State
*L
) {
292 int n1
= checkupval(L
, 1, 2);
293 int n2
= checkupval(L
, 3, 4);
294 luaL_argcheck(L
, !lua_iscfunction(L
, 1), 1, "Lua function expected");
295 luaL_argcheck(L
, !lua_iscfunction(L
, 3), 3, "Lua function expected");
296 lua_upvaluejoin(L
, 1, n1
, 3, n2
);
302 ** Call hook function registered at hook table for the current
303 ** thread (if there is one)
305 static void hookf (lua_State
*L
, lua_Debug
*ar
) {
306 static const char *const hooknames
[] =
307 {"call", "return", "line", "count", "tail call"};
308 lua_rawgetp(L
, LUA_REGISTRYINDEX
, &HOOKKEY
);
310 if (lua_rawget(L
, -2) == LUA_TFUNCTION
) { /* is there a hook function? */
311 lua_pushstring(L
, hooknames
[(int)ar
->event
]); /* push event name */
312 if (ar
->currentline
>= 0)
313 lua_pushinteger(L
, ar
->currentline
); /* push current line */
315 lua_assert(lua_getinfo(L
, "lS", ar
));
316 lua_call(L
, 2, 0); /* call hook function */
322 ** Convert a string mask (for 'sethook') into a bit mask
324 static int makemask (const char *smask
, int count
) {
326 if (strchr(smask
, 'c')) mask
|= LUA_MASKCALL
;
327 if (strchr(smask
, 'r')) mask
|= LUA_MASKRET
;
328 if (strchr(smask
, 'l')) mask
|= LUA_MASKLINE
;
329 if (count
> 0) mask
|= LUA_MASKCOUNT
;
335 ** Convert a bit mask (for 'gethook') into a string mask
337 static char *unmakemask (int mask
, char *smask
) {
339 if (mask
& LUA_MASKCALL
) smask
[i
++] = 'c';
340 if (mask
& LUA_MASKRET
) smask
[i
++] = 'r';
341 if (mask
& LUA_MASKLINE
) smask
[i
++] = 'l';
347 static int db_sethook (lua_State
*L
) {
348 int arg
, mask
, count
;
350 lua_State
*L1
= getthread(L
, &arg
);
351 if (lua_isnoneornil(L
, arg
+1)) { /* no hook? */
352 lua_settop(L
, arg
+1);
353 func
= NULL
; mask
= 0; count
= 0; /* turn off hooks */
356 const char *smask
= luaL_checkstring(L
, arg
+2);
357 luaL_checktype(L
, arg
+1, LUA_TFUNCTION
);
358 count
= (int)luaL_optinteger(L
, arg
+ 3, 0);
359 func
= hookf
; mask
= makemask(smask
, count
);
361 if (lua_rawgetp(L
, LUA_REGISTRYINDEX
, &HOOKKEY
) == LUA_TNIL
) {
362 lua_createtable(L
, 0, 2); /* create a hook table */
363 lua_pushvalue(L
, -1);
364 lua_rawsetp(L
, LUA_REGISTRYINDEX
, &HOOKKEY
); /* set it in position */
365 lua_pushstring(L
, "k");
366 lua_setfield(L
, -2, "__mode"); /** hooktable.__mode = "k" */
367 lua_pushvalue(L
, -1);
368 lua_setmetatable(L
, -2); /* setmetatable(hooktable) = hooktable */
370 checkstack(L
, L1
, 1);
371 lua_pushthread(L1
); lua_xmove(L1
, L
, 1); /* key (thread) */
372 lua_pushvalue(L
, arg
+ 1); /* value (hook function) */
373 lua_rawset(L
, -3); /* hooktable[L1] = new Lua hook */
374 lua_sethook(L1
, func
, mask
, count
);
379 static int db_gethook (lua_State
*L
) {
381 lua_State
*L1
= getthread(L
, &arg
);
383 int mask
= lua_gethookmask(L1
);
384 lua_Hook hook
= lua_gethook(L1
);
385 if (hook
== NULL
) /* no hook? */
387 else if (hook
!= hookf
) /* external hook? */
388 lua_pushliteral(L
, "external hook");
389 else { /* hook table must exist */
390 lua_rawgetp(L
, LUA_REGISTRYINDEX
, &HOOKKEY
);
391 checkstack(L
, L1
, 1);
392 lua_pushthread(L1
); lua_xmove(L1
, L
, 1);
393 lua_rawget(L
, -2); /* 1st result = hooktable[L1] */
394 lua_remove(L
, -2); /* remove hook table */
396 lua_pushstring(L
, unmakemask(mask
, buff
)); /* 2nd result = mask */
397 lua_pushinteger(L
, lua_gethookcount(L1
)); /* 3rd result = count */
402 static int db_debug (lua_State
*L
) {
405 lua_writestringerror("%s", "lua_debug> ");
406 if (fgets(buffer
, sizeof(buffer
), stdin
) == 0 ||
407 strcmp(buffer
, "cont\n") == 0)
409 if (luaL_loadbuffer(L
, buffer
, strlen(buffer
), "=(debug command)") ||
410 lua_pcall(L
, 0, 0, 0))
411 lua_writestringerror("%s\n", lua_tostring(L
, -1));
412 lua_settop(L
, 0); /* remove eventual returns */
417 static int db_traceback (lua_State
*L
) {
419 lua_State
*L1
= getthread(L
, &arg
);
420 const char *msg
= lua_tostring(L
, arg
+ 1);
421 if (msg
== NULL
&& !lua_isnoneornil(L
, arg
+ 1)) /* non-string 'msg'? */
422 lua_pushvalue(L
, arg
+ 1); /* return it untouched */
424 int level
= (int)luaL_optinteger(L
, arg
+ 2, (L
== L1
) ? 1 : 0);
425 luaL_traceback(L
, L1
, msg
, level
);
431 static const luaL_Reg dblib
[] = {
433 {"getuservalue", db_getuservalue
},
434 {"gethook", db_gethook
},
435 {"getinfo", db_getinfo
},
436 {"getlocal", db_getlocal
},
437 {"getregistry", db_getregistry
},
438 {"getmetatable", db_getmetatable
},
439 {"getupvalue", db_getupvalue
},
440 {"upvaluejoin", db_upvaluejoin
},
441 {"upvalueid", db_upvalueid
},
442 {"setuservalue", db_setuservalue
},
443 {"sethook", db_sethook
},
444 {"setlocal", db_setlocal
},
445 {"setmetatable", db_setmetatable
},
446 {"setupvalue", db_setupvalue
},
447 {"traceback", db_traceback
},
452 LUAMOD_API
int luaopen_debug (lua_State
*L
) {
453 luaL_newlib(L
, dblib
);