]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Tool/memcheck.pm
Miscellanous doc nits
[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.10
13
14 =cut
15
16 our $VERSION = '1.10';
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 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;
62 }
63
64 sub new_trainer { shift->new(callers => 50) }
65
66 =head2 C<callers>
67
68 Read-only accessor for the C<callers> option.
69
70 =cut
71
72 sub callers { $_[0]->{callers} }
73
74 sub suppressions_tag { 'memcheck-' . $_[1]->version }
75
76 =head2 C<parser_class $session>
77
78 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.
79
80 =cut
81
82 sub parser_class {
83  my ($self, $session) = @_;
84
85  my $class = $session->do_suppressions
86            ? 'Test::Valgrind::Parser::Suppressions::Text'
87            : 'Test::Valgrind::Parser::XML::Twig';
88
89  local $@;
90  eval "require $class";
91
92  return $class;
93 }
94
95 =head2 C<report_class $session>
96
97 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.
98
99 =cut
100
101 sub report_class {
102  my ($self, $session) = @_;
103
104  $session->do_suppressions ? 'Test::Valgrind::Report::Suppressions'
105                            : 'Test::Valgrind::Tool::memcheck::Report'
106 }
107
108 sub args {
109  my $self = shift;
110  my ($sess) = @_;
111
112  my @args = (
113   '--tool=memcheck',
114   '--leak-check=full',
115   '--leak-resolution=high',
116   '--show-reachable=yes',
117   '--num-callers=' . $self->callers,
118   '--error-limit=yes',
119  );
120
121  push @args, '--track-origins=yes' if  $sess->version ge '3.4.0'
122                                    and not $sess->do_suppressions;
123
124  push @args, $self->SUPER::args(@_);
125
126  return @args;
127 }
128
129 =head1 SEE ALSO
130
131 L<Test::Valgrind>, L<Test::Valgrind::Tool>.
132
133 =head1 AUTHOR
134
135 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
136
137 You can contact me by mail or on C<irc.perl.org> (vincent).
138
139 =head1 BUGS
140
141 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>.
142 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
143
144 =head1 SUPPORT
145
146 You can find documentation for this module with the perldoc command.
147
148     perldoc Test::Valgrind::Tool::memcheck
149
150 =head1 COPYRIGHT & LICENSE
151
152 Copyright 2009 Vincent Pit, all rights reserved.
153
154 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
155
156 =cut
157
158 # End of Test::Valgrind::Tool::memcheck
159
160 package Test::Valgrind::Tool::memcheck::Report;
161
162 use base qw/Test::Valgrind::Report/;
163
164 our $VERSION = '1.10';
165
166 my @kinds = qw/
167  InvalidFree
168  MismatchedFree
169  InvalidRead
170  InvalidWrite
171  InvalidJump
172  Overlap
173  InvalidMemPool
174  UninitCondition
175  UninitValue
176  SyscallParam
177  ClientCheck
178  Leak_DefinitelyLost
179  Leak_IndirectlyLost
180  Leak_PossiblyLost
181  Leak_StillReachable
182 /;
183 push @kinds, __PACKAGE__->SUPER::kinds();
184
185 my %kinds_hashed = map { $_ => 1 } @kinds;
186
187 sub kinds      { @kinds }
188
189 sub valid_kind { exists $kinds_hashed{$_[1]} }
190
191 sub is_leak    { $_[0]->kind =~ /^Leak_/ ? 1 : '' }
192
193 my $pad;
194 BEGIN {
195  require Config;
196  $pad = 2 * ($Config::Config{ptrsize} || 4);
197 }
198
199 sub dump {
200  my ($self) = @_;
201
202  my $data = $self->data;
203
204  my $desc = '';
205
206  for ([ '', 2, 4 ], [ 'aux', 4, 6 ], [ 'orig', 4, 6 ]) {
207   my ($prefix, $wind, $sind) = @$_;
208
209   my ($what, $stack) = @{$data}{"${prefix}what", "${prefix}stack"};
210   next unless defined $what and defined $stack;
211
212   $_ = ' ' x $_ for $wind, $sind;
213
214   $desc .= "$wind$what\n";
215   for (@$stack) {
216    my ($ip, $obj, $fn, $dir, $file, $line) = map { (defined) ? $_ : '?' } @$_;
217    my $frame;
218    if ($fn eq '?' and $obj eq '?') {
219     $ip =~ s/^0x//g;
220     $ip = hex $ip;
221     $frame = sprintf "0x%0${pad}X", $ip;
222    } else {
223     $frame = sprintf '%s (%s) [%s:%s]', $fn, $obj, $file, $line;
224    }
225    $desc .= "$sind$frame\n";
226   }
227  }
228
229  return $desc;
230 }
231
232 # End of Test::Valgrind::Tool::memcheck::Report
233