How to do code coverage with Gcov tool
1. Prerequisites
GCC comes with a code coverage tool called gcov. Install required tools with sudo apt install gcc lcov.
2. Sample code
$ cat hello.c
#include <stdio.h>
void print_hello()
{
printf("Helloworld!!!");
return;
}
int main()
{
print_hello();
return 0;
}3. Compile source with --coverage or -fprofile-arcs -ftest-coverage flags
--coverage or -fprofile-arcs -ftest-coverage flagsThe .gcno record file is generated after adding the GCC compile option -ftest-coverage, which contains information for reconstructing the base block map and assigning source line numbers to blocks during the compilation process.
4. Run the user program
Run the user program to generate the .gcda file that contains the coverage data counts.
5. Run gcov tool to generate human readable coverage report
Run gcov hello.c to generate the .gcov text file.
The generated .gcov file looks like below
The .gcov file contains : separated fields along with program source code
in the format of <execution_count>:<line_number>:<source line text>.
A count of the number of times the given line was executed A
-indicates a line with no executable code (e.g. a declaration) A#####for lines which were never executedThe line number
The source code
6. Generating HTML reports
1. Generate the coverage.info data file
2. Generate a report from this data file
3. Open index.html file to browse the coverage report
index.html file to browse the coverage reportLast updated