GCC的代码覆盖选项如何工作? [英] How do the code coverage options of GCC work?

查看:224
本文介绍了GCC的代码覆盖选项如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  gcc -fprofile-arcs -ftest-coverage main.c 

code>

它生成g.gv要使用的文件main.gcda来生成覆盖率分析。
那么main.gcda是如何生成的呢?仪器如何完成?我可以看到检测到的代码吗?

解决方案

.gcda不是由编译器生成的;它是您的程序在执行时生成的。



.gcno是在编译时生成的文件,它是'备注文件'。 gcc为每个CU(编译器单元)生成一个基本的块图注释文件(.gcno)。


那么main.gcda是如何生成的?

在运行时统计数据被收集并存储在内存中。某些退出回调已注册,并在程序终止时被调用以将数据写入.gcda文件。这意味着如果您在程序中调用abort()而不是exit(),则不会生成.gcda文件。


已经完成了?我可以看到检测到的代码吗?

您需要检查gcc的实现以获取详细信息,但基本上这些工具是通过将指令该程序用于统计每条指令执行的次数。但它并不需要为每条指令保留一个计数器; GCC使用一些算法来生成程序流程图并为图找到一棵生成树。只有一些特殊的弧线需要检测,并从中产生所有代码分支的覆盖范围。
您可以反汇编该二进制文件以查看代码。
如果您想查看gcc源文件,可以参考以下文件:
$ b toplev.c
coverage.c
profile.c
libgcov.c
gcov.c
gcov-io.c



编辑:一些已知的gcov漏洞仅供参考:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49484



http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28441



http://gcc.gnu.org/bugzilla/show_bug.cgi?id = 44779



http:/ /gcc.gnu.org/bugzilla/show_bug.cgi?id=7970


Consider the following command:

gcc -fprofile-arcs -ftest-coverage main.c

It generates the file, main.gcda, which is to be used by gcov, to generate the coverage analysis. So how does main.gcda is generated? How the instrumentation is done? Can I see the instrumented code?

解决方案

.gcda is not generated by compiler; it's generated by your program when you execute it.

.gcno is the file generated at compilation time and it is the 'note file'. gcc generate a basic block graph notes file (.gcno) for each CU(compiler unit).

So how does main.gcda is generated?

At running time the statistic data is gathered and stored in memory. Some exit callback is registered and is called to write the data to the .gcda file when the program terminates. This means if you call abort() instead of exit() in your program, no .gcda file would be generated.

How the instrumentation is done? Can I see the instrumented code?

You way need check gcc's implementation to get the details but basically the instrumentation is done by inserting instruction to the program to count the number of times each instruction is executed. But it doesn't really have to keep a counter for each instruction; GCC uses some algorithm to generate a program flow graph and finds a spanning tree for the graph. Only some special arcs have to be instrumented and from them the coverage of all code branches can be generated. You can disassemble the binary to see the instrumented code. And here are some files for coverage if you want to look into the gcc source file:

toplev.c coverage.c profile.c libgcov.c gcov.c gcov-io.c

edit: some known gcov bugs FYI:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49484

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28441

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44779

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7970

这篇关于GCC的代码覆盖选项如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆