1 /*
2 ** $Id: ldblib.c $
3 ** Interface from Lua to its debug API
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define ldblib_c
8 #define LUA_LIB
9 
10 #include "lprefix.h"
11 
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "lua.h"
18 
19 #include "lauxlib.h"
20 #include "lualib.h"
21 
22 
23 /*
24 ** The hook table at registry[HOOKKEY] maps threads to their current
25 ** hook function.
26 */
27 static const char *const HOOKKEY = "_HOOKKEY";
28 
29 
30 /*
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.
34 */
checkstack(lua_State * L,lua_State * L1,int n)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");
38 }
39 
40 
db_getregistry(lua_State * L)41 static int db_getregistry (lua_State *L) {
42   lua_pushvalue(L, LUA_REGISTRYINDEX);
43   return 1;
44 }
45 
46 
db_getmetatable(lua_State * L)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 */
51   }
52   return 1;
53 }
54 
55 
db_setmetatable(lua_State * L)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 */
62 }
63 
64 
db_getuservalue(lua_State * L)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;
72   }
73   return 1;
74 }
75 
76 
db_setuservalue(lua_State * L)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;
85 }
86 
87 
88 /*
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)
93 */
getthread(lua_State * L,int * arg)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   }
99   else {
100     *arg = 0;
101     return L;  /* function will operate over current thread */
102   }
103 }
104 
105 
106 /*
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.
110 */
settabss(lua_State * L,const char * k,const char * v)111 static void settabss (lua_State *L, const char *k, const char *v) {
112   lua_pushstring(L, v);
113   lua_setfield(L, -2, k);
114 }
115 
settabsi(lua_State * L,const char * k,int v)116 static void settabsi (lua_State *L, const char *k, int v) {
117   lua_pushinteger(L, v);
118   lua_setfield(L, -2, k);
119 }
120 
settabsb(lua_State * L,const char * k,int v)121 static void settabsb (lua_State *L, const char *k, int v) {
122   lua_pushboolean(L, v);
123   lua_setfield(L, -2, k);
124 }
125 
126 
127 /*
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
132 ** 'lua_setfield'.
133 */
treatstackoption(lua_State * L,lua_State * L1,const char * fname)134 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
135   if (L == L1)
136     lua_rotate(L, -2, 1);  /* exchange object and table */
137   else
138     lua_xmove(L1, L, 1);  /* move object to the "main" stack */
139   lua_setfield(L, -2, fname);  /* put object into table */
140 }
141 
142 
143 /*
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
147 ** 'lua_getinfo'.
148 */
db_getinfo(lua_State * L)149 static int db_getinfo (lua_State *L) {
150   lua_Debug ar;
151   int arg;
152   lua_State *L1 = getthread(L, &arg);
153   const char *options = luaL_optstring(L, arg+2, "flnSrtu");
154   checkstack(L, L1, 3);
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   }
160   else {  /* stack level */
161     if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
162       luaL_pushfail(L);  /* level out of range */
163       return 1;
164     }
165   }
166   if (!lua_getinfo(L1, options, &ar))
167     return luaL_argerror(L, arg+2, "invalid option");
168   lua_newtable(L);  /* table to collect results */
169   if (strchr(options, 'S')) {
170     lua_pushlstring(L, ar.source, ar.srclen);
171     lua_setfield(L, -2, "source");
172     settabss(L, "short_src", ar.short_src);
173     settabsi(L, "linedefined", ar.linedefined);
174     settabsi(L, "lastlinedefined", ar.lastlinedefined);
175     settabss(L, "what", ar.what);
176   }
177   if (strchr(options, 'l'))
178     settabsi(L, "currentline", ar.currentline);
179   if (strchr(options, 'u')) {
180     settabsi(L, "nups", ar.nups);
181     settabsi(L, "nparams", ar.nparams);
182     settabsb(L, "isvararg", ar.isvararg);
183   }
184   if (strchr(options, 'n')) {
185     settabss(L, "name", ar.name);
186     settabss(L, "namewhat", ar.namewhat);
187   }
188   if (strchr(options, 'r')) {
189     settabsi(L, "ftransfer", ar.ftransfer);
190     settabsi(L, "ntransfer", ar.ntransfer);
191   }
192   if (strchr(options, 't'))
193     settabsb(L, "istailcall", ar.istailcall);
194   if (strchr(options, 'L'))
195     treatstackoption(L, L1, "activelines");
196   if (strchr(options, 'f'))
197     treatstackoption(L, L1, "func");
198   return 1;  /* return table */
199 }
200 
201 
db_getlocal(lua_State * L)202 static int db_getlocal (lua_State *L) {
203   int arg;
204   lua_State *L1 = getthread(L, &arg);
205   int nvar = (int)luaL_checkinteger(L, arg + 2);  /* local-variable index */
206   if (lua_isfunction(L, arg + 1)) {  /* function argument? */
207     lua_pushvalue(L, arg + 1);  /* push function */
208     lua_pushstring(L, lua_getlocal(L, NULL, nvar));  /* push local name */
209     return 1;  /* return only name (there is no value) */
210   }
211   else {  /* stack-level argument */
212     lua_Debug ar;
213     const char *name;
214     int level = (int)luaL_checkinteger(L, arg + 1);
215     if (!lua_getstack(L1, level, &ar))  /* out of range? */
216       return luaL_argerror(L, arg+1, "level out of range");
217     checkstack(L, L1, 1);
218     name = lua_getlocal(L1, &ar, nvar);
219     if (name) {
220       lua_xmove(L1, L, 1);  /* move local value */
221       lua_pushstring(L, name);  /* push name */
222       lua_rotate(L, -2, 1);  /* re-order */
223       return 2;
224     }
225     else {
226       luaL_pushfail(L);  /* no name (nor value) */
227       return 1;
228     }
229   }
230 }
231 
232 
db_setlocal(lua_State * L)233 static int db_setlocal (lua_State *L) {
234   int arg;
235   const char *name;
236   lua_State *L1 = getthread(L, &arg);
237   lua_Debug ar;
238   int level = (int)luaL_checkinteger(L, arg + 1);
239   int nvar = (int)luaL_checkinteger(L, arg + 2);
240   if (!lua_getstack(L1, level, &ar))  /* out of range? */
241     return luaL_argerror(L, arg+1, "level out of range");
242   luaL_checkany(L, arg+3);
243   lua_settop(L, arg+3);
244   checkstack(L, L1, 1);
245   lua_xmove(L, L1, 1);
246   name = lua_setlocal(L1, &ar, nvar);
247   if (name == NULL)
248     lua_pop(L1, 1);  /* pop value (if not popped by 'lua_setlocal') */
249   lua_pushstring(L, name);
250   return 1;
251 }
252 
253 
254 /*
255 ** get (if 'get' is true) or set an upvalue from a closure
256 */
auxupvalue(lua_State * L,int get)257 static int auxupvalue (lua_State *L, int get) {
258   const char *name;
259   int n = (int)luaL_checkinteger(L, 2);  /* upvalue index */
260   luaL_checktype(L, 1, LUA_TFUNCTION);  /* closure */
261   name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
262   if (name == NULL) return 0;
263   lua_pushstring(L, name);
264   lua_insert(L, -(get+1));  /* no-op if get is false */
265   return get + 1;
266 }
267 
268 
db_getupvalue(lua_State * L)269 static int db_getupvalue (lua_State *L) {
270   return auxupvalue(L, 1);
271 }
272 
273 
db_setupvalue(lua_State * L)274 static int db_setupvalue (lua_State *L) {
275   luaL_checkany(L, 3);
276   return auxupvalue(L, 0);
277 }
278 
279 
280 /*
281 ** Check whether a given upvalue from a given closure exists and
282 ** returns its index
283 */
checkupval(lua_State * L,int argf,int argnup)284 static int checkupval (lua_State *L, int argf, int argnup) {
285   int nup = (int)luaL_checkinteger(L, argnup);  /* upvalue index */
286   luaL_checktype(L, argf, LUA_TFUNCTION);  /* closure */
287   luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
288                    "invalid upvalue index");
289   return nup;
290 }
291 
292 
db_upvalueid(lua_State * L)293 static int db_upvalueid (lua_State *L) {
294   int n = checkupval(L, 1, 2);
295   lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
296   return 1;
297 }
298 
299 
db_upvaluejoin(lua_State * L)300 static int db_upvaluejoin (lua_State *L) {
301   int n1 = checkupval(L, 1, 2);
302   int n2 = checkupval(L, 3, 4);
303   luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
304   luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
305   lua_upvaluejoin(L, 1, n1, 3, n2);
306   return 0;
307 }
308 
309 
310 /*
311 ** Call hook function registered at hook table for the current
312 ** thread (if there is one)
313 */
hookf(lua_State * L,lua_Debug * ar)314 static void hookf (lua_State *L, lua_Debug *ar) {
315   static const char *const hooknames[] =
316     {"call", "return", "line", "count", "tail call"};
317   lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
318   lua_pushthread(L);
319   if (lua_rawget(L, -2) == LUA_TFUNCTION) {  /* is there a hook function? */
320     lua_pushstring(L, hooknames[(int)ar->event]);  /* push event name */
321     if (ar->currentline >= 0)
322       lua_pushinteger(L, ar->currentline);  /* push current line */
323     else lua_pushnil(L);
324     lua_assert(lua_getinfo(L, "lS", ar));
325     lua_call(L, 2, 0);  /* call hook function */
326   }
327 }
328 
329 
330 /*
331 ** Convert a string mask (for 'sethook') into a bit mask
332 */
makemask(const char * smask,int count)333 static int makemask (const char *smask, int count) {
334   int mask = 0;
335   if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
336   if (strchr(smask, 'r')) mask |= LUA_MASKRET;
337   if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
338   if (count > 0) mask |= LUA_MASKCOUNT;
339   return mask;
340 }
341 
342 
343 /*
344 ** Convert a bit mask (for 'gethook') into a string mask
345 */
unmakemask(int mask,char * smask)346 static char *unmakemask (int mask, char *smask) {
347   int i = 0;
348   if (mask & LUA_MASKCALL) smask[i++] = 'c';
349   if (mask & LUA_MASKRET) smask[i++] = 'r';
350   if (mask & LUA_MASKLINE) smask[i++] = 'l';
351   smask[i] = '\0';
352   return smask;
353 }
354 
355 
db_sethook(lua_State * L)356 static int db_sethook (lua_State *L) {
357   int arg, mask, count;
358   lua_Hook func;
359   lua_State *L1 = getthread(L, &arg);
360   if (lua_isnoneornil(L, arg+1)) {  /* no hook? */
361     lua_settop(L, arg+1);
362     func = NULL; mask = 0; count = 0;  /* turn off hooks */
363   }
364   else {
365     const char *smask = luaL_checkstring(L, arg+2);
366     luaL_checktype(L, arg+1, LUA_TFUNCTION);
367     count = (int)luaL_optinteger(L, arg + 3, 0);
368     func = hookf; mask = makemask(smask, count);
369   }
370   if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
371     /* table just created; initialize it */
372     lua_pushstring(L, "k");
373     lua_setfield(L, -2, "__mode");  /** hooktable.__mode = "k" */
374     lua_pushvalue(L, -1);
375     lua_setmetatable(L, -2);  /* metatable(hooktable) = hooktable */
376   }
377   checkstack(L, L1, 1);
378   lua_pushthread(L1); lua_xmove(L1, L, 1);  /* key (thread) */
379   lua_pushvalue(L, arg + 1);  /* value (hook function) */
380   lua_rawset(L, -3);  /* hooktable[L1] = new Lua hook */
381   lua_sethook(L1, func, mask, count);
382   return 0;
383 }
384 
385 
db_gethook(lua_State * L)386 static int db_gethook (lua_State *L) {
387   int arg;
388   lua_State *L1 = getthread(L, &arg);
389   char buff[5];
390   int mask = lua_gethookmask(L1);
391   lua_Hook hook = lua_gethook(L1);
392   if (hook == NULL) {  /* no hook? */
393     luaL_pushfail(L);
394     return 1;
395   }
396   else if (hook != hookf)  /* external hook? */
397     lua_pushliteral(L, "external hook");
398   else {  /* hook table must exist */
399     lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
400     checkstack(L, L1, 1);
401     lua_pushthread(L1); lua_xmove(L1, L, 1);
402     lua_rawget(L, -2);   /* 1st result = hooktable[L1] */
403     lua_remove(L, -2);  /* remove hook table */
404   }
405   lua_pushstring(L, unmakemask(mask, buff));  /* 2nd result = mask */
406   lua_pushinteger(L, lua_gethookcount(L1));  /* 3rd result = count */
407   return 3;
408 }
409 
410 
db_debug(lua_State * L)411 static int db_debug (lua_State *L) {
412   for (;;) {
413     char buffer[250];
414     lua_writestringerror("%s", "lua_debug> ");
415     if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
416         strcmp(buffer, "cont\n") == 0)
417       return 0;
418     if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
419         lua_pcall(L, 0, 0, 0))
420       lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
421     lua_settop(L, 0);  /* remove eventual returns */
422   }
423 }
424 
425 
db_traceback(lua_State * L)426 static int db_traceback (lua_State *L) {
427   int arg;
428   lua_State *L1 = getthread(L, &arg);
429   const char *msg = lua_tostring(L, arg + 1);
430   if (msg == NULL && !lua_isnoneornil(L, arg + 1))  /* non-string 'msg'? */
431     lua_pushvalue(L, arg + 1);  /* return it untouched */
432   else {
433     int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
434     luaL_traceback(L, L1, msg, level);
435   }
436   return 1;
437 }
438 
439 
db_setcstacklimit(lua_State * L)440 static int db_setcstacklimit (lua_State *L) {
441   int limit = (int)luaL_checkinteger(L, 1);
442   int res = lua_setcstacklimit(L, limit);
443   if (res == 0)
444     lua_pushboolean(L, 0);
445   else
446     lua_pushinteger(L, res);
447   return 1;
448 }
449 
450 
451 static const luaL_Reg dblib[] = {
452   {"debug", db_debug},
453   {"getuservalue", db_getuservalue},
454   {"gethook", db_gethook},
455   {"getinfo", db_getinfo},
456   {"getlocal", db_getlocal},
457   {"getregistry", db_getregistry},
458   {"getmetatable", db_getmetatable},
459   {"getupvalue", db_getupvalue},
460   {"upvaluejoin", db_upvaluejoin},
461   {"upvalueid", db_upvalueid},
462   {"setuservalue", db_setuservalue},
463   {"sethook", db_sethook},
464   {"setlocal", db_setlocal},
465   {"setmetatable", db_setmetatable},
466   {"setupvalue", db_setupvalue},
467   {"traceback", db_traceback},
468   {"setcstacklimit", db_setcstacklimit},
469   {NULL, NULL}
470 };
471 
472 
luaopen_debug(lua_State * L)473 LUAMOD_API int luaopen_debug (lua_State *L) {
474   luaL_newlib(L, dblib);
475   return 1;
476 }
477 
478