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