Sie sind auf Seite 1von 8

Understanding usage of GNU tools gcov and lcov

Test host sped13 (RHEL 6.0) with kernel version 2.6.32 is used for demonstration. gcov comes preinstalled as OS utility. lcov needs to be installed. Copy of a rpm lcov-1.9-1.noarch.rpm is kept at /toc/usr/nasimm/NFT/coverage The gcov and lcov tool works in conjunction with the GNU gcc compiler. To demonstrate the functionality of the tool let us consider a simple problem statement /requirement wherein we have to write a piece of code to identify whether a number is even or odd and greater than 10 or not. Here is the code sample.c file. Please do not bother about the efficiency or logic used. The source file can also be found at /toc/usr/nasimm/NFT/coverage/src.

/****************************************************************************** * The sample code to find whether the number is even or odd. Also will find * whether the number is greater than 10 or not. ******************************************************************************/ #include<stdio.h> #include<stdlib.h> int isEven(int number) { return (!(number % 2)); } int isOdd(int number) { return (number % 2); } int isGreaterThanTen(int number) { return (number >= 10); } int main (int argc, char *argv[]) { int number = 0; if (1 == argc) { printf("Please enter a number\n"); exit(1); } number = atoi(argv[1]); if (isEven(number)) { if (isGreaterThanTen(number)) { printf("The given number is } else { printf("The given number is } } else if (isOdd(number)) { if (isGreaterThanTen(number)) { printf("The given number is } else { printf("The given number is } } return 0; }

even and greater than 10\n");

even and less than 10\n");

odd and greater than 10\n");

odd and less than 10\n");

We also need to write Makefile using with different compilation options (fprofile-arcs & -ftest-coverage) to generate instrumented binary.
CC=gcc CPPFLAGS= -g -Wall -W -pedantic -fprofile-arcs -ftest-coverage COVLIB=-lgcov TARGETS = \ sample all: $(TARGETS)

sample: sample.c $(CC) $(CPPFLAGS) $(COVLIB) $@.c -o $@ clean: /bin/rm -f *.gcno *.gcov *.gcda $(TARGETS)

Build the binary


[root@sped13 src]# ls Makefile sample.c [root@sped13 src]# make gcc -g -Wall -W -pedantic -fprofile-arcs sample.c -o sample [root@sped13 src]# ls -l total 26 -rw-r--r--. 1 4294967294 4294967294 1231 -rw-r--r--. 1 4294967294 4294967294 237 -rw-r--r--. 1 4294967294 4294967294 1684 -rwxr-xr-x. 1 4294967294 4294967294 21499 [root@sped13 src]#

-ftest-coverage -lgcov

Jan Jan Jan Jan

12 12 12 12

11:30 14:24 15:16 15:16

sample.c Makefile sample.gcno sample

The flag -fprofile-arcs causes the compiler to generate additional code which counts the number of times each basic block is executed The flag -ftest-coverage results in additional files with .gcno suffixes which allow gcov to map these basic blocks onto lines of source code. Example: sample.gcno. Identify the test cases o Positive TCs Test input is even and less than 10. Test input is even and greater than 10. Test input is odd and less than 10. Test input is odd and greater than 10. o Negative TCs Run the application without test input.

Run the application for one of the TC.


[root@sped13 src]# ./sample 10 The given number is even and greater than [root@sped13 src]# ls -ltr total 27 -rw-r--r--. 1 4294967294 4294967294 1231 -rw-r--r--. 1 4294967294 4294967294 237 -rw-r--r--. 1 4294967294 4294967294 1684 -rwxr-xr-x. 1 4294967294 4294967294 21499 -rw-r--r--. 1 4294967294 4294967294 352 [root@sped13 src]# 10

Jan Jan Jan Jan Jan

12 12 12 12 12

11:30 14:24 15:16 15:16 15:40

sample.c Makefile sample.gcno sample sample.gcda

Running the application created a new file with extension .gcda. Run the application with for one of the TC. gcov will read in the basic block counts from the generated .gcda file. These basic blocks are mapped onto the source code using the information in the .gcno files. Measure the percentage of coverage in textual form using gcov tool.
[root@sped13 src]# gcov ./sample.c File 'sample.c' Lines executed:57.14% of 21 sample.c:creating 'sample.c.gcov' [root@sped13 src]# ls -ltr total 30 -rw-r--r--. 1 4294967294 4294967294 1231 Jan 12 -rw-r--r--. 1 4294967294 4294967294 237 Jan 12 -rw-r--r--. 1 4294967294 4294967294 1684 Jan 12 -rwxr-xr-x. 1 4294967294 4294967294 21499 Jan 12 -rw-r--r--. 1 4294967294 4294967294 352 Jan 12 -rw-r--r--. 1 4294967294 4294967294 2340 Jan 12 [root@sped13 src]#

11:30 14:24 15:16 15:16 15:40 15:47

sample.c Makefile sample.gcno sample sample.gcda sample.c.gcov

A new file with extension .gcov is generated. This file contains the information about the functions/lines hit. Subsequent run of the application with different TC will show an improvement in coverage. This is demonstrated below. We can notice the coverage file is also updated, thus accumulating the result of subsequent run.
[root@sped13 src]# ./sample 13 The given number is odd and greater than 10 [root@sped13 src]# gcov ./sample.c File 'sample.c' Lines executed:80.95% of 21 sample.c:creating 'sample.c.gcov' [root@sped13 src]# ls -ltr total 30 -rw-r--r--. 1 4294967294 4294967294 1231 Jan 12 -rw-r--r--. 1 4294967294 4294967294 237 Jan 12 -rw-r--r--. 1 4294967294 4294967294 1684 Jan 12 -rwxr-xr-x. 1 4294967294 4294967294 21499 Jan 12 -rw-r--r--. 1 4294967294 4294967294 352 Jan 12 -rw-r--r--. 1 4294967294 4294967294 2340 Jan 12 [root@sped13 src]#

11:30 14:24 15:16 15:16 16:12 16:12

sample.c Makefile sample.gcno sample sample.gcda sample.c.gcov

Generate the lcov file with extension .info.


[root@sped13 src]# lcov -d . -c -o sample.info Capturing coverage data from . Found gcov version: 4.4.4 Scanning . for .gcda files ... Found 1 data files in . Processing sample.gcda Finished .info-file creation [root@sped13 src]# ls -ltr total 28 -rw-r--r--. 1 4294967294 4294967294 1231 Jan 12 -rw-r--r--. 1 4294967294 4294967294 237 Jan 12 -rw-r--r--. 1 4294967294 4294967294 1684 Jan 12 -rwxr-xr-x. 1 4294967294 4294967294 21499 Jan 12 -rw-r--r--. 1 4294967294 4294967294 352 Jan 12 -rw-r--r--. 1 4294967294 4294967294 534 Jan 12 [root@sped13 src]#

11:30 14:24 15:16 15:16 16:12 16:22

sample.c Makefile sample.gcno sample sample.gcda sample.info

Generate the HTML file for viewing the coverage result through web interface.
[root@sped13 src]# genhtml sample.info Reading data file sample.info Found 1 entries. Found common filename prefix "/toc/usr/nasimm/NFT" Writing .css and .png files. Generating output. Processing file coverage/src/sample.c Writing directory view page. Overall coverage rate: lines......: 81.0% (17 of 21 lines) functions..: 100.0% (4 of 4 functions) branches...: 60.0% (6 of 10 branches) [root@sped13 src]# ls -ltr total 65 -rw-r--r--. 1 4294967294 4294967294 1231 Jan 12 11:30 -rw-r--r--. 1 4294967294 4294967294 237 Jan 12 14:24 -rw-r--r--. 1 4294967294 4294967294 1684 Jan 12 15:16 -rwxr-xr-x. 1 4294967294 4294967294 21499 Jan 12 15:16 -rw-r--r--. 1 4294967294 4294967294 352 Jan 12 16:12 -rw-r--r--. 1 4294967294 4294967294 534 Jan 12 16:22 -rw-r--r--. 1 4294967294 4294967294 9893 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 141 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 117 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 167 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 141 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 141 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 141 Jan 12 16:28 drwxr-xr-x. 3 4294967294 4294967294 512 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 4404 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 4397 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 4397 Jan 12 16:28 -rw-r--r--. 1 4294967294 4294967294 4397 Jan 12 16:28 [root@sped13 src]#

sample.c Makefile sample.gcno sample sample.gcda sample.info gcov.css emerald.png updown.png glass.png ruby.png snow.png amber.png coverage index.html index-sort-l.html index-sort-f.html index-sort-b.html

Enjoy the view. Overall result.

Functions hit.

Lines hit.

Lets us achieve 100% of coverage by executing remaining TCs.


[root@sped13 src]# ./sample 2 The given number is even and less than 10 [root@sped13 src]# ./sample 3 The given number is odd and less than 10 [root@sped13 src]# ./sample Please enter a number [root@sped13 src]# gcov sample.c File 'sample.c' Lines executed:100.00% of 21 sample.c:creating 'sample.c.gcov' [root@sped13 src]#

And regenerate the html files with updated result. I am still trying to figure out why am I not getting 100% branch coverage????
[root@sped13 src]# lcov -d . -c -o sample.info Capturing coverage data from . Found gcov version: 4.4.4 Scanning . for .gcda files ... Found 1 data files in . Processing sample.gcda Finished .info-file creation [root@sped13 src]# genhtml sample.info Reading data file sample.info Found 1 entries. Found common filename prefix "/toc/usr/nasimm/NFT" Writing .css and .png files. Generating output. Processing file coverage/src/sample.c Writing directory view page. Overall coverage rate: lines......: 100.0% (21 of 21 lines) functions..: 100.0% (4 of 4 functions) branches...: 90.0% (9 of 10 branches) [root@sped13 src]#

Updated result through web interface.

Updated view of functions hit.

Das könnte Ihnen auch gefallen