ldblib.c 13 KB

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