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>)
可以定义多个更换