1 package Test::Valgrind::Session;
8 Test::Valgrind::Session - Test::Valgrind session object.
16 our $VERSION = '1.00';
20 This class supervises the execution of the C<valgrind> process.
21 It also acts as a dispatcher between the different components.
25 # All these modules are required at configure time.
31 require Fcntl; # F_SETFD
32 require POSIX; # SIGKILL
37 use base qw/Test::Valgrind::Carp/;
39 use Test::Valgrind::Report;
43 =head2 C<< new search_dirs => \@search_dirs, valgrind => [ $valgrind | \@valgrind ], min_version => $min_version, no_def_supp => $no_def_supp, extra_supps => \@extra_supps >>
45 The package constructor, which takes several options :
51 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
53 Defaults to the current C<PATH> environment variable.
57 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
58 C<@search_dirs> will then be ignored.
60 If an array refernce C<\@valgrind> is passed, its values will be I<prepended> to the list of the candidates resulting from C<@search_dirs>.
64 C<$min_version> specifies the minimal C<valgrind> version required.
65 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
71 If C<$no_def_supp> is false, C<valgrind> won't read the default suppression file associated with the tool and the command.
77 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
87 $class = ref($class) || $class;
92 my $vg = delete $args{vg};
93 if (defined $vg and not ref $vg) {
96 push @paths, @$vg if $vg and ref $vg eq 'ARRAY';
97 my $dirs = delete $args{search_dirs};
98 $dirs = [ File::Spec->path ] unless $dirs;
99 push @paths, map File::Spec->catfile($_, 'valgrind'), @$dirs
100 if ref $dirs eq 'ARRAY';
102 $class->_croak('Empty valgrind candidates list') unless @paths;
104 my $min_version = delete $args{min_version};
105 defined and not ref and $_ = version->new($_) for $min_version;
107 my ($valgrind, $version);
110 my $ver = qx/$_ --version/;
111 if ($ver =~ /^valgrind-(\d+(\.\d+)*)/) {
112 $version = version->new($1);
113 next if $min_version and $version < $min_version;
118 $class->_croak('No appropriate valgrind executable could be found')
119 unless defined $valgrind;
121 my $extra_supps = delete $args{extra_supps};
122 $extra_supps = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
123 @$extra_supps = grep { defined && -f $_ && -r _ } @$extra_supps;
126 valgrind => $valgrind,
128 no_def_supp => delete($args{no_def_supp}),
129 extra_supps => $extra_supps,
135 The path to the selected C<valgrind> executable.
139 The L<version> object associated to the selected C<valgrind>.
141 =head2 C<no_def_supp>
143 Read-only accessor for the C<no_def_supp> option.
147 eval "sub $_ { \$_[0]->{$_} }" for qw/valgrind version no_def_supp/;
149 =head2 C<extra_supps>
151 Read-only accessor for the C<extra_supps> option.
155 sub extra_supps { @{$_[0]->{extra_supps} || []} }
157 =head2 C<< run action => $action, tool => $tool, command => $command >>
159 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
167 my $guard = bless sub { $self->finish } => 'Test::Valgrind::Session::Guard';
169 $self->report(Test::Valgrind::Report->new_diag(
170 'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
173 my $env = $self->command->env($self);
176 if ($self->do_suppressions) {
177 push @supp_args, '--gen-suppressions=all';
180 if (not $self->no_def_supp) {
181 my $def_supp = $self->def_supp_file;
182 if (defined $def_supp and not -e $def_supp) {
183 $self->report(Test::Valgrind::Report->new_diag("Generating suppressions..."));
184 require Test::Valgrind::Suppressions;
185 Test::Valgrind::Suppressions->generate(
187 command => $self->command,
190 $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
191 $self->report(Test::Valgrind::Report->new_diag("Suppressions for this perl stored in $def_supp"));
194 push @supp_args, '--suppressions=' . $_ for $self->suppressions;
197 pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
199 my $oldfh = select $vrdr;
205 $self->_croak("fork(): $!") unless defined $pid;
209 close $vrdr or $self->_croak("close(\$vrdr): $!");
210 fcntl $vwtr, Fcntl::F_SETFD(), 0
211 or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
215 '--log-fd=' . fileno($vwtr),
216 $self->tool->args($self),
218 $self->command->args($self),
221 # $self->report(Test::Valgrind::Report->new_diag("@args"));
223 exec { $args[0] } @args or $self->_croak("exec @args: $!");
226 local $SIG{INT} = sub {
227 kill -(POSIX::SIGKILL()) => $pid;
232 close $vwtr or $self->_croak("close(\$vwtr): $!");
234 $self->tool->parse($self, $vrdr);
236 $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
238 close $vrdr or $self->_croak("close(\$vrdr): $!");
243 sub Test::Valgrind::Session::Guard::DESTROY { $_[0]->() }
247 Read-only accessor for the C<action> associated to the current run.
251 Read-only accessor for the C<tool> associated to the current run.
255 Read-only accessor for the C<command> associated to the current run.
261 @members = qw/action tool command/;
263 eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
268 =head2 C<do_suppressions>
270 Forwards to C<< ->action->do_suppressions >>.
274 sub do_suppressions { $_[0]->action->do_suppressions }
276 =head2 C<report_class>
278 Calls C<< ->action->report_class >> with the current session object as the sole argument.
282 sub report_class { $_[0]->tool->report_class($_[0]) }
284 =head2 C<def_supp_file>
286 Returns an absolute path to the default suppression file associated to the current session.
287 C<undef> will be returned as soon as any of C<< ->tool->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
288 Otherwise, the file part of the name is builded by joining those two together, and the directory part is roughly F<< File::HomeDir->my_home / .perl / Test-Valgrind / suppressions / $VERSION >>.
295 my $tool_tag = $self->tool->suppressions_tag($self);
296 return unless defined $tool_tag;
298 my $cmd_tag = $self->command->suppressions_tag($self);
299 return unless defined $cmd_tag;
301 require File::HomeDir; # So that it's not needed at configure time.
303 return File::Spec->catfile(
304 File::HomeDir->my_home,
309 "$tool_tag-$cmd_tag.supp",
313 =head2 C<suppressions>
315 Returns the list of all the suppressions that will be passed to C<valgrind>.
316 Honors L</no_def_supp> and L</extra_supps>.
324 unless ($self->no_def_supp) {
325 my $def_supp = $self->def_supp_file;
326 push @supps, $def_supp if defined $def_supp;
328 push @supps, $self->extra_supps;
335 Starts the action and tool associated to the current run.
336 It's automatically called at the beginning of L</run>.
346 my $base = 'Test::Valgrind::' . ucfirst;
347 my $value = $args{$_};
348 $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
349 and $value->isa($base);
353 delete @{$self}{qw/last_status exit_code/};
355 $self->tool->start($self);
356 $self->action->start($self);
363 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
369 $self->action->abort($self, @_);
372 =head2 C<report $report>
374 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
380 $self->action->report($self, @_);
385 Finishes the action and tool associated to the current run.
386 It's automatically called at the end of L</run>.
393 my $action = $self->action;
394 $action->finish($self);
395 $self->tool->finish($self);
397 my $status = $action->status($self);
398 $self->{last_status} = defined $status ? $status : $self->{exit_code};
400 $self->$_(undef) for @members;
407 Returns the status code of the last run of the session.
411 sub status { $_[0]->{last_status} }
415 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Command>.
417 L<version>, L<File::HomeDir>.
421 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
423 You can contact me by mail or on C<irc.perl.org> (vincent).
427 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>.
428 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
432 You can find documentation for this module with the perldoc command.
434 perldoc Test::Valgrind::Session
436 =head1 COPYRIGHT & LICENSE
438 Copyright 2009 Vincent Pit, all rights reserved.
440 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
444 1; # End of Test::Valgrind::Session