1 package Test::Valgrind::Session;
8 Test::Valgrind::Session - Test::Valgrind session object.
16 our $VERSION = '1.02';
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
35 use base qw/Test::Valgrind::Carp/;
37 use Test::Valgrind::Report;
41 =head2 C<< new search_dirs => \@search_dirs, valgrind => [ $valgrind | \@valgrind ], min_version => $min_version, no_def_supp => $no_def_supp, extra_supps => \@extra_supps >>
43 The package constructor, which takes several options :
49 All the directories from C<@search_dirs> will have F<valgrind> appended to create a list of candidates for the C<valgrind> executable.
51 Defaults to the current C<PATH> environment variable.
55 If a simple scalar C<$valgrind> is passed as the value to C<'valgrind'>, it will be the only candidate.
56 C<@search_dirs> will then be ignored.
58 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>.
62 C<$min_version> specifies the minimal C<valgrind> version required.
63 The constructor will croak if it's not able to find an adequate C<valgrind> from the supplied candidates list and search path.
69 If C<$no_def_supp> is false, C<valgrind> won't read the default suppression file associated with the tool and the command.
75 C<$extra_supps> is a reference to an array of optional suppression files that will be passed to C<valgrind>.
83 my $build_version = sub {
90 $class = ref($class) || $class;
95 my $vg = delete $args{vg};
96 if (defined $vg and not ref $vg) {
99 push @paths, @$vg if $vg and ref $vg eq 'ARRAY';
100 my $dirs = delete $args{search_dirs};
101 $dirs = [ File::Spec->path ] unless $dirs;
102 push @paths, map File::Spec->catfile($_, 'valgrind'), @$dirs
103 if ref $dirs eq 'ARRAY';
105 $class->_croak('Empty valgrind candidates list') unless @paths;
107 my $min_version = delete $args{min_version};
108 defined and not ref and $_ = $build_version->($_) for $min_version;
110 my ($valgrind, $version);
113 my $ver = qx/$_ --version/;
114 if ($ver =~ /^valgrind-(\d+(\.\d+)*)/) {
116 $version = $build_version->($1);
117 next if $version < $min_version;
125 $class->_croak('No appropriate valgrind executable could be found')
126 unless defined $valgrind;
128 my $extra_supps = delete $args{extra_supps};
129 $extra_supps = [ ] unless $extra_supps and ref $extra_supps eq 'ARRAY';
130 @$extra_supps = grep { defined && -f $_ && -r _ } @$extra_supps;
133 valgrind => $valgrind,
135 no_def_supp => delete($args{no_def_supp}),
136 extra_supps => $extra_supps,
142 The path to the selected C<valgrind> executable.
146 The L<version> object associated to the selected C<valgrind>.
153 my $version = $self->{version};
154 $self->{version} = $version = $build_version->($version) unless ref $version;
159 =head2 C<no_def_supp>
161 Read-only accessor for the C<no_def_supp> option.
165 eval "sub $_ { \$_[0]->{$_} }" for qw/valgrind no_def_supp/;
167 =head2 C<extra_supps>
169 Read-only accessor for the C<extra_supps> option.
173 sub extra_supps { @{$_[0]->{extra_supps} || []} }
175 =head2 C<< run action => $action, tool => $tool, command => $command >>
177 Runs the command C<$command> through C<valgrind> with the tool C<$tool>, which will report to the action C<$action>.
179 If the command is a L<Test::Valgrind::Command::Aggregate> object, the action and the tool will be initialized once before running all the aggregated commands.
189 my $guard = bless sub { $self->finish } => 'Test::Valgrind::Session::Guard';
191 $self->_run($args{command});
195 my ($self, $cmd) = @_;
197 if ($cmd->isa('Test::Valgrind::Command::Aggregate')) {
198 $self->_run($_) for $cmd->commands;
202 $self->command($cmd);
204 $self->report(Test::Valgrind::Report->new_diag(
205 'Using valgrind ' . $self->version . ' located at ' . $self->valgrind
208 my $env = $self->command->env($self);
211 if ($self->do_suppressions) {
212 push @supp_args, '--gen-suppressions=all';
213 } elsif (not $self->no_def_supp) {
214 my $def_supp = $self->def_supp_file;
215 if (defined $def_supp and not -e $def_supp) {
216 $self->report(Test::Valgrind::Report->new_diag(
217 "Generating suppressions..."
219 require Test::Valgrind::Suppressions;
220 Test::Valgrind::Suppressions->generate(
222 command => $self->command,
225 $self->_croak('Couldn\'t generate suppressions') unless -e $def_supp;
226 $self->report(Test::Valgrind::Report->new_diag(
227 "Suppressions for this perl stored in $def_supp"
230 push @supp_args, '--suppressions=' . $_ for $self->suppressions;
233 pipe my $vrdr, my $vwtr or $self->_croak("pipe(\$vrdr, \$vwtr): $!");
235 my $oldfh = select $vrdr;
241 $self->_croak("fork(): $!") unless defined $pid;
245 close $vrdr or $self->_croak("close(\$vrdr): $!");
246 fcntl $vwtr, Fcntl::F_SETFD(), 0
247 or $self->_croak("fcntl(\$vwtr, F_SETFD, 0): $!");
251 '--log-fd=' . fileno($vwtr),
252 $self->tool->args($self),
254 $self->command->args($self),
257 # $self->report(Test::Valgrind::Report->new_diag("@args"));
259 exec { $args[0] } @args or $self->_croak("exec @args: $!");
262 local $SIG{INT} = sub {
263 kill -(POSIX::SIGKILL()) => $pid;
268 close $vwtr or $self->_croak("close(\$vwtr): $!");
270 $self->tool->parse($self, $vrdr);
272 $self->{exit_code} = (waitpid($pid, 0) == $pid) ? $? >> 8 : 255;
274 close $vrdr or $self->_croak("close(\$vrdr): $!");
279 sub Test::Valgrind::Session::Guard::DESTROY { $_[0]->() }
283 Read-only accessor for the C<action> associated to the current run.
287 Read-only accessor for the C<tool> associated to the current run.
291 Read-only accessor for the C<command> associated to the current run.
297 @members = qw/action tool command/;
299 eval "sub $_ { \@_ <= 1 ? \$_[0]->{$_} : (\$_[0]->{$_} = \$_[1]) }";
304 =head2 C<do_suppressions>
306 Forwards to C<< ->action->do_suppressions >>.
310 sub do_suppressions { $_[0]->action->do_suppressions }
312 =head2 C<report_class>
314 Calls C<< ->action->report_class >> with the current session object as the unique argument.
318 sub report_class { $_[0]->tool->report_class($_[0]) }
320 =head2 C<def_supp_file>
322 Returns an absolute path to the default suppression file associated to the current session.
324 C<undef> will be returned as soon as any of C<< ->command->suppressions_tag >> or C<< ->tool->suppressions_tag >> are also C<undef>.
325 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 >>.
332 my $tool_tag = $self->tool->suppressions_tag($self);
333 return unless defined $tool_tag;
335 my $cmd_tag = $self->command->suppressions_tag($self);
336 return unless defined $cmd_tag;
338 require File::HomeDir; # So that it's not needed at configure time.
340 return File::Spec->catfile(
341 File::HomeDir->my_home,
346 "$tool_tag-$cmd_tag.supp",
350 =head2 C<suppressions>
352 Returns the list of all the suppressions that will be passed to C<valgrind>.
353 Honors L</no_def_supp> and L</extra_supps>.
361 unless ($self->no_def_supp) {
362 my $def_supp = $self->def_supp_file;
363 push @supps, $def_supp if defined $def_supp;
365 push @supps, $self->extra_supps;
372 Starts the action and tool associated to the current run.
373 It's automatically called at the beginning of L</run>.
383 my $base = 'Test::Valgrind::' . ucfirst;
384 my $value = $args{$_};
385 $self->_croak("Invalid $_") unless Scalar::Util::blessed($value)
386 and $value->isa($base);
390 delete @{$self}{qw/last_status exit_code/};
392 $self->tool->start($self);
393 $self->action->start($self);
400 Forwards to C<< ->action->abort >> after unshifting the session object to the argument list.
406 $self->action->abort($self, @_);
409 =head2 C<report $report>
411 Forwards to C<< ->action->report >> after unshifting the session object to the argument list.
416 my ($self, $report) = @_;
418 return unless defined $report;
420 $report = $self->command->filter($self, $report);
421 return unless defined $report;
423 $self->action->report($self, $report);
428 Finishes the action and tool associated to the current run.
429 It's automatically called at the end of L</run>.
436 my $action = $self->action;
437 $action->finish($self);
438 $self->tool->finish($self);
440 my $status = $action->status($self);
441 $self->{last_status} = defined $status ? $status : $self->{exit_code};
443 $self->$_(undef) for @members;
450 Returns the status code of the last run of the session.
454 sub status { $_[0]->{last_status} }
458 L<Test::Valgrind>, L<Test::Valgrind::Action>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Command>.
460 L<version>, L<File::HomeDir>.
464 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
466 You can contact me by mail or on C<irc.perl.org> (vincent).
470 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>.
471 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
475 You can find documentation for this module with the perldoc command.
477 perldoc Test::Valgrind::Session
479 =head1 COPYRIGHT & LICENSE
481 Copyright 2009 Vincent Pit, all rights reserved.
483 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
487 1; # End of Test::Valgrind::Session