Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 1 | .. _coverage: |
| 2 | |
| 3 | Generating coverage reports |
| 4 | ########################### |
| 5 | |
| 6 | With Zephyr, you can generate code coverage reports to analyze which parts of |
| 7 | the code are covered by a given test or application. |
| 8 | |
| 9 | You can do this in two ways: |
| 10 | |
| 11 | * In a real embedded target or QEMU, using Zephyr's gcov integration |
| 12 | * Directly in your host computer, by compiling your application targeting |
| 13 | the POSIX architecture |
| 14 | |
| 15 | Test coverage reports in embedded devices or QEMU |
| 16 | ************************************************* |
| 17 | |
| 18 | Overview |
| 19 | ======== |
| 20 | `GCC GCOV <gcov_>`_ is a test coverage program |
| 21 | used together with the GCC compiler to analyze and create test coverage reports |
| 22 | for your programs, helping you create more efficient, faster running code and |
| 23 | discovering untested code paths |
| 24 | |
| 25 | In Zephyr, gcov collects coverage profiling data in RAM (and not to a file |
| 26 | system) while your application is running. Support for gcov collection and |
| 27 | reporting is limited by available RAM size and so is currently enabled only |
| 28 | for QEMU emulation of embedded targets. |
| 29 | |
| 30 | Details |
| 31 | ======= |
| 32 | There are 2 parts to enable this feature. The first is to enable the coverage for the |
| 33 | device and the second to enable in the test application. As explained earlier the |
| 34 | code coverage with gcov is a function of RAM available. Therefore ensure that the |
| 35 | device has enough RAM when enabling the coverage for it. For example a small device |
| 36 | like frdm_k64f can run a simple test application but the more complex test |
| 37 | cases which consume more RAM will crash when coverage is enabled. |
| 38 | |
| 39 | To enable the device for coverage, select :option:`CONFIG_HAS_COVERAGE_SUPPORT` |
| 40 | in the Kconfig.board file. |
| 41 | |
| 42 | To report the coverage for the particular test application set :option:`CONFIG_COVERAGE`. |
| 43 | |
| 44 | Steps to generate code coverage reports |
| 45 | ======================================= |
| 46 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 47 | These steps will produce an HTML coverage report for a single application. |
| 48 | |
| 49 | 1. Build the code with CONFIG_COVERAGE=y. Some boards like qemu_x86_coverage |
| 50 | automatically enable this, but for boards that do not you will need to |
Carles Cufi | b476643 | 2019-06-13 21:59:12 +0200 | [diff] [blame] | 51 | enable the configuration manually: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 52 | |
Carles Cufi | b476643 | 2019-06-13 21:59:12 +0200 | [diff] [blame] | 53 | .. zephyr-app-commands:: |
Carles Cufi | b476643 | 2019-06-13 21:59:12 +0200 | [diff] [blame] | 54 | :board: mps2_an385 |
| 55 | :gen-args: -DCONFIG_COVERAGE=y |
| 56 | :goals: build |
| 57 | :compact: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 58 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 59 | #. Capture the emulator output into a log file. You may need to terminate |
| 60 | the emulator with :kbd:`Ctrl-A X` for this to complete after the coverage dump |
Carles Cufi | b476643 | 2019-06-13 21:59:12 +0200 | [diff] [blame] | 61 | has been printed: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 62 | |
Carles Cufi | b476643 | 2019-06-13 21:59:12 +0200 | [diff] [blame] | 63 | .. code-block:: console |
| 64 | |
| 65 | ninja -Cbuild run | tee log.log |
| 66 | |
| 67 | or |
| 68 | |
| 69 | .. code-block:: console |
| 70 | |
| 71 | ninja -Cbuild run | tee log.log |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 72 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 73 | #. Generate the gcov ``.gcda`` and ``.gcno`` files from the log file that was |
| 74 | saved:: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 75 | |
| 76 | $ python3 scripts/gen_gcov_files.py -i log.log |
| 77 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 78 | #. Find the gcov binary placed in the SDK. You will need to pass the path to |
| 79 | the gcov binary for the appropriate architecture when you later invoke |
| 80 | ``gcovr``:: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 81 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 82 | $ find $ZEPHYR_SDK_INSTALL_DIR -iregex ".*gcov" |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 83 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 84 | #. Create an output directory for the reports:: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 85 | |
Andrew Boie | 9354bb5 | 2019-06-07 10:50:46 -0700 | [diff] [blame] | 86 | $ mkdir -p gcov_report |
| 87 | |
| 88 | #. Run ``gcovr`` to get the reports:: |
| 89 | |
| 90 | $ gcovr -r $ZEPHYR_BASE . --html -o gcov_report/coverage.html --html-details --gcov-executable <gcov_path_in_SDK> |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 91 | |
| 92 | .. _coverage_posix: |
| 93 | |
| 94 | Coverage reports using the POSIX architecture |
| 95 | ********************************************* |
| 96 | |
| 97 | When compiling for the POSIX architecture, you utilize your host native tooling |
| 98 | to build a native executable which contains your application, the Zephyr OS, |
| 99 | and some basic HW emulation. |
| 100 | |
| 101 | That means you can use the same tools you would while developing any |
| 102 | other desktop application. |
| 103 | |
| 104 | To build your application with ``gcc``'s `gcov`_, simply set |
| 105 | :option:`CONFIG_COVERAGE` before compiling it. |
| 106 | When you run your application, ``gcov`` coverage data will be dumped into the |
| 107 | respective ``gcda`` and ``gcno`` files. |
| 108 | You may postprocess these with your preferred tools. For example: |
| 109 | |
| 110 | .. zephyr-app-commands:: |
| 111 | :zephyr-app: samples/hello_world |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 112 | :gen-args: -DCONFIG_COVERAGE=y |
| 113 | :host-os: unix |
| 114 | :board: native_posix |
| 115 | :goals: build |
| 116 | :compact: |
| 117 | |
| 118 | .. code-block:: console |
| 119 | |
Carles Cufi | b476643 | 2019-06-13 21:59:12 +0200 | [diff] [blame] | 120 | $ ./build/zephyr/zephyr.exe |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 121 | # Press Ctrl+C to exit |
| 122 | lcov --capture --directory ./ --output-file lcov.info -q --rc lcov_branch_coverage=1 |
| 123 | genhtml lcov.info --output-directory lcov_html -q --ignore-errors source --branch-coverage --highlight --legend |
| 124 | |
| 125 | Sanitycheck coverage reports |
Andrew Boie | f3c624c | 2019-07-04 14:34:26 -0700 | [diff] [blame] | 126 | **************************** |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 127 | |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 128 | Zephyr's :ref:`sanitycheck script <sanitycheck_script>` can automatically |
| 129 | generate a coverage report from the tests which were executed. |
Andrew Boie | f3c624c | 2019-07-04 14:34:26 -0700 | [diff] [blame] | 130 | You just need to invoke it with the ``--coverage`` command line option. |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 131 | |
Andrew Boie | f3c624c | 2019-07-04 14:34:26 -0700 | [diff] [blame] | 132 | For example, you may invoke:: |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 133 | |
Andrew Boie | f3c624c | 2019-07-04 14:34:26 -0700 | [diff] [blame] | 134 | $ sanitycheck --coverage -p qemu_x86 -T tests/kernel |
| 135 | |
| 136 | or:: |
| 137 | |
| 138 | $ sanitycheck --coverage -p native_posix -T tests/bluetooth |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 139 | |
| 140 | which will produce ``sanity-out/coverage/index.html`` with the report. |
| 141 | |
Alberto Escolar Piedras | 80c9a6a | 2019-02-20 16:04:14 +0100 | [diff] [blame] | 142 | .. _gcov: |
| 143 | https://gcc.gnu.org/onlinedocs/gcc/Gcov.html |
| 144 | |