How would I use Perl module Test::Valgrind for an executable written in C or C++? The documentation is not clear about it.
Asked
Active
Viewed 662 times
2
-
From docs: `This module is a front-end to the Test::Valgrind::* API that lets you run **Perl code** through the memcheck tool of the valgrind memory debugger, to test for memory errors and leaks` so for c/c++ you should use more appropriate tool. – edem Dec 04 '13 at 09:25
-
Also From docs: `The complete API is much more versatile than this. By declaring an appropriate Test::Valgrind::Command class, you can run any executable (that is, not only Perl scripts) under valgrind, generate the corresponding suppressions on-the-fly and convert the analysis result to TAP output so that it can be incorporated into your project's testsuite.` @edem – Pritesh Acharya Dec 04 '13 at 09:46
1 Answers
1
I've run some simple programs on C/Perl and have got all tests pass for every files.
perl -MTest::Valgrind a.out
perl -MTest::Valgrind b.pl
ok 1 - InvalidFree
ok 2 - MismatchedFree
ok 3 - InvalidRead
ok 4 - InvalidWrite
ok 5 - InvalidJump
ok 6 - Overlap
ok 7 - InvalidMemPool
ok 8 - UninitCondition
ok 9 - UninitValue
ok 10 - SyscallParam
ok 11 - ClientCheck
ok 12 - Leak_DefinitelyLost
ok 13 - Leak_IndirectlyLost
ok 14 - Leak_PossiblyLost
ok 15 - Leak_StillReachable
So you have to just use other file argument for this from your shell.
UPD
perl -MTest::Valgrind -e 0
get the same output as above so that's not what you need I guess.
If we run perl -mTest::Valgrind Valgrind/Test-Valgrind-1.14/samples/map.pl
from source distribution. we'll get a more believable result:
# ...
# 4,080 bytes in 1 blocks are still reachable in loss record 769 of 786
# malloc (/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) [?:?]
# Perl_safesysmalloc (/usr/bin/perl) [?:?]
# ? (/usr/bin/perl) [?:?]
# Perl_newSV (/usr/bin/perl) [?:?]
# Perl_yylex (/usr/bin/perl) [?:?]
# Perl_yyparse (/usr/bin/perl) [?:?]
# ? (/usr/bin/perl) [?:?]
# Perl_pp_require (/usr/bin/perl) [?:?]
# Perl_runops_standard (/usr/bin/perl) [?:?]
# Perl_call_sv (/usr/bin/perl) [?:?]
# Perl_call_list (/usr/bin/perl) [?:?]
# ? (/usr/bin/perl) [?:?]
# Looks like you failed 3 tests of 15.
# Looks like your test exited with 3 just after 15.
You may also use valgrind
tool for test with more options to control it.
DESCRIPTION Valgrind is a flexible program for debugging and profiling Linux executables. It consists of a core, which provides a synthetic CPU in software, and a series of debugging and profiling tools.
I've checked this simple perl script:
for (1..10) {
for (1..324) {
print $_ * $_;
}
}
valgrind ./test.pl
and got this:
==16749== HEAP SUMMARY:
==16749== in use at exit: 224,640 bytes in 2,409 blocks
==16749== total heap usage: 8,846 allocs, 6,437 frees, 439,538 bytes allocated
==16749==
==16749== LEAK SUMMARY:
==16749== definitely lost: 9,001 bytes in 17 blocks
==16749== indirectly lost: 215,639 bytes in 2,392 blocks
==16749== possibly lost: 0 bytes in 0 blocks
==16749== still reachable: 0 bytes in 0 blocks
==16749== suppressed: 0 bytes in 0 blocks
==16749== Rerun with --leak-check=full to see details of leaked memory
==16749==
==16749== For counts of detected and suppressed errors, rerun with: -v
==16749== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

edem
- 3,222
- 3
- 19
- 45
-
I think this output is static regardless of input file. `#include
#include – Pritesh Acharya Dec 04 '13 at 11:27#include void leak_more(void) { void *abc = malloc(sizeof(char)*100); return; } int main(void) { char *msg = strdup("abc"); printf("msg = %s\n",msg); leak_more(); return 0; }` I tested the above code for memory leak, got the same output. @edem