blob: e91b2a0dd872d3a838a4523fad807b4862c8affe [file] [log] [blame]
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +01001.. _coverage:
2
3Generating coverage reports
4###########################
5
6With Zephyr, you can generate code coverage reports to analyze which parts of
7the code are covered by a given test or application.
8
9You 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
15Test coverage reports in embedded devices or QEMU
16*************************************************
17
18Overview
19========
20`GCC GCOV <gcov_>`_ is a test coverage program
21used together with the GCC compiler to analyze and create test coverage reports
22for your programs, helping you create more efficient, faster running code and
23discovering untested code paths
24
25In Zephyr, gcov collects coverage profiling data in RAM (and not to a file
26system) while your application is running. Support for gcov collection and
27reporting is limited by available RAM size and so is currently enabled only
28for QEMU emulation of embedded targets.
29
30Details
31=======
32There are 2 parts to enable this feature. The first is to enable the coverage for the
33device and the second to enable in the test application. As explained earlier the
34code coverage with gcov is a function of RAM available. Therefore ensure that the
35device has enough RAM when enabling the coverage for it. For example a small device
36like frdm_k64f can run a simple test application but the more complex test
37cases which consume more RAM will crash when coverage is enabled.
38
39To enable the device for coverage, select :option:`CONFIG_HAS_COVERAGE_SUPPORT`
40in the Kconfig.board file.
41
42To report the coverage for the particular test application set :option:`CONFIG_COVERAGE`.
43
44Steps to generate code coverage reports
45=======================================
46
Andrew Boie9354bb52019-06-07 10:50:46 -070047These steps will produce an HTML coverage report for a single application.
48
491. 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 Cufib4766432019-06-13 21:59:12 +020051 enable the configuration manually:
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +010052
Carles Cufib4766432019-06-13 21:59:12 +020053 .. zephyr-app-commands::
Carles Cufib4766432019-06-13 21:59:12 +020054 :board: mps2_an385
55 :gen-args: -DCONFIG_COVERAGE=y
56 :goals: build
57 :compact:
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +010058
Andrew Boie9354bb52019-06-07 10:50:46 -070059#. 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 Cufib4766432019-06-13 21:59:12 +020061 has been printed:
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +010062
Carles Cufib4766432019-06-13 21:59:12 +020063 .. 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 Piedras80c9a6a2019-02-20 16:04:14 +010072
Andrew Boie9354bb52019-06-07 10:50:46 -070073#. Generate the gcov ``.gcda`` and ``.gcno`` files from the log file that was
74 saved::
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +010075
76 $ python3 scripts/gen_gcov_files.py -i log.log
77
Andrew Boie9354bb52019-06-07 10:50:46 -070078#. 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 Piedras80c9a6a2019-02-20 16:04:14 +010081
Andrew Boie9354bb52019-06-07 10:50:46 -070082 $ find $ZEPHYR_SDK_INSTALL_DIR -iregex ".*gcov"
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +010083
Andrew Boie9354bb52019-06-07 10:50:46 -070084#. Create an output directory for the reports::
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +010085
Andrew Boie9354bb52019-06-07 10:50:46 -070086 $ 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 Piedras80c9a6a2019-02-20 16:04:14 +010091
92.. _coverage_posix:
93
94Coverage reports using the POSIX architecture
95*********************************************
96
97When compiling for the POSIX architecture, you utilize your host native tooling
98to build a native executable which contains your application, the Zephyr OS,
99and some basic HW emulation.
100
101That means you can use the same tools you would while developing any
102other desktop application.
103
104To build your application with ``gcc``'s `gcov`_, simply set
105:option:`CONFIG_COVERAGE` before compiling it.
106When you run your application, ``gcov`` coverage data will be dumped into the
107respective ``gcda`` and ``gcno`` files.
108You may postprocess these with your preferred tools. For example:
109
110.. zephyr-app-commands::
111 :zephyr-app: samples/hello_world
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100112 :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 Cufib4766432019-06-13 21:59:12 +0200120 $ ./build/zephyr/zephyr.exe
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100121 # 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
125Sanitycheck coverage reports
Andrew Boief3c624c2019-07-04 14:34:26 -0700126****************************
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100127
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100128Zephyr's :ref:`sanitycheck script <sanitycheck_script>` can automatically
129generate a coverage report from the tests which were executed.
Andrew Boief3c624c2019-07-04 14:34:26 -0700130You just need to invoke it with the ``--coverage`` command line option.
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100131
Andrew Boief3c624c2019-07-04 14:34:26 -0700132For example, you may invoke::
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100133
Andrew Boief3c624c2019-07-04 14:34:26 -0700134 $ sanitycheck --coverage -p qemu_x86 -T tests/kernel
135
136or::
137
138 $ sanitycheck --coverage -p native_posix -T tests/bluetooth
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100139
140which will produce ``sanity-out/coverage/index.html`` with the report.
141
Alberto Escolar Piedras80c9a6a2019-02-20 16:04:14 +0100142.. _gcov:
143 https://gcc.gnu.org/onlinedocs/gcc/Gcov.html
144