Documentation/translations/zh_CN/dev-tools/gcov.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/dev-tools/gcov.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/dev-tools/gcov.rst- Extension
.rst- Size
- 8381 bytes
- Lines
- 265
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
.. include:: ../disclaimer-zh_CN.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
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.