| 1 |
GCOV |
| 2 |
|
| 3 |
GCOV can be used to gather code coverage information. GCOV is a component of |
| 4 |
G++, and therefore can only be used on machines which use that compiler (this |
| 5 |
should not be an issue, as code coverage is machine-independent). In order to |
| 6 |
use GCOV, the configure script needs a few extra options. Under normal |
| 7 |
conditions, this should just be: |
| 8 |
|
| 9 |
$./configure CXXFLAGS="-fprofile-arcs -ftest-coverage" [other options] |
| 10 |
|
| 11 |
If the configure script does not automatically detect G++, you may have to |
| 12 |
specify it as an option: |
| 13 |
|
| 14 |
$./configure CXX=g++ CXXFLAGS="-fprofile-arcs -ftest-coverage" [other options] |
| 15 |
|
| 16 |
Also, note that it is a good idea to remove optimization options via "-O0" |
| 17 |
(i.e., dash-oh-zero). Optimization can change the accuracy of the coverage |
| 18 |
information. |
| 19 |
|
| 20 |
Then do: |
| 21 |
|
| 22 |
$make |
| 23 |
|
| 24 |
and optionally: |
| 25 |
|
| 26 |
$make install |
| 27 |
|
| 28 |
Now, run whatever functionality tests you wish to run. In theory, this should |
| 29 |
be every test you have. Time lapses and test order do not matter. If you are |
| 30 |
not testing the entirety of Trilinos, it may also be a good idea to run tests |
| 31 |
for packages that depend on the packages you are testing, as these external |
| 32 |
tests may test your packages in ways that your tests do not. Note that since |
| 33 |
GCOV will eventually be part of the daily or weekly tests, it would be a good |
| 34 |
idea for these functionality tests to reflect what is on the CVS repository |
| 35 |
(whether that means updating your files or the repository's). |
| 36 |
|
| 37 |
Unfortunately, GCOV by itself cannot work with multiple files at once. |
| 38 |
However, there is a Perl script (Trilinos/test/coverage/gcovmerge) which |
| 39 |
automates the process of running GCOV over a directory and summarizing the |
| 40 |
resulting information. In order for the latest version of gcovmerge to work, |
| 41 |
your machine must have GCC 3.4.3 or later. |
| 42 |
|
| 43 |
At minimum, gcovmerge takes one argument: the path to search for .gcda files |
| 44 |
(which indicate files that have been compiled and tested as explained above). |
| 45 |
The following arguments affect the coverage testing (these should come before |
| 46 |
the path): |
| 47 |
--with-files=min-max [0-100] |
| 48 |
When specified, no coverage information is displayed for |
| 49 |
any individual files with a coverage percent outside of |
| 50 |
the range given. |
| 51 |
|
| 52 |
--with-directories |
| 53 |
When specified, coverage percentages are displayed for |
| 54 |
each directory examined. |
| 55 |
|
| 56 |
--without-files |
| 57 |
No coverage information will be given for individual files. |
| 58 |
|
| 59 |
--exclude=word [] |
| 60 |
No files with word in their name or path will be examined. |
| 61 |
This option may be specified more than once, in order to |
| 62 |
exclude a variety of files. |
| 63 |
|
| 64 |
--with-total |
| 65 |
The total coverage for all files examined will be displayed. |
| 66 |
|
| 67 |
--source-dir=path |
| 68 |
The path to look for source files if the source and build |
| 69 |
directories are seperate. This directory should have an identical |
| 70 |
hierarchy to that of build_dir, or else this script will not be |
| 71 |
able to reconcile the data in source_dir with that in build_dir. |
| 72 |
If not specified, the script will assume that the source files |
| 73 |
are in build-dir. |
| 74 |
|
| 75 |
--with-gcov=GCOV |
| 76 |
Execute GCOV instead of "gcov". This is useful for using a |
| 77 |
different version of gcov than is located in the user path, |
| 78 |
or one with a different name (e.g., gcov-4.0) |
| 79 |
|
| 80 |
--old-school |
| 81 |
Revert to previous (ver 1.5) behavior of gcovmerge. Coverage |
| 82 |
information will be generated from a single executable per |
| 83 |
source file. This methodology results in lower and less accurate |
| 84 |
coverage numbers. |
| 85 |
|
| 86 |
--with-count |
| 87 |
Specifies that coverage should be reported as "n of t", instead of |
| 88 |
the percentage. |
| 89 |
|
| 90 |
--with-percent |
| 91 |
Specifies that coverage should be reported as a percentage. |
| 92 |
If neither --with-count nor --with-percent are specified, |
| 93 |
then --with-percent is the default. |
| 94 |
|
| 95 |
--with-report |
| 96 |
--with-report=REP |
| 97 |
Specifies that a report should be generated. |
| 98 |
This report will produce a listing of the |
| 99 |
all source files whose coverage was analyzed, |
| 100 |
wherein the lines of code are displayed as either |
| 101 |
covered, not-covered, or not compiled. |
| 102 |
If --with-report is given without the REP |
| 103 |
option, then the filename for the report |
| 104 |
will be coverage.html. |
| 105 |
--with-full-paths |
| 106 |
Outputs full pathnames instead of abbrieviated file names. |
| 107 |
|
| 108 |
Running gcovmerge with the default options from the Trilinos level will most |
| 109 |
likely generate too much information to really use. Specifying the search path |
| 110 |
and the options will tone this down to something more useful. For example, to |
| 111 |
check the coverage percentages on all source in the EPetra package (but not |
| 112 |
tests, documentation, or examples), one would do this: |
| 113 |
|
| 114 |
$./commonTools/test/coverage/gcovmerge --exclude=test/ --exclude=example/ \ |
| 115 |
--exclude=doc/ --source-dir=./packages/epetra --with-gcov=/opt/gcc-3.4.4/bin/gcov \ |
| 116 |
./BUILD_DIR/packages/epetra |
| 117 |
|
| 118 |
To get a summary of the coverage on each package, one could do this: |
| 119 |
|
| 120 |
$./commonTools/test/coverage/gcovmerge --without-files \ |
| 121 |
--with-directories --exclude=test/ --exclude=example/ \ |
| 122 |
--exclude=examples/ --exclude=doc/ --source-dir=./packages \ |
| 123 |
--with-gcov=/opt/gcc-3.4.4/bin/gcov \ |
| 124 |
./BUILD_DIR/packages |
| 125 |
|
| 126 |
In theory, every package should have 100% test coverage. This, however, is not |
| 127 |
a practical goal. A more realistic goal is for every source file to have 85% |
| 128 |
test coverage. |
| 129 |
|
| 130 |
gcov and gcovmerge are not perfect. It is impossible to run gcov on particular |
| 131 |
source files -- this could happen if a source file is a templated header that |
| 132 |
was left completely untested, or if a source file in the requested source-dir |
| 133 |
was never compiled. In such cases, these source files will not be reported. |
| 134 |
|
| 135 |
If you want to see specific coverage information for any particular file, you |
| 136 |
should use GCOV directly: |
| 137 |
|
| 138 |
$gcov dotofile.o |
| 139 |
100.00% of 1 lines executed in file /usr/lib/.../ansilib |
| 140 |
Creating ansilib.gcov. |
| 141 |
80.00% of 10 lines executed in file ../.../dotofile.cpp |
| 142 |
Creating dotofile.cpp.gcov |
| 143 |
|
| 144 |
(Note that this command must be run from the same directory as g++ was invoked. |
| 145 |
When following the configure/make/make install pattern, this would normally be |
| 146 |
the directory that contains dotofile.o.) |
| 147 |
|
| 148 |
Obviously, the information regarding library files is quite useless, so it will |
| 149 |
be ignored here. |
| 150 |
|
| 151 |
The .gcov files are recreations of the original source files, with extra |
| 152 |
annotation to indicate how often certain lines have been executed: |
| 153 |
|
| 154 |
$cat dotofile.cpp.gcov |
| 155 |
-: 0:Source:dotofile.cpp |
| 156 |
-: 0:Object:dotofile.bb |
| 157 |
-: 1:#include <iostream> |
| 158 |
-: 2:using namespace std; |
| 159 |
-: 3: |
| 160 |
1: 4:main(){ |
| 161 |
1: 6: int num; |
| 162 |
1: 7: cout << "Please enter a number:\n"; |
| 163 |
1: 8: cin >> num; |
| 164 |
-: 9: //comment |
| 165 |
4: 10: for(int i = 2; |
| 166 |
-: 11: i < num; |
| 167 |
-: 12: i++){ |
| 168 |
4: 13: if(num % i == 0){ |
| 169 |
1: 14: cout << num << " is divisible by << i << ".\n"; |
| 170 |
1: 15: exit(0); |
| 171 |
-: 16: } |
| 172 |
-: 17: } |
| 173 |
#####: 18: cout << num << " is prime.\n"; |
| 174 |
-: 19:} |
| 175 |
|
| 176 |
The first column indicates how many times each line was executed (- means that |
| 177 |
the line is not executable and ##### means the line was never executed). The |
| 178 |
second is the line number in the code (lines 0 are for gcov's use). The |
| 179 |
remainder of the file is the actual source. |