Gcov
lastmod 2021-01-29created 2016-01-157 edits
gcov is a coverage testing program. Using gcov together with GNU CC lets you run analyses for making faster and more efficient program code. gcov can be used as a profiling tool to decide which parts of the program code are the best choices to optimize, and when used together with gprof and similar tools, it can find which parts of the program code take up the most time in actual computation, such as arithmetic operations, excluding data I/O processing.
Profiling tools help analyze the performance of program code, and a profiler such as gprof gives the following basic statistics.
- How many times each line of the code is executed
- Which lines of the code are actually executed
- The computation time spent by specific parts of the code
With the information above, you can check how the written code behaves through the compilation process and then decide which parts to optimize. gcov helps you decide which parts should be optimized.
Software developers use tests such as unit tests together with coverage analysis to check whether the developed software performs well enough to be released. These tests can verify that the program works as expected, while the coverage program is used to see how the program behaves under the tests. Based on the results, developers can improve their tests and decide which test items or features should be added to improve the product being built.
If you want to use gcov, you must not use GNU CC optimization options when compiling. This is because when code is compiled with optimization, several lines can be merged into one function, so you cannot get much information for finding the parts that take a lot of computation time when the program runs. Also, because gcov computes statistics with the line as the smallest unit, it gives the best results with a programming style that puts one statement per line. If you use complex macros that expand into loops or control statements, gcov's statistics, which only report the line where the macro is called, are not very helpful. When complex macros are used like functions, you can solve this problem by rewriting them as inline functions instead of macros.
For the source file sourcefile.c', gcov generates a log file,sourcefile.gcov', that tells how many times each of its lines was executed, and this log file can be used together with gprof to tune the program for better performance. In addition to the information from gcov, gprof can also provide timing information.
gcov works only on code compiled with GNU CC and is not compatible with other kinds of profiling or test coverage methods.
How to use
Add the following options to GCC.
-
CFLAGS:-fprofile-arcs-ftest-coverage -
LFLAGS:-lgcov--coverage
Trubleshooting
undefined reference __gcov_init
If a symbol such as __gcov_init cannot be found, check whether -lgcov has been added to the linker options.