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*/
27static 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*/
35static 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
41static int db_getregistry (lua_State *L) {
42 lua_pushvalue(L, LUA_REGISTRYINDEX);
43 return 1;
44}
45
46
47static 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
56static 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
65static 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
77static 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*/
94static 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*/
111static void settabss (lua_State *L, const char *k, const char *v) {
112 lua_pushstring(L, v);
113 lua_setfield(L, -2, k);
114}
115
116static void settabsi (lua_State *L, const char *k, int v) {
117 lua_pushinteger(L, v);
118 lua_setfield(L, -2, k);
119}
120
121static 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*/
134static 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*/
149static 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
202static 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
233static 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*/
257static 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
269static int db_getupvalue (lua_State *L) {
270 return auxupvalue(L, 1);
271}
272
273
274static 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*/
284static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
285 void *id;
286 int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
287 luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
288 id = lua_upvalueid(L, argf, nup);
289 if (pnup) {
290 luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
291 *pnup = nup;
292 }
293 return id;
294}
295
296
297static int db_upvalueid (lua_State *L) {
298 void *id = checkupval(L, 1, 2, NULL);
299 if (id != NULL)
300 lua_pushlightuserdata(L, id);
301 else
302 luaL_pushfail(L);
303 return 1;
304}
305
306
307static int db_upvaluejoin (lua_State *L) {
308 int n1, n2;
309 checkupval(L, 1, 2, &n1);
310 checkupval(L, 3, 4, &n2);
311 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
312 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
313 lua_upvaluejoin(L, 1, n1, 3, n2);
314 return 0;
315}
316
317
318/*
319** Call hook function registered at hook table for the current
320** thread (if there is one)
321*/
322static void hookf (lua_State *L, lua_Debug *ar) {
323 static const char *const hooknames[] =
324 {"call", "return", "line", "count", "tail call"};
325 lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
326 lua_pushthread(L);
327 if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
328 lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
329 if (ar->currentline >= 0)
330 lua_pushinteger(L, ar->currentline); /* push current line */
331 else lua_pushnil(L);
332 lua_assert(lua_getinfo(L, "lS", ar));
333 lua_call(L, 2, 0); /* call hook function */
334 }
335}
336
337
338/*
339** Convert a string mask (for 'sethook') into a bit mask
340*/
341static int makemask (const char *smask, int count) {
342 int mask = 0;
343 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
344 if (strchr(smask, 'r')) mask |= LUA_MASKRET;
345 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
346 if (count > 0) mask |= LUA_MASKCOUNT;
347 return mask;
348}
349
350
351/*
352** Convert a bit mask (for 'gethook') into a string mask
353*/
354static char *unmakemask (int mask, char *smask) {
355 int i = 0;
356 if (mask & LUA_MASKCALL) smask[i++] = 'c';
357 if (mask & LUA_MASKRET) smask[i++] = 'r';
358 if (mask & LUA_MASKLINE) smask[i++] = 'l';
359 smask[i] = '\0';
360 return smask;
361}
362
363
364static int db_sethook (lua_State *L) {
365 int arg, mask, count;
366 lua_Hook func;
367 lua_State *L1 = getthread(L, &arg);
368 if (lua_isnoneornil(L, arg+1)) { /* no hook? */
369 lua_settop(L, arg+1);
370 func = NULL; mask = 0; count = 0; /* turn off hooks */
371 }
372 else {
373 const char *smask = luaL_checkstring(L, arg+2);
374 luaL_checktype(L, arg+1, LUA_TFUNCTION);
375 count = (int)luaL_optinteger(L, arg + 3, 0);
376 func = hookf; mask = makemask(smask, count);
377 }
378 if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
379 /* table just created; initialize it */
380 lua_pushstring(L, "k");
381 lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
382 lua_pushvalue(L, -1);
383 lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
384 }
385 checkstack(L, L1, 1);
386 lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
387 lua_pushvalue(L, arg + 1); /* value (hook function) */
388 lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
389 lua_sethook(L1, func, mask, count);
390 return 0;
391}
392
393
394static int db_gethook (lua_State *L) {
395 int arg;
396 lua_State *L1 = getthread(L, &arg);
397 char buff[5];
398 int mask = lua_gethookmask(L1);
399 lua_Hook hook = lua_gethook(L1);
400 if (hook == NULL) { /* no hook? */
401 luaL_pushfail(L);
402 return 1;
403 }
404 else if (hook != hookf) /* external hook? */
405 lua_pushliteral(L, "external hook");
406 else { /* hook table must exist */
407 lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
408 checkstack(L, L1, 1);
409 lua_pushthread(L1); lua_xmove(L1, L, 1);
410 lua_rawget(L, -2); /* 1st result = hooktable[L1] */
411 lua_remove(L, -2); /* remove hook table */
412 }
413 lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
414 lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
415 return 3;
416}
417
418
419static int db_debug (lua_State *L) {
420 for (;;) {
421 char buffer[250];
422 lua_writestringerror("%s", "lua_debug> ");
423 if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
424 strcmp(buffer, "cont\n") == 0)
425 return 0;
426 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
427 lua_pcall(L, 0, 0, 0))
428 lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
429 lua_settop(L, 0); /* remove eventual returns */
430 }
431}
432
433
434static int db_traceback (lua_State *L) {
435 int arg;
436 lua_State *L1 = getthread(L, &arg);
437 const char *msg = lua_tostring(L, arg + 1);
438 if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
439 lua_pushvalue(L, arg + 1); /* return it untouched */
440 else {
441 int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
442 luaL_traceback(L, L1, msg, level);
443 }
444 return 1;
445}
446
447
448static int db_setcstacklimit (lua_State *L) {
449 int limit = (int)luaL_checkinteger(L, 1);
450 int res = lua_setcstacklimit(L, limit);
451 lua_pushinteger(L, res);
452 return 1;
453}
454
455
456static const luaL_Reg dblib[] = {
457 {"debug", db_debug},
458 {"getuservalue", db_getuservalue},
459 {"gethook", db_gethook},
460 {"getinfo", db_getinfo},
461 {"getlocal", db_getlocal},
462 {"getregistry", db_getregistry},
463 {"getmetatable", db_getmetatable},
464 {"getupvalue", db_getupvalue},
465 {"upvaluejoin", db_upvaluejoin},
466 {"upvalueid", db_upvalueid},
467 {"setuservalue", db_setuservalue},
468 {"sethook", db_sethook},
469 {"setlocal", db_setlocal},
470 {"setmetatable", db_setmetatable},
471 {"setupvalue", db_setupvalue},
472 {"traceback", db_traceback},
473 {"setcstacklimit", db_setcstacklimit},
474 {NULL, NULL}
475};
476
477
478LUAMOD_API int luaopen_debug (lua_State *L) {
479 luaL_newlib(L, dblib);
480 return 1;
481}
482
483