يوفر غطاء Memcheck مساعد باش لتحليل Valgrind Memcheck.
كما يوفر أيضًا مولد تقارير HTML تنسيق تقارير Valgrind RAW وتسليط الضوء على الأجزاء المهمة لمساعدتك على تفسير تلك النتائج.
يمكن استخدامه بسهولة في بيئة CI لإنشاء التقارير تلقائيًا.
مظاهرة أفضل من أي كلمات ، يمكنك العثور على مثال تقرير HTML تم إنشاؤه هنا.
البرنامج النصي memcheck_runner.sh عبارة عن غلاف سيشغل ثنائيًا تحت أداة Memcheck الخاصة بـ Valgrind.
لكي يعمل برنامج 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 ، والذي تم إنشاؤه في هذا المثال.
تم تشغيل تحليل 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 أن 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 بإنشاء تقرير HTML مع جميع ملفات نتائج Memcheck في دليل معين.
من أجل عمل generate_html_report.sh SCRIPT ، يجب تثبيت الأدوات التالية والوصول إليها باستخدام متغير بيئة المسار:
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 الذي يحدد المسار الذي سيتم فيه إنشاء تقرير 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'] => مفتاح 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 |-خيار generate-config ).
يمكن تعريفه عن طريق ملء المصفوفة الترابطية التالية:
memcheck_path_prefix_replacement[ " <prefix_to_replace> " ]= " <replacement_value> " حيث يتم استبدال prefix_to_replace المفاتيح
و Value 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> " حيث يكون key path_prefix هو بادئة مسار الملف إلى دليل جذر المستودع.
إن Value repository_url_prefix هو بادئة خادم التحكم في المصدر إلى المستودع.
إليك بعض الأمثلة:
و Value 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>)
يمكن تحديد بدائل متعددة