Merge pull request #2672 from kitsunehunter/laundry-keys
[RRG-proxmark3.git] / client / deps / liblua / ldblib.c
blob3da76fc69487e72902c05dc117fd69e92a5a4bc7
1 /*
2 ** $Id: ldblib.c $
3 ** Interface from Lua to its debug API
4 ** See Copyright Notice in lua.h
5 */
7 #define ldblib_c
8 #define LUA_LIB
10 #include "lprefix.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include "lua.h"
19 #include "lauxlib.h"
20 #include "lualib.h"
24 ** The hook table at registry[HOOKKEY] maps threads to their current
25 ** hook function.
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
33 ** checked.
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);
43 return 1;
47 static int db_getmetatable(lua_State *L) {
48 luaL_checkany(L, 1);
49 if (!lua_getmetatable(L, 1)) {
50 lua_pushnil(L); /* no metatable */
52 return 1;
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");
59 lua_settop(L, 2);
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)
68 luaL_pushfail(L);
69 else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
70 lua_pushboolean(L, 1);
71 return 2;
73 return 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);
80 luaL_checkany(L, 2);
81 lua_settop(L, 2);
82 if (!lua_setiuservalue(L, 1, n))
83 luaL_pushfail(L);
84 return 1;
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)) {
96 *arg = 1;
97 return lua_tothread(L, 1);
98 } else {
99 *arg = 0;
100 return L; /* function will operate over current thread */
106 ** Variations of 'lua_settable', used by 'db_getinfo' to put results
107 ** from 'lua_getinfo' into result table. Key is always a string;
108 ** value can be a string, an int, or a boolean.
110 static void settabss(lua_State *L, const char *k, const char *v) {
111 lua_pushstring(L, v);
112 lua_setfield(L, -2, k);
115 static void settabsi(lua_State *L, const char *k, int v) {
116 lua_pushinteger(L, v);
117 lua_setfield(L, -2, k);
120 static void settabsb(lua_State *L, const char *k, int v) {
121 lua_pushboolean(L, v);
122 lua_setfield(L, -2, k);
127 ** In function 'db_getinfo', the call to 'lua_getinfo' may push
128 ** results on the stack; later it creates the result table to put
129 ** these objects. Function 'treatstackoption' puts the result from
130 ** 'lua_getinfo' on top of the result table so that it can call
131 ** 'lua_setfield'.
133 static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) {
134 if (L == L1)
135 lua_rotate(L, -2, 1); /* exchange object and table */
136 else
137 lua_xmove(L1, L, 1); /* move object to the "main" stack */
138 lua_setfield(L, -2, fname); /* put object into table */
143 ** Calls 'lua_getinfo' and collects all results in a new table.
144 ** L1 needs stack space for an optional input (function) plus
145 ** two optional outputs (function and line table) from function
146 ** 'lua_getinfo'.
148 static int db_getinfo(lua_State *L) {
149 lua_Debug ar;
150 int arg;
151 lua_State *L1 = getthread(L, &arg);
152 const char *options = luaL_optstring(L, arg + 2, "flnSrtu");
153 checkstack(L, L1, 3);
154 luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
155 if (lua_isfunction(L, arg + 1)) { /* info about a function? */
156 options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
157 lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
158 lua_xmove(L, L1, 1);
159 } else { /* stack level */
160 if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
161 luaL_pushfail(L); /* level out of range */
162 return 1;
165 if (!lua_getinfo(L1, options, &ar))
166 return luaL_argerror(L, arg + 2, "invalid option");
167 lua_newtable(L); /* table to collect results */
168 if (strchr(options, 'S')) {
169 lua_pushlstring(L, ar.source, ar.srclen);
170 lua_setfield(L, -2, "source");
171 settabss(L, "short_src", ar.short_src);
172 settabsi(L, "linedefined", ar.linedefined);
173 settabsi(L, "lastlinedefined", ar.lastlinedefined);
174 settabss(L, "what", ar.what);
176 if (strchr(options, 'l'))
177 settabsi(L, "currentline", ar.currentline);
178 if (strchr(options, 'u')) {
179 settabsi(L, "nups", ar.nups);
180 settabsi(L, "nparams", ar.nparams);
181 settabsb(L, "isvararg", ar.isvararg);
183 if (strchr(options, 'n')) {
184 settabss(L, "name", ar.name);
185 settabss(L, "namewhat", ar.namewhat);
187 if (strchr(options, 'r')) {
188 settabsi(L, "ftransfer", ar.ftransfer);
189 settabsi(L, "ntransfer", ar.ntransfer);
191 if (strchr(options, 't'))
192 settabsb(L, "istailcall", ar.istailcall);
193 if (strchr(options, 'L'))
194 treatstackoption(L, L1, "activelines");
195 if (strchr(options, 'f'))
196 treatstackoption(L, L1, "func");
197 return 1; /* return table */
201 static int db_getlocal(lua_State *L) {
202 int arg;
203 lua_State *L1 = getthread(L, &arg);
204 int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
205 if (lua_isfunction(L, arg + 1)) { /* function argument? */
206 lua_pushvalue(L, arg + 1); /* push function */
207 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
208 return 1; /* return only name (there is no value) */
209 } else { /* stack-level argument */
210 lua_Debug ar;
211 const char *name;
212 int level = (int)luaL_checkinteger(L, arg + 1);
213 if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
214 return luaL_argerror(L, arg + 1, "level out of range");
215 checkstack(L, L1, 1);
216 name = lua_getlocal(L1, &ar, nvar);
217 if (name) {
218 lua_xmove(L1, L, 1); /* move local value */
219 lua_pushstring(L, name); /* push name */
220 lua_rotate(L, -2, 1); /* re-order */
221 return 2;
222 } else {
223 luaL_pushfail(L); /* no name (nor value) */
224 return 1;
230 static int db_setlocal(lua_State *L) {
231 int arg;
232 const char *name;
233 lua_State *L1 = getthread(L, &arg);
234 lua_Debug ar;
235 int level = (int)luaL_checkinteger(L, arg + 1);
236 int nvar = (int)luaL_checkinteger(L, arg + 2);
237 if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
238 return luaL_argerror(L, arg + 1, "level out of range");
239 luaL_checkany(L, arg + 3);
240 lua_settop(L, arg + 3);
241 checkstack(L, L1, 1);
242 lua_xmove(L, L1, 1);
243 name = lua_setlocal(L1, &ar, nvar);
244 if (name == NULL)
245 lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
246 lua_pushstring(L, name);
247 return 1;
252 ** get (if 'get' is true) or set an upvalue from a closure
254 static int auxupvalue(lua_State *L, int get) {
255 const char *name;
256 int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
257 luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
258 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
259 if (name == NULL) return 0;
260 lua_pushstring(L, name);
261 lua_insert(L, -(get + 1)); /* no-op if get is false */
262 return get + 1;
266 static int db_getupvalue(lua_State *L) {
267 return auxupvalue(L, 1);
271 static int db_setupvalue(lua_State *L) {
272 luaL_checkany(L, 3);
273 return auxupvalue(L, 0);
278 ** Check whether a given upvalue from a given closure exists and
279 ** returns its index
281 static void *checkupval(lua_State *L, int argf, int argnup, int *pnup) {
282 void *id;
283 int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
284 luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
285 id = lua_upvalueid(L, argf, nup);
286 if (pnup) {
287 luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
288 *pnup = nup;
290 return id;
294 static int db_upvalueid(lua_State *L) {
295 void *id = checkupval(L, 1, 2, NULL);
296 if (id != NULL)
297 lua_pushlightuserdata(L, id);
298 else
299 luaL_pushfail(L);
300 return 1;
304 static int db_upvaluejoin(lua_State *L) {
305 int n1, n2;
306 checkupval(L, 1, 2, &n1);
307 checkupval(L, 3, 4, &n2);
308 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
309 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
310 lua_upvaluejoin(L, 1, n1, 3, n2);
311 return 0;
316 ** Call hook function registered at hook table for the current
317 ** thread (if there is one)
319 static void hookf(lua_State *L, lua_Debug *ar) {
320 static const char *const hooknames[] =
321 {"call", "return", "line", "count", "tail call"};
322 lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
323 lua_pushthread(L);
324 if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
325 lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
326 if (ar->currentline >= 0)
327 lua_pushinteger(L, ar->currentline); /* push current line */
328 else lua_pushnil(L);
329 lua_assert(lua_getinfo(L, "lS", ar));
330 lua_call(L, 2, 0); /* call hook function */
336 ** Convert a string mask (for 'sethook') into a bit mask
338 static int makemask(const char *smask, int count) {
339 int mask = 0;
340 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
341 if (strchr(smask, 'r')) mask |= LUA_MASKRET;
342 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
343 if (count > 0) mask |= LUA_MASKCOUNT;
344 return mask;
349 ** Convert a bit mask (for 'gethook') into a string mask
351 static char *unmakemask(int mask, char *smask) {
352 int i = 0;
353 if (mask & LUA_MASKCALL) smask[i++] = 'c';
354 if (mask & LUA_MASKRET) smask[i++] = 'r';
355 if (mask & LUA_MASKLINE) smask[i++] = 'l';
356 smask[i] = '\0';
357 return smask;
361 static int db_sethook(lua_State *L) {
362 int arg, mask, count;
363 lua_Hook func;
364 lua_State *L1 = getthread(L, &arg);
365 if (lua_isnoneornil(L, arg + 1)) { /* no hook? */
366 lua_settop(L, arg + 1);
367 func = NULL;
368 mask = 0;
369 count = 0; /* turn off hooks */
370 } else {
371 const char *smask = luaL_checkstring(L, arg + 2);
372 luaL_checktype(L, arg + 1, LUA_TFUNCTION);
373 count = (int)luaL_optinteger(L, arg + 3, 0);
374 func = hookf;
375 mask = makemask(smask, count);
377 if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
378 /* table just created; initialize it */
379 lua_pushliteral(L, "k");
380 lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
381 lua_pushvalue(L, -1);
382 lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
384 checkstack(L, L1, 1);
385 lua_pushthread(L1);
386 lua_xmove(L1, L, 1); /* key (thread) */
387 lua_pushvalue(L, arg + 1); /* value (hook function) */
388 lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
389 lua_sethook(L1, func, mask, count);
390 return 0;
394 static int db_gethook(lua_State *L) {
395 int arg;
396 lua_State *L1 = getthread(L, &arg);
397 char buff[5];
398 int mask = lua_gethookmask(L1);
399 lua_Hook hook = lua_gethook(L1);
400 if (hook == NULL) { /* no hook? */
401 luaL_pushfail(L);
402 return 1;
403 } else if (hook != hookf) /* external hook? */
404 lua_pushliteral(L, "external hook");
405 else { /* hook table must exist */
406 lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
407 checkstack(L, L1, 1);
408 lua_pushthread(L1);
409 lua_xmove(L1, L, 1);
410 lua_rawget(L, -2); /* 1st result = hooktable[L1] */
411 lua_remove(L, -2); /* remove hook table */
413 lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
414 lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
415 return 3;
419 static int db_debug(lua_State *L) {
420 for (;;) {
421 char buffer[250];
422 lua_writestringerror("%s", "lua_debug> ");
423 if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
424 strcmp(buffer, "cont\n") == 0)
425 return 0;
426 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
427 lua_pcall(L, 0, 0, 0))
428 lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
429 lua_settop(L, 0); /* remove eventual returns */
434 static int db_traceback(lua_State *L) {
435 int arg;
436 lua_State *L1 = getthread(L, &arg);
437 const char *msg = lua_tostring(L, arg + 1);
438 if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
439 lua_pushvalue(L, arg + 1); /* return it untouched */
440 else {
441 int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
442 luaL_traceback(L, L1, msg, level);
444 return 1;
448 static int db_setcstacklimit(lua_State *L) {
449 int limit = (int)luaL_checkinteger(L, 1);
450 int res = lua_setcstacklimit(L, limit);
451 lua_pushinteger(L, res);
452 return 1;
456 static const luaL_Reg dblib[] = {
457 {"debug", db_debug},
458 {"getuservalue", db_getuservalue},
459 {"gethook", db_gethook},
460 {"getinfo", db_getinfo},
461 {"getlocal", db_getlocal},
462 {"getregistry", db_getregistry},
463 {"getmetatable", db_getmetatable},
464 {"getupvalue", db_getupvalue},
465 {"upvaluejoin", db_upvaluejoin},
466 {"upvalueid", db_upvalueid},
467 {"setuservalue", db_setuservalue},
468 {"sethook", db_sethook},
469 {"setlocal", db_setlocal},
470 {"setmetatable", db_setmetatable},
471 {"setupvalue", db_setupvalue},
472 {"traceback", db_traceback},
473 {"setcstacklimit", db_setcstacklimit},
474 {NULL, NULL}
478 LUAMOD_API int luaopen_debug(lua_State *L) {
479 luaL_newlib(L, dblib);
480 return 1;