# Linux Kernel Module + GCOV ###### tags: `blog` ## 簡介 gcov 是一個測量程式執行 coverage 的工具,原理是編譯程式時在所有branch上插入紀錄函式 便可得知執行期間的執行狀況 通常 gcov 會用在一般 C/C++ 程式上,但是 Linux Kernel 其實也支援 gcov profiling,因此可以用 gcov 來紀錄 Linux Kernel 的 Coverage,當然 Kernel module 也不例外 這篇會分享用 gcov profiling Kernel module 的步驟 ## 1. Kerenl 支援 先確定 Kernel 有支援 gcov profiling,可以在察看現在的 kernel 的 config 是否有以下參數,沒有的話要自己重新編譯一個 ``` CONFIG_DEBUG_FS=y CONFIG_GCOV_KERNEL=y ``` 另外,若也想 profile 整個 Kenel, 則需要 `CONFIG_GCOV_PROFILE_ALL=y` 參數,注意該 kernel 會因此變慢不少,且並非所有硬體架構都支援 若 Kernel 支援,再確認是否已開啟 debugfs ``` ls /sys/kernel/debug/gcov ``` 如果沒有就開啟 ``` mount -t debugfs none /sys/kernel/debug ``` ## 2. 修改 Makefile `Use cp -P (capital P) to never traverse any symbolic link and copy the symbolic link instead.` ```shell PWD=`pwd` $ sudo ls -l sys/kernel/debug/gcov/$PWD sample.gcda sample.gcno ``` sample.gcno 會是 symbolic link 因為在 gcov 資料夾下權限很麻煩 搬回來處理 (cp -P 會直接複製 soft link) ``` $ sudo cp -P /sys/kernel/debug/gcov/$PWD/* . ``` ## 用 gcov 產生可讀的檔案 ``` $ sudo sudo gcov sample.gcda File 'include/linux/kasan-checks.h' Lines executed:0.00% of 2 Creating 'kasan-checks.h.gcov' Cannot open source file include/linux/kasan-checks.h ... ```` ``` $ vim sample.c.gcov 1: 72:static int __init sample_init(void) -: 73:{ 1: 74: printk("Loaded sample module\n"); -: 75: 1: 76: ret_reg_dev = register_chrdev(42, "sample", &sample_ops); 1: 77: if (ret_reg_dev) { #####: 78: printk("Error on register_chrdev\n"); #####: 79: return ret_reg_dev; -: 80: } 1: 81: sample_class = class_create(THIS_MODULE, "sample"); -: 82: 2: 83: if (IS_ERR(sample_class)) { #####: 84: printk("Error on class_create\n"); #####: 85: return PTR_ERR(sample_class); -: 86: } 2: 87: device = 1: 88: device_create(sample_class, NULL, MKDEV(42, 0), NULL, "sample"); -: 89: 2: 90: if (IS_ERR(device)) { #####: 91: printk("Error on device_create\n"); #####: 92: return PTR_ERR(device); -: 93: } -: 94: -: 95: return 0; -: 96:} -: 97: ``` # lcov lcov 會 ``` $ sudo geninfo . -o ./coverage.info Found gcov version: 8.3.0 Scanning . for .gcda files ... Found 1 data files in . Processing sample.gcda Cannot open source file include/linux/kasan-checks.h Cannot open source file include/linux/err.h Cannot open source file include/linux/fs.h ... geninfo: WARNING: skipping empty file arch#x86#include#asm#atomic.h.gcov geninfo: WARNING: skipping empty file arch#x86#include#asm#jump_label.h.gcov ... Finished .info-file creation ``` ``` $ genhtml coverage.info -o html Reading data file coverage.info Found 1 entries. Found common filename prefix "/home/pschen/sslab/kernel_module_example/gup" Writing .css and .png files. Generating output. Processing file module/sample.c Writing directory view page. Overall coverage rate: lines......: 71.2% (37 of 52 lines) functions..: 85.7% (6 of 7 functions) ``` ## 重置 profiling 結果 `sudo echo 1 > /sys/kernel/debug/gcov/resest` ## Reference * http://ltp.sourceforge.net/coverage/lcov.php * https://01.org/linuxgraphics/gfx-docs/drm/dev-tools/gcov.html * http://techvolve.blogspot.com/2014/03/how-to-gcovlcov-for-linux-kernel-modules.html
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up