]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Tool/memcheck.pm
Tool::memcheck::args needs to forward its $session argument in the SUPER call
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Tool / memcheck.pm
1 package Test::Valgrind::Tool::memcheck;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Tool::memcheck - Run an analysis through the memcheck tool.
9
10 =head1 VERSION
11
12 Version 1.02
13
14 =cut
15
16 our $VERSION = '1.02';
17
18 =head1 DESCRIPTION
19
20 This tool parses the XML output of a C<memcheck> run with L<XML::Twig>.
21
22 =cut
23
24 use base qw/Test::Valgrind::Tool::SuppressionsParser Test::Valgrind::Tool/;
25
26 =head1 METHODS
27
28 This class inherits L<Test::Valgrind::Tool> and L<Test::Valgrind::Tool::SuppressionsParser>.
29
30 =head2 C<requires_version>
31
32 This tool requires C<valgrind> C<3.1.0>.
33
34 =cut
35
36 sub requires_version { '3.1.0' }
37
38 =head2 C<< new callers => $callers, ... >>
39
40 Your usual constructor.
41
42 C<$callers> specifies the number of stack frames to inspect for errors : the bigger you set it, the more granular the analysis is.
43
44 Other arguments are passed straight to C<< Test::Valgrind::Tool->new >>.
45
46 =cut
47
48 sub new {
49  my $class = shift;
50  $class = ref($class) || $class;
51
52  my %args = @_;
53
54  my $callers = delete $args{callers} || 12;
55  $callers =~ s/\D//g;
56
57  my $self = bless $class->Test::Valgrind::Tool::new(%args), $class;
58
59  $self->{callers} = $callers;
60
61  $self->{twig} = Test::Valgrind::Tool::memcheck::Twig->new(tool => $self);
62
63  $self;
64 }
65
66 sub new_trainer { shift->new(callers => 50) }
67
68 =head2 C<callers>
69
70 Read-only accessor for the C<callers> option.
71
72 =cut
73
74 sub callers { $_[0]->{callers} }
75
76 =head2 C<twig>
77
78 Read-only accessor for the underlying L<XML::Twig> parser.
79
80 =cut
81
82 sub twig { $_[0]->{twig} }
83
84 sub suppressions_tag { 'memcheck-' . $_[1]->version }
85
86 =head2 C<report_class_analysis $session>
87
88 This tool emits C<Test::Valgrind::Tool::memcheck::Report> object reports in analysis mode.
89
90 =cut
91
92 sub report_class_analysis { 'Test::Valgrind::Tool::memcheck::Report' }
93
94 sub args {
95  my $self = shift;
96  my ($sess) = @_;
97
98  my @args = (
99   '--tool=memcheck',
100   '--leak-check=full',
101   '--leak-resolution=high',
102   '--show-reachable=yes',
103   '--num-callers=' . $self->callers,
104   '--error-limit=yes',
105  );
106
107  unless ($sess->do_suppressions) {
108   push @args, '--track-origins=yes' if $sess->version ge '3.4.0';
109   push @args, '--xml=yes';
110  }
111
112  push @args, $self->SUPER::args(@_);
113
114  return @args;
115 }
116
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.
119
120 sub _session { @_ <= 1 ? $_[0]->{_session} : ($_[0]->{_session} = $_[1]) }
121
122 sub start {
123  my ($self, $sess) = @_;
124
125  $self->SUPER::start($sess);
126  $self->_session($sess);
127
128  return;
129 }
130
131 sub parse_analysis {
132  my ($self, $sess, $fh) = @_;
133
134  my $twig = $self->twig;
135  $twig->parse($fh);
136  $twig->purge;
137
138  return;
139 }
140
141 sub finish {
142  my ($self, $sess) = @_;
143
144  $self->_session(undef);
145  $self->SUPER::finish($sess);
146
147  return;
148 }
149
150 =head1 SEE ALSO
151
152 L<Test::Valgrind>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Tool::SuppressionsParser>.
153
154 L<XML::Twig>.
155
156 =head1 AUTHOR
157
158 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
159
160 You can contact me by mail or on C<irc.perl.org> (vincent).
161
162 =head1 BUGS
163
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.
166
167 =head1 SUPPORT
168
169 You can find documentation for this module with the perldoc command.
170
171     perldoc Test::Valgrind::Tool::memcheck
172
173 =head1 COPYRIGHT & LICENSE
174
175 Copyright 2009 Vincent Pit, all rights reserved.
176
177 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
178
179 =cut
180
181 # End of Test::Valgrind::Tool::memcheck
182
183 package Test::Valgrind::Tool::memcheck::Report;
184
185 use base qw/Test::Valgrind::Report/;
186
187 our $VERSION = '1.02';
188
189 my @kinds = qw/
190  InvalidFree
191  MismatchedFree
192  InvalidRead
193  InvalidWrite
194  InvalidJump
195  Overlap
196  InvalidMemPool
197  UninitCondition
198  UninitValue
199  SyscallParam
200  ClientCheck
201  Leak_DefinitelyLost
202  Leak_IndirectlyLost
203  Leak_PossiblyLost
204  Leak_StillReachable
205 /;
206 push @kinds, __PACKAGE__->SUPER::kinds();
207
208 my %kinds_hashed = map { $_ => 1 } @kinds;
209
210 sub kinds      { @kinds }
211
212 sub valid_kind { exists $kinds_hashed{$_[1]} }
213
214 sub is_leak    { $_[0]->kind =~ /^Leak_/ ? 1 : '' }
215
216 my $pad;
217 BEGIN {
218  require Config;
219  $pad = 2 * ($Config::Config{ptrsize} || 4);
220 }
221
222 sub dump {
223  my ($self) = @_;
224
225  my $data = $self->data;
226
227  my $desc = '';
228
229  for ([ '', 2, 4 ], [ 'aux', 4, 6 ], [ 'orig', 4, 6 ]) {
230   my ($prefix, $wind, $sind) = @$_;
231
232   my ($what, $stack) = @{$data}{"${prefix}what", "${prefix}stack"};
233   next unless defined $what and defined $stack;
234
235   $_ = ' ' x $_ for $wind, $sind;
236
237   $desc .= "$wind$what\n";
238   for (@$stack) {
239    my ($ip, $obj, $fn, $dir, $file, $line) = map { (defined) ? $_ : '?' } @$_;
240    my $frame;
241    if ($fn eq '?' and $obj eq '?') {
242     $ip =~ s/^0x//g;
243     $ip = hex $ip;
244     $frame = sprintf "0x%0${pad}X", $ip;
245    } else {
246     $frame = sprintf '%s (%s) [%s:%s]', $fn, $obj, $file, $line;
247    }
248    $desc .= "$sind$frame\n";
249   }
250  }
251
252  return $desc;
253 }
254
255 # End of Test::Valgrind::Tool::memcheck::Report
256
257 package Test::Valgrind::Tool::memcheck::Twig;
258
259 our $VERSION = '1.02';
260
261 use Scalar::Util;
262
263 use base qw/XML::Twig Test::Valgrind::Carp/;
264
265 BEGIN { XML::Twig->add_options('Stash'); }
266
267 my %handlers = (
268  '/valgrindoutput/error' => \&handle_error,
269 );
270
271 sub new {
272  my $class = shift;
273  $class = ref($class) || $class;
274
275  my %args = @_;
276  my $stash = delete $args{stash} || { };
277
278  my $tool = delete $args{tool};
279  $class->_croak('Invalid tool') unless Scalar::Util::blessed($tool)
280                                          and $tool->isa('Test::Valgrind::Tool');
281  $stash->{tool} = $tool;
282
283  bless $class->XML::Twig::new(
284   elt_class     => __PACKAGE__ . '::Elt',
285   stash         => $stash,
286   twig_roots    => { map { $_ => 1             } keys %handlers },
287   twig_handlers => { map { $_ => $handlers{$_} } keys %handlers },
288  ), $class;
289 }
290
291 sub stash { shift->{Stash} }
292
293 sub handle_error {
294  my ($twig, $node) = @_;
295
296  my $id   = $node->kid('unique')->text;
297  my $kind = $node->kid('kind')->text;
298
299  my $data;
300
301  $data->{what}  = $node->kid('what')->text;
302  $data->{stack} = [ map $_->listify_frame,
303                                        $node->kid('stack')->children('frame') ];
304
305  for (qw/leakedbytes leakedblocks/) {
306   my $kid = $node->first_child($_);
307   next unless $kid;
308   $data->{$_} = int $kid->text;
309  }
310
311  if (my $auxwhat = $node->first_child('auxwhat')) {
312   if (my $stack = $auxwhat->next_sibling('stack')) {
313    $data->{auxstack} = [ map $_->listify_frame, $stack->children('frame') ];
314   }
315   $data->{auxwhat} = $auxwhat->text;
316  }
317
318  if (my $origin = $node->first_child('origin')) {
319   $data->{origwhat}  = $origin->kid('what')->text;
320   $data->{origstack} = [ map $_->listify_frame,
321                                      $origin->kid('stack')->children('frame') ];
322  }
323
324  my $tool = $twig->stash->{tool};
325  my $sess = $tool->_session;
326
327  $sess->report($tool->report_class($sess)->new(
328   kind => $kind,
329   id   => $id,
330   data => $data,
331  ));
332
333  $twig->purge;
334 }
335
336 # End of Test::Valgrind::Tool::memcheck::Twig
337
338 package Test::Valgrind::Tool::memcheck::Twig::Elt;
339
340 our $VERSION = '1.02';
341
342 BEGIN { require XML::Twig; }
343
344 use base qw/XML::Twig::Elt Test::Valgrind::Carp/;
345
346 sub kid {
347  my ($self, $what) = @_;
348  my $node = $self->first_child($what);
349  $self->_croak("Couldn't get first $what child node") unless $node;
350  return $node;
351 }
352
353 sub listify_frame {
354  my ($frame) = @_;
355
356  return unless $frame->tag eq 'frame';
357
358  return [
359   map {
360    my $x = $frame->first_child($_);
361    $x ? $x->text : undef
362   } qw/ip obj fn dir file line/
363  ];
364 }
365
366 1; # End of Test::Valgrind::Tool::memcheck::Twig::Elt