2
0

ldblib.c 12 KB

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