3. Compile source with --coverage or -fprofile-arcs -ftest-coverage flags
$ cc -fprofile-arcs -ftest-coverage hello.c -o hello
The .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.
$ ls
total 32K
-rw-rw-r-- 1 shanmugk shanmugk 123 Dec 11 11:08 hello.c
-rw-rw-r-- 1 shanmugk shanmugk 608 Dec 11 11:08 hello.gcno
-rwxrwxr-x 1 shanmugk shanmugk 24K Dec 11 11:08 hello
4. Run the user program
Run the user program to generate the .gcda file that contains the coverage data counts.
$ ./hello
Helloworld!!!
$ ls
total 36K
-rw-rw-r-- 1 shanmugk shanmugk 123 Dec 11 11:08 hello.c
-rw-rw-r-- 1 shanmugk shanmugk 608 Dec 11 11:08 hello.gcno
-rwxrwxr-x 1 shanmugk shanmugk 24K Dec 11 11:08 hello
-rw-rw-r-- 1 shanmugk shanmugk 120 Dec 11 11:08 hello.gcda
5. Run gcov tool to generate human readable coverage report
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 executed
The line number
The source code
6. Generating HTML reports
1. Generate the coverage.info data file
$ lcov --capture --directory . --output-file coverage.info
Capturing coverage data from .
Found gcov version: 9.4.0
Using intermediate gcov format
Scanning . for .gcda files ...
Found 1 data files in .
Processing hello.gcda
Finished .info-file creation
2. Generate a report from this data file
$ genhtml coverage.info --output-directory __html
Reading data file coverage.info
Found 1 entries.
Found common filename prefix "/home/shanmugk/Dropbox/frameworks/gcov"
Writing .css and .png files.
Generating output.
Processing file hello/hello.c
Writing directory view page.
Overall coverage rate:
lines......: 100.0% (6 of 6 lines)
functions..: 100.0% (2 of 2 functions)
$ ls __html/
total 52K
-rw-rw-r-- 1 shanmugk shanmugk 117 Dec 11 11:22 updown.png
-rw-rw-r-- 1 shanmugk shanmugk 141 Dec 11 11:22 snow.png
-rw-rw-r-- 1 shanmugk shanmugk 141 Dec 11 11:22 ruby.png
-rw-rw-r-- 1 shanmugk shanmugk 167 Dec 11 11:22 glass.png
-rw-rw-r-- 1 shanmugk shanmugk 9.7K Dec 11 11:22 gcov.css
-rw-rw-r-- 1 shanmugk shanmugk 141 Dec 11 11:22 emerald.png
-rw-rw-r-- 1 shanmugk shanmugk 141 Dec 11 11:22 amber.png
-rw-rw-r-- 1 shanmugk shanmugk 3.6K Dec 11 11:22 index-sort-l.html
-rw-rw-r-- 1 shanmugk shanmugk 3.6K Dec 11 11:22 index-sort-f.html
-rw-rw-r-- 1 shanmugk shanmugk 3.6K Dec 11 11:22 index.html
drwxrwxr-x 2 shanmugk shanmugk 4.0K Dec 11 11:22 hello
3. Open index.html file to browse the coverage report