Linux平台代码覆盖率测试-从GCC源码中抽取gcov/gcov-dump程序


Content

0.

1. gcov

1.1 gcov 必须的文件

(1) 实现文件

(2) 版本文件

(3) 配置文件

(4) 系统文件

1.2 如何编译生成 gcov

2. gcov-dump

3. gcov-tools

4. 小结

Reference

附:本文代码下载地址

 

 

0.

 

若想研究 gcov/gcov-dump 原理或者代码,深入函数内部跟踪调试是最好的理解方式,但 gcc 的源代码毕竟比较庞大,欲从中抽丝剥茧,往往会被 gcc 的庞大源代码吓住。那么,有没有一种方式,允许我们从 gcc 的源代码中抽取想要研究的程序或代码?

 

有!

 

本文以 gcov 程序为例,说明如何从 GCC 源代码中抽取 gcov/gcov-dump 程序并编译生成可执行的程序。有了这个独立的 gcov/gcov-dump ,研究、调试很方便。想搞清楚 gcc 的内部机理,并非一朝一夕之功,本文只是一种探索,希望对一些想研究 gcc coverage test 的朋友有些帮助。余愿足矣。

 

本文 gcc 源代码版本为 gcc-4.1.2 ,其位置在 /usr/src/gcc-4.1.2 目录, . 表示 /usr/src/gcc-4.1.2

 

1. gcov

 

gcov 程序的输入是一个 .c 文件,前提是已经编译生成了 .gcno 文件并运行可执行程序生成 .gcda 文件; gcov 根据 .c 文件相应的 .gcda 文件和 .gcno 文件生成相应的 .c.gcov 并报告覆盖率测试结果。

 

1.1 gcov 必须的文件

 

(1) 实现文件

 

根据 " Linux 平台代码覆盖率测试 -GCC 如何编译生成 gcov/gcov-dump 程序及其 bug 分析 " 一文的讨论, gcov 所需的 .c 文件有 gcov.c, gcov-io.c, intl.c, error.c, version.c

 

注: gcov-io.c 在编译 gcov 时并没有显示被编译 ( .o 文件 ) ,实际上, gcov-io.c 被包含进了 gcov.c 文件中,请参考 gcov.c 代码。

 

因此,我们需要将这些 .c 文件及其 .h 文件抽取出来。

 

(2) 版本文件

 

gcov-iov.h : 该文件的内容由 ./gcc/gcov-iov 程序生成。请参考 " Linux 平台代码覆盖率测试工具 GCOV 相关文件分析 " 一文。内容如下。

  1. /* Generated automatically by the program `./gcov-iov'  
  2.    from `4.1.2 (4 1) and p (p)'.  */  
  3. #define GCOV_VERSION ((gcov_unsigned_t)0x34303170)  /* 401p */  

(3) 配置文件

 

auto-host.h

config.h

 

其中,

auto-host.h 文件可以使用 ./gcc/configure 程序自动生成,当然,这里的 auto-host.h 文件只需要包含在 gcov 程序中需要的常量,且有些常量需要修改,内容如下。

  1. /* auto-host.h.  Generated from auto-host.h.in by configure.  */  
  2. /* auto-host.h.in.  Generated from configure.ac by autoheader.  */  
  3. /* Define to 1 if you have the <boost/filesystem/path.hpp> header file. */  
  4. #define HAVE_BOOST_FILESYSTEM_PATH_HPP 1   
  5. /* Define to 1 if you have the <boost/graph/graph_utility.hpp> header file. */  
  6. #define HAVE_BOOST_GRAPH_GRAPH_UTILITY_HPP 1   
  7. /* Define to 1 if you have the <dlfcn.h> header file. */  
  8. #define HAVE_DLFCN_H 1   
  9. /* Define to 1 if you have the <inttypes.h> header file. */  
  10. #define HAVE_INTTYPES_H 1   
  11. /* Define to 1 if you have the <memory.h> header file. */  
  12. #define HAVE_MEMORY_H 1   
  13. /* Define to 1 if you have the <stdint.h> header file. */  
  14. #define HAVE_STDINT_H 1   
  15. /* Define to 1 if you have the <stdlib.h> header file. */  
  16. #define HAVE_STDLIB_H 1   
  17. /* Define to 1 if you have the <strings.h> header file. */  
  18. #define HAVE_STRINGS_H 1   
  19. /* Define to 1 if you have the <string.h> header file. */  
  20. #define HAVE_STRING_H 1   
  21. /* Define to 1 if you have the <sys/stat.h> header file. */  
  22. #define HAVE_SYS_STAT_H 1   
  23. /* Define to 1 if you have the <sys/types.h> header file. */  
  24. #define HAVE_SYS_TYPES_H 1   
  25. /* Define to 1 if you have the <unistd.h> header file. */  
  26. #define HAVE_UNISTD_H 1   
  27. /* Name of package */  
  28. #define PACKAGE "gcov"   
  29. /* Define to the address where bug reports for this package should be sent. */  
  30. #define PACKAGE_BUGREPORT "livelylittlefish@gmail.com"   
  31. /* Define to the full name of this package. */  
  32. #define PACKAGE_NAME "gcov"   
  33. /* Define to the full name and version of this package. */  
  34. #define PACKAGE_STRING "gcov 1.0"   
  35. /* Define to the one symbol short name of this package. */  
  36. #define PACKAGE_TARNAME "gcov"   
  37. /* Define to the version of this package. */  
  38. #define PACKAGE_VERSION "1.0"   
  39. /* Define to 1 if you have the ANSI C header files. */  
  40. #define STDC_HEADERS 1   
  41. /* Version number of package */  
  42. #define VERSION "1.0"  

config.h 文件也可以参考 ./gcc/build/config.h( 该文件是在编译 gcc 时自动生成的 ) ,也可自己手写,内容如下。

  1. #ifndef _GCOV_DUMP_CONFIG_H_   
  2. #define _GCOV_DUMP_CONFIG_H_   
  3. #define ATTRIBUTE_NORETURN   
  4. #define ATTRIBUTE_UNUSED   
  5. #define ATTRIBUTE_PRINTF_1   
  6. #define ATTRIBUTE_PRINTF_2   
  7. /**  
  8.  * this macro definition is for the following warning.  
  9.  * In file included from gcov.c:62:  
  10.  * gcov-io.c: In function ‘gcov_allocate’:  
  11.  * gcov-io.c:204: warning: implicit declaration of function ‘xrealloc’  
  12.  * gcov-io.c:204: warning: assignment makes pointer from integer without a cast  
  13.  */  
  14. #define xrealloc realloc   
  15. #include "auto-host.h"   
  16. #ifdef IN_GCC   
  17. /* # include "ansidecl.h" */  
  18. #endif   
  19. #endif /* _GCOV_DUMP_CONFIG_H_ */  
  • 1
  • 2
  • 下一页

相关内容