]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Tool/memcheck.pm
Tools can only be run in one session at a time by default
[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, $sess) = @_;
96
97  my @args = (
98   '--tool=memcheck',
99   '--leak-check=full',
100   '--leak-resolution=high',
101   '--show-reachable=yes',
102   '--num-callers=' . $self->callers,
103   '--error-limit=yes',
104  );
105
106  unless ($sess->do_suppressions) {
107   push @args, '--track-origins=yes' if $sess->version ge '3.4.0';
108   push @args, '--xml=yes';
109  }
110
111  push @args, $self->SUPER::args();
112
113  return @args;
114 }
115
116 # We must store the session in ourselves because it's only possible to pass
117 # arguments to XML::Twig objects by a global stash.
118
119 sub _session { @_ <= 1 ? $_[0]->{_session} : ($_[0]->{_session} = $_[1]) }
120
121 sub start {
122  my ($self, $sess) = @_;
123
124  $self->SUPER::start($sess);
125  $self->_session($sess);
126
127  return;
128 }
129
130 sub parse_analysis {
131  my ($self, $sess, $fh) = @_;
132
133  my $twig = $self->twig;
134  $twig->parse($fh);
135  $twig->purge;
136
137  return;
138 }
139
140 sub finish {
141  my ($self, $sess) = @_;
142
143  $self->_session(undef);
144  $self->SUPER::finish($sess);
145
146  return;
147 }
148
149 =head1 SEE ALSO
150
151 L<Test::Valgrind>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Tool::SuppressionsParser>.
152
153 L<XML::Twig>.
154
155 =head1 AUTHOR
156
157 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
158
159 You can contact me by mail or on C<irc.perl.org> (vincent).
160
161 =head1 BUGS
162
163 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>.
164 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
165
166 =head1 SUPPORT
167
168 You can find documentation for this module with the perldoc command.
169
170     perldoc Test::Valgrind::Tool::memcheck
171
172 =head1 COPYRIGHT & LICENSE
173
174 Copyright 2009 Vincent Pit, all rights reserved.
175
176 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
177
178 =cut
179
180 # End of Test::Valgrind::Tool::memcheck
181
182 package Test::Valgrind::Tool::memcheck::Report;
183
184 use base qw/Test::Valgrind::Report/;
185
186 our $VERSION = '1.02';
187
188 my @kinds = qw/
189  InvalidFree
190  MismatchedFree
191  InvalidRead
192  InvalidWrite
193  InvalidJump
194  Overlap
195  InvalidMemPool
196  UninitCondition
197  UninitValue
198  SyscallParam
199  ClientCheck
200  Leak_DefinitelyLost
201  Leak_IndirectlyLost
202  Leak_PossiblyLost
203  Leak_StillReachable
204 /;
205 push @kinds, __PACKAGE__->SUPER::kinds();
206
207 my %kinds_hashed = map { $_ => 1 } @kinds;
208
209 sub kinds      { @kinds }
210
211 sub valid_kind { exists $kinds_hashed{$_[1]} }
212
213 sub is_leak    { $_[0]->kind =~ /^Leak_/ ? 1 : '' }
214
215 my $pad;
216 BEGIN {
217  require Config;
218  $pad = 2 * ($Config::Config{ptrsize} || 4);
219 }
220
221 sub dump {
222  my ($self) = @_;
223
224  my $data = $self->data;
225
226  my $desc = '';
227
228  for ([ '', 2, 4 ], [ 'aux', 4, 6 ], [ 'orig', 4, 6 ]) {
229   my ($prefix, $wind, $sind) = @$_;
230
231   my ($what, $stack) = @{$data}{"${prefix}what", "${prefix}stack"};
232   next unless defined $what and defined $stack;
233
234   $_ = ' ' x $_ for $wind, $sind;
235
236   $desc .= "$wind$what\n";
237   for (@$stack) {
238    my ($ip, $obj, $fn, $dir, $file, $line) = map { (defined) ? $_ : '?' } @$_;
239    my $frame;
240    if ($fn eq '?' and $obj eq '?') {
241     $ip =~ s/^0x//g;
242     $ip = hex $ip;
243     $frame = sprintf "0x%0${pad}X", $ip;
244    } else {
245     $frame = sprintf '%s (%s) [%s:%s]', $fn, $obj, $file, $line;
246    }
247    $desc .= "$sind$frame\n";
248   }
249  }
250
251  return $desc;
252 }
253
254 # End of Test::Valgrind::Tool::memcheck::Report
255
256 package Test::Valgrind::Tool::memcheck::Twig;
257
258 our $VERSION = '1.02';
259
260 use Scalar::Util;
261
262 use base qw/XML::Twig Test::Valgrind::Carp/;
263
264 BEGIN { XML::Twig->add_options('Stash'); }
265
266 my %handlers = (
267  '/valgrindoutput/error' => \&handle_error,
268 );
269
270 sub new {
271  my $class = shift;
272  $class = ref($class) || $class;
273
274  my %args = @_;
275  my $stash = delete $args{stash} || { };
276
277  my $tool = delete $args{tool};
278  $class->_croak('Invalid tool') unless Scalar::Util::blessed($tool)
279                                          and $tool->isa('Test::Valgrind::Tool');
280  $stash->{tool} = $tool;
281
282  bless $class->XML::Twig::new(
283   elt_class     => __PACKAGE__ . '::Elt',
284   stash         => $stash,
285   twig_roots    => { map { $_ => 1             } keys %handlers },
286   twig_handlers => { map { $_ => $handlers{$_} } keys %handlers },
287  ), $class;
288 }
289
290 sub stash { shift->{Stash} }
291
292 sub handle_error {
293  my ($twig, $node) = @_;
294
295  my $id   = $node->kid('unique')->text;
296  my $kind = $node->kid('kind')->text;
297
298  my $data;
299
300  $data->{what}  = $node->kid('what')->text;
301  $data->{stack} = [ map $_->listify_frame,
302                                        $node->kid('stack')->children('frame') ];
303
304  for (qw/leakedbytes leakedblocks/) {
305   my $kid = $node->first_child($_);
306   next unless $kid;
307   $data->{$_} = int $kid->text;
308  }
309
310  if (my $auxwhat = $node->first_child('auxwhat')) {
311   if (my $stack = $auxwhat->next_sibling('stack')) {
312    $data->{auxstack} = [ map $_->listify_frame, $stack->children('frame') ];
313   }
314   $data->{auxwhat} = $auxwhat->text;
315  }
316
317  if (my $origin = $node->first_child('origin')) {
318   $data->{origwhat}  = $origin->kid('what')->text;
319   $data->{origstack} = [ map $_->listify_frame,
320                                      $origin->kid('stack')->children('frame') ];
321  }
322
323  my $tool = $twig->stash->{tool};
324  my $sess = $tool->_session;
325
326  $sess->report($tool->report_class($sess)->new(
327   kind => $kind,
328   id   => $id,
329   data => $data,
330  ));
331
332  $twig->purge;
333 }
334
335 # End of Test::Valgrind::Tool::memcheck::Twig
336
337 package Test::Valgrind::Tool::memcheck::Twig::Elt;
338
339 our $VERSION = '1.02';
340
341 BEGIN { require XML::Twig; }
342
343 use base qw/XML::Twig::Elt Test::Valgrind::Carp/;
344
345 sub kid {
346  my ($self, $what) = @_;
347  my $node = $self->first_child($what);
348  $self->_croak("Couldn't get first $what child node") unless $node;
349  return $node;
350 }
351
352 sub listify_frame {
353  my ($frame) = @_;
354
355  return unless $frame->tag eq 'frame';
356
357  return [
358   map {
359    my $x = $frame->first_child($_);
360    $x ? $x->text : undef
361   } qw/ip obj fn dir file line/
362  ];
363 }
364
365 1; # End of Test::Valgrind::Tool::memcheck::Twig::Elt