How do GCC code coverage features work?

Consider the following command:

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

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

+6
source share
3 answers

.gcda is not generated by the compiler; it is generated by your program when it is executed.

.gcno is a file generated at compile time, and it is a β€œnotes file”. gcc generates a notes file for the main unit (.gcno) for each control unit (compiler unit).

So how is main.gcda generated?

During operation, statistics are collected and stored in memory. Some output feedback is registered and called to write data to the .gcda file when the program exits. This means that if you call abort () instead of exit () in your program, the .gcda file will not be created.

How is measuring equipment manufactured? Can I see the tool code?

You need to check the gcc implementation to get the details, but basically the toolkit is done by inserting the instruction into the program to count the number of times each instruction is executed. But you don’t really need 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. You only need to equip some special arcs, and from them you can create coverage for all branches of the code. You can parse the binary to see the tool code. And here are some files to cover if you want to look at the gcc source file:

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

edit: some known gcov FYI errors:

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

+12
source

Can I see the instrumented code?

You cannot see tools like gcda files.

How does gcov work?

GCOV works in four stages:

1. Code instrumentation during compilation

2. Data collection during code execution

3. Data extraction at program exit time

4. Coverage analysis and presentation post-mortem.

To learn more about the individual steps, you can view this pdf file.

http://ltp.sourceforge.net/documentation/technical_papers/gcov-ols2003.pdf

+3
source

You can see which code related to gcov is used for the compilation executable or obj file, you can use the following steps.

 nm executable/objfile 

Below is an image of steps and output: -

gcov tool code steps

0
source

Source: https://habr.com/ru/post/956695/


All Articles