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