]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Tool/memcheck.pm
1b1ad4bc190e12201ed60b8b8bbd3551905be7f8
[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.13
13
14 =cut
15
16 our $VERSION = '1.13';
17
18 =head1 DESCRIPTION
19
20 This class contains the information required by the session for running the C<memcheck> tool.
21
22 =cut
23
24 use base qw<Test::Valgrind::Tool>;
25
26 =head1 METHODS
27
28 This class inherits L<Test::Valgrind::Tool>.
29
30 =head2 C<requires_version>
31
32     my $required_version = $tvt->requires_version;
33
34 This tool requires C<valgrind> C<3.1.0>.
35
36 =cut
37
38 sub requires_version { '3.1.0' }
39
40 =head2 C<new>
41
42     my $tvtm = Test::Valgrind::Tool::memcheck->new(
43      callers => $callers,
44      %extra_args,
45     );
46
47 Your usual constructor.
48
49 C<$callers> specifies the number of stack frames to inspect for errors : the bigger you set it, the more granular the analysis is.
50
51 Other arguments are passed straight to C<< Test::Valgrind::Tool->new >>.
52
53 =cut
54
55 sub new {
56  my $class = shift;
57  $class = ref($class) || $class;
58
59  my %args = @_;
60
61  my $callers = delete $args{callers} || 12;
62  $callers =~ s/\D//g;
63
64  my $self = bless $class->Test::Valgrind::Tool::new(%args), $class;
65
66  $self->{callers} = $callers;
67
68  $self;
69 }
70
71 sub new_trainer { shift->new(callers => 50) }
72
73 =head2 C<callers>
74
75     my $callers = $tvtm->callers;
76
77 Read-only accessor for the C<callers> option.
78
79 =cut
80
81 sub callers { $_[0]->{callers} }
82
83 sub suppressions_tag { 'memcheck-' . $_[1]->version }
84
85 =head2 C<parser_class>
86
87     my $parser_class = $tvtm->parser_class($session);
88
89 This tool uses a L<Test::Valgrind::Parser::XML::Twig> parser in analysis mode, and a L<Test::Valgrind::Parser::Suppressions::Text> parser in suppressions mode.
90
91 =cut
92
93 sub parser_class {
94  my ($self, $session) = @_;
95
96  my $class = $session->do_suppressions
97            ? 'Test::Valgrind::Parser::Suppressions::Text'
98            : 'Test::Valgrind::Parser::XML::Twig';
99
100  local $@;
101  eval "require $class";
102
103  return $class;
104 }
105
106 =head2 C<report_class>
107
108     my $report_class = $tvtm->report_class($session);
109
110 This tool emits C<Test::Valgrind::Tool::memcheck::Report> object reports in analysis mode, and C<Test::Valgrind::Report::Suppressions> object reports in suppressions mode.
111
112 =cut
113
114 sub report_class {
115  my ($self, $session) = @_;
116
117  $session->do_suppressions ? 'Test::Valgrind::Report::Suppressions'
118                            : 'Test::Valgrind::Tool::memcheck::Report'
119 }
120
121 sub args {
122  my $self = shift;
123  my ($sess) = @_;
124
125  my @args = (
126   '--tool=memcheck',
127   '--leak-check=full',
128   '--leak-resolution=high',
129   '--show-reachable=yes',
130   '--num-callers=' . $self->callers,
131   '--error-limit=yes',
132  );
133
134  push @args, '--track-origins=yes' if  $sess->version ge '3.4.0'
135                                    and not $sess->do_suppressions;
136
137  push @args, $self->SUPER::args(@_);
138
139  return @args;
140 }
141
142 =head1 SEE ALSO
143
144 L<Test::Valgrind>, L<Test::Valgrind::Tool>.
145
146 =head1 AUTHOR
147
148 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
149
150 You can contact me by mail or on C<irc.perl.org> (vincent).
151
152 =head1 BUGS
153
154 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>.
155 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
156
157 =head1 SUPPORT
158
159 You can find documentation for this module with the perldoc command.
160
161     perldoc Test::Valgrind::Tool::memcheck
162
163 =head1 COPYRIGHT & LICENSE
164
165 Copyright 2009,2010,2011 Vincent Pit, all rights reserved.
166
167 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
168
169 =cut
170
171 # End of Test::Valgrind::Tool::memcheck
172
173 package Test::Valgrind::Tool::memcheck::Report;
174
175 use base qw<Test::Valgrind::Report>;
176
177 our $VERSION = '1.13';
178
179 my @kinds = qw<
180  InvalidFree
181  MismatchedFree
182  InvalidRead
183  InvalidWrite
184  InvalidJump
185  Overlap
186  InvalidMemPool
187  UninitCondition
188  UninitValue
189  SyscallParam
190  ClientCheck
191  Leak_DefinitelyLost
192  Leak_IndirectlyLost
193  Leak_PossiblyLost
194  Leak_StillReachable
195 >;
196 push @kinds, __PACKAGE__->SUPER::kinds();
197
198 my %kinds_hashed = map { $_ => 1 } @kinds;
199
200 sub kinds      { @kinds }
201
202 sub valid_kind { exists $kinds_hashed{$_[1]} }
203
204 sub is_leak    { $_[0]->kind =~ /^Leak_/ ? 1 : '' }
205
206 my $pad;
207 BEGIN {
208  require Config;
209  $pad = 2 * ($Config::Config{ptrsize} || 4);
210 }
211
212 sub dump {
213  my ($self) = @_;
214
215  my $data = $self->data;
216
217  my $desc = '';
218
219  for ([ '', 2, 4 ], [ 'aux', 4, 6 ], [ 'orig', 4, 6 ]) {
220   my ($prefix, $wind, $sind) = @$_;
221
222   my ($what, $stack) = @{$data}{"${prefix}what", "${prefix}stack"};
223   next unless defined $what and defined $stack;
224
225   $_ = ' ' x $_ for $wind, $sind;
226
227   $desc .= "$wind$what\n";
228   for (@$stack) {
229    my ($ip, $obj, $fn, $dir, $file, $line) = map { (defined) ? $_ : '?' } @$_;
230    my $frame;
231    if ($fn eq '?' and $obj eq '?') {
232     $ip =~ s/^0x//gi;
233     my $l = length $ip;
234     $frame = '0x' . ($l < $pad ? ('0' x ($pad - $l)) : '') . uc($ip);
235    } else {
236     $frame = sprintf '%s (%s) [%s:%s]', $fn, $obj, $file, $line;
237    }
238    $desc .= "$sind$frame\n";
239   }
240  }
241
242  return $desc;
243 }
244
245 # End of Test::Valgrind::Tool::memcheck::Report
246