MemCheckカバーは、Valgrind Memcheck分析のためのバッシュヘルパーを提供します。
また、HTMLレポートジェネレーターのフォーマットValgrind Rawレポートを提供し、重要な部分を強調して、これらの結果を解釈するのに役立ちます。
CI環境で簡単に使用して、レポートを自動的に生成できます。
デモンストレーションはどの言葉よりも優れているので、ここで生成されたHTMLレポートの例を見つけることができます。
memcheck_runner.shスクリプトは、ValgrindのMemCheckツールの下でバイナリを実行するラッパーです。
memcheck_runner.shスクリプトが機能するには、Path環境変数を使用して、次のツールをインストールし、アクセスできる必要があります。
bash Bashバージョン4およびアッパーのみがサポートされています。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
[...]
この呼び出しはmy/output/path/ディレクトリ内のfilename.memcheckファイルにValgindのレポートを出力します。この例では、この例で作成されます。
Valgrind分析は、 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 to 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 AWK4.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.
ジェネレーターには2つの必須パラメーターがあります。
--input-dirです。.memcheckファイルを処理します。--output-dir HTMLレポートが生成されるパスを定義します。デフォルトの違反設定が適合しない場合、各違反の重大度を調整できます。
利用可能なすべての臨界設定とそのデフォルト値を備えた構成ファイルは、 --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'] => memcheck_violation_criticalityにはdummy_paramキーが存在しませんこれが次のツリーを持つ世代の例です。
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 | -generate-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 | -generate-configオプションを参照)。
次の連想配列を入力することで定義できます(両方とも必要です)。
memcheck_url_prefix_replacement[ " <path_prefix> " ]= " <repository_url_prefix> "
memcheck_url_prefix_replacement_type[ " <path_prefix> " ]= " <repository_type> "キーpath_prefixは、リポジトリルートディレクトリのファイルパスプレフィックスです。
Value repository_url_prefixは、リポジトリのソースコントロールサーバープレフィックスです。
いくつかの例があります:
値repository_typeは、サポートされているサーバータイプの1つです(ケースは関係ありません):
たとえば、設定:
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>)
複数の代替品を定義できます