使用GCOV统计内核代码率

摘要

Gcov是gcc自带的代码覆盖率统计工具,配合lcov工具可以统计并生成代码覆盖率。Linux kernel将Gcov嵌入了原生的内核编译框架中,可以使用gcov统计内核代码覆盖情况。

内核编译

1.版本编译
1)打开kernel GCOV支持
我们需要打开如下的几个config项:
CONFIG_DEBUG_FS=y
CONFIG_GCOV_KERNEL=y
CONFIG_GCOV_FORMAT_AUTODETECT=y
CONFIG_GCOV_PROFILE_ALL=y
2)使用
make; make install等步骤替换系统内核,然后重启系统,进入新编译内核。

覆盖了统计

1.执行下面命令对覆盖率清零

# lcov  -z
Auto-detecting gcov kernel support.
Found upstream gcov kernel support at /sys/kernel/debug/gcov
Resetting kernel execution counters
Done.

2.编译下面测试用例,并执行

# cat test.c
#include 
#include 
#include 
#include 

void main()
{
    open("a", O_WRONLY|O_CREAT);
}

# ./test

3.下面命令统计覆盖率

# lcov -c  -o  kernel.info (统计内核下所有代码覆盖了)
# lcov -c  -k  fs  -o  fs.info(指定内核代码下fs文件夹代码覆盖率)

4.获取 HTML 输出

# genhtml  fs.info -o fs_conv_result 

fs_conv_result 指定生成的文件存放路径,然后文件夹拷贝出来可浏览器打开查看。

5.Linux下查看

# lcov -l fs.info
Reading tracefile fs.info
lcov: WARNING: negative counts found in tracefile fs.info
                                               |Lines      |Functions|Branches
Filename                                       |Rate    Num|Rate  Num|Rate   Num
================================================================================
[/home/cuibixuan/git/linux/]
arch/x86/include/asm/arch_hweight.h            | 0.0%     2|    -   0|    -    0
arch/x86/include/asm/atomic.h                  |64.3%    14|    -   0|    -    0
arch/x86/include/asm/atomic64_64.h             |92.3%    13|    -   0|    -    0
arch/x86/include/asm/barrier.h                 | 100%     1|    -   0|    -    0
arch/x86/include/asm/bitops.h                  |63.3%    30|    -   0|    -    0
arch/x86/include/asm/cpufeature.h              | 0.0%     1|    -   0|    -    0
arch/x86/include/asm/current.h                 | 100%     1|    -   0|    -    0
arch/x86/include/asm/elf.h                     | 100%     7|    -   0|    -    0
arch/x86/include/asm/hugetlb.h                 | 0.0%     1|    -   0|    -    0
arch/x86/include/asm/io.h                      | 0.0%     1|    -   0|    -    0
arch/x86/include/asm/irqflags.h                | 100%     5|    -   0|    -    0
arch/x86/include/asm/jump_label.h              | 100%     1|    -   0|    -    0
arch/x86/include/asm/msr.h                     | 0.0%     2|    -   0|    -    0
arch/x86/include/asm/page.h                    | 0.0%     1|    -   0|    -    0
arch/x86/include/asm/page_64.h                 |66.7%     3|    -   0|    -    0
arch/x86/include/asm/pgtable.h                 | 0.0%    18|    -   0|    -    0
arch/x86/include/asm/pgtable_64.h              | 0.0%     1|    -   0|    -    0
arch/x86/include/asm/pgtable_types.h           | 0.0%     4|    -   0|    -    0
arch/x86/include/asm/pkeys.h                   | 0.0%     1|    -   0|    -    0
arch/x86/include/asm/preempt.h                 | 100%     1|    -   0|    -    0
arch/x86/include/asm/processor.h               | 100%     2|    -   0|    -    0
arch/x86/include/asm/smap.h                    | 100%     2|    -   0|    -    0
arch/x86/include/asm/stacktrace.h              | 0.0%     2|    -   0|    -    0
arch/x86/include/asm/uaccess.h                 |83.3%     6|    -   0|    -    0
arch/x86/include/asm/uaccess_64.h              |37.5%    24|    -   0|    -    0
arch/x86/include/asm/word-at-a-time.h          | 100%     5|    -   0|    -    0
fs/aio.c                                       | 0.7%   591| 2.0%  50|    -    0
fs/anon_inodes.c                               | 0.0%    42| 0.0%   5|    -    0
fs/attr.c                                      |57.1%   112|66.7%   6|    -    0
fs/autofs/autofs_i.h                           | 0.0%    17|    -   0|    -    0
fs/autofs/dev-ioctl.c                          | 0.0%   212| 0.0%  25|    -    0
fs/autofs/expire.c                             | 0.0%   193| 0.0%  13|    -    0
fs/autofs/init.c                               | 0.0%    12| 0.0%   3|    -    0
fs/autofs/inode.c                              | 0.0%   170| 0.0%   9|    -    0
fs/autofs/root.c                               | 0.0%   277| 0.0%  21|    -    0
fs/autofs/symlink.c                            | 0.0%     6| 0.0%   1|    -    0
fs/autofs/waitq.c                              | 0.0%   215| 0.0%   8|    -    0
fs/bad_inode.c                                 | 3.4%    58| 4.0%  25|    -    0
fs/binfmt_elf.c                                |42.2%   661|33.3%  24|    -    0
fs/binfmt_script.c                             |83.7%    49|33.3%   3|    -    0
fs/block_dev.c                                 | 1.9%   748| 6.8%  73|    -    0
fs/btrfs/acl.c                                 | 0.0%    48| 0.0%   4|    -    0
fs/btrfs/async-thread.c                        | 0.0%   133| 0.0%  34|    -    0
fs/btrfs/backref.c                             | 0.0%   760| 0.0%  38|    -    0
fs/btrfs/btrfs_inode.h                         | 0.0%    26|    -   0|    -    0
fs/btrfs/compression.c                         | 0.0%   493| 0.0%  27|    -    0
fs/btrfs/ctree.c                               | 0.0%  2151| 0.0%  86|    -    0

使用GCOV统计内核代码率_第1张图片

你可能感兴趣的:(linux,trace)