blob: 77ddd5847589824de4926997ab055c48f2156e00 [file] [log] [blame] [view]
Max Moroz3a928902018-05-15 14:39:501# Code Coverage in Chromium
2
Prakhare84bb3b2021-11-29 03:28:443### Coverage Dashboard: [link](https://analysis.chromium.org/coverage/p/chromium)
Yuke Liaod3b46272018-03-14 18:25:144
Yuke Liao1ffc8cb62018-04-06 19:09:075Table of contents:
Max Moroz3a928902018-05-15 14:39:506
Roberto Carrillo3b567862019-01-30 19:01:257- [Coverage Infrastructure](#coverage-infra)
8 * [Coverage Builders](#coverage-builders)
9 * [Coverage Service](#coverage-service)
10 * [Coverage Clients](#coverage-clients)
Roberto Carrillobc1560e2019-01-30 20:08:5811- [Local Coverage Script](#local-coverage-script)
Yuke Liao1ffc8cb62018-04-06 19:09:0712 * [Step 0 Download Tooling](#step-0-download-tooling)
13 * [Step 1 Build](#step-1-build)
14 * [Step 2 Create Raw Profiles](#step-2-create-raw-profiles)
15 * [Step 3 Create Indexed Profile](#step-3-create-indexed-profile)
16 * [Step 4 Create Coverage Reports](#step-4-create-coverage-reports)
Max Morozd73e45f2018-04-24 18:32:4717- [Contacts](#contacts)
18- [FAQ](#faq)
Yuke Liaod3b46272018-03-14 18:25:1419
Yuke Liaod3b46272018-03-14 18:25:1420
Prakharf41864b02022-07-18 19:03:5121This document is divided into two parts.
22- The first part introduces the code coverage infrastructure that
Roberto Carrillo3b567862019-01-30 19:01:2523continuously generates code coverage information for the whole codebase and for
24specific CLs in Gerrit. For the latter, refer to
Roberto Carrillo5221fc12019-01-30 21:34:5025[code\_coverage\_in\_gerrit.md](code_coverage_in_gerrit.md).
Prakharf41864b02022-07-18 19:03:5126- The second part talks about how to generate code coverage locally for Clang-compiled languages like C++. Refer to [android code coverage instructions] for instructions for java code.
Roberto Carrillo3b567862019-01-30 19:01:2527
28## Coverage Infrastructure
29
30![coverage infra diagram]
31
32There are 3 layers in the system:
33
34### Coverage Builders
35
36The first layer is the LUCI builders that
37 - build instrumented targets,
38 - run the instrumented tests,
39 - merge the results into single streams,
40 - upload data to cloud storage.
41
42There are two types of builder:
43
44CI Builder
45
Yuke Liao43bbbcd52019-06-21 19:34:5046The code coverage CI Builders periodically build all the test targets and fuzzer
Roberto Carrillo3b567862019-01-30 19:01:2547targets for a given platform and instrument all available source files. Then
48save the coverage data to a dedicated storage bucket.
49
50CQ Builder
51
52The code coverage CQ builders instrument only the files changed for a given CL.
Yuke Liao43bbbcd52019-06-21 19:34:5053More information about per-cl coverage info in [this
Roberto Carrillo5221fc12019-01-30 21:34:5054doc](code_coverage_in_gerrit.md).
Roberto Carrillo3b567862019-01-30 19:01:2555
56### Coverage Service
57
58The second layer in the system consists of an AppEngine application that
59consumes the coverage data from the builders above, structures it and stores it
60in cloud datastore. It then serves the information to the clients below.
61
62### Coverage Clients
63
64In the last layer we currently have two clients that consume the service:
65
66#### Coverage Dashboard
67
68The [coverage dashboard] front end is hosted in the same application as the
69service above.
70It shows the full-code coverage reports with links to the builds that generated
71them, as well as per-directory and per-component aggregation, and can be drilled
72down to the single line of code level of detail.
73
Yuke Liao43bbbcd52019-06-21 19:34:5074Refer to the following screenshots:
Roberto Carrillo3b567862019-01-30 19:01:2575
76##### Directory View
77
Yuke Liao43bbbcd52019-06-21 19:34:5078See coverage breakdown by directories (default landing page).
79
Roberto Carrillo3b567862019-01-30 19:01:2580![coverage dashboard directory view]
81
82##### Component View
83
Yuke Liao43bbbcd52019-06-21 19:34:5084Use the view dropdown menu to switch between directory and component.
85
Roberto Carrillo3b567862019-01-30 19:01:2586![coverage dashboard component view]
87
88##### Source View
89
Yuke Liao43bbbcd52019-06-21 19:34:5090Click on a particular source file in one of the views above to see line-by-line
91coverage breakdown, and it's useful to identify:
92- Uncovered lines and code blocks that lack test coverage.
Roberto Carrillo3b567862019-01-30 19:01:2593- Potentially dead code. See [dead code example].
Yuke Liao43bbbcd52019-06-21 19:34:5094- Hot spots in your code.
Roberto Carrillo3b567862019-01-30 19:01:2595
96![coverage dashboard file view]
97
Yuke Liao43bbbcd52019-06-21 19:34:5098##### Project View
99
100Click on "Previous Reports" to check out the coverage history of the project.
101
102![coverage dashboard link to previous reports]
103
104List of historical coverage reports are in reverse chronological order.
105
106![coverage dashboard previous reports]
107
Roberto Carrillo3b567862019-01-30 19:01:25108#### Gerrit Coverage View
109
110The other client supported at the moment is the gerrit plugin for code coverage.
111
112![gerrit coverage view]
113
114See [this doc](code_coverage_in_gerrit.md) for information about the feature
115that allows gerrit to display code coverage information generated for a given CL
116by CQ bot. Or see this
Roberto Carrillo5221fc12019-01-30 21:34:50117[15-second video tutorial](https://www.youtube.com/watch?v=cxXlYcSgIPE).
Yuke Liaod3b46272018-03-14 18:25:14118
Roberto Carrillobc1560e2019-01-30 20:08:58119## Local Coverage Script
Prakharf41864b02022-07-18 19:03:51120This [documentation] explains how to use Clang’s source-based coverage
121features in general. The [coverage script] automates the process described below and provides a
Roberto Carrillo3b567862019-01-30 19:01:25122one-stop service to generate code coverage reports locally in just one command.
Yuke Liaod3b46272018-03-14 18:25:14123
Ben Joyce88282362021-01-29 23:53:31124This script is currently supported on Android, Linux, Mac, iOS and ChromeOS
125platforms.
Yuke Liao1ffc8cb62018-04-06 19:09:07126
127Here is an example usage:
128
Yuke Liaod3b46272018-03-14 18:25:14129```
Yuke Liao1ffc8cb62018-04-06 19:09:07130$ gn gen out/coverage \
Bryce Thomasc9137cf2020-03-31 19:47:17131 --args="use_clang_coverage=true is_component_build=false
132 dcheck_always_on=true is_debug=false"
Yuke Liao1ffc8cb62018-04-06 19:09:07133$ python tools/code_coverage/coverage.py \
134 crypto_unittests url_unittests \
135 -b out/coverage -o out/report \
136 -c 'out/coverage/crypto_unittests' \
137 -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \
138 -f url/ -f crypto/
139```
140The command above builds `crypto_unittests` and `url_unittests` targets and then
Max Moroza5a95272018-08-31 16:20:55141runs them individually with their commands and arguments specified by the `-c` flag.
Abhishek Aryaaf9811f22018-05-11 22:17:48142For `url_unittests`, it only runs the test `URLParser.PathURL`. The coverage report
Yuke Liao1ffc8cb62018-04-06 19:09:07143is filtered to include only files and sub-directories under `url/` and `crypto/`
144directories.
145
Abhishek Aryaaf9811f22018-05-11 22:17:48146Aside from automating the process, this script provides visualization features to
Roberto Carrillo3b567862019-01-30 19:01:25147view code coverage breakdown by directories and by components, similar to the
148views in the [coverage dashboard](#coverage-dashboard) above.
Abhishek Aryaaf9811f22018-05-11 22:17:48149
Yuke Liao1ffc8cb62018-04-06 19:09:07150## Workflow
151This section presents the workflow of generating code coverage reports using two
152unit test targets in Chromium repo as an example: `crypto_unittests` and
153`url_unittests`, and the following diagram shows a step-by-step overview of the
154process.
155
156![code coverage generation workflow](images/code_coverage_workflow.png)
157
158### Step 0 Download Tooling
159Generating code coverage reports requires llvm-profdata and llvm-cov tools.
Zequan Wu6d00cccd2021-03-15 22:40:44160You can get them by adding `"checkout_clang_coverage_tools": True,` to
161`custom_vars` in the `.gclient` config and run `gclient runhooks`. You can also
162download the tools manually ([tools link])
Yuke Liao1ffc8cb62018-04-06 19:09:07163
164### Step 1 Build
165In Chromium, to compile code with coverage enabled, one needs to add
Yuke Liao8c0868fe62019-10-22 21:02:33166`use_clang_coverage=true`, `is_component_build=false` and `is_debug=false` GN
167flags to the args.gn file in the build output directory. Under the hood, they
168ensure `-fprofile-instr-generate` and `-fcoverage-mapping` flags are passed to
169the compiler.
Yuke Liao1ffc8cb62018-04-06 19:09:07170
171```
172$ gn gen out/coverage \
Yuke Liao8c0868fe62019-10-22 21:02:33173 --args='use_clang_coverage=true is_component_build=false is_debug=false'
Yuke Liao1ffc8cb62018-04-06 19:09:07174$ gclient runhooks
Max Morozf5b31fcd2018-08-10 21:55:48175$ autoninja -C out/coverage crypto_unittests url_unittests
Yuke Liaod3b46272018-03-14 18:25:14176```
177
Yuke Liao1ffc8cb62018-04-06 19:09:07178### Step 2 Create Raw Profiles
Yuke Liaobc35726b2018-10-31 22:16:21179The next step is to run the instrumented binaries. When the program exits, it
Abhishek Aryaaf9811f22018-05-11 22:17:48180writes a raw profile for each process. Because Chromium runs tests in
181multiple processes, the number of processes spawned can be as many as a few
182hundred, resulting in the generation of a few hundred gigabytes’ raw
183profiles. To limit the number of raw profiles, `%Nm` pattern in
Yuke Liao1ffc8cb62018-04-06 19:09:07184`LLVM_PROFILE_FILE` environment variable is used to run tests in multi-process
185mode, where `N` is the number of raw profiles. With `N = 4`, the total size of
Ben Joyce88282362021-01-29 23:53:31186the raw profiles are limited to a few gigabytes. (If working on Android, the
187.profraw files will be located in ./out/coverage/coverage by default.)
Yuke Liao1ffc8cb62018-04-06 19:09:07188
Alan Zhaof8fa31302024-01-11 03:35:39189Additionally, we also recommend enabling the continuous mode by adding the `%c`
190pattern to `LLVM_PROFILE_FILE`. The continuous mode updates counters in real
191time instead of flushing to disk at process exit. This recovers coverage data
192from tests that exit abnormally (e.g. death tests). Furthermore, the continuous
193mode is required to recover coverage data for tests that run in sandboxed
194processes. For more information, see crbug.com/1468343.
195
Yuke Liao1ffc8cb62018-04-06 19:09:07196```
Alan Zhaof8fa31302024-01-11 03:35:39197$ export LLVM_PROFILE_FILE="out/report/crypto_unittests.%4m%c.profraw"
Yuke Liao1ffc8cb62018-04-06 19:09:07198$ ./out/coverage/crypto_unittests
199$ ls out/report/
200crypto_unittests.3657994905831792357_0.profraw
201...
202crypto_unittests.3657994905831792357_3.profraw
203```
204
205### Step 3 Create Indexed Profile
206Raw profiles must be indexed before generating code coverage reports, and this
207is done using the `merge` command of `llvm-profdata` tool, which merges multiple
Abhishek Aryaaf9811f22018-05-11 22:17:48208raw profiles (.profraw) and indexes them to create a single profile (.profdata).
Yuke Liao1ffc8cb62018-04-06 19:09:07209
210At this point, all the raw profiles can be thrown away because their information
Abhishek Aryaaf9811f22018-05-11 22:17:48211is already contained in the indexed profile.
Yuke Liao1ffc8cb62018-04-06 19:09:07212
213```
214$ llvm-profdata merge -o out/report/coverage.profdata \
215 out/report/crypto_unittests.3657994905831792357_0.profraw
216...
217out/report/crypto_unittests.3657994905831792357_3.profraw
218out/report/url_unittests.714228855822523802_0.profraw
219...
220out/report/url_unittests.714228855822523802_3.profraw
221$ ls out/report/coverage.profdata
222out/report/coverage.profdata
223```
224
225### Step 4 Create Coverage Reports
226Finally, `llvm-cov` is used to render code coverage reports. There are different
Abhishek Aryaaf9811f22018-05-11 22:17:48227report generation modes, and all of them require the following as input:
228- Indexed profile
229- All built target binaries
Roberto Carrillo5221fc12019-01-30 21:34:50230- All exercised source files
Yuke Liao1ffc8cb62018-04-06 19:09:07231
Abhishek Aryaaf9811f22018-05-11 22:17:48232For example, the following command can be used to generate per-file line-by-line
Yuke Liao1ffc8cb62018-04-06 19:09:07233code coverage report:
234
235```
236$ llvm-cov show -output-dir=out/report -format=html \
237 -instr-profile=out/report/coverage.profdata \
Choongwoo Han56752522021-06-10 17:38:34238 -compilation-dir=out/coverage \
Yuke Liao1ffc8cb62018-04-06 19:09:07239 -object=out/coverage/url_unittests \
240 out/coverage/crypto_unittests
241```
242
Ben Joyce88282362021-01-29 23:53:31243If creating a report for Android, the -object arg would be the lib.unstripped
244file, ie out/coverage/lib.unstripped/libcrypto_unittests__library.so
245
Yuke Liao1ffc8cb62018-04-06 19:09:07246For more information on how to use llvm-cov, please refer to the [guide].
Yuke Liaod3b46272018-03-14 18:25:14247
Max Morozd73e45f2018-04-24 18:32:47248## Contacts
249
250### Reporting problems
Yuke Liaod3b46272018-03-14 18:25:14251For any breakage report and feature requests, please [file a bug].
252
Max Morozd73e45f2018-04-24 18:32:47253### Mailing list
Yuke Liaobc35726b2018-10-31 22:16:21254For questions and general discussions, please join [code-coverage group].
Yuke Liao1ffc8cb62018-04-06 19:09:07255
Max Morozd73e45f2018-04-24 18:32:47256## FAQ
257
258### Can I use `is_component_build=true` for code coverage build?
259
260Yes, code coverage instrumentation works with both component and non-component
261builds. Component build is usually faster to compile, but can be up to several
262times slower to run with code coverage instrumentation. For more information,
Max Morozc5e364a2018-04-25 23:19:49263see [crbug.com/831939].
264
265### I am getting some warnings while using the script, is that fine?
266
Abhishek Aryaaf9811f22018-05-11 22:17:48267Usually this is not a critical issue, but in general we tend not to have any
Max Morozc5e364a2018-04-25 23:19:49268warnings. Please check the list of [known issues], and if there is a similar
269bug, leave a comment with the command you run, the output you get, and Chromium
Yuke Liao03c644072019-07-30 18:33:40270revision you use. Otherwise, please [file a bug] providing the same information.
Max Morozc5e364a2018-04-25 23:19:49271
272### How do crashes affect code coverage?
273
Max Moroza5a95272018-08-31 16:20:55274If a crash of any type occurs (e.g. Segmentation Fault or ASan error), the
275crashing process might not dump coverage information necessary to generate
Max Morozc5e364a2018-04-25 23:19:49276code coverage report. For single-process applications (e.g. fuzz targets), that
Max Moroza5a95272018-08-31 16:20:55277means no coverage might be reported at all. For multi-process applications, the
278report might be incomplete. It is important to fix the crash first. If this is
Abhishek Aryaaf9811f22018-05-11 22:17:48279happening only in the coverage instrumented build, please [file a bug].
Max Morozd73e45f2018-04-24 18:32:47280
Max Moroza5a95272018-08-31 16:20:55281### How do assertions affect code coverage?
282
283If a crash is caused by CHECK or DCHECK, the coverage dump will still be written
284on the disk ([crrev.com/c/1172932]). However, if a crashing process calls the
285standard [assert] directly or through a custom wrapper, the dump will not be
286written (see [How do crashes affect code coverage?]).
287
Max Moroz63cd04d2018-05-02 16:40:23288### Is it possible to obtain code coverage from a full Chromium build?
289
290Yes, with some important caveats. It is possible to build `chrome` target with
291code coverage instrumentation enabled. However, there are some inconveniences
292involved:
293
Kai Ninomiya6f537eb2023-01-04 23:43:47294* Linking may take a while, especially if you use a non-component build.
295* The binary is huge (2-4GB).
296* The browser may be noticeably slow and laggy.
Max Moroz63cd04d2018-05-02 16:40:23297
298For more information, please see [crbug.com/834781].
299
Max Moroz3a928902018-05-15 14:39:50300### Why do we see significantly different coverage reported on different revisions?
301
302There can be two possible scenarios:
303
304* It can be a one time flakiness due to a broken build or failing tests.
305* It can be caused by extension of the test suite used for generating code
306coverage reports. When we add new tests to the suite, the aggregate coverage
307reported usually grows after that.
308
309### How can I improve [coverage dashboard]?
310
Roberto Carrillo3b567862019-01-30 19:01:25311The code for the service and dashboard currently lives along with findit at
John Palmer046f9872021-05-24 01:24:56312[this location](https://chromium.googlesource.com/infra/infra/+/main/appengine/findit/)
Roberto Carrillo3b567862019-01-30 19:01:25313because of significant shared logic.
314
315The code used by the bots that generate the coverage data lives (among other
316places) in the
John Palmer046f9872021-05-24 01:24:56317[code coverage recipe module](https://chromium.googlesource.com/chromium/tools/build/+/main/scripts/slave/recipe_modules/code_coverage/).
Roberto Carrillo3b567862019-01-30 19:01:25318
Max Moroz3a928902018-05-15 14:39:50319### Why is coverage for X not reported or unreasonably low, even though there is a test for X?
320
321There are several reasons why coverage reports can be incomplete or incorrect:
322
323* A particular test is not used for code coverage report generation. Please
Roberto Carrillo5221fc12019-01-30 21:34:50324[file a bug].
Roberto Carrillo3b567862019-01-30 19:01:25325* A test may have a build failure or a runtime crash. Please check the build
326for that particular report (rightmost column on the [coverage dashboard]).
Max Moroz3a928902018-05-15 14:39:50327If there is any failure, please upload a CL with the fix. If you can't fix it,
328feel free to [file a bug].
329* A particular test may not be available on a particular platform. As of now,
Yuke Liao43bbbcd52019-06-21 19:34:50330only reports generated on Linux and CrOS are available on the
331[coverage dashboard].
Max Moroz3a928902018-05-15 14:39:50332
Max Moroz3a928902018-05-15 14:39:50333### Is coverage reported for the code executed inside the sandbox?
334
Yuke Liao8c0868fe62019-10-22 21:02:33335Yes!
Max Moroz3a928902018-05-15 14:39:50336
Max Morozd73e45f2018-04-24 18:32:47337
Max Moroza5a95272018-08-31 16:20:55338[assert]: http://man7.org/linux/man-pages/man3/assert.3.html
Yuke Liaobc35726b2018-10-31 22:16:21339[code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage
Max Moroz3a928902018-05-15 14:39:50340[code-coverage repository]: https://chrome-internal.googlesource.com/chrome/tools/code-coverage
Prakhar74514ba2022-05-19 17:14:04341[coverage dashboard]: https://analysis.chromium.org/coverage/p/chromium
Yuke Liao1ffc8cb62018-04-06 19:09:07342[coverage script]: https://cs.chromium.org/chromium/src/tools/code_coverage/coverage.py
Roberto Carrillo3b567862019-01-30 19:01:25343[coverage infra diagram]: images/code_coverage_infra_diagram.png
344[coverage dashboard file view]: images/code_coverage_dashboard_file_view.png
345[coverage dashboard component view]: images/code_coverage_dashboard_component_view.png
346[coverage dashboard directory view]: images/code_coverage_dashboard_directory_view.png
Yuke Liao43bbbcd52019-06-21 19:34:50347[coverage dashboard link to previous reports]: images/code_coverage_dashboard_link_to_previous_reports.png
348[coverage dashboard previous reports]: images/code_coverage_dashboard_previous_reports.png
Max Moroz3a928902018-05-15 14:39:50349[crbug.com/821617]: https://crbug.com/821617
Max Morozc5e364a2018-04-25 23:19:49350[crbug.com/831939]: https://crbug.com/831939
Max Moroz63cd04d2018-05-02 16:40:23351[crbug.com/834781]: https://crbug.com/834781
Max Moroza5a95272018-08-31 16:20:55352[crrev.com/c/1172932]: https://crrev.com/c/1172932
Max Moroz3a928902018-05-15 14:39:50353[clang roll]: https://crbug.com/841908
Abhishek Aryab23b1a72018-05-17 20:11:09354[dead code example]: https://chromium.googlesource.com/chromium/src/+/ac6e09311fcc7e734be2ef21a9ccbbe04c4c4706
Max Morozc5e364a2018-04-25 23:19:49355[documentation]: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
Yuke Liao03c644072019-07-30 18:33:40356[file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ETest%3ECodeCoverage
Yuke Liao43bbbcd52019-06-21 19:34:50357[gerrit coverage view]: images/code_coverage_annotations.png
Max Morozc5e364a2018-04-25 23:19:49358[guide]: http://llvm.org/docs/CommandGuide/llvm-cov.html
Max Moroza5a95272018-08-31 16:20:55359[How do crashes affect code coverage?]: #how-do-crashes-affect-code-coverage
Yuke Liao03c644072019-07-30 18:33:40360[known issues]: https://bugs.chromium.org/p/chromium/issues/list?q=component:Infra%3ETest%3ECodeCoverage
Roberto Carrillo3b567862019-01-30 19:01:25361[tools link]: https://storage.googleapis.com/chromium-browser-clang-staging/
Prakharf41864b02022-07-18 19:03:51362[android code coverage instructions]: https://chromium.googlesource.com/chromium/src/+/HEAD/build/android/docs/coverage.md