代码覆盖率分析(gcov)(转)

上一篇 / 下一篇  2008-08-11 12:48:40 / 个人分类:SoftTest

我的栏目
  • 栏目:

为什么需要代码覆盖率分析?
T[6D~[4J.a0
h*w^U;KK c }#n1C*O0在发布代码的时候,我们常常会对其进行一系列的测试来协调软件的性能和功能,使他们和预计的相同。但是检验通常都是相当的困难,即使程序相当的简单。开发者常常会借助一些测试工具(test suite)来模拟或者重建执行脚本。如果测试程序组是彻底的,那么程序的各个功能都将被测试到并且都可以证明是可以工作的。
A!^0TZ:}HB051Testing软件测试网Jr$W.I2M
但是怎样才算彻底呢?简单点说就是测试程序的每一条路径,验证每一个结果,执行每一条语句,证明没一句语句是没用的。gcov就是一个用来检验你的每一句语句是否都执行了的工具。51Testing软件测试网(H;]:n ^!z+{"~4f{.]
51Testing软件测试网p z&I&R;Y8o/m4Em
什么是代码覆盖率分析?
MU P_B kp0代码覆盖率分析就是找到定位没用的或者不执行的代码的过程。没用的代码不会存在什么问题,但是他们会影响程序的可读性;不执行的代码则可能是未来bug的所在。所以找到他们,把他们从你的程序中移处是大有裨益的。
RY9D({UHBVn0覆盖率分析主要有下面的几个过程:51Testing软件测试网3^'}K"ap&E*V$n
    通过测试程序组找到不执行的程序段;51Testing软件测试网FKs4o'?0J9P%k'_ s
    添加额外测试程序组,以便增加代码覆盖率;
L ?NR G0    决定代码覆盖率的定量测度,他也是程序质量的间接测度。51Testing软件测试网C5O5P_s.v

代码覆盖率分析的缺陷51Testing软件测试网 n~9}Jt)fF `
代码覆盖率分析不能找出程序的逻辑错误。考虑一下下面的代码51Testing软件测试网"RLs.a$}$X
10:  rc = call_to_xx ();
sW#h|ANEU011:  if (rc == ERROR_FATAL)51Testing软件测试网$K E3KT%Tq
12:    exit(2);    /* exit with error code 2 */51Testing软件测试网_Ik/uZh'P![
13:  else51Testing软件测试网 Vkx1m#P*`[:Yc,l
14:    /* continue on */
xC f(xHPr0当测试程序段运行到11行时,11行始终都不能为真。call_to_xx返回了另外的一个错误比如ERROR_HANDLE,除非我们加入这种错误的处理方式的代码。51Testing软件测试网5]m:IV,B-P#l]
代码覆盖测试工具不会告诉你什么是必须的,他们只能显示已经存在的代码的覆盖率。51Testing软件测试网`2Cojd#~J}

代码覆盖率的类型
l\2D |Yw0gcov可以用来测量各种形式的代码覆盖率。最常见最有用的两种是分支覆盖(branch coverage)和循环覆盖(loop coverage)51Testing软件测试网y9`5v2Q;K]
分支覆盖证明各个方向的每一条分支都被执行到了。循环覆盖试图证明循环内部的每一条路径都被测试到了。循环覆盖似乎非常的复杂,但基本上只要满足下面的三个状况,就可以作了。
+Umq+v xKgS&J#d0    1。循环条件不满足,循环没有内部没有执行;
JzBg4kZ5i]3M.j f0    2。循环条件就满足了一次,循环内部就执行了一次;51Testing软件测试网Sd7B7JKm
    3。循环条件至少满足了两次,循环至少执行了两次。
1Z1^@JSZ3k4|0举个例子51Testing软件测试网g5a){!M#s-T)w a@7PF
void  function(int number)51Testing软件测试网,a.\~N_z~ b.E
{51Testing软件测试网ac-j~4f
  if (number % 2) == 0)
9_3~ t7|z\@8Z0    printf("even \n");
6c-]~4M:X.k-ox'R6@0  for (;number < 9; number++){51Testing软件测试网0|z[7M(c'sS!};`#j
    printf("number is %d\n", number);51Testing软件测试网(CjTbZ+e}{I a
  }
2lOql9k p4a,WU0}
F}3h?%J3p;H051Testing软件测试网i*uA0BY)G
function(11);   满足状况一51Testing软件测试网t%zp"{j3s~p g#rI
function(8);    满足状况二
.n'Jzr8T5J)q0function(6);    满足状况三   
*YX'y(j)Do8{0
代码覆盖率工具gcov的使用
1?o/l\~P4z%P0要使用gcov,需要在我们用gcc编译程序时加入两个参数fprofile-arcsftest-coverage.
S-\ g)h\0fprofile-arcs参数使gcc创建一个程序的流图,之后找到适合图的生成树。只有不在生成树中的弧被操纵(instrumented):gcc添加了代码来清点这些弧执行的次数。当这段弧是一个块的唯一出口或入口时,操纵工具代码(instrumentation code)将会添加到块中,否则创建一个基础块来包含操纵工具代码。
V8?qFVJ h8J'|"A0gcov主要使用.gcno和.gcda两个文件51Testing软件测试网~g|K(i1r
.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。51Testing软件测试网'Y1y{XwJ9MDyKr
.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。51Testing软件测试网;xS!_$^n9n z|j
51Testing软件测试网C'd$deoI^
下面是一个简要的范例:
N9S#w5u!t*i0  1 #include <stdlib.h>51Testing软件测试网4bio0w!f b
  2 #include <stdio.h>
5mJ4OwT0TZSNv4_+{0  3
m6vs%b5M g J%S&qH1M0  4 int main(int argc,char** argv)51Testing软件测试网#EV3Cw/T%}'I
  5 {51Testing软件测试网U pvU,|B \
  6     int x,y;51Testing软件测试网 U*^o2b0B/U]5NM
  7     int arraysize;
-PWQc#f f#mVnG0  8     int **array;
Y,Sd G9kf0  9
D(lr[1BTVUa L0 10     if(argc!=2)
1B_A/^I _7Ut(x0 11     {51Testing软件测试网 ^5C;b2W9JNF
 12         printf("Usage: %s Enter arraysize value\n;",argv[0]);
qU9Pp5Gy/j`\0 13         exit(-1);
-T7h3ZkW,g`M0 14     }
0U!X8a)RD8O!R0 15     else
1{D6`/Uo{G$e0 16     {51Testing软件测试网k:Xf%C {+^2e
 17         arraysize = atoi(argv[1]);51Testing软件测试网vk$a8z k+Oe
 18         if(arraysize <=0)
w;KH~t0 19         {51Testing软件测试网bT"[ \&i
 20             printf("Array size must be larger than 0\n;");51Testing软件测试网j2X A6h5j6];]0[M+x7l
 21             exit(-1);51Testing软件测试网(p[B0n8F,GQX \
 22         }51Testing软件测试网Q pw;HC h
 23     }51Testing软件测试网x-b[D p A\U
 2451Testing软件测试网4a$Oo4DR@.r_
 25     array = (int**) malloc( arraysize*sizeof(int*));51Testing软件测试网C#|_#D^ m,n
 2651Testing软件测试网 e x4n7mH
 27     printf("Creating an %d by %d array \n",arraysize,arraysize);
7uwkr4RZ SU0 28
5J'{/\7E9v&De]y;K0 29     if(array == NULL)51Testing软件测试网7t+`hh)r'x[
 30     {51Testing软件测试网.nn ztA'BO)q
 31         printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网0y ?$M_M"u,hB
 32         exit(-1);
5}$fxt+b~6M!{dJ f0 33     }51Testing软件测试网So#@5l*K@ E'?
 34
5Ao Rvz]2v0 35     for (x=0;x<arraysize;x++)
%lG y3s$C ` C7f+fw0 36     {
{[ oO_\,_5o8{0 37         array[x] = (int*) malloc (arraysize*sizeof(int));
-U7_r?qxca hD0 38
a_+IN_x Ky0 39         if(array[x] == NULL)
,{o6S.P}2wz:g0 40         {
&b@2gJ P-|0 41             printf("Failed malloc for array size %d\n",arraysize);
z6I,Ps%K0 42             exit(-1);
K(r%gVv2@%]0 43         }51Testing软件测试网.l?N,x+D$k
 44     }51Testing软件测试网6I5N#u:v'a4jEZ:}
 45
#j-l4Jp4T7rV A"[h6t0 46     exit(0);
G(fZ"Q3p/C Snw0 47 }51Testing软件测试网1Y u Y3dIFn8]
51Testing软件测试网#kY(I@E @ta
$ gcc -fprofile-arcs -ftest-coverage -g -o sample test.c
_Dw$c{3|5}&wfn0$ ./sample 1051Testing软件测试网h Ff,nS4]+d(C,O
Creating an 10 by 10 array51Testing软件测试网Qy7Oc U7B.\!B
 $ gcov test.c51Testing软件测试网H!A0p_(e0q"I@/C
File ''/usr/include/sys/sysmacros.h''
8AB,|+Ukmn&h4B U2|P0Lines executed:0.00% of 6
x)tm)I:gD0/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''51Testing软件测试网3Z"zZiEf/[!u

Q6tt x'VPHu\0File ''test.c''
5j F_6Dy*Gs6T_:a0Lines executed:57.89% of 1951Testing软件测试网 r]zQ.j @ G
test.c:creating ''test.c.gcov''
RkA |A*Q8jB5Fwn0$ cat test.c.gcov51Testing软件测试网g?CR7tK|wb
        -:    0:Source:test.c51Testing软件测试网s^#^ v$N+o
        -:    0:Graph:test.gcno
"prQ3{ F A6m]]0        -:    0:Data:test.gcda
3C"{8YN2]M!Y v0        -:    0:Runs:1
pon&\l$goe0        -:    0:Programs:151Testing软件测试网v/E)J:y3Nj L

-:    1:#include <stdlib.h>51Testing软件测试网Bh#b9Z6Y
        -:    2:#include <stdio.h>
_'d5{GV)I k0        -:    3:51Testing软件测试网*u!p g!q;xO ?]
        -:    4:int main(int argc,char** argv)
:wA'}rJY$\/B7}!n7p0        1:    5:{
)V h6l+K,B6Ql0        -:    6:        int x,y;51Testing软件测试网0F d uaj'HT
        -:    7:        int arraysize;
V|!}#CKpH0        -:    8:        int **array;51Testing软件测试网]"@cv}k
        -:    9:
a,H\#|Tf%\/p X0        1:   10:        if(argc!=2)51Testing软件测试网Z1b)B,B(bL#}
        -:   11:        {
K5]I:KNf0    #####:   12:                printf("Usage: %s Enter arraysize value;\n",argv[0]);
g\!vXI/vN0    #####:   13:                exit(-1);51Testing软件测试网3FTo%q?|%e2R`3E+~K#F
        -:   14:        }
aMNzG}n:E0        -:   15:        else
ch#hEy8O5yij;k0        -:   16:        {
Y%M+O \?t0        1:   17:                arraysize = atoi(argv[1]);51Testing软件测试网0wW)f'j:IHf1a
        1:   18:                if(arraysize <=0)
]e}m'?&C0        -:   19:                {51Testing软件测试网2OSp L.Yg*^2y
    #####:   20:                        printf("Array size must be larger than 0;\n");51Testing软件测试网9@g:aO#hJs
    #####:   21:                        exit(-1);
+s!i({$v^9RT0        -:   22:                }
t+ujL#UIH-W0        -:   23:        }51Testing软件测试网5i5_IB J}/^
        -:   24:
Ux#^?e4S:Oe0        1:   25:        array = (int**) malloc( arraysize*sizeof(int*));51Testing软件测试网dG l)T8Nj:z
        -:   26:
F$L)mNBBX.y0        1:   27:        printf("Creating an %d by %d array \n",arraysize,arraysize);
Q c.qud8V s'u,a0        -:   28:
Eoli&e(QiO I0        1:   29:        if(array == NULL)
J/OO&e!{:mR0        -:   30:        {
:mWN[j0    #####:   31:                printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网#r V%|hp |1z \
    #####:   32:                exit(-1);51Testing软件测试网]'{Q!je,k
        -:   33:        }51Testing软件测试网G;h|y Z |M
        -:   34:
jP}@tK0       11:   35:        for (x=0;x<arraysize;x++)51Testing软件测试网Ch*RFXO5UX
        -:   36:        {51Testing软件测试网^2P%Z!\#z'[:i
       10:   37:                array[x] = (int*) malloc (arraysize*sizeof(int));51Testing软件测试网t#QO;C m]%G"C
        -:   38:51Testing软件测试网\?{*t'd#L*ur
       10:   39:                if(array[x] == NULL)51Testing软件测试网g m%].p-x^
        -:   40:                {51Testing软件测试网2fnbk*Q#F^$CI
    #####:   41:                        printf("Failed malloc for array size %d\n",arraysize);
v1{y3uu!I'IOOl0    #####:   42:                        exit(-1);51Testing软件测试网;U(d,Rgf+QM/a\h
        -:   43:                }51Testing软件测试网zds4rDS&C:aZ;fca
        -:   44:        }
J!fa7i(Hf J7r z0        -:   45:
2UFvzJv0        1:   46:&n51Testing软件测试网d5i]q{I
     51Testing软件测试网D Zh$H)M

51Testing软件测试网Ph/CQ V\ Icc

bsp;       exit(0);
7K*C-n,~'p9PN4c0        -:   47:}51Testing软件测试网4pOEH8uxAS\
51Testing软件测试网uW7Yw"@O
正如我们看到的,我们现在的覆盖率是57.89%,#####后面的语句是现在没有够执行到的。
:Y+Q}tf&k M V0
o&p[]s&m(O+|\z%Y0$ ./sample51Testing软件测试网$Qq2c!foX
Usage: ./sample Enter arraysize value;
h_"_(l8m7yM0$ gcov test.c51Testing软件测试网4J[B$@u4CI0P$g
File ''/usr/include/sys/sysmacros.h''51Testing软件测试网-}2QB o,{(]
Lines executed:0.00% of 6
9c(~"h _0jt1N*C#DO0/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''
9]9[r+Qc Ub051Testing软件测试网 |+? dIhNy
File ''test.c''
(E(l'i4_ }$Y n.Wy0Lines executed:68.42% of 19
)y o"DAUMn0test.c:creating ''test.c.gcov''
9D[ O e:C*?US0现在运行了没有参数的情况,这种情况占了10.53%(68.42-57.89)51Testing软件测试网'j%[I#n3Q&k'Y
51Testing软件测试网pp7m8e1x/~L`
$ ./sample 051Testing软件测试网9~.DY2e3{f
Array size must be larger than 0;
]*IK$hqaJ)S#V0$ gcov test.c51Testing软件测试网a{8x5~L(fG
File ''/usr/include/sys/sysmacros.h''51Testing软件测试网7nT2Zd;zqf;IA
Lines executed:0.00% of 6
M,Qri%^?O)\"@0/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''
cV6M%^(Q~.x-gY0
!Vm9AF,lDnsA0File ''test.c''
&a ]+x.e*JG1l0Lines executed:78.95% of 1951Testing软件测试网` f{ F_/oEB
test.c:creating ''test.c.gcov''
.o | US0G6_a0有测试了一种情况,那就是arraysize=0现在的覆盖率已经达到了78.95%51Testing软件测试网C&a!to r

*Fv\3e7tK#U9P|y0还有两种情况,那就是二维数组的第一维和第二维内存空间的申请了。在现在的状况下,malloc()不太可能分配失败,所以我们借助工具gdb来模拟malloc失败时的情况。51Testing软件测试网;dH%qr U
$ gdb sample
-eH&f^&uc0GNU gdb Red Hat Linux (6.6-16.fc7rh)
B7D#q|'k{S0Copyright (C) 2006 Free Software Foundation, Inc.51Testing软件测试网E_8zOd8H7S R,`
GDB is free software, covered by the GNU General Public License, and you are
{)Hq1C#\/|V0welcome to change it and/or distribute copies of it under certain conditions.
cA{9v1n0Type "show copying" to see the conditions.
+w~Se{.P(e0There is absolutely no warranty for GDB.  Type "show warranty" for details.51Testing软件测试网+I r8E0RDP0\XS"p
This GDB was configured as "i386-redhat-linux-gnu"...
B\X Tyj8[cV^'W0Using host libthread_db library "/lib/libthread_db.so.1".
;oMg{4Zng0(gdb) l
z\8Obm`01       #include <stdlib.h>51Testing软件测试网E$m~gF
2       #include <stdio.h>51Testing软件测试网_iQw^.b7z5k
351Testing软件测试网*p5Q ^r|.@
4       int main(int argc,char** argv)51Testing软件测试网7fmjLa:Ud:M
5       {
-@ o.@hq9e ^ID06               int x,y;
d`\ Vth,dDG07               int arraysize;51Testing软件测试网1D2^5n9U4d8\
8               int **array;51Testing软件测试网;iIr\[,i(ij_,\
9
1Rm ^,@4w1E5I010              if(argc!=2)
N:~] q#O O&P3R B0(gdb) l
F't&w:{1U7f8A]Q011              {
m Fv*{Yb&`;|:V012                      printf("Usage: %s Enter arraysize value\n;",argv[0]);
q}V&g:^P*ynb:cF013                      exit(-1);
*^^;Q"C|&y omI014              }51Testing软件测试网 `Q]%D"j u
15              else
0z u7n8E.N-{"Kc016              {51Testing软件测试网I{ d [ i+ey T
17                      arraysize = atoi(argv[1]);51Testing软件测试网] k2}GX,m}T
18                      if(arraysize <=0)51Testing软件测试网 H j.Y:Ys/{t
19                      {51Testing软件测试网^] L_M J c
20                              printf("Array size must be larger than 0\n;");
m&P5NL5Iz)e GE0(gdb) l51Testing软件测试网yA{~}4l\'a@1sW^
21                              exit(-1);
%_(v8I,x3B022                      }51Testing软件测试网y EW.}P;Z0W
23              }
)J p6JY{$V02451Testing软件测试网I)`7F7K^ v
25              array = (int**) malloc( arraysize*sizeof(int*));51Testing软件测试网9b{8j1jbbW
26
J[l;l:B:l|EC027              printf("Creating an %d by %d array \n",arraysize,arraysize);
o]FEd!X[OY028
/}J'^M e ~ ~029              if(array == NULL)51Testing软件测试网(@'z\%i(u
30              {
&mikZdh L me(y0(gdb) b 29
3{-A,vA T Viq Ws`0Breakpoint 1 at 0x8048ada: file test.c, line 29.51Testing软件测试网k/xI:Fa
(gdb) r 1051Testing软件测试网(D1r^8c$S-m&Z
Starting program: /home/secularbird/Templates/test/sample 1051Testing软件测试网W7O-a^uX?I
Creating an 10 by 10 array51Testing软件测试网bOs+U#T

P#T*d IU#j N)S0Breakpoint 1, main (argc=2, argv=0xbf9b59f4) at test.c:2951Testing软件测试网LSfv[
29              if(array == NULL)
w6E7B4^ F!W0(gdb) print array51Testing软件测试网 NGQ(e.j2{ l*p
$1 = (int **) 0x90af00851Testing软件测试网X+^y0Af4dSyi
(gdb) set array=0
Z*B:]T@b+dO#s/Ia2~0(gdb) print array51Testing软件测试网d Z:Eg1D"\:x
$2 = (int **) 0x051Testing软件测试网 j(k&F?j3e!P0g:?j
(gdb) step
}7UV e"\ v}031                      printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网;z(im7x3W1^9V-j$Z
(gdb) cont
XB5MV.y_1Tm0Continuing.
FH]U~ kmr0Malloc failed for array size 1051Testing软件测试网.A0Gq3}Q.r
51Testing软件测试网,t }eo0h}(X)S
Program exited with code 0377.51Testing软件测试网)v v"T!Eo7w1e
(gdb) quit
2N4VL*\bT+D!\0l命令是list的简写,显示了程序的源代码。51Testing软件测试网)g'k!H [x!O.H1}!c
b 29是在29行设置一个断点,之所以这样设置是因为array空间申请过了,我们为了模拟需要改变它的值。
/}%n)V8G n/cg0r 10是run 10,也就是运行程序,10是传递的命令行参数。
makgP5[!t0print array打印array的地址51Testing软件测试网 C(SK;|"H.`'q c
set array=0将array指向空地址51Testing软件测试网$j3a ])d5|jqG&m
step一步一步的运行程序,简写是s51Testing软件测试网 HZ3H `-S7BZ1Jy
cont继续运行程序51Testing软件测试网:[yjc(o3KYLmk
quit退出gdb。
\}o%N6q ? x-k051Testing软件测试网Gc @C.@ s-O._2y+y
接下来做类似的工作产生另一个malloc fail。51Testing软件测试网!Q\OOgkas:` E
$ gdb sample
5pZu*G$UA"ic*Mt I0GNU gdb Red Hat Linux (6.6-16.fc7rh)51Testing软件测试网%v\cp0l4r O
Copyright (C) 2006 Free Software Foundation, Inc.
"`&C9s*O NoF0GDB is free software, covered by the GNU General Public License, and you are
4G;dlm T.} ^Q0welcome to change it and/or distribute copies of it under certain conditions.51Testing软件测试网 M9ral h-Sz
Type "sh ow copying" to see the conditions.
%m ptFjje}Z0There is absolutely no warranty for GDB.  Type "show warranty" for details.
Jm X-Lrd0This GDB was configured as "i386-redhat-linux-gnu"...
!h,_?!X T9kP/]"tv r0Using host libthread_db library "/lib/libthread_db.so.1".51Testing软件测试网/A Oz`@B
(gdb) l51Testing软件测试网"d9tS,yM^i+JVK[
1       #include <stdlib.h>
:| dU5QN x3m o[6BF02       #include <stdio.h>51Testing软件测试网&t8OGE9E
3
;Ub:U5Og+n04       int main(int argc,char** argv)
p4X%}$l,zu6g{8I'IJ_05       {
E#i0Tt^G"i06               int x,y;
Jdh s AE07               int arraysize;
L+an:q lm08               int **array;51Testing软件测试网[WoRoB]J8?$|
951Testing软件测试网;M&O^6`0U+Xg)[
10              if(argc!=2)51Testing软件测试网n6l$AnBf{W]
(gdb) l
u\+{hLp ] n011              {51Testing软件测试网NW.n5v5rld
12                      printf("Usage: %s Enter arraysize value\n;",argv[0]);
(L5xur0D8Ssy013                      exit(-1);51Testing软件测试网a-CM g y@(MI `N
14              }
-V/ntC9Zb015              else51Testing软件测试网Bl/g2D;Cz
16              {51Testing软件测试网vpz/_ C
17                      arraysize = atoi(argv[1]);51Testing软件测试网#`.[hJR\0G.Z
18                      if(arraysize <=0)
(Xb%J u}019                      {
2o.Q3aJK wR6V*w020                              printf("Array size must be larger than 0\n;");
MYmRUR ~0(gdb) l
+L$D_*D@9q6C/N021                              exit(-1);
M5[$h}iJ`!HK@+d022                      }51Testing软件测试网Zz)W+Q EV
23              }
;Mo:]%e#|.K k{024
*a6n;_Dm G025              array = (int**) malloc( arraysize*sizeof(int*));
!Er8m$R6Pam$H |!D02651Testing软件测试网,NpCD4_VE ]"a!?(cu
27              printf("Creating an %d by %d array \n",arraysize,arraysize);51Testing软件测试网%u,h"\en4}$fVm
2851Testing软件测试网F9\&xwo G
29              if(array == NULL)
~TB^e ?d _$g'H030              {51Testing软件测试网1r OsoH
(gdb) l
fe5Ho-o`V'lM,R+oG031                      printf("Malloc failed for array size %d \n",arraysize);51Testing软件测试网 Tp!MG@
32                      exit(-1);51Testing软件测试网/T!["r\5h+zg
33              }51Testing软件测试网&bz!~h%^
34
4a5J.g7Y6l:j4X!Q5T035              for (x=0;x<arraysize;x++)51Testing软件测试网D1W@2{^E ]
36              {51Testing软件测试网1Hq.X{ Bf
37                      array[x] = (int*) malloc (arraysize*sizeof(int));
oxhCt03851Testing软件测试网qy |!y f+F4J%jhT
39                      if(array[x] == NULL)51Testing软件测试网} y't#m@(kZ N
40                      {51Testing软件测试网 y[TS{
(gdb) b 39
E&NvI%df"t!D0Breakpoint 1 at 0x8048b7a: file test.c, line 39.51Testing软件测试网q)iS4~9Gf
(gdb) r 10
9s$bby*P ?2S0Starting program: /home/secularbird/Templates/test/sample 1051Testing软件测试网lE)Y G k8^'{ W&|%T
Creating an 10 by 10 array51Testing软件测试网4g8mg*?$Dt?+]:dR-}'S0F y
51Testing软件测试网k] p.fM dw[
Breakpoint 1, main (argc=2, argv=0xbfdcae04) at test.c:39
;e9j c$m3z p+`._sq039                      if(array[x] == NULL)
H4}.b7j!^$Jp6sS0(gdb) print array[0]
-QY,O'Lx(Cf0$1 = (int *) 0x9bc203851Testing软件测试网m jlDM M$~EM Q
(gdb) set array[0]=051Testing软件测试网2Y]*CUO$AeJ
(gdb) step
3tox*D%yE%| }'I:@/D }7@041                              printf("Failed malloc for array size %d\n",arraysize);
.\*H/cx;@@s0(gdb) cont51Testing软件测试网t5y6Mhr
Continuing.51Testing软件测试网4X'PY*N/j.tbE
Failed malloc for array size 1051Testing软件测试网;W:N"~9[Nmo;S
51Testing软件测试网!jXt7e7_n
Program exited with code 0377.51Testing软件测试网 X _~_x9z5Wa
(gdb) quit51Testing软件测试网 QU x!L$w5il ` {
51Testing软件测试网 bt4q4W(U!{[4S
$ gcov test.c
$N'qz4TKY^,U_w5P+z&C0File ''/usr/include/sys/sysmacros.h''51Testing软件测试网1x5d4u(z,of-S;Dz
Lines executed:0.00% of 651Testing软件测试网[+}KLZ$t(S&kzE,S
/usr/include/sys/sysmacros.h:creating ''sysmacros.h.gcov''51Testing软件测试网m0jv}A-BZd3k
51Testing软件测试网] N zk#K3W
File ''test.c''51Testing软件测试网-p m.rxf!r
Lines executed:100.00% of 19
BVIUaA vp0test.c:creating ''test.c.gcov''
p6z}@"ZsX"_I;k0到现在为止每条路径都测试过了,代码覆盖率也已经达到了100%,也可以查看一下test.c.gcov文件51Testing软件测试网6k+cwq7Jo0rgT
里面已经没有#####了。
[rbO:fie051Testing软件测试网e,^$RJh2fo0}
参考资料:
g,H"O K ^@0H4xv0Linux® Debugging and Performance Tuning: Tips and Techniques By Steve Best
E;Z El@%jT0man gcov51Testing软件测试网0a~W D)J&PY;A0k
http://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html#Gcov-Data-Files
cWI4oC/b~7C0
+C{$T;N$\L0

$F?y GP2S0

TAG: Linux Codec SoftTest

 

评分:0

我来说两句

Open Toolbar