發布: 2007-4-29 00:25 |
作者: seanhe |
來源:
www-128.ibm.com |
查看: 137次 | 進入軟件測試論壇討論
領測軟件測試網
高偉斌 , IBM 中國軟件開發中心軟件工程師
2005 年 9 月
javascript:;" onClick="javascript:tagshow(event, 'Rational');" target="_self">Rational PureCoverage 是專門進行自動化運行分析代碼覆蓋信息的工具,是一個單獨的產品,也是 Rational PurifyPlus 家族中的一員?捎糜诟纳茟贸绦可靠性和性能。在 Linux、UNIX 和 Windows 上都可使用和集成,PureCoverage 支持 C/C++、Java、.NET、Visual Basic 和 HTML。它可以實時診斷出應用程序覆蓋的函數或方法(包括調用次數)和代碼行信息。
1 概述
Rational PureCoverage 是專門進行自動化運行分析代碼覆蓋信息的工具,是一個單獨的產品,也是 Rational PurifyPlus 家族中的一員?捎糜诟纳茟贸绦蚩煽啃院托阅。在 Linux、UNIX 和 Windows 上都可使用和集成,PureCoverage 支持 C/C++、Java、.NET、Visual Basic 和 HTML。它可以實時診斷出應用程序覆蓋的函數或方法(包括調用次數)和代碼行信息。
PureCoverage 產品除了有自己專門的用戶界面外,也可以和 Rational ClearQuest、Rational Robot');" target="_self">Robot、Rational Purify、IBM WebSphere Studio、Microsoft Visual Studio .NET、Microsoft Visual Studio 6 和 Microsoft Visual Basic 等無縫的集成,還可以用強大的命令行接口。所有通過圖形界面的 PureCoverage 功能都可用命令行來完成,此外 PureCoverage 還為命令行提供了更多的選項和功能。
本文分別介紹了在 Linux / UNIX 和 Windows 上如何利用 PureCoverage 圖形界面和通過命令行工具分析 C/C++ 程序,對分析結果的整理和報告,以及和用戶環境的集成達到自動診斷和測試的目的。
2 收集覆蓋信息數據
2.1 在 Linux 或 UNIX 下收集覆蓋信息數據
Linux 或 UNIX 下用 PureCoverage 要用 purecov 命令 Build 用戶程序,如果用戶還想通過 PureCoverage 獲得調用所有代碼行的信息,那么用戶程序還需要 Build 成 Debug 版本(如用 -g 選項)。用 purecov 封裝 Build 就是在執行 cc 或 gcc, g++ 前加 purecov 即可,例如:
% purecov cc -g admetadata.cpp
PureCoverage 2003a.06.15 Solaris 2 (32-bit) (c) Copyright IBM Corp. 1992, 2005 All rights reserved.
Instrumenting: crti.o crt1.o values-xa.o admetadata.o libCstd.a...
libC_mtstubs.a. libcx.a... crtn.o Linking
|
然后運行封裝好的可執行程序就可生成此程序所覆蓋的數據,例如:
% ./a.out
**** PureCoverage instrumented a.out (pid 8480 at Mon Aug 31 04:25:00 2005)
* PureCoverage 2003a.06.15 Solaris 2 (32-bit) (c) Copyright IBM Corp. 1992, 2005 All rights reserved.
* For contact information type: "purecov -help"
* Options settings: -purecov \
-purecov-home=/usr/local/pure/releases/purecov.sol.2003a.06.15 \
-cache-dir=/usr/local/pure/releases/purecov.sol.2003a.06.15/cache
* License successfully checked out.
* Command-line: ./a.out
Hello, World
**** PureCoverage instrumented a.out (pid 8480) ****
* Preparing coverage data.. Please note this might take some time..
* Saving coverage data to /net/ma-homes/homes/gaowb/a.out.pcv.
* To view results type: purecov -view /net/ma-homes/homes/gaowb/a.out.pcv
|
運行完后 PureCoverage 會保存覆蓋信息到一個擴展名為 pcv 的二進制文件,如果此文件已經存在, PureCoverage 缺省會更新合并此文件中的覆蓋信息。
Windows 下用 PureCoverage 前先要 Build 好用戶程序,如果用戶還想通過 PureCoverage 獲得調用所有函數和代碼行的信息,那么用戶程序需要 Build 成 Debug 版本。Build 好用戶程序之后就可以用 PureCoverage 圖形界面工具或 coverage 命令行工具加參數生成覆蓋信息數據,PureCoverage 是一個實時診斷工具,所以在生成覆蓋信息數據時它會執行用戶程序。
2.2 在 Windows 下用 PureCoverage 圖形界面收集覆蓋信息數據
啟動"Rational PureCoverage"后,在歡迎界面點擊"Run"按鈕或從文件菜單或工具條選擇"Run"開始一個診斷程序,如圖1:
圖1 PureCoverage 開始運行對話框

在運行過程中 PureCoverage 會首先顯示一個包含當前狀態程序覆蓋情況的概要窗口,如圖2:
圖2 PureCoverage 概要窗口

2.3 在 Windows 下用其他開發工具集成調用 PureCoverage 收集和查看覆蓋信息數據
為方便用戶開發和測試代碼,PureCoverage 可以集成到Microsoft Visual Studio 6,Microsoft Visual Studio .NET,Microsoft Visual Basic,Rational Robot,Rational ClearQuest 和 IBM WebSphere Studio 等開發工具。要在這些集成開發環境中使用 PureCoverage 請確保安裝 PureCoverage 時正確安裝了和這些開發工具的集成。下面的例圖3 是用 Microsoft Visual Studio 6 調用 PureCoverage 收集覆蓋信息數據:
圖3 在 Microsoft Visual Studio 6 中調用 PureCoverage 來收集和查看覆蓋信息數據

2.4 在 Windows 下用命令行工具收集覆蓋信息數據
命令行工具用法如下:
coverage [] []
例如:
coverage /SaveTextData="C:\pamsglib.txt" pamsglib.exe arg1 arg2 arg3
下面是生成的結果實例:
comment PureCoverage text data file:
comment Data fields are separated by tabs.
comment Data appears in the following sections:
comment Comment - These lines plus overall information about the run
comment CoverageData - A spreadsheet-compatible table of tab-separated coverage results
comment SourceLines - A table for each source file showing line numbers with line coverage
comment (SourceLines is only available when at least one module is instrumented in line mode).
comment Program Name S:\bugbert\nt_i386\pvtinstall\tests\admetadata.exe
comment Program Arguments util miscthrow
comment Working Directory S:\bugbert\test\admetadata\harness\util
comment User Name gaowb
comment Program Version 2003.06.03 5352
comment Host Name QWIN154
comment Machine Type Intel Pentium 4 Model 3 Stepping 4
comment O/S Version Windows NT 5.1.2600 Service Pack 2
comment #Processors 1
comment Physical Memory (MBytes) 2038
comment PID 0x1564
comment Language C
comment Start Time 08/31/05 09:00:41
comment Stop Time 08/31/05 09:00:43
comment Dataset Size (bytes) 10313728
comment Elapsed Time (milliseconds) 12767979643649
comment CaseInsensitive DeleteModule Enabled *MFC*.dll
comment CaseInsensitive DeleteModule Enabled *MSVC*RT*.dll
comment CaseInsensitive DeleteModule Enabled *MFC*.dll
comment CaseInsensitive DeleteModule Enabled *MSVC*RT*.dll
comment CaseInsensitive DeleteFile Enabled *Unknown Directory*
comment CaseInsensitive DeleteFile Enabled *Unknown File*
comment CaseInsensitive DeleteFile Enabled *pure_api.c
comment CaseInsensitive DeleteFile Enabled *\\crt\\src*
comment CaseInsensitive DeleteFile Enabled sys*
comment CaseInsensitive DeleteFile Enabled intel*
comment CaseInsensitive DeleteFile Enabled *Unknown Directory*
comment CaseInsensitive DeleteFile Enabled *Unknown File*
comment CaseInsensitive DeleteFile Enabled *pure_api.c
comment CaseInsensitive DeleteFile Enabled *\\crt\\src*
comment CaseInsensitive DeleteFile Enabled sys*
comment CaseInsensitive DeleteFile Enabled intel*
comment CaseInsensitive HideFunction Disabled *unknown_function*
comment CaseInsensitive DeleteFunction Enabled SwitchToFiber
comment CaseInsensitive DeleteFunction Enabled UnnamedFunction*
comment CaseInsensitive DeleteFunction Enabled _*
comment CaseInsensitive DeleteFunction Enabled $E*
comment CaseInsensitive HideFunction Disabled *unknown_function*
comment CaseInsensitive DeleteFunction Enabled SwitchToFiber
comment CaseInsensitive DeleteFunction Enabled UnnamedFunction*
comment CaseInsensitive DeleteFunction Enabled _*
comment CaseInsensitive DeleteFunction Enabled $E*
CoverageData Name Type ParentSource ParentModule
FunctionsUnused FunctionsUsed TotalFunctions FunctionsPercent
LinesUnused LinesUsed TotalLines LinesPercent SourceLine NumberOfCalls
CoverageData ThisRun Run 10137 303 10440
2.90 77049 838 77887 1.08 21931797
CoverageData S:\bugbert\nt_i386\pvtinstall\tests\admetadata.exe Module
98 49 147 33.33 243 260 503 51.69 17283342
CoverageData S:\bugbert\nt_i386\pvtinstall\clearquest\CQPACKAGELIB.DLL Module
99 7 106 6.60 192 15 207 7.25 8478
... ...
CoverageData (Unknown Directory) Directory
S:\bugbert\nt_i386\pvtinstall\tests\admetadata.exe 0 0 0
0.00 0 0 0 0.00 0
CoverageData \sys_nt\MSVC\common\include Directory
S:\bugbert\nt_i386\pvtinstall\tests\admetadata.exe 3 1 4
25.00 5 1 6 16.67 2134067
... ...
CoverageData \bugbert\src\include\pamessage.h SourceFile \bugbert\src\include
S:\bugbert\nt_i386\pvtinstall\tests\admetadata.exe 25 0 25
0.00 25 0 25 0.00 0
CoverageData \sys_nt\MSVC\common\mfc\include\afx.inl SourceFile
\sys_nt\MSVC\common\mfc\include S:\bugbert\nt_i386\pvtinstall\tests\admetadata.exe
9 7 16 43.75 9 7 16 43.75 6412263
... ...
CoverageData CGXGridRangeNameImp::GetRangeName(CGXGridCore *,char const*,CGXRange&)
Function src\gxcrgnam.cpp S:\bugbert\nt_i386\pvtinstall\clearquest\OG701CQAS.DLL
1 0 1 0.0 4 0 4 0.00 119 0
CoverageData CGXGridRangeNameImp::DeleteRangeName
(CGXGridCore *,char const*,CObject *,GXCmdType) Function
src\gxcrgnam.cpp S:\bugbert\nt_i386\pvtinstall\clearquest\OG701CQAS.DLL
1 0 1 0.0 16 0 16 0.00 126 0
... ...
SourceLines \bugbert\src\include\pamessage.h
S:\bugbert\nt_i386\pvtinstall\tests\pamsglib.exe
LineNumber LineCoverage
108.1 0
... ...
153.1 1
... ...
... ...
|
 |
3 查看和過濾覆蓋信息數據
3.1 在 Linux 或 UNIX 下用 PureCoverage 查看器查看和過濾生成的覆蓋信息數據
用 purecov -view 命令打開 PureCoverage 查看器可以顯示覆蓋信息,例如:
% purecov -view a.out.pcv
|
此命令將打開 PureCoverage 查看器圖形窗口,如圖4:
圖4 PureCoverage 查看器

如果在 Build 的時候有 -g 調試選項,就可以點擊 Annotated Source 按鈕或從 Actions 菜單選 Show annotated source 打開源程序行覆蓋信息,如圖5:
圖5 PureCoverage 源程序查看器

PureCoverage 會高亮度顯示未調用的行。
PureCoverage 查看器還提供了幾個常用的報告腳本工具,可以從菜單 File -> Run script 來選擇使用。例如:
pc_annote:生成一個源程序分析結果的文本文件
pc_below:報告低級覆蓋信息
pc_build_diff:比較一個程序新舊版本的 PureCoverage 數據
pc_diff:列出新舊覆蓋數據改變的文件
pc_covdiff:比較統計輸出改變源代碼后的覆蓋信息
pc_email:給最近編輯覆蓋數據的人發送郵件報告
pc_select:識別改變源代碼測試的子集
pc_ssheet:生成一個數據表格式的概要
pc_summary:生成一個制表格式的全部概要
3.2 在 Linux 或 UNIX 下用導出的文本文件查看和過濾生成的覆蓋信息數據
用 purecov -export 命令可以將生成的覆蓋信息數據導出到一個文本文件,例如:
% purecov -export=purecov.txt a.out.pcv
下面是導出結果的結果實例:
###############################################################################
# This is a PureCoverage export file, created 23-Aug-2005 08:39:12 AM #
# Coverage data type: adjusted. #
# #
# Summary of keys and field meanings: #
# #
# to unused-blocks blocks unused-lines lines unused-funcs funcs files dirs #
# di name unused-blocks blocks unused-lines lines unused-funcs funcs files #
# fi name unused-blocks blocks unused-lines lines unused-funcs funcs sessions #
# fu name unused-blocks blocks unused-lines lines calls #
# li line_no hits #
# bl line_no hits #
# efu name #
# efi name #
# edi name #
# #
# In all of these entries, the character separating the values is a TAB, not #
# a space. #
# #
# Values for hits, calls, and sessions stop accumulating once they reach #
# 10000, so a value of 10000 should be taken as 10000 or greater. #
# #
# 'to' -- Total coverage summary. #
# 'di' -- Coverage summary for one directory, followed by the files in the #
# directory. Repeated for each directory. #
# 'edi' -- End of information for named directory. #
# 'fi' -- Coverage summary for one file, followed by the functions in the #
# file. Repeated for each file within a directory. #
# 'efi' -- End of information for named file. #
# 'fu' -- Coverage summary for one function, followed by line and block #
# coverage information for the function. Repeated for each function #
# within the file. #
# 'efu' -- End of information for named function. #
# 'li' -- Use count for the indicated line. Repeated for each line in the #
# function. If coverage adjustments are NOT applied, then the #
# hits figure is the true hits figure for the line. If coverage #
# adjustments ARE applied, then inspected and tested lines will #
# indicate INSP or TEST as appropriate, while other used and unused #
# lines will indicate their counts. Deadcode lines do not appear #
# in export format when adjustments are applied. #
# 'bl' -- Use count for the basic block. Repeated for each block in the #
# function. Note that a single line may contain multiple blocks, #
# and that a single block may span multiple lines. #
###############################################################################
to 3 9 3 8 1 3 1 1
di /net/ma-homes/homes/gaowb/ 3 9 3 8 1 3 1
fi admetadata.cpp 3 9 3 8 1 3 1
fu void display_message(char*) 2 2 2 2 0
bl 19 0
bl 20 0
li 19 0
li 20 0
efu void display_message(char*)
fu main 1 5 1 4 1
bl 6 1
bl 7 1
bl 9 0
bl 10 1
bl 10 1
li 6 1
li 7 1
li 9 0
li 10 1
efu main
fu void display_hello_world() 0 2 0 2 1
bl 14 1
bl 15 1
li 14 1
li 15 1
efu void display_hello_world()
efi admetadata.cpp
edi /net/ma-homes/homes/gaowb/
|
在 PureCoverage 導出的結果中,# 開頭的行表示注釋,包含文件描述、數據類型和結構介紹等。
to 開頭的表示覆蓋結果統計信息,各字段分別是未使用塊、總塊數、未執行行數、總行數、未調用函數或方法的個數、總函數或方法的個數、文件數和目錄數。
di 開頭的表示一個目錄總的覆蓋信息,各字段分別是目錄名、未使用塊、總塊數、未執行行數、總行數、未調用函數或方法的個數、總函數或方法的個數和文件數。
fi 開頭的表示一個文件總的覆蓋信息,各字段分別是文件名、未使用塊、總塊數、未執行行數、總行數、未調用函數或方法的個數、總函數或方法的個數和會話數。
fu 開頭的表示一個函數或方法的覆蓋信息,各字段分別是函數或方法名、未使用塊、總塊數、未執行行數、總行數和被調用的次數。
li 開頭的表示源文件中行的覆蓋信息,各字段分別是行號和執行的次數。
bi 開頭的表示基本塊的覆蓋信息,各字段分別是行號和執行的次數。
efu、efi 和 edi 分別表示該函數或方法、文件和目錄信息結束。
如果用戶覺得 PureCoverage 導出的結果信息太多,可以根據自己需要再過濾,如只想得到調用過的函數或方法,可以用下面的 Perl 語句:
if ( @ARGV != 1 ) { # Specify file name as command line argument
die "Usage: $0 XPORTFILENAME>\n";
}
open iFH, $ARGV[0] or die "Can not open PureCoverage exported file $ARGV[0]: $!";
while ( ) {
if ( /^fu\t.*\t0$/ ) { # Skip not calls function(s)
next;
} elsif ( /^fu\t(.*)\t\d+\t\d+\t\d+\t\d+\t\d+$/ ) { # Get called function(s)
print "$1\n";
}
}
|
3.3 在 Windows 下用 PureCoverage 圖形界面查看和過濾數據
用 PureCoverage 運行完用戶程序以后,選擇 Coverage 瀏覽器窗口可以分等級按源文件顯示覆蓋信息數據,如圖6:
圖6 PureCoverage 瀏覽器窗口

選擇 Coverage 函數列表窗口可以顯示整個程序中函數或方法的覆蓋信息數據,如圖7:
圖7 PureCoverage 函數列表窗口

缺省 PureCoverage 會過濾掉它認為一般用戶不關心的數據,如操作系統模塊,但用戶也可以根據自己的實際需要定制過濾器,PureCoverage 提供了一個圖形化的過濾管理器。從 View 菜單中選擇 Filter Manager 或點擊 Filter Manager 工具條打開過濾管理器窗口,如圖8:
圖8 PureCoverage 過濾管理器

PureCoverage 可以在源代碼中一行一行的識別覆蓋情況,雙擊一個函數或方法,就會打開源程序查看器窗口,如圖9:
圖9 PureCoverage 源程序查看器

缺省 PureCoverage 未測到的行用紅色表示,已測到的行用藍色表示,固定行用黑色表示,在多塊行中部分測到用粉色表示,總報告用綠色表示。用戶可根據自己的喜好定制顏色。
3.4 在 Windows 下查看和過濾用命令行工具生成的覆蓋信息數據
在 PureCoverage 命令行工具生成的結果中,comment 開頭的行表示注釋,包含文件結構簡單介紹、用戶程序信息、工作路徑、PureCoverage 版本信息、運行機器信息、運行時間統計、數據集大小、過濾器信息等。
CoverageData 開頭的表示覆蓋結果信息,CoverageData 第一行說明各字段含義,用 Tab 字符隔開;第二行是總的統計報告,包括未使用和使用的函數或方法、總函數或方法、使用函數或方法的百分比、未使用和使用的代碼行數、總行數、使用行數百分比和總調用次數等;從第三行開始是具體收集到的模塊、目錄、源文件和函數或方法等信息,每行都有名稱、類型、父源代碼、父模塊、未使用和使用的函數或方法、總函數或方法、使用函數或方法的百分比、未使用和使用的代碼行數、總行數、使用行數百分比、源代碼行號和總調用次數等。
SourceLines 開頭的表示源程序中行號的覆蓋信息。
如果用戶覺得 PureCoverage 生成的結果信息太多,可以根據自己需要再過濾,如只想得到調用過的函數或方法,可以用下面的 Perl 語句:
if ( @ARGV != 1 ) { # Specify file name as command line argument
die "Usage: $0 \n";
}
open iFH, $ARGV[0] or die "Can not open PureCoverage text data file $ARGV[0]: $!";
while ( ) {
if ( /^CoverageData\t.*\t0$/ ) { # Skip not calls function(s)
next;
} elsif ( /^CoverageData\t(.*)\tFunction\t.*/ ) { # Get called function(s)
print "$1\n";
}
}
|
4 集成
4.1 在 Linux 或 UNIX 下和用戶環境的集成
只需要將 "purecov " 添加到編譯器 cc、gcc 或 g++ 命令的前面即可;蛟黾右粋 Build 規則在 Makefile 中,例如:
pc_relink:
admetadata.o: admetadata.cpp
purecov cc admetadata.cpp
|
如果用戶環境中可執行程序被調用多次或指定程序不同的參數,PureCoverage 會自動合并生成的結果。
再用"purecov -export"命令將 .pcv 文件批量轉換成覆蓋信息數據文本文件,也可再進一步過濾得出恰當的結果。
4.2 在 Windows 下和用戶環境的集成
只需要將 "coverage /SaveTextData " 添加到可執行程序的前面即可。如果用戶環境中可執行程序被調用多次或指定程序不同的參數,那么用戶可以用 /SaveMergeTextData 代替 /SaveTextData 選項來合并生成的結果。
可再進一步過濾得出恰當的結果。
注釋
1) 本文所述的 Linux 或 UNIX 是指只要支持任何一個標準 cc、gcc 或 g++ 編譯器的任何 Linux 或 UNIX。
2) 查看所有命令行選項可用 purecov -help(Linux/UNIX)或 coverage /help(Windows)命令。
3) PureCoverage 缺省監聽級別是最詳細的代碼行級,也可以用-purecov-granularity=function 或者/CollectionGranularity=function 定制到函數或方法級或其他級別以收集到更精簡或符合用戶需要的數據。
4) 如果用戶不能獲得想要的函數或代碼行,請確認程序是 Debug 版,即 Linux/UNIX 下 Build 的時候用了 -g 編譯選項,如果用戶用的是 MFC,請確認有 /ZI 編譯選項。
5) 如果用戶覺得缺省的 filter 不太合適,可以用 /FilterFiles 選項自己定制 filter。
6) PureCoverage 有自動調節行覆蓋功能,例如在某些情況下不想統計某些行,用此功能可能更滿足客戶的需求。
7) Windows 下 coverage 是在 Rational/common 目錄下,如果不能執行請檢查此目錄是否在 PATH 環境變量里。
8) 如果想在 Windows 下查看 Linux 或 UNIX 上生成的覆蓋信息數據,可用 -view-file-format 選項生成 Windows PureCoverage 可識別的 .cfy 覆蓋信息數據文件,但要注意 Linux 或 UNIX 下不會合并 .cfy 文件。
參考資料
關于作者
 |
|
高偉斌,IBM 中國軟件開發中心軟件工程師。參與過 IBM 軟件開發和 WebSphere、Java 和 Rational 等的軟件測試,熟悉Globalization、數據庫等技術,最近主要從事 Rational 產品的 Globalization 開發。感謝楊曉斌、吳嘉、蔡林、田明君等同事和家人的支持。 |