]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Suppressions.pm
This is 1.14
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Suppressions.pm
1 package Test::Valgrind::Suppressions;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Suppressions - Generate suppressions for given tool and command.
9
10 =head1 VERSION
11
12 Version 1.14
13
14 =cut
15
16 our $VERSION = '1.14';
17
18 =head1 DESCRIPTION
19
20 This module is an helper for generating suppressions.
21
22 =cut
23
24 use base qw<Test::Valgrind::Carp>;
25
26 =head1 METHODS
27
28 =head2 C<generate>
29
30     Test::Valgrind::Suppressions->generate(
31      tool    => $tool,
32      command => $command,
33      target  => $target,
34     );
35
36 Generates suppressions for the command C<< $command->new_trainer >> and the tool C<< $tool->new_trainer >>, and writes them in the file specified by C<$target>.
37 The action used behind the scenes is L<Test::Valgrind::Action::Suppressions>.
38
39 Returns the status code.
40
41 =cut
42
43 sub generate {
44  my $self = shift;
45
46  my %args = @_;
47
48  my $cmd = delete $args{command};
49  unless (ref $cmd) {
50   require Test::Valgrind::Command;
51   $cmd = Test::Valgrind::Command->new(
52    command => $cmd,
53    args    => [ ],
54   );
55  }
56  $cmd = $cmd->new_trainer;
57  return unless defined $cmd;
58
59  my $tool = delete $args{tool};
60  unless (ref $tool) {
61   require Test::Valgrind::Tool;
62   $tool = Test::Valgrind::Tool->new(tool => $tool);
63  }
64  $tool = $tool->new_trainer;
65  return unless defined $tool;
66
67  my $target = delete $args{target};
68  $self->_croak('Invalid target') unless $target and not ref $target;
69
70  require Test::Valgrind::Action;
71  my $action = Test::Valgrind::Action->new(
72   action => 'Suppressions',
73   target => $target,
74   name   => 'PerlSuppression',
75  );
76
77  require Test::Valgrind::Session;
78  my $sess = Test::Valgrind::Session->new(
79   min_version => $tool->requires_version,
80  );
81
82  eval {
83   $sess->run(
84    command => $cmd,
85    tool    => $tool,
86    action  => $action,
87   );
88  };
89  $self->_croak($@) if $@;
90
91  my $status = $sess->status;
92  $status = 255 unless defined $status;
93
94  return $status;
95 }
96
97 =head2 C<strip_tail>
98
99     my $mangled_suppression = Test::Valgrind::Suppressions->strip_tail(
100      $session,
101      $suppression,
102     );
103
104 Removes all wildcard frames at the end of the suppression.
105 Moreover, C<'...'> is appended when C<valgrind> C<3.4.0> or higher is used.
106 Returns the mangled suppression.
107
108 =cut
109
110 sub strip_tail {
111  shift;
112
113  my ($sess, $supp) = @_;
114
115  1 while $supp =~ s/[^\r\n]*:\s*\*\s*$//;
116  # With valgrind 3.4.0, we can replace unknown series of frames by '...'
117  if ($sess->version ge '3.4.0') {
118   1 while $supp =~ s/[^\r\n]*\.{3}\s*$//;
119   $supp .= "...\n";
120  }
121
122  $supp;
123 }
124
125 =head2 C<maybe_z_demangle>
126
127     my $demangled_symbol = Test::Valgrind::Suppressions->maybe_z_demangle(
128      $symbol,
129     );
130
131 If C<$symbol> is Z-encoded as described in C<valgrind>'s F<include/pub_tool_redir.h>, extract and decode its function name part.
132 Otherwise, C<$symbol> is returned as is.
133
134 This routine follows C<valgrind>'s F<coregrind/m_demangle/demangle.c:maybe_Z_demangle>.
135
136 =cut
137
138 my %z_escapes = (
139  a => '*',
140  c => ':',
141  d => '.',
142  h => '-',
143  p => '+',
144  s => ' ',
145  u => '_',
146  A => '@',
147  D => '$',
148  L => '(',
149  R => ')',
150  Z => 'Z',
151 );
152
153 sub maybe_z_demangle {
154  my ($self, $sym) = @_;
155
156  $sym =~ s/^_vg[rwn]Z([ZU])_// or return $sym;
157
158  my $fn_is_encoded = $1 eq 'Z';
159
160  $sym =~ /^VG_Z_/ and $self->_croak('Symbol with a "VG_Z_" prefix is invalid');
161  $sym =~ s/^[^_]*_//
162                    or $self->_croak('Symbol doesn\'t contain a function name');
163
164  if ($fn_is_encoded) {
165   $sym =~ s/Z(.)/
166    my $c = $z_escapes{$1};
167    $self->_croak('Invalid escape sequence') unless defined $c;
168    $c;
169   /ge;
170  }
171
172  $self->_croak('Empty symbol') unless length $sym;
173
174  return $sym;
175 }
176
177 =head1 SEE ALSO
178
179 L<Test::Valgrind>, L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Action::Suppressions>.
180
181 =head1 AUTHOR
182
183 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
184
185 You can contact me by mail or on C<irc.perl.org> (vincent).
186
187 =head1 BUGS
188
189 Please report any bugs or feature requests to C<bug-test-valgrind-suppressions at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
190 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
191
192 =head1 SUPPORT
193
194 You can find documentation for this module with the perldoc command.
195
196     perldoc Test::Valgrind::Suppressions
197
198 =head1 COPYRIGHT & LICENSE
199
200 Copyright 2008,2009,2010,2011,2013 Vincent Pit, all rights reserved.
201
202 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
203
204 =cut
205
206 1; # End of Test::Valgrind::Suppressions