develop/test/coverage.html · develop/test/coverage.rst 공식 원문 ↗

Zephyr 3.7.0 · Develop · Testing · Coverage

Coverage report 생성

Gcov data를 RAM이나 host file로 수집하고 gcovr·lcov·Twister report로 변환합니다.

Official pathdevelop/test/coverage.html
Source filedevelop/test/coverage.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

QEMU dump에서 gcda/gcno를 복원하는 6단계, native_sim의 lcov, Twister 자동 coverage와 toolchain별 gcov 선택을 설명합니다.

Embedded

QEMU · RAM dump

POSIX

lcov · gcovr

Twister

coverage · formats

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 0D4DC4EF5A5B764A00DB2A5F61D96E4ADFDD40BFE18A4A5F206220E0E0339CF2
.. _coverage:

Generating coverage reports
###########################

With Zephyr, you can generate code coverage reports to analyze which parts of
the code are covered by a given test or application.

You can do this in two ways:

* In a real embedded target or QEMU, using Zephyr's gcov integration
* Directly in your host computer, by compiling your application targeting
  the POSIX architecture

Test coverage reports in embedded devices or QEMU
*************************************************

Overview
========
`GCC GCOV <gcov_>`_ is a test coverage program
used together with the GCC compiler to analyze and create test coverage reports
for your programs, helping you create more efficient, faster running code and
discovering untested code paths

In Zephyr, gcov collects coverage profiling data in RAM (and not to a file
system) while your application is running. Support for gcov collection and
reporting is limited by available RAM size and so is currently enabled only
for QEMU emulation of embedded targets.

Details
=======
There are 2 parts to enable this feature. The first is to enable the coverage for the
device and the second to enable in the test application. As explained earlier the
code coverage with gcov is a function of RAM available. Therefore ensure that the
device has enough RAM when enabling the coverage for it. For example a small device
like frdm_k64f can run a simple test application but the more complex test
cases which consume more RAM will crash when coverage is enabled.

To enable the device for coverage, select :kconfig:option:`CONFIG_HAS_COVERAGE_SUPPORT`
in the Kconfig.board file.

To report the coverage for the particular test application set :kconfig:option:`CONFIG_COVERAGE`.

Steps to generate code coverage reports
=======================================

These steps will produce an HTML coverage report for a single application.

1. Build the code with CONFIG_COVERAGE=y.

   .. zephyr-app-commands::
      :board: mps2/an385
      :gen-args: -DCONFIG_COVERAGE=y -DCONFIG_COVERAGE_DUMP=y
      :goals: build
      :compact:

#. Capture the emulator output into a log file. You may need to terminate
   the emulator with :kbd:`Ctrl-A X` for this to complete after the coverage dump
   has been printed:

   .. code-block:: console

      ninja -Cbuild run | tee log.log

   or

   .. code-block:: console

      ninja -Cbuild run | tee log.log

#. Generate the gcov ``.gcda`` and ``.gcno`` files from the log file that was
   saved::

     $ python3 scripts/gen_gcov_files.py -i log.log

#. Find the gcov binary placed in the SDK. You will need to pass the path to
   the gcov binary for the appropriate architecture when you later invoke
   ``gcovr``::

     $ find $ZEPHYR_SDK_INSTALL_DIR -iregex ".*gcov"

#. Create an output directory for the reports::

     $ mkdir -p gcov_report

#. Run ``gcovr`` to get the reports::

     $ gcovr -r $ZEPHYR_BASE . --html -o gcov_report/coverage.html --html-details --gcov-executable <gcov_path_in_SDK>

.. _coverage_posix:

Coverage reports using the POSIX architecture
*********************************************

When compiling for the POSIX architecture, you utilize your host native tooling
to build a native executable which contains your application, the Zephyr OS,
and some basic HW emulation.

That means you can use the same tools you would while developing any
other desktop application.

To build your application with ``gcc``'s `gcov`_, simply set
:kconfig:option:`CONFIG_COVERAGE` before compiling it.
When you run your application, ``gcov`` coverage data will be dumped into the
respective ``gcda`` and ``gcno`` files.
You may postprocess these with your preferred tools. For example:

.. zephyr-app-commands::
   :zephyr-app: samples/hello_world
   :gen-args: -DCONFIG_COVERAGE=y
   :host-os: unix
   :board: native_sim
   :goals: build
   :compact:

.. code-block:: console

   $ ./build/zephyr/zephyr.exe
   # Press Ctrl+C to exit
   lcov --capture --directory ./ --output-file lcov.info -q --rc lcov_branch_coverage=1
   genhtml lcov.info --output-directory lcov_html -q --ignore-errors source --branch-coverage --highlight --legend

.. note::

   You need a recent version of lcov (at least 1.14) with support for
   intermediate text format. Such packages exist in recent Linux distributions.

   Alternatively, you can use gcovr (at least version 4.2).

Coverage reports using Twister
******************************

Zephyr's :ref:`twister script <twister_script>` can automatically
generate a coverage report from the tests which were executed.
You just need to invoke it with the ``--coverage`` command line option.

For example, you may invoke::

    $ twister --coverage -p qemu_x86 -T tests/kernel

or::

    $ twister --coverage -p native_sim -T tests/bluetooth

which will produce ``twister-out/coverage/index.html`` report as well as
the coverage data collected by ``gcovr`` tool in ``twister-out/coverage.json``.

Other reports might be chosen with ``--coverage-tool`` and ``--coverage-formats``
command line options.

The process differs for unit tests, which are built with the host
toolchain and require a different board::

    $ twister --coverage -p unit_testing -T tests/unit

which produces a report in the same location as non-unit testing.

.. _gcov:
   https://gcc.gnu.org/onlinedocs/gcc/Gcov.html

Using different toolchains
==========================

Twister looks at the environment variable ``ZEPHYR_TOOLCHAIN_VARIANT``
to check which gcov tool to use by default. The following are used as the
default for the Twister ``--gcov-tool`` argument default:

+-----------+-----------------------+
| Toolchain | ``--gcov-tool`` value |
+-----------+-----------------------+
| host      | ``gcov``              |
+-----------+-----------------------+
| llvm      | ``llvm-cov gcov``     |
+-----------+-----------------------+
| zephyr    | ``gcov``              |
+-----------+-----------------------+

Part 3

한국어 전문 번역

Coverage report 생성

Zephyr에서는 code coverage report를 생성해 주어진 test 또는 application이 code의 어느 부분을 실행했는지 분석할 수 있습니다.

두 가지 방법이 있습니다.

  • 실제 embedded target 또는 QEMU에서 Zephyr gcov integration 사용
  • POSIX architecture target으로 application을 compile해 host computer에서 직접 사용

Embedded device 또는 QEMU의 test coverage report

개요

GCC gcov는 GCC compiler와 함께 사용해 program의 test coverage report를 분석·생성하는 도구입니다. 더 효율적이고 빠른 code를 만들고 test되지 않은 code path를 찾는 데 도움을 줍니다.

Zephyr에서 gcov는 application 실행 중 coverage profiling data를 filesystem이 아닌 RAM에 수집합니다. 사용 가능한 RAM 크기의 제약 때문에 현재 embedded target의 QEMU emulation에서만 gcov 수집·report를 활성화합니다.

세부 사항

이 기능을 활성화하려면 device와 test application 양쪽을 설정해야 합니다. Gcov coverage는 사용 가능한 RAM에 좌우되므로 충분한 RAM이 있는지 확인하십시오. frdm_k64f 같은 작은 device는 단순한 test는 실행할 수 있지만 coverage 활성화 시 더 복잡하고 RAM을 많이 쓰는 test는 crash할 수 있습니다.

Device coverage 지원은 Kconfig.board에서 CONFIG_HAS_COVERAGE_SUPPORT를 선택하고, 특정 test application의 report는 CONFIG_COVERAGE를 설정합니다.

Code coverage report 생성 단계

다음 단계는 application 하나의 HTML coverage report를 생성합니다.

  1. CONFIG_COVERAGE=y와 CONFIG_COVERAGE_DUMP=y로 code를 build합니다.
    west build -b mps2/an385 -- -DCONFIG_COVERAGE=y -DCONFIG_COVERAGE_DUMP=y
  2. Emulator 출력을 log file로 저장합니다. Coverage dump가 출력된 뒤 완료하려면 Ctrl-A X로 emulator를 종료해야 할 수 있습니다.
    ninja -Cbuild run | tee log.log
    또는:
    ninja -Cbuild run | tee log.log
  3. 저장한 log file에서 gcov .gcda와 .gcno file을 생성합니다.
    $ python3 scripts/gen_gcov_files.py -i log.log
  4. SDK에 설치된 gcov binary를 찾습니다. 나중에 gcovr를 호출할 때 해당 architecture의 gcov path를 전달합니다.
    $ find $ZEPHYR_SDK_INSTALL_DIR -iregex ".*gcov"
  5. Report output directory를 만듭니다.
    $ mkdir -p gcov_report
  6. gcovr를 실행해 report를 만듭니다.
    $ gcovr -r $ZEPHYR_BASE . --html -o gcov_report/coverage.html --html-details --gcov-executable <gcov_path_in_SDK>

POSIX architecture를 사용하는 coverage report

POSIX architecture target으로 compile하면 host native toolchain으로 application, Zephyr OS와 기본 hardware emulation이 들어 있는 native executable을 build합니다.

따라서 일반 desktop application을 개발할 때와 같은 도구를 사용할 수 있습니다.

gcc의 gcov로 application을 build하려면 compile 전에 CONFIG_COVERAGE를 설정합니다. Application을 실행하면 gcov data가 해당 gcda와 gcno file에 저장됩니다. 선호하는 도구로 후처리할 수 있습니다.

west build -b native_sim samples/hello_world -- -DCONFIG_COVERAGE=y
$ ./build/zephyr/zephyr.exe
# Press Ctrl+C to exit
lcov --capture --directory ./ --output-file lcov.info -q --rc lcov_branch_coverage=1
genhtml lcov.info --output-directory lcov_html -q --ignore-errors source --branch-coverage --highlight --legend

Twister를 사용하는 coverage report

Zephyr Twister script는 실행한 test에서 coverage report를 자동 생성할 수 있습니다. --coverage command-line option으로 호출합니다.

예:

$ twister --coverage -p qemu_x86 -T tests/kernel

또는:

$ twister --coverage -p native_sim -T tests/bluetooth

twister-out/coverage/index.html report와 gcovr가 수집한 twister-out/coverage.json data가 생성됩니다.

--coverage-tool과 --coverage-formats option으로 다른 report를 선택할 수 있습니다.

Host toolchain으로 build하고 다른 board를 요구하는 unit test는 절차가 다릅니다.

$ twister --coverage -p unit_testing -T tests/unit

Non-unit test와 같은 위치에 report가 생성됩니다.

다른 toolchain 사용

ZEPHYR_TOOLCHAIN_VARIANT environment variable을 확인해 Twister --gcov-tool argument의 기본 도구를 정합니다.

Toolchain--gcov-tool 값
hostgcov
llvmllvm-cov gcov
zephyrgcov

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.