Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 1 | # Code Coverage in Chromium |
| 2 | |
| 3 | ### Coverage Dashboard: [https://chromium-coverage.appspot.com/] |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 4 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 5 | Table of contents: |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 6 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 7 | - [Coverage Script](#coverage-script) |
| 8 | - [Workflow](#workflow) |
| 9 | * [Step 0 Download Tooling](#step-0-download-tooling) |
| 10 | * [Step 1 Build](#step-1-build) |
| 11 | * [Step 2 Create Raw Profiles](#step-2-create-raw-profiles) |
| 12 | * [Step 3 Create Indexed Profile](#step-3-create-indexed-profile) |
| 13 | * [Step 4 Create Coverage Reports](#step-4-create-coverage-reports) |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 14 | - [Contacts](#contacts) |
| 15 | - [FAQ](#faq) |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 16 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 17 | Chromium uses Clang source-based code coverage. This [documentation] explains |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 18 | how to use Clang’s source-based coverage features in general. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 19 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 20 | In this document, we first introduce a code coverage script that can be used to |
| 21 | generate code coverage reports for Chromium code in one command, and then |
| 22 | describe the code coverage reports generation workflow. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 23 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 24 | ## Coverage Script |
| 25 | The [coverage script] automates the process described below and provides a |
| 26 | one-stop service to generate code coverage reports in just one command. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 27 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 28 | This script is currently supported on Linux, Mac, iOS and ChromeOS platforms. |
| 29 | |
| 30 | Here is an example usage: |
| 31 | |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 32 | ``` |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 33 | $ gn gen out/coverage \ |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 34 | --args='use_clang_coverage=true is_component_build=false dcheck_always_on=true' |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 35 | $ python tools/code_coverage/coverage.py \ |
| 36 | crypto_unittests url_unittests \ |
| 37 | -b out/coverage -o out/report \ |
| 38 | -c 'out/coverage/crypto_unittests' \ |
| 39 | -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \ |
| 40 | -f url/ -f crypto/ |
| 41 | ``` |
| 42 | The command above builds `crypto_unittests` and `url_unittests` targets and then |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 43 | runs them individually with their commands and arguments specified by the `-c` flag. |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 44 | For `url_unittests`, it only runs the test `URLParser.PathURL`. The coverage report |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 45 | is filtered to include only files and sub-directories under `url/` and `crypto/` |
| 46 | directories. |
| 47 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 48 | Aside from automating the process, this script provides visualization features to |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 49 | view code coverage breakdown by directories and by components, for example: |
| 50 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 51 | ### Directory View |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 52 | |
| 53 | ![code coverage report directory view] |
| 54 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 55 | ### Component View |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 56 | |
| 57 | ![code coverage report component view] |
| 58 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 59 | ### Source View |
| 60 | |
| 61 | When you click on a particular source file in one of the views above, you can check |
| 62 | per-line coverage information such as |
| 63 | |
Yuke Liao | bc35726b | 2018-10-31 22:16:21 | [diff] [blame^] | 64 | - Uncovered / Covered line fragments, lines and code blocks. This information can be |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 65 | useful to identify areas of code that lack test coverage. |
Yuke Liao | bc35726b | 2018-10-31 22:16:21 | [diff] [blame^] | 66 | - Per-line hit counts indicating how many times this line was hit by all tested targets. |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 67 | This information can be useful to determine hot spots in your code. |
Abhishek Arya | b23b1a7 | 2018-05-17 20:11:09 | [diff] [blame] | 68 | - Potentially dead code. See [dead code example]. |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 69 | |
| 70 | ![code coverage source view] |
| 71 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 72 | ## Workflow |
| 73 | This section presents the workflow of generating code coverage reports using two |
| 74 | unit test targets in Chromium repo as an example: `crypto_unittests` and |
| 75 | `url_unittests`, and the following diagram shows a step-by-step overview of the |
| 76 | process. |
| 77 | |
| 78 |  |
| 79 | |
| 80 | ### Step 0 Download Tooling |
| 81 | Generating code coverage reports requires llvm-profdata and llvm-cov tools. |
| 82 | Currently, these two tools are not part of Chromium’s Clang bundle, |
| 83 | [coverage script] downloads and updates them automatically, you can also |
| 84 | download the tools manually ([link]). |
| 85 | |
| 86 | ### Step 1 Build |
| 87 | In Chromium, to compile code with coverage enabled, one needs to add |
| 88 | `use_clang_coverage=true` and `is_component_build=false` GN flags to the args.gn |
| 89 | file in the build output directory. Under the hood, they ensure |
| 90 | `-fprofile-instr-generate` and `-fcoverage-mapping` flags are passed to the |
| 91 | compiler. |
| 92 | |
| 93 | ``` |
| 94 | $ gn gen out/coverage \ |
| 95 | --args='use_clang_coverage=true is_component_build=false' |
| 96 | $ gclient runhooks |
Max Moroz | f5b31fcd | 2018-08-10 21:55:48 | [diff] [blame] | 97 | $ autoninja -C out/coverage crypto_unittests url_unittests |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 98 | ``` |
| 99 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 100 | ### Step 2 Create Raw Profiles |
Yuke Liao | bc35726b | 2018-10-31 22:16:21 | [diff] [blame^] | 101 | The next step is to run the instrumented binaries. When the program exits, it |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 102 | writes a raw profile for each process. Because Chromium runs tests in |
| 103 | multiple processes, the number of processes spawned can be as many as a few |
| 104 | hundred, resulting in the generation of a few hundred gigabytes’ raw |
| 105 | profiles. To limit the number of raw profiles, `%Nm` pattern in |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 106 | `LLVM_PROFILE_FILE` environment variable is used to run tests in multi-process |
| 107 | mode, where `N` is the number of raw profiles. With `N = 4`, the total size of |
| 108 | the raw profiles are limited to a few gigabytes. |
| 109 | |
| 110 | ``` |
| 111 | $ export LLVM_PROFILE_FILE=”out/report/crypto_unittests.%4m.profraw” |
| 112 | $ ./out/coverage/crypto_unittests |
| 113 | $ ls out/report/ |
| 114 | crypto_unittests.3657994905831792357_0.profraw |
| 115 | ... |
| 116 | crypto_unittests.3657994905831792357_3.profraw |
| 117 | ``` |
| 118 | |
| 119 | ### Step 3 Create Indexed Profile |
| 120 | Raw profiles must be indexed before generating code coverage reports, and this |
| 121 | is done using the `merge` command of `llvm-profdata` tool, which merges multiple |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 122 | raw profiles (.profraw) and indexes them to create a single profile (.profdata). |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 123 | |
| 124 | At this point, all the raw profiles can be thrown away because their information |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 125 | is already contained in the indexed profile. |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 126 | |
| 127 | ``` |
| 128 | $ llvm-profdata merge -o out/report/coverage.profdata \ |
| 129 | out/report/crypto_unittests.3657994905831792357_0.profraw |
| 130 | ... |
| 131 | out/report/crypto_unittests.3657994905831792357_3.profraw |
| 132 | out/report/url_unittests.714228855822523802_0.profraw |
| 133 | ... |
| 134 | out/report/url_unittests.714228855822523802_3.profraw |
| 135 | $ ls out/report/coverage.profdata |
| 136 | out/report/coverage.profdata |
| 137 | ``` |
| 138 | |
| 139 | ### Step 4 Create Coverage Reports |
| 140 | Finally, `llvm-cov` is used to render code coverage reports. There are different |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 141 | report generation modes, and all of them require the following as input: |
| 142 | - Indexed profile |
| 143 | - All built target binaries |
| 144 | - All exercised source files. |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 145 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 146 | For example, the following command can be used to generate per-file line-by-line |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 147 | code coverage report: |
| 148 | |
| 149 | ``` |
| 150 | $ llvm-cov show -output-dir=out/report -format=html \ |
| 151 | -instr-profile=out/report/coverage.profdata \ |
| 152 | -object=out/coverage/url_unittests \ |
| 153 | out/coverage/crypto_unittests |
| 154 | ``` |
| 155 | |
| 156 | For more information on how to use llvm-cov, please refer to the [guide]. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 157 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 158 | ## Contacts |
| 159 | |
| 160 | ### Reporting problems |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 161 | For any breakage report and feature requests, please [file a bug]. |
| 162 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 163 | ### Mailing list |
Yuke Liao | bc35726b | 2018-10-31 22:16:21 | [diff] [blame^] | 164 | For questions and general discussions, please join [code-coverage group]. |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 165 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 166 | ## FAQ |
| 167 | |
| 168 | ### Can I use `is_component_build=true` for code coverage build? |
| 169 | |
| 170 | Yes, code coverage instrumentation works with both component and non-component |
| 171 | builds. Component build is usually faster to compile, but can be up to several |
| 172 | times slower to run with code coverage instrumentation. For more information, |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 173 | see [crbug.com/831939]. |
| 174 | |
| 175 | ### I am getting some warnings while using the script, is that fine? |
| 176 | |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 177 | Usually this is not a critical issue, but in general we tend not to have any |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 178 | warnings. Please check the list of [known issues], and if there is a similar |
| 179 | bug, leave a comment with the command you run, the output you get, and Chromium |
| 180 | revision you use. Otherwise, please [file a new issue] providing the same |
| 181 | information. |
| 182 | |
| 183 | ### How do crashes affect code coverage? |
| 184 | |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 185 | If a crash of any type occurs (e.g. Segmentation Fault or ASan error), the |
| 186 | crashing process might not dump coverage information necessary to generate |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 187 | code coverage report. For single-process applications (e.g. fuzz targets), that |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 188 | means no coverage might be reported at all. For multi-process applications, the |
| 189 | report might be incomplete. It is important to fix the crash first. If this is |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 190 | happening only in the coverage instrumented build, please [file a bug]. |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 191 | |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 192 | ### How do assertions affect code coverage? |
| 193 | |
| 194 | If a crash is caused by CHECK or DCHECK, the coverage dump will still be written |
| 195 | on the disk ([crrev.com/c/1172932]). However, if a crashing process calls the |
| 196 | standard [assert] directly or through a custom wrapper, the dump will not be |
| 197 | written (see [How do crashes affect code coverage?]). |
| 198 | |
Max Moroz | 63cd04d | 2018-05-02 16:40:23 | [diff] [blame] | 199 | ### Is it possible to obtain code coverage from a full Chromium build? |
| 200 | |
| 201 | Yes, with some important caveats. It is possible to build `chrome` target with |
| 202 | code coverage instrumentation enabled. However, there are some inconveniences |
| 203 | involved: |
| 204 | |
| 205 | * Linking may take a while |
| 206 | * The binary is huge (~4GB) |
| 207 | * The browser "works", but is noticeably slow and laggy |
| 208 | * The sandbox needs to be disabled (`--no-sandbox`) |
Max Moroz | 63cd04d | 2018-05-02 16:40:23 | [diff] [blame] | 209 | |
| 210 | For more information, please see [crbug.com/834781]. |
| 211 | |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 212 | ### Why do we see significantly different coverage reported on different revisions? |
| 213 | |
| 214 | There can be two possible scenarios: |
| 215 | |
| 216 | * It can be a one time flakiness due to a broken build or failing tests. |
| 217 | * It can be caused by extension of the test suite used for generating code |
| 218 | coverage reports. When we add new tests to the suite, the aggregate coverage |
| 219 | reported usually grows after that. |
| 220 | |
| 221 | ### How can I improve [coverage dashboard]? |
| 222 | |
| 223 | Source code of the dashboard is not open sourced at the moment, but if you are a |
| 224 | Googler, you should have access to the code-coverage repository. There is a |
| 225 | documentation and scripts for running it locally. To get access and report |
| 226 | issues, ping chrome-code-coverage@ list. |
| 227 | |
| 228 | ### Why is coverage for X not reported or unreasonably low, even though there is a test for X? |
| 229 | |
| 230 | There are several reasons why coverage reports can be incomplete or incorrect: |
| 231 | |
| 232 | * A particular test is not used for code coverage report generation. Please |
| 233 | check the [test suite], and if the test is missing, upload a CL to add it. |
| 234 | * A test may have a build failure or a runtime crash. Please check [the logs] |
| 235 | for that particular test target (rightmost column on the [coverage dashboard]). |
| 236 | If there is any failure, please upload a CL with the fix. If you can't fix it, |
| 237 | feel free to [file a bug]. |
| 238 | * A particular test may not be available on a particular platform. As of now, |
| 239 | only reports generated on Linux are available on the [coverage dashboard]. |
| 240 | |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 241 | ### Is coverage reported for the code executed inside the sandbox? |
| 242 | |
| 243 | Not at the moment until [crbug.com/842424] is resolved. We do not disable the |
| 244 | sandbox when running the tests. However, if there are any other non-sandbox'ed |
| 245 | tests for the same code, the coverage should be reported from those. For more |
| 246 | information, see [crbug.com/842424]. |
| 247 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 248 | |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 249 | [assert]: http://man7.org/linux/man-pages/man3/assert.3.html |
Yuke Liao | bc35726b | 2018-10-31 22:16:21 | [diff] [blame^] | 250 | [code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 251 | [code-coverage repository]: https://chrome-internal.googlesource.com/chrome/tools/code-coverage |
| 252 | [coverage dashboard]: https://chromium-coverage.appspot.com/ |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 253 | [coverage script]: https://cs.chromium.org/chromium/src/tools/code_coverage/coverage.py |
| 254 | [code coverage report directory view]: images/code_coverage_directory_view.png |
| 255 | [code coverage report component view]: images/code_coverage_component_view.png |
Abhishek Arya | af9811f2 | 2018-05-11 22:17:48 | [diff] [blame] | 256 | [code coverage source view]: images/code_coverage_source_view.png |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 257 | [crbug.com/821617]: https://crbug.com/821617 |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 258 | [crbug.com/831939]: https://crbug.com/831939 |
Max Moroz | 63cd04d | 2018-05-02 16:40:23 | [diff] [blame] | 259 | [crbug.com/834781]: https://crbug.com/834781 |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 260 | [crbug.com/842424]: https://crbug.com/842424 |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 261 | [crrev.com/c/1172932]: https://crrev.com/c/1172932 |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 262 | [clang roll]: https://crbug.com/841908 |
Abhishek Arya | b23b1a7 | 2018-05-17 20:11:09 | [diff] [blame] | 263 | [dead code example]: https://chromium.googlesource.com/chromium/src/+/ac6e09311fcc7e734be2ef21a9ccbbe04c4c4706 |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 264 | [documentation]: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 265 | [file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Tools%3ECodeCoverage |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 266 | [file a new issue]: https://bugs.chromium.org/p/chromium/issues/entry?components=Tools%3ECodeCoverage |
| 267 | [guide]: http://llvm.org/docs/CommandGuide/llvm-cov.html |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 268 | [How do crashes affect code coverage?]: #how-do-crashes-affect-code-coverage |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 269 | [https://chromium-coverage.appspot.com/]: https://chromium-coverage.appspot.com/ |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 270 | [known issues]: https://bugs.chromium.org/p/chromium/issues/list?q=component:Tools%3ECodeCoverage |
| 271 | [link]: https://storage.googleapis.com/chromium-browser-clang-staging/ |
Max Moroz | 3a92890 | 2018-05-15 14:39:50 | [diff] [blame] | 272 | [test suite]: https://cs.chromium.org/chromium/src/tools/code_coverage/test_suite.txt |
| 273 | [the logs]: https://chromium-coverage.appspot.com/reports/latest/linux/metadata/index.html |