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