blob: f2f3f82e435c159a7b92cd03f7ac9c8c8fd8d17f [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.
21
22From this point onwards "code coverage" will refer to the source-based kind.
23
24The code coverage workflow
25==========================
26
27The code coverage workflow consists of three main steps:
28
Vedant Kumar0819f362016-06-02 02:25:13 +000029* Compiling with coverage enabled.
Vedant Kumara530a362016-06-02 00:51:50 +000030
Vedant Kumar0819f362016-06-02 02:25:13 +000031* Running the instrumented program.
Vedant Kumara530a362016-06-02 00:51:50 +000032
Vedant Kumar0819f362016-06-02 02:25:13 +000033* Creating coverage reports.
Vedant Kumara530a362016-06-02 00:51:50 +000034
35The next few sections work through a complete, copy-'n-paste friendly example
36based on this program:
37
Vedant Kumar4c1112c2016-06-02 01:15:59 +000038.. code-block:: cpp
Vedant Kumara530a362016-06-02 00:51:50 +000039
40 % cat <<EOF > foo.cc
41 #define BAR(x) ((x) || (x))
42 template <typename T> void foo(T x) {
43 for (unsigned I = 0; I < 10; ++I) { BAR(I); }
44 }
45 int main() {
46 foo<int>(0);
47 foo<float>(0);
48 return 0;
49 }
50 EOF
51
52Compiling with coverage enabled
53===============================
54
Vedant Kumar6c53d8f2016-06-02 02:45:59 +000055To compile code with coverage enabled, pass ``-fprofile-instr-generate
Vedant Kumara530a362016-06-02 00:51:50 +000056-fcoverage-mapping`` to the compiler:
57
58.. code-block:: console
59
60 # Step 1: Compile with coverage enabled.
61 % clang++ -fprofile-instr-generate -fcoverage-mapping foo.cc -o foo
62
63Note that linking together code with and without coverage instrumentation is
64supported: any uninstrumented code simply won't be accounted for.
65
66Running the instrumented program
67================================
68
69The next step is to run the instrumented program. When the program exits it
70will write a **raw profile** to the path specified by the ``LLVM_PROFILE_FILE``
Vedant Kumar0819f362016-06-02 02:25:13 +000071environment variable. If that variable does not exist, the profile is written
72to ``default.profraw`` in the current directory of the program. If
73``LLVM_PROFILE_FILE`` contains a path to a non-existent directory, the missing
74directory structure will be created. Additionally, the following special
75**pattern strings** are rewritten:
Vedant Kumara530a362016-06-02 00:51:50 +000076
77* "%p" expands out to the process ID.
78
79* "%h" expands out to the hostname of the machine running the program.
80
Vedant Kumarf3300c92016-06-14 00:42:12 +000081* "%Nm" expands out to the instrumented binary's signature. When this pattern
82 is specified, the runtime creates a pool of N raw profiles which are used for
83 on-line profile merging. The runtime takes care of selecting a raw profile
84 from the pool, locking it, and updating it before the program exits. If N is
85 not specified (i.e the pattern is "%m"), it's assumed that ``N = 1``. N must
86 be between 1 and 9. The merge pool specifier can only occur once per filename
87 pattern.
88
Vedant Kumara530a362016-06-02 00:51:50 +000089.. code-block:: console
90
91 # Step 2: Run the program.
92 % LLVM_PROFILE_FILE="foo.profraw" ./foo
93
94Creating coverage reports
95=========================
96
Vedant Kumar0819f362016-06-02 02:25:13 +000097Raw profiles have to be **indexed** before they can be used to generate
Vedant Kumara530a362016-06-02 00:51:50 +000098coverage reports. This is done using the "merge" tool in ``llvm-profdata``, so
99named because it can combine and index profiles at the same time:
100
101.. code-block:: console
102
103 # Step 3(a): Index the raw profile.
104 % llvm-profdata merge -sparse foo.profraw -o foo.profdata
105
106There are multiple different ways to render coverage reports. One option is to
107generate a line-oriented report:
108
109.. code-block:: console
110
111 # Step 3(b): Create a line-oriented coverage report.
112 % llvm-cov show ./foo -instr-profile=foo.profdata
113
Vedant Kumar3f42b132016-07-28 23:18:48 +0000114To generate the same report in html with demangling turned on, use:
Vedant Kumara530a362016-06-02 00:51:50 +0000115
116.. code-block:: console
Vedant Kumar01d91ee2016-06-02 01:01:48 +0000117
Vedant Kumarae008852016-07-28 23:22:42 +0000118 % llvm-cov show ./foo -instr-profile=foo.profdata -format html -o report.dir -Xdemangler c++filt -Xdemangler -n
Vedant Kumara530a362016-06-02 00:51:50 +0000119
120This report includes a summary view as well as dedicated sub-views for
121templated functions and their instantiations. For our example program, we get
122distinct views for ``foo<int>(...)`` and ``foo<float>(...)``. If
123``-show-line-counts-or-regions`` is enabled, ``llvm-cov`` displays sub-line
124region counts (even in macro expansions):
125
George Burgess IVbc8cc5ac2016-06-21 02:19:43 +0000126.. code-block:: none
Vedant Kumara530a362016-06-02 00:51:50 +0000127
Vedant Kumar9ed58022016-09-19 01:42:38 +0000128 1| 20|#define BAR(x) ((x) || (x))
Vedant Kumara530a362016-06-02 00:51:50 +0000129 ^20 ^2
130 2| 2|template <typename T> void foo(T x) {
Vedant Kumar9ed58022016-09-19 01:42:38 +0000131 3| 22| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50 +0000132 ^22 ^20 ^20^20
Vedant Kumar9ed58022016-09-19 01:42:38 +0000133 4| 2|}
Vedant Kumara530a362016-06-02 00:51:50 +0000134 ------------------
135 | void foo<int>(int):
Vedant Kumar9ed58022016-09-19 01:42:38 +0000136 | 2| 1|template <typename T> void foo(T x) {
137 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50 +0000138 | ^11 ^10 ^10^10
Vedant Kumar9ed58022016-09-19 01:42:38 +0000139 | 4| 1|}
Vedant Kumara530a362016-06-02 00:51:50 +0000140 ------------------
141 | void foo<float>(int):
Vedant Kumar9ed58022016-09-19 01:42:38 +0000142 | 2| 1|template <typename T> void foo(T x) {
143 | 3| 11| for (unsigned I = 0; I < 10; ++I) { BAR(I); }
Vedant Kumara530a362016-06-02 00:51:50 +0000144 | ^11 ^10 ^10^10
Vedant Kumar9ed58022016-09-19 01:42:38 +0000145 | 4| 1|}
Vedant Kumara530a362016-06-02 00:51:50 +0000146 ------------------
147
148It's possible to generate a file-level summary of coverage statistics (instead
149of a line-oriented report) with:
150
151.. code-block:: console
152
153 # Step 3(c): Create a coverage summary.
154 % llvm-cov report ./foo -instr-profile=foo.profdata
Vedant Kumar3f42b132016-07-28 23:18:48 +0000155 Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover
156 --------------------------------------------------------------------------------------------------------------------------------------
157 /tmp/foo.cc 13 0 100.00% 3 0 100.00% 13 0 100.00%
158 --------------------------------------------------------------------------------------------------------------------------------------
159 TOTAL 13 0 100.00% 3 0 100.00% 13 0 100.00%
Vedant Kumara530a362016-06-02 00:51:50 +0000160
161A few final notes:
162
163* The ``-sparse`` flag is optional but can result in dramatically smaller
164 indexed profiles. This option should not be used if the indexed profile will
165 be reused for PGO.
166
167* Raw profiles can be discarded after they are indexed. Advanced use of the
168 profile runtime library allows an instrumented program to merge profiling
169 information directly into an existing raw profile on disk. The details are
170 out of scope.
171
172* The ``llvm-profdata`` tool can be used to merge together multiple raw or
173 indexed profiles. To combine profiling data from multiple runs of a program,
174 try e.g:
175
Vedant Kumar553a0d62016-06-02 17:19:45 +0000176 .. code-block:: console
Vedant Kumara530a362016-06-02 00:51:50 +0000177
Vedant Kumar553a0d62016-06-02 17:19:45 +0000178 % llvm-profdata merge -sparse foo1.profraw foo2.profdata -o foo3.profdata
Vedant Kumara530a362016-06-02 00:51:50 +0000179
Vedant Kumar9ed58022016-09-19 01:42:38 +0000180Interpreting reports
181====================
182
183There are four statistics tracked in a coverage summary:
184
185* Function coverage is the percentage of functions which have been executed at
186 least once. A function is treated as having been executed if any of its
187 instantiations are executed.
188
189* Instantiation coverage is the percentage of function instantiations which
190 have been executed at least once.
191
192* Line coverage is the percentage of code lines which have been executed at
193 least once.
194
195* Region coverage is the percentage of code regions which have been executed at
196 least once. A code region may span multiple lines (e.g a large function with
197 no control flow). However, it's also possible for a single line to contain
198 multiple code regions (e.g some short-circuited logic).
199
Vedant Kumara530a362016-06-02 00:51:50 +0000200Format compatibility guarantees
201===============================
202
203* There are no backwards or forwards compatibility guarantees for the raw
204 profile format. Raw profiles may be dependent on the specific compiler
205 revision used to generate them. It's inadvisable to store raw profiles for
206 long periods of time.
207
208* Tools must retain **backwards** compatibility with indexed profile formats.
209 These formats are not forwards-compatible: i.e, a tool which uses format
210 version X will not be able to understand format version (X+k).
211
212* There is a third format in play: the format of the coverage mappings emitted
213 into instrumented binaries. Tools must retain **backwards** compatibility
214 with these formats. These formats are not forwards-compatible.
Vedant Kumar553a0d62016-06-02 17:19:45 +0000215
Vedant Kumarb06294d2016-06-07 22:25:29 +0000216Using the profiling runtime without static initializers
217=======================================================
218
219By default the compiler runtime uses a static initializer to determine the
220profile output path and to register a writer function. To collect profiles
221without using static initializers, do this manually:
222
Vedant Kumar32a9bfa2016-06-08 22:24:52 +0000223* Export a ``int __llvm_profile_runtime`` symbol from each instrumented shared
224 library and executable. When the linker finds a definition of this symbol, it
225 knows to skip loading the object which contains the profiling runtime's
226 static initializer.
Vedant Kumarb06294d2016-06-07 22:25:29 +0000227
Vedant Kumar32a9bfa2016-06-08 22:24:52 +0000228* Forward-declare ``void __llvm_profile_initialize_file(void)`` and call it
229 once from each instrumented executable. This function parses
230 ``LLVM_PROFILE_FILE``, sets the output path, and truncates any existing files
231 at that path. To get the same behavior without truncating existing files,
232 pass a filename pattern string to ``void __llvm_profile_set_filename(char
233 *)``. These calls can be placed anywhere so long as they precede all calls
234 to ``__llvm_profile_write_file``.
Vedant Kumarb06294d2016-06-07 22:25:29 +0000235
Vedant Kumar32a9bfa2016-06-08 22:24:52 +0000236* Forward-declare ``int __llvm_profile_write_file(void)`` and call it to write
Vedant Kumar89262b62016-06-08 22:32:03 +0000237 out a profile. This function returns 0 when it succeeds, and a non-zero value
238 otherwise. Calling this function multiple times appends profile data to an
239 existing on-disk raw profile.
Vedant Kumarb06294d2016-06-07 22:25:29 +0000240
Vedant Kumar553a0d62016-06-02 17:19:45 +0000241Drawbacks and limitations
242=========================
243
Vedant Kumar62baa4c2016-06-06 15:44:40 +0000244* Code coverage does not handle unpredictable changes in control flow or stack
245 unwinding in the presence of exceptions precisely. Consider the following
246 function:
Vedant Kumar553a0d62016-06-02 17:19:45 +0000247
248 .. code-block:: cpp
249
250 int f() {
251 may_throw();
252 return 0;
253 }
254
Vedant Kumar62baa4c2016-06-06 15:44:40 +0000255 If the call to ``may_throw()`` propagates an exception into ``f``, the code
Vedant Kumar553a0d62016-06-02 17:19:45 +0000256 coverage tool may mark the ``return`` statement as executed even though it is
Vedant Kumar62baa4c2016-06-06 15:44:40 +0000257 not. A call to ``longjmp()`` can have similar effects.