GNU Toolchain —— (五)gcov与lcov入门

gcov 是一个可用于C/C++的代码覆盖工具,是gcc 的内建工具。下面介绍一下如何利用gcov 来收集代码覆盖信息。
想要用gcov 收集代码覆盖信息,需要在gcc 编译代码的时候加上这2个选项 “-fprofile-arcs -ftest-coverage”,把这个简单的程序编译一下

gcc -fprofile-arcs -ftest-coverage hello.c -o hello

编译后会得到一个可执行文件hello和hello.gcno文件,当用gcc编译文件的时候,如果带有“-ftest-coverage”参数,就会生成这个.gcno文件,它包含了程序块和行号等信息

note:这里有一个问题要注意的就是-fprofile-arcs -ftest-coverage必须同时传给编译器和链接器(makefile里面可以加在 CFLAGS 和 LDFLAGS上),如果连接的时候出现undefined reference to `__gcov_init' 错误,则还要加上-lgocv


接下来可以运行这个hello的程序

./hello 5
./hello 12

运行结束以后会生成一个hello.gcda文件,如果一个可执行文件带有“-fprofile-arcs”参数编译出来,并且运行过至少一次,就会生成。这个文件包含了程序基本块跳转的信息。接下来可以用gcov生成代码覆盖信息:

gcov hello.c

运行结束以后会生成2个文件hello.c.gcov和myfunc.c.gcov。打开看里面的信息:

-: 0:Source:myfunc.c
-: 0:Graph:hello.gcno
-: 0:Data:hello.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include
-: 2:
-: 3:void test(int count)
1: 4:{
-: 5: int i;
10: 6: for (i = 1; i < count; i++)
-: 7: {
9: 8: if (i % 3 == 0)
3: 9: printf (“%d is divisible by 3 /n”, i);
9: 10: if (i % 11 == 0)
#####: 11: printf (“%d is divisible by 11 /n”, i);
9: 12: if (i % 13 == 0)
#####: 13: printf (“%d is divisible by 13 /n”, i);
-: 14: }
1: 15:}

gcov的各个参数使用如下:    
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 ,可以用程序解析这些晦涩的字符,最终输出成html格式的报告,很好吧!

lcov -d . -t ‘Hello test’ -o ‘hello_test.info’ -b . -c

指定lcov在当前目录“.”去找代码覆盖的信息,输出为’hello_test.info’ ,这个hello_test.info是一个中间结果,需要把它用genhtml来处理一下,genhtml是lcov里面的一个工具。

genhtml -o result hello_test.info

指定输出目录是 result。一个完整的html报告就生成了,做一个连接,把这个目录连到随便一个web server的目录下,就可以看报告了。

 

测试结果概览

具体某个文件的覆盖率

GNU Toolchain —— (五)gcov与lcov入门_第1张图片

gcov和lcov基本上能满足测试过程中收集代码覆盖率信息的需求,不过有个遗憾就是gcov不能收集.so文件的代码覆盖信息。

 

Related posts:

  1. 分享两个PPT,关于gcov和lcov,还有代码覆盖
  2. Python代码覆盖工具coverage.py介绍
  3. 解决gcov不能生成.gcda文件,以及其他错误

 

转自  http://magustest.com/blog/whiteboxtesting/using-gcov-lcov/

 

 

 

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