Documentation/translations/zh_TW/dev-tools/gcov.rst

Source file repositories/reference/linux-study-clean/Documentation/translations/zh_TW/dev-tools/gcov.rst

File Facts

System
Linux kernel
Corpus path
Documentation/translations/zh_TW/dev-tools/gcov.rst
Extension
.rst
Size
8382 bytes
Lines
266
Domain
Support Tooling And Documentation
Bucket
Documentation
Inferred role
Support Tooling And Documentation: documentation
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

.. include:: ../disclaimer-zh_TW.rst

:Original: Documentation/dev-tools/gcov.rst
:Translator: 趙軍奎 Bernard Zhao <bernard@vivo.com>

在Linux內核裏使用gcov做代碼覆蓋率檢查
=====================================

gcov分析核心支持在Linux內核中啓用GCC的覆蓋率測試工具 gcov_ ,Linux內核
運行時的代碼覆蓋率數據會以gcov兼容的格式導出到“gcov”debugfs目錄中,可
以通過gcov的 ``-o`` 選項(如下示例)獲得指定文件的代碼運行覆蓋率統計數據
(需要跳轉到內核編譯路徑下並且要有root權限)::

    # cd /tmp/linux-out
    # gcov -o /sys/kernel/debug/gcov/tmp/linux-out/kernel spinlock.c

這將在當前目錄中創建帶有執行計數註釋的源代碼文件。
在獲得這些統計文件後,可以使用圖形化的gcov前端工具(比如 lcov_ ),來實現
自動化處理Linux內核的覆蓋率運行數據,同時生成易於閱讀的HTML格式文件。

可能的用途:

* 調試(用來判斷每一行的代碼是否已經運行過)
* 測試改進(如何修改測試代碼,儘可能地覆蓋到沒有運行過的代碼)
* 內核最小化配置(對於某一個選項配置,如果關聯的代碼從來沒有運行過,
  是否還需要這個配置)

.. _gcov: https://gcc.gnu.org/onlinedocs/gcc/Gcov.html
.. _lcov: http://ltp.sourceforge.net/coverage/lcov.php


準備
----

內核打開如下配置::

        CONFIG_DEBUG_FS=y
        CONFIG_GCOV_KERNEL=y

獲取整個內核的覆蓋率數據,還需要打開::

        CONFIG_GCOV_PROFILE_ALL=y

需要注意的是,整個內核開啓覆蓋率統計會造成內核鏡像文件尺寸的增大,
同時內核運行也會變慢一些。
另外,並不是所有的架構都支持整個內核開啓覆蓋率統計。

代碼運行覆蓋率數據只在debugfs掛載完成後纔可以訪問::

        mount -t debugfs none /sys/kernel/debug


定製化
------

如果要單獨針對某一個路徑或者文件進行代碼覆蓋率統計,可以在內核相應路
徑的Makefile中增加如下的配置:

- 單獨統計單個文件(例如main.o)::

    GCOV_PROFILE_main.o := y

- 單獨統計某一個路徑::

    GCOV_PROFILE := y

如果要在整個內核的覆蓋率統計(開啓CONFIG_GCOV_PROFILE_ALL)中單獨排除
某一個文件或者路徑,可以使用如下的方法::

    GCOV_PROFILE_main.o := n

Annotation

Implementation Notes