Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
26 views21 pages

Gcov Final

Uploaded by

riataghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views21 pages

Gcov Final

Uploaded by

riataghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Code coverage tools

Learning objectives

• At the end of this lesson students will able to:


• Explain the use of coverage reporting tools
• Use gcov to generate program coverage
reporting
• Use gprof for profiling
What is the purpose of the profiling
tools?
• Profiling tools help you analyze your code’s
performance.
• How often each line of code executes
• What lines of code are actually executed
• How much computing time each section of
code uses.
Relevance
• Testing is important to ensure code reliability .
• Based on these profiling report optimization is
done on the code.
Gcov
• A tool that comes with gcc to help with code
coverage and profiling.
Gcov provides the following details:
• How often each line of code executes
• What lines of code are actually executed
• How much computing time each section of
code uses
• gcov is a source code coverage analysis and
statement-by-statement profiling tool.
• gcov generates exact counts of the number
of times each statement in a program has
been executed
• gcov does not produce any time-based data
(you should use gprof for this purpose) and
works only on code compiled with the GCC
suite.
Steps
• The .gcda and .gcno files are derived from the
original object file.
• These files contain coverage and profile data
stored in a platform-independent format
• .gcno – flow graph
• .gcda –profile output
How to use
• When using gcov, you must first compile your
program with two
special GCC options: -fprofile-arcs
-ftest-coverage.
• This tells the compiler to generate additional
information needed by gcov (basically a flow
graph of the program) and also includes additional
code in the object files for generating the extra
profiling information needed by gcov.
• These additional files are placed in the directory
where the object file is located.
How?
• For example, if your program is called tmp.c,
this is what you see when you use the
basic gcov facility:
• $ gcc -fprofile-arcs -ftest-coverage tmp.c
• $ a.out
• $ gcov tmp.c
• 90.00% of 10 source lines executed in file
tmp.c Creating tmp.c.gcov.
• gcov creates a logfile called sourcefile.gcov
which indicates how many times each line of a
source file sourcefile.c has executed.
EXAMPLE
• #include<stdio.h>
• int main() {
• int cond = 1;
• if ( cond ) {
• printf("Condition is true...!\n"); }
• else { printf("Condition is false...!\n");
• return(1); }
• printf("End of program\n");
• return(0);}

• Compile the program using gcc
• $ gcc -fprofile-arcs -ftest-coverage -o Example
Example.c

• This will create .gcno file in addition to the


executable.
• $ ls
• Example Example.c Example.gcno

Run the executable
• $ ./Example
• Condition is true...!
• End of program
• This will create .gcda file
• $ ls
• Example Example.c Example.gcda
Example.gcno
Now gcov can be run to get the
coverage data.
• $ gcov -b -c Example.c
• File 'Example.c‘
• Lines executed:75.00% of 8
• Branches executed:100.00% of 2
• Taken at least once:50.00% of 2
• Calls executed:66.67% of 3
• Example.c:creating 'Example.c.gcov'
• $ cat Example.c.gcov
• -: 0:Source:Example.c
• -: 0:Graph:Example.gcno
• -: 0:Data:Example.gcda
• -: 0:Runs:1
• -: 0:Programs:1
• -: 1:#include
• -: 2:
• -: 3:int main()
• function main called 1 returned 100% blocks executed 71%
• 1: 4:{
• 1: 5: int cond = 1;
• -: 6:
• 1: 7: if ( cond ) {
• branch 0 taken 1 (fallthrough)
• branch 1 taken 0
• 1: 8: printf("Condition is true...!\n");
• call 0 returned 1
• -: 9: } else {
• #####: 10: printf("Condition is false...!\n");
• call 0 never executed
• #####: 11: return(1);
• -: 12: }
• -: 13:
• 1: 14: printf("End of program\n");
• call 0 returned 1
• -: 15:
• 1: 16: return(0);
• -: 17:}
Interesting points
• The execution counts are cumulative.
• If the program is executed again without
removing the .gcda file, the count for the number
of times each line in the source is executed are
added to the results of the previous run(s).
• • This is useful in several ways. For example, it can
be used to accumulate data over a number of
program runs as part of a test verification suite .
• The data in the .gcda files is saved
immediately before the program exits.
• For each source file compiled with
-fprofile-arcs, the profiling code first attempts to
read in an existing .gcda file; if the file doesn't
match the executable (differing number of basic
block counts) it will ignore the contents of the
file. It then adds in the new execution counts
and finally writes the data to the file.
Gcov with all options
• gcov [-v|--version] [-h|--help]
[-a|--all-blocks]
[-b|--branch-probabilities]
[-c|--branch-counts]
[-n|--no-output]
[-l|--long-file-names]
[-p|--preserve-paths]
[-f|--function-summaries]
[-o|--object-directory directory|file] sourcefile
[-u|--unconditional-branches]
1. Given prg1.c as below, Use gcov on this piece of code and
explain the result line by line. How can you spot a line that has
never been executed?
#include <stdio.h>
int main (void)
{
int i, total; total = 0;
for (i = 0; i < 100; i++)
total += i;
if (total = 450)
printf ("Failure\n");
else
printf ("Success\n");
return 0;
}

You might also like