MEMCHECK蓋提供了用於Valgrind Memcheck分析的狂歡助理。
它還提供了HTML報告生成器格式化Valgrind Raw Reports並突出顯示重要部分以幫助您解釋這些結果。
它可以輕鬆地在您的CI環境中用於自動生成報告。
演示比任何單詞都更好,您可以在此處找到生成的HTML報告示例。
memcheck_runner.sh腳本是一個包裝器,它將在Valgrind的Memcheck工具下運行二進製文件。
對於memcheck_runner.sh腳本的工作,必須使用路徑環境變量安裝和訪問以下工具:
bash BASH版本4和鞋面僅受支持。valgrind Valgrind版本3.13進行了測試,較舊的版本可能無法按預期工作這是其用法(可以使用--help選項訪問):
Usage: memcheck_runner.sh [OPTIONS]... -- [BIN] [BIN_ARG]...
Runs the BIN with the BIN_ARGs through valgrind memcheck analyser.
Options:
-h|--help Displays this help message.
-i|--ignore=FILE Provides valgrind FILE as the suppression file.
-o|--output-name=NAME [MANDATORY] Defines the output file name
(will be suffixed with the .memcheck extension).
-s|--gen-suppressions Enables valgrind suppression generation in the output
file, those can be used to create a suppression file.
--fullpath-after= (with nothing after the '=')
Show full source paths in call stacks.
--fullpath-after=STR Like --fullpath-after=, but only show the part of the
path after 'STR'. Allows removal of path prefixes.
Use this flag multiple times to specify a set of
prefixes to remove.
唯一的強制性選項是--output-name ,它將定義輸出文件路徑和名稱。
如果不存在路徑,將創建它。
.memcheck擴展名將自動添加到其中,以兼容generate_html_report.sh腳本。
例子:
$ memcheck_runner.sh --output-name " my/output/path/filename " -- true can take useless params and still be one true self
Info: Output file set to: ' my/output/path/filename.memcheck '
Info: Creating output directory ' my/output/path/ '
Info: Running the following cmd with valgrind:
" true " " can " " take " " useless " " params " " and " " still " " be " " one " " true " " self " 您可以使用--ignore選項指定違規抑製文件。
這樣的抑製文件必須遵循Valgrind的抑製文件規則。
可以在報告中使用--gen-suppressions選項生成抑制。
報告中的抑製作用看起來像是這樣:
[...]
==21849== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==21849== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21849== by 0x108687: breakage::evil_definitely_lost_func() (main.cpp:4)
==21849== by 0x10869B: main (main.cpp:10)
==21849==
{
<insert_a_suppression_name_here>
Memcheck:Leak
match-leak-kinds: definite
fun:_Znwm
fun:_ZN8breakage25evil_definitely_lost_funcEv
fun:main
}
==21849== LEAK SUMMARY:
==21849== definitely lost: 4 bytes in 1 blocks
[...]
此調用將將Valgind的報告輸出到filename.memcheck文件中,在此示例中創建了my/output/path/ DIRECTORY中的內容。
使用true二進制運行閥門分析,將其傳遞到許多參數。
來自Valgrind的文檔:
By default Valgrind only shows the filenames in stack traces, but not full paths to source files.
When using Valgrind in large projects where the sources reside in multiple different directories, this can be inconvenient.
`--fullpath-after` provides a flexible solution to this problem.
When this option is present, the path to each source file is shown, with the following all-important caveat:
if string is found in the path, then the path up to and including string is omitted, else the path is shown unmodified.
Note that string is not required to be a prefix of the path.
For example, consider a file named /home/janedoe/blah/src/foo/bar/xyzzy.c. Specifying --fullpath-after=/home/janedoe/blah/src/ will cause Valgrind to show the name as foo/bar/xyzzy.c.
Because the string is not required to be a prefix, --fullpath-after=src/ will produce the same output. This is useful when the path contains arbitrary machine-generated characters.
For example, the path /my/build/dir/C32A1B47/blah/src/foo/xyzzy can be pruned to foo/xyzzy using --fullpath-after=/blah/src/.
If you simply want to see the full path, just specify an empty string: --fullpath-after=. This isn't a special case, merely a logical consequence of the above rules.
Finally, you can use --fullpath-after multiple times. Any appearance of it causes Valgrind to switch to producing full paths and applying the above filtering rule.
Each produced path is compared against all the --fullpath-after-specified strings, in the order specified. The first string to match causes the path to be truncated as described above.
If none match, the full path is shown.
This facilitates chopping off prefixes when the sources are drawn from a number of unrelated directories.
將--fullpath-after傳遞到memcheck_runner.sh將其直接將其轉發到Valgrind,具有先前描述的效果。
目前,Valgrind的選項無法自定義。
使用以下選項:
--track-origins=yes--leak-check=full--show-reachable=yes--show-leak-kinds=all--show-leak-kinds=all--show-leak-kinds=definite,indirect,possible,reachable--num-callers=50--fair-sched=yes有關更多詳細信息,請參閱Valgrind的文檔。
generate_html_report.sh腳本將在給定目錄中生成帶有所有Memcheck結果文件的HTML報告。
為了使generate_html_report.sh腳本工作,必須使用路徑環境變量安裝和訪問以下工具:
bash BASH版本4和鞋面僅受支持。gawk GNU AWK 4.1.4經過測試,較舊的版本可能無法按預期工作這是其用法(可以使用--help選項訪問):
Usage: generate_html_report.sh [OPTIONS]...
Parses all .memcheck files from a given directory and generates an HTML
report.
Options:
-h|--help Displays this help message.
-g|--generate-config Generates a 'memcheck-cover.config' file in the current
directory, containing the default configuration values.
-c|--config=FILE Loads the configuration from FILE. An example
configuration file can be generated using the
--generate-config option.
If this option is not set, or values are missing in
FILE, the default values will be used.
-i|--input-dir=DIR [MANDATORY] Defines the input directory where the
.memcheck files are.
The files will be searched in directories recursivly.
-o|--output-dir=DIR [MANDATORY] Defines the output directory where the
HTML report will be produced.
發電機有兩個強制性參數:
--input-dir定義了要處理的備忘錄所在的路徑。.memcheck文件。--output-dir 。如果默認違規設置不適合您,則可以調整每個違規行為。
具有所有可用關鍵性設置及其默認值的配置文件可以使用--generate-config選項生成:
$ generate_html_report.sh --generate-config
Info: Generating configuration with default values: ' memcheck-cover.config ' ...
Done. The generated configuration can be modified and then loaded
by the current script using the --config option.
If a violation is not set, the default value will be used.默認配置文件是在文件memcheck-cover.config中的當前目錄中生成的。
然後,您可以更改任何關鍵,以適應您的需求,並使用--config選項將其傳遞給發電機。
如果文件中缺少任何關鍵性,則將應用一個默認情況。
傳遞具有無效參數或參數值的任何配置文件將導致錯誤。
例如:
Error: Invalid configuration value 'dummy' for parameter: memcheck_violation_criticality['contains_unaddressable'] => dummy臨界性不存在Error: Invalid configuration parameter: memcheck_violation_criticality['dummy_param'] => dummy_param鍵不存在memcheck_violation_criticality這是一個一代示例,帶有以下樹:
my/output/path/
├── another_dir/
│ ├── a_report.memcheck
│ └── other_report.memcheck
└── filename.memcheck
讓我們運行HTML發電機:
generate_html_report.sh --input-dir " my/output/path/ " --output-dir " my/output/path/report/ " --config memcheck-cover.config
Info: Input directory set to: ' my/output/path/ '
Info: Loading configuration from file ' memcheck-cover.config ' ...
Info: Processing memcheck file ' filename.memcheck ' ...
Info: Processing memcheck file ' another_dir/a_report.memcheck ' ...
Info: Processing memcheck file ' another_dir/other_report.memcheck ' ...
Info: Generating index.html...結果看起來像這樣:
my/output/path/report/
├── another_dir/
│ ├── a_report.memcheck.html.part.js
│ └── other_report.memcheck.html.part.js
├── filename.memcheck.html.part.js
├── index.html
├── memcheck-cover.css
└── memcheck-cover.js
此選項僅通過配置文件可用(請參閱-g | - 生成 - config選項)。
可以通過填充以下關聯數組來定義它:
memcheck_path_prefix_replacement[ " <prefix_to_replace> " ]= " <replacement_value> "密鑰prefix_to_replace是要替換的路徑前綴
值replacement_value是替換值(可以將其空為空以完全刪除前綴)
例如,設置:
memcheck_path_prefix_replacement[ " /var/user/repo " ]= " <repo> "將轉換以下報告行:
==1== at 0x10101042: myFunc() (/var/user/repo/src/lib1/MyClass.cpp:14)
到:
==1== at 0x10101042: myFunc() (<repo>/src/lib1/MyClass.cpp:14)
可以定義多個更換
此選項僅通過配置文件可用(請參閱-g | - 生成 - config選項)。
可以通過填充以下關聯數組來定義它:
memcheck_url_prefix_replacement[ " <path_prefix> " ]= " <repository_url_prefix> "
memcheck_url_prefix_replacement_type[ " <path_prefix> " ]= " <repository_type> "其中鍵path_prefix是存儲庫根目錄的文件路徑前綴。
值repository_url_prefix是存儲庫的源控制服務器前綴。
這裡有一些例子:
值repository_type是受支持的服務器類型之一(情況無關緊要):
例如,設置:
memcheck_url_prefix_replacement[ " /var/user/repo/ " ]= " https://github.com/example/example_project/blob/master/ "
memcheck_url_prefix_replacement_type[ " /var/user/repo/ " ]= " github "將轉換以下報告行:
==1== at 0x10101042: myFunc() (/var/user/repo/src/lib1/MyClass.cpp:14)
到:
==1== at 0x10101042: myFunc() (<a href="https://github.com/example/example_project/blob/master/src/lib1/MyClass.cpp#L14">/var/user/repo/src/lib1/MyClass.cpp:14</a>)
可以定義多個更換