Documentation/translations/zh_CN/dev-tools/kmemleak.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/dev-tools/kmemleak.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/dev-tools/kmemleak.rst- Extension
.rst- Size
- 10260 bytes
- Lines
- 230
- 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.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
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
.. SPDX-License-Identifier: GPL-2.0
.. include:: ../disclaimer-zh_CN.rst
:Original: Documentation/dev-tools/kmemleak.rst
:Translator: 刘浩阳 Haoyang Liu <tttturtleruss@hust.edu.cn>
内核内存泄露检测器
==================
Kmemleak 提供了一个类似 `可追踪的垃圾收集器 <https://en.wikipedia.org/wiki/Tra
cing_garbage_collection>`_ 的方法来检测可能的内核内存泄漏,不同的是孤立对象不会
被释放,而是仅通过 /sys/kernel/debug/kmemleak 报告。Valgrind 工具
(``memcheck --leak-check``)使用了一种相似的方法来检测用户空间应用中的内存泄
露。
用法
----
"Kernel hacking" 中的 CONFIG_DEBUG_KMEMLEAK 必须被启用。一个内核线程每10分钟
(默认情况下)扫描一次内存,并且打印出新发现的未被引用的对象个数。
如果 ``debugfs`` 没有挂载,则执行::
# mount -t debugfs nodev /sys/kernel/debug/
显示所有扫描出的可能的内存泄漏的细节信息::
# cat /sys/kernel/debug/kmemleak
启动一次中等程度的内存扫描::
# echo scan > /sys/kernel/debug/kmemleak
清空当前所有可能的内存泄露列表::
# echo clear > /sys/kernel/debug/kmemleak
当再次读取 ``/sys/kernel/debug/kmemleak`` 文件时,将会输出自上次扫描以来检测到的
新的内存泄露。
注意,孤立目标是通过被分配时间来排序的,列表开始的对象可能会导致后续的对象都被
识别为孤立对象。
可以通过写入 ``/sys/kernel/debug/kmemleak`` 文件在运行时修改内存扫描参数。下面是
支持的参数:
* off
禁用 kmemleak(不可逆)
* stack=on
开启任务栈扫描(默认)
* stack=off
禁用任务栈扫描
* scan=on
开启自动内存扫描线程(默认)
* scan=off
关闭自动内存扫描线程
* scan=<secs>;
设定自动内存扫描间隔,以秒为单位(默认值为 600,设置为 0 表示停
止自动扫描)
* scan
触发一次内存扫描
* clear
通过标记所有当前已报告的未被引用对象为灰,从而清空当前可能的内存泄露列
表;如果 kmemleak 被禁用,则释放所有 kmemleak 对象,。
* dump=<addr>
输出存储在 <addr> 中的对象信息
可以通过在内核命令行中传递 ``kmemleak=off`` 参数从而在启动时禁用 Kmemleak。
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.