Memcheck Cover ให้ผู้ช่วยทุบตีสำหรับการวิเคราะห์ Memcheck Valgrind
นอกจากนี้ยังมีการจัดรูปแบบตัวสร้างรายงาน HTML การจัดรูปแบบ Valgrind Raw และเน้นชิ้นส่วนสำคัญเพื่อช่วยคุณตีความผลลัพธ์เหล่านั้น
สามารถใช้งานได้อย่างง่ายดายในสภาพแวดล้อม CI ของคุณเพื่อสร้างรายงานโดยอัตโนมัติ
การสาธิตดีกว่าคำใด ๆ คุณสามารถค้นหาตัวอย่างรายงาน HTML ที่สร้างขึ้นได้ที่นี่
สคริปต์ memcheck_runner.sh เป็น wrapper ที่จะเรียกใช้ไบนารีภายใต้เครื่องมือ memcheck ของ Valgrind
สำหรับสคริปต์ memcheck_runner.sh ในการใช้งานเครื่องมือต่อไปนี้จะต้องติดตั้งและเข้าถึงได้โดยใช้ตัวแปรสภาพแวดล้อมพา ธ :
bash Bash Version 4 และ Upper เท่านั้น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 ในการทำงานเครื่องมือต่อไปนี้จะต้องติดตั้งและเข้าถึงได้โดยใช้ตัวแปรสภาพแวดล้อมพา ธ :
bash Bash Version 4 และ Upper เท่านั้น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 จะถูกประมวลผลคือ.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 criticality ไม่มีอยู่จริง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 เป็นคำนำหน้าของพา ธ ที่จะถูกแทนที่
และค่า 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 เป็นคำนำหน้าพา ธ ไฟล์ไปยังไดเรกทอรีรูทที่เก็บ
ค่า 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>)
สามารถกำหนดได้หลายครั้ง