llvm-cov.rst 14.9 KB

llvm-cov - emit coverage information

SYNOPSIS

:program:`llvm-cov` command [args...]

DESCRIPTION

The :program:`llvm-cov` tool shows code coverage information for programs that are instrumented to emit profile data. It can be used to work with gcov-style coverage or with clang's instrumentation based profiling.

If the program is invoked with a base name of gcov, it will behave as if the :program:`llvm-cov gcov` command were called. Otherwise, a command should be provided.

COMMANDS

GCOV COMMAND

SYNOPSIS

:program:`llvm-cov gcov` [options] SOURCEFILE

DESCRIPTION

The :program:`llvm-cov gcov` tool reads code coverage data files and displays the coverage information for a specified source file. It is compatible with the gcov tool from version 4.2 of GCC and may also be compatible with some later versions of gcov.

To use :program:`llvm-cov gcov`, you must first build an instrumented version of your application that collects coverage data as it runs. Compile with the -fprofile-arcs and -ftest-coverage options to add the instrumentation. (Alternatively, you can use the --coverage option, which includes both of those other options.) You should compile with debugging information (-g) and without optimization (-O0); otherwise, the coverage data cannot be accurately mapped back to the source code.

At the time you compile the instrumented code, a .gcno data file will be generated for each object file. These .gcno files contain half of the coverage data. The other half of the data comes from .gcda files that are generated when you run the instrumented program, with a separate .gcda file for each object file. Each time you run the program, the execution counts are summed into any existing .gcda files, so be sure to remove any old files if you do not want their contents to be included.

By default, the .gcda files are written into the same directory as the object files, but you can override that by setting the GCOV_PREFIX and GCOV_PREFIX_STRIP environment variables. The GCOV_PREFIX_STRIP variable specifies a number of directory components to be removed from the start of the absolute path to the object file directory. After stripping those directories, the prefix from the GCOV_PREFIX variable is added. These environment variables allow you to run the instrumented program on a machine where the original object file directories are not accessible, but you will then need to copy the .gcda files back to the object file directories where :program:`llvm-cov gcov` expects to find them.

Once you have generated the coverage data files, run :program:`llvm-cov gcov` for each main source file where you want to examine the coverage results. This should be run from the same directory where you previously ran the compiler. The results for the specified source file are written to a file named by appending a .gcov suffix. A separate output file is also created for each file included by the main source file, also with a .gcov suffix added.

The basic content of an .gcov output file is a copy of the source file with an execution count and line number prepended to every line. The execution count is shown as - if a line does not contain any executable code. If a line contains code but that code was never executed, the count is displayed as #####.

OPTIONS

EXIT STATUS

:program:`llvm-cov gcov` returns 1 if it cannot read input files. Otherwise, it exits with zero.

SHOW COMMAND

SYNOPSIS

:program:`llvm-cov show` [options] -instr-profile PROFILE BIN [-object BIN,...] [[-object BIN]] [SOURCES]

DESCRIPTION

The :program:`llvm-cov show` command shows line by line coverage of the binaries BIN,... using the profile data PROFILE. It can optionally be filtered to only show the coverage for the files listed in SOURCES.

BIN may be an executable, object file, dynamic library, or archive (thin or otherwise).

To use :program:`llvm-cov show`, you need a program that is compiled with instrumentation to emit profile and coverage data. To build such a program with clang use the -fprofile-instr-generate and -fcoverage-mapping flags. If linking with the clang driver, pass -fprofile-instr-generate to the link stage to make sure the necessary runtime libraries are linked in.

The coverage information is stored in the built executable or library itself, and this is what you should pass to :program:`llvm-cov show` as a BIN argument. The profile data is generated by running this instrumented program normally. When the program exits it will write out a raw profile file, typically called default.profraw, which can be converted to a format that is suitable for the PROFILE argument using the :program:`llvm-profdata merge` tool.

OPTIONS

REPORT COMMAND

SYNOPSIS

:program:`llvm-cov report` [options] -instr-profile PROFILE BIN [-object BIN,...] [[-object BIN]] [SOURCES]

DESCRIPTION

The :program:`llvm-cov report` command displays a summary of the coverage of the binaries BIN,... using the profile data PROFILE. It can optionally be filtered to only show the coverage for the files listed in SOURCES.

BIN may be an executable, object file, dynamic library, or archive (thin or otherwise).

If no source files are provided, a summary line is printed for each file in the coverage data. If any files are provided, summaries can be shown for each function in the listed files if the -show-functions option is enabled.

For information on compiling programs for coverage and generating profile data, see :ref:`llvm-cov-show`.

OPTIONS

EXPORT COMMAND

SYNOPSIS

:program:`llvm-cov export` [options] -instr-profile PROFILE BIN [-object BIN,...] [[-object BIN]] [SOURCES]

DESCRIPTION

The :program:`llvm-cov export` command exports coverage data of the binaries BIN,... using the profile data PROFILE in either JSON or lcov trace file format.

When exporting JSON, the regions, functions, expansions, and summaries of the coverage data will be exported. When exporting an lcov trace file, the line-based coverage and summaries will be exported.

The exported data can optionally be filtered to only export the coverage for the files listed in SOURCES.

For information on compiling programs for coverage and generating profile data, see :ref:`llvm-cov-show`.

OPTIONS