GCC and Code Coverage

My only experience with generating coverage results has been through Gcov and Lcov. This post shows a simple example of how to obtain the results and present them in HTML.

1 – Write a simple piece of code

#include 

void Example_function(int number);

int main(int argc, char* argv[]){
    Example_function(3);
    return 0;
}

void Example_function(int number)
{
  if ((number % 2) == 0){
      printf("even \n");
  }

  for (;number < 9; number++){
    printf("number is %d\n", number);
  }
}

2 – Compile with the flags required for code coverage.

gcc -fprofile-arcs -ftest-coverage -g sample.cpp -o sample

This should generate a .gcno file that is used to generate the code coverage date when the tests are executed.

3 – Run the tests

./sample

The tests will run (hopefully pass!) and then you will have a .gcda file that contains details of the coverage results.

4 – Run Gcov against the source file

gcov -b sample.cpp

The -b option will also give you branch coverage details, but you can just run this command without the options.

Once run, you will have a .cpp.gcov file that can be read to show all of the coverage details (although not in the most presentable way).

cat sample.cpp.gcov
-:    0:Source:sample.cpp
-:    0:Graph:sample.gcno
-:    0:Data:sample.gcda
-:    0:Runs:1
-:    0:Programs:1
-:    1:#include
-:    2:
-:    3:void Example_function(int number);
-:    4:
function main called 1 returned 100% blocks executed 100%
1:    5:int main(int argc, char* argv[])
-:    6:{
1:    7:    Example_function(3);
call    0 returned 100%
-:    8:
1:    9:    return 0;
-:   10:}
-:   11:
function _Z16Example_functioni called 1 returned 100% blocks executed 83%
1:   12:void Example_function(int number)
-:   13:{
-:   14:
1:   15:  if ((number % 2) == 0)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
-:   16:  {
#####:   17:      printf("even \n");
call    0 never executed
-:   18:  }
-:   19:
13:   20:  for (;number < 9; number++){
branch  0 taken 86% (fallthrough)
branch  1 taken 14%
-:   21:
6:   22:    printf("number is %d\n", number);
call    0 returned 100%
-:   23:
-:   24:  }
-:   25:
1:   26:}
-:   27:
-:   28:

5. Install lcov (Ubuntu)

sudo apt install lcov

6. Run lcov

lcov --capture --directory [Location of gcda files] --output-file [Location to send lcov results]

7. Open index.html in your favorite browser

firefox index.html

Selection_002

8. External links

https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html#Invoking-Gcov

http://www.linux-mag.com/id/1409/

https://qiaomuf.wordpress.com/2011/05/26/use-gcov-and-lcov-to-know-your-test-coverage/

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: