Introduction to gcov: C/C++ Code Coverage Tool and Instrumentation Process
This article explains gcov, the GCC‑bundled C/C++ code coverage tool, covering its basic concepts, the workflow for generating coverage data, and the instrumentation technique using basic blocks and arcs to record execution counts.
Gcov is a code coverage testing tool for C/C++ that ships with GCC and can measure statement, function, and branch coverage.
The coverage generation process involves compiling with the –ftest-coverage option, which inserts instrumentation code after each executable statement, produces .gcno files containing basic block and line information, and at program exit writes execution counts to .gcda files via gcov_init and gcov_exit .
Instrumentation works by using basic blocks (BB) and arcs (ARC) to build a program flow graph; each BB is a sequence of statements ending with a jump, and each ARC represents a transition between BBs. Counting BB and ARC executions allows reconstruction of statement and branch execution frequencies.
The data structures used to record BB and ARC information include a struct definition:
struct bb {
long zero_word; // whether inserted into list
const char *file_name; // source file name
long *count; // pointer to BX2
long ncounts; // number of instrumentation points
struct bb *next; // next file's BX2 info
};During compilation GCC adds a static BX2 array at the end of each source file to store execution counts for each instrumentation point, inserts assembly increments for each point, and links all BX2 arrays into a global list that is processed before main() and finalized on program exit to produce the .gcda files.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.