lua的程式碼覆蓋率工具
# cmake .
# make
# go build clua.go
-- 載入libclua.so
local cl = require "libclua"
-- 開始記錄執行過程,生成結果檔案
-- 第一個引數為結果檔案的檔名
-- 第二個引數為定時生成結果檔案的間隔(秒),0表示關閉
cl.start("test.cov", 5)
-- 執行某些事情
do_something()
-- 結束記錄
cl.stop()
a) 首先獲取程序中的Lua_State指標,比如程序呼叫了lua_settop(L)函式,那麼就取第一個引數
# ./hookso arg $PID liblua.so lua_settop 1
123456
b) 載入libclua.so
# ./hookso dlopen $PID ./libclua.so
c) 執行libclua.so的start_cov手動開啟,等價於start_cov(L, "./test.cov", 5)
# ./hookso call $PID libclua.so start_cov i=123456 s="./test.cov" i=5
c) 執行libclua.so的stop_cov手動關閉,等價於stop_cov(L)
# ./hookso call $PID libclua.so stop_cov i=123456
# ./clua -i test.cov
# lua5.3 ./test.lua
# ll test.cov
# ./clua -i test.cov
total points = 20, files = 1
coverage of /home/project/clua/test.lua:
local cl = require "libclua"
cl.start("test.cov", 5)
1 function test1(i)
10 if i % 2 then
10 print("a "..i)
else
print("b "..i)
end
11 end
1 function test2(i)
40 if i > 30 then
19 print("c "..i)
else
21 print("d "..i)
end
41 end
1 function test3(i)
51 if i > 0 then
51 print("e "..i)
else
print("f "..i)
end
52 end
102 for i = 0, 100 do
101 if i < 10 then
10 test1(i)
91 elseif i < 50 then
40 test2(i)
else
51 test3(i)
end
end
1 cl.stop()
/home/project/clua/test.lua total coverage 60%
[admin
]