1 /*
2 ** $Id: ldblib.c,v 1.148 2015/01/02 12:52:22 roberto Exp $
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. (We only need the unique address of 'HOOKKEY'.)
26 */
27 static const int HOOKKEY = 0;
28 
29 
db_getregistry(lua_State * L)30 static int db_getregistry (lua_State *L) {
31   lua_pushvalue(L, LUA_REGISTRYINDEX);
32   return 1;
33 }
34 
35 
db_getmetatable(lua_State * L)36 static int db_getmetatable (lua_State *L) {
37   luaL_checkany(L, 1);
38   if (!lua_getmetatable(L, 1)) {
39     lua_pushnil(L);  /* no metatable */
40   }
41   return 1;
42 }
43 
44 
db_setmetatable(lua_State * L)45 static int db_setmetatable (lua_State *L) {
46   int t = lua_type(L, 2);
47   luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
48                     "nil or table expected");
49   lua_settop(L, 2);
50   lua_setmetatable(L, 1);
51   return 1;  /* return 1st argument */
52 }
53 
54 
db_getuservalue(lua_State * L)55 static int db_getuservalue (lua_State *L) {
56   if (lua_type(L, 1) != LUA_TUSERDATA)
57     lua_pushnil(L);
58   else
59     lua_getuservalue(L, 1);
60   return 1;
61 }
62 
63 
db_setuservalue(lua_State * L)64 static int db_setuservalue (lua_State *L) {
65   luaL_checktype(L, 1, LUA_TUSERDATA);
66   luaL_checkany(L, 2);
67   lua_settop(L, 2);
68   lua_setuservalue(L, 1);
69   return 1;
70 }
71 
72 
73 /*
74 ** Auxiliary function used by several library functions: check for
75 ** an optional thread as function's first argument and set 'arg' with
76 ** 1 if this argument is present (so that functions can skip it to
77 ** access their other arguments)
78 */
getthread(lua_State * L,int * arg)79 static lua_State *getthread (lua_State *L, int *arg) {
80   if (lua_isthread(L, 1)) {
81     *arg = 1;
82     return lua_tothread(L, 1);
83   }
84   else {
85     *arg = 0;
86     return L;  /* function will operate over current thread */
87   }
88 }
89 
90 
91 /*
92 ** Variations of 'lua_settable', used by 'db_getinfo' to put results
93 ** from 'lua_getinfo' into result table. Key is always a string;
94 ** value can be a string, an int, or a boolean.
95 */
settabss(lua_State * L,const char * k,const char * v)96 static void settabss (lua_State *L, const char *k, const char *v) {
97   lua_pushstring(L, v);
98   lua_setfield(L, -2, k);
99 }
100 
settabsi(lua_State * L,const char * k,int v)101 static void settabsi (lua_State *L, const char *k, int v) {
102   lua_pushinteger(L, v);
103   lua_setfield(L, -2, k);
104 }
105 
settabsb(lua_State * L,const char * k,int v)106 static void settabsb (lua_State *L, const char *k, int v) {
107   lua_pushboolean(L, v);
108   lua_setfield(L, -2, k);
109 }
110 
111 
112 /*
113 ** In function 'db_getinfo', the call to 'lua_getinfo' may push
114 ** results on the stack; later it creates the result table to put
115 ** these objects. Function 'treatstackoption' puts the result from
116 ** 'lua_getinfo' on top of the result table so that it can call
117 ** 'lua_setfield'.
118 */
treatstackoption(lua_State * L,lua_State * L1,const char * fname)119 static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
120   if (L == L1)
121     lua_rotate(L, -2, 1);  /* exchange object and table */
122   else
123     lua_xmove(L1, L, 1);  /* move object to the "main" stack */
124   lua_setfield(L, -2, fname);  /* put object into table */
125 }
126 
127 
128 /*
129 ** Calls 'lua_getinfo' and collects all results in a new table.
130 */
db_getinfo(lua_State * L)131 static int db_getinfo (lua_State *L) {
132   lua_Debug ar;
133   int arg;
134   lua_State *L1 = getthread(L, &arg);
135   const char *options = luaL_optstring(L, arg+2, "flnStu");
136   if (lua_isfunction(L, arg + 1)) {  /* info about a function? */
137     options = lua_pushfstring(L, ">%s", options);  /* add '>' to 'options' */
138     lua_pushvalue(L, arg + 1);  /* move function to 'L1' stack */
139     lua_xmove(L, L1, 1);
140   }
141   else {  /* stack level */
142     if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
143       lua_pushnil(L);  /* level out of range */
144       return 1;
145     }
146   }
147   if (!lua_getinfo(L1, options, &ar))
148     return luaL_argerror(L, arg+2, "invalid option");
149   lua_newtable(L);  /* table to collect results */
150   if (strchr(options, 'S')) {
151     settabss(L, "source", ar.source);
152     settabss(L, "short_src", ar.short_src);
153     settabsi(L, "linedefined", ar.linedefined);
154     settabsi(L, "lastlinedefined", ar.lastlinedefined);
155     settabss(L, "what", ar.what);
156   }
157   if (strchr(options, 'l'))
158     settabsi(L, "currentline", ar.currentline);
159   if (strchr(options, 'u')) {
160     settabsi(L, "nups", ar.nups);
161     settabsi(L, "nparams", ar.nparams);
162     settabsb(L, "isvararg", ar.isvararg);
163   }
164   if (strchr(options, 'n')) {
165     settabss(L, "name", ar.name);
166     settabss(L, "namewhat", ar.namewhat);
167   }
168   if (strchr(options, 't'))
169     settabsb(L, "istailcall", ar.istailcall);
170   if (strchr(options, 'L'))
171     treatstackoption(L, L1, "activelines");
172   if (strchr(options, 'f'))
173     treatstackoption(L, L1, "func");
174   return 1;  /* return table */
175 }
176 
177 
db_getlocal(lua_State * L)178 static int db_getlocal (lua_State *L) {
179   int arg;
180   lua_State *L1 = getthread(L, &arg);
181   lua_Debug ar;
182   const char *name;
183   int nvar = (int)luaL_checkinteger(L, arg + 2);  /* local-variable index */
184   if (lua_isfunction(L, arg + 1)) {  /* function argument? */
185     lua_pushvalue(L, arg + 1);  /* push function */
186     lua_pushstring(L, lua_getlocal(L, NULL, nvar));  /* push local name */
187     return 1;  /* return only name (there is no value) */
188   }
189   else {  /* stack-level argument */
190     int level = (int)luaL_checkinteger(L, arg + 1);
191     if (!lua_getstack(L1, level, &ar))  /* out of range? */
192       return luaL_argerror(L, arg+1, "level out of range");
193     name = lua_getlocal(L1, &ar, nvar);
194     if (name) {
195       lua_xmove(L1, L, 1);  /* move local value */
196       lua_pushstring(L, name);  /* push name */
197       lua_rotate(L, -2, 1);  /* re-order */
198       return 2;
199     }
200     else {
201       lua_pushnil(L);  /* no name (nor value) */
202       return 1;
203     }
204   }
205 }
206 
207 
db_setlocal(lua_State * L)208 static int db_setlocal (lua_State *L) {
209   int arg;
210   const char *name;
211   lua_State *L1 = getthread(L, &arg);
212   lua_Debug ar;
213   int level = (int)luaL_checkinteger(L, arg + 1);
214   int nvar = (int)luaL_checkinteger(L, arg + 2);
215   if (!lua_getstack(L1, level, &ar))  /* out of range? */
216     return luaL_argerror(L, arg+1, "level out of range");
217   luaL_checkany(L, arg+3);
218   lua_settop(L, arg+3);
219   lua_xmove(L, L1, 1);
220   name = lua_setlocal(L1, &ar, nvar);
221   if (name == NULL)
222     lua_pop(L1, 1);  /* pop value (if not popped by 'lua_setlocal') */
223   lua_pushstring(L, name);
224   return 1;
225 }
226 
227 
228 /*
229 ** get (if 'get' is true) or set an upvalue from a closure
230 */
auxupvalue(lua_State * L,int get)231 static int auxupvalue (lua_State *L, int get) {
232   const char *name;
233   int n = (int)luaL_checkinteger(L, 2);  /* upvalue index */
234   luaL_checktype(L, 1, LUA_TFUNCTION);  /* closure */
235   name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
236   if (name == NULL) return 0;
237   lua_pushstring(L, name);
238   lua_insert(L, -(get+1));  /* no-op if get is false */
239   return get + 1;
240 }
241 
242 
db_getupvalue(lua_State * L)243 static int db_getupvalue (lua_State *L) {
244   return auxupvalue(L, 1);
245 }
246 
247 
db_setupvalue(lua_State * L)248 static int db_setupvalue (lua_State *L) {
249   luaL_checkany(L, 3);
250   return auxupvalue(L, 0);
251 }
252 
253 
254 /*
255 ** Check whether a given upvalue from a given closure exists and
256 ** returns its index
257 */
checkupval(lua_State * L,int argf,int argnup)258 static int checkupval (lua_State *L, int argf, int argnup) {
259   int nup = (int)luaL_checkinteger(L, argnup);  /* upvalue index */
260   luaL_checktype(L, argf, LUA_TFUNCTION);  /* closure */
261   luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
262                    "invalid upvalue index");
263   return nup;
264 }
265 
266 
db_upvalueid(lua_State * L)267 static int db_upvalueid (lua_State *L) {
268   int n = checkupval(L, 1, 2);
269   lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
270   return 1;
271 }
272 
273 
db_upvaluejoin(lua_State * L)274 static int db_upvaluejoin (lua_State *L) {
275   int n1 = checkupval(L, 1, 2);
276   int n2 = checkupval(L, 3, 4);
277   luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
278   luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
279   lua_upvaluejoin(L, 1, n1, 3, n2);
280   return 0;
281 }
282 
283 
284 /*
285 ** Call hook function registered at hook table for the current
286 ** thread (if there is one)
287 */
hookf(lua_State * L,lua_Debug * ar)288 static void hookf (lua_State *L, lua_Debug *ar) {
289   static const char *const hooknames[] =
290     {"call", "return", "line", "count", "tail call"};
291   lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
292   lua_pushthread(L);
293   if (lua_rawget(L, -2) == LUA_TFUNCTION) {  /* is there a hook function? */
294     lua_pushstring(L, hooknames[(int)ar->event]);  /* push event name */
295     if (ar->currentline >= 0)
296       lua_pushinteger(L, ar->currentline);  /* push current line */
297     else lua_pushnil(L);
298     lua_assert(lua_getinfo(L, "lS", ar));
299     lua_call(L, 2, 0);  /* call hook function */
300   }
301 }
302 
303 
304 /*
305 ** Convert a string mask (for 'sethook') into a bit mask
306 */
makemask(const char * smask,int count)307 static int makemask (const char *smask, int count) {
308   int mask = 0;
309   if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
310   if (strchr(smask, 'r')) mask |= LUA_MASKRET;
311   if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
312   if (count > 0) mask |= LUA_MASKCOUNT;
313   return mask;
314 }
315 
316 
317 /*
318 ** Convert a bit mask (for 'gethook') into a string mask
319 */
unmakemask(int mask,char * smask)320 static char *unmakemask (int mask, char *smask) {
321   int i = 0;
322   if (mask & LUA_MASKCALL) smask[i++] = 'c';
323   if (mask & LUA_MASKRET) smask[i++] = 'r';
324   if (mask & LUA_MASKLINE) smask[i++] = 'l';
325   smask[i] = '\0';
326   return smask;
327 }
328 
329 
db_sethook(lua_State * L)330 static int db_sethook (lua_State *L) {
331   int arg, mask, count;
332   lua_Hook func;
333   lua_State *L1 = getthread(L, &arg);
334   if (lua_isnoneornil(L, arg+1)) {  /* no hook? */
335     lua_settop(L, arg+1);
336     func = NULL; mask = 0; count = 0;  /* turn off hooks */
337   }
338   else {
339     const char *smask = luaL_checkstring(L, arg+2);
340     luaL_checktype(L, arg+1, LUA_TFUNCTION);
341     count = (int)luaL_optinteger(L, arg + 3, 0);
342     func = hookf; mask = makemask(smask, count);
343   }
344   if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
345     lua_createtable(L, 0, 2);  /* create a hook table */
346     lua_pushvalue(L, -1);
347     lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY);  /* set it in position */
348     lua_pushstring(L, "k");
349     lua_setfield(L, -2, "__mode");  /** hooktable.__mode = "k" */
350     lua_pushvalue(L, -1);
351     lua_setmetatable(L, -2);  /* setmetatable(hooktable) = hooktable */
352   }
353   lua_pushthread(L1); lua_xmove(L1, L, 1);  /* key (thread) */
354   lua_pushvalue(L, arg + 1);  /* value (hook function) */
355   lua_rawset(L, -3);  /* hooktable[L1] = new Lua hook */
356   lua_sethook(L1, func, mask, count);
357   return 0;
358 }
359 
360 
db_gethook(lua_State * L)361 static int db_gethook (lua_State *L) {
362   int arg;
363   lua_State *L1 = getthread(L, &arg);
364   char buff[5];
365   int mask = lua_gethookmask(L1);
366   lua_Hook hook = lua_gethook(L1);
367   if (hook == NULL)  /* no hook? */
368     lua_pushnil(L);
369   else if (hook != hookf)  /* external hook? */
370     lua_pushliteral(L, "external hook");
371   else {  /* hook table must exist */
372     lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
373     lua_pushthread(L1); lua_xmove(L1, L, 1);
374     lua_rawget(L, -2);   /* 1st result = hooktable[L1] */
375     lua_remove(L, -2);  /* remove hook table */
376   }
377   lua_pushstring(L, unmakemask(mask, buff));  /* 2nd result = mask */
378   lua_pushinteger(L, lua_gethookcount(L1));  /* 3rd result = count */
379   return 3;
380 }
381 
382 
db_debug(lua_State * L)383 static int db_debug (lua_State *L) {
384   for (;;) {
385     char buffer[250];
386     lua_writestringerror("%s", "lua_debug> ");
387     if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
388         strcmp(buffer, "cont\n") == 0)
389       return 0;
390     if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
391         lua_pcall(L, 0, 0, 0))
392       lua_writestringerror("%s\n", lua_tostring(L, -1));
393     lua_settop(L, 0);  /* remove eventual returns */
394   }
395 }
396 
397 
db_traceback(lua_State * L)398 static int db_traceback (lua_State *L) {
399   int arg;
400   lua_State *L1 = getthread(L, &arg);
401   const char *msg = lua_tostring(L, arg + 1);
402   if (msg == NULL && !lua_isnoneornil(L, arg + 1))  /* non-string 'msg'? */
403     lua_pushvalue(L, arg + 1);  /* return it untouched */
404   else {
405     int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
406     luaL_traceback(L, L1, msg, level);
407   }
408   return 1;
409 }
410 
411 
412 static const luaL_Reg dblib[] = {
413   {"debug", db_debug},
414   {"getuservalue", db_getuservalue},
415   {"gethook", db_gethook},
416   {"getinfo", db_getinfo},
417   {"getlocal", db_getlocal},
418   {"getregistry", db_getregistry},
419   {"getmetatable", db_getmetatable},
420   {"getupvalue", db_getupvalue},
421   {"upvaluejoin", db_upvaluejoin},
422   {"upvalueid", db_upvalueid},
423   {"setuservalue", db_setuservalue},
424   {"sethook", db_sethook},
425   {"setlocal", db_setlocal},
426   {"setmetatable", db_setmetatable},
427   {"setupvalue", db_setupvalue},
428   {"traceback", db_traceback},
429   {NULL, NULL}
430 };
431 
432 
luaopen_debug(lua_State * L)433 LUAMOD_API int luaopen_debug (lua_State *L) {
434   luaL_newlib(L, dblib);
435   return 1;
436 }
437 
438