Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the code coverage options of GCC work?

Consider the following command:

gcc -fprofile-arcs -ftest-coverage main.c

It generates the file, main.gcda, which is to be used by gcov, to generate the coverage analysis. So how does main.gcda is generated? How the instrumentation is done? Can I see the instrumented code?

like image 686
Karthik Avatar asked Oct 25 '13 06:10

Karthik


People also ask

How does gcov work in Linux?

gcov is a test coverage program. Use it in concert with GCC to analyze your programs to help create more efficient, faster running code and to discover untested parts of your program. You can use gcov as a profiling tool to help discover where your optimization efforts will best affect your code.

What is LCOV code coverage?

LCOV is a graphical tool for GCC's coverage testing with gcov. It creates HTML pages containing the source code annotated with coverage information by collecting gcov data from multiple source files. LCOV supports “Lines coverage” and “Functions coverage” measurement.

How are Gcda files generated?

gcda file is generated when a program containing object files built with the GCC -fprofile-arcs option is executed. A separate . gcda file is created for each object file compiled with this option. It contains arc transition counts, and some summary information.


1 Answers

.gcda is not generated by compiler; it's generated by your program when you execute it.

.gcno is the file generated at compilation time and it is the 'note file'. gcc generate a basic block graph notes file (.gcno) for each CU(compiler unit).

So how does main.gcda is generated?

At running time the statistic data is gathered and stored in memory. Some exit callback is registered and is called to write the data to the .gcda file when the program terminates. This means if you call abort() instead of exit() in your program, no .gcda file would be generated.

How the instrumentation is done? Can I see the instrumented code?

You way need check gcc's implementation to get the details but basically the instrumentation is done by inserting instruction to the program to count the number of times each instruction is executed. But it doesn't really have to keep a counter for each instruction; GCC uses some algorithm to generate a program flow graph and finds a spanning tree for the graph. Only some special arcs have to be instrumented and from them the coverage of all code branches can be generated. You can disassemble the binary to see the instrumented code. And here are some files for coverage if you want to look into the gcc source file:

toplev.c coverage.c profile.c libgcov.c gcov.c gcov-io.c

edit: some known gcov bugs FYI:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49484

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28441

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44779

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7970

like image 187
tristan Avatar answered Nov 16 '22 03:11

tristan