1 package Test::Valgrind::Tool::memcheck;
8 Test::Valgrind::Tool::memcheck - Run an analysis through the memcheck tool.
16 our $VERSION = '1.02';
20 This tool parses the XML output of a C<memcheck> run with L<XML::Twig>.
24 use base qw/Test::Valgrind::Tool/;
28 This class inherits L<Test::Valgrind::Tool>.
30 =head2 C<requires_version>
32 This tool requires C<valgrind> C<3.1.0>.
36 sub requires_version { '3.1.0' }
38 =head2 C<< new callers => $callers, ... >>
40 Your usual constructor.
42 C<$callers> specifies the number of stack frames to inspect for errors : the bigger you set it, the more granular the analysis is.
44 Other arguments are passed straight to C<< Test::Valgrind::Tool->new >>.
50 $class = ref($class) || $class;
54 my $callers = delete $args{callers} || 12;
57 my $self = bless $class->Test::Valgrind::Tool::new(%args), $class;
59 $self->{callers} = $callers;
61 $self->{twig} = Test::Valgrind::Tool::memcheck::Twig->new(tool => $self);
66 sub new_trainer { shift->new(callers => 50) }
70 Read-only accessor for the C<callers> option.
74 sub callers { $_[0]->{callers} }
78 Read-only accessor for the underlying L<XML::Twig> parser.
82 sub twig { $_[0]->{twig} }
84 sub suppressions_tag { 'memcheck-' . $_[1]->version }
86 =head2 C<report_class_analysis $session>
88 This tool emits C<Test::Valgrind::Tool::memcheck::Report> object reports in analysis mode.
92 sub report_class_analysis { 'Test::Valgrind::Tool::memcheck::Report' }
101 '--leak-resolution=high',
102 '--show-reachable=yes',
103 '--num-callers=' . $self->callers,
107 unless ($sess->do_suppressions) {
108 push @args, '--track-origins=yes' if $sess->version ge '3.4.0';
109 push @args, '--xml=yes';
112 push @args, $self->SUPER::args(@_);
117 # We must store the session in ourselves because it's only possible to pass
118 # arguments to XML::Twig objects by a global stash.
120 sub _session { @_ <= 1 ? $_[0]->{_session} : ($_[0]->{_session} = $_[1]) }
123 my ($self, $sess) = @_;
125 $self->SUPER::start($sess);
126 $self->_session($sess);
132 my ($self, $sess, $fh) = @_;
134 my $twig = $self->twig;
142 my ($self, $sess) = @_;
144 $self->_session(undef);
145 $self->SUPER::finish($sess);
152 L<Test::Valgrind>, L<Test::Valgrind::Tool>.
158 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
160 You can contact me by mail or on C<irc.perl.org> (vincent).
164 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
165 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
169 You can find documentation for this module with the perldoc command.
171 perldoc Test::Valgrind::Tool::memcheck
173 =head1 COPYRIGHT & LICENSE
175 Copyright 2009 Vincent Pit, all rights reserved.
177 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
181 # End of Test::Valgrind::Tool::memcheck
183 package Test::Valgrind::Tool::memcheck::Report;
185 use base qw/Test::Valgrind::Report/;
187 our $VERSION = '1.02';
206 push @kinds, __PACKAGE__->SUPER::kinds();
208 my %kinds_hashed = map { $_ => 1 } @kinds;
212 sub valid_kind { exists $kinds_hashed{$_[1]} }
214 sub is_leak { $_[0]->kind =~ /^Leak_/ ? 1 : '' }
219 $pad = 2 * ($Config::Config{ptrsize} || 4);
225 my $data = $self->data;
229 for ([ '', 2, 4 ], [ 'aux', 4, 6 ], [ 'orig', 4, 6 ]) {
230 my ($prefix, $wind, $sind) = @$_;
232 my ($what, $stack) = @{$data}{"${prefix}what", "${prefix}stack"};
233 next unless defined $what and defined $stack;
235 $_ = ' ' x $_ for $wind, $sind;
237 $desc .= "$wind$what\n";
239 my ($ip, $obj, $fn, $dir, $file, $line) = map { (defined) ? $_ : '?' } @$_;
241 if ($fn eq '?' and $obj eq '?') {
244 $frame = sprintf "0x%0${pad}X", $ip;
246 $frame = sprintf '%s (%s) [%s:%s]', $fn, $obj, $file, $line;
248 $desc .= "$sind$frame\n";
255 # End of Test::Valgrind::Tool::memcheck::Report