一、关于gcov工具

gcov伴随gcc发布。gcc编译加入-fprofile-arcs -ftest-coverage参数生成二进制程序,执行测试用例生成代码覆盖率信息。1、如何使用gcov

用GCC编译的时候加上-fprofile-arcs -ftest-coverage选项,链接的时候也加上。fprofile-arcs参数使gcc创建一个程序的流图,之后找到适合图的生成树。只有不在生成树中的弧被操纵(instrumented):gcc添加了代码来清点这些弧执行的次数。当这段弧是一个块的唯一出口或入口时,操纵工具代码(instrumentation code)将会添加到块中,否则创建一个基础块来包含操纵工具代码。

gcov主要使用.gcno和.gcda两个文件。.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息(而gcda只能在程序运行完毕后才能产生的)。Gcov执行函数覆盖、语句覆盖和分支覆盖。

举个例子,程序代码由main.c和tmp.c两个文件组成,编译、链接、运行程序编译:gcc -fprofile-arcs -ftest-coverage -o myapp main.c tmp.c运行:./myapp然后输入命令:gcov main.c,gcov tmp.c

这个时候当前目录下有了新的文档main.c.gcov,和tmp.c.gcov若想保存覆盖率文件,上述命令修改为:命令:gcov main.c >>yourfilename,gcov tmp.c >>yourfilename

而这时候的main.c.gcov,和tmp.c.gcov就包含了函数和代码执行次数的信息,我们可以查看结果:-:   65:/***************************************************************************************

-:   66: * name         : main

-:   67: * return       : 0 OK

-:   68: *                other ERROR

-:   69: * history      : 2006-06-13

-:   70:****************************************************************************************/

-:   71:int main( int argc, char *argv[] )                                                      /* the entrance for program

*/

function main called 4 returned 100% blocks executed 81%

4:   72:{

4:   73:        int loop = 0 ;

4:   74:        int ret = OK ;

4:   75:        int empty_line = 0 ;

4:   76:        int code_line = 0 ;

4:   77:        int annotation_line = 0 ;

4:   78:        struct stat file_stat ;                                                         /* use for file state */

4:   79:        char recu_name[256] ;

4:   80:        char *pwd = NULL ;

4:   81:        char *tmp = NULL ;

-:   82:

4:   83:        if( argc = MAX_FILE ){                                    /* file size larger than max size */

#####:   98:                        printf( "file [%s] size is over 64K! \ncontinue....\n", argv[loop] ) ;

#####:   99:                        continue ;

-: 100:                }

#####这就是表示没跑到的

各个参数使用如下:gcov [-b] [-c] [-v] [-n] [-l] [-f] [-o directory] sourcefile

-b

Write branch frequencies to the output file, and write branch summary info to the standard output. This option allows you to

see how often each branch in your program was taken.

//b(ranch),分支测试-c

Write branch frequencies as the number of branches taken, rather than the percentage of branches taken.

-v

Display the gcov version number (on the standard error stream).

//太简单了吧,我上面用了-n

Do not create the gcov output file.

-l

Create long file names for included source files. For example, if the header file `x.h' contains code, and was included in the

file `a.c', then running gcov on the file `a.c' will produce an output file called `a.c.x.h.gcov' instead of `x.h.gcov'. This can

be useful if `x.h' is included in multiple source files.

-f

Output summaries for each function in addition to the file level summary.

-o

The directory where the object files live. Gcov will search for `.bb', `.bbg', and `.da' files in this directory.新版的是这么说的-o directory│file

--object-directory directory

--object-file file

Specify either the directory containing the gcov data files, or the

object path name. The .gcno, and .gcda data files are searched for

using this option. If a directory is specified, the data files are

in that directory and named after the source file name, without its

extension. If a file is specified here, the data files are named

after that file, without its extension. If this option is not sup-

plied, it defaults to the current directory.其他的更有新版的-u,

-u

--unconditional-branches

When branch counts are given, include those of unconditional

branches. Unconditional branches are normally not interesting.

-p

--preserve-paths

Preserve complete path information in the names of generated .gcov

files. Without this option, just the filename component is used.

With this option, all directories are used, with ’/’ characters

translated to ’#’ characters, ’.’ directory components removed and

’..’ components renamed to ’^’. This is useful if sourcefiles are

in several different directories. It also affects the -l option.

二、关于lcov

Lcov则是上的gcov结果展现的一个前端,可以将覆盖率信息转换成html展现。

1.如何访问用户空间应用程序代码覆盖数据的示例---------------------------------------------------------------------前提条件:使用GCC以-fprofile-arcs和-ftest-coverage选项编译程序。假设编译目录名称为"/root/test/code_cover/",然后执行:

以我的一个实例/root/test/code_cover/下的fork.c为例看代码的覆盖率:

首先进入/root/test/code_cover/目录a)重置计数器lcov --directory . --zerocounters

b)收集当前代码覆盖状态到一个文件(应用程序启动和停止至少一次后,该命令才能正常工作)lcov --directory . --capture --output-file app.info

Capturing coverage data from .

Found gcov version: 3.4.6

Scanning . for .gcda files ...

Found 1 data files in .

Processing ./TestQuery.gcda

Finished .info-file creation

c)获取HTML输出genhtml -o results app.info

其中的“-o results”是指示结果存放在什么位置的。

Readingdata file app.info

Found 18 entries.

Found common filename prefix "/home/search/isearch_yb/src"

Writing .css and .png files.

Generating output.

Processing file cpp/core/basis/GlobalDef.h

Processing file cpp/core/search/QueryCache.h

...

Writing directory view page.

Overall coverage rate: 117 of 514 lines (22.8%)使用web浏览器打开index.html文件查看代码覆盖结果。

三、Linux内核覆盖率测试:

将最终的gcov内核模块文件复制到system wide modules目录或者PERL脚本所在目录。以root身份,执行:

Gcov:在对Linux内核程序进行代码覆盖率测试时,同样可以采用gcov,但是需要对kernel打一个补丁。Gcov的内核补丁下载地址:。下载gcov内核补丁(wget),解压补丁,然后为一个kernel打补丁(patch –p1 < /home/linux-v3.4-mem/gcov-kernel-2/linux-2.6.18-gcov.patch)注意打补丁时应该处于内核的主目录下也即/home/linux-v3.4-mem,打完补丁之后,通过make menuconfig配置gcov,配置页面显示如下:

配置完毕之后,重新编译内核,将编译成功的gcov这个内核模块/home/linux-v3.4-mem /kernel/gcov/gcov-proc.ko拷贝到网络文件系统下面,arm linux系统启动后加载这个模块

(1)/insmod gcov-proc.ko

然后再跑其他测试程序,跑了一段时间,你会发现在/proc/gcov目录下有各种gcov的统计文件:

/proc/gcov # ls

arch      crypto    init      kernel    security  vmlinux

block     drivers   ipc       net       sound

把这整个目录拷贝到fedora虚拟机下的一个目录,我是拷贝到/nfs/kernel_test/gcov目录下,然后

(2)收集当前代码覆盖状态到一个文件lcov --directory . --output-file kernel.info

(3)获取HTML输出genhtml kernel.info使用web浏览器打开index.html文件查看代码覆盖结果。

上面是怎样获取内核运行代码覆盖率的一般方法及流程。

但如果我们只想获取一个程序运行时的内核代码覆盖率,改怎么办呢???

这里先说几个gcov-proc模块的特性:

(1)模块属性:

我们发现/sys/module/gcov_proc下有parameters文件夹,进去我们发现有两个属性文件:

/sys/module/gcov_proc/parameters # ls

gcov_linkgcov_persist

这两个属性是控制什么的呢???看看官方表述:

- gcov_link=0/1 (default is 1): When set to non-zero, symbolic links to

source and gcov graph files are created in /proc/gcov along with the data

files.

- gcov_persist=0/1 (default is 0): When set to non-zero, gcov data for

kernel modules is kept even after those modules are unloaded so that

coverage measurements can be extended to module cleanup code. To clear

this persistent data, write to /proc/vmlinux.

(2)重启内核覆盖率采集的数据

To reset coverage data for a specific file, simply write to the associated data

file in the /proc/gcov hierarchy:

echo 0 > /proc/gcov/kernel/signal.da

To reset all coverage data, write to the extra file '/proc/gcov/vmlinux':

echo 0 > /proc/gcov/vmlinux

四、获取程序运行时段的内核覆盖率

我的方法是在运行应用程序(这里面是以我的/nfs/memtest/timetest下的timetest应用程序为例)的开始先用“echo 0 > /proc/gcov/vmlinux”指令将我们的/proc/gcov下的统计信息全部清空让它重新计数的,下面说下我们的方法和步骤:

1、先获得一个含义gcov信息的内核和gcov-proc.ko,这个上面已经说过了;

2、启动linux内核,然后安装gcov-proc.ko

/memtest # insmod gcov-proc.ko

gcov-proc: initializing proc module: persist=0 link=1 format=gcc 3.4

gcov-proc: init done

/memtest # cd timetest/

/memtest/timetest # ls

20100106.logfp2timetesttimetest.ctimetest.gcno

20100111.logresultstimetest-lcovtimetest.gcda

3、这时先用“echo 0 > /proc/gcov/vmlinux”指令清空gcov,然后运行timetest,然后将

/proc/gcov拷贝到/tmp下面,这是防止直接拷到虚拟机下会产生大量的网络函数调用,增加统计误差。

/memtest/timetest # echo 0 > /proc/gcov/vmlinux && ./timetest && cp -r /proc/gcov/ /tmp

game over count1 is 0xc9413e40,count2 is 0x0,count3 is 0x3c8b1e45,count4 is 0x0

/memtest/timetest # ls /tmp

gcov

4、现在统计的数据是放在/tp/gcov下面的,我们需要将之拷贝到上位机的虚拟机中才能分析,因为我们的lcov只能在虚拟机中才能运行的。

/memtest/timetest # cp -r /tmp/gcov/ ./

/memtest/timetest #

5、现在需要转到虚拟机下接着运行lcov来产生最后的html页面了。

[root@localhost timetest]# cd gcov/

[root@localhost gcov]# lcov --directory . --capture --output-file timetest.info

[root@localhost gcov]# genhtml -o results timetest.info

这样就生成了最后基本上只运行在timetest时间段的内核代码覆盖率了。

Linux进程核心代码怎么查看,GCOV查看arm-linux代码覆盖率相关推荐

  1. 高效管理 Linux 进程:如何后台执行程序、查看进程、终止任务

    目录 前言 一.nohup命令详解 1-1.nohup命令介绍 1-2.语法格式 1-2-1.基础语法介绍 1-2-2.执行脚本文件 1-2-3.执行python文件 1-2-4.拓展延申:在服务器上 ...

  2. linux 内核 核心代码,8分钟掌握Linux内核分析的核心科技

    原标题:8分钟掌握Linux内核分析的核心科技 作者: OUYANG_LINUX007 来源: http://blog.csdn.net/ouyang_linux007/article/details ...

  3. Linux进程核心调度器之主调度器schedule--Linux进程的管理与调度(十九)

    日期 内核版本 架构 作者 GitHub CSDN 2016-06-30 Linux-4.6 X86 & arm gatieme LinuxDeviceDrivers Linux进程管理与调度 ...

  4. Linux进程描述符task_struct结构体详解--Linux进程的管理与调度(一)

    转自:http://blog.csdn.net/gatieme/article/details/51383272 日期 内核版本 架构 作者 GitHub CSDN 2016-05-12 Linux- ...

  5. linux java web.pdf,Java Web应用在ARM Linux平台上的实现.pdf

    Java Web应用在ARM Linux平台上的实现.pdf lSSN1009-3044 and KnowledgeTechnology电■知识与技术 Computer l-5690963 V01.5 ...

  6. linux进程 crash 分析工具,crash工具分析大型Linux服务器死锁实战

    Linux服务器背景: CPUS: 40 MEMORY: 127.6 GB MACHINE: x86_64 (2199 Mhz) Linux Kernel: 4.4.121 TASKS: 19411 ...

  7. linux 进程监控 工具,你值得拥有:25个Linux性能监控工具

    一段时间以来,我们在网上向读者介绍了如何为Linux以及类Linux操作系统配置多种不同的性能监控工具.在这篇文章中我们将罗列一系列使用最频繁的性能监控工具,并对介绍到的每一个工具提供了相应的简介链接 ...

  8. linux进程管理 pdf,高效与精细的结合--Linux的进程管理.pdf

    高效与精细的结合--Linux的进程管理.pdf 第 卷 第 期 A 文献标识码 I T6L 76 28 L J6 7 8 676 LJ Q Q656 8J6 6 82 K 797863 R28J 2 ...

  9. linux 进程复活,复活意义何在:QQ for Linux 新版测试

    QQ,一个承载了太多国人记忆的聊天软件,也是一个承载了 Linux 用户太多纠结的聊天软件.薄荷君自 2004 年接触 Linux 开始,如何在 Linux 上运行 QQ,就是一个经久不衰的话题.开始 ...

最新文章

  1. 【Ubuntu】使用过的ubuntu工具记录
  2. h264中profile和level的含义
  3. UVALive - 3231 Fair Share(最大流+二分)
  4. php 派生类 构造,C++派生类的构造函数和析构函数
  5. 双端队列 BFS + Chamber of Secrets CodeForces - 173B
  6. python实训第一天
  7. java swing 组件渲染过程,swing组件介绍
  8. 餐厅点餐系统源码(带电脑端和手机端)
  9. Android选项卡TabHost功能和用法
  10. python3中英文混合字符串的分离
  11. 虚幻3和虚幻4_虚幻的非会议
  12. 如何对PDF文件中的内容进行编辑修改
  13. Android 语音识别+语音搜索源码 Voice Search
  14. Failed to load local image resource /pages/pics/cloud://xxxxxxxxx.jpg
  15. Python分布式通用爬虫(4)
  16. 如何在服务器中搭建网站
  17. 微信聊天记录同步电脑
  18. 右键桌面刷新,反应迟钝,刷新后很长一段时间才能显示出桌面图标的解决办法(重点是如何删除workfolders)
  19. 普通二本,学这些东西,也能进大厂
  20. 精通特征工程 —— 2.简单得数字奇特技巧

热门文章

  1. 阿里云马涛:什么是操作系统的云原生?
  2. 高精地图技术专栏 | 基于空间连续性的异常3D点云修复技术
  3. 搭载敏捷飞天底座,阿里云专有云敏捷版全面升级
  4. 阿里云峰会|阿里云数据中台重磅升级后拟扶持100万家企业数智化
  5. 像数据科学家一样思考:12步指南(下)
  6. 服务化改造实践(三) | Dubbo + Zipkin
  7. 聚焦效率与目标差距,数据才是远程办公的内核!
  8. 微服务精华问答 | 如何理解中台战略和微服务
  9. 微服务、Kubernetes和无服务器之后,即将发生的……
  10. 高可用Redis服务架构分析与搭建