ldblib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. ** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp $
  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, "flnStu");
  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, 't'))
  154. settabsb(L, "istailcall", ar.istailcall);
  155. if (strchr(options, 'L'))
  156. treatstackoption(L, L1, "activelines");
  157. if (strchr(options, 'f'))
  158. treatstackoption(L, L1, "func");
  159. return 1; /* return table */
  160. }
  161. static int db_getlocal (lua_State *L) {
  162. int arg;
  163. lua_State *L1 = getthread(L, &arg);
  164. lua_Debug ar;
  165. const char *name;
  166. int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
  167. if (lua_isfunction(L, arg + 1)) { /* function argument? */
  168. lua_pushvalue(L, arg + 1); /* push function */
  169. lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
  170. return 1; /* return only name (there is no value) */
  171. }
  172. else { /* stack-level argument */
  173. int level = (int)luaL_checkinteger(L, arg + 1);
  174. if (!lua_getstack(L1, level, &ar)) /* out of range? */
  175. return luaL_argerror(L, arg+1, "level out of range");
  176. checkstack(L, L1, 1);
  177. name = lua_getlocal(L1, &ar, nvar);
  178. if (name) {
  179. lua_xmove(L1, L, 1); /* move local value */
  180. lua_pushstring(L, name); /* push name */
  181. lua_rotate(L, -2, 1); /* re-order */
  182. return 2;
  183. }
  184. else {
  185. lua_pushnil(L); /* no name (nor value) */
  186. return 1;
  187. }
  188. }
  189. }
  190. static int db_setlocal (lua_State *L) {
  191. int arg;
  192. const char *name;
  193. lua_State *L1 = getthread(L, &arg);
  194. lua_Debug ar;
  195. int level = (int)luaL_checkinteger(L, arg + 1);
  196. int nvar = (int)luaL_checkinteger(L, arg + 2);
  197. if (!lua_getstack(L1, level, &ar)) /* out of range? */
  198. return luaL_argerror(L, arg+1, "level out of range");
  199. luaL_checkany(L, arg+3);
  200. lua_settop(L, arg+3);
  201. checkstack(L, L1, 1);
  202. lua_xmove(L, L1, 1);
  203. name = lua_setlocal(L1, &ar, nvar);
  204. if (name == NULL)
  205. lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
  206. lua_pushstring(L, name);
  207. return 1;
  208. }
  209. /*
  210. ** get (if 'get' is true) or set an upvalue from a closure
  211. */
  212. static int auxupvalue (lua_State *L, int get) {
  213. const char *name;
  214. int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
  215. luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
  216. name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
  217. if (name == NULL) return 0;
  218. lua_pushstring(L, name);
  219. lua_insert(L, -(get+1)); /* no-op if get is false */
  220. return get + 1;
  221. }
  222. static int db_getupvalue (lua_State *L) {
  223. return auxupvalue(L, 1);
  224. }
  225. static int db_setupvalue (lua_State *L) {
  226. luaL_checkany(L, 3);
  227. return auxupvalue(L, 0);
  228. }
  229. /*
  230. ** Check whether a given upvalue from a given closure exists and
  231. ** returns its index
  232. */
  233. static int checkupval (lua_State *L, int argf, int argnup) {
  234. int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
  235. luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
  236. luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
  237. "invalid upvalue index");
  238. return nup;
  239. }
  240. static int db_upvalueid (lua_State *L) {
  241. int n = checkupval(L, 1, 2);
  242. lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
  243. return 1;
  244. }
  245. static int db_upvaluejoin (lua_State *L) {
  246. int n1 = checkupval(L, 1, 2);
  247. int n2 = checkupval(L, 3, 4);
  248. luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
  249. luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
  250. lua_upvaluejoin(L, 1, n1, 3, n2);
  251. return 0;
  252. }
  253. /*
  254. ** Call hook function registered at hook table for the current
  255. ** thread (if there is one)
  256. */
  257. static void hookf (lua_State *L, lua_Debug *ar) {
  258. static const char *const hooknames[] =
  259. {"call", "return", "line", "count", "tail call"};
  260. lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
  261. lua_pushthread(L);
  262. if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
  263. lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
  264. if (ar->currentline >= 0)
  265. lua_pushinteger(L, ar->currentline); /* push current line */
  266. else lua_pushnil(L);
  267. lua_assert(lua_getinfo(L, "lS", ar));
  268. lua_call(L, 2, 0); /* call hook function */
  269. }
  270. }
  271. /*
  272. ** Convert a string mask (for 'sethook') into a bit mask
  273. */
  274. static int makemask (const char *smask, int count) {
  275. int mask = 0;
  276. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  277. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  278. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  279. if (count > 0) mask |= LUA_MASKCOUNT;
  280. return mask;
  281. }
  282. /*
  283. ** Convert a bit mask (for 'gethook') into a string mask
  284. */
  285. static char *unmakemask (int mask, char *smask) {
  286. int i = 0;
  287. if (mask & LUA_MASKCALL) smask[i++] = 'c';
  288. if (mask & LUA_MASKRET) smask[i++] = 'r';
  289. if (mask & LUA_MASKLINE) smask[i++] = 'l';
  290. smask[i] = '\0';
  291. return smask;
  292. }
  293. static int db_sethook (lua_State *L) {
  294. int arg, mask, count;
  295. lua_Hook func;
  296. lua_State *L1 = getthread(L, &arg);
  297. if (lua_isnoneornil(L, arg+1)) { /* no hook? */
  298. lua_settop(L, arg+1);
  299. func = NULL; mask = 0; count = 0; /* turn off hooks */
  300. }
  301. else {
  302. const char *smask = luaL_checkstring(L, arg+2);
  303. luaL_checktype(L, arg+1, LUA_TFUNCTION);
  304. count = (int)luaL_optinteger(L, arg + 3, 0);
  305. func = hookf; mask = makemask(smask, count);
  306. }
  307. if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
  308. lua_createtable(L, 0, 2); /* create a hook table */
  309. lua_pushvalue(L, -1);
  310. lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
  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. checkstack(L, L1, 1);
  317. lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
  318. lua_pushvalue(L, arg + 1); /* value (hook function) */
  319. lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
  320. lua_sethook(L1, func, mask, count);
  321. return 0;
  322. }
  323. static int db_gethook (lua_State *L) {
  324. int arg;
  325. lua_State *L1 = getthread(L, &arg);
  326. char buff[5];
  327. int mask = lua_gethookmask(L1);
  328. lua_Hook hook = lua_gethook(L1);
  329. if (hook == NULL) /* no hook? */
  330. lua_pushnil(L);
  331. else if (hook != hookf) /* external hook? */
  332. lua_pushliteral(L, "external hook");
  333. else { /* hook table must exist */
  334. lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
  335. checkstack(L, L1, 1);
  336. lua_pushthread(L1); lua_xmove(L1, L, 1);
  337. lua_rawget(L, -2); /* 1st result = hooktable[L1] */
  338. lua_remove(L, -2); /* remove hook table */
  339. }
  340. lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */
  341. lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
  342. return 3;
  343. }
  344. static int db_debug (lua_State *L) {
  345. for (;;) {
  346. char buffer[250];
  347. lua_writestringerror("%s", "lua_debug> ");
  348. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  349. strcmp(buffer, "cont\n") == 0)
  350. return 0;
  351. if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
  352. lua_pcall(L, 0, 0, 0))
  353. lua_writestringerror("%s\n", lua_tostring(L, -1));
  354. lua_settop(L, 0); /* remove eventual returns */
  355. }
  356. }
  357. static int db_traceback (lua_State *L) {
  358. int arg;
  359. lua_State *L1 = getthread(L, &arg);
  360. const char *msg = lua_tostring(L, arg + 1);
  361. if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
  362. lua_pushvalue(L, arg + 1); /* return it untouched */
  363. else {
  364. int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
  365. luaL_traceback(L, L1, msg, level);
  366. }
  367. return 1;
  368. }
  369. static const luaL_Reg dblib[] = {
  370. {"debug", db_debug},
  371. {"getuservalue", db_getuservalue},
  372. {"gethook", db_gethook},
  373. {"getinfo", db_getinfo},
  374. {"getlocal", db_getlocal},
  375. {"getregistry", db_getregistry},
  376. {"getmetatable", db_getmetatable},
  377. {"getupvalue", db_getupvalue},
  378. {"upvaluejoin", db_upvaluejoin},
  379. {"upvalueid", db_upvalueid},
  380. {"setuservalue", db_setuservalue},
  381. {"sethook", db_sethook},
  382. {"setlocal", db_setlocal},
  383. {"setmetatable", db_setmetatable},
  384. {"setupvalue", db_setupvalue},
  385. {"traceback", db_traceback},
  386. {NULL, NULL}
  387. };
  388. LUAMOD_API int luaopen_debug (lua_State *L) {
  389. luaL_newlib(L, dblib);
  390. return 1;
  391. }