Home -> Unix Programming Tools

gcov is a standard Linux code coverage testing tool. This tool lets you see how many times each line of your code has been executed since it was compiled. The purpose of this is to help you develop better testing cases by looking for any areas of your code that have not been reached by your tests.

Example output of gcov:

kandrea@zeus-1:~$ gcov c_test
File 'c_test.c'
Lines executed:80.00% of 5
Creating 'c_test.c.gcov'
kandrea@zeus-1:~$
	  
Each execution of the program is tracked and every line of code visited is counted. This information is only updated by running the gcov tool, which will generate the above output and an accompanying file, as shown below:
kandrea@zeus-1:~$ cat c_test.c.gcov 
       -:    0:Source:c_test.c
       -:    0:Graph:c_test.gcno
       -:    0:Data:c_test.gcda
       -:    0:Runs:3
       -:    0:Programs:1
       -:    1:#include 
       -:    2:
       3:    3:int main(int argc, char **argv) {
       3:    4:  if(argc == 1) {
       2:    5:    printf("No Args!\n");
       -:    6:  } else {
       1:    7:    printf("Args!\n");
       -:    8:  }
       3:    9:  return 0;
       -:   10:}
kandrea@zeus-1:~$ 
	  
This file shows that the program being tested has been run three times since it was compiled. During those three runs, line 3 (a call to main) and the first if statement were each run 3 times, indicating they were executed during each test of this program.