]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Tool/memcheck.pm
This is 1.01
[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.01
13
14 =cut
15
16 our $VERSION = '1.01';
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->_croak('This memcheck tool can\'t be run in two sessions at once')
125                                                              if $self->_session;
126
127  $self->SUPER::start($sess);
128  $self->_session($sess);
129
130  return;
131 }
132
133 sub parse_analysis {
134  my ($self, $sess, $fh) = @_;
135
136  my $twig = $self->twig;
137  $twig->parse($fh);
138  $twig->purge;
139
140  return;
141 }
142
143 sub finish {
144  my ($self, $sess) = @_;
145
146  $self->_session(undef);
147  $self->SUPER::start($sess);
148
149  return;
150 }
151
152 =head1 SEE ALSO
153
154 L<Test::Valgrind>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Tool::SuppressionsParser>.
155
156 L<XML::Twig>.
157
158 =head1 AUTHOR
159
160 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
161
162 You can contact me by mail or on C<irc.perl.org> (vincent).
163
164 =head1 BUGS
165
166 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>.
167 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
168
169 =head1 SUPPORT
170
171 You can find documentation for this module with the perldoc command.
172
173     perldoc Test::Valgrind::Tool::memcheck
174
175 =head1 COPYRIGHT & LICENSE
176
177 Copyright 2009 Vincent Pit, all rights reserved.
178
179 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
180
181 =cut
182
183 # End of Test::Valgrind::Tool::memcheck
184
185 package Test::Valgrind::Tool::memcheck::Report;
186
187 use base qw/Test::Valgrind::Report/;
188
189 use Config qw/%Config/;
190
191 our $VERSION = '1.01';
192
193 my @kinds = qw/
194  InvalidFree
195  MismatchedFree
196  InvalidRead
197  InvalidWrite
198  InvalidJump
199  Overlap
200  InvalidMemPool
201  UninitCondition
202  UninitValue
203  SyscallParam
204  ClientCheck
205  Leak_DefinitelyLost
206  Leak_IndirectlyLost
207  Leak_PossiblyLost
208  Leak_StillReachable
209 /;
210 push @kinds, __PACKAGE__->SUPER::kinds();
211
212 my %kinds_hashed = map { $_ => 1 } @kinds;
213
214 sub kinds      { @kinds }
215
216 sub valid_kind { exists $kinds_hashed{$_[1]} }
217
218 sub is_leak    { $_[0]->kind =~ /^Leak_/ ? 1 : '' }
219
220 my $pad = 2 * ($Config{ptrsize} || 4);
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.01';
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 $report = Test::Valgrind::Tool::memcheck::Report->new(
325   kind => $kind,
326   id   => $id,
327   data => $data,
328  );
329
330  $twig->stash->{tool}->_session->report($report);
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.01';
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