ldblib.c 13 KB

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