xref: /llvm-project/clang/docs/SourceBasedCodeCoverage.rst (revision 71f8b441ed6a944ceb4530b49e8588dcbb1e0066)
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
67To compile code with Modified Condition/Decision Coverage (MC/DC) enabled,
68pass ``-fcoverage-mcdc`` in addition to the clang options specified above.
69MC/DC is an advanced form of code coverage most applicable in the embedded
70space.
71
72Running the instrumented program
73================================
74
75The next step is to run the instrumented program. When the program exits it
76will write a **raw profile** to the path specified by the ``LLVM_PROFILE_FILE``
77environment variable. If that variable does not exist, the profile is written
78to ``default.profraw`` in the current directory of the program. If
79``LLVM_PROFILE_FILE`` contains a path to a non-existent directory, the missing
80directory structure will be created.  Additionally, the following special
81**pattern strings** are rewritten:
82
83* "%p" expands out to the process ID.
84
85* "%h" expands out to the hostname of the machine running the program.
86
87* "%t" expands out to the value of the ``TMPDIR`` environment variable. On
88  Darwin, this is typically set to a temporary scratch directory.
89
90* "%Nm" expands out to the instrumented binary's signature. When this pattern
91  is specified, the runtime creates a pool of N raw profiles which are used for
92  on-line profile merging. The runtime takes care of selecting a raw profile
93  from the pool, locking it, and updating it before the program exits.  If N is
94  not specified (i.e the pattern is "%m"), it's assumed that ``N = 1``. The
95  merge pool specifier can only occur once per filename pattern.
96
97* "%c" expands out to nothing, but enables a mode in which profile counter
98  updates are continuously synced to a file. This means that if the
99  instrumented program crashes, or is killed by a signal, perfect coverage
100  information can still be recovered. Continuous mode does not support value
101  profiling for PGO, and is only supported on Darwin at the moment. Support for
102  Linux may be mostly complete but requires testing, and support for Windows
103  may require more extensive changes: please get involved if you are interested
104  in porting this feature.
105
106.. code-block:: console
107
108    # Step 2: Run the program.
109    % LLVM_PROFILE_FILE="foo.profraw" ./foo
110
111Note that continuous mode is also used on Fuchsia where it's the only supported
112mode, but the implementation is different. The Darwin and Linux implementation
113relies on padding and the ability to map a file over the existing memory
114mapping which is generally only available on POSIX systems and isn't suitable
115for other platforms.
116
117On Fuchsia, we rely on the ability to relocate counters at runtime using a
118level of indirection. On every counter access, we add a bias to the counter
119address. This bias is stored in ``__llvm_profile_counter_bias`` symbol that's
120provided by the profile runtime and is initially set to zero, meaning no
121relocation. The runtime can map the profile into memory at arbitrary locations,
122and set bias to the offset between the original and the new counter location,
123at which point every subsequent counter access will be to the new location,
124which allows updating profile directly akin to the continuous mode.
125
126The advantage of this approach is that doesn't require any special OS support.
127The disadvantage is the extra overhead due to additional instructions required
128for each counter access (overhead both in terms of binary size and performance)
129plus duplication of counters (i.e. one copy in the binary itself and another
130copy that's mapped into memory). This implementation can be also enabled for
131other platforms by passing the ``-runtime-counter-relocation`` option to the
132backend during compilation.
133
134For a program such as the `Lit <https://llvm.org/docs/CommandGuide/lit.html>`_
135testing tool which invokes other programs, it may be necessary to set
136``LLVM_PROFILE_FILE`` for each invocation. The pattern strings "%p" or "%Nm"
137may help to avoid corruption due to concurrency. Note that "%p" is also a Lit
138token and needs to be escaped as "%%p".
139
140.. code-block:: console
141
142    % clang++ -fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation foo.cc -o foo
143
144Creating coverage reports
145=========================
146
147Raw profiles have to be **indexed** before they can be used to generate
148coverage reports. This is done using the "merge" tool in ``llvm-profdata``
149(which can combine multiple raw profiles and index them at the same time):
150
151.. code-block:: console
152
153    # Step 3(a): Index the raw profile.
154    % llvm-profdata merge -sparse foo.profraw -o foo.profdata
155
156For an example of merging multiple profiles created by testing,
157see the LLVM `coverage build script <https://github.com/llvm/llvm-zorg/blob/main/zorg/jenkins/jobs/jobs/llvm-coverage>`_.
158
159There are multiple different ways to render coverage reports. The simplest
160option is to generate a line-oriented report:
161
162.. code-block:: console
163
164    # Step 3(b): Create a line-oriented coverage report.
165    % llvm-cov show ./foo -instr-profile=foo.profdata
166
167This report includes a summary view as well as dedicated sub-views for
168templated functions and their instantiations. For our example program, we get
169distinct views for ``foo<int>(...)`` and ``foo<float>(...)``.  If
170``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line
171region counts (even in macro expansions):
172
173.. code-block:: none
174
175        1|   20|#define BAR(x) ((x) || (x))
176                               ^20     ^2
177        2|    2|template <typename T> void foo(T x) {
178        3|   22|  for (unsigned I = 0; I < 10; ++I) { BAR(I); }
179                                       ^22     ^20  ^20^20
180        4|    2|}
181    ------------------
182    | void foo<int>(int):
183    |      2|    1|template <typename T> void foo(T x) {
184    |      3|   11|  for (unsigned I = 0; I < 10; ++I) { BAR(I); }
185    |                                     ^11     ^10  ^10^10
186    |      4|    1|}
187    ------------------
188    | void foo<float>(int):
189    |      2|    1|template <typename T> void foo(T x) {
190    |      3|   11|  for (unsigned I = 0; I < 10; ++I) { BAR(I); }
191    |                                     ^11     ^10  ^10^10
192    |      4|    1|}
193    ------------------
194
195If ``--show-branches=count`` and ``--show-expansions`` are also enabled, the
196sub-views will show detailed branch coverage information in addition to the
197region counts:
198
199.. code-block:: none
200
201    ------------------
202    | void foo<float>(int):
203    |      2|    1|template <typename T> void foo(T x) {
204    |      3|   11|  for (unsigned I = 0; I < 10; ++I) { BAR(I); }
205    |                                     ^11     ^10  ^10^10
206    |  ------------------
207    |  |  |    1|     10|#define BAR(x) ((x) || (x))
208    |  |  |                             ^10     ^1
209    |  |  |  ------------------
210    |  |  |  |  Branch (1:17): [True: 9, False: 1]
211    |  |  |  |  Branch (1:24): [True: 0, False: 1]
212    |  |  |  ------------------
213    |  ------------------
214    |  |  Branch (3:23): [True: 10, False: 1]
215    |  ------------------
216    |      4|    1|}
217    ------------------
218
219If the application was instrumented for Modified Condition/Decision Coverage
220(MC/DC) using the clang option ``-fcoverage-mcdc``, an MC/DC subview can be
221enabled using ``--show-mcdc`` that will show detailed MC/DC information for
222each complex condition boolean expression containing at most six conditions.
223
224To generate a file-level summary of coverage statistics instead of a
225line-oriented report, try:
226
227.. code-block:: console
228
229    # Step 3(c): Create a coverage summary.
230    % llvm-cov report ./foo -instr-profile=foo.profdata
231    Filename           Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover     Branches    Missed Branches     Cover
232    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
233    /tmp/foo.cc             13                 0   100.00%           3                 0   100.00%          13                 0   100.00%           12                  2    83.33%
234    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
235    TOTAL                   13                 0   100.00%           3                 0   100.00%          13                 0   100.00%           12                  2    83.33%
236
237The ``llvm-cov`` tool supports specifying a custom demangler, writing out
238reports in a directory structure, and generating html reports. For the full
239list of options, please refer to the `command guide
240<https://llvm.org/docs/CommandGuide/llvm-cov.html>`_.
241
242A few final notes:
243
244* The ``-sparse`` flag is optional but can result in dramatically smaller
245  indexed profiles. This option should not be used if the indexed profile will
246  be reused for PGO.
247
248* Raw profiles can be discarded after they are indexed. Advanced use of the
249  profile runtime library allows an instrumented program to merge profiling
250  information directly into an existing raw profile on disk. The details are
251  out of scope.
252
253* The ``llvm-profdata`` tool can be used to merge together multiple raw or
254  indexed profiles. To combine profiling data from multiple runs of a program,
255  try e.g:
256
257  .. code-block:: console
258
259      % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata
260
261Exporting coverage data
262=======================
263
264Coverage data can be exported into JSON using the ``llvm-cov export``
265sub-command. There is a comprehensive reference which defines the structure of
266the exported data at a high level in the llvm-cov source code.
267
268Interpreting reports
269====================
270
271There are six statistics tracked in a coverage summary:
272
273* Function coverage is the percentage of functions which have been executed at
274  least once. A function is considered to be executed if any of its
275  instantiations are executed.
276
277* Instantiation coverage is the percentage of function instantiations which
278  have been executed at least once. Template functions and static inline
279  functions from headers are two kinds of functions which may have multiple
280  instantiations. This statistic is hidden by default in reports, but can be
281  enabled via the ``-show-instantiation-summary`` option.
282
283* Line coverage is the percentage of code lines which have been executed at
284  least once. Only executable lines within function bodies are considered to be
285  code lines.
286
287* Region coverage is the percentage of code regions which have been executed at
288  least once. A code region may span multiple lines (e.g in a large function
289  body with no control flow). However, it's also possible for a single line to
290  contain multiple code regions (e.g in "return x || y && z").
291
292* Branch coverage is the percentage of "true" and "false" branches that have
293  been taken at least once. Each branch is tied to individual conditions in the
294  source code that may each evaluate to either "true" or "false".  These
295  conditions may comprise larger boolean expressions linked by boolean logical
296  operators. For example, "x = (y == 2) || (z < 10)" is a boolean expression
297  that is comprised of two individual conditions, each of which evaluates to
298  either true or false, producing four total branch outcomes.
299
300* Modified Condition/Decision Coverage (MC/DC) is the percentage of individual
301  branch conditions that have been shown to independently affect the decision
302  outcome of the boolean expression they comprise. This is accomplished using
303  the analysis of executed control flow through the expression (i.e. test
304  vectors) to show that as a condition's outcome is varied between "true" and
305  false", the decision's outcome also varies between "true" and false", while
306  the outcome of all other conditions is held fixed (or they are masked out as
307  unevaluatable, as happens in languages whose logical operators have
308  short-circuit semantics).  MC/DC builds on top of branch coverage and
309  requires that all code blocks and all execution paths have been tested.  This
310  statistic is hidden by default in reports, but it can be enabled via the
311  ``-show-mcdc-summary`` option as long as code was also compiled using the
312  clang option ``-fcoverage-mcdc``.
313
314  * Boolean expressions that are only comprised of one condition (and therefore
315    have no logical operators) are not included in MC/DC analysis and are
316    trivially deducible using branch coverage.
317
318Of these six statistics, function coverage is usually the least granular while
319branch coverage (with MC/DC) is the most granular. 100% branch coverage for a
320function implies 100% region coverage for a function. The project-wide totals
321for each statistic are listed in the summary.
322
323Format compatibility guarantees
324===============================
325
326* There are no backwards or forwards compatibility guarantees for the raw
327  profile format. Raw profiles may be dependent on the specific compiler
328  revision used to generate them. It's inadvisable to store raw profiles for
329  long periods of time.
330
331* Tools must retain **backwards** compatibility with indexed profile formats.
332  These formats are not forwards-compatible: i.e, a tool which uses format
333  version X will not be able to understand format version (X+k).
334
335* Tools must also retain **backwards** compatibility with the format of the
336  coverage mappings emitted into instrumented binaries. These formats are not
337  forwards-compatible.
338
339* The JSON coverage export format has a (major, minor, patch) version triple.
340  Only a major version increment indicates a backwards-incompatible change. A
341  minor version increment is for added functionality, and patch version
342  increments are for bugfixes.
343
344Impact of llvm optimizations on coverage reports
345================================================
346
347llvm optimizations (such as inlining or CFG simplification) should have no
348impact on coverage report quality. This is due to the fact that the mapping
349from source regions to profile counters is immutable, and is generated before
350the llvm optimizer kicks in. The optimizer can't prove that profile counter
351instrumentation is safe to delete (because it's not: it affects the profile the
352program emits), and so leaves it alone.
353
354Note that this coverage feature does not rely on information that can degrade
355during the course of optimization, such as debug info line tables.
356
357Using the profiling runtime without static initializers
358=======================================================
359
360By default the compiler runtime uses a static initializer to determine the
361profile output path and to register a writer function. To collect profiles
362without using static initializers, do this manually:
363
364* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared
365  library and executable. When the linker finds a definition of this symbol, it
366  knows to skip loading the object which contains the profiling runtime's
367  static initializer.
368
369* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it
370  once from each instrumented executable. This function parses
371  ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files
372  at that path. To get the same behavior without truncating existing files,
373  pass a filename pattern string to ``void __llvm_profile_set_filename(char
374  *)``.  These calls can be placed anywhere so long as they precede all calls
375  to ``__llvm_profile_write_file``.
376
377* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write
378  out a profile. This function returns 0 when it succeeds, and a non-zero value
379  otherwise. Calling this function multiple times appends profile data to an
380  existing on-disk raw profile.
381
382In C++ files, declare these as ``extern "C"``.
383
384Using the profiling runtime without a filesystem
385------------------------------------------------
386
387The profiling runtime also supports freestanding environments that lack a
388filesystem. The runtime ships as a static archive that's structured to make
389dependencies on a hosted environment optional, depending on what features
390the client application uses.
391
392The first step is to export ``__llvm_profile_runtime``, as above, to disable
393the default static initializers. Instead of calling the ``*_file()`` APIs
394described above, use the following to save the profile directly to a buffer
395under your control:
396
397* Forward-declare ``uint64_t __llvm_profile_get_size_for_buffer(void)`` and
398  call it to determine the size of the profile. You'll need to allocate a
399  buffer of this size.
400
401* Forward-declare ``int __llvm_profile_write_buffer(char *Buffer)`` and call it
402  to copy the current counters to ``Buffer``, which is expected to already be
403  allocated and big enough for the profile.
404
405* Optionally, forward-declare ``void __llvm_profile_reset_counters(void)`` and
406  call it to reset the counters before entering a specific section to be
407  profiled. This is only useful if there is some setup that should be excluded
408  from the profile.
409
410In C++ files, declare these as ``extern "C"``.
411
412Collecting coverage reports for the llvm project
413================================================
414
415To prepare a coverage report for llvm (and any of its sub-projects), add
416``-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On`` to the cmake configuration. Raw
417profiles will be written to ``$BUILD_DIR/profiles/``. To prepare an html
418report, run ``llvm/utils/prepare-code-coverage-artifact.py``.
419
420To specify an alternate directory for raw profiles, use
421``-DLLVM_PROFILE_DATA_DIR``. To change the size of the profile merge pool, use
422``-DLLVM_PROFILE_MERGE_POOL_SIZE``.
423
424Drawbacks and limitations
425=========================
426
427* Prior to version 2.26, the GNU binutils BFD linker is not able link programs
428  compiled with ``-fcoverage-mapping`` in its ``--gc-sections`` mode.  Possible
429  workarounds include disabling ``--gc-sections``, upgrading to a newer version
430  of BFD, or using the Gold linker.
431
432* Code coverage does not handle unpredictable changes in control flow or stack
433  unwinding in the presence of exceptions precisely. Consider the following
434  function:
435
436  .. code-block:: cpp
437
438      int f() {
439        may_throw();
440        return 0;
441      }
442
443  If the call to ``may_throw()`` propagates an exception into ``f``, the code
444  coverage tool may mark the ``return`` statement as executed even though it is
445  not. A call to ``longjmp()`` can have similar effects.
446
447Clang implementation details
448============================
449
450This section may be of interest to those wishing to understand or improve
451the clang code coverage implementation.
452
453Gap regions
454-----------
455
456Gap regions are source regions with counts. A reporting tool cannot set a line
457execution count to the count from a gap region unless that region is the only
458one on a line.
459
460Gap regions are used to eliminate unnatural artifacts in coverage reports, such
461as red "unexecuted" highlights present at the end of an otherwise covered line,
462or blue "executed" highlights present at the start of a line that is otherwise
463not executed.
464
465Branch regions
466--------------
467When viewing branch coverage details in source-based file-level sub-views using
468``--show-branches``, it is recommended that users show all macro expansions
469(using option ``--show-expansions``) since macros may contain hidden branch
470conditions.  The coverage summary report will always include these macro-based
471boolean expressions in the overall branch coverage count for a function or
472source file.
473
474Branch coverage is not tracked for constant folded branch conditions since
475branches are not generated for these cases.  In the source-based file-level
476sub-view, these branches will simply be shown as ``[Folded - Ignored]`` so that
477users are informed about what happened.
478
479Branch coverage is tied directly to branch-generating conditions in the source
480code.  Users should not see hidden branches that aren't actually tied to the
481source code.
482
483MC/DC Instrumentation
484---------------------
485
486When instrumenting for Modified Condition/Decision Coverage (MC/DC) using the
487clang option ``-fcoverage-mcdc``, there are two hard limits.
488
489The maximum number of terms is limited to 32767, which is practical for
490handwritten expressions. To be more restrictive in order to enforce coding rules,
491use ``-Xclang -fmcdc-max-conditions=n``. Expressions with exceeded condition
492counts ``n`` will generate warnings and will be excluded in the MC/DC coverage.
493
494The number of test vectors (the maximum number of possible combinations of
495expressions) is limited to 2,147,483,646. In this case, approximately
496256MiB (==2GiB/8) is used to record test vectors.
497
498To reduce memory usage, users can limit the maximum number of test vectors per
499expression with ``-Xclang -fmcdc-max-test-vectors=m``.
500If the number of test vectors resulting from the analysis of an expression
501exceeds ``m``, a warning will be issued and the expression will be excluded
502from the MC/DC coverage.
503
504The number of test vectors ``m``, for ``n`` terms in an expression, can be
505``m <= 2^n`` in the theoretical worst case, but is usually much smaller.
506In simple cases, such as expressions consisting of a sequence of single
507operators, ``m == n+1``. For example, ``(a && b && c && d && e && f && g)``
508requires 8 test vectors.
509
510Expressions such as ``((a0 && b0) || (a1 && b1) || ...)`` can cause the
511number of test vectors to increase exponentially.
512
513Also, if a boolean expression is embedded in the nest of another boolean
514expression but separated by a non-logical operator, this is also not supported.
515For example, in ``x = (a && b && c && func(d && f))``, the ``d && f`` case
516starts a new boolean expression that is separated from the other conditions by
517the operator ``func()``.  When this is encountered, a warning will be generated
518and the boolean expression will not be instrumented.
519
520Switch statements
521-----------------
522
523The region mapping for a switch body consists of a gap region that covers the
524entire body (starting from the '{' in 'switch (...) {', and terminating where the
525last case ends). This gap region has a zero count: this causes "gap" areas in
526between case statements, which contain no executable code, to appear uncovered.
527
528When a switch case is visited, the parent region is extended: if the parent
529region has no start location, its start location becomes the start of the case.
530This is used to support switch statements without a ``CompoundStmt`` body, in
531which the switch body and the single case share a count.
532
533For switches with ``CompoundStmt`` bodies, a new region is created at the start
534of each switch case.
535
536Branch regions are also generated for each switch case, including the default
537case. If there is no explicitly defined default case in the source code, a
538branch region is generated to correspond to the implicit default case that is
539generated by the compiler.  The implicit branch region is tied to the line and
540column number of the switch statement condition since no source code for the
541implicit case exists.
542