gcov 简述   
               添加时间:2013-5-31 点击量: 
 
                      代码覆盖率是单位测试的一个指标,凡是覆盖率越高,单位测试就做得更完全。(然而,覆盖率是不是和软件质量成正比关系呢?)gcov是GNU对象链中的一个首要的对象,固然gcov是覆盖率很好的对象,然则gcov的更首要的应用是机能的调优。gcov经由过程把守法度的履行,从而断定某行代码有没有履行,履行了几许次。gcov的呈报是基于文本的格局的,看起来是斗劲丢脸点。然则,有个叫lcov的对象,将gcov的呈报格局转换为html的直观情势,后面介绍。
   
      gcov应用:
      如有以下代码:
        1:  #include <stdio.h>
     2:  &#160;
     3:  void bubbleSort( int list[], int size )
     4:  {
     5:      int i, j, temp, swap = 1;
     6:  &#160;
     7:      while (swap) {
     8:  &#160;
     9:          swap = 0;
    10:  &#160;
    11:          for ( i = (size-1) ; i >= 0 ; i-- ) {
    12:  &#160;
    13:              for ( j = 1 ; j <= i ; j++ ) {
    14:  &#160;
    15:                  if ( list[j-1] > list[j] ) {
    16:  &#160;
    17:                      temp = list[j-1];
    18:                      list[j-1] = list[j];
    19:                      list[j] = temp;
    20:                      swap = 1;
    21:  &#160;
    22:                  }
    23:  &#160;
    24:              }
    25:  &#160;
    26:          }
    27:  &#160;
    28:      }
    29:  &#160;
    30:  &#160;
    31:  }
    32:  &#160;
    33:  int main()
    34:  {
    35:      int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    36:      int i;
    37:  &#160;
    38:      / Invoke the bubble sort algorithm /
    39:      bubbleSort( theList, 10 );
    40:  &#160;
    41:      / Print out the final list /
    42:      for (i = 0 ; i < 10 ; i++) { 
    43:          printf("%d\n", theList[i]);
    44:      }
    45:      if(i == 0){
    46:          printf("i = 0\n");
    47:      }else{
    48:          printf("i != 0\n");
    49:      }
    50:  &#160;
    51:  }
&#160;&#160;&#160; 1.&#160; 编译法度是增长 -ftest-coverage -fprofile-arcs 选项。
[heidong@HEIDONGVM gcov]¥ gcc -o bbsort bbsort.c -ftest-coverage -fprofile-arcs
&#160;&#160;&#160; 生成.gcno文件。
&#160;&#160;&#160; 2. 履行法度,将生成.a文件,用gcov法度搜检响应的源代码文件,将生成成果文件。
[heidong@HEIDONGVM gcov]¥ ./bbsort 
  
1 
  
2 
  
3 
  
4 
  
5 
  
6 
  
7 
  
8 
  
9 
  
10 
  
i != 0 
  
[heidong@HEIDONGVM gcov]¥ gcov bbsort.c 
  
File‘bbsort.c’ 
  
已履行的行数:95.24% (共 21 行) 
  
bbsort.c:正在创建‘bbsort.c.gcov
&#160;&#160;&#160; 3. 搜检响应的成果文件
[heidong@HEIDONGVM gcov]¥ cat bbsort.c.gcov 
        -:    0:Source:bbsort.c
        -:    0:Graph:bbsort.gcno
        -:    0:Data:bbsort.a
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdio.h>
        -:    2:
        1:    3:void bubbleSort( int list[], int size )
        -:    4:{
        1:    5:    int i, j, temp, swap = 1;
        -:    6:
        4:    7:    while (swap) {
        -:    8:
        2:    9:        swap = 0;
        -:   10:
       22:   11:        for ( i = (size-1) ; i >= 0 ; i-- ) {
        -:   12:
      110:   13:            for ( j = 1 ; j <= i ; j++ ) {
        -:   14:
       90:   15:                if ( list[j-1] > list[j] ) {
        -:   16:
       45:   17:                    temp = list[j-1];
       45:   18:                    list[j-1] = list[j];
       45:   19:                    list[j] = temp;
       45:   20:                    swap = 1;
        -:   21:
        -:   22:                }
        -:   23:
        -:   24:            }
        -:   25:
        -:   26:        }
        -:   27:
        -:   28:    }
        -:   29:
        -:   30:
        1:   31:}
        -:   32:
        1:   33:int main()
        -:   34:{
        1:   35:    int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        -:   36:    int i;
        -:   37:
        -:   38:    / Invoke the bubble sort algorithm /
        1:   39:    bubbleSort( theList, 10 );
        -:   40:
        -:   41:    / Print out the final list /
       11:   42:    for (i = 0 ; i < 10 ; i++) { 
       10:   43:        printf("%d\n", theList[i]);
        -:   44:    }
        1:   45:    if(i == 0){
    #####:   46:        printf("i = 0\n");
        -:   47:    }else{
        1:   48:        printf("i != 0\n");
        -:   49:    }
        -:   50:
        1:   51:}
        -:   52:
[heidong@HEIDONGVM gcov]¥
&#160;
&#160;&#160;&#160; 可以看到某行履行了几许次,哪些行没有履行过(####标示)。gcov 还可以搜检其他很多的信息,如分支,函数等,具体参考gcov的帮助文档,并测试之。
&#160;
&#160;&#160;&#160; 对于文本格局,信赖很多人的感觉不是很直观,于是便有了lcov这个对象,它可以算是gcov的前端对象,如许生成html文件,可以很直观的看到代码覆盖景象。
&#160;&#160;&#160; lcov不是标准的unix/linux对象,须要,地址是:ltp.sourceforge.net/coverage/lcov.php 重视要翻墙才可以接见。
&#160;&#160;&#160; 应用体式格式:
&#160;&#160;&#160; 1. 履行完gcov的步调后,履行下列号令:
[heidong@HEIDONGVM gcov]¥ lcov --capture --directory ./&#160; --output-file bbsort.info 
  
Capturing coverage data  ./ 
  
Found gcov version: 4.4.6 
  
Scanning ./ for .a files ... 
  
Found 1 data files in ./ 
  
Processing bbsort.a 
  
Finished .info-file creation
&#160;&#160; 2. 生成html文件:
[heidong@HEIDONGVM gcov]¥ genhtml bbsort.info --output-directory ./lcov/ 
  
Reading data file bbsort.info 
  
Found 1 entries. 
  
Found common filename prefix "/home/heidong/tmp" 
  
Writing .css and .png files. 
  
Generating output. 
  
Processing file gcov/bbsort.c 
  
Writing directory view page. 
  
Overall coverage rate: 
  
&#160; lines......: 95.2% (20 of 21 lines) 
  
&#160; functions..: 100.0% (2 of 2 functions
&#160;
看下生成的html文件:
 
&#160;
&#160;&#160;&#160; 完毕。
无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》
                     
                  
     
  
 
    
    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; 代码覆盖率是单位测试的一个指标,凡是覆盖率越高,单位测试就做得更完全。(然而,覆盖率是不是和软件质量成正比关系呢?)gcov是GNU对象链中的一个首要的对象,固然gcov是覆盖率很好的对象,然则gcov的更首要的应用是机能的调优。gcov经由过程把守法度的履行,从而断定某行代码有没有履行,履行了几许次。gcov的呈报是基于文本的格局的,看起来是斗劲丢脸点。然则,有个叫lcov的对象,将gcov的呈报格局转换为html的直观情势,后面介绍。
&#160;
&#160;&#160;&#160; gcov应用:
&#160;&#160;&#160; 如有以下代码:
   1:  #include <stdio.h>
   2:  &#160;
3: void bubbleSort( int list[], int size )
   4:  {
5: int i, j, temp, swap = 1;
   6:  &#160;
7: while (swap) {
   8:  &#160;
   9:          swap = 0;
  10:  &#160;
11: for ( i = (size-1) ; i >= 0 ; i-- ) {
  12:  &#160;
13: for ( j = 1 ; j <= i ; j++ ) {
  14:  &#160;
15: if ( list[j-1] > list[j] ) {
  16:  &#160;
  17:                      temp = list[j-1];
  18:                      list[j-1] = list[j];
  19:                      list[j] = temp;
  20:                      swap = 1;
  21:  &#160;
  22:                  }
  23:  &#160;
  24:              }
  25:  &#160;
  26:          }
  27:  &#160;
  28:      }
  29:  &#160;
  30:  &#160;
  31:  }
  32:  &#160;
33: int main()
  34:  {
35: int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
36: int i;
  37:  &#160;
38: / Invoke the bubble sort algorithm /
  39:      bubbleSort( theList, 10 );
  40:  &#160;
41: / Print out the final list /
42: for (i = 0 ; i < 10 ; i++) {
43: printf("%d\n", theList[i]);
  44:      }
45: if(i == 0){
46: printf("i = 0\n");
47: }else{
48: printf("i != 0\n");
  49:      }
  50:  &#160;
  51:  }
&#160;&#160;&#160; 1.&#160; 编译法度是增长 -ftest-coverage -fprofile-arcs 选项。
[heidong@HEIDONGVM gcov]¥ gcc -o bbsort bbsort.c -ftest-coverage -fprofile-arcs
&#160;&#160;&#160; 生成.gcno文件。
&#160;&#160;&#160; 2. 履行法度,将生成.a文件,用gcov法度搜检响应的源代码文件,将生成成果文件。
[heidong@HEIDONGVM gcov]¥ ./bbsort 
  
1 
  
2 
  
3 
  
4 
  
5 
  
6 
  
7 
  
8 
  
9 
  
10 
  
i != 0 
  
[heidong@HEIDONGVM gcov]¥ gcov bbsort.c 
  
File‘bbsort.c’ 
  
已履行的行数:95.24% (共 21 行) 
  
bbsort.c:正在创建‘bbsort.c.gcov
&#160;&#160;&#160; 3. 搜检响应的成果文件
[heidong@HEIDONGVM gcov]¥ cat bbsort.c.gcov
-: 0:Source:bbsort.c
-: 0:Graph:bbsort.gcno
-: 0:Data:bbsort.a
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
1: 3:void bubbleSort( int list[], int size )
-: 4:{
1: 5: int i, j, temp, swap = 1;
-: 6:
4: 7: while (swap) {
-: 8:
2: 9: swap = 0;
-: 10:
22: 11: for ( i = (size-1) ; i >= 0 ; i-- ) {
-: 12:
110: 13: for ( j = 1 ; j <= i ; j++ ) {
-: 14:
90: 15: if ( list[j-1] > list[j] ) {
-: 16:
45: 17: temp = list[j-1];
45: 18: list[j-1] = list[j];
45: 19: list[j] = temp;
45: 20: swap = 1;
-: 21:
-: 22: }
-: 23:
-: 24: }
-: 25:
-: 26: }
-: 27:
-: 28: }
-: 29:
-: 30:
1: 31:}
-: 32:
1: 33:int main()
-: 34:{
1: 35: int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
-: 36: int i;
-: 37:
-: 38: / Invoke the bubble sort algorithm /
1: 39: bubbleSort( theList, 10 );
-: 40:
-: 41: / Print out the final list /
11: 42: for (i = 0 ; i < 10 ; i++) {
10: 43: printf("%d\n", theList[i]);
-: 44: }
1: 45: if(i == 0){
#####: 46: printf("i = 0\n");
-: 47: }else{
1: 48: printf("i != 0\n");
-: 49: }
-: 50:
1: 51:}
-: 52:
[heidong@HEIDONGVM gcov]¥
&#160;
&#160;&#160;&#160; 可以看到某行履行了几许次,哪些行没有履行过(####标示)。gcov 还可以搜检其他很多的信息,如分支,函数等,具体参考gcov的帮助文档,并测试之。
&#160;
&#160;&#160;&#160; 对于文本格局,信赖很多人的感觉不是很直观,于是便有了lcov这个对象,它可以算是gcov的前端对象,如许生成html文件,可以很直观的看到代码覆盖景象。
&#160;&#160;&#160; lcov不是标准的unix/linux对象,须要,地址是:ltp.sourceforge.net/coverage/lcov.php 重视要翻墙才可以接见。
&#160;&#160;&#160; 应用体式格式:
&#160;&#160;&#160; 1. 履行完gcov的步调后,履行下列号令:
[heidong@HEIDONGVM gcov]¥ lcov --capture --directory ./&#160; --output-file bbsort.info 
  
Capturing coverage data  ./ 
  
Found gcov version: 4.4.6 
  
Scanning ./ for .a files ... 
  
Found 1 data files in ./ 
  
Processing bbsort.a 
  
Finished .info-file creation
&#160;&#160; 2. 生成html文件:
[heidong@HEIDONGVM gcov]¥ genhtml bbsort.info --output-directory ./lcov/ 
  
Reading data file bbsort.info 
  
Found 1 entries. 
  
Found common filename prefix "/home/heidong/tmp" 
  
Writing .css and .png files. 
  
Generating output. 
  
Processing file gcov/bbsort.c 
  
Writing directory view page. 
  
Overall coverage rate: 
  
&#160; lines......: 95.2% (20 of 21 lines) 
  
&#160; functions..: 100.0% (2 of 2 functions
&#160;
看下生成的html文件:
 
&#160;
&#160;&#160;&#160; 完毕。
无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》



