gcov/lcov查看代码运行覆盖率

代码覆盖率——gcov lcov的使用

一、关于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编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。
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

查看结果:
        -:   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、如何使用lcov
Makefile 在编译和link环节都加入 -fprofile-arcs -ftest-coverage 选项     

收集覆盖率数据生成app.info文件
命令:cov --directory .   --capture --output-file myapp.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

转换成html格式
命令:genhtml -o results app.info
Reading data 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%)

2、查看html文件
html包含代码覆盖的详细信息

更多命令选项

http://ltp.sourceforge.net/coverage/lcov/lcov.1.php?PHPSESSID=26d7173d1f492f5f691715ef8b7d0b40

参考资料
1、http://ltp.sourceforge.net/coverage/

 
 
 
android下native code用gcov:

业内有不少工具能够做代码覆盖率检查,并且还有详细的GUI的结果分析,可惜都是收费的。

现在在自己的项目里面简单地用gcov来进行检查。配置方法如下:

1. 代码编译时候加上两个CFLAG:

export XLOCAL_OPTIONAL_COVERAGE_CFLAGS = -fprofile-arcs -ftest-coverage

LOCAL_CFLAGS := $(XLOCAL_OPTIONAL_COVERAGE_CFLAGS)

2. 加上对gcov以及gcc库的链接:

export XLOCAL_OPTIONAL_COVERAGE_LINKFLAGS = -lgcov -lgcc

LOCAL_LDLIBS := $(XLOCAL_OPTIONAL_COVERAGE_LINKFLAGS)

3. 设置gcov文件的输出路径(通过c来设置,参考http://hi.baidu.com/bzrobert/blo ... 87f11f8a82a188.html):

changePath.setEnv(“GCOV_PREFIX”, “/sdcard/mycov”, 1);
changePath.setEnv(“GCOV_PREFIX_STRIP”, “14″, 1); // 14是我要去掉的目录级别,不要的话就不设置这个变量

4. 退出的时候在native调用exit(0),这时开始写结果文件到$(GCOV_PREFIX)

5.然后从/sdcard/mycov里面拷贝出所有.gcda文件到编译输出目录(r3和r4的ndk不同),另外也拷贝源码到一起,然后运行ndk里面的arm-eabi-gcov xxx.cpp即可得到xxx.cpp.gcov文件,打开就能看到覆盖结果,可惜要一个文件一个文件地看,比较麻烦,暂时不知道有没有图形前端可以批量处理并且查看结果。

转自:http://www.devdiv.net/bbs/thread-28888-1-1.html

你可能感兴趣的:(html,File,gcc,branch,output,Components)