Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 1 | # Code coverage in Chromium |
| 2 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 3 | Table of contents: |
| 4 | - [Coverage Script](#coverage-script) |
| 5 | - [Workflow](#workflow) |
| 6 | * [Step 0 Download Tooling](#step-0-download-tooling) |
| 7 | * [Step 1 Build](#step-1-build) |
| 8 | * [Step 2 Create Raw Profiles](#step-2-create-raw-profiles) |
| 9 | * [Step 3 Create Indexed Profile](#step-3-create-indexed-profile) |
| 10 | * [Step 4 Create Coverage Reports](#step-4-create-coverage-reports) |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 11 | - [Contacts](#contacts) |
| 12 | - [FAQ](#faq) |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 13 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 14 | Chromium uses Clang source-based code coverage, this [documentation] explains |
| 15 | how to use Clang’s source-based coverage features in general. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 16 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 17 | In this doc, we first introduce a code coverage script that can be used to |
| 18 | generate code coverage reports for Chromium code in one command, and then dive |
| 19 | into details to describe the code coverage reports generation workflow. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 20 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 21 | ## Coverage Script |
| 22 | The [coverage script] automates the process described below and provides a |
| 23 | one-stop service to generate code coverage reports in just one command. |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 24 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 25 | This script is currently supported on Linux, Mac, iOS and ChromeOS platforms. |
| 26 | |
| 27 | Here is an example usage: |
| 28 | |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 29 | ``` |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 30 | $ gn gen out/coverage \ |
| 31 | --args='use_clang_coverage=true is_component_build=false' |
| 32 | $ python tools/code_coverage/coverage.py \ |
| 33 | crypto_unittests url_unittests \ |
| 34 | -b out/coverage -o out/report \ |
| 35 | -c 'out/coverage/crypto_unittests' \ |
| 36 | -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \ |
| 37 | -f url/ -f crypto/ |
| 38 | ``` |
| 39 | The command above builds `crypto_unittests` and `url_unittests` targets and then |
| 40 | runs each with the command and arguments specified by the `-c` flag. For |
| 41 | `url_unittests`, it only runs the test `URLParser.PathURL`. The coverage report |
| 42 | is filtered to include only files and sub-directories under `url/` and `crypto/` |
| 43 | directories. |
| 44 | |
| 45 | Aside from automating the process, this script provides additional features to |
| 46 | view code coverage breakdown by directories and by components, for example: |
| 47 | |
| 48 | Directory View: |
| 49 | |
| 50 | ![code coverage report directory view] |
| 51 | |
| 52 | Component View: |
| 53 | |
| 54 | ![code coverage report component view] |
| 55 | |
| 56 | ## Workflow |
| 57 | This section presents the workflow of generating code coverage reports using two |
| 58 | unit test targets in Chromium repo as an example: `crypto_unittests` and |
| 59 | `url_unittests`, and the following diagram shows a step-by-step overview of the |
| 60 | process. |
| 61 | |
| 62 |  |
| 63 | |
| 64 | ### Step 0 Download Tooling |
| 65 | Generating code coverage reports requires llvm-profdata and llvm-cov tools. |
| 66 | Currently, these two tools are not part of Chromium’s Clang bundle, |
| 67 | [coverage script] downloads and updates them automatically, you can also |
| 68 | download the tools manually ([link]). |
| 69 | |
| 70 | ### Step 1 Build |
| 71 | In Chromium, to compile code with coverage enabled, one needs to add |
| 72 | `use_clang_coverage=true` and `is_component_build=false` GN flags to the args.gn |
| 73 | file in the build output directory. Under the hood, they ensure |
| 74 | `-fprofile-instr-generate` and `-fcoverage-mapping` flags are passed to the |
| 75 | compiler. |
| 76 | |
| 77 | ``` |
| 78 | $ gn gen out/coverage \ |
| 79 | --args='use_clang_coverage=true is_component_build=false' |
| 80 | $ gclient runhooks |
| 81 | $ ninja -C out/coverage crypto_unittests url_unittests |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 82 | ``` |
| 83 | |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 84 | ### Step 2 Create Raw Profiles |
| 85 | The next step is to run the instrumented binaries, and when the program exits it |
| 86 | will write a raw profile for each process. Because Chromium runs tests in |
| 87 | multiple processes, and the number of processes spawned can be as many as a few |
| 88 | hundred, which results in the generation of a few hundred gigabytes’ raw |
| 89 | profiles, to limit the number of raw profiles, `%Nm` pattern in |
| 90 | `LLVM_PROFILE_FILE` environment variable is used to run tests in multi-process |
| 91 | mode, where `N` is the number of raw profiles. With `N = 4`, the total size of |
| 92 | the raw profiles are limited to a few gigabytes. |
| 93 | |
| 94 | ``` |
| 95 | $ export LLVM_PROFILE_FILE=”out/report/crypto_unittests.%4m.profraw” |
| 96 | $ ./out/coverage/crypto_unittests |
| 97 | $ ls out/report/ |
| 98 | crypto_unittests.3657994905831792357_0.profraw |
| 99 | ... |
| 100 | crypto_unittests.3657994905831792357_3.profraw |
| 101 | ``` |
| 102 | |
| 103 | ### Step 3 Create Indexed Profile |
| 104 | Raw profiles must be indexed before generating code coverage reports, and this |
| 105 | is done using the `merge` command of `llvm-profdata` tool, which merges multiple |
| 106 | raw profiles (.profraw) and index them to create a single profile (.profdata). |
| 107 | |
| 108 | At this point, all the raw profiles can be thrown away because their information |
| 109 | are already contained in the indexed profile. |
| 110 | |
| 111 | ``` |
| 112 | $ llvm-profdata merge -o out/report/coverage.profdata \ |
| 113 | out/report/crypto_unittests.3657994905831792357_0.profraw |
| 114 | ... |
| 115 | out/report/crypto_unittests.3657994905831792357_3.profraw |
| 116 | out/report/url_unittests.714228855822523802_0.profraw |
| 117 | ... |
| 118 | out/report/url_unittests.714228855822523802_3.profraw |
| 119 | $ ls out/report/coverage.profdata |
| 120 | out/report/coverage.profdata |
| 121 | ``` |
| 122 | |
| 123 | ### Step 4 Create Coverage Reports |
| 124 | Finally, `llvm-cov` is used to render code coverage reports. There are different |
| 125 | report generation modes, and all of them require the indexed profile, all the |
| 126 | built binaries and all the exercised source files to be available. |
| 127 | |
| 128 | For example, following command can be used to generate per-file line-by-line |
| 129 | code coverage report: |
| 130 | |
| 131 | ``` |
| 132 | $ llvm-cov show -output-dir=out/report -format=html \ |
| 133 | -instr-profile=out/report/coverage.profdata \ |
| 134 | -object=out/coverage/url_unittests \ |
| 135 | out/coverage/crypto_unittests |
| 136 | ``` |
| 137 | |
| 138 | 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] | 139 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 140 | ## Contacts |
| 141 | |
| 142 | ### Reporting problems |
Yuke Liao | d3b4627 | 2018-03-14 18:25:14 | [diff] [blame] | 143 | For any breakage report and feature requests, please [file a bug]. |
| 144 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 145 | ### Mailing list |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 146 | For questions and general discussions, please join [chrome-code-coverage group]. |
| 147 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 148 | ## FAQ |
| 149 | |
| 150 | ### Can I use `is_component_build=true` for code coverage build? |
| 151 | |
| 152 | Yes, code coverage instrumentation works with both component and non-component |
| 153 | builds. Component build is usually faster to compile, but can be up to several |
| 154 | times slower to run with code coverage instrumentation. For more information, |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 155 | see [crbug.com/831939]. |
| 156 | |
| 157 | ### I am getting some warnings while using the script, is that fine? |
| 158 | |
| 159 | Usually that is not a critical issue, but in general we tend not to have any |
| 160 | warnings. Please check the list of [known issues], and if there is a similar |
| 161 | bug, leave a comment with the command you run, the output you get, and Chromium |
| 162 | revision you use. Otherwise, please [file a new issue] providing the same |
| 163 | information. |
| 164 | |
| 165 | ### How do crashes affect code coverage? |
| 166 | |
| 167 | If a crash of any type occurs (Segmentation Fault, CHECK failure, ASan error), |
| 168 | the crashing process will not dump coverage information necessary to generate |
| 169 | code coverage report. For single-process applications (e.g. fuzz targets), that |
| 170 | means no coverage will be reported at all. For multi-process applications, the |
| 171 | report will be incomplete. |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 172 | |
Max Moroz | 63cd04d | 2018-05-02 16:40:23 | [diff] [blame^] | 173 | ### Is it possible to obtain code coverage from a full Chromium build? |
| 174 | |
| 175 | Yes, with some important caveats. It is possible to build `chrome` target with |
| 176 | code coverage instrumentation enabled. However, there are some inconveniences |
| 177 | involved: |
| 178 | |
| 179 | * Linking may take a while |
| 180 | * The binary is huge (~4GB) |
| 181 | * The browser "works", but is noticeably slow and laggy |
| 182 | * The sandbox needs to be disabled (`--no-sandbox`) |
| 183 | * Coverage can be incomplete for child processes |
| 184 | |
| 185 | For more information, please see [crbug.com/834781]. |
| 186 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 187 | |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 188 | [chrome-code-coverage group]: https://groups.google.com/a/google.com/forum/#!forum/chrome-code-coverage |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 189 | [coverage script]: https://cs.chromium.org/chromium/src/tools/code_coverage/coverage.py |
| 190 | [code coverage report directory view]: images/code_coverage_directory_view.png |
| 191 | [code coverage report component view]: images/code_coverage_component_view.png |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 192 | [crbug.com/831939]: https://crbug.com/831939 |
Max Moroz | 63cd04d | 2018-05-02 16:40:23 | [diff] [blame^] | 193 | [crbug.com/834781]: https://crbug.com/834781 |
Max Moroz | c5e364a | 2018-04-25 23:19:49 | [diff] [blame] | 194 | [documentation]: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html |
Yuke Liao | 1ffc8cb6 | 2018-04-06 19:09:07 | [diff] [blame] | 195 | [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] | 196 | [file a new issue]: https://bugs.chromium.org/p/chromium/issues/entry?components=Tools%3ECodeCoverage |
| 197 | [guide]: http://llvm.org/docs/CommandGuide/llvm-cov.html |
| 198 | [known issues]: https://bugs.chromium.org/p/chromium/issues/list?q=component:Tools%3ECodeCoverage |
| 199 | [link]: https://storage.googleapis.com/chromium-browser-clang-staging/ |