ldblib.c 13 KB

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