source: trunk/src/lua/ldblib.c @ 2542

Last change on this file since 2542 was 2542, checked in by sam, 10 years ago

lua: compilation fixes for non-quite-POSIX platforms.

File size: 9.9 KB
Line 
1/*
2** $Id: ldblib.c,v 1.132 2012/01/19 20:14:44 roberto Exp $
3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h
5*/
6
7#if defined HAVE_CONFIG_H // LOL BEGIN
8#   include "config.h"
9#endif // LOL END
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#define ldblib_c
16#define LUA_LIB
17
18#include "lua.h"
19
20#include "lauxlib.h"
21#include "lualib.h"
22
23
24#define HOOKKEY         "_HKEY"
25
26
27
28static int db_getregistry (lua_State *L) {
29  lua_pushvalue(L, LUA_REGISTRYINDEX);
30  return 1;
31}
32
33
34static int db_getmetatable (lua_State *L) {
35  luaL_checkany(L, 1);
36  if (!lua_getmetatable(L, 1)) {
37    lua_pushnil(L);  /* no metatable */
38  }
39  return 1;
40}
41
42
43static int db_setmetatable (lua_State *L) {
44  int t = lua_type(L, 2);
45  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
46                    "nil or table expected");
47  lua_settop(L, 2);
48  lua_setmetatable(L, 1);
49  return 1;  /* return 1st argument */
50}
51
52
53static int db_getuservalue (lua_State *L) {
54  if (lua_type(L, 1) != LUA_TUSERDATA)
55    lua_pushnil(L);
56  else
57    lua_getuservalue(L, 1);
58  return 1;
59}
60
61
62static int db_setuservalue (lua_State *L) {
63  if (lua_type(L, 1) == LUA_TLIGHTUSERDATA)
64    luaL_argerror(L, 1, "full userdata expected, got light userdata");
65  luaL_checktype(L, 1, LUA_TUSERDATA);
66  if (!lua_isnoneornil(L, 2))
67    luaL_checktype(L, 2, LUA_TTABLE);
68  lua_settop(L, 2);
69  lua_setuservalue(L, 1);
70  return 1;
71}
72
73
74static void settabss (lua_State *L, const char *i, const char *v) {
75  lua_pushstring(L, v);
76  lua_setfield(L, -2, i);
77}
78
79
80static void settabsi (lua_State *L, const char *i, int v) {
81  lua_pushinteger(L, v);
82  lua_setfield(L, -2, i);
83}
84
85
86static void settabsb (lua_State *L, const char *i, int v) {
87  lua_pushboolean(L, v);
88  lua_setfield(L, -2, i);
89}
90
91
92static lua_State *getthread (lua_State *L, int *arg) {
93  if (lua_isthread(L, 1)) {
94    *arg = 1;
95    return lua_tothread(L, 1);
96  }
97  else {
98    *arg = 0;
99    return L;
100  }
101}
102
103
104static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
105  if (L == L1) {
106    lua_pushvalue(L, -2);
107    lua_remove(L, -3);
108  }
109  else
110    lua_xmove(L1, L, 1);
111  lua_setfield(L, -2, fname);
112}
113
114
115static int db_getinfo (lua_State *L) {
116  lua_Debug ar;
117  int arg;
118  lua_State *L1 = getthread(L, &arg);
119  const char *options = luaL_optstring(L, arg+2, "flnStu");
120  if (lua_isnumber(L, arg+1)) {
121    if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
122      lua_pushnil(L);  /* level out of range */
123      return 1;
124    }
125  }
126  else if (lua_isfunction(L, arg+1)) {
127    lua_pushfstring(L, ">%s", options);
128    options = lua_tostring(L, -1);
129    lua_pushvalue(L, arg+1);
130    lua_xmove(L, L1, 1);
131  }
132  else
133    return luaL_argerror(L, arg+1, "function or level expected");
134  if (!lua_getinfo(L1, options, &ar))
135    return luaL_argerror(L, arg+2, "invalid option");
136  lua_createtable(L, 0, 2);
137  if (strchr(options, 'S')) {
138    settabss(L, "source", ar.source);
139    settabss(L, "short_src", ar.short_src);
140    settabsi(L, "linedefined", ar.linedefined);
141    settabsi(L, "lastlinedefined", ar.lastlinedefined);
142    settabss(L, "what", ar.what);
143  }
144  if (strchr(options, 'l'))
145    settabsi(L, "currentline", ar.currentline);
146  if (strchr(options, 'u')) {
147    settabsi(L, "nups", ar.nups);
148    settabsi(L, "nparams", ar.nparams);
149    settabsb(L, "isvararg", ar.isvararg);
150  }
151  if (strchr(options, 'n')) {
152    settabss(L, "name", ar.name);
153    settabss(L, "namewhat", ar.namewhat);
154  }
155  if (strchr(options, 't'))
156    settabsb(L, "istailcall", ar.istailcall);
157  if (strchr(options, 'L'))
158    treatstackoption(L, L1, "activelines");
159  if (strchr(options, 'f'))
160    treatstackoption(L, L1, "func");
161  return 1;  /* return table */
162}
163
164
165static int db_getlocal (lua_State *L) {
166  int arg;
167  lua_State *L1 = getthread(L, &arg);
168  lua_Debug ar;
169  const char *name;
170  int nvar = luaL_checkint(L, arg+2);  /* local-variable index */
171  if (lua_isfunction(L, arg + 1)) {  /* function argument? */
172    lua_pushvalue(L, arg + 1);  /* push function */
173    lua_pushstring(L, lua_getlocal(L, NULL, nvar));  /* push local name */
174    return 1;
175  }
176  else {  /* stack-level argument */
177    if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */
178      return luaL_argerror(L, arg+1, "level out of range");
179    name = lua_getlocal(L1, &ar, nvar);
180    if (name) {
181      lua_xmove(L1, L, 1);  /* push local value */
182      lua_pushstring(L, name);  /* push name */
183      lua_pushvalue(L, -2);  /* re-order */
184      return 2;
185    }
186    else {
187      lua_pushnil(L);  /* no name (nor value) */
188      return 1;
189    }
190  }
191}
192
193
194static int db_setlocal (lua_State *L) {
195  int arg;
196  lua_State *L1 = getthread(L, &arg);
197  lua_Debug ar;
198  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */
199    return luaL_argerror(L, arg+1, "level out of range");
200  luaL_checkany(L, arg+3);
201  lua_settop(L, arg+3);
202  lua_xmove(L, L1, 1);
203  lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
204  return 1;
205}
206
207
208static int auxupvalue (lua_State *L, int get) {
209  const char *name;
210  int n = luaL_checkint(L, 2);
211  luaL_checktype(L, 1, LUA_TFUNCTION);
212  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
213  if (name == NULL) return 0;
214  lua_pushstring(L, name);
215  lua_insert(L, -(get+1));
216  return get + 1;
217}
218
219
220static int db_getupvalue (lua_State *L) {
221  return auxupvalue(L, 1);
222}
223
224
225static int db_setupvalue (lua_State *L) {
226  luaL_checkany(L, 3);
227  return auxupvalue(L, 0);
228}
229
230
231static int checkupval (lua_State *L, int argf, int argnup) {
232  lua_Debug ar;
233  int nup = luaL_checkint(L, argnup);
234  luaL_checktype(L, argf, LUA_TFUNCTION);
235  lua_pushvalue(L, argf);
236  lua_getinfo(L, ">u", &ar);
237  luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
238  return nup;
239}
240
241
242static int db_upvalueid (lua_State *L) {
243  int n = checkupval(L, 1, 2);
244  lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
245  return 1;
246}
247
248
249static int db_upvaluejoin (lua_State *L) {
250  int n1 = checkupval(L, 1, 2);
251  int n2 = checkupval(L, 3, 4);
252  luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
253  luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
254  lua_upvaluejoin(L, 1, n1, 3, n2);
255  return 0;
256}
257
258
259#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
260
261
262static void hookf (lua_State *L, lua_Debug *ar) {
263  static const char *const hooknames[] =
264    {"call", "return", "line", "count", "tail call"};
265  gethooktable(L);
266  lua_pushthread(L);
267  lua_rawget(L, -2);
268  if (lua_isfunction(L, -1)) {
269    lua_pushstring(L, hooknames[(int)ar->event]);
270    if (ar->currentline >= 0)
271      lua_pushinteger(L, ar->currentline);
272    else lua_pushnil(L);
273    lua_assert(lua_getinfo(L, "lS", ar));
274    lua_call(L, 2, 0);
275  }
276}
277
278
279static int makemask (const char *smask, int count) {
280  int mask = 0;
281  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
282  if (strchr(smask, 'r')) mask |= LUA_MASKRET;
283  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
284  if (count > 0) mask |= LUA_MASKCOUNT;
285  return mask;
286}
287
288
289static char *unmakemask (int mask, char *smask) {
290  int i = 0;
291  if (mask & LUA_MASKCALL) smask[i++] = 'c';
292  if (mask & LUA_MASKRET) smask[i++] = 'r';
293  if (mask & LUA_MASKLINE) smask[i++] = 'l';
294  smask[i] = '\0';
295  return smask;
296}
297
298
299static int db_sethook (lua_State *L) {
300  int arg, mask, count;
301  lua_Hook func;
302  lua_State *L1 = getthread(L, &arg);
303  if (lua_isnoneornil(L, arg+1)) {
304    lua_settop(L, arg+1);
305    func = NULL; mask = 0; count = 0;  /* turn off hooks */
306  }
307  else {
308    const char *smask = luaL_checkstring(L, arg+2);
309    luaL_checktype(L, arg+1, LUA_TFUNCTION);
310    count = luaL_optint(L, arg+3, 0);
311    func = hookf; mask = makemask(smask, count);
312  }
313  if (gethooktable(L) == 0) {  /* creating hook table? */
314    lua_pushstring(L, "k");
315    lua_setfield(L, -2, "__mode");  /** hooktable.__mode = "k" */
316    lua_pushvalue(L, -1);
317    lua_setmetatable(L, -2);  /* setmetatable(hooktable) = hooktable */
318  }
319  lua_pushthread(L1); lua_xmove(L1, L, 1);
320  lua_pushvalue(L, arg+1);
321  lua_rawset(L, -3);  /* set new hook */
322  lua_sethook(L1, func, mask, count);  /* set hooks */
323  return 0;
324}
325
326
327static int db_gethook (lua_State *L) {
328  int arg;
329  lua_State *L1 = getthread(L, &arg);
330  char buff[5];
331  int mask = lua_gethookmask(L1);
332  lua_Hook hook = lua_gethook(L1);
333  if (hook != NULL && hook != hookf)  /* external hook? */
334    lua_pushliteral(L, "external hook");
335  else {
336    gethooktable(L);
337    lua_pushthread(L1); lua_xmove(L1, L, 1);
338    lua_rawget(L, -2);   /* get hook */
339    lua_remove(L, -2);  /* remove hook table */
340  }
341  lua_pushstring(L, unmakemask(mask, buff));
342  lua_pushinteger(L, lua_gethookcount(L1));
343  return 3;
344}
345
346
347static int db_debug (lua_State *L) {
348  for (;;) {
349    char buffer[250];
350    luai_writestringerror("%s", "lua_debug> ");
351    if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
352        strcmp(buffer, "cont\n") == 0)
353      return 0;
354    if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
355        lua_pcall(L, 0, 0, 0))
356      luai_writestringerror("%s\n", lua_tostring(L, -1));
357    lua_settop(L, 0);  /* remove eventual returns */
358  }
359}
360
361
362static int db_traceback (lua_State *L) {
363  int arg;
364  lua_State *L1 = getthread(L, &arg);
365  const char *msg = lua_tostring(L, arg + 1);
366  if (msg == NULL && !lua_isnoneornil(L, arg + 1))  /* non-string 'msg'? */
367    lua_pushvalue(L, arg + 1);  /* return it untouched */
368  else {
369    int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
370    luaL_traceback(L, L1, msg, level);
371  }
372  return 1;
373}
374
375
376static const luaL_Reg dblib[] = {
377  {"debug", db_debug},
378  {"getuservalue", db_getuservalue},
379  {"gethook", db_gethook},
380  {"getinfo", db_getinfo},
381  {"getlocal", db_getlocal},
382  {"getregistry", db_getregistry},
383  {"getmetatable", db_getmetatable},
384  {"getupvalue", db_getupvalue},
385  {"upvaluejoin", db_upvaluejoin},
386  {"upvalueid", db_upvalueid},
387  {"setuservalue", db_setuservalue},
388  {"sethook", db_sethook},
389  {"setlocal", db_setlocal},
390  {"setmetatable", db_setmetatable},
391  {"setupvalue", db_setupvalue},
392  {"traceback", db_traceback},
393  {NULL, NULL}
394};
395
396
397LUAMOD_API int luaopen_debug (lua_State *L) {
398  luaL_newlib(L, dblib);
399  return 1;
400}
401
Note: See TracBrowser for help on using the repository browser.