ldblib.c 12 KB

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