中間文件類型介紹
? .gcno
(gcov note) : 包含重建基本塊圖和相應(yīng)的塊的源碼的行號的信息.
? .gcda
(gcov data): 包含弧跳變的次數(shù)和其他的概要信息.
? .gcov
:
? .info
: 包含一個或多個源文件所對應(yīng)的覆蓋率信息歹苦,一個源文件對應(yīng)一條“記錄”
.info record format
: TN: <Test name> 表示測試用例名稱玫霎,即通過geninfo中的--test-name選項來命名的測試用例名稱闯狱,默認(rèn)為空圃伶;
SF: <File name> 表示帶全路徑的源代碼文件名;
FN: <函數(shù)啟始行號>, <函數(shù)名>; <函數(shù)有效行總數(shù)>; <函數(shù)有效行總數(shù)中被執(zhí)行個數(shù)]]>
FNDA: <函數(shù)被執(zhí)行的次數(shù)>, <函數(shù)名>; <函數(shù)有效行總數(shù)>; <函數(shù)有效行總數(shù)中被執(zhí)行個數(shù)]]>
FNF: <函數(shù)總數(shù)]]>
FNH: <函數(shù)總數(shù)中被執(zhí)行到的個數(shù)]]>
BRDA: <分支所在行號>, <對應(yīng)的代碼塊編號>, <分支編號>, <執(zhí)行的次數(shù)]]>
BRF: <分支總數(shù)]]>
BRH: <分支總數(shù)中被執(zhí)行到的個數(shù)]]>
DA: <代碼行號>, <當(dāng)前行被執(zhí)行到的次數(shù)]]>
LF: < counts> 代碼有效行總數(shù)
LH: <counts> 代碼有效行總數(shù)中被執(zhí)行到的個數(shù)
end_of_record 一條“記錄”結(jié)束符
Command
gcov
: Option :
: -a
--all-blocks
: Show information for every basic block(基本快如果沒有-a選項,則輸出'main'函數(shù)這個block的執(zhí)行次數(shù),如上所示。使用該選項可以
> Write individual execution counts for every basic block. Normally gcov outputs execution counts only for the main blocks of a line. With this option you can determine if blocks within a single line are not being executed.
? -b
--branch-probabilities
: Include branch probabilities(分支概率) in output.
? -c
--branch-counts
: Given counts of branches(分支計數(shù)) taken rather than percentages.
? -n
--no-output
: Do not create an output file(.gcov).
? -l
--long-file-names
: Use long output file names for included source files.
? -f
--function-summaries
: Output summaries for each function.
? -o
--object-directory DIR|FILE
: Search for object files in DIR or called FILE.
lcov
: GCOV圖形化的前端工具
? Linux Test Project維護(hù)的開放源代碼工具酝陈,最初被設(shè)計用來支持Linux內(nèi)核覆蓋率的度量
? 輸出包括概述、覆蓋率百分比毁涉、圖表沉帮,能快速瀏覽覆蓋率數(shù)據(jù)
? 支持大項目,提供三個級別的視圖:目錄視圖贫堰、文件視圖穆壕、源碼視圖
: Operation :
: -c
--capture
: Capture coverage data
: Options :
: -o
--output-file FILENAME
: Write data to FILENAME instead of stdout.
? -d
--directory DIR
: Use .da
files in DIR instead of kernel. (.gcno .gcda 所在的文件夾)
Procedure
-ftest-coverage
: Produce a notes file that thegcov
code-coverage utility can use to show program coverage. Each source file’s note file is calledauxname.gcno
. Refer to the-fprofile-arcs
option above for a description of auxname and instructions on how to generate test coverage data. Coverage data will match the source files more closely, if you do not optimise.
-fprofile-arcs
: Add code so that program flow arcs are instrumented. During execution the program records how many times each branch and call is executed and how many times it is taken or returns. When the compiled program exits it saves this data to a file calledauxname.gcda
for each source file. The data may be used for profile-directed optimisations (-fbranch-probabilities
), or for test coverage analysis (-ftest-coverage
). Each object file’s auxname is generated from the name of the output file, if explicitly specified and it is not the final executable, otherwise it is the base name of the source file. In both cases any suffix is removed (e.g. ‘foo.gcda’ for input file ‘dir/foo.c’, or ‘dir/foo.gcda’ for output file specified as ‘-o dir/foo.o’).
--coverage
: This option is used to compile and link code instrumented for coverage analysis. The option is a synonym for-fprofile-arcs
-ftest-coverage
(when com- piling) and-lgcov
(when linking). See the documentation for those options for more details.
: Compile the source files with-fprofile-arcs
plus optimisation and code generation options. For test coverage analysis, use the additional-ftest-coverage
option. You do not need to profile every source file in a program.
Link your object files with-lgcov
or-fprofile-arcs
(thelatterimplies the former).
Run the program on a representative workload to generate the arc profile information. This may be repeated any number of times. You can run concurrent instances of your program, and provided that the file system supports locking, the data files will be correctly updated. Also fork calls are detected and correctly handled (double counting will not happen).
For profile-directed optimisations, compile the source files again with the same optimisation and code generation options plus-fbranch-probabilities
.
For test coverage analysis,use gcov to produce human readable information from the.gcno
and.gcda
files. Refer to the gcov documentation for further information.
: With-fprofile-arcs
, for each function of your program GCC creates a program flow graph, then finds a spanning tree for the graph. Only arcs that are not on the spanning tree have to be instrumented: the compiler adds code to count the number of times that these arcs are executed. When an arc is the only exit or only entrance to a block, the instrumentation code can be added to the block; otherwise, a new basic block must be created to hold the instrumentation code.
STEP1: Build & Compile (Generate .gcno
)
? Single File :
gcc -fprofile-arcs -ftest-coverage SRC_FILE.c -o SRC_FILE
```
gcc --coverage SRC_FILE.c -o SRC_FILE
```
? Multiple File* :
Compile
gcc -fprofile-arcs -ftest-coverage -c SRC_FILE.c
Link
```
gcc SRC_FILE.o -o SRC_FILE -lgcov
```
```
gcc SRC_FILE.o –o SRC_FILE -fprofile-arcs
```
```
gcc SRC_FILE.o –o SRC_FILE --coverage
```
STEP2: Run (Generate .gcda
)
./SRC_FILE