ldblib.c 13 KB

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