db.lua 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. -- $Id: testes/db.lua $
  2. -- See Copyright Notice in file lua.h
  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,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. -- declare some globals to check that they don't interfere with 'getlocal'
  292. global collectgarbage
  293. collectgarbage()
  294. local _, x = debug.getlocal(1, 1)
  295. global assert, g, string
  296. local _, y = debug.getlocal(1, 2)
  297. assert(x == a and y == b)
  298. assert(debug.setlocal(2, 4, "pera") == "AA".."AA")
  299. assert(debug.setlocal(2, 5, "manga") == "B")
  300. x = debug.getinfo(2)
  301. assert(x.func == g and x.what == "Lua" and x.name == 'g' and
  302. x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
  303. glob = glob+1
  304. assert(debug.getinfo(1, "l").currentline == L+1)
  305. assert(debug.getinfo(1, "l").currentline == L+2)
  306. end
  307. function foo()
  308. glob = glob+1
  309. assert(debug.getinfo(1, "l").currentline == L+1)
  310. end; foo() -- set L
  311. -- check line counting inside strings and empty lines
  312. local _ = 'alo\
  313. alo' .. [[
  314. ]]
  315. --[[
  316. ]]
  317. assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines
  318. function g (...)
  319. local arg = {...}
  320. do local a,b,c; a=math.sin(40); end
  321. local feijao
  322. local AAAA,B = "xuxu", "abacate"
  323. f(AAAA,B)
  324. assert(AAAA == "pera" and B == "manga")
  325. do
  326. global *
  327. local B = 13
  328. global<const> assert
  329. local x,y = debug.getlocal(1,6)
  330. assert(x == 'B' and y == 13)
  331. end
  332. end
  333. g()
  334. assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print])
  335. -- tests for manipulating non-registered locals (C and Lua temporaries)
  336. local n, v = debug.getlocal(0, 1)
  337. assert(v == 0 and n == "(C temporary)")
  338. local n, v = debug.getlocal(0, 2)
  339. assert(v == 2 and n == "(C temporary)")
  340. assert(not debug.getlocal(0, 3))
  341. assert(not debug.getlocal(0, 0))
  342. function f()
  343. assert(select(2, debug.getlocal(2,3)) == 1)
  344. assert(not debug.getlocal(2,4))
  345. debug.setlocal(2, 3, 10)
  346. return 20
  347. end
  348. function g(a,b) return (a+1) + f() end
  349. assert(g(0,0) == 30)
  350. _G.f, _G.g = nil
  351. debug.sethook(nil);
  352. assert(not debug.gethook())
  353. -- minimal tests for setuservalue/getuservalue
  354. do
  355. assert(not debug.setuservalue(io.stdin, 10))
  356. local a, b = debug.getuservalue(io.stdin, 10)
  357. assert(a == nil and not b)
  358. end
  359. -- testing interaction between multiple values x hooks
  360. do
  361. local function f(...) return 3, ... end
  362. local count = 0
  363. local a = {}
  364. for i = 1, 100 do a[i] = i end
  365. debug.sethook(function () count = count + 1 end, "", 1)
  366. local t = {table.unpack(a)}
  367. assert(#t == 100)
  368. t = {table.unpack(a, 1, 3)}
  369. assert(#t == 3)
  370. t = {f(table.unpack(a, 1, 30))}
  371. assert(#t == 31)
  372. end
  373. -- testing access to function arguments
  374. local function collectlocals (level)
  375. local tab = {}
  376. for i = 1, math.huge do
  377. local n, v = debug.getlocal(level + 1, i)
  378. if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or
  379. n == "(vararg table)") then
  380. break -- consider only real variables
  381. end
  382. tab[n] = v
  383. end
  384. return tab
  385. end
  386. local X = nil
  387. a = {}
  388. function a:f (a, b, ...) local arg = {...}; local c = 13 end
  389. debug.sethook(function (e)
  390. assert(e == "call")
  391. dostring("XX = 12") -- test dostring inside hooks
  392. -- testing errors inside hooks
  393. assert(not pcall(load("a='joao'+1")))
  394. debug.sethook(function (e, l)
  395. assert(debug.getinfo(2, "l").currentline == l)
  396. local f,m,c = debug.gethook()
  397. assert(e == "line")
  398. assert(m == 'l' and c == 0)
  399. debug.sethook(nil) -- hook is called only once
  400. assert(not X) -- check that
  401. X = collectlocals(2)
  402. end, "l")
  403. end, "c")
  404. a:f(1,2,3,4,5)
  405. assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
  406. assert(XX == 12)
  407. assert(not debug.gethook())
  408. _G.XX = nil
  409. -- testing access to local variables in return hook (bug in 5.2)
  410. do
  411. local X = false
  412. local function foo (a, b, ...)
  413. do local x,y,z end
  414. local c, d = 10, 20
  415. return
  416. end
  417. local function aux ()
  418. if debug.getinfo(2).name == "foo" then
  419. X = true -- to signal that it found 'foo'
  420. local tab = {a = 100, b = 200, c = 10, d = 20}
  421. for n, v in pairs(collectlocals(2)) do
  422. assert(tab[n] == v)
  423. tab[n] = undef
  424. end
  425. assert(next(tab) == nil) -- 'tab' must be empty
  426. end
  427. end
  428. debug.sethook(aux, "r"); foo(100, 200); debug.sethook()
  429. assert(X)
  430. end
  431. local function eqseq (t1, t2)
  432. assert(#t1 == #t2)
  433. for i = 1, #t1 do
  434. assert(t1[i] == t2[i])
  435. end
  436. end
  437. do print("testing inspection of parameters/returned values")
  438. local on = false
  439. local inp, out
  440. local function hook (event)
  441. if not on then return end
  442. local ar = debug.getinfo(2, "ruS")
  443. local t = {}
  444. for i = ar.ftransfer, ar.ftransfer + ar.ntransfer - 1 do
  445. local _, v = debug.getlocal(2, i)
  446. t[#t + 1] = v
  447. end
  448. if event == "return" then
  449. out = t
  450. else
  451. inp = t
  452. end
  453. end
  454. debug.sethook(hook, "cr")
  455. on = true; math.sin(3); on = false
  456. eqseq(inp, {3}); eqseq(out, {math.sin(3)})
  457. on = true; select(2, 10, 20, 30, 40); on = false
  458. eqseq(inp, {2, 10, 20, 30, 40}); eqseq(out, {20, 30, 40})
  459. local function foo (a, ...) return ... end
  460. local function foo1 () on = not on; return foo(20, 10, 0) end
  461. foo1(); on = false
  462. eqseq(inp, {20}); eqseq(out, {10, 0})
  463. debug.sethook()
  464. end
  465. -- testing upvalue access
  466. local function getupvalues (f)
  467. local t = {}
  468. local i = 1
  469. while true do
  470. local name, value = debug.getupvalue(f, i)
  471. if not name then break end
  472. assert(not t[name])
  473. t[name] = value
  474. i = i + 1
  475. end
  476. return t
  477. end
  478. local a,b,c = 1,2,3
  479. local function foo1 (a) b = a; return c end
  480. local function foo2 (x) a = x; return c+b end
  481. assert(not debug.getupvalue(foo1, 3))
  482. assert(not debug.getupvalue(foo1, 0))
  483. assert(not debug.setupvalue(foo1, 3, "xuxu"))
  484. local t = getupvalues(foo1)
  485. assert(t.a == nil and t.b == 2 and t.c == 3)
  486. t = getupvalues(foo2)
  487. assert(t.a == 1 and t.b == 2 and t.c == 3)
  488. assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
  489. assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
  490. -- upvalues of C functions are always named "" (the empty string)
  491. assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
  492. -- testing count hooks
  493. local a=0
  494. debug.sethook(function (e) a=a+1 end, "", 1)
  495. a=0; for i=1,1000 do end; assert(1000 < a and a < 1012)
  496. debug.sethook(function (e) a=a+1 end, "", 4)
  497. a=0; for i=1,1000 do end; assert(250 < a and a < 255)
  498. local f,m,c = debug.gethook()
  499. assert(m == "" and c == 4)
  500. debug.sethook(function (e) a=a+1 end, "", 4000)
  501. a=0; for i=1,1000 do end; assert(a == 0)
  502. do
  503. debug.sethook(print, "", 2^24 - 1) -- count upperbound
  504. local f,m,c = debug.gethook()
  505. assert(({debug.gethook()})[3] == 2^24 - 1)
  506. end
  507. debug.sethook()
  508. local g, g1
  509. -- tests for tail calls
  510. local function f (x)
  511. if x then
  512. assert(debug.getinfo(1, "S").what == "Lua")
  513. assert(debug.getinfo(1, "t").istailcall == true)
  514. local tail = debug.getinfo(2)
  515. assert(tail.func == g1 and tail.istailcall == true)
  516. assert(debug.getinfo(3, "S").what == "main")
  517. print"+"
  518. end
  519. end
  520. assert(debug.getinfo(print, 't').istailcall == false)
  521. assert(debug.getinfo(print, 't').extraargs == 0)
  522. function g(x) return f(x) end
  523. function g1(x) g(x) end
  524. local function h (x) local f=g1; return f(x) end
  525. h(true)
  526. local b = {}
  527. debug.sethook(function (e) table.insert(b, e) end, "cr")
  528. h(false)
  529. debug.sethook()
  530. local res = {"return", -- first return (from sethook)
  531. "call", "tail call", "call", "tail call",
  532. "return", "return",
  533. "call", -- last call (to sethook)
  534. }
  535. for i = 1, #res do assert(res[i] == table.remove(b, 1)) end
  536. b = 0
  537. debug.sethook(function (e)
  538. if e == "tail call" then
  539. b = b + 1
  540. assert(debug.getinfo(2, "t").istailcall == true)
  541. else
  542. assert(debug.getinfo(2, "t").istailcall == false)
  543. end
  544. end, "c")
  545. h(false)
  546. debug.sethook()
  547. assert(b == 2) -- two tail calls
  548. local lim = _soft and 3000 or 30000
  549. local function foo (x)
  550. if x==0 then
  551. assert(debug.getinfo(2).what == "main")
  552. local info = debug.getinfo(1)
  553. assert(info.istailcall == true and info.func == foo)
  554. else return foo(x-1)
  555. end
  556. end
  557. foo(lim)
  558. print"+"
  559. -- testing local function information
  560. co = load[[
  561. local A = function ()
  562. return x
  563. end
  564. return
  565. ]]
  566. local a = 0
  567. -- 'A' should be visible to debugger only after its complete definition
  568. debug.sethook(function (e, l)
  569. if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(temporary)")
  570. elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A")
  571. end
  572. end, "l")
  573. co() -- run local function definition
  574. debug.sethook() -- turn off hook
  575. assert(a == 2) -- ensure all two lines where hooked
  576. -- testing traceback
  577. assert(debug.traceback(print) == print)
  578. assert(debug.traceback(print, 4) == print)
  579. assert(string.find(debug.traceback("hi", 4), "^hi\n"))
  580. assert(string.find(debug.traceback("hi"), "^hi\n"))
  581. assert(not string.find(debug.traceback("hi"), "'debug.traceback'"))
  582. assert(string.find(debug.traceback("hi", 0), "'traceback'"))
  583. assert(string.find(debug.traceback(), "^stack traceback:\n"))
  584. do -- C-function names in traceback
  585. local st, msg = (function () return pcall end)()(debug.traceback)
  586. assert(st == true and string.find(msg, "pcall"))
  587. end
  588. -- testing nparams, nups e isvararg
  589. local t = debug.getinfo(print, "u")
  590. assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
  591. t = debug.getinfo(function (a,b,c) end, "u")
  592. assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
  593. t = debug.getinfo(function (a,b,...) return t[a] end, "u")
  594. assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
  595. t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u")
  596. assert(t.isvararg == true and t.nparams == 2 and t.nups == 0)
  597. t = debug.getinfo(1) -- main
  598. assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
  599. debug.getupvalue(t.func, 1) == "_ENV")
  600. t = debug.getinfo(math.sin) -- C function
  601. assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
  602. t = debug.getinfo(string.gmatch("abc", "a")) -- C closure
  603. assert(t.isvararg == true and t.nparams == 0 and t.nups > 0)
  604. -- testing debugging of coroutines
  605. local function checktraceback (co, p, level)
  606. local tb = debug.traceback(co, nil, level)
  607. local i = 0
  608. for l in string.gmatch(tb, "[^\n]+\n?") do
  609. assert(i == 0 or string.find(l, p[i]))
  610. i = i+1
  611. end
  612. assert(p[i] == undef)
  613. end
  614. local function f (n)
  615. if n > 0 then f(n-1)
  616. else coroutine.yield() end
  617. end
  618. local co = coroutine.create(f)
  619. coroutine.resume(co, 3)
  620. checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"})
  621. checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1)
  622. checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2)
  623. checktraceback(co, {"db.lua"}, 4)
  624. checktraceback(co, {}, 40)
  625. co = coroutine.create(function (x)
  626. local a = 1
  627. coroutine.yield(debug.getinfo(1, "l"))
  628. coroutine.yield(debug.getinfo(1, "l").currentline)
  629. return a
  630. end)
  631. local tr = {}
  632. local foo = function (e, l) if l then table.insert(tr, l) end end
  633. debug.sethook(co, foo, "lcr")
  634. local _, l = coroutine.resume(co, 10)
  635. local x = debug.getinfo(co, 1, "lfLS")
  636. assert(x.currentline == l.currentline and x.activelines[x.currentline])
  637. assert(type(x.func) == "function")
  638. for i=x.linedefined + 1, x.lastlinedefined do
  639. assert(x.activelines[i])
  640. x.activelines[i] = undef
  641. end
  642. assert(next(x.activelines) == nil) -- no 'extra' elements
  643. assert(not debug.getinfo(co, 2))
  644. local a,b = debug.getlocal(co, 1, 1)
  645. assert(a == "x" and b == 10)
  646. a,b = debug.getlocal(co, 1, 2)
  647. assert(a == "a" and b == 1)
  648. debug.setlocal(co, 1, 2, "hi")
  649. assert(debug.gethook(co) == foo)
  650. assert(#tr == 2 and
  651. tr[1] == l.currentline-1 and tr[2] == l.currentline)
  652. a,b,c = pcall(coroutine.resume, co)
  653. assert(a and b and c == l.currentline+1)
  654. checktraceback(co, {"yield", "in function <"})
  655. a,b = coroutine.resume(co)
  656. assert(a and b == "hi")
  657. assert(#tr == 4 and tr[4] == l.currentline+2)
  658. assert(debug.gethook(co) == foo)
  659. assert(not debug.gethook())
  660. checktraceback(co, {})
  661. -- check get/setlocal in coroutines
  662. co = coroutine.create(function (x)
  663. local a, b = coroutine.yield(x)
  664. assert(a == 100 and b == nil)
  665. return x
  666. end)
  667. a, b = coroutine.resume(co, 10)
  668. assert(a and b == 10)
  669. a, b = debug.getlocal(co, 1, 1)
  670. assert(a == "x" and b == 10)
  671. assert(not debug.getlocal(co, 1, 5))
  672. assert(debug.setlocal(co, 1, 1, 30) == "x")
  673. assert(not debug.setlocal(co, 1, 5, 40))
  674. a, b = coroutine.resume(co, 100)
  675. assert(a and b == 30)
  676. -- check traceback of suspended (or dead with error) coroutines
  677. function f(i)
  678. if i == 0 then error(i)
  679. else coroutine.yield(); f(i-1)
  680. end
  681. end
  682. co = coroutine.create(function (x) f(x) end)
  683. a, b = coroutine.resume(co, 3)
  684. t = {"'yield'", "'f'", "in function <"}
  685. while coroutine.status(co) == "suspended" do
  686. checktraceback(co, t)
  687. a, b = coroutine.resume(co)
  688. table.insert(t, 2, "'f'") -- one more recursive call to 'f'
  689. end
  690. t[1] = "'error'"
  691. checktraceback(co, t)
  692. -- test accessing line numbers of a coroutine from a resume inside
  693. -- a C function (this is a known bug in Lua 5.0)
  694. local function g(x)
  695. coroutine.yield(x)
  696. end
  697. local function f (i)
  698. debug.sethook(function () end, "l")
  699. for j=1,1000 do
  700. g(i+j)
  701. end
  702. end
  703. local co = coroutine.wrap(f)
  704. co(10)
  705. pcall(co)
  706. pcall(co)
  707. assert(type(debug.getregistry()) == "table")
  708. -- test tagmethod information
  709. local a = {}
  710. local function f (t)
  711. local info = debug.getinfo(1);
  712. assert(info.namewhat == "metamethod")
  713. a.op = info.name
  714. return info.name
  715. end
  716. setmetatable(a, {
  717. __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f;
  718. __mul = f; __idiv = f; __unm = f; __len = f; __sub = f;
  719. __shl = f; __shr = f; __bor = f; __bxor = f;
  720. __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f;
  721. __bnot = f;
  722. })
  723. local b = setmetatable({}, getmetatable(a))
  724. assert(a[3] == "index" and a^3 == "pow" and a..a == "concat")
  725. assert(a/3 == "div" and 3%a == "mod")
  726. assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and
  727. -a == "unm" and #a == "len" and a&3 == "band")
  728. assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
  729. -a == "unm" and #a == "len" and a & 3 == "band")
  730. assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr")
  731. assert (a==b and a.op == "eq")
  732. assert (a>=b and a.op == "le")
  733. assert ("x">=a and a.op == "le")
  734. assert (a>b and a.op == "lt")
  735. assert (a>10 and a.op == "lt")
  736. assert(~a == "bnot")
  737. do -- testing for-iterator name
  738. local function f()
  739. assert(debug.getinfo(1).name == "for iterator")
  740. end
  741. for i in f do end
  742. end
  743. do -- testing debug info for finalizers
  744. local name = nil
  745. -- create a piece of garbage with a finalizer
  746. setmetatable({}, {__gc = function ()
  747. local t = debug.getinfo(1) -- get function information
  748. assert(t.namewhat == "metamethod")
  749. name = t.name
  750. end})
  751. -- repeat until previous finalizer runs (setting 'name')
  752. repeat local a = {} until name
  753. assert(name == "__gc")
  754. end
  755. do
  756. print("testing traceback sizes")
  757. local function countlines (s)
  758. return select(2, string.gsub(s, "\n", ""))
  759. end
  760. local function deep (lvl, n)
  761. if lvl == 0 then
  762. return (debug.traceback("message", n))
  763. else
  764. return (deep(lvl-1, n))
  765. end
  766. end
  767. local function checkdeep (total, start)
  768. local s = deep(total, start)
  769. local rest = string.match(s, "^message\nstack traceback:\n(.*)$")
  770. local cl = countlines(rest)
  771. -- at most 10 lines in first part, 11 in second, plus '...'
  772. assert(cl <= 10 + 11 + 1)
  773. local brk = string.find(rest, "%.%.%.\t%(skip")
  774. if brk then -- does message have '...'?
  775. local rest1 = string.sub(rest, 1, brk)
  776. local rest2 = string.sub(rest, brk, #rest)
  777. assert(countlines(rest1) == 10 and countlines(rest2) == 11)
  778. else
  779. assert(cl == total - start + 2)
  780. end
  781. end
  782. for d = 1, 51, 10 do
  783. for l = 1, d do
  784. -- use coroutines to ensure complete control of the stack
  785. coroutine.wrap(checkdeep)(d, l)
  786. end
  787. end
  788. end
  789. print("testing debug functions on chunk without debug info")
  790. local prog = [[-- program to be loaded without debug information (strip)
  791. local debug = require'debug'
  792. local a = 12 -- a local variable
  793. local n, v = debug.getlocal(1, 1)
  794. assert(n == "(temporary)" and v == debug) -- unknown name but known value
  795. n, v = debug.getlocal(1, 2)
  796. assert(n == "(temporary)" and v == 12) -- unknown name but known value
  797. -- a function with an upvalue
  798. local f = function () local x; return a end
  799. n, v = debug.getupvalue(f, 1)
  800. assert(n == "(no name)" and v == 12)
  801. assert(debug.setupvalue(f, 1, 13) == "(no name)")
  802. assert(a == 13)
  803. local t = debug.getinfo(f)
  804. assert(t.name == nil and t.linedefined > 0 and
  805. t.lastlinedefined == t.linedefined and
  806. t.short_src == "?")
  807. assert(debug.getinfo(1).currentline == -1)
  808. t = debug.getinfo(f, "L").activelines
  809. assert(next(t) == nil) -- active lines are empty
  810. -- dump/load a function without debug info
  811. f = load(string.dump(f))
  812. t = debug.getinfo(f)
  813. assert(t.name == nil and t.linedefined > 0 and
  814. t.lastlinedefined == t.linedefined and
  815. t.short_src == "?")
  816. assert(debug.getinfo(1).currentline == -1)
  817. return a
  818. ]]
  819. -- load 'prog' without debug info
  820. local f = assert(load(string.dump(load(prog), true)))
  821. assert(f() == 13)
  822. do -- bug in 5.4.0: line hooks in stripped code
  823. local function foo ()
  824. local a = 1
  825. local b = 2
  826. return b
  827. end
  828. local s = load(string.dump(foo, true))
  829. local line = true
  830. debug.sethook(function (e, l)
  831. assert(e == "line")
  832. line = l
  833. end, "l")
  834. assert(s() == 2); debug.sethook(nil)
  835. assert(line == nil) -- hook called without debug info for 1st instruction
  836. end
  837. do -- tests for 'source' in binary dumps
  838. local prog = [[
  839. return function (x)
  840. return function (y)
  841. return x + y
  842. end
  843. end
  844. ]]
  845. local name = string.rep("x", 1000)
  846. local p = assert(load(prog, name))
  847. -- load 'p' as a binary chunk with debug information
  848. local c = string.dump(p)
  849. assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump
  850. local f = assert(load(c))
  851. local g = f()
  852. local h = g(3)
  853. assert(h(5) == 8)
  854. assert(debug.getinfo(f).source == name and -- all functions have 'source'
  855. debug.getinfo(g).source == name and
  856. debug.getinfo(h).source == name)
  857. -- again, without debug info
  858. local c = string.dump(p, true)
  859. assert(#c < 500) -- no 'source' in dump
  860. local f = assert(load(c))
  861. local g = f()
  862. local h = g(30)
  863. assert(h(50) == 80)
  864. assert(debug.getinfo(f).source == '=?' and -- no function has 'source'
  865. debug.getinfo(g).source == '=?' and
  866. debug.getinfo(h).source == '=?')
  867. end
  868. print"OK"