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