1========================== 2Source-based Code Coverage 3========================== 4 5.. contents:: 6 :local: 7 8Introduction 9============ 10 11This document explains how to use clang's source-based code coverage feature. 12It's called "source-based" because it operates on AST and preprocessor 13information directly. This allows it to generate very precise coverage data. 14 15Clang ships two other code coverage implementations: 16 17* :doc:`SanitizerCoverage` - A low-overhead tool meant for use alongside the 18 various sanitizers. It can provide up to edge-level coverage. 19 20* gcov - A GCC-compatible coverage implementation which operates on DebugInfo. 21 This is enabled by ``-ftest-coverage`` or ``--coverage``. 22 23From this point onwards "code coverage" will refer to the source-based kind. 24 25The code coverage workflow 26========================== 27 28The code coverage workflow consists of three main steps: 29 30* Compiling with coverage enabled. 31 32* Running the instrumented program. 33 34* Creating coverage reports. 35 36The next few sections work through a complete, copy-'n-paste friendly example 37based on this program: 38 39.. code-block:: cpp 40 41 % cat <<EOF > foo.cc 42 #define BAR(x) ((x) || (x)) 43 template <typename T> void foo(T x) { 44 for (unsigned I = 0; I < 10; ++I) { BAR(I); } 45 } 46 int main() { 47 foo<int>(0); 48 foo<float>(0); 49 return 0; 50 } 51 EOF 52 53Compiling with coverage enabled 54=============================== 55 56To compile code with coverage enabled, pass ``-fprofile-instr-generate 57-fcoverage-mapping`` to the compiler: 58 59.. code-block:: console 60 61 # Step 1: Compile with coverage enabled. 62 % clang++ -fprofile-instr-generate -fcoverage-mapping foo.cc -o foo 63 64Note that linking together code with and without coverage instrumentation is 65supported. Uninstrumented code simply won't be accounted for in reports. 66 67Running the instrumented program 68================================ 69 70The next step is to run the instrumented program. When the program exits it 71will write a **raw profile** to the path specified by the ``LLVM_PROFILE_FILE`` 72environment variable. If that variable does not exist, the profile is written 73to ``default.profraw`` in the current directory of the program. If 74``LLVM_PROFILE_FILE`` contains a path to a non-existent directory, the missing 75directory structure will be created. Additionally, the following special 76**pattern strings** are rewritten: 77 78* "%p" expands out to the process ID. 79 80* "%h" expands out to the hostname of the machine running the program. 81 82* "%t" expands out to the value of the ``TMPDIR`` environment variable. On 83 Darwin, this is typically set to a temporary scratch directory. 84 85* "%Nm" expands out to the instrumented binary's signature. When this pattern 86 is specified, the runtime creates a pool of N raw profiles which are used for 87 on-line profile merging. The runtime takes care of selecting a raw profile 88 from the pool, locking it, and updating it before the program exits. If N is 89 not specified (i.e the pattern is "%m"), it's assumed that ``N = 1``. N must 90 be between 1 and 9. The merge pool specifier can only occur once per filename 91 pattern. 92 93* "%c" expands out to nothing, but enables a mode in which profile counter 94 updates are continuously synced to a file. This means that if the 95 instrumented program crashes, or is killed by a signal, perfect coverage 96 information can still be recovered. Continuous mode does not support value 97 profiling for PGO, and is only supported on Darwin at the moment. Support for 98 Linux may be mostly complete but requires testing, and support for Windows 99 may require more extensive changes: please get involved if you are interested 100 in porting this feature. 101 102.. code-block:: console 103 104 # Step 2: Run the program. 105 % LLVM_PROFILE_FILE="foo.profraw" ./foo 106 107Note that continuous mode is also used on Fuchsia where it's the only supported 108mode, but the implementation is different. The Darwin and Linux implementation 109relies on padding and the ability to map a file over the existing memory 110mapping which is generally only available on POSIX systems and isn't suitable 111for other platforms. 112 113On Fuchsia, we rely on the ability to relocate counters at runtime using a 114level of indirection. On every counter access, we add a bias to the counter 115address. This bias is stored in ``__llvm_profile_counter_bias`` symbol that's 116provided by the profile runtime and is initially set to zero, meaning no 117relocation. The runtime can map the profile into memory at arbitrary locations, 118and set bias to the offset between the original and the new counter location, 119at which point every subsequent counter access will be to the new location, 120which allows updating profile directly akin to the continuous mode. 121 122The advantage of this approach is that doesn't require any special OS support. 123The disadvantage is the extra overhead due to additional instructions required 124for each counter access (overhead both in terms of binary size and performance) 125plus duplication of counters (i.e. one copy in the binary itself and another 126copy that's mapped into memory). This implementation can be also enabled for 127other platforms by passing the ``-runtime-counter-relocation`` option to the 128backend during compilation. 129 130.. code-block:: console 131 132 % clang++ -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation foo.cc -o foo 133 134Creating coverage reports 135========================= 136 137Raw profiles have to be **indexed** before they can be used to generate 138coverage reports. This is done using the "merge" tool in ``llvm-profdata`` 139(which can combine multiple raw profiles and index them at the same time): 140 141.. code-block:: console 142 143 # Step 3(a): Index the raw profile. 144 % llvm-profdata merge -sparse foo.profraw -o foo.profdata 145 146There are multiple different ways to render coverage reports. The simplest 147option is to generate a line-oriented report: 148 149.. code-block:: console 150 151 # Step 3(b): Create a line-oriented coverage report. 152 % llvm-cov show ./foo -instr-profile=foo.profdata 153 154This report includes a summary view as well as dedicated sub-views for 155templated functions and their instantiations. For our example program, we get 156distinct views for ``foo<int>(...)`` and ``foo<float>(...)``. If 157``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line 158region counts (even in macro expansions): 159 160.. code-block:: none 161 162 1| 20|#define BAR(x) ((x) || (x)) 163 ^20 ^2 164 2| 2|template <typename T> void foo(T x) { 165 3| 22| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 166 ^22 ^20 ^20^20 167 4| 2|} 168 ------------------ 169 | void foo<int>(int): 170 | 2| 1|template <typename T> void foo(T x) { 171 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 172 | ^11 ^10 ^10^10 173 | 4| 1|} 174 ------------------ 175 | void foo<float>(int): 176 | 2| 1|template <typename T> void foo(T x) { 177 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); } 178 | ^11 ^10 ^10^10 179 | 4| 1|} 180 ------------------ 181 182To generate a file-level summary of coverage statistics instead of a 183line-oriented report, try: 184 185.. code-block:: console 186 187 # Step 3(c): Create a coverage summary. 188 % llvm-cov report ./foo -instr-profile=foo.profdata 189 Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover 190 -------------------------------------------------------------------------------------------------------------------------------------- 191 /tmp/foo.cc 13 0 100.00% 3 0 100.00% 13 0 100.00% 192 -------------------------------------------------------------------------------------------------------------------------------------- 193 TOTAL 13 0 100.00% 3 0 100.00% 13 0 100.00% 194 195The ``llvm-cov`` tool supports specifying a custom demangler, writing out 196reports in a directory structure, and generating html reports. For the full 197list of options, please refer to the `command guide 198<https://llvm.org/docs/CommandGuide/llvm-cov.html>`_. 199 200A few final notes: 201 202* The ``-sparse`` flag is optional but can result in dramatically smaller 203 indexed profiles. This option should not be used if the indexed profile will 204 be reused for PGO. 205 206* Raw profiles can be discarded after they are indexed. Advanced use of the 207 profile runtime library allows an instrumented program to merge profiling 208 information directly into an existing raw profile on disk. The details are 209 out of scope. 210 211* The ``llvm-profdata`` tool can be used to merge together multiple raw or 212 indexed profiles. To combine profiling data from multiple runs of a program, 213 try e.g: 214 215 .. code-block:: console 216 217 % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata 218 219Exporting coverage data 220======================= 221 222Coverage data can be exported into JSON using the ``llvm-cov export`` 223sub-command. There is a comprehensive reference which defines the structure of 224the exported data at a high level in the llvm-cov source code. 225 226Interpreting reports 227==================== 228 229There are four statistics tracked in a coverage summary: 230 231* Function coverage is the percentage of functions which have been executed at 232 least once. A function is considered to be executed if any of its 233 instantiations are executed. 234 235* Instantiation coverage is the percentage of function instantiations which 236 have been executed at least once. Template functions and static inline 237 functions from headers are two kinds of functions which may have multiple 238 instantiations. 239 240* Line coverage is the percentage of code lines which have been executed at 241 least once. Only executable lines within function bodies are considered to be 242 code lines. 243 244* Region coverage is the percentage of code regions which have been executed at 245 least once. A code region may span multiple lines (e.g in a large function 246 body with no control flow). However, it's also possible for a single line to 247 contain multiple code regions (e.g in "return x || y && z"). 248 249Of these four statistics, function coverage is usually the least granular while 250region coverage is the most granular. The project-wide totals for each 251statistic are listed in the summary. 252 253Format compatibility guarantees 254=============================== 255 256* There are no backwards or forwards compatibility guarantees for the raw 257 profile format. Raw profiles may be dependent on the specific compiler 258 revision used to generate them. It's inadvisable to store raw profiles for 259 long periods of time. 260 261* Tools must retain **backwards** compatibility with indexed profile formats. 262 These formats are not forwards-compatible: i.e, a tool which uses format 263 version X will not be able to understand format version (X+k). 264 265* Tools must also retain **backwards** compatibility with the format of the 266 coverage mappings emitted into instrumented binaries. These formats are not 267 forwards-compatible. 268 269* The JSON coverage export format has a (major, minor, patch) version triple. 270 Only a major version increment indicates a backwards-incompatible change. A 271 minor version increment is for added functionality, and patch version 272 increments are for bugfixes. 273 274Using the profiling runtime without static initializers 275======================================================= 276 277By default the compiler runtime uses a static initializer to determine the 278profile output path and to register a writer function. To collect profiles 279without using static initializers, do this manually: 280 281* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared 282 library and executable. When the linker finds a definition of this symbol, it 283 knows to skip loading the object which contains the profiling runtime's 284 static initializer. 285 286* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it 287 once from each instrumented executable. This function parses 288 ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files 289 at that path. To get the same behavior without truncating existing files, 290 pass a filename pattern string to ``void __llvm_profile_set_filename(char 291 *)``. These calls can be placed anywhere so long as they precede all calls 292 to ``__llvm_profile_write_file``. 293 294* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write 295 out a profile. This function returns 0 when it succeeds, and a non-zero value 296 otherwise. Calling this function multiple times appends profile data to an 297 existing on-disk raw profile. 298 299In C++ files, declare these as ``extern "C"``. 300 301Collecting coverage reports for the llvm project 302================================================ 303 304To prepare a coverage report for llvm (and any of its sub-projects), add 305``-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On`` to the cmake configuration. Raw 306profiles will be written to ``$BUILD_DIR/profiles/``. To prepare an html 307report, run ``llvm/utils/prepare-code-coverage-artifact.py``. 308 309To specify an alternate directory for raw profiles, use 310``-DLLVM_PROFILE_DATA_DIR``. To change the size of the profile merge pool, use 311``-DLLVM_PROFILE_MERGE_POOL_SIZE``. 312 313Drawbacks and limitations 314========================= 315 316* Prior to version 2.26, the GNU binutils BFD linker is not able link programs 317 compiled with ``-fcoverage-mapping`` in its ``--gc-sections`` mode. Possible 318 workarounds include disabling ``--gc-sections``, upgrading to a newer version 319 of BFD, or using the Gold linker. 320 321* Code coverage does not handle unpredictable changes in control flow or stack 322 unwinding in the presence of exceptions precisely. Consider the following 323 function: 324 325 .. code-block:: cpp 326 327 int f() { 328 may_throw(); 329 return 0; 330 } 331 332 If the call to ``may_throw()`` propagates an exception into ``f``, the code 333 coverage tool may mark the ``return`` statement as executed even though it is 334 not. A call to ``longjmp()`` can have similar effects. 335 336Clang implementation details 337============================ 338 339This section may be of interest to those wishing to understand or improve 340the clang code coverage implementation. 341 342Gap regions 343----------- 344 345Gap regions are source regions with counts. A reporting tool cannot set a line 346execution count to the count from a gap region unless that region is the only 347one on a line. 348 349Gap regions are used to eliminate unnatural artifacts in coverage reports, such 350as red "unexecuted" highlights present at the end of an otherwise covered line, 351or blue "executed" highlights present at the start of a line that is otherwise 352not executed. 353 354Switch statements 355----------------- 356 357The region mapping for a switch body consists of a gap region that covers the 358entire body (starting from the '{' in 'switch (...) {', and terminating where the 359last case ends). This gap region has a zero count: this causes "gap" areas in 360between case statements, which contain no executable code, to appear uncovered. 361 362When a switch case is visited, the parent region is extended: if the parent 363region has no start location, its start location becomes the start of the case. 364This is used to support switch statements without a ``CompoundStmt`` body, in 365which the switch body and the single case share a count. 366 367For switches with ``CompoundStmt`` bodies, a new region is created at the start 368of each switch case. 369