一、关于gcov工具
gcov伴随gcc发布。gcc编译加入-fprofile-arcs -ftest-coverage参数生成二进制程序,执行测试用例生成代码覆盖率信息。1、如何使用gcov
用GCC编译的时候加上-fprofile-arcs -ftest-coverage选项,链接的时候也加上。fprofile-arcs参数使gcc创建一个程序的流图,之后找到适合图的生成树。只有不在生成树中的弧被操纵(instrumented):gcc添加了代码来清点这些弧执行的次数。当这段弧是一个块的唯一出口或入口时,操纵工具代码(instrumentation code)将会添加到块中,否则创建一个基础块来包含操纵工具代码。
gcov主要使用.gcno和.gcda两个文件。.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息(而gcda只能在程序运行完毕后才能产生的)。Gcov执行函数覆盖、语句覆盖和分支覆盖。
举个例子,程序代码由main.c和tmp.c两个文件组成,编译、链接、运行程序编译:gcc -fprofile-arcs -ftest-coverage -o myapp main.c tmp.c运行:./myapp然后输入命令:gcov main.c,gcov tmp.c
这个时候当前目录下有了新的文档main.c.gcov,和tmp.c.gcov若想保存覆盖率文件,上述命令修改为:命令:gcov main.c >>yourfilename,gcov tmp.c >>yourfilename
而这时候的main.c.gcov,和tmp.c.gcov就包含了函数和代码执行次数的信息,我们可以查看结果:-: 65:/***************************************************************************************
-: 66: * name : main
-: 67: * return : 0 OK
-: 68: * other ERROR
-: 69: * history : 2006-06-13
-: 70:****************************************************************************************/
-: 71:int main( int argc, char *argv[] ) /* the entrance for program
*/
function main called 4 returned 100% blocks executed 81%
4: 72:{
4: 73: int loop = 0 ;
4: 74: int ret = OK ;
4: 75: int empty_line = 0 ;
4: 76: int code_line = 0 ;
4: 77: int annotation_line = 0 ;
4: 78: struct stat file_stat ; /* use for file state */
4: 79: char recu_name[256] ;
4: 80: char *pwd = NULL ;
4: 81: char *tmp = NULL ;
-: 82:
4: 83: if( argc = MAX_FILE ){ /* file size larger than max size */
#####: 98: printf( "file [%s] size is over 64K! \ncontinue....\n", argv[loop] ) ;
#####: 99: continue ;
-: 100: }
#####这就是表示没跑到的
各个参数使用如下:gcov [-b] [-c] [-v] [-n] [-l] [-f] [-o directory] sourcefile
-b
Write branch frequencies to the output file, and write branch summary info to the standard output. This option allows you to
see how often each branch in your program was taken.
//b(ranch),分支测试-c
Write branch frequencies as the number of branches taken, rather than the percentage of branches taken.
-v
Display the gcov version number (on the standard error stream).
//太简单了吧,我上面用了-n
Do not create the gcov output file.
-l
Create long file names for included source files. For example, if the header file `x.h' contains code, and was included in the
file `a.c', then running gcov on the file `a.c' will produce an output file called `a.c.x.h.gcov' instead of `x.h.gcov'. This can
be useful if `x.h' is included in multiple source files.
-f
Output summaries for each function in addition to the file level summary.
-o
The directory where the object files live. Gcov will search for `.bb', `.bbg', and `.da' files in this directory.新版的是这么说的-o directory│file
--object-directory directory
--object-file file
Specify either the directory containing the gcov data files, or the
object path name. The .gcno, and .gcda data files are searched for
using this option. If a directory is specified, the data files are
in that directory and named after the source file name, without its
extension. If a file is specified here, the data files are named
after that file, without its extension. If this option is not sup-
plied, it defaults to the current directory.其他的更有新版的-u,
-u
--unconditional-branches
When branch counts are given, include those of unconditional
branches. Unconditional branches are normally not interesting.
-p
--preserve-paths
Preserve complete path information in the names of generated .gcov
files. Without this option, just the filename component is used.
With this option, all directories are used, with ’/’ characters
translated to ’#’ characters, ’.’ directory components removed and
’..’ components renamed to ’^’. This is useful if sourcefiles are
in several different directories. It also affects the -l option.
二、关于lcov
Lcov则是上的gcov结果展现的一个前端,可以将覆盖率信息转换成html展现。
1.如何访问用户空间应用程序代码覆盖数据的示例---------------------------------------------------------------------前提条件:使用GCC以-fprofile-arcs和-ftest-coverage选项编译程序。假设编译目录名称为"/root/test/code_cover/",然后执行:
以我的一个实例/root/test/code_cover/下的fork.c为例看代码的覆盖率:
首先进入/root/test/code_cover/目录a)重置计数器lcov --directory . --zerocounters
b)收集当前代码覆盖状态到一个文件(应用程序启动和停止至少一次后,该命令才能正常工作)lcov --directory . --capture --output-file app.info
Capturing coverage data from .
Found gcov version: 3.4.6
Scanning . for .gcda files ...
Found 1 data files in .
Processing ./TestQuery.gcda
Finished .info-file creation
c)获取HTML输出genhtml -o results app.info
其中的“-o results”是指示结果存放在什么位置的。
Readingdata file app.info
Found 18 entries.
Found common filename prefix "/home/search/isearch_yb/src"
Writing .css and .png files.
Generating output.
Processing file cpp/core/basis/GlobalDef.h
Processing file cpp/core/search/QueryCache.h
...
Writing directory view page.
Overall coverage rate: 117 of 514 lines (22.8%)使用web浏览器打开index.html文件查看代码覆盖结果。
三、Linux内核覆盖率测试:
将最终的gcov内核模块文件复制到system wide modules目录或者PERL脚本所在目录。以root身份,执行:
Gcov:在对Linux内核程序进行代码覆盖率测试时,同样可以采用gcov,但是需要对kernel打一个补丁。Gcov的内核补丁下载地址:。下载gcov内核补丁(wget),解压补丁,然后为一个kernel打补丁(patch –p1 < /home/linux-v3.4-mem/gcov-kernel-2/linux-2.6.18-gcov.patch)注意打补丁时应该处于内核的主目录下也即/home/linux-v3.4-mem,打完补丁之后,通过make menuconfig配置gcov,配置页面显示如下:
配置完毕之后,重新编译内核,将编译成功的gcov这个内核模块/home/linux-v3.4-mem /kernel/gcov/gcov-proc.ko拷贝到网络文件系统下面,arm linux系统启动后加载这个模块
(1)/insmod gcov-proc.ko
然后再跑其他测试程序,跑了一段时间,你会发现在/proc/gcov目录下有各种gcov的统计文件:
/proc/gcov # ls
arch crypto init kernel security vmlinux
block drivers ipc net sound
把这整个目录拷贝到fedora虚拟机下的一个目录,我是拷贝到/nfs/kernel_test/gcov目录下,然后
(2)收集当前代码覆盖状态到一个文件lcov --directory . --output-file kernel.info
(3)获取HTML输出genhtml kernel.info使用web浏览器打开index.html文件查看代码覆盖结果。
上面是怎样获取内核运行代码覆盖率的一般方法及流程。
但如果我们只想获取一个程序运行时的内核代码覆盖率,改怎么办呢???
这里先说几个gcov-proc模块的特性:
(1)模块属性:
我们发现/sys/module/gcov_proc下有parameters文件夹,进去我们发现有两个属性文件:
/sys/module/gcov_proc/parameters # ls
gcov_linkgcov_persist
这两个属性是控制什么的呢???看看官方表述:
- gcov_link=0/1 (default is 1): When set to non-zero, symbolic links to
source and gcov graph files are created in /proc/gcov along with the data
files.
- gcov_persist=0/1 (default is 0): When set to non-zero, gcov data for
kernel modules is kept even after those modules are unloaded so that
coverage measurements can be extended to module cleanup code. To clear
this persistent data, write to /proc/vmlinux.
(2)重启内核覆盖率采集的数据
To reset coverage data for a specific file, simply write to the associated data
file in the /proc/gcov hierarchy:
echo 0 > /proc/gcov/kernel/signal.da
To reset all coverage data, write to the extra file '/proc/gcov/vmlinux':
echo 0 > /proc/gcov/vmlinux
四、获取程序运行时段的内核覆盖率
我的方法是在运行应用程序(这里面是以我的/nfs/memtest/timetest下的timetest应用程序为例)的开始先用“echo 0 > /proc/gcov/vmlinux”指令将我们的/proc/gcov下的统计信息全部清空让它重新计数的,下面说下我们的方法和步骤:
1、先获得一个含义gcov信息的内核和gcov-proc.ko,这个上面已经说过了;
2、启动linux内核,然后安装gcov-proc.ko
/memtest # insmod gcov-proc.ko
gcov-proc: initializing proc module: persist=0 link=1 format=gcc 3.4
gcov-proc: init done
/memtest # cd timetest/
/memtest/timetest # ls
20100106.logfp2timetesttimetest.ctimetest.gcno
20100111.logresultstimetest-lcovtimetest.gcda
3、这时先用“echo 0 > /proc/gcov/vmlinux”指令清空gcov,然后运行timetest,然后将
/proc/gcov拷贝到/tmp下面,这是防止直接拷到虚拟机下会产生大量的网络函数调用,增加统计误差。
/memtest/timetest # echo 0 > /proc/gcov/vmlinux && ./timetest && cp -r /proc/gcov/ /tmp
game over count1 is 0xc9413e40,count2 is 0x0,count3 is 0x3c8b1e45,count4 is 0x0
/memtest/timetest # ls /tmp
gcov
4、现在统计的数据是放在/tp/gcov下面的,我们需要将之拷贝到上位机的虚拟机中才能分析,因为我们的lcov只能在虚拟机中才能运行的。
/memtest/timetest # cp -r /tmp/gcov/ ./
/memtest/timetest #
5、现在需要转到虚拟机下接着运行lcov来产生最后的html页面了。
[root@localhost timetest]# cd gcov/
[root@localhost gcov]# lcov --directory . --capture --output-file timetest.info
[root@localhost gcov]# genhtml -o results timetest.info
这样就生成了最后基本上只运行在timetest时间段的内核代码覆盖率了。