0%

gcov,lcov使用方法|普通程序测试和linux内核测试

gcov

下载安装

1
2
sudo apt-get update
sudo apt-get install gcov

普通程序

当前目录:

1
root@ubuntu:/home/kun/Documents/test#

编译源文件test,生成twonumber可执行文件。

1
gcc -fprofile-arcs -ftest-coverage -o twonumber test.c

生成以下两个文件:

1
test.gcno  twonumber

.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。

执行twonumber。

1
./twonumber

生成下面的文件:

1
test.gcda

.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息(而gcda只能在程序运行完毕后才能产生的)

Gcov执行函数覆盖、语句覆盖和分支覆盖。有几个.c就执行几次。

1
2
gcov test.c #默认保存覆盖率文件为test.c.gcov
gcov test.c>>test_cov #保存覆盖率文件为test_cov
1
2
3
File 'test.c'
Lines executed:51.85% of 27
Creating 'test.c.gcov'

生成覆盖率文件

1
test.c.gcov

查看该文件可以看到代码执行情况。

https://blog.csdn.net/yukin_xue/article/details/7653482

注:一次编译后,执行多次(若执行路径不同),gcov统计出的信息也不同。

内核覆盖率

进入内核源文件(这是当初gcc编译内核的位置):

1
cd /usr/src/linux-4.6/

执行gcov进行覆盖统计,生成”gcov”文件。gcov文件需要有gcda和gcno文件(执行的统计信息)的支持才能产生。所需要用到的统计信息放在/sys/kernel/debug/gcov/{内核源文件路径}/kernel/gcov/,因此需要-o。

1
gcov kernel/gcov/base.c -o /sys/kernel/debug/gcov/usr/src/linux-4.6/kernel/gcov/
1
2
3
4
5
6
7
File 'kernel/gcov/base.c'
Lines executed:68.18% of 44
Creating 'base.c.gcov'

File 'include/linux/module.h'
Lines executed:100.00% of 5
Creating 'module.h.gcov'

‘base.c.gcov’以及其包含文件的覆盖率文件”.gcov”都会生成在用户当前所在目录下。

注意:gcov命令依旧只能在当初编译的目录下进行,否则会找不到源码文件,生成的.gcov文件是残缺的。也就是说只能在编译的目录下生成完整的gcov文件。

尝试查看其他文件的覆盖内容:

1
gcov net/ipv4/xfrm4_state.c -o /sys/kernel/debug/gcov/usr/src/linux-4.6/net/ipv4/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
File 'include/net/xfrm.h'
Lines executed:0.00% of 13
Creating 'xfrm.h.gcov'

File 'net/ipv4/xfrm4_state.c'
Lines executed:7.50% of 40
Creating 'xfrm4_state.c.gcov'

File 'include/linux/skbuff.h'
Lines executed:0.00% of 1
Creating 'skbuff.h.gcov'

File 'include/net/net_namespace.h'
Lines executed:0.00% of 1
Creating 'net_namespace.h.gcov'

此时,xfrm4_state.c.gcov、xfrm.h.gcov、net_namespace.h.gcov、skbuff.h.gcov在编译目录下生成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@ubuntu:/usr/src/linux-4.6# ls
arch Kconfig scripts
block kernel security
certs lib skbuff.h.gcov
COPYING MAINTAINERS sound
CREDITS Makefile System.map
crypto mm tools
Documentation modules.builtin usr
drivers modules.order virt
firmware Module.symvers vmlinux
fs net vmlinux.o
include net_namespace.h.gcov xfrm4_state.c.gcov
init README xfrm.h.gcov
ipc REPORTING-BUGS
Kbuild samples

net/ipv4/icmp.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
gcov net/ipv4/icmp.c -o /sys/kernel/debug/gcov/usr/src/linux-4.6/net/ipv4/
File 'net/ipv4/icmp.c'
Lines executed:61.14% of 332
Creating 'icmp.c.gcov'

File 'include/net/l3mdev.h'
Lines executed:100.00% of 1
Creating 'l3mdev.h.gcov'

File 'include/net/inetpeer.h'
Lines executed:0.00% of 4
Creating 'inetpeer.h.gcov'

File 'include/net/sock.h'
Lines executed:100.00% of 1
Creating 'sock.h.gcov'

File 'arch/x86/include/asm/bitops.h'
Lines executed:100.00% of 1
Creating 'bitops.h.gcov'

File 'include/linux/cpumask.h'
Lines executed:100.00% of 1
Creating 'cpumask.h.gcov'

File 'include/net/inet_common.h'
Lines executed:100.00% of 2
Creating 'inet_common.h.gcov'

File 'include/linux/skbuff.h'
Lines executed:52.94% of 51
Creating 'skbuff.h.gcov'

File 'include/net/net_namespace.h'
Lines executed:100.00% of 1
Creating 'net_namespace.h.gcov'

File 'include/net/xfrm.h'
Lines executed:72.73% of 11
Creating 'xfrm.h.gcov'

File 'arch/x86/include/asm/atomic64_64.h'
Lines executed:100.00% of 1
Creating 'atomic64_64.h.gcov'

File 'include/linux/compiler.h'
Lines executed:100.00% of 1
Creating 'compiler.h.gcov'

File 'include/linux/slab.h'
Lines executed:100.00% of 1
Creating 'slab.h.gcov'

File 'include/net/route.h'
Lines executed:33.33% of 9
Creating 'route.h.gcov'

File 'include/net/ip.h'
Lines executed:100.00% of 1
Creating 'ip.h.gcov'

File 'include/linux/err.h'
Lines executed:66.67% of 3
Creating 'err.h.gcov'

File 'include/net/dst.h'
Lines executed:14.29% of 7
Creating 'dst.h.gcov'

File 'include/linux/spinlock.h'
Lines executed:66.67% of 3
Creating 'spinlock.h.gcov'

File 'include/net/ip_fib.h'
Lines executed:100.00% of 2
Creating 'ip_fib.h.gcov'

File 'include/linux/jhash.h'
Lines executed:100.00% of 7
Creating 'jhash.h.gcov'

File 'arch/x86/include/asm/checksum_64.h'
Lines executed:100.00% of 5
Creating 'checksum_64.h.gcov'

File 'include/net/checksum.h'
Lines executed:100.00% of 1
Creating 'checksum.h.gcov'

File 'include/linux/bitops.h'
Lines executed:50.00% of 2
Creating 'bitops.h.gcov'

File 'arch/x86/include/asm/paravirt.h'
Lines executed:0.00% of 1
Creating 'paravirt.h.gcov'

File 'include/linux/spinlock_api_smp.h'
Lines executed:0.00% of 1
Creating 'spinlock_api_smp.h.gcov'

File 'arch/x86/include/asm/preempt.h'
Lines executed:100.00% of 1
Creating 'preempt.h.gcov'

File 'include/linux/bottom_half.h'
Lines executed:50.00% of 2
Creating 'bottom_half.h.gcov'

File 'arch/x86/include/asm/atomic.h'
Lines executed:0.00% of 1
Creating 'atomic.h.gcov'

执行了一个ping命令后,再次查看覆盖率:

1
2
3
4
5
6
gcov net/ipv4/icmp.c -o /sys/kernel/debug/gcov/usr/src/linux-4.6/net/ipv4/
File 'net/ipv4/icmp.c'
Lines executed:61.75% of 332
Creating 'icmp.c.gcov'

...

icmp.c的覆盖率有变化。

lcov

下载安装

1
2
sudo apt-get update
sudo apt-get install lcov

普通程序

当前目录:

1
root@ubuntu:/home/kun/Documents/test#

已经编译过:

1
test  test.c  test.gcda  test.gcno  test.h

扫描指定目录下的.gcno文件,生成基准数据文件,此处起名init.info:

1
2
# -c捕获,收集运行后所产生的信息 -i初始化 -d工作目录 -o输出文件
lcov -c -i -d [源码工作目录] -o [基准数据文件名]
1
2
3
4
5
6
7
lcov -c -i -d ./ -o init.info
Capturing coverage data from ./
Found gcov version: 5.4.0
Scanning ./ for .gcno files ...
Found 1 graph files in ./
Processing test.gcno
Finished .info-file creation

执行程序

1
./test

扫描指定目录下的.gcda文件,生成覆盖率文件,此处起名cover.info:

1
2
3
4
5
6
7
lcov -c -d ./ -o cover.info
Capturing coverage data from ./
Found gcov version: 5.4.0
Scanning ./ for .gcda files ...
Found 1 data files in ./
Processing test.gcda
Finished .info-file creation

合并基准数据文件覆盖率文件,生成一个总数据文件,total.info:

1
2
3
4
5
6
7
8
9
10
# -a合并 [文件名之一]
lcov -a init.info -a cover.info -o total.info
Combining tracefiles.
Reading tracefile init.info
Reading tracefile cover.info
Writing data to total.info
Summary coverage rate:
lines......: 64.9% (24 of 37 lines)
functions..: 80.0% (4 of 5 functions)
branches...: no data found

如果有不需要的信息可以删掉(可省略这步):

1
2
# --remove 删除统计信息中如下的代码或文件,支持正则
lcov --remove total.info '*/usr/include/*' -o total.info

通过总数据文件生成html文件:

1
2
3
4
5
# -o 输出的文件夹
# --legend 简单的统计信息说明
# --title 项目名称,最后会出现在html页里test一项
# --prefix 将要生成的html文件路径(基于[输出的文件夹])
genhtml -o cover_report --legend --title "lcov-test" --prefix=./ total.info

生成的文件:

1
2
3
ls
cover.info init.info test.c test.gcno total.info
cover_report test test.gcda test.h

cover_report/下,浏览器打开index.html文件则可以看到结果:

1
2
3
4
ls
amber.png glass.png index-sort-f.html snow.png
emerald.png home index-sort-l.html updown.png
gcov.css index.html ruby.png

常用参数

  1. -d 项目路径,即.gcda .gcno所在的路径
  2. -a 合并(归并)多个lcov生成的info文件
  3. -c 捕获,也即收集代码运行后所产生的统计计数信息
  4. –external 捕获其它目录产生的统计计数文件
  5. -i/–initial 初始化所有的覆盖率信息,作为基准数据
  6. -o 生成处理后的文件
  7. -r/–remove 移除不需要关注的覆盖率信息文件
  8. -z 重置所有执行程序所产生的统计信息为0

内核覆盖率

linux-test-project

As root:

1
lcov --zerocounters

捕捉当前覆盖状态,存于一个文件(需要等几分钟):

1
2
3
4
5
6
7
lcov -c -o lcov_res/kernel.info
...
Processing crypto/gf128mul.gcda
Processing crypto/gcm.gcda
Processing crypto/ghash-generic.gcda
Finished .info-file creation
Removing temporary directories.

生成html输出。读当前文件/lcov_res/kernel.info,在当前目录/cover_report/下生成html文件。

1
2
3
4
5
6
7
8
9
10
11
genhtml -o cover_report --legend --title "kernel_cov" --prefix=./ lcov_res/kernel.info
...
Processing file /usr/src/linux-4.6/sound/pci/ens1370.c
Processing file /usr/src/linux-4.6/sound/pci/ac97/ac97_pcm.c
Processing file /usr/src/linux-4.6/sound/pci/ac97/ac97_patch.c
Processing file /usr/src/linux-4.6/sound/pci/ac97/ac97_codec.c
Processing file /usr/src/linux-4.6/sound/pci/ac97/ac97_proc.c
Writing directory view page.
Overall coverage rate:
lines......: 7.0% (42498 of 605635 lines)
functions..: 8.7% (4784 of 54929 functions)