Brief introduction:

Gcov is the exact count of the number of times each statement is executed in a source code coverage analysis and statement analysis tool Gcov generator program Gcov is available as a standard program option -fprofile-arcs in the GNU Compiler Collection (GCC) suite -ftest-coverage is used to compile the program for coverage analysis (the first option is used to record branch statistics, the second option is used to save line execution counts)

Function:

Profiling tools such as Perf, Gprof and LCOV can be used to generate visual, graphical coverage reports: Consists of a set of Perl scripts built on top of text-based Gcov output to implement code coverage file output in HTML format

Note:

Since Gcov accumulates statistics on a per-line basis, it suits a programming style where there is only one program statement per line. If macros are used and macros behave like functions, statistics are less useful. Gcov only works on code compiled using GCC

Workflow:

  1. Before compiling, add the following parameters to the CMake organization project:
if(USE_GCOV) message(STATUS "using gcov") SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage") SET(GCC_COVERAGE_LINK_FLAGS "-lgcov") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") //--> Target_link_libraries (binaryname gcov) endif(USE_GCOV)Copy the code
  1. Specified when the makefile is generated using CMakeUSE_GCOVE:

cmake -DUSE_GCOV=1

  1. Generated after compilation.gcnoFile, and.gcdaThe file must execute the generated binary for it to be generated. The directories of the two files are usually in the binary directory, or in the case of CMakebuild/CMakeFile/binary-name/
  1. No kill process generation.gcdafile

Do not kill the process to generate. Gcda files

  1. Install the lcov

Coverage files can be generated by the yum installation or github installation process:

lcov -c -d . -o cover.info
Copy the code
  1. Merge files
lcov -a x.info -a y.info -o total.info
Copy the code
  1. Filtering information
Lcov --remove total.info '/ SRC /include/*' '/usr/bin/*' -o final.info (exclude include and bin coverage information)Copy the code
  1. Generating HTML files
Genhtml -o dir final.info //Copy the code

Note: -c: obtain coverage information. -d: gcda and gCno directories. -o: output files

Principle:

Source code after compilation and preprocessing, generate assembly file, in the generation of assembly file at the same time complete staking, directly inserted into the generated *.S file

Assembler assembler generates object files, generates executable files, and generates.gcno files

Execution binaries, during the execution of the insertion of the stake is responsible for statistical execution information.

Generate. Gcda file, processing to get code coverage statistics