db.lua 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. -- $Id: testes/db.lua $
  2. -- See Copyright Notice in file all.lua
  3. -- testing debug library
  4. local debug = require "debug"
  5. local function dostring(s) return assert(load(s))() end
  6. print"testing debug library and debug information"
  7. do
  8. local a=1
  9. end
  10. assert(not debug.gethook())
  11. local testline = 19 -- line where 'test' is defined
  12. local function test (s, l, p) -- this must be line 19
  13. collectgarbage() -- avoid gc during trace
  14. local function f (event, line)
  15. assert(event == 'line')
  16. local l = table.remove(l, 1)
  17. if p then print(l, line) end
  18. assert(l == line, "wrong trace!!")
  19. end
  20. debug.sethook(f,"l"); load(s)(); debug.sethook()
  21. assert(#l == 0)
  22. end
  23. do
  24. assert(not pcall(debug.getinfo, print, "X")) -- invalid option
  25. assert(not pcall(debug.getinfo, 0, ">")) -- invalid option
  26. assert(not debug.getinfo(1000)) -- out of range level
  27. assert(not debug.getinfo(-1)) -- out of range level
  28. local a = debug.getinfo(print)
  29. assert(a.what == "C" and a.short_src == "[C]")
  30. a = debug.getinfo(print, "L")
  31. assert(a.activelines == nil)
  32. local b = debug.getinfo(test, "SfL")
  33. assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and
  34. b.lastlinedefined == b.linedefined + 10 and
  35. b.func == test and not string.find(b.short_src, "%["))
  36. assert(b.activelines[b.linedefined + 1] and
  37. b.activelines[b.lastlinedefined])
  38. assert(not b.activelines[b.linedefined] and
  39. not b.activelines[b.lastlinedefined + 1])
  40. end
  41. -- bug in 5.4.4-5.4.6: activelines in vararg functions
  42. -- without debug information
  43. do
  44. local func = load(string.dump(load("print(10)"), true))
  45. local actl = debug.getinfo(func, "L").activelines
  46. assert(#actl == 0) -- no line info
  47. end
  48. -- test file and string names truncation
  49. local a = "function f () end"
  50. local function dostring (s, x) return load(s, x)() end
  51. dostring(a)
  52. assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
  53. dostring(a..string.format("; %s\n=1", string.rep('p', 400)))
  54. assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
  55. dostring(a..string.format("; %s=1", string.rep('p', 400)))
  56. assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
  57. dostring("\n"..a)
  58. assert(debug.getinfo(f).short_src == '[string "..."]')
  59. dostring(a, "")
  60. assert(debug.getinfo(f).short_src == '[string ""]')
  61. dostring(a, "@xuxu")
  62. assert(debug.getinfo(f).short_src == "xuxu")
  63. dostring(a, "@"..string.rep('p', 1000)..'t')
  64. assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$"))
  65. dostring(a, "=xuxu")
  66. assert(debug.getinfo(f).short_src == "xuxu")
  67. dostring(a, string.format("=%s", string.rep('x', 500)))
  68. assert(string.find(debug.getinfo(f).short_src, "^x*$"))
  69. dostring(a, "=")
  70. assert(debug.getinfo(f).short_src == "")
  71. _G.a = nil; _G.f = nil;
  72. _G[string.rep("p", 400)] = nil
  73. repeat
  74. local g = {x = function ()
  75. local a = debug.getinfo(2)
  76. assert(a.name == 'f' and a.namewhat == 'local')
  77. a = debug.getinfo(1)
  78. assert(a.name == 'x' and a.namewhat == 'field')
  79. return 'xixi'
  80. end}
  81. local f = function () return 1+1 and (not 1 or g.x()) end
  82. assert(f() == 'xixi')
  83. g = debug.getinfo(f)
  84. assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name)
  85. function f (x, name) -- local!
  86. name = name or 'f'
  87. local a = debug.getinfo(1)
  88. assert(a.name == name and a.namewhat == 'local')
  89. return x
  90. end
  91. -- breaks in different conditions
  92. if 3>4 then break end; f()
  93. if 3<4 then a=1 else break end; f()
  94. while 1 do local x=10; break end; f()
  95. local b = 1
  96. if 3>4 then return math.sin(1) end; f()
  97. a = 3<4; f()
  98. a = 3<4 or 1; f()
  99. repeat local x=20; if 4>3 then f() else break end; f() until 1
  100. g = {}
  101. f(g).x = f(2) and f(10)+f(9)
  102. assert(g.x == f(19))
  103. function g(x) if not x then return 3 end return (x('a', 'x')) end
  104. assert(g(f) == 'a')
  105. until 1
  106. test([[if
  107. math.sin(1)
  108. then
  109. a=1
  110. else
  111. a=2
  112. end
  113. ]], {2,3,4,7})
  114. test([[
  115. local function foo()
  116. end
  117. foo()
  118. A = 1
  119. A = 2
  120. A = 3
  121. ]], {2, 3, 2, 4, 5, 6})
  122. _G.A = nil
  123. test([[--
  124. if nil then
  125. a=1
  126. else
  127. a=2
  128. end
  129. ]], {2,5,6})
  130. test([[a=1
  131. repeat
  132. a=a+1
  133. until a==3
  134. ]], {1,3,4,3,4})
  135. test([[ do
  136. return
  137. end
  138. ]], {2})
  139. test([[local a
  140. a=1
  141. while a<=3 do
  142. a=a+1
  143. end
  144. ]], {1,2,3,4,3,4,3,4,3,5})
  145. test([[while math.sin(1) do
  146. if math.sin(1)
  147. then break
  148. end
  149. end
  150. a=1]], {1,2,3,6})
  151. test([[for i=1,3 do
  152. a=i
  153. end
  154. ]], {1,2,1,2,1,2,1,3})
  155. test([[for i,v in pairs{'a','b'} do
  156. a=tostring(i) .. v
  157. end
  158. ]], {1,2,1,2,1,3})
  159. test([[for i=1,4 do a=1 end]], {1,1,1,1})
  160. _G.a = nil
  161. do -- testing line info/trace with large gaps in source
  162. local a = {1, 2, 3, 10, 124, 125, 126, 127, 128, 129, 130,
  163. 255, 256, 257, 500, 1000}
  164. local s = [[
  165. local b = {10}
  166. a = b[1] X + Y b[1]
  167. b = 4
  168. ]]
  169. for _, i in ipairs(a) do
  170. local subs = {X = string.rep("\n", i)}
  171. for _, j in ipairs(a) do
  172. subs.Y = string.rep("\n", j)
  173. local s = string.gsub(s, "[XY]", subs)
  174. test(s, {1, 2 + i, 2 + i + j, 2 + i, 2 + i + j, 3 + i + j})
  175. end
  176. end
  177. end
  178. _G.a = nil
  179. do -- testing active lines
  180. local function checkactivelines (f, lines)
  181. local t = debug.getinfo(f, "SL")
  182. for _, l in pairs(lines) do
  183. l = l + t.linedefined
  184. assert(t.activelines[l])
  185. t.activelines[l] = undef
  186. end
  187. assert(next(t.activelines) == nil) -- no extra lines
  188. end
  189. checkactivelines(function (...) -- vararg function
  190. -- 1st line is empty
  191. -- 2nd line is empty
  192. -- 3th line is empty
  193. local a = 20
  194. -- 5th line is empty
  195. local b = 30
  196. -- 7th line is empty
  197. end, {4, 6, 8})
  198. checkactivelines(function (a)
  199. -- 1st line is empty
  200. -- 2nd line is empty
  201. local a = 20
  202. local b = 30
  203. -- 5th line is empty
  204. end, {3, 4, 6})
  205. checkactivelines(function (a, b, ...) end, {0})
  206. checkactivelines(function (a, b)
  207. end, {1})
  208. for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do
  209. checkactivelines(
  210. load(string.format("%s return 1", string.rep("\n", n))),
  211. {n + 1})
  212. end
  213. end
  214. print'+'
  215. -- invalid levels in [gs]etlocal
  216. assert(not pcall(debug.getlocal, 20, 1))
  217. assert(not pcall(debug.setlocal, -1, 1, 10))
  218. -- parameter names
  219. local function foo (a,b,...) local d, e end
  220. local co = coroutine.create(foo)
  221. assert(debug.getlocal(foo, 1) == 'a')
  222. assert(debug.getlocal(foo, 2) == 'b')
  223. assert(not debug.getlocal(foo, 3))
  224. assert(debug.getlocal(co, foo, 1) == 'a')
  225. assert(debug.getlocal(co, foo, 2) == 'b')
  226. assert(not debug.getlocal(co, foo, 3))
  227. assert(not debug.getlocal(print, 1))
  228. local function foo () return (debug.getlocal(1, -1)) end
  229. assert(not foo(10))
  230. -- varargs
  231. local function foo (a, ...)
  232. local t = table.pack(...)
  233. for i = 1, t.n do
  234. local n, v = debug.getlocal(1, -i)
  235. assert(n == "(vararg)" and v == t[i])
  236. end
  237. assert(not debug.getlocal(1, -(t.n + 1)))
  238. assert(not debug.setlocal(1, -(t.n + 1), 30))
  239. if t.n > 0 then
  240. (function (x)
  241. assert(debug.setlocal(2, -1, x) == "(vararg)")
  242. assert(debug.setlocal(2, -t.n, x) == "(vararg)")
  243. end)(430)
  244. assert(... == 430)
  245. end
  246. end
  247. foo()
  248. foo(print)
  249. foo(200, 3, 4)
  250. local a = {}
  251. for i = 1, (_soft and 100 or 1000) do a[i] = i end
  252. foo(table.unpack(a))
  253. do -- test hook presence in debug info
  254. assert(not debug.gethook())
  255. local count = 0
  256. local function f ()
  257. assert(debug.getinfo(1).namewhat == "hook")
  258. local sndline = string.match(debug.traceback(), "\n(.-)\n")
  259. assert(string.find(sndline, "hook"))
  260. count = count + 1
  261. end
  262. debug.sethook(f, "l")
  263. local a = 0
  264. _ENV.a = a
  265. a = 1
  266. debug.sethook()
  267. assert(count == 4)
  268. end
  269. _ENV.a = nil
  270. -- hook table has weak keys
  271. assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
  272. a = {}; local L = nil
  273. local glob = 1
  274. local oldglob = glob
  275. debug.sethook(function (e,l)
  276. collectgarbage() -- force GC during a hook
  277. local f, m, c = debug.gethook()
  278. assert(m == 'crl' and c == 0)
  279. if e == "line" then
  280. if glob ~= oldglob then
  281. L = l-1 -- get the first line where "glob" has changed
  282. oldglob = glob
  283. end
  284. elseif e == "call" then
  285. local f = debug.getinfo(2, "f").func
  286. a[f] = 1
  287. else assert(e == "return")
  288. end
  289. end, "crl")
  290. function f(a,b)
  291. collectgarbage()
  292. local _, x = debug.getlocal(1, 1)
  293. local _, y = debug.getlocal(1, 2)
  294. assert(x == a and y == b)
  295. assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
  296. assert(debug.setlocal(2, 4, "manga") == "B")
  297. x = debug.getinfo(2)
  298. assert(x.func == g and x.what == "Lua" and x.name == 'g' and
  299. x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
  300. glob = glob+1
  301. assert(debug.getinfo(1, "l").currentline == L+1)
  302. assert(debug.getinfo(1, "l").currentline == L+2)
  303. end
  304. function foo()
  305. glob = glob+1
  306. assert(debug.getinfo(1, "l").currentline == L+1)
  307. end; foo() -- set L
  308. -- check line counting inside strings and empty lines
  309. local _ = 'alo\
  310. alo' .. [[
  311. ]]
  312. --[[
  313. ]]
  314. assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines
  315. function g (...)
  316. local arg = {...}
  317. do local a,b,c; a=math.sin(40); end
  318. local feijao
  319. local AAAA,B = "xuxu", "abacate"
  320. f(AAAA,B)
  321. assert(AAAA == "pera" and B == "manga")
  322. do
  323. local B = 13
  324. local x,y = debug.getlocal(1,5)
  325. assert(x == 'B' and y == 13)
  326. end
  327. end
  328. g()
  329. assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print])
  330. -- tests for manipulating non-registered locals (C and Lua temporaries)
  331. local n, v = debug.getlocal(0, 1)
  332. assert(v == 0 and n == "(C temporary)")
  333. local n, v = debug.getlocal(0, 2)
  334. assert(v == 2 and n == "(C temporary)")
  335. assert(not debug.getlocal(0, 3))
  336. assert(not debug.getlocal(0, 0))
  337. function f()
  338. assert(select(2, debug.getlocal(2,3)) == 1)
  339. assert(not debug.getlocal(2,4))
  340. debug.setlocal(2, 3, 10)
  341. return 20
  342. end
  343. function g(a,b) return (a+1) + f() end
  344. assert(g(0,0) == 30)
  345. _G.f, _G.g = nil
  346. debug.sethook(nil);
  347. assert(not debug.gethook())
  348. -- minimal tests for setuservalue/getuservalue
  349. do
  350. assert(not debug.setuservalue(io.stdin, 10))
  351. local a, b = debug.getuservalue(io.stdin, 10)
  352. assert(a == nil and not b)
  353. end
  354. -- testing iteraction between multiple values x hooks
  355. do
  356. local function f(...) return 3, ... end
  357. local count = 0
  358. local a = {}
  359. for i = 1, 100 do a[i] = i end
  360. debug.sethook(function () count = count + 1 end, "", 1)
  361. local t = {table.unpack(a)}
  362. assert(#t == 100)
  363. t = {table.unpack(a, 1, 3)}
  364. assert(#t == 3)
  365. t = {f(table.unpack(a, 1, 30))}
  366. assert(#t == 31)
  367. end
  368. -- testing access to function arguments
  369. local function collectlocals (level)
  370. local tab = {}
  371. for i = 1, math.huge do
  372. local n, v = debug.getlocal(level + 1, i)
  373. if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then
  374. break -- consider only real variables
  375. end
  376. tab[n] = v
  377. end
  378. return tab
  379. end
  380. local X = nil
  381. a = {}
  382. function a:f (a, b, ...) local arg = {...}; local c = 13 end
  383. debug.sethook(function (e)
  384. assert(e == "call")
  385. dostring("XX = 12") -- test dostring inside hooks
  386. -- testing errors inside hooks
  387. assert(not pcall(load("a='joao'+1")))
  388. debug.sethook(function (e, l)
  389. assert(debug.getinfo(2, "l").currentline == l)
  390. local f,m,c = debug.gethook()
  391. assert(e == "line")
  392. assert(m == 'l' and c == 0)
  393. debug.sethook(nil) -- hook is called only once
  394. assert(not X) -- check that
  395. X = collectlocals(2)
  396. end, "l")
  397. end, "c")
  398. a:f(1,2,3,4,5)
  399. assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
  400. assert(XX == 12)
  401. assert(not debug.gethook())
  402. _G.XX = nil
  403. -- testing access to local variables in return hook (bug in 5.2)
  404. do
  405. local X = false
  406. local function foo (a, b, ...)
  407. do local x,y,z end
  408. local c, d = 10, 20
  409. return
  410. end
  411. local function aux ()
  412. if debug.getinfo(2).name == "foo" then
  413. X = true -- to signal that it found 'foo'
  414. local tab = {a = 100, b = 200, c = 10, d = 20}
  415. for n, v in pairs(collectlocals(2)) do
  416. assert(tab[n] == v)
  417. tab[n] = undef
  418. end
  419. assert(next(tab) == nil) -- 'tab' must be empty
  420. end
  421. end
  422. debug.sethook(aux, "r"); foo(100, 200); debug.sethook()
  423. assert(X)
  424. end
  425. local function eqseq (t1, t2)
  426. assert(#t1 == #t2)
  427. for i = 1, #t1 do
  428. assert(t1[i] == t2[i])
  429. end
  430. end
  431. do print("testing inspection of parameters/returned values")
  432. local on = false
  433. local inp, out
  434. local function hook (event)
  435. if not on then return end
  436. local ar = debug.getinfo(2, "ruS")
  437. local t = {}
  438. for i = ar.ftransfer, ar.ftransfer + ar.ntransfer - 1 do
  439. local _, v = debug.getlocal(2, i)
  440. t[#t + 1] = v
  441. end
  442. if event == "return" then
  443. out = t
  444. else
  445. inp = t
  446. end
  447. end
  448. debug.sethook(hook, "cr")
  449. on = true; math.sin(3); on = false
  450. eqseq(inp, {3}); eqseq(out, {math.sin(3)})
  451. on = true; select(2, 10, 20, 30, 40); on = false
  452. eqseq(inp, {2, 10, 20, 30, 40}); eqseq(out, {20, 30, 40})
  453. local function foo (a, ...) return ... end
  454. local function foo1 () on = not on; return foo(20, 10, 0) end
  455. foo1(); on = false
  456. eqseq(inp, {20}); eqseq(out, {10, 0})
  457. debug.sethook()
  458. end
  459. -- testing upvalue access
  460. local function getupvalues (f)
  461. local t = {}
  462. local i = 1
  463. while true do
  464. local name, value = debug.getupvalue(f, i)
  465. if not name then break end
  466. assert(not t[name])
  467. t[name] = value
  468. i = i + 1
  469. end
  470. return t
  471. end
  472. local a,b,c = 1,2,3
  473. local function foo1 (a) b = a; return c end
  474. local function foo2 (x) a = x; return c+b end
  475. assert(not debug.getupvalue(foo1, 3))
  476. assert(not debug.getupvalue(foo1, 0))
  477. assert(not debug.setupvalue(foo1, 3, "xuxu"))
  478. local t = getupvalues(foo1)
  479. assert(t.a == nil and t.b == 2 and t.c == 3)
  480. t = getupvalues(foo2)
  481. assert(t.a == 1 and t.b == 2 and t.c == 3)
  482. assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
  483. assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
  484. -- upvalues of C functions are allways "called" "" (the empty string)
  485. assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
  486. -- testing count hooks
  487. local a=0
  488. debug.sethook(function (e) a=a+1 end, "", 1)
  489. a=0; for i=1,1000 do end; assert(1000 < a and a < 1012)
  490. debug.sethook(function (e) a=a+1 end, "", 4)
  491. a=0; for i=1,1000 do end; assert(250 < a and a < 255)
  492. local f,m,c = debug.gethook()
  493. assert(m == "" and c == 4)
  494. debug.sethook(function (e) a=a+1 end, "", 4000)
  495. a=0; for i=1,1000 do end; assert(a == 0)
  496. do
  497. debug.sethook(print, "", 2^24 - 1) -- count upperbound
  498. local f,m,c = debug.gethook()
  499. assert(({debug.gethook()})[3] == 2^24 - 1)
  500. end
  501. debug.sethook()
  502. local g, g1
  503. -- tests for tail calls
  504. local function f (x)
  505. if x then
  506. assert(debug.getinfo(1, "S").what == "Lua")
  507. assert(debug.getinfo(1, "t").istailcall == true)
  508. local tail = debug.getinfo(2)
  509. assert(tail.func == g1 and tail.istailcall == true)
  510. assert(debug.getinfo(3, "S").what == "main")
  511. print"+"
  512. end
  513. end
  514. assert(debug.getinfo(print, 't').istailcall == false)
  515. assert(debug.getinfo(print, 't').extraargs == 0)
  516. function g(x) return f(x) end
  517. function g1(x) g(x) end
  518. local function h (x) local f=g1; return f(x) end
  519. h(true)
  520. local b = {}
  521. debug.sethook(function (e) table.insert(b, e) end, "cr")
  522. h(false)
  523. debug.sethook()
  524. local res = {"return", -- first return (from sethook)
  525. "call", "tail call", "call", "tail call",
  526. "return", "return",
  527. "call", -- last call (to sethook)
  528. }
  529. for i = 1, #res do assert(res[i] == table.remove(b, 1)) end
  530. b = 0
  531. debug.sethook(function (e)
  532. if e == "tail call" then
  533. b = b + 1
  534. assert(debug.getinfo(2, "t").istailcall == true)
  535. else
  536. assert(debug.getinfo(2, "t").istailcall == false)
  537. end
  538. end, "c")
  539. h(false)
  540. debug.sethook()
  541. assert(b == 2) -- two tail calls
  542. local lim = _soft and 3000 or 30000
  543. local function foo (x)
  544. if x==0 then
  545. assert(debug.getinfo(2).what == "main")
  546. local info = debug.getinfo(1)
  547. assert(info.istailcall == true and info.func == foo)
  548. else return foo(x-1)
  549. end
  550. end
  551. foo(lim)
  552. print"+"
  553. -- testing local function information
  554. co = load[[
  555. local A = function ()
  556. return x
  557. end
  558. return
  559. ]]
  560. local a = 0
  561. -- 'A' should be visible to debugger only after its complete definition
  562. debug.sethook(function (e, l)
  563. if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(temporary)")
  564. elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A")
  565. end
  566. end, "l")
  567. co() -- run local function definition
  568. debug.sethook() -- turn off hook
  569. assert(a == 2) -- ensure all two lines where hooked
  570. -- testing traceback
  571. assert(debug.traceback(print) == print)
  572. assert(debug.traceback(print, 4) == print)
  573. assert(string.find(debug.traceback("hi", 4), "^hi\n"))
  574. assert(string.find(debug.traceback("hi"), "^hi\n"))
  575. assert(not string.find(debug.traceback("hi"), "'debug.traceback'"))
  576. assert(string.find(debug.traceback("hi", 0), "'debug.traceback'"))
  577. assert(string.find(debug.traceback(), "^stack traceback:\n"))
  578. do -- C-function names in traceback
  579. local st, msg = (function () return pcall end)()(debug.traceback)
  580. assert(st == true and string.find(msg, "pcall"))
  581. end
  582. -- testing nparams, nups e isvararg
  583. local t = debug.getinfo(print, "u")
  584. assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
  585. t = debug.getinfo(function (a,b,c) end, "u")
  586. assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
  587. t = debug.getinfo(function (a,b,...) return t[a] end, "u")
  588. assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
  589. t = debug.getinfo(1) -- main
  590. assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
  591. debug.getupvalue(t.func, 1) == "_ENV")
  592. t = debug.getinfo(math.sin) -- C function
  593. assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
  594. t = debug.getinfo(string.gmatch("abc", "a")) -- C closure
  595. assert(t.isvararg == true and t.nparams == 0 and t.nups > 0)
  596. -- testing debugging of coroutines
  597. local function checktraceback (co, p, level)
  598. local tb = debug.traceback(co, nil, level)
  599. local i = 0
  600. for l in string.gmatch(tb, "[^\n]+\n?") do
  601. assert(i == 0 or string.find(l, p[i]))
  602. i = i+1
  603. end
  604. assert(p[i] == undef)
  605. end
  606. local function f (n)
  607. if n > 0 then f(n-1)
  608. else coroutine.yield() end
  609. end
  610. local co = coroutine.create(f)
  611. coroutine.resume(co, 3)
  612. checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"})
  613. checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1)
  614. checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2)
  615. checktraceback(co, {"db.lua"}, 4)
  616. checktraceback(co, {}, 40)
  617. co = coroutine.create(function (x)
  618. local a = 1
  619. coroutine.yield(debug.getinfo(1, "l"))
  620. coroutine.yield(debug.getinfo(1, "l").currentline)
  621. return a
  622. end)
  623. local tr = {}
  624. local foo = function (e, l) if l then table.insert(tr, l) end end
  625. debug.sethook(co, foo, "lcr")
  626. local _, l = coroutine.resume(co, 10)
  627. local x = debug.getinfo(co, 1, "lfLS")
  628. assert(x.currentline == l.currentline and x.activelines[x.currentline])
  629. assert(type(x.func) == "function")
  630. for i=x.linedefined + 1, x.lastlinedefined do
  631. assert(x.activelines[i])
  632. x.activelines[i] = undef
  633. end
  634. assert(next(x.activelines) == nil) -- no 'extra' elements
  635. assert(not debug.getinfo(co, 2))
  636. local a,b = debug.getlocal(co, 1, 1)
  637. assert(a == "x" and b == 10)
  638. a,b = debug.getlocal(co, 1, 2)
  639. assert(a == "a" and b == 1)
  640. debug.setlocal(co, 1, 2, "hi")
  641. assert(debug.gethook(co) == foo)
  642. assert(#tr == 2 and
  643. tr[1] == l.currentline-1 and tr[2] == l.currentline)
  644. a,b,c = pcall(coroutine.resume, co)
  645. assert(a and b and c == l.currentline+1)
  646. checktraceback(co, {"yield", "in function <"})
  647. a,b = coroutine.resume(co)
  648. assert(a and b == "hi")
  649. assert(#tr == 4 and tr[4] == l.currentline+2)
  650. assert(debug.gethook(co) == foo)
  651. assert(not debug.gethook())
  652. checktraceback(co, {})
  653. -- check get/setlocal in coroutines
  654. co = coroutine.create(function (x)
  655. local a, b = coroutine.yield(x)
  656. assert(a == 100 and b == nil)
  657. return x
  658. end)
  659. a, b = coroutine.resume(co, 10)
  660. assert(a and b == 10)
  661. a, b = debug.getlocal(co, 1, 1)
  662. assert(a == "x" and b == 10)
  663. assert(not debug.getlocal(co, 1, 5))
  664. assert(debug.setlocal(co, 1, 1, 30) == "x")
  665. assert(not debug.setlocal(co, 1, 5, 40))
  666. a, b = coroutine.resume(co, 100)
  667. assert(a and b == 30)
  668. -- check traceback of suspended (or dead with error) coroutines
  669. function f(i)
  670. if i == 0 then error(i)
  671. else coroutine.yield(); f(i-1)
  672. end
  673. end
  674. co = coroutine.create(function (x) f(x) end)
  675. a, b = coroutine.resume(co, 3)
  676. t = {"'coroutine.yield'", "'f'", "in function <"}
  677. while coroutine.status(co) == "suspended" do
  678. checktraceback(co, t)
  679. a, b = coroutine.resume(co)
  680. table.insert(t, 2, "'f'") -- one more recursive call to 'f'
  681. end
  682. t[1] = "'error'"
  683. checktraceback(co, t)
  684. -- test acessing line numbers of a coroutine from a resume inside
  685. -- a C function (this is a known bug in Lua 5.0)
  686. local function g(x)
  687. coroutine.yield(x)
  688. end
  689. local function f (i)
  690. debug.sethook(function () end, "l")
  691. for j=1,1000 do
  692. g(i+j)
  693. end
  694. end
  695. local co = coroutine.wrap(f)
  696. co(10)
  697. pcall(co)
  698. pcall(co)
  699. assert(type(debug.getregistry()) == "table")
  700. -- test tagmethod information
  701. local a = {}
  702. local function f (t)
  703. local info = debug.getinfo(1);
  704. assert(info.namewhat == "metamethod")
  705. a.op = info.name
  706. return info.name
  707. end
  708. setmetatable(a, {
  709. __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f;
  710. __mul = f; __idiv = f; __unm = f; __len = f; __sub = f;
  711. __shl = f; __shr = f; __bor = f; __bxor = f;
  712. __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f;
  713. __bnot = f;
  714. })
  715. local b = setmetatable({}, getmetatable(a))
  716. assert(a[3] == "index" and a^3 == "pow" and a..a == "concat")
  717. assert(a/3 == "div" and 3%a == "mod")
  718. assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and
  719. -a == "unm" and #a == "len" and a&3 == "band")
  720. assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
  721. -a == "unm" and #a == "len" and a & 3 == "band")
  722. assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr")
  723. assert (a==b and a.op == "eq")
  724. assert (a>=b and a.op == "le")
  725. assert ("x">=a and a.op == "le")
  726. assert (a>b and a.op == "lt")
  727. assert (a>10 and a.op == "lt")
  728. assert(~a == "bnot")
  729. do -- testing for-iterator name
  730. local function f()
  731. assert(debug.getinfo(1).name == "for iterator")
  732. end
  733. for i in f do end
  734. end
  735. do -- testing debug info for finalizers
  736. local name = nil
  737. -- create a piece of garbage with a finalizer
  738. setmetatable({}, {__gc = function ()
  739. local t = debug.getinfo(1) -- get function information
  740. assert(t.namewhat == "metamethod")
  741. name = t.name
  742. end})
  743. -- repeat until previous finalizer runs (setting 'name')
  744. repeat local a = {} until name
  745. assert(name == "__gc")
  746. end
  747. do
  748. print("testing traceback sizes")
  749. local function countlines (s)
  750. return select(2, string.gsub(s, "\n", ""))
  751. end
  752. local function deep (lvl, n)
  753. if lvl == 0 then
  754. return (debug.traceback("message", n))
  755. else
  756. return (deep(lvl-1, n))
  757. end
  758. end
  759. local function checkdeep (total, start)
  760. local s = deep(total, start)
  761. local rest = string.match(s, "^message\nstack traceback:\n(.*)$")
  762. local cl = countlines(rest)
  763. -- at most 10 lines in first part, 11 in second, plus '...'
  764. assert(cl <= 10 + 11 + 1)
  765. local brk = string.find(rest, "%.%.%.\t%(skip")
  766. if brk then -- does message have '...'?
  767. local rest1 = string.sub(rest, 1, brk)
  768. local rest2 = string.sub(rest, brk, #rest)
  769. assert(countlines(rest1) == 10 and countlines(rest2) == 11)
  770. else
  771. assert(cl == total - start + 2)
  772. end
  773. end
  774. for d = 1, 51, 10 do
  775. for l = 1, d do
  776. -- use coroutines to ensure complete control of the stack
  777. coroutine.wrap(checkdeep)(d, l)
  778. end
  779. end
  780. end
  781. print("testing debug functions on chunk without debug info")
  782. local prog = [[-- program to be loaded without debug information (strip)
  783. local debug = require'debug'
  784. local a = 12 -- a local variable
  785. local n, v = debug.getlocal(1, 1)
  786. assert(n == "(temporary)" and v == debug) -- unkown name but known value
  787. n, v = debug.getlocal(1, 2)
  788. assert(n == "(temporary)" and v == 12) -- unkown name but known value
  789. -- a function with an upvalue
  790. local f = function () local x; return a end
  791. n, v = debug.getupvalue(f, 1)
  792. assert(n == "(no name)" and v == 12)
  793. assert(debug.setupvalue(f, 1, 13) == "(no name)")
  794. assert(a == 13)
  795. local t = debug.getinfo(f)
  796. assert(t.name == nil and t.linedefined > 0 and
  797. t.lastlinedefined == t.linedefined and
  798. t.short_src == "?")
  799. assert(debug.getinfo(1).currentline == -1)
  800. t = debug.getinfo(f, "L").activelines
  801. assert(next(t) == nil) -- active lines are empty
  802. -- dump/load a function without debug info
  803. f = load(string.dump(f))
  804. t = debug.getinfo(f)
  805. assert(t.name == nil and t.linedefined > 0 and
  806. t.lastlinedefined == t.linedefined and
  807. t.short_src == "?")
  808. assert(debug.getinfo(1).currentline == -1)
  809. return a
  810. ]]
  811. -- load 'prog' without debug info
  812. local f = assert(load(string.dump(load(prog), true)))
  813. assert(f() == 13)
  814. do -- bug in 5.4.0: line hooks in stripped code
  815. local function foo ()
  816. local a = 1
  817. local b = 2
  818. return b
  819. end
  820. local s = load(string.dump(foo, true))
  821. local line = true
  822. debug.sethook(function (e, l)
  823. assert(e == "line")
  824. line = l
  825. end, "l")
  826. assert(s() == 2); debug.sethook(nil)
  827. assert(line == nil) -- hook called withoug debug info for 1st instruction
  828. end
  829. do -- tests for 'source' in binary dumps
  830. local prog = [[
  831. return function (x)
  832. return function (y)
  833. return x + y
  834. end
  835. end
  836. ]]
  837. local name = string.rep("x", 1000)
  838. local p = assert(load(prog, name))
  839. -- load 'p' as a binary chunk with debug information
  840. local c = string.dump(p)
  841. assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump
  842. local f = assert(load(c))
  843. local g = f()
  844. local h = g(3)
  845. assert(h(5) == 8)
  846. assert(debug.getinfo(f).source == name and -- all functions have 'source'
  847. debug.getinfo(g).source == name and
  848. debug.getinfo(h).source == name)
  849. -- again, without debug info
  850. local c = string.dump(p, true)
  851. assert(#c < 500) -- no 'source' in dump
  852. local f = assert(load(c))
  853. local g = f()
  854. local h = g(30)
  855. assert(h(50) == 80)
  856. assert(debug.getinfo(f).source == '=?' and -- no function has 'source'
  857. debug.getinfo(g).source == '=?' and
  858. debug.getinfo(h).source == '=?')
  859. end
  860. print"OK"