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