blob: 0beb284e475ee1eaf28449fe4cfb6eb1df5f042d [file] [log] [blame]
Vedant Kumara530a362016-06-02 00:51:50 +00001==========================
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.
Vedant Kumar6eed0d52017-02-09 21:33:21 +000021 This is enabled by ``-ftest-coverage`` or ``--coverage``.
Vedant Kumara530a362016-06-02 00:51:50 +000022
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
Vedant Kumar0819f362016-06-02 02:25:13 +000030* Compiling with coverage enabled.
Vedant Kumara530a362016-06-02 00:51:50 +000031
Vedant Kumar0819f362016-06-02 02:25:13 +000032* Running the instrumented program.
Vedant Kumara530a362016-06-02 00:51:50 +000033
Vedant Kumar0819f362016-06-02 02:25:13 +000034* Creating coverage reports.
Vedant Kumara530a362016-06-02 00:51:50 +000035
36The next few sections work through a complete, copy-'n-paste friendly example
37based on this program:
38
Vedant Kumar4c1112c2016-06-02 01:15:59 +000039.. code-block:: cpp
Vedant Kumara530a362016-06-02 00:51:50 +000040
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
Vedant Kumar6c53d8f2016-06-02 02:45:59 +000056To compile code with coverage enabled, pass ``-fprofile-instr-generate
Vedant Kumara530a362016-06-02 00:51:50 +000057-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
Vedant Kumar74c3fd12016-09-22 15:34:33 +000065supported. Uninstrumented code simply won't be accounted for in reports.
Vedant Kumara530a362016-06-02 00:51:50 +000066
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``
Vedant Kumar0819f362016-06-02 02:25:13 +000072environment 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:
Vedant Kumara530a362016-06-02 00:51:50 +000077
78* "%p" expands out to the process ID.
79
80* "%h" expands out to the hostname of the machine running the program.
81
Vedant Kumarf3300c92016-06-14 00:42:12 +000082* "%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
Vedant Kumard889d1e2019-09-19 11:56:43 -070090* "%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 is not yet compatible with
94 the "%Nm" merging mode described above, does not support value profiling for
95 PGO, and is only supported on Darwin. Support for Linux may be mostly
96 complete but requires testing, and support for Fuchsia/Windows may require
97 more extensive changes: please get involved if you are interested in porting
98 this feature.
99
Vedant Kumara530a362016-06-02 00:51:50 +0000100.. code-block:: console
101
102 # Step 2: Run the program.
103 % LLVM_PROFILE_FILE="foo.profraw" ./foo
104
105Creating coverage reports
106=========================
107
Vedant Kumar0819f362016-06-02 02:25:13 +0000108Raw profiles have to be **indexed** before they can be used to generate
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000109coverage reports. This is done using the "merge" tool in ``llvm-profdata``
110(which can combine multiple raw profiles and index them at the same time):
Vedant Kumara530a362016-06-02 00:51:50 +0000111
112.. code-block:: console
113
114 # Step 3(a): Index the raw profile.
115 % llvm-profdata merge -sparse foo.profraw -o foo.profdata
116
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000117There are multiple different ways to render coverage reports. The simplest
118option is to generate a line-oriented report:
Vedant Kumara530a362016-06-02 00:51:50 +0000119
120.. code-block:: console
121
122 # Step 3(b): Create a line-oriented coverage report.
123 % llvm-cov show ./foo -instr-profile=foo.profdata
124
Vedant Kumara530a362016-06-02 00:51:50 +0000125This report includes a summary view as well as dedicated sub-views for
126templated functions and their instantiations. For our example program, we get
127distinct views for ``foo<int>(...)`` and ``foo<float>(...)``. If
128``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line
129region counts (even in macro expansions):
130
George Burgess IVbc8cc5ac2016-06-21 02:19:43 +0000131.. code-block:: none
Vedant Kumara530a362016-06-02 00:51:50 +0000132
Vedant Kumar9ed58022016-09-19 01:42:38 +0000133 1| 20|#define BAR(x) ((x) || (x))
Vedant Kumara530a362016-06-02 00:51:50 +0000134 ^20 ^2
135 2| 2|template <typename T> void foo(T x) {
Vedant Kumar9ed58022016-09-19 01:42:38 +0000136 3| 22| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50 +0000137 ^22 ^20 ^20^20
Vedant Kumar9ed58022016-09-19 01:42:38 +0000138 4| 2|}
Vedant Kumara530a362016-06-02 00:51:50 +0000139 ------------------
140 | void foo<int>(int):
Vedant Kumar9ed58022016-09-19 01:42:38 +0000141 | 2| 1|template <typename T> void foo(T x) {
142 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50 +0000143 | ^11 ^10 ^10^10
Vedant Kumar9ed58022016-09-19 01:42:38 +0000144 | 4| 1|}
Vedant Kumara530a362016-06-02 00:51:50 +0000145 ------------------
146 | void foo<float>(int):
Vedant Kumar9ed58022016-09-19 01:42:38 +0000147 | 2| 1|template <typename T> void foo(T x) {
148 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50 +0000149 | ^11 ^10 ^10^10
Vedant Kumar9ed58022016-09-19 01:42:38 +0000150 | 4| 1|}
Vedant Kumara530a362016-06-02 00:51:50 +0000151 ------------------
152
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000153To generate a file-level summary of coverage statistics instead of a
154line-oriented report, try:
Vedant Kumara530a362016-06-02 00:51:50 +0000155
156.. code-block:: console
157
158 # Step 3(c): Create a coverage summary.
159 % llvm-cov report ./foo -instr-profile=foo.profdata
Vedant Kumar3f42b132016-07-28 23:18:48 +0000160 Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover
161 --------------------------------------------------------------------------------------------------------------------------------------
162 /tmp/foo.cc 13 0 100.00% 3 0 100.00% 13 0 100.00%
163 --------------------------------------------------------------------------------------------------------------------------------------
164 TOTAL 13 0 100.00% 3 0 100.00% 13 0 100.00%
Vedant Kumara530a362016-06-02 00:51:50 +0000165
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000166The ``llvm-cov`` tool supports specifying a custom demangler, writing out
167reports in a directory structure, and generating html reports. For the full
168list of options, please refer to the `command guide
Sylvestre Ledrubc5c3f52018-11-04 17:02:00 +0000169<https://llvm.org/docs/CommandGuide/llvm-cov.html>`_.
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000170
Vedant Kumara530a362016-06-02 00:51:50 +0000171A few final notes:
172
173* The ``-sparse`` flag is optional but can result in dramatically smaller
174 indexed profiles. This option should not be used if the indexed profile will
175 be reused for PGO.
176
177* Raw profiles can be discarded after they are indexed. Advanced use of the
178 profile runtime library allows an instrumented program to merge profiling
179 information directly into an existing raw profile on disk. The details are
180 out of scope.
181
182* The ``llvm-profdata`` tool can be used to merge together multiple raw or
183 indexed profiles. To combine profiling data from multiple runs of a program,
184 try e.g:
185
Vedant Kumar553a0d62016-06-02 17:19:45 +0000186 .. code-block:: console
Vedant Kumara530a362016-06-02 00:51:50 +0000187
Vedant Kumar553a0d62016-06-02 17:19:45 +0000188 % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata
Vedant Kumara530a362016-06-02 00:51:50 +0000189
Vedant Kumar6fe6eae2016-09-20 17:11:18 +0000190Exporting coverage data
191=======================
192
193Coverage data can be exported into JSON using the ``llvm-cov export``
194sub-command. There is a comprehensive reference which defines the structure of
195the exported data at a high level in the llvm-cov source code.
196
Vedant Kumar9ed58022016-09-19 01:42:38 +0000197Interpreting reports
198====================
199
200There are four statistics tracked in a coverage summary:
201
202* Function coverage is the percentage of functions which have been executed at
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000203 least once. A function is considered to be executed if any of its
Vedant Kumar9ed58022016-09-19 01:42:38 +0000204 instantiations are executed.
205
206* Instantiation coverage is the percentage of function instantiations which
Vedant Kumar6fe6eae2016-09-20 17:11:18 +0000207 have been executed at least once. Template functions and static inline
208 functions from headers are two kinds of functions which may have multiple
209 instantiations.
Vedant Kumar9ed58022016-09-19 01:42:38 +0000210
211* Line coverage is the percentage of code lines which have been executed at
Vedant Kumar6fe6eae2016-09-20 17:11:18 +0000212 least once. Only executable lines within function bodies are considered to be
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000213 code lines.
Vedant Kumar9ed58022016-09-19 01:42:38 +0000214
215* Region coverage is the percentage of code regions which have been executed at
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000216 least once. A code region may span multiple lines (e.g in a large function
217 body with no control flow). However, it's also possible for a single line to
218 contain multiple code regions (e.g in "return x || y && z").
Vedant Kumar6fe6eae2016-09-20 17:11:18 +0000219
220Of these four statistics, function coverage is usually the least granular while
221region coverage is the most granular. The project-wide totals for each
222statistic are listed in the summary.
Vedant Kumar9ed58022016-09-19 01:42:38 +0000223
Vedant Kumara530a362016-06-02 00:51:50 +0000224Format compatibility guarantees
225===============================
226
227* There are no backwards or forwards compatibility guarantees for the raw
228 profile format. Raw profiles may be dependent on the specific compiler
229 revision used to generate them. It's inadvisable to store raw profiles for
230 long periods of time.
231
232* Tools must retain **backwards** compatibility with indexed profile formats.
233 These formats are not forwards-compatible: i.e, a tool which uses format
234 version X will not be able to understand format version (X+k).
235
Vedant Kumar74c3fd12016-09-22 15:34:33 +0000236* Tools must also retain **backwards** compatibility with the format of the
237 coverage mappings emitted into instrumented binaries. These formats are not
238 forwards-compatible.
Vedant Kumar553a0d62016-06-02 17:19:45 +0000239
Vedant Kumar6fe6eae2016-09-20 17:11:18 +0000240* The JSON coverage export format has a (major, minor, patch) version triple.
241 Only a major version increment indicates a backwards-incompatible change. A
242 minor version increment is for added functionality, and patch version
243 increments are for bugfixes.
244
Vedant Kumarb06294d2016-06-07 22:25:29 +0000245Using the profiling runtime without static initializers
246=======================================================
247
248By default the compiler runtime uses a static initializer to determine the
249profile output path and to register a writer function. To collect profiles
250without using static initializers, do this manually:
251
Vedant Kumar32a9bfa2016-06-08 22:24:52 +0000252* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared
253 library and executable. When the linker finds a definition of this symbol, it
254 knows to skip loading the object which contains the profiling runtime's
255 static initializer.
Vedant Kumarb06294d2016-06-07 22:25:29 +0000256
Vedant Kumar32a9bfa2016-06-08 22:24:52 +0000257* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it
258 once from each instrumented executable. This function parses
259 ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files
260 at that path. To get the same behavior without truncating existing files,
261 pass a filename pattern string to ``void __llvm_profile_set_filename(char
262 *)``. These calls can be placed anywhere so long as they precede all calls
263 to ``__llvm_profile_write_file``.
Vedant Kumarb06294d2016-06-07 22:25:29 +0000264
Vedant Kumar32a9bfa2016-06-08 22:24:52 +0000265* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write
Vedant Kumar89262b62016-06-08 22:32:03 +0000266 out a profile. This function returns 0 when it succeeds, and a non-zero value
267 otherwise. Calling this function multiple times appends profile data to an
268 existing on-disk raw profile.
Vedant Kumarb06294d2016-06-07 22:25:29 +0000269
Nico Weberb1706ca2017-01-25 16:01:32 +0000270In C++ files, declare these as ``extern "C"``.
271
Vedant Kumar6fe6eae2016-09-20 17:11:18 +0000272Collecting coverage reports for the llvm project
273================================================
274
275To prepare a coverage report for llvm (and any of its sub-projects), add
276``-DLLVM_BUILD_INSTRUMENTED_COVERAGE=On`` to the cmake configuration. Raw
277profiles will be written to ``$BUILD_DIR/profiles/``. To prepare an html
278report, run ``llvm/utils/prepare-code-coverage-artifact.py``.
279
280To specify an alternate directory for raw profiles, use
281``-DLLVM_PROFILE_DATA_DIR``. To change the size of the profile merge pool, use
282``-DLLVM_PROFILE_MERGE_POOL_SIZE``.
283
Vedant Kumar553a0d62016-06-02 17:19:45 +0000284Drawbacks and limitations
285=========================
286
Vedant Kumar82cd7702017-06-19 21:22:05 +0000287* Prior to version 2.26, the GNU binutils BFD linker is not able link programs
Vedant Kumar1c5f3122017-06-19 21:26:04 +0000288 compiled with ``-fcoverage-mapping`` in its ``--gc-sections`` mode. Possible
289 workarounds include disabling ``--gc-sections``, upgrading to a newer version
290 of BFD, or using the Gold linker.
Vedant Kumar82cd7702017-06-19 21:22:05 +0000291
Vedant Kumar62baa4c2016-06-06 15:44:40 +0000292* Code coverage does not handle unpredictable changes in control flow or stack
293 unwinding in the presence of exceptions precisely. Consider the following
294 function:
Vedant Kumar553a0d62016-06-02 17:19:45 +0000295
296 .. code-block:: cpp
297
298 int f() {
299 may_throw();
300 return 0;
301 }
302
Vedant Kumar62baa4c2016-06-06 15:44:40 +0000303 If the call to ``may_throw()`` propagates an exception into ``f``, the code
Vedant Kumar553a0d62016-06-02 17:19:45 +0000304 coverage tool may mark the ``return`` statement as executed even though it is
Vedant Kumar62baa4c2016-06-06 15:44:40 +0000305 not. A call to ``longjmp()`` can have similar effects.